codebuff-agent 2.1.74__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codebuff_agent-2.1.74.data/data/etc/jupyter/jupyter_server_config.d/codebuff_agent.json +7 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/package.json +157 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/104.2b8f20f9d1ac2c9db01b.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/150.39fa98117cc8ca590465.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/166.386ff9b2c37ac865fdee.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/216.25cf81131bf7274dabf4.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/353.581bd1cb00746399b62e.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/367.575f9621b63c9a641aa9.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/421.11dac223a2c2da1b09f5.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/432.8513b4c6cc1d1232d165.js +2 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/432.8513b4c6cc1d1232d165.js.LICENSE.txt +19 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/439.ba5b5ef70ec6de214b8c.js +2 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/439.ba5b5ef70ec6de214b8c.js.LICENSE.txt +8 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/481.cc2dec9fa869d66f1407.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/487.0796e931d6e651482c14.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/49.d326f554b6f9ccde6580.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/541.26897ea32845659e36be.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/579.e569d3edd8391fd8fbb3.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/58.eb7cc679f7535cc8a2f1.js +2 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/58.eb7cc679f7535cc8a2f1.js.LICENSE.txt +8 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/666.d9bfdb23f98489d0a188.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/677.d266c39dbed33bc388b3.js +2 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/677.d266c39dbed33bc388b3.js.LICENSE.txt +8 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/68.26b6f59c8454adb17f20.js +204 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/694.4ac443814260057e68f7.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/791.61affb8d3480d3d60e0d.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/848.4d2db09cfbb09374b299.js +2 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/848.4d2db09cfbb09374b299.js.LICENSE.txt +9 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/868.5cfe7e1a348ec5af1b8c.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/97.ea559a5b8a9a09f17c6b.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/remoteEntry.5292fc9c6df7e7920cc8.js +1 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/style.js +4 -0
- codebuff_agent-2.1.74.data/data/share/jupyter/labextensions/codebuff/static/third-party-licenses.json +724 -0
- codebuff_agent-2.1.74.dist-info/METADATA +142 -0
- codebuff_agent-2.1.74.dist-info/RECORD +217 -0
- codebuff_agent-2.1.74.dist-info/WHEEL +4 -0
- codebuff_agent-2.1.74.dist-info/licenses/LICENSE +21 -0
- codebuff_core/__init__.py +153 -0
- codebuff_core/codebuff_config_defaults.json +64 -0
- codebuff_core/core/__init__.py +35 -0
- codebuff_core/core/code_validator.py +1238 -0
- codebuff_core/core/error_classifier.py +579 -0
- codebuff_core/core/exceptions.py +49 -0
- codebuff_core/data/tiktoken_cache/9b5ad71b2ce5302211f9c61530b329a4922fc6a4 +100256 -0
- codebuff_core/directory_policy.py +32 -0
- codebuff_core/factory.py +155 -0
- codebuff_core/interfaces.py +197 -0
- codebuff_core/knowledge/__init__.py +31 -0
- codebuff_core/knowledge/chunking.py +375 -0
- codebuff_core/knowledge/loader.py +339 -0
- codebuff_core/knowledge/watchdog_service.py +354 -0
- codebuff_core/legacy_paths.py +69 -0
- codebuff_core/llm/__init__.py +13 -0
- codebuff_core/llm/service.py +407 -0
- codebuff_core/managers/__init__.py +22 -0
- codebuff_core/managers/config_manager.py +690 -0
- codebuff_core/managers/remote_config.py +673 -0
- codebuff_core/managers/remote_skills.py +539 -0
- codebuff_core/managers/session_manager.py +255 -0
- codebuff_core/middleware/__init__.py +3 -0
- codebuff_core/middleware/error_recovery.py +394 -0
- codebuff_core/middleware/logging_utils.py +33 -0
- codebuff_core/models/__init__.py +111 -0
- codebuff_core/models/agent.py +306 -0
- codebuff_core/models/chat.py +57 -0
- codebuff_core/models/common.py +82 -0
- codebuff_core/models/rag.py +300 -0
- codebuff_core/platform_rules.py +127 -0
- codebuff_core/prompts/__init__.py +44 -0
- codebuff_core/prompts/auto_agent_prompts.py +1278 -0
- codebuff_core/services/__init__.py +30 -0
- codebuff_core/services/agent_service.py +345 -0
- codebuff_core/services/analytics_aggregate.py +837 -0
- codebuff_core/services/chat_service.py +898 -0
- codebuff_core/services/context_condenser.py +306 -0
- codebuff_core/services/pii_guardrail.py +393 -0
- codebuff_core/services/rag_chat_agent.py +2993 -0
- codebuff_core/services/rag_collections.py +2740 -0
- codebuff_core/services/rag_manager.py +890 -0
- codebuff_core/services/rag_service.py +145 -0
- codebuff_core/services/retriever.py +260 -0
- codebuff_core/services/vllm_embedding_service.py +290 -0
- codebuff_core/tests/__init__.py +1 -0
- codebuff_core/tests/conftest.py +81 -0
- codebuff_core/tests/test_error_recovery.py +647 -0
- codebuff_core/tests/test_factory.py +135 -0
- codebuff_core/tests/test_services.py +150 -0
- jupyter_ext/__init__.py +266 -0
- jupyter_ext/_version.py +4 -0
- jupyter_ext/agent/__init__.py +49 -0
- jupyter_ext/agent/agent_factory.py +2270 -0
- jupyter_ext/agent/ast_tools.py +451 -0
- jupyter_ext/agent/autofix_middleware.py +481 -0
- jupyter_ext/agent/background_procs.py +372 -0
- jupyter_ext/agent/code_history_middleware.py +211 -0
- jupyter_ext/agent/codex_autofix.py +160 -0
- jupyter_ext/agent/codex_bridge.py +3339 -0
- jupyter_ext/agent/codex_hooks.py +320 -0
- jupyter_ext/agent/codex_slash.py +685 -0
- jupyter_ext/agent/codex_threads.py +114 -0
- jupyter_ext/agent/content_injection.py +377 -0
- jupyter_ext/agent/continuation_middleware.py +548 -0
- jupyter_ext/agent/custom_skills.py +335 -0
- jupyter_ext/agent/file_diffs.py +99 -0
- jupyter_ext/agent/git_history.py +188 -0
- jupyter_ext/agent/guardrail_middleware.py +421 -0
- jupyter_ext/agent/json_repair_utils.py +54 -0
- jupyter_ext/agent/langchain_tools.py +2673 -0
- jupyter_ext/agent/mermaid_fixer.py +289 -0
- jupyter_ext/agent/mermaid_middleware.py +135 -0
- jupyter_ext/agent/middleware.py +359 -0
- jupyter_ext/agent/middleware_prompts.py +53 -0
- jupyter_ext/agent/observability.py +334 -0
- jupyter_ext/agent/patch_tool_calls.py +57 -0
- jupyter_ext/agent/prompts.py +112 -0
- jupyter_ext/agent/skill_middleware.py +257 -0
- jupyter_ext/agent/skills/airflow_dag_development.md +36 -0
- jupyter_ext/agent/skills/anomaly_detection.md +12 -0
- jupyter_ext/agent/skills/athena_sql.md +156 -0
- jupyter_ext/agent/skills/bdp_table_search.md +33 -0
- jupyter_ext/agent/skills/bigquery.md +65 -0
- jupyter_ext/agent/skills/bigquery_oracle.md +231 -0
- jupyter_ext/agent/skills/bigquery_tuning.md +307 -0
- jupyter_ext/agent/skills/classification_evaluation.md +11 -0
- jupyter_ext/agent/skills/clustering_analysis.md +12 -0
- jupyter_ext/agent/skills/comprehensive_eda.md +11 -0
- jupyter_ext/agent/skills/data_analysis.md +236 -0
- jupyter_ext/agent/skills/data_cleaning_pipeline.md +12 -0
- jupyter_ext/agent/skills/data_loading.md +158 -0
- jupyter_ext/agent/skills/data_quality_report.md +13 -0
- jupyter_ext/agent/skills/dimensionality_reduction.md +12 -0
- jupyter_ext/agent/skills/emr_script_development.md +17 -0
- jupyter_ext/agent/skills/experiment_tracking.md +13 -0
- jupyter_ext/agent/skills/feature_importance_analysis.md +10 -0
- jupyter_ext/agent/skills/hyperparameter_tuning.md +12 -0
- jupyter_ext/agent/skills/hypothesis_testing.md +12 -0
- jupyter_ext/agent/skills/imbalanced_data_handling.md +12 -0
- jupyter_ext/agent/skills/inference.md +392 -0
- jupyter_ext/agent/skills/lambda_function_development.md +19 -0
- jupyter_ext/agent/skills/model_comparison.md +11 -0
- jupyter_ext/agent/skills/model_explainability.md +12 -0
- jupyter_ext/agent/skills/model_training.md +318 -0
- jupyter_ext/agent/skills/pyspark.md +352 -0
- jupyter_ext/agent/skills/python_code_quality.md +35 -0
- jupyter_ext/agent/skills/regression_evaluation.md +12 -0
- jupyter_ext/agent/skills/spring_java_development.md +35 -0
- jupyter_ext/agent/skills/text_preprocessing.md +12 -0
- jupyter_ext/agent/skills/time_series_analysis.md +13 -0
- jupyter_ext/agent/skills/unit_testing.md +203 -0
- jupyter_ext/agent/todo_prompts.py +57 -0
- jupyter_ext/agent/tool_report.py +45 -0
- jupyter_ext/analytics_ingest.py +599 -0
- jupyter_ext/etc/jupyter/jupyter_server_config.d/codebuff_agent.json +7 -0
- jupyter_ext/handlers/__init__.py +8 -0
- jupyter_ext/handlers/analytics_handler.py +170 -0
- jupyter_ext/handlers/benchmark_handler.py +794 -0
- jupyter_ext/handlers/eda_benchmark_handler.py +1328 -0
- jupyter_ext/handlers/rag_admin_handler.py +689 -0
- jupyter_ext/handlers/websocket.py +497 -0
- jupyter_ext/http_handlers.py +8744 -0
- jupyter_ext/idle_monitor.py +346 -0
- jupyter_ext/labextension/package.json +157 -0
- jupyter_ext/labextension/static/104.2b8f20f9d1ac2c9db01b.js +1 -0
- jupyter_ext/labextension/static/150.39fa98117cc8ca590465.js +1 -0
- jupyter_ext/labextension/static/166.386ff9b2c37ac865fdee.js +1 -0
- jupyter_ext/labextension/static/216.25cf81131bf7274dabf4.js +1 -0
- jupyter_ext/labextension/static/353.581bd1cb00746399b62e.js +1 -0
- jupyter_ext/labextension/static/367.575f9621b63c9a641aa9.js +1 -0
- jupyter_ext/labextension/static/421.11dac223a2c2da1b09f5.js +1 -0
- jupyter_ext/labextension/static/432.8513b4c6cc1d1232d165.js +2 -0
- jupyter_ext/labextension/static/432.8513b4c6cc1d1232d165.js.LICENSE.txt +19 -0
- jupyter_ext/labextension/static/439.ba5b5ef70ec6de214b8c.js +2 -0
- jupyter_ext/labextension/static/439.ba5b5ef70ec6de214b8c.js.LICENSE.txt +8 -0
- jupyter_ext/labextension/static/481.cc2dec9fa869d66f1407.js +1 -0
- jupyter_ext/labextension/static/487.0796e931d6e651482c14.js +1 -0
- jupyter_ext/labextension/static/49.d326f554b6f9ccde6580.js +1 -0
- jupyter_ext/labextension/static/541.26897ea32845659e36be.js +1 -0
- jupyter_ext/labextension/static/579.e569d3edd8391fd8fbb3.js +1 -0
- jupyter_ext/labextension/static/58.eb7cc679f7535cc8a2f1.js +2 -0
- jupyter_ext/labextension/static/58.eb7cc679f7535cc8a2f1.js.LICENSE.txt +8 -0
- jupyter_ext/labextension/static/666.d9bfdb23f98489d0a188.js +1 -0
- jupyter_ext/labextension/static/677.d266c39dbed33bc388b3.js +2 -0
- jupyter_ext/labextension/static/677.d266c39dbed33bc388b3.js.LICENSE.txt +8 -0
- jupyter_ext/labextension/static/68.26b6f59c8454adb17f20.js +204 -0
- jupyter_ext/labextension/static/694.4ac443814260057e68f7.js +1 -0
- jupyter_ext/labextension/static/791.61affb8d3480d3d60e0d.js +1 -0
- jupyter_ext/labextension/static/848.4d2db09cfbb09374b299.js +2 -0
- jupyter_ext/labextension/static/848.4d2db09cfbb09374b299.js.LICENSE.txt +9 -0
- jupyter_ext/labextension/static/868.5cfe7e1a348ec5af1b8c.js +1 -0
- jupyter_ext/labextension/static/97.ea559a5b8a9a09f17c6b.js +1 -0
- jupyter_ext/labextension/static/remoteEntry.5292fc9c6df7e7920cc8.js +1 -0
- jupyter_ext/labextension/static/style.js +4 -0
- jupyter_ext/labextension/static/third-party-licenses.json +724 -0
- jupyter_ext/log_paths.py +113 -0
- jupyter_ext/resource_usage.py +519 -0
- jupyter_ext/secrets_bootstrap.py +116 -0
- jupyter_ext/team_access.py +105 -0
- jupyter_ext/tests/__init__.py +1 -0
- jupyter_ext/tests/test_context.py +234 -0
- jupyter_ext/tests/test_factory.py +563 -0
- jupyter_ext/tests/test_file_finder.py +139 -0
- jupyter_ext/tests/test_handlers.py +58 -0
- jupyter_ext/tests/test_mcp_selftest.py +204 -0
- jupyter_ext/tests/test_mermaid_fixer.py +272 -0
- jupyter_ext/tests/test_resource_tiers.py +298 -0
- jupyter_ext/tools/__init__.py +15 -0
- jupyter_ext/tools/base.py +90 -0
- jupyter_ext/tools/context.py +268 -0
- jupyter_ext/tools/dir_complete.py +86 -0
- jupyter_ext/tools/file_finder.py +303 -0
- jupyter_ext/tools/file_tools.py +244 -0
- jupyter_ext/tools/jupyter_tools.py +296 -0
- jupyter_ext/tools/registry.py +159 -0
- jupyter_ext/tools/search_tools.py +230 -0
- jupyter_ext/tools/security_tools.py +657 -0
- jupyter_ext/tools/shell_tools.py +109 -0
- jupyter_ext/tools/user_tools.py +132 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: inference
|
|
3
|
+
description: 모델 추론 최적화. 추론 속도가 느리거나 배치 처리가 필요할 때 사용. 배치 추론, 양자화(INT8/FP16), ONNX 변환, TensorRT 가속 가이드 제공.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Model Inference Optimization Guide
|
|
7
|
+
|
|
8
|
+
모델 추론 속도를 높이고 리소스 효율을 개선하는 방법을 안내합니다.
|
|
9
|
+
|
|
10
|
+
## Resource Tiers
|
|
11
|
+
|
|
12
|
+
### TIER_SMALL: CPU only 또는 단일 추론
|
|
13
|
+
- 배치 처리 불필요
|
|
14
|
+
- ONNX Runtime CPU 권장
|
|
15
|
+
- 동적 양자화(INT8) 적용
|
|
16
|
+
|
|
17
|
+
### TIER_MEDIUM: GPU 사용, 중간 규모 추론
|
|
18
|
+
- 배치 크기: 8-64
|
|
19
|
+
- FP16 추론
|
|
20
|
+
- ONNX Runtime GPU 또는 TorchScript
|
|
21
|
+
|
|
22
|
+
### TIER_LARGE: 대규모 서빙, 고성능 필요
|
|
23
|
+
- 배치 크기: 64-256
|
|
24
|
+
- TensorRT 또는 TensorRT-LLM
|
|
25
|
+
- INT8 양자화 + 배치 최적화
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 1. 배치 추론 (Batch Inference)
|
|
30
|
+
|
|
31
|
+
### PyTorch 배치 처리
|
|
32
|
+
```python
|
|
33
|
+
import torch
|
|
34
|
+
|
|
35
|
+
model.eval()
|
|
36
|
+
|
|
37
|
+
# 개별 추론 (느림)
|
|
38
|
+
# for item in items:
|
|
39
|
+
# result = model(item)
|
|
40
|
+
|
|
41
|
+
# 배치 추론 (빠름)
|
|
42
|
+
batch = torch.stack(items) # [N, C, H, W]
|
|
43
|
+
with torch.no_grad():
|
|
44
|
+
results = model(batch)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 동적 배칭 (Dynamic Batching)
|
|
48
|
+
```python
|
|
49
|
+
def dynamic_batch_inference(items, model, max_batch=32, max_wait_ms=50):
|
|
50
|
+
"""요청을 모아서 배치 처리"""
|
|
51
|
+
import time
|
|
52
|
+
|
|
53
|
+
batch = []
|
|
54
|
+
start_time = time.time()
|
|
55
|
+
|
|
56
|
+
for item in items:
|
|
57
|
+
batch.append(item)
|
|
58
|
+
|
|
59
|
+
# 배치가 꽉 차거나 대기 시간 초과
|
|
60
|
+
if len(batch) >= max_batch or (time.time() - start_time) * 1000 > max_wait_ms:
|
|
61
|
+
batch_tensor = torch.stack(batch)
|
|
62
|
+
with torch.no_grad():
|
|
63
|
+
yield model(batch_tensor)
|
|
64
|
+
batch = []
|
|
65
|
+
start_time = time.time()
|
|
66
|
+
|
|
67
|
+
# 남은 배치 처리
|
|
68
|
+
if batch:
|
|
69
|
+
batch_tensor = torch.stack(batch)
|
|
70
|
+
with torch.no_grad():
|
|
71
|
+
yield model(batch_tensor)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 최적 배치 크기
|
|
75
|
+
| GPU Memory | 권장 Batch Size | 참고 |
|
|
76
|
+
|------------|----------------|------|
|
|
77
|
+
| 4GB | 8-16 | 작은 모델 |
|
|
78
|
+
| 8GB | 16-32 | 중간 모델 |
|
|
79
|
+
| 16GB | 32-64 | 큰 모델 |
|
|
80
|
+
| 24GB+ | 64-128 | 대형 모델 |
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 2. 양자화 (Quantization)
|
|
85
|
+
|
|
86
|
+
### PyTorch 동적 양자화 (가장 간단)
|
|
87
|
+
```python
|
|
88
|
+
import torch
|
|
89
|
+
|
|
90
|
+
# 동적 양자화: 추론 시 가중치를 INT8로 변환
|
|
91
|
+
model_int8 = torch.quantization.quantize_dynamic(
|
|
92
|
+
model,
|
|
93
|
+
{torch.nn.Linear, torch.nn.LSTM}, # 양자화할 레이어 타입
|
|
94
|
+
dtype=torch.qint8
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# 모델 크기 약 4배 감소
|
|
98
|
+
# 추론 속도 2-4배 향상 (CPU)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### PyTorch 정적 양자화 (더 빠름)
|
|
102
|
+
```python
|
|
103
|
+
import torch
|
|
104
|
+
from torch.quantization import get_default_qconfig, prepare, convert
|
|
105
|
+
|
|
106
|
+
# 1. 양자화 설정
|
|
107
|
+
model.qconfig = get_default_qconfig('fbgemm') # CPU용
|
|
108
|
+
# model.qconfig = get_default_qconfig('qnnpack') # 모바일용
|
|
109
|
+
|
|
110
|
+
# 2. 준비 (observer 삽입)
|
|
111
|
+
model_prepared = prepare(model)
|
|
112
|
+
|
|
113
|
+
# 3. 캘리브레이션 (대표 데이터로)
|
|
114
|
+
with torch.no_grad():
|
|
115
|
+
for data in calibration_loader:
|
|
116
|
+
model_prepared(data)
|
|
117
|
+
|
|
118
|
+
# 4. 변환
|
|
119
|
+
model_quantized = convert(model_prepared)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 양자화 효과
|
|
123
|
+
| Precision | Model Size | Speed (CPU) | Speed (GPU) | Accuracy |
|
|
124
|
+
|-----------|------------|-------------|-------------|----------|
|
|
125
|
+
| FP32 | 1x | 1x | 1x | Baseline |
|
|
126
|
+
| FP16 | 0.5x | 1x | 1.5-2x | ~동일 |
|
|
127
|
+
| INT8 | 0.25x | 2-4x | 2-3x | 약간 감소 |
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## 3. ONNX 변환 및 최적화
|
|
132
|
+
|
|
133
|
+
### PyTorch → ONNX 변환
|
|
134
|
+
```python
|
|
135
|
+
import torch
|
|
136
|
+
|
|
137
|
+
model.eval()
|
|
138
|
+
dummy_input = torch.randn(1, 3, 224, 224)
|
|
139
|
+
|
|
140
|
+
torch.onnx.export(
|
|
141
|
+
model,
|
|
142
|
+
dummy_input,
|
|
143
|
+
"model.onnx",
|
|
144
|
+
input_names=["input"],
|
|
145
|
+
output_names=["output"],
|
|
146
|
+
dynamic_axes={
|
|
147
|
+
"input": {0: "batch_size"}, # 동적 배치 지원
|
|
148
|
+
"output": {0: "batch_size"}
|
|
149
|
+
},
|
|
150
|
+
opset_version=17 # 최신 opset 권장
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### ONNX Runtime 추론
|
|
155
|
+
```python
|
|
156
|
+
import onnxruntime as ort
|
|
157
|
+
import numpy as np
|
|
158
|
+
|
|
159
|
+
# CPU 세션
|
|
160
|
+
session = ort.InferenceSession(
|
|
161
|
+
"model.onnx",
|
|
162
|
+
providers=['CPUExecutionProvider']
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# GPU 세션 (CUDA)
|
|
166
|
+
session = ort.InferenceSession(
|
|
167
|
+
"model.onnx",
|
|
168
|
+
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# 추론
|
|
172
|
+
input_name = session.get_inputs()[0].name
|
|
173
|
+
output = session.run(None, {input_name: input_data.numpy()})
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### ONNX 모델 최적화
|
|
177
|
+
```python
|
|
178
|
+
import onnx
|
|
179
|
+
from onnxruntime.transformers import optimizer
|
|
180
|
+
|
|
181
|
+
# 자동 최적화 (Transformer 모델용)
|
|
182
|
+
optimized_model = optimizer.optimize_model(
|
|
183
|
+
"model.onnx",
|
|
184
|
+
model_type='bert', # 'gpt2', 'bert', 'vit' 등
|
|
185
|
+
num_heads=12,
|
|
186
|
+
hidden_size=768
|
|
187
|
+
)
|
|
188
|
+
optimized_model.save_model_to_file("model_optimized.onnx")
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### ONNX 속도 향상
|
|
192
|
+
- PyTorch → ONNX: **CPU 최대 3배**, GPU 1.5-2배 속도 향상
|
|
193
|
+
- 추가 그래프 최적화: 10-30% 추가 향상
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 4. TensorRT 가속
|
|
198
|
+
|
|
199
|
+
### ONNX → TensorRT 변환
|
|
200
|
+
```python
|
|
201
|
+
import tensorrt as trt
|
|
202
|
+
|
|
203
|
+
logger = trt.Logger(trt.Logger.WARNING)
|
|
204
|
+
builder = trt.Builder(logger)
|
|
205
|
+
network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))
|
|
206
|
+
parser = trt.OnnxParser(network, logger)
|
|
207
|
+
|
|
208
|
+
# ONNX 모델 파싱
|
|
209
|
+
with open("model.onnx", "rb") as f:
|
|
210
|
+
parser.parse(f.read())
|
|
211
|
+
|
|
212
|
+
# 빌더 설정
|
|
213
|
+
config = builder.create_builder_config()
|
|
214
|
+
config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, 1 << 30) # 1GB
|
|
215
|
+
|
|
216
|
+
# FP16 활성화 (Volta 이상)
|
|
217
|
+
config.set_flag(trt.BuilderFlag.FP16)
|
|
218
|
+
|
|
219
|
+
# INT8 활성화 (캘리브레이션 필요)
|
|
220
|
+
# config.set_flag(trt.BuilderFlag.INT8)
|
|
221
|
+
# config.int8_calibrator = calibrator
|
|
222
|
+
|
|
223
|
+
# 엔진 빌드
|
|
224
|
+
engine = builder.build_serialized_network(network, config)
|
|
225
|
+
|
|
226
|
+
# 저장
|
|
227
|
+
with open("model.trt", "wb") as f:
|
|
228
|
+
f.write(engine)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Torch-TensorRT (더 간단)
|
|
232
|
+
```python
|
|
233
|
+
import torch
|
|
234
|
+
import torch_tensorrt
|
|
235
|
+
|
|
236
|
+
model = model.eval().cuda()
|
|
237
|
+
|
|
238
|
+
# TensorRT 컴파일
|
|
239
|
+
trt_model = torch_tensorrt.compile(
|
|
240
|
+
model,
|
|
241
|
+
inputs=[
|
|
242
|
+
torch_tensorrt.Input(
|
|
243
|
+
min_shape=[1, 3, 224, 224],
|
|
244
|
+
opt_shape=[8, 3, 224, 224],
|
|
245
|
+
max_shape=[32, 3, 224, 224],
|
|
246
|
+
dtype=torch.float16
|
|
247
|
+
)
|
|
248
|
+
],
|
|
249
|
+
enabled_precisions={torch.float16},
|
|
250
|
+
workspace_size=1 << 30
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
# 추론
|
|
254
|
+
with torch.no_grad():
|
|
255
|
+
output = trt_model(input_tensor.cuda().half())
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### TensorRT 속도 향상
|
|
259
|
+
- PyTorch → TensorRT: **GPU 최대 5배** 속도 향상
|
|
260
|
+
- FP16 + TensorRT: 3-4배 일반적
|
|
261
|
+
- INT8 + TensorRT: 5-8배 가능
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## 5. Hugging Face Transformers 최적화
|
|
266
|
+
|
|
267
|
+
### 기본 최적화
|
|
268
|
+
```python
|
|
269
|
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
270
|
+
import torch
|
|
271
|
+
|
|
272
|
+
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
|
273
|
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
|
274
|
+
|
|
275
|
+
# FP16 추론
|
|
276
|
+
model = model.half().cuda()
|
|
277
|
+
|
|
278
|
+
# BetterTransformer (Flash Attention)
|
|
279
|
+
model = model.to_bettertransformer()
|
|
280
|
+
|
|
281
|
+
# 추론
|
|
282
|
+
with torch.no_grad():
|
|
283
|
+
inputs = tokenizer(texts, return_tensors="pt", padding=True).to("cuda")
|
|
284
|
+
outputs = model(**inputs)
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Optimum + ONNX Runtime
|
|
288
|
+
```python
|
|
289
|
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
|
290
|
+
from transformers import AutoTokenizer
|
|
291
|
+
|
|
292
|
+
# ONNX 변환 및 로드
|
|
293
|
+
model = ORTModelForSequenceClassification.from_pretrained(
|
|
294
|
+
"bert-base-uncased",
|
|
295
|
+
export=True,
|
|
296
|
+
provider="CUDAExecutionProvider"
|
|
297
|
+
)
|
|
298
|
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
|
299
|
+
|
|
300
|
+
# 추론
|
|
301
|
+
inputs = tokenizer(texts, return_tensors="pt", padding=True)
|
|
302
|
+
outputs = model(**inputs)
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## 6. 추론 서버 최적화
|
|
308
|
+
|
|
309
|
+
### torch.inference_mode (torch.no_grad 보다 빠름)
|
|
310
|
+
```python
|
|
311
|
+
# torch.no_grad() 대신 사용
|
|
312
|
+
with torch.inference_mode():
|
|
313
|
+
output = model(input_tensor)
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### CUDA Graphs (반복 추론용)
|
|
317
|
+
```python
|
|
318
|
+
import torch
|
|
319
|
+
|
|
320
|
+
# 워밍업
|
|
321
|
+
s = torch.cuda.Stream()
|
|
322
|
+
s.wait_stream(torch.cuda.current_stream())
|
|
323
|
+
with torch.cuda.stream(s):
|
|
324
|
+
for _ in range(3):
|
|
325
|
+
output = model(static_input)
|
|
326
|
+
torch.cuda.current_stream().wait_stream(s)
|
|
327
|
+
|
|
328
|
+
# 그래프 캡처
|
|
329
|
+
g = torch.cuda.CUDAGraph()
|
|
330
|
+
with torch.cuda.graph(g):
|
|
331
|
+
static_output = model(static_input)
|
|
332
|
+
|
|
333
|
+
# 추론 (그래프 재생)
|
|
334
|
+
g.replay()
|
|
335
|
+
result = static_output.clone()
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
## 7. 메모리 최적화
|
|
341
|
+
|
|
342
|
+
```python
|
|
343
|
+
import torch
|
|
344
|
+
|
|
345
|
+
# 추론 후 캐시 정리
|
|
346
|
+
torch.cuda.empty_cache()
|
|
347
|
+
|
|
348
|
+
# 그래디언트 비활성화 (메모리 절약)
|
|
349
|
+
for param in model.parameters():
|
|
350
|
+
param.requires_grad = False
|
|
351
|
+
|
|
352
|
+
# 스트리밍 추론 (대용량 데이터)
|
|
353
|
+
def stream_inference(dataloader, model):
|
|
354
|
+
model.eval()
|
|
355
|
+
with torch.inference_mode():
|
|
356
|
+
for batch in dataloader:
|
|
357
|
+
yield model(batch.cuda())
|
|
358
|
+
torch.cuda.empty_cache() # 배치마다 정리
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
## Quick Reference: 최적화 선택 가이드
|
|
364
|
+
|
|
365
|
+
| 상황 | 권장 방법 | 예상 향상 |
|
|
366
|
+
|------|----------|----------|
|
|
367
|
+
| CPU 추론 | ONNX Runtime + INT8 | 3-4x |
|
|
368
|
+
| GPU 추론 (간단) | FP16 + torch.compile | 1.5-2x |
|
|
369
|
+
| GPU 추론 (최대 성능) | TensorRT + FP16/INT8 | 3-5x |
|
|
370
|
+
| Transformer 모델 | BetterTransformer + ONNX | 2-3x |
|
|
371
|
+
| 대량 추론 | 배치 처리 + TensorRT | 5-10x |
|
|
372
|
+
|
|
373
|
+
### 빠른 적용 체크리스트
|
|
374
|
+
```python
|
|
375
|
+
# 1. 기본 최적화
|
|
376
|
+
model.eval()
|
|
377
|
+
with torch.inference_mode():
|
|
378
|
+
...
|
|
379
|
+
|
|
380
|
+
# 2. FP16 (GPU)
|
|
381
|
+
model = model.half().cuda()
|
|
382
|
+
|
|
383
|
+
# 3. torch.compile (PyTorch 2.0+)
|
|
384
|
+
model = torch.compile(model, mode="reduce-overhead")
|
|
385
|
+
|
|
386
|
+
# 4. ONNX 변환 (더 빠른 추론 필요시)
|
|
387
|
+
torch.onnx.export(model, ...)
|
|
388
|
+
|
|
389
|
+
# 5. TensorRT (최대 성능 필요시)
|
|
390
|
+
torch_tensorrt.compile(model, ...)
|
|
391
|
+
```
|
|
392
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lambda_function_development
|
|
3
|
+
description: 'Lambda Development Workflow. 이런 요청에 쓴다: lambda, serverless, sam, handler, api gateway, 람다'
|
|
4
|
+
---
|
|
5
|
+
## Lambda Development Workflow
|
|
6
|
+
1. **Handler**: def handler(event, context)
|
|
7
|
+
2. **SAM**: template.yaml with Resources
|
|
8
|
+
3. **Local Test**: sam local invoke -e event.json
|
|
9
|
+
4. **Unit Test**: pytest with mocked event/context
|
|
10
|
+
|
|
11
|
+
## Guidelines
|
|
12
|
+
- handler(event, context) 시그니처 준수
|
|
13
|
+
- event/context 타입 힌트 포함
|
|
14
|
+
- 비즈니스 로직은 별도 함수로 분리 (테스트 용이성)
|
|
15
|
+
- 적절한 에러 핸들링 및 로깅 (logger = logging.getLogger())
|
|
16
|
+
- 환경변수는 os.environ으로 참조
|
|
17
|
+
- Cold start 최적화: SDK 클라이언트는 핸들러 밖에서 생성
|
|
18
|
+
- SAM template 요청 시 template.yaml 포함
|
|
19
|
+
- 유닛테스트: pytest + mocked event/context
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: model_comparison
|
|
3
|
+
description: 'Model Comparison Template. 이런 요청에 쓴다: 모델 비교, model comparison, 어떤 모델, best model, 모델 선택'
|
|
4
|
+
---
|
|
5
|
+
## Model Comparison Template
|
|
6
|
+
1. **Baseline**: Simple model (DummyClassifier/DummyRegressor)
|
|
7
|
+
2. **Candidates**: Train LogisticRegression, RandomForest, GradientBoosting, etc.
|
|
8
|
+
3. **Cross-Validation**: `cross_val_score()` with appropriate cv strategy
|
|
9
|
+
4. **Metrics Table**: Compare accuracy, F1, precision, recall (or RMSE, MAE, R²)
|
|
10
|
+
5. **Statistical Test**: Paired t-test or Wilcoxon on CV scores
|
|
11
|
+
6. **Selection**: Best model → hyperparameter tuning with GridSearchCV/Optuna
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: model_explainability
|
|
3
|
+
description: 'Model Explainability Template. 이런 요청에 쓴다: shap, lime, explainability, interpretability, 모델 해석, 설명 가능'
|
|
4
|
+
---
|
|
5
|
+
## Model Explainability Template
|
|
6
|
+
1. **SHAP Setup**: `explainer = shap.TreeExplainer(model)` or `shap.KernelExplainer`
|
|
7
|
+
2. **SHAP Values**: `shap_values = explainer.shap_values(X_test)`
|
|
8
|
+
3. **Summary Plot**: `shap.summary_plot(shap_values, X_test)` — global feature impact
|
|
9
|
+
4. **Dependence Plot**: `shap.dependence_plot("feature", shap_values, X_test)`
|
|
10
|
+
5. **LIME**: `lime.lime_tabular.LimeTabularExplainer` for local instance explanation
|
|
11
|
+
6. **Force Plot**: `shap.force_plot()` for single prediction breakdown
|
|
12
|
+
7. **Report**: Summarize top drivers, interaction effects, and model behavior
|