# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from: https://github.com/vllm-project/vllm/blob/v0.7.3/vllm/model_executor/utils.py from typing import Any import torch from sglang.multimodal_gen.runtime.platforms import current_platform def set_weight_attrs( weight: torch.Tensor, weight_attrs: dict[str, Any] | None, ): """Set attributes on a weight tensor without overwriting existing ones.""" if weight_attrs is None: return for key, value in weight_attrs.items(): assert not hasattr(weight, key), f"Overwriting existing tensor attribute: {key}" if current_platform.is_tpu() and key == "weight_loader": value = make_synced_weight_loader(value) setattr(weight, key, value) def make_synced_weight_loader(original_weight_loader) -> Any: def _synced_weight_loader(param, *args, **kwargs): original_weight_loader(param, *args, **kwargs) torch._sync(param) return _synced_weight_loader