main
py 38 lines 1.26 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from functools import partial
5
6 import pytest
7 import torch
8 import torch.nn as nn
9
10 from vllm.model_executor.layers.activation import GeluAndMul, SiluAndMul
11 from vllm.platforms import CpuArchEnum, current_platform
12
13
14 @pytest.mark.skipif(
15 not current_platform.is_cpu()
16 or current_platform.get_cpu_architecture() == CpuArchEnum.POWERPC,
17 reason="Native CPU activations require a non-PowerPC CPU platform",
18 )
19 @pytest.mark.parametrize(
20 "activation_factory,reference",
21 [
22 (SiluAndMul, nn.SiLU()),
23 (GeluAndMul, nn.GELU()),
24 (partial(GeluAndMul, approximate="tanh"), nn.GELU(approximate="tanh")),
25 ],
26 ids=["silu", "gelu", "gelu_tanh"],
27 )
28 def test_cpu_act_and_mul_without_compiled_ops(
29 default_vllm_config, monkeypatch, activation_factory, reference
30 ):
31 """CPU activations must construct and run without compiled wheel operators."""
32 default_vllm_config.compilation_config.custom_ops = ["all"]
33 monkeypatch.setattr(torch.ops, "_C", object())
34 x = torch.tensor([[-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]], device="cpu")
35
36 layer = activation_factory()
37
38 torch.testing.assert_close(layer(x), reference(x[:, :4]) * x[:, 4:])