Skip to content

[nativert] Move TensorMeta to pytorch core#152475

Closed
yiming0416 wants to merge 1 commit intopytorch:mainfrom
yiming0416:export-D73820548
Closed

[nativert] Move TensorMeta to pytorch core#152475
yiming0416 wants to merge 1 commit intopytorch:mainfrom
yiming0416:export-D73820548

Conversation

@yiming0416
Copy link
Contributor

@yiming0416 yiming0416 commented Apr 29, 2025

Summary:
Torch Native Runtime RFC: pytorch/rfcs#72

This diff moves TensorMeta.cpp and TensorMeta.h to PyTorch core under torch/nativert/graph/

Existing torch::_export::TensorMeta in torch/csrc/utils/generated_serialization_types.h is auto-generated from the export serde schema and therefore only containing the most basic serializable types. We need the newly added TensorMeta.cpp to deserialize the metadata into a in-memory class with c10 types so that it can be consumed by the runtime later.

Test Plan:

Added test under test/cpp/nativert/test_tensor_meta.cpp

Differential Revision: D73820548

@pytorch-bot
Copy link

pytorch-bot bot commented Apr 29, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/152475

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (1 Unrelated Failure)

As of commit 004d31a with merge base 99dac70 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@yiming0416 yiming0416 marked this pull request as draft April 29, 2025 21:53
@yiming0416 yiming0416 added the topic: not user facing topic category label Apr 29, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@yiming0416 yiming0416 marked this pull request as ready for review April 30, 2025 16:49
@yiming0416 yiming0416 added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 30, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

yiming0416 added a commit to yiming0416/pytorch that referenced this pull request Apr 30, 2025
Summary:
Pull Request resolved: pytorch#152475

Torch Native Runtime RFC: pytorch/rfcs#72

Thi diff moves `TensorMeta.cpp` and `TensorMeta.h` to PyTorch core.

Test Plan:
```
buck2 run mode/dev-nosan caffe2/test/cpp/nativert:nativert
```

Differential Revision: D73820548
@albanD
Copy link
Collaborator

albanD commented Apr 30, 2025

Can you add some details in the description on how this is different from export::TensorMeta and why we need this separate one?

)

# We also build with UBSAN flag in build_asan.h
if(USE_ASAN)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually run this under asan?
Or is this a CMake from somewhere else?

Copy link
Contributor Author

@yiming0416 yiming0416 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from test/cpp/jit/CMakeLists.txt. I guess we can remove the USE_ASAN branch if not needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes let's remove everything we don't need from this cmake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albanD sure! removed in the latest version.

)

libtorch_cmake_sources = libtorch_core_sources + libtorch_core_jit_sources
libtorch_cmake_sources = libtorch_core_sources + libtorch_core_jit_sources + libtorch_nativert_sources
Copy link
Collaborator

@albanD albanD Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the runtime a separate .so like other subsets of libtorch to improve dependency management?
In particular, do you expect you users to only link against your function (and transitively get the rest of libtorch). Or you expect them to use whole of libtorch?

cc @malfet

Copy link
Contributor Author

@yiming0416 yiming0416 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we expect the users to use whole of libtorch cc @zhxchen17

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we expect users to use the whole of libtorch.

@yiming0416 yiming0416 requested a review from albanD May 1, 2025 15:45
@yiming0416
Copy link
Contributor Author

Can you add some details in the description on how this is different from export::TensorMeta and why we need this separate one?

@albanD Added details in the PR summary

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

yiming0416 added a commit to yiming0416/pytorch that referenced this pull request May 2, 2025
Summary:
Pull Request resolved: pytorch#152475

Torch Native Runtime RFC: pytorch/rfcs#72

Thi diff moves `TensorMeta.cpp` and `TensorMeta.h` to PyTorch core.

Test Plan:
Internal CI.
GitHub CI with newly added test under `test/cpp/nativert/test_tensor_meta.cpp`

Differential Revision: D73820548
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

yiming0416 added a commit to yiming0416/pytorch that referenced this pull request May 2, 2025
Summary:
Pull Request resolved: pytorch#152475

Torch Native Runtime RFC: pytorch/rfcs#72

Thi diff moves `TensorMeta.cpp` and `TensorMeta.h` to PyTorch core.

Test Plan:
Internal CI.
GitHub CI with newly added test under `test/cpp/nativert/test_tensor_meta.cpp`

Differential Revision: D73820548
@yiming0416 yiming0416 requested a review from albanD May 2, 2025 20:00
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

yiming0416 added a commit to yiming0416/pytorch that referenced this pull request May 3, 2025
Summary:
Pull Request resolved: pytorch#152475

Torch Native Runtime RFC: pytorch/rfcs#72

Thi diff moves `TensorMeta.cpp` and `TensorMeta.h` to PyTorch core.

Test Plan:
Internal CI.
GitHub CI with newly added test under `test/cpp/nativert/test_tensor_meta.cpp`

Differential Revision: D73820548
@yiming0416
Copy link
Contributor Author

@albanD Can you pls review again? I have made APIs private and added the corresponding source file to the test cmake file. If there is any other suggesting changes before merging, I'd be happy to make those. Thanks!

Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds ok!

Comment on lines +7 to +98
c10::ScalarType convertJsonScalarType(
const torch::_export::ScalarType& scalarType) {
switch (scalarType) {
case torch::_export::ScalarType::UNKNOWN:
TORCH_CHECK(false, "scalar type is not properly set");
case torch::_export::ScalarType::BYTE:
return c10::ScalarType::Byte;
case torch::_export::ScalarType::CHAR:
return c10::ScalarType::Char;
case torch::_export::ScalarType::SHORT:
return c10::ScalarType::Short;
case torch::_export::ScalarType::INT:
return c10::ScalarType::Int;
case torch::_export::ScalarType::LONG:
return c10::ScalarType::Long;
case torch::_export::ScalarType::HALF:
return c10::ScalarType::Half;
case torch::_export::ScalarType::FLOAT:
return c10::ScalarType::Float;
case torch::_export::ScalarType::DOUBLE:
return c10::ScalarType::Double;
case torch::_export::ScalarType::COMPLEXHALF:
return c10::ScalarType::ComplexHalf;
case torch::_export::ScalarType::COMPLEXFLOAT:
return c10::ScalarType::ComplexFloat;
case torch::_export::ScalarType::COMPLEXDOUBLE:
return c10::ScalarType::ComplexDouble;
case torch::_export::ScalarType::BOOL:
return c10::ScalarType::Bool;
case torch::_export::ScalarType::BFLOAT16:
return c10::ScalarType::BFloat16;
case torch::_export::ScalarType::UINT16:
return c10::ScalarType::UInt16;
case torch::_export::ScalarType::FLOAT8E4M3FN:
return c10::ScalarType::Float8_e4m3fn;
case torch::_export::ScalarType::FLOAT8E5M2:
return c10::ScalarType::Float8_e5m2;
default:
TORCH_CHECK(false, "unknown scalar type", static_cast<int>(scalarType));
}
}

c10::MemoryFormat convertJsonMemoryFormat(
const torch::_export::MemoryFormat& memoryFormat) {
switch (memoryFormat) {
case torch::_export::MemoryFormat::Unknown:
TORCH_CHECK(false, "got unknown scalar type");
case torch::_export::MemoryFormat::ContiguousFormat:
return c10::MemoryFormat::Contiguous;
case torch::_export::MemoryFormat::ChannelsLast:
return c10::MemoryFormat::ChannelsLast;
case torch::_export::MemoryFormat::ChannelsLast3d:
return c10::MemoryFormat::ChannelsLast3d;
case torch::_export::MemoryFormat::PreserveFormat:
return c10::MemoryFormat::Preserve;
default:
TORCH_CHECK(
false, "unknown memory format", static_cast<int>(memoryFormat));
}
}

c10::Layout convertJsonLayout(const torch::_export::Layout& layout) {
switch (layout) {
case torch::_export::Layout::Unknown:
TORCH_CHECK(false, "got unknown layout");
case torch::_export::Layout::SparseCoo:
// TODO is this the right translation
return c10::Layout::Sparse;
case torch::_export::Layout::SparseCsr:
return c10::Layout::SparseCsr;
case torch::_export::Layout::SparseCsc:
return c10::Layout::SparseCsc;
case torch::_export::Layout::SparseBsr:
return c10::Layout::SparseBsr;
case torch::_export::Layout::SparseBsc:
return c10::Layout::SparseBsc;
case torch::_export::Layout::_mkldnn:
return c10::Layout::Mkldnn;
case torch::_export::Layout::Strided:
return c10::Layout::Strided;
default:
TORCH_CHECK(false, "unknown layout", static_cast<int>(layout));
}
}

c10::Device convertJsonDevice(const torch::_export::Device& device) {
c10::Device d(device.get_type());
if (auto index = device.get_index()) {
d.set_index(*index);
}
return d;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, as long as we don't make them public from here, that sounds like a good tradeoff for now

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

Summary:
Pull Request resolved: pytorch#152475

Torch Native Runtime RFC: pytorch/rfcs#72

Thi diff moves `TensorMeta.cpp` and `TensorMeta.h` to PyTorch core.

Test Plan:
Internal CI.
GitHub CI with newly added test under `test/cpp/nativert/test_tensor_meta.cpp`

Reviewed By: zhxchen17

Differential Revision: D73820548
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D73820548

@facebook-github-bot
Copy link
Contributor

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request fb-exported Merged topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants