@@ -884,6 +884,46 @@ std::string_view check_node(
884884 return {};
885885}
886886
887+ using Sizes = std::vector<aten::SizesType>;
888+
889+ struct LinearViews {
890+ std::array<Sizes, 3 > inputs;
891+ Sizes output;
892+ };
893+
894+ // aten::linear.out requires an input of rank >= 2, a 2D weight and a 1D bias.
895+ std::optional<LinearViews> linear_views (
896+ const Kernel& node,
897+ const ptn::Graph& graph) {
898+ if (node.target != " torch.ops.aten.linear.default" ||
899+ node.inputs .size () < 2 ||
900+ node.inputs [0 ].arg .kind () != ptn::ArgKind::Tensor ||
901+ node.inputs [1 ].arg .kind () != ptn::ArgKind::Tensor) {
902+ return std::nullopt ;
903+ }
904+ const auto & input =
905+ graph.value (node.inputs [0 ].arg .as_tensor ().id ).tensor_meta ().sizes ;
906+ const auto & weight =
907+ graph.value (node.inputs [1 ].arg .as_tensor ().id ).tensor_meta ().sizes ;
908+ if (input.empty () || (input.size () >= 2 && weight.size () == 2 )) {
909+ return std::nullopt ;
910+ }
911+ const auto row = [](const auto & sizes) {
912+ return sizes.size () == 1 ? Sizes{1 , static_cast <aten::SizesType>(sizes[0 ])}
913+ : Sizes (sizes.begin (), sizes.end ());
914+ };
915+ LinearViews views{{row (input), row (weight), {}}, {}};
916+ if (node.inputs .size () > 2 &&
917+ node.inputs [2 ].arg .kind () == ptn::ArgKind::Tensor) {
918+ const auto & bias =
919+ graph.value (node.inputs [2 ].arg .as_tensor ().id ).tensor_meta ().sizes ;
920+ views.inputs [2 ] = bias.empty () ? Sizes{1 } : Sizes (bias.begin (), bias.end ());
921+ }
922+ views.output .assign (views.inputs [0 ].begin (), views.inputs [0 ].end () - 1 );
923+ views.output .push_back (views.inputs [1 ][0 ]);
924+ return views;
925+ }
926+
887927struct Instruction {
888928 const ptn::Node* node = nullptr ;
889929 OpFunction kernel = nullptr ;
@@ -908,9 +948,16 @@ class ReferenceExecutable final : public Executable {
908948 };
909949
910950 Result<EValue*> tensor_value (ptn::ValueId id, bool writable) {
951+ const auto & shape = context_.graph .value (id).tensor_meta ().sizes ;
952+ return tensor_value (id, writable, Sizes (shape.begin (), shape.end ()));
953+ }
954+
955+ // Views of one value share its buffer but keep separate tensor metadata.
956+ Result<EValue*>
957+ tensor_value (ptn::ValueId id, bool writable, const Sizes& sizes) {
911958 const auto existing = std::find_if (
912- tensors_.begin (), tensors_.end (), [id](const auto & storage) {
913- return storage->id == id;
959+ tensors_.begin (), tensors_.end (), [id, &sizes ](const auto & storage) {
960+ return storage->id == id && storage-> sizes == sizes ;
914961 });
915962 if (existing != tensors_.end ()) {
916963 (*existing)->writable |= writable;
@@ -921,23 +968,22 @@ class ReferenceExecutable final : public Executable {
921968 return bytes.error ();
922969 }
923970 const auto & meta = context_.graph .value (id).tensor_meta ();
924- const auto & shape = meta.sizes ;
925971 auto storage = std::make_unique<TensorStorage>();
926972 storage->id = id;
927973 storage->writable = writable;
928- storage->sizes . assign (shape. begin (), shape. end ()) ;
929- storage->order .resize (shape .size ());
930- storage->strides .resize (shape .size ());
974+ storage->sizes = sizes ;
975+ storage->order .resize (sizes .size ());
976+ storage->strides .resize (sizes .size ());
931977 int64_t stride = 1 ;
932- for (size_t reverse = shape .size (); reverse > 0 ; --reverse) {
978+ for (size_t reverse = sizes .size (); reverse > 0 ; --reverse) {
933979 const auto dim = reverse - 1 ;
934980 storage->order [dim] = dim;
935981 storage->strides [dim] = static_cast <aten::StridesType>(stride);
936- stride *= shape [dim];
982+ stride *= sizes [dim];
937983 }
938984 storage->tensor = std::make_unique<aten::TensorImpl>(
939985 to_aten (meta.dtype ),
940- shape .size (),
986+ sizes .size (),
941987 storage->sizes .data (),
942988 nullptr ,
943989 storage->order .data (),
@@ -1036,6 +1082,7 @@ class ReferenceExecutable final : public Executable {
10361082 " Empty negative view copy requires matching input/output metadata" );
10371083 continue ;
10381084 }
1085+ const auto views = linear_views (node, context_.graph );
10391086 auto instruction = std::make_unique<Instruction>();
10401087 instruction->node = &node;
10411088 instruction->kernel = route->kernel ;
@@ -1059,7 +1106,9 @@ class ReferenceExecutable final : public Executable {
10591106 const auto & input = node.inputs [index];
10601107 if (input.arg .kind () == ptn::ArgKind::Tensor) {
10611108 const auto value_id = input.arg .as_tensor ().id ;
1062- auto value = tensor_value (value_id, false );
1109+ auto value = views && index < views->inputs .size ()
1110+ ? tensor_value (value_id, false , views->inputs [index])
1111+ : tensor_value (value_id, false );
10631112 if (!value.ok ()) {
10641113 return value.error ();
10651114 }
@@ -1114,7 +1163,8 @@ class ReferenceExecutable final : public Executable {
11141163 InvalidProgram,
11151164 " Reference provider requires tensor outputs: %s" ,
11161165 node.target .c_str ());
1117- auto value = tensor_value (output.value_id , true );
1166+ auto value = views ? tensor_value (output.value_id , true , views->output )
1167+ : tensor_value (output.value_id , true );
11181168 if (!value.ok ()) {
11191169 return value.error ();
11201170 }
0 commit comments