From 7ba6d5d2ee19a9651dec5e0b853e1e56b05e0607 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 22 Feb 2024 23:47:17 +0800 Subject: [PATCH 1/7] support `CALL_INTRINSIC_1` --- .../executor/opcode_executor.py | 43 +++++++++++++++++++ test/sot/skip_files_py312 | 2 - 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index 5f193cebc085d..a0d56553d68db 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -22,6 +22,7 @@ import traceback import types from dataclasses import dataclass +from enum import Enum from itertools import chain from typing import Any, Callable @@ -1493,6 +1494,41 @@ def LIST_TO_TUPLE(self, instr: Instruction): ) ) + def CALL_INTRINSIC_1(self, instr: Instruction): + MAX_INTRINSIC_1 = 11 # in Python 3.12 + assert isinstance(instr.arg, int) + assert instr.arg <= MAX_INTRINSIC_1 + + OpcodeExecutor_ = self + + class Intrinsics_UnaryFunctions(Enum): + INTRINSIC_1_INVALID = 0 + INTRINSIC_PRINT = 1 # no support, print + INTRINSIC_IMPORT_STAR = 2 # no support, `from module import *` + INTRINSIC_STOPITERATION_ERROR = ( + 3 # no support, generator or coroutine + ) + INTRINSIC_ASYNC_GEN_WRAP = 4 # no support, async + INTRINSIC_UNARY_POSITIVE = 5 + INTRINSIC_LIST_TO_TUPLE = 6 + INTRINSIC_TYPEVAR = 7 # no support, PEP 695 + INTRINSIC_PARAMSPEC = 8 # no support, PEP 695 + INTRINSIC_TYPEVARTUPLE = 9 # no support, PEP 695 + INTRINSIC_SUBSCRIPT_GENERIC = 10 # no support, PEP 695 + INTRINSIC_TYPEALIAS = 11 # no support, PEP 695 + + def to_func(self): + if self == self.INTRINSIC_1_INVALID: + raise RuntimeError("invalid intrinsic function") + elif self == self.INTRINSIC_UNARY_POSITIVE: + return OpcodeExecutor_.UNARY_POSITIVE + elif self == self.INTRINSIC_LIST_TO_TUPLE: + return OpcodeExecutor_.LIST_TO_TUPLE + else: + raise BreakGraphError(f"No support Intrinsics, {self.name}") + + Intrinsics_UnaryFunctions(instr.arg).to_func()(instr) + class OpcodeExecutor(OpcodeExecutorBase): """ @@ -2114,6 +2150,13 @@ def RETURN_VALUE(self, instr: Instruction): len(self.stack) == 1 ), f"Stack must have one element, but get {len(self.stack)} elements." ret_val = self.stack.pop() + return self.compile_return(ret_val) + + def RETURN_CONST(self, instr: Instruction): + ret_const = self._co_consts[instr.arg] + return self.compile_return(ret_const) + + def compile_return(self, ret_val): compile_fn = self._graph.get_compiled_fn(ret_val) if compile_fn.graph_size() < ENV_MIN_GRAPH_SIZE.get(): self.new_code = None diff --git a/test/sot/skip_files_py312 b/test/sot/skip_files_py312 index 3cc5b8d4439e0..c8ff859415af7 100644 --- a/test/sot/skip_files_py312 +++ b/test/sot/skip_files_py312 @@ -1,7 +1,5 @@ -./test_10_build_unpack.py ./test_11_jumps.py ./test_12_for_loop.py -./test_14_operators.py ./test_15_slice.py ./test_17_paddle_layer.py ./test_21_global.py From 1bf29f817a1b77cf982abd3cef5b09cbd7462470 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 22 Feb 2024 23:57:26 +0800 Subject: [PATCH 2/7] clean --- .../jit/sot/opcode_translator/executor/opcode_executor.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index a0d56553d68db..f7bbd1bf4d6ca 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -2150,13 +2150,6 @@ def RETURN_VALUE(self, instr: Instruction): len(self.stack) == 1 ), f"Stack must have one element, but get {len(self.stack)} elements." ret_val = self.stack.pop() - return self.compile_return(ret_val) - - def RETURN_CONST(self, instr: Instruction): - ret_const = self._co_consts[instr.arg] - return self.compile_return(ret_const) - - def compile_return(self, ret_val): compile_fn = self._graph.get_compiled_fn(ret_val) if compile_fn.graph_size() < ENV_MIN_GRAPH_SIZE.get(): self.new_code = None From e8cab5335d0847edad71efabd5489a26ffd0f6c6 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 23 Feb 2024 15:05:14 +0800 Subject: [PATCH 3/7] move to `instr_flag.py` --- .../opcode_translator/executor/instr_flag.py | 19 ++++++++ .../executor/opcode_executor.py | 45 ++++++------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py b/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py index 1dd795439d459..874d98af01d09 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py +++ b/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py @@ -13,6 +13,7 @@ # limitations under the License. # flags for instructions +from enum import Enum class FORMAT_VALUE_FLAG: @@ -34,3 +35,21 @@ class MAKE_FUNCTION_FLAG: class CALL_FUNCTION_EX_FLAG: CFE_HAS_KWARGS = 0x01 + + +class Intrinsics_UnaryFunctions(Enum): + INTRINSIC_1_INVALID = 0 + INTRINSIC_PRINT = 1 # no support, only non-interactive mode + INTRINSIC_IMPORT_STAR = 2 # no support, `from module import *` + INTRINSIC_STOPITERATION_ERROR = 3 # no support, generator or coroutine + INTRINSIC_ASYNC_GEN_WRAP = 4 # no support, async + INTRINSIC_UNARY_POSITIVE = 5 + INTRINSIC_LIST_TO_TUPLE = 6 + INTRINSIC_TYPEVAR = 7 # no support, PEP 695 + INTRINSIC_PARAMSPEC = 8 # no support, PEP 695 + INTRINSIC_TYPEVARTUPLE = 9 # no support, PEP 695 + INTRINSIC_SUBSCRIPT_GENERIC = 10 # no support, PEP 695 + INTRINSIC_TYPEALIAS = 11 # no support, PEP 695 + + +MAX_INTRINSIC_1 = 11 # in Python 3.12, association `Intrinsics_UnaryFunctions` diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index f7bbd1bf4d6ca..066c05dcf7bbf 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -22,7 +22,6 @@ import traceback import types from dataclasses import dataclass -from enum import Enum from itertools import chain from typing import Any, Callable @@ -66,6 +65,8 @@ CALL_FUNCTION_EX_FLAG as CFE, FORMAT_VALUE_FLAG as FV, MAKE_FUNCTION_FLAG as MF, + MAX_INTRINSIC_1, + Intrinsics_UnaryFunctions, ) from .pycode_generator import PyCodeGen from .tracker import ( @@ -1495,39 +1496,21 @@ def LIST_TO_TUPLE(self, instr: Instruction): ) def CALL_INTRINSIC_1(self, instr: Instruction): - MAX_INTRINSIC_1 = 11 # in Python 3.12 assert isinstance(instr.arg, int) assert instr.arg <= MAX_INTRINSIC_1 + opce: OpcodeExecutorBase = self + + def to_func(args): + if args == Intrinsics_UnaryFunctions.INTRINSIC_1_INVALID: + raise RuntimeError("invalid intrinsic function") + elif args == Intrinsics_UnaryFunctions.INTRINSIC_UNARY_POSITIVE: + return opce.UNARY_POSITIVE + elif args == Intrinsics_UnaryFunctions.INTRINSIC_LIST_TO_TUPLE: + return opce.LIST_TO_TUPLE + else: + raise FallbackError(f"No support Intrinsics, {args.name}") - OpcodeExecutor_ = self - - class Intrinsics_UnaryFunctions(Enum): - INTRINSIC_1_INVALID = 0 - INTRINSIC_PRINT = 1 # no support, print - INTRINSIC_IMPORT_STAR = 2 # no support, `from module import *` - INTRINSIC_STOPITERATION_ERROR = ( - 3 # no support, generator or coroutine - ) - INTRINSIC_ASYNC_GEN_WRAP = 4 # no support, async - INTRINSIC_UNARY_POSITIVE = 5 - INTRINSIC_LIST_TO_TUPLE = 6 - INTRINSIC_TYPEVAR = 7 # no support, PEP 695 - INTRINSIC_PARAMSPEC = 8 # no support, PEP 695 - INTRINSIC_TYPEVARTUPLE = 9 # no support, PEP 695 - INTRINSIC_SUBSCRIPT_GENERIC = 10 # no support, PEP 695 - INTRINSIC_TYPEALIAS = 11 # no support, PEP 695 - - def to_func(self): - if self == self.INTRINSIC_1_INVALID: - raise RuntimeError("invalid intrinsic function") - elif self == self.INTRINSIC_UNARY_POSITIVE: - return OpcodeExecutor_.UNARY_POSITIVE - elif self == self.INTRINSIC_LIST_TO_TUPLE: - return OpcodeExecutor_.LIST_TO_TUPLE - else: - raise BreakGraphError(f"No support Intrinsics, {self.name}") - - Intrinsics_UnaryFunctions(instr.arg).to_func()(instr) + to_func(Intrinsics_UnaryFunctions(instr.arg))(instr) class OpcodeExecutor(OpcodeExecutorBase): From 2b16f57b92ea7cdc3fcdd5f8d7486aa4bdcde1ed Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 23 Feb 2024 16:26:13 +0800 Subject: [PATCH 4/7] clean code --- .../opcode_translator/executor/instr_flag.py | 6 ++-- .../executor/opcode_executor.py | 29 ++++++++----------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py b/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py index 874d98af01d09..448eac6ff95c2 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py +++ b/python/paddle/jit/sot/opcode_translator/executor/instr_flag.py @@ -37,7 +37,8 @@ class CALL_FUNCTION_EX_FLAG: CFE_HAS_KWARGS = 0x01 -class Intrinsics_UnaryFunctions(Enum): +# see https://github.com/python/cpython/blob/3.12/Python/intrinsics.c#L211-L225 +class IntrinsicsUnaryFunctions(Enum): INTRINSIC_1_INVALID = 0 INTRINSIC_PRINT = 1 # no support, only non-interactive mode INTRINSIC_IMPORT_STAR = 2 # no support, `from module import *` @@ -50,6 +51,3 @@ class Intrinsics_UnaryFunctions(Enum): INTRINSIC_TYPEVARTUPLE = 9 # no support, PEP 695 INTRINSIC_SUBSCRIPT_GENERIC = 10 # no support, PEP 695 INTRINSIC_TYPEALIAS = 11 # no support, PEP 695 - - -MAX_INTRINSIC_1 = 11 # in Python 3.12, association `Intrinsics_UnaryFunctions` diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index 066c05dcf7bbf..ce941bc993d89 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -65,8 +65,7 @@ CALL_FUNCTION_EX_FLAG as CFE, FORMAT_VALUE_FLAG as FV, MAKE_FUNCTION_FLAG as MF, - MAX_INTRINSIC_1, - Intrinsics_UnaryFunctions, + IntrinsicsUnaryFunctions, ) from .pycode_generator import PyCodeGen from .tracker import ( @@ -1496,21 +1495,17 @@ def LIST_TO_TUPLE(self, instr: Instruction): ) def CALL_INTRINSIC_1(self, instr: Instruction): - assert isinstance(instr.arg, int) - assert instr.arg <= MAX_INTRINSIC_1 - opce: OpcodeExecutorBase = self - - def to_func(args): - if args == Intrinsics_UnaryFunctions.INTRINSIC_1_INVALID: - raise RuntimeError("invalid intrinsic function") - elif args == Intrinsics_UnaryFunctions.INTRINSIC_UNARY_POSITIVE: - return opce.UNARY_POSITIVE - elif args == Intrinsics_UnaryFunctions.INTRINSIC_LIST_TO_TUPLE: - return opce.LIST_TO_TUPLE - else: - raise FallbackError(f"No support Intrinsics, {args.name}") - - to_func(Intrinsics_UnaryFunctions(instr.arg))(instr) + Intrinsic_Func = IntrinsicsUnaryFunctions(instr.arg) + if Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_1_INVALID: + raise RuntimeError("invalid intrinsic function") + elif ( + Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_UNARY_POSITIVE + ): + return self.UNARY_POSITIVE(instr) + elif Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_LIST_TO_TUPLE: + return self.LIST_TO_TUPLE(instr) + else: + raise FallbackError(f"No support Intrinsics, {Intrinsic_Func.name}") class OpcodeExecutor(OpcodeExecutorBase): From ca58213e176beb77a327bc795cb72f153b699e91 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:27:52 +0800 Subject: [PATCH 5/7] Update python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py Co-authored-by: Nyakku Shigure --- .../jit/sot/opcode_translator/executor/opcode_executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index ce941bc993d89..c5f41df4c29cd 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -1495,7 +1495,7 @@ def LIST_TO_TUPLE(self, instr: Instruction): ) def CALL_INTRINSIC_1(self, instr: Instruction): - Intrinsic_Func = IntrinsicsUnaryFunctions(instr.arg) + intrinsic_func = IntrinsicsUnaryFunctions(instr.arg) if Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_1_INVALID: raise RuntimeError("invalid intrinsic function") elif ( From 8b67cd93ec2de93d34c460109ee723a54d961081 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Fri, 23 Feb 2024 16:29:09 +0800 Subject: [PATCH 6/7] Update python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py --- .../jit/sot/opcode_translator/executor/opcode_executor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index c5f41df4c29cd..251f999ed9fba 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -1496,16 +1496,16 @@ def LIST_TO_TUPLE(self, instr: Instruction): def CALL_INTRINSIC_1(self, instr: Instruction): intrinsic_func = IntrinsicsUnaryFunctions(instr.arg) - if Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_1_INVALID: + if intrinsic_func == IntrinsicsUnaryFunctions.INTRINSIC_1_INVALID: raise RuntimeError("invalid intrinsic function") elif ( - Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_UNARY_POSITIVE + intrinsic_func == IntrinsicsUnaryFunctions.INTRINSIC_UNARY_POSITIVE ): return self.UNARY_POSITIVE(instr) - elif Intrinsic_Func == IntrinsicsUnaryFunctions.INTRINSIC_LIST_TO_TUPLE: + elif intrinsic_func == IntrinsicsUnaryFunctions.INTRINSIC_LIST_TO_TUPLE: return self.LIST_TO_TUPLE(instr) else: - raise FallbackError(f"No support Intrinsics, {Intrinsic_Func.name}") + raise FallbackError(f"No support Intrinsics, {intrinsic_func.name}") class OpcodeExecutor(OpcodeExecutorBase): From cd14707a10da41ccca16f5bfaa6a5071b76aeb59 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 23 Feb 2024 16:51:50 +0800 Subject: [PATCH 7/7] clean return --- .../jit/sot/opcode_translator/executor/opcode_executor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py index 251f999ed9fba..c248dad401566 100644 --- a/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py +++ b/python/paddle/jit/sot/opcode_translator/executor/opcode_executor.py @@ -1501,9 +1501,9 @@ def CALL_INTRINSIC_1(self, instr: Instruction): elif ( intrinsic_func == IntrinsicsUnaryFunctions.INTRINSIC_UNARY_POSITIVE ): - return self.UNARY_POSITIVE(instr) + self.UNARY_POSITIVE(instr) elif intrinsic_func == IntrinsicsUnaryFunctions.INTRINSIC_LIST_TO_TUPLE: - return self.LIST_TO_TUPLE(instr) + self.LIST_TO_TUPLE(instr) else: raise FallbackError(f"No support Intrinsics, {intrinsic_func.name}")