报错 RuntimeError: Only consecutive 1-d tensor indices are supported in exporting aten::index_put to O

张开发
2026/4/19 18:30:11 15 分钟阅读

分享文章

报错 RuntimeError: Only consecutive 1-d tensor indices are supported in exporting aten::index_put to O
多个轴索引存在多个数值需要满足【:】所在轴的数值在内存中是连续的也就是【:】只能出现在最后的dimension不能出现在前面先放到最后然后用permute函数错误的方式1x[self.c1[:, 0], :, self.c1[:, 1], self.c1[:, 3], self.c1[:, 2]] self.value错误的方式1x[self.c1[:, 0], self.c1[:, 1], self.c1[:, 3], :, self.c1[:, 2]] self.value正确的方式importonnximporttorchimportonnxsimfromtorchimportnnfromtorch.nnimportModulefromonnxsimimportsimplifyclassnets(Module):def__init__(self):super(nets,self).__init__()self.c1torch.tensor([[0,0,1,0],[1,2,2,1]],dtypetorch.int64)self.valuetorch.randn((2,3),dtypetorch.float32)defforward(self,x):x[self.c1[:,0],self.c1[:,1],self.c1[:,3],self.c1[:,2],:]self.valuereturnxif__name____main__:path1r/projects/Fisheye3D/tmp1.onnxnetworknets()xtorch.ones((3,3,3,3,3),dtypetorch.float32)cnt0forhinrange(3):foriinrange(3):forjinrange(3):forkinrange(3):forwinrange(3):x[h,i,j,k,w]cnt cnt1network.eval()outputnetwork(x)# [0, 0, 0, 0]# [0, 1, 0, 0]# [0, 2, 0, 0]# [2, 0, 2, 2]# [2, 1, 2, 2]# [2, 2, 2, 2]input_names[k1]output_names[o1]dynamic_axes{1:{0:batch_size,1:num,2:kk},o1:{0:number}}withtorch.no_grad():torch.onnx.export(network,x,path1,verboseFalse,opset_version16,do_constant_foldingTrue,# WARNING: DNN inference with torch1.12 may require do_constant_foldingFalsekeep_initializers_as_inputsTrue,# dynamic_axes dynamic_axes,# input_names input_names,# output_names output_names,)# grid_sampler argsortonnx_modelonnx.load(path1)# convert modelmodel_simp,checksimplify(onnx_model)assertcheck,Simplified ONNX model could not be validatedonnx.checker.check_model(onnx_model)onnx.save(model_simp,path1)

更多文章