Skip to content

Conversation

@lucylq
Copy link
Contributor

@lucylq lucylq commented Feb 10, 2025

Summary

Add flat tensor to cmake

Test plan

PYTHON_EXECUTABLE=python EXECUTORCH_BUILD_PYBIND=ON CMAKE_ARGS="-DEXECUTORCH_BUILD_XNNPACK=ON -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON" .ci/scripts/setup-linux.sh cmake

sh test/run_oss_cpp_tests.sh extension/flat_tensor/test

lucylq added 30 commits December 4, 2024 22:18
To indicate if a tensor is external to the PTE file or not.

Currently, we can also use the existence of 'fqn' to determine if a tensor is external or not.
I think it's better to have a specific location field as fqn may be required for cases besides external tensor storage.

Differential Revision: [D66523171](https://our.internmc.facebook.com/intern/diff/D66523171/)

[ghstack-poisoned]
Emit FQNs for external tensors. In the emitter, store external tensors as:
```
// list of unique tensors
external_constants_buffer: List[bytes]

// map of {constant_tag: {fqn: index into external_constant_buffer}}
// constant_tag: may want to save multiple external constant files; group them together via the tag.
// {fqn: index}; there may be multiple fqns pointing to the same data buffer. This is for deduplication.
external_constants_map: [Dict[str, Dict[str, int]]
```

Differential Revision: [D66523226](https://our.internmc.facebook.com/intern/diff/D66523226/)

[ghstack-poisoned]
…l file

- Add config 'external_constants' to ExecutorchBackendConfig.
- When set to True, run the 'external_constants_pass'
- This tags all constants as external, and moves them into a separate buffer to be serialized outside of the PTE file.

Note: users can write their own passes to tag weights to specific files / multiple files.

TODO: write example pass and test for the case where we have two constant files.

Differential Revision: [D66560903](https://our.internmc.facebook.com/intern/diff/D66560903/)

[ghstack-poisoned]
Introduce data serialization interface.

Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/)

[ghstack-poisoned]
…tion interface"

Introduce data serialization interface.

Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/)

[ghstack-poisoned]
Introduce data serialization interface.

Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/)

[ghstack-poisoned]
For usage in extension/flat_tensor/serialize.

Differential Revision: [D66854756](https://our.internmc.facebook.com/intern/diff/D66854756/)

[ghstack-poisoned]
1. Move schema files under flat_tensor/serialize
2. Add targets to generate schema files

Differential Revision: [D66903492](https://our.internmc.facebook.com/intern/diff/D66903492/)

[ghstack-poisoned]
Serialize a flat tensor file. The resulting file looks like:

Header with
- flatbuffer offset and size
- segment data offset and size

Flatbuffer
Tensor data (in segment)

Differential Revision: [D66374253](https://our.internmc.facebook.com/intern/diff/D66374253/)

[ghstack-poisoned]
More comprehensive testing for flat tensor serialization.

Differential Revision: [D67007821](https://our.internmc.facebook.com/intern/diff/D67007821/)

[ghstack-poisoned]
Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
…files."

Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
…files."

Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
…files."

Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
…files."

Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
Introduce top-level serialization file that calls:
- serialize_pte_binary for PTE file
- FlatTensor.serialize_tensors for PTD files.

Differential Revision: [D66523267](https://our.internmc.facebook.com/intern/diff/D66523267/)

[ghstack-poisoned]
Introduce TensorLayout class, used to describe external tensors. Currently contains:
- scalar_type
- sizes
- dim_order

Differential Revision: [D67048723](https://our.internmc.facebook.com/intern/diff/D67048723/)

[ghstack-poisoned]
Add NamedDataMap interface to runtime.

Differential Revision: [D66834552](https://our.internmc.facebook.com/intern/diff/D66834552/)

[ghstack-poisoned]
Add NamedDataMap interface to runtime.

Differential Revision: [D66834552](https://our.internmc.facebook.com/intern/diff/D66834552/)

[ghstack-poisoned]
Add NamedDataMap interface to runtime.

Differential Revision: [D66834552](https://our.internmc.facebook.com/intern/diff/D66834552/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
…tation"

DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
…tation"

DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
…tation"

DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
lucylq added 10 commits January 24, 2025 11:26
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
…tation"

DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
…tation"

DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
DataMap implementation that
* Loads a flat_tensor file
* Populates a map with {fqn: tensor} and {fqn: TensorLayout}.
* Makes tensor information available via the named_data_map.h interface.

For now, DataMap doesn't store the DataLoader.
- If/when tensors are in their own segments, DataMap should also store a DataLoader.

Differential Revision: [D67064580](https://our.internmc.facebook.com/intern/diff/D67064580/)

[ghstack-poisoned]
Add NamedDataMap as an arg to:
- Method
- load_method
- parseTensor

Use NamedDataMap to resolve external tensors in parseTensor.

Test that the PTE + PTD file run well inside method_test.

Differential Revision: [D67127327](https://our.internmc.facebook.com/intern/diff/D67127327/)

[ghstack-poisoned]
…method load"

Add NamedDataMap as an arg to:
- Method
- load_method
- parseTensor

Use NamedDataMap to resolve external tensors in parseTensor.

Test that the PTE + PTD file run well inside method_test.

Differential Revision: [D67127327](https://our.internmc.facebook.com/intern/diff/D67127327/)

[ghstack-poisoned]
Add NamedDataMap as an arg to:
- Method
- load_method
- parseTensor

Use NamedDataMap to resolve external tensors in parseTensor.

Test that the PTE + PTD file run well inside method_test.

Differential Revision: [D67127327](https://our.internmc.facebook.com/intern/diff/D67127327/)

[ghstack-poisoned]
…method load"

Add NamedDataMap as an arg to:
- Method
- load_method
- parseTensor

Use NamedDataMap to resolve external tensors in parseTensor.

Test that the PTE + PTD file run well inside method_test.

Differential Revision: [D67127327](https://our.internmc.facebook.com/intern/diff/D67127327/)

[ghstack-poisoned]
Add NamedDataMap as an arg to:
- Method
- load_method
- parseTensor

Use NamedDataMap to resolve external tensors in parseTensor.

Test that the PTE + PTD file run well inside method_test.

Differential Revision: [D67127327](https://our.internmc.facebook.com/intern/diff/D67127327/)

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Feb 10, 2025

🔗 Helpful Links

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

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

❌ 1 New Failure, 2 Cancelled Jobs

As of commit 03569f7 with merge base 8ee637e (image):

NEW FAILURE - The following job has failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 10, 2025
@github-actions
Copy link

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@lucylq lucylq force-pushed the lfq.flat-tensor branch 4 times, most recently from d62d1b4 to 17fe113 Compare February 10, 2025 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants