Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (brevitas_examples/llm): remove dependencies from optimum-amd #1094

Merged
merged 6 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ def tests_brevitas_examples_llm(session, pytorch, jit_status):
install_pytorch(pytorch, session)
install_torchvision(pytorch, session) # Optimum seems to require torchvision
session.install('-e', '.[test, llm, export]')
session.install(
'optimum-amd[brevitas] @ git+https://github.com/huggingface/optimum-amd.git@main')
session.run('pytest', '-n', 'logical', '-k', 'llm', 'tests/brevitas_examples/test_llm.py')


Expand Down
7 changes: 6 additions & 1 deletion requirements/requirements-llm.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# optimum-amd[brevitas] @ git+https://github.com/huggingface/optimum-amd.git@main
accelerate
datasets
onnx
onnx-tools
onnxruntime
optimum
tqdm
transformers[sentencepiece]==4.45.2
30 changes: 19 additions & 11 deletions src/brevitas_examples/llm/llm_quant/data.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
"""
Adapted from https://github.com/IST-DASLab/gptq, released under the following LICENSE:
Adapted from https://github.com/huggingface/optimum-amd, released under the following LICENSE:

Copyright 2023 IST-DASLab
MIT License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright (c) 2023 Hugging Face

http://www.apache.org/licenses/LICENSE-2.0
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import random
Expand Down
54 changes: 23 additions & 31 deletions src/brevitas_examples/llm/llm_quant/eval.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
"""
Adapted from https://github.com/IST-DASLab/gptq, released under the following LICENSE:
Adapted from https://github.com/huggingface/optimum-amd, released under the following LICENSE:

Copyright 2023 IST-DASLab
MIT License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright (c) 2023 Hugging Face

http://www.apache.org/licenses/LICENSE-2.0
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from random import random
from typing import Any, Dict, Iterable, List, Union
import random
from typing import Any, Dict, List

import numpy as np
import torch
from torch import nn
from tqdm import tqdm

from brevitas_examples.llm.llm_quant.data_utils import recursive_to_device


def create_validation_dataloader(data, seqlen, device):
nsamples = data['input_ids'].numel() // seqlen
Expand All @@ -35,24 +45,6 @@ def create_validation_dataloader(data, seqlen, device):
return val_dataloader


@torch.no_grad()
def recursive_to_device(tensor_or_iterable: Union[Iterable, torch.Tensor], device) -> None:
if isinstance(tensor_or_iterable, torch.Tensor):
return tensor_or_iterable.to(device)
elif isinstance(tensor_or_iterable,
tuple): # Special handling of tuples, since they are immutable
tmp_list = []
for i in tensor_or_iterable:
tmp_list.append(recursive_to_device(i, device))
return tuple(tmp_list)
elif isinstance(tensor_or_iterable, Iterable):
for i in tensor_or_iterable:
tensor_or_iterable[i] = recursive_to_device(i, device)
return tensor_or_iterable
else:
raise ValueError(f"Cannot move {type(tensor_or_iterable)} to {device}")


@torch.no_grad()
def compute_perplexity(
model: torch.nn.Module,
Expand Down
Loading