Skip to content

Conversation

SS-JIA
Copy link
Contributor

@SS-JIA SS-JIA commented Aug 18, 2025

Stack from ghstack (oldest at bottom):

Summary:
When exporting models to Vulkan backend, save constant tensors in the NamedDataMap instead of the constant data section of the delegate header.

Motivation

Prevent screen blackout (Llama 3.2 1B) / device crash (Llama 3.2 3B) when running Llama 3.2 models on Samsung Galaxy S24. This behaviour is related to high peak memory usage when loading the model. For more information, see the top diff/PR in the stack.

Context

This change is based on the equivalent change D70315207/#9153 in XNNPACK.

Test Plan:

Memory Comparison with/without NamedDataMap

Measured VmRss using

uint64_t getVmRssInKB() {
  std::ifstream statusFile("/proc/self/status");
  std::string l, num;
  while (std::getline(statusFile, l)) {
    if (l.substr(0, 5) == "VmRSS") {
      size_t pos = l.find_first_of("0123456789");
      num = l.substr(pos);
      break;
    }
  }
  uint64_t vmRssInKB = std::stoi(num);
  return vmRssInKB;
}

P1908019767 (Meta only)

Excerpt:

Log 1                                             | Log 2
--------------------------------------------------|--------------------------------------------------
Memory usage before model compilation: 1115416 KB | Memory usage before model compilation: 1919228 KB
Memory usage after graph building: 1924340 KB     | Memory usage after graph building: 1924256 KB
Memory usage after graph preparation: 1798968 KB  | Memory usage after graph preparation: 1782464 KB
Memory usage prepack start: 1798968 KB            | Memory usage prepack start: 1781968 KB

Memory usage after prepack operations: 1271924 KB | Memory usage after prepack operations: 1653496 KB

Differential Revision: D80460034

Summary:
When exporting models to Vulkan backend, save constant tensors in the NamedDataMap instead of the constant data section of the delegate header.

## Motivation

Prevent screen blackout (Llama 3.2 1B) / device crash (Llama 3.2 3B) when running Llama 3.2 models on Samsung Galaxy S24. This behaviour is related to high peak memory usage when loading the model. For more information, see the top diff/PR in the stack.

## Context

This change is based on the equivalent change D70315207/#9153 in XNNPACK.

Test Plan:


## Memory Comparison with/without NamedDataMap

Measured VmRss using

```
uint64_t getVmRssInKB() {
  std::ifstream statusFile("/proc/self/status");
  std::string l, num;
  while (std::getline(statusFile, l)) {
    if (l.substr(0, 5) == "VmRSS") {
      size_t pos = l.find_first_of("0123456789");
      num = l.substr(pos);
      break;
    }
  }
  uint64_t vmRssInKB = std::stoi(num);
  return vmRssInKB;
}
```

P1908019767 (Meta only)

Excerpt:

```
Log 1                                             | Log 2
--------------------------------------------------|--------------------------------------------------
Memory usage before model compilation: 1115416 KB | Memory usage before model compilation: 1919228 KB
Memory usage after graph building: 1924340 KB     | Memory usage after graph building: 1924256 KB
Memory usage after graph preparation: 1798968 KB  | Memory usage after graph preparation: 1782464 KB
Memory usage prepack start: 1798968 KB            | Memory usage prepack start: 1781968 KB

Memory usage after prepack operations: 1271924 KB | Memory usage after prepack operations: 1653496 KB
```

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Aug 18, 2025

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure

As of commit 13b671d with merge base 8ef9595 (image):

NEW FAILURE - The following job has failed:

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

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

  • pull / test-binary-size-linux-gcc / linux-job (gh) (trunk failure)
    /pytorch/executorch/kernels/portable/cpu/op_stack.cpp:129:26: error: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘ssize_t’ {aka ‘long int’} [-Werror=sign-compare]

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

@meta-cla meta-cla 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 Aug 18, 2025
Copy link

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

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

@SS-JIA
Copy link
Contributor Author

SS-JIA commented Aug 18, 2025

@SS-JIA has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Summary:
When exporting models to Vulkan backend, save constant tensors in the NamedDataMap instead of the constant data section of the delegate header.

## Motivation

Prevent screen blackout (Llama 3.2 1B) / device crash (Llama 3.2 3B) when running Llama 3.2 models on Samsung Galaxy S24. This behaviour is related to high peak memory usage when loading the model. For more information, see the top diff/PR in the stack.

## Context

This change is based on the equivalent change D70315207/#9153 in XNNPACK.

Test Plan:


## Memory Comparison with/without NamedDataMap

Measured VmRss using

```
uint64_t getVmRssInKB() {
  std::ifstream statusFile("/proc/self/status");
  std::string l, num;
  while (std::getline(statusFile, l)) {
    if (l.substr(0, 5) == "VmRSS") {
      size_t pos = l.find_first_of("0123456789");
      num = l.substr(pos);
      break;
    }
  }
  uint64_t vmRssInKB = std::stoi(num);
  return vmRssInKB;
}
```

P1908019767 (Meta only)

Excerpt:

```
Log 1                                             | Log 2
--------------------------------------------------|--------------------------------------------------
Memory usage before model compilation: 1115416 KB | Memory usage before model compilation: 1919228 KB
Memory usage after graph building: 1924340 KB     | Memory usage after graph building: 1924256 KB
Memory usage after graph preparation: 1798968 KB  | Memory usage after graph preparation: 1782464 KB
Memory usage prepack start: 1798968 KB            | Memory usage prepack start: 1781968 KB

Memory usage after prepack operations: 1271924 KB | Memory usage after prepack operations: 1653496 KB
```

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

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

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

Summary:
When exporting models to Vulkan backend, save constant tensors in the NamedDataMap instead of the constant data section of the delegate header.

## Motivation

Prevent screen blackout (Llama 3.2 1B) / device crash (Llama 3.2 3B) when running Llama 3.2 models on Samsung Galaxy S24. This behaviour is related to high peak memory usage when loading the model. For more information, see the top diff/PR in the stack.

## Context

This change is based on the equivalent change D70315207/#9153 in XNNPACK.

Test Plan:


## Memory Comparison with/without NamedDataMap

Measured VmRss using

```
uint64_t getVmRssInKB() {
  std::ifstream statusFile("/proc/self/status");
  std::string l, num;
  while (std::getline(statusFile, l)) {
    if (l.substr(0, 5) == "VmRSS") {
      size_t pos = l.find_first_of("0123456789");
      num = l.substr(pos);
      break;
    }
  }
  uint64_t vmRssInKB = std::stoi(num);
  return vmRssInKB;
}
```

P1908019767 (Meta only)

Excerpt:

```
Log 1                                             | Log 2
--------------------------------------------------|--------------------------------------------------
Memory usage before model compilation: 1115416 KB | Memory usage before model compilation: 1919228 KB
Memory usage after graph building: 1924340 KB     | Memory usage after graph building: 1924256 KB
Memory usage after graph preparation: 1798968 KB  | Memory usage after graph preparation: 1782464 KB
Memory usage prepack start: 1798968 KB            | Memory usage prepack start: 1781968 KB

Memory usage after prepack operations: 1271924 KB | Memory usage after prepack operations: 1653496 KB
```

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

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

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

@facebook-github-bot facebook-github-bot merged commit 546ec43 into gh/SS-JIA/291/base Aug 19, 2025
102 of 106 checks passed
@facebook-github-bot facebook-github-bot deleted the gh/SS-JIA/291/head branch August 19, 2025 02:24
sha256_hash = hashlib.sha256(tensor_bytes)
named_key = sha256_hash.hexdigest()

# Add to named data store with 16-byte alignment (matching XNNPACK)
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

# Generate SHA256 hash as the named key
tensor_bytes = bytes(array)
sha256_hash = hashlib.sha256(tensor_bytes)
named_key = sha256_hash.hexdigest()
Copy link
Contributor

Choose a reason for hiding this comment

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

we deduplicate the data in the pte using the value, and not the key right?

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. fb-exported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants