상세 컨텐츠

본문 제목

(AI 친해지기) 무료 Colab으로 AI 두뇌 실험하기 빠른 답 vs 깊은 답

AI & Business (인공지능과 경영)

by 꿈공장장100 2025. 8. 18. 19:00

본문

반응형

📌 AI도 사람처럼 깊게 생각할 수 있을까요?

이번에는 무료 Colab 환경에서 GPT-OSS 모델을 직접 실행해 보며,

“빠른 답 vs 깊은 답” 실험을 통해 AI의 사고 깊이(Reasoning Effort)를 체험합니다.


🧠 이번 영상에서 다루는 내용

1. 무료 Colab에서 GPT-OSS 실행하기

2. Reasoning Effort 실험: Low / Medium / High 비교

3. 쉬운 수학 문제로 빠른 답 vs 깊은 답 직접 확인

 

🚀 시청 포인트

* 비전문가도 쉽게 따라할 수 있는 실습

* “AI도 깊게 생각할까?”라는 흥미로운 질문 체험

* Colab 무료 환경에서 누구나 가능

https://youtu.be/C_kGjQitLDU

 

- YouTube

 

www.youtube.com

 

🔗 자료 & 링크

* 코드 요약 노트북:

# 1단계: Colab 준비 및 설치
%%capture
!pip install --upgrade -qqq uv

try:
    import numpy
    install_numpy = f"numpy=={numpy.__version__}"
except:
    install_numpy = "numpy"

!uv pip install -qqq \
    "torch>=2.8.0" "triton>=3.4.0" {install_numpy} \
    "unsloth_zoo[base] @ git+https://github.com/unslothai/unsloth-zoo" \
    "unsloth[base] @ git+https://github.com/unslothai/unsloth" \
    torchvision bitsandbytes \
    git+https://github.com/huggingface/transformers \
    git+https://github.com/triton-lang/triton.git@05b2c186c1b6c9a08375389d5efe9cb4c401c075#subdirectory=python/triton_kernels


# 2단계: 모델 불러오기
from unsloth import FastLanguageModel
import torch

max_seq_length = 1024
dtype = None

fourbit_models = [
    "unsloth/gpt-oss-20b-unsloth-bnb-4bit",
    "unsloth/gpt-oss-120b-unsloth-bnb-4bit",
    "unsloth/gpt-oss-20b",
    "unsloth/gpt-oss-120b",
]

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/gpt-oss-20b",
    dtype = dtype,
    max_seq_length = max_seq_length,
    load_in_4bit = True,
    full_finetuning = False,
)


# 3단계: LoRA 어댑터 추가
model = FastLanguageModel.get_peft_model(
    model,
    r = 8,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj"],
    lora_alpha = 16,
    lora_dropout = 0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,
    loftq_config = None,
)


# 4단계: Reasoning Effort 실습
from transformers import TextStreamer

# Low
messages = [{"role": "user", "content": "Solve x^5 + 3x^4 - 10 = 3."}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True,
    return_tensors="pt", return_dict=True, reasoning_effort="low").to(model.device)
_ = model.generate(**inputs, max_new_tokens=64, streamer=TextStreamer(tokenizer))

# Medium
messages = [{"role": "user", "content": "Solve x^5 + 3x^4 - 10 = 3."}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True,
    return_tensors="pt", return_dict=True, reasoning_effort="medium").to(model.device)
_ = model.generate(**inputs, max_new_tokens=64, streamer=TextStreamer(tokenizer))

# High
messages = [{"role": "user", "content": "Solve x^5 + 3x^4 - 10 = 3."}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True,
    return_tensors="pt", return_dict=True, reasoning_effort="high").to(model.device)
_ = model.generate(**inputs, max_new_tokens=64, streamer=TextStreamer(tokenizer))


# 5단계: 데이터 준비
from datasets import load_dataset
from unsloth.chat_templates import standardize_sharegpt

def formatting_prompts_func(examples):
    convos = examples["messages"]
    texts = [tokenizer.apply_chat_template(convo, tokenize=False,
        add_generation_prompt=False) for convo in convos]
    return { "text": texts }

dataset = load_dataset("HuggingFaceH4/Multilingual-Thinking", split="train")
dataset = standardize_sharegpt(dataset)
dataset = dataset.map(formatting_prompts_func, batched=True)
print(dataset[0]['text'])


# 6단계: 모델 학습
from trl import SFTConfig, SFTTrainer

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    args = SFTConfig(
        per_device_train_batch_size = 1,
        gradient_accumulation_steps = 4,
        warmup_steps = 5,
        max_steps = 30,
        learning_rate = 2e-4,
        logging_steps = 1,
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
        report_to = "none",
    ),
)

gpu_stats = torch.cuda.get_device_properties(0)
start_gpu_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)
print(f"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.")
print(f"{start_gpu_memory} GB of memory reserved.")

trainer_stats = trainer.train()
used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
used_memory_for_lora = round(used_memory - start_gpu_memory, 3)
print(f"{trainer_stats.metrics['train_runtime']} seconds used for training.")
print(f"Peak reserved memory = {used_memory} GB.")
print(f"Peak reserved memory for training = {used_memory_for_lora} GB.")


# 7단계: 추론 (Inference)
messages = [
    {"role": "system", "content": "reasoning language: French\n\nYou are a helpful assistant that can solve mathematical problems."},
    {"role": "user", "content": "Solve x^5 + 3x^4 - 10 = 3."},
]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True,
    return_tensors="pt", return_dict=True, reasoning_effort="medium").to(model.device)
_ = model.generate(**inputs, max_new_tokens=64, streamer=TextStreamer(tokenizer))


# 8단계: 모델 저장
model.save_pretrained("finetuned_model")

 

* Unsloth 공식 문서: [https://docs.unsloth.ai](https://docs.unsloth.ai)

* HuggingFace 데이터셋: [https://huggingface.co/datasets/HuggingFaceH4/Multilingual-Thinking]

 

🙌 이런 분께 추천해요

* AI가 어떻게 추론을 다르게 하는지 궁금한 분

* 무료 환경에서 직접 AI를 체험해보고 싶은 분

* AI에 거부감 없이 쉽게 친해지고 싶은 분

 

 

관련글 더보기