Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
22 changes: 22 additions & 0 deletions .ai-rulez/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$schema: https://raw.githubusercontent.com/Goldziher/ai-rulez/main/schema/ai-rules-v3.schema.json
description: Convert PaddlePaddle models to ONNX format
gitignore: true
name: Paddle2ONNX

builtins:
- python
- security
- git-workflow
- code-quality
- testing
- token-efficiency
- default-commands

presets:
- claude
- gemini
- codex

compression:
level: moderate
version: "3.0"
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[submodule "third_party/onnx"]
path = third_party/onnx
url = https://github.com/onnx/onnx.git
branch = 990217f043af7222348ca8f0301e17fa7b841781
branch = d3f6b795aedb48eaecc881bf5e8f5dd6efbe25b3
[submodule "third_party/optimizer"]
path = third_party/optimizer
url = https://github.com/onnx/optimizer.git
branch = b3a4611861734e0731bbcc2bed1f080139e4988b
branch = 94d238d96e3fb3a7ba34f03c284b9ad3516163be
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11.git
branch = 3e9dfa2866941655c56877882565e7577de6fc7b
branch = 45fab4087eaaff234227a10cf7845e8b07f28a98
[submodule "third_party/glog"]
path = third_party/glog
url = https://github.com/google/glog.git
163 changes: 53 additions & 110 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,111 +1,54 @@
# Exclude all third-party libraries
exclude: |
(?x)^(
patches/.+|
third_party/.+
)$
default_install_hook_types:
- pre-commit
- commit-msg
exclude: ^third_party/|vendor/|node_modules/|dist/|\.setuptools-cmake-build/|patches/

repos:
# Common hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
- id: check-symlinks
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/Lucas-C/pre-commit-hooks.git
rev: v1.5.1
hooks:
- id: remove-crlf
- id: remove-tabs
name: Tabs remover (C++)
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|xpu|kps)$
args: [--whitespaces-count, '2']
- id: remove-tabs
name: Tabs remover (Python)
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$
args: [--whitespaces-count, '4']
# Exclude some unit test files that require tabs.
- repo: local
hooks:
- id: copyright_checker
name: copyright_checker
entry: python ./tools/codestyle/copyright.py
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|pyi|sh)$
# For Python files
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --no-cache]
# For C++ files
- repo: local
hooks:
- id: clang-format
name: clang-format
description: Format files with ClangFormat.
entry: bash ./tools/codestyle/clang_format.sh -i
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|xpu|kps)$
- repo: local
hooks:
- id: cpplint-cpp-source
name: cpplint
description: Check C++ code style using cpplint.py.
entry: bash ./tools/codestyle/cpplint_pre_commit.sh
language: system
files: \.(cc|cxx|cpp|cu|h|hpp|hxx)$
args:
- --extensions=cc,cxx,cpp,cu,cuh,h,hpp,hxx,kps
- --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11,-whitespace/parens
- --quiet
- repo: local
hooks:
- id: clang-tidy
name: clang-tidy
description: Parallel clang-tidy runner.
entry: python ./tools/codestyle/clang-tidy.py
language: system
files: \.(c|cc|cxx|cpp|h|hpp|hxx)$
args:
- -p=build/
- -extra-arg=-Wno-unknown-warning-option
- -extra-arg=-Wno-pessimizing-move
- -extra-arg=-Wno-braced-scalar-init
- -extra-arg=-Wno-dangling-gsl
- -extra-arg=-Wno-deprecated-copy
- -extra-arg=-Wno-final-dtor-non-final-class
- -extra-arg=-Wno-implicit-int-float-conversion
- -extra-arg=-Wno-inconsistent-missing-override
- -extra-arg=-Wno-infinite-recursion
- -extra-arg=-Wno-mismatched-tags
- -extra-arg=-Wno-self-assign
- -extra-arg=-Wno-sign-compare
- -extra-arg=-Wno-sometimes-uninitialized
- -extra-arg=-Wno-tautological-overlap-compare
- -extra-arg=-Wno-unused-const-variable
- -extra-arg=-Wno-unused-lambda-capture
- -extra-arg=-Wno-unused-private-field
- -extra-arg=-Wno-unused-value
- -extra-arg=-Wno-unused-variable
- -extra-arg=-Wno-overloaded-virtual
- -extra-arg=-Wno-defaulted-function-deleted
- -extra-arg=-Wno-delete-non-abstract-non-virtual-dtor
- -extra-arg=-Wno-return-type-c-linkage
# For CMake files
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
hooks:
- id: cmake-format
- repo: https://github.com/PFCCLab/cmake-lint-paddle
rev: v1.5.1
hooks:
- id: cmakelint
args: [--config=./tools/codestyle/.cmakelintrc]
# Commit message linting
- repo: https://github.com/Goldziher/gitfluff
rev: v0.8.0
hooks:
- id: gitfluff-lint
args: ["--write"]
stages: [commit-msg]

# AI-Rulez: auto-generate AI assistant configuration files
- repo: https://github.com/Goldziher/ai-rulez
rev: v3.8.3
hooks:
- id: ai-rulez-generate

# General file checks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- id: check-added-large-files
- id: detect-private-key
- id: check-json
- id: check-yaml
args: ["--allow-multiple-documents", "--unsafe"]
- id: check-toml
- id: check-case-conflict

# TOML formatting
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "v2.16.2"
hooks:
- id: pyproject-fmt

- repo: https://github.com/ComPWA/taplo-pre-commit
rev: v0.9.3
hooks:
- id: taplo-format
exclude: "pyproject.toml"

# Python: ruff (linting + formatting)
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.5
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(paddle2onnx C CXX)
# ONNX 1.16 requires C++ 17
# ONNX 1.20 requires C++ 17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Build the libraries with - fPIC
Expand All @@ -24,7 +24,7 @@ endif()

# Set max opset version for onnx if you build from other version of onnx this
# should be modified.
add_definitions(-DMAX_ONNX_OPSET_VERSION=23)
add_definitions(-DMAX_ONNX_OPSET_VERSION=25)
add_definitions(-DPADDLE2ONNX_LIB)

# Internal flags for convert.h.in
Expand Down Expand Up @@ -57,10 +57,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${PROJECT_SOURCE_DIR}/third_party/optimizer)

file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc
${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/*.cc)
${PROJECT_SOURCE_DIR}/third_party/optimizer/onnx/optimizer/*.cc)
list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/cpp2py_export.cc)
list(REMOVE_ITEM ALL_SRCS
${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/cpp2py_export.cc)

file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" PADDLE2ONNX_VERSION)
string(STRIP "${PADDLE2ONNX_VERSION}" PADDLE2ONNX_VERSION)
Expand Down
38 changes: 17 additions & 21 deletions debug/p2o_infer_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
# limitations under the License.

import argparse
import logging
import os
import queue
import re
import sys
import logging
import shutil
import sys
import tempfile
import traceback
from contextlib import contextmanager

import numpy as np
from onnxruntime import InferenceSession
from prune_onnx_model import prune_onnx_model

import paddle
import paddle2onnx
from prune_onnx_model import prune_onnx_model
from contextlib import contextmanager
import traceback
import queue
import tempfile

current_dir = os.path.dirname(os.path.abspath(__file__))
tests_dir = os.path.join(current_dir, "..", "tests")
Expand Down Expand Up @@ -202,8 +204,8 @@ def check_operator_with_print(
def _redirect_paddle_output_to_file(
paddle_model_file, log_file, inputs_data: tuple
):
import subprocess
import pickle
import subprocess
import tempfile

temp_filename = None
Expand Down Expand Up @@ -266,7 +268,7 @@ def _compare_results(paddle_model_path, onnx_model_path, inputs_data: tuple):
# TODO(wangmingkai02): adjust n according to the number of print op
n = 8
shape_list, dtype, data_list = [], None, []
with open(log_file, "r", encoding="utf-8") as f:
with open(log_file, encoding="utf-8") as f:
lines = []
for _ in range(n):
line = f.readline()
Expand All @@ -291,7 +293,7 @@ def _compare_results(paddle_model_path, onnx_model_path, inputs_data: tuple):
providers=["CPUExecutionProvider"],
)
input_names = session.get_inputs()
input_feed = dict()
input_feed = {}
for idx, input_name in enumerate(input_names):
input_feed[input_name.name] = inputs_data[idx]
result = session.run(output_names=None, input_feed=input_feed)
Expand Down Expand Up @@ -359,10 +361,7 @@ def _binary_search(program, block):
idx = (left + right) // 2 + offset
op = block.ops[idx]
op_name = op.name()
if idx < left:
left = idx - offset + 1
offset = 0
elif op_name in SKIP_FORWARD_OP_LIST:
if idx < left or op_name in SKIP_FORWARD_OP_LIST:
left = idx - offset + 1
offset = 0
elif op_name in SKIP_BACKWARD_OP_LIST:
Expand Down Expand Up @@ -533,7 +532,7 @@ def _compare_results(
providers=["CPUExecutionProvider"],
)
input_names = session.get_inputs()
input_feed = dict()
input_feed = {}
for idx, input_name in enumerate(input_names):
input_feed[input_name.name] = inputs_data[idx]
result = session.run(output_names=None, input_feed=input_feed)
Expand Down Expand Up @@ -606,10 +605,7 @@ def _check_operator(program, model_file, idx, input_shapes, input_dtypes):
clone_program = program.clone()
idx = (left + right) // 2 + offset
op = clone_program.blocks[0].ops[idx]
if idx < left:
left = idx - offset + 1
offset = 0
elif op.name() in SKIP_FORWARD_OP_LIST:
if idx < left or op.name() in SKIP_FORWARD_OP_LIST:
left = idx - offset + 1
offset = 0
elif op.name() in SKIP_BACKWARD_OP_LIST:
Expand Down Expand Up @@ -654,7 +650,7 @@ def locate_issue(
model_file,
input_shapes,
input_dtypes,
candidates: list[int] = None,
candidates: list[int] | None = None,
has_cf=False,
binary_search=False,
output_num=1,
Expand Down Expand Up @@ -705,7 +701,7 @@ def _get_mapping_and_uniq_set(program):
index_mapping = {}
ops = set()
global_ops = set()
global_res = list()
global_res = []
shadow_output_op_num = 0
for block in program.blocks:
ops |= _dfs(block, index_mapping)
Expand Down
13 changes: 8 additions & 5 deletions debug/prune_onnx_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import onnx
from onnx import helper, TensorProto
import os
import math
import logging
import math
import os

import onnx
from onnx import TensorProto, helper

logger = logging.getLogger("p2o-logger")


def prune_onnx_model(
onnx_model_file,
target_node_name="p2o.print",
target_dims=[1],
target_dims=None,
target_dtype="float32",
):
if target_dims is None:
target_dims = [1]
dtype_map = {
"bool": (TensorProto.BOOL, bool),
"float32": (TensorProto.FLOAT, float),
Expand Down
Loading