Skip to content

Commit 720aea5

Browse files
authored
Add CI for checking file size (#14274)
As title. xrefs will hang if the files change too much, however it's a bit hard to tell. Make file size check explicit. The rule is following 1. For all files, the file size can't be larger than 1MB 2. For images/vidoes, the files size can't be larger than 7MB 3. There is an exception list defined in the script if it's really needed
1 parent b1a41e7 commit 720aea5

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.github/workflows/_link_check.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,29 @@ jobs:
5555
echo "Or add \`@lint-ignore\` somewhere on the same line as the reference you want to skip checking."
5656
exit 1
5757
}
58+
59+
lint-file-size:
60+
if: ${{ github.event_name == 'pull_request' }}
61+
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
62+
with:
63+
runner: linux.2xlarge
64+
docker-image: ci-image:executorch-ubuntu-22.04-linter
65+
submodules: false
66+
fetch-depth: 0
67+
ref: ${{ inputs.ref }}
68+
timeout: 30
69+
script: |
70+
chmod +x ./scripts/lint_file_size.sh
71+
./scripts/lint_file_size.sh $(
72+
if [ "${{ github.event_name }}" = "pull_request" ]; then
73+
echo "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}"
74+
else
75+
echo "${{ github.event.before }}" "${{ github.sha }}"
76+
fi
77+
) || {
78+
echo
79+
echo "File size lint failed: some files exceed the 1 MB limit."
80+
echo "If you really need large files, consider using Git LFS or storing them elsewhere."
81+
echo "If you really need to get unblocked and check in the file, can add it to the EXCEPTIONS list in scripts/lint_file_size.sh."
82+
exit 1
83+
}

scripts/lint_file_size.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -euo pipefail
9+
10+
status=0
11+
12+
green='\e[1;32m'; red='\e[1;31m'; cyan='\e[1;36m'; reset='\e[0m'
13+
14+
# Following is the rules for the file size linting:
15+
# 1. For all files, the file size can't be larger than 1MB
16+
# 2. For images/videos, the file size can't be larger than 8MB
17+
# 3. There is an exception list defined in the script if it's really needed
18+
19+
# List of files to skip (relative paths)
20+
EXCEPTIONS=(
21+
"examples/models/llama/params/demo_rand_params.pth"
22+
"examples/models/llama/tokenizer/test/resources/test_tiktoken_tokenizer.model"
23+
"examples/qualcomm/oss_scripts/llama/artifacts/stories260k_hybrid_llama_qnn.pte"
24+
# Following needs to be clean up
25+
"examples/mediatek/models/llm_models/weights/Llama-3.2-1B-Instruct/tokenizer.json"
26+
"examples/mediatek/models/llm_models/weights/Llama-3.2-3B-Instruct/tokenizer.json"
27+
"examples/mediatek/models/llm_models/weights/llama3-8B-instruct/tokenizer.json"
28+
)
29+
30+
is_exception() {
31+
local f=$1
32+
for ex in "${EXCEPTIONS[@]}"; do
33+
if [[ "$f" == "$ex" ]]; then
34+
return 0
35+
fi
36+
done
37+
return 1
38+
}
39+
40+
if [ $# -eq 2 ]; then
41+
base=$1
42+
head=$2
43+
echo "Checking changed files between $base...$head"
44+
files=$(git diff --name-only "$base...$head")
45+
else
46+
echo "Checking all files in repository"
47+
files=$(git ls-files)
48+
fi
49+
50+
for file in $files; do
51+
if is_exception "$file"; then
52+
echo -e "${cyan}SKIP${reset} $file (in exception list)"
53+
continue
54+
fi
55+
if [ -f "$file" ]; then
56+
# Set size limit depending on extension
57+
if [[ "$file" =~ \.(png|jpg|jpeg|gif|svg|mp3|mp4)$ ]]; then
58+
MAX_SIZE=$((8 * 1024 * 1024)) # 8 MB for pictures
59+
else
60+
MAX_SIZE=$((1 * 1024 * 1024)) # 1 MB for others
61+
fi
62+
63+
size=$(wc -c <"$file")
64+
if [ "$size" -gt "$MAX_SIZE" ]; then
65+
echo -e "${red}FAIL${reset} $file (${cyan}${size} bytes${reset}) exceeds ${MAX_SIZE} bytes"
66+
status=1
67+
else
68+
echo -e "${green}OK${reset} $file (${size} bytes)"
69+
fi
70+
fi
71+
done
72+
73+
exit $status

0 commit comments

Comments
 (0)