diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_infer_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_infer_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..fa726e1f98b4c68866f4437e1677b88253a449c9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_infer_gpu_one.md @@ -0,0 +1,297 @@ + + +# GPU + +GPUs are the standard hardware for machine learning because they're optimized for memory bandwidth and parallelism. With the increasing sizes of modern models, it's more important than ever to make sure GPUs are capable of efficiently handling and delivering the best possible performance. + +This guide will demonstrate a few ways to optimize inference on a GPU. The optimization methods shown below can be combined with each other to achieve even better performance, and they also work for distributed GPUs. + +## bitsandbytes + +[bitsandbytes](https://hf.co/docs/bitsandbytes/index) is a quantization library that supports 8-bit and 4-bit quantization. Quantization represents weights in a lower precision compared to the original full precision format. It reduces memory requirements and makes it easier to fit large model into memory. + +Make sure bitsandbytes and Accelerate are installed first. + +```bash +pip install bitsandbytes accelerate +``` + + + + +For text generation with 8-bit quantization, you should use [`~GenerationMixin.generate`] instead of the high-level [`Pipeline`] API. The [`Pipeline`] returns slower performance because it isn't optimized for 8-bit models, and some sampling strategies (nucleus sampling) also aren't supported. + +Set up a [`BitsAndBytesConfig`] and set `load_in_8bit=True` to load a model in 8-bit precision. The [`BitsAndBytesConfig`] is passed to the `quantization_config` parameter in [`~PreTrainedModel.from_pretrained`]. + +Allow Accelerate to automatically distribute the model across your available hardware by setting [device_map="auto"](https://hf.co/docs/accelerate/concept_guides/big_model_inference#designing-a-device-map). + +Place all inputs on the same device as the model. + +```py +from transformers import BitsAndBytesConfig, AutoTokenizer, AutoModelForCausalLM + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B") +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", quantization_config=quantization_config) + +prompt = "Hello, my llama is cute" +inputs = tokenizer(prompt, return_tensors="pt").to("cuda") +generated_ids = model.generate(**inputs) +outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +``` + +For distributed setups, use the `max_memory` parameter to create a mapping of the amount of memory to allocate to each GPU. The example below distributes 16GB of memory to the first GPU and 16GB of memory to the second GPU. + +```py +max_memory_mapping = {0: "16GB", 1: "16GB"} +model_8bit = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", device_map="auto", quantization_config=quantization_config, max_memory=max_memory_mapping +) +``` + +Learn in more detail the concepts underlying 8-bit quantization in the [Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using Hugging Face Transformers, Accelerate and bitsandbytes](https://hf.co/blog/hf-bitsandbytes-integration) blog post. + + + + +Set up a [`BitsAndBytesConfig`] and set `load_in_4bit=True` to load a model in 4-bit precision. The [`BitsAndBytesConfig`] is passed to the `quantization_config` parameter in [`~PreTrainedModel.from_pretrained`]. + +Allow Accelerate to automatically distribute the model across your available hardware by setting `device_map=โ€œautoโ€`. + +Place all inputs on the same device as the model. + +```py +from transformers import BitsAndBytesConfig, AutoTokenizer, AutoModelForCausalLM + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) +tokenizer = AutoTokenizer("meta-llama/Llama-3.1-8B") +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", quantization_config=quantization_config) + +prompt = "Hello, my llama is cute" +inputs = tokenizer(prompt, return_tensors="pt").to(model_8bit.device) +generated_ids = model_8bit.generate(**inputs) +outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +``` + +For distributed setups, use the `max_memory` parameter to create a mapping of the amount of memory to allocate to each GPU. The example below distributes 16GB of memory to the first GPU and 16GB of memory to the second GPU. + +```py +max_memory_mapping = {0: "16GB", 1: "16GB"} +model_4bit = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", device_map="auto", quantization_config=quantization_config, max_memory=max_memory_mapping +) +``` + + + + +## Optimum + +[Optimum](https://hf.co/docs/optimum/en/index) is a Hugging Face library focused on optimizing model performance across various hardware. It supports [ONNX Runtime](https://onnxruntime.ai/docs/) (ORT), a model accelerator, for a wide range of hardware and frameworks including NVIDIA GPUs and AMD GPUs that use the [ROCm](https://www.amd.com/en/products/software/rocm.html) stack. + +ORT uses optimization techniques that fuse common operations into a single node and constant folding to reduce the number of computations. ORT also places the most computationally intensive operations on the GPU and the rest on the CPU to intelligently distribute the workload between the two devices. + +Optimum provides the [`~optimum.onnxruntime.ORTModel`] class for loading ONNX models. Set the `provider` parameter according to the table below. + +| provider | hardware | +|---|---| +| [CUDAExecutionProvider](https://hf.co/docs/optimum/main/en/onnxruntime/usage_guides/gpu#cudaexecutionprovider) | CUDA-enabled GPUs | +| [ROCMExecutionProvider](https://hf.co/docs/optimum/onnxruntime/usage_guides/amdgpu) | AMD Instinct, Radeon Pro, Radeon GPUs | +| [TensorrtExecutionProvider](https://hf.co/docs/optimum/onnxruntime/usage_guides/gpu#tensorrtexecutionprovider) | TensorRT | + +For example, load the [distilbert/distilbert-base-uncased-finetuned-sst-2-english](https://hf.co/optimum/roberta-base-squad2) checkpoint for sequence classification. This checkpoint contains a [model.onnx](https://hf.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english/blob/main/onnx/model.onnx) file. If a checkpoint doesn't have a `model.onnx` file, set `export=True` to convert a checkpoint on the fly to the ONNX format. + +```py +from optimum.onnxruntime import ORTModelForSequenceClassification + +ort_model = ORTModelForSequenceClassification.from_pretrained( + "distilbert/distilbert-base-uncased-finetuned-sst-2-english", + #export=True, + provider="CUDAExecutionProvider", +) +``` + +Now you can use the model for inference in a [`Pipeline`]. + +```py +from optimum.pipelines import pipeline +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased-finetuned-sst-2-english") +pipeline = pipeline(task="text-classification", model=ort_model, tokenizer=tokenizer, device="cuda:0") +result = pipeline("Both the music and visual were astounding, not to mention the actors performance.") +``` + +Learn more details about using ORT with Optimum in the [Accelerated inference on NVIDIA GPUs](https://hf.co/docs/optimum/onnxruntime/usage_guides/gpu#accelerated-inference-on-nvidia-gpus) and [Accelerated inference on AMD GPUs](https://hf.co/docs/optimum/onnxruntime/usage_guides/amdgpu#accelerated-inference-on-amd-gpus) guides. + +### BetterTransformer + +[BetterTransformer](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/) is a *fastpath* execution of specialized Transformers functions directly on the hardware level such as a GPU. There are two main components of the fastpath execution. + +- fusing multiple operations into a single kernel for faster and more efficient execution +- skipping unnecessary computation of padding tokens with nested tensors + +> [!WARNING] +> Some BetterTransformer features are being upstreamed to Transformers with default support for native [torch.nn.functional.scaled_dot_product_attention](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) (SDPA). BetterTransformer has a wider coverage than the Transformers SDPA integration, but you can expect more and more architectures to natively support SDPA in Transformers. + +BetterTransformer is available through Optimum with [`~PreTrainedModel.to_bettertransformer`]. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("bigscience/bloom") +model = model.to_bettertransformer() +``` + +Call [`~PreTrainedModel.reverse_bettertransformer`] and save it first to return the model to the original Transformers model. + +```py +model = model.reverse_bettertransformer() +model.save_pretrained("saved_model") +``` + +Refer to the benchmarks in [Out of the box acceleration and memory savings of ๐Ÿค— decoder models with PyTorch 2.0](https://pytorch.org/blog/out-of-the-box-acceleration/) for BetterTransformer and scaled dot product attention performance. The [BetterTransformer](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2) blog post also discusses fastpath execution in greater detail if you're interested in learning more. + +## Scaled dot product attention (SDPA) + +PyTorch's [torch.nn.functional.scaled_dot_product_attention](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) (SDPA) is a native implementation of the scaled dot product attention mechanism. SDPA is a more efficient and optimized version of the attention mechanism used in transformer models. + +There are three supported implementations available. + +- [FlashAttention2](https://github.com/Dao-AILab/flash-attention) only supports models with the fp16 or bf16 torch type. Make sure to cast your model to the appropriate type first. +- [xFormers](https://github.com/facebookresearch/xformers) or Memory-Efficient Attention is able to support models with the fp32 torch type. +- C++ implementation of scaled dot product attention + +SDPA is used by default for PyTorch v2.1.1. and greater when an implementation is available. You could explicitly enable SDPA by setting `attn_implementation="sdpa"` in [`~PreTrainedModel.from_pretrained`] though. Certain attention parameters, such as `head_mask` and `output_attentions=True`, are unsupported and returns a warning that Transformers will fall back to the (slower) eager implementation. + +Refer to the [AttentionInterface](./attention_interface) guide to learn how to change the attention implementation after loading a model. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", attn_implementation="sdpa") + +# Change the model's attention dynamically after loading it +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto") +model.set_attention_implementation("sdpa") +``` + +SDPA selects the most performant implementation available, but you can also explicitly select an implementation with [torch.nn.attention.sdpa_kernel](https://pytorch.org/docs/master/backends.html#torch.backends.cuda.sdp_kernel) as a context manager. The example below shows how to enable the FlashAttention2 implementation with `enable_flash=True`. + +```py +import torch +from torch.nn.attention import SDPBackend, sdpa_kernel +from transformers import AutoModelForCausalLM, AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B") +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto").to("cuda") + +input_text = "Hello, my llama is cute" +inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + +with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + outputs = model.generate(**inputs) + +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` + +If you encounter the following `RuntimeError`, try installing the nightly version of PyTorch which has broader coverage for FlashAttention. + +```bash +RuntimeError: No available kernel. Aborting execution. + +pip3 install -U --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118 +``` + +## FlashAttention + +[FlashAttention](https://github.com/Dao-AILab/flash-attention) is also available as a standalone package. It can significantly speed up inference by: + +1. additionally parallelizing the attention computation over sequence length +2. partitioning the work between GPU threads to reduce communication and shared memory reads/writes between them + +Install FlashAttention first for the hardware you're using. + + + + +```bash +pip install flash-attn --no-build-isolation +``` + + + + +FlashAttention2 support is currently limited to Instinct MI210, Instinct MI250 and Instinct MI300. We strongly suggest running this [Dockerfile](https://github.com/huggingface/optimum-amd/tree/main/docker/transformers-pytorch-amd-gpu-flash/Dockerfile) for FlashAttention2 on AMD GPUs. + + + + +Enable FlashAttention2 by setting `attn_implementation="flash_attention_2"` in [`~PreTrainedModel.from_pretrained`] or by setting `model.set_attention_implementation("flash_attention_2")` to dynamically update the [attention interface](./attention_interface). FlashAttention2 is only supported for models with the fp16 or bf16 torch type. Make sure to cast your model to the appropriate data type first. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2") +``` + +### Benchmarks + +FlashAttention2 speeds up inference considerably especially for inputs with long sequences. However, since FlashAttention2 doesn't support computing attention scores with padding tokens, you must manually pad and unpad the attention scores for batched inference if a sequence contains padding tokens. The downside is batched generation is slower with padding tokens. + + + + +With a relatively small sequence length, a single forward pass creates overhead leading to a small speed up. The graph below shows the expected speed up for a single forward pass with [meta-llama/Llama-7b-hf](https://hf.co/meta-llama/Llama-7b-hf) with padding. + +
+ +
+ +
+ + +You can train on much longer sequence lengths without running into out-of-memory issues with FlashAttention2, and potentially reduce memory usage up to 20x. The speed up benefits are even better. The graph below shows the expected speed up for a single forward pass with [meta-llama/Llama-7b-hf](https://hf.co/meta-llama/Llama-7b-hf) with padding on a longer sequence length. + +
+ +
+ +
+
+ +To avoid this slowdown, use FlashAttention2 without padding tokens in the sequence during training. Pack the dataset or concatenate sequences until reaching the maximum sequence length. + + + + +The graph below shows the expected speed up for a single forward pass with [tiiuae/falcon-7b](https://hf.co/tiiuae/falcon-7b) with a sequence length of 4096 and various batch sizes without padding tokens. + +
+ +
+ +
+ + +The graph below shows the expected speed up for a single forward pass with [meta-llama/Llama-7b-hf](https://hf.co/meta-llama/Llama-7b-hf) with a sequence length of 4096 and various batch sizes without padding tokens. + +
+ +
+ +
+
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_torch_compile.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_torch_compile.md new file mode 100644 index 0000000000000000000000000000000000000000..e7bef363e03936fd76bbe9253384a2eb80cfc3ca --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_torch_compile.md @@ -0,0 +1,71 @@ + + +# torch.compile + +[torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) compiles PyTorch code into optimized kernels that significantly speed up inference. This feature relies on [TorchDynamo](https://pytorch.org/docs/stable/torch.compiler_dynamo_overview.html) to compile the code into graphs and [TorchInductor](https://dev-discuss.pytorch.org/t/torchinductor-a-pytorch-native-compiler-with-define-by-run-ir-and-symbolic-shapes/747) to further compile the graphs into optimized kernels. It is a powerful optimization tool, and in many cases, only requires adding a single line of code. + +Wrap a model with torch.compile to compile and return an optimized model. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") +compiled_model = torch.compile(model) +``` + +> [!TIP] +> The initial call to torch.compile is slow because the model needs to be compiled. Subsequent calls to the compiled model are much faster because it doesn't need to compile again. + +There are several parameters to customize the compilation process. Two of the more important ones are listed below. For a full list of parameters, refer to the torch.compile [documentation](https://pytorch.org/docs/stable/generated/torch.compile.html). + +## Modes + +The `mode` parameter offers several performance options for compiling. Try different modes to see which one works best for your use case. + +- `default` is a balanced option between speed and memory. +- `reduce-overhead` reduces the Python overhead at the expense of a little more memory, but it can be faster. +- `max-autotune` offers the fastest speed, but compilation takes longer. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") +compiled_model = torch.compile(model, mode="reduce-overhead") +``` + +## Fullgraph + +Fullgraph attempts to compile the entire model into a single graph to maximize performance. torch.compile raises an error if it encounters a graph break, which means it can't compile the model into a single graph. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") +compiled_model = torch.compile(model, mode="reduce-overhead", fullgraph=True) +``` + +## Benchmarks + +Refer to the table below for performance benchmarks comparing the mean inference time in milliseconds with torch.compile enabled and disabled across various GPUs and batch sizes on the same image for different vision tasks. + +Select **Subset** in the table below to switch between different GPUs, as well as benchmarks on [PyTorch nightly](https://download.pytorch.org/whl/nightly/cu118) 2.1.0dev and torch.compile with `reduce-overhead` mode enabled. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..29641a99843fe74d7b86b0ff9ce2603a744da10a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu.md @@ -0,0 +1,52 @@ + + +# CPU + +A modern CPU is capable of efficiently training large models by leveraging the underlying optimizations built into the hardware and training on fp16 or bf16 data types. + +This guide focuses on how to train large models on an Intel CPU using mixed precision. AMP is enabled for CPU backends training with PyTorch. + +[`Trainer`] supports AMP training with CPU by adding the `--use_cpu`, and `--bf16` parameters. The example below demonstrates the [run_qa.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) script. + +```bash +python run_qa.py \ + --model_name_or_path google-bert/bert-base-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --bf16 \ + --use_cpu +``` + +These parameters can also be added to [`TrainingArguments`] as shown below. + +```py +training_args = TrainingArguments( + output_dir="./outputs", + bf16=True, + use_cpu=True, +) +``` + +## Resources + +Learn more about training on Intel CPUs in the [Accelerating PyTorch Transformers with Intel Sapphire Rapids](https://huggingface.co/blog/intel-sapphire-rapids) blog post. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..0de9f895a78ad86e1ca0c388989de1ded3690ea5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_cpu_many.md @@ -0,0 +1,274 @@ + + +# Distributed CPUs + +CPUs are commonly available and can be a cost-effective training option when GPUs are unavailable. When training large models or if a single CPU is too slow, distributed training with CPUs can help speed up training. + +This guide demonstrates how to perform distributed training with multiple CPUs using a [DistributedDataParallel (DDP)](./perf_train_gpu_many#distributeddataparallel) strategy on bare metal with [`Trainer`] and a Kubernetes cluster. All examples shown in this guide depend on the [Intel oneAPI HPC Toolkit](https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit.html). + +There are two toolkits you'll need from Intel oneAPI. + +1. [oneCCL](https://www.intel.com/content/www/us/en/developer/tools/oneapi/oneccl.html) includes efficient implementations of collectives commonly used in deep learning such as all-gather, all-reduce, and reduce-scatter. To install from a prebuilt wheel, make sure you always use the latest release. Refer to the table [here](https://github.com/intel/torch-ccl#install-prebuilt-wheel) to check if a version of oneCCL is supported for a Python and PyTorch version. + +```bash +# installs oneCCL for PyTorch 2.4.0 +pip install oneccl_bind_pt==2.4.0 -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +> [!TIP] +> Refer to the oneCCL [installation](https://github.com/intel/torch-ccl#installation) for more details. + +1. [MPI](https://www.intel.com/content/www/us/en/developer/tools/oneapi/mpi-library.html) is a message-passing interface for communications between hardware and networks. The oneCCL toolkit is installed along with MPI, but you need to source the environment as shown below before using it. + +```bash +oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") +source $oneccl_bindings_for_pytorch_path/env/setvars.sh +``` + +Lastly, install the [Intex Extension for PyTorch (IPEX)](https://intel.github.io/intel-extension-for-pytorch/index.html) which enables additional performance optimizations for Intel hardware such as weight sharing and better thread runtime control. + +```bash +pip install intel_extension_for_pytorch== -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +> [!TIP] +> Refer to the IPEX [installation](https://intel.github.io/intel-extension-for-pytorch/index.html#installation) for more details. + +## Trainer + +[`Trainer`] supports distributed training with CPUs with the oneCCL backend. Add the `--ddp_backend ccl` parameter in the command arguments to enable it. + + + + +The example below demonstrates the [run_qa.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) script. It enables training with two processes on one Xeon CPU, with one process running per socket. + +> [!TIP] +> Tune the variable `OMP_NUM_THREADS/CCL_WORKER_COUNT` for optimal performance. + +```bash +export CCL_WORKER_COUNT=1 +export MASTER_ADDR=127.0.0.1 +mpirun -n 2 -genv OMP_NUM_THREADS=23 \ +python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl +``` + + + + +Scale the training script to four processes on two Xeon CPUs (`node0` and `node1`) by setting `-n 4` and `ppn 2`. The `ppn` parameter specifies the number of processes per node, with one process running per socket. + +Assume `node0` is the main process and create a configuration file containing the IP addresses of each node (for example, hostfile) and pass the configuration file path as an argument. + +```bash +cat hostfile +xxx.xxx.xxx.xxx #node0 ip +xxx.xxx.xxx.xxx #node1 ip +``` + +Run the script below on `node0` to enable DDP on `node0` and `node1` and train with bf16 auto mixed precision. + +> [!TIP] +> Tune the variable `OMP_NUM_THREADS/CCL_WORKER_COUNT` for optimal performance. + +```bash +export CCL_WORKER_COUNT=1 +export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip +mpirun -f hostfile -n 4 -ppn 2 \ + -genv OMP_NUM_THREADS=23 \ +python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --bf16 +``` + + + + +## Kubernetes + +Distributed training with CPUs can also be deployed to a Kubernetes cluster with [PyTorchJob](https://www.kubeflow.org/docs/components/training/user-guides/pytorch/). Before you get started, you should perform the following setup steps. + +1. Ensure you have access to a Kubernetes cluster with [Kubeflow](https://www.kubeflow.org/docs/started/installing-kubeflow/) installed. +1. Install and configure [kubectl](https://kubernetes.io/docs/tasks/tools) to interact with the cluster. +1. Set up a [PersistentVolumeClaim (PVC)](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) to store datasets and model files. There are multiple options to choose from, including a [StorageClass](https://kubernetes.io/docs/concepts/storage/storage-classes/) or a cloud storage bucket. +1. Set up a Docker container for the training script and all required dependencies such as PyTorch, Transformers, IPEX, oneCCL, and OpenSSH to facilitate communicattion between containers. + +The example Dockerfile below uses a base image that supports distributed training with CPUs, and extracts Transformers to the `/workspace` directory to include the training scripts in the image. The image needs to be built and copied to the clusters nodes or pushed to a container registry prior to deployment. + +```dockerfile +FROM intel/intel-optimized-pytorch:2.4.0-pip-multinode + +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends --fix-missing \ + google-perftools \ + libomp-dev + +WORKDIR /workspace + +# Download and extract the transformers code +ARG HF_TRANSFORMERS_VER="4.46.0" +RUN pip install --no-cache-dir \ + transformers==${HF_TRANSFORMERS_VER} && \ + mkdir transformers && \ + curl -sSL --retry 5 https://github.com/huggingface/transformers/archive/refs/tags/v${HF_TRANSFORMERS_VER}.tar.gz | tar -C transformers --strip-components=1 -xzf - +``` + +### PyTorchJob + +[PyTorchJob](https://www.kubeflow.org/docs/components/training/user-guides/pytorch/) is an extension of the Kubernetes API for running PyTorch training jobs on Kubernetes. It includes a yaml file that defines the training jobs parameters such as the name of the PyTorchJob, number of workers, types of resources for each worker, and more. + +The volume mount parameter is a path to where the PVC is mounted in the container for each worker pod. The PVC is typically used to hold the dataset, checkpoint files, and the model after it has finished training. + +The example yaml file below sets up four workers on the [run_qa.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) script. Adapt the yaml file based on your training script and number of nodes in your cluster. + +The CPU resource limits and requests are defined in [CPU units](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu). One CPU unit is equivalent to one physical CPU core or virtual core. The CPU units defined in the yaml file should be less than the amount of available CPU and memory capacity of a single machine in order to leave some resources for kubelet and the system. For a `Guaranteed` [quality of service](https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod), set the same CPU and memory amounts for both the resource limits and requests. + +```yaml +apiVersion: "kubeflow.org/v1" +kind: PyTorchJob +metadata: + name: transformers-pytorchjob +spec: + elasticPolicy: + rdzvBackend: c10d + minReplicas: 1 + maxReplicas: 4 + maxRestarts: 10 + pytorchReplicaSpecs: + Worker: + replicas: 4 # The number of worker pods + restartPolicy: OnFailure + template: + spec: + containers: + - name: pytorch + image: : # Specify the docker image to use for the worker pods + imagePullPolicy: IfNotPresent + command: ["/bin/bash", "-c"] + args: + - >- + cd /workspace/transformers; + pip install -r /workspace/transformers/examples/pytorch/question-answering/requirements.txt; + source /usr/local/lib/python3.10/dist-packages/oneccl_bindings_for_pytorch/env/setvars.sh; + torchrun /workspace/transformers/examples/pytorch/question-answering/run_qa.py \ + --model_name_or_path distilbert/distilbert-base-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/pvc-mount/output_$(date +%Y%m%d_%H%M%S) \ + --no_cuda \ + --ddp_backend ccl \ + --bf16; + env: + - name: LD_PRELOAD + value: "/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4.5.9:/usr/local/lib/libiomp5.so" + - name: TRANSFORMERS_CACHE + value: "/tmp/pvc-mount/transformers_cache" + - name: HF_DATASETS_CACHE + value: "/tmp/pvc-mount/hf_datasets_cache" + - name: LOGLEVEL + value: "INFO" + - name: CCL_WORKER_COUNT + value: "1" + - name: OMP_NUM_THREADS # Can be tuned for optimal performance + value: "240" + resources: + limits: + cpu: 240 # Update the CPU and memory limit values based on your nodes + memory: 128Gi + requests: + cpu: 240 # Update the CPU and memory request values based on your nodes + memory: 128Gi + volumeMounts: + - name: pvc-volume + mountPath: /tmp/pvc-mount + - mountPath: /dev/shm + name: dshm + restartPolicy: Never + nodeSelector: # Optionally use nodeSelector to match a certain node label for the worker pods + node-type: gnr + volumes: + - name: pvc-volume + persistentVolumeClaim: + claimName: transformers-pvc + - name: dshm + emptyDir: + medium: Memory +``` + +### Deploy + +After you've setup the PyTorchJob yaml file with the appropriate settings for your cluster and training job, deploy it to the cluster with the command below. + +```bash +export NAMESPACE= + +kubectl create -f pytorchjob.yaml -n ${NAMESPACE} +``` + +List the pods in the namespace with `kubectl get pods -n ${NAMESPACE}`. At first, the status may be "Pending" but it should change to "Running" once the containers are pulled and created. + +```bash +kubectl get pods -n ${NAMESPACE} + +NAME READY STATUS RESTARTS AGE +... +transformers-pytorchjob-worker-0 1/1 Running 0 7m37s +transformers-pytorchjob-worker-1 1/1 Running 0 7m37s +transformers-pytorchjob-worker-2 1/1 Running 0 7m37s +transformers-pytorchjob-worker-3 1/1 Running 0 7m37s +... +``` + +Inspect the logs for each worker with the following command. Add `-f` to stream the logs. + +```bash +kubectl logs transformers-pytorchjob-worker-0 -n ${NAMESPACE} -f +``` + +Once training is complete, the trained model can be copied from the PVC or storage location. Delete the PyTorchJob resource from the cluster with the command below. + +```bash +kubectl delete -f pytorchjob.yaml -n ${NAMESPACE} +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gaudi.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gaudi.md new file mode 100644 index 0000000000000000000000000000000000000000..2ba792d484a3e26d8315384573eaddd9aa04c863 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gaudi.md @@ -0,0 +1,34 @@ + + +# Intel Gaudi + +The Intel Gaudi AI accelerator family includes [Intel Gaudi 1](https://habana.ai/products/gaudi/), [Intel Gaudi 2](https://habana.ai/products/gaudi2/), and [Intel Gaudi 3](https://habana.ai/products/gaudi3/). Each server is equipped with 8 devices, known as Habana Processing Units (HPUs), providing 128GB of memory on Gaudi 3, 96GB on Gaudi 2, and 32GB on the first-gen Gaudi. For more details on the underlying hardware architecture, check out the [Gaudi Architecture](https://docs.habana.ai/en/latest/Gaudi_Overview/Gaudi_Architecture.html) overview. + +[`TrainingArguments`], [`Trainer`] and [`Pipeline`] detect and set the backend device to `hpu` if an Intel Gaudi device is available. No additional changes are required to enable training and inference on your device. + +Some modeling code in Transformers is not optimized for HPU lazy mode. If you encounter any errors, set the environment variable below to use eager mode: +``` +PT_HPU_LAZY_MODE=0 +``` + +In some cases, you'll also need to enable int64 support to avoid casting issues with long integers: +``` +PT_ENABLE_INT64_SUPPORT=1 +``` +Refer to the [Gaudi docs](https://docs.habana.ai/en/latest/index.html) for more details. + +> [!TIP] +> For training and inference with Gaudi-optimized model implementations, we recommend using [Optimum for Intel Gaudi](https://huggingface.co/docs/optimum/main/en/habana/index). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..7fdbb9d8afe391071fc9a3c85d35108b1a4446e5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_many.md @@ -0,0 +1,124 @@ + + +# Parallelism methods + +Multi-GPU setups are effective for accelerating training and fitting large models in memory that otherwise wouldn't fit on a single GPU. It relies on parallelizing the workload across GPUs. There are several types of parallelism such as data parallelism, tensor parallelism, pipeline parallelism, and model parallelism. Each type of parallelism splits the workload differently, whether it's the data or the model. + +This guide will discuss the various parallelism methods, combining them, and choosing an appropriate strategy for your setup. For more details about distributed training, refer to the [Accelerate](https://hf.co/docs/accelerate/index) documentation. + +For a comprehensive guide on scaling large language models, check out the [Ultrascale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook), which provides detailed strategies and best practices for training at scale. + +## Scalability strategy + +Use the [Model Memory Calculator](https://huggingface.co/spaces/hf-accelerate/model-memory-usage) to calculate how much memory a model requires. Then refer to the table below to select a strategy based on your setup. + +| setup | scenario | strategy | +|---|---|---| +| single node/multi-GPU | fits on single GPU | DistributedDataParallel or ZeRO | +| | doesn't fit on single GPU | PipelineParallel, ZeRO or TensorParallel | +| | largest model layer doesn't fit | TensorParallel or ZeRO | +| multi-node/multi-GPU | fast inter-node connectivity (NVLink or NVSwitch) | ZeRO or 3D parallelism (PipelineParallel, TensorParallel, DataParallel) | +| | slow inter-node connectivity | ZeRO or 3D parallelism (PipelineParallel, TensorParallel, DataParallel) | + +## Data parallelism + +Data parallelism evenly distributes data across multiple GPUs. Each GPU holds a copy of the model and concurrently processes their portion of the data. At the end, the results from each GPU are synchronized and combined. + +Data parallelism significantly reduces training time by processing data in parallel, and it is scalable to the number of GPUs available. However, synchronizing results from each GPU can add overhead. + +There are two types of data parallelism, DataParallel (DP) and DistributedDataParallel (DDP). + +### DataParallel + +[DataParallel](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html) supports distributed training on a *single machine* with multiple GPUs. + +1. The default GPU, `GPU 0`, reads a batch of data and sends a mini batch of it to the other GPUs. +2. An up-to-date model is replicated from `GPU 0` to the other GPUs. +3. A `forward` pass is performed on each GPU and their outputs are sent to `GPU 0` to compute the loss. +4. The loss is distributed from `GPU 0` to the other GPUs for the `backward` pass. +5. The gradients from each GPU are sent back to `GPU 0` and averaged. + +### DistributedDataParallel + +[DistributedDataParallel](https://pytorch.org/docs/main/notes/ddp.html) supports distributed training across *multiple machines* with multiple GPUs. + +1. The main process replicates the model from the default GPU, `GPU 0`, to each GPU. +2. Each GPU directly processes a mini batch of data. +3. The local gradients are averaged across all GPUs during the `backward` pass. + +DDP is recommended because it reduces communication overhead between GPUs, efficiently utilizes each GPU, and scales to more than one machine. + +### ZeRO data parallelism + +[Zero Redundancy Optimizer](https://www.deepspeed.ai/tutorials/zero/) is a more memory efficient type of data parallelism. It significantly improves memory efficiency by partitioning parameters, gradients, and optimizer states across data parallel processes to reduce memory usage. There are three ZeRO stages: + +- Stage 1 partitions the optimizer states +- Stage 2 partitions the optimizer and gradient states +- Stage 3 partitions the optimizer, gradient, and parameters + +
+ +
+ +## Model parallelism + +Model parallelism distributes a model across multiple GPUs. There are several ways to split a model, but the typical method distributes the model layers across GPUs. On the `forward` pass, the first GPU processes a batch of data and passes it to the next group of layers on the next GPU. For the `backward` pass, the data is sent backward from the final layer to the first layer. + +Model parallelism is a useful strategy for training models that are too large to fit into the memory of a single GPU. However, GPU utilization is unbalanced because only one GPU is active at a time. Passing results between GPUs also adds communication overhead and it can be a bottleneck. + +## Pipeline parallelism + +Pipeline parallelism is conceptually very similar to model parallelism, but it's more efficient because it reduces the amount of idle GPU time. Instead of waiting for each GPU to finish processing a batch of data, pipeline parallelism creates *micro-batches* of data. As soon as one micro-batch is finished, it is passed to the next GPU. This way, each GPU can concurrently process part of the data without waiting for the other GPU to completely finish processing a mini batch of data. + +Pipeline parallelism shares the same advantages as model parallelism, but it optimizes GPU utilization and reduces idle time. But pipeline parallelism can be more complex because models may need to be rewritten as a sequence of [nn.Sequential](https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html) modules and it also isn't possible to completely reduce idle time because the last `forward` pass must also wait for the `backward` pass to finish. + +## Tensor parallelism + +Tensor parallelism distributes large tensor computations across multiple GPUs. The tensors are sliced horizontally or vertically and each slice is processed by a separate GPU. Each GPU performs its calculations on its tensor slice and the results are synchronized at the end to reconstruct the final result. + +Tensor parallelism is effective for training large models that don't fit into the memory of a single GPU. It is also faster and more efficient because each GPU can process its tensor slice in parallel, and it can be combined with other parallelism methods. Like other parallelism methods though, tensor parallelism adds communication overhead between GPUs. + +Refer to the [Tensor parallelism](./perf_infer_gpu_multi) guide to learn how to use it for inference. + +## Hybrid parallelism + +Parallelism methods can be combined to achieve even greater memory savings and more efficiently train models with billions of parameters. + +### Data parallelism and pipeline parallelism + +Data and pipeline parallelism distributes the data across GPUs and divides each mini batch of data into micro-batches to achieve pipeline parallelism. + +Each data parallel rank treats the process as if there were only one GPU instead of two, but GPUs 0 and 1 can offload micro-batches of data to GPUs 2 and 3 and reduce idle time. + +This approach optimizes parallel data processing by reducing idle GPU utilization. + +
+ +
+ +### ZeRO data parallelism, pipeline parallelism, and model parallelism (3D parallelism) + +Data, pipeline and model parallelism combine to form [3D parallelism](https://www.microsoft.com/en-us/research/blog/deepspeed-extreme-scale-model-training-for-everyone/) to optimize memory and compute efficiency. + +Memory efficiency is achieved by splitting the model across GPUs and also dividing it into stages to create a pipeline. This allows GPUs to work in parallel on micro-batches of data, reducing the memory usage of the model, optimizer, and activations. + +Compute efficiency is enabled by ZeRO data parallelism where each GPU only stores a slice of the model, optimizer, and activations. This allows higher communication bandwidth between data parallel nodes because communication can occur independently or in parallel with the other pipeline stages. + +This approach is scalable to extremely large models with trillions of parameters. + +
+ +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..e8cc6ec7579e435371b9508deb214e35d70e42e3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_gpu_one.md @@ -0,0 +1,296 @@ + + +# GPU + +GPUs are commonly used to train deep learning models due to their high memory bandwidth and parallel processing capabilities. Depending on your GPU and model size, it is possible to even train models with billions of parameters. The key is to find the right balance between GPU memory utilization (data throughput/training time) and training speed. + +This guide will show you the features available in Transformers and PyTorch for efficiently training a model on GPUs. In many cases, you'll want to use a combination of these features to optimize training. + +Refer to the table below to quickly help you identify the features relevant to your training scenario. + +| Feature | Training speed | Memory usage | +|---|---|---| +| batch size | yes | yes | +| gradient accumulation | no | yes | +| gradient checkpointing | no | yes | +| mixed precision | yes | depends | +| optimizers | yes | yes | +| data preloading | yes | no | +| torch_empty_cache_steps | no | yes | +| torch.compile | yes | no | +| scaled dot production attention (SDPA) | yes | yes | + +## Trainer + +[Trainer](./trainer) supports many useful training features that can be configured through [`TrainingArguments`]. This section highlights some of the more important features for optimizing training. + +### Batch size + +Batch size is one of the most important hyperparameters for efficient GPU training because it affects memory usage and training speed. Larger batch sizes lead to faster training because it takes advantage of a GPUs parallel processing power. It is recommended to use batch sizes that are powers of 2, such as 8, 64, 128, 256, 512, etc. The batch size depends on your GPU and the models data type. + +Configure [`~TrainingArguments.per_device_train_batch_size`] in [`TrainingArguments`]. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=256, + per_device_eval_batch_size=256, +) +``` + +Refer to the NVIDIA [Performance](https://docs.nvidia.com/deeplearning/performance/dl-performance-fully-connected/index.html#input-features) guide to learn more about how input features and output neuron counts and batch size affect performance. These are involved in the General Matrix Multiplications (GEMMs) performed by the GPU. Larger parameters are better for parallelization and efficiency. + +The [Tensor Core Requirements](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#requirements-tc) section is also useful for selecting a batch size that maximizes the speed of tensor multiplication based on the data type and GPU. For example, multiples of 8 are recommended for fp16, unless it's an A100 GPU, in which case use multiples of 64. + +Finally, consider [Dimension Quantization Effects](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#dim-quantization) for smaller parameters. Tile quantization results when matrix dimensions aren't divisible by a GPUs thread block tile size, causing the GPU to underutilize its resources. Selecting the correct batch size multiplier, such that the matrix is divisible by the tile size, can significantly speed up training. + +### Gradient accumulation + +Gradient accumulation overcomes memory constraints - useful for fitting a very large model that otherwise wouldn't fit on a single GPU - by accumulating gradients over multiple mini-batches before updating the parameters. This reduces memory by storing fewer gradients and enables training with a larger *effective batch size* because usually, the parameters are updated from a single batch of data. Training can slow down though due to the additional forward and backward passes introduced by gradient accumulation. + +Configure [`~TrainingArguments.per_device_train_batch_size`] in [`TrainingArguments`] to enable gradient accumulation. + +```py +from transformers import TrainingArguments + +# effective batch size of 64 +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, +) +``` + +Try to avoid too many gradient accumulation steps because it can really slow down training. Consider the example below, where the maximum batch size that'll fit on your GPU is 4. You should keep your batch size at 4 to better utilize the GPU. + +| batch size | gradient accumulation steps | effective batch size | | +|---|---|---|---| +| 1 | 64 | 64 | ๐Ÿ‘Ž | +| 4 | 16 | 64 | ๐Ÿ‘ | + +### Gradient checkpointing + +Gradient checkpointing reduces memory usage by only storing some of the intermediate activations during the backward pass and recomputing the remaining activations. This avoids storing *all* of the intermediate activations from the forward pass, which can require a lot of memory overhead. However, it comes at the cost of slower training speed (~20%). + +Configure [`~TrainingArguments.gradient_checkpointing`] in [`TrainingArguments`] to enable gradient checkpointing. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, +) +``` + +### Mixed precision + +Mixed precision accelerates training speed by performing some calculations in half-precision (fp16) and some in full-precision (fp32). The half-precision calculations boosts training speed because it's not as computationally expensive as performing the calculations in full-precision. Meanwhile, preserving some of the calculations in full-precision maintains accuracy. + +There are several data types available for mixed precision training. + + + + +The main advantage of mixed precision training is saving the activations in fp16. + +Configure [`~TrainingArguments.fp16`] in [`TrainingArguments`] to enable mixed precision training with the fp16 data type. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + fp16=True. +) +``` + +fp16 isn't memory-optimized because the gradients that are computed in fp16 are converted back to fp32 during the optimization step. You may end up using more GPU memory, especially for small batch sizes, because there are now two versions (fp16 and fp32) of the model on the GPU. + + + + +[bf16](https://cloud.google.com/blog/products/ai-machine-learning/bfloat16-the-secret-to-high-performance-on-cloud-tpus) trades off some precision for a much larger dynamic range, which is helpful for avoiding overflow and underflow errors. You can use bf16 without adding any loss scaling methods like you would with fp16. bf16 is supported by NVIDIAs Ampere architecture or newer. + +Configure [`~TrainingArguments.bf16`] in [`TrainingArguments`] to enable mixed precision training with the bf16 data type. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, +) +``` + + + + +[tf32](https://blogs.nvidia.com/blog/tensorfloat-32-precision-format/) is a mode on NVIDIA Ampere GPUs that convert the convolution and matrix multiplication inputs to tf32. All other storage and operations are kept in fp32. This allows tf32 to maintain the same range as fp32, the same precision as fp16 and more precision than bf16. Combining tf32 with fp16 or bf16 mixed precision training can improve throughput by 16x. + +tf32 is enabled by default on NVIDIA Ampere GPUs, but you can also add the code below to your fp32 training or inference code to explicitly enable it. + +```py +import torch +torch.backends.cuda.matmul.allow_tf32 = True +torch.backends.cudnn.allow_tf32 = True +``` + +Configure [tf32()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.tf32) in [`TrainingArguments`] to enable mixed precision training with tf32 mode. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True. + tf32=True, +) +``` + + + + +### Optimizers + +Transformers implements the [AdamW (adamw_torch)](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) optimizer from PyTorch by default. But because it stores a weighted average of past gradients, it requires additional memory proportional to the number of model parameters to store the past gradients. This can be an issue when training very large models, and in such cases, you should consider choosing a different optimizer. For example, if you have [Apex](https://nvidia.github.io/apex/index.html) installed on either [NVIDIA](https://github.com/NVIDIA/apex) or [AMD](https://github.com/ROCm/apex), then using the `adamw_apex_fused` optimizer provides the fastest training for all AdamW optimizers. + +Configure [`~TrainingArguments.optim`] in [`TrainingArguments`] to choose an optimizer. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit" +) +``` + +There are many optimizers to choose from (refer to [OptimizerNames](https://github.com/huggingface/transformers/blob/34f4080ff59b1668d919a1ba9f8bc4a3a2a3f478/src/transformers/training_args.py#L145) for a full supported list) depending on your training scenario. For example, Adafactor can significantly reduce memory requirements by storing a weighted average of a row or column instead of each element in the matrix at the cost of slower convergence. Another example is using a [8-bit AdamW optimizer](https://huggingface.co/docs/bitsandbytes) from bitsandbytes to quantize optimizer states. The optimizer state is stored in a lower precision and dequantized before being used in the optimizer step. + +Refer to the [optimizer](./optimizers) guide for to learn about more specialized optimizers. + +### Data preloading + +Data preloading loads and prepares batches of data in advance on the CPU to ensure the GPU is continuously working, reducing GPU idling and increasing utilization. There are two ways to preload data to ensure the GPU is always working. + +1. Allocate pinned memory on the CPU to store the data and transfer it directly to the GPU. +2. Increase the number of CPU threads or workers to preload the data faster. + +Configure [`~TrainingArguments.dataloader_pin_memory`] and [`~TrainingArguments.dataloader_num_workers`] in [`TrainingArguments`] to allocate pinned memory and increase the number of workers. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, +) +``` + +## PyTorch + +PyTorch provides several features for reducing memory requirements and increasing training speed. These features can often be enabled in Transformers by only adding a few lines of code. + +### torch.empty_cache_steps + +The [torch.cuda.empty_cache](https://pytorch.org/docs/stable/generated/torch.cuda.empty_cache.html#torch.cuda.empty_cache) function releases unused cached memory, which can help avoid out-of-memory (OOM) errors at the cost of ~10% slower training. + +Use [torch_empty_cache_steps()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.torch_empty_cache_steps) in [`TrainingArguments`] to enable it after a certain number of training steps. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, + torch_empty_cache_steps=4, +) +``` + +### torch.compile + +[torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) compiles PyTorch code into optimized kernels that significantly speed up training. This feature relies on TorchDynamo to capture PyTorch graphs with the Frame Evaluation API. The graph can be further compiled into optimized kernels for different backends. + +Configure [`~TrainingArguments.torch_compile`] in [`TrainingArguments`] to enable it, and configure [torch_compile_backend()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.torch_compile_backend) to select a backend to use. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, + torch_empty_cache_steps=4, + torch_compile=True, + torch_compile_backend="inductor" +) +``` + +Refer to the table below to help you choose the right backend for your training scenario. + +| backend | description | goal | +|---|---|---| +| eager | uses PyTorch to run extracted GraphModule | debugging | +| aot_eager | uses PyTorch eager mode for AOTAutograd's extracted forward and backward graphs | debugging | +| inductor | uses TorchInductor with AOTAutograd and CUDA Graphs by leveraging Triton kernels | training and inference | +| nvfuser | uses nvFuser with TorchScript | training and inference | +| aot_nvfuser | uses nvFuser with AOTAutograd | training and inference | +| aot_cudagraphs | uses CUDA Graphs with AOTAutograd | training and inference | +| ofi | uses TorchScripts [optimize_for_inference](https://pytorch.org/docs/stable/generated/torch.jit.optimize_for_inference.html#torch-jit-optimize-for-inference) | inference | +| fx2trt | uses [Torch-TensorRT](https://pytorch.org/TensorRT/tutorials/getting_started_with_fx_path.html) | inference | +| onnxrt | uses [ONNX-RT](https://onnxruntime.ai/) for CPU and GPU inference | inference | +| ipex | uses [IPEX](https://github.com/intel/intel-extension-for-pytorch) for CPU inference | inference | + +### Scaled dot production attention + +[torch.nn.functional.scaled_dot_product_attention](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) (SDPA) is a native PyTorch implementation of the scaled dot product attention mechanism. SDPA is more efficient and optimized than the original attention mechanism in transformer models. It supports three types of scaled dot product attention. + +- [FlashAttention2](https://github.com/Dao-AILab/flash-attention) is automatically enabled for models with the fp16 or bf16 torch type. Make sure to cast your model to the appropriate type first. +- [xFormers](https://github.com/facebookresearch/xformers) or Memory-Efficient Attention supports models with the fp32 torch type. +- C++ implementation of scaled dot product attention. + +SDPA is enabled by default for PyTorch 2.1.1+, but it can be explicitly enabled by setting `attn_implementation="sdpa"` in [`~PreTrainedModel.from_pretrained`]. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", attn_implementation="sdpa") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_special.md new file mode 100644 index 0000000000000000000000000000000000000000..128f83c23ad721dd925ff65073aed7154887de8e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perf_train_special.md @@ -0,0 +1,31 @@ + + +# Apple Silicon + +Apple Silicon (M series) features a unified memory architecture, making it possible to efficiently train large models locally and improves performance by reducing latency associated with data retrieval. You can take advantage of Apple Silicon for training with PyTorch due to its integration with [Metal Performance Shaders (MPS)](https://pytorch.org/docs/stable/notes/mps.html). + +The `mps` backend requires macOS 12.3 or later. + +> [!WARNING] +> Some PyTorch operations are not implemented in MPS yet. To avoid an error, set the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to fallback on the CPU kernels. Please open an issue in the [PyTorch](https://github.com/pytorch/pytorch/issues) repository if you encounter any other issues. + +[`TrainingArguments`] and [`Trainer`] detects and sets the backend device to `mps` if an Apple Silicon device is available. No additional changes are required to enable training on your device. + +The `mps` backend doesn't support [distributed training](https://pytorch.org/docs/stable/distributed.html#backends). + +## Resources + +Learn more about the MPS backend in the [Introducing Accelerated PyTorch Training on Mac](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) blog post. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perplexity.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perplexity.md new file mode 100644 index 0000000000000000000000000000000000000000..4350b444e62f148ec0209dabc4a7f408d7adffd0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/perplexity.md @@ -0,0 +1,150 @@ + + +# Perplexity of fixed-length models + +[[open-in-colab]] + +Perplexity (PPL) is one of the most common metrics for evaluating language models. Before diving in, we should note +that the metric applies specifically to classical language models (sometimes called autoregressive or causal language +models) and is not well defined for masked language models like BERT (see [summary of the models](model_summary)). + +Perplexity is defined as the exponentiated average negative log-likelihood of a sequence. If we have a tokenized +sequence \\(X = (x_0, x_1, \dots, x_t)\\), then the perplexity of \\(X\\) is, + +$$\text{PPL}(X) = \exp \left\{ {-\frac{1}{t}\sum_i^t \log p_\theta (x_i|x_{ + +When working with approximate models, however, we typically have a constraint on the number of tokens the model can +process. The largest version of [GPT-2](model_doc/gpt2), for example, has a fixed length of 1024 tokens, so we +cannot calculate \\(p_\theta(x_t|x_{ + +This is quick to compute since the perplexity of each segment can be computed in one forward pass, but serves as a poor +approximation of the fully-factorized perplexity and will typically yield a higher (worse) PPL because the model will +have less context at most of the prediction steps. + +Instead, the PPL of fixed-length models should be evaluated with a sliding-window strategy. This involves repeatedly +sliding the context window so that the model has more context when making each prediction. + +Sliding window PPL taking advantage of all available context + +This is a closer approximation to the true decomposition of the sequence probability and will typically yield a more +favorable score. The downside is that it requires a separate forward pass for each token in the corpus. A good +practical compromise is to employ a strided sliding window, moving the context by larger strides rather than sliding by +1 token a time. This allows computation to proceed much faster while still giving the model a large context to make +predictions at each step. + +## Example: Calculating perplexity with GPT-2 in ๐Ÿค— Transformers + +Let's demonstrate this process with GPT-2. + +```python +from transformers import GPT2LMHeadModel, GPT2TokenizerFast, infer_device + +device = infer_device() +model_id = "openai-community/gpt2-large" +model = GPT2LMHeadModel.from_pretrained(model_id).to(device) +tokenizer = GPT2TokenizerFast.from_pretrained(model_id) +``` + +We'll load in the WikiText-2 dataset and evaluate the perplexity using a few different sliding-window strategies. Since +this dataset is small and we're just doing one forward pass over the set, we can just load and encode the entire +dataset in memory. + +```python +from datasets import load_dataset + +test = load_dataset("wikitext", "wikitext-2-raw-v1", split="test") +encodings = tokenizer("\n\n".join(test["text"]), return_tensors="pt") +``` + +With ๐Ÿค— Transformers, we can simply pass the `input_ids` as the `labels` to our model, and the average negative +log-likelihood for each token is returned as the loss. With our sliding window approach, however, there is overlap in +the tokens we pass to the model at each iteration. We don't want the log-likelihood for the tokens we're just treating +as context to be included in our loss, so we can set these targets to `-100` so that they are ignored. The following +is an example of how we could do this with a stride of `512`. This means that the model will have at least 512 tokens +for context when calculating the conditional likelihood of any one token (provided there are 512 preceding tokens +available to condition on). + +```python +import torch +from tqdm import tqdm + +max_length = model.config.n_positions +stride = 512 +seq_len = encodings.input_ids.size(1) + +nll_sum = 0.0 +n_tokens = 0 +prev_end_loc = 0 +for begin_loc in tqdm(range(0, seq_len, stride)): + end_loc = min(begin_loc + max_length, seq_len) + trg_len = end_loc - prev_end_loc # may be different from stride on last loop + input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device) + target_ids = input_ids.clone() + target_ids[:, :-trg_len] = -100 + + with torch.no_grad(): + outputs = model(input_ids, labels=target_ids) + + # loss is calculated using CrossEntropyLoss which averages over valid labels + # N.B. the model only calculates loss over trg_len - 1 labels, because it internally shifts the labels + # to the left by 1. + neg_log_likelihood = outputs.loss + + # Accumulate the total negative log-likelihood and the total number of tokens + num_valid_tokens = (target_ids != -100).sum().item() # number of valid tokens in target_ids + batch_size = target_ids.size(0) + num_loss_tokens = num_valid_tokens - batch_size # subtract batch_size due to internal label shift + nll_sum += neg_log_likelihood * num_loss_tokens + n_tokens += num_loss_tokens + + prev_end_loc = end_loc + if end_loc == seq_len: + break + +avg_nll = nll_sum / n_tokens # average negative log-likelihood per token +ppl = torch.exp(avg_nll) +``` + +Running this with the stride length equal to the max input length is equivalent to the suboptimal, non-sliding-window +strategy we discussed above. The smaller the stride, the more context the model will have in making each prediction, +and the better the reported perplexity will typically be. + +When we run the above with `stride = 1024`, i.e. no overlap, the resulting PPL is `19.44`, which is about the same +as the `19.93` reported in the GPT-2 paper. By using `stride = 512` and thereby employing our striding window +strategy, this jumps down to `16.44`. This is not only a more favorable score, but is calculated in a way that is +closer to the true autoregressive decomposition of a sequence likelihood. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/philosophy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/philosophy.md new file mode 100644 index 0000000000000000000000000000000000000000..628cb39bbb33006180064433abf790f88194c948 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/philosophy.md @@ -0,0 +1,79 @@ + + +# Philosophy + +๐Ÿค— Transformers is an opinionated library built for: + +- machine learning researchers and educators seeking to use, study or extend large-scale Transformers models. +- hands-on practitioners who want to fine-tune those models or serve them in production, or both. +- engineers who just want to download a pretrained model and use it to solve a given machine learning task. + +The library was designed with two strong goals in mind: + +1. Be as easy and fast to use as possible: + + - We strongly limited the number of user-facing abstractions to learn, in fact, there are almost no abstractions, + just three standard classes required to use each model: [configuration](main_classes/configuration), + [models](main_classes/model), and a preprocessing class ([tokenizer](main_classes/tokenizer) for NLP, [image processor](main_classes/image_processor) for vision, [feature extractor](main_classes/feature_extractor) for audio, and [processor](main_classes/processors) for multimodal inputs). + - All of these classes can be initialized in a simple and unified way from pretrained instances by using a common + `from_pretrained()` method which downloads (if needed), caches and + loads the related class instance and associated data (configurations' hyperparameters, tokenizers' vocabulary, + and models' weights) from a pretrained checkpoint provided on [Hugging Face Hub](https://huggingface.co/models) or your own saved checkpoint. + - On top of those three base classes, the library provides two APIs: [`pipeline`] for quickly + using a model for inference on a given task and [`Trainer`] to quickly train or fine-tune a PyTorch model (all TensorFlow models are compatible with `Keras.fit`). + - As a consequence, this library is NOT a modular toolbox of building blocks for neural nets. If you want to + extend or build upon the library, just use regular Python, PyTorch, TensorFlow, Keras modules and inherit from the base + classes of the library to reuse functionalities like model loading and saving. If you'd like to learn more about our coding philosophy for models, check out our [Repeat Yourself](https://huggingface.co/blog/transformers-design-philosophy) blog post. + +2. Provide state-of-the-art models with performances as close as possible to the original models: + + - We provide at least one example for each architecture which reproduces a result provided by the official authors + of said architecture. + - The code is usually as close to the original code base as possible which means some PyTorch code may be not as + *pytorchic* as it could be as a result of being converted TensorFlow code and vice versa. + +A few other goals: + +- Expose the models' internals as consistently as possible: + + - We give access, using a single API, to the full hidden-states and attention weights. + - The preprocessing classes and base model APIs are standardized to easily switch between models. + +- Incorporate a subjective selection of promising tools for fine-tuning and investigating these models: + + - A simple and consistent way to add new tokens to the vocabulary and embeddings for fine-tuning. + - Simple ways to mask and prune Transformer heads. + +- Easily switch between PyTorch, TensorFlow 2.0 and Flax, allowing training with one framework and inference with another. + +## Main concepts + +The library is built around three types of classes for each model: + +- **Model classes** can be PyTorch models ([torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)), Keras models ([tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model)) or JAX/Flax models ([flax.linen.Module](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html)) that work with the pretrained weights provided in the library. +- **Configuration classes** store the hyperparameters required to build a model (such as the number of layers and hidden size). You don't always need to instantiate these yourself. In particular, if you are using a pretrained model without any modification, creating the model will automatically take care of instantiating the configuration (which is part of the model). +- **Preprocessing classes** convert the raw data into a format accepted by the model. A [tokenizer](main_classes/tokenizer) stores the vocabulary for each model and provide methods for encoding and decoding strings in a list of token embedding indices to be fed to a model. [Image processors](main_classes/image_processor) preprocess vision inputs, [feature extractors](main_classes/feature_extractor) preprocess audio inputs, and a [processor](main_classes/processors) handles multimodal inputs. + +All these classes can be instantiated from pretrained instances, saved locally, and shared on the Hub with three methods: + +- `from_pretrained()` lets you instantiate a model, configuration, and preprocessing class from a pretrained version either + provided by the library itself (the supported models can be found on the [Model Hub](https://huggingface.co/models)) or + stored locally (or on a server) by the user. +- `save_pretrained()` lets you save a model, configuration, and preprocessing class locally so that it can be reloaded using + `from_pretrained()`. +- `push_to_hub()` lets you share a model, configuration, and a preprocessing class to the Hub, so it is easily accessible to everyone. + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_gradio.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_gradio.md new file mode 100644 index 0000000000000000000000000000000000000000..0cd65665d33d7af7015e7a994a9f3ff08399150d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_gradio.md @@ -0,0 +1,52 @@ + + +# Machine learning apps + +[Gradio](https://www.gradio.app/), a fast and easy library for building and sharing machine learning apps, is integrated with [`Pipeline`] to quickly create a simple interface for inference. + +Before you begin, make sure Gradio is installed. + +```py +!pip install gradio +``` + +Create a pipeline for your task, and then pass it to Gradio's [Interface.from_pipeline](https://www.gradio.app/docs/gradio/interface#interface-from_pipeline) function to create the interface. Gradio automatically determines the appropriate input and output components for a [`Pipeline`]. + +Add [launch](https://www.gradio.app/main/docs/gradio/blocks#blocks-launch) to create a web server and start up the app. + +```py +from transformers import pipeline +import gradio as gr + +pipeline = pipeline("image-classification", model="google/vit-base-patch16-224") +gr.Interface.from_pipeline(pipeline).launch() +``` + +The web app runs on a local server by default. To share the app with other users, set `share=True` in [launch](https://www.gradio.app/main/docs/gradio/blocks#blocks-launch) to generate a temporary public link. For a more permanent solution, host the app on Hugging Face [Spaces](https://hf.co/spaces). + +```py +gr.Interface.from_pipeline(pipeline).launch(share=True) +``` + +The Space below is created with the code above and hosted on Spaces. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..24fa6275acee86e1b344db9f4d532bcdff68c23b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_tutorial.md @@ -0,0 +1,345 @@ + + +# Pipeline + +The [`Pipeline`] is a simple but powerful inference API that is readily available for a variety of machine learning tasks with any model from the Hugging Face [Hub](https://hf.co/models). + +Tailor the [`Pipeline`] to your task with task specific parameters such as adding timestamps to an automatic speech recognition (ASR) pipeline for transcribing meeting notes. [`Pipeline`] supports GPUs, Apple Silicon, and half-precision weights to accelerate inference and save memory. + + + +Transformers has two pipeline classes, a generic [`Pipeline`] and many individual task-specific pipelines like [`TextGenerationPipeline`] or [`VisualQuestionAnsweringPipeline`]. Load these individual pipelines by setting the task identifier in the `task` parameter in [`Pipeline`]. You can find the task identifier for each pipeline in their API documentation. + +Each task is configured to use a default pretrained model and preprocessor, but this can be overridden with the `model` parameter if you want to use a different model. + +For example, to use the [`TextGenerationPipeline`] with [Gemma 2](./model_doc/gemma2), set `task="text-generation"` and `model="google/gemma-2-2b"`. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b") +pipeline("the secret to baking a really good cake is ") +[{'generated_text': 'the secret to baking a really good cake is 1. the right ingredients 2. the'}] +``` + +When you have more than one input, pass them as a list. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b", device="cuda") +pipeline(["the secret to baking a really good cake is ", "a baguette is "]) +[[{'generated_text': 'the secret to baking a really good cake is 1. the right ingredients 2. the'}], + [{'generated_text': 'a baguette is 100% bread.\n\na baguette is 100%'}]] +``` + +This guide will introduce you to the [`Pipeline`], demonstrate its features, and show how to configure its various parameters. + +## Tasks + +[`Pipeline`] is compatible with many machine learning tasks across different modalities. Pass an appropriate input to the pipeline and it will handle the rest. + +Here are some examples of how to use [`Pipeline`] for different tasks and modalities. + + + + +```py +from transformers import pipeline + +pipeline = pipeline(task="summarization", model="google/pegasus-billsum") +pipeline("Section was formerly set out as section 44 of this title. As originally enacted, this section contained two further provisions that 'nothing in this act shall be construed as in any wise affecting the grant of lands made to the State of California by virtue of the act entitled 'An act authorizing a grant to the State of California of the Yosemite Valley, and of the land' embracing the Mariposa Big-Tree Grove, approved June thirtieth, eighteen hundred and sixty-four; or as affecting any bona-fide entry of land made within the limits above described under any law of the United States prior to the approval of this act.' The first quoted provision was omitted from the Code because the land, granted to the state of California pursuant to the Act cite, was receded to the United States. Resolution June 11, 1906, No. 27, accepted the recession.") +[{'summary_text': 'Instructs the Secretary of the Interior to convey to the State of California all right, title, and interest of the United States in and to specified lands which are located within the Yosemite and Mariposa National Forests, California.'}] +``` + + + + +```py +from transformers import pipeline + +pipeline = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3") +pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + + + + +```py +from transformers import pipeline + +pipeline = pipeline(task="image-classification", model="google/vit-base-patch16-224") +pipeline(images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg") +[{'label': 'lynx, catamount', 'score': 0.43350091576576233}, + {'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor', + 'score': 0.034796204417943954}, + {'label': 'snow leopard, ounce, Panthera uncia', + 'score': 0.03240183740854263}, + {'label': 'Egyptian cat', 'score': 0.02394474856555462}, + {'label': 'tiger cat', 'score': 0.02288915030658245}] +``` + + + + +```py +from transformers import pipeline + +pipeline = pipeline(task="visual-question-answering", model="Salesforce/blip-vqa-base") +pipeline( + image="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-few-shot.jpg", + question="What is in the image?", +) +[{'answer': 'statue of liberty'}] +``` + + + + +## Parameters + +At a minimum, [`Pipeline`] only requires a task identifier, model, and the appropriate input. But there are many parameters available to configure the pipeline with, from task-specific parameters to optimizing performance. + +This section introduces you to some of the more important parameters. + +### Device + +[`Pipeline`] is compatible with many hardware types, including GPUs, CPUs, Apple Silicon, and more. Configure the hardware type with the `device` parameter. By default, [`Pipeline`] runs on a CPU which is given by `device=-1`. + + + + +To run [`Pipeline`] on a GPU, set `device` to the associated CUDA device id. For example, `device=0` runs on the first GPU. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b", device=0) +pipeline("the secret to baking a really good cake is ") +``` + +You could also let [Accelerate](https://hf.co/docs/accelerate/index), a library for distributed training, automatically choose how to load and store the model weights on the appropriate device. This is especially useful if you have multiple devices. Accelerate loads and stores the model weights on the fastest device first, and then moves the weights to other devices (CPU, hard drive) as needed. Set `device_map="auto"` to let Accelerate choose the device. + +> [!TIP] +> Make sure have [Accelerate](https://hf.co/docs/accelerate/basic_tutorials/install) is installed. +> +> ```py +> !pip install -U accelerate +> ``` + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b", device_map="auto") +pipeline("the secret to baking a really good cake is ") +``` + + + + +To run [`Pipeline`] on Apple silicon, set `device="mps"`. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b", device="mps") +pipeline("the secret to baking a really good cake is ") +``` + + + + +### Batch inference + +[`Pipeline`] can also process batches of inputs with the `batch_size` parameter. Batch inference may improve speed, especially on a GPU, but it isn't guaranteed. Other variables such as hardware, data, and the model itself can affect whether batch inference improves speed. For this reason, batch inference is disabled by default. + +In the example below, when there are 4 inputs and `batch_size` is set to 2, [`Pipeline`] passes a batch of 2 inputs to the model at a time. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="google/gemma-2-2b", device="cuda", batch_size=2) +pipeline(["the secret to baking a really good cake is", "a baguette is", "paris is the", "hotdogs are"]) +[[{'generated_text': 'the secret to baking a really good cake is to use a good cake mix.\n\niโ€™'}], + [{'generated_text': 'a baguette is'}], + [{'generated_text': 'paris is the most beautiful city in the world.\n\niโ€™ve been to paris 3'}], + [{'generated_text': 'hotdogs are a staple of the american diet. they are a great source of protein and can'}]] +``` + +Another good use case for batch inference is for streaming data in [`Pipeline`]. + +```py +from transformers import pipeline +from transformers.pipelines.pt_utils import KeyDataset +import datasets + +# KeyDataset is a utility that returns the item in the dict returned by the dataset +dataset = datasets.load_dataset("imdb", name="plain_text", split="unsupervised") +pipeline = pipeline(task="text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", device="cuda") +for out in pipeline(KeyDataset(dataset, "text"), batch_size=8, truncation="only_first"): + print(out) +``` + +Keep the following general rules of thumb in mind for determining whether batch inference can help improve performance. + +1. The only way to know for sure is to measure performance on your model, data, and hardware. +2. Don't batch inference if you're constrained by latency (a live inference product for example). +3. Don't batch inference if you're using a CPU. +4. Don't batch inference if you don't know the `sequence_length` of your data. Measure performance, iteratively add to `sequence_length`, and include out-of-memory (OOM) checks to recover from failures. +5. Do batch inference if your `sequence_length` is regular, and keep pushing it until you reach an OOM error. The larger the GPU, the more helpful batch inference is. +6. Do make sure you can handle OOM errors if you decide to do batch inference. + +### Task-specific parameters + +[`Pipeline`] accepts any parameters that are supported by each individual task pipeline. Make sure to check out each individual task pipeline to see what type of parameters are available. If you can't find a parameter that is useful for your use case, please feel free to open a GitHub [issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml) to request it! + +The examples below demonstrate some of the task-specific parameters available. + + + + +Pass the `return_timestamps="word"` parameter to [`Pipeline`] to return when each word was spoken. + +```py +from transformers import pipeline + +pipeline = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3") +pipeline(audio="https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", return_timestamp="word") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.', + 'chunks': [{'text': ' I', 'timestamp': (0.0, 1.1)}, + {'text': ' have', 'timestamp': (1.1, 1.44)}, + {'text': ' a', 'timestamp': (1.44, 1.62)}, + {'text': ' dream', 'timestamp': (1.62, 1.92)}, + {'text': ' that', 'timestamp': (1.92, 3.7)}, + {'text': ' one', 'timestamp': (3.7, 3.88)}, + {'text': ' day', 'timestamp': (3.88, 4.24)}, + {'text': ' this', 'timestamp': (4.24, 5.82)}, + {'text': ' nation', 'timestamp': (5.82, 6.78)}, + {'text': ' will', 'timestamp': (6.78, 7.36)}, + {'text': ' rise', 'timestamp': (7.36, 7.88)}, + {'text': ' up', 'timestamp': (7.88, 8.46)}, + {'text': ' and', 'timestamp': (8.46, 9.2)}, + {'text': ' live', 'timestamp': (9.2, 10.34)}, + {'text': ' out', 'timestamp': (10.34, 10.58)}, + {'text': ' the', 'timestamp': (10.58, 10.8)}, + {'text': ' true', 'timestamp': (10.8, 11.04)}, + {'text': ' meaning', 'timestamp': (11.04, 11.4)}, + {'text': ' of', 'timestamp': (11.4, 11.64)}, + {'text': ' its', 'timestamp': (11.64, 11.8)}, + {'text': ' creed.', 'timestamp': (11.8, 12.3)}]} +``` + + + + +Pass `return_full_text=False` to [`Pipeline`] to only return the generated text instead of the full text (prompt and generated text). + +[`~TextGenerationPipeline.__call__`] also supports additional keyword arguments from the [`~GenerationMixin.generate`] method. To return more than one generated sequence, set `num_return_sequences` to a value greater than 1. + +```py +from transformers import pipeline + +pipeline = pipeline(task="text-generation", model="openai-community/gpt2") +pipeline("the secret to baking a good cake is", num_return_sequences=4, return_full_text=False) +[{'generated_text': ' how easy it is for me to do it with my hands. You must not go nuts, or the cake is going to fall out.'}, + {'generated_text': ' to prepare the cake before baking. The key is to find the right type of icing to use and that icing makes an amazing frosting cake.\n\nFor a good icing cake, we give you the basics'}, + {'generated_text': " to remember to soak it in enough water and don't worry about it sticking to the wall. In the meantime, you could remove the top of the cake and let it dry out with a paper towel.\n"}, + {'generated_text': ' the best time to turn off the oven and let it stand 30 minutes. After 30 minutes, stir and bake a cake in a pan until fully moist.\n\nRemove the cake from the heat for about 12'}] +``` + + + + +## Chunk batching + +There are some instances where you need to process data in chunks. + +- for some data types, a single input (for example, a really long audio file) may need to be chunked into multiple parts before it can be processed +- for some tasks, like zero-shot classification or question answering, a single input may need multiple forward passes which can cause issues with the `batch_size` parameter + +The [ChunkPipeline](https://github.com/huggingface/transformers/blob/99e0ab6ed888136ea4877c6d8ab03690a1478363/src/transformers/pipelines/base.py#L1387) class is designed to handle these use cases. Both pipeline classes are used in the same way, but since [ChunkPipeline](https://github.com/huggingface/transformers/blob/99e0ab6ed888136ea4877c6d8ab03690a1478363/src/transformers/pipelines/base.py#L1387) can automatically handle batching, you don't need to worry about the number of forward passes your inputs trigger. Instead, you can optimize `batch_size` independently of the inputs. + +The example below shows how it differs from [`Pipeline`]. + +```py +# ChunkPipeline +all_model_outputs = [] +for preprocessed in pipeline.preprocess(inputs): + model_outputs = pipeline.model_forward(preprocessed) + all_model_outputs.append(model_outputs) +outputs =pipeline.postprocess(all_model_outputs) + +# Pipeline +preprocessed = pipeline.preprocess(inputs) +model_outputs = pipeline.forward(preprocessed) +outputs = pipeline.postprocess(model_outputs) +``` + +## Large datasets + +For inference with large datasets, you can iterate directly over the dataset itself. This avoids immediately allocating memory for the entire dataset, and you don't need to worry about creating batches yourself. Try [Batch inference](#batch-inference) with the `batch_size` parameter to see if it improves performance. + +```py +from transformers.pipelines.pt_utils import KeyDataset +from transformers import pipeline +from datasets import load_dataset + +dataset = datasets.load_dataset("imdb", name="plain_text", split="unsupervised") +pipeline = pipeline(task="text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", device="cuda") +for out in pipeline(KeyDataset(dataset, "text"), batch_size=8, truncation="only_first"): + print(out) +``` + +Other ways to run inference on large datasets with [`Pipeline`] include using an iterator or generator. + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + +pipeline = pipeline(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipeline(data()): + generated_characters += len(out[0]["generated_text"]) +``` + +## Large models + +[Accelerate](https://hf.co/docs/accelerate/index) enables a couple of optimizations for running large models with [`Pipeline`]. Make sure Accelerate is installed first. + +```py +!pip install -U accelerate +``` + +The `device_map="auto"` setting is useful for automatically distributing the model across the fastest devices (GPUs) first before dispatching to other slower devices if available (CPU, hard drive). + +[`Pipeline`] supports half-precision weights (torch.float16), which can be significantly faster and save memory. Performance loss is negligible for most models, especially for larger ones. If your hardware supports it, you can enable torch.bfloat16 instead for more range. + +> [!TIP] +> Inputs are internally converted to torch.float16 and it only works for models with a PyTorch backend. + +Lastly, [`Pipeline`] also accepts quantized models to reduce memory usage even further. Make sure you have the [bitsandbytes](https://hf.co/docs/bitsandbytes/installation) library installed first, and then add `load_in_8bit=True` to `model_kwargs` in the pipeline. + +```py +import torch +from transformers import pipeline, BitsAndBytesConfig + +pipeline = pipeline(model="google/gemma-7b", torch_dtype=torch.bfloat16, device_map="auto", model_kwargs={"quantization_config": BitsAndBytesConfig(load_in_8bit=True)}) +pipeline("the secret to baking a good cake is ") +[{'generated_text': 'the secret to baking a good cake is 1. the right ingredients 2. the right'}] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_webserver.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_webserver.md new file mode 100644 index 0000000000000000000000000000000000000000..0112d116c47da49ca6824c6046b5845425d48d5d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pipeline_webserver.md @@ -0,0 +1,162 @@ + + +# Web server inference + +A web server is a system that waits for requests and serves them as they come in. This means you can use [`Pipeline`] as an inference engine on a web server, since you can use an iterator (similar to how you would [iterate over a dataset](./pipeline_tutorial#large-datasets)) to handle each incoming request. + +Designing a web server with [`Pipeline`] is unique though because they're fundamentally different. Web servers are multiplexed (multithreaded, async, etc.) to handle multiple requests concurrently. [`Pipeline`] and its underlying model on the other hand are not designed for parallelism because they take a lot of memory. It's best to give a [`Pipeline`] all the available resources when they're running or for a compute intensive job. + +This guide shows how to work around this difference by using a web server to handle the lighter load of receiving and sending requests, and having a single thread to handle the heavier load of running [`Pipeline`]. + +## Create a server + +[Starlette](https://www.starlette.io/) is a lightweight framework for building web servers. You can use any other framework you'd like, but you may have to make some changes to the code below. + +Before you begin, make sure Starlette and [uvicorn](http://www.uvicorn.org/) are installed. + +```py +!pip install starlette uvicorn +``` + +Now you can create a simple web server in a `server.py` file. The key is to only load the model **once** to prevent unnecessary copies of it from consuming memory. + +Create a pipeline to fill in the masked token, `[MASK]`. + +```py +from starlette.applications import Starlette +from starlette.responses import JSONResponse +from starlette.routing import Route +from transformers import pipeline +import asyncio + +async def homepage(request): + payload = await request.body() + string = payload.decode("utf-8") + response_q = asyncio.Queue() + await request.app.model_queue.put((string, response_q)) + output = await response_q.get() + return JSONResponse(output) + +async def server_loop(q): + pipe = pipeline(task="fill-mask",model="google-bert/bert-base-uncased") + while True: + (string, response_q) = await q.get() + out = pipe(string) + await response_q.put(out) + +app = Starlette( + routes=[ + Route("/", homepage, methods=["POST"]), + ], +) + +@app.on_event("startup") +async def startup_event(): + q = asyncio.Queue() + app.model_queue = q + asyncio.create_task(server_loop(q)) +``` + +Start the server with the following command. + +```bash +uvicorn server:app +``` + +Query the server with a POST request. + +```bash +curl -X POST -d "Paris is the [MASK] of France." http://localhost:8000/ +``` +This should return the output below. + +```bash +[{'score': 0.9969332218170166, + 'token': 3007, + 'token_str': 'capital', + 'sequence': 'paris is the capital of france.'}, + {'score': 0.0005914849461987615, + 'token': 2540, + 'token_str': 'heart', + 'sequence': 'paris is the heart of france.'}, + {'score': 0.00043787318281829357, + 'token': 2415, + 'token_str': 'center', + 'sequence': 'paris is the center of france.'}, + {'score': 0.0003378340043127537, + 'token': 2803, + 'token_str': 'centre', + 'sequence': 'paris is the centre of france.'}, + {'score': 0.00026995912776328623, + 'token': 2103, + 'token_str': 'city', + 'sequence': 'paris is the city of france.'}] +``` + +## Queuing requests + +The server's queuing mechanism can be used for some interesting applications such as dynamic batching. Dynamic batching accumulates several requests first before processing them with [`Pipeline`]. + +The example below is written in pseudocode for readability rather than performance, in particular, you'll notice that: + +1. There is no batch size limit. +2. The timeout is reset on every queue fetch, so you could end up waiting much longer than the `timeout` value before processing a request. This would also delay the first inference request by that amount of time. The web server always waits 1ms even if the queue is empty, which is inefficient, because that time can be used to start inference. It could make sense though if batching is essential to your use case. + + It would be better to have a single 1ms deadline, instead of resetting it on every fetch, as shown below. + +```py +async def server_loop(q): + pipe = pipeline(task="fill-mask", model="google-bert/bert-base-uncased") + while True: + (string, rq) = await q.get() + strings = [] + queues = [] + strings.append(string) + queues.append(rq) + while True: + try: + (string, rq) = await asyncio.wait_for(q.get(), timeout=1) + except asyncio.exceptions.TimeoutError: + break + strings.append(string) + queues.append(rq) + outs = pipe(strings, batch_size=len(strings)) + for rq, out in zip(queues, outs): + await rq.put(out) +``` + +## Error checking + +There are many things that can go wrong in production. You could run out-of-memory, out of space, fail to load a model, have an incorrect model configuration, have an incorrect query, and so much more. + +Adding `try...except` statements is helpful for returning these errors to the user for debugging. Keep in mind this could be a security risk if you shouldn't be revealing certain information. + +## Circuit breaking + +Try to return a 503 or 504 error when the server is overloaded instead of forcing a user to wait indefinitely. + +It is relatively simple to implement these error types since it's only a single queue. Take a look at the queue size to determine when to start returning errors before your server fails under load. + +## Block the main thread + +PyTorch is not async aware, so computation will block the main thread from running. + +For this reason, it's better to run PyTorch on its own separate thread or process. When inference of a single request is especially long (more than 1s), it's even more important because it means every query during inference must wait 1s before even receiving an error. + +## Dynamic batching + +Dynamic batching can be very effective when used in the correct setting, but it's not necessary when you're only passing 1 request at a time (see [batch inference](./pipeline_tutorial#batch-inference) for more details). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pr_checks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pr_checks.md new file mode 100644 index 0000000000000000000000000000000000000000..efddf3a5b1690a73ab59751098f92954b53f43c3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/pr_checks.md @@ -0,0 +1,200 @@ + + +# Checks on a Pull Request + +When you open a pull request on ๐Ÿค— Transformers, a fair number of checks will be run to make sure the patch you are adding is not breaking anything existing. Those checks are of four types: +- regular tests +- documentation build +- code and documentation style +- general repository consistency + +In this document, we will take a stab at explaining what those various checks are and the reason behind them, as well as how to debug them locally if one of them fails on your PR. + +Note that, ideally, they require you to have a dev install: + +```bash +pip install transformers[dev] +``` + +or for an editable install: + +```bash +pip install -e .[dev] +``` + +inside the Transformers repo. Since the number of optional dependencies of Transformers has grown a lot, it's possible you don't manage to get all of them. If the dev install fails, make sure to install the Deep Learning framework you are working with (PyTorch, TensorFlow and/or Flax) then do + +```bash +pip install transformers[quality] +``` + +or for an editable install: + +```bash +pip install -e .[quality] +``` + + +## Tests + +All the jobs that begin with `ci/circleci: run_tests_` run parts of the Transformers testing suite. Each of those jobs focuses on a part of the library in a certain environment: for instance `ci/circleci: run_tests_pipelines_tf` runs the pipelines test in an environment where TensorFlow only is installed. + +Note that to avoid running tests when there is no real change in the modules they are testing, only part of the test suite is run each time: a utility is run to determine the differences in the library between before and after the PR (what GitHub shows you in the "Files changes" tab) and picks the tests impacted by that diff. That utility can be run locally with: + +```bash +python utils/tests_fetcher.py +``` + +from the root of the Transformers repo. It will: + +1. Check for each file in the diff if the changes are in the code or only in comments or docstrings. Only the files with real code changes are kept. +2. Build an internal map that gives for each file of the source code of the library all the files it recursively impacts. Module A is said to impact module B if module B imports module A. For the recursive impact, we need a chain of modules going from module A to module B in which each module imports the previous one. +3. Apply this map on the files gathered in step 1, which gives us the list of model files impacted by the PR. +4. Map each of those files to their corresponding test file(s) and get the list of tests to run. + +When executing the script locally, you should get the results of step 1, 3 and 4 printed and thus know which tests are run. The script will also create a file named `test_list.txt` which contains the list of tests to run, and you can run them locally with the following command: + +```bash +python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) +``` + +Just in case anything slipped through the cracks, the full test suite is also run daily. + +## Documentation build + +The `build_pr_documentation` job builds and generates a preview of the documentation to make sure everything looks okay once your PR is merged. A bot will add a link to preview the documentation in your PR. Any changes you make to the PR are automatically updated in the preview. If the documentation fails to build, click on **Details** next to the failed job to see where things went wrong. Often, the error is as simple as a missing file in the `toctree`. + +If you're interested in building or previewing the documentation locally, take a look at the [`README.md`](https://github.com/huggingface/transformers/tree/main/docs) in the docs folder. + +## Code and documentation style + +Code formatting is applied to all the source files, the examples and the tests using `black` and `ruff`. We also have a custom tool taking care of the formatting of docstrings and `rst` files (`utils/style_doc.py`), as well as the order of the lazy imports performed in the Transformers `__init__.py` files (`utils/custom_init_isort.py`). All of this can be launched by executing + +```bash +make style +``` + +The CI checks those have been applied inside the `ci/circleci: check_code_quality` check. It also runs `ruff`, that will have a basic look at your code and will complain if it finds an undefined variable, or one that is not used. To run that check locally, use + +```bash +make quality +``` + +This can take a lot of time, so to run the same thing on only the files you modified in the current branch, run + +```bash +make fixup +``` + +This last command will also run all the additional checks for the repository consistency. Let's have a look at them. + +## Repository consistency + +This regroups all the tests to make sure your PR leaves the repository in a good state, and is performed by the `ci/circleci: check_repository_consistency` check. You can locally run that check by executing the following: + +```bash +make repo-consistency +``` + +This checks that: + +- All objects added to the init are documented (performed by `utils/check_repo.py`) +- All `__init__.py` files have the same content in their two sections (performed by `utils/check_inits.py`) +- All code identified as a copy from another module is consistent with the original (performed by `utils/check_copies.py`) +- All configuration classes have at least one valid checkpoint mentioned in their docstrings (performed by `utils/check_config_docstrings.py`) +- All configuration classes only contain attributes that are used in corresponding modeling files (performed by `utils/check_config_attributes.py`) +- The translations of the READMEs and the index of the doc have the same model list as the main README (performed by `utils/check_copies.py`) +- The auto-generated tables in the documentation are up to date (performed by `utils/check_table.py`) +- The library has all objects available even if not all optional dependencies are installed (performed by `utils/check_dummies.py`) +- All docstrings properly document the arguments in the signature of the object (performed by `utils/check_docstrings.py`) + +Should this check fail, the first two items require manual fixing, the last four can be fixed automatically for you by running the command + +```bash +make fix-copies +``` + +Additional checks concern PRs that add new models, mainly that: + +- All models added are in an Auto-mapping (performed by `utils/check_repo.py`) + +- All models are properly tested (performed by `utils/check_repo.py`) + + + +### Check copies + +Since the Transformers library is very opinionated with respect to model code, and each model should fully be implemented in a single file without relying on other models, we have added a mechanism that checks whether a copy of the code of a layer of a given model stays consistent with the original. This way, when there is a bug fix, we can see all other impacted models and choose to trickle down the modification or break the copy. + + + +If a file is a full copy of another file, you should register it in the constant `FULL_COPIES` of `utils/check_copies.py`. + + + +This mechanism relies on comments of the form `# Copied from xxx`. The `xxx` should contain the whole path to the class of function which is being copied below. For instance, `RobertaSelfOutput` is a direct copy of the `BertSelfOutput` class, so you can see [here](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L289) it has a comment: + +```py +# Copied from transformers.models.bert.modeling_bert.BertSelfOutput +``` + +Note that instead of applying this to a whole class, you can apply it to the relevant methods that are copied from. For instance [here](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L598) you can see how `RobertaPreTrainedModel._init_weights` is copied from the same method in `BertPreTrainedModel` with the comment: + +```py +# Copied from transformers.models.bert.modeling_bert.BertPreTrainedModel._init_weights +``` + +Sometimes the copy is exactly the same except for names: for instance in `RobertaAttention`, we use `RobertaSelfAttention` instead of `BertSelfAttention` but other than that, the code is exactly the same. This is why `# Copied from` supports simple string replacements with the following syntax: `Copied from xxx with foo->bar`. This means the code is copied with all instances of `foo` being replaced by `bar`. You can see how it used [here](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L304C1-L304C86) in `RobertaAttention` with the comment: + +```py +# Copied from transformers.models.bert.modeling_bert.BertAttention with Bert->Roberta +``` + +Note that there shouldn't be any spaces around the arrow (unless that space is part of the pattern to replace of course). + +You can add several patterns separated by a comma. For instance here `CamemberForMaskedLM` is a direct copy of `RobertaForMaskedLM` with two replacements: `Roberta` to `Camembert` and `ROBERTA` to `CAMEMBERT`. You can see [here](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/camembert/modeling_camembert.py#L929) this is done with the comment: + +```py +# Copied from transformers.models.roberta.modeling_roberta.RobertaForMaskedLM with Roberta->Camembert, ROBERTA->CAMEMBERT +``` + +If the order matters (because one of the replacements might conflict with a previous one), the replacements are executed from left to right. + + + +If the replacements change the formatting (if you replace a short name by a very long name for instance), the copy is checked after applying the auto-formatter. + + + +Another way when the patterns are just different casings of the same replacement (with an uppercased and a lowercased variants) is just to add the option `all-casing`. [Here](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/mobilebert/modeling_mobilebert.py#L1237) is an example in `MobileBertForSequenceClassification` with the comment: + +```py +# Copied from transformers.models.bert.modeling_bert.BertForSequenceClassification with Bert->MobileBert all-casing +``` + +In this case, the code is copied from `BertForSequenceClassification` by replacing: +- `Bert` by `MobileBert` (for instance when using `MobileBertModel` in the init) +- `bert` by `mobilebert` (for instance when defining `self.mobilebert`) +- `BERT` by `MOBILEBERT` (in the constant `MOBILEBERT_INPUTS_DOCSTRING`) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/processors.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/processors.md new file mode 100644 index 0000000000000000000000000000000000000000..bb2a149ce620ba145dd57a018f70dfdee4f4a204 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/processors.md @@ -0,0 +1,131 @@ + + +# Processors + +Multimodal models require a preprocessor capable of handling inputs that combine more than one modality. Depending on the input modality, a processor needs to convert text into an array of tensors, images into pixel values, and audio into an array with tensors with the correct sampling rate. + +For example, [PaliGemma](./model_doc/paligemma) is a vision-language model that uses the [SigLIP](./model_doc/siglip) image processor and the [Llama](./model_doc/llama) tokenizer. A [`ProcessorMixin`] class wraps both of these preprocessor types, providing a single and unified processor class for a multimodal model. + +Call [`~ProcessorMixin.from_pretrained`] to load a processor. Pass the input type to the processor to generate the expected model inputs, input ids and pixel values. + +```py +from transformers import AutoProcessor, PaliGemmaForConditionalGeneration +from PIL import Image +import requests + +processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-224") + +prompt = "answer en Where is the cat standing?" +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +image = Image.open(requests.get(url, stream=True).raw) + +inputs = processor(text=prompt, images=image, return_tensors="pt") +inputs +``` + +This guide describes the processor class and how to preprocess multimodal inputs. + +## Processor classes + +All processors inherit from the [`ProcessorMixin`] class which provides methods like [`~ProcessorMixin.from_pretrained`], [`~ProcessorMixin.save_pretrained`], and [`~ProcessorMixin.push_to_hub`] for loading, saving, and sharing processors to the Hub. + +There are two ways to load a processor, with an [`AutoProcessor`] and with a model-specific processor class. + + + + +The [AutoClass](./model_doc/auto) API provides a simple interface to load processors without directly specifying the specific model class it belongs to. + +Use [`~AutoProcessor.from_pretrained`] to load a processor. + +```py +from transformers import AutoProcessor + +processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-224") +``` + + + + +Processors are also associated with a specific pretrained multimodal model class. You can load a processor directly from the model class with [`~ProcessorMixin.from_pretrained`]. + +```py +from transformers import WhisperProcessor + +processor = WhisperProcessor.from_pretrained("openai/whisper-tiny") +``` + +You could also separately load the two preprocessor types, [`WhisperTokenizerFast`] and [`WhisperFeatureExtractor`]. + +```py +from transformers import WhisperTokenizerFast, WhisperFeatureExtractor, WhisperProcessor + +tokenizer = WhisperTokenizerFast.from_pretrained("openai/whisper-tiny") +feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-tiny") +processor = WhisperProcessor(feature_extractor=feature_extractor, tokenizer=tokenizer) +``` + + + + +## Preprocess + +Processors preprocess multimodal inputs into the expected Transformers format. There are a couple combinations of input modalities that a processor can handle such as text and audio or text and image. + +Automatic speech recognition (ASR) tasks require a processor that can handle text and audio inputs. Load a dataset and take a look at the `audio` and `text` columns (you can remove the other columns which aren't needed). + +```py +from datasets import load_dataset + +dataset = load_dataset("lj_speech", split="train") +dataset = dataset.map(remove_columns=["file", "id", "normalized_text"]) +dataset[0]["audio"] +{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., + 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 22050} + +dataset[0]["text"] +'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition' +``` + +Remember to resample the sampling rate to match the pretrained models required sampling rate. + +```py +from datasets import Audio + +dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +``` + +Load a processor and pass the audio `array` and `text` columns to it. + +```py +from transformers import AutoProcessor + +processor = AutoProcessor.from_pretrained("openai/whisper-tiny") + +def prepare_dataset(example): + audio = example["audio"] + example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) + return example +``` + +Apply the `prepare_dataset` function to preprocess the dataset. The processor returns `input_features` for the `audio` column and `labels` for the text column. + +```py +prepare_dataset(dataset[0]) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/aqlm.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/aqlm.md new file mode 100644 index 0000000000000000000000000000000000000000..49bd7f89bc92be0f51ed190ee8163e95060040aa --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/aqlm.md @@ -0,0 +1,56 @@ + + +# AQLM + +Additive Quantization of Language Models ([AQLM](https://huggingface.co/papers/2401.06118)) quantizes multiple weights together and takes advantage of interdependencies between them. AQLM represents groups of 8-16 weights as a sum of multiple vector codes. + +AQLM also supports fine-tuning with [LoRA](https://huggingface.co/docs/peft/package_reference/lora) with the [PEFT](https://huggingface.co/docs/peft) library, and is fully compatible with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) for even faster inference and training. + +Run the command below to install the AQLM library with kernel support for both GPU and CPU inference and training. AQLM only works with Python 3.10+. + +```bash +pip install aqlm[gpu,cpu] +``` + +Load an AQLM-quantized model with [`~PreTrainedModel.from_pretrained`]. + +```python +from transformers import AutoTokenizer, AutoModelForCausalLM + +quantized_model = AutoModelForCausalLM.from_pretrained( + "ISTA-DASLab/Mixtral-8x7b-AQLM-2Bit-1x16-hf", + torch_dtype="auto", + device_map="auto" +) +``` + +## Configurations + +AQLM quantization setups vary mainly in the number of codebooks used, as well as codebook sizes in bits. The most popular setups and supported inference kernels are shown below. + +| Kernel | Number of codebooks | Codebook size, bits | Notation | Accuracy | Speedup | Fast GPU inference | Fast CPU inference | +|---|---------------------|---------------------|----------|-------------|-------------|--------------------|--------------------| +| Triton | K | N | KxN | - | Up to ~0.7x | โœ… | โŒ | +| CUDA | 1 | 16 | 1x16 | Best | Up to ~1.3x | โœ… | โŒ | +| CUDA | 2 | 8 | 2x8 | OK | Up to ~3.0x | โœ… | โŒ | +| Numba | K | 8 | Kx8 | Good | Up to ~4.0x | โŒ | โœ… | + +## Resources + +Run the AQLM demo [notebook](https://colab.research.google.com/drive/1-xZmBRXT5Fm3Ghn4Mwa2KRypORXb855X?usp=sharing) for more examples of how to quantize a model, push a quantized model to the Hub, and more. + +For more example demo notebooks, visit the AQLM [repository](https://github.com/Vahe1994/AQLM). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/auto_round.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/auto_round.md new file mode 100644 index 0000000000000000000000000000000000000000..174a726dc35e3fd4044580268e1772a40b0e0a5e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/auto_round.md @@ -0,0 +1,286 @@ + + +# AutoRound + +[AutoRound](https://github.com/intel/auto-round) is an advanced quantization algorithm that delivers strong accuracy, even at 2-bit precision. +It leverages sign gradient descent to fine-tune both rounding values and min-max clipping thresholds in just 200 steps. Designed for broad compatibility, it seamlessly supports a wide range of LLMs and is actively expanding to cover more VLMs as well. +It also supports quantization and inference across multiple hardware platforms, including CPU, XPU, and CUDA. + +AutoRound also offers a variety of useful features, including mixed-bit tuning and inference, lm-head quantization, support for exporting to formats like GPTQ/AWQ/GGUF, and flexible tuning recipes. +For a comprehensive overview and the latest updates, check out the AutoRound [README](https://github.com/intel/auto-round). + +AutoRound was originally developed as part of the [Intel Neural Compressor](https://github.com/intel/neural-compressor), serving as a general-purpose model compression library for deep learning. +It has since evolved into a standalone library focused specifically on low-precision optimization for large language models (LLMs). +AutoRound remains fully integrated with the Intel Neural Compressor, and you can explore the repository for more details. + + +## Installation + +```bash +pip install auto-round +``` + +## Supported Quantization Configurations + +AutoRound supports several quantization configurations: + +- **Int8 Weight Only** +- **Int4 Weight Only** +- **Int3 Weight Only** +- **Int2 Weight Only** +- **Mixed bits Weight only** + +## Hardware Compatibility + +CPU, XPU, and CUDA for both quantization and inference. + +## Quantization and Serialization (offline) + +Currently, only offline mode is supported to generate quantized models. + + + + +### Command Line Usage +```bash +auto-round \ + --model facebook/opt-125m \ + --bits 4 \ + --group_size 128 \ + --output_dir ./tmp_autoround +``` + +AutoRound also offer another two recipes, `auto-round-best` and `auto-round-light`, designed for optimal accuracy and improved speed, respectively. +For 2 bits, we recommend using `auto-round-best` or `auto-round`. + + + + +### AutoRound API Usage +This setting offers a better trade-off between accuracy and tuning cost, and is recommended in all scenarios. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +from auto_round import AutoRound + +model_name = "facebook/opt-125m" +model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +bits, group_size, sym = 4, 128, True +# mixed bits config +# layer_config = {"model.decoder.layers.6.self_attn.out_proj": {"bits": 2, "group_size": 32}} +autoround = AutoRound( + model, + tokenizer, + bits=bits, + group_size=group_size, + sym=sym, + # enable_torch_compile=True, + # layer_config=layer_config, +) + +output_dir = "./tmp_autoround" +# format= 'auto_round'(default), 'auto_gptq', 'auto_awq' +autoround.quantize_and_save(output_dir, format='auto_round') +``` + + + + + +### AutoRoundBest recipe +This setting provides the best accuracy in most scenarios but is 4โ€“5ร— slower than the standard AutoRound recipe. It is especially recommended for 2-bit quantization and is a good choice if sufficient resources are available. +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +from auto_round import AutoRound + +model_name = "facebook/opt-125m" +model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +bits, group_size, sym = 4, 128, True +autoround = AutoRound( + model, + tokenizer, + bits=bits, + group_size=group_size, + sym=sym, + nsamples=512, + iters=1000, + low_gpu_mem_usage=True +) + +output_dir = "./tmp_autoround" +autoround.quantize_and_save(output_dir, format='auto_round') +``` + + + + +### AutoRoundLight recipe +This setting offers the best speed (2 - 3X faster than AutoRound), but it may cause a significant accuracy drop for small models and 2-bit quantization. It is recommended for 4-bit settings and models larger than 3B. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +from auto_round import AutoRound + +model_name = "facebook/opt-125m" +model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +bits, group_size, sym = 4, 128, True +autoround = AutoRound( + model, + tokenizer, + bits=bits, + group_size=group_size, + sym=sym, + iters=50, + lr=5e-3, +) + +output_dir = "./tmp_autoround" +autoround.quantize_and_save(output_dir, format='auto_round') +``` + + + + + +W4G128 Average Accuracy of 13 tasks (mmlu-pro, if_eval, gsm8k, etc) and Time Cost Results (Testing was conducted on the Nvidia A100 80G using the version of PyTorch 2.6.0 with enable_torch_compile): + +| Model | Qwen2.5-0.5B-Instruct | Falcon3-3B | Qwen2.5-7B-Instruct | Meta-Llama-3.1-8B-Instruct | Falcon3-10B | Qwen2.5-72B-Instruct | +|---------|--------------------|---------------|------------------|----------------------------|---------------|-------------------| +| 16bits | 0.4192 | 0.5203 | 0.6470 | 0.6212 | 0.6151 | 0.7229 | +| Best | **0.4137**(7m) | **0.5142**(23m) | 0.6426(58m) | **0.6116**(65m) | **0.6092**(81m) | 0.7242(575m) | +| Default | 0.4129(2m) | 0.5133(6m) | 0.6441(13m) | 0.6106(13m) | 0.6080(18m) | **0.7252**(118m) | +| Light | 0.4052(2m) | 0.5108(3m) | **0.6453**(5m) | 0.6104(6m) | 0.6063(6m) | 0.7243(37m) | + +## Inference + +AutoRound automatically selects the best available backend based on the installed libraries and prompts the user to install additional libraries when a better backend is found. + + + +### CPU + +Supports 2, 4, and 8 bits. We recommend using intel-extension-for-pytorch (IPEX) for 4 bits inference. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_name = "OPEA/Qwen2.5-1.5B-Instruct-int4-sym-inc" +model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cpu", torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +text = "There is a girl who likes adventure," +inputs = tokenizer(text, return_tensors="pt").to(model.device) +print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50, do_sample=False)[0])) +``` + + + + + +### XPU + +Supports 4 bits only. We recommend using intel-extension-for-pytorch (IPEX) for inference. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_name = "OPEA/Qwen2.5-1.5B-Instruct-int4-sym-inc" +model = AutoModelForCausalLM.from_pretrained(model_name, device_map="xpu", torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +text = "There is a girl who likes adventure," +inputs = tokenizer(text, return_tensors="pt").to(model.device) +print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50, do_sample=False)[0])) +``` + + + + + +### CUDA + +Supports 2, 3, 4, and 8 bits. We recommend using GPTQModel for 4 and 8 bits inference. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_name = "OPEA/Qwen2.5-1.5B-Instruct-int4-sym-inc" +model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cuda", torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +text = "There is a girl who likes adventure," +inputs = tokenizer(text, return_tensors="pt").to(model.device) +print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50, do_sample=False)[0])) +``` + + + + + +### Specify Inference Backend + +AutoRound automatically selects the backend for each layer based on compatibility. In general, the priority order is Marlin > ExLLaMAV2 > Triton, but the final choice depends on factors such as group size, bit width, packing format, hardware device, and other implementation details. For more details, please refer to [backends](https://github.com/intel/auto-round?tab=readme-ov-file#specify-backend), + +The backend may not always be the most suitable for certain devices. +You can specify your preferred backend such as "ipex" for CPU, "ipex/triton" for XPU, "marlin/exllamav2/triton" for CUDA, according to your needs or hardware compatibility. Please note that additional corresponding libraries may be required. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, AutoRoundConfig + +model_name = "OPEA/Qwen2.5-1.5B-Instruct-int4-sym-inc" +quantization_config = AutoRoundConfig(backend="ipex") +model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cpu", quantization_config=quantization_config, torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +text = "There is a girl who likes adventure," +inputs = tokenizer(text, return_tensors="pt").to(model.device) +print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50, do_sample=False)[0])) +``` + + + + + + +### Convert GPTQ/AWQ to AutoRound + +Most GPTQ/AWQ models can be converted to the AutoRound format for better compatibility and support with Intel devices. Please note that the quantization config will be changed if the model is serialized. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, AutoRoundConfig + +model_name = "ybelkada/opt-125m-gptq-4bit" +quantization_config = AutoRoundConfig() +model = AutoModelForCausalLM.from_pretrained(model_name, device_map="cpu", quantization_config=quantization_config, torch_dtype="auto") +tokenizer = AutoTokenizer.from_pretrained(model_name) +text = "There is a girl who likes adventure," +inputs = tokenizer(text, return_tensors="pt").to(model.device) +print(tokenizer.decode(model.generate(**inputs, max_new_tokens=50, do_sample=False)[0])) +``` + + + + + +## Issues + +If you encounter any issues with the transformers integration, please open an issue on +the [transformers](https://github.com/huggingface/transformers/issues) repository. +If you encounter any issues with auto-round, please open an issue on +the [AutoRound](https://github.com/intel/auto-round/issues) repository. + + +## Acknowledgement +Special thanks to open-source low precision libraries such as AutoGPTQ, AutoAWQ, GPTQModel, Triton, Marlin, and ExLLaMAV2 for providing low-precision CUDA kernels, which are leveraged in AutoRound. + +## Contribution +Contributions to [AutoRound](https://github.com/intel/auto-round/pulls) are welcome and greatly appreciated! +Whether it's fixing bugs, improving documentation, adding new features, or suggesting improvements, your help is always valued. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/awq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/awq.md new file mode 100644 index 0000000000000000000000000000000000000000..aa57da0f811f06da3727523d8dd2e49398cde9fb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/awq.md @@ -0,0 +1,251 @@ + + +# AWQ + +[Activation-aware Weight Quantization (AWQ)](https://hf.co/papers/2306.00978) preserves a small fraction of the weights that are important for LLM performance to compress a model to 4-bits with minimal performance degradation. + +There are several libraries for quantizing models with the AWQ algorithm, such as [llm-awq](https://github.com/mit-han-lab/llm-awq), [autoawq](https://github.com/casper-hansen/AutoAWQ) or [optimum-intel](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc). Transformers supports loading models quantized with the llm-awq and autoawq libraries. This guide will show you how to load models quantized with autoawq, but the process is similar for llm-awq quantized models. + +Run the command below to install autoawq + +```bash +pip install autoawq +``` +> [!WARNING] +> AutoAWQ downgrades Transformers to version 4.47.1. If you want to do inference with AutoAWQ, you may need to reinstall your Transformers' version after installing AutoAWQ. + +Identify an AWQ-quantized model by checking the `quant_method` key in the models [config.json](https://huggingface.co/TheBloke/zephyr-7B-alpha-AWQ/blob/main/config.json) file. + +```json +{ + "_name_or_path": "/workspace/process/huggingfaceh4_zephyr-7b-alpha/source", + "architectures": [ + "MistralForCausalLM" + ], + ... + ... + ... + "quantization_config": { + "quant_method": "awq", + "zero_point": true, + "group_size": 128, + "bits": 4, + "version": "gemm" + } +} +``` + +Load the AWQ-quantized model with [`~PreTrainedModel.from_pretrained`]. This automatically sets the other weights to fp16 by default for performance reasons. Use the `torch_dtype` parameter to load these other weights in a different format. + +If the model is loaded on the CPU, use the `device_map` parameter to move it to a GPU. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/zephyr-7B-alpha-AWQ", + torch_dtype=torch.float32, + device_map="cuda:0" +) +``` + +Use `attn_implementation` to enable [FlashAttention2](../perf_infer_gpu_one#flashattention-2) to further accelerate inference. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/zephyr-7B-alpha-AWQ", + attn_implementation="flash_attention_2", + device_map="cuda:0" +) +``` + +## Fused modules + +Fused modules offer improved accuracy and performance. They are supported out-of-the-box for AWQ modules for [Llama](https://huggingface.co/meta-llama) and [Mistral](https://huggingface.co/mistralai/Mistral-7B-v0.1) architectures, but you can also fuse AWQ modules for unsupported architectures. + +> [!WARNING] +> Fused modules cannot be combined with other optimization techniques such as FlashAttention2. + + + + +Create an [`AwqConfig`] and set the parameters `fuse_max_seq_len` and `do_fuse=True` to enable fused modules. The `fuse_max_seq_len` parameter is the total sequence length and it should include the context length and the expected generation length. Set it to a larger value to be safe. + +The example below fuses the AWQ modules of the [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) model. + +```python +import torch +from transformers import AwqConfig, AutoModelForCausalLM + +quantization_config = AwqConfig( + bits=4, + fuse_max_seq_len=512, + do_fuse=True, +) +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/Mistral-7B-OpenOrca-AWQ", + quantization_config=quantization_config +).to(0) +``` + +The [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) model was benchmarked with `batch_size=1` with and without fused modules. + +
Unfused module
+ +| Batch Size | Prefill Length | Decode Length | Prefill tokens/s | Decode tokens/s | Memory (VRAM) | +|-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| +| 1 | 32 | 32 | 60.0984 | 38.4537 | 4.50 GB (5.68%) | +| 1 | 64 | 64 | 1333.67 | 31.6604 | 4.50 GB (5.68%) | +| 1 | 128 | 128 | 2434.06 | 31.6272 | 4.50 GB (5.68%) | +| 1 | 256 | 256 | 3072.26 | 38.1731 | 4.50 GB (5.68%) | +| 1 | 512 | 512 | 3184.74 | 31.6819 | 4.59 GB (5.80%) | +| 1 | 1024 | 1024 | 3148.18 | 36.8031 | 4.81 GB (6.07%) | +| 1 | 2048 | 2048 | 2927.33 | 35.2676 | 5.73 GB (7.23%) | + +
Fused module
+ +| Batch Size | Prefill Length | Decode Length | Prefill tokens/s | Decode tokens/s | Memory (VRAM) | +|-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| +| 1 | 32 | 32 | 81.4899 | 80.2569 | 4.00 GB (5.05%) | +| 1 | 64 | 64 | 1756.1 | 106.26 | 4.00 GB (5.05%) | +| 1 | 128 | 128 | 2479.32 | 105.631 | 4.00 GB (5.06%) | +| 1 | 256 | 256 | 1813.6 | 85.7485 | 4.01 GB (5.06%) | +| 1 | 512 | 512 | 2848.9 | 97.701 | 4.11 GB (5.19%) | +| 1 | 1024 | 1024 | 3044.35 | 87.7323 | 4.41 GB (5.57%) | +| 1 | 2048 | 2048 | 2715.11 | 89.4709 | 5.57 GB (7.04%) | + +The speed and throughput of fused and unfused modules were also tested with the [optimum-benchmark](https://github.com/huggingface/optimum-benchmark) library. + +
+
+ generate throughput per batch size +
forward peak memory/batch size
+
+
+ forward latency per batch size +
generate throughput/batch size
+
+
+ +
+ + +For architectures that don't support fused modules, create an [`AwqConfig`] and define a custom fusing mapping in `modules_to_fuse` to determine which modules need to be fused. + +The example below fuses the AWQ modules of the [TheBloke/Yi-34B-AWQ](https://huggingface.co/TheBloke/Yi-34B-AWQ) model. + +```python +import torch +from transformers import AwqConfig, AutoModelForCausalLM + +quantization_config = AwqConfig( + bits=4, + fuse_max_seq_len=512, + modules_to_fuse={ + "attention": ["q_proj", "k_proj", "v_proj", "o_proj"], + "layernorm": ["ln1", "ln2", "norm"], + "mlp": ["gate_proj", "up_proj", "down_proj"], + "use_alibi": False, + "num_attention_heads": 56, + "num_key_value_heads": 8, + "hidden_size": 7168 + } +) + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/Yi-34B-AWQ", + quantization_config=quantization_config +).to(0) +``` + +The parameter `modules_to_fuse` should include the following keys. + +- `"attention"`: The names of the attention layers to fuse in the following order: query, key, value and output projection layer. If you don't want to fuse these layers, pass an empty list. +- `"layernorm"`: The names of all the LayerNorm layers you want to replace with a custom fused LayerNorm. If you don't want to fuse these layers, pass an empty list. +- `"mlp"`: The names of the MLP layers you want to fuse into a single MLP layer in the order: (gate (dense, layer, post-attention) / up / down layers). +- `"use_alibi"`: If your model uses ALiBi positional embedding. +- `"num_attention_heads"`: The number of attention heads. +- `"num_key_value_heads"`: The number of key value heads that should be used to implement Grouped Query Attention (GQA). + + | parameter value | attention | + |---|---| + | `num_key_value_heads=num_attention_heads` | Multi-Head Attention | + | `num_key_value_heads=1` | Multi-Query Attention | + | `num_key_value_heads=...` | Grouped Query Attention | + +- `"hidden_size"`: The dimension of the hidden representations. + + +
+ +## ExLlamaV2 + +[ExLlamaV2](https://github.com/turboderp/exllamav2) kernels support faster prefill and decoding. Run the command below to install the latest version of autoawq with ExLlamaV2 support. + +```bash +pip install git+https://github.com/casper-hansen/AutoAWQ.git +``` + +Set `version="exllama"` in [`AwqConfig`] to enable ExLlamaV2 kernels. + +> [!TIP] +> ExLlamaV2 is supported on AMD GPUs. + +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig + +quantization_config = AwqConfig(version="exllama") + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/Mistral-7B-Instruct-v0.1-AWQ", + quantization_config=quantization_config, + device_map="auto", +) +``` + +## CPU + +[Intel Extension for PyTorch (IPEX)](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/) is designed to enable performance optimizations on Intel hardware. Run the command below to install the latest version of autoawq with IPEX support. + +```bash +pip install intel-extension-for-pytorch # for IPEX-GPU refer to https://intel.github.io/intel-extension-for-pytorch/xpu/2.5.10+xpu/ +pip install git+https://github.com/casper-hansen/AutoAWQ.git +``` + +Set `version="ipex"` in [`AwqConfig`] to enable ExLlamaV2 kernels. + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig + +device = "cpu" # set to "xpu" for Intel GPU +quantization_config = AwqConfig(version="ipex") + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/TinyLlama-1.1B-Chat-v0.3-AWQ", + quantization_config=quantization_config, + device_map=device, +) +``` + +## Resources + +Run the AWQ demo [notebook](https://colab.research.google.com/drive/1HzZH89yAXJaZgwJDhQj9LqSBux932BvY#scrollTo=Wwsg6nCwoThm) for more examples of how to quantize a model, push a quantized model to the Hub, and more. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitnet.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitnet.md new file mode 100644 index 0000000000000000000000000000000000000000..922210b2137b22d417ec4679f900c534f5d32d9b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitnet.md @@ -0,0 +1,48 @@ + + +# BitNet + +[BitNet](https://huggingface.co/papers/2402.17764) replaces traditional linear layers in Multi-Head Attention and feed-forward networks with specialized BitLinear layers. The BitLinear layers quantize the weights using ternary precision (with values of -1, 0, and 1) and quantize the activations to 8-bit precision. + +
+ Alt Text +
The architecture of BitNet with BitLinear layers.
+
+ +BitNet models can't be quantized on the fly. They need to be quantized during pretraining or fine-tuning because it is a Quantization-Aware Training (QAT) technique. During training, the weights are quantized to ternary values with symmetric per tensor quantization. + +1. Compute the average of the absolute values of the weight matrix and use as a scale. +2. Divide the weights by the scale, round the values, constrain them between -1 and 1, and rescale them to continue in full precision. +3. Activations are quantized to a specified bit-width (8-bit) using [absmax](https://huggingface.co/papers/2208.07339) quantization (symmetric per channel quantization). This involves scaling the activations into a range of [โˆ’128,127]. + +Refer to this [PR](https://github.com/huggingface/nanotron/pull/180) to pretrain or fine-tune a 1.58-bit model with [Nanotron](https://github.com/huggingface/nanotron). For fine-tuning, convert a model from the Hugging Face to Nanotron format. Find the conversion steps in this [PR](https://github.com/huggingface/nanotron/pull/174). + +Load a BitNet quantized model with [`~PreTrainedModel.from_pretrained`]. + +```py +from transformers import AutoModelForCausalLM +path = "/path/to/model" +model = AutoModelForCausalLM.from_pretrained(path, device_map="auto") +``` + +## Kernels + +`@torch.compile` is used to unpack the weights and perform the forward pass. Itโ€™s very straightforward to implement and delivers significant speed improvements. Additional optimized kernels will be integrated in future versions. + +## Resources + +Read [Fine-tuning LLMs to 1.58bit: extreme quantization made easy](https://huggingface.co/blog/1_58_llm_extreme_quantization) to learn more about how BitNet models are trained and fine-tuned. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitsandbytes.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitsandbytes.md new file mode 100644 index 0000000000000000000000000000000000000000..3ffdd89f2fd563f41237432d3d41f20b2eed47a2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/bitsandbytes.md @@ -0,0 +1,338 @@ + + +# Bitsandbytes + +The [bitsandbytes](https://github.com/bitsandbytes-foundation/bitsandbytes) library provides quantization tools for LLMs through a lightweight Python wrapper around CUDA functions. It enables working with large models using limited computational resources by reducing their memory footprint. + +At its core, bitsandbytes provides: + +- **Quantized Linear Layers**: `Linear8bitLt` and `Linear4bit` layers that replace standard PyTorch linear layers with memory-efficient quantized alternatives +- **Optimized Optimizers**: 8-bit versions of common optimizers through its `optim` module, enabling training of large models with reduced memory requirements +- **Matrix Multiplication**: Optimized matrix multiplication operations that leverage the quantized format + +bitsandbytes offers two main quantization features: + +1. **LLM.int8()** - An 8-bit quantization method that makes inference more accessible without significant performance degradation. Unlike naive quantization, [LLM.int8()](https://hf.co/papers/2208.07339) dynamically preserves higher precision for critical computations, preventing information loss in sensitive parts of the model. + +2. **QLoRA** - A 4-bit quantization technique that compresses models even further while maintaining trainability by inserting a small set of trainable low-rank adaptation (LoRA) weights. + +> **Note:** For a user-friendly quantization experience, you can use the `bitsandbytes` [community space](https://huggingface.co/spaces/bnb-community/bnb-my-repo). + + +Run the command below to install bitsandbytes. + +```bash +pip install --upgrade transformers accelerate bitsandbytes +``` +To compile from source, follow the instructions in the [bitsandbytes installation guide](https://huggingface.co/docs/bitsandbytes/main/en/installation). + +## Hardware Compatibility +bitsandbytes is currently only supported on CUDA GPUs for CUDA versions 11.0 - 12.8. However, there's an ongoing multi-backend effort under development, which is currently in alpha. If you're interested in providing feedback or testing, check out the [bitsandbytes repository](https://github.com/bitsandbytes-foundation/bitsandbytes) for more information. + +### CUDA + +| Feature | Minimum Hardware Requirement | +|---------|-------------------------------| +| 8-bit optimizers | NVIDIA Maxwell (GTX 900 series, TITAN X, M40) or newer GPUs * | +| LLM.int8() | NVIDIA Turing (RTX 20 series, T4) or newer GPUs | +| NF4/FP4 quantization | NVIDIA Maxwell (GTX 900 series, TITAN X, M40) or newer GPUs * | + +### Multi-backend + +| Backend | Supported Versions | Python versions | Architecture Support | Status | +|---------|-------------------|----------------|---------------------|---------| +| AMD ROCm | 6.1+ | 3.10+ | minimum CDNA - gfx90a, RDNA - gfx1100 | Alpha | +| Apple Silicon (MPS) | WIP | 3.10+ | M1/M2 chips | Planned | +| Intel CPU | v2.4.0+ (ipex) | 3.10+ | Intel CPU | Alpha | +| Intel GPU | v2.4.0+ (ipex) | 3.10+ | Intel GPU | Experimental | +| Ascend NPU | 2.1.0+ (torch_npu) | 3.10+ | Ascend NPU | Experimental | + +> **Note:** Bitsandbytes is moving away from the multi-backend approach towards using [Pytorch Custom Operators](https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html), as the main mechanism for supporting new hardware, and dispatching to the correct backend. + +## Quantization Examples + +Quantize a model by passing a [`BitsAndBytesConfig`] to [`~PreTrainedModel.from_pretrained`]. This works for any model in any modality, as long as it supports [Accelerate](https://huggingface.co/docs/accelerate/index) and contains [torch.nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) layers. + + + +
+Quantizing a model in 8-bit halves the memory-usage, and for large models, set `device_map="auto"` to efficiently distribute the weights across all available GPUs. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model_8bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + device_map="auto", + quantization_config=quantization_config +) +``` + +By default, all other modules such as [torch.nn.LayerNorm](https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html) are set to the default torch dtype. You can change the data type of these modules with the `torch_dtype` parameter. Setting `torch_dtype="auto"` loads the model in the data type defined in a model's `config.json` file. + +```py +import torch +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model_8bit = AutoModelForCausalLM.from_pretrained( + "facebook/opt-350m", + device_map="auto", + quantization_config=quantization_config, + torch_dtype="auto" +) +model_8bit.model.decoder.layers[-1].final_layer_norm.weight.dtype +``` + +Once a model is quantized to 8-bit, you can't push the quantized weights to the Hub unless you're using the latest version of Transformers and bitsandbytes. If you have the latest versions, then you can push the 8-bit model to the Hub with [`~PreTrainedModel.push_to_hub`]. The quantization config.json file is pushed first, followed by the quantized model weights. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-560m", + device_map="auto", + quantization_config=quantization_config +) + +model.push_to_hub("bloom-560m-8bit") +``` +
+
+ +
+Quantizing a model in 4-bit reduces your memory-usage by 4x, and for large models, set `device_map="auto"` to efficiently distribute the weights across all available GPUs. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) + +model_4bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + device_map="auto", + quantization_config=quantization_config +) +``` + +By default, all other modules such as [torch.nn.LayerNorm](https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html) are converted to `torch.float16`. You can change the data type of these modules with the `torch_dtype` parameter.. Setting `torch_dtype="auto"` loads the model in the data type defined in a model's `config.json` file. + +```py +import torch +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) + +model_4bit = AutoModelForCausalLM.from_pretrained( + "facebook/opt-350m", + device_map="auto", + quantization_config=quantization_config, + torch_dtype="auto" +) +model_4bit.model.decoder.layers[-1].final_layer_norm.weight.dtype +``` + +Make sure you have the latest bitsandbytes version so you can serialize 4-bit models and push them to the Hub with [`~PreTrainedModel.push_to_hub`]. Use [`~PreTrainedModel.save_pretrained`] to save the 4-bit model locally. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) + +model = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-560m", + device_map="auto", + quantization_config=quantization_config +) + +model.push_to_hub("bloom-560m-4bit") +``` +
+
+
+ +> [!WARNING] +> 8 and 4-bit training is only supported for training *extra* parameters. + +Check your memory footprint with `get_memory_footprint`. + +```py +print(model.get_memory_footprint()) +``` + +Load quantized models with [`~PreTrainedModel.from_pretrained`] without a `quantization_config`. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("{your_username}/bloom-560m-8bit", device_map="auto") +``` + +## LLM.int8 + +This section explores some of the specific features of 8-bit quantization, such as offloading, outlier thresholds, skipping module conversion, and finetuning. + +### Offloading + +8-bit models can offload weights between the CPU and GPU to fit very large models into memory. The weights dispatched to the CPU are stored in **float32** and aren't converted to 8-bit. For example, enable offloading for [bigscience/bloom-1b7](https://huggingface.co/bigscience/bloom-1b7) through [`BitsAndBytesConfig`]. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload=True) +``` + +Design a custom device map to fit everything on your GPU except for the `lm_head`, which is dispatched to the CPU. + +```py +device_map = { + "transformer.word_embeddings": 0, + "transformer.word_embeddings_layernorm": 0, + "lm_head": "cpu", + "transformer.h": 0, + "transformer.ln_f": 0, +} +``` + +Now load your model with the custom `device_map` and `quantization_config`. + +```py +model_8bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + torch_dtype="auto", + device_map=device_map, + quantization_config=quantization_config, +) +``` + +### Outlier threshold + +An "outlier" is a hidden state value greater than a certain threshold, and these values are computed in fp16. While the values are usually normally distributed ([-3.5, 3.5]), this distribution can be very different for large models ([-60, 6] or [6, 60]). 8-bit quantization works well for values ~5, but beyond that, there is a significant performance penalty. A good default threshold value is 6, but a lower threshold may be needed for more unstable models (small models or finetuning). + +To find the best threshold for your model, experiment with the `llm_int8_threshold` parameter in [`BitsAndBytesConfig`]. For example, setting the threshold to `0.0` significantly speeds up inference at the potential cost of some accuracy loss. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_threshold=0.0, + llm_int8_enable_fp32_cpu_offload=True +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + torch_dtype="auto", + device_map=device_map, + quantization_config=quantization_config, +) +``` + +### Skip module conversion + +For some models, like [Jukebox](model_doc/jukebox), you don't need to quantize every module to 8-bit because it can actually cause instability. With Jukebox, there are several `lm_head` modules that should be skipped using the `llm_int8_skip_modules` parameter in [`BitsAndBytesConfig`]. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_skip_modules=["lm_head"], +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config, +) +``` + +### Finetuning + +The [PEFT](https://github.com/huggingface/peft) library supports fine-tuning large models like [flan-t5-large](https://huggingface.co/google/flan-t5-large) and [facebook/opt-6.7b](https://huggingface.co/facebook/opt-6.7b) with 8-bit quantization. You don't need to pass the `device_map` parameter for training because it automatically loads your model on a GPU. However, you can still customize the device map with the `device_map` parameter (`device_map="auto"` should only be used for inference). + +## QLoRA + +This section explores some of the specific features of 4-bit quantization, such as changing the compute data type, the Normal Float 4 (NF4) data type, and nested quantization. + +### Compute data type + +Change the data type from float32 (the default value) to bf16 in [`BitsAndBytesConfig`] to speedup computation. + +```py +import torch +from transformers import BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16) +``` + +### Normal Float 4 (NF4) + +NF4 is a 4-bit data type from the [QLoRA](https://hf.co/papers/2305.14314) paper, adapted for weights initialized from a normal distribution. You should use NF4 for training 4-bit base models. + +```py +from transformers import BitsAndBytesConfig + +nf4_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", +) + +model_nf4 = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", quantization_config=nf4_config) +``` + +For inference, the `bnb_4bit_quant_type` does not have a huge impact on performance. However, to remain consistent with the model weights, you should use the `bnb_4bit_compute_dtype` and `torch_dtype` values. + +### Nested quantization + +Nested quantization can save additional memory at no additional performance cost. This feature performs a second quantization of the already quantized weights to save an additional 0.4 bits/parameter. For example, with nested quantization, you can finetune a [Llama-13b](https://huggingface.co/meta-llama/Llama-2-13b) model on a 16GB NVIDIA T4 GPU with a sequence length of 1024, a batch size of 1, and enable gradient accumulation with 4 steps. + +```py +from transformers import BitsAndBytesConfig + +double_quant_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_use_double_quant=True, +) + +model_double_quant = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf", torch_dtype="auto", quantization_config=double_quant_config) +``` + +## Dequantizing bitsandbytes models + +Once quantized, you can [`~PreTrainedModel.dequantize`] a model to the original precision but this may result in some quality loss. Make sure you have enough GPU memory to fit the dequantized model. + +```python +from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m", BitsAndBytesConfig(load_in_4bit=True)) +model.dequantize() +``` + +## Resources + +Learn more about the details of 8-bit quantization in [A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using Hugging Face Transformers, Accelerate and bitsandbytes](https://huggingface.co/blog/hf-bitsandbytes-integration). + +Try 4-bit quantization in this [notebook](https://colab.research.google.com/drive/1ge2F1QSK8Q7h0hn3YKuBCOAS0bK8E0wf) and learn more about it's details in [Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA](https://huggingface.co/blog/4bit-transformers-bitsandbytes). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/compressed_tensors.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/compressed_tensors.md new file mode 100644 index 0000000000000000000000000000000000000000..a3b01a1b44894e3dfa303c71e17b42102b0adda4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/compressed_tensors.md @@ -0,0 +1,190 @@ + + +# compressed-tensors + +[compressed-tensors](https://github.com/neuralmagic/compressed-tensors) extends [safetensors](https://github.com/huggingface/safetensors) files to compressed tensor data types to provide a unified checkpoint format for storing and loading various quantization and sparsity formats such dense, int-quantized (int8), float-quantized (fp8), and pack-quantized (int4 or int8 weight-quantized packed into int32). + +compressed-tensors supports fine-tuning with [PEFT](https://huggingface.co/docs/peft) and includes the following features as well. + +- fp8, int4, int8 weight and activation precisions. +- Quantization scales and zero-points strategies for [tensor, channel, group, block, token](https://github.com/neuralmagic/compressed-tensors/blob/83b2e7a969d70606421a76b9a3d112646077c8de/src/compressed_tensors/quantization/quant_args.py#L43-L52). +- Dynamic per-token activation quantization (or any static strategy). +- Weight sparsity (unstructured or semi-structured like 2:4) can be composed with quantization for extreme compression. +- Quantization of arbitrary modules, not just [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) modules. +- Targeted support for specific modules by name or class. + +Install compressed-tensors from [PyPI](https://pypi.org/project/compressed-tensors) to get the latest stable release (recommended) or install it from source to get the latest features. + + + + +```bash +pip install compressed-tensors +``` + + + + +```bash +git clone https://github.com/neuralmagic/compressed-tensors +cd compressed-tensors +pip install -e . +``` + + + + +Search using the compressed-tensors [tag](https://huggingface.co/models?other=compressed-tensors) to find a compatible model on the Hugging Face Hub. + +Only models that have already been quantized can be loaded at the moment, and once a model is loaded, it cannot be saved. To quantize a model into the compressed-tensors format, see [llm-compressor](https://github.com/vllm-project/llm-compressor). Alternatively, models can be created independently and serizlied with a compressed-tensors config. + +```python +from transformers import AutoModelForCausalLM + +ct_model = AutoModelForCausalLM.from_pretrained("nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf", device_map="auto") + +# measure memory usage +mem_params = sum([param.nelement()*param.element_size() for param in ct_model.parameters()]) +print(f"{mem_params/2**30:.4f} GB") +# 8.4575 GB +``` + +## Model checkpoint + +compressed-tensor models are defined through its configuration entry. The following example is taken from the [nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf](https://huggingface.co/nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf/blob/main/config.json) `config.json` file. + +There are a lot of entries to allow for flexible expression both during and after compression, but the entries for loading and inference can be simplified to focus on just a few key entries. + +```yaml +"quantization_config": { + "config_groups": { + "group_0": { + "input_activations": { + "num_bits": 8, + "strategy": "tensor", + "type": "float" + }, + "targets": ["Linear"], + "weights": { + "num_bits": 8, + "strategy": "tensor", + "type": "float" + } + } + }, + "format": "naive-quantized", + "ignore": ["lm_head"], + "quant_method": "compressed-tensors", + "quantization_status": "frozen" +}, +``` + +The config file specifies the quantization of a config group (`group_0`), which includes weight and activation quantization to fp8 with a static per-tensor strategy. The `lm_head` module is unquantized as shown in the `ignore` key. + +For a more detailed look at the model weights, use the [safetensors viewer](https://huggingface.co/nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf?show_file_info=model.safetensors.index.json) on the model card to see the quantized weights, input scale, and weight scale for all [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) modules. + +| Tensors | Shape | Precision | +| ------- | ----- | --------- | +model.layers.0.input_layernorm.weight | [4โ€ฏ096] | BF16 +model.layers.0.mlp.down_proj.input_scale | [1] | BF16 +model.layers.0.mlp.down_proj.weight | [4โ€ฏ096, 14โ€ฏ336] | F8_E4M3 +model.layers.0.mlp.down_proj.weight_scale | [1] | BF16 +model.layers.0.mlp.gate_proj.input_scale | [1] | BF16 +model.layers.0.mlp.gate_proj.weight | [14โ€ฏ336, 4โ€ฏ096] | F8_E4M3 +model.layers.0.mlp.gate_proj.weight_scale | [1] | BF16 +model.layers.0.mlp.up_proj.input_scale| [1] |BF16 +model.layers.0.mlp.up_proj.weight | [14โ€ฏ336, 4โ€ฏ096] | F8_E4M3 +model.layers.0.mlp.up_proj.weight_scale | [1] | BF16 +model.layers.0.post_attention_layernorm.weight | [4โ€ฏ096] |BF16 +model.layers.0.self_attn.k_proj.input_scale | [1] | BF16 +model.layers.0.self_attn.k_proj.weight | [1โ€ฏ024, 4โ€ฏ096]| F8_E4M3 +model.layers.0.self_attn.k_proj.weight_scale |[1] | BF16 +model.layers.0.self_attn.o_proj.input_scale | [1] | BF16 +model.layers.0.self_attn.o_proj.weight | [4โ€ฏ096, 4โ€ฏ096] | F8_E4M3 +model.layers.0.self_attn.o_proj.weight_scale | [1] | BF16 +model.layers.0.self_attn.q_proj.input_scale | [1] | BF16 +model.layers.0.self_attn.q_proj.weight | [4โ€ฏ096, 4โ€ฏ096] | F8_E4M3 +model.layers.0.self_attn.q_proj.weight_scale | [1] | BF16 +model.layers.0.self_attn.v_proj.input_scale | [1] | BF16 +model.layers.0.self_attn.v_proj.weight | [1โ€ฏ024, 4โ€ฏ096] | F8_E4M3 +model.layers.0.self_attn.v_proj.weight_scale | [1] | BF16 + +When loading a compressed-tensors model with the [`~quantizers.HFQuantizer`] integration, all the [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) modules specified in the quantization config are replaced by [CompressedLinear](https://github.com/neuralmagic/compressed-tensors/blob/975cb223b19fcac2b98a4271d17668462d4d6e1d/src/compressed_tensors/linear/compressed_linear.py#L30) modules that manage the compressed weights and forward pass for inference. The `lm_head` module is still kept as an unquantized nn.Linear module. + +```python +from transformers import AutoModelForCausalLM + +ct_model = AutoModelForCausalLM.from_pretrained("nm-testing/Meta-Llama-3.1-8B-Instruct-FP8-hf") +print(ct_model) +""" +LlamaForCausalLM( + (model): LlamaModel( + (embed_tokens): Embedding(128256, 4096) + (layers): ModuleList( + (0-31): 32 x LlamaDecoderLayer( + (self_attn): LlamaSdpaAttention( + (q_proj): CompressedLinear( + in_features=4096, out_features=4096, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (k_proj): CompressedLinear( + in_features=4096, out_features=1024, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (v_proj): CompressedLinear( + in_features=4096, out_features=1024, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (o_proj): CompressedLinear( + in_features=4096, out_features=4096, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (rotary_emb): LlamaRotaryEmbedding() + ) + (mlp): LlamaMLP( + (gate_proj): CompressedLinear( + in_features=4096, out_features=14336, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (up_proj): CompressedLinear( + in_features=4096, out_features=14336, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (down_proj): CompressedLinear( + in_features=14336, out_features=4096, bias=False + (input_observer): MovingAverageMinMaxObserver() + (weight_observer): MovingAverageMinMaxObserver() + ) + (act_fn): SiLU() + ) + (input_layernorm): LlamaRMSNorm((4096,), eps=1e-05) + (post_attention_layernorm): LlamaRMSNorm((4096,), eps=1e-05) + ) + ) + (norm): LlamaRMSNorm((4096,), eps=1e-05) + (rotary_emb): LlamaRotaryEmbedding() + ) + (lm_head): Linear(in_features=4096, out_features=128256, bias=False) +) +""" +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/concept_guide.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/concept_guide.md new file mode 100644 index 0000000000000000000000000000000000000000..2b68b83e4c099c796b14fe03fcf281a507da092b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/concept_guide.md @@ -0,0 +1,178 @@ + + +# Quantization concepts + +Quantization reduces the memory footprint and computational cost of large machine learning models like those found in the Transformers library. It achieves this by representing the model's weights and or activations with lower-precision data types (like 8-bit integers or int8) instead of the standard 32-bit floating-point (float32). + + +Reducing a model's precision offers several significant benefits: + +- Smaller model size: Lower-precision data types require less storage space. An int8 model, for example, is roughly 4 times smaller than its float32 counterpart. +- Faster inference: Operations on lower-precision data types, especially integers, can be significantly faster on compatible hardware (CPUs and GPUs often have specialized instructions for int8 operations). This leads to lower latency. +- Reduced energy consumption: Faster computations and smaller memory transfers often translate to lower power usage. + +The primary trade-off in quantization is *efficiency* vs. *accuracy*. Reducing precision saves resources but inevitably introduces small errors (quantization noise). The goal is to minimize this error using appropriate schemes (affine/symmetric), granularity (per-tensor/channel), and techniques (PTQ/QAT) so that the model's performance on its target task degrades as little as possible. + +The sections below cover quantization schemes, granularity, and techniques. + +## Quantization schemes + +The core idea is to map the range of values found in the original float32 weights and activations to the much smaller range represented by int8 (typically \\([-128, 127]\\)). + +This section covers how some quantization techniques work. + +
+ quant_visual +
+ +### Affine quantization + +The most common method is *affine quantization*. For a given float32 tensor (like a layer's weights), it finds the minimum \\(val_{min}\\) and maximum \\(val_{max}\\) values. This range \\([val_{min}, val_{max}]\\) is mapped to the int8 range \\([q_{min}, q_{max}]\\), which is typically \\([-128, 127]\\). + +There are two main ways to perform this mapping, *symmetric* and *asymmetric*. The choice between symmetric and asymmetric quantization determines how the float32 range is mapped to the int8 range. + +- Symmetric: This method assumes the original float32 range is symmetric around zero ( \\([ -a, a ]\\) ). This range is mapped symmetrically to the int8 range, for example, \\([-127, 127]\\). A key characteristic is that the float32 value \\(0.0\\) maps directly to the int8 value \\(0\\). This only requires one parameter, the **scale ( \\(S\\) )**, to define the mapping. It can simplify computations, but it might be less accurate if the original data distribution isn't naturally centered around zero. +- Asymmetric (Affine): This method does not assume the data is centered around zero. It maps the exact range \\([val_{min}, val_{max}]\\) from float32 to the full int8 range, like \\([-128, 127]\\). This requires two parameters, a **scale ( \\(S\\) )** and a **zero-point ( \\(Z\\) )**. + + + scale ( \\(S\\) ): A positive float32 number representing the ratio between the float32 and the int8 range. + +$$ +S = \frac{val_{max} - val_{min}}{q_{max} - q_{min}} +$$ + +zero-Point ( \\(Z\\) ): An int8 value that corresponds to the float32 value \\(0.0\\). + +$$ +Z = q_{min} - round\left(\frac{val_{min}}{S}\right) +$$ + +> [!TIP] +> In symmetric quantization, Z would typically be fixed at 0. + +With these parameters, a float32 value, \\(x\\). can be quantized to int8 ( \\(q\\) ) with the formula below. + +$$ +q = round\left(\frac{x}{S} + Z\right) +$$ + +The int8 value, \\(q\\), can be dequantized back to approximate float32 with the formula below. + +$$ +x \approx S \cdot (q - Z) +$$ + +
+ dequant +
+ +During inference, computations like matrix multiplication are performed using the int8 values ( \\(q\\) ), and the result is dequantized back to float32 (often using a higher-precision accumulation type like int32 internally) before it is passed to the next layer. + +### int4 and weight packing + +
+ weight packing +
+ +int4 quantization further reduces the model size and memory usage (halving it compared to int8). The same affine or symmetric quantization principles apply, mapping the float32 range to the 16 possible values representable by int4 ( \\([-8, 7]\\) for signed int4). + +A key aspect of int4 quantization is **weight packing**. Since most hardware can't natively handle 4-bit data types in memory, two int4 values are typically packed together into a single int8 byte for storage and transfer. For example, the first value might occupy the lower 4 bits and the second value the upper 4 bits of the byte (`packed_byte = (val1 & 0x0F) | (val2 << 4)`). + +int4 is still beneficial even without native int4 compute because the primary benefit comes from reduced memory bandwidth. Loading packed int4 weights (stored as int8) from memory (RAM or VRAM) to the compute units is twice as fast as loading int8 weights. For large models, memory access is often a significant bottleneck. The speed up from faster data transfer can outweigh the computational overhead of unpacking and dequantizing on the fly, leading to overall faster inference, especially in memory-bound scenarios. + +However, int4 quantization typically results in a larger accuracy drop compared to int8. Advanced quantization techniques like [GPTQ](./gptq) or [AWQ](./awq) are often necessary for good performance with int4. + +### FP8 Quantization (A8W8) + +
+ fp8 +
+A newer datatype, 8-bit floating-point (FP8), offers another way to reduce precision while retaining more accuracy than int8 in certain scenarios. FP8 keeps the floating-point structure (sign, exponent, mantissa) but uses fewer bits. + +There are two common FP8 variants. + +- E4M3: 1 sign bit, 4 exponent bits, 3 mantissa bits. Offers higher precision (more mantissa bits) but a smaller dynamic range (fewer exponent bits). +- E5M2: 1 sign bit, 5 exponent bits, 2 mantissa bits. Offers a wider dynamic range but lower precision. + +FP8 is used in the *A8W8* quantization scheme, which quantizes both activations (A) and weights (W) to 8-bit precision. + +While int8 has broad support, efficient FP8 computation requires specific hardware capabilities found in newer GPUs like NVIDIA H100/H200/B100 and AMD Instinct MI300 series. Without native hardware acceleration, the benefits of FP8 might not be fully realized. + +Transformers supports FP8 through specific backends like [FBGEMM](./fbgemm_fp8), [FineGrainedFP8](./finegrained_fp8), and [compressed-tensors](./compressed_tensors). These backends handle the underlying FP8 conversion and computation when the appropriate hardware and configurations are used. + +## Granularity + +Quantization parameters ( \\(S\\) and \\(Z\\)) can be calculated in one of two ways. + +- Per-Tensor: One set of \\(S\\) and \\(Z\\) for the entire tensor. Simpler, but less accurate if data values vary greatly within the tensor. +- Per-Channel (or Per-Group/Block): Separate \\(S\\) and \\(Z\\) for each channel or group. More accurate and better performance at the cost of slightly more complexity and memory. + +
+ Granularities +
+ +## Quantization techniques + +There are two main types of quantization techniques. + +- Post-Training Quantization (PTQ): Quantization is applied *after* the model is fully trained. +- Quantization-Aware Training (QAT): Quantization effects are simulated *during* training by inserting "fake quantization" ops that simulate the rounding errors of quantization. This lets the model adapt to quantization, and usually results in better accuracy, especially at lower bit-widths. + +## Quantization in Transformers + +Transformers integrates several quantization backends such as bitsandbytes, torchao, compressed-tensors, and more (refer to the quantization [overview](./overview) for more backends). + + +All backends are unified under the [`HfQuantizer`] API and associated [`QuantizationConfig`] classes. You can integrate your own custom quantization backends by implementing a custom [`HfQuantizer`] and [`QuantizationConfig`], as shown in the [Contribution](./contribute) guide. + +The typical workflow for quantization in Transformers is to: + +1. Choose a quantization method suitable for your hardware and use case (see the [Overview](./overview) or [Selecting a quantization method](./selecting) guide to help you). +2. Load a pre-quantized model from the Hugging Face Hub or load a float32/float16/bfloat16 model and apply a specific quantization method with [`QuantizationConfig`]. + +The example below demonstrates loading a 8B parameter model and quantizing it to 4-bits with bitsandbytes. + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "meta-llama/Llama-3.1-8B-Instruct" + +quantization_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_compute_dtype=torch.bfloat16 +) + +model = AutoModelForCausalLM.from_pretrained( + model_id, + quantization_config=quantization_config, + torch_dtype=torch.bfloat16, + device_map="auto" +) +``` + + +## Resources + +To explore quantization and related performance optimization concepts more deeply, check out the following resources. + +- [Quantization Fundamentals with Hugging Face](https://www.deeplearning.ai/short-courses/quantization-fundamentals-with-hugging-face/) +- [Quantization in Depth](https://www.deeplearning.ai/short-courses/quantization-in-depth) +- [Introduction to Quantization cooked in ๐Ÿค— with ๐Ÿ’—๐Ÿง‘โ€๐Ÿณ](https://huggingface.co/blog/merve/quantization) +- [EfficientML.ai Lecture 5 - Quantization Part I](https://www.youtube.com/watch?v=RP23-dRVDWM) +- [Making Deep Learning Go Brrrr From First Principles](https://horace.io/brrr_intro.html) +- [Accelerating Generative AI with PyTorch Part 2: LLM Optimizations](https://pytorch.org/blog/accelerating-generative-ai-2/) \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/contribute.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/contribute.md new file mode 100644 index 0000000000000000000000000000000000000000..0d56d7e9d71ab0712a33611624f1ab14bf5034c6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/contribute.md @@ -0,0 +1,71 @@ + + +# Contribute + +Transformers supports many quantization methods such as QLoRA, GPTQ, LLM.int8, and AWQ. However, there are still many more quantization approaches that haven't been integrated yet. To make adding and using these quantization methods with Transformers easier, use the [`~quantizers.HfQuantizer`] class. [`~quantizers.HfQuantizer`] is designed to be an internal helper class for adding a quantization method instead of something applied to every PyTorch module. + +This guide will show you how to integrate a new quantization method with [`~quantizers.HfQuantizer`]. + +## Requirements + +Before integrating a new quantization method into Transformers, ensure the method meets the following requirements. Only quantization methods that can be run with PyTorch modules are supported. + +- The quantization method is available through a Python package that is pip-installable (it is also fine if you can only install the package from source). Ideally, pre-compiled kernels are included in the pip package. +- The method can run on commonly-used hardware (CPU, GPU, etc.). +- The method is wrapped in a [nn.Module](https://pytorch.org/docs/stable/generated/torch.nn.Module.html) ([`~bitsandbytes.nn.Linear8bitLt`], [`~bitsandbytes.nn.Linear4bit`]), and the quantized linear layer should have the following definition. + + ```py + class Linear4bit(nn.Module): + def __init__(self, ...): + ... + + def forward(self, x): + return my_4bit_kernel(x, self.weight, self.bias) + ``` + + This way, Transformers models are easily quantized by replacing instances of [nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) with a target class. + +- The quantization method should be serializable. You can save the quantized weights locally or push them to the Hub. +- Make sure the package containing the quantization kernels/primitive is stable (no frequent breaking changes). + +Some quantization methods may require "pre-quantizing" the model through data calibration (AWQ). In this case, we prefer to only support inference in Transformers and let the third-party library maintained by the ML community deal handle the model quantization itself. + +## Create new HFQuantizer class + +1. Create a new quantization config class inside [src/transformers/utils/quantization_config.py](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/utils/quantization_config.py). Add the new quantization config to the [_import_structure](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py#L1088) inside Transformers' [src/transformers/__init__.py](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py) file. + +2. Create a new file inside [src/transformers/quantizers/](https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers) named `quantizer_your_method.py`, and make it inherit from [`~quantizers.HfQuantizer]. Make sure to add the new quantizer and quantization config in the quantization auto-mapping in [src/transformers/quantizers/auto.py](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers/auto.py). + +3. Define the following class attributes and property methods for your quantization method. + + - `requires_calibration`: Whether the quantization method requires a data calibration process. If set to `True`, you can only support inference (with quantized weights) and not inference and quantization. + - `required_packages`: A list of strings of the required packages to use the quantized weights. You might need to define some new utility methods such as `is_auto_awq_available` in [transformers/src/utils/import_utils.py](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/utils/import_utils.py). + - `requires_parameters_quantization`: Only required if your quantization method requires extra attention to the underlying [nn.Parameter](https://pytorch.org/docs/stable/generated/torch.nn.parameter.Parameter.html) object. For example, bitsandbytes uses [`~bitsandbytes.nn.Params4bit`] and [`~bitsandbytes.nn.Int8Params`], which requires some extra attention when quantizing the model. Most of the recent quantization method packs int2 and int4 weights inside [torch.uint8](https://pytorch.org/docs/stable/tensors.html) weights, so this flag should not be really required (set to `False` by default). + - `is_serializable`: A property method to determine whether the method is serializable or not. + - `is_trainable`: A property method to determine whether you can fine-tune models on top of the quantization method (with or without PEFT approaches). + +4. Write the `validate_environment` and `update_torch_dtype` methods. These methods are called before creating the quantized model to ensure users use the right configuration. Refer to other quantizers for an example of it is implemented. + +5. Write the `_process_model_before_weight_loading` method. In Transformers, the quantized models are initialized first on the `"meta"` device before loading the weights. This means the `_process_model_before_weight_loading` method takes care of manipulating the model skeleton to replace some modules ([nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html)) with the target modules (quantization modules). + + You can define module replacement logic or any other utility method by creating a new file in [transformers/src/integrations/](https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/integrations) and exposing the relevant methods in that folder's `__init__.py` file. The best starting point would be to have a look at another quantization method such as [quantizer_awq.py](https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers/quantizer_awq.py). + +6. Write the `_process_model_after_weight_loading` method. This method enables implementing additional features that require manipulating the model after loading the weights. + +7. Document everything! Make sure your quantization method is documented by adding a new file under `docs/source/en/quantization`. + +8. You should add tests by adding the package in our nightly Dockerfile inside `docker/transformers-quantization-latest-gpu` and then adding a new test file in `tests/quantization/xxx`. Feel free to check out existing quantization methods to see how it is implemented. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/eetq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/eetq.md new file mode 100644 index 0000000000000000000000000000000000000000..07cb25e437f80c937866721bebc13fee3978dcbd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/eetq.md @@ -0,0 +1,65 @@ + + +# EETQ + +The [Easy & Efficient Quantization for Transformers (EETQ)](https://github.com/NetEase-FuXi/EETQ) library supports int8 weight-only per-channel quantization for NVIDIA GPUs. It uses high-performance GEMM and GEMV kernels from [FasterTransformer](https://github.com/NVIDIA/FasterTransformer) and [TensorRT-LLM](https://github.com/NVIDIA/TensorRT-LLM). The attention layer is optimized with [FlashAttention2](https://github.com/Dao-AILab/flash-attention). No calibration dataset is required, and the model doesn't need to be pre-quantized. Accuracy degradation is negligible owing to the per-channel quantization. + +EETQ further supports fine-tuning with [PEFT](https://huggingface.co/docs/peft). + +Install EETQ from the [release page](https://github.com/NetEase-FuXi/EETQ/releases) or [source code](https://github.com/NetEase-FuXi/EETQ). CUDA 11.4+ is required for EETQ. + + + + +```bash +pip install --no-cache-dir https://github.com/NetEase-FuXi/EETQ/releases/download/v1.0.0/EETQ-1.0.0+cu121+torch2.1.2-cp310-cp310-linux_x86_64.whl +``` + + + + +```bash +git clone https://github.com/NetEase-FuXi/EETQ.git +cd EETQ/ +git submodule update --init --recursive +pip install . +``` + + + + +Quantize a model on-the-fly by defining the quantization data type in [`EetqConfig`]. + +```py +from transformers import AutoModelForCausalLM, EetqConfig + +quantization_config = EetqConfig("int8") +model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) +``` + +Save the quantized model with [`~PreTrainedModel.save_pretrained`] so it can be reused again with [`~PreTrainedModel.from_pretrained`]. + +```py +quant_path = "/path/to/save/quantized/model" +model.save_pretrained(quant_path) +model = AutoModelForCausalLM.from_pretrained(quant_path, device_map="auto") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fbgemm_fp8.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fbgemm_fp8.md new file mode 100644 index 0000000000000000000000000000000000000000..b9382f74952aa8ed259b7a9eb825bfa2dfdd4e86 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fbgemm_fp8.md @@ -0,0 +1,56 @@ + + +# FBGEMM + +[FBGEMM (Facebook GEneral Matrix Multiplication)](https://github.com/pytorch/FBGEMM) is a low-precision matrix multiplication library for small batch sizes and support for accuracy-loss minimizing techniques such as row-wise quantization and outlier-aware quantization. With FBGEMM, quantize a models weights to 8-bits/channel and the activations to 8-bits/token (also known as fp8 or w8a8). + +> [!TIP] +> You need a GPU with [compute capability 9+](https://developer.nvidia.com/cuda-gpus#collapseOne) like a H100. + +Install the FBGEMM_GPU package with the command below to ensure you have the latest version. + +```bash +pip install --upgrade accelerate fbgemm-gpu torch +``` + +If you're having installation issues, try installing the [nightly release](https://pytorch.org/FBGEMM/fbgemm_gpu-development/InstallationInstructions.html#fbgemm-gpu-install-libraries:~:text=found%20here.-,Install%20the%20FBGEMM_GPU%20Package,-Install%20through%20PyTorch). + +Create a [`FbgemmFp8Config`] and pass it to [`~PreTrainedModel.from_pretrained`] to quantize a model to fp8. + +```py +from transformers import FbgemmFp8Config, AutoModelForCausalLM + +quantization_config = FbgemmFp8Config() +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Meta-Llama-3-8B", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) +``` + +[`~PreTrainedModel.save_pretrained`] and [`~PreTrainedModel.from_pretrained`] enable saving and loading a quantized model. + +```py +quant_path = "/path/to/save/quantized/model" +model.save_pretrained(quant_path) +model = AutoModelForCausalLM.from_pretrained(quant_path, device_map="auto") +``` + +## Resources + +Read the [Open-sourcing FBGEMM for state-of-the-art server-side inference](https://engineering.fb.com/2018/11/07/ml-applications/fbgemm/) blog post for more details on FBGEMM. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/finegrained_fp8.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/finegrained_fp8.md new file mode 100644 index 0000000000000000000000000000000000000000..c09835483633c082eedc825e0e43099205036f2e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/finegrained_fp8.md @@ -0,0 +1,62 @@ + + +# Fine-grained FP8 + +Fine-grained FP8 quantization quantizes the weights and activations to fp8. + +- The weights are quantized to 8-bits for each 2D block (`weight_block_size=(128, 128)`). +- The activations are quantized to 8-bits for each group per token. The group value matches the weights in the input channel (128 by default). + +FP8 quantization enables support for [DeepSeek-V3](https://hf.co/papers/2412.19437) and DeepSeek-R1. + +
+ +
+ +> [!TIP] +> You need a GPU with Compute Capability>=9 (H100), and install a PyTorch version compatible with the CUDA version of your GPU. + +Install Accelerate and upgrade to the latest version of PyTorch. + +```bash +pip install --upgrade accelerate torch +``` + +Create a [`FineGrainedFP8Config`] class and pass it to [`~PreTrainedModel.from_pretrained`] to quantize it. The weights are loaded in full precision (`torch.float32`) by default regardless of the actual data type the weights are stored in. Set `torch_dtype="auto"` to load the weights in the data type defined in a models `config.json` file to automatically load the most memory-optiomal data type. + +```py +from transformers import FineGrainedFP8Config, AutoModelForCausalLM, AutoTokenizer + +model_name = "meta-llama/Meta-Llama-3-8B" +quantization_config = FineGrainedFP8Config() +quantized_model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto", quantization_config=quantization_config) + +tokenizer = AutoTokenizer.from_pretrained(model_name) +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to(quantized_model.device.type) + +output = quantized_model.generate(**input_ids, max_new_tokens=10) +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + +Use [`~PreTrainedModel.save_pretrained`] to save the quantized model and reload it with [`~PreTrainedModel.from_pretrained`]. + +```py +quant_path = "/path/to/save/quantized/model" +model.save_pretrained(quant_path) +model = AutoModelForCausalLM.from_pretrained(quant_path, device_map="auto") +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fp_quant.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fp_quant.md new file mode 100644 index 0000000000000000000000000000000000000000..abe645acbff6ea02f4cc70466e96516df9e95494 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/fp_quant.md @@ -0,0 +1,66 @@ + + +# FP-Quant + +[FP-Quant](https://github.com/IST-DASLab/FP-Quant) is a family of quantization algorithms tailored for the Blackwell generation of Nvidia GPUs. The goal is to allow for efficient post-training quantization (PTQ) and quantization-aware training (QAT) of LLMs in the [MXFP4 and NVFP4 data-types](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf). + +Currently, only PTQ with MXFP4 is supported. Models can either be quantized on the fly with `quantization_config=FPQuantConfig()`: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, FPQuantConfig +import torch + +model = AutoModelForCausalLM.from_pretrained( + "qwen/Qwen3-8B", + quantization_config=FPQuantConfig(), + device_map="auto", + torch_dtype=torch.bfloat16, +) +``` + +or pre-processed with GPTQ for better quality (see [FP Format Quantization Harness](https://github.com/IST-DASLab/FP-Quant)). + +A **Blackwell-generation GPU is required** to run the kernels. Runtime support for FP-Quant is implemented through the [QuTLASS](https://github.com/IST-DASLab/qutlass) library and a lightweight PyTorch interface lib [`fp_quant`](https://github.com/IST-DASLab/FP-Quant/tree/master/inference_lib). We recommend installing the former **from source** and the latter with `pip install fp_quant`. + +Users **without a Blackwell-generation GPU** , can use the method with `quantization_config=FPQuantConfig(pseudoquant=True)` without having to install [QuTLASS](https://github.com/IST-DASLab/qutlass). This would provide no speedups but would fully emulate the effect of quantization. + +> [!TIP] +> Find models pre-quantized with FP-Quant in the official ISTA-DASLab [collection](https://huggingface.co/collections/ISTA-DASLab/fp-quant-6877c186103a21d3a02568ee). + +## torch.compile + +FP-Quant is fully compatible with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html). + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, FPQuantConfig + +model = AutoModelForCausalLM.from_pretrained( + "qwen/Qwen3-8B", + quantization_config=FPQuantConfig(), + device_map="auto", + torch_dtype=torch.bfloat16, +) + +model.forward = torch.compile(model.forward, mode="max-autotune", fullgraph=True) +``` + +## Speedups + +FP-Quant currently performs best for very large batch size processing. + +See [QuTLASS README](https://github.com/IST-DASLab/qutlass/blob/main/README.md) for speedups. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/gptq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/gptq.md new file mode 100644 index 0000000000000000000000000000000000000000..a9878bbc362e379dc8de5c364df55ec1e2f07171 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/gptq.md @@ -0,0 +1,171 @@ + + +# GPTQ + +The [GPTQModel](https://github.com/ModelCloud/GPTQModel) and [AutoGPTQ](https://github.com/PanQiWei/AutoGPTQ) implements the GPTQ algorithm, a post-training quantization technique where each row of the weight matrix is quantized independently to find a version of the weights that minimizes the error. These weights are quantized to int4, but they're restored to fp16 on the fly during inference. This can save memory usage by 4x because the int4 weights are dequantized in a fused kernel rather than a GPU's global memory. Inference is also faster because a lower bitwidth takes less time to communicate. + +> [!WARNING] +> AutoGPTQ is likely to be deprecated in the future due to lack of continued support for new models and features. See the [GPTQModel](#gptqmodel) section for more details. + +Install Accelerate, Transformers and Optimum first. + +```bash +pip install --upgrade accelerate optimum transformers +``` + +Then run the command below to install a GPTQ library. + + + + +```bash +pip install gptqmodel --no-build-isolation +``` + + + + +```bash +pip install auto-gptq --no-build-isolation +``` + + + + +Create a [`GPTQConfig`] class and set the number of bits to quantize to, a dataset to calbrate the weights for quantization, and a tokenizer to prepare the dataset. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-125m") +gptq_config = GPTQConfig(bits=4, dataset="c4", tokenizer=tokenizer) +``` + +You can pass your own dataset as a list of strings, but it is highly recommended to use the same dataset from the GPTQ paper. + +```py +dataset = ["auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."] +gptq_config = GPTQConfig(bits=4, dataset=dataset, tokenizer=tokenizer) +``` + +Load a model to quantize and pass [`GPTQConfig`] to [`~AutoModelForCausalLM.from_pretrained`]. Set `device_map="auto"` to automatically offload the model to a CPU to help fit the model in memory, and allow the model modules to be moved between the CPU and GPU for quantization. + +```py +quantized_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m", device_map="auto", quantization_config=gptq_config) +``` + +If you're running out of memory because a dataset is too large (disk offloading is not supported), try passing the `max_memory` parameter to allocate the amount of memory to use on your device (GPU and CPU). + +```py +quantized_model = AutoModelForCausalLM.from_pretrained( + "facebook/opt-125m", + device_map="auto", + max_memory={0: "30GiB", 1: "46GiB", "cpu": "30GiB"}, + quantization_config=gptq_config +) +``` + +> [!WARNING] +> Depending on your hardware, it can take some time to quantize a model from scratch. It can take ~5 minutes to quantize the [facebook/opt-350m](https://huggingface.co/facebook/opt-350m) model on a free-tier Google Colab GPU, but it'll take ~4 hours to quantize a 175B parameter model on a NVIDIA A100. Before you quantize a model, it is a good idea to check the Hub if a GPTQ-quantized version of the model already exists. + +Once a model is quantized, you can use [`~PreTrainedModel.push_to_hub`] to push the model and tokenizer to the Hub where it can be easily shared and accessed. This saves the [`GPTQConfig`]. + +```py +quantized_model.push_to_hub("opt-125m-gptq") +tokenizer.push_to_hub("opt-125m-gptq") +``` + +[`~PreTrainedModel.save_pretrained`] saves a quantized model locally. If the model was quantized with the `device_map` parameter, make sure to move the entire model to a GPU or CPU before saving it. The example below saves the model on a CPU. + +```py +quantized_model.save_pretrained("opt-125m-gptq") +tokenizer.save_pretrained("opt-125m-gptq") + +# if quantized with device_map set +quantized_model.to("cpu") +quantized_model.save_pretrained("opt-125m-gptq") +``` + +Reload a quantized model with [`~PreTrainedModel.from_pretrained`], and set `device_map="auto"` to automatically distribute the model on all available GPUs to load the model faster without using more memory than needed. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto") +``` + +## Marlin + +[Marlin](https://github.com/IST-DASLab/marlin) is a 4-bit only CUDA GPTQ kernel, highly optimized for the NVIDIA A100 GPU (Ampere) architecture. Loading, dequantization, and execution of post-dequantized weights are highly parallelized, offering a substantial inference improvement versus the original CUDA GPTQ kernel. Marlin is only available for quantized inference and does not support model quantization. + +Marlin inference can be activated with the `backend` parameter in [`GPTQConfig`]. + +```py + +from transformers import AutoModelForCausalLM, GPTQConfig + +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto", quantization_config=GPTQConfig(bits=4, backend="marlin")) +``` + +## ExLlama + +> [!WARNING] +> Only 4-bit models are supported, and we recommend deactivating the ExLlama kernels if you're finetuning a quantized model with PEFT. + +[ExLlama](https://github.com/turboderp/exllama) is a Python/C++/CUDA implementation of the [Llama](model_doc/llama) model that is designed for faster inference with 4-bit GPTQ weights (check out these [benchmarks](https://github.com/huggingface/optimum/tree/main/tests/benchmark#gptq-benchmark)). The ExLlama kernel is activated by default when you create a [`GPTQConfig`] object. + +To boost inference speed even further, use the [ExLlamaV2](https://github.com/turboderp/exllamav2) kernels by configuring the `exllama_config` parameter in [`GPTQConfig`]. + +```py +import torch +from transformers import AutoModelForCausalLM, GPTQConfig + +gptq_config = GPTQConfig(bits=4, exllama_config={"version":2}) +model = AutoModelForCausalLM.from_pretrained( + "{your_username}/opt-125m-gptq", + device_map="auto", + quantization_config=gptq_config +) +``` + +The ExLlama kernels are only supported when the entire model is on the GPU. If you're doing inference on a CPU with AutoGPTQ 0.4.2+, disable the ExLlama kernel in [`GPTQConfig`]. This overwrites the attributes related to the ExLlama kernels in the quantization config of the `config.json` file. + +```py +import torch +from transformers import AutoModelForCausalLM, GPTQConfig + +gptq_config = GPTQConfig(bits=4, use_exllama=False) +model = AutoModelForCausalLM.from_pretrained( + "{your_username}/opt-125m-gptq", + device_map="cpu", + quantization_config=gptq_config +) +``` + +## GPTQModel + +It is recommended to use GPTQModel, originally a maintained fork of AutoGPTQ, because it has since diverged from AutoGTPQ with some significant features. GPTQModel has faster quantization, lower memory usage, and more accurate default quantization. + +GPTQModel provides asymmetric quantization which can potentially lower quantization errors compared to symmetric quantization. It is not backward compatible with AutoGPTQ, and not all kernels (Marlin) support asymmetric quantization. + +GPTQModel also has broader support for the latest LLM models, multimodal models (Qwen2-VL and Ovis1.6-VL), platforms (Linux, macOS, Windows 11), and hardware (AMD ROCm, Apple Silicon, Intel/AMD CPUs, and Intel Datacenter Max/Arc GPUs, etc.). + +The Marlin kernels are also updated for A100 GPUs and other kernels are updated to include auto-padding for legacy models and models with non-uniform in/out-features. + +## Resources + +Run the GPTQ quantization with PEFT [notebook](https://colab.research.google.com/drive/1_TIrmuKOFhuRRiTWN94iLKUFu6ZX4ceb?usp=sharing) for a hands-on experience, and read [Making LLMs lighter with AutoGPTQ and transformers](https://huggingface.co/blog/gptq-integration) to learn more about the AutoGPTQ integration. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/higgs.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/higgs.md new file mode 100644 index 0000000000000000000000000000000000000000..07f6e2b31feddbedea704c69a0b468aa916f894f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/higgs.md @@ -0,0 +1,80 @@ + + +# HIGGS + +[HIGGS](https://huggingface.co/papers/2411.17525) is a zero-shot quantization algorithm that combines Hadamard preprocessing with MSE-Optimal quantization grids to achieve lower quantization error and state-of-the-art performance. + +Runtime support for HIGGS is implemented through the [FLUTE](https://github.com/HanGuo97/flute) library. Only the 70B and 405B variants of Llama 3 and Llama 3.0, and the 8B and 27B variants of Gemma 2 are currently supported. HIGGS also doesn't support quantized training and backward passes in general at the moment. + +Run the command below to install FLUTE. + + + + +```bash +pip install flute-kernel +``` + + + + +```bash +pip install flute-kernel -i https://flute-ai.github.io/whl/cu12.4 +``` + + + + +Create a [`HiggsConfig`] with the number of bits to quantize a model to. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, HiggsConfig + +model = AutoModelForCausalLM.from_pretrained( + "google/gemma-2-9b-it", + quantization_config=HiggsConfig(bits=4), + device_map="auto", +) +``` + +> [!TIP] +> Find models pre-quantized with HIGGS in the official ISTA-DASLab [collection](https://huggingface.co/collections/ISTA-DASLab/higgs-675308e432fd56b7f6dab94e). + +## torch.compile + +HIGGS is fully compatible with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html). + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, HiggsConfig + +model = AutoModelForCausalLM.from_pretrained( + "google/gemma-2-9b-it", + quantization_config=HiggsConfig(bits=4), + device_map="auto", +) + +model = torch.compile(model) +``` + +Refer to the table below for a benchmark of forward passes/sec for Llama-3.1-8B-Instruct on a RTX4090. + +| Batch Size | BF16 (with `torch.compile`) | HIGGS 4bit (without `torch.compile`) | HIGGS 4bit (with `torch.compile`) | +|------------|-----------------------------|----------------------------------|-----------------------------------| +| 1 | 59 | 41 | 124 | +| 4 | 57 | 42 | 123 | +| 16 | 56 | 41 | 120 | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/hqq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/hqq.md new file mode 100644 index 0000000000000000000000000000000000000000..4c0ea92f430c9eb493362832d4f73c80f05bec71 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/hqq.md @@ -0,0 +1,101 @@ + + +# HQQ + +[Half-Quadratic Quantization (HQQ)](https://github.com/mobiusml/hqq/) supports fast on-the-fly quantization for 8, 4, 3, 2, and even 1-bits. It doesn't require calibration data, and it is compatible with any model modality (LLMs, vision, etc.). + +HQQ further supports fine-tuning with [PEFT](https://huggingface.co/docs/peft) and is fully compatible with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) for even faster inference and training. + +Install HQQ with the following command to get the latest version and to build its corresponding CUDA kernels if you are using a cuda device. It also support Intel XPU with pure pytorch implementation. + +```bash +pip install hqq +``` + +You can choose to either replace all the linear layers in a model with the same quantization config or dedicate a specific quantization config for specific linear layers. + + + + +Quantize a model by creating a [`HqqConfig`] and specifying the `nbits` and `group_size` to replace for all the linear layers ([torch.nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html)) of the model. + +``` py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, HqqConfig + +quant_config = HqqConfig(nbits=8, group_size=64) +model = transformers.AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", + torch_dtype=torch.float16, + device_map="auto", + quantization_config=quant_config +) +``` + + + + +Quantize a model by creating a dictionary specifying the `nbits` and `group_size` for the linear layers to quantize. Pass them to [`HqqConfig`] and set which layers to quantize with the config. This approach is especially useful for quantizing mixture-of-experts (MoEs) because they are less affected ly lower quantization settings. + +``` py +q4_config = {'nbits':4, 'group_size':64} +q3_config = {'nbits':3, 'group_size':32} +quant_config = HqqConfig(dynamic_config={ + 'self_attn.q_proj':q4_config, + 'self_attn.k_proj':q4_config, + 'self_attn.v_proj':q4_config, + 'self_attn.o_proj':q4_config, + + 'mlp.gate_proj':q3_config, + 'mlp.up_proj' :q3_config, + 'mlp.down_proj':q3_config, +}) + +model = transformers.AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", + torch_dtype=torch.float16, + device_map="auto", + quantization_config=quant_config +) +``` + + + + +## Backends + +HQQ supports various backends, including pure PyTorch and custom dequantization CUDA kernels. These backends are suitable for older GPUs and PEFT/QLoRA training. + +```py +from hqq.core.quantize import * + +HQQLinear.set_backend(HQQBackend.PYTORCH) +``` + +For faster inference, HQQ supports 4-bit fused kernels (torchao and Marlin) after a model is quantized. These can reach up to 200 tokens/sec on a single 4090. The example below demonstrates enabling the torchao_int4 backend. + +```py +from hqq.utils.patching import prepare_for_inference + +prepare_for_inference("model", backend="torchao_int4") +``` + +Refer to the [Backend](https://github.com/mobiusml/hqq/#backend) guide for more details. + +## Resources + +Read the [Half-Quadratic Quantization of Large Machine Learning Models](https://mobiusml.github.io/hqq_blog/) blog post for more details about HQQ. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/optimum.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/optimum.md new file mode 100644 index 0000000000000000000000000000000000000000..5498e715ee18d9e02e15cd99126556956d62951d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/optimum.md @@ -0,0 +1,19 @@ + + +# Optimum + +[Optimum](https://huggingface.co/docs/optimum/index) is an optimization library that supports quantization for Intel, Furiousa, ONNX Runtime, GPTQ, and lower-level PyTorch quantization functions. It is designed to enhance performance for specific hardware - Intel CPUs/HPUs, AMD GPUs, Furiousa NPUs, etc. - and model accelerators like ONNX Runtime. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/overview.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/overview.md new file mode 100644 index 0000000000000000000000000000000000000000..f551d0690d96acabedd69c3a7f85de4fd68dd9c3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/overview.md @@ -0,0 +1,61 @@ + + +# Overview + +Quantization lowers the memory requirements of loading and using a model by storing the weights in a lower precision while trying to preserve as much accuracy as possible. Weights are typically stored in full-precision (fp32) floating point representations, but half-precision (fp16 or bf16) are increasingly popular data types given the large size of models today. Some quantization methods can reduce the precision even further to integer representations, like int8 or int4. + +Transformers supports many quantization methods, each with their pros and cons, so you can pick the best one for your specific use case. Some methods require calibration for greater accuracy and extreme compression (1-2 bits), while other methods work out of the box with on-the-fly quantization. + +Use the Space below to help you pick a quantization method depending on your hardware and number of bits to quantize to. + +| Quantization Method | On the fly quantization | CPU | CUDA GPU | ROCm GPU | Metal (Apple Silicon) | Intel GPU | Torch compile() | Bits | PEFT Fine Tuning | Serializable with ๐Ÿค—Transformers | ๐Ÿค—Transformers Support | Link to library | +|-------------------------------------------|----------------------|-----------------|----------|-----------|------------------------------------|-----------------|-----------------|--------------|------------------|-----------------------------|-------------------------|---------------------------------------------| +| [AQLM](./aqlm) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 1/2 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/Vahe1994/AQLM | +| [AutoRound](./auto_round) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | 2/3/4/8 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/intel/auto-round | +| [AWQ](./awq) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ? | 4 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/casper-hansen/AutoAWQ | +| [bitsandbytes](./bitsandbytes) | ๐ŸŸข | ๐ŸŸก | ๐ŸŸข | ๐ŸŸก | ๐Ÿ”ด | ๐ŸŸก | ๐ŸŸข | 4/8 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/bitsandbytes-foundation/bitsandbytes | +| [compressed-tensors](./compressed_tensors) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | 1/8 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/neuralmagic/compressed-tensors | +| [EETQ](./eetq) | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ? | 8 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/NetEase-FuXi/EETQ | +| [FP-Quant](./fp_quant) | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 4 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/IST-DASLab/FP-Quant | +| [GGUF / GGML (llama.cpp)](../gguf) | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | 1/8 | ๐Ÿ”ด | [See Notes](../gguf) | [See Notes](../gguf) | https://github.com/ggerganov/llama.cpp | +| [GPTQModel](./gptq) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | 2/3/4/8 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/ModelCloud/GPTQModel | +| [AutoGPTQ](./gptq) | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | 2/3/4/8 | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | https://github.com/AutoGPTQ/AutoGPTQ | +| [HIGGS](./higgs) | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 2/4 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/HanGuo97/flute | +| [HQQ](./hqq) | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 1/8 | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | https://github.com/mobiusml/hqq/ | +| [optimum-quanto](./quanto) | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | 2/4/8 | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | https://github.com/huggingface/optimum-quanto | +| [FBGEMM_FP8](./fbgemm_fp8) | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | 8 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/pytorch/FBGEMM | +| [torchao](./torchao) | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸก | ๐Ÿ”ด | | 4/8 | | ๐ŸŸข๐Ÿ”ด | ๐ŸŸข | https://github.com/pytorch/ao | +| [VPTQ](./vptq) | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸก | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 1/8 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/microsoft/VPTQ | +| [FINEGRAINED_FP8](./finegrained_fp8) | ๐ŸŸข | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | 8 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | | +| [SpQR](./spqr) | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | ๐Ÿ”ด | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | 3 | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | https://github.com/Vahe1994/SpQR/ | +| [Quark](./quark) | ๐Ÿ”ด | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ๐ŸŸข | ? | 2/4/6/8/9/16 | ๐Ÿ”ด | ๐Ÿ”ด | ๐ŸŸข | https://quark.docs.amd.com/latest/ | + +## Resources + +If you are new to quantization, we recommend checking out these beginner-friendly quantization courses in collaboration with DeepLearning.AI. + +* [Quantization Fundamentals with Hugging Face](https://www.deeplearning.ai/short-courses/quantization-fundamentals-with-hugging-face/) +* [Quantization in Depth](https://www.deeplearning.ai/short-courses/quantization-in-depth) + +## User-Friendly Quantization Tools + +If you are looking for a user-friendly quantization experience, you can use the following community spaces and notebooks: + +* [Bitsandbytes Space](https://huggingface.co/spaces/bnb-community/bnb-my-repo) +* [GGUF Space](https://huggingface.co/spaces/ggml-org/gguf-my-repo) +* [MLX Space](https://huggingface.co/spaces/mlx-community/mlx-my-repo) +* [AuoQuant Notebook](https://colab.research.google.com/drive/1b6nqC7UZVt8bx4MksX7s656GXPM-eWw4?usp=sharing#scrollTo=ZC9Nsr9u5WhN) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quanto.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quanto.md new file mode 100644 index 0000000000000000000000000000000000000000..7635b72d9767e810353f5987af0ceca617aca6c3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quanto.md @@ -0,0 +1,69 @@ + + +# Optimum Quanto + +[Quanto](https://github.com/huggingface/optimum-quanto) is a PyTorch quantization backend for [Optimum](https://huggingface.co/docs/optimum/index). It features linear quantization for weights (float8, int8, int4, int2) with accuracy very similar to full-precision models. Quanto is compatible with any model modality and device, making it simple to use regardless of hardware. + +Quanto is also compatible with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) for faster generation. + +Install Quanto with the following command. + +```bash +pip install optimum-quanto accelerate transformers +``` + +Quantize a model by creating a [`QuantoConfig`] and specifying the `weights` parameter to quantize to. This works for any model in any modality as long as it contains [torch.nn.Linear](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) layers. + +> [!TIP] +> The Transformers integration only supports weight quantization. Use the Quanto library directly if you need activation quantization, calibration, or QAT. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfig + +quant_config = QuantoConfig(weights="int8") +model = transformers.AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B", + torch_dtype="auto", + device_map="auto", + quantization_config=quant_config +) +``` + +## torch.compile + +Wrap a Quanto model with [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) for faster generation. + +```py +import torch +from transformers import AutoModelForSpeechSeq2Seq, QuantoConfig + +quant_config = QuantoConfig(weights="int8") +model = AutoModelForSpeechSeq2Seq.from_pretrained( + "openai/whisper-large-v2", + torch_dtype="auto", + device_map="auto", + quantization_config=quant_config +) + +model = torch.compile(model) +``` + +## Resources + +Read the [Quanto: a PyTorch quantization backend for Optimum](https://huggingface.co/blog/quanto-introduction) blog post to learn more about the library design and benchmarks. + +For more hands-on examples, take a look at the Quanto [notebook](https://colab.research.google.com/drive/16CXfVmtdQvciSh9BopZUDYcmXCDpvgrT?usp=sharing). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quark.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quark.md new file mode 100644 index 0000000000000000000000000000000000000000..94250a8c2f6e9363ac319d2279715e085272b5c9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/quark.md @@ -0,0 +1,83 @@ + + +# Quark + +[Quark](https://quark.docs.amd.com/latest/) is a deep learning quantization toolkit designed to be agnostic to specific data types, algorithms, and hardware. Different pre-processing strategies, algorithms and data-types can be combined in Quark. + +The PyTorch support integrated through ๐Ÿค— Transformers primarily targets AMD CPUs and GPUs, and is primarily meant to be used for evaluation purposes. For example, it is possible to use [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) with ๐Ÿค— Transformers backend and evaluate a wide range of models quantized through Quark seamlessly. + +Users interested in Quark can refer to its [documentation](https://quark.docs.amd.com/latest/) to get started quantizing models and using them in supported open-source libraries! + +Although Quark has its own checkpoint / [configuration format](https://huggingface.co/amd/Llama-3.1-8B-Instruct-FP8-KV-Quark-test/blob/main/config.json#L26), the library also supports producing models with a serialization layout compliant with other quantization/runtime implementations ([AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq), [native fp8 in ๐Ÿค— Transformers](https://huggingface.co/docs/transformers/quantization/finegrained_fp8)). + +To be able to load Quark quantized models in Transformers, the library first needs to be installed: + +```bash +pip install amd-quark +``` + +## Support matrix + +Models quantized through Quark support a large range of features, that can be combined together. All quantized models independently of their configuration can seamlessly be reloaded through `PretrainedModel.from_pretrained`. + +The table below shows a few features supported by Quark: + +| **Feature** | **Supported subset in Quark** | | +|---------------------------------|-----------------------------------------------------------------------------------------------------------|---| +| Data types | int8, int4, int2, bfloat16, float16, fp8_e5m2, fp8_e4m3, fp6_e3m2, fp6_e2m3, fp4, OCP MX, MX6, MX9, bfp16 | | +| Pre-quantization transformation | SmoothQuant, QuaRot, SpinQuant, AWQ | | +| Quantization algorithm | GPTQ | | +| Supported operators | ``nn.Linear``, ``nn.Conv2d``, ``nn.ConvTranspose2d``, ``nn.Embedding``, ``nn.EmbeddingBag`` | | +| Granularity | per-tensor, per-channel, per-block, per-layer, per-layer type | | +| KV cache | fp8 | | +| Activation calibration | MinMax / Percentile / MSE | | +| Quantization strategy | weight-only, static, dynamic, with or without output quantization | | + +## Models on Hugging Face Hub + +Public models using Quark native serialization can be found at https://huggingface.co/models?other=quark. + +Although Quark also supports [models using `quant_method="fp8"`](https://huggingface.co/models?other=fp8) and [models using `quant_method="awq"`](https://huggingface.co/models?other=awq), Transformers loads these models rather through [AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq) or uses the [native fp8 support in ๐Ÿค— Transformers](https://huggingface.co/docs/transformers/quantization/finegrained_fp8). + +## Using Quark models in Transformers + +Here is an example of how one can load a Quark model in Transformers: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "EmbeddedLLM/Llama-3.1-8B-Instruct-w_fp8_per_channel_sym" +model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto") + +print(model.model.layers[0].self_attn.q_proj) +# QParamsLinear( +# (weight_quantizer): ScaledRealQuantizer() +# (input_quantizer): ScaledRealQuantizer() +# (output_quantizer): ScaledRealQuantizer() +# ) + +tokenizer = AutoTokenizer.from_pretrained(model_id) +inp = tokenizer("Where is a good place to cycle around Tokyo?", return_tensors="pt") +inp = inp.to(model.device) + +res = model.generate(**inp, min_new_tokens=50, max_new_tokens=100) + +print(tokenizer.batch_decode(res)[0]) +# <|begin_of_text|>Where is a good place to cycle around Tokyo? There are several places in Tokyo that are suitable for cycling, depending on your skill level and interests. Here are a few suggestions: +# 1. Yoyogi Park: This park is a popular spot for cycling and has a wide, flat path that's perfect for beginners. You can also visit the Meiji Shrine, a famous Shinto shrine located in the park. +# 2. Imperial Palace East Garden: This beautiful garden has a large, flat path that's perfect for cycling. You can also visit the +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/selecting.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/selecting.md new file mode 100644 index 0000000000000000000000000000000000000000..7653e946dd8051890a42d143bd5ad0e2329ca5e9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/selecting.md @@ -0,0 +1,157 @@ + + +# Selecting a quantization method + +There are many quantization methods available in Transformers for inference and fine-tuning. This guide helps you choose the most common and production-ready quantization techniques depending on your use case, and presents the advantages and disadvantages of each technique. + +For a comprehensive overview of all supported methods and their features, refer back to the table in the [Overview](./overview). + +## Inference + +Consider the quantization methods below for inference. + +| quantization method | use case | +|---|---| +| bitsandbytes | ease of use and QLoRA fine-tuning on NVIDIA GPUs | +| compressed-tensors | loading specific quantized formats (FP8, Sparse) | +| GPTQModel or AWQ | good 4-bit accuracy with upfront calibration | +| HQQ | fast on the fly quantization without calibration | +| torchao | flexibility and fast inference with torch.compile | + +### No Calibration Required (On-the-fly Quantization) + +These methods are generally easier to use as they don't need a separate calibration dataset or step. + +#### bitsandbytes + +| Pros | Cons | +|--------------------------------------------------------------|---------------------------------------------------------| +| Very simple, no calibration dataset required for inference. | Primarily optimized for NVIDIA GPUs (CUDA). | +| Good community support and widely adopted. | Inference speedup isn't guaranteed. | + +See the [bitsandbytes documentation](./bitsandbytes) for more details. + +#### HQQ (Half-Quadratic Quantization) + +| Pros | Cons | +|----------------------------------------------------------------------|----------------------------------------------------------------------------| +| Fast quantization process, no calibration data needed. | Accuracy can degrade significantly at bit depths <4-bit. | +| Multiple backends for fast inference. | Inference speed may not match others unless using `torch.compile` or backends. | +| Compatible with `torch.compile`. | | +| Supports wide range of bit depths (8, 4, 3, 2, 1-bit). | | + +See the [HQQ documentation](./hqq) for more details. + +#### torchao + +| Pros | Cons | +|----------------------------------------------------------------------|----------------------------------------------------------------------| +| Strong integration with `torch.compile` for potential speedups. | Newer library, ecosystem still evolving. | +| Offers decent CPU quantization support. | Performance depends on `torch.compile` working well. | +| Flexibility in quantization schemes (int8, int4, fp8). | 4-bit quantization (int4wo) may not match GPTQ/AWQ in accuracy. | + +See the [torchao documentation](./torchao) for more details. + +### Calibration-based Quantization + +These methods require an upfront calibration step using a dataset to potentially achieve higher accuracy. + +#### GPTQ/GPTQModel + +Calibration for 8B model takes ~20 minutes on one A100 gpu. + +| Pros | Cons | +|----------------------------------------------------------------------|----------------------------------------------------------------------| +| Often achieves high accuracy. | Requires a calibration dataset and a separate calibration step. | +| Can lead to inference speedups. | Possible to overfit on calibration data. | +| Many pre-quantized GPTQ models on [Hugging Face Hub](https://huggingface.co/models?other=gptq). | | + +See the [GPTQ documentation](./gptq) for more details. + +#### AWQ (Activation-aware Weight Quantization) + +Calibration for 8B model takes ~10 minutes on one A100 gpu. + +| Pros | Cons | +|----------------------------------------------------------------------|-----------------------------------------------------| +| Often achieves high accuracy at 4-bit. (Sometimes surpasses GPTQ on specific tasks.) | Requires calibration if quantizing yourself. | +| Can lead to inference speedups. | | +| Shorter calibration time than GPTQ. | | +| Many pre-quantized AWQ models on [Hugging Face Hub](https://huggingface.co/models?other=awq). | | + +See the [AWQ documentation](./awq) for more details. + +### Loading Specific Formats + +#### compressed-tensors + +| Pros | Cons | +|--------------------------------------------------------------|-------------------------------------------------------------| +| Supports flexible formats including FP8 and sparsity. | Primarily for loading pre-quantized models. | +| | Doesn't perform quantization within Transformers directly. | + +See the [compressed-tensors documentation](./compressed_tensors) for more details. + +## Fine-tuning + +Consider the quantization method below during fine-tuning to save memory. + +### bitsandbytes[[training]] + +* **Description:** The standard method for QLoRA fine-tuning via PEFT. +* **Pros:** Enables fine-tuning large models on consumer GPUs; widely supported and documented for PEFT. +* **Cons:** Primarily for NVIDIA GPUs. + +Other methods offer PEFT compatibility, though bitsandbytes is the most established and straightforward path for QLoRA. + +See the [bitsandbytes documentation](./bitsandbytes#qlora) and [PEFT Docs](https://huggingface.co/docs/peft/developer_guides/quantization#aqlm-quantization) for more details. + +## Research + +Methods like [AQLM](./aqlm), [SpQR](./spqr), [VPTQ](./vptq), [HIGGS](./higgs), etc., push the boundaries of compression (< 2-bit) or explore novel techniques. + +* Consider these if: + * You need extreme compression (sub-4-bit). + * You are conducting research or require state-of-the-art results from their respective papers. + * You have significant compute resources available for potentially complex quantization procedures. +We recommend consulting each methods documentation and associated papers carefully before choosing one for use in production. + +## Benchmark Comparison + +To provide a quantitative comparison of different quantization methods, we benchmarked several popular techniques on the Llama 3.1 8B and 70B models. The following tables show results for accuracy (higher is better), inference throughput measured in tokens/second (higher is better), peak VRAM usage measured in GB (lower is better), and quantization time. + +Performance metrics were measured on 2 NVIDIA A100 80GB GPU for Llama 3.1 70B (bfloat16), 1 NVIDIA H100 80GB GPU for FP8 methods, and 1 NVIDIA A100 80GB GPU for all other methods. Throughput was measured with a batch size of 1 and generating 64 tokens. +Results for `torch.compile` and Marlin kernels are included where applicable and supported. + + + +The key takeaways are: + +| Quantization & Methods | Memory Savings (vs bf16) | Accuracy | Other Notes | +|-------------------------------------------- |------------------------- |--------------------- |------------------------------------------------------------------- | +| **8-bit** (bnb-int8, HQQ, Quanto, torchao, fp8) | ~2x | Very close to baseline bf16 model | | +| **4-bit** (AWQ, GPTQ, HQQ, bnb-nf4) | ~4x | Relatively high accuracy | AWQ/GPTQ often lead in accuracy but need calibration. HQQ/bnb-nf4 are easy on-the-fly. | +| **Sub-4-bit** (VPTQ, AQLM, 2-bit GPTQ) | Extreme (>4x) | Noticeable drop, especially at 2-bit | Quantization times can be very long (AQLM, VPTQ). Performance varies. | + +> [!TIP] +> Always benchmark the performance (accuracy and speed) of the quantized model on your specific task and hardware to ensure it meets your requirements. Refer to the individual documentation pages linked above for detailed usage instructions. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/spqr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/spqr.md new file mode 100644 index 0000000000000000000000000000000000000000..087a2ff489d5e90e3cb9d7e896c26a6504296b72 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/spqr.md @@ -0,0 +1,40 @@ + + +# SpQR + +The [SpQR](https://hf.co/papers/2306.03078) quantization algorithm involves a 16x16 tiled bi-level group 3-bit quantization structure with sparse outliers. + +
+ +
+ +> [!TIP] +> To quantize a model with SpQR, refer to the [Vahe1994/SpQR](https://github.com/Vahe1994/SpQR) repository. + +Load a SpQR-quantized model with [`~PreTrainedModel.from_pretrained`]. + +```python +from transformers import AutoTokenizer, AutoModelForCausalLM +import torch + +quantized_model = AutoModelForCausalLM.from_pretrained( + "elvircrn/Llama-2-7b-SPQR-3Bit-16x16-red_pajama-hf", + torch_dtype=torch.half, + device_map="auto" +) +tokenizer = AutoTokenizer.from_pretrained("elvircrn/Llama-2-7b-SPQR-3Bit-16x16-red_pajama-hf") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/torchao.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/torchao.md new file mode 100644 index 0000000000000000000000000000000000000000..77616be4cbb8526ef9841cc5582671de0b55caa1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/torchao.md @@ -0,0 +1,675 @@ + + +# torchao + +[![Open In Colab: Torchao Demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/quantization/torchao.ipynb) + +[torchao](https://github.com/pytorch/ao) is a PyTorch architecture optimization library with support for custom high performance data types, quantization, and sparsity. It is composable with native PyTorch features such as [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) for even faster inference and training. + +See the table below for additional torchao features. + +| Feature | Description | +|--------|-------------| +| **Quantization Aware Training (QAT)** | Train quantized models with minimal accuracy loss (see [QAT README](https://github.com/pytorch/ao/blob/main/torchao/quantization/qat/README.md)) | +| **Float8 Training** | High-throughput training with float8 formats (see [torchtitan](https://github.com/pytorch/torchtitan/blob/main/docs/float8.md) and [Accelerate](https://huggingface.co/docs/accelerate/usage_guides/low_precision_training#configuring-torchao) docs) | +| **Sparsity Support** | Semi-structured (2:4) sparsity for faster inference (see [Accelerating Neural Network Training with Semi-Structured (2:4) Sparsity](https://pytorch.org/blog/accelerating-neural-network-training/) blog post) | +| **Optimizer Quantization** | Reduce optimizer state memory with 4 and 8-bit variants of Adam | +| **KV Cache Quantization** | Enables long context inference with lower memory (see [KV Cache Quantization](https://github.com/pytorch/ao/blob/main/torchao/_models/llama/README.md)) | +| **Custom Kernels Support** | use your own `torch.compile` compatible ops | +| **FSDP2** | Composable with FSDP2 for training| + +> [!TIP] +> Refer to the torchao [README.md](https://github.com/pytorch/ao#torchao-pytorch-architecture-optimization) for more details about the library. + + +torchao supports the [quantization techniques](https://github.com/pytorch/ao/blob/main/torchao/quantization/README.md) below. + +- A16W8 Float8 Dynamic Quantization +- A16W8 Float8 WeightOnly Quantization +- A8W8 Int8 Dynamic Quantization +- A16W8 Int8 Weight Only Quantization +- A16W4 Int4 Weight Only Quantization +- A16W4 Int4 Weight Only Quantization + 2:4 Sparsity +- Autoquantization + +torchao also supports module level configuration by specifying a dictionary from fully qualified name of module and its corresponding quantization config. This allows skip quantizing certain layers and using different quantization config for different modules. + + +Check the table below to see if your hardware is compatible. + +| Component | Compatibility | +|----------|----------------| +| CUDA Versions | โœ… cu118, cu126, cu128 | +| XPU Versions | โœ… pytorch2.8 | +| CPU | โœ… change `device_map="cpu"` (see examples below) | + + + +Install torchao from PyPi or the PyTorch index with the following commands. + + + + +```bash +# Updating ๐Ÿค— Transformers to the latest version, as the example script below uses the new auto compilation +# Stable release from Pypi which will default to CUDA 12.6 +pip install --upgrade torchao transformers +``` + + +Stable Release from the PyTorch index + +```bash +pip install torchao --index-url https://download.pytorch.org/whl/cu126 # options are cpu/cu118/cu126/cu128 +``` + + + +If your torchao version is below 0.10.0, you need to upgrade it, please refer to the [deprecation notice](#deprecation-notice) for more details. + +## Quantization examples + +TorchAO provides a variety of quantization configurations. Each configuration can be further customized with parameters such as `group_size`, `scheme`, and `layout` to optimize for specific hardware and model architectures. + +For a complete list of available configurations, see the [quantization API documentation](https://github.com/pytorch/ao/blob/main/torchao/quantization/quant_api.py). + +You can manually choose the quantization types and settings or automatically select the quantization types. + +Create a [`TorchAoConfig`] and specify the quantization type and `group_size` of the weights to quantize (for int8 weight only and int4 weight only). Set the `cache_implementation` to `"static"` to automatically [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) the forward method. + +We'll show examples for recommended quantization methods based on hardwares, e.g. A100 GPU, H100 GPU, CPU. + +### H100 GPU + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, Float8WeightOnlyConfig + +quant_config = Float8DynamicActivationFloat8WeightConfig() +# or float8 weight only quantization +# quant_config = Float8WeightOnlyConfig() +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import GemliteUIntXWeightOnlyConfig + +# We integrated with gemlite, which optimizes for batch size N on A100 and H100 +quant_config = GemliteUIntXWeightOnlyConfig(group_size=128) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int4WeightOnlyConfig +from torchao.dtypes import MarlinSparseLayout + +quant_config = Int4WeightOnlyConfig(layout=MarlinSparseLayout()) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model with sparsity. A sparse checkpoint is needed to accelerate without accuracy loss +quantized_model = AutoModelForCausalLM.from_pretrained( + "RedHatAI/Sparse-Llama-3.1-8B-2of4", + torch_dtype=torch.float16, + device_map="cuda", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("RedHatAI/Sparse-Llama-3.1-8B-2of4") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +### A100 GPU + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int8DynamicActivationInt8WeightConfig, Int8WeightOnlyConfig + +quant_config = Int8DynamicActivationInt8WeightConfig() +# or int8 weight only quantization +# quant_config = Int8WeightOnlyConfig() +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import GemliteUIntXWeightOnlyConfig, Int4WeightOnlyConfig + +# For batch size N, we recommend gemlite, which may require autotuning +# default is 4 bit, 8 bit is also supported by passing `bit_width=8` +quant_config = GemliteUIntXWeightOnlyConfig(group_size=128) + +# For batch size 1, we also have custom tinygemm kernel that's only optimized for this +# We can set `use_hqq` to `True` for better accuracy +# quant_config = Int4WeightOnlyConfig(group_size=128, use_hqq=True) + +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int4WeightOnlyConfig +from torchao.dtypes import MarlinSparseLayout + +quant_config = Int4WeightOnlyConfig(layout=MarlinSparseLayout()) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model with sparsity. A sparse checkpoint is needed to accelerate without accuracy loss +quantized_model = AutoModelForCausalLM.from_pretrained( + "RedHatAI/Sparse-Llama-3.1-8B-2of4", + torch_dtype=torch.float16, + device_map="cuda", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("RedHatAI/Sparse-Llama-3.1-8B-2of4") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +### Intel XPU + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int8DynamicActivationInt8WeightConfig, Int8WeightOnlyConfig + +quant_config = Int8DynamicActivationInt8WeightConfig() +# or int8 weight only quantization +# quant_config = Int8WeightOnlyConfig() +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("xpu") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int4WeightOnlyConfig +from torchao.dtypes import Int4XPULayout +from torchao.quantization.quant_primitives import ZeroPointDomain + + +quant_config = Int4WeightOnlyConfig(group_size=128, layout=Int4XPULayout(), zero_point_domain=ZeroPointDomain.INT) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to("xpu") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + +### CPU + + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int8DynamicActivationInt8WeightConfig, Int8WeightOnlyConfig + +quant_config = Int8DynamicActivationInt8WeightConfig() +# quant_config = Int8WeightOnlyConfig() +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="cpu", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +> [!TIP] +> Run the quantized model on a CPU by changing `device_map` to `"cpu"` and `layout` to `Int4CPULayout()`. + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int4WeightOnlyConfig +from torchao.dtypes import Int4CPULayout + +quant_config = Int4WeightOnlyConfig(group_size=128, layout=Int4CPULayout()) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="cpu", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt") + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +### Per Module Quantization +#### 1. Skip quantization for certain layers +With `ModuleFqnToConfig` we can specify a default configuration for all layers while skipping quantization for certain layers. +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig + +model_id = "meta-llama/Llama-3.1-8B-Instruct" + +from torchao.quantization import Int4WeightOnlyConfig, ModuleFqnToConfig +config = Int4WeightOnlyConfig(group_size=128) + +# set default to int4 (for linears), and skip quantizing `model.layers.0.self_attn.q_proj` +quant_config = ModuleFqnToConfig({"_default": config, "model.layers.0.self_attn.q_proj": None}) +quantization_config = TorchAoConfig(quant_type=quant_config) +quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, quantization_config=quantization_config) +# lm_head is not quantized and model.layers.0.self_attn.q_proj is not quantized +print("quantized model:", quantized_model) +tokenizer = AutoTokenizer.from_pretrained(model_id) + +# Manual Testing +prompt = "Hey, are you conscious? Can you talk to me?" +inputs = tokenizer(prompt, return_tensors="pt").to(quantized_model.device.type) +generated_ids = quantized_model.generate(**inputs, max_new_tokens=128) +output_text = tokenizer.batch_decode( + generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False +) +print(output_text) +``` + +#### 2. Quantizing different layers with different quantization configs +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig + +model_id = "facebook/opt-125m" + +from torchao.quantization import Int4WeightOnlyConfig, ModuleFqnToConfig, Int8DynamicActivationInt4WeightConfig, IntxWeightOnlyConfig, PerAxis, MappingType + +weight_dtype = torch.int8 +granularity = PerAxis(0) +mapping_type = MappingType.ASYMMETRIC +embedding_config = IntxWeightOnlyConfig( + weight_dtype=weight_dtype, + granularity=granularity, + mapping_type=mapping_type, +) +linear_config = Int8DynamicActivationInt4WeightConfig(group_size=128) +quant_config = ModuleFqnToConfig({"_default": linear_config, "model.decoder.embed_tokens": embedding_config, "model.decoder.embed_positions": None}) +# set `include_embedding` to True in order to include embedding in quantization +# when `include_embedding` is True, we'll remove input embedding from `modules_not_to_convert` as well +quantization_config = TorchAoConfig(quant_type=quant_config, include_embedding=True) +quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu", torch_dtype=torch.bfloat16, quantization_config=quantization_config) +print("quantized model:", quantized_model) +# make sure embedding is quantized +print("embed_tokens weight:", quantized_model.model.decoder.embed_tokens.weight) +tokenizer = AutoTokenizer.from_pretrained(model_id) + +# Manual Testing +prompt = "Hey, are you conscious? Can you talk to me?" +inputs = tokenizer(prompt, return_tensors="pt").to("cpu") +generated_ids = quantized_model.generate(**inputs, max_new_tokens=128, cache_implementation="static") +output_text = tokenizer.batch_decode( + generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False +) +print(output_text) +``` + +### Autoquant + +If you want to automatically choose a quantization type for quantizable layers (`nn.Linear`) you can use the [autoquant](https://pytorch.org/ao/stable/generated/torchao.quantization.autoquant.html#torchao.quantization.autoquant) API. + +The `autoquant` API automatically chooses a quantization type by micro-benchmarking on input type and shape and compiling a single linear layer. + +Note: autoquant is for GPU only right now. + +Create a [`TorchAoConfig`] and set to `"autoquant"`. Set the `cache_implementation` to `"static"` to automatically [torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) the forward method. Finally, call `finalize_autoquant` on the quantized model to finalize the quantization and log the input shapes. + + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer + +quantization_config = TorchAoConfig("autoquant", min_sqnr=None) +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="auto", + quantization_config=quantization_config +) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to(quantized_model.device.type) + +# auto-compile the quantized model with `cache_implementation="static"` to get speed up +output = quantized_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") +# explicitly call `finalize_autoquant` (may be refactored and removed in the future) +quantized_model.finalize_autoquant() +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + +## Serialization + +torchao implements [torch.Tensor subclasses](https://pytorch.org/docs/stable/notes/extending.html#subclassing-torch-tensor) for maximum flexibility in supporting new quantized torch.Tensor formats. [Safetensors](https://huggingface.co/docs/safetensors/en/index) serialization and deserialization does not work with torchao. + +To avoid arbitrary user code execution, torchao sets `weights_only=True` in [torch.load](https://pytorch.org/docs/stable/generated/torch.load.html) to ensure only tensors are loaded. Any known user functions can be whitelisted with [add_safe_globals](https://pytorch.org/docs/stable/notes/serialization.html#torch.serialization.add_safe_globals). + + + + +```py +# don't serialize model with Safetensors +output_dir = "llama3-8b-int4wo-128" +quantized_model.save_pretrained("llama3-8b-int4wo-128", safe_serialization=False) +``` + + + +```py +# don't serialize model with Safetensors +USER_ID = "your_huggingface_user_id" +REPO_ID = "llama3-8b-int4wo-128" +quantized_model.push_to_hub(f"{USER_ID}/llama3-8b-int4wo-128", safe_serialization=False) +tokenizer.push_to_hub(f"{USER_ID}/llama3-8b-int4wo-128") +``` + + + + +## Loading quantized models + +Loading a quantized model depends on the quantization scheme. For quantization schemes, like int8 and float8, you can quantize the model on any device and also load it on any device. The example below demonstrates quantizing a model on the CPU and then loading it on CUDA or XPU. +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int8WeightOnlyConfig + +quant_config = Int8WeightOnlyConfig(group_size=128) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="cpu", + quantization_config=quantization_config +) +# save the quantized model +output_dir = "llama-3.1-8b-torchao-int8" +quantized_model.save_pretrained(output_dir, safe_serialization=False) + +# reload the quantized model +reloaded_model = AutoModelForCausalLM.from_pretrained( + output_dir, + device_map="auto", + torch_dtype=torch.bfloat16 +) +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt").to(reloaded_model.device.type) + +output = reloaded_model.generate(**input_ids, max_new_tokens=10) +print(tokenizer.decode(output[0], skip_special_tokens=True)) + +``` +For int4, the model can only be loaded on the same device it was quantized on because the layout is specific to the device. The example below demonstrates quantizing and loading a model on the CPU. + +```py +import torch +from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer +from torchao.quantization import Int4WeightOnlyConfig +from torchao.dtypes import Int4CPULayout + +quant_config = Int4WeightOnlyConfig(group_size=128, layout=Int4CPULayout()) +quantization_config = TorchAoConfig(quant_type=quant_config) + +# Load and quantize the model +quantized_model = AutoModelForCausalLM.from_pretrained( + "meta-llama/Llama-3.1-8B-Instruct", + torch_dtype="auto", + device_map="cpu", + quantization_config=quantization_config +) +# save the quantized model +output_dir = "llama-3.1-8b-torchao-int4-cpu" +quantized_model.save_pretrained(output_dir, safe_serialization=False) + +# reload the quantized model +reloaded_model = AutoModelForCausalLM.from_pretrained( + output_dir, + device_map="cpu", + torch_dtype=torch.bfloat16 +) +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct") +input_text = "What are we having for dinner?" +input_ids = tokenizer(input_text, return_tensors="pt") + +output = reloaded_model.generate(**input_ids, max_new_tokens=10) +print(tokenizer.decode(output[0], skip_special_tokens=True)) + +``` + +## โš ๏ธ Deprecation Notice + +> Starting with version 0.10.0, the string-based API for quantization configuration (e.g., `TorchAoConfig("int4_weight_only", group_size=128)`) is **deprecated** and will be removed in a future release. +> +> Please use the new `AOBaseConfig`-based approach instead: +> +> ```python +> # Old way (deprecated) +> quantization_config = TorchAoConfig("int4_weight_only", group_size=128) +> +> # New way (recommended) +> from torchao.quantization import Int4WeightOnlyConfig +> quant_config = Int4WeightOnlyConfig(group_size=128) +> quantization_config = TorchAoConfig(quant_type=quant_config) +> ``` +> +> The new API offers greater flexibility, better type safety, and access to the full range of features available in torchao. +> +> [Migration Guide](#migration-guide) +> +> Here's how to migrate from common string identifiers to their `AOBaseConfig` equivalents: +> +> | Old String API | New `AOBaseConfig` API | +> |----------------|------------------------| +> | `"int4_weight_only"` | `Int4WeightOnlyConfig()` | +> | `"int8_weight_only"` | `Int8WeightOnlyConfig()` | +> | `"int8_dynamic_activation_int8_weight"` | `Int8DynamicActivationInt8WeightConfig()` | +> +> All configuration objects accept parameters for customization (e.g., `group_size`, `scheme`, `layout`). + + + +## Resources + +For a better sense of expected performance, view the [benchmarks](https://github.com/pytorch/ao/tree/main/torchao/quantization#benchmarks) for various models with CUDA and XPU backends. You can also run the code below to benchmark a model yourself. + +```py +from torch._inductor.utils import do_bench_using_profiling +from typing import Callable + +def benchmark_fn(func: Callable, *args, **kwargs) -> float: + """Thin wrapper around do_bench_using_profiling""" + no_args = lambda: func(*args, **kwargs) + time = do_bench_using_profiling(no_args) + return time * 1e3 + +MAX_NEW_TOKENS = 1000 +print("int4wo-128 model:", benchmark_fn(quantized_model.generate, **input_ids, max_new_tokens=MAX_NEW_TOKENS, cache_implementation="static")) + +bf16_model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.bfloat16) +output = bf16_model.generate(**input_ids, max_new_tokens=10, cache_implementation="static") # auto-compile +print("bf16 model:", benchmark_fn(bf16_model.generate, **input_ids, max_new_tokens=MAX_NEW_TOKENS, cache_implementation="static")) +``` + +> [!TIP] +> For best performance, you can use recommended settings by calling `torchao.quantization.utils.recommended_inductor_config_setter()` + +Refer to [Other Available Quantization Techniques](https://github.com/pytorch/ao/tree/main/torchao/quantization#other-available-quantization-techniques) for more examples and documentation. + +## Issues + +If you encounter any issues with the Transformers integration, please open an issue on the [Transformers](https://github.com/huggingface/transformers/issues) repository. For issues directly related to torchao, please open an issue on the [torchao](https://github.com/pytorch/ao/issues) repository. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/vptq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/vptq.md new file mode 100644 index 0000000000000000000000000000000000000000..392895e0ea14f54d6cb9bf8469f587d0f8a56e1d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quantization/vptq.md @@ -0,0 +1,72 @@ + + +# VPTQ + +[Vector Post-Training Quantization (VPTQ)](https://github.com/microsoft/VPTQ) is a Post-Training Quantization (PTQ) method that leverages vector quantization to quantize LLMs at an extremely low bit-width (<2-bit). VPTQ can compress a 70B, even a 405B model, to 1-2 bits without retraining and still maintain a high-degree of accuracy. It is a lightweight quantization algorithm that takes ~17 hours to quantize a 405B model. VPTQ features agile quantization inference with low decoding overhead and high throughput and Time To First Token (TTFT). + +Run the command below to install VPTQ which provides efficient kernels for inference on NVIDIA and AMD GPUs. + +```bash +pip install vptq +``` + +The [VPTQ-community](https://huggingface.co/VPTQ-community) provides a collection of VPTQ-quantized models. The model name contains information about its bitwidth (excluding cookbook, parameter, and padding overhead). Consider the [Meta-Llama-3.1-70B-Instruct-v8-k65536-256-woft] model as an example. + +- The model name is Meta-Llama-3.1-70B-Instruct. +- The number of centroids is given by 65536 (2^16). +- The number of residual centroids is given by 256 (2^8). + +The equivalent bit-width calculation is given by the following. + +- index: log2(65536) = 16 / 8 = 2-bits +- residual index: log2(256) = 8 / 8 = 1-bit +- total bit-width: 2 + 1 = 3-bits + +From here, estimate the model size by multiplying 70B * 3-bits / 8-bits/byte for a total of 26.25GB. + +Load a VPTQ quantized model with [`~PreTrainedModel.from_pretrained`]. + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM + +quantized_model = AutoModelForCausalLM.from_pretrained( + "VPTQ-community/Meta-Llama-3.1-70B-Instruct-v16-k65536-65536-woft", + torch_dtype="auto", + device_map="auto" +) +``` + +To quantize your own model, refer to the [VPTQ Quantization Algorithm Tutorial](https://github.com/microsoft/VPTQ/blob/algorithm/algorithm.md) tutorial. + +## Benchmarks + +VPTQ achieves better accuracy and higher throughput with lower quantization overhead across models of different sizes. The following experimental results are for reference only; VPTQ can achieve better outcomes under reasonable parameters, especially in terms of model accuracy and inference speed. + +| Model | bitwidth | W2โ†“ | C4โ†“ | AvgQAโ†‘ | tok/sโ†‘ | mem(GB) | cost/hโ†“ | +| ----------- | -------- | ---- | ---- | ------ | ------ | ------- | ------- | +| LLaMA-2 7B | 2.02 | 6.13 | 8.07 | 58.2 | 39.9 | 2.28 | 2 | +| | 2.26 | 5.95 | 7.87 | 59.4 | 35.7 | 2.48 | 3.1 | +| LLaMA-2 13B | 2.02 | 5.32 | 7.15 | 62.4 | 26.9 | 4.03 | 3.2 | +| | 2.18 | 5.28 | 7.04 | 63.1 | 18.5 | 4.31 | 3.6 | +| LLaMA-2 70B | 2.07 | 3.93 | 5.72 | 68.6 | 9.7 | 19.54 | 19 | +| | 2.11 | 3.92 | 5.71 | 68.7 | 9.7 | 20.01 | 19 | + +## Resources + +See an example demo of VPTQ on the VPTQ Online Demo [Space](https://huggingface.co/spaces/microsoft/VPTQ) or try running the VPTQ inference [notebook](https://colab.research.google.com/github/microsoft/VPTQ/blob/main/notebooks/vptq_example.ipynb). + +For more information, read the VPTQ [paper](https://huggingface.co/papers/2409.17066). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..18e273651c1f128b94f65caa978af35eeec8642a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/quicktour.md @@ -0,0 +1,361 @@ + + +# Quickstart + +[[open-in-colab]] + +Transformers is designed to be fast and easy to use so that everyone can start learning or building with transformer models. + +The number of user-facing abstractions is limited to only three classes for instantiating a model, and two APIs for inference or training. This quickstart introduces you to Transformers' key features and shows you how to: + +- load a pretrained model +- run inference with [`Pipeline`] +- fine-tune a model with [`Trainer`] + +## Set up + +To start, we recommend creating a Hugging Face [account](https://hf.co/join). An account lets you host and access version controlled models, datasets, and [Spaces](https://hf.co/spaces) on the Hugging Face [Hub](https://hf.co/docs/hub/index), a collaborative platform for discovery and building. + +Create a [User Access Token](https://hf.co/docs/hub/security-tokens#user-access-tokens) and log in to your account. + + + + +Paste your User Access Token into [`~huggingface_hub.notebook_login`] when prompted to log in. + +```py +from huggingface_hub import notebook_login + +notebook_login() +``` + + + + +Make sure the [huggingface_hub[cli]](https://huggingface.co/docs/huggingface_hub/guides/cli#getting-started) package is installed and run the command below. Paste your User Access Token when prompted to log in. + +```bash +hf auth login +``` + + + + +Install a machine learning framework. + + + + +```bash +!pip install torch +``` + + + + +```bash +!pip install tensorflow +``` + + + + +Then install an up-to-date version of Transformers and some additional libraries from the Hugging Face ecosystem for accessing datasets and vision models, evaluating training, and optimizing training for large models. + +```bash +!pip install -U transformers datasets evaluate accelerate timm +``` + +## Pretrained models + +Each pretrained model inherits from three base classes. + +| **Class** | **Description** | +|---|---| +| [`PretrainedConfig`] | A file that specifies a models attributes such as the number of attention heads or vocabulary size. | +| [`PreTrainedModel`] | A model (or architecture) defined by the model attributes from the configuration file. A pretrained model only returns the raw hidden states. For a specific task, use the appropriate model head to convert the raw hidden states into a meaningful result (for example, [`LlamaModel`] versus [`LlamaForCausalLM`]). | +| Preprocessor | A class for converting raw inputs (text, images, audio, multimodal) into numerical inputs to the model. For example, [`PreTrainedTokenizer`] converts text into tensors and [`ImageProcessingMixin`] converts pixels into tensors. | + +We recommend using the [AutoClass](./model_doc/auto) API to load models and preprocessors because it automatically infers the appropriate architecture for each task and machine learning framework based on the name or path to the pretrained weights and configuration file. + +Use [`~PreTrainedModel.from_pretrained`] to load the weights and configuration file from the Hub into the model and preprocessor class. + + + + +When you load a model, configure the following parameters to ensure the model is optimally loaded. + +- `device_map="auto"` automatically allocates the model weights to your fastest device first. +- `torch_dtype="auto"` directly initializes the model weights in the data type they're stored in, which can help avoid loading the weights twice (PyTorch loads weights in `torch.float32` by default). + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf", torch_dtype="auto", device_map="auto") +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf") +``` + +Tokenize the text and return PyTorch tensors with the tokenizer. Move the model to an accelerator if it's available to accelerate inference. + +```py +model_inputs = tokenizer(["The secret to baking a good cake is "], return_tensors="pt").to(model.device) +``` + +The model is now ready for inference or training. + +For inference, pass the tokenized inputs to [`~GenerationMixin.generate`] to generate text. Decode the token ids back into text with [`~PreTrainedTokenizerBase.batch_decode`]. + +```py +generated_ids = model.generate(**model_inputs, max_length=30) +tokenizer.batch_decode(generated_ids)[0] +' The secret to baking a good cake is 100% in the preparation. There are so many recipes out there,' +``` + + + + +```py +from transformers import TFAutoModelForCausalLM, AutoTokenizer + +model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2-xl") +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2-xl") +``` + +Tokenize the text and return TensorFlow tensors with the tokenizer. + +```py +model_inputs = tokenizer(["The secret to baking a good cake is "], return_tensors="tf") +``` + +The model is now ready for inference or training. + +For inference, pass the tokenized inputs to [`~GenerationMixin.generate`] to generate text. Decode the token ids back into text with [`~PreTrainedTokenizerBase.batch_decode`]. + +```py +generated_ids = model.generate(**model_inputs, max_length=30) +tokenizer.batch_decode(generated_ids)[0] +'The secret to baking a good cake is \xa0to use the right ingredients. \xa0The secret to baking a good cake is to use the right' +``` + + + + +> [!TIP] +> Skip ahead to the [Trainer](#trainer-api) section to learn how to fine-tune a model. + +## Pipeline + +The [`Pipeline`] class is the most convenient way to inference with a pretrained model. It supports many tasks such as text generation, image segmentation, automatic speech recognition, document question answering, and more. + +> [!TIP] +> Refer to the [Pipeline](./main_classes/pipelines) API reference for a complete list of available tasks. + +Create a [`Pipeline`] object and select a task. By default, [`Pipeline`] downloads and caches a default pretrained model for a given task. Pass the model name to the `model` parameter to choose a specific model. + + + + +Use [`~infer_device`] to automatically detect an available accelerator for inference. + +```py +from transformers import pipeline, infer_device + +device = infer_device() + +pipeline = pipeline("text-generation", model="meta-llama/Llama-2-7b-hf", device=device) +``` + +Prompt [`Pipeline`] with some initial text to generate more text. + +```py +pipeline("The secret to baking a good cake is ", max_length=50) +[{'generated_text': 'The secret to baking a good cake is 100% in the batter. The secret to a great cake is the icing.\nThis is why weโ€™ve created the best buttercream frosting reci'}] +``` + + + + +Use [`~infer_device`] to automatically detect an available accelerator for inference. + +```py +from transformers import pipeline, infer_device + +device = infer_device() + +pipeline = pipeline("image-segmentation", model="facebook/detr-resnet-50-panoptic", device=device) +``` + +Pass an image - a URL or local path to the image - to [`Pipeline`]. + +
+ +
+ +```py +segments = pipeline("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png") +segments[0]["label"] +'bird' +segments[1]["label"] +'bird' +``` + +
+ + +Use [`~infer_device`] to automatically detect an available accelerator for inference. + +```py +from transformers import pipeline, infer_device + +device = infer_device() + +pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3", device=device) +``` + +Pass an audio file to [`Pipeline`]. + +```py +pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac") +{'text': ' He hoped there would be stew for dinner, turnips and carrots and bruised potatoes and fat mutton pieces to be ladled out in thick, peppered flour-fatten sauce.'} +``` + + +
+ +## Trainer + +[`Trainer`] is a complete training and evaluation loop for PyTorch models. It abstracts away a lot of the boilerplate usually involved in manually writing a training loop, so you can start training faster and focus on training design choices. You only need a model, dataset, a preprocessor, and a data collator to build batches of data from the dataset. + +Use the [`TrainingArguments`] class to customize the training process. It provides many options for training, evaluation, and more. Experiment with training hyperparameters and features like batch size, learning rate, mixed precision, torch.compile, and more to meet your training needs. You could also use the default training parameters to quickly produce a baseline. + +Load a model, tokenizer, and dataset for training. + +```py +from transformers import AutoModelForSequenceClassification, AutoTokenizer +from datasets import load_dataset + +model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +dataset = load_dataset("rotten_tomatoes") +``` + +Create a function to tokenize the text and convert it into PyTorch tensors. Apply this function to the whole dataset with the [`~datasets.Dataset.map`] method. + +```py +def tokenize_dataset(dataset): + return tokenizer(dataset["text"]) +dataset = dataset.map(tokenize_dataset, batched=True) +``` + +Load a data collator to create batches of data and pass the tokenizer to it. + +```py +from transformers import DataCollatorWithPadding + +data_collator = DataCollatorWithPadding(tokenizer=tokenizer) +``` + +Next, set up [`TrainingArguments`] with the training features and hyperparameters. + +```py +from transformers import TrainingArguments + +training_args = TrainingArguments( + output_dir="distilbert-rotten-tomatoes", + learning_rate=2e-5, + per_device_train_batch_size=8, + per_device_eval_batch_size=8, + num_train_epochs=2, + push_to_hub=True, +) +``` + +Finally, pass all these separate components to [`Trainer`] and call [`~Trainer.train`] to start. + +```py +from transformers import Trainer + +trainer = Trainer( + model=model, + args=training_args, + train_dataset=dataset["train"], + eval_dataset=dataset["test"], + tokenizer=tokenizer, + data_collator=data_collator, +) + +trainer.train() +``` + +Share your model and tokenizer to the Hub with [`~Trainer.push_to_hub`]. + +```py +trainer.push_to_hub() +``` + +Congratulations, you just trained your first model with Transformers! + +### TensorFlow + +> [!WARNING] +> Not all pretrained models are available in TensorFlow. Refer to a models API doc to check whether a TensorFlow implementation is supported. + +[`Trainer`] doesn't work with TensorFlow models, but you can still train a Transformers model implemented in TensorFlow with [Keras](https://keras.io/). Transformers TensorFlow models are a standard [tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model), which is compatible with Keras' [compile](https://keras.io/api/models/model_training_apis/#compile-method) and [fit](https://keras.io/api/models/model_training_apis/#fit-method) methods. + +Load a model, tokenizer, and dataset for training. + +```py +from transformers import TFAutoModelForSequenceClassification, AutoTokenizer + +model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Create a function to tokenize the text and convert it into TensorFlow tensors. Apply this function to the whole dataset with the [`~datasets.Dataset.map`] method. + +```py +def tokenize_dataset(dataset): + return tokenizer(dataset["text"]) +dataset = dataset.map(tokenize_dataset) +``` + +Transformers provides the [`~TFPreTrainedModel.prepare_tf_dataset`] method to collate and batch a dataset. + +```py +tf_dataset = model.prepare_tf_dataset( + dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer +) +``` + +Finally, call [compile](https://keras.io/api/models/model_training_apis/#compile-method) to configure the model for training and [fit](https://keras.io/api/models/model_training_apis/#fit-method) to start. + +```py +from tensorflow.keras.optimizers import Adam + +model.compile(optimizer="adam") +model.fit(tf_dataset) +``` + +## Next steps + +Now that you have a better understanding of Transformers and what it offers, it's time to keep exploring and learning what interests you the most. + +- **Base classes**: Learn more about the configuration, model and processor classes. This will help you understand how to create and customize models, preprocess different types of inputs (audio, images, multimodal), and how to share your model. +- **Inference**: Explore the [`Pipeline`] further, inference and chatting with LLMs, agents, and how to optimize inference with your machine learning framework and hardware. +- **Training**: Study the [`Trainer`] in more detail, as well as distributed training and optimizing training on specific hardware. +- **Quantization**: Reduce memory and storage requirements with quantization and speed up inference by representing weights with fewer bits. +- **Resources**: Looking for end-to-end recipes for how to train and inference with a model for a specific task? Check out the task recipes! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/reference/environment_variables.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/reference/environment_variables.md new file mode 100644 index 0000000000000000000000000000000000000000..fc20c08f9e6a359e103cc285b7426e684fca08dd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/reference/environment_variables.md @@ -0,0 +1,58 @@ + + +# Environment Variables + +## HF_ENABLE_PARALLEL_LOADING + +By default this is disabled. Enables the loading of torch and safetensor based weights to be loaded in parallel. Can decrease the time to load large models significantly, often times producing speed ups around ~50%. + +Can be set to a string equal to `"false"` or `"true"`. e.g. `os.environ["HF_ENABLE_PARALLEL_LOADING"] = "true"`. + +e.g. `facebook/opt-30b` on an AWS EC2 g4dn.metal instance can be made to load in ~30s with this enabled vs ~55s without it. + +Profile before committing to using this environment variable, this will not produce speed ups for smaller models. + +```py +import os + +os.environ["HF_ENABLE_PARALLEL_LOADING"] = "true" + +from transformers import pipeline + +model = pipeline(task="text-generation", model="facebook/opt-30b", device_map="auto") +``` + +## HF_PARALLEL_LOADING_WORKERS + +Determines how many threads should be used when parallel loading is enabled. Default is `8`. + +If the number of files that are being loaded is less than the number of threads specified, the number that is actually spawned will be equal to the number of files. + +e.g. If you specify 8 workers, and there are only 2 files, only 2 workers will be spawned. + +Tune as you see fit. + +```py +import os + +os.environ["HF_ENABLE_PARALLEL_LOADING"] = "true" +os.environ["HF_PARALLEL_LOADING_WORKERS"] = "4" + +from transformers import pipeline + +model = pipeline(task="text-generation", model="facebook/opt-30b", device_map="auto") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/run_scripts.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/run_scripts.md new file mode 100644 index 0000000000000000000000000000000000000000..37e00c9974c35458eaa5e79585919898c94aa0bb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/run_scripts.md @@ -0,0 +1,211 @@ + + +# Training scripts + +Transformers provides many example training scripts for deep learning frameworks (PyTorch, TensorFlow, Flax) and tasks in [transformers/examples](https://github.com/huggingface/transformers/tree/main/examples). There are additional scripts in [transformers/research projects](https://github.com/huggingface/transformers-research-projects/) and [transformers/legacy](https://github.com/huggingface/transformers/tree/main/examples/legacy), but these aren't actively maintained and requires a specific version of Transformers. + +Example scripts are only examples and you may need to adapt the script to your use-case. To help you with this, most scripts are very transparent in how data is preprocessed, allowing you to edit it as necessary. + +For any feature you'd like to implement in an example script, please discuss it on the [forum](https://discuss.huggingface.co/) or in an [issue](https://github.com/huggingface/transformers/issues) before submitting a pull request. While we welcome contributions, it is unlikely a pull request that adds more functionality is added at the cost of readability. + +This guide will show you how to run an example summarization training script in [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) and [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization). + +## Setup + +Install Transformers from source in a new virtual environment to run the latest version of the example script. + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` + +Run the command below to checkout a script from a specific or older version of Transformers. + +```bash +git checkout tags/v3.5.1 +``` + +After you've setup the correct version, navigate to the example folder of your choice and install the example specific requirements. + +```bash +pip install -r requirements.txt +``` + +## Run a script + +Start with a smaller dataset by including the `max_train_samples`, `max_eval_samples`, and `max_predict_samples` parameters to truncate the dataset to a maximum number of samples. This helps ensure training works as expected before committing to the entire dataset which can take hours to complete. + +> [!WARNING] +> Not all example scripts support the `max_predict_samples` parameter. Run the command below to check whether a script supports it or not. +> ```bash +> examples/pytorch/summarization/run_summarization.py -h +> ``` + +The example below fine-tunes [T5-small](https://huggingface.co/google-t5/t5-small) on the [CNN/DailyMail](https://huggingface.co/datasets/abisee/cnn_dailymail) dataset. T5 requires an additional `source_prefix` parameter to prompt it to summarize. + + + + +The example script downloads and preprocesses a dataset, and then fine-tunes it with [`Trainer`] with a supported model architecture. + +Resuming training from a checkpoint is very useful if training is interrupted because you don't have to start over again. There are two ways to resume training from a checkpoint. + +* `--output dir previous_output_dir` resumes training from the latest checkpoint stored in `output_dir`. Remove the `--overwrite_output_dir` parameter if you're using this method. +* `--resume_from_checkpoint path_to_specific_checkpoint` resumes training from a specific checkpoint folder. + +Share your model on the [Hub](https://huggingface.co/) with the `--push_to_hub` parameter. It creates a repository and uploads the model to the folder name specified in `--output_dir`. You could also use the `--push_to_hub_model_id` parameter to specify the repository name. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + # remove the `max_train_samples`, `max_eval_samples` and `max_predict_samples` if everything works + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + # remove if using `output_dir previous_output_dir` + # --overwrite_output_dir \ + --output_dir previous_output_dir \ + # --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate \ +``` + +For mixed precision and distributed training, include the following parameters and launch training with [torchrun](https://pytorch.org/docs/stable/elastic/run.html). + +* Add the `fp16` or `bf16` parameters to enable mixed precision training. XPU devices only supports `bf16`. +* Add the `nproc_per_node` parameter to set number of GPUs to train with. + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + ... + ... +``` + +PyTorch supports TPUs, hardware designed to accelerate performance, through the [PyTorch/XLA](https://github.com/pytorch/xla/blob/master/README.md) package. Launch the `xla_spawn.py` script and use `num _cores` to set the number of TPU cores to train with. + +```bash +python xla_spawn.py --num_cores 8 pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + ... + ... +``` + + + + +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + # remove the `max_train_samples`, `max_eval_samples` and `max_predict_samples` if everything works + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval \ +``` + +TensorFlow uses the [MirroredStrategy](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy) for distributed training and doesn't require adding any additional parameters. The script uses multiple GPUs by default if they are available. + +For TPU training, TensorFlow scripts use the [TPUStrategy](https://www.tensorflow.org/guide/distributed_training#tpustrategy). Pass the TPU resource name to the `--tpu` parameter. + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + ... + ... +``` + + + + +## Accelerate + +[Accelerate](https://huggingface.co/docs/accelerate) is designed to simplify distributed training while offering complete visibility into the PyTorch training loop. If you're planning on training with a script with Accelerate, use the `_no_trainer.py` version of the script. + +Install Accelerate from source to ensure you have the latest version. + +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +Run the [accelerate config](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-config) command to answer a few questions about your training setup. This creates and saves a config file about your system. + +```bash +accelerate config +``` + +You can use [accelerate test](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-test) to ensure your system is properly configured. + +```bash +accelerate test +``` + +Run [accelerate launch](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-launch) to start training. + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization \ +``` + +## Custom dataset + +The summarization scripts supports custom datasets as long as they are a CSV or JSONL file. When using your own dataset, you need to specify the following additional parameters. + +* `train_file` and `validation_file` specify the path to your training and validation files. +* `text_column` is the input text to summarize. +* `summary_column` is the target text to output. + +An example command for summarizing a custom dataset is shown below. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate \ +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serialization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serialization.md new file mode 100644 index 0000000000000000000000000000000000000000..b62b76d1f0632a86f92c52ce6c1dd4b58adc674c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serialization.md @@ -0,0 +1,99 @@ + + +# ONNX + +[ONNX](http://onnx.ai) is an open standard that defines a common set of operators and a file format to represent deep learning models in different frameworks, including PyTorch and TensorFlow. When a model is exported to ONNX, the operators construct a computational graph (or *intermediate representation*) which represents the flow of data through the model. Standardized operators and data types makes it easy to switch between frameworks. + +The [Optimum](https://huggingface.co/docs/optimum/index) library exports a model to ONNX with configuration objects which are supported for [many architectures](https://huggingface.co/docs/optimum/exporters/onnx/overview) and can be easily extended. If a model isn't supported, feel free to make a [contribution](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/contribute) to Optimum. + +The benefits of exporting to ONNX include the following. + +- [Graph optimization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization) and [quantization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization) for improving inference. +- Use the [`~optimum.onnxruntime.ORTModel`] API to run a model with [ONNX Runtime](https://onnxruntime.ai/). +- Use [optimized inference pipelines](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines) for ONNX models. + +Export a Transformers model to ONNX with the Optimum CLI or the `optimum.onnxruntime` module. + +## Optimum CLI + +Run the command below to install Optimum and the [exporters](https://huggingface.co/docs/optimum/exporters/overview) module. + +```bash +pip install optimum[exporters] +``` + +> [!TIP] +> Refer to the [Export a model to ONNX with optimum.exporters.onnx](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli) guide for all available arguments or with the command below. +> ```bash +> optimum-cli export onnx --help +> ``` + +Set the `--model` argument to export a PyTorch or TensorFlow model from the Hub. + +```bash +optimum-cli export onnx --model distilbert/distilbert-base-uncased-distilled-squad distilbert_base_uncased_squad_onnx/ +``` + +You should see logs indicating the progress and showing where the resulting `model.onnx` is saved. + +```bash +Validating ONNX model distilbert_base_uncased_squad_onnx/model.onnx... + -[โœ“] ONNX model output names match reference model (start_logits, end_logits) + - Validating ONNX Model output "start_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) + - Validating ONNX Model output "end_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) +The ONNX export succeeded and the exported model was saved at: distilbert_base_uncased_squad_onnx +``` + +For local models, make sure the model weights and tokenizer files are saved in the same directory, for example `local_path`. Pass the directory to the `--model` argument and use `--task` to indicate the [task](https://huggingface.co/docs/optimum/exporters/task_manager) a model can perform. If `--task` isn't provided, the model architecture without a task-specific head is used. + +```bash +optimum-cli export onnx --model local_path --task question-answering distilbert_base_uncased_squad_onnx/ +``` + +The `model.onnx` file can be deployed with any [accelerator](https://onnx.ai/supported-tools.html#deployModel) that supports ONNX. The example below demonstrates loading and running a model with ONNX Runtime. + +```python +>>> from transformers import AutoTokenizer +>>> from optimum.onnxruntime import ORTModelForQuestionAnswering + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> model = ORTModelForQuestionAnswering.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> inputs = tokenizer("What am I using?", "Using DistilBERT with ONNX Runtime!", return_tensors="pt") +>>> outputs = model(**inputs) +``` + +## optimum.onnxruntime + +The `optimum.onnxruntime` module supports programmatically exporting a Transformers model. Instantiate a [`~optimum.onnxruntime.ORTModel`] for a task and set `export=True`. Use [`~OptimizedModel.save_pretrained`] to save the ONNX model. + +```python +>>> from optimum.onnxruntime import ORTModelForSequenceClassification +>>> from transformers import AutoTokenizer + +>>> model_checkpoint = "distilbert/distilbert-base-uncased-distilled-squad" +>>> save_directory = "onnx/" + +>>> ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=True) +>>> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) + +>>> ort_model.save_pretrained(save_directory) +>>> tokenizer.save_pretrained(save_directory) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serving.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serving.md new file mode 100644 index 0000000000000000000000000000000000000000..218cb18682e31296f01f625d3a009f41727ca226 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/serving.md @@ -0,0 +1,371 @@ + + +# Serving + +Transformer models can be efficiently deployed using libraries such as vLLM, Text Generation Inference (TGI), and others. These libraries are designed for production-grade user-facing services, and can scale to multiple servers and millions of concurrent users. Refer to [Transformers as Backend for Inference Servers](./transformers_as_backends) for usage examples. + +> [!TIP] +> Responses API is now supported as an experimental API! Read more about it [here](#responses-api). + +Apart from that you can also serve transformer models easily using the `transformers serve` CLI. This is ideal for experimentation purposes, or to run models locally for personal and private use. + +In this document, we dive into the different supported endpoints and modalities; we also cover the setup of several user interfaces that can be used on top of `transformers serve` in the following guides: +- [Jan (text and MCP user interface)](./jan.md) +- [Cursor (IDE)](./cursor.md) +- [Open WebUI (text, image, speech user interface)](./open_webui.md) +- [Tiny-Agents (text and MCP CLI tool)](./tiny_agents.md) + +## Serve CLI + +> [!WARNING] +> This section is experimental and subject to change in future versions + +You can serve models of diverse modalities supported by `transformers` with the `transformers serve` CLI. It spawns a local server that offers compatibility with the OpenAI SDK, which is the _de facto_ standard for LLM conversations and other related tasks. This way, you can use the server from many third party applications, or test it using the `transformers chat` CLI ([docs](conversations.md#chat-cli)). + +The server supports the following REST APIs: +- `/v1/chat/completions` +- `/v1/responses` +- `/v1/audio/transcriptions` +- `/v1/models` + +To launch a server, simply use the `transformers serve` CLI command: + +```shell +transformers serve +``` + +The simplest way to interact with the server is through our `transformers chat` CLI + +```shell +transformers chat localhost:8000 --model-name-or-path Qwen/Qwen3-4B +``` + +or by sending an HTTP request, like we'll see below. + +## Chat Completions - text-based + +See below for examples for text-based requests. Both LLMs and VLMs should handle + + + + +```shell +curl -X POST http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{"messages": [{"role": "system", "content": "hello"}], "temperature": 0.9, "max_tokens": 1000, "stream": true, "model": "Qwen/Qwen2.5-0.5B-Instruct"}' +``` + +from which you'll receive multiple chunks in the Completions API format + +```shell +data: {"object": "chat.completion.chunk", "id": "req_0", "created": 1751377863, "model": "Qwen/Qwen2.5-0.5B-Instruct", "system_fingerprint": "", "choices": [{"delta": {"role": "assistant", "content": "", "tool_call_id": null, "tool_calls": null}, "index": 0, "finish_reason": null, "logprobs": null}]} + +data: {"object": "chat.completion.chunk", "id": "req_0", "created": 1751377863, "model": "Qwen/Qwen2.5-0.5B-Instruct", "system_fingerprint": "", "choices": [{"delta": {"role": "assistant", "content": "", "tool_call_id": null, "tool_calls": null}, "index": 0, "finish_reason": null, "logprobs": null}]} + +(...) +``` + + + + +```python +import asyncio +from huggingface_hub import AsyncInferenceClient + +messages = [{"role": "user", "content": "What is the Transformers library known for?"}] +client = AsyncInferenceClient("http://localhost:8000") + +async def responses_api_test_async(): + async for chunk in (await client.chat_completion(messages, model="Qwen/Qwen2.5-0.5B-Instruct", max_tokens=256, stream=True)): + token = chunk.choices[0].delta.content + if token: + print(token, end='') + +asyncio.run(responses_api_test_async()) +asyncio.run(client.close()) +``` + +From which you should get an iterative string printed: + +```shell +The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...] +``` + + + + +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8000/v1", api_key="") + +completion = client.chat.completions.create( + model="Qwen/Qwen2.5-0.5B-Instruct", + messages=[ + { + "role": "user", + "content": "What is the Transformers library known for?" + } + ], + stream=True +) + +for chunk in completion: + token = chunk.choices[0].delta.content + if token: + print(token, end='') +``` + +From which you should get an iterative string printed: + +```shell +The Transformers library is primarily known for its ability to create and manipulate large-scale language models [...] +``` + + + + +## Chat Completions - VLMs + +The Chat Completion API also supports images; see below for examples for text-and-image-based requests. + + + + +```shell +curl http://localhost:8000/v1/chat/completions \ + -H "Content-Type: application/json" \ + -d '{ + "model": "Qwen/Qwen2.5-VL-7B-Instruct", + "stream": true, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What is in this image?" + }, + { + "type": "image_url", + "image_url": { + "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + } + } + ] + } + ], + "max_tokens": 300 + }' + +``` + +from which you'll receive multiple chunks in the Completions API format + +```shell +data: {"id":"req_0","choices":[{"delta":{"role":"assistant"},"index":0}],"created":1753366665,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""} + +data: {"id":"req_0","choices":[{"delta":{"content":"The "},"index":0}],"created":1753366701,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""} + +data: {"id":"req_0","choices":[{"delta":{"content":"image "},"index":0}],"created":1753366701,"model":"Qwen/Qwen2.5-VL-7B-Instruct@main","object":"chat.completion.chunk","system_fingerprint":""} +``` + + + + +```python +import asyncio +from huggingface_hub import AsyncInferenceClient + +messages = [ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": { + "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg", + } + }, + ], + } +] +client = AsyncInferenceClient("http://localhost:8000") + +async def responses_api_test_async(): + async for chunk in (await client.chat_completion(messages, model="Qwen/Qwen2.5-VL-7B-Instruct", max_tokens=256, stream=True)): + token = chunk.choices[0].delta.content + if token: + print(token, end='') + +asyncio.run(responses_api_test_async()) +asyncio.run(client.close()) +``` + +From which you should get an iterative string printed: + +```xmp +The image depicts an astronaut in a space suit standing on what appears to be the surface of the moon, given the barren, rocky landscape and the dark sky in the background. The astronaut is holding a large egg that has cracked open, revealing a small creature inside. The scene is imaginative and playful, combining elements of space exploration with a whimsical twist involving the egg and the creature. +``` + + + + +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8000/v1", api_key="") + +completion = client.chat.completions.create( + model="Qwen/Qwen2.5-VL-7B-Instruct", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "What's in this image?"}, + { + "type": "image_url", + "image_url": { + "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg", + } + }, + ], + } + ], + stream=True +) + +for chunk in completion: + token = chunk.choices[0].delta.content + if token: + print(token, end='') +``` + +From which you should get an iterative string printed: + +```xmp +The image depicts an astronaut in a space suit standing on what appears to be the surface of the moon, given the barren, rocky landscape and the dark sky in the background. The astronaut is holding a large egg that has cracked open, revealing a small creature inside. The scene is imaginative and playful, combining elements of space exploration with a whimsical twist involving the egg and the creature. +``` + + + + +## Responses API + +The Responses API is the newest addition to the supported APIs of `transformers serve`. + +> [!TIP] +> This API is still experimental: expect bug patches and additition of new features in the coming weeks. +> If you run into any issues, please let us know and we'll work on fixing them ASAP. + +Instead of the previous `/v1/chat/completions` path, the Responses API lies behind the `/v1/responses` path. +See below for examples interacting with our Responses endpoint with `curl`, as well as the Python OpenAI client. + +So far, this endpoint only supports text and therefore only LLMs. VLMs to come! + + + + +```shell +curl http://localhost:8000/v1/responses \ + -H "Content-Type: application/json" \ + -d '{ + "model": "Qwen/Qwen2.5-0.5B-Instruct", + "stream": true, + "input": "Tell me a three sentence bedtime story about a unicorn." + }' +``` + +from which you'll receive multiple chunks in the Responses API format + +```shell +data: {"response":{"id":"resp_req_0","created_at":1754059817.783648,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"response","output":[],"parallel_tool_calls":false,"tool_choice":"auto","tools":[],"status":"queued","text":{"format":{"type":"text"}}},"sequence_number":0,"type":"response.created"} + +data: {"response":{"id":"resp_req_0","created_at":1754059817.783648,"model":"Qwen/Qwen2.5-0.5B-Instruct@main","object":"response","output":[],"parallel_tool_calls":false,"tool_choice":"auto","tools":[],"status":"in_progress","text":{"format":{"type":"text"}}},"sequence_number":1,"type":"response.in_progress"} + +data: {"item":{"id":"msg_req_0","content":[],"role":"assistant","status":"in_progress","type":"message"},"output_index":0,"sequence_number":2,"type":"response.output_item.added"} + +data: {"content_index":0,"item_id":"msg_req_0","output_index":0,"part":{"annotations":[],"text":"","type":"output_text"},"sequence_number":3,"type":"response.content_part.added"} + +data: {"content_index":0,"delta":"","item_id":"msg_req_0","output_index":0,"sequence_number":4,"type":"response.output_text.delta"} + +data: {"content_index":0,"delta":"Once ","item_id":"msg_req_0","output_index":0,"sequence_number":5,"type":"response.output_text.delta"} + +data: {"content_index":0,"delta":"upon ","item_id":"msg_req_0","output_index":0,"sequence_number":6,"type":"response.output_text.delta"} + +data: {"content_index":0,"delta":"a ","item_id":"msg_req_0","output_index":0,"sequence_number":7,"type":"response.output_text.delta"} +``` + + + + +```python +from openai import OpenAI + +client = OpenAI(base_url="http://localhost:8000/v1", api_key="") + +response = client.responses.create( + model="Qwen/Qwen2.5-0.5B-Instruct", + instructions="You are a helpful assistant.", + input="Hello!", + stream=True, + metadata={"foo": "bar"}, +) + +for event in response: + print(event) +``` + +From which you should get events printed out successively. + +```shell +ResponseCreatedEvent(response=Response(id='resp_req_0', created_at=1754060400.3718212, error=None, incomplete_details=None, instructions='You are a helpful assistant.', metadata={'foo': 'bar'}, model='Qwen/Qwen2.5-0.5B-Instruct@main', object='response', output=[], parallel_tool_calls=False, temperature=None, tool_choice='auto', tools=[], top_p=None, background=None, max_output_tokens=None, max_tool_calls=None, previous_response_id=None, prompt=None, reasoning=None, service_tier=None, status='queued', text=ResponseTextConfig(format=ResponseFormatText(type='text')), top_logprobs=None, truncation=None, usage=None, user=None), sequence_number=0, type='response.created') +ResponseInProgressEvent(response=Response(id='resp_req_0', created_at=1754060400.3718212, error=None, incomplete_details=None, instructions='You are a helpful assistant.', metadata={'foo': 'bar'}, model='Qwen/Qwen2.5-0.5B-Instruct@main', object='response', output=[], parallel_tool_calls=False, temperature=None, tool_choice='auto', tools=[], top_p=None, background=None, max_output_tokens=None, max_tool_calls=None, previous_response_id=None, prompt=None, reasoning=None, service_tier=None, status='in_progress', text=ResponseTextConfig(format=ResponseFormatText(type='text')), top_logprobs=None, truncation=None, usage=None, user=None), sequence_number=1, type='response.in_progress') +ResponseOutputItemAddedEvent(item=ResponseOutputMessage(id='msg_req_0', content=[], role='assistant', status='in_progress', type='message'), output_index=0, sequence_number=2, type='response.output_item.added') +ResponseContentPartAddedEvent(content_index=0, item_id='msg_req_0', output_index=0, part=ResponseOutputText(annotations=[], text='', type='output_text', logprobs=None), sequence_number=3, type='response.content_part.added') +ResponseTextDeltaEvent(content_index=0, delta='', item_id='msg_req_0', output_index=0, sequence_number=4, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='', item_id='msg_req_0', output_index=0, sequence_number=5, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='Hello! ', item_id='msg_req_0', output_index=0, sequence_number=6, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='How ', item_id='msg_req_0', output_index=0, sequence_number=7, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='can ', item_id='msg_req_0', output_index=0, sequence_number=8, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='I ', item_id='msg_req_0', output_index=0, sequence_number=9, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='assist ', item_id='msg_req_0', output_index=0, sequence_number=10, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='you ', item_id='msg_req_0', output_index=0, sequence_number=11, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='', item_id='msg_req_0', output_index=0, sequence_number=12, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='', item_id='msg_req_0', output_index=0, sequence_number=13, type='response.output_text.delta') +ResponseTextDeltaEvent(content_index=0, delta='today?', item_id='msg_req_0', output_index=0, sequence_number=14, type='response.output_text.delta') +ResponseTextDoneEvent(content_index=0, item_id='msg_req_0', output_index=0, sequence_number=15, text='Hello! How can I assist you today?', type='response.output_text.done') +ResponseContentPartDoneEvent(content_index=0, item_id='msg_req_0', output_index=0, part=ResponseOutputText(annotations=[], text='Hello! How can I assist you today?', type='output_text', logprobs=None), sequence_number=16, type='response.content_part.done') +ResponseOutputItemDoneEvent(item=ResponseOutputMessage(id='msg_req_0', content=[ResponseOutputText(annotations=[], text='Hello! How can I assist you today?', type='output_text', logprobs=None)], role='assistant', status='completed', type='message', annotations=[]), output_index=0, sequence_number=17, type='response.output_item.done') +ResponseCompletedEvent(response=Response(id='resp_req_0', created_at=1754060400.3718212, error=None, incomplete_details=None, instructions='You are a helpful assistant.', metadata={'foo': 'bar'}, model='Qwen/Qwen2.5-0.5B-Instruct@main', object='response', output=[ResponseOutputMessage(id='msg_req_0', content=[ResponseOutputText(annotations=[], text='Hello! How can I assist you today?', type='output_text', logprobs=None)], role='assistant', status='completed', type='message', annotations=[])], parallel_tool_calls=False, temperature=None, tool_choice='auto', tools=[], top_p=None, background=None, max_output_tokens=None, max_tool_calls=None, previous_response_id=None, prompt=None, reasoning=None, service_tier=None, status='completed', text=ResponseTextConfig(format=ResponseFormatText(type='text')), top_logprobs=None, truncation=None, usage=None, user=None), sequence_number=18, type='response.completed') +``` + + + + + +## MCP integration + +The `transformers serve` server is also an MCP client, so it can interact with MCP tools in agentic use cases. This, of course, requires the use of an LLM that is designed to use tools. + +> [!TIP] +> At the moment, MCP tool usage in `transformers` is limited to the `qwen` family of models. + + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/asr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/asr.md new file mode 100644 index 0000000000000000000000000000000000000000..760f701cf5cc2d5dc29b55e6041d3efa96adb9cd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/asr.md @@ -0,0 +1,371 @@ + + +# Automatic speech recognition + +[[open-in-colab]] + + + +Automatic speech recognition (ASR) converts a speech signal to text, mapping a sequence of audio inputs to text outputs. Virtual assistants like Siri and Alexa use ASR models to help users every day, and there are many other useful user-facing applications like live captioning and note-taking during meetings. + +This guide will show you how to: + +1. Fine-tune [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) on the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset to transcribe audio to text. +2. Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/automatic-speech-recognition) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate jiwer +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load MInDS-14 dataset + +Start by loading a smaller subset of the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset from the ๐Ÿค— Datasets library. This will give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train[:100]") +``` + +Split the dataset's `train` split into a train and test set with the [`~Dataset.train_test_split`] method: + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +Then take a look at the dataset: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 16 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 4 + }) +}) +``` + +While the dataset contains a lot of useful information, like `lang_id` and `english_transcription`, this guide focuses on the `audio` and `transcription`. Remove the other columns with the [`~datasets.Dataset.remove_columns`] method: + +```py +>>> minds = minds.remove_columns(["english_transcription", "intent_class", "lang_id"]) +``` + +Review the example again: + +```py +>>> minds["train"][0] +{'audio': {'array': array([-0.00024414, 0. , 0. , ..., 0.00024414, + 0.00024414, 0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 8000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +There are two fields: + +- `audio`: a 1-dimensional `array` of the speech signal that must be called to load and resample the audio file. +- `transcription`: the target text. + +## Preprocess + +The next step is to load a Wav2Vec2 processor to process the audio signal: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base") +``` + +The MInDS-14 dataset has a sampling rate of 8000Hz (you can find this information in its [dataset card](https://huggingface.co/datasets/PolyAI/minds14)), which means you'll need to resample the dataset to 16000Hz to use the pretrained Wav2Vec2 model: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([-2.38064706e-04, -1.58618059e-04, -5.43987835e-06, ..., + 2.78103951e-04, 2.38446111e-04, 1.18740834e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 16000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +As you can see in the `transcription` above, the text contains a mix of uppercase and lowercase characters. The Wav2Vec2 tokenizer is only trained on uppercase characters so you'll need to make sure the text matches the tokenizer's vocabulary: + +```py +>>> def uppercase(example): +... return {"transcription": example["transcription"].upper()} + + +>>> minds = minds.map(uppercase) +``` + +Now create a preprocessing function that: + +1. Calls the `audio` column to load and resample the audio file. +2. Extracts the `input_values` from the audio file and tokenize the `transcription` column with the processor. + +```py +>>> def prepare_dataset(batch): +... audio = batch["audio"] +... batch = processor(audio["array"], sampling_rate=audio["sampling_rate"], text=batch["transcription"]) +... batch["input_length"] = len(batch["input_values"][0]) +... return batch +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up `map` by increasing the number of processes with the `num_proc` parameter. Remove the columns you don't need with the [`~datasets.Dataset.remove_columns`] method: + +```py +>>> encoded_minds = minds.map(prepare_dataset, remove_columns=minds.column_names["train"], num_proc=4) +``` + +๐Ÿค— Transformers doesn't have a data collator for ASR, so you'll need to adapt the [`DataCollatorWithPadding`] to create a batch of examples. It'll also dynamically pad your text and labels to the length of the longest element in its batch (instead of the entire dataset) so they are a uniform length. While it is possible to pad your text in the `tokenizer` function by setting `padding=True`, dynamic padding is more efficient. + +Unlike other data collators, this specific data collator needs to apply a different padding method to `input_values` and `labels`: + +```py +>>> import torch + +>>> from dataclasses import dataclass, field +>>> from typing import Any, Dict, List, Optional, Union + + +>>> @dataclass +... class DataCollatorCTCWithPadding: +... processor: AutoProcessor +... padding: Union[bool, str] = "longest" + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... # split inputs and labels since they have to be of different lengths and need +... # different padding methods +... input_features = [{"input_values": feature["input_values"][0]} for feature in features] +... label_features = [{"input_ids": feature["labels"]} for feature in features] + +... batch = self.processor.pad(input_features, padding=self.padding, return_tensors="pt") + +... labels_batch = self.processor.pad(labels=label_features, padding=self.padding, return_tensors="pt") + +... # replace padding with -100 to ignore loss correctly +... labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) + +... batch["labels"] = labels + +... return batch +``` + +Now instantiate your `DataCollatorForCTCWithPadding`: + +```py +>>> data_collator = DataCollatorCTCWithPadding(processor=processor, padding="longest") +``` + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load an evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [word error rate](https://huggingface.co/spaces/evaluate-metric/wer) (WER) metric (refer to the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about loading and computing metrics): + +```py +>>> import evaluate + +>>> wer = evaluate.load("wer") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the WER: + +```py +>>> import numpy as np + + +>>> def compute_metrics(pred): +... pred_logits = pred.predictions +... pred_ids = np.argmax(pred_logits, axis=-1) + +... pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id + +... pred_str = processor.batch_decode(pred_ids) +... label_str = processor.batch_decode(pred.label_ids, group_tokens=False) + +... wer = wer.compute(predictions=pred_str, references=label_str) + +... return {"wer": wer} +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You are now ready to start training your model! Load Wav2Vec2 with [`AutoModelForCTC`]. Specify the reduction to apply with the `ctc_loss_reduction` parameter. It is often better to use the average instead of the default summation: + +```py +>>> from transformers import AutoModelForCTC, TrainingArguments, Trainer + +>>> model = AutoModelForCTC.from_pretrained( +... "facebook/wav2vec2-base", +... ctc_loss_reduction="mean", +... pad_token_id=processor.tokenizer.pad_token_id, +... ) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the WER and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to fine-tune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_asr_mind_model", +... per_device_train_batch_size=8, +... gradient_accumulation_steps=2, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=2000, +... gradient_checkpointing=True, +... fp16=True, +... group_by_length=True, +... eval_strategy="steps", +... per_device_eval_batch_size=8, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... load_best_model_at_end=True, +... metric_for_best_model="wer", +... greater_is_better=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=processor, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so it can be accessible to everyone: + +```py +>>> trainer.push_to_hub() +``` + + + + + +For a more in-depth example of how to fine-tune a model for automatic speech recognition, take a look at this blog [post](https://huggingface.co/blog/fine-tune-wav2vec2-english) for English ASR and this [post](https://huggingface.co/blog/fine-tune-xlsr-wav2vec2) for multilingual ASR. + + + +## Inference + +Great, now that you've fine-tuned a model, you can use it for inference! + +Load an audio file you'd like to run inference on. Remember to resample the sampling rate of the audio file to match the sampling rate of the model if you need to! + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +The simplest way to try out your fine-tuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for automatic speech recognition with your model, and pass your audio file to it: + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") +>>> transcriber(audio_file) +{'text': 'I WOUD LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'} +``` + + + +The transcription is decent, but it could be better! Try finetuning your model on more examples to get even better results! + + + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Load a processor to preprocess the audio file and transcription and return the `input` as PyTorch tensors: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +Pass your inputs to the model and return the logits: + +```py +>>> from transformers import AutoModelForCTC + +>>> model = AutoModelForCTC.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Get the predicted `input_ids` with the highest probability, and use the processor to decode the predicted `input_ids` back into text: + +```py +>>> import torch + +>>> predicted_ids = torch.argmax(logits, dim=-1) +>>> transcription = processor.batch_decode(predicted_ids) +>>> transcription +['I WOUL LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/audio_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/audio_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..973f95e1e9555dd77e3a5a6b48a5131598dfa2df --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/audio_classification.md @@ -0,0 +1,324 @@ + + +# Audio classification + +[[open-in-colab]] + + + +Audio classification - just like with text - assigns a class label as output from the input data. The only difference is instead of text inputs, you have raw audio waveforms. Some practical applications of audio classification include identifying speaker intent, language classification, and even animal species by their sounds. + +This guide will show you how to: + +1. Fine-tune [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) on the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset to classify speaker intent. +2. Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/audio-classification) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load MInDS-14 dataset + +Start by loading the MInDS-14 dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +Split the dataset's `train` split into a smaller train and test set with the [`~datasets.Dataset.train_test_split`] method. This will give you a chance to experiment and make sure everything works before spending more time on the full dataset. + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +Then take a look at the dataset: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 450 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 113 + }) +}) +``` + +While the dataset contains a lot of useful information, like `lang_id` and `english_transcription`, you will focus on the `audio` and `intent_class` in this guide. Remove the other columns with the [`~datasets.Dataset.remove_columns`] method: + +```py +>>> minds = minds.remove_columns(["path", "transcription", "english_transcription", "lang_id"]) +``` + +Here's an example: + +```py +>>> minds["train"][0] +{'audio': {'array': array([ 0. , 0. , 0. , ..., -0.00048828, + -0.00024414, -0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 8000}, + 'intent_class': 2} +``` + +There are two fields: + +- `audio`: a 1-dimensional `array` of the speech signal that must be called to load and resample the audio file. +- `intent_class`: represents the class id of the speaker's intent. + +To make it easier for the model to get the label name from the label id, create a dictionary that maps the label name to an integer and vice versa: + +```py +>>> labels = minds["train"].features["intent_class"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +Now you can convert the label id to a label name: + +```py +>>> id2label[str(2)] +'app_error' +``` + +## Preprocess + +The next step is to load a Wav2Vec2 feature extractor to process the audio signal: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +The MInDS-14 dataset has a sampling rate of 8kHz (you can find this information in its [dataset card](https://huggingface.co/datasets/PolyAI/minds14)), which means you'll need to resample the dataset to 16kHz to use the pretrained Wav2Vec2 model: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([ 2.2098757e-05, 4.6582241e-05, -2.2803260e-05, ..., + -2.8419291e-04, -2.3305941e-04, -1.1425107e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 16000}, + 'intent_class': 2} +``` + +Now create a preprocessing function that: + +1. Calls the `audio` column to load, and if necessary, resample the audio file. +2. Checks if the sampling rate of the audio file matches the sampling rate of the audio data a model was pretrained with. You can find this information in the Wav2Vec2 [model card](https://huggingface.co/facebook/wav2vec2-base). +3. Set a maximum input length to batch longer inputs without truncating them. + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, sampling_rate=feature_extractor.sampling_rate, max_length=16000, truncation=True +... ) +... return inputs +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up `map` by setting `batched=True` to process multiple elements of the dataset at once. Remove unnecessary columns and rename `intent_class` to `label`, as required by the model: + +```py +>>> encoded_minds = minds.map(preprocess_function, remove_columns="audio", batched=True) +>>> encoded_minds = encoded_minds.rename_column("intent_class", "label") +``` + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load an evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the accuracy: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions = np.argmax(eval_pred.predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load Wav2Vec2 with [`AutoModelForAudioClassification`] along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import AutoModelForAudioClassification, TrainingArguments, Trainer + +>>> num_labels = len(id2label) +>>> model = AutoModelForAudioClassification.from_pretrained( +... "facebook/wav2vec2-base", num_labels=num_labels, label2id=label2id, id2label=id2label +... ) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir`, which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the accuracy and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to fine-tune your model. + + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_mind_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=3e-5, +... per_device_train_batch_size=32, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=32, +... num_train_epochs=10, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=feature_extractor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + + +For a more in-depth example of how to fine-tune a model for audio classification, take a look at the corresponding [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb). + + + +## Inference + +Great, now that you've fine-tuned a model, you can use it for inference! + +Load an audio file for inference. Remember to resample the sampling rate of the audio file to match the model's sampling rate, if necessary. + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +The simplest way to try out your fine-tuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for audio classification with your model, and pass your audio file to it: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("audio-classification", model="stevhliu/my_awesome_minds_model") +>>> classifier(audio_file) +[ + {'score': 0.09766869246959686, 'label': 'cash_deposit'}, + {'score': 0.07998877018690109, 'label': 'app_error'}, + {'score': 0.0781070664525032, 'label': 'joint_account'}, + {'score': 0.07667109370231628, 'label': 'pay_bill'}, + {'score': 0.0755252093076706, 'label': 'balance'} +] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Load a feature extractor to preprocess the audio file and return the `input` as PyTorch tensors: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("stevhliu/my_awesome_minds_model") +>>> inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +Pass your inputs to the model and return the logits: + +```py +>>> from transformers import AutoModelForAudioClassification + +>>> model = AutoModelForAudioClassification.from_pretrained("stevhliu/my_awesome_minds_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Get the class with the highest probability, and use the model's `id2label` mapping to convert it to a label: + +```py +>>> import torch + +>>> predicted_class_ids = torch.argmax(logits).item() +>>> predicted_label = model.config.id2label[predicted_class_ids] +>>> predicted_label +'cash_deposit' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/document_question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/document_question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..d83e025c409019330f762b551f17e944a50de23b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/document_question_answering.md @@ -0,0 +1,492 @@ + + +# Document Question Answering + +[[open-in-colab]] + +Document Question Answering, also referred to as Document Visual Question Answering, is a task that involves providing +answers to questions posed about document images. The input to models supporting this task is typically a combination of an image and +a question, and the output is an answer expressed in natural language. These models utilize multiple modalities, including +text, the positions of words (bounding boxes), and the image itself. + +This guide illustrates how to: + +- Fine-tune [LayoutLMv2](../model_doc/layoutlmv2) on the [DocVQA dataset](https://huggingface.co/datasets/nielsr/docvqa_1200_examples_donut). +- Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/image-to-text) + + + +LayoutLMv2 solves the document question-answering task by adding a question-answering head on top of the final hidden +states of the tokens, to predict the positions of the start and end tokens of the +answer. In other words, the problem is treated as extractive question answering: given the context, extract which piece +of information answers the question. The context comes from the output of an OCR engine, here it is Google's Tesseract. + +Before you begin, make sure you have all the necessary libraries installed. LayoutLMv2 depends on detectron2, torchvision and tesseract. + +```bash +pip install -q transformers datasets +``` + +```bash +pip install 'git+https://github.com/facebookresearch/detectron2.git' +pip install torchvision +``` + +```bash +sudo apt install tesseract-ocr +pip install -q pytesseract +``` + +Once you have installed all of the dependencies, restart your runtime. + +We encourage you to share your model with the community. Log in to your Hugging Face account to upload it to the ๐Ÿค— Hub. +When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +Let's define some global variables. + +```py +>>> model_checkpoint = "microsoft/layoutlmv2-base-uncased" +>>> batch_size = 4 +``` + +## Load the data + +In this guide we use a small sample of preprocessed DocVQA that you can find on ๐Ÿค— Hub. If you'd like to use the full +DocVQA dataset, you can register and download it on [DocVQA homepage](https://rrc.cvc.uab.es/?ch=17). If you do so, to +proceed with this guide check out [how to load files into a ๐Ÿค— dataset](https://huggingface.co/docs/datasets/loading#local-and-remote-files). + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("nielsr/docvqa_1200_examples") +>>> dataset +DatasetDict({ + train: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 1000 + }) + test: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 200 + }) +}) +``` + +As you can see, the dataset is split into train and test sets already. Take a look at a random example to familiarize +yourself with the features. + +```py +>>> dataset["train"].features +``` + +Here's what the individual fields represent: +* `id`: the example's id +* `image`: a PIL.Image.Image object containing the document image +* `query`: the question string - natural language asked question, in several languages +* `answers`: a list of correct answers provided by human annotators +* `words` and `bounding_boxes`: the results of OCR, which we will not use here +* `answer`: an answer matched by a different model which we will not use here + +Let's leave only English questions, and drop the `answer` feature which appears to contain predictions by another model. +We'll also take the first of the answers from the set provided by the annotators. Alternatively, you can randomly sample it. + +```py +>>> updated_dataset = dataset.map(lambda example: {"question": example["query"]["en"]}, remove_columns=["query"]) +>>> updated_dataset = updated_dataset.map( +... lambda example: {"answer": example["answers"][0]}, remove_columns=["answer", "answers"] +... ) +``` + +Note that the LayoutLMv2 checkpoint that we use in this guide has been trained with `max_position_embeddings = 512` (you can +find this information in the [checkpoint's `config.json` file](https://huggingface.co/microsoft/layoutlmv2-base-uncased/blob/main/config.json#L18)). +We can truncate the examples but to avoid the situation where the answer might be at the end of a large document and end up truncated, +here we'll remove the few examples where the embedding is likely to end up longer than 512. +If most of the documents in your dataset are long, you can implement a sliding window strategy - check out [this notebook](https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb) for details. + +```py +>>> updated_dataset = updated_dataset.filter(lambda x: len(x["words"]) + len(x["question"].split()) < 512) +``` + +At this point let's also remove the OCR features from this dataset. These are a result of OCR for fine-tuning a different +model. They would still require some processing if we wanted to use them, as they do not match the input requirements +of the model we use in this guide. Instead, we can use the [`LayoutLMv2Processor`] on the original data for both OCR and +tokenization. This way we'll get the inputs that match model's expected input. If you want to process images manually, +check out the [`LayoutLMv2` model documentation](../model_doc/layoutlmv2) to learn what input format the model expects. + +```py +>>> updated_dataset = updated_dataset.remove_columns("words") +>>> updated_dataset = updated_dataset.remove_columns("bounding_boxes") +``` + +Finally, the data exploration won't be complete if we don't peek at an image example. + +```py +>>> updated_dataset["train"][11]["image"] +``` + +
+ DocVQA Image Example +
+ +## Preprocess the data + +The Document Question Answering task is a multimodal task, and you need to make sure that the inputs from each modality +are preprocessed according to the model's expectations. Let's start by loading the [`LayoutLMv2Processor`], which internally combines an image processor that can handle image data and a tokenizer that can encode text data. + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(model_checkpoint) +``` + +### Preprocessing document images + +First, let's prepare the document images for the model with the help of the `image_processor` from the processor. +By default, image processor resizes the images to 224x224, makes sure they have the correct order of color channels, +applies OCR with tesseract to get words and normalized bounding boxes. In this tutorial, all of these defaults are exactly what we need. +Write a function that applies the default image processing to a batch of images and returns the results of OCR. + +```py +>>> image_processor = processor.image_processor + + +>>> def get_ocr_words_and_boxes(examples): +... images = [image.convert("RGB") for image in examples["image"]] +... encoded_inputs = image_processor(images) + +... examples["image"] = encoded_inputs.pixel_values +... examples["words"] = encoded_inputs.words +... examples["boxes"] = encoded_inputs.boxes + +... return examples +``` + +To apply this preprocessing to the entire dataset in a fast way, use [`~datasets.Dataset.map`]. + +```py +>>> dataset_with_ocr = updated_dataset.map(get_ocr_words_and_boxes, batched=True, batch_size=2) +``` + +### Preprocessing text data + +Once we have applied OCR to the images, we need to encode the text part of the dataset to prepare it for the model. +This involves converting the words and boxes that we got in the previous step to token-level `input_ids`, `attention_mask`, +`token_type_ids` and `bbox`. For preprocessing text, we'll need the `tokenizer` from the processor. + +```py +>>> tokenizer = processor.tokenizer +``` + +On top of the preprocessing mentioned above, we also need to add the labels for the model. For `xxxForQuestionAnswering` models +in ๐Ÿค— Transformers, the labels consist of the `start_positions` and `end_positions`, indicating which token is at the +start and which token is at the end of the answer. + +Let's start with that. Define a helper function that can find a sublist (the answer split into words) in a larger list (the words list). + +This function will take two lists as input, `words_list` and `answer_list`. It will then iterate over the `words_list` and check +if the current word in the `words_list` (words_list[i]) is equal to the first word of answer_list (answer_list[0]) and if +the sublist of `words_list` starting from the current word and of the same length as `answer_list` is equal `to answer_list`. +If this condition is true, it means that a match has been found, and the function will record the match, its starting index (idx), +and its ending index (idx + len(answer_list) - 1). If more than one match was found, the function will return only the first one. +If no match is found, the function returns (`None`, 0, and 0). + +```py +>>> def subfinder(words_list, answer_list): +... matches = [] +... start_indices = [] +... end_indices = [] +... for idx, i in enumerate(range(len(words_list))): +... if words_list[i] == answer_list[0] and words_list[i : i + len(answer_list)] == answer_list: +... matches.append(answer_list) +... start_indices.append(idx) +... end_indices.append(idx + len(answer_list) - 1) +... if matches: +... return matches[0], start_indices[0], end_indices[0] +... else: +... return None, 0, 0 +``` + +To illustrate how this function finds the position of the answer, let's use it on an example: + +```py +>>> example = dataset_with_ocr["train"][1] +>>> words = [word.lower() for word in example["words"]] +>>> match, word_idx_start, word_idx_end = subfinder(words, example["answer"].lower().split()) +>>> print("Question: ", example["question"]) +>>> print("Words:", words) +>>> print("Answer: ", example["answer"]) +>>> print("start_index", word_idx_start) +>>> print("end_index", word_idx_end) +Question: Who is in cc in this letter? +Words: ['wie', 'baw', 'brown', '&', 'williamson', 'tobacco', 'corporation', 'research', '&', 'development', 'internal', 'correspondence', 'to:', 'r.', 'h.', 'honeycutt', 'ce:', 't.f.', 'riehl', 'from:', '.', 'c.j.', 'cook', 'date:', 'may', '8,', '1995', 'subject:', 'review', 'of', 'existing', 'brainstorming', 'ideas/483', 'the', 'major', 'function', 'of', 'the', 'product', 'innovation', 'graup', 'is', 'to', 'develop', 'marketable', 'nove!', 'products', 'that', 'would', 'be', 'profitable', 'to', 'manufacture', 'and', 'sell.', 'novel', 'is', 'defined', 'as:', 'of', 'a', 'new', 'kind,', 'or', 'different', 'from', 'anything', 'seen', 'or', 'known', 'before.', 'innovation', 'is', 'defined', 'as:', 'something', 'new', 'or', 'different', 'introduced;', 'act', 'of', 'innovating;', 'introduction', 'of', 'new', 'things', 'or', 'methods.', 'the', 'products', 'may', 'incorporate', 'the', 'latest', 'technologies,', 'materials', 'and', 'know-how', 'available', 'to', 'give', 'then', 'a', 'unique', 'taste', 'or', 'look.', 'the', 'first', 'task', 'of', 'the', 'product', 'innovation', 'group', 'was', 'to', 'assemble,', 'review', 'and', 'categorize', 'a', 'list', 'of', 'existing', 'brainstorming', 'ideas.', 'ideas', 'were', 'grouped', 'into', 'two', 'major', 'categories', 'labeled', 'appearance', 'and', 'taste/aroma.', 'these', 'categories', 'are', 'used', 'for', 'novel', 'products', 'that', 'may', 'differ', 'from', 'a', 'visual', 'and/or', 'taste/aroma', 'point', 'of', 'view', 'compared', 'to', 'canventional', 'cigarettes.', 'other', 'categories', 'include', 'a', 'combination', 'of', 'the', 'above,', 'filters,', 'packaging', 'and', 'brand', 'extensions.', 'appearance', 'this', 'category', 'is', 'used', 'for', 'novel', 'cigarette', 'constructions', 'that', 'yield', 'visually', 'different', 'products', 'with', 'minimal', 'changes', 'in', 'smoke', 'chemistry', 'two', 'cigarettes', 'in', 'cne.', 'emulti-plug', 'te', 'build', 'yaur', 'awn', 'cigarette.', 'eswitchable', 'menthol', 'or', 'non', 'menthol', 'cigarette.', '*cigarettes', 'with', 'interspaced', 'perforations', 'to', 'enable', 'smoker', 'to', 'separate', 'unburned', 'section', 'for', 'future', 'smoking.', 'ยซshort', 'cigarette,', 'tobacco', 'section', '30', 'mm.', 'ยซextremely', 'fast', 'buming', 'cigarette.', 'ยซnovel', 'cigarette', 'constructions', 'that', 'permit', 'a', 'significant', 'reduction', 'iretobacco', 'weight', 'while', 'maintaining', 'smoking', 'mechanics', 'and', 'visual', 'characteristics.', 'higher', 'basis', 'weight', 'paper:', 'potential', 'reduction', 'in', 'tobacco', 'weight.', 'ยซmore', 'rigid', 'tobacco', 'column;', 'stiffing', 'agent', 'for', 'tobacco;', 'e.g.', 'starch', '*colored', 'tow', 'and', 'cigarette', 'papers;', 'seasonal', 'promotions,', 'e.g.', 'pastel', 'colored', 'cigarettes', 'for', 'easter', 'or', 'in', 'an', 'ebony', 'and', 'ivory', 'brand', 'containing', 'a', 'mixture', 'of', 'all', 'black', '(black', 'paper', 'and', 'tow)', 'and', 'ail', 'white', 'cigarettes.', '499150498'] +Answer: T.F. Riehl +start_index 17 +end_index 18 +``` + +Once examples are encoded, however, they will look like this: + +```py +>>> encoding = tokenizer(example["question"], example["words"], example["boxes"]) +>>> tokenizer.decode(encoding["input_ids"]) +[CLS] who is in cc in this letter? [SEP] wie baw brown & williamson tobacco corporation research & development ... +``` + +We'll need to find the position of the answer in the encoded input. +* `token_type_ids` tells us which tokens are part of the question, and which ones are part of the document's words. +* `tokenizer.cls_token_id` will help find the special token at the beginning of the input. +* `word_ids` will help match the answer found in the original `words` to the same answer in the full encoded input and determine +the start/end position of the answer in the encoded input. + +With that in mind, let's create a function to encode a batch of examples in the dataset: + +```py +>>> def encode_dataset(examples, max_length=512): +... questions = examples["question"] +... words = examples["words"] +... boxes = examples["boxes"] +... answers = examples["answer"] + +... # encode the batch of examples and initialize the start_positions and end_positions +... encoding = tokenizer(questions, words, boxes, max_length=max_length, padding="max_length", truncation=True) +... start_positions = [] +... end_positions = [] + +... # loop through the examples in the batch +... for i in range(len(questions)): +... cls_index = encoding["input_ids"][i].index(tokenizer.cls_token_id) + +... # find the position of the answer in example's words +... words_example = [word.lower() for word in words[i]] +... answer = answers[i] +... match, word_idx_start, word_idx_end = subfinder(words_example, answer.lower().split()) + +... if match: +... # if match is found, use `token_type_ids` to find where words start in the encoding +... token_type_ids = encoding["token_type_ids"][i] +... token_start_index = 0 +... while token_type_ids[token_start_index] != 1: +... token_start_index += 1 + +... token_end_index = len(encoding["input_ids"][i]) - 1 +... while token_type_ids[token_end_index] != 1: +... token_end_index -= 1 + +... word_ids = encoding.word_ids(i)[token_start_index : token_end_index + 1] +... start_position = cls_index +... end_position = cls_index + +... # loop over word_ids and increase `token_start_index` until it matches the answer position in words +... # once it matches, save the `token_start_index` as the `start_position` of the answer in the encoding +... for id in word_ids: +... if id == word_idx_start: +... start_position = token_start_index +... else: +... token_start_index += 1 + +... # similarly loop over `word_ids` starting from the end to find the `end_position` of the answer +... for id in word_ids[::-1]: +... if id == word_idx_end: +... end_position = token_end_index +... else: +... token_end_index -= 1 + +... start_positions.append(start_position) +... end_positions.append(end_position) + +... else: +... start_positions.append(cls_index) +... end_positions.append(cls_index) + +... encoding["image"] = examples["image"] +... encoding["start_positions"] = start_positions +... encoding["end_positions"] = end_positions + +... return encoding +``` + +Now that we have this preprocessing function, we can encode the entire dataset: + +```py +>>> encoded_train_dataset = dataset_with_ocr["train"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["train"].column_names +... ) +>>> encoded_test_dataset = dataset_with_ocr["test"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["test"].column_names +... ) +``` + +Let's check what the features of the encoded dataset look like: + +```py +>>> encoded_train_dataset.features +{'image': Sequence(feature=Sequence(feature=Sequence(feature=Value(dtype='uint8', id=None), length=-1, id=None), length=-1, id=None), length=-1, id=None), + 'input_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), + 'token_type_ids': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'attention_mask': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'bbox': Sequence(feature=Sequence(feature=Value(dtype='int64', id=None), length=-1, id=None), length=-1, id=None), + 'start_positions': Value(dtype='int64', id=None), + 'end_positions': Value(dtype='int64', id=None)} +``` + +## Evaluation + +Evaluation for document question answering requires a significant amount of postprocessing. To avoid taking up too much +of your time, this guide skips the evaluation step. The [`Trainer`] still calculates the evaluation loss during training so +you're not completely in the dark about your model's performance. Extractive question answering is typically evaluated using F1/exact match. +If you'd like to implement it yourself, check out the [Question Answering chapter](https://huggingface.co/course/chapter7/7?fw=pt#postprocessing) +of the Hugging Face course for inspiration. + +## Train + +Congratulations! You've successfully navigated the toughest part of this guide and now you are ready to train your own model. +Training involves the following steps: +* Load the model with [`AutoModelForDocumentQuestionAnswering`] using the same checkpoint as in the preprocessing. +* Define your training hyperparameters in [`TrainingArguments`]. +* Define a function to batch examples together, here the [`DefaultDataCollator`] will do just fine +* Pass the training arguments to [`Trainer`] along with the model, dataset, and data collator. +* Call [`~Trainer.train`] to finetune your model. + +```py +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained(model_checkpoint) +``` + +In the [`TrainingArguments`] use `output_dir` to specify where to save your model, and configure hyperparameters as you see fit. +If you wish to share your model with the community, set `push_to_hub` to `True` (you must be signed in to Hugging Face to upload your model). +In this case the `output_dir` will also be the name of the repo where your model checkpoint will be pushed. + +```py +>>> from transformers import TrainingArguments + +>>> # REPLACE THIS WITH YOUR REPO ID +>>> repo_id = "MariaK/layoutlmv2-base-uncased_finetuned_docvqa" + +>>> training_args = TrainingArguments( +... output_dir=repo_id, +... per_device_train_batch_size=4, +... num_train_epochs=20, +... save_steps=200, +... logging_steps=50, +... eval_strategy="steps", +... learning_rate=5e-5, +... save_total_limit=2, +... remove_unused_columns=False, +... push_to_hub=True, +... ) +``` + +Define a simple data collator to batch examples together. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + +Finally, bring everything together, and call [`~Trainer.train`]: + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=encoded_train_dataset, +... eval_dataset=encoded_test_dataset, +... processing_class=processor, +... ) + +>>> trainer.train() +``` + +To add the final model to ๐Ÿค— Hub, create a model card and call `push_to_hub`: + +```py +>>> trainer.create_model_card() +>>> trainer.push_to_hub() +``` + +## Inference + +Now that you have finetuned a LayoutLMv2 model, and uploaded it to the ๐Ÿค— Hub, you can use it for inference. The simplest +way to try out your finetuned model for inference is to use it in a [`Pipeline`]. + +Let's take an example: +```py +>>> example = dataset["test"][2] +>>> question = example["query"]["en"] +>>> image = example["image"] +>>> print(question) +>>> print(example["answers"]) +'Who is โ€˜presidingโ€™ TRRF GENERAL SESSION (PART 1)?' +['TRRF Vice President', 'lee a. waller'] +``` + +Next, instantiate a pipeline for +document question answering with your model, and pass the image + question combination to it. + +```py +>>> from transformers import pipeline + +>>> qa_pipeline = pipeline("document-question-answering", model="MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> qa_pipeline(image, question) +[{'score': 0.9949808120727539, + 'answer': 'Lee A. Waller', + 'start': 55, + 'end': 57}] +``` + +You can also manually replicate the results of the pipeline if you'd like: +1. Take an image and a question, prepare them for the model using the processor from your model. +2. Forward the result or preprocessing through the model. +3. The model returns `start_logits` and `end_logits`, which indicate which token is at the start of the answer and +which token is at the end of the answer. Both have shape (batch_size, sequence_length). +4. Take an argmax on the last dimension of both the `start_logits` and `end_logits` to get the predicted `start_idx` and `end_idx`. +5. Decode the answer with the tokenizer. + +```py +>>> import torch +>>> from transformers import AutoProcessor +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> processor = AutoProcessor.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") + +>>> with torch.no_grad(): +... encoding = processor(image.convert("RGB"), question, return_tensors="pt") +... outputs = model(**encoding) +... start_logits = outputs.start_logits +... end_logits = outputs.end_logits +... predicted_start_idx = start_logits.argmax(-1).item() +... predicted_end_idx = end_logits.argmax(-1).item() + +>>> processor.tokenizer.decode(encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + 1]) +'lee a. waller' +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/idefics.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/idefics.md new file mode 100644 index 0000000000000000000000000000000000000000..81ab60fecd0cb87beaccaa1362f39855c4c4892f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/idefics.md @@ -0,0 +1,423 @@ + + +# Image tasks with IDEFICS + +[[open-in-colab]] + +While individual tasks can be tackled by fine-tuning specialized models, an alternative approach +that has recently emerged and gained popularity is to use large models for a diverse set of tasks without fine-tuning. +For instance, large language models can handle such NLP tasks as summarization, translation, classification, and more. +This approach is no longer limited to a single modality, such as text, and in this guide, we will illustrate how you can +solve image-text tasks with a large multimodal model called IDEFICS. + +[IDEFICS](../model_doc/idefics) is an open-access vision and language model based on [Flamingo](https://huggingface.co/papers/2204.14198), +a state-of-the-art visual language model initially developed by DeepMind. The model accepts arbitrary sequences of image +and text inputs and generates coherent text as output. It can answer questions about images, describe visual content, +create stories grounded in multiple images, and so on. IDEFICS comes in two variants - [80 billion parameters](https://huggingface.co/HuggingFaceM4/idefics-80b) +and [9 billion parameters](https://huggingface.co/HuggingFaceM4/idefics-9b), both of which are available on the ๐Ÿค— Hub. For each variant, you can also find fine-tuned instructed +versions of the model adapted for conversational use cases. + +This model is exceptionally versatile and can be used for a wide range of image and multimodal tasks. However, +being a large model means it requires significant computational resources and infrastructure. It is up to you to decide whether +this approach suits your use case better than fine-tuning specialized models for each individual task. + +In this guide, you'll learn how to: +- [Load IDEFICS](#loading-the-model) and [load the quantized version of the model](#quantized-model) +- Use IDEFICS for: + - [Image captioning](#image-captioning) + - [Prompted image captioning](#prompted-image-captioning) + - [Few-shot prompting](#few-shot-prompting) + - [Visual question answering](#visual-question-answering) + - [Image classification](#image-classification) + - [Image-guided text generation](#image-guided-text-generation) +- [Run inference in batch mode](#running-inference-in-batch-mode) +- [Run IDEFICS instruct for conversational use](#idefics-instruct-for-conversational-use) + +Before you begin, make sure you have all the necessary libraries installed. + +```bash +pip install -q bitsandbytes sentencepiece accelerate transformers +``` + + +To run the following examples with a non-quantized version of the model checkpoint you will need at least 20GB of GPU memory. + + +## Loading the model + +Let's start by loading the model's 9 billion parameters checkpoint: + +```py +>>> checkpoint = "HuggingFaceM4/idefics-9b" +``` + +Just like for other Transformers models, you need to load a processor and the model itself from the checkpoint. +The IDEFICS processor wraps a [`LlamaTokenizer`] and IDEFICS image processor into a single processor to take care of +preparing text and image inputs for the model. + +```py +>>> import torch + +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +``` + +Setting `device_map` to `"auto"` will automatically determine how to load and store the model weights in the most optimized +manner given existing devices. + +### Quantized model + +If high-memory device availability is an issue, you can load the quantized version of the model. To load the model and the +processor in 4bit precision, pass a `BitsAndBytesConfig` to the `from_pretrained` method and the model will be compressed +on the fly while loading. + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor, BitsAndBytesConfig + +>>> quantization_config = BitsAndBytesConfig( +... load_in_4bit=True, +... bnb_4bit_compute_dtype=torch.float16, +... ) + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained( +... checkpoint, +... quantization_config=quantization_config, +... device_map="auto" +... ) +``` + +Now that you have the model loaded in one of the suggested ways, let's move on to exploring tasks that you can use IDEFICS for. + +## Image captioning +Image captioning is the task of predicting a caption for a given image. A common application is to aid visually impaired +people navigate through different situations, for instance, explore image content online. + +To illustrate the task, get an image to be captioned, e.g.: + +
+ Image of a puppy in a flower bed +
+ +Photo by [Hendo Wang](https://unsplash.com/@hendoo). + +IDEFICS accepts text and image prompts. However, to caption an image, you do not have to provide a text prompt to the +model, only the preprocessed input image. Without a text prompt, the model will start generating text from the +BOS (beginning-of-sequence) token thus creating a caption. + +As image input to the model, you can use either an image object (`PIL.Image`) or a url from which the image can be retrieved. + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1583160247711-2191776b4b91?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3542&q=80", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +A puppy in a flower bed +``` + + + +It is a good idea to include the `bad_words_ids` in the call to `generate` to avoid errors arising when increasing +the `max_new_tokens`: the model will want to generate a new `` or `` token when there +is no image being generated by the model. +You can set it on-the-fly as in this guide, or store in the `GenerationConfig` as described in the [Text generation strategies](../generation_strategies) guide. + + +## Prompted image captioning + +You can extend image captioning by providing a text prompt, which the model will continue given the image. Let's take +another image to illustrate: + +
+ Image of the Eiffel Tower at night +
+ +Photo by [Denys Nevozhai](https://unsplash.com/@dnevozhai). + +Textual and image prompts can be passed to the model's processor as a single list to create appropriate inputs. + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +This is an image of the Eiffel Tower in Paris, France. +``` + +## Few-shot prompting + +While IDEFICS demonstrates great zero-shot results, your task may require a certain format of the caption, or come with +other restrictions or requirements that increase task's complexity. Few-shot prompting can be used to enable in-context learning. +By providing examples in the prompt, you can steer the model to generate results that mimic the format of given examples. + +Let's use the previous image of the Eiffel Tower as an example for the model and build a prompt that demonstrates to the model +that in addition to learning what the object in an image is, we would also like to get some interesting information about it. +Then, let's see, if we can get the same response format for an image of the Statue of Liberty: + +
+ Image of the Statue of Liberty +
+ +Photo by [Juan Mayobre](https://unsplash.com/@jmayobres). + +```py +>>> prompt = ["User:", +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "Describe this image.\nAssistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building.\n", +... "User:", +... "https://images.unsplash.com/photo-1524099163253-32b7f0256868?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3387&q=80", +... "Describe this image.\nAssistant:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=30, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +User: Describe this image. +Assistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building. +User: Describe this image. +Assistant: An image of the Statue of Liberty. Fun fact: the Statue of Liberty is 151 feet tall. +``` + +Notice that just from a single example (i.e., 1-shot) the model has learned how to perform the task. For more complex tasks, +feel free to experiment with a larger number of examples (e.g., 3-shot, 5-shot, etc.). + +## Visual question answering + +Visual Question Answering (VQA) is the task of answering open-ended questions based on an image. Similar to image +captioning it can be used in accessibility applications, but also in education (reasoning about visual materials), customer +service (questions about products based on images), and image retrieval. + +Let's get a new image for this task: + +
+ Image of a couple having a picnic +
+ +Photo by [Jarritos Mexican Soda](https://unsplash.com/@jarritos). + +You can steer the model from image captioning to visual question answering by prompting it with appropriate instructions: + +```py +>>> prompt = [ +... "Instruction: Provide an answer to the question. Use the image to answer.\n", +... "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Question: Where are these people and what's the weather like? Answer:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=20, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Provide an answer to the question. Use the image to answer. + Question: Where are these people and what's the weather like? Answer: They're in a park in New York City, and it's a beautiful day. +``` + +## Image classification + +IDEFICS is capable of classifying images into different categories without being explicitly trained on data containing +labeled examples from those specific categories. Given a list of categories and using its image and text understanding +capabilities, the model can infer which category the image likely belongs to. + +Say, we have this image of a vegetable stand: + +
+ Image of a vegetable stand +
+ +Photo by [Peter Wendt](https://unsplash.com/@peterwendt). + +We can instruct the model to classify the image into one of the categories that we have: + +```py +>>> categories = ['animals','vegetables', 'city landscape', 'cars', 'office'] +>>> prompt = [f"Instruction: Classify the following image into a single category from the following list: {categories}.\n", +... "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Category: " +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=6, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Classify the following image into a single category from the following list: ['animals', 'vegetables', 'city landscape', 'cars', 'office']. +Category: Vegetables +``` + +In the example above we instruct the model to classify the image into a single category, however, you can also prompt the model to do rank classification. + +## Image-guided text generation + +For more creative applications, you can use image-guided text generation to generate text based on an image. This can be +useful to create descriptions of products, ads, descriptions of a scene, etc. + +Let's prompt IDEFICS to write a story based on a simple image of a red door: + +
+ Image of a red door with a pumpkin on the steps +
+ +Photo by [Craig Tidball](https://unsplash.com/@devonshiremedia). + +```py +>>> prompt = ["Instruction: Use the image to write a story. \n", +... "https://images.unsplash.com/photo-1517086822157-2b0358e7684a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2203&q=80", +... "Story: \n"] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, num_beams=2, max_new_tokens=200, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Use the image to write a story. + Story: +Once upon a time, there was a little girl who lived in a house with a red door. She loved her red door. It was the prettiest door in the whole world. + +One day, the little girl was playing in her yard when she noticed a man standing on her doorstep. He was wearing a long black coat and a top hat. + +The little girl ran inside and told her mother about the man. + +Her mother said, โ€œDonโ€™t worry, honey. Heโ€™s just a friendly ghost.โ€ + +The little girl wasnโ€™t sure if she believed her mother, but she went outside anyway. + +When she got to the door, the man was gone. + +The next day, the little girl was playing in her yard again when she noticed the man standing on her doorstep. + +He was wearing a long black coat and a top hat. + +The little girl ran +``` + +Looks like IDEFICS noticed the pumpkin on the doorstep and went with a spooky Halloween story about a ghost. + + + +For longer outputs like this, you will greatly benefit from tweaking the text generation strategy. This can help +you significantly improve the quality of the generated output. Check out [Text generation strategies](../generation_strategies) +to learn more. + + +## Running inference in batch mode + +All of the earlier sections illustrated IDEFICS for a single example. In a very similar fashion, you can run inference +for a batch of examples by passing a list of prompts: + +```py +>>> prompts = [ +... [ "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... ] + +>>> inputs = processor(prompts, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i,t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +0: +This is an image of the Eiffel Tower in Paris, France. + +1: +This is an image of a couple on a picnic blanket. + +2: +This is an image of a vegetable stand. +``` + +## IDEFICS instruct for conversational use + +For conversational use cases, you can find fine-tuned instructed versions of the model on the ๐Ÿค— Hub: +`HuggingFaceM4/idefics-80b-instruct` and `HuggingFaceM4/idefics-9b-instruct`. + +These checkpoints are the result of fine-tuning the respective base models on a mixture of supervised and instruction +fine-tuning datasets, which boosts the downstream performance while making the models more usable in conversational settings. + +The use and prompting for the conversational use is very similar to using the base models: + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct" +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> prompts = [ +... [ +... "User: What is in this image?", +... "https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG", +... "", + +... "\nAssistant: This picture depicts Idefix, the dog of Obelix in Asterix and Obelix. Idefix is running on the ground.", + +... "\nUser:", +... "https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052", +... "And who is that?", + +... "\nAssistant:", +... ], +... ] + +>>> # --batched mode +>>> inputs = processor(prompts, add_end_of_utterance_token=False, return_tensors="pt").to(model.device) +>>> # --single sample mode +>>> # inputs = processor(prompts[0], return_tensors="pt").to(model.device) + +>>> # Generation args +>>> exit_condition = processor.tokenizer("", add_special_tokens=False).input_ids +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, eos_token_id=exit_condition, bad_words_ids=bad_words_ids, max_length=100) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i, t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_captioning.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_captioning.md new file mode 100644 index 0000000000000000000000000000000000000000..f9716f29a20447152c78eb8af7b25f3e16d57bc8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_captioning.md @@ -0,0 +1,277 @@ + + + +# Image captioning + +[[open-in-colab]] + +Image captioning is the task of predicting a caption for a given image. Common real world applications of it include +aiding visually impaired people that can help them navigate through different situations. Therefore, image captioning +helps to improve content accessibility for people by describing images to them. + +This guide will show you how to: + +* Fine-tune an image captioning model. +* Use the fine-tuned model for inference. + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate -q +pip install jiwer -q +``` + +We encourage you to log in to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to log in: + + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +## Load the Pokรฉmon BLIP captions dataset + +Use the ๐Ÿค— Dataset library to load a dataset that consists of {image-caption} pairs. To create your own image captioning dataset +in PyTorch, you can follow [this notebook](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/GIT/Fine_tune_GIT_on_an_image_captioning_dataset.ipynb). + + +```python +from datasets import load_dataset + +ds = load_dataset("lambdalabs/pokemon-blip-captions") +ds +``` +```bash +DatasetDict({ + train: Dataset({ + features: ['image', 'text'], + num_rows: 833 + }) +}) +``` + +The dataset has two features, `image` and `text`. + + + +Many image captioning datasets contain multiple captions per image. In those cases, a common strategy is to randomly sample a caption amongst the available ones during training. + + + +Split the datasetโ€™s train split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + + +```python +ds = ds["train"].train_test_split(test_size=0.1) +train_ds = ds["train"] +test_ds = ds["test"] +``` + +Let's visualize a couple of samples from the training set. + + +```python +from textwrap import wrap +import matplotlib.pyplot as plt +import numpy as np + + +def plot_images(images, captions): + plt.figure(figsize=(20, 20)) + for i in range(len(images)): + ax = plt.subplot(1, len(images), i + 1) + caption = captions[i] + caption = "\n".join(wrap(caption, 12)) + plt.title(caption) + plt.imshow(images[i]) + plt.axis("off") + + +sample_images_to_visualize = [np.array(train_ds[i]["image"]) for i in range(5)] +sample_captions = [train_ds[i]["text"] for i in range(5)] +plot_images(sample_images_to_visualize, sample_captions) +``` + +
+ Sample training images +
+ +## Preprocess the dataset + +Since the dataset has two modalities (image and text), the pre-processing pipeline will preprocess images and the captions. + +To do so, load the processor class associated with the model you are about to fine-tune. + +```python +from transformers import AutoProcessor + +checkpoint = "microsoft/git-base" +processor = AutoProcessor.from_pretrained(checkpoint) +``` + +The processor will internally pre-process the image (which includes resizing, and pixel scaling) and tokenize the caption. + +```python +def transforms(example_batch): + images = [x for x in example_batch["image"]] + captions = [x for x in example_batch["text"]] + inputs = processor(images=images, text=captions, padding="max_length") + inputs.update({"labels": inputs["input_ids"]}) + return inputs + + +train_ds.set_transform(transforms) +test_ds.set_transform(transforms) +``` + +With the dataset ready, you can now set up the model for fine-tuning. + +## Load a base model + +Load the ["microsoft/git-base"](https://huggingface.co/microsoft/git-base) into a [`AutoModelForCausalLM`](https://huggingface.co/docs/transformers/model_doc/auto#transformers.AutoModelForCausalLM) object. + + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained(checkpoint) +``` + +## Evaluate + +Image captioning models are typically evaluated with the [Rouge Score](https://huggingface.co/spaces/evaluate-metric/rouge) or [Word Error Rate](https://huggingface.co/spaces/evaluate-metric/wer). For this guide, you will use the Word Error Rate (WER). + +We use the ๐Ÿค— Evaluate library to do so. For potential limitations and other gotchas of the WER, refer to [this guide](https://huggingface.co/spaces/evaluate-metric/wer). + + +```python +from evaluate import load +import torch + +wer = load("wer") + + +def compute_metrics(eval_pred): + logits, labels = eval_pred + predicted = logits.argmax(-1) + decoded_labels = processor.batch_decode(labels, skip_special_tokens=True) + decoded_predictions = processor.batch_decode(predicted, skip_special_tokens=True) + wer_score = wer.compute(predictions=decoded_predictions, references=decoded_labels) + return {"wer_score": wer_score} +``` + +## Train! + +Now, you are ready to start fine-tuning the model. You will use the ๐Ÿค— [`Trainer`] for this. + +First, define the training arguments using [`TrainingArguments`]. + + +```python +from transformers import TrainingArguments, Trainer + +model_name = checkpoint.split("/")[1] + +training_args = TrainingArguments( + output_dir=f"{model_name}-pokemon", + learning_rate=5e-5, + num_train_epochs=50, + fp16=True, + per_device_train_batch_size=32, + per_device_eval_batch_size=32, + gradient_accumulation_steps=2, + save_total_limit=3, + eval_strategy="steps", + eval_steps=50, + save_strategy="steps", + save_steps=50, + logging_steps=50, + remove_unused_columns=False, + push_to_hub=True, + label_names=["labels"], + load_best_model_at_end=True, +) +``` + +Then pass them along with the datasets and the model to ๐Ÿค— Trainer. + +```python +trainer = Trainer( + model=model, + args=training_args, + train_dataset=train_ds, + eval_dataset=test_ds, + compute_metrics=compute_metrics, +) +``` + +To start training, simply call [`~Trainer.train`] on the [`Trainer`] object. + +```python +trainer.train() +``` + +You should see the training loss drop smoothly as training progresses. + +Once training is completed, share your model to the Hub with the [`~Trainer.push_to_hub`] method so everyone can use your model: + + +```python +trainer.push_to_hub() +``` + +## Inference + +Take a sample image from `test_ds` to test the model. + + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png" +image = Image.open(requests.get(url, stream=True).raw) +image +``` + +
+ Test image +
+ +Prepare image for the model. + +```python +from transformers import infer_device + +device = infer_device() +inputs = processor(images=image, return_tensors="pt").to(device) +pixel_values = inputs.pixel_values +``` + +Call [`generate`] and decode the predictions. + +```python +generated_ids = model.generate(pixel_values=pixel_values, max_length=50) +generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] +print(generated_caption) +``` +```bash +a drawing of a pink and blue pokemon +``` + +Looks like the fine-tuned model generated a pretty good caption! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..49fdc9db60d4d7650a4ef85c506f7fb4a3f2f0ef --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_classification.md @@ -0,0 +1,542 @@ + + +# Image classification + +[[open-in-colab]] + + + +Image classification assigns a label or class to an image. Unlike text or audio classification, the inputs are the +pixel values that comprise an image. There are many applications for image classification, such as detecting damage +after a natural disaster, monitoring crop health, or helping screen medical images for signs of disease. + +This guide illustrates how to: + +1. Fine-tune [ViT](../model_doc/vit) on the [Food-101](https://huggingface.co/datasets/food101) dataset to classify a food item in an image. +2. Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/image-classification) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate accelerate pillow torchvision scikit-learn +``` + +We encourage you to log in to your Hugging Face account to upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load Food-101 dataset + +Start by loading a smaller subset of the Food-101 dataset from the ๐Ÿค— Datasets library. This will give you a chance to +experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset + +>>> food = load_dataset("food101", split="train[:5000]") +``` + +Split the dataset's `train` split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> food = food.train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> food["train"][0] +{'image': , + 'label': 79} +``` + +Each example in the dataset has two fields: + +- `image`: a PIL image of the food item +- `label`: the label class of the food item + +To make it easier for the model to get the label name from the label id, create a dictionary that maps the label name +to an integer and vice versa: + +```py +>>> labels = food["train"].features["label"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +Now you can convert the label id to a label name: + +```py +>>> id2label[str(79)] +'prime_rib' +``` + +## Preprocess + +The next step is to load a ViT image processor to process the image into a tensor: + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "google/vit-base-patch16-224-in21k" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +``` + + + +Apply some image transformations to the images to make the model more robust against overfitting. Here you'll use torchvision's [`transforms`](https://pytorch.org/vision/stable/transforms.html) module, but you can also use any image library you like. + +Crop a random part of the image, resize it, and normalize it with the image mean and standard deviation: + +```py +>>> from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor + +>>> normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std) +>>> size = ( +... image_processor.size["shortest_edge"] +... if "shortest_edge" in image_processor.size +... else (image_processor.size["height"], image_processor.size["width"]) +... ) +>>> _transforms = Compose([RandomResizedCrop(size), ToTensor(), normalize]) +``` + +Then create a preprocessing function to apply the transforms and return the `pixel_values` - the inputs to the model - of the image: + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(img.convert("RGB")) for img in examples["image"]] +... del examples["image"] +... return examples +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.with_transform`] method. The transforms are applied on the fly when you load an element of the dataset: + +```py +>>> food = food.with_transform(transforms) +``` + +Now create a batch of examples using [`DefaultDataCollator`]. Unlike other data collators in ๐Ÿค— Transformers, the `DefaultDataCollator` does not apply additional preprocessing such as padding. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + + + + + + +To avoid overfitting and to make the model more robust, add some data augmentation to the training part of the dataset. +Here we use Keras preprocessing layers to define the transformations for the training data (includes data augmentation), +and transformations for the validation data (only center cropping, resizing and normalizing). You can use `tf.image`or +any other library you prefer. + +```py +>>> from tensorflow import keras +>>> from tensorflow.keras import layers + +>>> size = (image_processor.size["height"], image_processor.size["width"]) + +>>> train_data_augmentation = keras.Sequential( +... [ +... layers.RandomCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... layers.RandomFlip("horizontal"), +... layers.RandomRotation(factor=0.02), +... layers.RandomZoom(height_factor=0.2, width_factor=0.2), +... ], +... name="train_data_augmentation", +... ) + +>>> val_data_augmentation = keras.Sequential( +... [ +... layers.CenterCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... ], +... name="val_data_augmentation", +... ) +``` + +Next, create functions to apply appropriate transformations to a batch of images, instead of one image at a time. + +```py +>>> import numpy as np +>>> import tensorflow as tf +>>> from PIL import Image + + +>>> def convert_to_tf_tensor(image: Image): +... np_image = np.array(image) +... tf_image = tf.convert_to_tensor(np_image) +... # `expand_dims()` is used to add a batch dimension since +... # the TF augmentation layers operates on batched inputs. +... return tf.expand_dims(tf_image, 0) + + +>>> def preprocess_train(example_batch): +... """Apply train_transforms across a batch.""" +... images = [ +... train_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch + + +... def preprocess_val(example_batch): +... """Apply val_transforms across a batch.""" +... images = [ +... val_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch +``` + +Use ๐Ÿค— Datasets [`~datasets.Dataset.set_transform`] to apply the transformations on the fly: + +```py +food["train"].set_transform(preprocess_train) +food["test"].set_transform(preprocess_val) +``` + +As a final preprocessing step, create a batch of examples using `DefaultDataCollator`. Unlike other data collators in ๐Ÿค— Transformers, the +`DefaultDataCollator` does not apply additional preprocessing, such as padding. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load an +evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load +the [accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the accuracy: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you set up your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load ViT with [`AutoModelForImageClassification`]. Specify the number of labels along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import AutoModelForImageClassification, TrainingArguments, Trainer + +>>> model = AutoModelForImageClassification.from_pretrained( +... checkpoint, +... num_labels=len(labels), +... id2label=id2label, +... label2id=label2id, +... ) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. It is important you don't remove unused columns because that'll drop the `image` column. Without the `image` column, you can't create `pixel_values`. Set `remove_unused_columns=False` to prevent this behavior! The only other required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the accuracy and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_food_model", +... remove_unused_columns=False, +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=food["train"], +... eval_dataset=food["test"], +... processing_class=image_processor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + + + + + +If you are unfamiliar with fine-tuning a model with Keras, check out the [basic tutorial](./training#train-a-tensorflow-model-with-keras) first! + + + +To fine-tune a model in TensorFlow, follow these steps: +1. Define the training hyperparameters, and set up an optimizer and a learning rate schedule. +2. Instantiate a pre-trained model. +3. Convert a ๐Ÿค— Dataset to a `tf.data.Dataset`. +4. Compile your model. +5. Add callbacks and use the `fit()` method to run the training. +6. Upload your model to ๐Ÿค— Hub to share with the community. + +Start by defining the hyperparameters, optimizer and learning rate schedule: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 5 +>>> num_train_steps = len(food["train"]) * num_epochs +>>> learning_rate = 3e-5 +>>> weight_decay_rate = 0.01 + +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=learning_rate, +... num_train_steps=num_train_steps, +... weight_decay_rate=weight_decay_rate, +... num_warmup_steps=0, +... ) +``` + +Then, load ViT with [`TFAutoModelForImageClassification`] along with the label mappings: + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ) +``` + +Convert your datasets to the `tf.data.Dataset` format using the [`~datasets.Dataset.to_tf_dataset`] and your `data_collator`: + +```py +>>> # converting our train dataset to tf.data.Dataset +>>> tf_train_dataset = food["train"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) + +>>> # converting our test dataset to tf.data.Dataset +>>> tf_eval_dataset = food["test"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) +``` + +Configure the model for training with `compile()`: + +```py +>>> from tensorflow.keras.losses import SparseCategoricalCrossentropy + +>>> loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) +>>> model.compile(optimizer=optimizer, loss=loss) +``` + +To compute the accuracy from the predictions and push your model to the ๐Ÿค— Hub, use [Keras callbacks](../main_classes/keras_callbacks). +Pass your `compute_metrics` function to [KerasMetricCallback](../main_classes/keras_callbacks#transformers.KerasMetricCallback), +and use the [PushToHubCallback](../main_classes/keras_callbacks#transformers.PushToHubCallback) to upload the model: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset) +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="food_classifier", +... tokenizer=image_processor, +... save_strategy="no", +... ) +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you are ready to train your model! Call `fit()` with your training and validation datasets, the number of epochs, +and your callbacks to fine-tune the model: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks) +Epoch 1/5 +250/250 [==============================] - 313s 1s/step - loss: 2.5623 - val_loss: 1.4161 - accuracy: 0.9290 +Epoch 2/5 +250/250 [==============================] - 265s 1s/step - loss: 0.9181 - val_loss: 0.6808 - accuracy: 0.9690 +Epoch 3/5 +250/250 [==============================] - 252s 1s/step - loss: 0.3910 - val_loss: 0.4303 - accuracy: 0.9820 +Epoch 4/5 +250/250 [==============================] - 251s 1s/step - loss: 0.2028 - val_loss: 0.3191 - accuracy: 0.9900 +Epoch 5/5 +250/250 [==============================] - 238s 949ms/step - loss: 0.1232 - val_loss: 0.3259 - accuracy: 0.9890 +``` + +Congratulations! You have fine-tuned your model and shared it on the ๐Ÿค— Hub. You can now use it for inference! + + + + + + +For a more in-depth example of how to finetune a model for image classification, take a look at the corresponding [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb). + + + +## Inference + +Great, now that you've fine-tuned a model, you can use it for inference! + +Load an image you'd like to run inference on: + +```py +>>> ds = load_dataset("food101", split="validation[:10]") +>>> image = ds["image"][0] +``` + +
+ image of beignets +
+ +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for image classification with your model, and pass your image to it: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("image-classification", model="my_awesome_food_model") +>>> classifier(image) +[{'score': 0.31856709718704224, 'label': 'beignets'}, + {'score': 0.015232225880026817, 'label': 'bruschetta'}, + {'score': 0.01519392803311348, 'label': 'chicken_wings'}, + {'score': 0.013022331520915031, 'label': 'pork_chop'}, + {'score': 0.012728818692266941, 'label': 'prime_rib'}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Load an image processor to preprocess the image and return the `input` as PyTorch tensors: + +```py +>>> from transformers import AutoImageProcessor +>>> import torch + +>>> image_processor = AutoImageProcessor.from_pretrained("my_awesome_food_model") +>>> inputs = image_processor(image, return_tensors="pt") +``` + +Pass your inputs to the model and return the logits: + +```py +>>> from transformers import AutoModelForImageClassification + +>>> model = AutoModelForImageClassification.from_pretrained("my_awesome_food_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Get the predicted label with the highest probability, and use the model's `id2label` mapping to convert it to a label: + +```py +>>> predicted_label = logits.argmax(-1).item() +>>> model.config.id2label[predicted_label] +'beignets' +``` + + + + + +Load an image processor to preprocess the image and return the `input` as TensorFlow tensors: + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("MariaK/food_classifier") +>>> inputs = image_processor(image, return_tensors="tf") +``` + +Pass your inputs to the model and return the logits: + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained("MariaK/food_classifier") +>>> logits = model(**inputs).logits +``` + +Get the predicted label with the highest probability, and use the model's `id2label` mapping to convert it to a label: + +```py +>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0]) +>>> model.config.id2label[predicted_class_id] +'beignets' +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_feature_extraction.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_feature_extraction.md new file mode 100644 index 0000000000000000000000000000000000000000..455a2b425d412b199374f79c3b04be50bee968e3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_feature_extraction.md @@ -0,0 +1,134 @@ + + +# Image Feature Extraction + +[[open-in-colab]] + +Image feature extraction is the task of extracting semantically meaningful features given an image. This has many use cases, including image similarity and image retrieval. Moreover, most computer vision models can be used for image feature extraction, where one can remove the task-specific head (image classification, object detection etc) and get the features. These features are very useful on a higher level: edge detection, corner detection and so on. They may also contain information about the real world (e.g. what a cat looks like) depending on how deep the model is. Therefore, these outputs can be used to train new classifiers on a specific dataset. + +In this guide, you will: + +- Learn to build a simple image similarity system on top of the `image-feature-extraction` pipeline. +- Accomplish the same task with bare model inference. + +## Image Similarity using `image-feature-extraction` Pipeline + +We have two images of cats sitting on top of fish nets, one of them is generated. + +```python +from PIL import Image +import requests + +img_urls = ["https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.jpeg"] +image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB") +image_gen = Image.open(requests.get(img_urls[1], stream=True).raw).convert("RGB") +``` + +Let's see the pipeline in action. First, initialize the pipeline. If you don't pass any model to it, the pipeline will be automatically initialized with [google/vit-base-patch16-224](google/vit-base-patch16-224). If you'd like to calculate similarity, set `pool` to True. + +```python +import torch +from transformers import pipeline, infer_device +# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.) +DEVICE = infer_device() +pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True) +``` + +To infer with `pipe` pass both images to it. + +```python +outputs = pipe([image_real, image_gen]) +``` + +The output contains pooled embeddings of those two images. + +```python +# get the length of a single output +print(len(outputs[0][0])) +# show outputs +print(outputs) + +# 768 +# [[[-0.03909236937761307, 0.43381670117378235, -0.06913255900144577, +``` + +To get the similarity score, we need to pass them to a similarity function. + +```python +from torch.nn.functional import cosine_similarity + +similarity_score = cosine_similarity(torch.Tensor(outputs[0]), + torch.Tensor(outputs[1]), dim=1) + +print(similarity_score) + +# tensor([0.6043]) +``` + +If you want to get the last hidden states before pooling, avoid passing any value for the `pool` parameter, as it is set to `False`ย by default. These hidden states are useful for training new classifiers or models based on the features from the model. + +```python +pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-224", device=DEVICE) +outputs = pipe(image_real) +``` + +Since the outputs are unpooled, we get the last hidden states where the first dimension is the batch size, and the last two are the embedding shape. + +```python +import numpy as np +print(np.array(outputs).shape) +# (1, 197, 768) +``` + +## Getting Features and Similarities using `AutoModel` + +We can also use `AutoModel`ย class of transformers to get the features. `AutoModel` loads any transformers model with no task-specific head, and we can use this to get the features. + +```python +from transformers import AutoImageProcessor, AutoModel + +processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +model = AutoModel.from_pretrained("google/vit-base-patch16-224").to(DEVICE) +``` + +Let's write a simple function for inference. We will pass the inputs to the `processor` first and pass its outputs to the `model`. + +```python +def infer(image): + inputs = processor(image, return_tensors="pt").to(DEVICE) + outputs = model(**inputs) + return outputs.pooler_output +``` + +We can pass the images directly to this function and get the embeddings. + +```python +embed_real = infer(image_real) +embed_gen = infer(image_gen) +``` + +We can get the similarity again over the embeddings. + +```python +from torch.nn.functional import cosine_similarity + +similarity_score = cosine_similarity(embed_real, embed_gen, dim=1) +print(similarity_score) + +# tensor([0.6061], device='cuda:0', grad_fn=) +``` + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_text_to_text.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_text_to_text.md new file mode 100644 index 0000000000000000000000000000000000000000..a98b1e5509cc3f0f925c666429fcc5f10cf2b158 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_text_to_text.md @@ -0,0 +1,315 @@ + + +# Image-text-to-text + +[[open-in-colab]] + +Image-text-to-text models, also known as vision language models (VLMs), are language models that take an image input. These models can tackle various tasks, from visual question answering to image segmentation. This task shares many similarities with image-to-text, butย with some overlapping use cases like image captioning. Image-to-text models only take image inputs and often accomplish a specific task, whereas VLMs take open-ended text and image inputs and are more generalist models. + +In this guide, we provide a brief overview of VLMs and show how to use them with Transformers for inference. + +To begin with, there are multiple types of VLMs: +- base models used for fine-tuning +- chat fine-tuned models for conversation +- instruction fine-tuned models + +This guide focuses on inference with an instruction-tuned model. + +Let's begin installing the dependencies. + +```bash +pip install -q transformers accelerate flash_attn +``` + +Let's initialize the model and the processor. + +```python +from transformers import AutoProcessor, AutoModelForImageTextToText +import torch + +device = torch.device("cuda") +model = AutoModelForImageTextToText.from_pretrained( + "HuggingFaceM4/idefics2-8b", + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2", +).to(device) + +processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b") +``` + +This model has a [chat template](./chat_templating) that helps user parse chat outputs. Moreover, the model can also accept multiple images as input in a single conversation or message. We will now prepare the inputs. + +The image inputs look like the following. + +
+ Two cats sitting on a net +
+ +
+ A bee on a pink flower +
+ + +```python +from PIL import Image +import requests + +img_urls =["https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png", + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"] +images = [Image.open(requests.get(img_urls[0], stream=True).raw), + Image.open(requests.get(img_urls[1], stream=True).raw)] +``` + +Below is an example of the chat template. We can feed conversation turns and the last message as an input by appending it at the end of the template. + + +```python +messages = [ + { + "role": "user", + "content": [ + {"type": "image"}, + {"type": "text", "text": "What do we see in this image?"}, + ] + }, + { + "role": "assistant", + "content": [ + {"type": "text", "text": "In this image we can see two cats on the nets."}, + ] + }, + { + "role": "user", + "content": [ + {"type": "image"}, + {"type": "text", "text": "And how about this image?"}, + ] + }, +] +``` + +We will now call the processors' [`~ProcessorMixin.apply_chat_template`] method to preprocess its output along with the image inputs. + +```python +prompt = processor.apply_chat_template(messages, add_generation_prompt=True) +inputs = processor(text=prompt, images=[images[0], images[1]], return_tensors="pt").to(device) +``` + +We can now pass the preprocessed inputs to the model. + +```python +with torch.no_grad(): + generated_ids = model.generate(**inputs, max_new_tokens=500) +generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True) + +print(generated_texts) +## ['User: What do we see in this image? \nAssistant: In this image we can see two cats on the nets. \nUser: And how about this image? \nAssistant: In this image we can see flowers, plants and insect.'] +``` + +## Pipeline + +The fastest way to get started is to use the [`Pipeline`] API. Specify the `"image-text-to-text"` task and the model you want to use. + +```python +from transformers import pipeline +pipe = pipeline("image-text-to-text", model="llava-hf/llava-interleave-qwen-0.5b-hf") +``` + +The example below uses chat templates to format the text inputs. + +```python +messages = [ + { + "role": "user", + "content": [ + { + "type": "image", + "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg", + }, + {"type": "text", "text": "Describe this image."}, + ], + }, + { + "role": "assistant", + "content": [ + {"type": "text", "text": "There's a pink flower"}, + ], + }, + ] +``` + +Pass the chat template formatted text and image to [`Pipeline`] and set `return_full_text=False` to remove the input from the generated output. + +```python +outputs = pipe(text=messages, max_new_tokens=20, return_full_text=False) +outputs[0]["generated_text"] +# with a yellow center in the foreground. The flower is surrounded by red and white flowers with green stems +``` + +If you prefer, you can also load the images separately and pass them to the pipeline like so: + +```python +pipe = pipeline("image-text-to-text", model="HuggingFaceTB/SmolVLM-256M-Instruct") + +img_urls = [ + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png", + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg", +] +images = [ + Image.open(requests.get(img_urls[0], stream=True).raw), + Image.open(requests.get(img_urls[1], stream=True).raw), +] + +messages = [ + { + "role": "user", + "content": [ + {"type": "image"}, + {"type": "image"}, + {"type": "text", "text": "What do you see in these images?"}, + ], + } +] +outputs = pipe(text=messages, images=images, max_new_tokens=50, return_full_text=False) +outputs[0]["generated_text"] +" In the first image, there are two cats sitting on a plant. In the second image, there are flowers with a pinkish hue." +``` + +The images will still be included in the `"input_text"` field of the output: + +```python +outputs[0]['input_text'] +""" +[{'role': 'user', + 'content': [{'type': 'image', + 'image': }, + {'type': 'image', + 'image': }, + {'type': 'text', 'text': 'What do you see in these images?'}]}]## Streaming +""" +``` + +We can use [text streaming](./generation_strategies#streaming) for a better generation experience. Transformers supports streaming with the [`TextStreamer`] or [`TextIteratorStreamer`] classes. We will use the [`TextIteratorStreamer`] with IDEFICS-8B. + +Assume we have an application that keeps chat history and takes in the new user input. We will preprocess the inputs as usual and initialize [`TextIteratorStreamer`] to handle the generation in a separate thread. This allows you to stream the generated text tokens in real-time. Any generation arguments can be passed to [`TextIteratorStreamer`]. + + +```python +import time +from transformers import TextIteratorStreamer +from threading import Thread + +def model_inference( + user_prompt, + chat_history, + max_new_tokens, + images +): + user_prompt = { + "role": "user", + "content": [ + {"type": "image"}, + {"type": "text", "text": user_prompt}, + ] + } + chat_history.append(user_prompt) + streamer = TextIteratorStreamer( + processor.tokenizer, + skip_prompt=True, + timeout=5.0, + ) + + generation_args = { + "max_new_tokens": max_new_tokens, + "streamer": streamer, + "do_sample": False + } + + # add_generation_prompt=True makes model generate bot response + prompt = processor.apply_chat_template(chat_history, add_generation_prompt=True) + inputs = processor( + text=prompt, + images=images, + return_tensors="pt", + ).to(device) + generation_args.update(inputs) + + thread = Thread( + target=model.generate, + kwargs=generation_args, + ) + thread.start() + + acc_text = "" + for text_token in streamer: + time.sleep(0.04) + acc_text += text_token + if acc_text.endswith(""): + acc_text = acc_text[:-18] + yield acc_text + + thread.join() +``` + +Now let's call the `model_inference` function we created and stream the values. + +```python +generator = model_inference( + user_prompt="And what is in this image?", + chat_history=messages[:2], + max_new_tokens=100, + images=images +) + +for value in generator: + print(value) + +# In +# In this +# In this image ... +``` + +## Fit models in smaller hardware + +VLMs are often large and need to be optimized to fit on smaller hardware. Transformers supports many model quantization libraries, and here we will only show int8 quantization with [Quanto](./quantization/quanto#quanto). int8 quantization offers memory improvements up to 75 percent (if all weights are quantized). However it is no free lunch, since 8-bit is not a CUDA-native precision, the weights are quantized back and forth on the fly, which adds up to latency. + +First, install dependencies. + +```bash +pip install -U quanto bitsandbytes +``` + +To quantize a model during loading, we need to first create [`QuantoConfig`]. Then load the model as usual, but pass `quantization_config`ย during model initialization. + +```python +from transformers import AutoModelForImageTextToText, QuantoConfig + +model_id = "HuggingFaceM4/idefics2-8b" +quantization_config = QuantoConfig(weights="int8") +quantized_model = AutoModelForImageTextToText.from_pretrained( + model_id, device_map="cuda", quantization_config=quantization_config +) +``` + +And that's it, we can use the model the same way with no changes. + +## Further Reading + +Here are some more resources for the image-text-to-text task. + +- [Image-text-to-textย task page](https://huggingface.co/tasks/image-text-to-text) covers model types, use cases, datasets, and more. +- [Vision Language Models Explained](https://huggingface.co/blog/vlms) is a blog post that covers everything about vision language models and supervised fine-tuning using [TRL](https://huggingface.co/docs/trl/en/index). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_to_image.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_to_image.md new file mode 100644 index 0000000000000000000000000000000000000000..da6a57ac9aa99e8122a0779dcd76fd2ed40e916e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/image_to_image.md @@ -0,0 +1,133 @@ + + +# Image-to-Image Task Guide + +[[open-in-colab]] + +Image-to-Image task is the task where an application receives an image and outputs another image. This has various subtasks, including image enhancement (super resolution, low light enhancement, deraining and so on), image inpainting, and more. + +This guide will show you how to: +- Use an image-to-image pipeline for super resolution task, +- Run image-to-image models for same task without a pipeline. + +Note that as of the time this guide is released, `image-to-image` pipeline only supports super resolution task. + +Let's begin by installing the necessary libraries. + +```bash +pip install transformers +``` + +We can now initialize the pipeline with a [Swin2SR model](https://huggingface.co/caidas/swin2SR-lightweight-x2-64). We can then infer with the pipeline by calling it with an image. As of now, only [Swin2SR models](https://huggingface.co/models?sort=trending&search=swin2sr) are supported in this pipeline. + +```python +from transformers import pipeline, infer_device +import torch +# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.) +device = infer_device() +pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device) +``` + +Now, let's load an image. + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat.jpg" +image = Image.open(requests.get(url, stream=True).raw) + +print(image.size) +``` +```bash +# (532, 432) +``` +
+ Photo of a cat +
+ +We can now do inference with the pipeline. We will get an upscaled version of the cat image. + +```python +upscaled = pipe(image) +print(upscaled.size) +``` +```bash +# (1072, 880) +``` + +If you wish to do inference yourself with no pipeline, you can use the `Swin2SRForImageSuperResolution` and `Swin2SRImageProcessor` classes of transformers. We will use the same model checkpoint for this. Let's initialize the model and the processor. + +```python +from transformers import Swin2SRForImageSuperResolution, Swin2SRImageProcessor + +model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-lightweight-x2-64").to(device) +processor = Swin2SRImageProcessor("caidas/swin2SR-lightweight-x2-64") +``` + +`pipeline` abstracts away the preprocessing and postprocessing steps that we have to do ourselves, so let's preprocess the image. We will pass the image to the processor and then move the pixel values to GPU. + +```python +pixel_values = processor(image, return_tensors="pt").pixel_values +print(pixel_values.shape) + +pixel_values = pixel_values.to(device) +``` + +We can now infer the image by passing pixel values to the model. + +```python +import torch + +with torch.no_grad(): + outputs = model(pixel_values) +``` +Output is an object of type `ImageSuperResolutionOutput` that looks like below ๐Ÿ‘‡ + +``` +(loss=None, reconstruction=tensor([[[[0.8270, 0.8269, 0.8275, ..., 0.7463, 0.7446, 0.7453], + [0.8287, 0.8278, 0.8283, ..., 0.7451, 0.7448, 0.7457], + [0.8280, 0.8273, 0.8269, ..., 0.7447, 0.7446, 0.7452], + ..., + [0.5923, 0.5933, 0.5924, ..., 0.0697, 0.0695, 0.0706], + [0.5926, 0.5932, 0.5926, ..., 0.0673, 0.0687, 0.0705], + [0.5927, 0.5914, 0.5922, ..., 0.0664, 0.0694, 0.0718]]]], + device='cuda:0'), hidden_states=None, attentions=None) +``` +We need to get the `reconstruction` and post-process it for visualization. Let's see how it looks like. + +```python +outputs.reconstruction.data.shape +# torch.Size([1, 3, 880, 1072]) +``` + +We need to squeeze the output and get rid of axis 0, clip the values, then convert it to be numpy float. Then we will arrange axes to have the shape [1072, 880], and finally, bring the output back to range [0, 255]. + +```python +import numpy as np + +# squeeze, take to CPU and clip the values +output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy() +# rearrange the axes +output = np.moveaxis(output, source=0, destination=-1) +# bring values back to pixel values range +output = (output * 255.0).round().astype(np.uint8) +Image.fromarray(output) +``` +
+ Upscaled photo of a cat +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/keypoint_detection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/keypoint_detection.md new file mode 100644 index 0000000000000000000000000000000000000000..3a5871d01a2b8f47230768d0e9ead22b5eb38ecf --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/keypoint_detection.md @@ -0,0 +1,154 @@ + + +# Keypoint Detection + +[[open-in-colab]] + +Keypoint detection identifies and locates specific points of interest within an image. These keypoints, also known as landmarks, represent meaningful features of objects, such as facial features or object parts. These models take an image input and return the following outputs: + +- **Keypoints and Scores**: Points of interest and their confidence scores. +- **Descriptors**: A representation of the image region surrounding each keypoint, capturing its texture, gradient, orientation and other properties. + +In this guide, we will show how to extract keypoints from images. + +For this tutorial, we will use [SuperPoint](./model_doc/superpoint), a foundation model for keypoint detection. + +```python +from transformers import AutoImageProcessor, SuperPointForKeypointDetection +processor = AutoImageProcessor.from_pretrained("magic-leap-community/superpoint") +model = SuperPointForKeypointDetection.from_pretrained("magic-leap-community/superpoint") +``` + +Let's test the model on the images below. + +
+ Bee + Cats +
+ + +```python +import torch +from PIL import Image +import requests +import cv2 + + +url_image_1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" +image_1 = Image.open(requests.get(url_image_1, stream=True).raw) +url_image_2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png" +image_2 = Image.open(requests.get(url_image_2, stream=True).raw) + +images = [image_1, image_2] +``` + +We can now process our inputs and infer. + +```python +inputs = processor(images,return_tensors="pt").to(model.device, model.dtype) +outputs = model(**inputs) +``` + +The model output has relative keypoints, descriptors, masks and scores for each item in the batch. The mask highlights areas of the image where keypoints are present. + +```python +SuperPointKeypointDescriptionOutput(loss=None, keypoints=tensor([[[0.0437, 0.0167], + [0.0688, 0.0167], + [0.0172, 0.0188], + ..., + [0.5984, 0.9812], + [0.6953, 0.9812]]]), + scores=tensor([[0.0056, 0.0053, 0.0079, ..., 0.0125, 0.0539, 0.0377], + [0.0206, 0.0058, 0.0065, ..., 0.0000, 0.0000, 0.0000]], + grad_fn=), descriptors=tensor([[[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + ...], + grad_fn=), mask=tensor([[1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 0, 0, 0]], dtype=torch.int32), hidden_states=None) +``` + +To plot actual keypoints in the image, we need to postprocess the output. To do so, we have to pass the actual image sizes to `post_process_keypoint_detection` along with outputs. + +```python +image_sizes = [(image.size[1], image.size[0]) for image in images] +outputs = processor.post_process_keypoint_detection(outputs, image_sizes) +``` + +The outputs are now a list of dictionaries where each dictionary is a processed output of keypoints, scores and descriptors. + +```python +[{'keypoints': tensor([[ 226, 57], + [ 356, 57], + [ 89, 64], + ..., + [3604, 3391]], dtype=torch.int32), + 'scores': tensor([0.0056, 0.0053, ...], grad_fn=), + 'descriptors': tensor([[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357]], + grad_fn=)}, + {'keypoints': tensor([[ 46, 6], + [ 78, 6], + [422, 6], + [206, 404]], dtype=torch.int32), + 'scores': tensor([0.0206, 0.0058, 0.0065, 0.0053, 0.0070, ...,grad_fn=), + 'descriptors': tensor([[-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211], + [-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211]}] +``` + +We can use these to plot the keypoints. + +```python +import matplotlib.pyplot as plt +import torch + +for i in range(len(images)): + keypoints = outputs[i]["keypoints"] + scores = outputs[i]["scores"] + descriptors = outputs[i]["descriptors"] + keypoints = outputs[i]["keypoints"].detach().numpy() + scores = outputs[i]["scores"].detach().numpy() + image = images[i] + image_width, image_height = image.size + + plt.axis('off') + plt.imshow(image) + plt.scatter( + keypoints[:, 0], + keypoints[:, 1], + s=scores * 100, + c='cyan', + alpha=0.4 + ) + plt.show() +``` + +Below you can see the outputs. + +
+ Bee + Cats +
+ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/knowledge_distillation_for_image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/knowledge_distillation_for_image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..7c4a684d3c057f7c23616d94dcfcffe42739609c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/knowledge_distillation_for_image_classification.md @@ -0,0 +1,185 @@ + +# Knowledge Distillation for Computer Vision + +[[open-in-colab]] + +Knowledge distillation is a technique used to transfer knowledge from a larger, more complex model (teacher) to a smaller, simpler model (student). To distill knowledge from one model to another, we take a pre-trained teacher model trained on a certain task (image classification for this case) and randomly initialize a student model to be trained on image classification. Next, we train the student model to minimize the difference between its outputs and the teacher's outputs, thus making it mimic the behavior. It was first introduced in [Distilling the Knowledge in a Neural Network by Hinton et al](https://huggingface.co/papers/1503.02531). In this guide, we will do task-specific knowledge distillation. We will use the [beans dataset](https://huggingface.co/datasets/beans) for this. + +This guide demonstrates how you can distill a [fine-tuned ViT model](https://huggingface.co/merve/vit-mobilenet-beans-224) (teacher model) to a [MobileNet](https://huggingface.co/google/mobilenet_v2_1.4_224) (student model) using the [Trainerย API](https://huggingface.co/docs/transformers/en/main_classes/trainer#trainer) of ๐Ÿค— Transformers. + +Let's install the libraries needed for distillation and evaluating the process. + +```bash +pip install transformers datasets accelerate tensorboard evaluate --upgrade +``` + +In this example, we are using the `merve/beans-vit-224` model as teacher model. It's an image classification model, based on `google/vit-base-patch16-224-in21k` fine-tuned on beans dataset. We will distill this model to a randomly initialized MobileNetV2. + +We will now load the dataset. + +```python +from datasets import load_dataset + +dataset = load_dataset("beans") +``` + +We can use an image processor from either of the models, as in this case they return the same output with same resolution. We will use the `map()` method of `dataset` to apply the preprocessing to every split of the dataset. + +```python +from transformers import AutoImageProcessor +teacher_processor = AutoImageProcessor.from_pretrained("merve/beans-vit-224") + +def process(examples): + processed_inputs = teacher_processor(examples["image"]) + return processed_inputs + +processed_datasets = dataset.map(process, batched=True) +``` + +Essentially, we want the student model (a randomly initialized MobileNet) to mimic the teacher model (fine-tuned vision transformer). To achieve this, we first get the logits output from the teacher and the student. Then, we divide each of them by the parameter `temperature` which controls the importance of each soft target. A parameter called `lambda` weighs the importance of the distillation loss. In this example, we will use `temperature=5` and `lambda=0.5`. We will use the Kullback-Leibler Divergence loss to compute the divergence between the student and teacher. Given two data P and Q, KL Divergence explains how much extra information we need to represent P using Q. If two are identical, their KL divergence is zero, as there's no other information needed to explain P from Q. Thus, in the context of knowledge distillation, KL divergence is useful. + + +```python +from transformers import TrainingArguments, Trainer, infer_device +import torch +import torch.nn as nn +import torch.nn.functional as F + +class ImageDistilTrainer(Trainer): + def __init__(self, teacher_model=None, student_model=None, temperature=None, lambda_param=None, *args, **kwargs): + super().__init__(model=student_model, *args, **kwargs) + self.teacher = teacher_model + self.student = student_model + self.loss_function = nn.KLDivLoss(reduction="batchmean") + device = infer_device() + self.teacher.to(device) + self.teacher.eval() + self.temperature = temperature + self.lambda_param = lambda_param + + def compute_loss(self, student, inputs, return_outputs=False): + student_output = self.student(**inputs) + + with torch.no_grad(): + teacher_output = self.teacher(**inputs) + + # Compute soft targets for teacher and student + soft_teacher = F.softmax(teacher_output.logits / self.temperature, dim=-1) + soft_student = F.log_softmax(student_output.logits / self.temperature, dim=-1) + + # Compute the loss + distillation_loss = self.loss_function(soft_student, soft_teacher) * (self.temperature ** 2) + + # Compute the true label loss + student_target_loss = student_output.loss + + # Calculate final loss + loss = (1. - self.lambda_param) * student_target_loss + self.lambda_param * distillation_loss + return (loss, student_output) if return_outputs else loss +``` + +We will now login to Hugging Face Hub so we can push our model to the Hugging Face Hub through the `Trainer`. + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +Let's set the `TrainingArguments`, the teacher model and the student model. + +```python +from transformers import AutoModelForImageClassification, MobileNetV2Config, MobileNetV2ForImageClassification + +training_args = TrainingArguments( + output_dir="my-awesome-model", + num_train_epochs=30, + fp16=True, + logging_dir=f"{repo_name}/logs", + logging_strategy="epoch", + eval_strategy="epoch", + save_strategy="epoch", + load_best_model_at_end=True, + metric_for_best_model="accuracy", + report_to="tensorboard", + push_to_hub=True, + hub_strategy="every_save", + hub_model_id=repo_name, + ) + +num_labels = len(processed_datasets["train"].features["labels"].names) + +# initialize models +teacher_model = AutoModelForImageClassification.from_pretrained( + "merve/beans-vit-224", + num_labels=num_labels, + ignore_mismatched_sizes=True +) + +# training MobileNetV2 from scratch +student_config = MobileNetV2Config() +student_config.num_labels = num_labels +student_model = MobileNetV2ForImageClassification(student_config) +``` + +We can use `compute_metrics` function to evaluate our model on the test set. This function will be used during the training process to compute the `accuracy` & `f1` of our model. + +```python +import evaluate +import numpy as np + +accuracy = evaluate.load("accuracy") + +def compute_metrics(eval_pred): + predictions, labels = eval_pred + acc = accuracy.compute(references=labels, predictions=np.argmax(predictions, axis=1)) + return {"accuracy": acc["accuracy"]} +``` + +Let's initialize the `Trainer` with the training arguments we defined. We will also initialize our data collator. + +```python +from transformers import DefaultDataCollator + +data_collator = DefaultDataCollator() +trainer = ImageDistilTrainer( + student_model=student_model, + teacher_model=teacher_model, + training_args=training_args, + train_dataset=processed_datasets["train"], + eval_dataset=processed_datasets["validation"], + data_collator=data_collator, + processing_class=teacher_processor, + compute_metrics=compute_metrics, + temperature=5, + lambda_param=0.5 +) +``` + +We can now train our model. + +```python +trainer.train() +``` + +We can evaluate the model on the test set. + +```python +trainer.evaluate(processed_datasets["test"]) +``` + +On test set, our model reaches 72 percent accuracy. To have a sanity check over efficiency of distillation, we also trained MobileNet on the beans dataset from scratch with the same hyperparameters and observed 63 percent accuracy on the test set. We invite the readers to try different pre-trained teacher models, student architectures, distillation parameters and report their findings. The training logs and checkpoints for distilled model can be found in [this repository](https://huggingface.co/merve/vit-mobilenet-beans-224), and MobileNetV2 trained from scratch can be found in this [repository](https://huggingface.co/merve/resnet-mobilenet-beans-5). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..f6e71fe117677f87612f148470584b1014a228bd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/language_modeling.md @@ -0,0 +1,429 @@ + + +# Causal language modeling + +[[open-in-colab]] + +There are two types of language modeling, causal and masked. This guide illustrates causal language modeling. +Causal language models are frequently used for text generation. You can use these models for creative applications like +choosing your own text adventure or an intelligent coding assistant like Copilot or CodeParrot. + + + +Causal language modeling predicts the next token in a sequence of tokens, and the model can only attend to tokens on +the left. This means the model cannot see future tokens. GPT-2 is an example of a causal language model. + +This guide will show you how to: + +1. Finetune [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) on the [r/askscience](https://www.reddit.com/r/askscience/) subset of the [ELI5](https://huggingface.co/datasets/dany0407/eli5_category) dataset. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/text-generation) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate +``` + +We encourage you to log in to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load ELI5 dataset + +Start by loading the first 5000 examples from the [ELI5-Category](https://huggingface.co/datasets/dany0407/eli5_category) dataset with the ๐Ÿค— Datasets library. This'll give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("dany0407/eli5_category", split="train[:5000]") +``` + +Split the dataset's `train` split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> eli5["train"][0] +{'q_id': '7h191n', + 'title': 'What does the tax bill that was passed today mean? How will it affect Americans in each tax bracket?', + 'selftext': '', + 'category': 'Economics', + 'subreddit': 'explainlikeimfive', + 'answers': {'a_id': ['dqnds8l', 'dqnd1jl', 'dqng3i1', 'dqnku5x'], + 'text': ["The tax bill is 500 pages long and there were a lot of changes still going on right to the end. It's not just an adjustment to the income tax brackets, it's a whole bunch of changes. As such there is no good answer to your question. The big take aways are: - Big reduction in corporate income tax rate will make large companies very happy. - Pass through rate change will make certain styles of business (law firms, hedge funds) extremely happy - Income tax changes are moderate, and are set to expire (though it's the kind of thing that might just always get re-applied without being made permanent) - People in high tax states (California, New York) lose out, and many of them will end up with their taxes raised.", + 'None yet. It has to be reconciled with a vastly different house bill and then passed again.', + 'Also: does this apply to 2017 taxes? Or does it start with 2018 taxes?', + 'This article explains both the House and senate bills, including the proposed changes to your income taxes based on your income level. URL_0'], + 'score': [21, 19, 5, 3], + 'text_urls': [[], + [], + [], + ['https://www.investopedia.com/news/trumps-tax-reform-what-can-be-done/']]}, + 'title_urls': ['url'], + 'selftext_urls': ['url']} +``` + +While this may look like a lot, you're only really interested in the `text` field. What's cool about language modeling +tasks is you don't need labels (also known as an unsupervised task) because the next word *is* the label. + +## Preprocess + + + +The next step is to load a DistilGPT2 tokenizer to process the `text` subfield: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") +``` + +You'll notice from the example above, the `text` field is actually nested inside `answers`. This means you'll need to +extract the `text` subfield from its nested structure with the [`flatten`](https://huggingface.co/docs/datasets/process#flatten) method: + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'q_id': '7h191n', + 'title': 'What does the tax bill that was passed today mean? How will it affect Americans in each tax bracket?', + 'selftext': '', + 'category': 'Economics', + 'subreddit': 'explainlikeimfive', + 'answers.a_id': ['dqnds8l', 'dqnd1jl', 'dqng3i1', 'dqnku5x'], + 'answers.text': ["The tax bill is 500 pages long and there were a lot of changes still going on right to the end. It's not just an adjustment to the income tax brackets, it's a whole bunch of changes. As such there is no good answer to your question. The big take aways are: - Big reduction in corporate income tax rate will make large companies very happy. - Pass through rate change will make certain styles of business (law firms, hedge funds) extremely happy - Income tax changes are moderate, and are set to expire (though it's the kind of thing that might just always get re-applied without being made permanent) - People in high tax states (California, New York) lose out, and many of them will end up with their taxes raised.", + 'None yet. It has to be reconciled with a vastly different house bill and then passed again.', + 'Also: does this apply to 2017 taxes? Or does it start with 2018 taxes?', + 'This article explains both the House and senate bills, including the proposed changes to your income taxes based on your income level. URL_0'], + 'answers.score': [21, 19, 5, 3], + 'answers.text_urls': [[], + [], + [], + ['https://www.investopedia.com/news/trumps-tax-reform-what-can-be-done/']], + 'title_urls': ['url'], + 'selftext_urls': ['url']} +``` + +Each subfield is now a separate column as indicated by the `answers` prefix, and the `text` field is a list now. Instead +of tokenizing each sentence separately, convert the list to a string so you can jointly tokenize them. + +Here is a first preprocessing function to join the list of strings for each example and tokenize the result: + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +To apply this preprocessing function over the entire dataset, use the ๐Ÿค— Datasets [`~datasets.Dataset.map`] method. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once, and increasing the number of processes with `num_proc`. Remove any columns you don't need: + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +This dataset contains the token sequences, but some of these are longer than the maximum input length for the model. + +You can now use a second preprocessing function to + +- concatenate all the sequences +- split the concatenated sequences into shorter chunks defined by `block_size`, which should be both shorter than the maximum input length and short enough for your GPU RAM. + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... result["labels"] = result["input_ids"].copy() +... return result +``` + +Apply the `group_texts` function over the entire dataset: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +Now create a batch of examples using [`DataCollatorForLanguageModeling`]. It's more efficient to *dynamically pad* the +sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + +Use the end-of-sequence token as the padding token and set `mlm=False`. This will use the inputs as labels shifted to the right by one element: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) +``` + + + +Use the end-of-sequence token as the padding token and set `mlm=False`. This will use the inputs as labels shifted to the right by one element: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False, return_tensors="tf") +``` + + + + + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the [basic tutorial](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load DistilGPT2 with [`AutoModelForCausalLM`]: + +```py +>>> from transformers import AutoModelForCausalLM, TrainingArguments, Trainer + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). +2. Pass the training arguments to [`Trainer`] along with the model, datasets, and data collator. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_clm-model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... tokenizer=tokenizer, +... ) + +>>> trainer.train() +``` + +Once training is completed, use the [`~transformers.Trainer.evaluate`] method to evaluate your model and get its perplexity: + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 49.61 +``` + +Then share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the [basic tutorial](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Then you can load DistilGPT2 with [`TFAutoModelForCausalLM`]: + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +This can be done by specifying where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_clm-model", +... tokenizer=tokenizer, +... ) +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callback to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for causal language modeling, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with a prompt you'd like to generate text from: + +```py +>>> prompt = "Somatic hypermutation allows the immune system to" +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for text generation with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +>>> generator = pipeline("text-generation", model="username/my_awesome_eli5_clm-model") +>>> generator(prompt) +[{'generated_text': "Somatic hypermutation allows the immune system to be able to effectively reverse the damage caused by an infection.\n\n\nThe damage caused by an infection is caused by the immune system's ability to perform its own self-correcting tasks."}] +``` + + + +Tokenize the text and return the `input_ids` as PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="pt").input_ids +``` + +Use the [`~generation.GenerationMixin.generate`] method to generate text. +For more details about the different text generation strategies and parameters for controlling generation, check out the [Text generation strategies](../generation_strategies) page. + +```py +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained("username/my_awesome_eli5_clm-model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +["Somatic hypermutation allows the immune system to react to drugs with the ability to adapt to a different environmental situation. In other words, a system of 'hypermutation' can help the immune system to adapt to a different environmental situation or in some cases even a single life. In contrast, researchers at the University of Massachusetts-Boston have found that 'hypermutation' is much stronger in mice than in humans but can be found in humans, and that it's not completely unknown to the immune system. A study on how the immune system"] +``` + + +Tokenize the text and return the `input_ids` as TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="tf").input_ids +``` + +Use the [`~transformers.generation_tf_utils.TFGenerationMixin.generate`] method to create the summarization. For more details about the different text generation strategies and parameters for controlling generation, check out the [Text generation strategies](../generation_strategies) page. + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("username/my_awesome_eli5_clm-model") +>>> outputs = model.generate(input_ids=inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Somatic hypermutation allows the immune system to detect the presence of other viruses as they become more prevalent. Therefore, researchers have identified a high proportion of human viruses. The proportion of virus-associated viruses in our study increases with age. Therefore, we propose a simple algorithm to detect the presence of these new viruses in our samples as a sign of improved immunity. A first study based on this algorithm, which will be published in Science on Friday, aims to show that this finding could translate into the development of a better vaccine that is more effective for'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/mask_generation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/mask_generation.md new file mode 100644 index 0000000000000000000000000000000000000000..28ecfd193be67af211d9b32dc4303e6e5a8bff70 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/mask_generation.md @@ -0,0 +1,236 @@ + + +# Mask Generation + +Mask generation is the task of generating semantically meaningful masks for an image. +This task is very similar to [image segmentation](semantic_segmentation), but many differences exist. Image segmentation models are trained on labeled datasets and are limited to the classes they have seen during training; they return a set of masks and corresponding classes, given an image. + +Mask generation models are trained on large amounts of data and operate in two modes. +- Prompting mode: In this mode, the model takes in an image and a prompt, where a prompt can be a 2D point location (XY coordinates) in the image within an object or a bounding box surrounding an object. In prompting mode, the model only returns the mask over the object +that the prompt is pointing out. +- Segment Everything mode: In segment everything, given an image, the model generates every mask in the image. To do so, a grid of points is generated and overlaid on the image for inference. + +Mask generation task is supported by [Segment Anything Model (SAM)](model_doc/sam). It's a powerful model that consists of a Vision Transformer-based image encoder, a prompt encoder, and a two-way transformer mask decoder. Images and prompts are encoded, and the decoder takes these embeddings and generates valid masks. + +
+ SAM Architecture +
+ +SAM serves as a powerful foundation model for segmentation as it has large data coverage. It is trained on +[SA-1B](https://ai.meta.com/datasets/segment-anything/), a dataset with 1 million images and 1.1 billion masks. + +In this guide, you will learn how to: +- Infer in segment everything mode with batching, +- Infer in point prompting mode, +- Infer in box prompting mode. + +First, let's install `transformers`: + +```bash +pip install -q transformers +``` + +## Mask Generation Pipeline + +The easiest way to infer mask generation models is to use the `mask-generation` pipeline. + +```python +>>> from transformers import pipeline + +>>> checkpoint = "facebook/sam-vit-base" +>>> mask_generator = pipeline(model=checkpoint, task="mask-generation") +``` + +Let's see the image. + +```python +from PIL import Image +import requests + +img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" +image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB") +``` + +
+ Example Image +
+ +Let's segment everything. `points-per-batch` enables parallel inference of points in segment everything mode. This enables faster inference, but consumes more memory. Moreover, SAM only enables batching over points and not the images. `pred_iou_thresh` is the IoU confidence threshold where only the masks above that certain threshold are returned. + +```python +masks = mask_generator(image, points_per_batch=128, pred_iou_thresh=0.88) +``` + +The `masks` looks like the following: + +```bash +{'masks': [array([[False, False, False, ..., True, True, True], + [False, False, False, ..., True, True, True], + [False, False, False, ..., True, True, True], + ..., + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False]]), + array([[False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + ..., +'scores': tensor([0.9972, 0.9917, + ..., +} +``` + +We can visualize them like this: + +```python +import matplotlib.pyplot as plt + +plt.imshow(image, cmap='gray') + +for i, mask in enumerate(masks["masks"]): + plt.imshow(mask, cmap='viridis', alpha=0.1, vmin=0, vmax=1) + +plt.axis('off') +plt.show() +``` + +Below is the original image in grayscale with colorful maps overlaid. Very impressive. + +
+ Visualized +
+ + +## Model Inference + +### Point Prompting + +You can also use the model without the pipeline. To do so, initialize the model and +the processor. + +```python +from transformers import SamModel, SamProcessor, infer_device +import torch +device = infer_device() +model = SamModel.from_pretrained("facebook/sam-vit-base").to(device) +processor = SamProcessor.from_pretrained("facebook/sam-vit-base") +``` + +To do point prompting, pass the input point to the processor, then take the processor output +and pass it to the model for inference. To post-process the model output, pass the outputs and +`original_sizes` and `reshaped_input_sizes` we take from the processor's initial output. We need to pass these +since the processor resizes the image, and the output needs to be extrapolated. + +```python +input_points = [[[2592, 1728]]] # point location of the bee + +inputs = processor(image, input_points=input_points, return_tensors="pt").to(device) +with torch.no_grad(): + outputs = model(**inputs) +masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu()) +``` +We can visualize the three masks in the `masks`ย output. + +```python +import matplotlib.pyplot as plt +import numpy as np + +fig, axes = plt.subplots(1, 4, figsize=(15, 5)) + +axes[0].imshow(image) +axes[0].set_title('Original Image') +mask_list = [masks[0][0][0].numpy(), masks[0][0][1].numpy(), masks[0][0][2].numpy()] + +for i, mask in enumerate(mask_list, start=1): + overlayed_image = np.array(image).copy() + + overlayed_image[:,:,0] = np.where(mask == 1, 255, overlayed_image[:,:,0]) + overlayed_image[:,:,1] = np.where(mask == 1, 0, overlayed_image[:,:,1]) + overlayed_image[:,:,2] = np.where(mask == 1, 0, overlayed_image[:,:,2]) + + axes[i].imshow(overlayed_image) + axes[i].set_title(f'Mask {i}') +for ax in axes: + ax.axis('off') + +plt.show() +``` + +
+ Visualized +
+ +### Box Prompting + +You can also do box prompting in a similar fashion to point prompting. You can simply pass the input box in the format of a list +`[x_min, y_min, x_max, y_max]` format along with the image to the `processor`. Take the processor output and directly pass it +to the model, then post-process the output again. + + +```python +# bounding box around the bee +box = [2350, 1600, 2850, 2100] + +inputs = processor( + image, + input_boxes=[[[box]]], + return_tensors="pt" + ).to("cuda") + +with torch.no_grad(): + outputs = model(**inputs) + +mask = processor.image_processor.post_process_masks( + outputs.pred_masks.cpu(), + inputs["original_sizes"].cpu(), + inputs["reshaped_input_sizes"].cpu() +)[0][0][0].numpy() +``` + +You can visualize the bounding box around the bee as shown below. + +```python +import matplotlib.patches as patches + +fig, ax = plt.subplots() +ax.imshow(image) + +rectangle = patches.Rectangle((2350, 1600), 500, 500, linewidth=2, edgecolor='r', facecolor='none') +ax.add_patch(rectangle) +ax.axis("off") +plt.show() +``` + +
+ Visualized Bbox +
+ +You can see the inference output below. + +```python +fig, ax = plt.subplots() +ax.imshow(image) +ax.imshow(mask, cmap='viridis', alpha=0.4) + +ax.axis("off") +plt.show() +``` + +
+ Visualized Inference +
+ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/masked_language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/masked_language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..469b1d7fcb99f63b0746cbba82dd5e8657cae7a3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/masked_language_modeling.md @@ -0,0 +1,445 @@ + + +# Masked language modeling + +[[open-in-colab]] + + + +Masked language modeling predicts a masked token in a sequence, and the model can attend to tokens bidirectionally. This +means the model has full access to the tokens on the left and right. Masked language modeling is great for tasks that +require a good contextual understanding of an entire sequence. BERT is an example of a masked language model. + +This guide will show you how to: + +1. Finetune [DistilRoBERTa](https://huggingface.co/distilbert/distilroberta-base) on the [r/askscience](https://www.reddit.com/r/askscience/) subset of the [ELI5](https://huggingface.co/datasets/eli5) dataset. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/fill-mask) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate +``` + +We encourage you to log in to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load ELI5 dataset + +Start by loading the first 5000 examples from the [ELI5-Category](https://huggingface.co/datasets/eli5_category) dataset with the ๐Ÿค— Datasets library. This'll give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5_category", split="train[:5000]") +``` + +Split the dataset's `train` split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> eli5["train"][0] +{'q_id': '7h191n', + 'title': 'What does the tax bill that was passed today mean? How will it affect Americans in each tax bracket?', + 'selftext': '', + 'category': 'Economics', + 'subreddit': 'explainlikeimfive', + 'answers': {'a_id': ['dqnds8l', 'dqnd1jl', 'dqng3i1', 'dqnku5x'], + 'text': ["The tax bill is 500 pages long and there were a lot of changes still going on right to the end. It's not just an adjustment to the income tax brackets, it's a whole bunch of changes. As such there is no good answer to your question. The big take aways are: - Big reduction in corporate income tax rate will make large companies very happy. - Pass through rate change will make certain styles of business (law firms, hedge funds) extremely happy - Income tax changes are moderate, and are set to expire (though it's the kind of thing that might just always get re-applied without being made permanent) - People in high tax states (California, New York) lose out, and many of them will end up with their taxes raised.", + 'None yet. It has to be reconciled with a vastly different house bill and then passed again.', + 'Also: does this apply to 2017 taxes? Or does it start with 2018 taxes?', + 'This article explains both the House and senate bills, including the proposed changes to your income taxes based on your income level. URL_0'], + 'score': [21, 19, 5, 3], + 'text_urls': [[], + [], + [], + ['https://www.investopedia.com/news/trumps-tax-reform-what-can-be-done/']]}, + 'title_urls': ['url'], + 'selftext_urls': ['url']} +``` + +While this may look like a lot, you're only really interested in the `text` field. What's cool about language modeling tasks is you don't need labels (also known as an unsupervised task) because the next word *is* the label. + +## Preprocess + + + +For masked language modeling, the next step is to load a DistilRoBERTa tokenizer to process the `text` subfield: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilroberta-base") +``` + +You'll notice from the example above, the `text` field is actually nested inside `answers`. This means you'll need to extract the `text` subfield from its nested structure with the [`flatten`](https://huggingface.co/docs/datasets/process#flatten) method: + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'q_id': '7h191n', + 'title': 'What does the tax bill that was passed today mean? How will it affect Americans in each tax bracket?', + 'selftext': '', + 'category': 'Economics', + 'subreddit': 'explainlikeimfive', + 'answers.a_id': ['dqnds8l', 'dqnd1jl', 'dqng3i1', 'dqnku5x'], + 'answers.text': ["The tax bill is 500 pages long and there were a lot of changes still going on right to the end. It's not just an adjustment to the income tax brackets, it's a whole bunch of changes. As such there is no good answer to your question. The big take aways are: - Big reduction in corporate income tax rate will make large companies very happy. - Pass through rate change will make certain styles of business (law firms, hedge funds) extremely happy - Income tax changes are moderate, and are set to expire (though it's the kind of thing that might just always get re-applied without being made permanent) - People in high tax states (California, New York) lose out, and many of them will end up with their taxes raised.", + 'None yet. It has to be reconciled with a vastly different house bill and then passed again.', + 'Also: does this apply to 2017 taxes? Or does it start with 2018 taxes?', + 'This article explains both the House and senate bills, including the proposed changes to your income taxes based on your income level. URL_0'], + 'answers.score': [21, 19, 5, 3], + 'answers.text_urls': [[], + [], + [], + ['https://www.investopedia.com/news/trumps-tax-reform-what-can-be-done/']], + 'title_urls': ['url'], + 'selftext_urls': ['url']} +``` + +Each subfield is now a separate column as indicated by the `answers` prefix, and the `text` field is a list now. Instead +of tokenizing each sentence separately, convert the list to a string so you can jointly tokenize them. + +Here is a first preprocessing function to join the list of strings for each example and tokenize the result: + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +To apply this preprocessing function over the entire dataset, use the ๐Ÿค— Datasets [`~datasets.Dataset.map`] method. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once, and increasing the number of processes with `num_proc`. Remove any columns you don't need: + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +This dataset contains the token sequences, but some of these are longer than the maximum input length for the model. + +You can now use a second preprocessing function to +- concatenate all the sequences +- split the concatenated sequences into shorter chunks defined by `block_size`, which should be both shorter than the maximum input length and short enough for your GPU RAM. + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... return result +``` + +Apply the `group_texts` function over the entire dataset: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +Now create a batch of examples using [`DataCollatorForLanguageModeling`]. It's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + + +Use the end-of-sequence token as the padding token and specify `mlm_probability` to randomly mask tokens each time you iterate over the data: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15) +``` + + + +Use the end-of-sequence token as the padding token and specify `mlm_probability` to randomly mask tokens each time you iterate over the data: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15, return_tensors="tf") +``` + + + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load DistilRoBERTa with [`AutoModelForMaskedLM`]: + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). +2. Pass the training arguments to [`Trainer`] along with the model, datasets, and data collator. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_mlm_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... tokenizer=tokenizer, +... ) + +>>> trainer.train() +``` + +Once training is completed, use the [`~transformers.Trainer.evaluate`] method to evaluate your model and get its perplexity: + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 8.76 +``` + +Then share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Then you can load DistilRoBERTa with [`TFAutoModelForMaskedLM`]: + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +This can be done by specifying where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_mlm_model", +... tokenizer=tokenizer, +... ) +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callback to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for masked language modeling, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with some text you'd like the model to fill in the blank with, and use the special `` token to indicate the blank: + +```py +>>> text = "The Milky Way is a galaxy." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for fill-mask with your model, and pass your text to it. If you like, you can use the `top_k` parameter to specify how many predictions to return: + +```py +>>> from transformers import pipeline + +>>> mask_filler = pipeline("fill-mask", "username/my_awesome_eli5_mlm_model") +>>> mask_filler(text, top_k=3) +[{'score': 0.5150994658470154, + 'token': 21300, + 'token_str': ' spiral', + 'sequence': 'The Milky Way is a spiral galaxy.'}, + {'score': 0.07087188959121704, + 'token': 2232, + 'token_str': ' massive', + 'sequence': 'The Milky Way is a massive galaxy.'}, + {'score': 0.06434620916843414, + 'token': 650, + 'token_str': ' small', + 'sequence': 'The Milky Way is a small galaxy.'}] +``` + + + +Tokenize the text and return the `input_ids` as PyTorch tensors. You'll also need to specify the position of the `` token: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="pt") +>>> mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] +``` + +Pass your inputs to the model and return the `logits` of the masked token: + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("username/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +Then return the three masked tokens with the highest probability and print them out: + +```py +>>> top_3_tokens = torch.topk(mask_token_logits, 3, dim=1).indices[0].tolist() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + +Tokenize the text and return the `input_ids` as TensorFlow tensors. You'll also need to specify the position of the `` token: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="tf") +>>> mask_token_index = tf.where(inputs["input_ids"] == tokenizer.mask_token_id)[0, 1] +``` + +Pass your inputs to the model and return the `logits` of the masked token: + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("username/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +Then return the three masked tokens with the highest probability and print them out: + +```py +>>> top_3_tokens = tf.math.top_k(mask_token_logits, 3).indices.numpy() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/monocular_depth_estimation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/monocular_depth_estimation.md new file mode 100644 index 0000000000000000000000000000000000000000..c90abce1cd57b4d2525d541c6c9c3f41acbfc4f1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/monocular_depth_estimation.md @@ -0,0 +1,159 @@ + + +# Monocular depth estimation + +Monocular depth estimation is a computer vision task that involves predicting the depth information of a scene from a +single image. In other words, it is the process of estimating the distance of objects in a scene from +a single camera viewpoint. + +Monocular depth estimation has various applications, including 3D reconstruction, augmented reality, autonomous driving, +and robotics. It is a challenging task as it requires the model to understand the complex relationships between objects +in the scene and the corresponding depth information, which can be affected by factors such as lighting conditions, +occlusion, and texture. + +There are two main depth estimation categories: + +- **Absolute depth estimation**: This task variant aims to provide exact depth measurements from the camera. The term is used interchangeably with metric depth estimation, where depth is provided in precise measurements in meters or feet. Absolute depth estimation models output depth maps with numerical values that represent real-world distances. + +- **Relative depth estimation**: Relative depth estimation aims to predict the depth order of objects or points in a scene without providing the precise measurements. These models output a depth map that indicates which parts of the scene are closer or farther relative to each other without the actual distances to A and B. + +In this guide, we will see how to infer with [Depth Anything V2](https://huggingface.co/depth-anything/Depth-Anything-V2-Large), a state-of-the-art zero-shot relative depth estimation model, and [ZoeDepth](https://huggingface.co/docs/transformers/main/en/model_doc/zoedepth), an absolute depth estimation model. + + + +Check the [Depth Estimation](https://huggingface.co/tasks/depth-estimation) task page to view all compatible architectures and checkpoints. + + + +Before we begin, we need to install the latest version of Transformers: + +```bash +pip install -q -U transformers +``` + +## Depth estimation pipeline + +The simplest way to try out inference with a model supporting depth estimation is to use the corresponding [`pipeline`]. +Instantiate a pipeline from a [checkpoint on the Hugging Face Hub](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads): + +```py +>>> from transformers import pipeline, infer_device +>>> import torch +>>> device = infer_device() +>>> checkpoint = "depth-anything/Depth-Anything-V2-base-hf" +>>> pipe = pipeline("depth-estimation", model=checkpoint, device=device) +``` + +Next, choose an image to analyze: + +```py +>>> from PIL import Image +>>> import requests + +>>> url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> image +``` + +
+ Photo of a bee +
+ +Pass the image to the pipeline. + +```py +>>> predictions = pipe(image) +``` + +The pipeline returns a dictionary with two entries. The first one, called `predicted_depth`, is a tensor with the values +being the depth expressed in meters for each pixel. +The second one, `depth`, is a PIL image that visualizes the depth estimation result. + +Let's take a look at the visualized result: + +```py +>>> predictions["depth"] +``` + +
+ Depth estimation visualization +
+ +## Depth estimation inference by hand + +Now that you've seen how to use the depth estimation pipeline, let's see how we can replicate the same result by hand. + +Start by loading the model and associated processor from a [checkpoint on the Hugging Face Hub](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads). +Here we'll use the same checkpoint as before: + +```py +>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation + +>>> checkpoint = "Intel/zoedepth-nyu-kitti" + +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +>>> model = AutoModelForDepthEstimation.from_pretrained(checkpoint).to(device) +``` + +Prepare the image input for the model using the `image_processor` that will take care of the necessary image transformations +such as resizing and normalization: + +```py +>>> pixel_values = image_processor(image, return_tensors="pt").pixel_values.to(device) +``` + +Pass the prepared inputs through the model: + +```py +>>> import torch + +>>> with torch.no_grad(): +... outputs = model(pixel_values) +``` + +Let's post-process the results to remove any padding and resize the depth map to match the original image size. The `post_process_depth_estimation` outputs a list of dicts containing the `"predicted_depth"`. + +```py +>>> # ZoeDepth dynamically pads the input image. Thus we pass the original image size as argument +>>> # to `post_process_depth_estimation` to remove the padding and resize to original dimensions. +>>> post_processed_output = image_processor.post_process_depth_estimation( +... outputs, +... source_sizes=[(image.height, image.width)], +... ) + +>>> predicted_depth = post_processed_output[0]["predicted_depth"] +>>> depth = (predicted_depth - predicted_depth.min()) / (predicted_depth.max() - predicted_depth.min()) +>>> depth = depth.detach().cpu().numpy() * 255 +>>> depth = Image.fromarray(depth.astype("uint8")) +``` + + +

In the original implementation ZoeDepth model performs inference on both the original and flipped images and averages out the results. The post_process_depth_estimation function can handle this for us by passing the flipped outputs to the optional outputs_flipped argument:

+
>>> with torch.no_grad():   
+...     outputs = model(pixel_values)
+...     outputs_flipped = model(pixel_values=torch.flip(inputs.pixel_values, dims=[3]))
+>>> post_processed_output = image_processor.post_process_depth_estimation(
+...     outputs,
+...     source_sizes=[(image.height, image.width)],
+...     outputs_flipped=outputs_flipped,
+... )
+
+
+ +
+ Depth estimation visualization +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/multiple_choice.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/multiple_choice.md new file mode 100644 index 0000000000000000000000000000000000000000..0cac42a649859fb7c709c9b9dbaeb4f30d8f2b82 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/multiple_choice.md @@ -0,0 +1,369 @@ + + +# Multiple choice + +[[open-in-colab]] + +A multiple choice task is similar to question answering, except several candidate answers are provided along with a context and the model is trained to select the correct answer. + +This guide will show you how to: + +1. Finetune [BERT](https://huggingface.co/google-bert/bert-base-uncased) on the `regular` configuration of the [SWAG](https://huggingface.co/datasets/swag) dataset to select the best answer given multiple options and some context. +2. Use your finetuned model for inference. + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load SWAG dataset + +Start by loading the `regular` configuration of the SWAG dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset + +>>> swag = load_dataset("swag", "regular") +``` + +Then take a look at an example: + +```py +>>> swag["train"][0] +{'ending0': 'passes by walking down the street playing their instruments.', + 'ending1': 'has heard approaching them.', + 'ending2': "arrives and they're outside dancing and asleep.", + 'ending3': 'turns the lead singer watches the performance.', + 'fold-ind': '3416', + 'gold-source': 'gold', + 'label': 0, + 'sent1': 'Members of the procession walk down the street holding small horn brass instruments.', + 'sent2': 'A drum line', + 'startphrase': 'Members of the procession walk down the street holding small horn brass instruments. A drum line', + 'video-id': 'anetv_jkn6uvmqwh4'} +``` + +While it looks like there are a lot of fields here, it is actually pretty straightforward: + +- `sent1` and `sent2`: these fields show how a sentence starts, and if you put the two together, you get the `startphrase` field. +- `ending`: suggests a possible ending for how a sentence can end, but only one of them is correct. +- `label`: identifies the correct sentence ending. + +## Preprocess + +The next step is to load a BERT tokenizer to process the sentence starts and the four possible endings: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") +``` + +The preprocessing function you want to create needs to: + +1. Make four copies of the `sent1` field and combine each of them with `sent2` to recreate how a sentence starts. +2. Combine `sent2` with each of the four possible sentence endings. +3. Flatten these two lists so you can tokenize them, and then unflatten them afterward so each example has a corresponding `input_ids`, `attention_mask`, and `labels` field. + +```py +>>> ending_names = ["ending0", "ending1", "ending2", "ending3"] + + +>>> def preprocess_function(examples): +... first_sentences = [[context] * 4 for context in examples["sent1"]] +... question_headers = examples["sent2"] +... second_sentences = [ +... [f"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(question_headers) +... ] + +... first_sentences = sum(first_sentences, []) +... second_sentences = sum(second_sentences, []) + +... tokenized_examples = tokenizer(first_sentences, second_sentences, truncation=True) +... return {k: [v[i : i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()} +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] method. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once: + +```py +>>> tokenized_swag = swag.map(preprocess_function, batched=True) +``` + +To create a batch of examples, it's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. [`DataCollatorForMultipleChoice`] flattens all the model inputs, applies padding, and then unflattens the results. +```py +>>> from transformers import DataCollatorForMultipleChoice +>>> collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +``` + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the accuracy: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load BERT with [`AutoModelForMultipleChoice`]: + +```py +>>> from transformers import AutoModelForMultipleChoice, TrainingArguments, Trainer + +>>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the accuracy and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_swag_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... load_best_model_at_end=True, +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_swag["train"], +... eval_dataset=tokenized_swag["validation"], +... processing_class=tokenizer, +... data_collator=collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_train_epochs = 2 +>>> total_train_steps = (len(tokenized_swag["train"]) // batch_size) * num_train_epochs +>>> optimizer, schedule = create_optimizer(init_lr=5e-5, num_warmup_steps=0, num_train_steps=total_train_steps) +``` + +Then you can load BERT with [`TFAutoModelForMultipleChoice`]: + +```py +>>> from transformers import TFAutoModelForMultipleChoice + +>>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> data_collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_swag["train"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_swag["validation"], +... shuffle=False, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +The last two things to setup before you start training is to compute the accuracy from the predictions, and provide a way to push your model to the Hub. Both are done by using [Keras callbacks](../main_classes/keras_callbacks). + +Pass your `compute_metrics` function to [`~transformers.KerasMetricCallback`]: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_model", +... tokenizer=tokenizer, +... ) +``` + +Then bundle your callbacks together: + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callbacks to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=2, callbacks=callbacks) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + + +For a more in-depth example of how to finetune a model for multiple choice, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with some text and two candidate answers: + +```py +>>> prompt = "France has a bread law, Le Dรฉcret Pain, with strict rules on what is allowed in a traditional baguette." +>>> candidate1 = "The law does not apply to croissants and brioche." +>>> candidate2 = "The law applies to baguettes." +``` + + + +Tokenize each prompt and candidate answer pair and return PyTorch tensors. You should also create some `labels`: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_swag_model") +>>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="pt", padding=True) +>>> labels = torch.tensor(0).unsqueeze(0) +``` + +Pass your inputs and labels to the model and return the `logits`: + +```py +>>> from transformers import AutoModelForMultipleChoice + +>>> model = AutoModelForMultipleChoice.from_pretrained("username/my_awesome_swag_model") +>>> outputs = model(**{k: v.unsqueeze(0) for k, v in inputs.items()}, labels=labels) +>>> logits = outputs.logits +``` + +Get the class with the highest probability: + +```py +>>> predicted_class = logits.argmax().item() +>>> predicted_class +0 +``` + + +Tokenize each prompt and candidate answer pair and return TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_swag_model") +>>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="tf", padding=True) +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import TFAutoModelForMultipleChoice + +>>> model = TFAutoModelForMultipleChoice.from_pretrained("username/my_awesome_swag_model") +>>> inputs = {k: tf.expand_dims(v, 0) for k, v in inputs.items()} +>>> outputs = model(inputs) +>>> logits = outputs.logits +``` + +Get the class with the highest probability: + +```py +>>> predicted_class = int(tf.math.argmax(logits, axis=-1)[0]) +>>> predicted_class +0 +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/object_detection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/object_detection.md new file mode 100644 index 0000000000000000000000000000000000000000..394e77104b74c764fca4476aa3bb9af6b7c7958a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/object_detection.md @@ -0,0 +1,1540 @@ + + +# Object detection + +[[open-in-colab]] + +Object detection is the computer vision task of detecting instances (such as humans, buildings, or cars) in an image. Object detection models receive an image as input and output +coordinates of the bounding boxes and associated labels of the detected objects. An image can contain multiple objects, +each with its own bounding box and a label (e.g. it can have a car and a building), and each object can +be present in different parts of an image (e.g. the image can have several cars). +This task is commonly used in autonomous driving for detecting things like pedestrians, road signs, and traffic lights. +Other applications include counting objects in images, image search, and more. + +In this guide, you will learn how to: + + 1. Finetune [DETR](https://huggingface.co/docs/transformers/model_doc/detr), a model that combines a convolutional + backbone with an encoder-decoder Transformer, on the [CPPE-5](https://huggingface.co/datasets/cppe-5) + dataset. + 2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/object-detection) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install -q datasets transformers accelerate timm +pip install -q -U albumentations>=1.4.5 torchmetrics pycocotools +``` + +You'll use ๐Ÿค— Datasets to load a dataset from the Hugging Face Hub, ๐Ÿค— Transformers to train your model, +and `albumentations` to augment the data. + +We encourage you to share your model with the community. Log in to your Hugging Face account to upload it to the Hub. +When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +To get started, we'll define global constants, namely the model name and image size. For this tutorial, we'll use the conditional DETR model due to its faster convergence. Feel free to select any object detection model available in the `transformers` library. + +```py +>>> MODEL_NAME = "microsoft/conditional-detr-resnet-50" # or "facebook/detr-resnet-50" +>>> IMAGE_SIZE = 480 +``` + +## Load the CPPE-5 dataset + +The [CPPE-5 dataset](https://huggingface.co/datasets/cppe-5) contains images with +annotations identifying medical personal protective equipment (PPE) in the context of the COVID-19 pandemic. + +Start by loading the dataset and creating a `validation` split from `train`: + +```py +>>> from datasets import load_dataset + +>>> cppe5 = load_dataset("cppe-5") + +>>> if "validation" not in cppe5: +... split = cppe5["train"].train_test_split(0.15, seed=1337) +... cppe5["train"] = split["train"] +... cppe5["validation"] = split["test"] + +>>> cppe5 +DatasetDict({ + train: Dataset({ + features: ['image_id', 'image', 'width', 'height', 'objects'], + num_rows: 850 + }) + test: Dataset({ + features: ['image_id', 'image', 'width', 'height', 'objects'], + num_rows: 29 + }) + validation: Dataset({ + features: ['image_id', 'image', 'width', 'height', 'objects'], + num_rows: 150 + }) +}) +``` + +You'll see that this dataset has 1000 images for train and validation sets and a test set with 29 images. + +To get familiar with the data, explore what the examples look like. + +```py +>>> cppe5["train"][0] +{ + 'image_id': 366, + 'image': , + 'width': 500, + 'height': 500, + 'objects': { + 'id': [1932, 1933, 1934], + 'area': [27063, 34200, 32431], + 'bbox': [[29.0, 11.0, 97.0, 279.0], + [201.0, 1.0, 120.0, 285.0], + [382.0, 0.0, 113.0, 287.0]], + 'category': [0, 0, 0] + } +} +``` + +The examples in the dataset have the following fields: +- `image_id`: the example image id +- `image`: a `PIL.Image.Image` object containing the image +- `width`: width of the image +- `height`: height of the image +- `objects`: a dictionary containing bounding box metadata for the objects in the image: + - `id`: the annotation id + - `area`: the area of the bounding box + - `bbox`: the object's bounding box (in the [COCO format](https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/#coco) ) + - `category`: the object's category, with possible values including `Coverall (0)`, `Face_Shield (1)`, `Gloves (2)`, `Goggles (3)` and `Mask (4)` + +You may notice that the `bbox` field follows the COCO format, which is the format that the DETR model expects. +However, the grouping of the fields inside `objects` differs from the annotation format DETR requires. You will +need to apply some preprocessing transformations before using this data for training. + +To get an even better understanding of the data, visualize an example in the dataset. + +```py +>>> import numpy as np +>>> import os +>>> from PIL import Image, ImageDraw + +>>> image = cppe5["train"][2]["image"] +>>> annotations = cppe5["train"][2]["objects"] +>>> draw = ImageDraw.Draw(image) + +>>> categories = cppe5["train"].features["objects"]["category"].feature.names + +>>> id2label = {index: x for index, x in enumerate(categories, start=0)} +>>> label2id = {v: k for k, v in id2label.items()} + +>>> for i in range(len(annotations["id"])): +... box = annotations["bbox"][i] +... class_idx = annotations["category"][i] +... x, y, w, h = tuple(box) +... # Check if coordinates are normalized or not +... if max(box) > 1.0: +... # Coordinates are un-normalized, no need to re-scale them +... x1, y1 = int(x), int(y) +... x2, y2 = int(x + w), int(y + h) +... else: +... # Coordinates are normalized, re-scale them +... x1 = int(x * width) +... y1 = int(y * height) +... x2 = int((x + w) * width) +... y2 = int((y + h) * height) +... draw.rectangle((x, y, x + w, y + h), outline="red", width=1) +... draw.text((x, y), id2label[class_idx], fill="white") + +>>> image +``` +
+ CPPE-5 Image Example +
+ + +To visualize the bounding boxes with associated labels, you can get the labels from the dataset's metadata, specifically +the `category` field. +You'll also want to create dictionaries that map a label id to a label class (`id2label`) and the other way around (`label2id`). +You can use them later when setting up the model. Including these maps will make your model reusable by others if you share +it on the Hugging Face Hub. Please note that, the part of above code that draws the bounding boxes assume that it is in `COCO` format `(x_min, y_min, width, height)`. It has to be adjusted to work for other formats like `(x_min, y_min, x_max, y_max)`. + +As a final step of getting familiar with the data, explore it for potential issues. One common problem with datasets for +object detection is bounding boxes that "stretch" beyond the edge of the image. Such "runaway" bounding boxes can raise +errors during training and should be addressed. There are a few examples with this issue in this dataset. +To keep things simple in this guide, we will set `clip=True` for `BboxParams` in transformations below. + +## Preprocess the data + +To finetune a model, you must preprocess the data you plan to use to match precisely the approach used for the pre-trained model. +[`AutoImageProcessor`] takes care of processing image data to create `pixel_values`, `pixel_mask`, and +`labels` that a DETR model can train with. The image processor has some attributes that you won't have to worry about: + +- `image_mean = [0.485, 0.456, 0.406 ]` +- `image_std = [0.229, 0.224, 0.225]` + +These are the mean and standard deviation used to normalize images during the model pre-training. These values are crucial +to replicate when doing inference or finetuning a pre-trained image model. + +Instantiate the image processor from the same checkpoint as the model you want to finetune. + +```py +>>> from transformers import AutoImageProcessor + +>>> MAX_SIZE = IMAGE_SIZE + +>>> image_processor = AutoImageProcessor.from_pretrained( +... MODEL_NAME, +... do_resize=True, +... size={"max_height": MAX_SIZE, "max_width": MAX_SIZE}, +... do_pad=True, +... pad_size={"height": MAX_SIZE, "width": MAX_SIZE}, +... ) +``` + +Before passing the images to the `image_processor`, apply two preprocessing transformations to the dataset: +- Augmenting images +- Reformatting annotations to meet DETR expectations + +First, to make sure the model does not overfit on the training data, you can apply image augmentation with any data augmentation library. Here we use [Albumentations](https://albumentations.ai/docs/). +This library ensures that transformations affect the image and update the bounding boxes accordingly. +The ๐Ÿค— Datasets library documentation has a detailed [guide on how to augment images for object detection](https://huggingface.co/docs/datasets/object_detection), +and it uses the exact same dataset as an example. Apply some geometric and color transformations to the image. For additional augmentation options, explore the [Albumentations Demo Space](https://huggingface.co/spaces/qubvel-hf/albumentations-demo). + +```py +>>> import albumentations as A + +>>> train_augment_and_transform = A.Compose( +... [ +... A.Perspective(p=0.1), +... A.HorizontalFlip(p=0.5), +... A.RandomBrightnessContrast(p=0.5), +... A.HueSaturationValue(p=0.1), +... ], +... bbox_params=A.BboxParams(format="coco", label_fields=["category"], clip=True, min_area=25), +... ) + +>>> validation_transform = A.Compose( +... [A.NoOp()], +... bbox_params=A.BboxParams(format="coco", label_fields=["category"], clip=True), +... ) +``` + +The `image_processor` expects the annotations to be in the following format: `{'image_id': int, 'annotations': list[Dict]}`, + where each dictionary is a COCO object annotation. Let's add a function to reformat annotations for a single example: + +```py +>>> def format_image_annotations_as_coco(image_id, categories, areas, bboxes): +... """Format one set of image annotations to the COCO format + +... Args: +... image_id (str): image id. e.g. "0001" +... categories (list[int]): list of categories/class labels corresponding to provided bounding boxes +... areas (list[float]): list of corresponding areas to provided bounding boxes +... bboxes (list[tuple[float]]): list of bounding boxes provided in COCO format +... ([center_x, center_y, width, height] in absolute coordinates) + +... Returns: +... dict: { +... "image_id": image id, +... "annotations": list of formatted annotations +... } +... """ +... annotations = [] +... for category, area, bbox in zip(categories, areas, bboxes): +... formatted_annotation = { +... "image_id": image_id, +... "category_id": category, +... "iscrowd": 0, +... "area": area, +... "bbox": list(bbox), +... } +... annotations.append(formatted_annotation) + +... return { +... "image_id": image_id, +... "annotations": annotations, +... } + +``` + +Now you can combine the image and annotation transformations to use on a batch of examples: + +```py +>>> def augment_and_transform_batch(examples, transform, image_processor, return_pixel_mask=False): +... """Apply augmentations and format annotations in COCO format for object detection task""" + +... images = [] +... annotations = [] +... for image_id, image, objects in zip(examples["image_id"], examples["image"], examples["objects"]): +... image = np.array(image.convert("RGB")) + +... # apply augmentations +... output = transform(image=image, bboxes=objects["bbox"], category=objects["category"]) +... images.append(output["image"]) + +... # format annotations in COCO format +... formatted_annotations = format_image_annotations_as_coco( +... image_id, output["category"], objects["area"], output["bboxes"] +... ) +... annotations.append(formatted_annotations) + +... # Apply the image processor transformations: resizing, rescaling, normalization +... result = image_processor(images=images, annotations=annotations, return_tensors="pt") + +... if not return_pixel_mask: +... result.pop("pixel_mask", None) + +... return result +``` + +Apply this preprocessing function to the entire dataset using ๐Ÿค— Datasets [`~datasets.Dataset.with_transform`] method. This method applies +transformations on the fly when you load an element of the dataset. + +At this point, you can check what an example from the dataset looks like after the transformations. You should see a tensor +with `pixel_values`, a tensor with `pixel_mask`, and `labels`. + +```py +>>> from functools import partial + +>>> # Make transform functions for batch and apply for dataset splits +>>> train_transform_batch = partial( +... augment_and_transform_batch, transform=train_augment_and_transform, image_processor=image_processor +... ) +>>> validation_transform_batch = partial( +... augment_and_transform_batch, transform=validation_transform, image_processor=image_processor +... ) + +>>> cppe5["train"] = cppe5["train"].with_transform(train_transform_batch) +>>> cppe5["validation"] = cppe5["validation"].with_transform(validation_transform_batch) +>>> cppe5["test"] = cppe5["test"].with_transform(validation_transform_batch) + +>>> cppe5["train"][15] +{'pixel_values': tensor([[[ 1.9235, 1.9407, 1.9749, ..., -0.7822, -0.7479, -0.6965], + [ 1.9578, 1.9749, 1.9920, ..., -0.7993, -0.7650, -0.7308], + [ 2.0092, 2.0092, 2.0263, ..., -0.8507, -0.8164, -0.7822], + ..., + [ 0.0741, 0.0741, 0.0741, ..., 0.0741, 0.0741, 0.0741], + [ 0.0741, 0.0741, 0.0741, ..., 0.0741, 0.0741, 0.0741], + [ 0.0741, 0.0741, 0.0741, ..., 0.0741, 0.0741, 0.0741]], + + [[ 1.6232, 1.6408, 1.6583, ..., 0.8704, 1.0105, 1.1331], + [ 1.6408, 1.6583, 1.6758, ..., 0.8529, 0.9930, 1.0980], + [ 1.6933, 1.6933, 1.7108, ..., 0.8179, 0.9580, 1.0630], + ..., + [ 0.2052, 0.2052, 0.2052, ..., 0.2052, 0.2052, 0.2052], + [ 0.2052, 0.2052, 0.2052, ..., 0.2052, 0.2052, 0.2052], + [ 0.2052, 0.2052, 0.2052, ..., 0.2052, 0.2052, 0.2052]], + + [[ 1.8905, 1.9080, 1.9428, ..., -0.1487, -0.0964, -0.0615], + [ 1.9254, 1.9428, 1.9603, ..., -0.1661, -0.1138, -0.0790], + [ 1.9777, 1.9777, 1.9951, ..., -0.2010, -0.1138, -0.0790], + ..., + [ 0.4265, 0.4265, 0.4265, ..., 0.4265, 0.4265, 0.4265], + [ 0.4265, 0.4265, 0.4265, ..., 0.4265, 0.4265, 0.4265], + [ 0.4265, 0.4265, 0.4265, ..., 0.4265, 0.4265, 0.4265]]]), + 'labels': {'image_id': tensor([688]), 'class_labels': tensor([3, 4, 2, 0, 0]), 'boxes': tensor([[0.4700, 0.1933, 0.1467, 0.0767], + [0.4858, 0.2600, 0.1150, 0.1000], + [0.4042, 0.4517, 0.1217, 0.1300], + [0.4242, 0.3217, 0.3617, 0.5567], + [0.6617, 0.4033, 0.5400, 0.4533]]), 'area': tensor([ 4048., 4140., 5694., 72478., 88128.]), 'iscrowd': tensor([0, 0, 0, 0, 0]), 'orig_size': tensor([480, 480])}} +``` + +You have successfully augmented the individual images and prepared their annotations. However, preprocessing isn't +complete yet. In the final step, create a custom `collate_fn` to batch images together. +Pad images (which are now `pixel_values`) to the largest image in a batch, and create a corresponding `pixel_mask` +to indicate which pixels are real (1) and which are padding (0). + +```py +>>> import torch + +>>> def collate_fn(batch): +... data = {} +... data["pixel_values"] = torch.stack([x["pixel_values"] for x in batch]) +... data["labels"] = [x["labels"] for x in batch] +... if "pixel_mask" in batch[0]: +... data["pixel_mask"] = torch.stack([x["pixel_mask"] for x in batch]) +... return data + +``` + +## Preparing function to compute mAP + +Object detection models are commonly evaluated with a set of COCO-style metrics. We are going to use `torchmetrics` to compute `mAP` (mean average precision) and `mAR` (mean average recall) metrics and will wrap it to `compute_metrics` function in order to use in [`Trainer`] for evaluation. + +Intermediate format of boxes used for training is `YOLO` (normalized) but we will compute metrics for boxes in `Pascal VOC` (absolute) format in order to correctly handle box areas. Let's define a function that converts bounding boxes to `Pascal VOC` format: + +```py +>>> from transformers.image_transforms import center_to_corners_format + +>>> def convert_bbox_yolo_to_pascal(boxes, image_size): +... """ +... Convert bounding boxes from YOLO format (x_center, y_center, width, height) in range [0, 1] +... to Pascal VOC format (x_min, y_min, x_max, y_max) in absolute coordinates. + +... Args: +... boxes (torch.Tensor): Bounding boxes in YOLO format +... image_size (tuple[int, int]): Image size in format (height, width) + +... Returns: +... torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max) +... """ +... # convert center to corners format +... boxes = center_to_corners_format(boxes) + +... # convert to absolute coordinates +... height, width = image_size +... boxes = boxes * torch.tensor([[width, height, width, height]]) + +... return boxes +``` + +Then, in `compute_metrics` function we collect `predicted` and `target` bounding boxes, scores and labels from evaluation loop results and pass it to the scoring function. + +```py +>>> import numpy as np +>>> from dataclasses import dataclass +>>> from torchmetrics.detection.mean_ap import MeanAveragePrecision + + +>>> @dataclass +>>> class ModelOutput: +... logits: torch.Tensor +... pred_boxes: torch.Tensor + + +>>> @torch.no_grad() +>>> def compute_metrics(evaluation_results, image_processor, threshold=0.0, id2label=None): +... """ +... Compute mean average mAP, mAR and their variants for the object detection task. + +... Args: +... evaluation_results (EvalPrediction): Predictions and targets from evaluation. +... threshold (float, optional): Threshold to filter predicted boxes by confidence. Defaults to 0.0. +... id2label (Optional[dict], optional): Mapping from class id to class name. Defaults to None. + +... Returns: +... Mapping[str, float]: Metrics in a form of dictionary {: } +... """ + +... predictions, targets = evaluation_results.predictions, evaluation_results.label_ids + +... # For metric computation we need to provide: +... # - targets in a form of list of dictionaries with keys "boxes", "labels" +... # - predictions in a form of list of dictionaries with keys "boxes", "scores", "labels" + +... image_sizes = [] +... post_processed_targets = [] +... post_processed_predictions = [] + +... # Collect targets in the required format for metric computation +... for batch in targets: +... # collect image sizes, we will need them for predictions post processing +... batch_image_sizes = torch.tensor(np.array([x["orig_size"] for x in batch])) +... image_sizes.append(batch_image_sizes) +... # collect targets in the required format for metric computation +... # boxes were converted to YOLO format needed for model training +... # here we will convert them to Pascal VOC format (x_min, y_min, x_max, y_max) +... for image_target in batch: +... boxes = torch.tensor(image_target["boxes"]) +... boxes = convert_bbox_yolo_to_pascal(boxes, image_target["orig_size"]) +... labels = torch.tensor(image_target["class_labels"]) +... post_processed_targets.append({"boxes": boxes, "labels": labels}) + +... # Collect predictions in the required format for metric computation, +... # model produce boxes in YOLO format, then image_processor convert them to Pascal VOC format +... for batch, target_sizes in zip(predictions, image_sizes): +... batch_logits, batch_boxes = batch[1], batch[2] +... output = ModelOutput(logits=torch.tensor(batch_logits), pred_boxes=torch.tensor(batch_boxes)) +... post_processed_output = image_processor.post_process_object_detection( +... output, threshold=threshold, target_sizes=target_sizes +... ) +... post_processed_predictions.extend(post_processed_output) + +... # Compute metrics +... metric = MeanAveragePrecision(box_format="xyxy", class_metrics=True) +... metric.update(post_processed_predictions, post_processed_targets) +... metrics = metric.compute() + +... # Replace list of per class metrics with separate metric for each class +... classes = metrics.pop("classes") +... map_per_class = metrics.pop("map_per_class") +... mar_100_per_class = metrics.pop("mar_100_per_class") +... for class_id, class_map, class_mar in zip(classes, map_per_class, mar_100_per_class): +... class_name = id2label[class_id.item()] if id2label is not None else class_id.item() +... metrics[f"map_{class_name}"] = class_map +... metrics[f"mar_100_{class_name}"] = class_mar + +... metrics = {k: round(v.item(), 4) for k, v in metrics.items()} + +... return metrics + + +>>> eval_compute_metrics_fn = partial( +... compute_metrics, image_processor=image_processor, id2label=id2label, threshold=0.0 +... ) +``` + +## Training the detection model + +You have done most of the heavy lifting in the previous sections, so now you are ready to train your model! +The images in this dataset are still quite large, even after resizing. This means that finetuning this model will +require at least one GPU. + +Training involves the following steps: +1. Load the model with [`AutoModelForObjectDetection`] using the same checkpoint as in the preprocessing. +2. Define your training hyperparameters in [`TrainingArguments`]. +3. Pass the training arguments to [`Trainer`] along with the model, dataset, image processor, and data collator. +4. Call [`~Trainer.train`] to finetune your model. + +When loading the model from the same checkpoint that you used for the preprocessing, remember to pass the `label2id` +and `id2label` maps that you created earlier from the dataset's metadata. Additionally, we specify `ignore_mismatched_sizes=True` to replace the existing classification head with a new one. + +```py +>>> from transformers import AutoModelForObjectDetection + +>>> model = AutoModelForObjectDetection.from_pretrained( +... MODEL_NAME, +... id2label=id2label, +... label2id=label2id, +... ignore_mismatched_sizes=True, +... ) +``` + +In the [`TrainingArguments`] use `output_dir` to specify where to save your model, then configure hyperparameters as you see fit. For `num_train_epochs=30` training will take about 35 minutes in Google Colab T4 GPU, increase the number of epoch to get better results. + +Important notes: + - Do not remove unused columns because this will drop the image column. Without the image column, you +can't create `pixel_values`. For this reason, set `remove_unused_columns` to `False`. + - Set `eval_do_concat_batches=False` to get proper evaluation results. Images have different number of target boxes, if batches are concatenated we will not be able to determine which boxes belongs to particular image. + +If you wish to share your model by pushing to the Hub, set `push_to_hub` to `True` (you must be signed in to Hugging +Face to upload your model). + +```py +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments( +... output_dir="detr_finetuned_cppe5", +... num_train_epochs=30, +... fp16=False, +... per_device_train_batch_size=8, +... dataloader_num_workers=4, +... learning_rate=5e-5, +... lr_scheduler_type="cosine", +... weight_decay=1e-4, +... max_grad_norm=0.01, +... metric_for_best_model="eval_map", +... greater_is_better=True, +... load_best_model_at_end=True, +... eval_strategy="epoch", +... save_strategy="epoch", +... save_total_limit=2, +... remove_unused_columns=False, +... eval_do_concat_batches=False, +... push_to_hub=True, +... ) +``` + +Finally, bring everything together, and call [`~transformers.Trainer.train`]: + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=cppe5["train"], +... eval_dataset=cppe5["validation"], +... processing_class=image_processor, +... data_collator=collate_fn, +... compute_metrics=eval_compute_metrics_fn, +... ) + +>>> trainer.train() +``` +
+ + + [3210/3210 26:07, Epoch 30/30] +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
EpochTraining LossValidation LossMapMap 50Map 75Map SmallMap MediumMap LargeMar 1Mar 10Mar 100Mar SmallMar MediumMar LargeMap CoverallMar 100 CoverallMap Face ShieldMar 100 Face ShieldMap GlovesMar 100 GlovesMap GogglesMar 100 GogglesMap MaskMar 100 Mask
1No log2.6299030.0089000.0232000.0065000.0013000.0028000.0205000.0215000.0704000.1014000.0076000.1062000.0961000.0367000.2320000.0003000.0190000.0039000.1254000.0001000.0031000.0035000.127600
2No log3.4798640.0148000.0346000.0108000.0086000.0117000.0125000.0411000.0987000.1300000.0560000.0622000.1119000.0535000.4473000.0106000.1000000.0002000.0228000.0001000.0154000.0097000.064400
3No log2.1076220.0417000.0940000.0343000.0241000.0264000.0474000.0915000.1828000.2258000.0872000.1994000.2106000.1509000.5712000.0173000.1013000.0073000.1804000.0021000.0262000.0310000.250200
4No log2.0312420.0559000.1206000.0469000.0138000.0381000.0903000.1059000.2256000.2661000.1302000.2281000.3300000.1910000.5721000.0106000.1570000.0146000.2353000.0017000.0523000.0618000.313800
53.8894001.8834330.0897000.2018000.0673000.0228000.0653000.1295000.1360000.2722000.3037000.1129000.3125000.4246000.3002000.5851000.0327000.2025000.0313000.2710000.0087000.1262000.0755000.333800
63.8894001.8075030.1185000.2709000.0902000.0349000.0767000.1525000.1461000.2978000.3254000.1717000.2837000.5459000.3969000.5545000.0430000.2620000.0545000.2719000.0203000.2308000.0776000.308000
73.8894001.7161690.1435000.3077000.1232000.0458000.0978000.2583000.1653000.3277000.3526000.1409000.3367000.5994000.4429000.6207000.0694000.3013000.0816000.2920000.0110000.2308000.1127000.318200
83.8894001.6790140.1530000.3558000.1279000.0387000.1156000.2916000.1760000.3225000.3497000.1356000.3261000.6437000.4317000.5829000.0698000.2658000.0886000.2746000.0283000.2800000.1467000.345300
93.8894001.6182390.1721000.3753000.1376000.0461000.1417000.3085000.1940000.3562000.3862000.1624000.3592000.6777000.4698000.6239000.1021000.3177000.0991000.2902000.0293000.3354000.1602000.364000
101.5997001.5725120.1795000.4004000.1472000.0565000.1417000.3167000.2131000.3576000.3813000.1979000.3443000.6385000.4669000.6239000.1013000.3114000.1047000.2795000.0516000.3385000.1730000.353300
111.5997001.5288890.1922000.4150000.1608000.0537000.1505000.3780000.2115000.3717000.3978000.2049000.3746000.6848000.4919000.6324000.1312000.3468000.1220000.3009000.0384000.3446000.1775000.364400
121.5997001.5175320.1983000.4298000.1598000.0664000.1629000.3833000.2207000.3821000.4054000.2148000.3832000.6729000.4690000.6104000.1678000.3797000.1197000.3071000.0381000.3354000.1968000.394200
131.5997001.4888490.2098000.4523000.1723000.0949000.1711000.4378000.2220000.3798000.4115000.2038000.3973000.7075000.4707000.6207000.1869000.4076000.1242000.3067000.0593000.3554000.2077000.367100
141.5997001.4822100.2289000.4826000.1878000.0836000.1918000.4441000.2259000.3769000.4074000.1825000.3848000.7006000.5121000.6401000.1750000.3633000.1443000.3000000.0831000.3631000.2299000.370700
151.3268001.4751980.2163000.4556000.1749000.0885000.1835000.4244000.2269000.3734000.4043000.1992000.3964000.6778000.4963000.6338000.1663000.3924000.1289000.3129000.0852000.3123000.2050000.370200
161.3268001.4596970.2332000.5042000.1922000.0960000.2020000.4308000.2391000.3824000.4126000.2195000.4031000.6704000.4852000.6252000.1965000.4101000.1357000.2996000.1231000.3569000.2253000.371100
171.3268001.4073400.2434000.5119000.2045000.1210000.2157000.4680000.2462000.3946000.4242000.2259000.4161000.7052000.4949000.6383000.2249000.4304000.1572000.3179000.1157000.3692000.2242000.365300
181.3268001.4195220.2451000.5215000.2100000.1161000.2115000.4899000.2554000.3916000.4197000.1988000.4212000.7014000.5018000.6342000.2267000.4101000.1544000.3214000.1059000.3523000.2367000.380400
191.1586001.3987640.2536000.5192000.2136000.1352000.2077000.4919000.2573000.3973000.4280000.2414000.4018000.7035000.5097000.6311000.2367000.4418000.1559000.3308000.1281000.3523000.2375000.384000
201.1586001.3905910.2488000.5202000.2166000.1275000.2114000.4719000.2583000.4070000.4291000.2403000.4076000.7085000.5058000.6234000.2355000.4316000.1500000.3250000.1257000.3754000.2272000.390200
211.1586001.3606080.2627000.5448000.2221000.1347000.2300000.4875000.2695000.4133000.4363000.2362000.4191000.7093000.5141000.6374000.2572000.4506000.1651000.3384000.1394000.3723000.2377000.382700
221.1586001.3682960.2628000.5424000.2364000.1374000.2281000.4985000.2665000.4090000.4330000.2399000.4185000.6975000.5205000.6410000.2575000.4557000.1626000.3348000.1402000.3538000.2332000.379600
231.1586001.3681760.2648000.5411000.2331000.1382000.2239000.4987000.2723000.4074000.4344000.2331000.4183000.7020000.5244000.6423000.2623000.4443000.1597000.3353000.1405000.3662000.2369000.384000
241.0497001.3552710.2697000.5492000.2391000.1347000.2299000.5192000.2748000.4127000.4376000.2454000.4172000.7112000.5232000.6441000.2721000.4405000.1667000.3415000.1377000.3738000.2490000.388000
251.0497001.3551800.2725000.5479000.2438000.1497000.2299000.5231000.2725000.4157000.4422000.2562000.4202000.7058000.5239000.6396000.2717000.4519000.1663000.3469000.1537000.3831000.2470000.389300
261.0497001.3493370.2756000.5563000.2464000.1467000.2348000.5163000.2742000.4183000.4409000.2487000.4189000.7058000.5232000.6365000.2747000.4405000.1724000.3491000.1556000.3846000.2523000.393800
271.0497001.3507820.2752000.5487000.2468000.1473000.2364000.5272000.2801000.4162000.4426000.2534000.4240000.7103000.5266000.6401000.2732000.4456000.1670000.3469000.1601000.3877000.2492000.392900
281.0497001.3465330.2770000.5528000.2529000.1474000.2400000.5276000.2809000.4209000.4441000.2555000.4245000.7112000.5302000.6468000.2774000.4418000.1709000.3469000.1566000.3892000.2496000.396000
290.9937001.3465750.2771000.5548000.2529000.1484000.2397000.5236000.2784000.4200000.4433000.2563000.4240000.7056000.5296000.6473000.2739000.4392000.1743000.3487000.1576000.3862000.2501000.395100
300.9937001.3464460.2774000.5547000.2527000.1479000.2408000.5236000.2788000.4204000.4433000.2561000.4242000.7055000.5301000.6468000.2756000.4405000.1745000.3487000.1573000.3862000.2492000.394200

+ +If you have set `push_to_hub` to `True` in the `training_args`, the training checkpoints are pushed to the +Hugging Face Hub. Upon training completion, push the final model to the Hub as well by calling the [`~transformers.Trainer.push_to_hub`] method. + +```py +>>> trainer.push_to_hub() +``` + +## Evaluate + +```py +>>> from pprint import pprint + +>>> metrics = trainer.evaluate(eval_dataset=cppe5["test"], metric_key_prefix="test") +>>> pprint(metrics) +{'epoch': 30.0, + 'test_loss': 1.0877351760864258, + 'test_map': 0.4116, + 'test_map_50': 0.741, + 'test_map_75': 0.3663, + 'test_map_Coverall': 0.5937, + 'test_map_Face_Shield': 0.5863, + 'test_map_Gloves': 0.3416, + 'test_map_Goggles': 0.1468, + 'test_map_Mask': 0.3894, + 'test_map_large': 0.5637, + 'test_map_medium': 0.3257, + 'test_map_small': 0.3589, + 'test_mar_1': 0.323, + 'test_mar_10': 0.5237, + 'test_mar_100': 0.5587, + 'test_mar_100_Coverall': 0.6756, + 'test_mar_100_Face_Shield': 0.7294, + 'test_mar_100_Gloves': 0.4721, + 'test_mar_100_Goggles': 0.4125, + 'test_mar_100_Mask': 0.5038, + 'test_mar_large': 0.7283, + 'test_mar_medium': 0.4901, + 'test_mar_small': 0.4469, + 'test_runtime': 1.6526, + 'test_samples_per_second': 17.548, + 'test_steps_per_second': 2.42} +``` + +These results can be further improved by adjusting the hyperparameters in [`TrainingArguments`]. Give it a go! + +## Inference + +Now that you have finetuned a model, evaluated it, and uploaded it to the Hugging Face Hub, you can use it for inference. + +```py +>>> import torch +>>> import requests + +>>> from PIL import Image, ImageDraw +>>> from transformers import AutoImageProcessor, AutoModelForObjectDetection + +>>> url = "https://images.pexels.com/photos/8413299/pexels-photo-8413299.jpeg?auto=compress&cs=tinysrgb&w=630&h=375&dpr=2" +>>> image = Image.open(requests.get(url, stream=True).raw) +``` + +Load model and image processor from the Hugging Face Hub (skip to use already trained in this session): +```py +>>> from transformers import infer_device + +>>> device = infer_device() +>>> model_repo = "qubvel-hf/detr_finetuned_cppe5" + +>>> image_processor = AutoImageProcessor.from_pretrained(model_repo) +>>> model = AutoModelForObjectDetection.from_pretrained(model_repo) +>>> model = model.to(device) +``` + +And detect bounding boxes: + +```py + +>>> with torch.no_grad(): +... inputs = image_processor(images=[image], return_tensors="pt") +... outputs = model(**inputs.to(device)) +... target_sizes = torch.tensor([[image.size[1], image.size[0]]]) +... results = image_processor.post_process_object_detection(outputs, threshold=0.3, target_sizes=target_sizes)[0] + +>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): +... box = [round(i, 2) for i in box.tolist()] +... print( +... f"Detected {model.config.id2label[label.item()]} with confidence " +... f"{round(score.item(), 3)} at location {box}" +... ) +Detected Gloves with confidence 0.683 at location [244.58, 124.33, 300.35, 185.13] +Detected Mask with confidence 0.517 at location [143.73, 64.58, 219.57, 125.89] +Detected Gloves with confidence 0.425 at location [179.15, 155.57, 262.4, 226.35] +Detected Coverall with confidence 0.407 at location [307.13, -1.18, 477.82, 318.06] +Detected Coverall with confidence 0.391 at location [68.61, 126.66, 309.03, 318.89] +``` + +Let's plot the result: + +```py +>>> draw = ImageDraw.Draw(image) + +>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): +... box = [round(i, 2) for i in box.tolist()] +... x, y, x2, y2 = tuple(box) +... draw.rectangle((x, y, x2, y2), outline="red", width=1) +... draw.text((x, y), model.config.id2label[label.item()], fill="white") + +>>> image +``` + +

+ Object detection result on a new image +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/prompting.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/prompting.md new file mode 100644 index 0000000000000000000000000000000000000000..635b1fefa4189d8db19e2c53d1f73f8fdd66853e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/prompting.md @@ -0,0 +1,267 @@ + + +# Prompt engineering + +[[open-in-colab]] + +Prompt engineering or prompting, uses natural language to improve large language model (LLM) performance on a variety of tasks. A prompt can steer the model towards generating a desired output. In many cases, you don't even need a [fine-tuned](#finetuning) model for a task. You just need a good prompt. + +Try prompting a LLM to classify some text. When you create a prompt, it's important to provide very specific instructions about the task and what the result should look like. + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(task="text-generation", model="mistralai/Mistal-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Classify the text into neutral, negative or positive. +Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. +Sentiment: +""" + +outputs = pipeline(prompt, max_new_tokens=10) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: Classify the text into neutral, negative or positive. +Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. +Sentiment: +Positive +``` + +The challenge lies in designing prompts that produces the results you're expecting because language is so incredibly nuanced and expressive. + +This guide covers prompt engineering best practices, techniques, and examples for how to solve language and reasoning tasks. + +## Best practices + +1. Try to pick the latest models for the best performance. Keep in mind that LLMs can come in two variants, [base](https://hf.co/mistralai/Mistral-7B-v0.1) and [instruction-tuned](https://hf.co/mistralai/Mistral-7B-Instruct-v0.1) (or chat). + + Base models are excellent at completing text given an initial prompt, but they're not as good at following instructions. Instruction-tuned models are specifically trained versions of the base models on instructional or conversational data. This makes instruction-tuned models a better fit for prompting. + + > [!WARNING] + > Modern LLMs are typically decoder-only models, but there are some encoder-decoder LLMs like [Flan-T5](../model_doc/flan-t5) or [BART](../model_doc/bart) that may be used for prompting. For encoder-decoder models, make sure you set the pipeline task identifier to `text2text-generation` instead of `text-generation`. + +2. Start with a short and simple prompt, and iterate on it to get better results. + +3. Put instructions at the beginning or end of a prompt. For longer prompts, models may apply optimizations to prevent attention from scaling quadratically, which places more emphasis at the beginning and end of a prompt. + +4. Clearly separate instructions from the text of interest. + +5. Be specific and descriptive about the task and the desired output, including for example, its format, length, style, and language. Avoid ambiguous descriptions and instructions. + +6. Instructions should focus on "what to do" rather than "what not to do". + +7. Lead the model to generate the correct output by writing the first word or even the first sentence. + +8. Try other techniques like [few-shot](#few-shot) and [chain-of-thought](#chain-of-thought) to improve results. + +9. Test your prompts with different models to assess their robustness. + +10. Version and track your prompt performance. + +## Techniques + +Crafting a good prompt alone, also known as zero-shot prompting, may not be enough to get the results you want. You may need to try a few prompting techniques to get the best performance. + +This section covers a few prompting techniques. + +### Few-shot prompting + +Few-shot prompting improves accuracy and performance by including specific examples of what a model should generate given an input. The explicit examples give the model a better understanding of the task and the output format youโ€™re looking for. Try experimenting with different numbers of examples (2, 4, 8, etc.) to see how it affects performance. The example below provides the model with 1 example (1-shot) of the output format (a date in MM/DD/YYYY format) it should return. + +```python +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961. +Date: 04/12/1961 +Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. +Date:""" + +outputs = pipeline(prompt, max_new_tokens=12, do_sample=True, top_k=10) +for output in outputs: + print(f"Result: {output['generated_text']}") +# Result: Text: The first human went into space and orbited the Earth on April 12, 1961. +# Date: 04/12/1961 +# Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. +# Date: 09/28/1960 +``` + +The downside of few-shot prompting is that you need to create lengthier prompts which increases computation and latency. There is also a limit to prompt lengths. Finally, a model can learn unintended patterns from your examples, and it may not work well on complex reasoning tasks. + +To improve few-shot prompting for modern instruction-tuned LLMs, use a model's specific [chat template](../conversations). These models are trained on datasets with turn-based conversations between a "user" and "assistant". Structuring your prompt to align with this can improve performance. + +Structure your prompt as a turn-based conversation and use the [`apply_chat_template`] method to tokenize and format it. + +```python +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") + +messages = [ + {"role": "user", "content": "Text: The first human went into space and orbited the Earth on April 12, 1961."}, + {"role": "assistant", "content": "Date: 04/12/1961"}, + {"role": "user", "content": "Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon."} +] + +prompt = pipeline.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) + +outputs = pipeline(prompt, max_new_tokens=12, do_sample=True, top_k=10) + +for output in outputs: + print(f"Result: {output['generated_text']}") +``` + + +While the basic few-shot prompting approach embedded examples within a single text string, the chat template format offers the following benefits. + +- The model may have a potentially improved understanding because it can better recognize the pattern and the expected roles of user input and assistant output. +- The model may more consistently output the desired output format because it is structured like its input during training. + +Always consult a specific instruction-tuned model's documentation to learn more about the format of their chat template so that you can structure your few-shot prompts accordingly. + +### Chain-of-thought + +Chain-of-thought (CoT) is effective at generating more coherent and well-reasoned outputs by providing a series of prompts that help a model "think" more thoroughly about a topic. + +The example below provides the model with several prompts to work through intermediate reasoning steps. + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Let's go through this step-by-step: +1. You start with 15 muffins. +2. You eat 2 muffins, leaving you with 13 muffins. +3. You give 5 muffins to your neighbor, leaving you with 8 muffins. +4. Your partner buys 6 more muffins, bringing the total number of muffins to 14. +5. Your partner eats 2 muffins, leaving you with 12 muffins. +If you eat 6 muffins, how many are left?""" + +outputs = pipeline(prompt, max_new_tokens=20, do_sample=True, top_k=10) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: Let's go through this step-by-step: +1. You start with 15 muffins. +2. You eat 2 muffins, leaving you with 13 muffins. +3. You give 5 muffins to your neighbor, leaving you with 8 muffins. +4. Your partner buys 6 more muffins, bringing the total number of muffins to 14. +5. Your partner eats 2 muffins, leaving you with 12 muffins. +If you eat 6 muffins, how many are left? +Answer: 6 +``` + +Like [few-shot](#few-shot) prompting, the downside of CoT is that it requires more effort to design a series of prompts that help the model reason through a complex task and prompt length increases latency. + +## Fine-tuning + +While prompting is a powerful way to work with LLMs, there are scenarios where a fine-tuned model or even fine-tuning a model works better. + +Here are some examples scenarios where a fine-tuned model makes sense. + +- Your domain is extremely different from what a LLM was pretrained on, and extensive prompting didn't produce the results you want. +- Your model needs to work well in a low-resource language. +- Your model needs to be trained on sensitive data that have strict regulatory requirements. +- You're using a small model due to cost, privacy, infrastructure, or other constraints. + +In all of these scenarios, ensure that you have a large enough domain-specific dataset to train your model with, have enough time and resources, and the cost of fine-tuning is worth it. Otherwise, you may be better off trying to optimize your prompt. + +## Examples + +The examples below demonstrate prompting a LLM for different tasks. + + + + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Return a list of named entities in the text. +Text: The company was founded in 2016 by French entrepreneurs Clรฉment Delangue, Julien Chaumond, and Thomas Wolf in New York City, originally as a company that developed a chatbot app targeted at teenagers. +Named entities: +""" + +outputs = pipeline(prompt, max_new_tokens=50, return_full_text=False) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: [Clรฉment Delangue, Julien Chaumond, Thomas Wolf, company, New York City, chatbot app, teenagers] +``` + + + + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Translate the English text to French. +Text: Sometimes, I've believed as many as six impossible things before breakfast. +Translation: +""" + +outputs = pipeline(prompt, max_new_tokens=20, do_sample=True, top_k=10, return_full_text=False) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: ร€ l'occasion, j'ai croyu plus de six choses impossibles +``` + + + + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Permaculture is a design process mimicking the diversity, functionality and resilience of natural ecosystems. The principles and practices are drawn from traditional ecological knowledge of indigenous cultures combined with modern scientific understanding and technological innovations. Permaculture design provides a framework helping individuals and communities develop innovative, creative and effective strategies for meeting basic needs while preparing for and mitigating the projected impacts of climate change. +Write a summary of the above text. +Summary: +""" + +outputs = pipeline(prompt, max_new_tokens=30, do_sample=True, top_k=10, return_full_text=False) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: Permaculture is the design process that involves mimicking natural ecosystems to provide sustainable solutions to basic needs. It is a holistic approach that comb +``` + + + + +```py +from transformers import pipeline +import torch + +pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto") +prompt = """Answer the question using the context below. +Context: Gazpacho is a cold soup and drink made of raw, blended vegetables. Most gazpacho includes stale bread, tomato, cucumbers, onion, bell peppers, garlic, olive oil, wine vinegar, water, and salt. Northern recipes often include cumin and/or pimentรณn (smoked sweet paprika). Traditionally, gazpacho was made by pounding the vegetables in a mortar with a pestle; this more laborious method is still sometimes used as it helps keep the gazpacho cool and avoids the foam and silky consistency of smoothie versions made in blenders or food processors. +Question: What modern tool is used to make gazpacho? +Answer: +""" + +outputs = pipeline(prompt, max_new_tokens=10, do_sample=True, top_k=10, return_full_text=False) +for output in outputs: + print(f"Result: {output['generated_text']}") +Result: A blender or food processor is the modern tool +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..41d7fd48cf816e099892c7bfa3037825721f97eb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/question_answering.md @@ -0,0 +1,427 @@ + + +# Question answering + +[[open-in-colab]] + + + +Question answering tasks return an answer given a question. If you've ever asked a virtual assistant like Alexa, Siri or Google what the weather is, then you've used a question answering model before. There are two common types of question answering tasks: + +- Extractive: extract the answer from the given context. +- Abstractive: generate an answer from the context that correctly answers the question. + +This guide will show you how to: + +1. Finetune [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on the [SQuAD](https://huggingface.co/datasets/squad) dataset for extractive question answering. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/question-answering) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load SQuAD dataset + +Start by loading a smaller subset of the SQuAD dataset from the ๐Ÿค— Datasets library. This'll give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset + +>>> squad = load_dataset("squad", split="train[:5000]") +``` + +Split the dataset's `train` split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> squad = squad.train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> squad["train"][0] +{'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']}, + 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', + 'id': '5733be284776f41900661182', + 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', + 'title': 'University_of_Notre_Dame' +} +``` + +There are several important fields here: + +- `answers`: the starting location of the answer token and the answer text. +- `context`: background information from which the model needs to extract the answer. +- `question`: the question a model should answer. + +## Preprocess + + + +The next step is to load a DistilBERT tokenizer to process the `question` and `context` fields: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +There are a few preprocessing steps particular to question answering tasks you should be aware of: + +1. Some examples in a dataset may have a very long `context` that exceeds the maximum input length of the model. To deal with longer sequences, truncate only the `context` by setting `truncation="only_second"`. +2. Next, map the start and end positions of the answer to the original `context` by setting + `return_offset_mapping=True`. +3. With the mapping in hand, now you can find the start and end tokens of the answer. Use the [`~tokenizers.Encoding.sequence_ids`] method to + find which part of the offset corresponds to the `question` and which corresponds to the `context`. + +Here is how you can create a function to truncate and map the start and end tokens of the `answer` to the `context`: + +```py +>>> def preprocess_function(examples): +... questions = [q.strip() for q in examples["question"]] +... inputs = tokenizer( +... questions, +... examples["context"], +... max_length=384, +... truncation="only_second", +... return_offsets_mapping=True, +... padding="max_length", +... ) + +... offset_mapping = inputs.pop("offset_mapping") +... answers = examples["answers"] +... start_positions = [] +... end_positions = [] + +... for i, offset in enumerate(offset_mapping): +... answer = answers[i] +... start_char = answer["answer_start"][0] +... end_char = answer["answer_start"][0] + len(answer["text"][0]) +... sequence_ids = inputs.sequence_ids(i) + +... # Find the start and end of the context +... idx = 0 +... while sequence_ids[idx] != 1: +... idx += 1 +... context_start = idx +... while sequence_ids[idx] == 1: +... idx += 1 +... context_end = idx - 1 + +... # If the answer is not fully inside the context, label it (0, 0) +... if offset[context_start][0] > end_char or offset[context_end][1] < start_char: +... start_positions.append(0) +... end_positions.append(0) +... else: +... # Otherwise it's the start and end token positions +... idx = context_start +... while idx <= context_end and offset[idx][0] <= start_char: +... idx += 1 +... start_positions.append(idx - 1) + +... idx = context_end +... while idx >= context_start and offset[idx][1] >= end_char: +... idx -= 1 +... end_positions.append(idx + 1) + +... inputs["start_positions"] = start_positions +... inputs["end_positions"] = end_positions +... return inputs +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once. Remove any columns you don't need: + +```py +>>> tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names) +``` + +Now create a batch of examples using [`DefaultDataCollator`]. Unlike other data collators in ๐Ÿค— Transformers, the [`DefaultDataCollator`] does not apply any additional preprocessing such as padding. + + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load DistilBERT with [`AutoModelForQuestionAnswering`]: + +```py +>>> from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer + +>>> model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, and data collator. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_qa_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_squad["train"], +... eval_dataset=tokenized_squad["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 2 +>>> total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs +>>> optimizer, schedule = create_optimizer( +... init_lr=2e-5, +... num_warmup_steps=0, +... num_train_steps=total_train_steps, +... ) +``` + +Then you can load DistilBERT with [`TFAutoModelForQuestionAnswering`]: + +```py +>>> from transformers import TFAutoModelForQuestionAnswering + +>>> model = TFAutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_squad["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_squad["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +The last thing to setup before you start training is to provide a way to push your model to the Hub. This can be done by specifying where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_qa_model", +... tokenizer=tokenizer, +... ) +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callback to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=[callback]) +``` +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for question answering, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb). + + + +## Evaluate + +Evaluation for question answering requires a significant amount of postprocessing. To avoid taking up too much of your time, this guide skips the evaluation step. The [`Trainer`] still calculates the evaluation loss during training so you're not completely in the dark about your model's performance. + +If you have more time and you're interested in how to evaluate your model for question answering, take a look at the [Question answering](https://huggingface.co/course/chapter7/7?fw=pt#post-processing) chapter from the ๐Ÿค— Hugging Face Course! + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with a question and some context you'd like the model to predict: + +```py +>>> question = "How many programming languages does BLOOM support?" +>>> context = "BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for question answering with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +>>> question_answerer = pipeline("question-answering", model="my_awesome_qa_model") +>>> question_answerer(question=question, context=context) +{'score': 0.2058267742395401, + 'start': 10, + 'end': 95, + 'answer': '176 billion parameters and can generate text in 46 languages natural languages and 13'} +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Tokenize the text and return PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") +>>> inputs = tokenizer(question, context, return_tensors="pt") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> import torch +>>> from transformers import AutoModelForQuestionAnswering + +>>> model = AutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") +>>> with torch.no_grad(): +... outputs = model(**inputs) +``` + +Get the highest probability from the model output for the start and end positions: + +```py +>>> answer_start_index = outputs.start_logits.argmax() +>>> answer_end_index = outputs.end_logits.argmax() +``` + +Decode the predicted tokens to get the answer: + +```py +>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] +>>> tokenizer.decode(predict_answer_tokens) +'176 billion parameters and can generate text in 46 languages natural languages and 13' +``` + + +Tokenize the text and return TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") +>>> inputs = tokenizer(question, context, return_tensors="tf") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import TFAutoModelForQuestionAnswering + +>>> model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") +>>> outputs = model(**inputs) +``` + +Get the highest probability from the model output for the start and end positions: + +```py +>>> answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0]) +>>> answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0]) +``` + +Decode the predicted tokens to get the answer: + +```py +>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] +>>> tokenizer.decode(predict_answer_tokens) +'176 billion parameters and can generate text in 46 languages natural languages and 13' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/semantic_segmentation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/semantic_segmentation.md new file mode 100644 index 0000000000000000000000000000000000000000..3bede1a448beeb0db30e22a6cf9c189b0e78a15e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/semantic_segmentation.md @@ -0,0 +1,938 @@ + + +# Image Segmentation + +[[open-in-colab]] + + + +Image segmentation models separate areas corresponding to different areas of interest in an image. These models work by assigning a label to each pixel. There are several types of segmentation: semantic segmentation, instance segmentation, and panoptic segmentation. + +In this guide, we will: +1. [Take a look at different types of segmentation](#types-of-segmentation). +2. [Have an end-to-end fine-tuning example for semantic segmentation](#fine-tuning-a-model-for-segmentation). + +Before you begin, make sure you have all the necessary libraries installed: + +```py +# uncomment to install the necessary libraries +!pip install -q datasets transformers evaluate accelerate +``` + +We encourage you to log in to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Types of Segmentation + +Semantic segmentation assigns a label or class to every single pixel in an image. Let's take a look at a semantic segmentation model output. It will assign the same class to every instance of an object it comes across in an image, for example, all cats will be labeled as "cat" instead of "cat-1", "cat-2". +We can use transformers' image segmentation pipeline to quickly infer a semantic segmentation model. Let's take a look at the example image. + +```python +from transformers import pipeline +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/segmentation_input.jpg" +image = Image.open(requests.get(url, stream=True).raw) +image +``` + +
+ Segmentation Input +
+ +We will use [nvidia/segformer-b1-finetuned-cityscapes-1024-1024](https://huggingface.co/nvidia/segformer-b1-finetuned-cityscapes-1024-1024). + +```python +semantic_segmentation = pipeline("image-segmentation", "nvidia/segformer-b1-finetuned-cityscapes-1024-1024") +results = semantic_segmentation(image) +results +``` + +The segmentation pipeline output includes a mask for every predicted class. +```bash +[{'score': None, + 'label': 'road', + 'mask': }, + {'score': None, + 'label': 'sidewalk', + 'mask': }, + {'score': None, + 'label': 'building', + 'mask': }, + {'score': None, + 'label': 'wall', + 'mask': }, + {'score': None, + 'label': 'pole', + 'mask': }, + {'score': None, + 'label': 'traffic sign', + 'mask': }, + {'score': None, + 'label': 'vegetation', + 'mask': }, + {'score': None, + 'label': 'terrain', + 'mask': }, + {'score': None, + 'label': 'sky', + 'mask': }, + {'score': None, + 'label': 'car', + 'mask': }] +``` + +Taking a look at the mask for the car class, we can see every car is classified with the same mask. + +```python +results[-1]["mask"] +``` +
+ Semantic Segmentation Output +
+ +In instance segmentation, the goal is not to classify every pixel, but to predict a mask for **every instance of an object** in a given image. It works very similar to object detection, where there is a bounding box for every instance, there's a segmentation mask instead. We will use [facebook/mask2former-swin-large-cityscapes-instance](https://huggingface.co/facebook/mask2former-swin-large-cityscapes-instance) for this. + +```python +instance_segmentation = pipeline("image-segmentation", "facebook/mask2former-swin-large-cityscapes-instance") +results = instance_segmentation(image) +results +``` + +As you can see below, there are multiple cars classified, and there's no classification for pixels other than pixels that belong to car and person instances. + +```bash +[{'score': 0.999944, + 'label': 'car', + 'mask': }, + {'score': 0.999945, + 'label': 'car', + 'mask': }, + {'score': 0.999652, + 'label': 'car', + 'mask': }, + {'score': 0.903529, + 'label': 'person', + 'mask': }] +``` +Checking out one of the car masks below. + +```python +results[2]["mask"] +``` +
+ Semantic Segmentation Output +
+ +Panoptic segmentation combines semantic segmentation and instance segmentation, where every pixel is classified into a class and an instance of that class, and there are multiple masks for each instance of a class. We can use [facebook/mask2former-swin-large-cityscapes-panoptic](https://huggingface.co/facebook/mask2former-swin-large-cityscapes-panoptic) for this. + +```python +panoptic_segmentation = pipeline("image-segmentation", "facebook/mask2former-swin-large-cityscapes-panoptic") +results = panoptic_segmentation(image) +results +``` +As you can see below, we have more classes. We will later illustrate to see that every pixel is classified into one of the classes. + +```bash +[{'score': 0.999981, + 'label': 'car', + 'mask': }, + {'score': 0.999958, + 'label': 'car', + 'mask': }, + {'score': 0.99997, + 'label': 'vegetation', + 'mask': }, + {'score': 0.999575, + 'label': 'pole', + 'mask': }, + {'score': 0.999958, + 'label': 'building', + 'mask': }, + {'score': 0.999634, + 'label': 'road', + 'mask': }, + {'score': 0.996092, + 'label': 'sidewalk', + 'mask': }, + {'score': 0.999221, + 'label': 'car', + 'mask': }, + {'score': 0.99987, + 'label': 'sky', + 'mask': }] +``` + +Let's have a side by side comparison for all types of segmentation. + +
+ Segmentation Maps Compared +
+ +Seeing all types of segmentation, let's have a deep dive on fine-tuning a model for semantic segmentation. + +Common real-world applications of semantic segmentation include training self-driving cars to identify pedestrians and important traffic information, identifying cells and abnormalities in medical imagery, and monitoring environmental changes from satellite imagery. + +## Fine-tuning a Model for Segmentation + +We will now: + +1. Finetune [SegFormer](https://huggingface.co/docs/transformers/main/en/model_doc/segformer#segformer) on the [SceneParse150](https://huggingface.co/datasets/scene_parse_150) dataset. +2. Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/image-segmentation) + + + + +### Load SceneParse150 dataset + +Start by loading a smaller subset of the SceneParse150 dataset from the ๐Ÿค— Datasets library. This'll give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from datasets import load_dataset + +>>> ds = load_dataset("scene_parse_150", split="train[:50]") +``` + +Split the dataset's `train` split into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> ds = ds.train_test_split(test_size=0.2) +>>> train_ds = ds["train"] +>>> test_ds = ds["test"] +``` + +Then take a look at an example: + +```py +>>> train_ds[0] +{'image': , + 'annotation': , + 'scene_category': 368} + +# view the image +>>> train_ds[0]["image"] +``` + +- `image`: a PIL image of the scene. +- `annotation`: a PIL image of the segmentation map, which is also the model's target. +- `scene_category`: a category id that describes the image scene like "kitchen" or "office". In this guide, you'll only need `image` and `annotation`, both of which are PIL images. + +You'll also want to create a dictionary that maps a label id to a label class which will be useful when you set up the model later. Download the mappings from the Hub and create the `id2label` and `label2id` dictionaries: + +```py +>>> import json +>>> from pathlib import Path +>>> from huggingface_hub import hf_hub_download + +>>> repo_id = "huggingface/label-files" +>>> filename = "ade20k-id2label.json" +>>> id2label = json.loads(Path(hf_hub_download(repo_id, filename, repo_type="dataset")).read_text()) +>>> id2label = {int(k): v for k, v in id2label.items()} +>>> label2id = {v: k for k, v in id2label.items()} +>>> num_labels = len(id2label) +``` + +#### Custom dataset + +You could also create and use your own dataset if you prefer to train with the [run_semantic_segmentation.py](https://github.com/huggingface/transformers/blob/main/examples/pytorch/semantic-segmentation/run_semantic_segmentation.py) script instead of a notebook instance. The script requires: + +1. a [`~datasets.DatasetDict`] with two [`~datasets.Image`] columns, "image" and "label" + + ```py + from datasets import Dataset, DatasetDict, Image + + image_paths_train = ["path/to/image_1.jpg/jpg", "path/to/image_2.jpg/jpg", ..., "path/to/image_n.jpg/jpg"] + label_paths_train = ["path/to/annotation_1.png", "path/to/annotation_2.png", ..., "path/to/annotation_n.png"] + + image_paths_validation = [...] + label_paths_validation = [...] + + def create_dataset(image_paths, label_paths): + dataset = Dataset.from_dict({"image": sorted(image_paths), + "label": sorted(label_paths)}) + dataset = dataset.cast_column("image", Image()) + dataset = dataset.cast_column("label", Image()) + return dataset + + # step 1: create Dataset objects + train_dataset = create_dataset(image_paths_train, label_paths_train) + validation_dataset = create_dataset(image_paths_validation, label_paths_validation) + + # step 2: create DatasetDict + dataset = DatasetDict({ + "train": train_dataset, + "validation": validation_dataset, + } + ) + + # step 3: push to Hub (assumes you have ran the hf auth login command in a terminal/notebook) + dataset.push_to_hub("your-name/dataset-repo") + + # optionally, you can push to a private repo on the Hub + # dataset.push_to_hub("name of repo on the hub", private=True) + ``` + +2. an id2label dictionary mapping the class integers to their class names + + ```py + import json + # simple example + id2label = {0: 'cat', 1: 'dog'} + with open('id2label.json', 'w') as fp: + json.dump(id2label, fp) + ``` + +As an example, take a look at this [example dataset](https://huggingface.co/datasets/nielsr/ade20k-demo) which was created with the steps shown above. + +### Preprocess + +The next step is to load a SegFormer image processor to prepare the images and annotations for the model. Some datasets, like this one, use the zero-index as the background class. However, the background class isn't actually included in the 150 classes, so you'll need to set `do_reduce_labels=True` to subtract one from all the labels. The zero-index is replaced by `255` so it's ignored by SegFormer's loss function: + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "nvidia/mit-b0" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint, do_reduce_labels=True) +``` + + + + +It is common to apply some data augmentations to an image dataset to make a model more robust against overfitting. In this guide, you'll use the [`ColorJitter`](https://pytorch.org/vision/stable/generated/torchvision.transforms.ColorJitter.html) function from [torchvision](https://pytorch.org/vision/stable/index.html) to randomly change the color properties of an image, but you can also use any image library you like. + +```py +>>> from torchvision.transforms import ColorJitter + +>>> jitter = ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25, hue=0.1) +``` + +Now create two preprocessing functions to prepare the images and annotations for the model. These functions convert the images into `pixel_values` and annotations to `labels`. For the training set, `jitter` is applied before providing the images to the image processor. For the test set, the image processor crops and normalizes the `images`, and only crops the `labels` because no data augmentation is applied during testing. + +```py +>>> def train_transforms(example_batch): +... images = [jitter(x) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs + + +>>> def val_transforms(example_batch): +... images = [x for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs +``` + +To apply the `jitter` over the entire dataset, use the ๐Ÿค— Datasets [`~datasets.Dataset.set_transform`] function. The transform is applied on the fly which is faster and consumes less disk space: + +```py +>>> train_ds.set_transform(train_transforms) +>>> test_ds.set_transform(val_transforms) +``` + + + + + + +It is common to apply some data augmentations to an image dataset to make a model more robust against overfitting. +In this guide, you'll use [`tf.image`](https://www.tensorflow.org/api_docs/python/tf/image) to randomly change the color properties of an image, but you can also use any image +library you like. +Define two separate transformation functions: +- training data transformations that include image augmentation +- validation data transformations that only transpose the images, since computer vision models in ๐Ÿค— Transformers expect channels-first layout + +```py +>>> import tensorflow as tf + + +>>> def aug_transforms(image): +... image = tf.keras.utils.img_to_array(image) +... image = tf.image.random_brightness(image, 0.25) +... image = tf.image.random_contrast(image, 0.5, 2.0) +... image = tf.image.random_saturation(image, 0.75, 1.25) +... image = tf.image.random_hue(image, 0.1) +... image = tf.transpose(image, (2, 0, 1)) +... return image + + +>>> def transforms(image): +... image = tf.keras.utils.img_to_array(image) +... image = tf.transpose(image, (2, 0, 1)) +... return image +``` + +Next, create two preprocessing functions to prepare batches of images and annotations for the model. These functions apply +the image transformations and use the earlier loaded `image_processor` to convert the images into `pixel_values` and +annotations to `labels`. `ImageProcessor` also takes care of resizing and normalizing the images. + +```py +>>> def train_transforms(example_batch): +... images = [aug_transforms(x.convert("RGB")) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs + + +>>> def val_transforms(example_batch): +... images = [transforms(x.convert("RGB")) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs +``` + +To apply the preprocessing transformations over the entire dataset, use the ๐Ÿค— Datasets [`~datasets.Dataset.set_transform`] function. +The transform is applied on the fly which is faster and consumes less disk space: + +```py +>>> train_ds.set_transform(train_transforms) +>>> test_ds.set_transform(val_transforms) +``` + + + +### Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load an evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [mean Intersection over Union](https://huggingface.co/spaces/evaluate-metric/accuracy) (IoU) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> metric = evaluate.load("mean_iou") +``` + +Then create a function to [`~evaluate.EvaluationModule.compute`] the metrics. Your predictions need to be converted to +logits first, and then reshaped to match the size of the labels before you can call [`~evaluate.EvaluationModule.compute`]: + + + + +```py +>>> import numpy as np +>>> import torch +>>> from torch import nn + +>>> def compute_metrics(eval_pred): +... with torch.no_grad(): +... logits, labels = eval_pred +... logits_tensor = torch.from_numpy(logits) +... logits_tensor = nn.functional.interpolate( +... logits_tensor, +... size=labels.shape[-2:], +... mode="bilinear", +... align_corners=False, +... ).argmax(dim=1) + +... pred_labels = logits_tensor.detach().cpu().numpy() +... metrics = metric.compute( +... predictions=pred_labels, +... references=labels, +... num_labels=num_labels, +... ignore_index=255, +... reduce_labels=False, +... ) +... for key, value in metrics.items(): +... if isinstance(value, np.ndarray): +... metrics[key] = value.tolist() +... return metrics +``` + + + + + + + + +```py +>>> def compute_metrics(eval_pred): +... logits, labels = eval_pred +... logits = tf.transpose(logits, perm=[0, 2, 3, 1]) +... logits_resized = tf.image.resize( +... logits, +... size=tf.shape(labels)[1:], +... method="bilinear", +... ) + +... pred_labels = tf.argmax(logits_resized, axis=-1) +... metrics = metric.compute( +... predictions=pred_labels, +... references=labels, +... num_labels=num_labels, +... ignore_index=-1, +... reduce_labels=image_processor.do_reduce_labels, +... ) + +... per_category_accuracy = metrics.pop("per_category_accuracy").tolist() +... per_category_iou = metrics.pop("per_category_iou").tolist() + +... metrics.update({f"accuracy_{id2label[i]}": v for i, v in enumerate(per_category_accuracy)}) +... metrics.update({f"iou_{id2label[i]}": v for i, v in enumerate(per_category_iou)}) +... return {"val_" + k: v for k, v in metrics.items()} +``` + + + + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +### Train + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#finetune-with-trainer)! + + + +You're ready to start training your model now! Load SegFormer with [`AutoModelForSemanticSegmentation`], and pass the model the mapping between label ids and label classes: + +```py +>>> from transformers import AutoModelForSemanticSegmentation, TrainingArguments, Trainer + +>>> model = AutoModelForSemanticSegmentation.from_pretrained(checkpoint, id2label=id2label, label2id=label2id) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. It is important you don't remove unused columns because this'll drop the `image` column. Without the `image` column, you can't create `pixel_values`. Set `remove_unused_columns=False` to prevent this behavior! The only other required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the IoU metric and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="segformer-b0-scene-parse-150", +... learning_rate=6e-5, +... num_train_epochs=50, +... per_device_train_batch_size=2, +... per_device_eval_batch_size=2, +... save_total_limit=3, +... eval_strategy="steps", +... save_strategy="steps", +... save_steps=20, +... eval_steps=20, +... logging_steps=1, +... eval_accumulation_steps=5, +... remove_unused_columns=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=train_ds, +... eval_dataset=test_ds, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + + + + +If you are unfamiliar with fine-tuning a model with Keras, check out the [basic tutorial](./training#train-a-tensorflow-model-with-keras) first! + + + +To fine-tune a model in TensorFlow, follow these steps: +1. Define the training hyperparameters, and set up an optimizer and a learning rate schedule. +2. Instantiate a pretrained model. +3. Convert a ๐Ÿค— Dataset to a `tf.data.Dataset`. +4. Compile your model. +5. Add callbacks to calculate metrics and upload your model to ๐Ÿค— Hub +6. Use the `fit()` method to run the training. + +Start by defining the hyperparameters, optimizer and learning rate schedule: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 2 +>>> num_epochs = 50 +>>> num_train_steps = len(train_ds) * num_epochs +>>> learning_rate = 6e-5 +>>> weight_decay_rate = 0.01 + +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=learning_rate, +... num_train_steps=num_train_steps, +... weight_decay_rate=weight_decay_rate, +... num_warmup_steps=0, +... ) +``` + +Then, load SegFormer with [`TFAutoModelForSemanticSegmentation`] along with the label mappings, and compile it with the +optimizer. Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> from transformers import TFAutoModelForSemanticSegmentation + +>>> model = TFAutoModelForSemanticSegmentation.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ) +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +Convert your datasets to the `tf.data.Dataset` format using the [`~datasets.Dataset.to_tf_dataset`] and the [`DefaultDataCollator`]: + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") + +>>> tf_train_dataset = train_ds.to_tf_dataset( +... columns=["pixel_values", "label"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) + +>>> tf_eval_dataset = test_ds.to_tf_dataset( +... columns=["pixel_values", "label"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) +``` + +To compute the accuracy from the predictions and push your model to the ๐Ÿค— Hub, use [Keras callbacks](../main_classes/keras_callbacks). +Pass your `compute_metrics` function to [`KerasMetricCallback`], +and use the [`PushToHubCallback`] to upload the model: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback + +>>> metric_callback = KerasMetricCallback( +... metric_fn=compute_metrics, eval_dataset=tf_eval_dataset, batch_size=batch_size, label_cols=["labels"] +... ) + +>>> push_to_hub_callback = PushToHubCallback(output_dir="scene_segmentation", tokenizer=image_processor) + +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you are ready to train your model! Call `fit()` with your training and validation datasets, the number of epochs, +and your callbacks to fine-tune the model: + +```py +>>> model.fit( +... tf_train_dataset, +... validation_data=tf_eval_dataset, +... callbacks=callbacks, +... epochs=num_epochs, +... ) +``` + +Congratulations! You have fine-tuned your model and shared it on the ๐Ÿค— Hub. You can now use it for inference! + + + +### Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Reload the dataset and load an image for inference. + +```py +>>> from datasets import load_dataset + +>>> ds = load_dataset("scene_parse_150", split="train[:50]") +>>> ds = ds.train_test_split(test_size=0.2) +>>> test_ds = ds["test"] +>>> image = ds["test"][0]["image"] +>>> image +``` + +
+ Image of bedroom +
+ + + + +We will now see how to infer without a pipeline. Process the image with an image processor and place the `pixel_values` on a GPU: + +```py +>>> from transformers import infer_device +>>> device = infer_device() +>>> encoding = image_processor(image, return_tensors="pt") +>>> pixel_values = encoding.pixel_values.to(device) +``` + +Pass your input to the model and return the `logits`: + +```py +>>> outputs = model(pixel_values=pixel_values) +>>> logits = outputs.logits.cpu() +``` + +Next, rescale the logits to the original image size: + +```py +>>> upsampled_logits = nn.functional.interpolate( +... logits, +... size=image.size[::-1], +... mode="bilinear", +... align_corners=False, +... ) + +>>> pred_seg = upsampled_logits.argmax(dim=1)[0] +``` + + + + + + +Load an image processor to preprocess the image and return the input as TensorFlow tensors: + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("MariaK/scene_segmentation") +>>> inputs = image_processor(image, return_tensors="tf") +``` + +Pass your input to the model and return the `logits`: + +```py +>>> from transformers import TFAutoModelForSemanticSegmentation + +>>> model = TFAutoModelForSemanticSegmentation.from_pretrained("MariaK/scene_segmentation") +>>> logits = model(**inputs).logits +``` + +Next, rescale the logits to the original image size and apply argmax on the class dimension: +```py +>>> logits = tf.transpose(logits, [0, 2, 3, 1]) + +>>> upsampled_logits = tf.image.resize( +... logits, +... # We reverse the shape of `image` because `image.size` returns width and height. +... image.size[::-1], +... ) + +>>> pred_seg = tf.math.argmax(upsampled_logits, axis=-1)[0] +``` + + + + +To visualize the results, load the [dataset color palette](https://github.com/tensorflow/models/blob/3f1ca33afe3c1631b733ea7e40c294273b9e406d/research/deeplab/utils/get_dataset_colormap.py#L51) as `ade_palette()` that maps each class to their RGB values. + +```py +def ade_palette(): + return np.asarray([ + [0, 0, 0], + [120, 120, 120], + [180, 120, 120], + [6, 230, 230], + [80, 50, 50], + [4, 200, 3], + [120, 120, 80], + [140, 140, 140], + [204, 5, 255], + [230, 230, 230], + [4, 250, 7], + [224, 5, 255], + [235, 255, 7], + [150, 5, 61], + [120, 120, 70], + [8, 255, 51], + [255, 6, 82], + [143, 255, 140], + [204, 255, 4], + [255, 51, 7], + [204, 70, 3], + [0, 102, 200], + [61, 230, 250], + [255, 6, 51], + [11, 102, 255], + [255, 7, 71], + [255, 9, 224], + [9, 7, 230], + [220, 220, 220], + [255, 9, 92], + [112, 9, 255], + [8, 255, 214], + [7, 255, 224], + [255, 184, 6], + [10, 255, 71], + [255, 41, 10], + [7, 255, 255], + [224, 255, 8], + [102, 8, 255], + [255, 61, 6], + [255, 194, 7], + [255, 122, 8], + [0, 255, 20], + [255, 8, 41], + [255, 5, 153], + [6, 51, 255], + [235, 12, 255], + [160, 150, 20], + [0, 163, 255], + [140, 140, 140], + [250, 10, 15], + [20, 255, 0], + [31, 255, 0], + [255, 31, 0], + [255, 224, 0], + [153, 255, 0], + [0, 0, 255], + [255, 71, 0], + [0, 235, 255], + [0, 173, 255], + [31, 0, 255], + [11, 200, 200], + [255, 82, 0], + [0, 255, 245], + [0, 61, 255], + [0, 255, 112], + [0, 255, 133], + [255, 0, 0], + [255, 163, 0], + [255, 102, 0], + [194, 255, 0], + [0, 143, 255], + [51, 255, 0], + [0, 82, 255], + [0, 255, 41], + [0, 255, 173], + [10, 0, 255], + [173, 255, 0], + [0, 255, 153], + [255, 92, 0], + [255, 0, 255], + [255, 0, 245], + [255, 0, 102], + [255, 173, 0], + [255, 0, 20], + [255, 184, 184], + [0, 31, 255], + [0, 255, 61], + [0, 71, 255], + [255, 0, 204], + [0, 255, 194], + [0, 255, 82], + [0, 10, 255], + [0, 112, 255], + [51, 0, 255], + [0, 194, 255], + [0, 122, 255], + [0, 255, 163], + [255, 153, 0], + [0, 255, 10], + [255, 112, 0], + [143, 255, 0], + [82, 0, 255], + [163, 255, 0], + [255, 235, 0], + [8, 184, 170], + [133, 0, 255], + [0, 255, 92], + [184, 0, 255], + [255, 0, 31], + [0, 184, 255], + [0, 214, 255], + [255, 0, 112], + [92, 255, 0], + [0, 224, 255], + [112, 224, 255], + [70, 184, 160], + [163, 0, 255], + [153, 0, 255], + [71, 255, 0], + [255, 0, 163], + [255, 204, 0], + [255, 0, 143], + [0, 255, 235], + [133, 255, 0], + [255, 0, 235], + [245, 0, 255], + [255, 0, 122], + [255, 245, 0], + [10, 190, 212], + [214, 255, 0], + [0, 204, 255], + [20, 0, 255], + [255, 255, 0], + [0, 153, 255], + [0, 41, 255], + [0, 255, 204], + [41, 0, 255], + [41, 255, 0], + [173, 0, 255], + [0, 245, 255], + [71, 0, 255], + [122, 0, 255], + [0, 255, 184], + [0, 92, 255], + [184, 255, 0], + [0, 133, 255], + [255, 214, 0], + [25, 194, 194], + [102, 255, 0], + [92, 0, 255], + ]) +``` + +Then you can combine and plot your image and the predicted segmentation map: + +```py +>>> import matplotlib.pyplot as plt +>>> import numpy as np + +>>> color_seg = np.zeros((pred_seg.shape[0], pred_seg.shape[1], 3), dtype=np.uint8) +>>> palette = np.array(ade_palette()) +>>> for label, color in enumerate(palette): +... color_seg[pred_seg == label, :] = color +>>> color_seg = color_seg[..., ::-1] # convert to BGR + +>>> img = np.array(image) * 0.5 + color_seg * 0.5 # plot the image with the segmentation map +>>> img = img.astype(np.uint8) + +>>> plt.figure(figsize=(15, 10)) +>>> plt.imshow(img) +>>> plt.show() +``` + +
+ Image of bedroom overlaid with segmentation map +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/sequence_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/sequence_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..27516ace1cc345bd5caaf00774020749e32a2b84 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/sequence_classification.md @@ -0,0 +1,391 @@ + + +# Text classification + +[[open-in-colab]] + + + +Text classification is a common NLP task that assigns a label or class to text. Some of the largest companies run text classification in production for a wide range of practical applications. One of the most popular forms of text classification is sentiment analysis, which assigns a label like ๐Ÿ™‚ positive, ๐Ÿ™ negative, or ๐Ÿ˜ neutral to a sequence of text. + +This guide will show you how to: + +1. Finetune [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on the [IMDb](https://huggingface.co/datasets/imdb) dataset to determine whether a movie review is positive or negative. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/text-classification). + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate accelerate +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load IMDb dataset + +Start by loading the IMDb dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset + +>>> imdb = load_dataset("imdb") +``` + +Then take a look at an example: + +```py +>>> imdb["test"][0] +{ + "label": 0, + "text": "I love sci-fi and am willing to put up with a lot. Sci-fi movies/TV are usually underfunded, under-appreciated and misunderstood. I tried to like this, I really did, but it is to good TV sci-fi as Babylon 5 is to Star Trek (the original). Silly prosthetics, cheap cardboard sets, stilted dialogues, CG that doesn't match the background, and painfully one-dimensional characters cannot be overcome with a 'sci-fi' setting. (I'm sure there are those of you out there who think Babylon 5 is good sci-fi TV. It's not. It's clichรฉd and uninspiring.) While US viewers might like emotion and character development, sci-fi is a genre that does not take itself seriously (cf. Star Trek). It may treat important issues, yet not as a serious philosophy. It's really difficult to care about the characters here as they are not simply foolish, just missing a spark of life. Their actions and reactions are wooden and predictable, often painful to watch. The makers of Earth KNOW it's rubbish as they have to always say \"Gene Roddenberry's Earth...\" otherwise people would not continue watching. Roddenberry's ashes must be turning in their orbit as this dull, cheap, poorly edited (watching it without advert breaks really brings this home) trudging Trabant of a show lumbers into space. Spoiler. So, kill off a main character. And then bring him back as another actor. Jeeez! Dallas all over again.", +} +``` + +There are two fields in this dataset: + +- `text`: the movie review text. +- `label`: a value that is either `0` for a negative review or `1` for a positive review. + +## Preprocess + +The next step is to load a DistilBERT tokenizer to preprocess the `text` field: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Create a preprocessing function to tokenize `text` and truncate sequences to be no longer than DistilBERT's maximum input length: + +```py +>>> def preprocess_function(examples): +... return tokenizer(examples["text"], truncation=True) +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up `map` by setting `batched=True` to process multiple elements of the dataset at once: + +```py +tokenized_imdb = imdb.map(preprocess_function, batched=True) +``` + +Now create a batch of examples using [`DataCollatorWithPadding`]. It's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + +```py +>>> from transformers import DataCollatorWithPadding + +>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer) +``` + + +```py +>>> from transformers import DataCollatorWithPadding + +>>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors="tf") +``` + + + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the accuracy: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + +Before you start training your model, create a map of the expected ids to their labels with `id2label` and `label2id`: + +```py +>>> id2label = {0: "NEGATIVE", 1: "POSITIVE"} +>>> label2id = {"NEGATIVE": 0, "POSITIVE": 1} +``` + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load DistilBERT with [`AutoModelForSequenceClassification`] along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer + +>>> model = AutoModelForSequenceClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id +... ) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the accuracy and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_model", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=2, +... weight_decay=0.01, +... eval_strategy="epoch", +... save_strategy="epoch", +... load_best_model_at_end=True, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_imdb["train"], +... eval_dataset=tokenized_imdb["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + + + +[`Trainer`] applies dynamic padding by default when you pass `tokenizer` to it. In this case, you don't need to specify a data collator explicitly. + + + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer +>>> import tensorflow as tf + +>>> batch_size = 16 +>>> num_epochs = 5 +>>> batches_per_epoch = len(tokenized_imdb["train"]) // batch_size +>>> total_train_steps = int(batches_per_epoch * num_epochs) +>>> optimizer, schedule = create_optimizer(init_lr=2e-5, num_warmup_steps=0, num_train_steps=total_train_steps) +``` + +Then you can load DistilBERT with [`TFAutoModelForSequenceClassification`] along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id +... ) +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_imdb["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_imdb["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +The last two things to setup before you start training is to compute the accuracy from the predictions, and provide a way to push your model to the Hub. Both are done by using [Keras callbacks](../main_classes/keras_callbacks). + +Pass your `compute_metrics` function to [`~transformers.KerasMetricCallback`]: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_model", +... tokenizer=tokenizer, +... ) +``` + +Then bundle your callbacks together: + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callbacks to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=callbacks) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for text classification, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Grab some text you'd like to run inference on: + +```py +>>> text = "This was a masterpiece. Not completely faithful to the books, but enthralling from beginning to end. Might be my favorite of the three." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for sentiment analysis with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("sentiment-analysis", model="stevhliu/my_awesome_model") +>>> classifier(text) +[{'label': 'POSITIVE', 'score': 0.9994940757751465}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Tokenize the text and return PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model") +>>> inputs = tokenizer(text, return_tensors="pt") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Get the class with the highest probability, and use the model's `id2label` mapping to convert it to a text label: + +```py +>>> predicted_class_id = logits.argmax().item() +>>> model.config.id2label[predicted_class_id] +'POSITIVE' +``` + + +Tokenize the text and return TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model") +>>> inputs = tokenizer(text, return_tensors="tf") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model") +>>> logits = model(**inputs).logits +``` + +Get the class with the highest probability, and use the model's `id2label` mapping to convert it to a text label: + +```py +>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0]) +>>> model.config.id2label[predicted_class_id] +'POSITIVE' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/summarization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/summarization.md new file mode 100644 index 0000000000000000000000000000000000000000..e16dd17dfe1fc8186cd0f121f5f794d0f7f7c16e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/summarization.md @@ -0,0 +1,400 @@ + + +# Summarization + +[[open-in-colab]] + + + +Summarization creates a shorter version of a document or an article that captures all the important information. Along with translation, it is another example of a task that can be formulated as a sequence-to-sequence task. Summarization can be: + +- Extractive: extract the most relevant information from a document. +- Abstractive: generate new text that captures the most relevant information. + +This guide will show you how to: + +1. Finetune [T5](https://huggingface.co/google-t5/t5-small) on the California state bill subset of the [BillSum](https://huggingface.co/datasets/billsum) dataset for abstractive summarization. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/summarization) + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate rouge_score +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load BillSum dataset + +Start by loading the smaller California state bill subset of the BillSum dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset + +>>> billsum = load_dataset("billsum", split="ca_test") +``` + +Split the dataset into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> billsum = billsum.train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> billsum["train"][0] +{'summary': 'Existing law authorizes state agencies to enter into contracts for the acquisition of goods or services upon approval by the Department of General Services. Existing law sets forth various requirements and prohibitions for those contracts, including, but not limited to, a prohibition on entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between spouses and domestic partners or same-sex and different-sex couples in the provision of benefits. Existing law provides that a contract entered into in violation of those requirements and prohibitions is void and authorizes the state or any person acting on behalf of the state to bring a civil action seeking a determination that a contract is in violation and therefore void. Under existing law, a willful violation of those requirements and prohibitions is a misdemeanor.\nThis bill would also prohibit a state agency from entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between employees on the basis of gender identity in the provision of benefits, as specified. By expanding the scope of a crime, this bill would impose a state-mandated local program.\nThe California Constitution requires the state to reimburse local agencies and school districts for certain costs mandated by the state. Statutory provisions establish procedures for making that reimbursement.\nThis bill would provide that no reimbursement is required by this act for a specified reason.', + 'text': 'The people of the State of California do enact as follows:\n\n\nSECTION 1.\nSection 10295.35 is added to the Public Contract Code, to read:\n10295.35.\n(a) (1) Notwithstanding any other law, a state agency shall not enter into any contract for the acquisition of goods or services in the amount of one hundred thousand dollars ($100,000) or more with a contractor that, in the provision of benefits, discriminates between employees on the basis of an employeeโ€™s or dependentโ€™s actual or perceived gender identity, including, but not limited to, the employeeโ€™s or dependentโ€™s identification as transgender.\n(2) For purposes of this section, โ€œcontractโ€ includes contracts with a cumulative amount of one hundred thousand dollars ($100,000) or more per contractor in each fiscal year.\n(3) For purposes of this section, an employee health plan is discriminatory if the plan is not consistent with Section 1365.5 of the Health and Safety Code and Section 10140 of the Insurance Code.\n(4) The requirements of this section shall apply only to those portions of a contractorโ€™s operations that occur under any of the following conditions:\n(A) Within the state.\n(B) On real property outside the state if the property is owned by the state or if the state has a right to occupy the property, and if the contractorโ€™s presence at that location is connected to a contract with the state.\n(C) Elsewhere in the United States where work related to a state contract is being performed.\n(b) Contractors shall treat as confidential, to the maximum extent allowed by law or by the requirement of the contractorโ€™s insurance provider, any request by an employee or applicant for employment benefits or any documentation of eligibility for benefits submitted by an employee or applicant for employment.\n(c) After taking all reasonable measures to find a contractor that complies with this section, as determined by the state agency, the requirements of this section may be waived under any of the following circumstances:\n(1) There is only one prospective contractor willing to enter into a specific contract with the state agency.\n(2) The contract is necessary to respond to an emergency, as determined by the state agency, that endangers the public health, welfare, or safety, or the contract is necessary for the provision of essential services, and no entity that complies with the requirements of this section capable of responding to the emergency is immediately available.\n(3) The requirements of this section violate, or are inconsistent with, the terms or conditions of a grant, subvention, or agreement, if the agency has made a good faith attempt to change the terms or conditions of any grant, subvention, or agreement to authorize application of this section.\n(4) The contractor is providing wholesale or bulk water, power, or natural gas, the conveyance or transmission of the same, or ancillary services, as required for ensuring reliable services in accordance with good utility practice, if the purchase of the same cannot practically be accomplished through the standard competitive bidding procedures and the contractor is not providing direct retail services to end users.\n(d) (1) A contractor shall not be deemed to discriminate in the provision of benefits if the contractor, in providing the benefits, pays the actual costs incurred in obtaining the benefit.\n(2) If a contractor is unable to provide a certain benefit, despite taking reasonable measures to do so, the contractor shall not be deemed to discriminate in the provision of benefits.\n(e) (1) Every contract subject to this chapter shall contain a statement by which the contractor certifies that the contractor is in compliance with this section.\n(2) The department or other contracting agency shall enforce this section pursuant to its existing enforcement powers.\n(3) (A) If a contractor falsely certifies that it is in compliance with this section, the contract with that contractor shall be subject to Article 9 (commencing with Section 10420), unless, within a time period specified by the department or other contracting agency, the contractor provides to the department or agency proof that it has complied, or is in the process of complying, with this section.\n(B) The application of the remedies or penalties contained in Article 9 (commencing with Section 10420) to a contract subject to this chapter shall not preclude the application of any existing remedies otherwise available to the department or other contracting agency under its existing enforcement powers.\n(f) Nothing in this section is intended to regulate the contracting practices of any local jurisdiction.\n(g) This section shall be construed so as not to conflict with applicable federal laws, rules, or regulations. In the event that a court or agency of competent jurisdiction holds that federal law, rule, or regulation invalidates any clause, sentence, paragraph, or section of this code or the application thereof to any person or circumstances, it is the intent of the state that the court or agency sever that clause, sentence, paragraph, or section so that the remainder of this section shall remain in effect.\nSEC. 2.\nSection 10295.35 of the Public Contract Code shall not be construed to create any new enforcement authority or responsibility in the Department of General Services or any other contracting agency.\nSEC. 3.\nNo reimbursement is required by this act pursuant to Section 6 of Article XIII\u2009B of the California Constitution because the only costs that may be incurred by a local agency or school district will be incurred because this act creates a new crime or infraction, eliminates a crime or infraction, or changes the penalty for a crime or infraction, within the meaning of Section 17556 of the Government Code, or changes the definition of a crime within the meaning of Section 6 of Article XIII\u2009B of the California Constitution.', + 'title': 'An act to add Section 10295.35 to the Public Contract Code, relating to public contracts.'} +``` + +There are two fields that you'll want to use: + +- `text`: the text of the bill which'll be the input to the model. +- `summary`: a condensed version of `text` which'll be the model target. + +## Preprocess + +The next step is to load a T5 tokenizer to process `text` and `summary`: + +```py +>>> from transformers import AutoTokenizer + +>>> checkpoint = "google-t5/t5-small" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +``` + +The preprocessing function you want to create needs to: + +1. Prefix the input with a prompt so T5 knows this is a summarization task. Some models capable of multiple NLP tasks require prompting for specific tasks. +2. Use the keyword `text_target` argument when tokenizing labels. +3. Truncate sequences to be no longer than the maximum length set by the `max_length` parameter. + +```py +>>> prefix = "summarize: " + + +>>> def preprocess_function(examples): +... inputs = [prefix + doc for doc in examples["text"]] +... model_inputs = tokenizer(inputs, max_length=1024, truncation=True) + +... labels = tokenizer(text_target=examples["summary"], max_length=128, truncation=True) + +... model_inputs["labels"] = labels["input_ids"] +... return model_inputs +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] method. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once: + +```py +>>> tokenized_billsum = billsum.map(preprocess_function, batched=True) +``` + +Now create a batch of examples using [`DataCollatorForSeq2Seq`]. It's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint) +``` + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, return_tensors="tf") +``` + + + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [ROUGE](https://huggingface.co/spaces/evaluate-metric/rouge) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> rouge = evaluate.load("rouge") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the ROUGE metric: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) +... labels = np.where(labels != -100, labels, tokenizer.pad_token_id) +... decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) + +... result = rouge.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True) + +... prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in predictions] +... result["gen_len"] = np.mean(prediction_lens) + +... return {k: round(v, 4) for k, v in result.items()} +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load T5 with [`AutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`Seq2SeqTrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the ROUGE metric and save the training checkpoint. +2. Pass the training arguments to [`Seq2SeqTrainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="my_awesome_billsum_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... weight_decay=0.01, +... save_total_limit=3, +... num_train_epochs=4, +... predict_with_generate=True, +... fp16=True, #change to bf16=True for XPU +... push_to_hub=True, +... ) + +>>> trainer = Seq2SeqTrainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_billsum["train"], +... eval_dataset=tokenized_billsum["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Then you can load T5 with [`TFAutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_billsum["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... tokenized_billsum["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +The last two things to setup before you start training is to compute the ROUGE score from the predictions, and provide a way to push your model to the Hub. Both are done by using [Keras callbacks](../main_classes/keras_callbacks). + +Pass your `compute_metrics` function to [`~transformers.KerasMetricCallback`]: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_test_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_billsum_model", +... tokenizer=tokenizer, +... ) +``` + +Then bundle your callbacks together: + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callbacks to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for summarization, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with some text you'd like to summarize. For T5, you need to prefix your input depending on the task you're working on. For summarization you should prefix your input as shown below: + +```py +>>> text = "summarize: The Inflation Reduction Act lowers prescription drug costs, health care costs, and energy costs. It's the most aggressive action on tackling the climate crisis in American history, which will lift up American workers and create good-paying, union jobs across the country. It'll lower the deficit and ask the ultra-wealthy and corporations to pay their fair share. And no one making under $400,000 per year will pay a penny more in taxes." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for summarization with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +>>> summarizer = pipeline("summarization", model="username/my_awesome_billsum_model") +>>> summarizer(text) +[{"summary_text": "The Inflation Reduction Act lowers prescription drug costs, health care costs, and energy costs. It's the most aggressive action on tackling the climate crisis in American history, which will lift up American workers and create good-paying, union jobs across the country."}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + + +Tokenize the text and return the `input_ids` as PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_billsum_model") +>>> inputs = tokenizer(text, return_tensors="pt").input_ids +``` + +Use the [`~generation.GenerationMixin.generate`] method to create the summarization. For more details about the different text generation strategies and parameters for controlling generation, check out the [Text Generation](../main_classes/text_generation) API. + +```py +>>> from transformers import AutoModelForSeq2SeqLM + +>>> model = AutoModelForSeq2SeqLM.from_pretrained("username/my_awesome_billsum_model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'the inflation reduction act lowers prescription drug costs, health care costs, and energy costs. it's the most aggressive action on tackling the climate crisis in american history. it will ask the ultra-wealthy and corporations to pay their fair share.' +``` + + +Tokenize the text and return the `input_ids` as TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_billsum_model") +>>> inputs = tokenizer(text, return_tensors="tf").input_ids +``` + +Use the [`~transformers.generation_tf_utils.TFGenerationMixin.generate`] method to create the summarization. For more details about the different text generation strategies and parameters for controlling generation, check out the [Text Generation](../main_classes/text_generation) API. + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("username/my_awesome_billsum_model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'the inflation reduction act lowers prescription drug costs, health care costs, and energy costs. it's the most aggressive action on tackling the climate crisis in american history. it will ask the ultra-wealthy and corporations to pay their fair share.' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/text-to-speech.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/text-to-speech.md new file mode 100644 index 0000000000000000000000000000000000000000..28153cd1160eda0de08f300488ff23af5f10240a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/text-to-speech.md @@ -0,0 +1,637 @@ + + +# Text to speech + +[[open-in-colab]] + +Text-to-speech (TTS) is the task of creating natural-sounding speech from text, where the speech can be generated in multiple +languages and for multiple speakers. Several text-to-speech models are currently available in ๐Ÿค— Transformers, such as +[Bark](../model_doc/bark), [MMS](../model_doc/mms), [VITS](../model_doc/vits) and [SpeechT5](../model_doc/speecht5). + +You can easily generate audio using the `"text-to-audio"` pipeline (or its alias - `"text-to-speech"`). Some models, like Bark, +can also be conditioned to generate non-verbal communications such as laughing, sighing and crying, or even add music. +Here's an example of how you would use the `"text-to-speech"` pipeline with Bark: + +```py +>>> from transformers import pipeline + +>>> pipe = pipeline("text-to-speech", model="suno/bark-small") +>>> text = "[clears throat] This is a test ... and I just took a long pause." +>>> output = pipe(text) +``` + +Here's a code snippet you can use to listen to the resulting audio in a notebook: + +```python +>>> from IPython.display import Audio +>>> Audio(output["audio"], rate=output["sampling_rate"]) +``` + +For more examples on what Bark and other pretrained TTS models can do, refer to our +[Audio course](https://huggingface.co/learn/audio-course/chapter6/pre-trained_models). + +If you are looking to fine-tune a TTS model, the only text-to-speech models currently available in ๐Ÿค— Transformers +are [SpeechT5](model_doc/speecht5) and [FastSpeech2Conformer](model_doc/fastspeech2_conformer), though more will be added in the future. SpeechT5 is pre-trained on a combination of speech-to-text and text-to-speech data, allowing it to learn a unified space of hidden representations shared by both text and speech. This means that the same pre-trained model can be fine-tuned for different tasks. Furthermore, SpeechT5 supports multiple speakers through x-vector speaker embeddings. + +The remainder of this guide illustrates how to: + +1. Fine-tune [SpeechT5](../model_doc/speecht5) that was originally trained on English speech on the Dutch (`nl`) language subset of the [VoxPopuli](https://huggingface.co/datasets/facebook/voxpopuli) dataset. +2. Use your refined model for inference in one of two ways: using a pipeline or directly. + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install datasets soundfile speechbrain accelerate +``` + +Install ๐Ÿค—Transformers from source as not all the SpeechT5 features have been merged into an official release yet: + +```bash +pip install git+https://github.com/huggingface/transformers.git +``` + + + +To follow this guide you will need a GPU. If you're working in a notebook, run the following line to check if a GPU is available: + +```bash +!nvidia-smi +``` + +or alternatively for AMD GPUs: + +```bash +!rocm-smi +``` + + + +We encourage you to log in to your Hugging Face account to upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load the dataset + +[VoxPopuli](https://huggingface.co/datasets/facebook/voxpopuli) is a large-scale multilingual speech corpus consisting of +data sourced from 2009-2020 European Parliament event recordings. It contains labelled audio-transcription data for 15 +European languages. In this guide, we are using the Dutch language subset, feel free to pick another subset. + +Note that VoxPopuli or any other automated speech recognition (ASR) dataset may not be the most suitable +option for training TTS models. The features that make it beneficial for ASR, such as excessive background noise, are +typically undesirable in TTS. However, finding top-quality, multilingual, and multi-speaker TTS datasets can be quite +challenging. + +Let's load the data: + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("facebook/voxpopuli", "nl", split="train") +>>> len(dataset) +20968 +``` + +20968 examples should be sufficient for fine-tuning. SpeechT5 expects audio data to have a sampling rate of 16 kHz, so +make sure the examples in the dataset meet this requirement: + +```py +dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +``` + +## Preprocess the data + +Let's begin by defining the model checkpoint to use and loading the appropriate processor: + +```py +>>> from transformers import SpeechT5Processor + +>>> checkpoint = "microsoft/speecht5_tts" +>>> processor = SpeechT5Processor.from_pretrained(checkpoint) +``` + +### Text cleanup for SpeechT5 tokenization + +Start by cleaning up the text data. You'll need the tokenizer part of the processor to process the text: + +```py +>>> tokenizer = processor.tokenizer +``` + +The dataset examples contain `raw_text` and `normalized_text` features. When deciding which feature to use as the text input, +consider that the SpeechT5 tokenizer doesn't have any tokens for numbers. In `normalized_text` the numbers are written +out as text. Thus, it is a better fit, and we recommend using `normalized_text` as input text. + +Because SpeechT5 was trained on the English language, it may not recognize certain characters in the Dutch dataset. If +left as is, these characters will be converted to `` tokens. However, in Dutch, certain characters like `ร ` are +used to stress syllables. In order to preserve the meaning of the text, we can replace this character with a regular `a`. + +To identify unsupported tokens, extract all unique characters in the dataset using the `SpeechT5Tokenizer` which +works with characters as tokens. To do this, write the `extract_all_chars` mapping function that concatenates +the transcriptions from all examples into one string and converts it to a set of characters. +Make sure to set `batched=True` and `batch_size=-1` in `dataset.map()` so that all transcriptions are available at once for +the mapping function. + +```py +>>> def extract_all_chars(batch): +... all_text = " ".join(batch["normalized_text"]) +... vocab = list(set(all_text)) +... return {"vocab": [vocab], "all_text": [all_text]} + + +>>> vocabs = dataset.map( +... extract_all_chars, +... batched=True, +... batch_size=-1, +... keep_in_memory=True, +... remove_columns=dataset.column_names, +... ) + +>>> dataset_vocab = set(vocabs["vocab"][0]) +>>> tokenizer_vocab = {k for k, _ in tokenizer.get_vocab().items()} +``` + +Now you have two sets of characters: one with the vocabulary from the dataset and one with the vocabulary from the tokenizer. +To identify any unsupported characters in the dataset, you can take the difference between these two sets. The resulting +set will contain the characters that are in the dataset but not in the tokenizer. + +```py +>>> dataset_vocab - tokenizer_vocab +{' ', 'ร ', 'รง', 'รจ', 'รซ', 'รญ', 'รฏ', 'รถ', 'รผ'} +``` + +To handle the unsupported characters identified in the previous step, define a function that maps these characters to +valid tokens. Note that spaces are already replaced by `โ–` in the tokenizer and don't need to be handled separately. + +```py +>>> replacements = [ +... ("ร ", "a"), +... ("รง", "c"), +... ("รจ", "e"), +... ("รซ", "e"), +... ("รญ", "i"), +... ("รฏ", "i"), +... ("รถ", "o"), +... ("รผ", "u"), +... ] + + +>>> def cleanup_text(inputs): +... for src, dst in replacements: +... inputs["normalized_text"] = inputs["normalized_text"].replace(src, dst) +... return inputs + + +>>> dataset = dataset.map(cleanup_text) +``` + +Now that you have dealt with special characters in the text, it's time to shift focus to the audio data. + +### Speakers + +The VoxPopuli dataset includes speech from multiple speakers, but how many speakers are represented in the dataset? To +determine this, we can count the number of unique speakers and the number of examples each speaker contributes to the dataset. +With a total of 20,968 examples in the dataset, this information will give us a better understanding of the distribution of +speakers and examples in the data. + +```py +>>> from collections import defaultdict + +>>> speaker_counts = defaultdict(int) + +>>> for speaker_id in dataset["speaker_id"]: +... speaker_counts[speaker_id] += 1 +``` + +By plotting a histogram you can get a sense of how much data there is for each speaker. + +```py +>>> import matplotlib.pyplot as plt + +>>> plt.figure() +>>> plt.hist(speaker_counts.values(), bins=20) +>>> plt.ylabel("Speakers") +>>> plt.xlabel("Examples") +>>> plt.show() +``` + +
+ Speakers histogram +
+ +The histogram reveals that approximately one-third of the speakers in the dataset have fewer than 100 examples, while +around ten speakers have more than 500 examples. To improve training efficiency and balance the dataset, we can limit +the data to speakers with between 100 and 400 examples. + +```py +>>> def select_speaker(speaker_id): +... return 100 <= speaker_counts[speaker_id] <= 400 + + +>>> dataset = dataset.filter(select_speaker, input_columns=["speaker_id"]) +``` + +Let's check how many speakers remain: + +```py +>>> len(set(dataset["speaker_id"])) +42 +``` + +Let's see how many examples are left: + +```py +>>> len(dataset) +9973 +``` + +You are left with just under 10,000 examples from approximately 40 unique speakers, which should be sufficient. + +Note that some speakers with few examples may actually have more audio available if the examples are long. However, +determining the total amount of audio for each speaker requires scanning through the entire dataset, which is a +time-consuming process that involves loading and decoding each audio file. As such, we have chosen to skip this step here. + +### Speaker embeddings + +To enable the TTS model to differentiate between multiple speakers, you'll need to create a speaker embedding for each example. +The speaker embedding is an additional input into the model that captures a particular speaker's voice characteristics. +To generate these speaker embeddings, use the pre-trained [spkrec-xvect-voxceleb](https://huggingface.co/speechbrain/spkrec-xvect-voxceleb) +model from SpeechBrain. + +Create a function `create_speaker_embedding()` that takes an input audio waveform and outputs a 512-element vector +containing the corresponding speaker embedding. + +```py +>>> import os +>>> import torch +>>> from speechbrain.inference.classifiers import EncoderClassifier +>>> from transformers import infer_device + +>>> spk_model_name = "speechbrain/spkrec-xvect-voxceleb" +>>> device = infer_device() +>>> speaker_model = EncoderClassifier.from_hparams( +... source=spk_model_name, +... run_opts={"device": device}, +... savedir=os.path.join("/tmp", spk_model_name), +... ) + + +>>> def create_speaker_embedding(waveform): +... with torch.no_grad(): +... speaker_embeddings = speaker_model.encode_batch(torch.tensor(waveform)) +... speaker_embeddings = torch.nn.functional.normalize(speaker_embeddings, dim=2) +... speaker_embeddings = speaker_embeddings.squeeze().cpu().numpy() +... return speaker_embeddings +``` + +It's important to note that the `speechbrain/spkrec-xvect-voxceleb` model was trained on English speech from the VoxCeleb +dataset, whereas the training examples in this guide are in Dutch. While we believe that this model will still generate +reasonable speaker embeddings for our Dutch dataset, this assumption may not hold true in all cases. + +For optimal results, we recommend training an X-vector model on the target speech first. This will ensure that the model +is better able to capture the unique voice characteristics present in the Dutch language. + +### Processing the dataset + +Finally, let's process the data into the format the model expects. Create a `prepare_dataset` function that takes in a +single example and uses the `SpeechT5Processor` object to tokenize the input text and load the target audio into a log-mel spectrogram. +It should also add the speaker embeddings as an additional input. + +```py +>>> def prepare_dataset(example): +... audio = example["audio"] + +... example = processor( +... text=example["normalized_text"], +... audio_target=audio["array"], +... sampling_rate=audio["sampling_rate"], +... return_attention_mask=False, +... ) + +... # strip off the batch dimension +... example["labels"] = example["labels"][0] + +... # use SpeechBrain to obtain x-vector +... example["speaker_embeddings"] = create_speaker_embedding(audio["array"]) + +... return example +``` + +Verify the processing is correct by looking at a single example: + +```py +>>> processed_example = prepare_dataset(dataset[0]) +>>> list(processed_example.keys()) +['input_ids', 'labels', 'stop_labels', 'speaker_embeddings'] +``` + +Speaker embeddings should be a 512-element vector: + +```py +>>> processed_example["speaker_embeddings"].shape +(512,) +``` + +The labels should be a log-mel spectrogram with 80 mel bins. + +```py +>>> import matplotlib.pyplot as plt + +>>> plt.figure() +>>> plt.imshow(processed_example["labels"].T) +>>> plt.show() +``` + +
+ Log-mel spectrogram with 80 mel bins +
+ +Side note: If you find this spectrogram confusing, it may be due to your familiarity with the convention of placing low frequencies +at the bottom and high frequencies at the top of a plot. However, when plotting spectrograms as an image using the matplotlib library, +the y-axis is flipped and the spectrograms appear upside down. + +Now apply the processing function to the entire dataset. This will take between 5 and 10 minutes. + +```py +>>> dataset = dataset.map(prepare_dataset, remove_columns=dataset.column_names) +``` + +You'll see a warning saying that some examples in the dataset are longer than the maximum input length the model can handle (600 tokens). +Remove those examples from the dataset. Here we go even further and to allow for larger batch sizes we remove anything over 200 tokens. + +```py +>>> def is_not_too_long(input_ids): +... input_length = len(input_ids) +... return input_length < 200 + + +>>> dataset = dataset.filter(is_not_too_long, input_columns=["input_ids"]) +>>> len(dataset) +8259 +``` + +Next, create a basic train/test split: + +```py +>>> dataset = dataset.train_test_split(test_size=0.1) +``` + +### Data collator + +In order to combine multiple examples into a batch, you need to define a custom data collator. This collator will pad shorter sequences with padding +tokens, ensuring that all examples have the same length. For the spectrogram labels, the padded portions are replaced with the special value `-100`. This special value +instructs the model to ignore that part of the spectrogram when calculating the spectrogram loss. + +```py +>>> from dataclasses import dataclass +>>> from typing import Any, Dict, List, Union + + +>>> @dataclass +... class TTSDataCollatorWithPadding: +... processor: Any + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... input_ids = [{"input_ids": feature["input_ids"]} for feature in features] +... label_features = [{"input_values": feature["labels"]} for feature in features] +... speaker_features = [feature["speaker_embeddings"] for feature in features] + +... # collate the inputs and targets into a batch +... batch = processor.pad(input_ids=input_ids, labels=label_features, return_tensors="pt") + +... # replace padding with -100 to ignore loss correctly +... batch["labels"] = batch["labels"].masked_fill(batch.decoder_attention_mask.unsqueeze(-1).ne(1), -100) + +... # not used during fine-tuning +... del batch["decoder_attention_mask"] + +... # round down target lengths to multiple of reduction factor +... if model.config.reduction_factor > 1: +... target_lengths = torch.tensor([len(feature["input_values"]) for feature in label_features]) +... target_lengths = target_lengths.new( +... [length - length % model.config.reduction_factor for length in target_lengths] +... ) +... max_length = max(target_lengths) +... batch["labels"] = batch["labels"][:, :max_length] + +... # also add in the speaker embeddings +... batch["speaker_embeddings"] = torch.tensor(speaker_features) + +... return batch +``` + +In SpeechT5, the input to the decoder part of the model is reduced by a factor 2. In other words, it throws away every +other timestep from the target sequence. The decoder then predicts a sequence that is twice as long. Since the original +target sequence length may be odd, the data collator makes sure to round the maximum length of the batch down to be a +multiple of 2. + +```py +>>> data_collator = TTSDataCollatorWithPadding(processor=processor) +``` + +## Train the model + +Load the pre-trained model from the same checkpoint as you used for loading the processor: + +```py +>>> from transformers import SpeechT5ForTextToSpeech + +>>> model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint) +``` + +The `use_cache=True` option is incompatible with gradient checkpointing. Disable it for training. + +```py +>>> model.config.use_cache = False +``` + +Define the training arguments. Here we are not computing any evaluation metrics during the training process. Instead, we'll +only look at the loss: + +```python +>>> from transformers import Seq2SeqTrainingArguments + +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="speecht5_finetuned_voxpopuli_nl", # change to a repo name of your choice +... per_device_train_batch_size=4, +... gradient_accumulation_steps=8, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=4000, +... gradient_checkpointing=True, +... fp16=True, +... eval_strategy="steps", +... per_device_eval_batch_size=2, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... report_to=["tensorboard"], +... load_best_model_at_end=True, +... greater_is_better=False, +... label_names=["labels"], +... push_to_hub=True, +... ) +``` + +Instantiate the `Trainer` object and pass the model, dataset, and data collator to it. + +```py +>>> from transformers import Seq2SeqTrainer + +>>> trainer = Seq2SeqTrainer( +... args=training_args, +... model=model, +... train_dataset=dataset["train"], +... eval_dataset=dataset["test"], +... data_collator=data_collator, +... processing_class=processor, +... ) +``` + +And with that, you're ready to start training! Training will take several hours. Depending on your GPU, +it is possible that you will encounter a CUDA "out-of-memory" error when you start training. In this case, you can reduce +the `per_device_train_batch_size` incrementally by factors of 2 and increase `gradient_accumulation_steps` by 2x to compensate. + +```py +>>> trainer.train() +``` + +To be able to use your checkpoint with a pipeline, make sure to save the processor with the checkpoint: + +```py +>>> processor.save_pretrained("YOUR_ACCOUNT_NAME/speecht5_finetuned_voxpopuli_nl") +``` + +Push the final model to the ๐Ÿค— Hub: + +```py +>>> trainer.push_to_hub() +``` + +## Inference + +### Inference with a pipeline + +Great, now that you've fine-tuned a model, you can use it for inference! +First, let's see how you can use it with a corresponding pipeline. Let's create a `"text-to-speech"` pipeline with your +checkpoint: + +```py +>>> from transformers import pipeline + +>>> pipe = pipeline("text-to-speech", model="YOUR_ACCOUNT_NAME/speecht5_finetuned_voxpopuli_nl") +``` + +Pick a piece of text in Dutch you'd like narrated, e.g.: + +```py +>>> text = "hallo allemaal, ik praat nederlands. groetjes aan iedereen!" +``` + +To use SpeechT5 with the pipeline, you'll need a speaker embedding. Let's get it from an example in the test dataset: + +```py +>>> example = dataset["test"][304] +>>> speaker_embeddings = torch.tensor(example["speaker_embeddings"]).unsqueeze(0) +``` + +Now you can pass the text and speaker embeddings to the pipeline, and it will take care of the rest: + +```py +>>> forward_params = {"speaker_embeddings": speaker_embeddings} +>>> output = pipe(text, forward_params=forward_params) +>>> output +{'audio': array([-6.82714235e-05, -4.26525949e-04, 1.06134125e-04, ..., + -1.22392643e-03, -7.76011671e-04, 3.29112721e-04], dtype=float32), + 'sampling_rate': 16000} +``` + +You can then listen to the result: + +```py +>>> from IPython.display import Audio +>>> Audio(output['audio'], rate=output['sampling_rate']) +``` + +### Run inference manually + +You can achieve the same inference results without using the pipeline, however, more steps will be required. + +Load the model from the ๐Ÿค— Hub: + +```py +>>> model = SpeechT5ForTextToSpeech.from_pretrained("YOUR_ACCOUNT/speecht5_finetuned_voxpopuli_nl") +``` + +Pick an example from the test dataset obtain a speaker embedding. + +```py +>>> example = dataset["test"][304] +>>> speaker_embeddings = torch.tensor(example["speaker_embeddings"]).unsqueeze(0) +``` + +Define the input text and tokenize it. + +```py +>>> text = "hallo allemaal, ik praat nederlands. groetjes aan iedereen!" +>>> inputs = processor(text=text, return_tensors="pt") +``` + +Create a spectrogram with your model: + +```py +>>> spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings) +``` + +Visualize the spectrogram, if you'd like to: + +```py +>>> plt.figure() +>>> plt.imshow(spectrogram.T) +>>> plt.show() +``` + +
+ Generated log-mel spectrogram +
+ +Finally, use the vocoder to turn the spectrogram into sound. + +```py +>>> with torch.no_grad(): +... speech = vocoder(spectrogram) + +>>> from IPython.display import Audio + +>>> Audio(speech.numpy(), rate=16000) +``` + +In our experience, obtaining satisfactory results from this model can be challenging. The quality of the speaker +embeddings appears to be a significant factor. Since SpeechT5 was pre-trained with English x-vectors, it performs best +when using English speaker embeddings. If the synthesized speech sounds poor, try using a different speaker embedding. + +Increasing the training duration is also likely to enhance the quality of the results. Even so, the speech clearly is Dutch instead of English, and it does +capture the voice characteristics of the speaker (compare to the original audio in the example). +Another thing to experiment with is the model's configuration. For example, try using `config.reduction_factor = 1` to +see if this improves the results. + +Finally, it is essential to consider ethical considerations. Although TTS technology has numerous useful applications, it +may also be used for malicious purposes, such as impersonating someone's voice without their knowledge or consent. Please +use TTS judiciously and responsibly. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/token_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/token_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..b93dd0cbe26d975ffe14f9f684b829163f13db65 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/token_classification.md @@ -0,0 +1,557 @@ + + +# Token classification + +[[open-in-colab]] + + + +Token classification assigns a label to individual tokens in a sentence. One of the most common token classification tasks is Named Entity Recognition (NER). NER attempts to find a label for each entity in a sentence, such as a person, location, or organization. + +This guide will show you how to: + +1. Finetune [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on the [WNUT 17](https://huggingface.co/datasets/wnut_17) dataset to detect new entities. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/token-classification). + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate seqeval +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load WNUT 17 dataset + +Start by loading the WNUT 17 dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset + +>>> wnut = load_dataset("wnut_17") +``` + +Then take a look at an example: + +```py +>>> wnut["train"][0] +{'id': '0', + 'ner_tags': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0], + 'tokens': ['@paulwalk', 'It', "'s", 'the', 'view', 'from', 'where', 'I', "'m", 'living', 'for', 'two', 'weeks', '.', 'Empire', 'State', 'Building', '=', 'ESB', '.', 'Pretty', 'bad', 'storm', 'here', 'last', 'evening', '.'] +} +``` + +Each number in `ner_tags` represents an entity. Convert the numbers to their label names to find out what the entities are: + +```py +>>> label_list = wnut["train"].features[f"ner_tags"].feature.names +>>> label_list +[ + "O", + "B-corporation", + "I-corporation", + "B-creative-work", + "I-creative-work", + "B-group", + "I-group", + "B-location", + "I-location", + "B-person", + "I-person", + "B-product", + "I-product", +] +``` + +The letter that prefixes each `ner_tag` indicates the token position of the entity: + +- `B-` indicates the beginning of an entity. +- `I-` indicates a token is contained inside the same entity (for example, the `State` token is a part of an entity like + `Empire State Building`). +- `0` indicates the token doesn't correspond to any entity. + +## Preprocess + + + +The next step is to load a DistilBERT tokenizer to preprocess the `tokens` field: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +As you saw in the example `tokens` field above, it looks like the input has already been tokenized. But the input actually hasn't been tokenized yet and you'll need to set `is_split_into_words=True` to tokenize the words into subwords. For example: + +```py +>>> example = wnut["train"][0] +>>> tokenized_input = tokenizer(example["tokens"], is_split_into_words=True) +>>> tokens = tokenizer.convert_ids_to_tokens(tokenized_input["input_ids"]) +>>> tokens +['[CLS]', '@', 'paul', '##walk', 'it', "'", 's', 'the', 'view', 'from', 'where', 'i', "'", 'm', 'living', 'for', 'two', 'weeks', '.', 'empire', 'state', 'building', '=', 'es', '##b', '.', 'pretty', 'bad', 'storm', 'here', 'last', 'evening', '.', '[SEP]'] +``` + +However, this adds some special tokens `[CLS]` and `[SEP]` and the subword tokenization creates a mismatch between the input and labels. A single word corresponding to a single label may now be split into two subwords. You'll need to realign the tokens and labels by: + +1. Mapping all tokens to their corresponding word with the [`word_ids`](https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.BatchEncoding.word_ids) method. +2. Assigning the label `-100` to the special tokens `[CLS]` and `[SEP]` so they're ignored by the PyTorch loss function (see [CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html)). +3. Only labeling the first token of a given word. Assign `-100` to other subtokens from the same word. + +Here is how you can create a function to realign the tokens and labels, and truncate sequences to be no longer than DistilBERT's maximum input length: + +```py +>>> def tokenize_and_align_labels(examples): +... tokenized_inputs = tokenizer(examples["tokens"], truncation=True, is_split_into_words=True) + +... labels = [] +... for i, label in enumerate(examples[f"ner_tags"]): +... word_ids = tokenized_inputs.word_ids(batch_index=i) # Map tokens to their respective word. +... previous_word_idx = None +... label_ids = [] +... for word_idx in word_ids: # Set the special tokens to -100. +... if word_idx is None: +... label_ids.append(-100) +... elif word_idx != previous_word_idx: # Only label the first token of a given word. +... label_ids.append(label[word_idx]) +... else: +... label_ids.append(-100) +... previous_word_idx = word_idx +... labels.append(label_ids) + +... tokenized_inputs["labels"] = labels +... return tokenized_inputs +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] function. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once: + +```py +>>> tokenized_wnut = wnut.map(tokenize_and_align_labels, batched=True) +``` + +Now create a batch of examples using [`DataCollatorWithPadding`]. It's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + +```py +>>> from transformers import DataCollatorForTokenClassification + +>>> data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer) +``` + + +```py +>>> from transformers import DataCollatorForTokenClassification + +>>> data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer, return_tensors="tf") +``` + + + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [seqeval](https://huggingface.co/spaces/evaluate-metric/seqeval) framework (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric). Seqeval actually produces several scores: precision, recall, F1, and accuracy. + +```py +>>> import evaluate + +>>> seqeval = evaluate.load("seqeval") +``` + +Get the NER labels first, and then create a function that passes your true predictions and true labels to [`~evaluate.EvaluationModule.compute`] to calculate the scores: + +```py +>>> import numpy as np + +>>> labels = [label_list[i] for i in example[f"ner_tags"]] + + +>>> def compute_metrics(p): +... predictions, labels = p +... predictions = np.argmax(predictions, axis=2) + +... true_predictions = [ +... [label_list[p] for (p, l) in zip(prediction, label) if l != -100] +... for prediction, label in zip(predictions, labels) +... ] +... true_labels = [ +... [label_list[l] for (p, l) in zip(prediction, label) if l != -100] +... for prediction, label in zip(predictions, labels) +... ] + +... results = seqeval.compute(predictions=true_predictions, references=true_labels) +... return { +... "precision": results["overall_precision"], +... "recall": results["overall_recall"], +... "f1": results["overall_f1"], +... "accuracy": results["overall_accuracy"], +... } +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + +Before you start training your model, create a map of the expected ids to their labels with `id2label` and `label2id`: + +```py +>>> id2label = { +... 0: "O", +... 1: "B-corporation", +... 2: "I-corporation", +... 3: "B-creative-work", +... 4: "I-creative-work", +... 5: "B-group", +... 6: "I-group", +... 7: "B-location", +... 8: "I-location", +... 9: "B-person", +... 10: "I-person", +... 11: "B-product", +... 12: "I-product", +... } +>>> label2id = { +... "O": 0, +... "B-corporation": 1, +... "I-corporation": 2, +... "B-creative-work": 3, +... "I-creative-work": 4, +... "B-group": 5, +... "I-group": 6, +... "B-location": 7, +... "I-location": 8, +... "B-person": 9, +... "I-person": 10, +... "B-product": 11, +... "I-product": 12, +... } +``` + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load DistilBERT with [`AutoModelForTokenClassification`] along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import AutoModelForTokenClassification, TrainingArguments, Trainer + +>>> model = AutoModelForTokenClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=13, id2label=id2label, label2id=label2id +... ) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`TrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the seqeval scores and save the training checkpoint. +2. Pass the training arguments to [`Trainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_wnut_model", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=2, +... weight_decay=0.01, +... eval_strategy="epoch", +... save_strategy="epoch", +... load_best_model_at_end=True, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_wnut["train"], +... eval_dataset=tokenized_wnut["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_train_epochs = 3 +>>> num_train_steps = (len(tokenized_wnut["train"]) // batch_size) * num_train_epochs +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=2e-5, +... num_train_steps=num_train_steps, +... weight_decay_rate=0.01, +... num_warmup_steps=0, +... ) +``` + +Then you can load DistilBERT with [`TFAutoModelForTokenClassification`] along with the number of expected labels, and the label mappings: + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=13, id2label=id2label, label2id=label2id +... ) +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_wnut["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_wnut["validation"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +The last two things to setup before you start training is to compute the seqeval scores from the predictions, and provide a way to push your model to the Hub. Both are done by using [Keras callbacks](../main_classes/keras_callbacks). + +Pass your `compute_metrics` function to [`~transformers.KerasMetricCallback`]: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_wnut_model", +... tokenizer=tokenizer, +... ) +``` + +Then bundle your callbacks together: + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callbacks to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=callbacks) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for token classification, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Grab some text you'd like to run inference on: + +```py +>>> text = "The Golden State Warriors are an American professional basketball team based in San Francisco." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for NER with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("ner", model="stevhliu/my_awesome_wnut_model") +>>> classifier(text) +[{'entity': 'B-location', + 'score': 0.42658573, + 'index': 2, + 'word': 'golden', + 'start': 4, + 'end': 10}, + {'entity': 'I-location', + 'score': 0.35856336, + 'index': 3, + 'word': 'state', + 'start': 11, + 'end': 16}, + {'entity': 'B-group', + 'score': 0.3064001, + 'index': 4, + 'word': 'warriors', + 'start': 17, + 'end': 25}, + {'entity': 'B-location', + 'score': 0.65523505, + 'index': 13, + 'word': 'san', + 'start': 80, + 'end': 83}, + {'entity': 'B-location', + 'score': 0.4668663, + 'index': 14, + 'word': 'francisco', + 'start': 84, + 'end': 93}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Tokenize the text and return PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> inputs = tokenizer(text, return_tensors="pt") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import AutoModelForTokenClassification + +>>> model = AutoModelForTokenClassification.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Get the class with the highest probability, and use the model's `id2label` mapping to convert it to a text label: + +```py +>>> predictions = torch.argmax(logits, dim=2) +>>> predicted_token_class = [model.config.id2label[t.item()] for t in predictions[0]] +>>> predicted_token_class +['O', + 'O', + 'B-location', + 'I-location', + 'B-group', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'B-location', + 'B-location', + 'O', + 'O'] +``` + + +Tokenize the text and return TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> inputs = tokenizer(text, return_tensors="tf") +``` + +Pass your inputs to the model and return the `logits`: + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> logits = model(**inputs).logits +``` + +Get the class with the highest probability, and use the model's `id2label` mapping to convert it to a text label: + +```py +>>> predicted_token_class_ids = tf.math.argmax(logits, axis=-1) +>>> predicted_token_class = [model.config.id2label[t] for t in predicted_token_class_ids[0].numpy().tolist()] +>>> predicted_token_class +['O', + 'O', + 'B-location', + 'I-location', + 'B-group', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'B-location', + 'B-location', + 'O', + 'O'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/translation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/translation.md new file mode 100644 index 0000000000000000000000000000000000000000..922cdc7241176a22faf030e37ea21b5b7f467ba3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/translation.md @@ -0,0 +1,409 @@ + + +# Translation + +[[open-in-colab]] + + + +Translation converts a sequence of text from one language to another. It is one of several tasks you can formulate as a sequence-to-sequence problem, a powerful framework for returning some output from an input, like translation or summarization. Translation systems are commonly used for translation between different language texts, but it can also be used for speech or some combination in between like text-to-speech or speech-to-text. + +This guide will show you how to: + +1. Finetune [T5](https://huggingface.co/google-t5/t5-small) on the English-French subset of the [OPUS Books](https://huggingface.co/datasets/opus_books) dataset to translate English text to French. +2. Use your finetuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/translation). + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install transformers datasets evaluate sacrebleu +``` + +We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load OPUS Books dataset + +Start by loading the English-French subset of the [OPUS Books](https://huggingface.co/datasets/opus_books) dataset from the ๐Ÿค— Datasets library: + +```py +>>> from datasets import load_dataset + +>>> books = load_dataset("opus_books", "en-fr") +``` + +Split the dataset into a train and test set with the [`~datasets.Dataset.train_test_split`] method: + +```py +>>> books = books["train"].train_test_split(test_size=0.2) +``` + +Then take a look at an example: + +```py +>>> books["train"][0] +{'id': '90560', + 'translation': {'en': 'But this lofty plateau measured only a few fathoms, and soon we reentered Our Element.', + 'fr': 'Mais ce plateau รฉlevรฉ ne mesurait que quelques toises, et bientรดt nous fรปmes rentrรฉs dans notre รฉlรฉment.'}} +``` + +`translation`: an English and French translation of the text. + +## Preprocess + + + +The next step is to load a T5 tokenizer to process the English-French language pairs: + +```py +>>> from transformers import AutoTokenizer + +>>> checkpoint = "google-t5/t5-small" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +``` + +The preprocessing function you want to create needs to: + +1. Prefix the input with a prompt so T5 knows this is a translation task. Some models capable of multiple NLP tasks require prompting for specific tasks. +2. Set the target language (French) in the `text_target` parameter to ensure the tokenizer processes the target text correctly. If you don't set `text_target`, the tokenizer processes the target text as English. +3. Truncate sequences to be no longer than the maximum length set by the `max_length` parameter. + +```py +>>> source_lang = "en" +>>> target_lang = "fr" +>>> prefix = "translate English to French: " + + +>>> def preprocess_function(examples): +... inputs = [prefix + example[source_lang] for example in examples["translation"]] +... targets = [example[target_lang] for example in examples["translation"]] +... model_inputs = tokenizer(inputs, text_target=targets, max_length=128, truncation=True) +... return model_inputs +``` + +To apply the preprocessing function over the entire dataset, use ๐Ÿค— Datasets [`~datasets.Dataset.map`] method. You can speed up the `map` function by setting `batched=True` to process multiple elements of the dataset at once: + +```py +>>> tokenized_books = books.map(preprocess_function, batched=True) +``` + +Now create a batch of examples using [`DataCollatorForSeq2Seq`]. It's more efficient to *dynamically pad* the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length. + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint) +``` + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, return_tensors="tf") +``` + + + +## Evaluate + +Including a metric during training is often helpful for evaluating your model's performance. You can quickly load a evaluation method with the ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) library. For this task, load the [SacreBLEU](https://huggingface.co/spaces/evaluate-metric/sacrebleu) metric (see the ๐Ÿค— Evaluate [quick tour](https://huggingface.co/docs/evaluate/a_quick_tour) to learn more about how to load and compute a metric): + +```py +>>> import evaluate + +>>> metric = evaluate.load("sacrebleu") +``` + +Then create a function that passes your predictions and labels to [`~evaluate.EvaluationModule.compute`] to calculate the SacreBLEU score: + +```py +>>> import numpy as np + + +>>> def postprocess_text(preds, labels): +... preds = [pred.strip() for pred in preds] +... labels = [[label.strip()] for label in labels] + +... return preds, labels + + +>>> def compute_metrics(eval_preds): +... preds, labels = eval_preds +... if isinstance(preds, tuple): +... preds = preds[0] +... decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) + +... labels = np.where(labels != -100, labels, tokenizer.pad_token_id) +... decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) + +... decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels) + +... result = metric.compute(predictions=decoded_preds, references=decoded_labels) +... result = {"bleu": result["score"]} + +... prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds] +... result["gen_len"] = np.mean(prediction_lens) +... result = {k: round(v, 4) for k, v in result.items()} +... return result +``` + +Your `compute_metrics` function is ready to go now, and you'll return to it when you setup your training. + +## Train + + + + + +If you aren't familiar with finetuning a model with the [`Trainer`], take a look at the basic tutorial [here](../training#train-with-pytorch-trainer)! + + + +You're ready to start training your model now! Load T5 with [`AutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +At this point, only three steps remain: + +1. Define your training hyperparameters in [`Seq2SeqTrainingArguments`]. The only required parameter is `output_dir` which specifies where to save your model. You'll push this model to the Hub by setting `push_to_hub=True` (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the [`Trainer`] will evaluate the SacreBLEU metric and save the training checkpoint. +2. Pass the training arguments to [`Seq2SeqTrainer`] along with the model, dataset, tokenizer, data collator, and `compute_metrics` function. +3. Call [`~Trainer.train`] to finetune your model. + +```py +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="my_awesome_opus_books_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... weight_decay=0.01, +... save_total_limit=3, +... num_train_epochs=2, +... predict_with_generate=True, +... fp16=True, #change to bf16=True for XPU +... push_to_hub=True, +... ) + +>>> trainer = Seq2SeqTrainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_books["train"], +... eval_dataset=tokenized_books["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + + + + +If you aren't familiar with finetuning a model with Keras, take a look at the basic tutorial [here](../training#train-a-tensorflow-model-with-keras)! + + +To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters: + +```py +>>> from transformers import AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Then you can load T5 with [`TFAutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +Convert your datasets to the `tf.data.Dataset` format with [`~transformers.TFPreTrainedModel.prepare_tf_dataset`]: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_books["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... tokenized_books["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +Configure the model for training with [`compile`](https://keras.io/api/models/model_training_apis/#compile-method). Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +The last two things to setup before you start training is to compute the SacreBLEU metric from the predictions, and provide a way to push your model to the Hub. Both are done by using [Keras callbacks](../main_classes/keras_callbacks). + +Pass your `compute_metrics` function to [`~transformers.KerasMetricCallback`]: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_test_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_opus_books_model", +... tokenizer=tokenizer, +... ) +``` + +Then bundle your callbacks together: + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +Finally, you're ready to start training your model! Call [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) with your training and validation datasets, the number of epochs, and your callbacks to finetune the model: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks) +``` + +Once training is completed, your model is automatically uploaded to the Hub so everyone can use it! + + + + + +For a more in-depth example of how to finetune a model for translation, take a look at the corresponding +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb) +or [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb). + + + +## Inference + +Great, now that you've finetuned a model, you can use it for inference! + +Come up with some text you'd like to translate to another language. For T5, you need to prefix your input depending on the task you're working on. For translation from English to French, you should prefix your input as shown below: + +```py +>>> text = "translate English to French: Legumes share resources with nitrogen-fixing bacteria." +``` + +The simplest way to try out your finetuned model for inference is to use it in a [`pipeline`]. Instantiate a `pipeline` for translation with your model, and pass your text to it: + +```py +>>> from transformers import pipeline + +# Change `xx` to the language of the input and `yy` to the language of the desired output. +# Examples: "en" for English, "fr" for French, "de" for German, "es" for Spanish, "zh" for Chinese, etc; translation_en_to_fr translates English to French +# You can view all the lists of languages here - https://huggingface.co/languages +>>> translator = pipeline("translation_xx_to_yy", model="username/my_awesome_opus_books_model") +>>> translator(text) +[{'translation_text': 'Legumes partagent des ressources avec des bactรฉries azotantes.'}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like: + + + +Tokenize the text and return the `input_ids` as PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_opus_books_model") +>>> inputs = tokenizer(text, return_tensors="pt").input_ids +``` + +Use the [`~generation.GenerationMixin.generate`] method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the [Text Generation](../main_classes/text_generation) API. + +```py +>>> from transformers import AutoModelForSeq2SeqLM + +>>> model = AutoModelForSeq2SeqLM.from_pretrained("username/my_awesome_opus_books_model") +>>> outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Les lignรฉes partagent des ressources avec des bactรฉries enfixant l'azote.' +``` + + +Tokenize the text and return the `input_ids` as TensorFlow tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_opus_books_model") +>>> inputs = tokenizer(text, return_tensors="tf").input_ids +``` + +Use the [`~transformers.generation_tf_utils.TFGenerationMixin.generate`] method to create the translation. For more details about the different text generation strategies and parameters for controlling generation, check out the [Text Generation](../main_classes/text_generation) API. + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("username/my_awesome_opus_books_model") +>>> outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95) +``` + +Decode the generated token ids back into text: + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Les lugumes partagent les ressources avec des bactรฉries fixatrices d'azote.' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..7a8c6ba45d04d382fc01994e79e65f64326fca45 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_classification.md @@ -0,0 +1,516 @@ + + +# Video classification + +[[open-in-colab]] + +Video classification is the task of assigning a label or class to an entire video. Videos are expected to have only one class for each video. Video classification models take a video as input and return a prediction about which class the video belongs to. These models can be used to categorize what a video is all about. A real-world application of video classification is action / activity recognition, which is useful for fitness applications. It is also helpful for vision-impaired individuals, especially when they are commuting. + +This guide will show you how to: + +1. Fine-tune [VideoMAE](https://huggingface.co/docs/transformers/main/en/model_doc/videomae) on a subset of the [UCF101](https://www.crcv.ucf.edu/data/UCF101.php) dataset. +2. Use your fine-tuned model for inference. + + + +To see all architectures and checkpoints compatible with this task, we recommend checking the [task-page](https://huggingface.co/tasks/video-classification). + + + +Before you begin, make sure you have all the necessary libraries installed: + +```bash +pip install -q pytorchvideo transformers evaluate +``` + +You will use [PyTorchVideo](https://pytorchvideo.org/) (dubbed `pytorchvideo`) to process and prepare the videos. + +We encourage you to log in to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to log in: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load UCF101 dataset + +Start by loading a subset of the [UCF-101 dataset](https://www.crcv.ucf.edu/data/UCF101.php). This will give you a chance to experiment and make sure everything works before spending more time training on the full dataset. + +```py +>>> from huggingface_hub import hf_hub_download + +>>> hf_dataset_identifier = "sayakpaul/ucf101-subset" +>>> filename = "UCF101_subset.tar.gz" +>>> file_path = hf_hub_download(repo_id=hf_dataset_identifier, filename=filename, repo_type="dataset") +``` + +After the subset has been downloaded, you need to extract the compressed archive: + +```py +>>> import tarfile + +>>> with tarfile.open(file_path) as t: +... t.extractall(".") +``` + +At a high level, the dataset is organized like so: + +```bash +UCF101_subset/ + train/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... + val/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... + test/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... +``` + +You can then count the number of total videos. + +```py +>>> import pathlib +>>> dataset_root_path = "UCF101_subset" +>>> dataset_root_path = pathlib.Path(dataset_root_path) +``` + +```py +>>> video_count_train = len(list(dataset_root_path.glob("train/*/*.avi"))) +>>> video_count_val = len(list(dataset_root_path.glob("val/*/*.avi"))) +>>> video_count_test = len(list(dataset_root_path.glob("test/*/*.avi"))) +>>> video_total = video_count_train + video_count_val + video_count_test +>>> print(f"Total videos: {video_total}") +``` + +```py +>>> all_video_file_paths = ( +... list(dataset_root_path.glob("train/*/*.avi")) +... + list(dataset_root_path.glob("val/*/*.avi")) +... + list(dataset_root_path.glob("test/*/*.avi")) +... ) +>>> all_video_file_paths[:5] +``` + +The (`sorted`) video paths appear like so: + +```bash +... +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g07_c04.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g07_c06.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g08_c01.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g09_c02.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g09_c06.avi' +... +``` + +You will notice that there are video clips belonging to the same group / scene where group is denoted by `g` in the video file paths. `v_ApplyEyeMakeup_g07_c04.avi` and `v_ApplyEyeMakeup_g07_c06.avi`, for example. + +For the validation and evaluation splits, you wouldn't want to have video clips from the same group / scene to prevent [data leakage](https://www.kaggle.com/code/alexisbcook/data-leakage). The subset that you are using in this tutorial takes this information into account. + +Next up, you will derive the set of labels present in the dataset. Also, create two dictionaries that'll be helpful when initializing the model: + +* `label2id`: maps the class names to integers. +* `id2label`: maps the integers to class names. + +```py +>>> class_labels = sorted({str(path).split("/")[2] for path in all_video_file_paths}) +>>> label2id = {label: i for i, label in enumerate(class_labels)} +>>> id2label = {i: label for label, i in label2id.items()} + +>>> print(f"Unique classes: {list(label2id.keys())}.") + +# Unique classes: ['ApplyEyeMakeup', 'ApplyLipstick', 'Archery', 'BabyCrawling', 'BalanceBeam', 'BandMarching', 'BaseballPitch', 'Basketball', 'BasketballDunk', 'BenchPress']. +``` + +There are 10 unique classes. For each class, there are 30 videos in the training set. + +## Load a model to fine-tune + +Instantiate a video classification model from a pretrained checkpoint and its associated image processor. The model's encoder comes with pre-trained parameters, and the classification head is randomly initialized. The image processor will come in handy when writing the preprocessing pipeline for our dataset. + +```py +>>> from transformers import VideoMAEImageProcessor, VideoMAEForVideoClassification + +>>> model_ckpt = "MCG-NJU/videomae-base" +>>> image_processor = VideoMAEImageProcessor.from_pretrained(model_ckpt) +>>> model = VideoMAEForVideoClassification.from_pretrained( +... model_ckpt, +... label2id=label2id, +... id2label=id2label, +... ignore_mismatched_sizes=True, # provide this in case you're planning to fine-tune an already fine-tuned checkpoint +... ) +``` + +While the model is loading, you might notice the following warning: + +```bash +Some weights of the model checkpoint at MCG-NJU/videomae-base were not used when initializing VideoMAEForVideoClassification: [..., 'decoder.decoder_layers.1.attention.output.dense.bias', 'decoder.decoder_layers.2.attention.attention.key.weight'] +- This IS expected if you are initializing VideoMAEForVideoClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). +- This IS NOT expected if you are initializing VideoMAEForVideoClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). +Some weights of VideoMAEForVideoClassification were not initialized from the model checkpoint at MCG-NJU/videomae-base and are newly initialized: ['classifier.bias', 'classifier.weight'] +You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference. +``` + +The warning is telling us we are throwing away some weights (e.g. the weights and bias of the `classifier` layer) and randomly initializing some others (the weights and bias of a new `classifier` layer). This is expected in this case, because we are adding a new head for which we don't have pretrained weights, so the library warns us we should fine-tune this model before using it for inference, which is exactly what we are going to do. + +**Note** that [this checkpoint](https://huggingface.co/MCG-NJU/videomae-base-finetuned-kinetics) leads to better performance on this task as the checkpoint was obtained fine-tuning on a similar downstream task having considerable domain overlap. You can check out [this checkpoint](https://huggingface.co/sayakpaul/videomae-base-finetuned-kinetics-finetuned-ucf101-subset) which was obtained by fine-tuning `MCG-NJU/videomae-base-finetuned-kinetics`. + +## Prepare the datasets for training + +For preprocessing the videos, you will leverage the [PyTorchVideo library](https://pytorchvideo.org/). Start by importing the dependencies we need. + +```py +>>> import pytorchvideo.data + +>>> from pytorchvideo.transforms import ( +... ApplyTransformToKey, +... Normalize, +... RandomShortSideScale, +... RemoveKey, +... ShortSideScale, +... UniformTemporalSubsample, +... ) + +>>> from torchvision.transforms import ( +... Compose, +... Lambda, +... RandomCrop, +... RandomHorizontalFlip, +... Resize, +... ) +``` + +For the training dataset transformations, use a combination of uniform temporal subsampling, pixel normalization, random cropping, and random horizontal flipping. For the validation and evaluation dataset transformations, keep the same transformation chain except for random cropping and horizontal flipping. To learn more about the details of these transformations check out the [official documentation of PyTorchVideo](https://pytorchvideo.org). + +Use the `image_processor` associated with the pre-trained model to obtain the following information: + +* Image mean and standard deviation with which the video frame pixels will be normalized. +* Spatial resolution to which the video frames will be resized. + +Start by defining some constants. + +```py +>>> mean = image_processor.image_mean +>>> std = image_processor.image_std +>>> if "shortest_edge" in image_processor.size: +... height = width = image_processor.size["shortest_edge"] +>>> else: +... height = image_processor.size["height"] +... width = image_processor.size["width"] +>>> resize_to = (height, width) + +>>> num_frames_to_sample = model.config.num_frames +>>> sample_rate = 4 +>>> fps = 30 +>>> clip_duration = num_frames_to_sample * sample_rate / fps +``` + +Now, define the dataset-specific transformations and the datasets respectively. Starting with the training set: + +```py +>>> train_transform = Compose( +... [ +... ApplyTransformToKey( +... key="video", +... transform=Compose( +... [ +... UniformTemporalSubsample(num_frames_to_sample), +... Lambda(lambda x: x / 255.0), +... Normalize(mean, std), +... RandomShortSideScale(min_size=256, max_size=320), +... RandomCrop(resize_to), +... RandomHorizontalFlip(p=0.5), +... ] +... ), +... ), +... ] +... ) + +>>> train_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "train"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("random", clip_duration), +... decode_audio=False, +... transform=train_transform, +... ) +``` + +The same sequence of workflow can be applied to the validation and evaluation sets: + +```py +>>> val_transform = Compose( +... [ +... ApplyTransformToKey( +... key="video", +... transform=Compose( +... [ +... UniformTemporalSubsample(num_frames_to_sample), +... Lambda(lambda x: x / 255.0), +... Normalize(mean, std), +... Resize(resize_to), +... ] +... ), +... ), +... ] +... ) + +>>> val_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "val"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("uniform", clip_duration), +... decode_audio=False, +... transform=val_transform, +... ) + +>>> test_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "test"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("uniform", clip_duration), +... decode_audio=False, +... transform=val_transform, +... ) +``` + +**Note**: The above dataset pipelines are taken from the [official PyTorchVideo example](https://pytorchvideo.org/docs/tutorial_classification#dataset). We're using the [`pytorchvideo.data.Ucf101()`](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html#pytorchvideo.data.Ucf101) function because it's tailored for the UCF-101 dataset. Under the hood, it returns a [`pytorchvideo.data.labeled_video_dataset.LabeledVideoDataset`](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html#pytorchvideo.data.LabeledVideoDataset) object. `LabeledVideoDataset` class is the base class for all things video in the PyTorchVideo dataset. So, if you want to use a custom dataset not supported off-the-shelf by PyTorchVideo, you can extend the `LabeledVideoDataset` class accordingly. Refer to the `data` API [documentation to](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html) learn more. Also, if your dataset follows a similar structure (as shown above), then using the `pytorchvideo.data.Ucf101()` should work just fine. + +You can access the `num_videos` argument to know the number of videos in the dataset. + +```py +>>> print(train_dataset.num_videos, val_dataset.num_videos, test_dataset.num_videos) +# (300, 30, 75) +``` + +## Visualize the preprocessed video for better debugging + +```py +>>> import imageio +>>> import numpy as np +>>> from IPython.display import Image + +>>> def unnormalize_img(img): +... """Un-normalizes the image pixels.""" +... img = (img * std) + mean +... img = (img * 255).astype("uint8") +... return img.clip(0, 255) + +>>> def create_gif(video_tensor, filename="sample.gif"): +... """Prepares a GIF from a video tensor. +... +... The video tensor is expected to have the following shape: +... (num_frames, num_channels, height, width). +... """ +... frames = [] +... for video_frame in video_tensor: +... frame_unnormalized = unnormalize_img(video_frame.permute(1, 2, 0).numpy()) +... frames.append(frame_unnormalized) +... kargs = {"duration": 0.25} +... imageio.mimsave(filename, frames, "GIF", **kargs) +... return filename + +>>> def display_gif(video_tensor, gif_name="sample.gif"): +... """Prepares and displays a GIF from a video tensor.""" +... video_tensor = video_tensor.permute(1, 0, 2, 3) +... gif_filename = create_gif(video_tensor, gif_name) +... return Image(filename=gif_filename) + +>>> sample_video = next(iter(train_dataset)) +>>> video_tensor = sample_video["video"] +>>> display_gif(video_tensor) +``` + +
+ Person playing basketball +
+ +## Train the model + +Leverage [`Trainer`](https://huggingface.co/docs/transformers/main_classes/trainer) from ๐Ÿค— Transformers for training the model. To instantiate a `Trainer`, you need to define the training configuration and an evaluation metric. The most important is the [`TrainingArguments`](https://huggingface.co/transformers/main_classes/trainer.html#transformers.TrainingArguments), which is a class that contains all the attributes to configure the training. It requires an output folder name, which will be used to save the checkpoints of the model. It also helps sync all the information in the model repository on ๐Ÿค— Hub. + +Most of the training arguments are self-explanatory, but one that is quite important here is `remove_unused_columns=False`. This one will drop any features not used by the model's call function. By default it's `True` because usually it's ideal to drop unused feature columns, making it easier to unpack inputs into the model's call function. But, in this case, you need the unused features ('video' in particular) in order to create `pixel_values` (which is a mandatory key our model expects in its inputs). + + +```py +>>> from transformers import TrainingArguments, Trainer + +>>> model_name = model_ckpt.split("/")[-1] +>>> new_model_name = f"{model_name}-finetuned-ucf101-subset" +>>> num_epochs = 4 + +>>> args = TrainingArguments( +... new_model_name, +... remove_unused_columns=False, +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=batch_size, +... per_device_eval_batch_size=batch_size, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... max_steps=(train_dataset.num_videos // batch_size) * num_epochs, +... ) +``` + +The dataset returned by `pytorchvideo.data.Ucf101()` doesn't implement the `__len__` method. As such, we must define `max_steps` when instantiating `TrainingArguments`. + +Next, you need to define a function to compute the metrics from the predictions, which will use the `metric` you'll load now. The only preprocessing you have to do is to take the argmax of our predicted logits: + +```py +import evaluate + +metric = evaluate.load("accuracy") + + +def compute_metrics(eval_pred): + predictions = np.argmax(eval_pred.predictions, axis=1) + return metric.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +**A note on evaluation**: + +In the [VideoMAE paper](https://huggingface.co/papers/2203.12602), the authors use the following evaluation strategy. They evaluate the model on several clips from test videos and apply different crops to those clips and report the aggregate score. However, in the interest of simplicity and brevity, we don't consider that in this tutorial. + +Also, define a `collate_fn`, which will be used to batch examples together. Each batch consists of 2 keys, namely `pixel_values` and `labels`. + +```py +>>> def collate_fn(examples): +... # permute to (num_frames, num_channels, height, width) +... pixel_values = torch.stack( +... [example["video"].permute(1, 0, 2, 3) for example in examples] +... ) +... labels = torch.tensor([example["label"] for example in examples]) +... return {"pixel_values": pixel_values, "labels": labels} +``` + +Then you just pass all of this along with the datasets to `Trainer`: + +```py +>>> trainer = Trainer( +... model, +... args, +... train_dataset=train_dataset, +... eval_dataset=val_dataset, +... processing_class=image_processor, +... compute_metrics=compute_metrics, +... data_collator=collate_fn, +... ) +``` + +You might wonder why you passed along the `image_processor` as a tokenizer when you preprocessed the data already. This is only to make sure the image processor configuration file (stored as JSON) will also be uploaded to the repo on the Hub. + +Now fine-tune our model by calling the `train` method: + +```py +>>> train_results = trainer.train() +``` + +Once training is completed, share your model to the Hub with the [`~transformers.Trainer.push_to_hub`] method so everyone can use your model: + +```py +>>> trainer.push_to_hub() +``` + +## Inference + +Great, now that you have fine-tuned a model, you can use it for inference! + +Load a video for inference: + +```py +>>> sample_test_video = next(iter(test_dataset)) +``` + +
+ Teams playing basketball +
+ +The simplest way to try out your fine-tuned model for inference is to use it in a [`pipeline`](https://huggingface.co/docs/transformers/main/en/main_classes/pipelines#transformers.VideoClassificationPipeline). Instantiate a `pipeline` for video classification with your model, and pass your video to it: + +```py +>>> from transformers import pipeline + +>>> video_cls = pipeline(model="my_awesome_video_cls_model") +>>> video_cls("https://huggingface.co/datasets/sayakpaul/ucf101-subset/resolve/main/v_BasketballDunk_g14_c06.avi") +[{'score': 0.9272987842559814, 'label': 'BasketballDunk'}, + {'score': 0.017777055501937866, 'label': 'BabyCrawling'}, + {'score': 0.01663011871278286, 'label': 'BalanceBeam'}, + {'score': 0.009560945443809032, 'label': 'BandMarching'}, + {'score': 0.0068979403004050255, 'label': 'BaseballPitch'}] +``` + +You can also manually replicate the results of the `pipeline` if you'd like. + + +```py +>>> def run_inference(model, video): +... # (num_frames, num_channels, height, width) +... perumuted_sample_test_video = video.permute(1, 0, 2, 3) +... inputs = { +... "pixel_values": perumuted_sample_test_video.unsqueeze(0), +... "labels": torch.tensor( +... [sample_test_video["label"]] +... ), # this can be skipped if you don't have labels available. +... } + +... device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +... inputs = {k: v.to(device) for k, v in inputs.items()} +... model = model.to(device) + +... # forward pass +... with torch.no_grad(): +... outputs = model(**inputs) +... logits = outputs.logits + +... return logits +``` + +Now, pass your input to the model and return the `logits`: + +```py +>>> logits = run_inference(trained_model, sample_test_video["video"]) +``` + +Decoding the `logits`, we get: + +```py +>>> predicted_class_idx = logits.argmax(-1).item() +>>> print("Predicted class:", model.config.id2label[predicted_class_idx]) +# Predicted class: BasketballDunk +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_text_to_text.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_text_to_text.md new file mode 100644 index 0000000000000000000000000000000000000000..a7aafdf1954771ce015c8fa622026feb278d08e3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/en/tasks/video_text_to_text.md @@ -0,0 +1,147 @@ + + +# Video-text-to-text + +[[open-in-colab]] + +Video-text-to-text models, also known as video language models or vision language models with video input, are language models that take a video input. These models can tackle various tasks, from video question answering to video captioning. + +These models have nearly the same architecture as [image-text-to-text](../image_text_to_text) models except for some changes to accept video data, since video data is essentially image frames with temporal dependencies. Some image-text-to-text models take in multiple images, but this alone is inadequate for a model to accept videos. Moreover, video-text-to-text models are often trained with all vision modalities. Each example might have videos, multiple videos, images and multiple images. Some of these models can also take interleaved inputs. For example, you can refer to a specific video inside a string of text by adding a video token in text like "What is happening in this video? `
" + +``` +Observa cรณmo todo el chat se condensa en una sola cadena. Si usamos `tokenize=True`, que es la configuraciรณn predeterminada, esa cadena tambiรฉn serรก tokenizada para nosotros. Sin embargo, para ver una plantilla mรกs compleja en acciรณn, usemos el modelo `mistralai/Mistral-7B-Instruct-v0.1` + +```python +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") + +>>> chat = [ +... {"role": "user", "content": "Hello, how are you?"}, +... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, +... {"role": "user", "content": "I'd like to show off how chat templating works!"}, +... ] + +>>> tokenizer.apply_chat_template(chat, tokenize=False) +"[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" +``` + +Ten en cuenta que esta vez, el tokenizador ha aรฑadido los tokens de control [INST] y [/INST] para indicar el inicio y el final de los mensajes de usuario (ยกpero no de los mensajes del asistente!). Mistral-instruct fue entrenado con estos tokens, pero BlenderBot no lo fue. + +## ยฟCรณmo uso las plantillas de chat? + +Como puedes ver en el ejemplo anterior, las plantillas de chat son fรกciles de usar. Simplemente construye una lista de mensajes, con claves de `rol` y `contenido`, y luego pรกsala al mรฉtodo [`~PreTrainedTokenizer.apply_chat_template`]. Una vez que hagas eso, ยกobtendrรกs una salida lista para usar! Al utilizar plantillas de chat como entrada para la generaciรณn de modelos, tambiรฉn es una buena idea usar `add_generation_prompt=True` para agregar una [indicaciรณn de generaciรณn](#ยฟQuรฉ-son-los-"generation-prompts"?). + +Aquรญ tienes un ejemplo de cรณmo preparar la entrada para `model.generate()` utilizando el modelo de asistente `Zephyr`: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +checkpoint = "HuggingFaceH4/zephyr-7b-beta" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +model = AutoModelForCausalLM.from_pretrained(checkpoint) # You may want to use bfloat16 and/or move to GPU here + +messages = [ + { + "role": "system", + "content": "You are a friendly chatbot who always responds in the style of a pirate", + }, + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, + ] +tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") +print(tokenizer.decode(tokenized_chat[0])) +``` + +Esto generarรก una cadena en el formato de entrada que Zephyr espera. + +```text +<|system|> +You are a friendly chatbot who always responds in the style of a pirate +<|user|> +How many helicopters can a human eat in one sitting? +<|assistant|> +``` + +Ahora que nuestra entrada estรก formateada correctamente para Zephyr, podemos usar el modelo para generar una respuesta a la pregunta del usuario: + +```python +outputs = model.generate(tokenized_chat, max_new_tokens=128) +print(tokenizer.decode(outputs[0])) + +``` +Esto producirรก: + +```text +<|system|> +You are a friendly chatbot who always responds in the style of a pirate +<|user|> +How many helicopters can a human eat in one sitting? +<|assistant|> +Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all. +``` + +ยกArr, al final resultรณ ser fรกcil! + +## ยฟExiste un pipeline automatizado para chats? + +Sรญ, lo hay! Nuestros canales de generaciรณn de texto admiten entradas de chat, cual facilita mรกs facรญl utilizar los modelos de chat. En el pasado, solรญamos utilizar una clase dedicada "ConversationalPipeline", pero ahora ha quedado obsoleta y su funcionalidad se ha fusionado en [`TextGenerationPipeline`]. Este pipeline estรก diseรฑado para facilitar el uso de modelos de chat. Intentemos el ejemplo de `Zephyr` de nuevo, pero esta vez utilizando el pipeline: + +```python +from transformers import pipeline + +pipe = pipeline("conversational", "HuggingFaceH4/zephyr-7b-beta") +messages = [ + { + "role": "system", + "content": "You are a friendly chatbot who always responds in the style of a pirate", + }, + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, +] +print(pipe(messages, max_new_tokens=128)[0]['generated_text'][-1]) # Print the assistant's response +``` + +```text +{'role': 'assistant', 'content': "Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all."} +``` + + +La canalizaciรณn se encargarรก de todos los detalles de la tokenizaciรณn y de llamar a `apply_chat_template` por ti. Una vez que el modelo tenga una plantilla de chat, ยกtodo lo que necesitas hacer es inicializar el pipeline y pasarle la lista de mensajes! + +# ยฟQuรฉ son los "generation prompts"? + +Puede que hayas notado que el mรฉtodo `apply_chat_template` tiene un argumento `add_generation_prompt`. Este argumento indica a la plantilla que agregue tokens que indiquen el inicio de una respuesta del bot. Por ejemplo, considera el siguiente chat: + +```python +messages = [ + {"role": "user", "content": "Hi there!"}, + {"role": "assistant", "content": "Nice to meet you!"}, + {"role": "user", "content": "Can I ask a question?"} +] +``` + +Asรญ es cรณmo se verรก esto sin un "generation prompt", usando la plantilla ChatML que vimos en el ejemplo de Zephyr: + +```python +tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) +"""<|im_start|>user +Hi there!<|im_end|> +<|im_start|>assistant +Nice to meet you!<|im_end|> +<|im_start|>user +Can I ask a question?<|im_end|> +""" +``` + +Y asรญ es como se ve **con** un "generation prompt": + +```python +tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) +"""<|im_start|>user +Hi there!<|im_end|> +<|im_start|>assistant +Nice to meet you!<|im_end|> +<|im_start|>user +Can I ask a question?<|im_end|> +<|im_start|>assistant +""" +``` + +Ten en cuenta que esta vez, hemos agregado los tokens que indican el inicio de una respuesta del bot. Esto asegura que cuando el modelo genere texto, escribirรก una respuesta del bot en lugar de hacer algo inesperado, como continuar el mensaje del usuario. Recuerda, los modelos de chat siguen siendo solo modelos de lenguaje: estรกn entrenados para continuar texto, ยกy el chat es solo un tipo especial de texto para ellos! Necesitas guiarlos con los tokens de control apropiados para que sepan lo que se supone que deben estar haciendo. + +No todos los modelos requieren "generation prompts". Algunos modelos, como BlenderBot y LLaMA, no tienen ningรบn token especial antes de las respuestas del bot. En estos casos, el argumento `add_generation_prompt` no tendrรก ningรบn efecto. El efecto exacto que tiene `add_generation_prompt` dependerรก de la plantilla que se estรฉ utilizando. + +## ยฟPuedo usar plantillas de chat en el entrenamiento? + +ยกSรญ! Recomendamos que apliques la plantilla de chat como un paso de preprocesamiento para tu conjunto de datos. Despuรฉs de esto, simplemente puedes continuar como cualquier otra tarea de entrenamiento de modelos de lenguaje. Durante el entrenamiento, generalmente deberรญas establecer `add_generation_prompt=False`, porque los tokens aรฑadidos para solicitar una respuesta del asistente no serรกn รบtiles durante el entrenamiento. Veamos un ejemplo: + +```python +from transformers import AutoTokenizer +from datasets import Dataset + +tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta") + +chat1 = [ + {"role": "user", "content": "Which is bigger, the moon or the sun?"}, + {"role": "assistant", "content": "The sun."} +] +chat2 = [ + {"role": "user", "content": "Which is bigger, a virus or a bacterium?"}, + {"role": "assistant", "content": "A bacterium."} +] + +dataset = Dataset.from_dict({"chat": [chat1, chat2]}) +dataset = dataset.map(lambda x: {"formatted_chat": tokenizer.apply_chat_template(x["chat"], tokenize=False, add_generation_prompt=False)}) +print(dataset['formatted_chat'][0]) +``` + +Y obtenemos: + +```text +<|user|> +Which is bigger, the moon or the sun? +<|assistant|> +The sun. +``` + +Desde aquรญ, simplemente continรบa el entrenamiento como lo harรญas con una tarea estรกndar de modelado de lenguaje, utilizando la columna `formatted_chat`. + +## Avanzado: ยฟCรณmo funcionan las plantillas de chat? + +La plantilla de chat para un modelo se almacena en el atributo `tokenizer.chat_template`. Si no se establece ninguna plantilla de chat, se utiliza en su lugar la plantilla predeterminada para esa clase de modelo. Echemos un vistazo a la plantilla para `BlenderBot`: + +```python +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") + +>>> tokenizer.chat_template +"{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}" +``` + +ยกEs un poco intimidante! Vamos a agregar algunas lรญneas nuevas y sangria para que sea mรกs legible. Ten en cuenta que la primera lรญnea nueva despuรฉs de cada bloque, asรญ como cualquier espacio en blanco anterior a un bloque, se ignoran de forma predeterminada, utilizando las banderas `trim_blocks` y `lstrip_blocks` de Jinja. Sin embargo, ยกten cuidado! Aunque el espacio en blanco inicial en cada lรญnea se elimina, los espacios entre bloques en la misma lรญnea no. ยกTe recomendamos encarecidamente que verifiques que tu plantilla no estรฉ imprimiendo espacios adicionales donde no deberรญa estarlo! + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ ' ' }} + {% endif %} + {{ message['content'] }} + {% if not loop.last %} + {{ ' ' }} + {% endif %} +{% endfor %} +{{ eos_token }} +``` + +Si nunca has visto uno de estos antes, esto es una [plantilla de Jinja](https://jinja.palletsprojects.com/en/3.1.x/templates/). Jinja es un lenguaje de plantillas que te permite escribir cรณdigo simple que genera texto. En muchos aspectos, el cรณdigo y la sintaxis se asemejan a Python. En Python puro, esta plantilla se verรญa algo asรญ: + +```python +for idx, message in enumerate(messages): + if message['role'] == 'user': + print(' ') + print(message['content']) + if not idx == len(messages) - 1: # Check for the last message in the conversation + print(' ') +print(eos_token) +``` + +Efectivamente, la plantilla hace tres cosas: +1. Para cada mensaje, si el mensaje es un mensaje de usuario, aรฑade un espacio en blanco antes de รฉl, de lo contrario no imprime nada. +2. Aรฑade el contenido del mensaje. +3. Si el mensaje no es el รบltimo mensaje, aรฑade dos espacios despuรฉs de รฉl. Despuรฉs del รบltimo mensaje, imprime el token EOS. + +Esta es una plantilla bastante simple: no aรฑade ningรบn token de control y no admite mensajes "del sistema", que son una forma comรบn de dar al modelo directivas sobre cรณmo debe comportarse en la conversaciรณn posterior. ยกPero Jinja te brinda mucha flexibilidad para hacer esas cosas! Veamos una plantilla de Jinja que pueda formatear las entradas de manera similar a la forma en que LLaMA las formatea (nota que la plantilla real de LLaMA incluye el manejo de mensajes del sistema predeterminados y el manejo de mensajes del sistema ligeramente diferentes en general; ยกno uses esta en tu cรณdigo real!) + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ bos_token + '[INST] ' + message['content'] + ' [/INST]' }} + {% elif message['role'] == 'system' %} + {{ '<>\\n' + message['content'] + '\\n<>\\n\\n' }} + {% elif message['role'] == 'assistant' %} + {{ ' ' + message['content'] + ' ' + eos_token }} + {% endif %} +{% endfor %} +``` + +Si observas esto por un momento, puedas ver lo que esta plantilla estรก haciendo: aรฑade tokens especรญficos basados en el "rol" de cada mensaje, que representa quiรฉn lo enviรณ. Los mensajes de usuario, asistente y sistema son claramente distinguibles para el modelo debido a los tokens en los que estรกn envueltos. + +## Avanzado: Aรฑadiendo y editando plantillas de chat + +### ยฟCรณmo creo una plantilla de chat? + +Simple, solo escribe una plantilla de Jinja y establece `tokenizer.chat_template`. ยกPuede resultarte mรกs fรกcil comenzar con una plantilla existente de otro modelo y simplemente editarla segรบn tus necesidades! Por ejemplo, podrรญamos tomar la plantilla de LLaMA de arriba y aรฑadir "[ASST]" y "[/ASST]" a los mensajes del asistente: + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ bos_token + '[INST] ' + message['content'].strip() + ' [/INST]' }} + {% elif message['role'] == 'system' %} + {{ '<>\\n' + message['content'].strip() + '\\n<>\\n\\n' }} + {% elif message['role'] == 'assistant' %} + {{ '[ASST] ' + message['content'] + ' [/ASST]' + eos_token }} + {% endif %} +{% endfor %} +``` + +Ahora, simplemente establece el atributo `tokenizer.chat_template`. ยกLa prรณxima vez que uses [`~PreTrainedTokenizer.apply_chat_template`], se utilizarรก tu nueva plantilla! Este atributo se guardarรก en el archivo tokenizer_config.json, por lo que puedes usar [`~utils.PushToHubMixin.push_to_hub`] para cargar tu nueva plantilla en el Hub y asegurarte de que todos estรฉn utilizando la plantilla correcta para tu modelo. + +```python +template = tokenizer.chat_template +template = template.replace("SYS", "SYSTEM") # Change the system token +tokenizer.chat_template = template # Set the new template +tokenizer.push_to_hub("model_name") # Upload your new template to the Hub! +``` + +El mรฉtodo [`~PreTrainedTokenizer.apply_chat_template`], que utiliza tu plantilla de chat, es llamado por la clase [`TextGenerationPipeline`], asรญ que una vez que configures la plantilla de chat correcta, tu modelo se volverรก automรกticamente compatible con [`TextGenerationPipeline`]. + + + +Si estรกs ajustando finamente un modelo para chat, ademรกs de establecer una plantilla de chat, probablemente deberรญas agregar cualquier nuevo token de control de chat como los tokens especiales en el tokenizador. Los tokens especiales nunca se dividen, asegurando que tus tokens de control siempre se manejen como tokens รบnicos en lugar de ser tokenizados en piezas. Tambiรฉn deberรญas establecer el atributo `eos_token` del tokenizador con el token que marca el final de las generaciones del asistente en tu plantilla. Esto asegurarรก que las herramientas de generaciรณn de texto puedan determinar correctamente cuรกndo detener la generaciรณn de texto. + + + +### ยฟQuรฉ plantilla deberรญa usar? + +Cuando establezcas la plantilla para un modelo que ya ha sido entrenado para chat, debes asegurarte de que la plantilla coincida exactamente con el formato de mensajes que el modelo vio durante el entrenamiento, o de lo contrario es probable que experimentes degradaciรณn del rendimiento. Esto es cierto incluso si estรกs entrenando aรบn mรกs el modelo; probablemente obtendrรกs el mejor rendimiento si mantienes constantes los tokens de chat. Esto es muy anรกlogo a la tokenizaciรณn: generalmente obtienes el mejor rendimiento para la inferencia o el ajuste fino cuando coincides precisamente con la tokenizaciรณn utilizada durante el entrenamiento. + +Si estรกs entrenando un modelo desde cero o ajustando finamente un modelo de lenguaje base para chat, por otro lado, ยกtienes mucha libertad para elegir una plantilla apropiada! Los LLM son lo suficientemente inteligentes como para aprender a manejar muchos formatos de entrada diferentes. Nuestra plantilla predeterminada para modelos que no tienen una plantilla especรญfica de clase sigue el formato ChatML, y esta es una buena elecciรณn flexible para muchos casos de uso. Se ve asรญ: + +``` +{% for message in messages %} + {{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}} +{% endfor %} +``` + +Si te gusta esta plantilla, aquรญ estรก en forma de una sola lรญnea, lista para copiar en tu cรณdigo. La versiรณn de una sola lรญnea tambiรฉn incluye un prรกctico soporte para [prompts de generaciรณn](#ยฟQuรฉ-son-los-"generation-prompts"?), ยกpero ten en cuenta que no aรฑade tokens de BOS o EOS! Si tu modelo espera esos tokens, no se agregarรกn automรกticamente por `apply_chat_template`, en otras palabras, el texto serรก tokenizado con `add_special_tokens=False`. Esto es para evitar posibles conflictos entre la plantilla y la lรณgica de `add_special_tokens`. ยกSi tu modelo espera tokens especiales, asegรบrate de aรฑadirlos a la plantilla! + +```python +tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}" +``` + +Esta plantilla envuelve cada mensaje en tokens `<|im_start|>` y `<|im_end|>`, y simplemente escribe el rol como una cadena, lo que permite flexibilidad en los roles con los que entrenas. La salida se ve asรญ: + +```text +<|im_start|>system +You are a helpful chatbot that will do its best not to say anything so stupid that people tweet about it.<|im_end|> +<|im_start|>user +How are you?<|im_end|> +<|im_start|>assistant +I'm doing great!<|im_end|> +``` + +Los roles "usuario", "sistema" y "asistente" son los estรกndar para chat, y recomendamos usarlos cuando tenga sentido, particularmente si deseas que tu modelo funcione bien con [`TextGenerationPipeline`]. Sin embargo, no estรกs limitado a estos roles: la plantilla es extremadamente flexible y cualquier cadena puede ser un rol. + +### ยกQuiero aรฑadir algunas plantillas de chat! ยฟCรณmo debo empezar? + +Si tienes algรบn modelo de chat, debes establecer su atributo `tokenizer.chat_template` y probarlo usando [`~PreTrainedTokenizer.apply_chat_template`], luego subir el tokenizador actualizado al Hub. Esto se aplica incluso si no eres el propietario del modelo: si estรกs usando un modelo con una plantilla de chat vacรญa o que todavรญa estรก utilizando la plantilla predeterminada de clase, por favor abre una solicitud de extracciรณn [pull request](https://huggingface.co/docs/hub/repositories-pull-requests-discussions) al repositorio del modelo para que este atributo se pueda establecer correctamente. + +Una vez que se establece el atributo, ยกeso es todo, has terminado! `tokenizer.apply_chat_template` ahora funcionarรก correctamente para ese modelo, ยกlo que significa que tambiรฉn es compatible automรกticamente en lugares como `TextGenerationPipeline`! + +Al asegurarnos de que los modelos tengan este atributo, podemos garantizar que toda la comunidad pueda utilizar todo el poder de los modelos de cรณdigo abierto. Los desajustes de formato han estado acechando el campo y daรฑando silenciosamente el rendimiento durante demasiado tiempo: ยกes hora de ponerles fin! + +## Avanzado: Consejos para escribir plantillas + +Si no estรกs familiarizado con Jinja, generalmente encontramos que la forma mรกs fรกcil de escribir una plantilla de chat es primero escribir un script de Python corto que formatee los mensajes como desees, y luego convertir ese script en una plantilla. + +Recuerda que el manejador de plantillas recibirรก el historial de conversaciรณn como una variable llamada mensajes. Cada mensaje es un diccionario con dos claves, `role` y `content`. Podrรกs acceder a los `mensajes` en tu plantilla tal como lo harรญas en Python, lo que significa que puedes recorrerlo con `{% for message in messages %}` o acceder a mensajes individuales con, por ejemplo, `{{ messages[0] }}`. + +Tambiรฉn puedes usar los siguientes consejos para convertir tu cรณdigo a Jinja: + +### Bucles For + +Los bucles For en Jinja se ven asรญ: + +``` +{% for message in messages %} +{{ message['content'] }} +{% endfor %} +``` + +Ten en cuenta que todo lo que estรฉ dentro del {{bloque de expresiรณn}} se imprimirรก en la salida. Puedes usar operadores como `+` para combinar cadenas dentro de bloques de expresiรณn. + +### Declaraciones if + +Las declaraciones if en Jinja se ven asรญ: + +``` +{% if message['role'] == 'user' %} +{{ message['content'] }} +{% endif %} +``` + +Observa cรณmo donde Python utiliza espacios en blanco para marcar el inicio y el final de los bloques `for` e `if`, Jinja requiere que los termines explรญcitamente con `{% endfor %}` y `{% endif %}`. + +### Variables especiales + +Dentro de tu plantilla, tendrรกs acceso a la lista de `mensajes`, pero tambiรฉn puedes acceder a varias otras variables especiales. Estas incluyen tokens especiales como `bos_token` y `eos_token`, asรญ como la variable `add_generation_prompt` que discutimos anteriormente. Tambiรฉn puedes usar la variable `loop` para acceder a informaciรณn sobre la iteraciรณn actual del bucle, por ejemplo, usando `{% if loop.last %}` para verificar si el mensaje actual es el รบltimo mensaje en la conversaciรณn. Aquรญ tienes un ejemplo que combina estas ideas para agregar un prompt de generaciรณn al final de la conversaciรณn si add_generation_prompt es `True`: + +``` +{% if loop.last and add_generation_prompt %} +{{ bos_token + 'Assistant:\n' }} +{% endif %} +``` + +### Notas sobre los espacios en blanco + +Hemos intentado que Jinja ignore los espacios en blanco fuera de las {{expresiones}} tanto como sea posible. Sin embargo, ten en cuenta que Jinja es un motor de plantillas de propรณsito general y puede tratar el espacio en blanco entre bloques en la misma lรญnea como significativo e imprimirlo en la salida. ยกTe recomendamos **encarecidamente** que verifiques que tu plantilla no estรฉ imprimiendo espacios adicionales donde no deberรญa antes de subirla! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/community.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/community.md new file mode 100644 index 0000000000000000000000000000000000000000..71153fbc8336f629f7db3cb32277c07a099b650f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/community.md @@ -0,0 +1,69 @@ + + +# Comunidad + +Esta pรกgina agrupa los recursos de ๐Ÿค— Transformers desarrollados por la comunidad. + +## Los recursos de la comunidad: + +| Recurso | Descripciรณn | Autor | +|:----------|:-------------|------:| +| [Hugging Face Transformers Glossary Flashcards](https://www.darigovresearch.com/huggingface-transformers-glossary-flashcards) | Un conjunto de flashcards basadas en el [Glosario de documentos de Transformers] (glosario) que se ha puesto en un formato que se puede aprender/revisar fรกcilmente usando [Anki](https://apps.ankiweb.net/) una fuente abierta, aplicaciรณn de multiplataforma diseรฑada especรญficamente para la retenciรณn de conocimientos a largo plazo. Ve este [Introductory video on how to use the flashcards](https://www.youtube.com/watch?v=Dji_h7PILrw). | [Darigov Research](https://www.darigovresearch.com/) | + +## Los cuadernos de la comunidad: + +| Cuaderno | Descripciรณn | Autor | | +|:----------|:-------------|:-------------|------:| +| [Ajustar un transformador preentrenado para generar letras](https://github.com/AlekseyKorshuk/huggingartists) | Cรณmo generar letras al estilo de tu artista favorito ajustando un modelo GPT-2 | [Aleksey Korshuk](https://github.com/AlekseyKorshuk) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AlekseyKorshuk/huggingartists/blob/master/huggingartists-demo.ipynb) | +| [Entrenar T5 en Tensorflow 2](https://github.com/snapthat/TF-T5-text-to-text) | Cรณmo entrenar a T5 para cualquier tarea usando Tensorflow 2. Este cuaderno demuestra una tarea de preguntas y respuestas implementada en Tensorflow 2 usando SQUAD | [Muhammad Harris](https://github.com/HarrisDePerceptron) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/snapthat/TF-T5-text-to-text/blob/master/snapthatT5/notebooks/TF-T5-Datasets%20Training.ipynb) | +| [Entrenar T5 en TPU](https://github.com/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb) | Cรณmo entrenar a T5 en SQUAD con Transformers y Nlp | [Suraj Patil](https://github.com/patil-suraj) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb#scrollTo=QLGiFCDqvuil) | +| [Ajustar T5 para Clasificaciรณn y Opciรณn Mรบltiple](https://github.com/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | Cรณmo ajustar T5 para clasificaciรณn y tareas de opciรณn mรบltiple usando un formato de texto a texto con PyTorch Lightning | [Suraj Patil](https://github.com/patil-suraj) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | +| [Ajustar DialoGPT en nuevos conjuntos de datos e idiomas](https://github.com/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | Cรณmo ajustar el modelo DialoGPT en un nuevo conjunto de datos para chatbots conversacionales de diรกlogo abierto | [Nathan Cooper](https://github.com/ncoop57) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | +| [Modelado de secuencias largas con Reformer](https://github.com/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | Cรณmo entrenar en secuencias de hasta 500,000 tokens con Reformer | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | +| [Ajustar BART para resumir](https://github.com/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb) | Cรณmo ajustar BART para resumir con fastai usando blurr | [Wayde Gilliam](https://ohmeow.com/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb) | +| [Ajustar un Transformador previamente entrenado en los tweets de cualquier persona](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | Cรณmo generar tweets al estilo de tu cuenta de Twitter favorita ajustando un modelo GPT-2 | [Boris Dayma](https://github.com/borisdayma) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | +| [Optimizar ๐Ÿค— modelos de Hugging Face con pesos y sesgos](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | Un tutorial completo que muestra la integraciรณn de W&B con Hugging Face | [Boris Dayma](https://github.com/borisdayma) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | +| [Preentrenar Longformer](https://github.com/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | Cรณmo construir una versiรณn "larga" de modelos preentrenados existentes | [Iz Beltagy](https://beltagy.net) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | +| [Ajustar Longformer para control de calidad](https://github.com/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | Cรณmo ajustar el modelo antiguo para la tarea de control de calidad | [Suraj Patil](https://github.com/patil-suraj) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | +| [Evaluar modelo con ๐Ÿค—nlp](https://github.com/patrickvonplaten/notebooks/blob/master/How_to_evaluate_Longformer_on_TriviaQA_using_NLP.ipynb) | Cรณmo evaluar longformer en TriviaQA con `nlp` | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1m7eTGlPmLRgoPkkA7rkhQdZ9ydpmsdLE?usp=sharing) | +| [Ajustar fino de T5 para la extracciรณn de amplitud de opiniรณn](https://github.com/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | Cรณmo ajustar T5 para la extracciรณn de intervalos de opiniones mediante un formato de texto a texto con PyTorch Lightning | [Lorenzo Ampil](https://github.com/enzoampil) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | +| [Ajustar fino de DistilBert para la clasificaciรณn multiclase](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb) | Cรณmo ajustar DistilBert para la clasificaciรณn multiclase con PyTorch | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb)| +|[Ajustar BERT para la clasificaciรณn de etiquetas mรบltiples](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)| Cรณmo ajustar BERT para la clasificaciรณn de mรบltiples etiquetas usando PyTorch |[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)| +|[Ajustar T5 para resumir](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb)| Cรณmo ajustar T5 para resumir en PyTorch y realizar un seguimiento de los experimentos con WandB |[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb)| +|[Acelerar el ajuste fino en transformadores con Dynamic Padding/Bucketing](https://github.com/ELS-RD/transformers-notebook/blob/master/Divide_Hugging_Face_Transformers_training_time_by_2_or_more.ipynb)| Cรณmo acelerar el ajuste fino en un factor de 2 usando relleno dinรกmico/cubetas |[Michael Benesty](https://github.com/pommedeterresautee) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1CBfRU1zbfu7-ijiOqAAQUA-RJaxfcJoO?usp=sharing)| +|[Preentrenar Reformer para modelado de lenguaje enmascarado](https://github.com/patrickvonplaten/notebooks/blob/master/Reformer_For_Masked_LM.ipynb)| Cรณmo entrenar un modelo Reformer con capas de autoatenciรณn bidireccionales | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1tzzh0i8PgDQGV3SMFUGxM7_gGae3K-uW?usp=sharing)| +|[Ampliar y ajustar Sci-BERT](https://github.com/lordtt13/word-embeddings/blob/master/COVID-19%20Research%20Data/COVID-SciBERT.ipynb)| Cรณmo aumentar el vocabulario de un modelo SciBERT preentrenado de AllenAI en el conjunto de datos CORD y canalizarlo. | [Tanmay Thakur](https://github.com/lordtt13) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1rqAR40goxbAfez1xvF3hBJphSCsvXmh8)| +|[Ajustar fino de BlenderBotSmall para resรบmenes usando la API de Entrenador](https://github.com/lordtt13/transformers-experiments/blob/master/Custom%20Tasks/fine-tune-blenderbot_small-for-summarization.ipynb)| Cรณmo ajustar BlenderBotSmall para resumir en un conjunto de datos personalizado, utilizando la API de Entrenador. | [Tanmay Thakur](https://github.com/lordtt13) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19Wmupuls7mykSGyRN_Qo6lPQhgp56ymq?usp=sharing)| +|[Ajustar Electra e interpreta con gradientes integrados](https://github.com/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb) | Cรณmo ajustar Electra para el anรกlisis de sentimientos e interpretar predicciones con Captum Integrated Gradients | [Eliza Szczechla](https://elsanns.github.io) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb)| +|[ajustar un modelo GPT-2 que no estรก en inglรฉs con la clase Trainer](https://github.com/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb) | Cรณmo ajustar un modelo GPT-2 que no estรก en inglรฉs con la clase Trainer | [Philipp Schmid](https://www.philschmid.de) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb)| +|[Ajustar un modelo DistilBERT para la tarea de clasificaciรณn de mรบltiples etiquetas](https://github.com/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb) | Cรณmo ajustar un modelo DistilBERT para la tarea de clasificaciรณn de mรบltiples etiquetas | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb)| +|[Ajustar ALBERT para la clasificaciรณn de pares de oraciones](https://github.com/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb) | Cรณmo ajustar un modelo ALBERT u otro modelo basado en BERT para la tarea de clasificaciรณn de pares de oraciones | [Nadir El Manouzi](https://github.com/NadirEM) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb)| +|[Ajustar a Roberta para el anรกlisis de sentimientos](https://github.com/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb) | Cรณmo ajustar un modelo de Roberta para el anรกlisis de sentimientos | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb)| +|[Evaluaciรณn de modelos de generaciรณn de preguntas](https://github.com/flexudy-pipe/qugeev) | ยฟQuรฉ tan precisas son las respuestas a las preguntas generadas por tu modelo de transformador seq2seq? | [Pascal Zoleko](https://github.com/zolekode) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bpsSqCQU-iw_5nNoRm_crPq6FRuJthq_?usp=sharing)| +|[Clasificar texto con DistilBERT y Tensorflow](https://github.com/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb) | Cรณmo ajustar DistilBERT para la clasificaciรณn de texto en TensorFlow | [Peter Bayerle](https://github.com/peterbayerle) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb)| +|[Aprovechar BERT para el resumen de codificador y decodificador en CNN/Dailymail](https://github.com/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb) | Cรณmo iniciar en caliente un *EncoderDecoderModel* con un punto de control *google-bert/bert-base-uncased* para resumir en CNN/Dailymail | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)| +|[Aprovechar RoBERTa para el resumen de codificador-decodificador en BBC XSum](https://github.com/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb) | Cรณmo iniciar en caliente un *EncoderDecoderModel* compartido con un punto de control *FacebookAI/roberta-base* para resumir en BBC/XSum | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb)| +|[Ajustar TAPAS en Sequential Question Answering (SQA)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) | Cรณmo ajustar *TapasForQuestionAnswering* con un punto de control *tapas-base* en el conjunto de datos del Sequential Question Answering (SQA) | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb)| +|[Evaluar TAPAS en Table Fact Checking (TabFact)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb) | Cรณmo evaluar un *TapasForSequenceClassification* ajustado con un punto de control *tapas-base-finetuned-tabfact* usando una combinaciรณn de ๐Ÿค— conjuntos de datos y ๐Ÿค— bibliotecas de transformadores | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb)| +|[Ajustar de mBART para traducciรณn](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb) | Cรณmo ajustar mBART utilizando Seq2SeqTrainer para la traducciรณn del hindi al inglรฉs | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)| +|[Ajustar LayoutLM en FUNSD (a form understanding dataset)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb) | Cรณmo ajustar *LayoutLMForTokenClassification* en el conjunto de datos de FUNSD para la extracciรณn de informaciรณn de documentos escaneados | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb)| +|[Ajustar DistilGPT2 y genere texto](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb) | Cรณmo ajustar DistilGPT2 y generar texto | [Aakash Tripathi](https://github.com/tripathiaakash) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb)| +|[Ajustar LED en tokens de hasta 8K](https://github.com/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb) | Cรณmo ajustar LED en pubmed para resรบmenes de largo alcance | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb)| +|[Evaluar LED en Arxiv](https://github.com/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb) | Cรณmo evaluar efectivamente LED en resรบmenes de largo alcance | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb)| +|[Ajustar fino de LayoutLM en RVL-CDIP (un conjunto de datos de clasificaciรณn de imรกgenes de documentos)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb) | Cรณmo ajustar *LayoutLMForSequenceClassification* en el conjunto de datos RVL-CDIP para la clasificaciรณn de documentos escaneados | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb)| +|[Decodificaciรณn Wav2Vec2 CTC con ajuste GPT2](https://github.com/voidful/huggingface_notebook/blob/main/xlsr_gpt.ipynb) | Cรณmo decodificar la secuencia CTC con el ajuste del modelo de lenguaje | [Eric Lam](https://github.com/voidful) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing)| +|[Ajustar BART para resรบmenes en dos idiomas con la clase Trainer](https://github.com/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb) | Cรณmo ajustar BART para resรบmenes en dos idiomas con la clase Trainer | [Eliza Szczechla](https://github.com/elsanns) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)| +|[Evaluar Big Bird en Trivia QA](https://github.com/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb) | Cรณmo evaluar BigBird en respuesta a preguntas de documentos largos en Trivia QA | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb)| +| [Crear subtรญtulos de video usando Wav2Vec2](https://github.com/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | Cรณmo crear subtรญtulos de YouTube a partir de cualquier vรญdeo transcribiendo el audio con Wav2Vec | [Niklas Muennighoff](https://github.com/Muennighoff) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | +| [Ajustar el transformador de visiรณn en CIFAR-10 usando PyTorch Lightning](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | Cรณmo ajustar el transformador de visiรณn (ViT) en CIFAR-10 usando transformadores HuggingFace, conjuntos de datos y PyTorch Lightning | [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | +| [Ajustar el Transformador de visiรณn en CIFAR-10 usando el ๐Ÿค— Entrenador](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | Cรณmo ajustar el Vision Transformer (ViT) en CIFAR-10 usando HuggingFace Transformers, Datasets y el ๐Ÿค— Trainer | [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | +| [Evaluar LUKE en Open Entity, un conjunto de datos de tipificaciรณn de entidades](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | Cรณmo evaluar *LukeForEntityClassification* en el conjunto de datos de entidad abierta | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | +| [Evaluar LUKE en TACRED, un conjunto de datos de extracciรณn de relaciones](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | Cรณmo evaluar *LukeForEntityPairClassification* en el conjunto de datos TACRED | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | +| [Evaluar LUKE en CoNLL-2003, un punto de referencia importante de NER](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | Cรณmo evaluar *LukeForEntitySpanClassification* en el conjunto de datos CoNLL-2003 | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | +| [Evaluar BigBird-Pegasus en el conjunto de datos de PubMed](https://github.com/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | Cรณmo evaluar *BigBirdPegasusForConditionalGeneration* en el conjunto de datos de PubMed | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | +| [Clasificaciรณn de emociones del habla con Wav2Vec2](https://github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | Cรณmo aprovechar un modelo Wav2Vec2 preentrenado para la clasificaciรณn de emociones en el conjunto de datos MEGA | [Mehrdad Farahani](https://github.com/m3hrdadfi) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | +| [Detectar objetos en una imagen con DETR](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | Cรณmo usar un modelo entrenado *DetrForObjectDetection* para detectar objetos en una imagen y visualizar la atenciรณn | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | +| [Ajustar el DETR en un conjunto de datos de detecciรณn de objetos personalizados](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | Cรณmo ajustar *DetrForObjectDetection* en un conjunto de datos de detecciรณn de objetos personalizados | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | +| [Ajustar T5 para el reconocimiento de entidades nombradas](https://github.com/ToluClassics/Notebooks/blob/main/T5_Ner_Finetuning.ipynb) | Cรณmo ajustar *T5* en una tarea de reconocimiento de entidad nombrada | [Ogundepo Odunayo](https://github.com/ToluClassics) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1obr78FY_cBmWY5ODViCmzdY6O1KB65Vc?usp=sharing) | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/converting_tensorflow_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/converting_tensorflow_models.md new file mode 100644 index 0000000000000000000000000000000000000000..290f325b96c74bd9ddb3a69c9b78a59e57f1c729 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/converting_tensorflow_models.md @@ -0,0 +1,139 @@ + + +# Convertir checkpoints de Tensorflow + +Te proporcionamos una interfaz de lรญnea de comando (`CLI`, por sus siglas en inglรฉs) para convertir puntos de control (_checkpoints_) originales de Bert/GPT/GPT-2/Transformer-XL/XLNet/XLM en modelos que se puedan cargar utilizando los mรฉtodos `from_pretrained` de la biblioteca. + + + +Desde 2.3.0, el script para convertir es parte de la CLI de transformers (**transformers**) disponible en cualquier instalaciรณn de transformers >= 2.3.0. + +La siguiente documentaciรณn refleja el formato para el comando **transformers convert**. + + + +## BERT + +Puedes convertir cualquier checkpoint de TensorFlow para BERT (en particular, [los modelos pre-entrenados y publicados por Google](https://github.com/google-research/bert#pre-trained-models)) en un archivo de PyTorch mediante el script [convert_bert_original_tf_checkpoint_to_pytorch.py](https://github.com/huggingface/transformers/tree/main/src/transformers/models/bert/convert_bert_original_tf_checkpoint_to_pytorch.py). + +Esta CLI toma como entrada un checkpoint de TensorFlow (tres archivos que comienzan con `bert_model.ckpt`) y el archivo de configuraciรณn asociado (`bert_config.json`), y crea un modelo PyTorch para esta configuraciรณn, carga los pesos del checkpoint de TensorFlow en el modelo de PyTorch y guarda el modelo resultante en un archivo estรกndar de PyTorch que se puede importar usando `from_pretrained()` (ve el ejemplo en [Tour rรกpido](quicktour), [run_glue.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification/run_glue.py)). + +Solo necesitas ejecutar este script **una vez** para convertir un modelo a PyTorch. Despuรฉs, puedes ignorar el checkpoint de TensorFlow (los tres archivos que comienzan con `bert_model.ckpt`), pero asegรบrate de conservar el archivo de configuraciรณn (`bert_config.json`) y el archivo de vocabulario (`vocab.txt`) ya que estos tambiรฉn son necesarios para el modelo en PyTorch. + +Para ejecutar este script deberรกs tener instalado TensorFlow y PyTorch (`pip install tensorflow`). El resto del repositorio solo requiere PyTorch. + +Aquรญ hay un ejemplo del proceso para convertir un modelo `BERT-Base Uncased` pre-entrenado: + +```bash +export BERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12 + +transformers convert --model_type bert \ + --tf_checkpoint $BERT_BASE_DIR/bert_model.ckpt \ + --config $BERT_BASE_DIR/bert_config.json \ + --pytorch_dump_output $BERT_BASE_DIR/pytorch_model.bin +``` + +Puedes descargar los modelos pre-entrenados de Google para la conversiรณn [aquรญ](https://github.com/google-research/bert#pre-trained-models). + +## ALBERT + +Convierte los checkpoints del modelo ALBERT de TensorFlow a PyTorch usando el script [convert_albert_original_tf_checkpoint_to_pytorch.py](https://github.com/huggingface/transformers/tree/main/src/transformers/models/albert/convert_albert_original_tf_checkpoint_to_pytorch.py). + +La CLI toma como entrada un checkpoint de TensorFlow (tres archivos que comienzan con `model.ckpt-best`) y el archivo de configuraciรณn adjunto (`albert_config.json`), luego crea y guarda un modelo de PyTorch. Para ejecutar esta conversiรณn deberรกs tener instalados TensorFlow y PyTorch. + +Aquรญ hay un ejemplo del proceso para convertir un modelo `ALBERT Base` pre-entrenado: + +```bash +export ALBERT_BASE_DIR=/path/to/albert/albert_base + +transformers convert --model_type albert \ + --tf_checkpoint $ALBERT_BASE_DIR/model.ckpt-best \ + --config $ALBERT_BASE_DIR/albert_config.json \ + --pytorch_dump_output $ALBERT_BASE_DIR/pytorch_model.bin +``` + +Puedes descargar los modelos pre-entrenados de Google para la conversiรณn [aquรญ](https://github.com/google-research/albert#pre-trained-models). + +## OpenAI GPT + +Este es un ejemplo del proceso para convertir un modelo OpenAI GPT pre-entrenado, asumiendo que tu checkpoint de NumPy se guarda con el mismo formato que el modelo pre-entrenado de OpenAI (mรกs informaciรณn [aquรญ](https://github.com/openai/finetune-transformer-lm)): + +```bash +export OPENAI_GPT_CHECKPOINT_FOLDER_PATH=/path/to/openai/pretrained/numpy/weights + +transformers convert --model_type gpt \ + --tf_checkpoint $OPENAI_GPT_CHECKPOINT_FOLDER_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--config OPENAI_GPT_CONFIG] \ + [--finetuning_task_name OPENAI_GPT_FINETUNED_TASK] \ +``` + +## OpenAI GPT-2 + +Aquรญ hay un ejemplo del proceso para convertir un modelo OpenAI GPT-2 pre-entrenado (mรกs informaciรณn [aquรญ](https://github.com/openai/gpt-2)): + +```bash +export OPENAI_GPT2_CHECKPOINT_PATH=/path/to/openai-community/gpt2/pretrained/weights + +transformers convert --model_type gpt2 \ + --tf_checkpoint $OPENAI_GPT2_CHECKPOINT_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--config OPENAI_GPT2_CONFIG] \ + [--finetuning_task_name OPENAI_GPT2_FINETUNED_TASK] +``` + +## XLNet + +Aquรญ hay un ejemplo del proceso para convertir un modelo XLNet pre-entrenado: + +```bash +export TRANSFO_XL_CHECKPOINT_PATH=/path/to/xlnet/checkpoint +export TRANSFO_XL_CONFIG_PATH=/path/to/xlnet/config + +transformers convert --model_type xlnet \ + --tf_checkpoint $TRANSFO_XL_CHECKPOINT_PATH \ + --config $TRANSFO_XL_CONFIG_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--finetuning_task_name XLNET_FINETUNED_TASK] \ +``` + +## XLM + +Aquรญ hay un ejemplo del proceso para convertir un modelo XLM pre-entrenado: + +```bash +export XLM_CHECKPOINT_PATH=/path/to/xlm/checkpoint + +transformers convert --model_type xlm \ + --tf_checkpoint $XLM_CHECKPOINT_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT + [--config XML_CONFIG] \ + [--finetuning_task_name XML_FINETUNED_TASK] +``` + +## T5 + +Aquรญ hay un ejemplo del proceso para convertir un modelo T5 pre-entrenado: + +```bash +export T5=/path/to/t5/uncased_L-12_H-768_A-12 + +transformers convert --model_type t5 \ + --tf_checkpoint $T5/t5_model.ckpt \ + --config $T5/t5_config.json \ + --pytorch_dump_output $T5/pytorch_model.bin +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/create_a_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/create_a_model.md new file mode 100644 index 0000000000000000000000000000000000000000..560fbd74e3851c73afb4ddd999528267f321795c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/create_a_model.md @@ -0,0 +1,371 @@ + + +# Crea una arquitectura personalizada + +Una [`AutoClass`](model_doc/auto) infiere, automรกticamente, la arquitectura del modelo y descarga la configuraciรณn y los pesos del modelo preentrenado. Normalmente, recomendamos usar una `AutoClass` para producir un cรณdigo agnรณstico a puntos de guardado o checkpoints. Sin embargo, los usuarios que quieran mรกs control sobre los parรกmetros especรญficos de los modelos pueden crear su propio modelo ๐Ÿค— Transformers personalizado a partir de varias clases base. Esto puede ser particularmente รบtil para alguien que estรฉ interesado en estudiar, entrenar o experimentar con modelos ๐Ÿค— Transformers. En esta guรญa vamos a profundizar en la creaciรณn de modelos personalizados sin usar `AutoClass`. Aprenderemos a: + +- Cargar y personalizar una configuraciรณn para un modelo. +- Crear una arquitectura para un modelo. +- Crear tokenizadores rรกpidos y lentos para textos. +- Crear un extractor de propiedades para tareas de audio o imรกgenes. +- Crear un procesador para tareas multimodales. + +## Configuraciรณn + +Una [configuraciรณn](main_classes/configuration) es un conjunto de atributos especรญficos de un modelo. Cada configuraciรณn de modelo tiene atributos diferentes. Por ejemplo, todos los modelos de PLN tienen los atributos `hidden_size`, `num_attention_heads`, `num_hidden_layers` y `vocab_size` en comรบn. Estos atributos especifican el nรบmero de cabezas de atenciรณn o de capas ocultas con las que se construyen los modelos. + +Puedes echarle un vistazo a [DistilBERT](model_doc/distilbert) y sus atributos accediendo a [`DistilBertConfig`]: + +```py +>>> from transformers import DistilBertConfig + +>>> config = DistilBertConfig() +>>> print(config) +DistilBertConfig { + "activation": "gelu", + "attention_dropout": 0.1, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +[`DistilBertConfig`] muestra todos los atributos por defecto que se han usado para construir un modelo [`DistilBertModel`] base. Todos ellos son personalizables, lo que deja espacio para poder experimentar. Por ejemplo, puedes personalizar un modelo predeterminado para: + +- Probar una funciรณn de activaciรณn diferente, usando el parรกmetro `activation`. +- Usar un valor de abandono (tambiรฉn conocido como _dropout_) mรกs alto para las probabilidades de las capas de atenciรณn, usando el parรกmetro `attention_dropout`. + +```py +>>> my_config = DistilBertConfig(activation="relu", attention_dropout=0.4) +>>> print(my_config) +DistilBertConfig { + "activation": "relu", + "attention_dropout": 0.4, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +Los atributos de los modelos preentrenados pueden ser modificados con la funciรณn [`~PretrainedConfig.from_pretrained`]: + +```py +>>> my_config = DistilBertConfig.from_pretrained("distilbert/distilbert-base-uncased", activation="relu", attention_dropout=0.4) +``` + +Cuando estรฉs satisfecho con la configuraciรณn de tu modelo, puedes guardarlo con la funciรณn [`~PretrainedConfig.save_pretrained`]. Tu configuraciรณn se guardarรก en un archivo JSON dentro del directorio que le especifiques como parรกmetro. + +```py +>>> my_config.save_pretrained(save_directory="./your_model_save_path") +``` + +Para volver a usar el archivo de configuraciรณn, puedes cargarlo usando [`~PretrainedConfig.from_pretrained`]: + +```py +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +``` + + + +Tambiรฉn puedes guardar los archivos de configuraciรณn como un diccionario; o incluso guardar solo la diferencia entre tu archivo personalizado y la configuraciรณn por defecto. Consulta la [documentaciรณn sobre configuraciรณn](main_classes/configuration) para ver mรกs detalles. + + + +## Modelo + +El siguiente paso serรก crear un [modelo](main_classes/models). El modelo, al que a veces tambiรฉn nos referimos como arquitectura, es el encargado de definir cada capa y quรฉ operaciones se realizan. Los atributos como `num_hidden_layers` de la configuraciรณn se usan para definir la arquitectura. Todos los modelos comparten una clase base, [`PreTrainedModel`], y algunos mรฉtodos comunes que se pueden usar para redimensionar los _embeddings_ o para recortar cabezas de auto-atenciรณn (tambiรฉn llamadas _self-attention heads_). Ademรกs, todos los modelos son subclases de [`torch.nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html), [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) o [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html), lo que significa que son compatibles con su respectivo framework. + + + + +Carga los atributos de tu configuraciรณn personalizada en el modelo de la siguiente forma: + +```py +>>> from transformers import DistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +>>> model = DistilBertModel(my_config) +``` + +Esto crea un modelo con valores aleatorios, en lugar de crearlo con los pesos del preentrenamiento, por lo que no serรกs capaz de usar este modelo para nada รบtil hasta que no lo entrenes. El entrenamiento es un proceso costoso, tanto en cuestiรณn de recursos como de tiempo, por lo que generalmente es mejor usar un modelo preentrenado para obtener mejores resultados mรกs rรกpido, consumiendo una fracciรณn de los recursos que un entrenamiento completo hubiera requerido. + +Puedes crear un modelo preentrenado con [`~PreTrainedModel.from_pretrained`]: + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Cuando cargues tus pesos del preentrenamiento, el modelo por defecto se carga automรกticamente si nos lo proporciona ๐Ÿค— Transformers. Sin embargo, siempre puedes reemplazar (todos o algunos de) los atributos del modelo por defecto por los tuyos: + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + + +Carga los atributos de tu configuraciรณn personalizada en el modelo de la siguiente forma: + +```py +>>> from transformers import TFDistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +>>> tf_model = TFDistilBertModel(my_config) +``` + +Esto crea un modelo con valores aleatorios, en lugar de crearlo con los pesos del preentrenamiento, por lo que no serรกs capaz de usar este modelo para nada รบtil hasta que no lo entrenes. El entrenamiento es un proceso costoso, tanto en cuestiรณn de recursos como de tiempo, por lo que generalmente es mejor usar un modelo preentrenado para obtener mejores resultados mรกs rรกpido, consumiendo solo una fracciรณn de los recursos que un entrenamiento completo hubiera requerido. + +Puedes crear un modelo preentrenado con [`~TFPreTrainedModel.from_pretrained`]: + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Cuando cargues tus pesos del preentrenamiento, el modelo por defecto se carga automรกticamente si este nos lo proporciona ๐Ÿค— Transformers. Sin embargo, siempre puedes reemplazar (todos o algunos de) los atributos del modelo por defecto por los tuyos: + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + + +### Cabezas de modelo + +En este punto del tutorial, tenemos un modelo DistilBERT base que devuelve los *hidden states* o estados ocultos. Los *hidden states* se pasan como parรกmetros de entrada a la cabeza del modelo para producir la salida. ๐Ÿค— Transformers ofrece una cabeza de modelo diferente para cada tarea, siempre y cuando el modelo sea compatible para la tarea (por ejemplo, no puedes usar DistilBERT para una tarea secuencia a secuencia como la traducciรณn). + + + + + +Por ejemplo, [`DistilBertForSequenceClassification`] es un modelo DistilBERT base con una cabeza de clasificaciรณn de secuencias. La cabeza de clasificaciรณn de secuencias es una capa superior que precede a la recolecciรณn de las salidas. + +```py +>>> from transformers import DistilBertForSequenceClassification + +>>> model = DistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Puedes reutilizar este punto de guardado o *checkpoint* para otra tarea fรกcilmente cambiando a una cabeza de un modelo diferente. Para una tarea de respuesta a preguntas, puedes usar la cabeza del modelo [`DistilBertForQuestionAnswering`]. La cabeza de respuesta a preguntas es similar a la de clasificaciรณn de secuencias, excepto porque consta de una capa lineal delante de la salida de los *hidden states*. + + +```py +>>> from transformers import DistilBertForQuestionAnswering + +>>> model = DistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +Por ejemplo, [`TFDistilBertForSequenceClassification`] es un modelo DistilBERT base con una cabeza de clasificaciรณn de secuencias. La cabeza de clasificaciรณn de secuencias es una capa superior que precede a la recolecciรณn de las salidas. + +```py +>>> from transformers import TFDistilBertForSequenceClassification + +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Puedes reutilizar este punto de guardado o *checkpoint* para otra tarea fรกcilmente cambiando a una cabeza de un modelo diferente. Para una tarea de respuesta a preguntas, puedes usar la cabeza del modelo [`TFDistilBertForQuestionAnswering`]. La cabeza de respuesta a preguntas es similar a la de clasificaciรณn de secuencias, excepto porque consta de una capa lineal delante de la salida de los *hidden states*. + + +```py +>>> from transformers import TFDistilBertForQuestionAnswering + +>>> tf_model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +## Tokenizer + +La ultima clase base que debes conocer antes de usar un modelo con datos textuales es la clase [tokenizer](main_classes/tokenizer), que convierte el texto bruto en tensores. Hay dos tipos de *tokenizers* que puedes usar con ๐Ÿค— Transformers: + +- [`PreTrainedTokenizer`]: una implementaciรณn de un *tokenizer* hecha en Python. +- [`PreTrainedTokenizerFast`]: un *tokenizer* de nuestra librerรญa [๐Ÿค— Tokenizer](https://huggingface.co/docs/tokenizers/python/latest/), basada en Rust. Este tipo de *tokenizer* es bastante mรกs rรกpido, especialmente durante la tokenizaciรณn por lotes, gracias a estar implementado en Rust. Esta rรกpida tokenizaciรณn tambiรฉn ofrece mรฉtodos adicionales como el *offset mapping*, que relaciona los tokens con sus palabras o caracteres originales. + +Ambos *tokenizers* son compatibles con los mรฉtodos comunes, como los de encodificaciรณn y decodificaciรณn, los mรฉtodos para aรฑadir tokens y aquellos que manejan tokens especiales. + + + +No todos los modelos son compatibles con un *tokenizer* rรกpido. ร‰chale un vistazo a esta [tabla](index#supported-frameworks) para comprobar si un modelo especรญfico es compatible con un *tokenizer* rรกpido. + + + +Si has entrenado tu propio *tokenizer*, puedes crear uno desde tu archivo de โ€œvocabularioโ€: + +```py +>>> from transformers import DistilBertTokenizer + +>>> my_tokenizer = DistilBertTokenizer(vocab_file="my_vocab_file.txt", do_lower_case=False, padding_side="left") +``` + +Es importante recordar que los vocabularios que provienen de un *tokenizer* personalizado serรกn diferentes a los vocabularios generados por el *tokenizer* de un modelo preentrenado. Debes usar el vocabulario de un *tokenizer* preentrenado si vas a usar un modelo preentrenado, de lo contrario las entradas no tendrรกn sentido. Crea un *tokenizer* con el vocabulario de un modelo preentrenado usando la clase [`DistilBertTokenizer`]: + + +```py +>>> from transformers import DistilBertTokenizer + +>>> slow_tokenizer = DistilBertTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Crea un *tokenizer* rรกpido con la clase [`DistilBertTokenizerFast`]: + + +```py +>>> from transformers import DistilBertTokenizerFast + +>>> fast_tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +Por defecto, el [`AutoTokenizer`] intentarรก cargar un *tokenizer* rรกpido. Puedes desactivar este comportamiento cambiando el parรกmetro `use_fast=False` de `from_pretrained`. + + + + +## Extractor de Caracterรญsticas + +Un extractor de caracterรญsticas procesa entradas de audio e imagen. Hereda de la clase base [`~feature_extraction_utils.FeatureExtractionMixin`] y tambiรฉn puede heredar de la clase [`ImageFeatureExtractionMixin`] para el procesamiento de caracterรญsticas de las imรกgenes o de la clase [`SequenceFeatureExtractor`] para el procesamiento de entradas de audio. + +Dependiendo de si trabajas en una tarea de audio o de video, puedes crear un extractor de caracterรญsticas asociado al modelo que estรฉs usando. Por ejemplo, podrรญas crear un [`ViTFeatureExtractor`] por defecto si estรกs usando [ViT](model_doc/vit) para clasificaciรณn de imรกgenes: + +```py +>>> from transformers import ViTFeatureExtractor + +>>> vit_extractor = ViTFeatureExtractor() +>>> print(vit_extractor) +ViTFeatureExtractor { + "do_normalize": true, + "do_resize": true, + "feature_extractor_type": "ViTFeatureExtractor", + "image_mean": [ + 0.5, + 0.5, + 0.5 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": 2, + "size": 224 +} +``` + + + +Si no estรกs buscando ninguna personalizaciรณn en especรญfico, usa el mรฉtodo `from_pretrained` para cargar los parรกmetros del extractor de caracterรญsticas por defecto del modelo. + + + +Puedes modificar cualquier parรกmetro de [`ViTFeatureExtractor`] para crear tu extractor de caracterรญsticas personalizado: + +```py +>>> from transformers import ViTFeatureExtractor + +>>> my_vit_extractor = ViTFeatureExtractor(resample="PIL.Image.BOX", do_normalize=False, image_mean=[0.3, 0.3, 0.3]) +>>> print(my_vit_extractor) +ViTFeatureExtractor { + "do_normalize": false, + "do_resize": true, + "feature_extractor_type": "ViTFeatureExtractor", + "image_mean": [ + 0.3, + 0.3, + 0.3 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": "PIL.Image.BOX", + "size": 224 +} +``` + +Para las entradas de audio, puedes crear un [`Wav2Vec2FeatureExtractor`] y personalizar los parรกmetros de una forma similar: + + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> w2v2_extractor = Wav2Vec2FeatureExtractor() +>>> print(w2v2_extractor) +Wav2Vec2FeatureExtractor { + "do_normalize": true, + "feature_extractor_type": "Wav2Vec2FeatureExtractor", + "feature_size": 1, + "padding_side": "right", + "padding_value": 0.0, + "return_attention_mask": false, + "sampling_rate": 16000 +} +``` + +## Procesador + +Para modelos que son compatibles con tareas multimodales, ๐Ÿค— Transformers ofrece una clase *procesador* que agrupa un extractor de caracterรญsticas y un *tokenizer* en el mismo objeto. Por ejemplo, probemos a usar el procesador [`Wav2Vec2Processor`] para una tarea de reconocimiento de voz (ASR). Un ASR transcribe el audio a texto, por lo que necesitaremos un extractor de caracterรญsticas y un *tokenizer*. + +Crea un extractor de caracterรญsticas para manejar la entrada de audio: + + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> feature_extractor = Wav2Vec2FeatureExtractor(padding_value=1.0, do_normalize=True) +``` + +Crea un *tokenizer* para manejar la entrada de texto: + +```py +>>> from transformers import Wav2Vec2CTCTokenizer + +>>> tokenizer = Wav2Vec2CTCTokenizer(vocab_file="my_vocab_file.txt") +``` + +Puedes combinar el extractor de caracterรญsticas y el *tokenizer* en el [`Wav2Vec2Processor`]: + + +```py +>>> from transformers import Wav2Vec2Processor + +>>> processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer) +``` +Con dos clases base (la configuraciรณn y el modelo) y una clase de preprocesamiento adicional (*tokenizer*, extractor de caracterรญsticas o procesador), puedes crear cualquiera de los modelos compatibles con ๐Ÿค— Transformers. Cada una de estas clases son configurables, permitiรฉndote usar sus atributos especรญficos. Puedes crear un modelo para entrenarlo de una forma fรกcil, o modificar un modelo preentrenado disponible para especializarlo. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/custom_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/custom_models.md new file mode 100644 index 0000000000000000000000000000000000000000..fec50e4e7a18c3f3de6faf9c5f07cc8910b540e9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/custom_models.md @@ -0,0 +1,358 @@ + + +# Compartir modelos personalizados + +La biblioteca ๐Ÿค— Transformers estรก diseรฑada para ser fรกcilmente ampliable. Cada modelo estรก completamente codificado +sin abstracciรณn en una subcarpeta determinada del repositorio, por lo que puedes copiar fรกcilmente un archivo del modelo +y ajustarlo segรบn tus necesidades. + +Si estรกs escribiendo un modelo completamente nuevo, podrรญa ser mรกs fรกcil comenzar desde cero. En este tutorial, te mostraremos +cรณmo escribir un modelo personalizado y su configuraciรณn para que pueda usarse dentro de Transformers, y cรณmo puedes compartirlo +con la comunidad (con el cรณdigo en el que se basa) para que cualquiera pueda usarlo, incluso si no estรก presente en la biblioteca +๐Ÿค— Transformers. + +Ilustraremos todo esto con un modelo ResNet, envolviendo la clase ResNet de la [biblioteca timm](https://github.com/rwightman/pytorch-image-models) en un [`PreTrainedModel`]. + +## Escribir una configuraciรณn personalizada + +Antes de adentrarnos en el modelo, primero escribamos su configuraciรณn. La configuraciรณn de un modelo es un objeto que +contendrรก toda la informaciรณn necesaria para construir el modelo. Como veremos en la siguiente secciรณn, el modelo solo puede +tomar un `config` para ser inicializado, por lo que realmente necesitamos que ese objeto estรฉ lo mรกs completo posible. + +En nuestro ejemplo, tomaremos un par de argumentos de la clase ResNet que tal vez queramos modificar. Las diferentes +configuraciones nos darรกn los diferentes tipos de ResNet que son posibles. Luego simplemente almacenamos esos argumentos +despuรฉs de verificar la validez de algunos de ellos. + +```python +from transformers import PretrainedConfig +from typing import List + + +class ResnetConfig(PretrainedConfig): + model_type = "resnet" + + def __init__( + self, + block_type="bottleneck", + layers: list[int] = [3, 4, 6, 3], + num_classes: int = 1000, + input_channels: int = 3, + cardinality: int = 1, + base_width: int = 64, + stem_width: int = 64, + stem_type: str = "", + avg_down: bool = False, + **kwargs, + ): + if block_type not in ["basic", "bottleneck"]: + raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.") + if stem_type not in ["", "deep", "deep-tiered"]: + raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.") + + self.block_type = block_type + self.layers = layers + self.num_classes = num_classes + self.input_channels = input_channels + self.cardinality = cardinality + self.base_width = base_width + self.stem_width = stem_width + self.stem_type = stem_type + self.avg_down = avg_down + super().__init__(**kwargs) +``` + +Las tres cosas importantes que debes recordar al escribir tu propia configuraciรณn son las siguientes: +- tienes que heredar de `PretrainedConfig`, +- el `__init__` de tu `PretrainedConfig` debe aceptar cualquier `kwargs`, +- esos `kwargs` deben pasarse a la superclase `__init__`. + +La herencia es para asegurarte de obtener toda la funcionalidad de la biblioteca ๐Ÿค— Transformers, mientras que las otras dos +restricciones provienen del hecho de que una `PretrainedConfig` tiene mรกs campos que los que estรกs configurando. Al recargar una +`config` con el mรฉtodo `from_pretrained`, esos campos deben ser aceptados por tu `config` y luego enviados a la superclase. + +Definir un `model_type` para tu configuraciรณn (en este caso `model_type="resnet"`) no es obligatorio, a menos que quieras +registrar tu modelo con las clases automรกticas (ver la รบltima secciรณn). + +Una vez hecho esto, puedes crear y guardar fรกcilmente tu configuraciรณn como lo harรญas con cualquier otra configuraciรณn de un +modelo de la biblioteca. Asรญ es como podemos crear una configuraciรณn resnet50d y guardarla: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d_config.save_pretrained("custom-resnet") +``` + +Esto guardarรก un archivo llamado `config.json` dentro de la carpeta `custom-resnet`. Luego puedes volver a cargar tu configuraciรณn +con el mรฉtodo `from_pretrained`: + +```py +resnet50d_config = ResnetConfig.from_pretrained("custom-resnet") +``` + +Tambiรฉn puedes usar cualquier otro mรฉtodo de la clase [`PretrainedConfig`], como [`~PretrainedConfig.push_to_hub`], para cargar +directamente tu configuraciรณn en el Hub. + +## Escribir un modelo personalizado + +Ahora que tenemos nuestra configuraciรณn de ResNet, podemos seguir escribiendo el modelo. En realidad escribiremos dos: una que +extrae las caracterรญsticas ocultas de un grupo de imรกgenes (como [`BertModel`]) y una que es adecuada para clasificaciรณn de +imagenes (como [`BertForSequenceClassification`]). + +Como mencionamos antes, solo escribiremos un envoltura (_wrapper_) libre del modelo para simplificar este ejemplo. Lo รบnico que debemos +hacer antes de escribir esta clase es un mapeo entre los tipos de bloques y las clases de bloques reales. Luego se define el +modelo desde la configuraciรณn pasando todo a la clase `ResNet`: + +```py +from transformers import PreTrainedModel +from timm.models.resnet import BasicBlock, Bottleneck, ResNet +from .configuration_resnet import ResnetConfig + + +BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} + + +class ResnetModel(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor): + return self.model.forward_features(tensor) +``` + +Para el modelo que clasificarรก las imรกgenes, solo cambiamos el mรฉtodo de avance (es decir, el mรฉtodo `forward`): + +```py +import torch + + +class ResnetModelForImageClassification(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor, labels=None): + logits = self.model(tensor) + if labels is not None: + loss = torch.nn.functional.cross_entropy(logits, labels) + return {"loss": loss, "logits": logits} + return {"logits": logits} +``` + +En ambos casos, observa cรณmo heredamos de `PreTrainedModel` y llamamos a la inicializaciรณn de la superclase con `config` +(un poco como cuando escribes `torch.nn.Module`). La lรญnea que establece `config_class` no es obligatoria, a menos +que quieras registrar tu modelo con las clases automรกticas (consulta la รบltima secciรณn). + + + +Si tu modelo es muy similar a un modelo dentro de la biblioteca, puedes reutilizar la misma configuraciรณn de ese modelo. + + + +Puedes hacer que tu modelo devuelva lo que quieras, pero devolver un diccionario como lo hicimos para +`ResnetModelForImageClassification`, con el `loss` incluido cuando se pasan las etiquetas, harรก que tu modelo se pueda +usar directamente dentro de la clase [`Trainer`]. Usar otro formato de salida estรก bien, siempre y cuando estรฉs planeando usar +tu propio bucle de entrenamiento u otra biblioteca para el entrenamiento. + +Ahora que tenemos nuestra clase, vamos a crear un modelo: + +```py +resnet50d = ResnetModelForImageClassification(resnet50d_config) +``` + +Nuevamente, puedes usar cualquiera de los mรฉtodos de [`PreTrainedModel`], como [`~PreTrainedModel.save_pretrained`] o +[`~PreTrainedModel.push_to_hub`]. Usaremos el segundo en la siguiente secciรณn y veremos cรณmo pasar los pesos del modelo +con el cรณdigo de nuestro modelo. Pero primero, carguemos algunos pesos previamente entrenados dentro de nuestro modelo. + +En tu caso de uso, probablemente estarรกs entrenando tu modelo personalizado con tus propios datos. Para ir rรกpido en este +tutorial, usaremos la versiรณn preentrenada de resnet50d. Dado que nuestro modelo es solo un envoltorio alrededor del resnet50d +original, serรก fรกcil transferir esos pesos: + +```py +import timm + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +Ahora veamos cรณmo asegurarnos de que cuando hacemos [`~PreTrainedModel.save_pretrained`] o [`~PreTrainedModel.push_to_hub`], +se guarda el cรณdigo del modelo. + +## Enviar el cรณdigo al _Hub_ + + + +Esta _API_ es experimental y puede tener algunos cambios leves en las prรณximas versiones. + + + +Primero, asegรบrate de que tu modelo estรฉ completamente definido en un archivo `.py`. Puedes basarte en importaciones +relativas a otros archivos, siempre que todos los archivos estรฉn en el mismo directorio (aรบn no admitimos submรณdulos +para esta caracterรญstica). Para nuestro ejemplo, definiremos un archivo `modeling_resnet.py` y un archivo +`configuration_resnet.py` en una carpeta del directorio de trabajo actual llamado `resnet_model`. El archivo de configuraciรณn +contiene el cรณdigo de `ResnetConfig` y el archivo del modelo contiene el cรณdigo de `ResnetModel` y +`ResnetModelForImageClassification`. + +``` +. +โ””โ”€โ”€ resnet_model + โ”œโ”€โ”€ __init__.py + โ”œโ”€โ”€ configuration_resnet.py + โ””โ”€โ”€ modeling_resnet.py +``` + +El `__init__.py` puede estar vacรญo, solo estรก ahรญ para que Python detecte que `resnet_model` se puede usar como un mรณdulo. + + + +Si copias archivos del modelo desde la biblioteca, deberรกs reemplazar todas las importaciones relativas en la parte superior +del archivo para importarlos desde el paquete `transformers`. + + + +Ten en cuenta que puedes reutilizar (o subclasificar) una configuraciรณn o modelo existente. + +Para compartir tu modelo con la comunidad, sigue estos pasos: primero importa el modelo y la configuraciรณn de ResNet desde +los archivos reciรฉn creados: + +```py +from resnet_model.configuration_resnet import ResnetConfig +from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification +``` + +Luego, debes decirle a la biblioteca que deseas copiar el cรณdigo de esos objetos cuando usas el mรฉtodo `save_pretrained` +y registrarlos correctamente con una determinada clase automรกtica (especialmente para modelos), simplemente ejecuta: + +```py +ResnetConfig.register_for_auto_class() +ResnetModel.register_for_auto_class("AutoModel") +ResnetModelForImageClassification.register_for_auto_class("AutoModelForImageClassification") +``` + +Ten en cuenta que no es necesario especificar una clase automรกtica para la configuraciรณn (solo hay una clase automรกtica +para ellos, [`AutoConfig`]), pero es diferente para los modelos. Tu modelo personalizado podrรญa ser adecuado para muchas +tareas diferentes, por lo que debes especificar cuรกl de las clases automรกticas es la correcta para tu modelo. + +A continuaciรณn, vamos a crear la configuraciรณn y los modelos como lo hicimos antes: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d = ResnetModelForImageClassification(resnet50d_config) + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +Ahora, para enviar el modelo al Hub, asegรบrate de haber iniciado sesiรณn. Ejecuta en tu terminal: + +```bash +hf auth login +``` + +o desde un _notebook_: + +```py +from huggingface_hub import notebook_login + +notebook_login() +``` + +Luego puedes ingresar a tu propio espacio (o una organizaciรณn de la que seas miembro) de esta manera: + +```py +resnet50d.push_to_hub("custom-resnet50d") +``` + +Ademรกs de los pesos del modelo y la configuraciรณn en formato json, esto tambiรฉn copiรณ los archivos `.py` del modelo y la +configuraciรณn en la carpeta `custom-resnet50d` y subiรณ el resultado al Hub. Puedes verificar el resultado en este +[repositorio de modelos](https://huggingface.co/sgugger/custom-resnet50d). + +Consulta el tutorial sobre cรณmo [compartir modelos](model_sharing) para obtener mรกs informaciรณn sobre el mรฉtodo para subir modelos al Hub. + +## Usar un modelo con cรณdigo personalizado + +Puedes usar cualquier configuraciรณn, modelo o _tokenizador_ con archivos de cรณdigo personalizado en tu repositorio con las +clases automรกticas y el mรฉtodo `from_pretrained`. Todos los archivos y cรณdigos cargados en el Hub se analizan en busca de +malware (consulta la documentaciรณn de [seguridad del Hub](https://huggingface.co/docs/hub/security#malware-scanning) para +obtener mรกs informaciรณn), pero aรบn debes revisar el cรณdigo del modelo y el autor para evitar la ejecuciรณn de cรณdigo malicioso +en tu computadora. Configura `trust_remote_code=True` para usar un modelo con cรณdigo personalizado: + +```py +from transformers import AutoModelForImageClassification + +model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True) +``` + +Tambiรฉn se recomienda encarecidamente pasar un _hash_ de confirmaciรณn como una "revisiรณn" para asegurarte de que el autor +de los modelos no actualizรณ el cรณdigo con algunas lรญneas nuevas maliciosas (a menos que confรญes plenamente en los autores +de los modelos). + +```py +commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292" +model = AutoModelForImageClassification.from_pretrained( + "sgugger/custom-resnet50d", trust_remote_code=True, revision=commit_hash +) +``` + +Ten en cuenta que al navegar por el historial de confirmaciones del repositorio del modelo en Hub, hay un botรณn para copiar +fรกcilmente el hash de confirmaciรณn de cualquier _commit_. + +## Registrar un model con cรณdigo personalizado a las clases automรกticas + +Si estรกs escribiendo una biblioteca que amplรญa ๐Ÿค— Transformers, es posible que quieras ampliar las clases automรกticas para +incluir tu propio modelo. Esto es diferente de enviar el cรณdigo al Hub en el sentido de que los usuarios necesitarรกn importar +tu biblioteca para obtener los modelos personalizados (al contrario de descargar automรกticamente el cรณdigo del modelo desde Hub). + +Siempre que tu configuraciรณn tenga un atributo `model_type` que sea diferente de los tipos de modelos existentes, y que tus +clases modelo tengan los atributos `config_class` correctos, puedes agregarlos a las clases automรกticas de la siguiente manera: + +```py +from transformers import AutoConfig, AutoModel, AutoModelForImageClassification + +AutoConfig.register("resnet", ResnetConfig) +AutoModel.register(ResnetConfig, ResnetModel) +AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) +``` + +Ten en cuenta que el primer argumento utilizado al registrar tu configuraciรณn personalizada en [`AutoConfig`] debe coincidir +con el `model_type` de tu configuraciรณn personalizada, y el primer argumento utilizado al registrar tus modelos personalizados +en cualquier clase del modelo automรกtico debe coincidir con el `config_class ` de esos modelos. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/debugging.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/debugging.md new file mode 100644 index 0000000000000000000000000000000000000000..313566753052cbf147c4d28eaaa48d0f3f9bf5df --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/debugging.md @@ -0,0 +1,335 @@ + + +# Debugging + +## Debug de problemas de Network multi-GPU + +Cuando entrenas o infieres con `DistributedDataParallel` y varias GPUs, si encuentras problemas de intercomunicaciรณn entre procesos y/o nodos, puedes usar el siguiente script para diagnosticar problemas de red. + +```bash +wget https://raw.githubusercontent.com/huggingface/transformers/main/scripts/distributed/torch-distributed-gpu-test.py +``` + +Por ejemplo, para probar cรณmo interactรบan 2 GPUs, haz lo siguiente: + +```bash +python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` +Si ambos procesos pueden hablar entre sรญ y asignar la memoria de la GPU, cada uno imprimirรก un status OK. + +Para mรกs GPUs o nodos, ajusta los argumentos en el script. + +Encontrarรกs muchos mรกs detalles dentro del script de diagnรณstico e incluso una receta de cรณmo ejecutarlo en un entorno SLURM. + +Un nivel adicional de debug es agregar la variable de entorno `NCCL_DEBUG=INFO` de la siguiente manera: + +```bash +NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` + +Esto mostrarรก mucha informaciรณn de debug relacionada con NCCL, que luego puedes buscar online si encuentras que reporta algรบn problema. O si no estรกs seguro de cรณmo interpretar el output, puedes compartir el archivo de log en un Issue. + + +## Detecciรณn de Underflow y Overflow + + + +Esta funciรณn estรก disponible actualmente sรณlo para PyTorch. + + + + + +Para el entrenamiento multi-GPU, requiere DDP (`torch.distributed.launch`). + + + + + +Esta funciรณn puede utilizarse con cualquier modelo basado en `nn.Module`. + + + +Si empiezas a obtener `loss=NaN` o el modelo muestra algรบn otro comportamiento anormal debido a `inf` o `nan` en +activations o weights hay que descubrir dรณnde se produce el primer underflow o overflow y quรฉ lo ha provocado. Por suerte +puedes lograrlo fรกcilmente activando un mรณdulo especial que harรก la detecciรณn automรกticamente. + +Si estรกs usando [`Trainer`], solo necesitas aรฑadir: + +```bash +--debug underflow_overflow +``` + +a los argumentos normales de la lรญnea de comandos, o pasar `debug="underflow_overflow"` al crear el objeto [`TrainingArguments`]. + +Si estรกs usando tu propio bucle de entrenamiento u otro Trainer puedes lograr lo mismo con: + +```python +from .debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model) +``` + +[`~debug_utils.DebugUnderflowOverflow`] inserta hooks en el modelo que inmediatamente despuรฉs de cada forward +testearรก las variables de input y output y tambiรฉn los weights del mรณdulo correspondiente. Tan pronto como se detecte `inf` o +`nan` se detecta en al menos un elemento de las activations o weights, el programa afirmarรก e imprimirรก un informe +como este (esto fue capturado con `google/mt5-small` bajo fp16 mixed precision): + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata + encoder.block.1.layer.1.DenseReluDense.dropout Dropout +0.00e+00 2.57e+02 input[0] +0.00e+00 2.85e+02 output +[...] + encoder.block.2.layer.0 T5LayerSelfAttention +6.78e-04 3.15e+03 input[0] +2.65e-04 3.42e+03 output[0] + None output[1] +2.25e-01 1.00e+04 output[2] + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.dropout Dropout +0.00e+00 8.76e+03 input[0] +0.00e+00 9.74e+03 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +El output del ejemplo se ha recortado en el centro por razones de brevedad. + +La segunda columna muestra el valor del elemento mรกs grande en tรฉrminos absolutos, por lo que si observas con detenimiento los รบltimos fotogramas, +los inputs y outputs estaban en el rango de `1e4`. Asรญ que cuando este entrenamiento se hizo con fp16 mixed precision, +el รบltimo paso sufriรณ overflow (ya que bajo `fp16` el mayor nรบmero antes de `inf` es `64e3`). Para evitar overflows en +`fp16` las activations deben permanecer muy por debajo de `1e4`, porque `1e4 * 1e4 = 1e8` por lo que cualquier matrix multiplication con +grandes activations va a llevar a una condiciรณn de overflow numรฉrico. + +Al principio del output puedes descubrir en quรฉ nรบmero de batch se produjo el problema (aquรญ `Detected inf/nan during batch_number=0` significa que el problema se produjo en el primer batch). + +Cada frame del informe comienza declarando la entrada completamente calificada para el mรณdulo correspondiente que este frame estรก reportando. +Si nos fijamos sรณlo en este frame: + +``` + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output +``` + +Aquรญ, `encoder.block.2.layer.1.layer_norm` indica que era una layer norm para la primera capa, del segundo +block del encoder. Y la call especรญfica del `forward` es `T5LayerNorm`. + +Veamos los รบltimos frames de ese informe: + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata +[...] + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +El รบltimo frame informa para la funciรณn `Dropout.forward` con la primera entrada para el รบnico input y la segunda para el +รบnico output. Puedes ver que fue llamada desde un atributo `dropout` dentro de la clase `DenseReluDense`. Podemos ver +que ocurriรณ durante la primera capa, del segundo block, durante el primer batch. Por รบltimo, el mayor absoluto +elementos de input fue `6.27e+04` y el mismo para el output fue `inf`. + +Puedes ver aquรญ, que `T5DenseGatedGeluDense.forward` resultรณ en output activations, cuyo valor mรกximo absoluto fue +alrededor de 62.7K, que estรก muy cerca del lรญmite mรกximo de fp16 de 64K. En el siguiente frame tenemos `Dropout`, el cual renormaliza +los weights, despuรฉs de poner a cero algunos de los elementos, lo que empuja el valor mรกximo absoluto a mรกs de 64K, y obtenemos un +overflow (`inf`). + +Como puedes ver son los frames anteriores los que tenemos que mirar cuando los nรบmeros empiezan a ser muy grandes para nรบmeros fp16. + +Combinemos el informe con el cรณdigo de `models/t5/modeling_t5.py`: + +```python +class T5DenseGatedGeluDense(nn.Module): + def __init__(self, config): + super().__init__() + self.wi_0 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wi_1 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wo = nn.Linear(config.d_ff, config.d_model, bias=False) + self.dropout = nn.Dropout(config.dropout_rate) + self.gelu_act = ACT2FN["gelu_new"] + + def forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states +``` + +Ahora es fรกcil ver la call `dropout`, y tambiรฉn todas las calls anteriores. + +Dado que la detecciรณn se produce en un forward hook, estos informes se imprimen inmediatamente despuรฉs de que cada `forward` +responda. + +Volviendo al informe completo, para actuar sobre รฉl y arreglar el problema, tenemos que subir unos cuantos frames donde los nรบmeros +empezaron a subir y probablemente cambiar al modo `fp32` aquรญ, para que los nรบmeros no sufran overflow cuando se multipliquen +o al sumarlos. Por supuesto, puede haber otras soluciones. Por ejemplo, podrรญamos desactivar `amp` temporalmente si estรก +activado, despuรฉs de mover el original `forward` dentro de un helper wrapper, asรญ: + +```python +def _forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states + + +import torch + + +def forward(self, hidden_states): + if torch.is_autocast_enabled(): + with torch.cuda.amp.autocast(enabled=False): + return self._forward(hidden_states) + else: + return self._forward(hidden_states) +``` + +Como el detector automรกtico sรณlo informa de los inputs y outputs de los frames completos, una vez que sepas dรณnde buscar, puedes +analizar tambiรฉn las etapas intermedias de una funciรณn especรญfica de `forward`. En este caso, puede utilizar la funciรณn +funciรณn de ayuda `detect_overflow` para inyectar el detector donde quieras, por ejemplo: + +```python +from debug_utils import detect_overflow + + +class T5LayerFF(nn.Module): + [...] + + def forward(self, hidden_states): + forwarded_states = self.layer_norm(hidden_states) + detect_overflow(forwarded_states, "after layer_norm") + forwarded_states = self.DenseReluDense(forwarded_states) + detect_overflow(forwarded_states, "after DenseReluDense") + return hidden_states + self.dropout(forwarded_states) +``` + +Puedes ver que hemos aรฑadido 2 de estos y ahora se trackea si `inf` o `nan` para `forwarded_states` fue detectado +en algรบn punto intermedio. + +De hecho, el detector ya informa de esto porque cada una de las llamadas en el ejemplo anterior es un `nn.Module`, pero +digamos que si tuvieras algunos cรกlculos directos locales, asรญ es como lo harรญas. + +Ademรกs, si estรกs instanciando el debugger en tu propio cรณdigo, puedes ajustar el nรบmero de frames impresos de +su valor por defecto, por ejemplo: + +```python +from .debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model, max_frames_to_save=100) +``` + +### Rastreo de valores mรญnimos y mรกximos absolutos de batches especรญficos + +La misma clase de debugging se puede utilizar para el rastreo por batches con la funciรณn de detecciรณn de underflow/overflow desactivada. + +Digamos que quieres ver los valores mรญnimos y mรกximos absolutos de todos los ingredientes de cada call `forward` de un determinado +batch, y sรณlo hacerlo para los batches 1 y 3. Entonces instancias esta clase como: + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3]) +``` + +Y ahora los batches 1 y 3 completos serรกn rastreados usando el mismo formato que el detector de underflow/overflow. + +Los batches son 0-index. + +Esto es muy รบtil si sabes que el programa empieza a comportarse mal despuรฉs de un determinado nรบmero de batch, para que puedas avanzar rรกpidamente +hasta esa รกrea. Aquรญ hay un ejemplo de output recortado para tal configuraciรณn: + +``` + *** Starting batch number=1 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.47e+04 input[0] +5.36e-05 7.92e+02 output +[...] + decoder.dropout Dropout +1.60e-07 2.27e+01 input[0] +0.00e+00 2.52e+01 output + decoder T5Stack + not a tensor output + lm_head Linear +1.01e-06 7.92e+02 weight +0.00e+00 1.11e+00 input[0] +6.06e-02 8.39e+01 output + T5ForConditionalGeneration + not a tensor output + + *** Starting batch number=3 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.78e+04 input[0] +5.36e-05 7.92e+02 output +[...] +``` + +Aquรญ obtendrรกs un gran nรบmero de frames mostrados - tantos como forward calls haya en tu modelo, por lo que puede o no ser lo que quieras, pero a veces puede ser mรกs fรกcil de usar para debug que un debugger normal. +Por ejemplo, si un problema comienza a ocurrir en el batch 150. Entonces puedes mostrar las trazas de los batches 149 y 150 y comparar dรณnde +los nรบmeros empezaron a divergir. + +Tambiรฉn puedes especificar el nรบmero de batch despuรฉs del cual se debe detener el entrenamiento, con: + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3], abort_after_batch_num=3) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/fast_tokenizers.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/fast_tokenizers.md new file mode 100644 index 0000000000000000000000000000000000000000..92b925f67f7e47b604ba1e0efd7bae23324a4313 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/fast_tokenizers.md @@ -0,0 +1,74 @@ + + +# Usa los tokenizadores de ๐Ÿค— Tokenizers + +[`PreTrainedTokenizerFast`] depende de la biblioteca [๐Ÿค— Tokenizers](https://huggingface.co/docs/tokenizers). Los tokenizadores obtenidos desde la biblioteca ๐Ÿค— Tokenizers pueden ser +cargados de forma muy sencilla en los ๐Ÿค— Transformers. + +Antes de entrar en detalles, comencemos creando un tokenizador dummy en unas cuantas lรญneas: + +```python +>>> from tokenizers import Tokenizer +>>> from tokenizers.models import BPE +>>> from tokenizers.trainers import BpeTrainer +>>> from tokenizers.pre_tokenizers import Whitespace + +>>> tokenizer = Tokenizer(BPE(unk_token="[UNK]")) +>>> trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]) + +>>> tokenizer.pre_tokenizer = Whitespace() +>>> files = [...] +>>> tokenizer.train(files, trainer) +``` + +Ahora tenemos un tokenizador entrenado en los archivos que definimos. Lo podemos seguir utilizando en ese entorno de ejecuciรณn (runtime en inglรฉs), o puedes guardarlo +en un archivo JSON para reutilizarlo en un futuro. + +## Cargando directamente desde el objeto tokenizador + +Veamos cรณmo utilizar este objeto tokenizador en la biblioteca ๐Ÿค— Transformers. La clase +[`PreTrainedTokenizerFast`] permite una instanciaciรณn fรกcil, al aceptar el objeto +*tokenizer* instanciado como argumento: + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer) +``` + +Este objeto ya puede ser utilizado con todos los mรฉtodos compartidos por los tokenizadores de ๐Ÿค— Transformers! Visita la [pรกgina sobre tokenizadores +](main_classes/tokenizer) para mรกs informaciรณn. + +## Cargando desde un archivo JSON + +Para cargar un tokenizador desde un archivo JSON, comencemos por guardar nuestro tokenizador: + +```python +>>> tokenizer.save("tokenizer.json") +``` + +La localizaciรณn (path en inglรฉs) donde este archivo es guardado puede ser incluida en el mรฉtodo de inicializaciรณn de [`PreTrainedTokenizerFast`] +utilizando el parรกmetro `tokenizer_file`: + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json") +``` + +Este objeto ya puede ser utilizado con todos los mรฉtodos compartidos por los tokenizadores de ๐Ÿค— Transformers! Visita la [pรกgina sobre tokenizadores +](main_classes/tokenizer) para mรกs informaciรณn. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/glossary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/glossary.md new file mode 100644 index 0000000000000000000000000000000000000000..3debcdbd354580c243ef2afab449f84a632c4d27 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/glossary.md @@ -0,0 +1,464 @@ + + +# Glosario + +Este glosario define tรฉrminos generales de aprendizaje automรกtico y tรฉrminos relacionados con ๐Ÿค— Transformers para ayudarte a comprender mejor la documentaciรณn. + +## A + +### attention mask + +La mรกscara de atenciรณn es un argumento opcional utilizado al agrupar secuencias. + + + +Este argumento indica al modelo quรฉ tokens deben recibir atenciรณn y cuรกles no. + +Por ejemplo, considera estas dos secuencias: + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence_a = "This is a short sequence." +>>> sequence_b = "This is a rather long sequence. It is at least longer than the sequence A." + +>>> encoded_sequence_a = tokenizer(sequence_a)["input_ids"] +>>> encoded_sequence_b = tokenizer(sequence_b)["input_ids"] +``` + +Las versiones codificadas tienen longitudes diferentes: + +```python +>>> len(encoded_sequence_a), len(encoded_sequence_b) +(8, 19) +``` + +Por lo tanto, no podemos colocarlas juntas en el mismo tensor tal cual. La primera secuencia necesita ser rellenada hasta la longitud de la segunda, o la segunda necesita ser truncada hasta la longitud de la primera. + +En el primer caso, la lista de IDs se extenderรก con los รญndices de relleno. Podemos pasar una lista al tokenizador y pedirle que realice el relleno de esta manera: + +```python +>>> padded_sequences = tokenizer([sequence_a, sequence_b], padding=True) +``` + +Podemos ver que se han agregado ceros a la derecha de la primera oraciรณn para que tenga la misma longitud que la segunda: + +```python +>>> padded_sequences["input_ids"] +[[101, 1188, 1110, 170, 1603, 4954, 119, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [101, 1188, 1110, 170, 1897, 1263, 4954, 119, 1135, 1110, 1120, 1655, 2039, 1190, 1103, 4954, 138, 119, 102]] +``` + +Esto luego se puede convertir en un tensor en PyTorch o TensorFlow. La mรกscara de atenciรณn es un tensor binario que indica la posiciรณn de los รญndices de relleno para que el modelo no los tenga en cuenta. Para el [`BertTokenizer`], `1` indica un valor al que se debe prestar atenciรณn, mientras que `0` indica un valor de relleno. Esta mรกscara de atenciรณn estรก en el diccionario devuelto por el tokenizador bajo la clave "attention_mask": + +```python +>>> padded_sequences["attention_mask"] +[[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] +``` + +### autoencoding models + +Consulta [modelos de codificaciรณn](#encoder-models) y [modelado de lenguaje enmascarado](#masked-language-modeling-mlm) + +### autoregressive models + +Consulta [modelado de lenguaje causal](#causal-language-modeling) y [modelos de decodificaciรณn](#decoder-models) + +## B + +### backbone + +La columna vertebral, backbone en inglรฉs, es la red (embeddings y layers) que produce los estados ocultos o caracterรญsticas crudas. Normalmente, estรก conectado a una [cabecera](#head), que acepta las caracterรญsticas como entrada para hacer una predicciรณn. Por ejemplo, [`ViTModel`] es una columna vertebral sin una cabecera especรญfica encima. Otros modelos tambiรฉn pueden usar [`VitModel`] como columna vertebral, como por ejemplo [DPT](model_doc/dpt). + +## C + +### causal language modeling + +Una tarea de preentrenamiento donde el modelo lee los textos en orden y tiene que predecir la siguiente palabra. Generalmente, se realiza leyendo toda la oraciรณn, pero utilizando una mรกscara dentro del modelo para ocultar los tokens futuros en un cierto paso de tiempo. + +### channel + +Las imรกgenes a color estรกn compuestas por alguna combinaciรณn de valores en tres canales: rojo, verde y azul (RGB), y las imรกgenes en escala de grises solo tienen un canal. En ๐Ÿค— Transformers, el canal puede ser la primera o รบltima dimensiรณn del tensor de una imagen: [`n_channels`, `height`, `width`] o [`height`, `width`, `n_channels`]. + +### connectionist temporal classification (CTC) + +Un algoritmo que permite que un modelo aprenda sin saber exactamente cรณmo estรกn alineadas la entrada y la salida; CTC calcula la distribuciรณn de todas las salidas posibles para una entrada dada y elige la salida mรกs probable de ella. CTC se utiliza comรบnmente en tareas de reconocimiento de voz porque el habla no siempre se alinea perfectamente con la transcripciรณn debido a diversas razones, como las diferentes velocidades de habla de los oradores. + +### convolution + +Un tipo de capa en una red neuronal donde la matriz de entrada se multiplica elemento por elemento por una matriz mรกs pequeรฑa (nรบcleo o filtro) y los valores se suman en una nueva matriz. Esto se conoce como una operaciรณn de convoluciรณn que se repite sobre toda la matriz de entrada. Cada operaciรณn se aplica a un segmento diferente de la matriz de entrada. Las redes neuronales convolucionales (CNN) se utilizan comรบnmente en visiรณn por computadora. + +## D + +### DataParallel (DP) + +Tรฉcnica de paralelismo para entrenamiento en mรบltiples GPUs donde se replica la misma configuraciรณn varias veces, con cada instancia recibiendo una porciรณn de datos รบnica. El procesamiento se realiza en paralelo y todas las configuraciones se sincronizan al final de cada paso de entrenamiento. + +Obtรฉn mรกs informaciรณn sobre cรณmo funciona el DataParallel [aquรญ](perf_train_gpu_many#dataparallel-vs-distributeddataparallel). + +### decoder input IDs + +Esta entrada es especรญfica para modelos codificador-decodificador y contiene los IDs de entrada que se enviarรกn al decodificador. Estas entradas deben usarse para tareas de secuencia a secuencia, como traducciรณn o resumen, y generalmente se construyen de una manera especรญfica para cada modelo. + +La mayorรญa de los modelos codificador-decodificador (BART, T5) crean sus `decoder_input_ids` por sรญ mismos a partir de las `labels`. En tales modelos, pasar las `labels` es la forma preferida de manejar el entrenamiento. + +Consulta la documentaciรณn de cada modelo para ver cรณmo manejan estos IDs de entrada para el entrenamiento de secuencia a secuencia. + +### decoder models + +Tambiรฉn conocidos como modelos autorregresivos, los modelos decodificadores involucran una tarea de preentrenamiento (llamada modelado de lenguaje causal) donde el modelo lee los textos en orden y tiene que predecir la siguiente palabra. Generalmente, se realiza leyendo la oraciรณn completa con una mรกscara para ocultar los tokens futuros en un cierto paso de tiempo. + + + +### deep learning (DL) + +Algoritmos de aprendizaje automรกtico que utilizan redes neuronales con varias capas. + +## E + +### encoder models + +Tambiรฉn conocidos como modelos de codificaciรณn automรกtica (autoencoding models), los modelos codificadores toman una entrada (como texto o imรกgenes) y las transforman en una representaciรณn numรฉrica condensada llamada embedding. A menudo, los modelos codificadores se entrenan previamente utilizando tรฉcnicas como el [modelado de lenguaje enmascarado](#masked-language-modeling-mlm), que enmascara partes de la secuencia de entrada y obliga al modelo a crear representaciones mรกs significativas. + + + +## F + +### feature extraction + +El proceso de seleccionar y transformar datos crudos en un conjunto de caracterรญsticas mรกs informativas y รบtiles para algoritmos de aprendizaje automรกtico. Algunos ejemplos de extracciรณn de caracterรญsticas incluyen transformar texto crudo en embeddings de palabras y extraer caracterรญsticas importantes como bordes o formas de datos de imรกgenes/videos. + +### feed forward chunking + +En cada bloque de atenciรณn residual en los transformadores, la capa de autoatenciรณn suele ir seguida de 2 capas de avance. El tamaรฑo de embedding intermedio de las capas de avance suele ser mayor que el tamaรฑo oculto del modelo (por ejemplo, para `google-bert/bert-base-uncased`). + +Para una entrada de tamaรฑo `[batch_size, sequence_length]`, la memoria requerida para almacenar los embeddings intermedios de avance `[batch_size, sequence_length, config.intermediate_size]` puede representar una gran fracciรณn del uso de memoria. Los autores de [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) observaron que, dado que el cรกlculo es independiente de la dimensiรณn `sequence_length`, es matemรกticamente equivalente calcular los embeddings de salida de ambas capas de avance `[batch_size, config.hidden_size]_0, ..., [batch_size, config.hidden_size]_n` individualmente y concatenarlos despuรฉs a `[batch_size, sequence_length, config.hidden_size]` con `n = sequence_length`, lo que intercambia el aumento del tiempo de cรกlculo por una reducciรณn en el uso de memoria, pero produce un resultado matemรกticamente **equivalente**. + +Para modelos que utilizan la funciรณn [`apply_chunking_to_forward`], el `chunk_size` define el nรบmero de embeddings de salida que se calculan en paralelo y, por lo tanto, define el equilibrio entre la complejidad de memoria y tiempo. Si `chunk_size` se establece en 0, no se realiza ninguna fragmentaciรณn de avance. + +### finetuned models + +El ajuste fino es una forma de transferencia de aprendizaje que implica tomar un modelo entrenado previamente, congelar sus pesos y reemplazar la capa de salida con una nueva [cabecera de modelo](#head) reciรฉn aรฑadida. La cabecera del modelo se entrena en tu conjunto de datos objetivo. + +Consulta el tutorial [Ajustar finamente un modelo pre-entrenado](https://huggingface.co/docs/transformers/training) para obtener mรกs detalles y aprende cรณmo ajustar finamente modelos con ๐Ÿค— Transformers. + +## H + +### head + +La cabecera del modelo se refiere a la รบltima capa de una red neuronal que acepta los estados ocultos crudos y los proyecta en una dimensiรณn diferente. Hay una cabecera de modelo diferente para cada tarea. Por ejemplo: + + * [`GPT2ForSequenceClassification`] es una cabecera de clasificaciรณn de secuencias, es decir, una capa lineal, encima del modelo base [`GPT2Model`]. + * [`ViTForImageClassification`] es una cabecera de clasificaciรณn de imรกgenes, es decir, una capa lineal encima del estado oculto final del token `CLS`, encima del modelo base [`ViTModel`]. + * [`Wav2Vec2ForCTC`] es una cabecera de modelado de lenguaje con [CTC](#connectionist-temporal-classification-ctc) encima del modelo base [`Wav2Vec2Model`]. + +## I + +### image patch + +Los modelos de Transformers basados en visiรณn dividen una imagen en parches mรกs pequeรฑos que se incorporan linealmente y luego se pasan como una secuencia al modelo. Puedes encontrar el `patch_size` (o resoluciรณn del modelo) en su configuraciรณn. + +### inference + +La inferencia es el proceso de evaluar un modelo en nuevos datos despuรฉs de completar el entrenamiento. Consulta el tutorial [Pipeline for inference](https://huggingface.co/docs/transformers/pipeline_tutorial) para aprender cรณmo realizar inferencias con ๐Ÿค— Transformers. + +### input IDs + +Los IDs de entrada a menudo son los รบnicos parรกmetros necesarios que se deben pasar al modelo como entrada. Son รญndices de tokens, representaciones numรฉricas de tokens que construyen las secuencias que se utilizarรกn como entrada por el modelo. + + + +Cada tokenizador funciona de manera diferente, pero el mecanismo subyacente sigue siendo el mismo. Aquรญ tienes un ejemplo utilizando el tokenizador BERT, que es un tokenizador [WordPiece](https://huggingface.co/papers/1609.08144): + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence = "A Titan RTX has 24GB of VRAM" +``` + +El tokenizador se encarga de dividir la secuencia en tokens disponibles en el vocabulario del tokenizador. + +```python +>>> tokenized_sequence = tokenizer.tokenize(sequence) +``` + +Los tokens son palabras o sub palabras. Por ejemplo, "VRAM" no estaba en el vocabulario del modelo, asรญ que se dividiรณ +en "V", "RA" y "M". Para indicar que estos tokens no son palabras separadas sino partes de la misma palabra, se aรฑade un prefijo de doble almohadilla para "RA" y "M": + +```python +>>> print(tokenized_sequence) +['A', 'Titan', 'R', '##T', '##X', 'has', '24', '##GB', 'of', 'V', '##RA', '##M'] +``` + +Estos tokens luego se pueden convertir en IDs que son comprensibles por el modelo. Esto se puede hacer alimentando directamente la oraciรณn al tokenizador, que aprovecha la implementaciรณn en Rust de [๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers) para obtener un rendimiento รณptimo. + +```python +>>> inputs = tokenizer(sequence) +``` + +El tokenizador devuelve un diccionario con todos los argumentos necesarios para que su modelo correspondiente funcione correctamente. Los รญndices de los tokens estรกn bajo la clave `input_ids`: + +```python +>>> encoded_sequence = inputs["input_ids"] +>>> print(encoded_sequence) +[101, 138, 18696, 155, 1942, 3190, 1144, 1572, 13745, 1104, 159, 9664, 2107, 102] +``` + +Ten en cuenta que el tokenizador aรฑade automรกticamente "tokens especiales" (si el modelo asociado depende de ellos), que son IDs especiales que el modelo utiliza en ocasiones. + +Si descodificamos la secuencia anterior de IDs, + +```python +>>> decoded_sequence = tokenizer.decode(encoded_sequence) +``` + +Veremos + +```python +>>> print(decoded_sequence) +[CLS] A Titan RTX has 24GB of VRAM [SEP] +``` + +Porque esta es la forma en que un [`BertModel`] espera sus entradas. + +## L + +### labels + +Las etiquetas son un argumento opcional que se puede pasar para que el modelo calcule la pรฉrdida por sรญ mismo. Estas etiquetas deberรญan ser la predicciรณn esperada del modelo: usarรก la pรฉrdida estรกndar para calcular la pรฉrdida entre sus +predicciones y el valor esperado (la etiqueta). + +Estas etiquetas son diferentes segรบn la cabecera del modelo, por ejemplo: + +- Para modelos de clasificaciรณn de secuencias ([`BertForSequenceClassification`]), el modelo espera un tensor de dimensiรณn + `(batch_size)` con cada valor del lote correspondiente a la etiqueta esperada de toda la secuencia. +- Para modelos de clasificaciรณn de tokens ([`BertForTokenClassification`]), el modelo espera un tensor de dimensiรณn + `(batch_size, seq_length)` con cada valor correspondiente a la etiqueta esperada de cada token individual. +- Para el modelado de lenguaje enmascarado ([`BertForMaskedLM`]), el modelo espera un tensor de dimensiรณn `(batch_size, seq_length)` con cada valor correspondiente a la etiqueta esperada de cada token individual: las etiquetas son el ID del token enmascarado y los valores deben ignorarse para el resto (generalmente -100). +- Para tareas de secuencia a secuencia ([`BartForConditionalGeneration`], [`MBartForConditionalGeneration`]), el modelo + espera un tensor de dimensiรณn `(batch_size, tgt_seq_length)` con cada valor correspondiente a las secuencias objetivo asociadas con cada secuencia de entrada. Durante el entrenamiento, tanto BART como T5 generarรกn internamente los `decoder_input_ids` y las mรกscaras de atenciรณn del decodificador. Por lo general, no es necesario suministrarlos. Esto no se aplica a los modelos que aprovechan el marco codificador-decodificador. +- Para modelos de clasificaciรณn de imรกgenes ([`ViTForImageClassification`]), el modelo espera un tensor de dimensiรณn + `(batch_size)` con cada valor del lote correspondiente a la etiqueta esperada de cada imagen individual. +- Para modelos de segmentaciรณn semรกntica ([`SegformerForSemanticSegmentation`]), el modelo espera un tensor de dimensiรณn + `(batch_size, height, width)` con cada valor del lote correspondiente a la etiqueta esperada de cada pรญxel individual. +- Para modelos de detecciรณn de objetos ([`DetrForObjectDetection`]), el modelo espera una lista de diccionarios con claves `class_labels` y `boxes` donde cada valor del lote corresponde a la etiqueta esperada y el nรบmero de cajas delimitadoras de cada imagen individual. +- Para modelos de reconocimiento automรกtico de voz ([`Wav2Vec2ForCTC`]), el modelo espera un tensor de dimensiรณn `(batch_size, target_length)` con cada valor correspondiente a la etiqueta esperada de cada token individual. + + + +Las etiquetas de cada modelo pueden ser diferentes, asรญ que asegรบrate siempre de revisar la documentaciรณn de cada modelo para obtener mรกs informaciรณn sobre sus etiquetas especรญficas. + + + +Los modelos base ([`BertModel`]) no aceptan etiquetas, ya que estos son los modelos base de transformadores, que simplemente generan caracterรญsticas. + +### large language models (LLM) + +Un tรฉrmino genรฉrico que se refiere a modelos de lenguaje de transformadores (GPT-3, BLOOM, OPT) que fueron entrenados con una gran cantidad de datos. Estos modelos tambiรฉn tienden a tener un gran nรบmero de parรกmetros que se pueden aprender (por ejemplo, 175 mil millones para GPT-3). + +## M + +### masked language modeling (MLM) + +Una tarea de preentrenamiento en la que el modelo ve una versiรณn corrupta de los textos, generalmente hecha +al enmascarar algunos tokens al azar, y tiene que predecir el texto original. + +### multimodal + +Una tarea que combina textos con otro tipo de entradas (por ejemplo: imรกgenes). + +## N + +### Natural language generation (NLG) + +Todas las tareas relacionadas con la generaciรณn de texto (por ejemplo: [Escribe con Transformers](https://transformer.huggingface.co/) o traducciรณn). + +### Natural language processing (NLP) + +Una forma genรฉrica de decir "trabajar con textos". + +### Natural language understanding (NLU) + +Todas las tareas relacionadas con entender lo que hay en un texto (por ejemplo: clasificar el +texto completo o palabras individuales). + +## P + +### Pipeline + +Un pipeline en ๐Ÿค— Transformers es una abstracciรณn que se refiere a una serie de pasos que se ejecutan en un orden especรญfico para preprocesar y transformar datos y devolver una predicciรณn de un modelo. Algunas etapas de ejemplo que se encuentran en un pipeline pueden ser el preprocesamiento de datos, la extracciรณn de caracterรญsticas y la normalizaciรณn. + +Para obtener mรกs detalles, consulta [Pipelines para inferencia](https://huggingface.co/docs/transformers/pipeline_tutorial). + +### PipelineParallel (PP) + +Tรฉcnica de paralelismo en la que el modelo se divide verticalmente (a nivel de capa) en varios GPU, de modo que solo una o varias capas del modelo se colocan en un solo GPU. Cada GPU procesa en paralelo diferentes etapas del pipeline y trabaja en un pequeรฑo fragmento del lote. Obtรฉn mรกs informaciรณn sobre cรณmo funciona PipelineParallel [aquรญ](perf_train_gpu_many#from-naive-model-parallelism-to-pipeline-parallelism). + +### pixel values + +Un tensor de las representaciones numรฉricas de una imagen que se pasa a un modelo. Los valores de pรญxeles tienen una forma de [`batch_size`, `num_channels`, `height`, `width`], y se generan a partir de un procesador de imรกgenes. + +### pooling + +Una operaciรณn que reduce una matriz a una matriz mรกs pequeรฑa, ya sea tomando el mรกximo o el promedio de la dimensiรณn (o dimensiones) agrupada(s). Las capas de agrupaciรณn se encuentran comรบnmente entre capas convolucionales para reducir la representaciรณn de caracterรญsticas. + +### position IDs + +A diferencia de las RNN que tienen la posiciรณn de cada token incrustada en ellas, los transformers no son conscientes de la posiciรณn de cada token. Por lo tanto, se utilizan los IDs de posiciรณn (`position_ids`) para que el modelo identifique la posiciรณn de cada token en la lista de tokens. + +Son un parรกmetro opcional. Si no se pasan `position_ids` al modelo, los IDs se crean automรกticamente como embeddings de posiciรณn absolutas. + +Los embeddings de posiciรณn absolutas se seleccionan en el rango `[0, config.max_position_embeddings - 1]`. Algunos modelos utilizan otros tipos de embeddings de posiciรณn, como embeddings de posiciรณn sinusoidales o embeddings de posiciรณn relativas. + +### preprocessing + +La tarea de preparar datos crudos en un formato que pueda ser fรกcilmente consumido por modelos de aprendizaje automรกtico. Por ejemplo, el texto se preprocesa tรญpicamente mediante la tokenizaciรณn. Para tener una mejor idea de cรณmo es el preprocesamiento para otros tipos de entrada, consulta el tutorial [Pre-procesar](https://huggingface.co/docs/transformers/preprocessing). + +### pretrained model + +Un modelo que ha sido pre-entrenado en algunos datos (por ejemplo, toda Wikipedia). Los mรฉtodos de preentrenamiento involucran un objetivo auto-supervisado, que puede ser leer el texto e intentar predecir la siguiente palabra (ver [modelado de lenguaje causal](#causal-language-modeling)) o enmascarar algunas palabras e intentar predecirlas (ver [modelado de lenguaje enmascarado](#masked-language-modeling-mlm)). + +Los modelos de habla y visiรณn tienen sus propios objetivos de pre-entrenamiento. Por ejemplo, Wav2Vec2 es un modelo de habla pre-entrenado en una tarea contrastiva que requiere que el modelo identifique la representaciรณn de habla "verdadera" de un conjunto de representaciones de habla "falsas". Por otro lado, BEiT es un modelo de visiรณn pre-entrenado en una tarea de modelado de imรกgenes enmascaradas que enmascara algunos de los parches de la imagen y requiere que el modelo prediga los parches enmascarados (similar al objetivo de modelado de lenguaje enmascarado). + +## R + +### recurrent neural network (RNN) + +Un tipo de modelo que utiliza un bucle sobre una capa para procesar textos. + +### representation learning + +Un subcampo del aprendizaje automรกtico que se centra en aprender representaciones significativas de datos en bruto. Algunos ejemplos de tรฉcnicas de aprendizaje de representaciones incluyen embeddings de palabras, auto-encoders y Redes Generativas Adversarias (Generative Adversarial Networks, GANs). + +## S + +### sampling rate + +Una medida en hercios del nรบmero de muestras (la seรฑal de audio) tomadas por segundo. La tasa de muestreo es el resultado de aproximar una seรฑal continua como el habla. + +### self-attention + +Cada elemento de la entrada averigua a cuรกles otros elementos de la entrada debe prestar atenciรณn. + +### self-supervised learning + +Una categorรญa de tรฉcnicas de aprendizaje automรกtico en la que un modelo crea su propio objetivo de aprendizaje a partir de datos no etiquetados. Difiere del [aprendizaje no supervisado](#unsupervised-learning) y del [aprendizaje supervisado](#supervised-learning) en que el proceso de aprendizaje estรก supervisado, pero no explรญcitamente por el usuario. + +Un ejemplo de aprendizaje auto-supervisado es el [modelado de lenguaje enmascarado](#masked-language-modeling-mlm), donde un modelo recibe oraciones con una proporciรณn de sus tokens eliminados y aprende a predecir los tokens faltantes. + +### semi-supervised learning + +Una amplia categorรญa de tรฉcnicas de entrenamiento de aprendizaje automรกtico que aprovecha una pequeรฑa cantidad de datos etiquetados con una mayor cantidad de datos no etiquetados para mejorar la precisiรณn de un modelo, a diferencia del [aprendizaje supervisado](#supervised-learning) y del [aprendizaje no supervisado](#unsupervised-learning). + +Un ejemplo de un enfoque de aprendizaje semi-supervisado es "auto-entrenamiento", en el que un modelo se entrena con datos etiquetados y luego se utiliza para hacer predicciones sobre los datos no etiquetados. La porciรณn de datos no etiquetados que el modelo predice con mayor confianza se agrega al conjunto de datos etiquetados y se utiliza para volver a entrenar el modelo. + +### sequence-to-sequence (seq2seq) + +Modelos que generan una nueva secuencia a partir de una entrada, como modelos de traducciรณn o modelos de resumen (como +[Bart](model_doc/bart) o [T5](model_doc/t5)). + +### Sharded DDP + +Otro nombre para el concepto fundamental de [ZeRO](#zero-redundancy-optimizer-zero) utilizado por varias otras implementaciones de ZeRO. + +### stride + +En [convoluciรณn](#convolution) o [agrupaciรณn](#pooling), el paso (stride) se refiere a la distancia que recorre el nรบcleo sobre una matriz. Un paso de 1 significa que el nรบcleo se mueve un pรญxel a la vez, y un paso de 2 significa que el nรบcleo se mueve dos pรญxeles a la vez. + +### supervised learning + +Una forma de entrenamiento de modelos que utiliza directamente datos etiquetados para corregir y dirigir el rendimiento del modelo. Los datos se introducen en el modelo en entrenamiento, y sus predicciones se comparan con las etiquetas conocidas. El modelo actualiza sus pesos en funciรณn de cuรกn incorrectas fueron sus predicciones, y el proceso se repite para optimizar el rendimiento del modelo. + +## T + +### Tensor Parallelism (TP) + +Tรฉcnica de paralelismo para entrenamiento en mรบltiples GPU en la que cada tensor se divide en mรบltiples fragmentos, de modo que en lugar de tener todo el tensor en una sola GPU, cada fragmento del tensor reside en su GPU designada. Los fragmentos se procesan por separado y en paralelo en diferentes GPU y los resultados se sincronizan al final del paso de procesamiento.Esto es lo que a veces se llama paralelismo horizontal, ya que la divisiรณn ocurre a nivel horizontal. +Obtรฉn mรกs informaciรณn sobre el Paralelismo de Tensores [aquรญ](perf_train_gpu_many#tensor-parallelism). + +### token + +Parte de una oraciรณn, generalmente una palabra, pero tambiรฉn puede ser una sub-palabra (las palabras no comunes a menudo se dividen en sub-palabras) o un sรญmbolo de puntuaciรณn. + +### token Type IDs + +Algunos modelos tienen como objetivo realizar clasificaciรณn en pares de oraciones o responder preguntas. + + + +Estos requieren que dos secuencias diferentes se unan en una รบnica entrada "input_ids", lo cual generalmente se realiza con +la ayuda de tokens especiales, como el token de clasificaciรณn (`[CLS]`) y el token separador (`[SEP]`). Por ejemplo, el modelo BERT construye sus dos secuencias de entrada de la siguiente manera: + +```python +>>> # [CLS] SEQUENCE_A [SEP] SEQUENCE_B [SEP] +``` + +Podemos utilizar nuestro tokenizador para generar automรกticamente una oraciรณn de este tipo al pasar las dos secuencias a `tokenizer` como dos argumentos (y no como una lista, como antes) de la siguiente manera: + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") +>>> sequence_a = "HuggingFace is based in NYC" +>>> sequence_b = "Where is HuggingFace based?" + +>>> encoded_dict = tokenizer(sequence_a, sequence_b) +>>> decoded = tokenizer.decode(encoded_dict["input_ids"]) +``` + +Que devolverรก: + +```python +>>> print(decoded) +[CLS] HuggingFace is based in NYC [SEP] Where is HuggingFace based? [SEP] +``` + +Esto es suficiente para que algunos modelos comprendan dรณnde termina una secuencia y comienza otra. Sin embargo, otros modelos, como BERT, tambiรฉn utilizan identificadores de tipo de token (tambiรฉn llamados identificadores de segmento). Se representan como una mรกscara binaria que identifica los dos tipos de secuencia en el modelo. + +El tokenizador devuelve esta mรกscara como la entrada "token_type_ids": + +```python +>>> encoded_dict["token_type_ids"] +[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1] +``` + +La primera secuencia, el "contexto" utilizado para la pregunta, tiene todos sus tokens representados por un `0`, mientras que la segunda secuencia, correspondiente a la "pregunta", tiene todos sus tokens representados por un `1`. + +Algunos modelos, como [`XLNetModel`], utilizan un token adicional representado por un `2`. + +### transfer learning + +Una tรฉcnica que implica tomar un modelo pre-entrenado y adaptarlo a un conjunto de datos especรญfico para tu tarea. En lugar de entrenar un modelo desde cero, puedes aprovechar el conocimiento obtenido de un modelo existente como punto de partida. Esto acelera el proceso de aprendizaje y reduce la cantidad de datos de entrenamiento necesarios. + +### transformer + +Arquitectura de modelo de aprendizaje profundo basada en auto-atenciรณn (Self-attention). + +## U + +### unsupervised learning + +Una forma de entrenamiento de modelos en la que los datos proporcionados al modelo no estรกn etiquetados. Las tรฉcnicas de aprendizaje no supervisado aprovechan la informaciรณn estadรญstica de la distribuciรณn de datos para encontrar patrones รบtiles para la tarea en cuestiรณn. + +## Z + +### Zero Redundancy Optimizer (ZeRO) + +Tรฉcnica de paralelismo que realiza la fragmentaciรณn de los tensores de manera algo similar a [TensorParallel](#tensor-parallelism-tp), excepto que todo el tensor se reconstruye a tiempo para una computaciรณn hacia adelante o hacia atrรกs, por lo tanto, el modelo no necesita ser modificado. Este mรฉtodo tambiรฉn admite diversas tรฉcnicas de descarga para compensar la memoria limitada de la GPU. Obtรฉn mรกs informaciรณn sobre ZeRO [aquรญ](perf_train_gpu_many#zero-data-parallelism). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/index.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/index.md new file mode 100644 index 0000000000000000000000000000000000000000..428a7294475e546d2803702a8cb1f8e71d36e2db --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/index.md @@ -0,0 +1,281 @@ + + +# ๐Ÿค— Transformers + +Machine Learning de รบltima generaciรณn para PyTorch, TensorFlow y JAX. + +๐Ÿค— Transformers proporciona APIs para descargar y entrenar fรกcilmente modelos preentrenados de รบltima generaciรณn. El uso de modelos preentrenados puede reducir tus costos de cรณmputo, tu huella de carbono y ahorrarte tiempo al entrenar un modelo desde cero. Los modelos se pueden utilizar en diferentes modalidades, tales como: + +* ๐Ÿ“ Texto: clasificaciรณn de texto, extracciรณn de informaciรณn, respuesta a preguntas, resumir, traducciรณn y generaciรณn de texto en mรกs de 100 idiomas. +* ๐Ÿ–ผ๏ธ Imรกgenes: clasificaciรณn de imรกgenes, detecciรณn de objetos y segmentaciรณn. +* ๐Ÿ—ฃ๏ธ Audio: reconocimiento de voz y clasificaciรณn de audio. +* ๐Ÿ™ Multimodal: respuesta a preguntas en tablas, reconocimiento รณptico de caracteres, extracciรณn de informaciรณn de documentos escaneados, clasificaciรณn de videos y respuesta visual a preguntas. + +Nuestra biblioteca admite una integraciรณn perfecta entre tres de las bibliotecas de deep learning mรกs populares: [PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/) y [JAX](https://jax.readthedocs.io/en/latest/). Entrena tu modelo con tres lรญneas de cรณdigo en un framework y cรกrgalo para inferencia con otro. +Cada arquitectura de ๐Ÿค— Transformers se define en un mรณdulo de Python independiente para que se puedan personalizar fรกcilmente para investigaciรณn y experimentos. + +## Si estรกs buscando soporte personalizado del equipo de Hugging Face + + +HuggingFace Expert Acceleration Program + + +## Contenidos + +La documentaciรณn estรก organizada en cuatro partes: + +- **EMPEZAR** contiene un recorrido rรกpido e instrucciones de instalaciรณn para comenzar a usar ๐Ÿค— Transformers. +- **TUTORIALES** es un excelente lugar para comenzar. Esta secciรณn te ayudarรก a obtener las habilidades bรกsicas que necesitas para comenzar a usar ๐Ÿค— Transformers. +- **GUรAS PRรCTICAS** te mostrarรก cรณmo lograr un objetivo especรญfico, cรณmo hacer fine-tuning a un modelo preentrenado para el modelado de lenguaje o cรณmo crear un cabezal para un modelo personalizado. +- **GUรAS CONCEPTUALES** proporciona mรกs discusiรณn y explicaciรณn de los conceptos e ideas subyacentes detrรกs de los modelos, las tareas y la filosofรญa de diseรฑo de ๐Ÿค— Transformers. + +La biblioteca actualmente contiene implementaciones de JAX, PyTorch y TensorFlow, pesos de modelos preentrenados, scripts de uso y utilidades de conversiรณn para los siguientes modelos. + +### Modelos compatibles + + + +1. **[ALBERT](model_doc/albert)** (de Google Research y el Instituto Tecnolรณgico de Toyota en Chicago) publicado con el paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942), por Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. +1. **[ALIGN](model_doc/align)** (de Google Research) publicado con el paper [Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://huggingface.co/papers/2102.05918) por Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, Tom Duerig. +1. **[BART](model_doc/bart)** (de Facebook) publicado con el paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://huggingface.co/papers/1910.13461) por Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov y Luke Zettlemoyer. +1. **[BARThez](model_doc/barthez)** (de ร‰cole polytechnique) publicado con el paper [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) por Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis. +1. **[BARTpho](model_doc/bartpho)** (de VinAI Research) publicado con el paper [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701) por Nguyen Luong Tran, Duong Minh Le y Dat Quoc Nguyen. +1. **[BEiT](model_doc/beit)** (de Microsoft) publicado con el paper [BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) por Hangbo Bao, Li Dong, Furu Wei. +1. **[BERT](model_doc/bert)** (de Google) publicado con el paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) por Jacob Devlin, Ming-Wei Chang, Kenton Lee y Kristina Toutanova. +1. **[BERTweet](model_doc/bertweet)** (de VinAI Research) publicado con el paper [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) por Dat Quoc Nguyen, Thanh Vu y Anh Tuan Nguyen. +1. **[BERT For Sequence Generation](model_doc/bert-generation)** (de Google) publicado con el paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) por Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[BigBird-RoBERTa](model_doc/big_bird)** (de Google Research) publicado con el paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) por Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[BigBird-Pegasus](model_doc/bigbird_pegasus)** (de Google Research) publicado con el paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) por Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[Blenderbot](model_doc/blenderbot)** (de Facebook) publicado con el paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) por Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BlenderbotSmall](model_doc/blenderbot-small)** (de Facebook) publicado con el paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) por Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BORT](model_doc/bort)** (de Alexa) publicado con el paper [Optimal Subarchitecture Extraction For BERT](https://huggingface.co/papers/2010.10499) por Adrian de Wynter y Daniel J. Perry. +1. **[ByT5](model_doc/byt5)** (de Google Research) publicado con el paper [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) por Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel. +1. **[CamemBERT](model_doc/camembert)** (de Inria/Facebook/Sorbonne) publicado con el paper [CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) por Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suรกrez*, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la Clergerie, Djamรฉ Seddah y Benoรฎt Sagot. +1. **[CANINE](model_doc/canine)** (de Google Research) publicado con el paper [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://huggingface.co/papers/2103.06874) por Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. +1. **[ConvNeXT](model_doc/convnext)** (de Facebook AI) publicado con el paper [A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) por Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. +1. **[ConvNeXTV2](model_doc/convnextv2)** (de Facebook AI) publicado con el paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) por Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. +1. **[CLIP](model_doc/clip)** (de OpenAI) publicado con el paper [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) por Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. +1. **[ConvBERT](model_doc/convbert)** (de YituTech) publicado con el paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) por Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan. +1. **[CPM](model_doc/cpm)** (de Universidad de Tsinghua) publicado con el paper [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) por Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun. +1. **[CTRL](model_doc/ctrl)** (de Salesforce) publicado con el paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) por Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong y Richard Socher. +1. **[Data2Vec](model_doc/data2vec)** (de Facebook) publicado con el paper [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) por Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli. +1. **[DeBERTa](model_doc/deberta)** (de Microsoft) publicado con el paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) por Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[DeBERTa-v2](model_doc/deberta-v2)** (de Microsoft) publicado con el paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) por Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[Decision Transformer](model_doc/decision_transformer)** (de Berkeley/Facebook/Google) publicado con el paper [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) por Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. +1. **[DiT](model_doc/dit)** (de Microsoft Research) publicado con el paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://huggingface.co/papers/2203.02378) por Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei. +1. **[DeiT](model_doc/deit)** (de Facebook) publicado con el paper [Training data-efficient image transformers & distillation through attention](https://huggingface.co/papers/2012.12877) por Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervรฉ Jรฉgou. +1. **[DETR](model_doc/detr)** (de Facebook) publicado con el paper [End-to-End Object Detection with Transformers](https://huggingface.co/papers/2005.12872) por Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko. +1. **[DialoGPT](model_doc/dialogpt)** (de Microsoft Research) publicado con el paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) por Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan. +1. **[DistilBERT](model_doc/distilbert)** (de HuggingFace), publicado junto con el paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://huggingface.co/papers/1910.01108) por Victor Sanh, Lysandre Debut y Thomas Wolf. Se ha aplicado el mismo mรฉtodo para comprimir GPT2 en [DistilGPT2](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), RoBERTa en [DistilRoBERTa](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), BERT multilingรผe en [DistilmBERT](https://github.com/huggingface/transformers-research-projects/tree/main/distillation) y una versiรณn alemana de DistilBERT. +1. **[DPR](model_doc/dpr)** (de Facebook) publicado con el paper [Dense Passage Retrieval for Open-Domain Question Answering](https://huggingface.co/papers/2004.04906) por Vladimir Karpukhin, Barlas OฤŸuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, y Wen-tau Yih. +1. **[DPT](master/model_doc/dpt)** (de Intel Labs) publicado con el paper [Vision Transformers for Dense Prediction](https://huggingface.co/papers/2103.13413) por Renรฉ Ranftl, Alexey Bochkovskiy, Vladlen Koltun. +1. **[EfficientNet](model_doc/efficientnet)** (from Google Research) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://huggingface.co/papers/1905.11946) by Mingxing Tan and Quoc V. Le. +1. **[EncoderDecoder](model_doc/encoder-decoder)** (de Google Research) publicado con el paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) por Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[ELECTRA](model_doc/electra)** (de Google Research/Universidad de Stanford) publicado con el paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://huggingface.co/papers/2003.10555) por Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning. +1. **[FlauBERT](model_doc/flaubert)** (de CNRS) publicado con el paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://huggingface.co/papers/1912.05372) por Hang Le, Loรฏc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoรฎt Crabbรฉ, Laurent Besacier, Didier Schwab. +1. **[FNet](model_doc/fnet)** (de Google Research) publicado con el paper [FNet: Mixing Tokens with Fourier Transforms](https://huggingface.co/papers/2105.03824) por James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. +1. **[Funnel Transformer](model_doc/funnel)** (de CMU/Google Brain) publicado con el paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://huggingface.co/papers/2006.03236) por Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le. +1. **[GLPN](model_doc/glpn)** (de KAIST) publicado con el paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://huggingface.co/papers/2201.07436) por Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim. +1. **[GPT](model_doc/openai-gpt)** (de OpenAI) publicado con el paper [Improving Language Understanding by Generative Pre-Training](https://openai.com/research/language-unsupervised/) por Alec Radford, Karthik Narasimhan, Tim Salimans y Ilya Sutskever. +1. **[GPT-2](model_doc/gpt2)** (de OpenAI) publicado con el paper [Language Models are Unsupervised Multitask Learners](https://openai.com/research/better-language-models/) por Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei y Ilya Sutskever. +1. **[GPT-J](model_doc/gptj)** (de EleutherAI) publicado con el repositorio [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) por Ben Wang y Aran Komatsuzaki. +1. **[GPT Neo](model_doc/gpt_neo)** (de EleutherAI) publicado en el paper [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) por Sid Black, Stella Biderman, Leo Gao, Phil Wang y Connor Leahy. +1. **[GPTSAN-japanese](model_doc/gptsan-japanese)** released with [GPTSAN](https://github.com/tanreinama/GPTSAN) by Toshiyuki Sakamoto (tanreinama). +1. **[Hubert](model_doc/hubert)** (de Facebook) publicado con el paper [HuBERT: Self-Supervised Speech Representation Learning por Masked Prediction of Hidden Units](https://huggingface.co/papers/2106.07447) por Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed. +1. **[I-BERT](model_doc/ibert)** (de Berkeley) publicado con el paper [I-BERT: Integer-only BERT Quantization](https://huggingface.co/papers/2101.01321) por Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer. +1. **[ImageGPT](model_doc/imagegpt)** (de OpenAI) publicado con el paper [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) por Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. +1. **[LayoutLM](model_doc/layoutlm)** (de Microsoft Research Asia) publicado con el paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://huggingface.co/papers/1912.13318) por Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou. +1. **[LayoutLMv2](model_doc/layoutlmv2)** (de Microsoft Research Asia) publicado con el paper [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://huggingface.co/papers/2012.14740) por Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. +1. **[LayoutXLM](model_doc/layoutxlm)** (de Microsoft Research Asia) publicado con el paper [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://huggingface.co/papers/2104.08836) por Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei. +1. **[LED](model_doc/led)** (de AllenAI) publicado con el paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) por Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[Longformer](model_doc/longformer)** (de AllenAI) publicado con el paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) por Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LUKE](model_doc/luke)** (de Studio Ousia) publicado con el paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://huggingface.co/papers/2010.01057) por Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto. +1. **[mLUKE](model_doc/mluke)** (de Studio Ousia) publicado con el paper [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://huggingface.co/papers/2110.08151) por Ryokan Ri, Ikuya Yamada, y Yoshimasa Tsuruoka. +1. **[LXMERT](model_doc/lxmert)** (de UNC Chapel Hill) publicado con el paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://huggingface.co/papers/1908.07490) por Hao Tan y Mohit Bansal. +1. **[M2M100](model_doc/m2m_100)** (de Facebook) publicado con el paper [Beyond English-Centric Multilingual Machine Translation](https://huggingface.co/papers/2010.11125) por Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin. +1. **[MarianMT](model_doc/marian)** Modelos de traducciรณn automรกtica entrenados usando [OPUS](http://opus.nlpl.eu/) data por Jรถrg Tiedemann. El [Marian Framework](https://marian-nmt.github.io/) estรก siendo desarrollado por el equipo de traductores de Microsoft. +1. **[Mask2Former](model_doc/mask2former)** (de FAIR y UIUC) publicado con el paper [Masked-attention Mask Transformer for Universal Image Segmentation](https://huggingface.co/papers/2112.01527) por Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. +1. **[MaskFormer](model_doc/maskformer)** (de Meta y UIUC) publicado con el paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://huggingface.co/papers/2107.06278) por Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. +1. **[MBart](model_doc/mbart)** (de Facebook) publicado con el paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://huggingface.co/papers/2001.08210) por Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer. +1. **[MBart-50](model_doc/mbart)** (de Facebook) publicado con el paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://huggingface.co/papers/2008.00401) por Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan. +1. **[Megatron-BERT](model_doc/megatron-bert)** (de NVIDIA) publicado con el paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) por Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper y Bryan Catanzaro. +1. **[Megatron-GPT2](model_doc/megatron_gpt2)** (de NVIDIA) publicado con el paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) por Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper y Bryan Catanzaro. +1. **[MPNet](model_doc/mpnet)** (de Microsoft Research) publicado con el paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://huggingface.co/papers/2004.09297) por Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu. +1. **[MT5](model_doc/mt5)** (de Google AI) publicado con el paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://huggingface.co/papers/2010.11934) por Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel. +1. **[Nystrรถmformer](model_doc/nystromformer)** (de la Universidad de Wisconsin - Madison) publicado con el paper [Nystrรถmformer: A Nystrรถm-Based Algorithm for Approximating Self-Attention](https://huggingface.co/papers/2102.03902) por Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh. +1. **[OneFormer](model_doc/oneformer)** (de la SHI Labs) publicado con el paper [OneFormer: One Transformer to Rule Universal Image Segmentation](https://huggingface.co/papers/2211.06220) por Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. +1. **[Pegasus](model_doc/pegasus)** (de Google) publicado con el paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://huggingface.co/papers/1912.08777) por Jingqing Zhang, Yao Zhao, Mohammad Saleh y Peter J. Liu. +1. **[Perceiver IO](model_doc/perceiver)** (de Deepmind) publicado con el paper [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://huggingface.co/papers/2107.14795) por Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hรฉnaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, Joรฃo Carreira. +1. **[PhoBERT](model_doc/phobert)** (de VinAI Research) publicado con el paper [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) por Dat Quoc Nguyen y Anh Tuan Nguyen. +1. **[PLBart](model_doc/plbart)** (de UCLA NLP) publicado con el paper [Unified Pre-training for Program Understanding and Generation](https://huggingface.co/papers/2103.06333) por Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang. +1. **[PoolFormer](model_doc/poolformer)** (de Sea AI Labs) publicado con el paper [MetaFormer is Actually What You Need for Vision](https://huggingface.co/papers/2111.11418) por Yu, Weihao y Luo, Mi y Zhou, Pan y Si, Chenyang y Zhou, Yichen y Wang, Xinchao y Feng, Jiashi y Yan, Shuicheng. +1. **[ProphetNet](model_doc/prophetnet)** (de Microsoft Research) publicado con el paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) por Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang y Ming Zhou. +1. **[QDQBert](model_doc/qdqbert)** (de NVIDIA) publicado con el paper [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://huggingface.co/papers/2004.09602) por Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev y Paulius Micikevicius. +1. **[REALM](model_doc/realm.html)** (de Google Research) publicado con el paper [REALM: Retrieval-Augmented Language Model Pre-Training](https://huggingface.co/papers/2002.08909) por Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat y Ming-Wei Chang. +1. **[Reformer](model_doc/reformer)** (de Google Research) publicado con el paper [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) por Nikita Kitaev, ลukasz Kaiser, Anselm Levskaya. +1. **[RemBERT](model_doc/rembert)** (de Google Research) publicado con el paper [Rethinking embedding coupling in pre-trained language models](https://huggingface.co/papers/2010.12821) por Hyung Won Chung, Thibault Fรฉvry, Henry Tsai, M. Johnson, Sebastian Ruder. +1. **[RegNet](model_doc/regnet)** (de META Platforms) publicado con el paper [Designing Network Design Space](https://huggingface.co/papers/2003.13678) por Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollรกr. +1. **[ResNet](model_doc/resnet)** (de Microsoft Research) publicado con el paper [Deep Residual Learning for Image Recognition](https://huggingface.co/papers/1512.03385) por Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. +1. **[RoBERTa](model_doc/roberta)** (de Facebook), publicado junto con el paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692) por Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. +1. **[RoFormer](model_doc/roformer)** (de ZhuiyiTechnology), publicado junto con el paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://huggingface.co/papers/2104.09864) por Jianlin Su y Yu Lu y Shengfeng Pan y Bo Wen y Yunfeng Liu. +1. **[SegFormer](model_doc/segformer)** (de NVIDIA) publicado con el paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://huggingface.co/papers/2105.15203) por Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo. +1. **[SEW](model_doc/sew)** (de ASAPP) publicado con el paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) por Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SEW-D](model_doc/sew_d)** (de ASAPP) publicado con el paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) por Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SpeechToTextTransformer](model_doc/speech_to_text)** (de Facebook), publicado junto con el paper [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://huggingface.co/papers/2010.05171) por Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. +1. **[SpeechToTextTransformer2](model_doc/speech_to_text_2)** (de Facebook), publicado junto con el paper [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://huggingface.co/papers/2104.06678) por Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. +1. **[Splinter](model_doc/splinter)** (de Universidad de Tel Aviv), publicado junto con el paper [Few-Shot Question Answering by Pretraining Span Selection](https://huggingface.co/papers/2101.00438) pory Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. +1. **[SqueezeBert](model_doc/squeezebert)** (de Berkeley) publicado con el paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://huggingface.co/papers/2006.11316) por Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, y Kurt W. Keutzer. +1. **[Swin Transformer](model_doc/swin)** (de Microsoft) publicado con el paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030) por Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. +1. **[T5](model_doc/t5)** (de Google AI) publicado con el paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://huggingface.co/papers/1910.10683) por Colin Raffel y Noam Shazeer y Adam Roberts y Katherine Lee y Sharan Narang y Michael Matena y Yanqi Zhou y Wei Li y Peter J. Liu. +1. **[T5v1.1](model_doc/t5v1.1)** (de Google AI) publicado en el repositorio [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) por Colin Raffel y Noam Shazeer y Adam Roberts y Katherine Lee y Sharan Narang y Michael Matena y Yanqi Zhou y Wei Li y Peter J. Liu. +1. **[TAPAS](model_doc/tapas)** (de Google AI) publicado con el paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://huggingface.co/papers/2004.02349) por Jonathan Herzig, Paweล‚ Krzysztof Nowak, Thomas Mรผller, Francesco Piccinno y Julian Martin Eisenschlos. +1. **[TAPEX](model_doc/tapex)** (de Microsoft Research) publicado con el paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://huggingface.co/papers/2107.07653) por Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou. +1. **[Transformer-XL](model_doc/transfo-xl)** (de Google/CMU) publicado con el paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://huggingface.co/papers/1901.02860) por Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. +1. **[TrOCR](model_doc/trocr)** (de Microsoft), publicado junto con el paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://huggingface.co/papers/2109.10282) por Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei. +1. **[UniSpeech](model_doc/unispeech)** (de Microsoft Research) publicado con el paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://huggingface.co/papers/2101.07597) por Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang. +1. **[UniSpeechSat](model_doc/unispeech-sat)** (de Microsoft Research) publicado con el paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://huggingface.co/papers/2110.05752) por Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu. +1. **[VAN](model_doc/van)** (de la Universidad de Tsinghua y la Universidad de Nankai) publicado con el paper [Visual Attention Network](https://huggingface.co/papers/2202.09741) por Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu. +1. **[ViLT](model_doc/vilt)** (de NAVER AI Lab/Kakao Enterprise/Kakao Brain) publicado con el paper [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://huggingface.co/papers/2102.03334) por Wonjae Kim, Bokyung Son, Ildoo Kim. +1. **[Vision Transformer (ViT)](model_doc/vit)** (de Google AI) publicado con el paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) por Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. +1. **[ViTMAE](model_doc/vit_mae)** (de Meta AI) publicado con el paper [Masked Autoencoders Are Scalable Vision Learners](https://huggingface.co/papers/2111.06377) por Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollรกr, Ross Girshick. +1. **[VisualBERT](model_doc/visual_bert)** (de UCLA NLP) publicado con el paper [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://huggingface.co/papers/1908.03557) por Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang. +1. **[WavLM](model_doc/wavlm)** (de Microsoft Research) publicado con el paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://huggingface.co/papers/2110.13900) por Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei. +1. **[Wav2Vec2](model_doc/wav2vec2)** (de Facebook AI) publicado con el paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://huggingface.co/papers/2006.11477) por Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli. +1. **[Wav2Vec2Phoneme](model_doc/wav2vec2_phoneme)** (de Facebook AI) publicado con el paper [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://huggingface.co/papers/2109.11680) por Qiantong Xu, Alexei Baevski, Michael Auli. +1. **[XGLM](model_doc/xglm)** (de Facebook AI) publicado con el paper [Few-shot Learning with Multilingual Language Models](https://huggingface.co/papers/2112.10668) por Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O'Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li. +1. **[XLM](model_doc/xlm)** (de Facebook) publicado junto con el paper [Cross-lingual Language Model Pretraining](https://huggingface.co/papers/1901.07291) por Guillaume Lample y Alexis Conneau. +1. **[XLM-ProphetNet](model_doc/xlm-prophetnet)** (de Microsoft Research) publicado con el paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) por Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang y Ming Zhou. +1. **[XLM-RoBERTa](model_doc/xlm-roberta)** (de Facebook AI), publicado junto con el paper [Unsupervised Cross-lingual Representation Learning at Scale](https://huggingface.co/papers/1911.02116) por Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmรกn, Edouard Grave, Myle Ott, Luke Zettlemoyer y Veselin Stoyanov. +1. **[XLM-RoBERTa-XL](model_doc/xlm-roberta-xl)** (de Facebook AI), publicado junto con el paper [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://huggingface.co/papers/2105.00572) por Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau. +1. **[XLNet](model_doc/xlnet)** (de Google/CMU) publicado con el paper [XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://huggingface.co/papers/1906.08237) por Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. +1. **[XLSR-Wav2Vec2](model_doc/xlsr_wav2vec2)** (de Facebook AI) publicado con el paper [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://huggingface.co/papers/2006.13979) por Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli. +1. **[XLS-R](model_doc/xls_r)** (de Facebook AI) publicado con el paper [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://huggingface.co/papers/2111.09296) por Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli. +1. **[YOSO](model_doc/yoso)** (de la Universidad de Wisconsin-Madison) publicado con el paper [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://huggingface.co/papers/2111.09714) por Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh. + + +### Frameworks compatibles + +La siguiente tabla representa el soporte actual en la biblioteca para cada uno de esos modelos, ya sea que tengan un tokenizador de Python (llamado "slow"). Un tokenizador "fast" respaldado por la biblioteca ๐Ÿค— Tokenizers, ya sea que tengan soporte en Jax (a travรฉs de +Flax), PyTorch y/o TensorFlow. + + + +| Modelo | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | +|:---------------------------:|:--------------:|:--------------:|:---------------:|:------------------:|:------------:| +| ALBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| BART | โœ… | โœ… | โœ… | โœ… | โœ… | +| BEiT | โŒ | โŒ | โœ… | โŒ | โœ… | +| BERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| Bert Generation | โœ… | โŒ | โœ… | โŒ | โŒ | +| BigBird | โœ… | โœ… | โœ… | โŒ | โœ… | +| BigBirdPegasus | โŒ | โŒ | โœ… | โŒ | โŒ | +| Blenderbot | โœ… | โœ… | โœ… | โœ… | โœ… | +| BlenderbotSmall | โœ… | โœ… | โœ… | โœ… | โœ… | +| CamemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| Canine | โœ… | โŒ | โœ… | โŒ | โŒ | +| CLIP | โœ… | โœ… | โœ… | โœ… | โœ… | +| ConvBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ConvNext | โŒ | โŒ | โœ… | โœ… | โŒ | +| CTRL | โœ… | โŒ | โœ… | โœ… | โŒ | +| Data2VecAudio | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecText | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeBERTa | โœ… | โœ… | โœ… | โœ… | โŒ | +| DeBERTa-v2 | โœ… | โŒ | โœ… | โœ… | โŒ | +| Decision Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeiT | โŒ | โŒ | โœ… | โŒ | โŒ | +| DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DistilBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| DPR | โœ… | โœ… | โœ… | โœ… | โŒ | +| DPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ELECTRA | โœ… | โœ… | โœ… | โœ… | โœ… | +| Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| FairSeq Machine-Translation | โœ… | โŒ | โœ… | โŒ | โŒ | +| FlauBERT | โœ… | โŒ | โœ… | โœ… | โŒ | +| FNet | โœ… | โœ… | โœ… | โŒ | โŒ | +| Funnel Transformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| GLPN | โŒ | โŒ | โœ… | โŒ | โŒ | +| GPT Neo | โŒ | โŒ | โœ… | โŒ | โœ… | +| GPT-J | โŒ | โŒ | โœ… | โœ… | โœ… | +| Hubert | โŒ | โŒ | โœ… | โœ… | โŒ | +| I-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ImageGPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| LayoutLM | โœ… | โœ… | โœ… | โœ… | โŒ | +| LayoutLMv2 | โœ… | โœ… | โœ… | โŒ | โŒ | +| LED | โœ… | โœ… | โœ… | โœ… | โŒ | +| Longformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| LUKE | โœ… | โŒ | โœ… | โŒ | โŒ | +| LXMERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| M2M100 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Marian | โœ… | โŒ | โœ… | โœ… | โœ… | +| MaskFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| mBART | โœ… | โœ… | โœ… | โœ… | โœ… | +| MegatronBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| MPNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| mT5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Nystromformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OpenAI GPT | โœ… | โœ… | โœ… | โœ… | โŒ | +| OpenAI GPT-2 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Pegasus | โœ… | โœ… | โœ… | โœ… | โœ… | +| Perceiver | โœ… | โŒ | โœ… | โŒ | โŒ | +| PLBart | โœ… | โŒ | โœ… | โŒ | โŒ | +| PoolFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| QDQBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| RAG | โœ… | โŒ | โœ… | โœ… | โŒ | +| Realm | โœ… | โœ… | โœ… | โŒ | โŒ | +| Reformer | โœ… | โœ… | โœ… | โŒ | โŒ | +| RegNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ResNet | โŒ | โŒ | โœ… | โŒ | โœ… | +| RetriBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| RoFormer | โœ… | โœ… | โœ… | โœ… | โœ… | +| SegFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW-D | โŒ | โŒ | โœ… | โŒ | โŒ | +| Speech Encoder decoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| Speech2Text | โœ… | โŒ | โœ… | โœ… | โŒ | +| Speech2Text2 | โœ… | โŒ | โŒ | โŒ | โŒ | +| Splinter | โœ… | โœ… | โœ… | โŒ | โŒ | +| SqueezeBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| Swin | โŒ | โŒ | โœ… | โŒ | โŒ | +| T5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| TAPAS | โœ… | โŒ | โœ… | โœ… | โŒ | +| TAPEX | โœ… | โœ… | โœ… | โœ… | โœ… | +| Transformer-XL | โœ… | โŒ | โœ… | โœ… | โŒ | +| TrOCR | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeech | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeechSat | โŒ | โŒ | โœ… | โŒ | โŒ | +| VAN | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Vision Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| VisionTextDualEncoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| VisualBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViT | โŒ | โŒ | โœ… | โœ… | โœ… | +| ViTMAE | โŒ | โŒ | โœ… | โœ… | โŒ | +| Wav2Vec2 | โœ… | โŒ | โœ… | โœ… | โœ… | +| WavLM | โŒ | โŒ | โœ… | โŒ | โŒ | +| XGLM | โœ… | โœ… | โœ… | โŒ | โœ… | +| XLM | โœ… | โŒ | โœ… | โœ… | โŒ | +| XLM-RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM-RoBERTa-XL | โŒ | โŒ | โœ… | โŒ | โŒ | +| XLMProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| XLNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| YOSO | โŒ | โŒ | โœ… | โŒ | โŒ | + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/installation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/installation.md new file mode 100644 index 0000000000000000000000000000000000000000..714c3b195ebcc09d052f11ce9c34d4f301c54f02 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/installation.md @@ -0,0 +1,242 @@ + + +# Instalaciรณn + +En esta guรญa puedes encontrar informaciรณn para instalar ๐Ÿค— Transformers para cualquier biblioteca de Machine Learning con la que estรฉs trabajando. Ademรกs, encontrarรกs informaciรณn sobre cรณmo establecer el cachรฉ y cรณmo configurar ๐Ÿค— Transformers para correrlo de manera offline (opcional). + +๐Ÿค— Transformers ha sido probada en Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, y Flax. Para instalar la biblioteca de deep learning con la que desees trabajar, sigue las instrucciones correspondientes listadas a continuaciรณn: + +* [PyTorch](https://pytorch.org/get-started/locally/) +* [TensorFlow 2.0](https://www.tensorflow.org/install/pip) +* [Flax](https://flax.readthedocs.io/en/latest/) + +## Instalaciรณn con pip + +Es necesario instalar ๐Ÿค— Transformers en un [entorno virtual](https://docs.python.org/3/library/venv.html). Si necesitas mรกs informaciรณn sobre entornos virtuales de Python, consulta esta [guรญa](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ +). Un entorno virtual facilita el manejo de proyectos y evita problemas de compatibilidad entre dependencias. + +Comienza por crear un entorno virtual en el directorio de tu proyecto: + +```bash +python -m venv .env +``` + +Activa el entorno virtual: + +```bash +source .env/bin/activate +``` + +Ahora puedes instalar ๐Ÿค— Transformers con el siguiente comando: + +```bash +pip install transformers +``` + +Solo para CPU, puedes instalar ๐Ÿค— Transformers y una biblioteca de deep learning con un comando de una sola lรญnea. + +Por ejemplo, instala ๐Ÿค— Transformers y Pytorch: + +```bash +pip install transformers[torch] +``` + +๐Ÿค— Transformers y TensorFlow 2.0: + +```bash +pip install transformers[tf-cpu] +``` + +๐Ÿค— Transformers y Flax: + +```bash +pip install transformers[flax] +``` + +Por รบltimo, revisa si ๐Ÿค— Transformers ha sido instalada exitosamente con el siguiente comando que descarga un modelo pre-entrenado: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" +``` +Despuรฉs imprime la etiqueta y el puntaje: + +```bash +[{'label': 'POSITIVE', 'score': 0.9998704791069031}] +``` + +## Instalaciรณn desde la fuente + +Instala ๐Ÿค— Transformers desde la fuente con el siguiente comando: + +```bash +pip install git+https://github.com/huggingface/transformers +``` + +El comando de arriba instala la versiรณn `master` mรกs actual en vez de la รบltima versiรณn estable. La versiรณn `master` es รบtil para obtener los รบltimos avances de ๐Ÿค— Transformers. Por ejemplo, se puede dar el caso de que un error fue corregido despuรฉs de la รบltima versiรณn estable pero aรบn no se ha liberado un nuevo lanzamiento. Sin embargo, existe la posibilidad de que la versiรณn `master` no sea estable. El equipo trata de mantener la versiรณn `master` operacional y la mayorรญa de los errores son resueltos en unas cuantas horas o un dรญa. Si encuentras algรบn problema, por favor abre un [Issue](https://github.com/huggingface/transformers/issues) para que pueda ser corregido mรกs rรกpido. + +Verifica si ๐Ÿค— Transformers estรก instalada apropiadamente con el siguiente comando: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" +``` + +## Instalaciรณn editable + +Necesitarรกs una instalaciรณn editable si deseas: +* Usar la versiรณn `master` del cรณdigo fuente. +* Contribuir a ๐Ÿค— Transformers y necesitas probar cambios en el cรณdigo. + +Clona el repositorio e instala ๐Ÿค— Transformers con los siguientes comandos: + +```bash +git clone https://github.com/huggingface/transformers.git +cd transformers +pip install -e . +``` + +ร‰stos comandos van a ligar el directorio desde donde clonamos el repositorio al path de las bibliotecas de Python. Python ahora buscarรก dentro de la carpeta que clonaste ademรกs de los paths normales de la biblioteca. Por ejemplo, si los paquetes de Python se encuentran instalados en `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python tambiรฉn buscarรก en el directorio desde donde clonamos el repositorio `~/transformers/`. + + + +Debes mantener el directorio `transformers` si deseas seguir usando la biblioteca. + + + +Puedes actualizar tu copia local a la รบltima versiรณn de ๐Ÿค— Transformers con el siguiente comando: + +```bash +cd ~/transformers/ +git pull +``` + +El entorno de Python que creaste para la instalaciรณn de ๐Ÿค— Transformers encontrarรก la versiรณn `master` en la siguiente ejecuciรณn. + +## Instalaciรณn con conda + +Puedes instalar ๐Ÿค— Transformers desde el canal de conda `conda-forge` con el siguiente comando: + +```bash +conda install conda-forge::transformers +``` + +## Configuraciรณn de Cachรฉ + +Los modelos preentrenados se descargan y almacenan en cachรฉ localmente en: `~/.cache/huggingface/transformers/`. Este es el directorio predeterminado proporcionado por la variable de entorno de shell `TRANSFORMERS_CACHE`. En Windows, el directorio predeterminado es dado por `C:\Users\username\.cache\huggingface\transformers`. Puedes cambiar las variables de entorno de shell que se muestran a continuaciรณn, en orden de prioridad, para especificar un directorio de cachรฉ diferente: + +1. Variable de entorno del shell (por defecto): `TRANSFORMERS_CACHE`. +2. Variable de entorno del shell:`HF_HOME` + `transformers/`. +3. Variable de entorno del shell: `XDG_CACHE_HOME` + `/huggingface/transformers`. + + + +๐Ÿค— Transformers usarรก las variables de entorno de shell `PYTORCH_TRANSFORMERS_CACHE` o `PYTORCH_PRETRAINED_BERT_CACHE` si viene de una iteraciรณn anterior de la biblioteca y ha configurado esas variables de entorno, a menos que especifiques la variable de entorno de shell `TRANSFORMERS_CACHE`. + + + + +## Modo Offline + +๐Ÿค— Transformers puede ejecutarse en un entorno con firewall o fuera de lรญnea (offline) usando solo archivos locales. Configura la variable de entorno `HF_HUB_OFFLINE=1` para habilitar este comportamiento. + + + +Puedes aรฑadir [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/) al flujo de entrenamiento offline declarando la variable de entorno `HF_DATASETS_OFFLINE=1`. + + + +Por ejemplo, normalmente ejecutarรญas un programa en una red normal con firewall para instancias externas con el siguiente comando: + +```bash +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +Ejecuta este mismo programa en una instancia offline con el siguiente comando: + +```bash +HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +El script ahora deberรญa ejecutarse sin bloquearse ni esperar a que se agote el tiempo de espera porque sabe que solo debe buscar archivos locales. + +### Obtener modelos y tokenizers para uso offline + +Otra opciรณn para usar ๐Ÿค— Transformers offline es descargando previamente los archivos y despuรฉs apuntar al path local donde se encuentren. Hay tres maneras de hacer esto: + +* Descarga un archivo mediante la interfaz de usuario del [Model Hub](https://huggingface.co/models) haciendo click en el รญcono โ†“. + + ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) + + +* Utiliza el flujo de [`PreTrainedModel.from_pretrained`] y [`PreTrainedModel.save_pretrained`]: + 1. Descarga previamente los archivos con [`PreTrainedModel.from_pretrained`]: + + ```py + >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + + >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") + >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") + ``` + + + 2. Guarda los archivos en un directorio especรญfico con [`PreTrainedModel.save_pretrained`]: + + ```py + >>> tokenizer.save_pretrained("./your/path/bigscience_t0") + >>> model.save_pretrained("./your/path/bigscience_t0") + ``` + + 3. Cuando te encuentres offline, recarga los archivos con [`PreTrainedModel.from_pretrained`] desde el directorio especificado: + + ```py + >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") + >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") + ``` + +* Descarga de manera programรกtica los archivos con la biblioteca [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub): + + 1. Instala la biblioteca [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) en tu entorno virtual: + + ```bash + python -m pip install huggingface_hub + ``` + + 2. Utiliza la funciรณn [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) para descargar un archivo a un path especรญfico. Por ejemplo, el siguiente comando descarga el archivo `config.json` del modelo [T0](https://huggingface.co/bigscience/T0_3B) al path deseado: + + ```py + >>> from huggingface_hub import hf_hub_download + + >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0") + ``` + +Una vez que el archivo se descargue y se almacene en cachรฉ localmente, especifica tu ruta local para cargarlo y usarlo: + +```py +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") +``` + + + +Para mรกs detalles sobre cรณmo descargar archivos almacenados en el Hub consulta la secciรณn [How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream). + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_memory_anatomy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_memory_anatomy.md new file mode 100644 index 0000000000000000000000000000000000000000..54609a1c1e7959376c057d1664992cfb9a3eaa42 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_memory_anatomy.md @@ -0,0 +1,239 @@ + + +# Anatomรญa del entrenamiento de los modelos + +Para entender las tรฉcnicas de optimizaciรณn del rendimiento que se pueden aplicar para mejorar la eficiencia en la velocidad del entrenamiento de los modelos y la utilizaciรณn de la memoria, es รบtil familiarizarse con cรณmo se utiliza la GPU durante el entrenamiento y cรณmo varรญa la intensidad de cรกlculo segรบn la operaciรณn realizada. + +Empecemos explorando un ejemplo enfocado en la utilizaciรณn de la GPU y la ejecuciรณn del entrenamiento de un modelo. Para la demostraciรณn, necesitaremos instalar algunas bibliotecas: + +```bash +pip install transformers datasets accelerate nvidia-ml-py3 +``` + +La biblioteca `nvidia-ml-py3` nos permite monitorear la utilizaciรณn de memoria de los modelos desde Python. Es posible que estรฉs familiarizado con el comando `nvidia-smi` en la terminal, esta biblioteca nos permite acceder a la misma informaciรณn en Python directamente. + +Luego, creamos algunos datos ficticios: IDs de tokens aleatorios entre 100 y 30000 y etiquetas binarias para un clasificador. En total, obtenemos 512 secuencias cada una con longitud 512 y las almacenamos en un [`~datasets.Dataset`] con formato PyTorch. + + +```py +>>> import numpy as np +>>> from datasets import Dataset + + +>>> seq_len, dataset_size = 512, 512 +>>> dummy_data = { +... "input_ids": np.random.randint(100, 30000, (dataset_size, seq_len)), +... "labels": np.random.randint(0, 1, (dataset_size)), +... } +>>> ds = Dataset.from_dict(dummy_data) +>>> ds.set_format("pt") +``` + +Para imprimir estadรญsticas resumidas para la utilizaciรณn de la GPU y la ejecuciรณn del entrenamiento con [`Trainer`](https://huggingface.co/docs/transformers/en/main_classes/trainer#transformers.Trainer), definimos dos funciones auxiliares: + +```py +>>> from pynvml import * + + +>>> def print_gpu_utilization(): +... nvmlInit() +... handle = nvmlDeviceGetHandleByIndex(0) +... info = nvmlDeviceGetMemoryInfo(handle) +... print(f"GPU memory occupied: {info.used//1024**2} MB.") + + +>>> def print_summary(result): +... print(f"Time: {result.metrics['train_runtime']:.2f}") +... print(f"Samples/second: {result.metrics['train_samples_per_second']:.2f}") +... print_gpu_utilization() +``` + +Comencemos comprobando que la memoria GPU este libre: + +```py +>>> print_gpu_utilization() +GPU memory occupied: 0 MB. +``` + +Parece estar bien: la memoria de la GPU no estรก ocupada como esperarรญamos antes de cargar cualquier modelo. Si no es el caso en tu mรกquina, asegรบrate de detener todos los procesos que estรฉn utilizando la memoria de la GPU. Sin embargo, no toda la memoria libre de la GPU puede ser utilizada por el usuario. Cuando se carga un modelo en la GPU, tambiรฉn se cargan los kernels, lo que puede ocupar 1-2GB de memoria. Para ver cuรกnta memoria serรก ocupada por defecto, cargemos un tensor diminuto en la GPU, lo que tambiรฉn desencadena la carga de los kernels. + +```py +>>> import torch + + +>>> torch.ones((1, 1)).to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 1343 MB. +``` + +Vemos que los kernels solos ocupan 1,3GB de memoria de la GPU. Ahora, veamos cuรกnto espacio ocupa el modelo. + +## Cargar el Modelo + +Primero, cargamos el modelo `google-bert/bert-large-uncased`. Los pesos del modelo son cargados directamente en la GPU para que podamos verificar cuรกnto espacio ocupan solo los pesos. + +```py +>>> from transformers import AutoModelForSequenceClassification + + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-large-uncased").to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 2631 MB. +``` + +Podemos ver que los pesos del modelo solos ocupan 1,3 GB de memoria de la GPU. El nรบmero exacto depende de la GPU especรญfica que estรฉs utilizando. Ten en cuenta que en GPUs mรกs modernas, un modelo puede ocupar mรกs espacio ya que los pesos se cargan de manera optimizada lo cual acelera el uso del modelo. Ahora tambiรฉn podemos verificar rรกpidamente si obtenemos el mismo resultado que con la CLI de `nvidia-smi`: + +```bash +nvidia-smi +``` + +```bash +Tue Jan 11 08:58:05 2022 ++-----------------------------------------------------------------------------+ +| NVIDIA-SMI 460.91.03 Driver Version: 460.91.03 CUDA Version: 11.2 | +|-------------------------------+----------------------+----------------------+ +| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|===============================+======================+======================| +| 0 Tesla V100-SXM2... On | 00000000:00:04.0 Off | 0 | +| N/A 37C P0 39W / 300W | 2631MiB / 16160MiB | 0% Default | +| | | N/A | ++-------------------------------+----------------------+----------------------+ + ++-----------------------------------------------------------------------------+ +| Processes: | +| GPU GI CI PID Type Process name GPU Memory | +| ID ID Usage | +|=============================================================================| +| 0 N/A N/A 3721 C ...nvs/codeparrot/bin/python 2629MiB | ++-----------------------------------------------------------------------------+ +``` + +Obtenemos el mismo nรบmero que antes y tambiรฉn puedes ver que estamos utilizando una GPU V100 con 16GB de memoria. Ahora podemos empezar a entrenar el modelo y ver cรณmo cambia el consumo de memoria de la GPU. Primero, configuramos algunos argumentos de entrenamiento estรกndar: + +```py +default_args = { + "output_dir": "tmp", + "eval_strategy": "steps", + "num_train_epochs": 1, + "log_level": "error", + "report_to": "none", +} +``` + + + +Si planeas ejecutar varias pruebas, reinicie el kernel de Python entre cada prueba para borrar correctamente la memoria. + + + +## Utilizaciรณn de la memoria en el entrenamiento + +Vamos a utilizar el [`Trainer`](https://huggingface.co/docs/transformers/en/main_classes/trainer#transformers.Trainer) y entrenar el modelo sin utilizar ninguna tรฉcnica de optimizaciรณn del rendimiento de la GPU y un tamaรฑo de lote de 4: + +```py +>>> from transformers import TrainingArguments, Trainer, logging + +>>> logging.set_verbosity_error() + + +>>> training_args = TrainingArguments(per_device_train_batch_size=4, **default_args) +>>> trainer = Trainer(model=model, args=training_args, train_dataset=ds) +>>> result = trainer.train() +>>> print_summary(result) +``` + +``` +Time: 57.82 +Samples/second: 8.86 +GPU memory occupied: 14949 MB. +``` + +Vemos que incluso un tamaรฑo de lote relativamente pequeรฑo casi llena toda la memoria de nuestra GPU. Sin embargo, un tamaรฑo de lote mรกs grande a menudo puede resultar en una convergencia del modelo mรกs rรกpida o un mejor rendimiento final. Asรญ que idealmente queremos ajustar el tamaรฑo del lote a las necesidades del modelo y no a las limitaciones de la GPU. Lo interesante es que utilizamos mucha mรกs memoria que el tamaรฑo del modelo. +Para entender un poco mejor por quรฉ es el caso, echemos un vistazo a las operaciones y necesidades de memoria de un modelo. + +## Anatomรญa de las Operaciones del Modelo + +La arquitectura de los transformers incluye 3 grupos principales de operaciones agrupadas a continuaciรณn por intensidad de cรกlculo. + +1. **Contracciones de Tensores** + + Las capas lineales y componentes de la Atenciรณn Multi-Head realizan **multiplicaciones matriciales por lotes**. Estas operaciones son la parte mรกs intensiva en cรกlculo del entrenamiento de los transformers. + +2. **Normalizaciones Estadรญsticas** + + Softmax y normalizaciรณn de capas son menos intensivas en cรกlculo que las contracciones de tensores, e implican una o mรกs **operaciones de reducciรณn**, cuyo resultado se aplica luego mediante un mapa. + +3. **Operadores por Elemento** + + Estos son los operadores restantes: **sesgos, dropout, activaciones y conexiones residuales**. Estas son las operaciones menos intensivas en cรกlculo. + +Este conocimiento puede ser รบtil al analizar cuellos de botella de rendimiento. + +Este resumen se deriva de [Data Movement Is All You Need: A Case Study on Optimizing Transformers 2020](https://huggingface.co/papers/2007.00072) + + +## Anatomรญa de la Memoria del Modelo + +Hemos visto que al entrenar un modelo se utiliza mucha mรกs memoria que solo poner el modelo en la GPU. Esto se debe a que hay muchos componentes durante el entrenamiento que utilizan memoria de la GPU. Los componentes en memoria de la GPU son los siguientes: + +1. pesos del modelo +2. estados del optimizador +3. gradientes +4. activaciones hacia adelante guardadas para el cรกlculo del gradiente +5. buffers temporales +6. memoria especรญfica de funcionalidad + +Un modelo tรญpico entrenado en precisiรณn mixta con AdamW requiere 18 bytes por parรกmetro del modelo mรกs memoria de activaciรณn. Para la inferencia no hay estados del optimizador ni gradientes, por lo que podemos restarlos. Y asรญ terminamos con 6 bytes por parรกmetro del modelo para la inferencia en precisiรณn mixta, mรกs la memoria de activaciรณn. + +Veรกmoslo a detalle: + +**Pesos del Modelo:** + +- 4 bytes por nรบmero de parรกmetros para entrenamiento en fp32 +- 6 bytes por nรบmero de parรกmetros para entrenamiento en precisiรณn mixta (mantiene un modelo en fp32 y uno en fp16 en memoria) + +**Estados del Optimizador:** + +- 8 bytes por nรบmero de parรกmetros para un AdamW normal (mantiene 2 estados) +- 2 bytes por nรบmero de parรกmetros para optimizadores de 8 bits como [bitsandbytes](https://github.com/TimDettmers/bitsandbytes) +- 4 bytes por nรบmero de parรกmetros para optimizadores como SGD con momentum (mantiene solo 1 estado) + +**Gradientes** + +- 4 bytes por nรบmero de parรกmetros para entrenamiento en fp32 o precisiรณn mixta (los gradientes siempre se mantienen en fp32) + +**Activaciones hacia Adelante** + +- El tamaรฑo depende de muchos factores, los principales siendo la longitud de la secuencia, el tamaรฑo oculto y el tamaรฑo de lote. + +Hay entradas y salidas que se pasan y se devuelven por las funciones hacia adelante y hacia atrรกs, y las activaciones hacia adelante (*forward activations*) guardadas para el cรกlculo del gradiente. + +**Memoria Temporal** + +Ademรกs, hay todas clases de variables temporales que se liberan una vez que se completa el cรกlculo, pero en el momento podrรญan requerir memoria adicional y podrรญan provocar un error de memoria insuficiente. Por lo tanto, al codificar es crucial pensar estratรฉgicamente sobre tales variables temporales y a veces liberarlas explรญcitamente tan pronto como ya no se necesitan. + +**Memoria Especรญfica de Funcionalidad** + +Entonces, su software podrรญa tener necesidades especiales de memoria. Por ejemplo, al generar texto mediante la bรบsqueda por haz, el software necesita mantener mรบltiples copias de las entradas y salidas. + +**Velocidad de Ejecuciรณn `forward` vs `backward`** + +Para convoluciones y capas lineales, hay 2x flops en la ejecuciรณn hacia atrรกs (`backward`) en comparaciรณn con la ejecuciรณn hacia adelante (`forward`), lo que generalmente se traduce en ~2x mรกs lento (a veces mรกs, porque los tamaรฑos en la ejecuciรณn hacia atrรกs tienden a ser mรกs complejos). Las activaciones suelen ser limitadas por ancho de banda, y es tรญpico que una activaciรณn tenga que leer mรกs datos en la ejecuciรณn hacia atrรกs que en la ejecuciรณn hacia adelante (por ejemplo, la activaciรณn hacia adelante lee una vez, escribe una vez, la activaciรณn hacia atrรกs lee dos veces, gradOutput y salida de la ejecuciรณn hacia adelante, y escribe una vez, gradInput). + +Como puedes ver, hay potencialmente unos pocos lugares donde podrรญamos ahorrar memoria de la GPU o acelerar operaciones. Ahora que entiendes quรฉ afecta la utilizaciรณn de la GPU y la velocidad de cรกlculo, consulta la pรกgina de documentaciรณn [Mรฉtodos y herramientas para entrenamiento eficiente en una sola GPU](https://huggingface.co/docs/transformers/perf_train_gpu_one) para aprender sobre tรฉcnicas de optimizaciรณn del rendimiento. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_sharing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_sharing.md new file mode 100644 index 0000000000000000000000000000000000000000..aef87578da31c3f69602d7f19ab4cb28c281e778 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/model_sharing.md @@ -0,0 +1,223 @@ + + +# Compartir un modelo + +Los รบltimos dos tutoriales mostraron cรณmo puedes realizar fine-tunning a un modelo con PyTorch, Keras y ๐Ÿค— Accelerate para configuraciones distribuidas. ยกEl siguiente paso es compartir tu modelo con la comunidad! En Hugging Face creemos en compartir abiertamente a todos el conocimiento y los recursos para democratizar la inteligencia artificial. En este sentido, te animamos a considerar compartir tu modelo con la comunidad, de esta forma ayudas a otros ahorrando tiempo y recursos. + +En este tutorial aprenderรกs dos mรฉtodos para compartir un modelo trained o fine-tuned en el [Model Hub](https://huggingface.co/models): + +- Mediante Cรณdigo, enviando (push) tus archivos al Hub. +- Con la interfaz Web, con Drag-and-drop de tus archivos al Hub. + + + + + +Para compartir un modelo con la comunidad necesitas una cuenta en [huggingface.co](https://huggingface.co/join). Tambiรฉn puedes unirte a una organizaciรณn existente o crear una nueva. + + + +## Caracterรญsticas de los repositorios + +Cada repositorio en el Model Hub se comporta como cualquier otro repositorio en GitHub. Nuestros repositorios ofrecen versioning, commit history, y la habilidad para visualizar diferencias. + +El versioning desarrollado dentro del Model Hub es basado en git y [git-lfs](https://git-lfs.github.com/). En otras palabras, puedes tratar un modelo como un repositorio, brindando un mejor control de acceso y escalabilidad. Version control permite *revisions*, un mรฉtodo para apuntar a una versiรณn especรญfica de un modelo utilizando un commit hash, tag o branch. + +Como resultado, puedes cargar una versiรณn especรญfica del modelo con el parรกmetro `revision`: + +```py +>>> model = AutoModel.from_pretrained( +... "julien-c/EsperBERTo-small", revision="4c77982" # tag name, or branch name, or commit hash +... ) +``` + +Los archivos son editados fรกcilmente dentro de un repositorio. Incluso puedes observar el commit history y las diferencias: + +![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png) + +## Configuraciรณn inicial + +Antes de compartir un modelo al Hub necesitarรกs tus credenciales de Hugging Face. Si tienes acceso a una terminal ejecuta el siguiente comando en el entorno virtual donde ๐Ÿค— Transformers estรฉ instalado. Esto guardarรก tu token de acceso dentro de tu carpeta cache de Hugging Face (~/.cache/ by default): + +```bash +hf auth login +``` + +Si usas un notebook como Jupyter o Colaboratory, asegรบrate de tener instalada la biblioteca [`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library). Esta biblioteca te permitirรก interactuar por cรณdigo con el Hub. + +```bash +pip install huggingface_hub +``` + +Luego usa `notebook_login` para iniciar sesiรณn al Hub, y sigue el link [aquรญ](https://huggingface.co/settings/token) para generar un token con el que iniciaremos sesiรณn: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Convertir un modelo para todos los Frameworks + +Para asegurarnos que tu modelo pueda ser usado por alguien que estรฉ trabajando con un framework diferente, te recomendamos convertir y subir tu modelo con checkpoints de pytorch y tensorflow. Aunque los usuarios aรบn son capaces de cargar su modelo desde un framework diferente, si se omite este paso serรก mรกs lento debido a que ๐Ÿค— Transformers necesitarรก convertir el checkpoint sobre-la-marcha. + +Convertir un checkpoint para otro framework es fรกcil. Asegรบrate tener Pytorch y TensorFlow instalado (Vรฉase [aquรญ](installation) para instrucciones de instalaciรณn), y luego encuentra el modelo especรญfico para tu tarea en el otro Framework. + +Por ejemplo, supongamos que has entrenado DistilBert para clasificaciรณn de secuencias en PyTorch y quieres convertirlo a su equivalente en TensorFlow. Cargas el equivalente en TensorFlow de tu modelo para tu tarea y especificas `from_pt=True` asรญ ๐Ÿค— Transformers convertirรก el Pytorch checkpoint a un TensorFlow Checkpoint: + +```py +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_pt=True) +``` + +Luego guardas tu nuevo modelo TensorFlow con su nuevo checkpoint: + +```py +>>> tf_model.save_pretrained("path/to/awesome-name-you-picked") +``` + +De manera similar, especificas `from_tf=True` para convertir un checkpoint de TensorFlow a Pytorch: + +```py +>>> pt_model = DistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_tf=True) +>>> pt_model.save_pretrained("path/to/awesome-name-you-picked") +``` + +Si algรบn modelo estรก disponible en Flax, tambiรฉn puedes convertir un checkpoint de Pytorch a Flax: + +```py +>>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( +... "path/to/awesome-name-you-picked", from_pt=True +... ) +``` + +## Compartir un modelo con `Trainer` + + + +Compartir un modelo al Hub es tan simple como aรฑadir un parรกmetro extra o un callback. Si recuerdas del tutorial de [fine-tuning tutorial](training), la clase [`TrainingArguments`] es donde especificas los Hiperparรกmetros y opciones de entrenamiento adicionales. Una de estas opciones incluye la habilidad de compartir un modelo directamente al Hub. Para ello configuras `push_to_hub=True` dentro de [`TrainingArguments`]: + +```py +>>> training_args = TrainingArguments(output_dir="my-awesome-model", push_to_hub=True) +``` + +A continuaciรณn, como usualmente, pasa tus argumentos de entrenamiento a [`Trainer`]: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +Luego que realizas fine-tune a tu modelo, llamas [`~transformers.Trainer.push_to_hub`] en [`Trainer`] para enviar el modelo al Hub!๐Ÿค— Transformers incluso aรฑadirรก automรกticamente los Hiperparรกmetros de entrenamiento, resultados de entrenamiento y versiones del Framework a tu model card! + +```py +>>> trainer.push_to_hub() +``` + +## Compartir un modelo con `PushToHubCallback` + +Los usuarios de TensorFlow pueden activar la misma funcionalidad con [`PushToHubCallback`]. En la funcion [`PushToHubCallback`], agrega: + +- Un directorio de salida para tu modelo. +- Un tokenizador. +- El `hub_model_id`, el cual es tu usuario Hub y el nombre del modelo. + +```py +>>> from transformers import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="./your_model_save_path", tokenizer=tokenizer, hub_model_id="your-username/my-awesome-model" +... ) +``` + +Agregamos el callback a [`fit`](https://keras.io/api/models/model_training_apis/), y ๐Ÿค— Transformers enviarรก el modelo entrenado al Hub: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback) +``` + +## Usando la funciรณn `push_to_hub` + +Puedes llamar la funciรณn `push_to_hub` directamente en tu modelo para subirlo al Hub. + +Especifica el nombre del modelo en `push_to_hub`: + +```py +>>> pt_model.push_to_hub("my-awesome-model") +``` + +Esto crearรก un repositorio bajo tu usuario con el nombre del modelo `my-awesome-model`. Ahora los usuarios pueden cargar tu modelo con la funciรณn `from_pretrained`: + +```py +>>> from transformers import AutoModel + +>>> model = AutoModel.from_pretrained("your_username/my-awesome-model") +``` + +Si perteneces a una organizaciรณn y quieres compartir tu modelo bajo el nombre de la organizaciรณn, aรฑade el parรกmetro `organization`: + +```py +>>> pt_model.push_to_hub("my-awesome-model", organization="my-awesome-org") +``` + +La funciรณn `push_to_hub` tambiรฉn puede ser usada para aรฑadir archivos al repositorio del modelo. Por ejemplo, aรฑade un tokenizador al repositorio: + +```py +>>> tokenizer.push_to_hub("my-awesome-model") +``` + +O quizรกs te gustarรญa aรฑadir la versiรณn de TensorFlow de tu modelo fine-tuned en Pytorch: + +```py +>>> tf_model.push_to_hub("my-awesome-model") +``` + +Ahora, cuando navegues a tu perfil en Hugging Face, deberรญas observar el repositorio de tu modelo creado recientemente. Si das click en el tab **Files** observarรกs todos los archivos que has subido al repositorio. + +Para mรกs detalles sobre cรณmo crear y subir archivos al repositorio, consulta la [documentaciรณn del Hub](https://huggingface.co/docs/hub/how-to-upstream). + +## Compartir con la interfaz web + +Los usuarios que prefieran un enfoque no-code tienen la opciรณn de cargar su modelo a travรฉs de la interfaz grรกfica del Hub. Visita la pรกgina [huggingface.co/new](https://huggingface.co/new) para crear un nuevo repositorio: + +![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png) + +Desde aquรญ, aรฑade informaciรณn acerca del modelo: + +- Selecciona el **owner** (la persona propietaria) del repositorio. Puedes ser tรบ o cualquier organizaciรณn a la que pertenezcas. +- Escoge un nombre para tu modelo. Tambiรฉn serรก el nombre del repositorio. +- Elige si tu modelo es pรบblico o privado. +- Especifica la licencia que usarรก tu modelo. + +Ahora puedes hacer click en el tab **Files** y luego en el botรณn **Add file** para subir un nuevo archivo a tu repositorio. Luego arrastra y suelta un archivo a subir y le aรฑades un mensaje al commit. + +![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png) + +## Aรฑadiendo una tarjeta de modelo + +Para asegurarnos que los usuarios entiendan las capacidades de tu modelo, sus limitaciones, posibles sesgos y consideraciones รฉticas, por favor aรฑade una tarjeta (como una tarjeta de presentaciรณn) al repositorio del modelo. La tarjeta de modelo es definida en el archivo `README.md`. Puedes agregar una de la siguiente manera: + +* Elaborando y subiendo manualmente el archivo`README.md`. +* Dando click en el botรณn **Edit model card** dentro del repositorio. + +Toma un momento para ver la [tarjeta de modelo](https://huggingface.co/distilbert/distilbert-base-uncased) de DistilBert para que tengas un buen ejemplo del tipo de informaciรณn que deberรญa incluir. Consulta [la documentaciรณn](https://huggingface.co/docs/hub/models-cards) para mรกs detalles acerca de otras opciones que puedes controlar dentro del archivo `README.md` como la huella de carbono del modelo o ejemplos de widgets. Consulta la documentaciรณn [aquรญ](https://huggingface.co/docs/hub/models-cards). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/multilingual.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/multilingual.md new file mode 100644 index 0000000000000000000000000000000000000000..d49d54f196d54e160e02c93b9322255e3e0509bc --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/multilingual.md @@ -0,0 +1,179 @@ + + +# Modelos multilingรผes para inferencia + +[[open-in-colab]] + +Existen varios modelos multilingรผes en ๐Ÿค— Transformers y su uso para inferencia difiere de los modelos monolingรผes. Sin embargo, no *todos* los usos de los modelos multilingรผes son diferentes. Algunos modelos, como [google-bert/bert-base-multilingual-uncased](https://huggingface.co/google-bert/bert-base-multilingual-uncased), pueden utilizarse igual que un modelo monolingรผe. Esta guรญa te enseรฑarรก cรณmo utilizar modelos multilingรผes cuyo uso difiere en la inferencia. + +## XLM + +XLM tiene diez checkpoints diferentes de los cuales solo uno es monolingรผe. Los nueve checkpoints restantes del modelo pueden dividirse en dos categorรญas: los checkpoints que utilizan language embeddings y los que no. + +### XLM con language embeddings + +Los siguientes modelos XLM usan language embeddings para especificar el lenguaje utilizado en la inferencia: + +- `FacebookAI/xlm-mlm-ende-1024` (Masked language modeling, English-German) +- `FacebookAI/xlm-mlm-enfr-1024` (Masked language modeling, English-French) +- `FacebookAI/xlm-mlm-enro-1024` (Masked language modeling, English-Romanian) +- `FacebookAI/xlm-mlm-xnli15-1024` (Masked language modeling, XNLI languages) +- `FacebookAI/xlm-mlm-tlm-xnli15-1024` (Masked language modeling + translation, XNLI languages) +- `FacebookAI/xlm-clm-enfr-1024` (Causal language modeling, English-French) +- `FacebookAI/xlm-clm-ende-1024` (Causal language modeling, English-German) + +Los language embeddings son representados como un tensor de la mismas dimensiones que los `input_ids` pasados al modelo. Los valores de estos tensores dependen del idioma utilizado y se identifican mediante los atributos `lang2id` y `id2lang` del tokenizador. + +En este ejemplo, carga el checkpoint `FacebookAI/xlm-clm-enfr-1024` (Causal language modeling, English-French): + +```py +>>> import torch +>>> from transformers import XLMTokenizer, XLMWithLMHeadModel + +>>> tokenizer = XLMTokenizer.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +>>> model = XLMWithLMHeadModel.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +``` + +El atributo `lang2id` del tokenizador muestra los idiomas de este modelo y sus ids: + +```py +>>> print(tokenizer.lang2id) +{'en': 0, 'fr': 1} +``` + +A continuaciรณn, crea un input de ejemplo: + +```py +>>> input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) # batch size of 1 +``` + +Establece el id del idioma, por ejemplo `"en"`, y utilรญzalo para definir el language embedding. El language embedding es un tensor lleno de `0` ya que es el id del idioma para inglรฉs. Este tensor debe ser del mismo tamaรฑo que `input_ids`. + +```py +>>> language_id = tokenizer.lang2id["en"] # 0 +>>> langs = torch.tensor([language_id] * input_ids.shape[1]) # torch.tensor([0, 0, 0, ..., 0]) + +>>> # We reshape it to be of size (batch_size, sequence_length) +>>> langs = langs.view(1, -1) # is now of shape [1, sequence_length] (we have a batch size of 1) +``` + +Ahora puedes pasar los `input_ids` y el language embedding al modelo: + +```py +>>> outputs = model(input_ids, langs=langs) +``` + +El script [run_generation.py](https://github.com/huggingface/transformers/tree/master/examples/pytorch/text-generation/run_generation.py) puede generar texto con language embeddings utilizando los checkpoints `xlm-clm`. + +### XLM sin language embeddings + +Los siguientes modelos XLM no requieren language embeddings durante la inferencia: + +- `FacebookAI/xlm-mlm-17-1280` (modelado de lenguaje enmascarado, 17 idiomas) +- `FacebookAI/xlm-mlm-100-1280` (modelado de lenguaje enmascarado, 100 idiomas) + +Estos modelos se utilizan para representaciones genรฉricas de frases a diferencia de los anteriores checkpoints XLM. + +## BERT + +Los siguientes modelos de BERT pueden utilizarse para tareas multilingรผes: + +- `google-bert/bert-base-multilingual-uncased` (modelado de lenguaje enmascarado + predicciรณn de la siguiente oraciรณn, 102 idiomas) +- `google-bert/bert-base-multilingual-cased` (modelado de lenguaje enmascarado + predicciรณn de la siguiente oraciรณn, 104 idiomas) + +Estos modelos no requieren language embeddings durante la inferencia. Deben identificar la lengua a partir del +contexto e inferir en consecuencia. + +## XLM-RoBERTa + +Los siguientes modelos de XLM-RoBERTa pueden utilizarse para tareas multilingรผes: + +- `FacebookAI/xlm-roberta-base` (modelado de lenguaje enmascarado, 100 idiomas) +- `FacebookAI/xlm-roberta-large` (Modelado de lenguaje enmascarado, 100 idiomas) + +XLM-RoBERTa se entrenรณ con 2,5 TB de datos CommonCrawl reciรฉn creados y depurados en 100 idiomas. Proporciona fuertes ventajas sobre los modelos multilingรผes publicados anteriormente como mBERT o XLM en tareas posteriores como la clasificaciรณn, el etiquetado de secuencias y la respuesta a preguntas. + +## M2M100 + +Los siguientes modelos de M2M100 pueden utilizarse para traducciรณn multilingรผe: + +- `facebook/m2m100_418M` (traducciรณn) +- `facebook/m2m100_1.2B` (traducciรณn) + +En este ejemplo, carga el checkpoint `facebook/m2m100_418M` para traducir del chino al inglรฉs. Puedes establecer el idioma de origen en el tokenizador: + +```py +>>> from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> chinese_text = "ไธ่ฆๆ’ๆ‰‹ๅทซๅธซ็š„ไบ‹ๅ‹™, ๅ› ็‚บไป–ๅ€‘ๆ˜ฏๅพฎๅฆ™็š„, ๅพˆๅฟซๅฐฑๆœƒ็™ผๆ€’." + +>>> tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="zh") +>>> model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M") +``` + +Tokeniza el texto: + +```py +>>> encoded_zh = tokenizer(chinese_text, return_tensors="pt") +``` + +M2M100 fuerza el id del idioma de destino como el primer token generado para traducir al idioma de destino.. Establece el `forced_bos_token_id` a `en` en el mรฉtodo `generate` para traducir al inglรฉs: + +```py +>>> generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +'Do not interfere with the matters of the witches, because they are delicate and will soon be angry.' +``` + +## MBart + +Los siguientes modelos de MBart pueden utilizarse para traducciรณn multilingรผe: + +- `facebook/mbart-large-50-one-to-many-mmt` (traducciรณn automรกtica multilingรผe de uno a muchos, 50 idiomas) +- `facebook/mbart-large-50-many-to-many-mmt` (traducciรณn automรกtica multilingรผe de muchos a muchos, 50 idiomas) +- `facebook/mbart-large-50-many-to-one-mmt` (traducciรณn automรกtica multilingรผe muchos a uno, 50 idiomas) +- `facebook/mbart-large-50` (traducciรณn multilingรผe, 50 idiomas) +- `facebook/mbart-large-cc25` + +En este ejemplo, carga el checkpoint `facebook/mbart-large-50-many-to-many-mmt` para traducir del finlandรฉs al inglรฉs. Puedes establecer el idioma de origen en el tokenizador: + +```py +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> fi_text = "ร„lรค sekaannu velhojen asioihin, sillรค ne ovat hienovaraisia ja nopeasti vihaisia." + +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="fi_FI") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/mbart-large-50-many-to-many-mmt") +``` + +Tokeniza el texto: + +```py +>>> encoded_en = tokenizer(en_text, return_tensors="pt") +``` + +MBart fuerza el id del idioma de destino como el primer token generado para traducirlo. Establece el `forced_bos_token_id` a `en` en el mรฉtodo `generate` para traducir al inglรฉs: + +```py +>>> generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id("en_XX")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +"Don't interfere with the wizard's affairs, because they are subtle, will soon get angry." +``` + +Si estรกs usando el checkpoint `facebook/mbart-large-50-many-to-one-mmt` no necesitas forzar el id del idioma de destino como el primer token generado, de lo contrario el uso es el mismo. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pad_truncation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pad_truncation.md new file mode 100644 index 0000000000000000000000000000000000000000..6a31a69103502fa2736b870b1fa5bc61e8556657 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pad_truncation.md @@ -0,0 +1,69 @@ + + +# Relleno y truncamiento + +Las entradas agrupadas por lotes (batched) suelen tener longitudes diferentes, por lo que no se pueden convertir en tensores de tamaรฑo fijo. El relleno (tambiรฉn conocido como "Padding") y el truncamiento (conocido como "Truncation") son estrategias para abordar este problema y crear tensores rectangulares a partir de lotes de longitudes variables. El relleno agrega un **padding token** especial para garantizar que las secuencias mรกs cortas tengan la misma longitud que la secuencia mรกs larga en un lote o la longitud mรกxima aceptada por el modelo. El truncamiento funciona en la otra direcciรณn al truncar secuencias largas. + +En la mayorรญa de los casos, es bastante eficaz rellenar el lote hasta la longitud de la secuencia mรกs larga y truncar hasta la longitud mรกxima que un modelo puede aceptar. Sin embargo, la API admite mรกs estrategias si las necesitas. Los tres argumentos que necesitas son: `padding`, `truncation` y `max_length`. + +El argumento `padding` controla el relleno. Puede ser un booleano o una cadena: + + - `True` o `'longest'`: rellena hasta la longitud de la secuencia mรกs larga en el lote (no se aplica relleno si solo proporcionas una รบnica secuencia). + - `'max_length'`: rellena hasta una longitud especificada por el argumento `max_length` o la longitud mรกxima aceptada + por el modelo si no se proporciona `max_length` (`max_length=None`). El relleno se aplicarรก incluso si solo proporcionas una รบnica secuencia. + - `False` o `'do_not_pad'`: no se aplica relleno. Este es el comportamiento predeterminado. + +El argumento `truncation` controla el truncamiento. Puede ser un booleano o una cadena: + + - `True` o `'longest_first'`: trunca hasta una longitud mรกxima especificada por el argumento `max_length` o + la longitud mรกxima aceptada por el modelo si no se proporciona `max_length` (`max_length=None`). Esto + truncarรก token por token, eliminando un token de la secuencia mรกs larga en el par hasta alcanzar la longitud adecuada. + - `'only_second'`: trunca hasta una longitud mรกxima especificada por el argumento `max_length` o la longitud mรกxima + aceptada por el modelo si no se proporciona `max_length` (`max_length=None`). Esto solo truncarรก + la segunda oraciรณn de un par si se proporciona un par de secuencias (o un lote de pares de secuencias). + - `'only_first'`: trunca hasta una longitud mรกxima especificada por el argumento `max_length` o la longitud mรกxima + aceptada por el modelo si no se proporciona `max_length` (`max_length=None`). Esto solo truncarรก + la primera oraciรณn de un par si se proporciona un par de secuencias (o un lote de pares de secuencias). + - `False` o `'do_not_truncate'`: no se aplica truncamiento. Este es el comportamiento predeterminado. + +El argumento `max_length` controla la longitud del relleno y del truncamiento. Puede ser un nรบmero entero o `None`, en cuyo caso se establecerรก automรกticamente en la longitud mรกxima que el modelo puede aceptar. Si el modelo no tiene una longitud mรกxima de entrada especรญfica, se desactiva el truncamiento o el relleno hasta `max_length`. + +La siguiente tabla resume la forma recomendada de configurar el relleno y el truncamiento. Si usas pares de secuencias de entrada en alguno de los siguientes ejemplos, puedes reemplazar `truncation=True` por una `ESTRATEGIA` seleccionada en +`['only_first', 'only_second', 'longest_first']`, es decir, `truncation='only_second'` o `truncation='longest_first'` para controlar cรณmo se truncan ambas secuencias en el par, como se detallรณ anteriormente. + +| Truncation | Padding | Instrucciรณn | +|-----------------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------| +| sin truncamiento | sin relleno | `tokenizer(batch_sentences)` | +| | relleno hasta la longitud mรกxima del lote | `tokenizer(batch_sentences, padding=True)` o | +| | | `tokenizer(batch_sentences, padding='longest')` | +| | relleno hasta la longitud mรกxima del modelo | `tokenizer(batch_sentences, padding='max_length')` | +| | relleno hasta una longitud especรญfica | `tokenizer(batch_sentences, padding='max_length', max_length=42)` | +| | relleno hasta un mรบltiplo de un valor | `tokenizer(batch_sentences, padding=True, pad_to_multiple_of=8)` | +| truncamiento hasta la longitud mรกxima del modelo | sin relleno | `tokenizer(batch_sentences, truncation=True)` o | +| | | `tokenizer(batch_sentences, truncation=ESTRATEGIA)` | +| | relleno hasta la longitud mรกxima del lote | `tokenizer(batch_sentences, padding=True, truncation=True)` o | +| | | `tokenizer(batch_sentences, padding=True, truncation=ESTRATEGIA)` | +| | relleno hasta la longitud mรกxima del modelo | `tokenizer(batch_sentences, padding='max_length', truncation=True)` o | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=ESTRATEGIA)` | +| | relleno hasta una longitud especรญfica | No es posible | +| truncamiento hasta una longitud especรญfica | sin relleno | `tokenizer(batch_sentences, truncation=True, max_length=42)` o | +| | | `tokenizer(batch_sentences, truncation=ESTRATEGIA, max_length=42)` | +| | relleno hasta la longitud mรกxima del lote | `tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` o | +| | | `tokenizer(batch_sentences, padding=True, truncation=ESTRATEGIA, max_length=42)` | +| | relleno hasta la longitud mรกxima del modelo | No es posible | +| | relleno hasta una longitud especรญfica | `tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` o | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=ESTRATEGIA, max_length=42)` | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/performance.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/performance.md new file mode 100644 index 0000000000000000000000000000000000000000..4665c2961b3f23ddd22d129776c5f6bd91e80e5d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/performance.md @@ -0,0 +1,61 @@ + + +# Rendimiento y Escalabilidad + +Entrenar modelos grandes de transformadores y desplegarlos en producciรณn presenta varios desafรญos. Durante el entrenamiento, el modelo puede requerir mรกs memoria de GPU de la disponible o mostrar una velocidad de entrenamiento lenta. En la fase de implementaciรณn, el modelo puede tener dificultades para manejar el rendimiento necesario en un entorno de producciรณn. + +Esta documentaciรณn tiene como objetivo ayudarte a superar estos desafรญos y encontrar la configuraciรณn รณptima para tu caso de uso. Las guรญas estรกn divididas en secciones de entrenamiento e inferencia, ya que cada una presenta diferentes desafรญos y soluciones. Dentro de cada secciรณn, encontrarรกs guรญas separadas para diferentes configuraciones de hardware, como GPU รบnica vs. multi-GPU para el entrenamiento o CPU vs. GPU para la inferencia. + +Utiliza este documento como punto de partida para navegar hacia los mรฉtodos que se ajusten a tu escenario. + +## Entrenamiento + +Entrenar modelos grandes de transformadores de manera eficiente requiere un acelerador como una GPU o TPU. El caso mรกs comรบn es cuando tienes una GPU รบnica. Los mรฉtodos que puedes aplicar para mejorar la eficiencia de entrenamiento en una GPU รบnica tambiรฉn se aplican a otras configuraciones, como mรบltiples GPU. Sin embargo, tambiรฉn existen tรฉcnicas especรญficas para entrenamiento con mรบltiples GPU o CPU, las cuales cubrimos en secciones separadas. + +* [Mรฉtodos y herramientas para un entrenamiento eficiente en una sola GPU](https://huggingface.co/docs/transformers/perf_train_gpu_one): comienza aquรญ para aprender enfoques comunes que pueden ayudar a optimizar la utilizaciรณn de memoria de la GPU, acelerar el entrenamiento o ambas cosas. +* [Secciรณn de entrenamiento con varias GPU](https://huggingface.co/docs/transformers/perf_train_gpu_many): explora esta secciรณn para conocer mรฉtodos de optimizaciรณn adicionales que se aplican a configuraciones con varias GPU, como paralelismo de datos, tensores y canalizaciones. +* [Secciรณn de entrenamiento en CPU](https://huggingface.co/docs/transformers/perf_train_cpu): aprende sobre entrenamiento de precisiรณn mixta en CPU. +* [Entrenamiento eficiente en mรบltiples CPUs](https://huggingface.co/docs/transformers/perf_train_cpu_many): aprende sobre el entrenamiento distribuido en CPU. +* [Entrenamiento en TPU con TensorFlow](https://huggingface.co/docs/transformers/perf_train_tpu_tf): si eres nuevo en TPUs, consulta esta secciรณn para obtener una introducciรณn basada en opiniones sobre el entrenamiento en TPUs y el uso de XLA. +* [Hardware personalizado para el entrenamiento](https://huggingface.co/docs/transformers/perf_hardware): encuentra consejos y trucos al construir tu propia plataforma de aprendizaje profundo. +* [Bรบsqueda de hiperparรกmetros utilizando la API del Entrenador](https://huggingface.co/docs/transformers/hpo_train) + +## Inferencia + +Realizar inferencias eficientes con modelos grandes en un entorno de producciรณn puede ser tan desafiante como entrenarlos. En las siguientes secciones, describimos los pasos para ejecutar inferencias en CPU y configuraciones con GPU รบnica/mรบltiple. + +* [Inferencia en una sola CPU](https://huggingface.co/docs/transformers/perf_infer_cpu) +* [Inferencia en una sola GPU](https://huggingface.co/docs/transformers/perf_infer_gpu_one) +* [Inferencia con mรบltiples GPU](https://huggingface.co/docs/transformers/perf_infer_gpu_one) +* [Integraciรณn de XLA para modelos de TensorFlow](https://huggingface.co/docs/transformers/tf_xla) + +## Entrenamiento e Inferencia + +Aquรญ encontrarรกs tรฉcnicas, consejos y trucos que aplican tanto si estรกs entrenando un modelo como si estรกs ejecutando inferencias con รฉl. + +* [Instanciar un modelo grande](https://huggingface.co/docs/transformers/big_models) +* [Soluciรณn de problemas de rendimiento](https://huggingface.co/docs/transformers/debugging) + +## Contribuir + +Este documento estรก lejos de estar completo y aรบn se deben agregar muchas cosas, asรญ que si tienes adiciones o correcciones que hacer, no dudes en abrir un PR. Si no estรกs seguro, inicia un Issue y podemos discutir los detalles allรญ. + +Cuando hagas contribuciones que indiquen que A es mejor que B, intenta incluir un benchmark reproducible y/o un enlace a la fuente de esa informaciรณn (a menos que provenga directamente de ti). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/perplexity.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/perplexity.md new file mode 100644 index 0000000000000000000000000000000000000000..f07dc663f5524e7af98728ea3edde12466731a2d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/perplexity.md @@ -0,0 +1,116 @@ + + +# Perplejidad de los modelos de longitud fija + +[[open-in-colab]] + +La perplejidad, perplexity en inglรฉs (PPL), es una de las mรฉtricas mรกs comunes para evaluar modelos de lenguaje. Antes de sumergirnos, debemos tener en cuenta que esta mรฉtrica se aplica especรญficamente a modelos de lenguaje clรกsicos (a veces llamados modelos autorregresivos o causales) y no estรก bien definida para modelos de lenguaje enmascarados como BERT (ver [resumen del modelo](model_summary)). + +La perplejidad se define como la media negativa exponenciada del log-likelihood de una secuencia. Si tenemos una secuencia tokenizada \\(X = (x_0, x_1, \dots, x_t)\\), entonces la perplejidad de \\(X\\) es, + +$$\text{PPL}(X) = \exp \left\{ {-\frac{1}{t}\sum_i^t \log p_\theta (x_i|x_{ + +Sin embargo, al trabajar con modelos aproximados, generalmente tenemos una restricciรณn en la cantidad de tokens que el modelo puede procesar. La versiรณn mรกs grande de [GPT-2](model_doc/gpt2), por ejemplo, tiene una longitud fija de 1024 tokens, por lo que no podemos calcular \\(p_\theta(x_t|x_{ + +Esto es rรกpido de calcular, ya que la perplejidad de cada segmento se puede calcular en un solo pase hacia adelante, pero sirve como una aproximaciรณn pobre de la perplejidad completamente factorizada y generalmente darรก como resultado una PPL mรกs alta (peor) porque el modelo tendrรก menos contexto en la mayorรญa de los pasos de predicciรณn. + +En cambio, la PPL de modelos de longitud fija deberรญa evaluarse con una estrategia de ventana deslizante. Esto implica deslizar repetidamente la ventana de contexto para que el modelo tenga mรกs contexto al hacer cada predicciรณn. + +Sliding window PPL taking advantage of all available context + +Esta es una aproximaciรณn mรกs cercana a la verdadera descomposiciรณn de la probabilidad de la secuencia y generalmente darรก como resultado una puntuaciรณn mรกs favorable. La desventaja es que requiere un pase hacia adelante separado para cada token en el corpus. Un buen compromiso prรกctico es emplear una ventana deslizante estratificada, moviendo el contexto con pasos mรกs grandes en lugar de deslizarse de 1 token a la vez. Esto permite que la computaciรณn avance mucho mรกs rรกpido, mientras le da al modelo un contexto amplio para hacer +predicciones en cada paso. + +## Ejemplo: Cรกlculo de la perplejidad con GPT-2 en ๐Ÿค— Transformers + +Demostremos este proceso con GPT-2. + +```python +from transformers import GPT2LMHeadModel, GPT2TokenizerFast + +device = "cuda" +model_id = "openai-community/gpt2-large" +model = GPT2LMHeadModel.from_pretrained(model_id).to(device) +tokenizer = GPT2TokenizerFast.from_pretrained(model_id) +``` + +Carguemos el conjunto de datos WikiText-2 y evaluemos la perplejidad utilizando algunas estrategias de ventana deslizante diferentes. Dado que este conjunto de datos es pequeรฑo y solo estamos realizando un pase hacia adelante sobre el conjunto, podemos cargar y codificar todo el conjunto de datos en la memoria. + +```python +from datasets import load_dataset + +test = load_dataset("wikitext", "wikitext-2-raw-v1", split="test") +encodings = tokenizer("\n\n".join(test["text"]), return_tensors="pt") +``` + +Con ๐Ÿค— Transformers, simplemente podemos pasar los `input_ids` como las `labels` a nuestro modelo, y la media negativa del log-likelihood para cada token se devuelve como la pรฉrdida. Sin embargo, con nuestro enfoque de ventana deslizante, hay superposiciรณn en los tokens que pasamos al modelo en cada iteraciรณn. No queremos que el log-likelihood de los tokens que estamos tratando solo como contexto se incluya en nuestra pรฉrdida, por lo que podemos establecer estos objetivos en `-100` para que se ignoren. El siguiente es un ejemplo de cรณmo podrรญamos hacer esto con un paso de `512`. Esto significa que el modelo tendrรก al menos `512` tokens como contexto al calcular el log-likelihood condicional de cualquier token (siempre que haya `512` tokens precedentes disponibles para condicionar). + +```python +import torch +from tqdm import tqdm + +max_length = model.config.n_positions +stride = 512 +seq_len = encodings.input_ids.size(1) + +nlls = [] +prev_end_loc = 0 +for begin_loc in tqdm(range(0, seq_len, stride)): + end_loc = min(begin_loc + max_length, seq_len) + trg_len = end_loc - prev_end_loc # puede ser diferente del paso en el รบltimo bucle + input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device) + target_ids = input_ids.clone() + target_ids[:, :-trg_len] = -100 + + with torch.no_grad(): + outputs = model(input_ids, labels=target_ids) + + # la pรฉrdida se calcula utilizando CrossEntropyLoss, que promedia las etiquetas vรกlidas + # N.B. el modelo solo calcula la pรฉrdida sobre trg_len - 1 etiquetas, porque desplaza las etiqueta internamente + # a la izquierda por 1. + neg_log_likelihood = outputs.loss + + nlls.append(neg_log_likelihood) + + prev_end_loc = end_loc + if end_loc == seq_len: + break + +ppl = torch.exp(torch.stack(nlls).mean()) +``` + +Ejecuta esto con la longitud de paso igual a la longitud mรกxima de entrada es equivalente a la estrategia sub รณptima, +sin ventana deslizante, que discutimos anteriormente. Cuanto menor sea el paso, mรกs contexto tendrรก el modelo para +realizar cada predicciรณn y, por lo general, mejor serรก la perplejidad informada. + +Cuando ejecutamos lo anterior con `stride = 1024`, es decir, sin superposiciรณn, la PPL resultante es `19.44`, que es +aproximadamente la misma que la `19.93` informada en el artรญculo de GPT-2. Al utilizar `stride = 512` y, por lo tanto, +emplear nuestra estrategia de ventana deslizante, esto disminuye a `16.45`. Esto no solo es una puntuaciรณn mรกs favorable, sino que se calcula de una manera mรกs cercana a la verdadera descomposiciรณn autorregresiva de la probabilidad de una secuencia. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/philosophy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/philosophy.md new file mode 100644 index 0000000000000000000000000000000000000000..4054ac0ae507160dbe7b42023ab58910f875e847 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/philosophy.md @@ -0,0 +1,79 @@ + + +# Filosofรญa + +๐Ÿค— Transformers es una biblioteca construida para: + +- Los investigadores y educadores de NLP que busquen usar/estudiar/extender modelos transformers a gran escala +- Profesionales que quieren optimizar esos modelos y/o ponerlos en producciรณn +- Ingenieros que solo quieren descargar un modelo preentrenado y usarlo para resolver una tarea NLP dada. + +La biblioteca fue diseรฑada con dos fuertes objetivos en mente: + +- Que sea tan fรกcil y rรกpida de utilizar como sea posible: + + - Hemos limitado enormemente el nรบmero de abstracciones que el usuario tiene que aprender. De hecho, no hay casi abstracciones, + solo tres clases estรกndar necesarias para usar cada modelo: [configuration](main_classes/configuration), + [models](main_classes/model) y [tokenizer](main_classes/tokenizer). + - Todas estas clases pueden ser inicializadas de forma simple y unificada a partir de ejemplos pre-entrenados mediante el uso de un mรฉtodo + `from_pretrained()` comรบn de solicitud que se encargarรก de descargar (si es necesario), almacenar y cargar la solicitud de clase relacionada y datos asociados + (configurations' hyper-parameters, tokenizers' vocabulary, and models' weights) a partir de un control pre-entrenado proporcionado en + [Hugging Face Hub](https://huggingface.co/models) o de tu propio control guardado. + - Por encima de esas tres clases estรกndar, la biblioteca proporciona dos APIs: [`pipeline`] para usar rรกpidamente un modelo (junto a su configuracion y tokenizer asociados) + sobre una tarea dada, y [`Trainer`]/`Keras.fit` para entrenar u optimizar de forma rรกpida un modelo dado. + - Como consecuencia, esta biblioteca NO es una caja de herramientas modular de bloques individuales para redes neuronales. Si quieres extender/construir sobre la biblioteca, + usa simplemente los mรณdulos regulares de Python/PyTorch/TensorFlow/Keras y emplea las clases estรกndar de la biblioteca como punto de partida para reutilizar funcionalidades + tales como abrir/guardar modelo. + +- Proporciona modelos modernos con rendimientos lo mรกs parecido posible a los modelos originales: + + - Proporcionamos al menos un ejemplo para cada arquitectura que reproduce un resultado proporcionado por los autores de dicha arquitectura. + - El cรณdigo normalmente es parecido al cรณdigo base original, lo cual significa que algรบn cรณdigo Pytorch puede no ser tan + *pytorchic* como podrรญa ser por haber sido convertido a cรณdigo TensorFlow, y viceversa. + +Unos cuantos objetivos adicionales: + +- Exponer las caracterรญsticas internas de los modelos de la forma mรกs coherente posible: + + - Damos acceso, mediante una sola API, a todos los estados ocultos y pesos de atenciรณn. + - Tokenizer y el modelo de API base estรกn estandarizados para cambiar fรกcilmente entre modelos. + +- Incorporar una selecciรณn subjetiva de herramientas de gran potencial para la optimizaciรณn/investigaciรณn de estos modelos: + + - Una forma sencilla/coherente de aรฑadir nuevos tokens al vocabulario e incrustraciones (embeddings, en inglรฉs) para optimizaciรณn. + - Formas sencillas de camuflar y reducir "transformer heads". + +- Cambiar fรกcilmente entre PyTorch y TensorFlow 2.0, permitiendo el entrenamiento usando un marco y la inferencia usando otro. + +## Conceptos principales + +La biblioteca estรก construida alrededor de tres tipos de clases para cada modelo: + +- **Model classes** como [`BertModel`], que consisten en mรกs de 30 modelos PyTorch ([torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)) o modelos Keras ([tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model)) que funcionan con pesos pre-entrenados proporcionados en la + biblioteca. +- **Configuration classes** como [`BertConfig`], que almacena todos los parรกmetros necesarios para construir un modelo. + No siempre tienes que generarla tu. En particular, si estas usando un modelo pre-entrenado sin ninguna modificaciรณn, + la creaciรณn del modelo se encargarรก automรกticamente de generar la configuraciรณn (que es parte del modelo). +- **Tokenizer classes** como [`BertTokenizer`], que almacena el vocabulario para cada modelo y proporciona mรฉtodos para + codificar/decodificar strings en una lista de รญndices de "token embeddings" para ser empleados en un modelo. + +Todas estas clases pueden ser generadas a partir de ejemplos pre-entrenados, y guardados localmente usando dos mรฉtodos: + +- `from_pretrained()` permite generar un modelo/configuraciรณn/tokenizer a partir de una versiรณn pre-entrenada proporcionada ya sea por + la propia biblioteca (los modelos compatibles se pueden encontrar en [Model Hub](https://huggingface.co/models)) o + guardados localmente (o en un servidor) por el usuario. +- `save_pretrained()` permite guardar un modelo/configuraciรณn/tokenizer localmente, de forma que puede ser empleado de nuevo usando + `from_pretrained()`. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..3fbcce1447612cd5e07dbdfe6065da88f08f522d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_tutorial.md @@ -0,0 +1,324 @@ + + +# Pipelines para inferencia + +Un [`pipeline`] simplifica el uso de cualquier modelo del [Hub](https://huggingface.co/models) para la inferencia en una variedad de tareas como la generaciรณn de texto, la segmentaciรณn de imรกgenes y la clasificaciรณn de audio. Incluso si no tienes experiencia con una modalidad especรญfica o no comprendes el cรณdigo que alimenta los modelos, ยกaรบn puedes usarlos con el [`pipeline`]! Este tutorial te enseรฑarรก a: + +* Utilizar un [`pipeline`] para inferencia. +* Utilizar un tokenizador o modelo especรญfico. +* Utilizar un [`pipeline`] para tareas de audio y visiรณn. + + + +Echa un vistazo a la documentaciรณn de [`pipeline`] para obtener una lista completa de tareas admitidas. + + + +## Uso del pipeline + +Si bien cada tarea tiene un [`pipeline`] asociado, es mรกs sencillo usar la abstracciรณn general [`pipeline`] que contiene todos los pipelines de tareas especรญficas. El [`pipeline`] carga automรกticamente un modelo predeterminado y un tokenizador con capacidad de inferencia para tu tarea. Veamos el ejemplo de usar un [`pipeline`] para reconocimiento automรกtico del habla (ASR), o texto a voz. + +1. Comienza creando un [`pipeline`] y especรญfica una tarea de inferencia: + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition") +``` + +2. Pasa tu entrada a la [`pipeline`]. En el caso del reconocimiento del habla, esto es un archivo de entrada de audio: + +```py +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'} +``` + +ยฟNo es el resultado que tenรญas en mente? Echa un vistazo a algunos de los [modelos de reconocimiento automรกtico del habla mรกs descargados](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=trending) +en el Hub para ver si puedes obtener una mejor transcripciรณn. + +Intentemos con el modelo [Whisper large-v2](https://huggingface.co/openai/whisper-large) de OpenAI. Whisper se lanzรณ +2 aรฑos despuรฉs que Wav2Vec2, y se entrenรณ con cerca de 10 veces mรกs datos. Como tal, supera a Wav2Vec2 en la mayorรญa de las pruebas +downstream. Tambiรฉn tiene el beneficio adicional de predecir puntuaciรณn y mayรบsculas, ninguno de los cuales es posible con +Wav2Vec2. + +Vamos a probarlo aquรญ para ver cรณmo se desempeรฑa: + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +ยกAhora este resultado parece mรกs preciso! Para una comparaciรณn detallada de Wav2Vec2 vs Whisper, consulta el [Curso de Transformers de Audio](https://huggingface.co/learn/audio-course/chapter5/asr_models). +Realmente te animamos a que eches un vistazo al Hub para modelos en diferentes idiomas, modelos especializados en tu campo, y mรกs. +Puedes comparar directamente los resultados de los modelos desde tu navegador en el Hub para ver si se adapta o +maneja casos de borde mejor que otros. +Y si no encuentras un modelo para tu caso de uso, siempre puedes empezar a [entrenar](training) el tuyo propio. + +Si tienes varias entradas, puedes pasar tu entrada como una lista: + +```py +transcriber( + [ + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", + ] +) +``` + +Los pipelines son ideales para la experimentaciรณn, ya que cambiar de un modelo a otro es trivial; sin embargo, hay algunas formas de optimizarlas para cargas de trabajo mรกs grandes que la experimentaciรณn. Consulta las siguientes guรญas que profundizan en iterar sobre conjuntos de datos completos o utilizar pipelines en un servidor web: +de la documentaciรณn: + +* [Uso de pipelines en un conjunto de datos](#uso-de-pipelines-en-un-conjunto-de-datos) +* [Uso de pipelines para un servidor web](./pipeline_webserver) + +## Parรกmetros + +[`pipeline`] admite muchos parรกmetros; algunos son especรญficos de la tarea y algunos son generales para todas las pipelines. En general, puedes especificar parรกmetros en cualquier lugar que desees: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", my_parameter=1) + +out = transcriber(...) # This will use `my_parameter=1`. +out = transcriber(..., my_parameter=2) # This will override and use `my_parameter=2`. +out = transcriber(...) # This will go back to using `my_parameter=1`. +``` + +Vamos a echar un vistazo a tres importantes: + +### Device + +Si usas `device=n`, el pipeline automรกticamente coloca el modelo en el dispositivo especificado. Esto funcionarรก independientemente de si estรกs utilizando PyTorch o Tensorflow. + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0) +``` + +Si el modelo es demasiado grande para una sola GPU y estรกs utilizando PyTorch, puedes establecer `device_map="auto"` para determinar automรกticamente cรณmo cargar y almacenar los pesos del modelo. Utilizar el argumento `device_map` requiere el paquete ๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate): + +```bash +pip install --upgrade accelerate +``` + +El siguiente cรณdigo carga y almacena automรกticamente los pesos del modelo en varios dispositivos: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device_map="auto") +``` + +Tenga en cuenta que si se pasa `device_map="auto"`, no es necesario agregar el argumento `device=device` al instanciar tu `pipeline`, ยกya que podrรญas encontrar algรบn comportamiento inesperado! + +### Batch size + +Por defecto, los pipelines no realizarรกn inferencia por lotes por razones explicadas en detalle [aquรญ](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching). La razรณn es que la agrupaciรณn en lotes no es necesariamente mรกs rรกpida y, de hecho, puede ser bastante mรกs lenta en algunos casos. + +Pero si funciona en tu caso de uso, puedes utilizar: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0, batch_size=2) +audio_filenames = [f"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/{i}.flac" for i in range(1, 5)] +texts = transcriber(audio_filenames) +``` + +Esto ejecuta el pipeline en los 4 archivos de audio proporcionados, pero los pasarรก en lotes de a 2 al modelo (que estรก en una GPU, donde la agrupaciรณn en lotes es mรกs probable que ayude) sin requerir ningรบn cรณdigo adicional de tu parte. La salida siempre deberรญa coincidir con lo que habrรญas recibido sin agrupaciรณn en lotes. Solo se pretende como una forma de ayudarte a obtener mรกs velocidad de una pipeline. + +Los pipelines tambiรฉn pueden aliviar algunas de las complejidades de la agrupaciรณn en lotes porque, para algunos pipelines, un solo elemento (como un archivo de audio largo) necesita ser dividido en varias partes para ser procesado por un modelo. El pipeline realiza esta [*agrupaciรณn en lotes de fragmentos*](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-chunk-batching) por ti. + +### Task specific parameters + +Todas las tareas proporcionan parรกmetros especรญficos de la tarea que permiten flexibilidad adicional y opciones para ayudarte a completar tu trabajo. Por ejemplo, el mรฉtodo [`transformers.AutomaticSpeechRecognitionPipeline.__call__`] tiene un parรกmetro `return_timestamps` que suena prometedor para subtรญtulos de videos: + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2", return_timestamps=True) +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.', 'chunks': [{'timestamp': (0.0, 11.88), 'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its'}, {'timestamp': (11.88, 12.38), 'text': ' creed.'}]} +``` + +Como puedes ver, el modelo infiriรณ el texto y tambiรฉn saliรณ **cuรกndo** se pronunciaron las distintas oraciones. + +Hay muchos parรกmetros disponibles para cada tarea, asรญ que echa un vistazo a la referencia de la API de cada tarea para ver quรฉ puedes ajustar. Por ejemplo, el [`~transformers.AutomaticSpeechRecognitionPipeline`] tiene un parรกmetro `chunk_length_s` que es รบtil para trabajar con archivos de audio realmente largos (por ejemplo, subtรญtulos de pelรญculas completas o videos de una hora de duraciรณn) que un modelo tรญpicamente no puede manejar solo: + +```python +>>> transcriber = pipeline(model="openai/whisper-large-v2", chunk_length_s=30) +>>> transcriber("https://huggingface.co/datasets/reach-vb/random-audios/resolve/main/ted_60.wav") +{'text': " So in college, I was a government major, which means I had to write a lot of papers. Now, when a normal student writes a paper, they might spread the work out a little like this. So, you know. You get started maybe a little slowly, but you get enough done in the first week that with some heavier days later on, everything gets done and things stay civil. And I would want to do that like that. That would be the plan. I would have it all ready to go, but then actually the paper would come along, and then I would kind of do this. And that would happen every single paper. But then came my 90-page senior thesis, a paper you're supposed to spend a year on. I knew for a paper like that, my normal workflow was not an option, it was way too big a project. So I planned things out and I decided I kind of had to go something like this. This is how the year would go. So I'd start off light and I'd bump it up"} +``` + +ยกSi no puedes encontrar un parรกmetro que te ayude, no dudes en [solicitarlo](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml)! + +## Uso de pipelines en un conjunto de datos + +Los pipeline tambiรฉn puede ejecutar inferencia en un conjunto de datos grande. La forma mรกs fรกcil que recomendamos para hacer esto es utilizando un iterador: + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + + +pipe = pipeline(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipe(data()): + generated_characters += len(out[0]["generated_text"]) +``` + +El iterador `data()` produce cada resultado, y el pipeline automรกticamente +reconoce que la entrada es iterable y comenzarรก a buscar los datos mientras +continรบa procesรกndolos en la GPU (dicho proceso utiliza [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader)). Esto es importante porque no tienes que asignar memoria para todo el conjunto de datos y puedes alimentar la GPU lo mรกs rรกpido posible. + +Dado que la agrupaciรณn en lotes podrรญa acelerar las cosas, puede ser รบtil intentar ajustar el parรกmetro `batch_size` aquรญ. + +La forma mรกs sencilla de iterar sobre un conjunto de datos es cargandolo desde ๐Ÿค— [Datasets](https://github.com/huggingface/datasets/): + +```py +# KeyDataset is a util that will just output the item we're interested in. +from transformers.pipelines.pt_utils import KeyDataset +from datasets import load_dataset + +pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) +dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") + +for out in pipe(KeyDataset(dataset, "audio")): + print(out) +``` + +## Uso de pipelines para un servidor web + + +Crear un motor de inferencia es un tema complejo que merece su propia pรกgina. + + +[Link](./pipeline_webserver) + +## Pipeline de visiรณn + +Usar un [`pipeline`] para tareas de visiรณn es prรกcticamente idรฉntico. + +Especifica tu tarea y pasa tu imagen al clasificador. La imagen puede ser un enlace, una ruta local o una imagen codificada en base64. Por ejemplo, ยฟquรฉ especie de gato se muestra a continuaciรณn? + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(model="google/vit-base-patch16-224") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` + +## Pipeline de texto + +Usar un [`pipeline`] para tareas de PLN es prรกcticamente idรฉntico. + +```py +>>> from transformers import pipeline + +>>> # This model is a `zero-shot-classification` model. +>>> # It will classify text, except you are free to choose any label you might imagine +>>> classifier = pipeline(model="facebook/bart-large-mnli") +>>> classifier( +... "I have a problem with my iphone that needs to be resolved asap!!", +... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], +... ) +{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]} +``` + +## Pipeline multimodal + +[`pipeline`] admite mรกs de una modalidad. Por ejemplo, una tarea de respuesta a preguntas visuales (VQA) combina texto e imagen. No dudes en usar cualquier enlace de imagen que desees y una pregunta que quieras hacer sobre la imagen. La imagen puede ser una URL o una ruta local a la imagen. + +Por ejemplo, si usas esta [imagen de factura](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png): + +```py +>>> from transformers import pipeline + +>>> vqa = pipeline(model="impira/layoutlm-document-qa") +>>> output = vqa( +... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", +... question="What is the invoice number?", +... ) +>>> output[0]["score"] = round(output[0]["score"], 3) +>>> output +[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}] +``` + + + +Para ejecutar el ejemplo anterior, debe tener instalado [`pytesseract`](https://pypi.org/project/pytesseract/) ademรกs de ๐Ÿค— Transformers: + +```bash +sudo apt install -y tesseract-ocr +pip install pytesseract +``` + + + +## Uso de `pipeline` en modelos grandes con ๐Ÿค— `accelerate`: + +ยกPuedes ejecutar fรกcilmente `pipeline` en modelos grandes utilizando ๐Ÿค— `accelerate`! Primero asegรบrate de haber instalado `accelerate` con `pip install accelerate`. + +ยกLuego carga tu modelo utilizando `device_map="auto"`! Utilizaremos `facebook/opt-1.3b` para nuestro ejemplo. + +```py +# pip install accelerate +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto") +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +Tambiรฉn puedes pasar modelos cargados de 8 bits sรญ instalas `bitsandbytes` y agregas el argumento `load_in_8bit=True` + +```py +# pip install accelerate bitsandbytes +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}) +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +Nota que puedes reemplazar el punto de control con cualquier modelo de Hugging Face que admita la carga de modelos grandes, como BLOOM. + +## Crear demos web desde pipelines con `gradio` + +Los pipelines estรกn automรกticamente soportadas en [Gradio](https://github.com/gradio-app/gradio/), una biblioteca que hace que crear aplicaciones de aprendizaje automรกtico hermosas y fรกciles de usar en la web sea un proceso sencillo. Primero, asegรบrate de tener Gradio instalado: + +``` +pip install gradio +``` + +Luego, puedes crear una demo web alrededor de una pipeline de clasificaciรณn de imรกgenes (o cualquier otra pipeline) en una sola lรญnea de cรณdigo llamando a la funciรณn `Interface.from_pipeline` de Gradio para lanzar la pipeline. Esto crea una interfaz intuitiva *drag-and-drop* en tu navegador: + +```py +from transformers import pipeline +import gradio as gr + +pipe = pipeline("image-classification", model="google/vit-base-patch16-224") + +gr.Interface.from_pipeline(pipe).launch() +``` + + +![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/panda-classification.png) + +De forma predeterminada, la demo web se ejecuta en un servidor local. Si deseas compartirlo con otros, puedes generar un enlace pรบblico temporal estableciendo `share=True` en `launch()`. Tambiรฉn puedes hospedar tu demo en [Hugging Face Spaces](https://huggingface.co/spaces) para un enlace permanente. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_webserver.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_webserver.md new file mode 100644 index 0000000000000000000000000000000000000000..e268daabcbe881a617fad0cfd41c24d32bb78992 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pipeline_webserver.md @@ -0,0 +1,128 @@ + + +# Uso de un flujo de trabajo para un servidor web + + +Crear un motor de inferencia es un tema complejo, y la "mejor" soluciรณn probablemente dependerรก de tu caso de uso. ยฟEstรกs en CPU o en GPU? ยฟQuieres la latencia mรกs baja, el rendimiento mรกs alto, soporte para muchos modelos o simplemente optimizar altamente un modelo especรญfico? Hay muchas formas de abordar este tema, asรญ que lo que vamos a presentar es un buen valor predeterminado para comenzar, que no necesariamente serรก la soluciรณn mรกs รณptima para ti. + + + +Lo fundamental para entender es que podemos usar un iterador, tal como [en un conjunto de datos](pipeline_tutorial#uso-de-pipelines-en-un-conjunto-de-datos), ya que un servidor web es bรกsicamente un sistema que espera solicitudes y las trata a medida que llegan. + +Por lo general, los servidores web estรกn multiplexados (multihilo, asรญncrono, etc.) para manejar varias solicitudes simultรกneamente. Por otro lado, los flujos de trabajo (y principalmente los modelos subyacentes) no son realmente ideales para el paralelismo; consumen mucha RAM, por lo que es mejor darles todos los recursos disponibles cuando se estรกn ejecutando o es un trabajo intensivo en cรณmputo. + +Vamos a resolver esto haciendo que el servidor web maneje la carga ligera de recibir y enviar solicitudes, y que un รบnico hilo maneje el trabajo real. Este ejemplo va a utilizar `starlette`. El marco de trabajo no es realmente importante, pero es posible que debas ajustar o cambiar el cรณdigo si estรกs utilizando otro para lograr el mismo efecto. + +Crear `server.py`: + +```py +from starlette.applications import Starlette +from starlette.responses import JSONResponse +from starlette.routing import Route +from transformers import pipeline +import asyncio + + +async def homepage(request): + payload = await request.body() + string = payload.decode("utf-8") + response_q = asyncio.Queue() + await request.app.model_queue.put((string, response_q)) + output = await response_q.get() + return JSONResponse(output) + + +async def server_loop(q): + pipe = pipeline(model="google-bert/bert-base-uncased") + while True: + (string, response_q) = await q.get() + out = pipe(string) + await response_q.put(out) + + +app = Starlette( + routes=[ + Route("/", homepage, methods=["POST"]), + ], +) + + +@app.on_event("startup") +async def startup_event(): + q = asyncio.Queue() + app.model_queue = q + asyncio.create_task(server_loop(q)) +``` + +Ahora puedes empezar con: +```bash +uvicorn server:app +``` + +Y puedes consultarlo con: +```bash +curl -X POST -d "test [MASK]" http://localhost:8000/ +#[{"score":0.7742936015129089,"token":1012,"token_str":".","sequence":"test."},...] +``` + +ยกY listo, ahora tienes una buena idea de cรณmo crear un servidor web! + +Lo realmente importante es cargar el modelo solo **una vez**, de modo que no haya copias del modelo en el servidor web. De esta manera, no se utiliza RAM innecesariamente. Luego, el mecanismo de queuing (colas) te permite hacer cosas sofisticadas como acumular algunos elementos antes de inferir para usar el agrupamiento dinรกmico: + + + +El ejemplo de cรณdigo a continuaciรณn estรก escrito intencionalmente como pseudocรณdigo para facilitar la lectura. +ยกNo lo ejecutes sin verificar si tiene sentido para los recursos de tu sistema! + + + +```py +(string, rq) = await q.get() +strings = [] +queues = [] +while True: + try: + (string, rq) = await asyncio.wait_for(q.get(), timeout=0.001) # 1ms + except asyncio.exceptions.TimeoutError: + break + strings.append(string) + queues.append(rq) +strings +outs = pipe(strings, batch_size=len(strings)) +for rq, out in zip(queues, outs): + await rq.put(out) +``` + +Nuevamente, el cรณdigo propuesto estรก optimizado para la legibilidad, no para ser el mejor cรณdigo. +En primer lugar, no hay lรญmite de tamaรฑo de lote, lo cual generalmente no es una buena idea. Luego, el tiempo de espera se restablece en cada obtenciรณn de la cola, lo que significa que podrรญas esperar mucho mรกs de 1ms antes de ejecutar la inferencia (retrasando la primera solicitud en esa cantidad). + +Serรญa mejor tener un รบnico plazo de 1ms. + +Esto siempre esperarรก 1ms incluso si la cola estรก vacรญa, lo que podrรญa no ser lo mejor ya que probablemente quieras comenzar a hacer inferencias si no hay nada en la cola. Pero tal vez tenga sentido si el agrupamiento es realmente crucial para tu caso de uso. Nuevamente, no hay una soluciรณn รบnica y mejor. + + +## Algunas cosas que podrรญas considerar + +### Comprobaciรณn de errores + +Hay muchas cosas que pueden salir mal en producciรณn: falta de memoria, falta de espacio, cargar el modelo podrรญa fallar, la consulta podrรญa ser incorrecta, la consulta podrรญa ser correcta pero aรบn asรญ fallar debido a una mala configuraciรณn del modelo, y asรญ sucesivamente. + +Generalmente, es bueno que el servidor muestre los errores al usuario, por lo que agregar muchos bloques `try..except` para mostrar esos errores es una buena idea. Pero ten en cuenta que tambiรฉn puede ser un riesgo de seguridad revelar todos esos errores dependiendo de tu contexto de seguridad. + +### Interrupciรณn de circuito + +Los servidores web suelen verse mejor cuando hacen interrupciones de circuitos. Significa que devuelven errores adecuados cuando estรกn sobrecargados en lugar de simplemente esperar la consulta indefinidamente. Devolver un error 503 en lugar de esperar un tiempo muy largo o un error 504 despuรฉs de mucho tiempo. + +Esto es relativamente fรกcil de implementar en el cรณdigo propuesto ya que hay una sola cola. Mirar el tamaรฑo de la cola es una forma bรกsica de empezar a devolver errores antes de que tu servidor web falle bajo carga. + +### Bloqueo del hilo principal + +Actualmente, PyTorch no es consciente de la asincronรญa, y el cรกlculo bloquearรก el hilo principal mientras se ejecuta. Esto significa que serรญa mejor si PyTorch se viera obligado a ejecutarse en su propio hilo/proceso. Esto no se hizo aquรญ porque el cรณdigo es mucho mรกs complejo (principalmente porque los hilos, la asincronรญa y las colas no se llevan bien juntos). Pero en รบltima instancia, hace lo mismo. + +Esto serรญa importante si la inferencia de elementos individuales fuera larga (> 1s) porque en este caso, significa que cada consulta durante la inferencia tendrรญa que esperar 1s antes de recibir incluso un error. + +### Procesamiento por lotes dinรกmico + +En general, el procesamiento por lotes no es necesariamente una mejora respecto a pasar 1 elemento a la vez (ver [procesamiento por lotes](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching) para mรกs informaciรณn). Pero puede ser muy efectivo cuando se usa en el entorno correcto. En la API, no hay procesamiento por lotes dinรกmico por defecto (demasiada oportunidad para una desaceleraciรณn). Pero para la inferencia de BLOOM - que es un modelo muy grande - el procesamiento por lotes dinรกmico es **esencial** para proporcionar una experiencia decente para todos. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pr_checks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pr_checks.md new file mode 100644 index 0000000000000000000000000000000000000000..ba67e85306d3a9d9944f263ad79a70182b170d31 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/pr_checks.md @@ -0,0 +1,132 @@ + + +# Verificaciones en un Pull Request + +Cuando abres un _pull request_ en ๐Ÿค— Transformers, se ejecutarรกn una serie de verificaciones para asegurarte de que el _patch_ que estรกs agregando no rompa nada existente. Estas verificaciones son de cuatro tipos: +- pruebas regulares +- creaciรณn de la documentaciรณn +- estilo del cรณdigo y documentaciรณn +- consistencia del repositorio + +En este documento, intentaremos explicar cuรกles son esas diferentes verificaciones y el motivo detrรกs de ellas, asรญ como tambiรฉn cรณmo depurarlas localmente si una falla en tu PR. + +Recuerda que todas las verificaciones requieren que tengas una instalaciรณn de desarrollo: + +```bash +pip install transformers[dev] +``` + +o una instalaciรณn editable: + +```bash +pip install -e .[dev] +``` + +del repositorio de Transformers. + +## Pruebas + +Todos los procesos que comienzan con `ci/circleci: run_tests_` ejecutan partes del conjunto de pruebas de Transformers. Cada uno de esos procesos se enfoca en una parte de la biblioteca en un entorno determinado: por ejemplo, `ci/circleci: run_tests_pipelines_tf` ejecuta la prueba de _pipelines_ en un entorno donde solo estรก instalado TensorFlow. + +Ten en cuenta que para evitar ejecutar pruebas cuando no hay un cambio real en los mรณdulos que estรกs probando, solo se ejecuta una parte del conjunto de pruebas: se ejecuta una tarea auxiliar para determinar las diferencias en la biblioteca antes y despuรฉs del PR (lo que GitHub te muestra en la pestaรฑa "Files changes") y selecciona las pruebas afectadas por esa diferencia. Este auxiliar se puede ejecutar localmente usando: + +```bash +python utils/tests_fetcher.py +``` + +desde el directorio raiz del repositorio de Transformers. Se ejecutarรก lo siguiente: + +1. Verificaciรณn para cada archivo en el _diff_ si los cambios estรกn en el cรณdigo, solo en comentarios o _docstrings_. Solo los archivos con cambios reales de cรณdigo se conservan. +2. Creaciรณn de un mapa interno que proporciona para cada archivo del cรณdigo fuente de la biblioteca todos los archivos a los que impacta recursivamente. Se dice que el mรณdulo A impacta al mรณdulo B si el mรณdulo B importa el mรณdulo A. Para el impacto recursivo, necesitamos una cadena de mรณdulos que va del mรณdulo A al mรณdulo B en la que cada mรณdulo importa el anterior. +3. Aplicaciรณn de este mapa en los archivos recopilados en el paso 1, lo que nos da una lista de archivos modelo afectados por el PR. +4. Asignaciรณn de cada uno de esos archivos a sus archivos de prueba correspondientes y para obtener una la lista de pruebas a ejecutar. + +Al ejecutar el _script_ localmente, debes obtener los resultados de los pasos 1, 3 y 4 impresos y asรญ saber quรฉ pruebas se ejecutarรกn. El _script_ tambiรฉn crearรก un archivo llamado `test_list.txt` que contiene la lista de pruebas para ejecutar, y puede ejecutarlas localmente con el siguiente comando: + +```bash +python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) +``` + +En caso de que se te escape algo, el conjunto completo de pruebas tambiรฉn se ejecuta a diario. + +## Creaciรณn de la documentaciรณn + +El proceso `build_pr_documentation` compila y genera una vista previa de la documentaciรณn para asegurarse de que todo se vea bien una vez que se fusione tu PR. Un bot agregarรก un enlace para obtener una vista previa de la documentaciรณn en tu PR. Cualquier cambio que realices en el PR se actualiza automรกticamente en la vista previa. Si la documentaciรณn no se genera, haz clic en **Detalles** junto al proceso fallido para ver dรณnde saliรณ mal. A menudo, el error es tan simple como que falta un archivo en `toctree`. + +Si estรกs interesado en compilar u obtener una vista previa de la documentaciรณn localmente, echa un vistazo al [`README.md`](https://github.com/huggingface/transformers/tree/main/docs) en la carpeta `docs`. + +## Estilo de cรณdigo y documentaciรณn. + +El formato de cรณdigo se aplica a todos los archivos fuente, los ejemplos y las pruebas utilizando `black` e `ruff`. Tambiรฉn tenemos una herramienta personalizada que se ocupa del formato de los _docstrings_ y archivos `rst` (`utils/style_doc.py`), asรญ como del orden de las importaciones _lazy_ realizadas en los archivos `__init__.py` de Transformers (`utils /custom_init_isort.py`). Todo esto se puede probar ejecutando + +```bash +make style +``` + +CI verifica que se hayan aplicado dentro de la verificaciรณn `ci/circleci: check_code_quality`. Tambiรฉn se ejecuta `ruff`, que harรก una verificaciรณn bรกsica a tu cรณdigo y te harรก saber si encuentra una variable no definida, o una que no se usa. Para ejecutar esa verificaciรณn localmente, usa + +```bash +make quality +``` + +Esto puede llevar mucho tiempo, asรญ que para ejecutar lo mismo solo en los archivos que modificaste en la rama actual, ejecuta + +```bash +make fixup +``` + +Este รบltimo comando tambiรฉn ejecutarรก todas las verificaciones adicionales para la consistencia del repositorio. Echemos un vistazo a estas pruebas. + +## Consistencia del repositorio + +Esta verificaciรณn reagrupa todas las pruebas para asegurarse de que tu PR deja el repositorio en buen estado, y se realiza mediante `ci/circleci: check_repository_consistency`. Puedes ejecutar localmente esta verificaciรณn ejecutando lo siguiente: + +```bash +make repo-consistency +``` + +Esta instrucciรณn verifica que: + +- Todos los objetos agregados al _init_ estรกn documentados (realizados por `utils/check_repo.py`) +- Todos los archivos `__init__.py` tienen el mismo contenido en sus dos secciones (realizado por `utils/check_inits.py`) +- Todo el cรณdigo identificado como una copia de otro mรณdulo es consistente con el original (realizado por `utils/check_copies.py`) +- Todas las clases de configuraciรณn tienen al menos _checkpoint_ vรกlido mencionado en sus _docstrings_ (realizado por `utils/check_config_docstrings.py`) +- Las traducciones de los README y el รญndice del documento tienen la misma lista de modelos que el README principal (realizado por `utils/check_copies.py`) +- Las tablas generadas automaticamente en la documentaciรณn estรกn actualizadas (realizadas por `utils/check_table.py`) +- La biblioteca tiene todos los objetos disponibles incluso si no estรกn instaladas todas las dependencias opcionales (realizadas por `utils/check_dummies.py`) + +Si esta verificaciรณn falla, los primeros dos elementos requieren una reparaciรณn manual, los รบltimos cuatro pueden repararse automรกticamente ejecutando el comando + +```bash +make fix-copies +``` + +Las verificaciones adicionales se refieren a los PRs que agregan nuevos modelos, principalmente que: + +- Todos los modelos agregados estรกn en un Auto-mapping (realizado por `utils/check_repo.py`) + +- Todos los modelos se verifican correctamente (realizados por `utils/check_repo.py`) + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/preprocessing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/preprocessing.md new file mode 100644 index 0000000000000000000000000000000000000000..8486d6a0687abc296559a099884df6d6ae48cae2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/preprocessing.md @@ -0,0 +1,560 @@ + + +# Preprocesamiento + +[[open-in-colab]] + +Antes de que puedas utilizar los datos en un modelo, debes procesarlos en un formato aceptable para el modelo. Un modelo no entiende el texto en bruto, las imรกgenes o el audio. Estas entradas necesitan ser convertidas en nรบmeros y ensambladas en tensores. En este tutorial, podrรกs: + +* Preprocesar los datos textuales con un tokenizador. +* Preprocesar datos de imagen o audio con un extractor de caracterรญsticas. +* Preprocesar datos para una tarea multimodal con un procesador. + +## NLP + + + +La principal herramienta para procesar datos textuales es un [tokenizador](main_classes/tokenizer). Un tokenizador comienza dividiendo el texto en *tokens* segรบn un conjunto de reglas. Los tokens se convierten en nรบmeros, que se utilizan para construir tensores como entrada a un modelo. El tokenizador tambiรฉn aรฑade cualquier entrada adicional que requiera el modelo. + + + +Si tienes previsto utilizar un modelo pre-entrenado, es importante que utilices el tokenizador pre-entrenado asociado. Esto te asegura que el texto se divide de la misma manera que el corpus de pre-entrenamiento y utiliza el mismo รญndice de tokens correspondiente (usualmente referido como el *vocab*) durante el pre-entrenamiento. + + + +Comienza rรกpidamente cargando un tokenizador pre-entrenado con la clase [`AutoTokenizer`]. Esto descarga el *vocab* utilizado cuando un modelo es pre-entrenado. + +### Tokenizar + +Carga un tokenizador pre-entrenado con [`AutoTokenizer.from_pretrained`]: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") +``` + +A continuaciรณn, pasa tu frase al tokenizador: + +```py +>>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.") +>>> print(encoded_input) +{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +El tokenizador devuelve un diccionario con tres รญtems importantes: + +* [input_ids](glossary#input-ids) son los รญndices correspondientes a cada token de la frase. +* [attention_mask](glossary#attention-mask) indica si un token debe ser atendido o no. +* [token_type_ids](glossary#token-type-ids) identifica a quรฉ secuencia pertenece un token cuando hay mรกs de una secuencia. + +Tu puedes decodificar el `input_ids` para devolver la entrada original: + +```py +>>> tokenizer.decode(encoded_input["input_ids"]) +'[CLS] Do not meddle in the affairs of wizards, for they are subtle and quick to anger. [SEP]' +``` + +Como puedes ver, el tokenizador ha aรฑadido dos tokens especiales - `CLS` y `SEP` (clasificador y separador) - a la frase. No todos los modelos necesitan +tokens especiales, pero si lo llegas a necesitar, el tokenizador los aรฑadirรก automรกticamente. + +Si hay varias frases que quieres preprocesar, pasa las frases como una lista al tokenizador: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_inputs = tokenizer(batch_sentences) +>>> print(encoded_inputs) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1]]} +``` + +### Pad + +Esto nos lleva a un tema importante. Cuando se procesa un batch de frases, no siempre tienen la misma longitud. Esto es un problema porque los tensores que se introducen en el modelo deben tener una forma uniforme. El pad es una estrategia para asegurar que los tensores sean rectangulares aรฑadiendo un "padding token" especial a las oraciones con menos tokens. + +Establece el parรกmetro `padding` en `True` aplicando el pad a las secuencias mรกs cortas del batch para que coincidan con la secuencia mรกs larga: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + +Observa que el tokenizador ha aplicado el pad a la primera y la tercera frase con un "0" porque son mรกs cortas. + +### Truncamiento + +En el otro extremo del espectro, a veces una secuencia puede ser demasiado larga para un modelo. En este caso, tendrรกs que truncar la secuencia a una longitud mรกs corta. + +Establece el parรกmetro `truncation` a `True` para truncar una secuencia a la longitud mรกxima aceptada por el modelo: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + +### Construye tensores + +Finalmente, si quieres que el tokenizador devuelva los tensores reales que se introducen en el modelo. + +Establece el parรกmetro `return_tensors` como `pt` para PyTorch, o `tf` para TensorFlow: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch, padding=True, truncation=True, return_tensors="pt") +>>> print(encoded_input) +{'input_ids': tensor([[ 101, 153, 7719, 21490, 1122, 1114, 9582, 1623, 102], + [ 101, 5226, 1122, 9649, 1199, 2610, 1236, 102, 0]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 0]])} +===PT-TF-SPLIT=== +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch, padding=True, truncation=True, return_tensors="tf") +>>> print(encoded_input) +{'input_ids': , + 'token_type_ids': , + 'attention_mask': } +``` + +## Audio + +Las entradas de audio se preprocesan de forma diferente a las entradas textuales, pero el objetivo final es el mismo: crear secuencias numรฉricas que el modelo pueda entender. Un [extractor de caracterรญsticas](main_classes/feature_extractor) (o feature extractor en inglรฉs) estรก diseรฑado para extraer caracterรญsticas de datos provenientes de imรกgenes o audio sin procesar y convertirlos en tensores. Antes de empezar, instala ๐Ÿค— Datasets para cargar un dataset de audio para experimentar: + +```bash +pip install datasets +``` + +Carga la tarea de detecciรณn de palabras clave del benchmark [SUPERB](https://huggingface.co/datasets/superb) (consulta el [tutorial ๐Ÿค— Dataset](https://huggingface.co/docs/datasets/load_hub) para que obtengas mรกs detalles sobre cรณmo cargar un dataset): + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("superb", "ks") +``` + +Accede al primer elemento de la columna `audio` para echar un vistazo a la entrada. Al llamar a la columna `audio` se cargarรก y volverรก a muestrear automรกticamente el archivo de audio: + +```py +>>> dataset["train"][0]["audio"] +{'array': array([ 0. , 0. , 0. , ..., -0.00592041, + -0.00405884, -0.00253296], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/05734a36d88019a09725c20cc024e1c4e7982e37d7d55c0c1ca1742ea1cdd47f/_background_noise_/doing_the_dishes.wav', + 'sampling_rate': 16000} +``` + +Esto devuelve tres elementos: + +* `array` es la seรฑal de voz cargada - y potencialmente remuestreada - como un array 1D. +* `path` apunta a la ubicaciรณn del archivo de audio. +* `sampling_rate` se refiere a cuรกntos puntos de datos de la seรฑal de voz se miden por segundo. + +### Resample + +Para este tutorial, se utilizarรก el modelo [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base). Como puedes ver en la model card, el modelo Wav2Vec2 estรก pre-entrenado en audio de voz muestreado a 16kHz. Es importante que la tasa de muestreo de tus datos de audio coincida con la tasa de muestreo del dataset utilizado para pre-entrenar el modelo. Si la tasa de muestreo de tus datos no es la misma, deberรกs volver a muestrear tus datos de audio. + +Por ejemplo, carga el dataset [LJ Speech](https://huggingface.co/datasets/lj_speech) que tiene una tasa de muestreo de 22050kHz. Para utilizar el modelo Wav2Vec2 con este dataset, reduce la tasa de muestreo a 16kHz: + +```py +>>> lj_speech = load_dataset("lj_speech", split="train") +>>> lj_speech[0]["audio"] +{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., + 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 22050} +``` + +1. Usa el mรฉtodo ๐Ÿค— Datasets' [`cast_column`](https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.cast_column) para reducir la tasa de muestreo a 16kHz: + +```py +>>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +2. Carga el archivo de audio: + +```py +>>> lj_speech[0]["audio"] +{'array': array([-0.00064146, -0.00074657, -0.00068768, ..., 0.00068341, + 0.00014045, 0. ], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 16000} +``` + +Como puedes ver, el `sampling_rate` se ha reducido a 16kHz. Ahora que sabes cรณmo funciona el resampling, volvamos a nuestro ejemplo anterior con el dataset SUPERB. + +### Extractor de caracterรญsticas + +El siguiente paso es cargar un extractor de caracterรญsticas para normalizar y aplicar el pad a la entrada. Cuando se aplica padding a los datos textuales, se aรฑade un "0" para las secuencias mรกs cortas. La misma idea se aplica a los datos de audio y el extractor de caracterรญsticas de audio aรฑadirรก un "0" - interpretado como silencio - al "array". + +Carga el extractor de caracterรญsticas con [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +Pasa el `array` de audio al extractor de caracterรญsticas. Tambiรฉn te recomendamos aรฑadir el argumento `sampling_rate` en el extractor de caracterรญsticas para poder depurar mejor los errores silenciosos que puedan producirse. + +```py +>>> audio_input = [dataset["train"][0]["audio"]["array"]] +>>> feature_extractor(audio_input, sampling_rate=16000) +{'input_values': [array([ 0.00045439, 0.00045439, 0.00045439, ..., -0.1578519 , -0.10807519, -0.06727459], dtype=float32)]} +``` + +### Pad y truncamiento + +Al igual que el tokenizador, puedes aplicar padding o truncamiento para manejar secuencias variables en un batch. Fรญjate en la longitud de la secuencia de estas dos muestras de audio: + +```py +>>> dataset["train"][0]["audio"]["array"].shape +(1522930,) + +>>> dataset["train"][1]["audio"]["array"].shape +(988891,) +``` + +Como puedes ver, el `sampling_rate` se ha reducido a 16kHz. + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, +... sampling_rate=16000, +... padding=True, +... max_length=1000000, +... truncation=True, +... ) +... return inputs +``` + +Aplica la funciรณn a los primeros ejemplos del dataset: + +```py +>>> processed_dataset = preprocess_function(dataset["train"][:5]) +``` + +Ahora echa un vistazo a las longitudes de las muestras procesadas: + +```py +>>> processed_dataset["input_values"][0].shape +(1000000,) + +>>> processed_dataset["input_values"][1].shape +(1000000,) +``` + +Las longitudes de las dos primeras muestras coinciden ahora con la longitud mรกxima especificada. + +## Visiรณn + +Tambiรฉn se utiliza un extractor de caracterรญsticas para procesar imรกgenes para tareas de visiรณn por computadora. Una vez mรกs, el objetivo es convertir la imagen en bruto en un batch de tensores como entrada. + +Vamos a cargar el dataset [food101](https://huggingface.co/datasets/food101) para este tutorial. Usa el parรกmetro ๐Ÿค— Datasets `split` para cargar solo una pequeรฑa muestra de la divisiรณn de entrenamiento ya que el dataset es bastante grande: + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("food101", split="train[:100]") +``` + +A continuaciรณn, observa la imagen con la funciรณn ๐Ÿค— Datasets [`Image`](https://huggingface.co/docs/datasets/package_reference/main_classes?highlight=image#datasets.Image): + +```py +>>> dataset[0]["image"] +``` + +![vision-preprocess-tutorial.png](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png) + +### Extractor de caracterรญsticas + +Carga el extractor de caracterรญsticas con [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224") +``` + +### Aumento de Datos + +Para las tareas de visiรณn por computadora es comรบn aรฑadir algรบn tipo de aumento de datos (o data augmentation) a las imรกgenes como parte del preprocesamiento. Puedes aรฑadir el mรฉtodo de aumento de datos con cualquier librerรญa que quieras, pero en este tutorial utilizarรกs el mรณdulo [`transforms`](https://pytorch.org/vision/stable/transforms.html) de torchvision. + +1. Normaliza la imagen y utiliza [`Compose`](https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html) para encadenar algunas transformaciones - [`RandomResizedCrop`](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html) y [`ColorJitter`](https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html) - juntas: + +```py +>>> from torchvision.transforms import Compose, Normalize, RandomResizedCrop, ColorJitter, ToTensor + +>>> normalize = Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std) +>>> _transforms = Compose( +... [RandomResizedCrop(feature_extractor.size), ColorJitter(brightness=0.5, hue=0.5), ToTensor(), normalize] +... ) +``` + +2. El modelo acepta [`pixel_values`](model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values) como entrada. Este valor es generado por el extractor de caracterรญsticas. Crea una funciรณn que genere `pixel_values` a partir de las transformaciones: + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(image.convert("RGB")) for image in examples["image"]] +... return examples +``` + +3. A continuaciรณn, utiliza ๐Ÿค— Datasets [`set_transform`](https://huggingface.co/docs/datasets/process#format-transform) para aplicar las transformaciones sobre la marcha: + +```py +>>> dataset.set_transform(transforms) +``` + +4. Ahora, cuando accedes a la imagen, observarรกs que el extractor de caracterรญsticas ha aรฑadido a la entrada del modelo `pixel_values`: + +```py +>>> dataset[0]["image"] +{'image': , + 'label': 6, + 'pixel_values': tensor([[[ 0.0353, 0.0745, 0.1216, ..., -0.9922, -0.9922, -0.9922], + [-0.0196, 0.0667, 0.1294, ..., -0.9765, -0.9843, -0.9922], + [ 0.0196, 0.0824, 0.1137, ..., -0.9765, -0.9686, -0.8667], + ..., + [ 0.0275, 0.0745, 0.0510, ..., -0.1137, -0.1216, -0.0824], + [ 0.0667, 0.0824, 0.0667, ..., -0.0588, -0.0745, -0.0980], + [ 0.0353, 0.0353, 0.0431, ..., -0.0039, -0.0039, -0.0588]], + + [[ 0.2078, 0.2471, 0.2863, ..., -0.9451, -0.9373, -0.9451], + [ 0.1608, 0.2471, 0.3098, ..., -0.9373, -0.9451, -0.9373], + [ 0.2078, 0.2706, 0.3020, ..., -0.9608, -0.9373, -0.8275], + ..., + [-0.0353, 0.0118, -0.0039, ..., -0.2392, -0.2471, -0.2078], + [ 0.0196, 0.0353, 0.0196, ..., -0.1843, -0.2000, -0.2235], + [-0.0118, -0.0039, -0.0039, ..., -0.0980, -0.0980, -0.1529]], + + [[ 0.3961, 0.4431, 0.4980, ..., -0.9216, -0.9137, -0.9216], + [ 0.3569, 0.4510, 0.5216, ..., -0.9059, -0.9137, -0.9137], + [ 0.4118, 0.4745, 0.5216, ..., -0.9137, -0.8902, -0.7804], + ..., + [-0.2314, -0.1922, -0.2078, ..., -0.4196, -0.4275, -0.3882], + [-0.1843, -0.1686, -0.2000, ..., -0.3647, -0.3804, -0.4039], + [-0.1922, -0.1922, -0.1922, ..., -0.2941, -0.2863, -0.3412]]])} +``` + +Este es el aspecto de la imagen despuรฉs de preprocesarla. Como era de esperar por las transformaciones aplicadas, la imagen ha sido recortada aleatoriamente y sus propiedades de color son diferentes. + +```py +>>> import numpy as np +>>> import matplotlib.pyplot as plt + +>>> img = dataset[0]["pixel_values"] +>>> plt.imshow(img.permute(1, 2, 0)) +``` + +![preprocessed_image](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png) + +## Multimodal + +Para las tareas multimodales utilizarรกs una combinaciรณn de todo lo que has aprendido hasta ahora y aplicarรกs tus habilidades a una tarea de reconocimiento automรกtico de voz (ASR). Esto significa que necesitarรกs un: + +* Extractor de caracterรญsticas para preprocesar los datos de audio. +* Un tokenizador para procesar el texto. + +Volvamos al dataset [LJ Speech](https://huggingface.co/datasets/lj_speech): + +```py +>>> from datasets import load_dataset + +>>> lj_speech = load_dataset("lj_speech", split="train") +``` + +Suponiendo que te interesan principalmente las columnas `audio` y `texto`, elimina las demรกs columnas: + +```py +>>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"]) +``` + +Ahora echa un vistazo a las columnas `audio` y `texto`: + +```py +>>> lj_speech[0]["audio"] +{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., + 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 22050} + +>>> lj_speech[0]["text"] +'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition' +``` + +Recuerda la secciรณn anterior sobre el procesamiento de datos de audio, siempre debes [volver a muestrear](preprocessing#audio) la tasa de muestreo de tus datos de audio para que coincida con la tasa de muestreo del dataset utilizado para preentrenar un modelo: + +```py +>>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +### Processor + +Un processor combina un extractor de caracterรญsticas y un tokenizador. Cargue un procesador con [`AutoProcessor.from_pretrained`]: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h") +``` + +1. Crea una funciรณn para procesar los datos de audio en `input_values`, y tokeniza el texto en `labels`. Estas son las entradas del modelo: + +```py +>>> def prepare_dataset(example): +... audio = example["audio"] + +... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) + +... return example +``` + +2. Aplica la funciรณn `prepare_dataset` a una muestra: + +```py +>>> prepare_dataset(lj_speech[0]) +``` + +Observa que el mรฉtodo processor ha aรฑadido `input_values` y `labels`. La tasa de muestreo tambiรฉn se ha reducido correctamente a 16kHz. + +Genial, ahora deberรญas ser capaz de preprocesar datos para cualquier modalidad e incluso combinar diferentes modalidades. En el siguiente tutorial, aprenderรกs aplicar fine tuning a un modelo en tus datos reciรฉn preprocesados. + +## Todo lo que siempre quisiste saber sobre el padding y el truncamiento + +Hemos visto los comandos que funcionarรกn para la mayorรญa de los casos (hacer pad a tu batch teniendo en cuenta la longitud de la frase mรกxima y +truncar a la longitud mรกxima que el modelo puede aceptar). Sin embargo, la API admite mรกs estrategias si las necesitas. Los +tres argumentos que necesitas conocer para ello son `padding`, `truncation` y `max_length`. + +- `padding` controla el aplicarme padding al texto. Puede ser un booleano o una cadena que debe ser: + + - `True` o `'longest'` para aplicar el pad hasta la secuencia mรกs larga del batch (no apliques el padding si sรณlo le proporcionas + una sola secuencia). + - `'max_length'` para aplicar el pad hasta la longitud especificada por el argumento `max_length` o la longitud mรกxima aceptada + por el modelo si no le proporcionas `longitud_mรกxima` (`longitud_mรกxima=None`). Si sรณlo le proporcionas una รบnica secuencia + se le aplicarรก el padding. + `False` o `'do_not_pad'` para no aplicar pad a las secuencias. Como hemos visto antes, este es el comportamiento por + defecto. + +- `truncation` controla el truncamiento. Puede ser un booleano o una string que debe ser: + + - `True` o `'longest_first'` truncan hasta la longitud mรกxima especificada por el argumento `max_length` o + la longitud mรกxima aceptada por el modelo si no le proporcionas `max_length` (`max_length=None`). Esto + truncarรก token por token, eliminando un token de la secuencia mรกs larga del par hasta alcanzar la longitud + adecuada. + - `'only_second'` trunca hasta la longitud mรกxima especificada por el argumento `max_length` o la + longitud mรกxima aceptada por el modelo si no le proporcionas `max_length` (`max_length=None`). Esto sรณlo truncarรก + la segunda frase de un par si le proporcionas un par de secuencias (o un batch de pares de secuencias). + - `'only_first'` trunca hasta la longitud mรกxima especificada por el argumento `max_length` o la longitud mรกxima + aceptada por el modelo si no se proporciona `max_length` (`max_length=None`). Esto sรณlo truncarรก + la primera frase de un par si se proporciona un par de secuencias (o un lote de pares de secuencias). + - `False` o `'do_not_truncate'` para no truncar las secuencias. Como hemos visto antes, este es el comportamiento + por defecto. + +- `max_length` para controlar la longitud del padding/truncamiento. Puede ser un nรบmero entero o `None`, en cuyo caso +serรก por defecto la longitud mรกxima que el modelo puede aceptar. Si el modelo no tiene una longitud mรกxima de entrada especรญfica, el +padding/truncamiento a `longitud_mรกxima` se desactiva. + +A continuaciรณn te mostramos en una tabla que resume la forma recomendada de configurar el padding y el truncamiento. Si utilizas un par de secuencias de entrada en +algunos de los siguientes ejemplos, puedes sustituir `truncation=True` por una `STRATEGY` seleccionada en +`['only_first', 'only_second', 'longest_first']`, es decir, `truncation='only_second'` o `truncation= 'longest_first'` para controlar cรณmo se truncan ambas secuencias del par como se ha detallado anteriormente. + +| Truncation | Padding | Instrucciones | +|--------------------------------------|-----------------------------------|---------------------------------------------------------------------------------------------| +| no truncation | no padding | `tokenizer(batch_sentences)` | +| | padding secuencia max del batch | `tokenizer(batch_sentences, padding=True)` or | +| | | `tokenizer(batch_sentences, padding='longest')` | +| | padding long max de input model | `tokenizer(batch_sentences, padding='max_length')` | +| | padding a una long especifica | `tokenizer(batch_sentences, padding='max_length', max_length=42)` | +| truncation long max del input model | no padding | `tokenizer(batch_sentences, truncation=True)` or | +| | | `tokenizer(batch_sentences, truncation=STRATEGY)` | +| | padding secuencia max del batch | `tokenizer(batch_sentences, padding=True, truncation=True)` or | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY)` | +| | padding long max de input model | `tokenizer(batch_sentences, padding='max_length', truncation=True)` or | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY)` | +| | padding a una long especifica | Not possible | +| truncation a una long especifica | no padding | `tokenizer(batch_sentences, truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, truncation=STRATEGY, max_length=42)` | +| | padding secuencia max del batch | `tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42)` | +| | padding long max de input model | Not possible | +| | padding a una long especifica | `tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42)` | + + + + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..41b9c54002822cc7c04a476534e162bfc87757d3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/quicktour.md @@ -0,0 +1,401 @@ + + +# Tour rรกpido + +[[open-in-colab]] + +ยกEntra en marcha con los ๐Ÿค— Transformers! Comienza usando [`pipeline`] para una inferencia veloz, carga un modelo preentrenado y un tokenizador con una [AutoClass](./model_doc/auto) para resolver tu tarea de texto, visiรณn o audio. + + + +Todos los ejemplos de cรณdigo presentados en la documentaciรณn tienen un botรณn arriba a la derecha para elegir si quieres ocultar o mostrar el cรณdigo en Pytorch o TensorFlow. +Si no fuese asรญ, se espera que el cรณdigo funcione para ambos backends sin ningรบn cambio. + + + +## Pipeline + +[`pipeline`] es la forma mรกs fรกcil de usar un modelo preentrenado para una tarea dada. + + + +El [`pipeline`] soporta muchas tareas comunes listas para usar: + +**Texto**: +* Anรกlisis de Sentimiento (Sentiment Analysis, en inglรฉs): clasifica la polaridad de un texto dado. +* Generaciรณn de Texto (Text Generation, en inglรฉs): genera texto a partir de un input dado. +* Reconocimiento de Entidades (Name Entity Recognition o NER, en inglรฉs): etiqueta cada palabra con la entidad que representa (persona, fecha, ubicaciรณn, etc.). +* Responder Preguntas (Question answering, en inglรฉs): extrae la respuesta del contexto dado un contexto y una pregunta. +* Rellenar Mรกscara (Fill-mask, en inglรฉs): rellena el espacio faltante dado un texto con palabras enmascaradas. +* Resumir (Summarization, en inglรฉs): genera un resumen de una secuencia larga de texto o un documento. +* Traducciรณn (Translation, en inglรฉs): traduce un texto a otro idioma. +* Extracciรณn de Caracterรญsticas (Feature Extraction, en inglรฉs): crea una representaciรณn tensorial del texto. + +**Imagen**: +* Clasificaciรณn de Imรกgenes (Image Classification, en inglรฉs): clasifica una imagen. +* Segmentaciรณn de Imรกgenes (Image Segmentation, en inglรฉs): clasifica cada pixel de una imagen. +* Detecciรณn de Objetos (Object Detection, en inglรฉs): detecta objetos dentro de una imagen. + +**Audio**: +* Clasificaciรณn de Audios (Audio Classification, en inglรฉs): asigna una etiqueta a un segmento de audio. +* Reconocimiento de Voz Automรกtico (Automatic Speech Recognition o ASR, en inglรฉs): transcribe datos de audio a un texto. + + + +Para mรกs detalles acerca del [`pipeline`] y tareas asociadas, consulta la documentaciรณn [aquรญ](./main_classes/pipelines). + + + +### Uso del Pipeline + +En el siguiente ejemplo, usarรกs el [`pipeline`] para anรกlisis de sentimiento. + +Instala las siguientes dependencias si aรบn no lo has hecho: + + + + +```bash +pip install torch +``` + + + +```bash +pip install tensorflow +``` + + + +Importa [`pipeline`] y especifica la tarea que deseas completar: + +```py +>>> from transformers import pipeline + +>>> clasificador = pipeline("sentiment-analysis", model="pysentimiento/robertuito-sentiment-analysis") +``` + +El pipeline descarga y almacena en cachรฉ el [modelo preentrenado](https://huggingface.co/pysentimiento/robertuito-sentiment-analysis) y tokeniza para anรกlisis de sentimiento. Si no hubieramos elegido un modelo el pipeline habrรญa elegido uno por defecto. Ahora puedes usar `clasificador` en tu texto objetivo: + +```py +>>> clasificador("Estamos muy felices de mostrarte la biblioteca de ๐Ÿค— Transformers.") +[{'label': 'POS', 'score': 0.9320}] +``` + +Para mรกs de un enunciado, entrega una lista al [`pipeline`] que devolverรก una lista de diccionarios: + +El [`pipeline`] tambiรฉn puede iterar sobre un dataset entero. Comienza instalando la biblioteca [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/): + +```bash +pip install datasets +``` + +Crea un [`pipeline`] con la tarea que deseas resolver y el modelo que quieres usar. Coloca el parรกmetro `device` a `0` para poner los tensores en un dispositivo CUDA: + +```py +>>> import torch +>>> from transformers import pipeline + +>>> reconocedor_de_voz = pipeline( +... "automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-spanish", device=0 +... ) +``` + +A continuaciรณn, carga el dataset (ve ๐Ÿค— Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart.html) para mรกs detalles) sobre el que quisieras iterar. Por ejemplo, vamos a cargar el dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14): + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="es-ES", split="train") # doctest: +IGNORE_RESULT +``` + +Debemos asegurarnos de que la frecuencia de muestreo del conjunto de datos coincide con la frecuencia de muestreo con la que se entrenรณ `jonatasgrosman/wav2vec2-large-xlsr-53-spanish`. + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=reconocedor_de_voz.feature_extractor.sampling_rate)) +``` + +Los archivos de audio se cargan y remuestrean automรกticamente cuando llamamos a la columna `"audio"`. +Extraigamos las matrices de onda cruda (raw waveform, en inglรฉs) de las primeras 4 muestras y pasรฉmosla como una lista al pipeline: + +```py +>>> resultado = reconocedor_de_voz(dataset[:4]["audio"]) +>>> print([d["text"] for d in resultado]) +['ahora buenas eh a ver tengo un problema con vuestra aplicaciรณn resulta que que quiero hacer una transferencia bancaria a una cuenta conocida pero me da error la aplicaciรณn a ver que a ver que puede ser', 'la aplicaciรณn no cargue saldo de mi nueva cuenta', 'hola tengo un problema con la aplicaciรณn no carga y y tampoco veo que carga el saldo de mi cuenta nueva dice que la aplicaciรณn estรก siendo reparada y ahora no puedo acceder a mi cuenta no necesito inmediatamente', 'hora buena la aplicaciรณn no se carga la vida no carga el saldo de mi cuenta nueva dice que la villadenta siendo reparada y oro no puedo hacer a mi cuenta'] +``` + +Para un dataset mรกs grande, donde los inputs son de mayor tamaรฑo (como en habla/audio o visiรณn), querrรกs pasar un generador en lugar de una lista que carga todos los inputs en memoria. Ve la [documentaciรณn del pipeline](./main_classes/pipelines) para mรกs informaciรณn. + +### Usa otro modelo y otro tokenizador en el pipeline + +El [`pipeline`] puede acomodarse a cualquier modelo del [Model Hub](https://huggingface.co/models) haciendo mรกs fรกcil adaptar el [`pipeline`] para otros casos de uso. Por ejemplo, si quisieras un modelo capaz de manejar texto en francรฉs, usa los tags en el Model Hub para filtrar entre los modelos apropiados. El resultado mejor filtrado devuelve un [modelo BERT](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment) multilingual fine-tuned para el anรกlisis de sentimiento. Genial, ยกvamos a usar este modelo! + +```py +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +``` + + + +Usa [`AutoModelForSequenceClassification`] y ['AutoTokenizer'] para cargar un modelo preentrenado y un tokenizador asociado (mรกs en un `AutoClass` debajo): + +```py +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + + +Usa [`TFAutoModelForSequenceClassification`] y ['AutoTokenizer'] para cargar un modelo preentrenado y un tokenizador asociado (mรกs en un `TFAutoClass` debajo): + +```py +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + + +Despuรฉs puedes especificar el modelo y el tokenizador en el [`pipeline`], y aplicar el `classifier` en tu texto objetivo: + +```py +>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) +>>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") +[{'label': '5 stars', 'score': 0.7273}] +``` + +Si no pudieras encontrar el modelo para tu caso respectivo de uso necesitarรกs ajustar un modelo preentrenado a tus datos. Mira nuestro [tutorial de fine-tuning](./training) para aprender cรณmo. Finalmente, despuรฉs de que has ajustado tu modelo preentrenado, ยกpor favor considera compartirlo (ve el tutorial [aquรญ](./model_sharing)) con la comunidad en el Model Hub para democratizar el NLP! ๐Ÿค— + +## AutoClass + + + +Por debajo, las clases [`AutoModelForSequenceClassification`] y [`AutoTokenizer`] trabajan juntas para dar poder al [`pipeline`]. Una [AutoClass](./model_doc/auto) es un atajo que automรกticamente recupera la arquitectura de un modelo preentrenado con su nombre o el path. Sรณlo necesitarรกs seleccionar el `AutoClass` apropiado para tu tarea y tu tokenizador asociado con [`AutoTokenizer`]. + +Regresemos a nuestro ejemplo y veamos cรณmo puedes usar el `AutoClass` para reproducir los resultados del [`pipeline`]. + +### AutoTokenizer + +Un tokenizador es responsable de procesar el texto a un formato que sea entendible para el modelo. Primero, el tokenizador separarรก el texto en palabras llamadas *tokens*. Hay mรบltiples reglas que gobiernan el proceso de tokenizaciรณn incluyendo el cรณmo separar una palabra y en quรฉ nivel (aprende mรกs sobre tokenizaciรณn [aquรญ](./tokenizer_summary)). Lo mรกs importante es recordar que necesitarรกs instanciar el tokenizador con el mismo nombre del modelo para asegurar que estรกs usando las mismas reglas de tokenizaciรณn con las que el modelo fue preentrenado. + +Carga un tokenizador con [`AutoTokenizer`]: + +```py +>>> from transformers import AutoTokenizer + +>>> nombre_del_modelo = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tokenizer = AutoTokenizer.from_pretrained(nombre_del_modelo) +``` + +Despuรฉs, el tokenizador convierte los tokens a nรบmeros para construir un tensor que servirรก como input para el modelo. Esto es conocido como el *vocabulario* del modelo. + +Pasa tu texto al tokenizador: + +```py +>>> encoding = tokenizer("Estamos muy felices de mostrarte la biblioteca de ๐Ÿค— Transformers.") +>>> print(encoding) +{'input_ids': [101, 10602, 14000, 13653, 43353, 10107, 10102, 47201, 10218, 10106, 18283, 10102, 100, 58263, 119, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +El tokenizador devolverรก un diccionario conteniendo: + +* [input_ids](./glossary#input-ids): representaciones numรฉricas de los tokens. +* [attention_mask](.glossary#attention-mask): indica cuรกles tokens deben ser atendidos. + +Como con el [`pipeline`], el tokenizador aceptarรก una lista de inputs. Ademรกs, el tokenizador tambiรฉn puede rellenar (pad, en inglรฉs) y truncar el texto para devolver un lote (batch, en inglรฉs) de longitud uniforme: + + + + +```py +>>> pt_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="pt", +... ) +``` + + + +```py +>>> tf_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="tf", +... ) +``` + + + +Lee el tutorial de [preprocessing](./preprocessing) para mรกs detalles acerca de la tokenizaciรณn. + +### AutoModel + + + +๐Ÿค— Transformers provee una forma simple y unificada de cargar tus instancias preentrenadas. Esto significa que puedes cargar un [`AutoModel`] como cargarรญas un [`AutoTokenizer`]. La รบnica diferencia es seleccionar el [`AutoModel`] correcto para la tarea. Ya que estรกs clasificando texto, o secuencias, carga [`AutoModelForSequenceClassification`]: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +Ve el [task summary](./task_summary) para revisar quรฉ clase del [`AutoModel`] deberรญas usar para cada tarea. + + + +Ahora puedes pasar tu lote (batch) preprocesado de inputs directamente al modelo. Solo tienes que desempacar el diccionario aรฑadiendo `**`: + +```py +>>> pt_outputs = pt_model(**pt_batch) +``` + +El modelo producirรก las activaciones finales en el atributo `logits`. Aplica la funciรณn softmax a `logits` para obtener las probabilidades: + +```py +>>> from torch import nn + +>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) +>>> print(pt_predictions) +tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], + [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=) +``` + + +๐Ÿค— Transformers provee una forma simple y unificada de cargar tus instancias preentrenadas. Esto significa que puedes cargar un [`TFAutoModel`] como cargarรญas un [`AutoTokenizer`]. La รบnica diferencia es seleccionar el [`TFAutoModel`] correcto para la tarea. Ya que estรกs clasificando texto, o secuencias, carga [`TFAutoModelForSequenceClassification`]: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + Ve el [task summary](./task_summary) para revisar quรฉ clase del [`AutoModel`] + deberรญas usar para cada tarea. + + +Ahora puedes pasar tu lote preprocesado de inputs directamente al modelo pasando las llaves del diccionario directamente a los tensores: + +```py +>>> tf_outputs = tf_model(tf_batch) +``` + +El modelo producirรก las activaciones finales en el atributo `logits`. Aplica la funciรณn softmax a `logits` para obtener las probabilidades: + +```py +>>> import tensorflow as tf + +>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) +>>> print(tf.math.round(tf_predictions * 10**4) / 10**4) +tf.Tensor( +[[0.0021 0.0018 0.0116 0.2121 0.7725] + [0.2084 0.1826 0.1969 0.1755 0.2365]], shape=(2, 5), dtype=float32) +``` + + + + + +Todos los modelos de ๐Ÿค— Transformers (PyTorch o TensorFlow) producirรกn los tensores *antes* de la funciรณn de activaciรณn +final (como softmax) porque la funciรณn de activaciรณn final es comรบnmente fusionada con la pรฉrdida. + + + +Los modelos son [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) o [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) estรกndares asรญ que podrรกs usarlos en tu training loop usual. Sin embargo, para facilitar las cosas, ๐Ÿค— Transformers provee una clase [`Trainer`] para PyTorch que aรฑade funcionalidades para entrenamiento distribuido, preciciรณn mixta, y mรกs. Para TensorFlow, puedes usar el mรฉtodo `fit` desde [Keras](https://keras.io/). Consulta el [tutorial de entrenamiento](./training) para mรกs detalles. + + + +Los outputs del modelo de ๐Ÿค— Transformers son dataclasses especiales por lo que sus atributos pueden ser completados en un IDE. +Los outputs del modelo tambiรฉn se comportan como tuplas o diccionarios (e.g., puedes indexar con un entero, un slice o una cadena) en cuyo caso los atributos que son `None` son ignorados. + + + +### Guarda un modelo + + + +Una vez que se haya hecho fine-tuning a tu modelo puedes guardarlo con tu tokenizador usando [`PreTrainedModel.save_pretrained`]: + +```py +>>> pt_save_directory = "./pt_save_pretrained" +>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT +>>> pt_model.save_pretrained(pt_save_directory) +``` + +Cuando quieras usar el modelo otra vez cรกrgalo con [`PreTrainedModel.from_pretrained`]: + +```py +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") +``` + + + + +Una vez que se haya hecho fine-tuning a tu modelo puedes guardarlo con tu tokenizador usando [`TFPreTrainedModel.save_pretrained`]: + +```py +>>> tf_save_directory = "./tf_save_pretrained" +>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT +>>> tf_model.save_pretrained(tf_save_directory) +``` + +Cuando quieras usar el modelo otra vez cรกrgalo con [`TFPreTrainedModel.from_pretrained`]: + +```py +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") +``` + + + +Una caracterรญstica particularmente interesante de ๐Ÿค— Transformers es la habilidad de guardar el modelo y cargarlo como un modelo de PyTorch o TensorFlow. El parรกmetro `from_pt` o `from_tf` puede convertir el modelo de un framework al otro: + + + + +```py +>>> from transformers import AutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) +``` + + + +```py +>>> from transformers import TFAutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/run_scripts.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/run_scripts.md new file mode 100644 index 0000000000000000000000000000000000000000..cbabefa47b01bee1209f215e12f7985a0fcda0ad --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/run_scripts.md @@ -0,0 +1,351 @@ + + +# Entrenamiento con scripts + +Junto con los [notebooks](./notebooks) de ๐Ÿค— Transformers, tambiรฉn hay scripts con ejemplos que muestran cรณmo entrenar un modelo para una tarea en [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch), [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow), o [JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax). + +Tambiรฉn encontrarรกs scripts que hemos usado en nuestros [proyectos de investigaciรณn](https://github.com/huggingface/transformers-research-projects/) y [ejemplos pasados](https://github.com/huggingface/transformers/tree/main/examples/legacy) que en su mayorรญa son aportados por la comunidad. Estos scripts no se mantienen activamente y requieren una versiรณn especรญfica de ๐Ÿค— Transformers que probablemente sea incompatible con la รบltima versiรณn de la biblioteca. + +No se espera que los scripts de ejemplo funcionen de inmediato en todos los problemas, y es posible que debas adaptar el script al problema que estรกs tratando de resolver. Para ayudarte con esto, la mayorรญa de los scripts exponen completamente cรณmo se preprocesan los datos, lo que te permite editarlos segรบn sea necesario para tu caso de uso. + +Para cualquier caracterรญstica que te gustarรญa implementar en un script de ejemplo, por favor discรบtelo en el [foro](https://discuss.huggingface.co/) o con un [issue](https://github.com/huggingface/transformers/issues) antes de enviar un Pull Request. Si bien agradecemos las correcciones de errores, es poco probable que fusionemos un Pull Request que agregue mรกs funcionalidad a costa de la legibilidad. + +Esta guรญa te mostrarรก cรณmo ejecutar un ejemplo de un script de entrenamiento para resumir texto en [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) y [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization). Se espera que todos los ejemplos funcionen con ambos frameworks a menos que se especifique lo contrario. + +## Configuraciรณn + +Para ejecutar con รฉxito la รบltima versiรณn de los scripts de ejemplo debes **instalar ๐Ÿค— Transformers desde su fuente** en un nuevo entorno virtual: + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` + +Para versiones anteriores de los scripts de ejemplo, haz clic en alguno de los siguientes links: + +
+ Ejemplos de versiones anteriores de ๐Ÿค— Transformers + +
+ +Luego cambia tu clon actual de ๐Ÿค— Transformers a una versiรณn especรญfica, por ejemplo v3.5.1: + +```bash +git checkout tags/v3.5.1 +``` + +Una vez que hayas configurado la versiรณn correcta de la biblioteca, ve a la carpeta de ejemplo de tu elecciรณn e instala los requisitos especรญficos del ejemplo: + +```bash +pip install -r requirements.txt +``` + +## Ejecutar un script + + + +El script de ejemplo descarga y preprocesa un conjunto de datos de la biblioteca ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Luego, el script ajusta un conjunto de datos con [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) en una arquitectura que soporta la tarea de resumen. El siguiente ejemplo muestra cรณmo ajustar un [T5-small](https://huggingface.co/google-t5/t5-small) en el conjunto de datos [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). El modelo T5 requiere un argumento adicional `source_prefix` debido a cรณmo fue entrenado. Este aviso le permite a T5 saber que se trata de una tarea de resumir. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +El script de ejemplo descarga y preprocesa un conjunto de datos de la biblioteca ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Luego, el script ajusta un conjunto de datos utilizando Keras en una arquitectura que soporta la tarea de resumir. El siguiente ejemplo muestra cรณmo ajustar un [T5-small](https://huggingface.co/google-t5/t5-small) en el conjunto de datos [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). El modelo T5 requiere un argumento adicional `source_prefix` debido a cรณmo fue entrenado. Este aviso le permite a T5 saber que se trata de una tarea de resumir. + +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Entrenamiento distribuido y de precisiรณn mixta + +[Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) admite un entrenamiento distribuido y de precisiรณn mixta, lo que significa que tambiรฉn puedes usarlo en un script. Para habilitar ambas caracterรญsticas: + +- Agrega el argumento `fp16` para habilitar la precisiรณn mixta. +- Establece la cantidad de GPU que se usarรก con el argumento `nproc_per_node`. + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +Los scripts de TensorFlow utilizan [`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy) para el entrenamiento distribuido, y no es necesario agregar argumentos adicionales al script de entrenamiento. El script de TensorFlow utilizarรก mรบltiples GPUs de forma predeterminada si estรกn disponibles. + +## Ejecutar un script en una TPU + + + +Las Unidades de Procesamiento de Tensor (TPUs) estรกn diseรฑadas especรญficamente para acelerar el rendimiento. PyTorch admite TPU con el compilador de aprendizaje profundo [XLA](https://www.tensorflow.org/xla) (consulta [aquรญ](https://github.com/pytorch/xla/blob/master/README.md) para obtener mรกs detalles). Para usar una TPU, inicia el script `xla_spawn.py` y usa el argumento `num_cores` para establecer la cantidad de nรบcleos de TPU que deseas usar. + +```bash +python xla_spawn.py --num_cores 8 \ + summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +Las Unidades de Procesamiento de Tensor (TPUs) estรกn diseรฑadas especรญficamente para acelerar el rendimiento. TensorFlow utiliza [`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy) para entrenar en TPUs. Para usar una TPU, pasa el nombre del recurso de la TPU al argumento `tpu` + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Ejecutar un script con ๐Ÿค— Accelerate + +๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) es una biblioteca exclusiva de PyTorch que ofrece un mรฉtodo unificado para entrenar un modelo en varios tipos de configuraciones (solo CPU, GPU mรบltiples, TPU) mientras mantiene una visibilidad completa en el ciclo de entrenamiento de PyTorch. Asegรบrate de tener ๐Ÿค— Accelerate instalado si aรบn no lo tienes: + +> Nota: Como Accelerate se estรก desarrollando rรกpidamente, debes instalar la versiรณn git de Accelerate para ejecutar los scripts +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +En lugar del script `run_summarization.py`, debes usar el script `run_summarization_no_trainer.py`. Los scripts compatibles con ๐Ÿค— Accelerate tendrรกn un archivo `task_no_trainer.py` en la carpeta. Comienza ejecutando el siguiente comando para crear y guardar un archivo de configuraciรณn: + +```bash +accelerate config +``` + +Prueba tu configuraciรณn para asegurarte que estรก configurada correctamente: + +```bash +accelerate test +``` + +Todo listo para iniciar el entrenamiento: + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization +``` + +## Usar un conjunto de datos personalizado + +El script de la tarea resumir admite conjuntos de datos personalizados siempre que sean un archivo CSV o JSON Line. Cuando uses tu propio conjunto de datos, necesitas especificar varios argumentos adicionales: + +- `train_file` y `validation_file` especifican la ruta a tus archivos de entrenamiento y validaciรณn. +- `text_column` es el texto de entrada para resumir. +- `summary_column` es el texto de destino para la salida. + +Un script para resumir que utiliza un conjunto de datos personalizado se vera asรญ: + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate +``` + +## Prueba un script + +A veces, es una buena idea ejecutar tu secuencia de comandos en una cantidad menor de ejemplos para asegurarte de que todo funciona como se espera antes de comprometerte con un conjunto de datos completo, lo que puede demorar horas en completarse. Utiliza los siguientes argumentos para truncar el conjunto de datos a un nรบmero mรกximo de muestras: + +- `max_train_samples` +- `max_eval_samples` +- `max_predict_samples` + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +No todos los scripts de ejemplo admiten el argumento `max_predict_samples`. Puede que desconozcas si la secuencia de comandos admite este argumento, agrega `-h` para verificar: + +```bash +examples/pytorch/summarization/run_summarization.py -h +``` + +## Reanudar el entrenamiento desde el punto de control + +Otra opciรณn รบtil para habilitar es reanudar el entrenamiento desde un punto de control anterior. Esto asegurarรก que puedas continuar donde lo dejaste sin comenzar de nuevo si tu entrenamiento se interrumpe. Hay dos mรฉtodos para reanudar el entrenamiento desde un punto de control. + +El primer mรฉtodo utiliza el argumento `output_dir previous_output_dir` para reanudar el entrenamiento desde el รบltimo punto de control almacenado en `output_dir`. En este caso, debes eliminar `overwrite_output_dir`: + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --output_dir previous_output_dir \ + --predict_with_generate +``` + +El segundo mรฉtodo utiliza el argumento `resume_from_checkpoint path_to_specific_checkpoint` para reanudar el entrenamiento desde una carpeta de punto de control especรญfica. + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate +``` + +## Comparte tu modelo + +Todos los scripts pueden cargar tu modelo final en el [Model Hub](https://huggingface.co/models). Asegรบrate de haber iniciado sesiรณn en Hugging Face antes de comenzar: + +```bash +hf auth login +``` + +Luego agrega el argumento `push_to_hub` al script. Este argumento crearรก un repositorio con tu nombre de usuario Hugging Face y el nombre de la carpeta especificado en `output_dir`. + +Para darle a tu repositorio un nombre especรญfico, usa el argumento `push_to_hub_model_id` para aรฑadirlo. El repositorio se incluirรก automรกticamente en tu namespace. + +El siguiente ejemplo muestra cรณmo cargar un modelo con un nombre de repositorio especรญfico: + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/sagemaker.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/sagemaker.md new file mode 100644 index 0000000000000000000000000000000000000000..9bc5b74108419865a04b64388bbf49d6d5878aab --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/sagemaker.md @@ -0,0 +1,28 @@ + + +# Ejecutar el entrenamiento en Amazon SageMaker + +La documentaciรณn ha sido trasladada a [hf.co/docs/sagemaker](https://huggingface.co/docs/sagemaker). Esta pรกgina serรก eliminada en `transformers` 5.0. + +### Tabla de contenido + +- [Entrenar modelos de Hugging Face en Amazon SageMaker con SageMaker Python SDK](https://huggingface.co/docs/sagemaker/train) +- [Desplegar modelos de Hugging Face en Amazon SageMaker con SageMaker Python SDK](https://huggingface.co/docs/sagemaker/inference) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/serialization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/serialization.md new file mode 100644 index 0000000000000000000000000000000000000000..3ad7d08985305327301b9d385d20949de5260962 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/serialization.md @@ -0,0 +1,674 @@ + + +# Exportar modelos ๐Ÿค— Transformers + +Si necesitas implementar modelos ๐Ÿค— Transformers en entornos de producciรณn, te +recomendamos exportarlos a un formato serializado que se pueda cargar y ejecutar +en tiempos de ejecuciรณn y hardware especializados. En esta guรญa, te mostraremos cรณmo +exportar modelos ๐Ÿค— Transformers en dos formatos ampliamente utilizados: ONNX y TorchScript. + +Una vez exportado, un modelo puede optimizarse para la inferencia a travรฉs de tรฉcnicas +como la cuantizaciรณn y _pruning_. Si estรกs interesado en optimizar tus modelos para +que funcionen con la mรกxima eficiencia, consulta la +[biblioteca de ๐Ÿค— Optimum](https://github.com/huggingface/optimum). + +## ONNX + +El proyecto [ONNX (Open Neural Network eXchange)](http://onnx.ai) es un +estรกndar abierto que define un conjunto comรบn de operadores y un formato +de archivo comรบn para representar modelos de aprendizaje profundo en una +amplia variedad de _frameworks_, incluidos PyTorch y TensorFlow. Cuando un modelo +se exporta al formato ONNX, estos operadores se usan para construir un +grafo computacional (a menudo llamado _representaciรณn intermedia_) que +representa el flujo de datos a travรฉs de la red neuronal. + +Al exponer un grafo con operadores y tipos de datos estandarizados, ONNX facilita +el cambio entre frameworks. Por ejemplo, un modelo entrenado en PyTorch se puede +exportar a formato ONNX y luego importar en TensorFlow (y viceversa). + +๐Ÿค— Transformers proporciona un paquete llamado `transformers.onnx`, el cual permite convertir +los checkpoints de un modelo en un grafo ONNX aprovechando los objetos de configuraciรณn. +Estos objetos de configuraciรณn estรกn hechos a la medida de diferentes arquitecturas de modelos +y estรกn diseรฑados para ser fรกcilmente extensibles a otras arquitecturas. + +Las configuraciones a la medida incluyen las siguientes arquitecturas: + + + +- ALBERT +- BART +- BEiT +- BERT +- BigBird +- BigBird-Pegasus +- Blenderbot +- BlenderbotSmall +- BLOOM +- CamemBERT +- CLIP +- CodeGen +- ConvBERT +- ConvNeXT +- ConvNeXTV2 +- Data2VecText +- Data2VecVision +- DeBERTa +- DeBERTa-v2 +- DeiT +- DETR +- DistilBERT +- ELECTRA +- FlauBERT +- GPT Neo +- GPT-J +- I-BERT +- LayoutLM +- LayoutLMv3 +- LeViT +- LongT5 +- M2M100 +- Marian +- mBART +- MobileBERT +- MobileViT +- MT5 +- OpenAI GPT-2 +- Perceiver +- PLBart +- ResNet +- RoBERTa +- RoFormer +- SqueezeBERT +- T5 +- ViT +- XLM +- XLM-RoBERTa +- XLM-RoBERTa-XL +- YOLOS + +En las prรณximas dos secciones, te mostraremos cรณmo: + +* Exportar un modelo compatible utilizando el paquete `transformers.onnx`. +* Exportar un modelo personalizado para una arquitectura no compatible. + +### Exportar un model a ONNX + +Para exportar un modelo ๐Ÿค— Transformers a ONNX, tienes que instalar primero algunas +dependencias extra: + +```bash +pip install transformers[onnx] +``` + +El paquete `transformers.onnx` puede ser usado luego como un mรณdulo de Python: + +```bash +python -m transformers.onnx --help + +usage: Hugging Face Transformers ONNX exporter [-h] -m MODEL [--feature {causal-lm, ...}] [--opset OPSET] [--atol ATOL] output + +positional arguments: + output Path indicating where to store generated ONNX model. + +optional arguments: + -h, --help show this help message and exit + -m MODEL, --model MODEL + Model ID on huggingface.co or path on disk to load model from. + --feature {causal-lm, ...} + The type of features to export the model with. + --opset OPSET ONNX opset version to export the model with. + --atol ATOL Absolute difference tolerence when validating the model. +``` + +Exportar un checkpoint usando una configuraciรณn a la medida se puede hacer de la siguiente manera: + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased onnx/ +``` + +que deberรญa mostrar los siguientes registros: + +```bash +Validating ONNX model... + -[โœ“] ONNX model output names match reference model ({'last_hidden_state'}) + - Validating ONNX Model output "last_hidden_state": + -[โœ“] (2, 8, 768) matches (2, 8, 768) + -[โœ“] all values close (atol: 1e-05) +All good, model saved at: onnx/model.onnx +``` + +Esto exporta un grafo ONNX del checkpoint definido por el argumento `--model`. +En este ejemplo, es un modelo `distilbert/distilbert-base-uncased`, pero puede ser cualquier +checkpoint en Hugging Face Hub o que estรฉ almacenado localmente. + +El archivo `model.onnx` resultante se puede ejecutar en uno de los +[muchos aceleradores](https://onnx.ai/supported-tools.html#deployModel) +que admiten el estรกndar ONNX. Por ejemplo, podemos cargar y ejecutar el +modelo con [ONNX Runtime](https://onnxruntime.ai/) de la siguiente manera: + +```python +>>> from transformers import AutoTokenizer +>>> from onnxruntime import InferenceSession + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> session = InferenceSession("onnx/model.onnx") +>>> # ONNX Runtime expects NumPy arrays as input +>>> inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="np") +>>> outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs)) +``` + +Los nombres necesarios de salida (es decir, `["last_hidden_state"]`) se pueden obtener +echando un vistazo a la configuraciรณn ONNX de cada modelo. Por ejemplo, para DistilBERT tenemos: + +```python +>>> from transformers.models.distilbert import DistilBertConfig, DistilBertOnnxConfig + +>>> config = DistilBertConfig() +>>> onnx_config = DistilBertOnnxConfig(config) +>>> print(list(onnx_config.outputs.keys())) +["last_hidden_state"]s +``` + +El proceso es idรฉntico para los checkpoints de TensorFlow en Hub. +Por ejemplo, podemos exportar un checkpoint puro de TensorFlow desde +[Keras](https://huggingface.co/keras-io) de la siguiente manera: + +```bash +python -m transformers.onnx --model=keras-io/transformers-qa onnx/ +``` + +Para exportar un modelo que estรก almacenado localmente, deberรกs tener los pesos +y tokenizadores del modelo almacenados en un directorio. Por ejemplo, podemos cargar +y guardar un checkpoint de la siguiente manera: + + + +```python +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> # Load tokenizer and PyTorch weights form the Hub +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +>>> # Save to disk +>>> tokenizer.save_pretrained("local-pt-checkpoint") +>>> pt_model.save_pretrained("local-pt-checkpoint") +``` + +Una vez que se guarda el checkpoint, podemos exportarlo a ONNX usando el argumento `--model` +del paquete `transformers.onnx` al directorio deseado: + +```bash +python -m transformers.onnx --model=local-pt-checkpoint onnx/ +``` + + +```python +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> # Load tokenizer and TensorFlow weights from the Hub +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +>>> # Save to disk +>>> tokenizer.save_pretrained("local-tf-checkpoint") +>>> tf_model.save_pretrained("local-tf-checkpoint") +``` + +Una vez que se guarda el checkpoint, podemos exportarlo a ONNX usando el argumento `--model` +del paquete `transformers.onnx` al directorio deseado: + +```bash +python -m transformers.onnx --model=local-tf-checkpoint onnx/ +``` + + + +### Seleccionar caracterรญsticas para diferentes topologรญas de un modelo + +Cada configuraciรณn a la medida viene con un conjunto de _caracterรญsticas_ que te permiten exportar +modelos para diferentes tipos de topologรญas o tareas. Como se muestra en la siguiente tabla, cada +funciรณn estรก asociada con una auto-clase de automรณvil diferente: + +| Feature | Auto Class | +| ------------------------------------ | ------------------------------------ | +| `causal-lm`, `causal-lm-with-past` | `AutoModelForCausalLM` | +| `default`, `default-with-past` | `AutoModel` | +| `masked-lm` | `AutoModelForMaskedLM` | +| `question-answering` | `AutoModelForQuestionAnswering` | +| `seq2seq-lm`, `seq2seq-lm-with-past` | `AutoModelForSeq2SeqLM` | +| `sequence-classification` | `AutoModelForSequenceClassification` | +| `token-classification` | `AutoModelForTokenClassification` | + +Para cada configuraciรณn, puedes encontrar la lista de funciones admitidas a travรฉs de `FeaturesManager`. +Por ejemplo, para DistilBERT tenemos: + +```python +>>> from transformers.onnx.features import FeaturesManager + +>>> distilbert_features = list(FeaturesManager.get_supported_features_for_model_type("distilbert").keys()) +>>> print(distilbert_features) +["default", "masked-lm", "causal-lm", "sequence-classification", "token-classification", "question-answering"] +``` + +Le puedes pasar una de estas caracterรญsticas al argumento `--feature` en el paquete `transformers.onnx`. +Por ejemplo, para exportar un modelo de clasificaciรณn de texto, podemos elegir un modelo ya ajustado del Hub y ejecutar: + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased-finetuned-sst-2-english \ + --feature=sequence-classification onnx/ +``` + +que mostrarรก los siguientes registros: + +```bash +Validating ONNX model... + -[โœ“] ONNX model output names match reference model ({'logits'}) + - Validating ONNX Model output "logits": + -[โœ“] (2, 2) matches (2, 2) + -[โœ“] all values close (atol: 1e-05) +All good, model saved at: onnx/model.onnx +``` + +Ten en cuenta que, en este caso, los nombres de salida del modelo ajustado son `logits` en lugar de `last_hidden_state` +que vimos anteriormente con el checkpoint `distilbert/distilbert-base-uncased`. Esto es de esperarse ya que el modelo ajustado +tiene un cabezal de clasificaciรณn secuencial. + + + +Las caracterรญsticas que tienen un sufijo 'with-past' (por ejemplo, 'causal-lm-with-past') corresponden a topologรญas +de modelo con estados ocultos precalculados (clave y valores en los bloques de atenciรณn) que se pueden usar para una +decodificaciรณn autorregresiva mรกs rรกpida. + + + + +### Exportar un modelo para una arquitectura no compatible + +Si deseas exportar un modelo cuya arquitectura no es compatible de forma nativa +con la biblioteca, debes seguir tres pasos principales: + +1. Implementa una configuraciรณn personalizada en ONNX. +2. Exporta el modelo a ONNX. +3. Valide los resultados de PyTorch y los modelos exportados. + +En esta secciรณn, veremos cรณmo se implementรณ la serializaciรณn de DistilBERT +para mostrar lo que implica cada paso. + +#### Implementar una configuraciรณn personalizada en ONNX + +Comencemos con el objeto de configuraciรณn de ONNX. Proporcionamos tres clases abstractas +de las que debe heredar, segรบn el tipo de arquitectura del modelo que quieras exportar: + +* Modelos basados en el _Encoder_ inherente de [`~onnx.config.OnnxConfig`] +* Modelos basados en el _Decoder_ inherente de [`~onnx.config.OnnxConfigWithPast`] +* Modelos _Encoder-decoder_ inherente de [`~onnx.config.OnnxSeq2SeqConfigWithPast`] + + + +Una buena manera de implementar una configuraciรณn personalizada en ONNX es observar la implementaciรณn +existente en el archivo `configuration_.py` de una arquitectura similar. + + + +Dado que DistilBERT es un modelo de tipo _encoder_, su configuraciรณn se hereda de `OnnxConfig`: + +```python +>>> from typing import Mapping, OrderedDict +>>> from transformers.onnx import OnnxConfig + + +>>> class DistilBertOnnxConfig(OnnxConfig): +... @property +... def inputs(self) -> Mapping[str, Mapping[int, str]]: +... return OrderedDict( +... [ +... ("input_ids", {0: "batch", 1: "sequence"}), +... ("attention_mask", {0: "batch", 1: "sequence"}), +... ] +... ) +``` + +Cada objeto de configuraciรณn debe implementar la propiedad `inputs` y devolver un mapeo, +donde cada llave corresponde a una entrada esperada y cada valor indica el eje de esa entrada. +Para DistilBERT, podemos ver que se requieren dos entradas: `input_ids` y `attention_mask`. +Estas entradas tienen la misma forma de `(batch_size, sequence_length)`, es por lo que vemos +los mismos ejes utilizados en la configuraciรณn. + + + +Observa que la propiedad `inputs` para `DistilBertOnnxConfig` devuelve un `OrderedDict`. +Esto nos asegura que las entradas coincidan con su posiciรณn relativa dentro del mรฉtodo +`PreTrainedModel.forward()` al rastrear el grafo. Recomendamos usar un `OrderedDict` +para las propiedades `inputs` y `outputs` al implementar configuraciones ONNX personalizadas. + + + +Una vez que hayas implementado una configuraciรณn ONNX, puedes crear una +instancia proporcionando la configuraciรณn del modelo base de la siguiente manera: + +```python +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased") +>>> onnx_config = DistilBertOnnxConfig(config) +``` + +El objeto resultante tiene varias propiedades รบtiles. Por ejemplo, puedes ver el conjunto de operadores ONNX que se +utilizarรก durante la exportaciรณn: + +```python +>>> print(onnx_config.default_onnx_opset) +11 +``` + +Tambiรฉn puedes ver los resultados asociados con el modelo de la siguiente manera: + +```python +>>> print(onnx_config.outputs) +OrderedDict([("last_hidden_state", {0: "batch", 1: "sequence"})]) +``` + +Observa que la propiedad de salidas sigue la misma estructura que las entradas; +devuelve un objecto `OrderedDict` de salidas nombradas y sus formas. La estructura +de salida estรก vinculada a la elecciรณn de la funciรณn con la que se inicializa la configuraciรณn. +Por defecto, la configuraciรณn de ONNX se inicializa con la funciรณn `default` que +corresponde a exportar un modelo cargado con la clase `AutoModel`. Si quieres exportar +una topologรญa de modelo diferente, simplemente proporciona una caracterรญstica diferente +al argumento `task` cuando inicialices la configuraciรณn de ONNX. Por ejemplo, si quisiรฉramos +exportar DistilBERT con un cabezal de clasificaciรณn de secuencias, podrรญamos usar: + +```python +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased") +>>> onnx_config_for_seq_clf = DistilBertOnnxConfig(config, task="sequence-classification") +>>> print(onnx_config_for_seq_clf.outputs) +OrderedDict([('logits', {0: 'batch'})]) +``` + + + +Todas las propiedades base y mรฉtodos asociados con [`~onnx.config.OnnxConfig`] y las +otras clases de configuraciรณn se pueden sobreescribir si es necesario. +Consulte [`BartOnnxConfig`] para ver un ejemplo avanzado. + + + +#### Exportar el modelo + +Una vez que hayas implementado la configuraciรณn de ONNX, el siguiente paso es exportar el modelo. +Aquรญ podemos usar la funciรณn `export()` proporcionada por el paquete `transformers.onnx`. +Esta funciรณn espera la configuraciรณn de ONNX, junto con el modelo base y el tokenizador, +y la ruta para guardar el archivo exportado: + +```python +>>> from pathlib import Path +>>> from transformers.onnx import export +>>> from transformers import AutoTokenizer, AutoModel + +>>> onnx_path = Path("model.onnx") +>>> model_ckpt = "distilbert/distilbert-base-uncased" +>>> base_model = AutoModel.from_pretrained(model_ckpt) +>>> tokenizer = AutoTokenizer.from_pretrained(model_ckpt) + +>>> onnx_inputs, onnx_outputs = export(tokenizer, base_model, onnx_config, onnx_config.default_onnx_opset, onnx_path) +``` + +Los objetos `onnx_inputs` y `onnx_outputs` devueltos por la funciรณn `export()` +son listas de llaves definidas en las propiedades `inputs` y `outputs` de la configuraciรณn. +Una vez exportado el modelo, puedes probar que el modelo estรก bien formado de la siguiente manera: + +```python +>>> import onnx + +>>> onnx_model = onnx.load("model.onnx") +>>> onnx.checker.check_model(onnx_model) +``` + + + +Si tu modelo tiene mรกs de 2GB, verรกs que se crean muchos archivos adicionales durante la exportaciรณn. +Esto es _esperado_ porque ONNX usa [Bรบferes de protocolo](https://developers.google.com/protocol-buffers/) +para almacenar el modelo y รฉstos tienen un lรญmite de tamaรฑo de 2 GB. Consulta la +[documentaciรณn de ONNX](https://github.com/onnx/onnx/blob/master/docs/ExternalData.md) para obtener +instrucciones sobre cรณmo cargar modelos con datos externos. + + + +#### Validar los resultados del modelo + +El paso final es validar que los resultados del modelo base y exportado coincidan dentro +de cierta tolerancia absoluta. Aquรญ podemos usar la funciรณn `validate_model_outputs()` +proporcionada por el paquete `transformers.onnx` de la siguiente manera: + +```python +>>> from transformers.onnx import validate_model_outputs + +>>> validate_model_outputs( +... onnx_config, tokenizer, base_model, onnx_path, onnx_outputs, onnx_config.atol_for_validation +... ) +``` + +Esta funciรณn usa el mรฉtodo `OnnxConfig.generate_dummy_inputs()` para generar entradas para el modelo base +y exportado, y la tolerancia absoluta se puede definir en la configuraciรณn. En general, encontramos una +concordancia numรฉrica en el rango de 1e-6 a 1e-4, aunque es probable que cualquier valor menor que 1e-3 estรฉ bien. + +### Contribuir con una nueva configuraciรณn a ๐Ÿค— Transformers + +ยกEstamos buscando expandir el conjunto de configuraciones a la medida para usar y agradecemos las contribuciones de la comunidad! +Si deseas contribuir con su colaboraciรณn a la biblioteca, deberรกs: + +* Implementa la configuraciรณn de ONNX en el archivo `configuration_.py` correspondiente +* Incluye la arquitectura del modelo y las caracterรญsticas correspondientes en [`~onnx.features.FeatureManager`] +* Agrega tu arquitectura de modelo a las pruebas en `test_onnx_v2.py` + +Revisa cรณmo fue la contribuciรณn para la [configuraciรณn de IBERT](https://github.com/huggingface/transformers/pull/14868/files) +y asรญ tener una idea de lo que necesito. + +## TorchScript + + + +Este es el comienzo de nuestros experimentos con TorchScript y todavรญa estamos explorando sus capacidades con modelos de +tamaรฑo de entrada variable. Es un tema de interรฉs y profundizaremos nuestro anรกlisis en las prรณximas +versiones, con mรกs ejemplos de cรณdigo, una implementaciรณn mรกs flexible y puntos de referencia que comparen cรณdigos +basados en Python con TorchScript compilado. + + + +Segรบn la documentaciรณn de PyTorch: "TorchScript es una forma de crear modelos serializables y optimizables a partir del +cรณdigo de PyTorch". Los dos mรณdulos de Pytorch [JIT y TRACE](https://pytorch.org/docs/stable/jit.html) permiten al +desarrollador exportar su modelo para reutilizarlo en otros programas, como los programas C++ orientados a la eficiencia. + +Hemos proporcionado una interfaz que permite exportar modelos de ๐Ÿค— Transformers a TorchScript para que puedan reutilizarse +en un entorno diferente al de un programa Python basado en PyTorch. Aquรญ explicamos cรณmo exportar y usar nuestros modelos +usando TorchScript. + +Exportar un modelo requiere de dos cosas: + +- un pase hacia adelante con entradas ficticias. +- instanciaciรณn del modelo con la indicador `torchscript`. + +Estas necesidades implican varias cosas con las que los desarrolladores deben tener cuidado. ร‰stas se detallan a continuaciรณn. + +### Indicador de TorchScript y pesos atados + +Este indicador es necesario porque la mayorรญa de los modelos de lenguaje en este repositorio tienen pesos vinculados entre su capa +de `Embedding` y su capa de `Decoding`. TorchScript no permite la exportaciรณn de modelos que tengan pesos atados, por lo que es +necesario desvincular y clonar los pesos previamente. + +Esto implica que los modelos instanciados con el indicador `torchscript` tienen su capa `Embedding` y `Decoding` separadas, +lo que significa que no deben entrenarse mรกs adelante. El entrenamiento desincronizarรญa las dos capas, lo que generarรญa +resultados inesperados. + +Este no es el caso de los modelos que no tienen un cabezal de modelo de lenguaje, ya que no tienen pesos atados. +Estos modelos se pueden exportar de forma segura sin el indicador `torchscript`. + +### Entradas ficticias y longitudes estรกndar + +Las entradas ficticias se utilizan para crear un modelo de pase hacia adelante. Mientras los valores de las entradas se +propagan a travรฉs de las capas, PyTorch realiza un seguimiento de las diferentes operaciones ejecutadas en cada tensor. +Estas operaciones registradas se utilizan luego para crear el "rastro" del modelo. + +El rastro se crea en relaciรณn con las dimensiones de las entradas. Por lo tanto, estรก limitado por las dimensiones de la +entrada ficticia y no funcionarรก para ninguna otra longitud de secuencia o tamaรฑo de lote. Al intentar con un tamaรฑo diferente, +un error como: + +`The expanded size of the tensor (3) must match the existing size (7) at non-singleton dimension 2` + +aparecerรก. Por lo tanto, se recomienda rastrear el modelo con un tamaรฑo de entrada ficticia al menos tan grande como la +entrada mรกs grande que se alimentarรก al modelo durante la inferencia. El _padding_ se puede realizar para completar los +valores que faltan. Sin embargo, como el modelo se habrรก rastreado con un tamaรฑo de entrada grande, las dimensiones de +las diferentes matrices tambiรฉn serรกn grandes, lo que darรก como resultado mรกs cรกlculos. + +Se recomienda tener cuidado con el nรบmero total de operaciones realizadas en cada entrada y seguir de cerca el rendimiento +al exportar modelos de longitud de secuencia variable. + +### Usar TorchScript en Python + +A continuaciรณn se muestra un ejemplo que muestra cรณmo guardar, cargar modelos y cรณmo usar el rastreo para la inferencia. + +#### Guardando un modelo + +Este fragmento muestra cรณmo usar TorchScript para exportar un `BertModel`. Aquรญ, el `BertModel` se instancia de acuerdo +con la clase `BertConfig` y luego se guarda en el disco con el nombre de archivo `traced_bert.pt` + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch + +enc = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") + +# Tokenizing input text +text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" +tokenized_text = enc.tokenize(text) + +# Masking one of the input tokens +masked_index = 8 +tokenized_text[masked_index] = "[MASK]" +indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) +segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] + +# Creating a dummy input +tokens_tensor = torch.tensor([indexed_tokens]) +segments_tensors = torch.tensor([segments_ids]) +dummy_input = [tokens_tensor, segments_tensors] + +# Initializing the model with the torchscript flag +# Flag set to True even though it is not necessary as this model does not have an LM Head. +config = BertConfig( + vocab_size_or_config_json_file=32000, + hidden_size=768, + num_hidden_layers=12, + num_attention_heads=12, + intermediate_size=3072, + torchscript=True, +) + +# Instantiating the model +model = BertModel(config) + +# The model needs to be in evaluation mode +model.eval() + +# If you are instantiating the model with *from_pretrained* you can also easily set the TorchScript flag +model = BertModel.from_pretrained("google-bert/bert-base-uncased", torchscript=True) + +# Creating the trace +traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) +torch.jit.save(traced_model, "traced_bert.pt") +``` + +#### Cargar un modelo + +Este fragmento muestra cรณmo cargar el `BertModel` que se guardรณ previamente en el disco con el nombre `traced_bert.pt`. +Estamos reutilizando el `dummy_input` previamente inicializado. + +```python +loaded_model = torch.jit.load("traced_bert.pt") +loaded_model.eval() + +all_encoder_layers, pooled_output = loaded_model(*dummy_input) +``` + +#### Usar un modelo rastreado para la inferencia + +Usar el modelo rastreado para la inferencia es tan simple como usar su mรฉtodo `__call__`: + +```python +traced_model(tokens_tensor, segments_tensors) +``` + +### Implementar los modelos HuggingFace TorchScript en AWS mediante Neuron SDK + +AWS presentรณ la familia de instancias [Amazon EC2 Inf1](https://aws.amazon.com/ec2/instance-types/inf1/) para la inferencia +de aprendizaje automรกtico de bajo costo y alto rendimiento en la nube. Las instancias Inf1 funcionan con el chip AWS +Inferentia, un acelerador de hardware personalizado, que se especializa en cargas de trabajo de inferencia de aprendizaje +profundo. [AWS Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#) es el kit de desarrollo para Inferentia +que admite el rastreo y la optimizaciรณn de modelos de transformers para su implementaciรณn en Inf1. El SDK de Neuron proporciona: + + +1. API fรกcil de usar con una lรญnea de cambio de cรณdigo para rastrear y optimizar un modelo de TorchScript para la inferencia en la nube. +2. Optimizaciones de rendimiento listas para usar con un [costo-rendimiento mejorado](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/>) +3. Soporte para modelos HuggingFace Transformers construidos con [PyTorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html) +o [TensorFlow](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html). + +#### Implicaciones + +Los modelos Transformers basados en la arquitectura +[BERT (Representaciones de _Enconder_ bidireccional de Transformers)](https://huggingface.co/docs/transformers/main/model_doc/bert), +o sus variantes, como [distilBERT](https://huggingface.co/docs/transformers/main/model_doc/distilbert) y +[roBERTa](https://huggingface.co/docs/transformers/main/model_doc/roberta), se ejecutarรกn mejor en Inf1 para tareas no +generativas, como la respuesta extractiva de preguntas, la clasificaciรณn de secuencias y la clasificaciรณn de tokens. +Como alternativa, las tareas de generaciรณn de texto se pueden adaptar para ejecutarse en Inf1, segรบn este +[tutorial de AWS Neuron MarianMT](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html). +Puedes encontrar mรกs informaciรณn sobre los modelos que estรกn listos para usarse en Inferentia en la +[secciรณn _Model Architecture Fit_ de la documentaciรณn de Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia). + +#### Dependencias + +Usar AWS Neuron para convertir modelos requiere las siguientes dependencias y entornos: + +* Un [entorno Neuron SDK](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide), +que viene preconfigurado en [AWS Deep Learning AMI](https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html). + +#### Convertir un modelo a AWS Neuron + +Con el mismo script usado en [Uso de TorchScript en Python](https://huggingface.co/docs/transformers/main/es/serialization#using-torchscript-in-python) +para rastrear un "BertModel", puedes importar la extensiรณn del _framework_ `torch.neuron` para acceder a los componentes +del SDK de Neuron a travรฉs de una API de Python. + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch +import torch.neuron +``` +Y modificando la lรญnea de cรณdigo de rastreo de: + +```python +torch.jit.trace(model, [tokens_tensor, segments_tensors]) +``` + +con lo siguiente: + +```python +torch.neuron.trace(model, [token_tensor, segments_tensors]) +``` + +Este cambio permite a Neuron SDK rastrear el modelo y optimizarlo para ejecutarse en instancias Inf1. + +Para obtener mรกs informaciรณn sobre las funciones, las herramientas, los tutoriales de ejemplo y las รบltimas actualizaciones +de AWS Neuron SDK, consulte la [documentaciรณn de AWS NeuronSDK](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/task_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/task_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..639654c3697a2b85eabd5b3153efd841f5d69a61 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/task_summary.md @@ -0,0 +1,340 @@ + + +# Lo que ๐Ÿค— Transformers puede hacer + +๐Ÿค— Transformers es una biblioteca de modelos preentrenados de รบltima generaciรณn para procesamiento del lenguaje natural (NLP, por sus siglas en inglรฉs), visiรณn por computadora y tareas de procesamiento de audio y voz. No solo contiene modelos Transformer, sino tambiรฉn modelos no Transformer como redes convolucionales modernas para tareas de visiรณn por computadora. Si observas algunos de los productos de consumo mรกs populares hoy en dรญa, como telรฉfonos inteligentes, aplicaciones y televisores, es probable que haya alguna tecnologรญa de aprendizaje profundo detrรกs. ยฟQuieres quitar un objeto de fondo de una foto tomada por tu telรฉfono inteligente? Este es un ejemplo de una tarea de segmentaciรณn panรณptica (no te preocupes si aรบn no sabes quรฉ significa, ยกlo describiremos en las siguientes secciones!). + +Esta pรกgina proporciona una descripciรณn general de las diferentes tareas de procesamiento de audio y voz, visiรณn por computadora y NLP que se pueden resolver con la biblioteca ๐Ÿค— Transformers en solo tres lรญneas de cรณdigo. + +## Audio + +Las tareas de procesamiento de audio y voz son un poco diferentes de las otras modalidades principalmente porque el audio como entrada es una seรฑal continua. A diferencia del texto, una forma de onda de audio cruda no se puede dividir ordenadamente en fragmentos discretos de la misma manera en que una oraciรณn puede dividirse en palabras. Para superar esto, la seรฑal de audio cruda generalmente se muestrea a intervalos regulares. Si tomas mรกs muestras dentro de un intervalo, la tasa de muestreo es mayor y el audio se asemeja mรกs a la fuente de audio original. + +Enfoques anteriores preprocesaban el audio para extraer caracterรญsticas รบtiles. Ahora es mรกs comรบn comenzar las tareas de procesamiento de audio y voz alimentando directamente la forma de onda de audio cruda a un codificador de caracterรญsticas para extraer una representaciรณn de audio. Esto simplifica el paso de preprocesamiento y permite que el modelo aprenda las caracterรญsticas mรกs esenciales. + +### Clasificaciรณn de audio + +La clasificaciรณn de audio es una tarea que etiqueta datos de audio con un conjunto predefinido de clases. Es una categorรญa amplia con muchas aplicaciones especรญficas, algunas de las cuales incluyen: + +* clasificaciรณn de escena acรบstica: etiquetar audio con una etiqueta de escena ("oficina", "playa", "estadio") +* detecciรณn de eventos acรบsticos: etiquetar audio con una etiqueta de evento de sonido ("bocina de automรณvil", "llamada de ballena", "cristal rompiรฉndose") +* etiquetado: etiquetar audio que contiene varios sonidos (canto de pรกjaros, identificaciรณn de altavoces en una reuniรณn) +* clasificaciรณn de mรบsica: etiquetar mรบsica con una etiqueta de gรฉnero ("metal", "hip-hop", "country") + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="audio-classification", model="superb/hubert-base-superb-er") +>>> preds = classifier("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4532, 'label': 'hap'}, + {'score': 0.3622, 'label': 'sad'}, + {'score': 0.0943, 'label': 'neu'}, + {'score': 0.0903, 'label': 'ang'}] +``` + +### Reconocimiento automรกtico del habla + +El reconocimiento automรกtico del habla (ASR, por sus siglas en inglรฉs) transcribe el habla a texto. Es una de las tareas de audio mรกs comunes, en parte debido a que el habla es una forma natural de comunicaciรณn humana. Hoy en dรญa, los sistemas ASR estรกn integrados en productos de tecnologรญa "inteligente" como altavoces, telรฉfonos y automรณviles. Podemos pedirle a nuestros asistentes virtuales que reproduzcan mรบsica, establezcan recordatorios y nos informen sobre el clima. + +Pero uno de los desafรญos clave que las arquitecturas Transformer han ayudado a superar es en los idiomas con recursos limitados. Al preentrenar con grandes cantidades de datos de habla, afinar el modelo solo con una hora de datos de habla etiquetados en un idioma con recursos limitados aรบn puede producir resultados de alta calidad en comparaciรณn con los sistemas ASR anteriores entrenados con 100 veces mรกs datos etiquetados. + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-small") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +## Visiรณn por computadora + +Una de las primeras y exitosas tareas de visiรณn por computadora fue reconocer imรกgenes de nรบmeros de cรณdigo postal utilizando una [red neuronal convolucional](glossary#convolution) (CNN, por sus siglas en inglรฉs). Una imagen estรก compuesta por pรญxeles, y cada pรญxel tiene un valor numรฉrico. Esto facilita representar una imagen como una matriz de valores de pรญxeles. Cada combinaciรณn particular de valores de pรญxeles describe los colores de una imagen. + +Dos formas generales en las que se pueden resolver las tareas de visiรณn por computadora son: + +1. Utilizar convoluciones para aprender las caracterรญsticas jerรกrquicas de una imagen, desde caracterรญsticas de bajo nivel hasta cosas abstractas de alto nivel. +2. Dividir una imagen en parches y utilizar un Transformer para aprender gradualmente cรณmo cada parche de imagen se relaciona entre sรญ para formar una imagen. A diferencia del enfoque ascendente preferido por una CNN, esto es como comenzar con una imagen borrosa y luego enfocarla gradualmente. + +### Clasificaciรณn de imรกgenes + +La clasificaciรณn de imรกgenes etiqueta una imagen completa con un conjunto predefinido de clases. Como la mayorรญa de las tareas de clasificaciรณn, hay muchos casos prรกcticos para la clasificaciรณn de imรกgenes, algunos de los cuales incluyen: + +* salud: etiquetar imรกgenes mรฉdicas para detectar enfermedades o monitorear la salud del paciente +* medio ambiente: etiquetar imรกgenes de satรฉlite para monitorear la deforestaciรณn, informar la gestiรณn de รกreas silvestres o detectar incendios forestales +* agricultura: etiquetar imรกgenes de cultivos para monitorear la salud de las plantas o imรกgenes de satรฉlite para el monitoreo del uso del suelo +* ecologรญa: etiquetar imรกgenes de especies animales o vegetales para monitorear poblaciones de vida silvestre o rastrear especies en peligro de extinciรณn + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="image-classification") +>>> preds = classifier( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.4335, 'label': 'lynx, catamount'} +{'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'} +{'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'} +{'score': 0.0239, 'label': 'Egyptian cat'} +{'score': 0.0229, 'label': 'tiger cat'} +``` + +### Detecciรณn de objetos + +A diferencia de la clasificaciรณn de imรกgenes, la detecciรณn de objetos identifica mรบltiples objetos dentro de una imagen y las posiciones de los objetos en la imagen (definidas por el cuadro delimitador). Algunas aplicaciones ejemplares de la detecciรณn de objetos incluyen: + +* vehรญculos autรณnomos: detectar objetos de trรกfico cotidianos como otros vehรญculos, peatones y semรกforos +* teledetecciรณn: monitoreo de desastres, planificaciรณn urbana y pronรณstico del tiempo +* detecciรณn de defectos: detectar grietas o daรฑos estructurales en edificios y defectos de fabricaciรณn + +```py +>>> from transformers import pipeline + +>>> detector = pipeline(task="object-detection") +>>> preds = detector( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"], "box": pred["box"]} for pred in preds] +>>> preds +[{'score': 0.9865, + 'label': 'cat', + 'box': {'xmin': 178, 'ymin': 154, 'xmax': 882, 'ymax': 598}}] +``` + +### Segmentaciรณn de imรกgenes + +La segmentaciรณn de imรกgenes es una tarea a nivel de pรญxeles que asigna cada pรญxel en una imagen a una clase. A diferencia de la detecciรณn de objetos, que utiliza cuadros delimitadores para etiquetar y predecir objetos en una imagen, la segmentaciรณn es mรกs granular. La segmentaciรณn puede detectar objetos a nivel de pรญxeles. Hay varios tipos de segmentaciรณn de imรกgenes: + +* segmentaciรณn de instancias: ademรกs de etiquetar la clase de un objeto, tambiรฉn etiqueta cada instancia distinta de un objeto ("perro-1", "perro-2") +* segmentaciรณn panรณptica: una combinaciรณn de segmentaciรณn semรกntica y de instancias; etiqueta cada pรญxel con una clase semรกntica **y** cada instancia distinta de un objeto + +Las tareas de segmentaciรณn son รบtiles en vehรญculos autรณnomos para crear un mapa a nivel de pรญxeles del mundo que los rodea para que puedan navegar de manera segura alrededor de peatones y otros vehรญculos. Tambiรฉn es รบtil en imรกgenes mรฉdicas, donde la mayor granularidad de la tarea puede ayudar a identificar cรฉlulas anormales o caracterรญsticas de รณrganos. La segmentaciรณn de imรกgenes tambiรฉn se puede utilizar en comercio electrรณnico para probar virtualmente la ropa o crear experiencias de realidad aumentada superponiendo objetos en el mundo real a travรฉs de tu cรกmara. + +```py +>>> from transformers import pipeline + +>>> segmenter = pipeline(task="image-segmentation") +>>> preds = segmenter( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.9879, 'label': 'LABEL_184'} +{'score': 0.9973, 'label': 'snow'} +{'score': 0.9972, 'label': 'cat'} +``` + +### Estimaciรณn de profundidad + +La estimaciรณn de profundidad predice la distancia de cada pรญxel en una imagen desde la cรกmara. Esta tarea de visiรณn por computadora es especialmente importante para la comprensiรณn y reconstrucciรณn de escenas. Por ejemplo, en los vehรญculos autรณnomos, es necesario entender quรฉ tan lejos estรกn los objetos como peatones, seรฑales de trรกfico y otros vehรญculos para evitar obstรกculos y colisiones. La informaciรณn de profundidad tambiรฉn es รบtil para construir representaciones 3D a partir de imรกgenes 2D y se puede utilizar para crear representaciones 3D de alta calidad de estructuras biolรณgicas o edificios. + +Hay dos enfoques para la estimaciรณn de profundidad: + +* estรฉreo: las profundidades se estiman comparando dos imรกgenes de la misma escena desde รกngulos ligeramente diferentes +* monocular: las profundidades se estiman a partir de una sola imagen + +```py +>>> from transformers import pipeline + +>>> depth_estimator = pipeline(task="depth-estimation") +>>> preds = depth_estimator( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +``` + +## Procesamiento del lenguaje natural + +Las tareas de procesamiento del lenguaje natural (NLP, por sus siglas en inglรฉs) estรกn entre los tipos de tareas mรกs comunes porque el texto es una forma natural de comunicaciรณn para nosotros. Para convertir el texto en un formato reconocido por un modelo, es necesario tokenizarlo. Esto significa dividir una secuencia de texto en palabras o subpalabras separadas (tokens) y luego convertir estos tokens en nรบmeros. Como resultado, puedes representar una secuencia de texto como una secuencia de nรบmeros, y una vez que tienes una secuencia de nรบmeros, se puede ingresar a un modelo para resolver todo tipo de tareas de NLP. + +### Clasificaciรณn de texto + +Al igual que las tareas de clasificaciรณn en cualquier modalidad, la clasificaciรณn de texto etiqueta una secuencia de texto (puede ser a nivel de oraciรณn, pรกrrafo o documento) de un conjunto predefinido de clases. Hay muchas aplicaciones prรกcticas para la clasificaciรณn de texto, algunas de las cuales incluyen: + +* anรกlisis de sentimientos: etiquetar texto segรบn alguna polaridad como `positivo` o `negativo`, lo que puede informar y respaldar la toma de decisiones en campos como polรญtica, finanzas y marketing +* clasificaciรณn de contenido: etiquetar texto segรบn algรบn tema para ayudar a organizar y filtrar informaciรณn en noticias y feeds de redes sociales (`clima`, `deportes`, `finanzas`, etc.) + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="sentiment-analysis") +>>> preds = classifier("Hugging Face is the best thing since sliced bread!") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.9991, 'label': 'POSITIVE'}] +``` + +### Clasificaciรณn de tokens + +En cualquier tarea de NLP, el texto se procesa separando la secuencia de texto en palabras o subpalabras individuales. Estas se conocen como [tokens](glossary#token). La clasificaciรณn de tokens asigna a cada token una etiqueta de un conjunto predefinido de clases. + +Dos tipos comunes de clasificaciรณn de tokens son: + +* reconocimiento de entidades nombradas (NER, por sus siglas en inglรฉs): etiquetar un token segรบn una categorรญa de entidad como organizaciรณn, persona, ubicaciรณn o fecha. NER es especialmente popular en entornos biomรฉdicos, donde puede etiquetar genes, proteรญnas y nombres de medicamentos +* etiquetado de partes del discurso (POS, por sus siglas en inglรฉs): etiquetar un token segรบn su parte del discurso, como sustantivo, verbo o adjetivo. POS es รบtil para ayudar a los sistemas de traducciรณn a comprender cรณmo dos palabras idรฉnticas son gramaticalmente diferentes (por ejemplo, "corte" como sustantivo versus "corte" como verbo) + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="ner") +>>> preds = classifier("Hugging Face is a French company based in New York City.") +>>> preds = [ +... { +... "entity": pred["entity"], +... "score": round(pred["score"], 4), +... "index": pred["index"], +... "word": pred["word"], +... "start": pred["start"], +... "end": pred["end"], +... } +... for pred in preds +... ] +>>> print(*preds, sep="\n") +{'entity': 'I-ORG', 'score': 0.9968, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2} +{'entity': 'I-ORG', 'score': 0.9293, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7} +{'entity': 'I-ORG', 'score': 0.9763, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12} +{'entity': 'I-MISC', 'score': 0.9983, 'index': 6, 'word': 'French', 'start': 18, 'end': 24} +{'entity': 'I-LOC', 'score': 0.999, 'index': 10, 'word': 'New', 'start': 42, 'end': 45} +{'entity': 'I-LOC', 'score': 0.9987, 'index': 11, 'word': 'York', 'start': 46, 'end': 50} +{'entity': 'I-LOC', 'score': 0.9992, 'index': 12, 'word': 'City', 'start': 51, 'end': 55} +``` + +### Respuestas a preguntas + +Responder preguntas es otra tarea a nivel de tokens que devuelve una respuesta a una pregunta, a veces con contexto (dominio abierto) y otras veces sin contexto (dominio cerrado). Esta tarea ocurre cuando le preguntamos algo a un asistente virtual, como si un restaurante estรก abierto. Tambiรฉn puede proporcionar soporte al cliente o tรฉcnico y ayudar a los motores de bรบsqueda a recuperar la informaciรณn relevante que estรกs buscando. + +Hay dos tipos comunes de respuestas a preguntas: + +* extractivas: dada una pregunta y algรบn contexto, la respuesta es un fragmento de texto del contexto que el modelo debe extraer +* abstractivas: dada una pregunta y algรบn contexto, la respuesta se genera a partir del contexto; este enfoque lo maneja la [`Text2TextGenerationPipeline`] en lugar del [`QuestionAnsweringPipeline`] que se muestra a continuaciรณn + +```py +>>> from transformers import pipeline + +>>> question_answerer = pipeline(task="question-answering") +>>> preds = question_answerer( +... question="What is the name of the repository?", +... context="The name of the repository is huggingface/transformers", +... ) +>>> print( +... f"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}" +... ) +score: 0.9327, start: 30, end: 54, answer: huggingface/transformers +``` + +### Resumir + +Al resumir se crea una versiรณn mรกs corta de un texto mรกs largo mientras intenta preservar la mayor parte del significado del documento original. Resumir es una tarea de secuencia a secuencia; produce una secuencia de texto mรกs corta que la entrada. Hay muchos documentos de formato largo que se pueden resumir para ayudar a los lectores a comprender rรกpidamente los puntos principales. Proyectos de ley legislativos, documentos legales y financieros, patentes y artรญculos cientรญficos son algunos ejemplos de documentos que podrรญan resumirse para ahorrar tiempo a los lectores y servir como ayuda para la lectura. + +Al igual que en las respuestas a preguntas, hay dos tipos de resumen: + +* extractiva: identifica y extrae las oraciones mรกs importantes del texto original +* abstractiva: genera el resumen objetivo (que puede incluir nuevas palabras no presentes en el documento de entrada) a partir del texto original; el [`SummarizationPipeline`] utiliza el enfoque abstractivo + +```py +>>> from transformers import pipeline + +>>> summarizer = pipeline(task="summarization") +>>> summarizer( +... "In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. In the former task our best model outperforms even all previously reported ensembles." +... ) +[{'summary_text': ' The Transformer is the first sequence transduction model based entirely on attention . It replaces the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention . For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers .'}] +``` + +### Traducciรณn + +La traducciรณn convierte una secuencia de texto en un idioma a otro. Es importante para ayudar a personas de diferentes orรญgenes a comunicarse entre sรญ, traducir contenido para llegar a audiencias mรกs amplias e incluso ser una herramienta de aprendizaje para ayudar a las personas a aprender un nuevo idioma. Al igual que resumir, la traducciรณn es una tarea de secuencia a secuencia, lo que significa que el modelo recibe una secuencia de entrada y devuelve una secuencia de salida objetivo. + +En sus primeros dรญas, los modelos de traducciรณn eran principalmente monolingรผes, pero recientemente ha habido un creciente interรฉs en modelos multilingรผes que pueden traducir entre muchas combinaciones de idiomas. + +```py +>>> from transformers import pipeline + +>>> text = "translate English to French: Hugging Face is a community-based open-source platform for machine learning." +>>> translator = pipeline(task="translation", model="t5-small") +>>> translator(text) +[{'translation_text': "Hugging Face est une tribune communautaire de l'apprentissage des machines."}] +``` + +### Modelado de lenguaje + +El modelado de lenguaje es una tarea que predice una palabra en una secuencia de texto. Se ha vuelto una tarea de NLP muy popular porque un modelo de lenguaje preentrenado puede ser afinado para muchas otras tareas secundarias. รšltimamente, ha habido mucho interรฉs en modelos de lenguaje grandes (LLM, por sus siglas en inglรฉs) que demuestran aprendizaje de cero o con pocas muestras (zero- or few-shot learning). ยกEsto significa que el modelo puede resolver tareas para las cuales no fue entrenado explรญcitamente! Los modelos de lenguaje se pueden utilizar para generar texto fluido y convincente, aunque debes tener cuidado, ya que el texto no siempre puede ser preciso. + +Hay dos tipos de modelado de lenguaje: + +* causal: el objetivo del modelo es predecir el prรณximo token en una secuencia, y los tokens futuros estรกn enmascarados + + ```py + >>> from transformers import pipeline + + >>> prompt = "Hugging Face is a community-based open-source platform for machine learning." + >>> generator = pipeline(task="text-generation") + >>> generator(prompt) # doctest: +SKIP + ``` + +* enmascarado: el objetivo del modelo es predecir un token enmascarado en una secuencia con acceso completo a los tokens en la secuencia + + ```py + >>> text = "Hugging Face is a community-based open-source for machine learning." + >>> fill_mask = pipeline(task="fill-mask") + >>> preds = fill_mask(text, top_k=1) + >>> preds = [ + ... { + ... "score": round(pred["score"], 4), + ... "token": pred["token"], + ... "token_str": pred["token_str"], + ... "sequence": pred["sequence"], + ... } + ... for pred in preds + ... ] + >>> preds + [{'score': 0.2236, + 'token': 1761, + 'token_str': ' platform', + 'sequence': 'Hugging Face is a community-based open-source platform for machine learning.'}] + ``` + +## Multimodal + +Las tareas multimodales requieren que un modelo procese mรบltiples modalidades de datos (texto, imagen, audio, video) para resolver un problema particular. La descripciรณn de imรกgenes es un ejemplo de una tarea multimodal en la que el modelo toma una imagen como entrada y produce una secuencia de texto que describe la imagen o algunas propiedades de la imagen. + +Aunque los modelos multimodales trabajan con diferentes tipos de datos o modalidades, internamente, los pasos de preprocesamiento ayudan al modelo a convertir todos los tipos de datos en embeddings (vectores o listas de nรบmeros que contienen informaciรณn significativa sobre los datos). Para una tarea como la descripciรณn de imรกgenes, el modelo aprende las relaciones entre los embeddings de imรกgenes y los embeddings de texto. + +### Respuestas a preguntas de documentos + +Las respuestas a preguntas de documentos es una tarea que responde preguntas en lenguaje natural a partir de un documento. A diferencia de una tarea de respuestas a preguntas a nivel de token que toma texto como entrada, las respuestas a preguntas de documentos toman una imagen de un documento como entrada junto con una pregunta sobre el documento y devuelven una respuesta. Las respuestas a preguntas de documentos pueden usarse para analizar documentos estructurados y extraer informaciรณn clave de ellos. En el ejemplo a continuaciรณn, el monto total y el cambio debido se pueden extraer de un recibo. + +```py +>>> from transformers import pipeline +>>> from PIL import Image +>>> import requests + +>>> url = "https://huggingface.co/datasets/hf-internal-testing/example-documents/resolve/main/jpeg_images/2.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> doc_question_answerer = pipeline("document-question-answering", model="magorshunov/layoutlm-invoices") +>>> preds = doc_question_answerer( +... question="What is the total amount?", +... image=image, +... ) +>>> preds +[{'score': 0.8531, 'answer': '17,000', 'start': 4, 'end': 4}] +``` + +Con suerte, esta pรกgina te ha proporcionado mรกs informaciรณn de fondo sobre todos los tipos de tareas en cada modalidad y la importancia prรกctica de cada una. En la prรณxima [secciรณn](tasks_explained), aprenderรกs **cรณmo** ๐Ÿค— Transformers trabaja para resolver estas tareas. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/asr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/asr.md new file mode 100644 index 0000000000000000000000000000000000000000..d5bb614e70dabf78a39637538f73a7e3445d935b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/asr.md @@ -0,0 +1,366 @@ + + +# Reconocimiento automรกtico del habla + + + +El reconocimiento automรกtico del habla (ASR, por sus siglas en inglรฉs) convierte una seรฑal de habla en texto y mapea una secuencia de entradas de audio en salidas en forma de texto. Los asistentes virtuales como Siri y Alexa usan modelos de ASR para ayudar a sus usuarios todos los dรญas. De igual forma, hay muchas otras aplicaciones, como la transcripciรณn de contenidos en vivo y la toma automรกtica de notas durante reuniones. + +En esta guรญa te mostraremos como: + +1. Hacer fine-tuning al modelo [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) con el dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) para transcribir audio a texto. +2. Usar tu modelo ajustado para tareas de inferencia. + + + +Revisa la [pรกgina de la tarea](https://huggingface.co/tasks/automatic-speech-recognition) de reconocimiento automรกtico del habla para acceder a mรกs informaciรณn sobre los modelos, datasets y mรฉtricas asociados. + + + +Antes de comenzar, asegรบrate de haber instalado todas las librerรญas necesarias: + +```bash +pip install transformers datasets evaluate jiwer +``` + +Te aconsejamos iniciar sesiรณn con tu cuenta de Hugging Face para que puedas subir tu modelo y comartirlo con la comunidad. Cuando te sea solicitado, ingresa tu token para iniciar sesiรณn: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Cargar el dataset MInDS-14 + +Comencemos cargando un subconjunto mรกs pequeรฑo del dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) desde la biblioteca ๐Ÿค— Datasets. De esta forma, tendrรกs la oportunidad de experimentar y asegurarte de que todo funcione antes de invertir mรกs tiempo entrenando con el dataset entero. + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train[:100]") +``` +Divide la particiรณn `train` (entrenamiento) en una particiรณn de entrenamiento y una de prueba usando el mรฉtodo [`~Dataset.train_test_split`]: + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +Ahora รฉchale un vistazo al dataset: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 16 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 4 + }) +}) +``` + +Aunque el dataset contiene mucha informaciรณn รบtil, como los campos `lang_id` (identificador del lenguaje) y `english_transcription` (transcripciรณn al inglรฉs), en esta guรญa nos enfocaremos en los campos `audio` y `transcription`. Puedes quitar las otras columnas con el mรฉtodo [`~datasets.Dataset.remove_columns`]: + +```py +>>> minds = minds.remove_columns(["english_transcription", "intent_class", "lang_id"]) +``` + +Vuelve a echarle un vistazo al ejemplo: + +```py +>>> minds["train"][0] +{'audio': {'array': array([-0.00024414, 0. , 0. , ..., 0.00024414, + 0.00024414, 0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 8000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +Hay dos campos: + +- `audio`: un `array` (arreglo) unidimensional de la seรฑal de habla que debe ser invocado para cargar y re-muestrear el archivo de audio. +- `transcription`: el texto objetivo. + +## Preprocesamiento + +El siguiente paso es cargar un procesador Wav2Vec2 para procesar la seรฑal de audio: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base") +``` +El dataset MInDS-14 tiene una tasa de muestreo de 8000kHz (puedes encontrar esta informaciรณn en su [tarjeta de dataset](https://huggingface.co/datasets/PolyAI/minds14)), lo que significa que tendrรกs que re-muestrear el dataset a 16000kHz para poder usar el modelo Wav2Vec2 pre-entrenado: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([-2.38064706e-04, -1.58618059e-04, -5.43987835e-06, ..., + 2.78103951e-04, 2.38446111e-04, 1.18740834e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 16000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +Como puedes ver en el campo `transcription`, el texto contiene una mezcla de carรกcteres en mayรบsculas y en minรบsculas. El tokenizer Wav2Vec2 fue entrenado รบnicamente con carรกcteres en mayรบsculas, asรญ que tendrรกs que asegurarte de que el texto se ajuste al vocabulario del tokenizer: + +```py +>>> def uppercase(example): +... return {"transcription": example["transcription"].upper()} + + +>>> minds = minds.map(uppercase) +``` + +Ahora vamos a crear una funciรณn de preprocesamiento que: + +1. Invoque la columna `audio` para cargar y re-muestrear el archivo de audio. +2. Extraiga el campo `input_values` (valores de entrada) del archivo de audio y haga la tokenizaciรณn de la columna `transcription` con el procesador. + +```py +>>> def prepare_dataset(batch): +... audio = batch["audio"] +... batch = processor(audio["array"], sampling_rate=audio["sampling_rate"], text=batch["transcription"]) +... batch["input_length"] = len(batch["input_values"][0]) +... return batch +``` + +Para aplicar la funciรณn de preprocesamiento a todo el dataset, puedes usar la funciรณn [`~datasets.Dataset.map`] de ๐Ÿค— Datasets. Para acelerar la funciรณn `map` puedes incrementar el nรบmero de procesos con el parรกmetro `num_proc`. Quita las columnas que no necesites con el mรฉtodo [`~datasets.Dataset.remove_columns`]: + +```py +>>> encoded_minds = minds.map(prepare_dataset, remove_columns=minds.column_names["train"], num_proc=4) +``` + +๐Ÿค— Transformers no tiene un collator de datos para la tarea de ASR, asรญ que tendrรกs que adaptar el [`DataCollatorWithPadding`] para crear un lote de ejemplos. El collator tambiรฉn le aplicarรก padding dinรกmico a tu texto y etiquetas para que tengan la longitud del elemento mรกs largo en su lote (en vez de la mayor longitud en el dataset entero), de forma que todas las muestras tengan una longitud uniforme. Aunque es posible hacerle padding a tu texto con el `tokenizer` haciendo `padding=True`, el padding dinรกmico es mรกs eficiente. + +A diferencia de otros collators de datos, este tiene que aplicarle un mรฉtodo de padding distinto a los campos `input_values` (valores de entrada) y `labels` (etiquetas): + +```py +>>> import torch + +>>> from dataclasses import dataclass, field +>>> from typing import Any, Dict, List, Optional, Union + + +>>> @dataclass +... class DataCollatorCTCWithPadding: +... processor: AutoProcessor +... padding: Union[bool, str] = "longest" + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... # particiona las entradas y las etiquetas ya que tienen que tener longitudes distintas y +... # requieren mรฉtodos de padding diferentes +... input_features = [{"input_values": feature["input_values"][0]} for feature in features] +... label_features = [{"input_ids": feature["labels"]} for feature in features] + +... batch = self.processor.pad(input_features, padding=self.padding, return_tensors="pt") + +... labels_batch = self.processor.pad(labels=label_features, padding=self.padding, return_tensors="pt") + +... # remplaza el padding con -100 para ignorar la pรฉrdida de forma correcta +... labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) + +... batch["labels"] = labels + +... return batch +``` + +Ahora puedes instanciar tu `DataCollatorForCTCWithPadding`: + +```py +>>> data_collator = DataCollatorCTCWithPadding(processor=processor, padding="longest") +``` + +## Evaluaciรณn + +A menudo es รบtil incluir una mรฉtrica durante el entrenamiento para evaluar el rendimiento de tu modelo. Puedes cargar un mรฉtodo de evaluaciรณn rรกpidamente con la biblioteca ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index). Para esta tarea, puedes usar la mรฉtrica de [tasa de error por palabra](https://huggingface.co/spaces/evaluate-metric/wer) (WER, por sus siglas en inglรฉs). Puedes ver la [guรญa rรกpida](https://huggingface.co/docs/evaluate/a_quick_tour) de ๐Ÿค— Evaluate para aprender mรกs acerca de cรณmo cargar y computar una mรฉtrica. + +```py +>>> import evaluate + +>>> wer = evaluate.load("wer") +``` + +Ahora crea una funciรณn que le pase tus predicciones y etiquetas a [`~evaluate.EvaluationModule.compute`] para calcular la WER: + +```py +>>> import numpy as np + + +>>> def compute_metrics(pred): +... pred_logits = pred.predictions +... pred_ids = np.argmax(pred_logits, axis=-1) + +... pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id + +... pred_str = processor.batch_decode(pred_ids) +... label_str = processor.batch_decode(pred.label_ids, group_tokens=False) + +... wer = wer.compute(predictions=pred_str, references=label_str) + +... return {"wer": wer} +``` + +Ahora tu funciรณn `compute_metrics` (computar mรฉtricas) estรก lista y podrรกs usarla cuando estรฉs preparando tu entrenamiento. + +## Entrenamiento + + + + + +Si no tienes experiencia haciรฉndole fine-tuning a un modelo con el [`Trainer`], ยกรฉchale un vistazo al tutorial bรกsico [aquรญ](../training#train-with-pytorch-trainer)! + + + +ยกYa puedes empezar a entrenar tu modelo! Para ello, carga Wav2Vec2 con [`AutoModelForCTC`]. Especifica la reducciรณn que quieres aplicar con el parรกmetro `ctc_loss_reduction`. A menudo, es mejor usar el promedio en lugar de la sumatoria que se hace por defecto. + +```py +>>> from transformers import AutoModelForCTC, TrainingArguments, Trainer + +>>> model = AutoModelForCTC.from_pretrained( +... "facebook/wav2vec2-base", +... ctc_loss_reduction="mean", +... pad_token_id=processor.tokenizer.pad_token_id, +... ) +``` +En este punto, solo quedan tres pasos: + +1. Define tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. El รบnico parรกmetro obligatorio es `output_dir` (carpeta de salida), el cual especifica dรณnde guardar tu modelo. Puedes subir este modelo al Hub haciendo `push_to_hub=True` (debes haber iniciado sesiรณn en Hugging Face para subir tu modelo). Al final de cada รฉpoca, el [`Trainer`] evaluarรก la WER y guardarรก el punto de control del entrenamiento. +2. Pรกsale los argumentos del entrenamiento al [`Trainer`] junto con el modelo, el dataset, el tokenizer, el collator de datos y la funciรณn `compute_metrics`. +3. Llama el mรฉtodo [`~Trainer.train`] para hacerle fine-tuning a tu modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_asr_mind_model", +... per_device_train_batch_size=8, +... gradient_accumulation_steps=2, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=2000, +... gradient_checkpointing=True, +... fp16=True, +... group_by_length=True, +... eval_strategy="steps", +... per_device_eval_batch_size=8, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... load_best_model_at_end=True, +... metric_for_best_model="wer", +... greater_is_better=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=processor.feature_extractor, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Una vez que el entrenamiento haya sido completado, comparte tu modelo en el Hub con el mรฉtodo [`~transformers.Trainer.push_to_hub`] para que todo el mundo pueda usar tu modelo: + +```py +>>> trainer.push_to_hub() +``` + + + + + +Para ver un ejemplo mรกs detallado de cรณmo hacerle fine-tuning a un modelo para reconocimiento automรกtico del habla, รฉchale un vistazo a esta [entrada de blog](https://huggingface.co/blog/fine-tune-wav2vec2-english) para ASR en inglรฉs y a esta [entrada](https://huggingface.co/blog/fine-tune-xlsr-wav2vec2) para ASR multilingรผe. + + + +## Inferencia + +ยกGenial, ahora que le has hecho fine-tuning a un modelo, puedes usarlo para inferencia! + +Carga el archivo de audio sobre el cual quieras correr la inferencia. ยกRecuerda re-muestrar la tasa de muestreo del archivo de audio para que sea la misma del modelo si es necesario! + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +La manera mรกs simple de probar tu modelo para hacer inferencia es usarlo en un [`pipeline`]. Puedes instanciar un `pipeline` para reconocimiento automรกtico del habla con tu modelo y pasarle tu archivo de audio: + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") +>>> transcriber(audio_file) +{'text': 'I WOUD LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'} +``` + + + +La transcripciรณn es decente, pero podrรญa ser mejor. ยกIntenta hacerle fine-tuning a tu modelo con mรกs ejemplos para obtener resultados aรบn mejores! + + + +Tambiรฉn puedes replicar de forma manual los resultados del `pipeline` si lo deseas: + + + +Carga un procesador para preprocesar el archivo de audio y la transcripciรณn y devuelve el `input` como un tensor de PyTorch: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +Pรกsale tus entradas al modelo y devuelve los logits: + +```py +>>> from transformers import AutoModelForCTC + +>>> model = AutoModelForCTC.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Obtรฉn los identificadores de los tokens con mayor probabilidad en las predicciones y usa el procesador para decodificarlos y transformarlos en texto: + +```py +>>> import torch + +>>> predicted_ids = torch.argmax(logits, dim=-1) +>>> transcription = processor.batch_decode(predicted_ids) +>>> transcription +['I WOUL LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/audio_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/audio_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..69f180ba68baf8bc11c656e176a83c61fce270a9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/audio_classification.md @@ -0,0 +1,323 @@ + + +# Clasificaciรณn de audio + +[[open-in-colab]] + + + +Clasificaciรณn de audio - al igual que con texto โ€” asigna una etiqueta de clase como salida desde las entradas de datos. La diferencia รบnica es en vez de entrada de texto, tiene formas de onda de audio. Algunas aplicaciones prรกcticas de clasificaciรณn incluye identificar la intenciรณn del hablante, identificaciรณn del idioma, y la clasificaciรณn de animales por sus sonidos. + +En esta guรญa te mostraremos como: + +1. Hacer fine-tuning al modelo [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) en el dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) para clasificar la intenciรณn del hablante. +2. Usar tu modelo ajustado para tareas de inferencia. + + + + +Consulta la [pรกgina de la tarea](https://huggingface.co/tasks/audio-classification) de clasificaciรณn de audio para acceder a mรกs informaciรณn sobre los modelos, datasets, y mรฉtricas asociados. + + + +Antes de comenzar, asegรบrate de haber instalado todas las librerรญas necesarias: + +```bash +pip install transformers datasets evaluate +``` + +Te aconsejamos iniciar sesiรณn con tu cuenta de Hugging Face para que puedas subir tu modelo y compartirlo con la comunidad. Cuando se te solicite, ingresa tu token para iniciar sesiรณn: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Carga el dataset MInDS-14 + +Comencemos cargando el dataset MInDS-14 con la biblioteca de ๐Ÿค— Datasets: + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +Divide el conjunto de `train` (entrenamiento) en un conjunto de entrenamiento y prueba mas pequeรฑo con el mรฉtodo [`~datasets.Dataset.train_test_split`]. De esta forma, tendrรกs la oportunidad para experimentar y asegรบrate de que todo funciรณne antes de invertir mรกs tiempo entrenando con el dataset entero. + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +Ahora รฉchale un vistazo al dataset: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 450 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 113 + }) +}) +``` + +Aunque el dataset contiene mucha informaciรณn รบtil, como los campos `land_id` (identificador del lenguaje) y `english_transcription` (transcripciรณn al inglรฉs), en esta guรญa nos enfocaremos en los campos `audio` y `intent_class` (clase de intenciรณn). Puedes quitar las otras columnas con cel mรฉtodo [`~datasets.Dataset.remove_columns`]: + +```py +>>> minds = minds.remove_columns(["path", "transcription", "english_transcription", "lang_id"]) +``` + +Aquรญ estรก un ejemplo: + +```py +>>> minds["train"][0] +{'audio': {'array': array([ 0. , 0. , 0. , ..., -0.00048828, + -0.00024414, -0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 8000}, + 'intent_class': 2} +``` + +Hay dos campos: + +- `audio`: un `array` (arreglo) unidimensional de la seรฑal de voz que se obtiene al cargar y volver a muestrear el archivo de audio. +- `intent_class`: representa el identificador de la clase de la intenciรณn del hablante. + +Crea un diccionario que asigne el nombre de la etiqueta a un nรบmero entero y viceversa para facilitar la obtenciรณn del nombre de la etiqueta a partir de su identificador. + +```py +>>> labels = minds["train"].features["intent_class"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +Ahora puedes convertir el identificador de la etiqueta a un nombre de etiqueta: + +```py +>>> id2label[str(2)] +'app_error' +``` + +## Preprocesamiento + +Seguidamente carga el feature extractor (funciรณn de extracciรณn de caracterรญsticas) de Wav2Vec para procesar la seรฑal de audio: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +El dataset MInDS-14 tiene una tasa de muestreo de 8kHz (puedes encontrar esta informaciรณn en su [tarjeta de dataset](https://huggingface.co/datasets/PolyAI/minds14)), lo que significa que tendrรกs que volver a muestrear el dataset a 16kHZ para poder usar el modelo Wav2Vec2 preentranado: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([ 2.2098757e-05, 4.6582241e-05, -2.2803260e-05, ..., + -2.8419291e-04, -2.3305941e-04, -1.1425107e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 16000}, + 'intent_class': 2} +``` + +Ahora vamos a crear una funciรณn de preprocesamiento: + +1. Invoque la columna `audio` para cargar, y si es necesario, volver a muestrear al archivo de audio. +2. Comprueba si la frecuencia de muestreo del archivo de audio coincide con la frecuencia de muestreo de los datos de audio con los que se entrenรณ previamente el modelo. Puedes encontrar esta informaciรณn en la [tarjeta de modelo](https://huggingface.co/facebook/wav2vec2-base) de Wav2Vec2. +3. Establece una longitud de entrada mรกxima para agrupar entradas mรกs largas sin truncarlas. + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, sampling_rate=feature_extractor.sampling_rate, max_length=16000, truncation=True +... ) +... return inputs +``` + +Para aplicar la funciรณn de preprocesamiento a todo el dataset, puedes usar la funciรณn [`~datasets.Dataset.map`] de ๐Ÿค— Datasets. Acelera la funciรณn `map` haciendo `batched=True` para procesar varios elementos del dataset a la vez. Quitas las columnas que no necesites con el mรฉtodo `[~datasets.Dataset.remove_columns]` y cambia el nombre de `intent_class` a `label`, como requiere el modelo. + +```py +>>> encoded_minds = minds.map(preprocess_function, remove_columns="audio", batched=True) +>>> encoded_minds = encoded_minds.rename_column("intent_class", "label") +``` + +## Evaluaciรณn +A menudo es รบtil incluir una mรฉtrica durante el entrenamiento para evaluar el rendimiento de tu modelo. Puedes cargar un mรฉtodo de evaluaciรณn rapidamente con la biblioteca de ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index). Para esta tarea, puedes usar la mรฉtrica de [exactitud](https://huggingface.co/spaces/evaluate-metric/accuracy) (accuracy). Puedes ver la [guรญa rรกpida](https://huggingface.co/docs/evaluate/a_quick_tour) de ๐Ÿค— Evaluate para aprender mรกs de cรณmo cargar y computar una mรฉtrica: + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +Ahora crea una funciรณn que le pase tus predicciones y etiquetas a [`~evaluate.EvaluationModule.compute`] para calcular la exactitud: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions = np.argmax(eval_pred.predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +Ahora tu funciรณn `compute_metrics` (computar mรฉtricas) estรก lista y podrรกs usarla cuando estรฉs preparando tu entrenamiento. + +## Entrenamiento + + + + + +ยกSi no tienes experiencia haciรฉndo *fine-tuning* a un modelo con el [`Trainer`], รฉchale un vistazo al tutorial bรกsico [aquรญ](../training#train-with-pytorch-trainer)! + + + +ยกYa puedes empezar a entrenar tu modelo! Carga Wav2Vec2 con [`AutoModelForAudioClassification`] junto con el especifica el nรบmero de etiquetas, y pasa al modelo los *mappings* entre el nรบmero entero de etiqueta y la clase de etiqueta. + +```py +>>> from transformers import AutoModelForAudioClassification, TrainingArguments, Trainer + +>>> num_labels = len(id2label) +>>> model = AutoModelForAudioClassification.from_pretrained( +... "facebook/wav2vec2-base", num_labels=num_labels, label2id=label2id, id2label=id2label +... ) +``` + +Al llegar a este punto, solo quedan tres pasos: + +1. Define tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. El รบnico parรกmetro obligatorio es `output_dir` (carpeta de salida), el cual especifica dรณnde guardar tu modelo. Puedes subir este modelo al Hub haciendo `push_to_hub=True` (debes haber iniciado sesiรณn en Hugging Face para subir tu modelo). Al final de cada รฉpoca, el [`Trainer`] evaluarรก la exactitud y guardarรก el punto de control del entrenamiento. +2. Pรกsale los argumentos del entrenamiento al [`Trainer`] junto con el modelo, el dataset, el tokenizer, el data collator y la funciรณn `compute_metrics`. +3. Llama el mรฉtodo [`~Trainer.train`] para hacerle fine-tuning a tu modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_mind_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=3e-5, +... per_device_train_batch_size=32, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=32, +... num_train_epochs=10, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=feature_extractor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +Una vez que el entrenamiento haya sido completado, comparte tu modelo en el Hub con el mรฉtodo [`~transformers.Trainer.push_to_hub`] para que todo el mundo puede usar tu modelo. + +```py +>>> trainer.push_to_hub() +``` + + + + + +Para ver un ejemplo mรกs detallado de comรณ hacerle fine-tuning a un modelo para clasificaciรณn, รฉchale un vistazo al correspondiente [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb). + + + +## Inference + +ยกGenial, ahora que le has hecho *fine-tuned* a un modelo, puedes usarlo para hacer inferencia! + +Carga el archivo de audio para hacer inferencia. Recuerda volver a muestrear la tasa de muestreo del archivo de audio para que sea la misma del modelo si es necesario. + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +La manera mรกs simple de probar tu modelo para hacer inferencia es usarlo en un [`pipeline`]. Puedes instanciar un `pipeline` para clasificaciรณn de audio con tu modelo y pasarle tu archivo de audio: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("audio-classification", model="stevhliu/my_awesome_minds_model") +>>> classifier(audio_file) +[ + {'score': 0.09766869246959686, 'label': 'cash_deposit'}, + {'score': 0.07998877018690109, 'label': 'app_error'}, + {'score': 0.0781070664525032, 'label': 'joint_account'}, + {'score': 0.07667109370231628, 'label': 'pay_bill'}, + {'score': 0.0755252093076706, 'label': 'balance'} +] +``` + +Tambiรฉn puedes replicar de forma manual los resultados del `pipeline` si lo deseas: + + + +Carga el feature extractor para preprocesar el archivo de audio y devuelve el `input` como un tensor de PyTorch: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("stevhliu/my_awesome_minds_model") +>>> inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +Pรกsale tus entradas al modelo y devuelve los logits: + +```py +>>> from transformers import AutoModelForAudioClassification + +>>> model = AutoModelForAudioClassification.from_pretrained("stevhliu/my_awesome_minds_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +Obtรฉn los identificadores de los clases con mayor probabilidad y usa el *mapping* `id2label` del modelo para convertirle a una etiqueta: + +```py +>>> import torch + +>>> predicted_class_ids = torch.argmax(logits).item() +>>> predicted_label = model.config.id2label[predicted_class_ids] +>>> predicted_label +'cash_deposit' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_captioning.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_captioning.md new file mode 100644 index 0000000000000000000000000000000000000000..620dcec1bfbd1c719f6e3f0929015a60aa240fa4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_captioning.md @@ -0,0 +1,266 @@ + + +# Subtรญtulos de Imรกgenes + +[[open-in-colab]] + +Los subtรญtulos de imรกgenes es la tarea de predecir un subtรญtulo para una imagen dada. Las aplicaciones comunes en el mundo real incluyen +ayudar a personas con discapacidad visual que les puede ayudar a navegar a travรฉs de diferentes situaciones. Por lo tanto, los subtรญtulos de imรกgenes +ayuda a mejorar la accesibilidad del contenido para las personas describiรฉndoles imรกgenes. + +Esta guรญa te mostrarรก cรณmo: + +* Ajustar un modelo de subtรญtulos de imรกgenes. +* Usar el modelo ajustado para inferencia. + +Antes de comenzar, asegรบrate de tener todas las bibliotecas necesarias instaladas: + +```bash +pip install transformers datasets evaluate -q +pip install jiwer -q +``` + +Te animamos a que inicies sesiรณn en tu cuenta de Hugging Face para que puedas subir y compartir tu modelo con la comunidad. Cuando se te solicite, ingresa tu token para iniciar sesiรณn: + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +## Cargar el conjunto de datos de subtรญtulos BLIP de Pokรฉmon + +Utiliza la biblioteca ๐Ÿค— Dataset para cargar un conjunto de datos que consiste en pares {image-caption}. Para crear tu propio conjunto de datos de subtรญtulos de imรกgenes +en PyTorch, puedes seguir [este cuaderno](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/GIT/Fine_tune_GIT_on_an_image_captioning_dataset.ipynb). + +```python +from datasets import load_dataset + +ds = load_dataset("lambdalabs/pokemon-blip-captions") +ds +``` +```bash +DatasetDict({ + train: Dataset({ + features: ['image', 'text'], + num_rows: 833 + }) +}) +``` + +El conjunto de datos tiene dos caracterรญsticas, `image` y `text`. + + + +Muchos conjuntos de datos de subtรญtulos de imรกgenes contienen mรบltiples subtรญtulos por imagen. En esos casos, una estrategia comรบn es muestrear aleatoriamente un subtรญtulo entre los disponibles durante el entrenamiento. + + + +Divide el conjunto de entrenamiento del conjunto de datos en un conjunto de entrenamiento y de prueba con el mรฉtodo [`~datasets.Dataset.train_test_split`]: + +```python +ds = ds["train"].train_test_split(test_size=0.1) +train_ds = ds["train"] +test_ds = ds["test"] +``` + +Vamos a visualizar un par de muestras del conjunto de entrenamiento. + +```python +from textwrap import wrap +import matplotlib.pyplot as plt +import numpy as np + + +def plot_images(images, captions): + plt.figure(figsize=(20, 20)) + for i in range(len(images)): + ax = plt.subplot(1, len(images), i + 1) + caption = captions[i] + caption = "\n".join(wrap(caption, 12)) + plt.title(caption) + plt.imshow(images[i]) + plt.axis("off") + + +sample_images_to_visualize = [np.array(train_ds[i]["image"]) for i in range(5)] +sample_captions = [train_ds[i]["text"] for i in range(5)] +plot_images(sample_images_to_visualize, sample_captions) +``` + +
+ Sample training images +
+ +## Preprocesar el conjunto de datos + +Dado que el conjunto de datos tiene dos modalidades (imagen y texto), el proceso de preprocesamiento preprocesarรก las imรกgenes y los subtรญtulos. + +Para hacerlo, carga la clase de procesador asociada con el modelo que estรกs a punto de ajustar. + +```python +from transformers import AutoProcessor + +checkpoint = "microsoft/git-base" +processor = AutoProcessor.from_pretrained(checkpoint) +``` + +El procesador preprocesarรก internamente la imagen (lo que incluye el cambio de tamaรฑo y la escala de pรญxeles) y tokenizarรก el subtรญtulo. + +```python +def transforms(example_batch): + images = [x for x in example_batch["image"]] + captions = [x for x in example_batch["text"]] + inputs = processor(images=images, text=captions, padding="max_length") + inputs.update({"labels": inputs["input_ids"]}) + return inputs + + +train_ds.set_transform(transforms) +test_ds.set_transform(transforms) +``` + +Con el conjunto de datos listo, ahora puedes configurar el modelo para el ajuste fino. + +## Cargar un modelo base + +Carga ["microsoft/git-base"](https://huggingface.co/microsoft/git-base) en un objeto [`AutoModelForCausalLM`](https://huggingface.co/docs/transformers/model_doc/auto#transformers.AutoModelForCausalLM). + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained(checkpoint) +``` + +## Evaluar + +Los modelos de subtรญtulos de imรกgenes se evalรบan tรญpicamente con el [Rouge Score](https://huggingface.co/spaces/evaluate-metric/rouge) o Tasa de Error de Palabra ([Word Error Rate](https://huggingface.co/spaces/evaluate-metric/wer), por sus siglas en inglรฉs). Para esta guรญa, utilizarรกs la Tasa de Error de Palabra (WER). + +Usamos la biblioteca ๐Ÿค— Evaluate para hacerlo. Para conocer las limitaciones potenciales y otros problemas del WER, consulta [esta guรญa](https://huggingface.co/spaces/evaluate-metric/wer). + +```python +from evaluate import load +import torch + +wer = load("wer") + + +def compute_metrics(eval_pred): + logits, labels = eval_pred + predicted = logits.argmax(-1) + decoded_labels = processor.batch_decode(labels, skip_special_tokens=True) + decoded_predictions = processor.batch_decode(predicted, skip_special_tokens=True) + wer_score = wer.compute(predictions=decoded_predictions, references=decoded_labels) + return {"wer_score": wer_score} +``` + +## ยกEntrenar! + +Ahora, estรกs listo para comenzar a ajustar el modelo. Utilizarรกs el ๐Ÿค— [`Trainer`] para esto. + +Primero, define los argumentos de entrenamiento usando [`TrainingArguments`]. + +```python +from transformers import TrainingArguments, Trainer + +model_name = checkpoint.split("/")[1] + +training_args = TrainingArguments( + output_dir=f"{model_name}-pokemon", + learning_rate=5e-5, + num_train_epochs=50, + fp16=True, + per_device_train_batch_size=32, + per_device_eval_batch_size=32, + gradient_accumulation_steps=2, + save_total_limit=3, + eval_strategy="steps", + eval_steps=50, + save_strategy="steps", + save_steps=50, + logging_steps=50, + remove_unused_columns=False, + push_to_hub=True, + label_names=["labels"], + load_best_model_at_end=True, +) +``` + +Luego pรกsalos junto con los conjuntos de datos y el modelo al ๐Ÿค— Trainer. + +```python +trainer = Trainer( + model=model, + args=training_args, + train_dataset=train_ds, + eval_dataset=test_ds, + compute_metrics=compute_metrics, +) +``` + +Para comenzar el entrenamiento, simplemente llama a [`~Trainer.train`] en el objeto [`Trainer`]. + +```python +trainer.train() +``` + +Deberรญas ver cรณmo disminuye suavemente la pรฉrdida de entrenamiento a medida que avanza el entrenamiento. + +Una vez completado el entrenamiento, comparte tu modelo en el Hub con el mรฉtodo [`~Trainer.push_to_hub`] para que todos puedan usar tu modelo: + +```python +trainer.push_to_hub() +``` + +## Inferencia + +Toma una imagen de muestra de test_ds para probar el modelo. + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png" +image = Image.open(requests.get(url, stream=True).raw) +image +``` + +
+ Test image +
+ +Prepara la imagen para el modelo. + +```python +device = "cuda" if torch.cuda.is_available() else "cpu" + +inputs = processor(images=image, return_tensors="pt").to(device) +pixel_values = inputs.pixel_values +``` + +Llama a [`generate`] y decodifica las predicciones. + +```python +generated_ids = model.generate(pixel_values=pixel_values, max_length=50) +generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] +print(generated_caption) +``` +```bash +a drawing of a pink and blue pokemon +``` + +ยกParece que el modelo ajustado generรณ un subtรญtulo bastante bueno! \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..1bea46884202fe4ac5e49f50a2ffa4d56bdffc5c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/image_classification.md @@ -0,0 +1,173 @@ + + +# Clasificaciรณn de imรกgenes + + + +La clasificaciรณn de imรกgenes asigna una etiqueta o clase a una imagen. A diferencia de la clasificaciรณn de texto o audio, las entradas son los valores de los pรญxeles que representan una imagen. La clasificaciรณn de imรกgenes tiene muchos usos, como la detecciรณn de daรฑos tras una catรกstrofe, el control de la salud de los cultivos o la bรบsqueda de signos de enfermedad en imรกgenes mรฉdicas. + +Esta guรญa te mostrarรก como hacer fine-tune al [ViT](https://huggingface.co/docs/transformers/v4.16.2/en/model_doc/vit) en el dataset [Food-101](https://huggingface.co/datasets/food101) para clasificar un alimento en una imagen. + + + +Consulta la [pรกgina de la tarea](https://huggingface.co/tasks/audio-classification) de clasificaciรณn de imรกgenes para obtener mรกs informaciรณn sobre sus modelos, datasets y mรฉtricas asociadas. + + + +## Carga el dataset Food-101 + +Carga solo las primeras 5000 imรกgenes del dataset Food-101 de la biblioteca ๐Ÿค— de Datasets ya que es bastante grande: + +```py +>>> from datasets import load_dataset + +>>> food = load_dataset("food101", split="train[:5000]") +``` + +Divide el dataset en un train y un test set: + +```py +>>> food = food.train_test_split(test_size=0.2) +``` + +A continuaciรณn, observa un ejemplo: + +```py +>>> food["train"][0] +{'image': , + 'label': 79} +``` + +El campo `image` contiene una imagen PIL, y cada `label` es un nรบmero entero que representa una clase. Crea un diccionario que asigne un nombre de label a un entero y viceversa. El mapeo ayudarรก al modelo a recuperar el nombre de label a partir del nรบmero de la misma: + +```py +>>> labels = food["train"].features["label"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +Ahora puedes convertir el nรบmero de label en un nombre de label para obtener mรกs informaciรณn: + +```py +>>> id2label[str(79)] +'prime_rib' +``` + +Cada clase de alimento - o label - corresponde a un nรบmero; `79` indica una costilla de primera en el ejemplo anterior. + +## Preprocesa + +Carga el image processor de ViT para procesar la imagen en un tensor: + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k") +``` + +Aplica varias transformaciones de imagen al dataset para hacer el modelo mรกs robusto contra el overfitting. En este caso se utilizarรก el mรณdulo [`transforms`](https://pytorch.org/vision/stable/transforms.html) de torchvision. Recorta una parte aleatoria de la imagen, cambia su tamaรฑo y normalรญzala con la media y la desviaciรณn estรกndar de la imagen: + +```py +>>> from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor + +>>> normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std) +>>> _transforms = Compose([RandomResizedCrop(image_processor.size["height"]), ToTensor(), normalize]) +``` + +Crea una funciรณn de preprocesamiento que aplique las transformaciones y devuelva los `pixel_values` - los inputs al modelo - de la imagen: + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(img.convert("RGB")) for img in examples["image"]] +... del examples["image"] +... return examples +``` + +Utiliza el mรฉtodo [`with_transform`](https://huggingface.co/docs/datasets/package_reference/main_classes?#datasets.Dataset.with_transform) de ๐Ÿค— Dataset para aplicar las transformaciones sobre todo el dataset. Las transformaciones se aplican sobre la marcha cuando se carga un elemento del dataset: + +```py +>>> food = food.with_transform(transforms) +``` + +Utiliza [`DefaultDataCollator`] para crear un batch de ejemplos. A diferencia de otros data collators en ๐Ÿค— Transformers, el DefaultDataCollator no aplica un preprocesamiento adicional como el padding. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + +## Entrena +Carga ViT con [`AutoModelForImageClassification`]. Especifica el nรบmero de labels, y pasa al modelo el mapping entre el nรบmero de label y la clase de label: + +```py +>>> from transformers import AutoModelForImageClassification, TrainingArguments, Trainer + +>>> model = AutoModelForImageClassification.from_pretrained( +... "google/vit-base-patch16-224-in21k", +... num_labels=len(labels), +... id2label=id2label, +... label2id=label2id, +... ) +``` + + + +Si no estรกs familiarizado con el fine-tuning de un modelo con el [`Trainer`], echa un vistazo al tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +Al llegar a este punto, solo quedan tres pasos: + +1. Define tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. Es importante que no elimines las columnas que no se utilicen, ya que esto harรก que desaparezca la columna `image`. Sin la columna `image` no puedes crear `pixel_values`. Establece `remove_unused_columns=False` para evitar este comportamiento. +2. Pasa los training arguments al [`Trainer`] junto con el modelo, los datasets, tokenizer y data collator. +3. Llama [`~Trainer.train`] para hacer fine-tune de tu modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="./results", +... per_device_train_batch_size=16, +... eval_strategy="steps", +... num_train_epochs=4, +... fp16=True, +... save_steps=100, +... eval_steps=100, +... logging_steps=10, +... learning_rate=2e-4, +... save_total_limit=2, +... remove_unused_columns=False, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=food["train"], +... eval_dataset=food["test"], +... processing_class=image_processor, +... ) + +>>> trainer.train() +``` + + + +Para ver un ejemplo mรกs a profundidad de cรณmo hacer fine-tune a un modelo para clasificaciรณn de imรกgenes, echa un vistazo al correspondiente [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb). + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..9516876a00633e238f91507413acd0b4e6b5387a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/language_modeling.md @@ -0,0 +1,421 @@ + + +# Modelado de lenguaje + +El modelado de lenguaje predice palabras en un enunciado. Hay dos formas de modelado de lenguaje. + + + +El modelado de lenguaje causal predice el siguiente token en una secuencia de tokens, y el modelo solo puede considerar los tokens a la izquierda. + + + +El modelado de lenguaje por enmascaramiento predice un token enmascarado en una secuencia, y el modelo puede considerar los tokens bidireccionalmente. + +Esta guรญa te mostrarรก cรณmo realizar fine-tuning [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) para modelos de lenguaje causales y [DistilRoBERTa](https://huggingface.co/distilbert/distilroberta-base) para modelos de lenguaje por enmascaramiento en el [r/askscience](https://www.reddit.com/r/askscience/) subdataset [ELI5](https://huggingface.co/datasets/eli5). + + + +Mira la [pรกgina de tarea](https://huggingface.co/tasks/text-generation) para generaciรณn de texto y la [pรกgina de tarea](https://huggingface.co/tasks/fill-mask) para modelos de lenguajes por enmascaramiento para obtener mรกs informaciรณn sobre los modelos, datasets, y mรฉtricas asociadas. + + + +## Carga el dataset ELI5 + +Carga solo los primeros 5000 registros desde la biblioteca ๐Ÿค— Datasets, dado que es bastante grande: + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5", split="train_asks[:5000]") +``` + +Divide este dataset en subdatasets para el entrenamiento y el test: + +```py +eli5 = eli5.train_test_split(test_size=0.2) +``` + +Luego observa un ejemplo: + +```py +>>> eli5["train"][0] +{'answers': {'a_id': ['c3d1aib', 'c3d4lya'], + 'score': [6, 3], + 'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]}, + 'answers_urls': {'url': []}, + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']}, + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls': {'url': []}} +``` + +Observa que `text` es un subcampo anidado dentro del diccionario `answers`. Cuando preproceses el dataset, deberรกs extraer el subcampo `text` en una columna aparte. + +## Preprocesamiento + + + +Para modelados de lenguaje causales carga el tokenizador DistilGPT2 para procesar el subcampo `text`: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") +``` + + + +Para modelados de lenguaje por enmascaramiento carga el tokenizador DistilRoBERTa, en lugar de DistilGPT2: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilroberta-base") +``` + +Extrae el subcampo `text` desde su estructura anidado con el mรฉtodo [`flatten`](https://huggingface.co/docs/datasets/process#flatten): + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'answers.a_id': ['c3d1aib', 'c3d4lya'], + 'answers.score': [6, 3], + 'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"], + 'answers_urls.url': [], + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'], + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls.url': []} +``` + +Cada subcampo es ahora una columna separada, como lo indica el prefijo `answers`. Observa que `answers.text` es una lista. En lugar de tokenizar cada enunciado por separado, convierte la lista en un string para tokenizarlos conjuntamente. + +Asรญ es como puedes crear una funciรณn de preprocesamiento para convertir la lista en una cadena y truncar las secuencias para que no superen la longitud mรกxima de input de DistilGPT2: + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]], truncation=True) +``` + +Usa de ๐Ÿค— Datasets la funciรณn [`map`](https://huggingface.co/docs/datasets/process#map) para aplicar la funciรณn de preprocesamiento sobre el dataset en su totalidad. Puedes acelerar la funciรณn `map` configurando el argumento `batched=True` para procesar mรบltiples elementos del dataset a la vez y aumentar la cantidad de procesos con `num_proc`. Elimina las columnas que no necesitas: + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +Ahora necesitas una segunda funciรณn de preprocesamiento para capturar el texto truncado de cualquier ejemplo demasiado largo para evitar cualquier pรฉrdida de informaciรณn. Esta funciรณn de preprocesamiento deberรญa: + +- Concatenar todo el texto. +- Dividir el texto concatenado en trozos mรกs pequeรฑos definidos por un `block_size`. + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... total_length = (total_length // block_size) * block_size +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... result["labels"] = result["input_ids"].copy() +... return result +``` + +Aplica la funciรณn `group_texts` sobre todo el dataset: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +Para modelados de lenguaje causales, usa [`DataCollatorForLanguageModeling`] para crear un lote de ejemplos. Esto tambiรฉn *rellenarรก dinรกmicamente* tu texto a la dimensiรณn del elemento mรกs largo del lote para que de esta manera tengan largo uniforme. Si bien es posible rellenar tu texto en la funciรณn `tokenizer` mediante el argumento `padding=True`, el rellenado dinรกmico es mรกs eficiente. + + + +Puedes usar el token de final de secuencia como el token de relleno y asignar `mlm=False`. Esto usarรก los inputs como etiquetas movidas un elemento hacia la derecha: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) +``` + +Para modelados de lenguaje por enmascaramiento usa el mismo [`DataCollatorForLanguageModeling`] excepto que deberรกs especificar `mlm_probability` para enmascarar tokens aleatoriamente cada vez que iteras sobre los datos. + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15) +``` + + +Puedes usar el token de final de secuencia como el token de relleno y asignar `mlm=False`. Esto usarรก los inputs como etiquetas movidas un elemento hacia la derecha: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False, return_tensors="tf") +``` + +Para modelados de lenguajes por enmascaramiento usa el mismo [`DataCollatorForLanguageModeling`] excepto que deberรกs especificar `mlm_probability` para enmascarar tokens aleatoriamente cada vez que iteras sobre los datos. + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False, return_tensors="tf") +``` + + + +## Modelado de lenguaje causal + +El modelado de lenguaje causal es frecuentemente utilizado para generaciรณn de texto. Esta secciรณn te muestra cรณmo realizar fine-tuning a [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) para generar nuevo texto. + +### Entrenamiento + + + +Carga DistilGPT2 con [`AutoModelForCausalLM`]: + +```py +>>> from transformers import AutoModelForCausalLM, TrainingArguments, Trainer + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + + + +Si no estรกs familiarizado con el proceso de realizar fine-tuning sobre un modelo con [`Trainer`], considera el tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +A este punto, solo faltan tres pasos: + +1. Definir tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. +2. Pasarle los argumentos de entrenamiento a [`Trainer`] junto con el modelo, dataset, y el data collator. +3. Realiza la llamada [`~Trainer.train`] para realizar el fine-tuning sobre tu modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="./results", +... eval_strategy="epoch", +... learning_rate=2e-5, +... weight_decay=0.01, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + + +Para realizar el fine-tuning de un modelo en TensorFlow, comienza por convertir tus datasets al formato `tf.data.Dataset` con [`to_tf_dataset`](https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.to_tf_dataset). Especifica los inputs y etiquetas en `columns`, ya sea para mezclar el dataset, tamaรฑo de lote, y el data collator: + +```py +>>> tf_train_set = lm_dataset["train"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... dummy_labels=True, +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = lm_dataset["test"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... dummy_labels=True, +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + + + +Si no estรกs familiarizado con realizar fine-tuning de tus modelos con Keras, considera el tutorial bรกsico [aquรญ](training#finetune-with-keras)! + + + +Crea la funciรณn optimizadora, la tasa de aprendizaje, y algunos hiperparรกmetros de entrenamiento: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Carga DistilGPT2 con [`TFAutoModelForCausalLM`]: + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +Configura el modelo para entrenamiento con [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +Llama a [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) para realizar el fine-tuning del modelo: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3) +``` + + + +## Modelado de lenguaje por enmascaramiento + +El modelado de lenguaje por enmascaramiento es tambiรฉn conocido como una tarea de rellenar la mรกscara, pues predice un token enmascarado dada una secuencia. Los modelos de lenguaje por enmascaramiento requieren una buena comprensiรณn del contexto de una secuencia entera, en lugar de solo el contexto a la izquierda. Esta secciรณn te enseรฑa como realizar el fine-tuning de [DistilRoBERTa](https://huggingface.co/distilbert/distilroberta-base) para predecir una palabra enmascarada. + +### Entrenamiento + + + +Carga DistilRoBERTa con [`AutoModelForMaskedlM`]: + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + + + +Si no estรกs familiarizado con el proceso de realizar fine-tuning sobre un modelo con [`Trainer`], considera el tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +A este punto, solo faltan tres pasos: + +1. Definir tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. +2. Pasarle los argumentos de entrenamiento a [`Trainer`] junto con el modelo, dataset, y el data collator. +3. Realiza la llamada [`~Trainer.train`] para realizar el fine-tuning de tu modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="./results", +... eval_strategy="epoch", +... learning_rate=2e-5, +... num_train_epochs=3, +... weight_decay=0.01, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + + +Para realizar el fine-tuning de un modelo en TensorFlow, comienza por convertir tus datasets al formato `tf.data.Dataset` con [`to_tf_dataset`](https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.to_tf_dataset). Especifica los inputs y etiquetas en `columns`, ya sea para mezclar el dataset, tamaรฑo de lote, y el data collator: + +```py +>>> tf_train_set = lm_dataset["train"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... dummy_labels=True, +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = lm_dataset["test"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... dummy_labels=True, +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + + + +Si no estรกs familiarizado con realizar fine-tuning de tus modelos con Keras, considera el tutorial bรกsico [aquรญ](training#finetune-with-keras)! + + + +Crea la funciรณn optimizadora, la tasa de aprendizaje, y algunos hiperparรกmetros de entrenamiento: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Carga DistilRoBERTa con [`TFAutoModelForMaskedLM`]: + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("distilbert/distilroberta-base") +``` + +Configura el modelo para entrenamiento con [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +Llama a [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) para realizar el fine-tuning del modelo: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3) +``` + + + + + +Para un ejemplo mรกs profundo sobre cรณmo realizar el fine-tuning sobre un modelo de lenguaje causal, considera +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +o [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb). + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/multiple_choice.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/multiple_choice.md new file mode 100644 index 0000000000000000000000000000000000000000..7f44479ad24d7e67d3c74e13a027821513c16676 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/multiple_choice.md @@ -0,0 +1,207 @@ + + +# Selecciรณn mรบltiple + +La tarea de selecciรณn mรบltiple es parecida a la de responder preguntas, con la excepciรณn de que se dan varias opciones de respuesta junto con el contexto. El modelo se entrena para escoger la respuesta correcta +entre varias opciones a partir del contexto dado. + +Esta guรญa te mostrarรก como hacerle fine-tuning a [BERT](https://huggingface.co/google-bert/bert-base-uncased) en la configuraciรณn `regular` del dataset [SWAG](https://huggingface.co/datasets/swag), de forma +que seleccione la mejor respuesta a partir de varias opciones y algรบn contexto. + +## Cargar el dataset SWAG + +Carga el dataset SWAG con la biblioteca ๐Ÿค— Datasets: + +```py +>>> from datasets import load_dataset + +>>> swag = load_dataset("swag", "regular") +``` + +Ahora, รฉchale un vistazo a un ejemplo del dataset: + +```py +>>> swag["train"][0] +{'ending0': 'passes by walking down the street playing their instruments.', + 'ending1': 'has heard approaching them.', + 'ending2': "arrives and they're outside dancing and asleep.", + 'ending3': 'turns the lead singer watches the performance.', + 'fold-ind': '3416', + 'gold-source': 'gold', + 'label': 0, + 'sent1': 'Members of the procession walk down the street holding small horn brass instruments.', + 'sent2': 'A drum line', + 'startphrase': 'Members of the procession walk down the street holding small horn brass instruments. A drum line', + 'video-id': 'anetv_jkn6uvmqwh4'} +``` + +Los campos `sent1` y `sent2` muestran cรณmo comienza una oraciรณn, y cada campo `ending` indica cรณmo podrรญa terminar. Dado el comienzo de la oraciรณn, el modelo debe escoger el final de oraciรณn correcto indicado por el campo `label`. + +## Preprocesmaiento + +Carga el tokenizer de BERT para procesar el comienzo de cada oraciรณn y los cuatro finales posibles: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") +``` + +La funciรณn de preprocesmaiento debe hacer lo siguiente: + +1. Hacer cuatro copias del campo `sent1` de forma que se pueda combinar cada una con el campo `sent2` para recrear la forma en que empieza la oraciรณn. +2. Combinar `sent2` con cada uno de los cuatro finales de oraciรณn posibles. +3. Aplanar las dos listas para que puedas tokenizarlas, y luego des-aplanarlas para que cada ejemplo tenga los campos `input_ids`, `attention_mask` y `labels` correspondientes. + +```py +>>> ending_names = ["ending0", "ending1", "ending2", "ending3"] + + +>>> def preprocess_function(examples): +... first_sentences = [[context] * 4 for context in examples["sent1"]] +... question_headers = examples["sent2"] +... second_sentences = [ +... [f"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(question_headers) +... ] + +... first_sentences = sum(first_sentences, []) +... second_sentences = sum(second_sentences, []) + +... tokenized_examples = tokenizer(first_sentences, second_sentences, truncation=True) +... return {k: [v[i : i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()} +``` + +Usa la funciรณn [`~datasets.Dataset.map`] de ๐Ÿค— Datasets para aplicarle la funciรณn de preprocesamiento al dataset entero. Puedes acelerar la funciรณn `map` haciendo `batched=True` para procesar varios elementos del dataset a la vez. + +```py +tokenized_swag = swag.map(preprocess_function, batched=True) +``` + +Para crear un lote de ejemplos para selecciรณn mรบltiple, este tambiรฉn le *aรฑadirรก relleno de manera dinรกmica* a tu texto y a las etiquetas para que tengan la longitud del elemento mรกs largo en su lote, de forma que tengan una longitud uniforme. Aunque es posible rellenar el texto en la funciรณn `tokenizer` haciendo +`padding=True`, el rellenado dinรกmico es mรกs eficiente. + +El [`DataCollatorForMultipleChoice`] aplanarรก todas las entradas del modelo, les aplicarรก relleno y luego des-aplanarรก los resultados. +```py +>>> from transformers import DataCollatorForMultipleChoice +>>> collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +``` + +## Entrenamiento + + + +Carga el modelo BERT con [`AutoModelForMultipleChoice`]: + +```py +>>> from transformers import AutoModelForMultipleChoice, TrainingArguments, Trainer + +>>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + + + +Para familiarizarte con el fine-tuning con [`Trainer`], ยกmira el tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +En este punto, solo quedan tres pasos: + +1. Definir tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. +2. Pasarle los argumentos del entrenamiento al [`Trainer`] jnto con el modelo, el dataset, el tokenizer y el collator de datos. +3. Invocar el mรฉtodo [`~Trainer.train`] para realizar el fine-tuning del modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="./results", +... eval_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_swag["train"], +... eval_dataset=tokenized_swag["validation"], +... processing_class=tokenizer, +... data_collator=collator, +... ) + +>>> trainer.train() +``` + + +Para realizar el fine-tuning de un modelo en TensorFlow, primero convierte tus datasets al formato `tf.data.Dataset` con el mรฉtodo [`~TFPreTrainedModel.prepare_tf_dataset`]. + +```py +>>> data_collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_swag["train"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_swag["validation"], +... shuffle=False, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) +``` + + + +Para familiarizarte con el fine-tuning con Keras, ยกmira el tutorial bรกsico [aquรญ](training#finetune-with-keras)! + + + +Prepara una funciรณn de optimizaciรณn, un programa para la tasa de aprendizaje y algunos hiperparรกmetros de entrenamiento: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_train_epochs = 2 +>>> total_train_steps = (len(tokenized_swag["train"]) // batch_size) * num_train_epochs +>>> optimizer, schedule = create_optimizer(init_lr=5e-5, num_warmup_steps=0, num_train_steps=total_train_steps) +``` + +Carga el modelo BERT con [`TFAutoModelForMultipleChoice`]: + +```py +>>> from transformers import TFAutoModelForMultipleChoice + +>>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + +Configura el modelo para entrenarlo con [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> model.compile(optimizer=optimizer) +``` + +Invoca el mรฉtodo [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) para realizar el fine-tuning del modelo: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=2) +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..42a6e4b6e1bc4ee3af2d6e760054b398db9b5d48 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/question_answering.md @@ -0,0 +1,275 @@ + + +# Respuesta a preguntas + + + +La respuesta a preguntas devuelve una respuesta a partir de una pregunta dada. Existen dos formas comunes de responder preguntas: + +- Extractiva: extraer la respuesta a partir del contexto dado. +- Abstractiva: generar una respuesta que responda correctamente la pregunta a partir del contexto dado. + +Esta guรญa te mostrarรก como hacer fine-tuning de [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) en el dataset [SQuAD](https://huggingface.co/datasets/squad) para responder preguntas de forma extractiva. + + + +Revisa la [pรกgina de la tarea](https://huggingface.co/tasks/question-answering) de responder preguntas para tener mรกs informaciรณn sobre otras formas de responder preguntas y los modelos, datasets y mรฉtricas asociadas. + + + +## Carga el dataset SQuAD + +Carga el dataset SQuAD con la biblioteca ๐Ÿค— Datasets: + +```py +>>> from datasets import load_dataset + +>>> squad = load_dataset("squad") +``` + +Ahora, รฉchale un vistazo a una muestra: + +```py +>>> squad["train"][0] +{'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']}, + 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', + 'id': '5733be284776f41900661182', + 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', + 'title': 'University_of_Notre_Dame' +} +``` + +El campo `answers` es un diccionario que contiene la posiciรณn inicial de la respuesta y el `texto` de la respuesta. + +## Preprocesamiento + + + +Carga el tokenizer de DistilBERT para procesar los campos `question` (pregunta) y `context` (contexto): + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Hay algunos pasos de preprocesamiento especรญficos para la tarea de respuesta a preguntas que debes tener en cuenta: + +1. Algunos ejemplos en un dataset pueden tener un contexto que supera la longitud mรกxima de entrada de un modelo. Trunca solamente el contexto asignรกndole el valor `"only_second"` al parรกmetro `truncation`. +2. A continuaciรณn, mapea las posiciones de inicio y fin de la respuesta al contexto original asignรกndole el valor `True` al parรกmetro `return_offsets_mapping`. +3. Una vez tengas el mapeo, puedes encontrar los tokens de inicio y fin de la respuesta. Usa el mรฉtodo [`sequence_ids`](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#tokenizers.Encoding.sequence_ids) +para encontrar quรฉ parte de la lista de tokens desplazados corresponde a la pregunta y cuรกl corresponde al contexto. + +A continuaciรณn puedes ver como se crea una funciรณn para truncar y mapear los tokens de inicio y fin de la respuesta al `context`: + +```py +>>> def preprocess_function(examples): +... questions = [q.strip() for q in examples["question"]] +... inputs = tokenizer( +... questions, +... examples["context"], +... max_length=384, +... truncation="only_second", +... return_offsets_mapping=True, +... padding="max_length", +... ) + +... offset_mapping = inputs.pop("offset_mapping") +... answers = examples["answers"] +... start_positions = [] +... end_positions = [] + +... for i, offset in enumerate(offset_mapping): +... answer = answers[i] +... start_char = answer["answer_start"][0] +... end_char = answer["answer_start"][0] + len(answer["text"][0]) +... sequence_ids = inputs.sequence_ids(i) + +... # Encuentra el inicio y el fin del contexto +... idx = 0 +... while sequence_ids[idx] != 1: +... idx += 1 +... context_start = idx +... while sequence_ids[idx] == 1: +... idx += 1 +... context_end = idx - 1 + +... # Si la respuesta entera no estรก dentro del contexto, etiquรฉtala como (0, 0) +... if offset[context_start][0] > end_char or offset[context_end][1] < start_char: +... start_positions.append(0) +... end_positions.append(0) +... else: +... # De lo contrario, esta es la posiciรณn de los tokens de inicio y fin +... idx = context_start +... while idx <= context_end and offset[idx][0] <= start_char: +... idx += 1 +... start_positions.append(idx - 1) + +... idx = context_end +... while idx >= context_start and offset[idx][1] >= end_char: +... idx -= 1 +... end_positions.append(idx + 1) + +... inputs["start_positions"] = start_positions +... inputs["end_positions"] = end_positions +... return inputs +``` + +Usa la funciรณn [`~datasets.Dataset.map`] de ๐Ÿค— Datasets para aplicarle la funciรณn de preprocesamiento al dataset entero. Puedes acelerar la funciรณn `map` haciendo `batched=True` para procesar varios elementos del dataset a la vez. +Quita las columnas que no necesites: + +```py +>>> tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names) +``` + +Usa el [`DefaultDataCollator`] para crear un lote de ejemplos. A diferencia de los otros collators de datos en ๐Ÿค— Transformers, el `DefaultDataCollator` no aplica ningรบn procesamiento adicional (como el rellenado). + + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## Entrenamiento + + + +Carga el modelo DistilBERT con [`AutoModelForQuestionAnswering`]: + +```py +>>> from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer + +>>> model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +Para familiarizarte con el fine-tuning con [`Trainer`], ยกmira el tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +En este punto, solo quedan tres pasos: + +1. Definir tus hiperparรกmetros de entrenamiento en [`TrainingArguments`]. +2. Pasarle los argumentos del entrenamiento al [`Trainer`] junto con el modelo, el dataset, el tokenizer y el collator de datos. +3. Invocar el mรฉtodo [`~Trainer.train`] para realizar el fine-tuning del modelo. + +```py +>>> training_args = TrainingArguments( +... output_dir="./results", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_squad["train"], +... eval_dataset=tokenized_squad["validation"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + + +Para realizar el fine-tuning de un modelo en TensorFlow, primero convierte tus datasets al formato `tf.data.Dataset` con el mรฉtodo [`~TFPreTrainedModel.prepare_tf_dataset`]. + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_squad["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_squad["validation"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + + + +Para familiarizarte con el fine-tuning con Keras, ยกmira el tutorial bรกsico [aquรญ](training#finetune-with-keras)! + + + +Prepara una funciรณn de optimizaciรณn, un programa para la tasa de aprendizaje y algunos hiperparรกmetros de entrenamiento: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 2 +>>> total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs +>>> optimizer, schedule = create_optimizer( +... init_lr=2e-5, +... num_warmup_steps=0, +... num_train_steps=total_train_steps, +... ) +``` + +Carga el modelo DistilBERT con [`TFAutoModelForQuestionAnswering`]: + +```py +>>> from transformers import TFAutoModelForQuestionAnswering + +>>> model = TFAutoModelForQuestionAnswering("distilbert/distilbert-base-uncased") +``` + +Configura el modelo para entrenarlo con [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +Invoca el mรฉtodo [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) para realizar el fine-tuning del modelo: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3) +``` + + + + + +Para un ejemplo con mayor profundidad de cรณmo hacer fine-tuning a un modelo para responder preguntas, รฉchale un vistazo al +[cuaderno de PyTorch](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) o al +[cuaderno de TensorFlow](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb) correspondiente. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/summarization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/summarization.md new file mode 100644 index 0000000000000000000000000000000000000000..c9060cba6b771db41ee71be6e29af77345f14fc9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks/summarization.md @@ -0,0 +1,226 @@ + + +# Generaciรณn de resรบmenes + + + +La generaciรณn de resรบmenes (summarization, en inglรฉs) crea una versiรณn mรกs corta de un documento o un artรญculo que resume toda su informaciรณn importante. Junto con la traducciรณn, es un ejemplo de una tarea que puede ser formulada como una tarea secuencia a secuencia. La generaciรณn de resรบmenes puede ser: + +- Extractiva: Extrae la informaciรณn mรกs relevante de un documento. +- Abstractiva: Genera un texto nuevo que captura la informaciรณn mรกs importante. + +Esta guรญa te mostrarรก cรณmo puedes hacer fine-tuning del modelo [T5](https://huggingface.co/google-t5/t5-small) sobre el subset de proyectos de ley del estado de California, dentro del dataset [BillSum](https://huggingface.co/datasets/billsum) para hacer generaciรณn de resรบmenes abstractiva. + + + +Consulta la [pรกgina de la tarea](https://huggingface.co/tasks/summarization) de generaciรณn de resรบmenes para obtener mรกs informaciรณn sobre sus modelos, datasets y mรฉtricas asociadas. + + + +## Carga el dataset BillSum + +Carga el dataset BillSum de la biblioteca ๐Ÿค— Datasets: + +```py +>>> from datasets import load_dataset + +>>> billsum = load_dataset("billsum", split="ca_test") +``` + +Divide el dataset en un set de train y un set de test: + +```py +>>> billsum = billsum.train_test_split(test_size=0.2) +``` + +A continuaciรณn, observa un ejemplo: + +```py +>>> billsum["train"][0] +{'summary': 'Existing law authorizes state agencies to enter into contracts for the acquisition of goods or services upon approval by the Department of General Services. Existing law sets forth various requirements and prohibitions for those contracts, including, but not limited to, a prohibition on entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between spouses and domestic partners or same-sex and different-sex couples in the provision of benefits. Existing law provides that a contract entered into in violation of those requirements and prohibitions is void and authorizes the state or any person acting on behalf of the state to bring a civil action seeking a determination that a contract is in violation and therefore void. Under existing law, a willful violation of those requirements and prohibitions is a misdemeanor.\nThis bill would also prohibit a state agency from entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between employees on the basis of gender identity in the provision of benefits, as specified. By expanding the scope of a crime, this bill would impose a state-mandated local program.\nThe California Constitution requires the state to reimburse local agencies and school districts for certain costs mandated by the state. Statutory provisions establish procedures for making that reimbursement.\nThis bill would provide that no reimbursement is required by this act for a specified reason.', + 'text': 'The people of the State of California do enact as follows:\n\n\nSECTION 1.\nSection 10295.35 is added to the Public Contract Code, to read:\n10295.35.\n(a) (1) Notwithstanding any other law, a state agency shall not enter into any contract for the acquisition of goods or services in the amount of one hundred thousand dollars ($100,000) or more with a contractor that, in the provision of benefits, discriminates between employees on the basis of an employeeโ€™s or dependentโ€™s actual or perceived gender identity, including, but not limited to, the employeeโ€™s or dependentโ€™s identification as transgender.\n(2) For purposes of this section, โ€œcontractโ€ includes contracts with a cumulative amount of one hundred thousand dollars ($100,000) or more per contractor in each fiscal year.\n(3) For purposes of this section, an employee health plan is discriminatory if the plan is not consistent with Section 1365.5 of the Health and Safety Code and Section 10140 of the Insurance Code.\n(4) The requirements of this section shall apply only to those portions of a contractorโ€™s operations that occur under any of the following conditions:\n(A) Within the state.\n(B) On real property outside the state if the property is owned by the state or if the state has a right to occupy the property, and if the contractorโ€™s presence at that location is connected to a contract with the state.\n(C) Elsewhere in the United States where work related to a state contract is being performed.\n(b) Contractors shall treat as confidential, to the maximum extent allowed by law or by the requirement of the contractorโ€™s insurance provider, any request by an employee or applicant for employment benefits or any documentation of eligibility for benefits submitted by an employee or applicant for employment.\n(c) After taking all reasonable measures to find a contractor that complies with this section, as determined by the state agency, the requirements of this section may be waived under any of the following circumstances:\n(1) There is only one prospective contractor willing to enter into a specific contract with the state agency.\n(2) The contract is necessary to respond to an emergency, as determined by the state agency, that endangers the public health, welfare, or safety, or the contract is necessary for the provision of essential services, and no entity that complies with the requirements of this section capable of responding to the emergency is immediately available.\n(3) The requirements of this section violate, or are inconsistent with, the terms or conditions of a grant, subvention, or agreement, if the agency has made a good faith attempt to change the terms or conditions of any grant, subvention, or agreement to authorize application of this section.\n(4) The contractor is providing wholesale or bulk water, power, or natural gas, the conveyance or transmission of the same, or ancillary services, as required for ensuring reliable services in accordance with good utility practice, if the purchase of the same cannot practically be accomplished through the standard competitive bidding procedures and the contractor is not providing direct retail services to end users.\n(d) (1) A contractor shall not be deemed to discriminate in the provision of benefits if the contractor, in providing the benefits, pays the actual costs incurred in obtaining the benefit.\n(2) If a contractor is unable to provide a certain benefit, despite taking reasonable measures to do so, the contractor shall not be deemed to discriminate in the provision of benefits.\n(e) (1) Every contract subject to this chapter shall contain a statement by which the contractor certifies that the contractor is in compliance with this section.\n(2) The department or other contracting agency shall enforce this section pursuant to its existing enforcement powers.\n(3) (A) If a contractor falsely certifies that it is in compliance with this section, the contract with that contractor shall be subject to Article 9 (commencing with Section 10420), unless, within a time period specified by the department or other contracting agency, the contractor provides to the department or agency proof that it has complied, or is in the process of complying, with this section.\n(B) The application of the remedies or penalties contained in Article 9 (commencing with Section 10420) to a contract subject to this chapter shall not preclude the application of any existing remedies otherwise available to the department or other contracting agency under its existing enforcement powers.\n(f) Nothing in this section is intended to regulate the contracting practices of any local jurisdiction.\n(g) This section shall be construed so as not to conflict with applicable federal laws, rules, or regulations. In the event that a court or agency of competent jurisdiction holds that federal law, rule, or regulation invalidates any clause, sentence, paragraph, or section of this code or the application thereof to any person or circumstances, it is the intent of the state that the court or agency sever that clause, sentence, paragraph, or section so that the remainder of this section shall remain in effect.\nSEC. 2.\nSection 10295.35 of the Public Contract Code shall not be construed to create any new enforcement authority or responsibility in the Department of General Services or any other contracting agency.\nSEC. 3.\nNo reimbursement is required by this act pursuant to Section 6 of Article XIII\u2009B of the California Constitution because the only costs that may be incurred by a local agency or school district will be incurred because this act creates a new crime or infraction, eliminates a crime or infraction, or changes the penalty for a crime or infraction, within the meaning of Section 17556 of the Government Code, or changes the definition of a crime within the meaning of Section 6 of Article XIII\u2009B of the California Constitution.', + 'title': 'An act to add Section 10295.35 to the Public Contract Code, relating to public contracts.'} +``` + +El campo `text` es el input y el campo `summary` es el objetivo. + +## Preprocesa + +Carga el tokenizador T5 para procesar `text` y `summary`: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-small") +``` + +La funciรณn de preprocesamiento necesita: + +1. Agregar un prefijo al input; una clave para que T5 sepa que se trata de una tarea de generaciรณn de resรบmenes. Algunos modelos capaces de realizar mรบltiples tareas de NLP requieren una clave que indique la tarea especรญfica. +2. Usar el argumento `text_target` para tokenizar etiquetas. +3. Truncar secuencias para que no sean mรกs largas que la longitud mรกxima fijada por el parรกmetro `max_length`. + +```py +>>> prefix = "summarize: " + + +>>> def preprocess_function(examples): +... inputs = [prefix + doc for doc in examples["text"]] +... model_inputs = tokenizer(inputs, max_length=1024, truncation=True) + +... labels = tokenizer(text_target=examples["summary"], max_length=128, truncation=True) + +... model_inputs["labels"] = labels["input_ids"] +... return model_inputs +``` + +Usa la funciรณn [`~datasets.Dataset.map`] de ๐Ÿค— Datasets para aplicar la funciรณn de preprocesamiento sobre el dataset en su totalidad. Puedes acelerar la funciรณn `map` configurando el argumento `batched=True` para procesar mรบltiples elementos del dataset a la vez: + +```py +>>> tokenized_billsum = billsum.map(preprocess_function, batched=True) +``` + +Usa [`DataCollatorForSeq2Seq`] para crear un lote de ejemplos. Esto tambiรฉn *rellenarรก dinรกmicamente* tu texto y etiquetas a la dimensiรณn del elemento mรกs largo del lote para que tengan un largo uniforme. Si bien es posible rellenar tu texto en la funciรณn `tokenizer` mediante el argumento `padding=True`, el rellenado dinรกmico es mรกs eficiente. + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model) +``` + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model, return_tensors="tf") +``` + + + +## Entrenamiento + + + +Carga T5 con [`AutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer + +>>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-small") +``` + + + +Para familiarizarte con el proceso para realizar fine-tuning sobre un modelo con [`Trainer`], ยกmira el tutorial bรกsico [aquรญ](../training#finetune-with-trainer)! + + + +En este punto, solo faltan tres pasos: + +1. Definir tus hiperparรกmetros de entrenamiento en [`Seq2SeqTrainingArguments`]. +2. Pasarle los argumentos de entrenamiento a [`Seq2SeqTrainer`] junto con el modelo, dataset y data collator. +3. Llamar [`~Trainer.train`] para realizar el fine-tuning sobre tu modelo. + +```py +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="./results", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... weight_decay=0.01, +... save_total_limit=3, +... num_train_epochs=1, +... fp16=True, +... ) + +>>> trainer = Seq2SeqTrainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_billsum["train"], +... eval_dataset=tokenized_billsum["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + + +Para hacer fine-tuning de un modelo en TensorFlow, comienza por convertir tus datasets al formato `tf.data.Dataset` con [`~datasets.Dataset.to_tf_dataset`]. Especifica los inputs y etiquetas en `columns`, el tamaรฑo de lote, el data collator, y si es necesario mezclar el dataset: + +```py +>>> tf_train_set = tokenized_billsum["train"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = tokenized_billsum["test"].to_tf_dataset( +... columns=["attention_mask", "input_ids", "labels"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + + + +Para familiarizarte con el fine-tuning con Keras, ยกmira el tutorial bรกsico [aquรญ](training#finetune-with-keras)! + + + +Crea la funciรณn optimizadora, establece la tasa de aprendizaje y algunos hiperparรกmetros de entrenamiento: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +Carga T5 con [`TFAutoModelForSeq2SeqLM`]: + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-small") +``` + +Configura el modelo para entrenamiento con [`compile`](https://keras.io/api/models/model_training_apis/#compile-method): + +```py +>>> model.compile(optimizer=optimizer) +``` + +Llama a [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) para realizar el fine-tuning del modelo: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3) +``` + + + + + +Para un ejemplo con mayor profundidad de cรณmo hacer fine-tuning a un modelo para generaciรณn de resรบmenes, revisa la +[notebook en PyTorch](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb) +o a la [notebook en TensorFlow](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb). + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks_explained.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks_explained.md new file mode 100644 index 0000000000000000000000000000000000000000..69d822e82ac67001d0668fff8adb0e6041e2b46a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tasks_explained.md @@ -0,0 +1,295 @@ + + +# ยฟCรณmo los ๐Ÿค— Transformers resuelven tareas? + +En [Lo que ๐Ÿค— Transformers puede hacer](task_summary), aprendiste sobre el procesamiento de lenguaje natural (NLP), tareas de voz y audio, visiรณn por computadora y algunas aplicaciones importantes de ellas. Esta pรกgina se centrarรก en cรณmo los modelos resuelven estas tareas y explicarรก lo que estรก sucediendo debajo de la superficie. Hay muchas maneras de resolver una tarea dada, y diferentes modelos pueden implementar ciertas tรฉcnicas o incluso abordar la tarea desde un รกngulo nuevo, pero para los modelos Transformer, la idea general es la misma. Debido a su arquitectura flexible, la mayorรญa de los modelos son una variante de una estructura de codificador, descodificador o codificador-descodificador. Ademรกs de los modelos Transformer, nuestra biblioteca tambiรฉn tiene varias redes neuronales convolucionales (CNNs) modernas, que todavรญa se utilizan hoy en dรญa para tareas de visiรณn por computadora. Tambiรฉn explicaremos cรณmo funciona una CNN moderna. + +Para explicar cรณmo se resuelven las tareas, caminaremos a travรฉs de lo que sucede dentro del modelo para generar predicciones รบtiles. + +- [Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2) para clasificaciรณn de audio y reconocimiento automรกtico de habla (ASR) +- [Transformador de Visiรณn (ViT)](https://huggingface.co/docs/transformers/model_doc/vit) y [ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext) para clasificaciรณn de imรกgenes +- [DETR](https://huggingface.co/docs/transformers/model_doc/detr) para detecciรณn de objetos +- [Mask2Former](https://huggingface.co/docs/transformers/model_doc/mask2former) para segmentaciรณn de imagen +- [GLPN](https://huggingface.co/docs/transformers/model_doc/glpn) para estimaciรณn de profundidad +- [BERT](https://huggingface.co/docs/transformers/model_doc/bert) para tareas de NLP como clasificaciรณn de texto, clasificaciรณn de tokens y preguntas y respuestas que utilizan un codificador +- [GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2) para tareas de NLP como generaciรณn de texto que utilizan un descodificador +- [BART](https://huggingface.co/docs/transformers/model_doc/bart) para tareas de NLP como resumen y traducciรณn que utilizan un codificador-descodificador + + + +Antes de continuar, es bueno tener un conocimiento bรกsico de la arquitectura original del Transformer. Saber cรณmo funcionan los codificadores, decodificadores y la atenciรณn te ayudarรก a entender cรณmo funcionan los diferentes modelos de Transformer. Si estรกs empezando o necesitas repasar, ยกecha un vistazo a nuestro [curso](https://huggingface.co/course/chapter1/4?fw=pt) para obtener mรกs informaciรณn! + + + +## Habla y audio + +[Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2) es un modelo auto-supervisado preentrenado en datos de habla no etiquetados y ajustado en datos etiquetados para clasificaciรณn de audio y reconocimiento automรกtico de voz. + +
+ +
+ +Este modelo tiene cuatro componentes principales: + +1. Un *codificador de caracterรญsticas* toma la forma de onda de audio cruda, la normaliza a media cero y varianza unitaria, y la convierte en una secuencia de vectores de caracterรญsticas, cada uno de 20 ms de duraciรณn. + +2. Las formas de onda son continuas por naturaleza, por lo que no se pueden dividir en unidades separadas como una secuencia de texto se puede dividir en palabras. Por eso, los vectores de caracterรญsticas se pasan a un *mรณdulo de cuantificaciรณn*, que tiene como objetivo aprender unidades de habla discretas. La unidad de habla se elige de una colecciรณn de palabras de cรณdigo, conocidas como *codebook* (puedes pensar en esto como el vocabulario). Del codebook, se elige el vector o unidad de habla que mejor representa la entrada de audio continua y se envรญa a travรฉs del modelo. + +3. Alrededor de la mitad de los vectores de caracterรญsticas se enmascaran aleatoriamente, y el vector de caracterรญsticas enmascarado se alimenta a una *red de contexto*, que es un codificador Transformer que tambiรฉn agrega incrustaciones posicionales relativas. + +4. El objetivo del preentrenamiento de la red de contexto es una *tarea contrastiva*. El modelo tiene que predecir la verdadera representaciรณn de habla cuantizada de la predicciรณn enmascarada a partir de un conjunto de falsas, lo que anima al modelo a encontrar el vector de contexto y la unidad de habla cuantizada mรกs similares (la etiqueta objetivo). + +ยกAhora que wav2vec2 estรก preentrenado, puedes ajustarlo con tus datos para clasificaciรณn de audio o reconocimiento automรกtico de voz! + +### Clasificaciรณn de audio + +Para usar el modelo preentrenado para la clasificaciรณn de audio, aรฑade una capa de clasificaciรณn de secuencia encima del modelo base de Wav2Vec2. La capa de clasificaciรณn es una capa lineal que acepta los estados ocultos del codificador. Los estados ocultos representan las caracterรญsticas aprendidas de cada fotograma de audio, que pueden tener longitudes variables. Para crear un vector de longitud fija, primero se agrupan los estados ocultos y luego se transforman en logits sobre las etiquetas de clase. La pรฉrdida de entropรญa cruzada se calcula entre los logits y el objetivo para encontrar la clase mรกs probable. + +ยฟListo para probar la clasificaciรณn de audio? ยกConsulta nuestra guรญa completa de [clasificaciรณn de audio](https://huggingface.co/docs/transformers/tasks/audio_classification) para aprender cรณmo ajustar Wav2Vec2 y usarlo para inferencia! + +### Reconocimiento automรกtico de voz + +Para usar el modelo preentrenado para el reconocimiento automรกtico de voz, aรฑade una capa de modelado del lenguaje encima del modelo base de Wav2Vec2 para [CTC (clasificaciรณn temporal conexista)](glossary#connectionist-temporal-classification-ctc). La capa de modelado del lenguaje es una capa lineal que acepta los estados ocultos del codificador y los transforma en logits. Cada logit representa una clase de token (el nรบmero de tokens proviene del vocabulario de la tarea). La pรฉrdida de CTC se calcula entre los logits y los objetivos para encontrar la secuencia de tokens mรกs probable, que luego se decodifican en una transcripciรณn. + +ยฟListo para probar el reconocimiento automรกtico de voz? ยกConsulta nuestra guรญa completa de [reconocimiento automรกtico de voz](tasks/asr) para aprender cรณmo ajustar Wav2Vec2 y usarlo para inferencia! + +## Visiรณn por computadora + +Hay dos formas de abordar las tareas de visiรณn por computadora: + +1. Dividir una imagen en una secuencia de parches y procesarlos en paralelo con un Transformer. +2. Utilizar una CNN moderna, como [ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext), que se basa en capas convolucionales pero adopta diseรฑos de redes modernas. + + + +Un tercer enfoque combina Transformers con convoluciones (por ejemplo, [Convolutional Vision Transformer](https://huggingface.co/docs/transformers/model_doc/cvt) o [LeViT](https://huggingface.co/docs/transformers/model_doc/levit)). No discutiremos estos porque simplemente combinan los dos enfoques que examinamos aquรญ. + + + +ViT y ConvNeXT se utilizan comรบnmente para la clasificaciรณn de imรกgenes, pero para otras tareas de visiรณn como la detecciรณn de objetos, la segmentaciรณn y la estimaciรณn de profundidad, veremos DETR, Mask2Former y GLPN, respectivamente; estos modelos son mรกs adecuados para esas tareas. + +### Clasificaciรณn de imรกgenes + +ViT y ConvNeXT pueden usarse ambos para la clasificaciรณn de imรกgenes; la diferencia principal es que ViT utiliza un mecanismo de atenciรณn mientras que ConvNeXT utiliza convoluciones. + +#### Transformer + +[ViT](https://huggingface.co/docs/transformers/model_doc/vit) reemplaza completamente las convoluciones con una arquitectura de Transformer pura. Si estรกs familiarizado con el Transformer original, entonces ya estรกs en el camino para entender ViT. + +
+ +
+ +El cambio principal que introdujo ViT fue en cรณmo se alimentan las imรกgenes a un Transformer: + +1. Una imagen se divide en parches cuadrados no superpuestos, cada uno de los cuales se convierte en un vector o *incrustaciรณn de parche*(patch embedding). Las incrustaciones de parche se generan a partir de una capa convolucional 2D que crea las dimensiones de entrada adecuadas (que para un Transformer base son 768 valores para cada incrustaciรณn de parche). Si tuvieras una imagen de 224x224 pรญxeles, podrรญas dividirla en 196 parches de imagen de 16x16. Al igual que el texto se tokeniza en palabras, una imagen se "tokeniza" en una secuencia de parches. + +2. Se agrega una *incrustaciรณn aprendida* - un token especial `[CLS]` - al principio de las incrustaciones del parche, al igual que en BERT. El estado oculto final del token `[CLS]` se utiliza como la entrada para la cabecera de clasificaciรณn adjunta; otras salidas se ignoran. Este token ayuda al modelo a aprender cรณmo codificar una representaciรณn de la imagen. + +3. Lo รบltimo que se agrega a las incrustaciones de parche e incrustaciones aprendidas son las *incrustaciones de posiciรณn* porque el modelo no sabe cรณmo estรกn ordenados los parches de imagen. Las incrustaciones de posiciรณn tambiรฉn son aprendibles y tienen el mismo tamaรฑo que las incrustaciones de parche. Finalmente, todas las incrustaciones se pasan al codificador Transformer. + +4. La salida, especรญficamente solo la salida con el token `[CLS]`, se pasa a una cabecera de perceptrรณn multicapa (MLP). El objetivo del preentrenamiento de ViT es simplemente la clasificaciรณn. Al igual que otras cabeceras de clasificaciรณn, la cabecera de MLP convierte la salida en logits sobre las etiquetas de clase y calcula la pรฉrdida de entropรญa cruzada para encontrar la clase mรกs probable. + +ยฟListo para probar la clasificaciรณn de imรกgenes? ยกConsulta nuestra guรญa completa de [clasificaciรณn de imรกgenes](tasks/image_classification) para aprender cรณmo ajustar ViT y usarlo para inferencia! + +#### CNN + + + +Esta secciรณn explica brevemente las convoluciones, pero serรญa รบtil tener un entendimiento previo de cรณmo cambian la forma y el tamaรฑo de una imagen. Si no estรกs familiarizado con las convoluciones, ยกecha un vistazo al [capรญtulo de Redes Neuronales Convolucionales](https://github.com/fastai/fastbook/blob/master/13_convolutions.ipynb) del libro fastai! + + + +[ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext) es una arquitectura de CNN que adopta diseรฑos de redes nuevas y modernas para mejorar el rendimiento. Sin embargo, las convoluciones siguen siendo el nรบcleo del modelo. Desde una perspectiva de alto nivel, una [convoluciรณn](glossary#convolution) es una operaciรณn donde una matriz mรกs pequeรฑa (*kernel*) se multiplica por una pequeรฑa ventana de pรญxeles de la imagen. Esta calcula algunas caracterรญsticas de ella, como una textura particular o la curvatura de una lรญnea. Luego, se desliza hacia la siguiente ventana de pรญxeles; la distancia que recorre la convoluciรณn se conoce como el *stride*. + +
+ +
+ +Una convoluciรณn bรกsica sin relleno ni paso, tomada de Una guรญa para la aritmรฉtica de convoluciones para el aprendizaje profundo. + +Puedes alimentar esta salida a otra capa convolucional, y con cada capa sucesiva, la red aprende cosas mรกs complejas y abstractas como perros calientes o cohetes. Entre capas convolucionales, es comรบn aรฑadir una capa de agrupaciรณn para reducir la dimensionalidad y hacer que el modelo sea mรกs robusto a las variaciones de la posiciรณn de una caracterรญstica. + +
+ +
+ +ConvNeXT moderniza una CNN de cinco maneras: + +1. Cambia el nรบmero de bloques en cada etapa y "fragmenta" una imagen con un paso y tamaรฑo de kernel mรกs grandes. La ventana deslizante no superpuesta hace que esta estrategia de fragmentaciรณn sea similar a cรณmo ViT divide una imagen en parches. + +2. Una capa de *cuello de botella* reduce el nรบmero de canales y luego lo restaura porque es mรกs rรกpido hacer una convoluciรณn de 1x1, y se puede aumentar la profundidad. Un cuello de botella invertido hace lo contrario al expandir el nรบmero de canales y luego reducirlos, lo cual es mรกs eficiente en memoria. + +3. Reemplaza la tรญpica capa convolucional de 3x3 en la capa de cuello de botella con una convoluciรณn *depthwise*, que aplica una convoluciรณn a cada canal de entrada por separado y luego los apila de nuevo al final. Esto ensancha el ancho de la red para mejorar el rendimiento. + +4. ViT tiene un campo receptivo global, lo que significa que puede ver mรกs de una imagen a la vez gracias a su mecanismo de atenciรณn. ConvNeXT intenta replicar este efecto aumentando el tamaรฑo del kernel a 7x7. + +5. ConvNeXT tambiรฉn hace varios cambios en el diseรฑo de capas que imitan a los modelos Transformer. Hay menos capas de activaciรณn y normalizaciรณn, la funciรณn de activaciรณn se cambia a GELU en lugar de ReLU, y utiliza LayerNorm en lugar de BatchNorm. + +La salida de los bloques convolucionales se pasa a una cabecera de clasificaciรณn que convierte las salidas en logits y calcula la pรฉrdida de entropรญa cruzada para encontrar la etiqueta mรกs probable. + +### Object detection + +[DETR](https://huggingface.co/docs/transformers/model_doc/detr), *DEtection TRansformer*, es un modelo de detecciรณn de objetos de un extremo a otro que combina una CNN con un codificador-decodificador Transformer. + +
+ +
+ +1. Una CNN preentrenada *backbone* toma una imagen, representada por sus valores de pรญxeles, y crea un mapa de caracterรญsticas de baja resoluciรณn de la misma. A continuaciรณn, se aplica una convoluciรณn 1x1 al mapa de caracterรญsticas para reducir la dimensionalidad y se crea un nuevo mapa de caracterรญsticas con una representaciรณn de imagen de alto nivel. Dado que el Transformer es un modelo secuencial, el mapa de caracterรญsticas se aplana en una secuencia de vectores de caracterรญsticas que se combinan con incrustaciones posicionales. + +2. Los vectores de caracterรญsticas se pasan al codificador, que aprende las representaciones de imagen usando sus capas de atenciรณn. A continuaciรณn, los estados ocultos del codificador se combinan con *consultas de objeto* en el decodificador. Las consultas de objeto son incrustaciones aprendidas que se enfocan en las diferentes regiones de una imagen, y se actualizan a medida que avanzan a travรฉs de cada capa de atenciรณn. Los estados ocultos del decodificador se pasan a una red feedforward que predice las coordenadas del cuadro delimitador y la etiqueta de clase para cada consulta de objeto, o `no objeto` si no hay ninguno. + + DETR descodifica cada consulta de objeto en paralelo para producir *N* predicciones finales, donde *N* es el nรบmero de consultas. A diferencia de un modelo autoregresivo tรญpico que predice un elemento a la vez, la detecciรณn de objetos es una tarea de predicciรณn de conjuntos (`cuadro delimitador`, `etiqueta de clase`) que hace *N* predicciones en un solo paso. + +3. DETR utiliza una **pรฉrdida de coincidencia bipartita** durante el entrenamiento para comparar un nรบmero fijo de predicciones con un conjunto fijo de etiquetas de verdad bรกsica. Si hay menos etiquetas de verdad bรกsica en el conjunto de *N* etiquetas, entonces se rellenan con una clase `no objeto`. Esta funciรณn de pรฉrdida fomenta que DETR encuentre una asignaciรณn uno a uno entre las predicciones y las etiquetas de verdad bรกsica. Si los cuadros delimitadores o las etiquetas de clase no son correctos, se incurre en una pรฉrdida. Del mismo modo, si DETR predice un objeto que no existe, se penaliza. Esto fomenta que DETR encuentre otros objetos en una imagen en lugar de centrarse en un objeto realmente prominente. + +Se aรฑade una cabecera de detecciรณn de objetos encima de DETR para encontrar la etiqueta de clase y las coordenadas del cuadro delimitador. Hay dos componentes en la cabecera de detecciรณn de objetos: una capa lineal para transformar los estados ocultos del decodificador en logits sobre las etiquetas de clase, y una MLP para predecir el cuadro delimitador. + +ยฟListo para probar la detecciรณn de objetos? ยกConsulta nuestra guรญa completa de [detecciรณn de objetos](https://huggingface.co/docs/transformers/tasks/object_detection) para aprender cรณmo ajustar DETR y usarlo para inferencia! + +### Segmentaciรณn de imรกgenes + +[Mask2Former](https://huggingface.co/docs/transformers/model_doc/mask2former) es una arquitectura universal para resolver todos los tipos de tareas de segmentaciรณn de imรกgenes. Los modelos de segmentaciรณn tradicionales suelen estar adaptados a una tarea particular de segmentaciรณn de imรกgenes, como la segmentaciรณn de instancias, semรกntica o panรณptica. Mask2Former enmarca cada una de esas tareas como un problema de *clasificaciรณn de mรกscaras*. La clasificaciรณn de mรกscaras agrupa pรญxeles en *N* segmentos, y predice *N* mรกscaras y su etiqueta de clase correspondiente para una imagen dada. Explicaremos cรณmo funciona Mask2Former en esta secciรณn, y luego podrรกs probar el ajuste fino de SegFormer al final. + +
+ +
+ +Hay tres componentes principales en Mask2Former: + +1. Un [backbone Swin](https://huggingface.co/docs/transformers/model_doc/swin) acepta una imagen y crea un mapa de caracterรญsticas de imagen de baja resoluciรณn a partir de 3 convoluciones consecutivas de 3x3. + +2. El mapa de caracterรญsticas se pasa a un *decodificador de pรญxeles* que aumenta gradualmente las caracterรญsticas de baja resoluciรณn en incrustaciones de alta resoluciรณn por pรญxel. De hecho, el decodificador de pรญxeles genera caracterรญsticas multiescala (contiene caracterรญsticas de baja y alta resoluciรณn) con resoluciones de 1/32, 1/16 y 1/8 de la imagen original. + +3. Cada uno de estos mapas de caracterรญsticas de diferentes escalas se alimenta sucesivamente a una capa decodificadora Transformer a la vez para capturar objetos pequeรฑos de las caracterรญsticas de alta resoluciรณn. La clave de Mask2Former es el mecanismo de *atenciรณn enmascarada* en el decodificador. A diferencia de la atenciรณn cruzada que puede atender a toda la imagen, la atenciรณn enmascarada solo se centra en cierta รกrea de la imagen. Esto es mรกs rรกpido y conduce a un mejor rendimiento porque las caracterรญsticas locales de una imagen son suficientes para que el modelo aprenda. + +4. Al igual que [DETR](tasks_explained#object-detection), Mask2Former tambiรฉn utiliza consultas de objetos aprendidas y las combina con las caracterรญsticas de la imagen del decodificador de pรญxeles para hacer una predicciรณn de conjunto (`etiqueta de clase`, `predicciรณn de mรกscara`). Los estados ocultos del decodificador se pasan a una capa lineal y se transforman en logits sobre las etiquetas de clase. Se calcula la pรฉrdida de entropรญa cruzada entre los logits y la etiqueta de clase para encontrar la mรกs probable. + + Las predicciones de mรกscara se generan combinando las incrustaciones de pรญxeles con los estados ocultos finales del decodificador. La pรฉrdida de entropรญa cruzada sigmoidea y de la pรฉrdida DICE se calcula entre los logits y la mรกscara de verdad bรกsica para encontrar la mรกscara mรกs probable. + +ยฟListo para probar la detecciรณn de objetos? ยกConsulta nuestra guรญa completa de [segmentaciรณn de imรกgenes](https://huggingface.co/docs/transformers/tasks/semantic_segmentation) para aprender cรณmo ajustar SegFormer y usarlo para inferencia! + +### Estimaciรณn de profundidad + +[GLPN](https://huggingface.co/docs/transformers/model_doc/glpn), *Global-Local Path Network*, es un Transformer para la estimaciรณn de profundidad que combina un codificador [SegFormer](https://huggingface.co/docs/transformers/model_doc/segformer) con un decodificador ligero. + +
+ +
+ +1. Al igual que ViT, una imagen se divide en una secuencia de parches, excepto que estos parches de imagen son mรกs pequeรฑos. Esto es mejor para tareas de predicciรณn densa como la segmentaciรณn o la estimaciรณn de profundidad. Los parches de imagen se transforman en incrustaciones de parches (ver la secciรณn de [clasificaciรณn de imรกgenes](#clasificaciรณn-de-imรกgenes) para mรกs detalles sobre cรณmo se crean las incrustaciones de parches), que se alimentan al codificador. + +2. El codificador acepta las incrustaciones de parches y las pasa a travรฉs de varios bloques codificadores. Cada bloque consiste en capas de atenciรณn y Mix-FFN. El propรณsito de este รบltimo es proporcionar informaciรณn posicional. Al final de cada bloque codificador hay una capa de *fusiรณn de parches* para crear representaciones jerรกrquicas. Las caracterรญsticas de cada grupo de parches vecinos se concatenan, y se aplica una capa lineal a las caracterรญsticas concatenadas para reducir el nรบmero de parches a una resoluciรณn de 1/4. Esto se convierte en la entrada al siguiente bloque codificador, donde se repite todo este proceso hasta que tengas caracterรญsticas de imagen con resoluciones de 1/8, 1/16 y 1/32. + +3. Un decodificador ligero toma el รบltimo mapa de caracterรญsticas (escala 1/32) del codificador y lo aumenta a una escala de 1/16. A partir de aquรญ, la caracterรญstica se pasa a un mรณdulo de *Fusiรณn Selectiva de Caracterรญsticas (SFF)*, que selecciona y combina caracterรญsticas locales y globales de un mapa de atenciรณn para cada caracterรญstica y luego la aumenta a 1/8. Este proceso se repite hasta que las caracterรญsticas decodificadas sean del mismo tamaรฑo que la imagen original. La salida se pasa a travรฉs de dos capas de convoluciรณn y luego se aplica una activaciรณn sigmoide para predecir la profundidad de cada pรญxel. + +## Procesamiento del lenguaje natural + +El Transformer fue diseรฑado inicialmente para la traducciรณn automรกtica, y desde entonces, prรกcticamente se ha convertido en la arquitectura predeterminada para resolver todas las tareas de procesamiento del lenguaje natural (NLP, por sus siglas en inglรฉs). Algunas tareas se prestan a la estructura del codificador del Transformer, mientras que otras son mรกs adecuadas para el decodificador. Todavรญa hay otras tareas que hacen uso de la estructura codificador-decodificador del Transformer. + +### Clasificaciรณn de texto + +[BERT](https://huggingface.co/docs/transformers/model_doc/bert) es un modelo que solo tiene codificador y es el primer modelo en implementar efectivamente la bidireccionalidad profunda para aprender representaciones mรกs ricas del texto al atender a las palabras en ambos lados. + +1. BERT utiliza la tokenizaciรณn [WordPiece](https://huggingface.co/docs/transformers/tokenizer_summary#wordpiece) para generar una incrustaciรณn de tokens del texto. Para diferenciar entre una sola oraciรณn y un par de oraciones, se agrega un token especial `[SEP]` para diferenciarlos. Tambiรฉn se agrega un token especial `[CLS]` al principio de cada secuencia de texto. La salida final con el token `[CLS]` se utiliza como la entrada a la cabeza de clasificaciรณn para tareas de clasificaciรณn. BERT tambiรฉn agrega una incrustaciรณn de segmento para indicar si un token pertenece a la primera o segunda oraciรณn en un par de oraciones. + +2. BERT se preentrena con dos objetivos: modelar el lenguaje enmascarado y predecir de prรณxima oraciรณn. En el modelado de lenguaje enmascarado, un cierto porcentaje de los tokens de entrada se enmascaran aleatoriamente, y el modelo necesita predecir estos. Esto resuelve el problema de la bidireccionalidad, donde el modelo podrรญa hacer trampa y ver todas las palabras y "predecir" la siguiente palabra. Los estados ocultos finales de los tokens de mรกscara predichos se pasan a una red feedforward con una softmax sobre el vocabulario para predecir la palabra enmascarada. + + El segundo objetivo de preentrenamiento es la predicciรณn de prรณxima oraciรณn. El modelo debe predecir si la oraciรณn B sigue a la oraciรณn A. La mitad del tiempo, la oraciรณn B es la siguiente oraciรณn, y la otra mitad del tiempo, la oraciรณn B es una oraciรณn aleatoria. La predicciรณn, ya sea que sea la prรณxima oraciรณn o no, se pasa a una red feedforward con una softmax sobre las dos clases (`EsSiguiente` y `NoSiguiente`). + +3. Las incrustaciones de entrada se pasan a travรฉs de mรบltiples capas codificadoras para producir algunos estados ocultos finales. + +Para usar el modelo preentrenado para clasificaciรณn de texto, se aรฑade una cabecera de clasificaciรณn de secuencia encima del modelo base de BERT. La cabecera de clasificaciรณn de secuencia es una capa lineal que acepta los estados ocultos finales y realiza una transformaciรณn lineal para convertirlos en logits. Se calcula la pรฉrdida de entropรญa cruzada entre los logits y el objetivo para encontrar la etiqueta mรกs probable. + +ยฟListo para probar la clasificaciรณn de texto? ยกConsulta nuestra guรญa completa de [clasificaciรณn de texto](https://huggingface.co/docs/transformers/tasks/sequence_classification) para aprender cรณmo ajustar DistilBERT y usarlo para inferencia! + +### Clasificaciรณn de tokens + +Para usar BERT en tareas de clasificaciรณn de tokens como el reconocimiento de entidades nombradas (NER), aรฑade una cabecera de clasificaciรณn de tokens encima del modelo base de BERT. La cabecera de clasificaciรณn de tokens es una capa lineal que acepta los estados ocultos finales y realiza una transformaciรณn lineal para convertirlos en logits. Se calcula la pรฉrdida de entropรญa cruzada entre los logits y cada token para encontrar la etiqueta mรกs probable. + +ยฟListo para probar la clasificaciรณn de tokens? ยกConsulta nuestra guรญa completa de [clasificaciรณn de tokens](https://huggingface.co/docs/transformers/tasks/token_classification) para aprender cรณmo ajustar DistilBERT y usarlo para inferencia! + +### Respuesta a preguntas + +Para usar BERT en la respuesta a preguntas, aรฑade una cabecera de clasificaciรณn de span encima del modelo base de BERT. Esta capa lineal acepta los estados ocultos finales y realiza una transformaciรณn lineal para calcular los logits de inicio y fin del `span` correspondiente a la respuesta. Se calcula la pรฉrdida de entropรญa cruzada entre los logits y la posiciรณn de la etiqueta para encontrar el span mรกs probable de texto correspondiente a la respuesta. + +ยฟListo para probar la respuesta a preguntas? ยกConsulta nuestra guรญa completa de [respuesta a preguntas](tasks/question_answering) para aprender cรณmo ajustar DistilBERT y usarlo para inferencia! + + + +๐Ÿ’ก ยกObserva lo fรกcil que es usar BERT para diferentes tareas una vez que ha sido preentrenado! ยกSolo necesitas aรฑadir una cabecera especรญfica al modelo preentrenado para manipular los estados ocultos en tu salida deseada! + + + +### Generaciรณn de texto + +[GPT-2](https://huggingface.co/docs/transformers/model_doc/gpt2) es un modelo que solo tiene decodificador y se preentrena en una gran cantidad de texto. Puede generar texto convincente (ยกaunque no siempre verdadero!) dado un estรญmulo y completar otras tareas de procesamiento del lenguaje natural como responder preguntas, a pesar de no haber sido entrenado explรญcitamente para ello. + +
+ +
+ +1. GPT-2 utiliza [codificaciรณn de pares de bytes (BPE)](https://huggingface.co/docs/transformers/tokenizer_summary#bytepair-encoding-bpe) para tokenizar palabras y generar una incrustaciรณn de token. Se aรฑaden incrustaciones posicionales a las incrustaciones de token para indicar la posiciรณn de cada token en la secuencia. Las incrustaciones de entrada se pasan a travรฉs de varios bloques decodificadores para producir algรบn estado oculto final. Dentro de cada bloque decodificador, GPT-2 utiliza una capa de *autoatenciรณn enmascarada*, lo que significa que GPT-2 no puede atender a los tokens futuros. Solo puede atender a los tokens a la izquierda. Esto es diferente al token [`mask`] de BERT porque, en la autoatenciรณn enmascarada, se utiliza una mรกscara de atenciรณn para establecer la puntuaciรณn en `0` para los tokens futuros. + +2. La salida del decodificador se pasa a una cabecera de modelado de lenguaje, que realiza una transformaciรณn lineal para convertir los estados ocultos en logits. La etiqueta es el siguiente token en la secuencia, que se crea desplazando los logits a la derecha en uno. Se calcula la pรฉrdida de entropรญa cruzada entre los logits desplazados y las etiquetas para obtener el siguiente token mรกs probable. + +El objetivo del preentrenamiento de GPT-2 se basa completamente en el [modelado de lenguaje causal](glossary#causal-language-modeling), prediciendo la siguiente palabra en una secuencia. Esto hace que GPT-2 sea especialmente bueno en tareas que implican la generaciรณn de texto. + +ยฟListo para probar la generaciรณn de texto? ยกConsulta nuestra guรญa completa de [modelado de lenguaje causal](tasks/language_modeling#modelado-de-lenguaje-causal) para aprender cรณmo ajustar DistilGPT-2 y usarlo para inferencia! + + + +Para obtener mรกs informaciรณn sobre la generaciรณn de texto, ยกconsulta la guรญa de [estrategias de generaciรณn de texto](https://huggingface.co/docs/transformers/generation_strategies)! + + + +### Resumir + +Los modelos codificador-decodificador como [BART](https://huggingface.co/docs/transformers/model_doc/bart) y [T5](https://huggingface.co/docs/transformers/model_doc/t5) estรกn diseรฑados para el patrรณn de secuencia a secuencia de una tarea de resumen. Explicaremos cรณmo funciona BART en esta secciรณn, y luego podrรกs probar el ajuste fino de T5 al final. + +
+ +
+ +1. La arquitectura del codificador de BART es muy similar a la de BERT y acepta una incrustaciรณn de token y posicional del texto. BART se preentrena corrompiendo la entrada y luego reconstruyรฉndola con el decodificador. A diferencia de otros codificadores con estrategias especรญficas de corrupciรณn, BART puede aplicar cualquier tipo de corrupciรณn. Sin embargo, la estrategia de corrupciรณn de *relleno de texto* funciona mejor. En el relleno de texto, varios fragmentos de texto se reemplazan con un **รบnico** token [`mask`]. Esto es importante porque el modelo tiene que predecir los tokens enmascarados, y le enseรฑa al modelo a predecir la cantidad de tokens faltantes. Las incrustaciones de entrada y los fragmentos enmascarados se pasan a travรฉs del codificador para producir algunos estados ocultos finales, pero a diferencia de BERT, BART no aรฑade una red feedforward final al final para predecir una palabra. + +2. La salida del codificador se pasa al decodificador, que debe predecir los tokens enmascarados y cualquier token no corrompido de la salida del codificador. Esto proporciona un contexto adicional para ayudar al decodificador a restaurar el texto original. La salida del decodificador se pasa a una cabeza de modelado de lenguaje, que realiza una transformaciรณn lineal para convertir los estados ocultos en logits. Se calcula la pรฉrdida de entropรญa cruzada entre los logits y la etiqueta, que es simplemente el token desplazado hacia la derecha. + +ยฟListo para probar la sumarizaciรณn? ยกConsulta nuestra guรญa completa de [Generaciรณn de resรบmenes](tasks/summarization) para aprender cรณmo ajustar T5 y usarlo para inferencia! + + + +Para obtener mรกs informaciรณn sobre la generaciรณn de texto, ยกconsulta la guรญa de [estrategias de generaciรณn de texto](https://huggingface.co/docs/transformers/generation_strategies)! + + + +### Traducciรณn + +La traducciรณn es otro ejemplo de una tarea de secuencia a secuencia, lo que significa que puedes usar un modelo codificador-decodificador como [BART](https://huggingface.co/docs/transformers/model_doc/bart) o [T5](https://huggingface.co/docs/transformers/model_doc/t5) para hacerlo. Explicaremos cรณmo funciona BART en esta secciรณn, y luego podrรกs probar el ajuste fino de T5 al final. + +BART se adapta a la traducciรณn aรฑadiendo un codificador separado inicializado aleatoriamente para mapear un idioma fuente a una entrada que pueda ser decodificada en el idioma objetivo. Las incrustaciones de este nuevo codificador se pasan al codificador preentrenado en lugar de las incrustaciones de palabras originales. El codificador de origen se entrena actualizando el codificador de origen, las incrustaciones posicionales y las incrustaciones de entrada con la pรฉrdida de entropรญa cruzada de la salida del modelo. Los parรกmetros del modelo estรกn congelados en este primer paso, y todos los parรกmetros del modelo se entrenan juntos en el segundo paso. + +Desde entonces, BART ha sido seguido por una versiรณn multilingรผe, mBART, destinada a la traducciรณn y preentrenada en muchos idiomas diferentes. + +ยฟListo para probar la traducciรณn? ยกConsulta nuestra guรญa completa de [traducciรณn](https://huggingface.co/docs/transformers/tasks/translation) para aprender cรณmo ajustar T5 y usarlo para inferencia! + + + +Para obtener mรกs informaciรณn sobre la generaciรณn de texto, ยกconsulta la guรญa de [estrategias de generaciรณn de texto](https://huggingface.co/docs/transformers/generation_strategies)! + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tokenizer_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tokenizer_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..731f16b3fedd8579438c3cc0fd4cd5bfabfc0e68 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/tokenizer_summary.md @@ -0,0 +1,175 @@ + + +# Descripciรณn general de los tokenizadores + +[[open-in-colab]] + +En esta pรกgina, veremos mรกs de cerca la tokenizaciรณn. + + + +Como vimos en [el tutorial de preprocesamiento](preprocessing), tokenizar un texto es dividirlo en palabras o subpalabras, que luego se convierten en indices o ids a travรฉs de una tabla de bรบsqueda. Convertir palabras o subpalabras en ids es sencillo, asรญ que en esta descripciรณn general, nos centraremos en dividir un texto en palabras o subpalabras (es decir, tokenizar un texto). Mรกs especรญficamente, examinaremos los tres principales tipos de tokenizadores utilizados en ๐Ÿค— Transformers: [Byte-Pair Encoding (BPE)](#byte-pair-encoding), [WordPiece](#wordpiece) y [SentencePiece](#sentencepiece), y mostraremos ejemplos de quรฉ tipo de tokenizador se utiliza en cada modelo. + +Ten en cuenta que en las pรกginas de los modelos, puedes ver la documentaciรณn del tokenizador asociado para saber quรฉ tipo de tokenizador se utilizรณ en el modelo preentrenado. Por ejemplo, si miramos [BertTokenizer](https://huggingface.co/docs/transformers/en/model_doc/bert#transformers.BertTokenizer), podemos ver que dicho modelo utiliza [WordPiece](#wordpiece). + +## Introducciรณn + +Dividir un texto en trozos mรกs pequeรฑos es mรกs difรญcil de lo que parece, y hay mรบltiples formas de hacerlo. Por ejemplo, veamos la oraciรณn `"Don't you love ๐Ÿค— Transformers? We sure do."` + + + +Una forma sencilla de tokenizar este texto es dividirlo por espacios, lo que darรญa: + +``` +["Don't", "you", "love", "๐Ÿค—", "Transformers?", "We", "sure", "do."] +``` + +Este es un primer paso sensato, pero si miramos los tokens `"Transformers?"` y `"do."`, notamos que las puntuaciones estรกn unidas a las palabras `"Transformer"` y `"do"`, lo que es subรณptimo. Deberรญamos tener en cuenta la puntuaciรณn para que un modelo no tenga que aprender una representaciรณn diferente de una palabra y cada posible sรญmbolo de puntuaciรณn que podrรญa seguirle, lo que explotarรญa el nรบmero de representaciones que el modelo tiene que aprender. Teniendo en cuenta la puntuaciรณn, tokenizar nuestro texto darรญa: + +``` +["Don", "'", "t", "you", "love", "๐Ÿค—", "Transformers", "?", "We", "sure", "do", "."] +``` + +Mejor. Sin embargo, es desventajoso cรณmo la tokenizaciรณn trata la palabra `"Don't"`. `"Don't"` significa `"do not"`, asรญ que serรญa mejor tokenizada como `["Do", "n't"]`. Aquรญ es donde las cosas comienzan a complicarse, y es la razon por la que cada modelo tiene su propio tipo de tokenizador. Dependiendo de las reglas que apliquemos para tokenizar un texto, se genera una salida tokenizada diferente para el mismo texto. Un modelo preentrenado solo se desempeรฑa correctamente si se le proporciona una entrada que fue tokenizada con las mismas reglas que se utilizaron para tokenizar sus datos de entrenamiento. + +[spaCy](https://spacy.io/) y [Moses](http://www.statmt.org/moses/?n=Development.GetStarted) son dos tokenizadores basados en reglas populares. Al aplicarlos en nuestro ejemplo, *spaCy* y *Moses* generarรญan algo como: + +``` +["Do", "n't", "you", "love", "๐Ÿค—", "Transformers", "?", "We", "sure", "do", "."] +``` + +Como se puede ver, aquรญ se utiliza tokenizaciรณn de espacio y puntuaciรณn, asรญ como tokenizaciรณn basada en reglas. La tokenizaciรณn de espacio y puntuaciรณn y la tokenizaciรณn basada en reglas son ambos ejemplos de tokenizaciรณn de palabras, que se define de manera simple como dividir oraciones en palabras. Aunque es la forma mรกs intuitiva de dividir textos en trozos mรกs pequeรฑos, este mรฉtodo de tokenizaciรณn puede generar problemas para corpus de texto masivos. En este caso, la tokenizaciรณn de espacio y puntuaciรณn suele generar un vocabulario muy grande (el conjunto de todas las palabras y tokens รบnicos utilizados). *Ej.*, [Transformer XL](https://huggingface.co/docs/transformers/main/en/model_doc/transfo-xl) utiliza tokenizaciรณn de espacio y puntuaciรณn, lo que resulta en un tamaรฑo de vocabulario de 267,735. + +Un tamaรฑo de vocabulario tan grande fuerza al modelo a tener una matriz de embeddings enormemente grande como capa de entrada y salida, lo que causa un aumento tanto en la complejidad de memoria como en la complejidad de tiempo. En general, los modelos de transformadores rara vez tienen un tamaรฑo de vocabulario mayor que 50,000, especialmente si estรกn preentrenados solo en un idioma. + +Entonces, si la simple tokenizaciรณn de espacios y puntuaciรณn es insatisfactoria, ยฟpor quรฉ no tokenizar simplemente en caracteres? + + + +Aunque la tokenizaciรณn de caracteres es muy simple y reducirรญa significativamente la complejidad de memoria y tiempo, hace que sea mucho mรกs difรญcil para el modelo aprender representaciones de entrada significativas. *Ej.* aprender una representaciรณn independiente del contexto para la letra `"t"` es mucho mรกs difรญcil que aprender una representaciรณn independiente del contexto para la palabra `"today"`. Por lo tanto, la tokenizaciรณn de caracteres suele acompaรฑarse de una pรฉrdida de rendimiento. Asรญ que para obtener lo mejor de ambos mundos, los modelos de transformadores utilizan un hรญbrido entre la tokenizaciรณn de nivel de palabra y de nivel de carรกcter llamada **tokenizaciรณn de subpalabras**. + +## Tokenizaciรณn de subpalabras + + + +Los algoritmos de tokenizaciรณn de subpalabras se basan en el principio de que las palabras frecuentemente utilizadas no deberรญan dividirse en subpalabras mรกs pequeรฑas, pero las palabras raras deberรญan descomponerse en subpalabras significativas. Por ejemplo, `"annoyingly"` podrรญa considerarse una palabra rara y descomponerse en `"annoying"` y `"ly"`. Ambas `"annoying"` y `"ly"` como subpalabras independientes aparecerรญan con mรกs frecuencia al mismo tiempo que se mantiene el significado de `"annoyingly"` por el significado compuesto de `"annoying"` y `"ly"`. Esto es especialmente รบtil en lenguas aglutinantes como el turco, donde puedes formar palabras complejas (casi) arbitrariamente largas concatenando subpalabras. + +La tokenizaciรณn de subpalabras permite al modelo tener un tamaรฑo de vocabulario razonable mientras puede aprender representaciones contextuales independientes significativas. Ademรกs, la tokenizaciรณn de subpalabras permite al modelo procesar palabras que nunca ha visto antes, descomponiรฉndolas en subpalabras conocidas. Por ejemplo, el tokenizador [BertTokenizer](https://huggingface.co/docs/transformers/en/model_doc/bert#transformers.BertTokenizer) tokeniza `"I have a new GPU!"` de la siguiente manera: + +```py +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +>>> tokenizer.tokenize("I have a new GPU!") +["i", "have", "a", "new", "gp", "##u", "!"] +``` + +Debido a que estamos considerando el modelo sin mayรบsculas, la oraciรณn se convirtiรณ a minรบsculas primero. Podemos ver que las palabras `["i", "have", "a", "new"]` estรกn presentes en el vocabulario del tokenizador, pero la palabra `"gpu"` no. En consecuencia, el tokenizador divide `"gpu"` en subpalabras conocidas: `["gp" y "##u"]`. `"##"` significa que el resto del token deberรญa adjuntarse al anterior, sin espacio (para decodificar o revertir la tokenizaciรณn). + +Como otro ejemplo, el tokenizador [XLNetTokenizer](https://huggingface.co/docs/transformers/en/model_doc/xlnet#transformers.XLNetTokenizer) tokeniza nuestro texto de ejemplo anterior de la siguiente manera: + +```py +>>> from transformers import XLNetTokenizer + +>>> tokenizer = XLNetTokenizer.from_pretrained("xlnet/xlnet-base-cased") +>>> tokenizer.tokenize("Don't you love ๐Ÿค— Transformers? We sure do.") +["โ–Don", "'", "t", "โ–you", "โ–love", "โ–", "๐Ÿค—", "โ–", "Transform", "ers", "?", "โ–We", "โ–sure", "โ–do", "."] +``` + +Hablaremos del significado de esos `"โ–"` cuando veamos [SentencePiece](#sentencepiece). Como se puede ver, la palabra rara `"Transformers"` se ha dividido en las subpalabras mรกs frecuentes `"Transform"` y `"ers"`. + +Ahora, veamos cรณmo funcionan los diferentes algoritmos de tokenizaciรณn de subpalabras. Ten en cuenta que todos esos algoritmos de tokenizaciรณn se basan en alguna forma de entrenamiento que usualmente se realiza en el corpus en el que se entrenarรก el modelo correspondiente. + + + +### Byte-Pair Encoding (BPE) + +La Codificaciรณn por Pares de Bytes (BPE por sus siglas en inglรฉs) fue introducida en [Neural Machine Translation of Rare Words with Subword Units (Sennrich et al., 2015)](https://huggingface.co/papers/1508.07909). BPE se basa en un pre-tokenizador que divide los datos de entrenamiento en palabras. La pre-tokenizaciรณn puede ser tan simple como la tokenizaciรณn por espacio, por ejemplo, [GPT-2](https://huggingface.co/docs/transformers/en/model_doc/gpt2), [RoBERTa](https://huggingface.co/docs/transformers/en/model_doc/roberta). La pre-tokenizaciรณn mรกs avanzada incluye la tokenizaciรณn basada en reglas, por ejemplo, [XLM](https://huggingface.co/docs/transformers/en/model_doc/xlm), [FlauBERT](https://huggingface.co/docs/transformers/en/model_doc/flaubert) que utiliza Moses para la mayorรญa de los idiomas, o [GPT](https://huggingface.co/docs/transformers/en/model_doc/openai-gpt) que utiliza spaCy y ftfy, para contar la frecuencia de cada palabra en el corpus de entrenamiento. + +Despuรฉs de la pre-tokenizaciรณn, se ha creado un conjunto de palabras รบnicas y ha determinado la frecuencia con la que cada palabra apareciรณ en los datos de entrenamiento. A continuaciรณn, BPE crea un vocabulario base que consiste en todos los sรญmbolos que aparecen en el conjunto de palabras รบnicas y aprende reglas de fusiรณn para formar un nuevo sรญmbolo a partir de dos sรญmbolos del vocabulario base. Lo hace hasta que el vocabulario ha alcanzado el tamaรฑo de vocabulario deseado. Tenga en cuenta que el tamaรฑo de vocabulario deseado es un hiperparรกmetro que se debe definir antes de entrenar el tokenizador. + +Por ejemplo, supongamos que despuรฉs de la pre-tokenizaciรณn, se ha determinado el siguiente conjunto de palabras, incluyendo su frecuencia: + +``` +("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) +``` + +En consecuencia, el vocabulario base es `["b", "g", "h", "n", "p", "s", "u"]`. Dividiendo todas las palabras en sรญmbolos del vocabulario base, obtenemos: + +``` +("h" "u" "g", 10), ("p" "u" "g", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "u" "g" "s", 5) +``` + +Luego, BPE cuenta la frecuencia de cada par de sรญmbolos posible y selecciona el par de sรญmbolos que ocurre con mรกs frecuencia. En el ejemplo anterior, `"h"` seguido de `"u"` estรก presente _10 + 5 = 15_ veces (10 veces en las 10 ocurrencias de `"hug"`, 5 veces en las 5 ocurrencias de `"hugs"`). Sin embargo, el par de sรญmbolos mรกs frecuente es `"u"` seguido de `"g"`, que ocurre _10 + 5 + 5 = 20_ veces en total. Por lo tanto, la primera regla de fusiรณn que aprende el tokenizador es agrupar todos los sรญmbolos `"u"` seguidos de un sรญmbolo `"g"` juntos. A continuaciรณn, `"ug"` se agrega al vocabulario. El conjunto de palabras entonces se convierte en + +``` +("h" "ug", 10), ("p" "ug", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "ug" "s", 5) +``` + +Seguidamente, BPE identifica el prรณximo par de sรญmbolos mรกs comรบn. Es `"u"` seguido de `"n"`, que ocurre 16 veces. `"u"`, `"n"` se fusionan en `"un"` y se agregan al vocabulario. El prรณximo par de sรญmbolos mรกs frecuente es `"h"` seguido de `"ug"`, que ocurre 15 veces. De nuevo, el par se fusiona y `"hug"` se puede agregar al vocabulario. + +En este momento, el vocabulario es `["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"]` y nuestro conjunto de palabras รบnicas se representa como: + +``` +("hug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("hug" "s", 5) +``` + +Suponiendo que el entrenamiento por Byte-Pair Encoding se detuviera en este punto, las reglas de combinaciรณn aprendidas se aplicarรญan entonces a nuevas palabras (siempre que esas nuevas palabras no incluyan sรญmbolos que no estuvieran en el vocabulario base). Por ejemplo, la palabra `"bug"` se tokenizarรญa como `["b", "ug"]`, pero `"mug"` se tokenizarรญa como `["", "ug"]` ya que el sรญmbolo `"m"` no estรก en el vocabulario base. En general, las letras individuales como `"m"` no se reemplazan por el sรญmbolo `""` porque los datos de entrenamiento usualmente incluyen al menos una ocurrencia de cada letra, pero es probable que suceda para caracteres especiales como los emojis. + +Como se mencionรณ anteriormente, el tamaรฑo del vocabulario, es decir, el tamaรฑo del vocabulario base + el nรบmero de combinaciones, es un hiperparรกmetro que se debe elegir. Por ejemplo, [GPT](https://huggingface.co/docs/transformers/en/model_doc/openai-gpt) tiene un tamaรฑo de vocabulario de 40,478 ya que tienen 478 caracteres base y eligieron detener el entrenamiento despuรฉs de 40,000 combinaciones. + +#### Byte-level BPE + +Un vocabulario base que incluya todos los caracteres base posibles puede ser bastante extenso si, por ejemplo, se consideran todos los caracteres unicode como caracteres base. Para tener un vocabulario base mejor, [GPT-2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) utiliza bytes como vocabulario base, lo que es un truco astuto para forzar el vocabulario base a ser de tamaรฑo 256 mientras se asegura de que cada carรกcter base estรฉ incluido en el vocabulario. Con algunas reglas adicionales para tratar con la puntuaciรณn, el tokenizador de GPT2 puede tokenizar cualquier texto sin la necesidad del sรญmbolo ``. [GPT-2](https://huggingface.co/docs/transformers/en/model_doc/gpt2) tiene un tamaรฑo de vocabulario de 50,257, lo que corresponde a los 256 tokens base de bytes, un token especial de fin de texto y los sรญmbolos aprendidos con 50,000 combinaciones. + + + +### WordPiece + +WordPiece es el algoritmo de tokenizaciรณn de subpalabras utilizado por [BERT](https://huggingface.co/docs/transformers/en/model_doc/bert), [DistilBERT](https://huggingface.co/docs/transformers/main/en/model_doc/distilbert) y [Electra](https://huggingface.co/docs/transformers/main/en/model_doc/electra). El algoritmo fue descrito en [Japanese and Korean Voice Search (Schuster et al., 2012)](https://static.googleusercontent.com/media/research.google.com/ja//pubs/archive/37842.pdf) y es muy similar a BPE. WordPiece inicializa el vocabulario para incluir cada carรกcter presente en los datos de entrenamiento y aprende progresivamente un nรบmero determinado de reglas de fusiรณn. A diferencia de BPE, WordPiece no elige el par de sรญmbolos mรกs frecuente, sino el que maximiza la probabilidad de los datos de entrenamiento una vez agregado al vocabulario. + +ยฟQuรฉ significa esto exactamente? Refiriรฉndonos al ejemplo anterior, maximizar la probabilidad de los datos de entrenamiento es equivalente a encontrar el par de sรญmbolos cuya probabilidad dividida entre las probabilidades de su primer sรญmbolo seguido de su segundo sรญmbolo es la mayor entre todos los pares de sรญmbolos. *Ej.* `"u"` seguido de `"g"` solo habrรญa sido combinado si la probabilidad de `"ug"` dividida entre `"u"` y `"g"` habrรญa sido mayor que para cualquier otro par de sรญmbolos. Intuitivamente, WordPiece es ligeramente diferente a BPE en que evalรบa lo que _pierde_ al fusionar dos sรญmbolos para asegurarse de que _valga la pena_. + + + +### Unigram + +Unigram es un algoritmo de tokenizaciรณn de subpalabras introducido en [Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates (Kudo, 2018)](https://huggingface.co/papers/1804.10959). A diferencia de BPE o WordPiece, Unigram inicializa su vocabulario base con un gran nรบmero de sรญmbolos y progresivamente recorta cada sรญmbolo para obtener un vocabulario mรกs pequeรฑo. El vocabulario base podrรญa corresponder, por ejemplo, a todas las palabras pre-tokenizadas y las subcadenas mรกs comunes. Unigram no se utiliza directamente para ninguno de los modelos transformers, pero se utiliza en conjunto con [SentencePiece](#sentencepiece). + +En cada paso de entrenamiento, el algoritmo Unigram define una pรฉrdida (a menudo definida como la probabilidad logarรญtmica) sobre los datos de entrenamiento dados el vocabulario actual y un modelo de lenguaje unigram. Luego, para cada sรญmbolo en el vocabulario, el algoritmo calcula cuรกnto aumentarรญa la pรฉrdida general si el sรญmbolo se eliminara del vocabulario. Luego, Unigram elimina un porcentaje `p` de los sรญmbolos cuyo aumento de pรฉrdida es el mรกs bajo (siendo `p` generalmente 10% o 20%), es decir, aquellos sรญmbolos que menos afectan la pรฉrdida general sobre los datos de entrenamiento. Este proceso se repite hasta que el vocabulario haya alcanzado el tamaรฑo deseado. El algoritmo Unigram siempre mantiene los caracteres base para que cualquier palabra pueda ser tokenizada. + +Debido a que Unigram no se basa en reglas de combinaciรณn (en contraste con BPE y WordPiece), el algoritmo tiene varias formas de tokenizar nuevo texto despuรฉs del entrenamiento. Por ejemplo, si un tokenizador Unigram entrenado exhibe el vocabulario: + +``` +["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"], +``` + +`"hugs"` podrรญa ser tokenizado tanto como `["hug", "s"]`, `["h", "ug", "s"]` o `["h", "u", "g", "s"]`. ยฟCuรกl elegir? Unigram guarda la probabilidad de cada token en el corpus de entrenamiento junto con el vocabulario, para que la probabilidad de que cada posible tokenizaciรณn pueda ser computada despuรฉs del entrenamiento. El algoritmo simplemente elige la tokenizaciรณn mรกs probable en la prรกctica, pero tambiรฉn ofrece la posibilidad de muestrear una posible tokenizaciรณn segรบn sus probabilidades. + +Esas probabilidades estรกn definidas por la pรฉrdida en la que se entrena el tokenizador. Suponiendo que los datos de entrenamiento constan de las palabras \\(x_{1}, \dots, x_{N}\\) y que el conjunto de todas las posibles tokenizaciones para una palabra \\(x_{i}\\) se define como \\(S(x_{i})\\), entonces la pรฉrdida general se define como: + +$$\mathcal{L} = -\sum_{i=1}^{N} \log \left ( \sum_{x \in S(x_{i})} p(x) \right )$$ + + + +### SentencePiece + +Todos los algoritmos de tokenizaciรณn descritos hasta ahora tienen el mismo problema: se asume que el texto de entrada utiliza espacios para separar palabras. Sin embargo, no todos los idiomas utilizan espacios para separar palabras. Una posible soluciรณn es utilizar pre-tokenizadores especรญficos del idioma, *ej.* [XLM](https://huggingface.co/docs/transformers/en/model_doc/xlm) utiliza un pre-tokenizador especรญfico para chino, japonรฉs y tailandรฉs. Para resolver este problema de manera mรกs general, [SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing (Kudo et al., 2018)](https://huggingface.co/papers/1808.06226) trata el texto de entrada como una corriente de entrada bruta, por lo que incluye el espacio en el conjunto de caracteres para utilizar. Luego utiliza el algoritmo BPE o unigram para construir el vocabulario apropiado. + +Por ejemplo, [`XLNetTokenizer`](https://huggingface.co/docs/transformers/en/model_doc/xlnet#transformers.XLNetTokenizer) utiliza SentencePiece, razรณn por la cual en el ejemplo anterior se incluyรณ el carรกcter `"โ–"` en el vocabulario. Decodificar con SentencePiece es muy fรกcil, ya que todos los tokens pueden simplemente concatenarse y `"โ–"` se reemplaza por un espacio. + +Todos los modelos transformers de nuestra biblioteca que utilizan SentencePiece lo utilizan en combinaciรณn con Unigram. Ejemplos de los modelos que utilizan SentencePiece son [ALBERT](https://huggingface.co/docs/transformers/en/model_doc/albert), [XLNet](https://huggingface.co/docs/transformers/en/model_doc/xlnet), [Marian](https://huggingface.co/docs/transformers/en/model_doc/marian) y [T5](https://huggingface.co/docs/transformers/main/en/model_doc/t5). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/torchscript.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/torchscript.md new file mode 100644 index 0000000000000000000000000000000000000000..93873fadcae80043f57af5418768e561244501ef --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/torchscript.md @@ -0,0 +1,167 @@ + + +# Exportar a TorchScript + + +Este es el comienzo de nuestros experimentos con TorchScript y todavรญa estamos explorando sus capacidades con modelos de variables de entrada. Es un tema de interรฉs para nosotros y profundizaremos en nuestro anรกlisis en las prรณximas versiones, con mรกs ejemplos de cรณdigo, una implementaciรณn mรกs flexible y comparativas de rendimiento comparando cรณdigos basados en Python con TorchScript compilado. + + + +De acuerdo con la documentaciรณn de TorchScript: + +> "TorchScript es una manera de crear modelos serializables y optimizables a partir del cรณdigo PyTorch." + +Hay dos mรณdulos de PyTorch, [JIT y TRACE](https://pytorch.org/docs/stable/jit.html), que permiten a los desarrolladores exportar sus modelos para ser reusados en otros programas, como los programas de C++ orientados a la eficiencia. + +Nosotros proveemos una interface que te permite exportar los modelos ๐Ÿค—Transformers a TorchScript para que puedan ser reusados en un entorno diferente al de los programas Python basados en PyTorch. Aquรญ explicamos como exportar y usar nuestros modelos utilizando TorchScript. + +Exportar un modelo requiere de dos cosas: + +- La instanciaciรณn del modelo con la bandera TorchScript. +- Un paso hacia adelante con entradas ficticias. + +Estas necesidades implican varias cosas de las que los desarrolladores deben tener cuidado, como se detalla a continuaciรณn. + +## Bandera TorchScript y pesos atados. + +La bandera `torchscript` es necesaria porque la mayorรญa de los modelos de lenguaje de ๐Ÿค—Transformers tienen pesos atados entre su `capa de incrustaciรณn` (`Embedding`) y su `capa de decodificaciรณn` (`Decoding`). TorchScript no te permite exportar modelos que tienen pesos atados, por lo que es necesario desatar y clonar los pesos de antemano. + +Los modelos instanciados con la bandera `torchscript` tienen su `capa de incrustaciรณn` (`Embedding`) y su `capa de decodificaciรณn` (`Decoding`) separadas, lo que significa que no deben ser entrenados mรกs adelante. Entrenar desincronizarรญa las dos capas, lo que llevarรญa a resultados inesperados. + +Esto no es asรญ para los modelos que no tienen una cabeza de modelo de lenguaje, ya que esos modelos no tienen pesos atados. Estos modelos pueden ser exportados de manera segura sin la bandera `torchscript`. + +## Entradas ficticias y longitudes estรกndar + +Las entradas ficticias se utilizan para un paso del modelo hacia adelante. Mientras los valores de las entradas se propagan a travรฉs de las capas, PyTorch realiza un seguimiento de las diferentes operaciones ejecutadas en cada tensor. Estas operaciones registradas se utilizan luego para crear *la traza* del modelo. +La traza se crea en relaciรณn con las dimensiones de las entradas. Por lo tanto, estรก limitada por las dimensiones de la entrada ficticia y no funcionarรก para ninguna otra longitud de secuencia o tamaรฑo de lote. Cuando se intenta con un tamaรฑo diferente, se genera el siguiente error: + +``` +`El tamaรฑo expandido del tensor (3) debe coincidir con el tamaรฑo existente (7) en la dimensiรณn no singleton 2`. +``` + +Recomendamos trazar el modelo con un tamaรฑo de entrada ficticio al menos tan grande como la entrada mรกs grande con la que se alimentarรก al modelo durante la inferencia. El relleno puede ayudar a completar los valores faltantes. Sin embargo, dado que el modelo se traza con un tamaรฑo de entrada mรกs grande, las dimensiones de la matriz tambiรฉn serรกn grandes, lo que resultarรก en mรกs cรกlculos. + +Ten cuidado con el nรบmero total de operaciones realizadas en cada entrada y sigue de cerca el rendimiento al exportar modelos con longitudes de secuencia variables. + +## Usando TorchScript en Python + +Esta secciรณn demuestra cรณmo guardar y cargar modelos, asรญ como cรณmo usar la traza para la inferencia. + +### Guardando un modelo + +Para exportar un `BertModel` con TorchScript, instancia `BertModel` a partir de la clase `BertConfig` y luego guรกrdalo en disco bajo el nombre de archivo `traced_bert.pt`: + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch + +enc = BertTokenizer.from_pretrained("bert-base-uncased") + +# Tokenizing input text +text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" +tokenized_text = enc.tokenize(text) + +# Masking one of the input tokens +masked_index = 8 +tokenized_text[masked_index] = "[MASK]" +indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) +segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] + +# Creating a dummy input +tokens_tensor = torch.tensor([indexed_tokens]) +segments_tensors = torch.tensor([segments_ids]) +dummy_input = [tokens_tensor, segments_tensors] + +# Initializing the model with the torchscript flag +# Flag set to True even though it is not necessary as this model does not have an LM Head. +config = BertConfig( + vocab_size_or_config_json_file=32000, + hidden_size=768, + num_hidden_layers=12, + num_attention_heads=12, + intermediate_size=3072, + torchscript=True, +) + +# Instantiating the model +model = BertModel(config) + +# The model needs to be in evaluation mode +model.eval() + +# If you are instantiating the model with *from_pretrained* you can also easily set the TorchScript flag +model = BertModel.from_pretrained("bert-base-uncased", torchscript=True) + +# Creating the trace +traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) +torch.jit.save(traced_model, "traced_bert.pt") +``` +### Cargando un modelo + +Ahora puedes cargar el `BertModel` guardado anteriormente, `traced_bert.pt`, desde el disco y usarlo en la entrada ficticia (`dummy_input`) previamente inicializada: + +```python +loaded_model = torch.jit.load("traced_bert.pt") +loaded_model.eval() + +all_encoder_layers, pooled_output = loaded_model(*dummy_input) +``` + +## Usando un modelo trazado para inferencia + +Utiliza el modelo trazado para inferencia utilizando su mรฉtodo `_call_` dunder: + +```python +traced_model(tokens_tensor, segments_tensors) +``` +## Despliega modelos TorchScript de Hugging Face en AWS con el Neuron SDK + +AWS introdujo la familia de instancias [Amazon EC2 Inf1](https://aws.amazon.com/ec2/instance-types/inf1/) para inferencia de aprendizaje automรกtico de alto rendimiento y bajo costo en la nube. Las instancias Inf1 estรกn alimentadas por el chip AWS Inferentia, un acelerador de hardware personalizado que se especializa en cargas de trabajo de inferencia de aprendizaje profundo. [AWS Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#) es el SDK para Inferentia que admite el trazado y la optimizaciรณn de modelos de transformers para implementaciรณn en Inf1. El SDK Neuron proporciona: + +1. Una API fรกcil de usar con un solo cambio de lรญnea de cรณdigo para trazar y optimizar un modelo TorchScript para inferencia en la nube. + +2. Optimizaciones de rendimiento listas para usar [para mejorar el rendimiento y el costo](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/>). + +3. Soporte para modelos de transformers de Hugging Face construidos tanto con [PyTorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html) como con [TensorFlow](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html). + +### Implicaciones + +Los modelos transformers basados en la arquitectura [BERT (Bidirectional Encoder Representations from Transformers)](https://huggingface.co/docs/transformers/main/model_doc/bert), o sus variantes como [distilBERT](https://huggingface.co/docs/transformers/main/model_doc/distilbert) y [roBERTa](https://huggingface.co/docs/transformers/main/model_doc/roberta), funcionan mejor en Inf1 para tareas no generativas como la respuesta a preguntas extractivas, la clasificaciรณn de secuencias y la clasificaciรณn de tokens. Sin embargo, las tareas de generaciรณn de texto aรบn pueden adaptarse para ejecutarse en Inf1 segรบn este [tutorial de AWS Neuron MarianMT](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html). Se puede encontrar mรกs informaciรณn sobre los modelos que se pueden convertir fรกcilmente para usar en Inferentia en la secciรณn de [Model Architecture Fit](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia) de la documentaciรณn de Neuron. + +### Dependencias + +El uso de AWS Neuron para convertir modelos requiere un [entorno de Neuron SDK](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide) que viene preconfigurado en [la AMI de AWS Deep Learning](https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html). + +### Convertir un modelo para AWS Neuron + +Convierte un modelo para AWS NEURON utilizando el mismo cรณdigo de [Uso de TorchScript en Python](torchscript#using-torchscript-in-python) para trazar un `BertModel`. Importa la extensiรณn del framework `torch.neuron` para acceder a los componentes del Neuron SDK a travรฉs de una API de Python: + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch +import torch.neuron +``` +Solo necesitas la linea sigueda: + +```diff +- torch.jit.trace(model, [tokens_tensor, segments_tensors]) ++ torch.neuron.trace(model, [token_tensor, segments_tensors]) +``` + +Esto permite que el Neuron SDK trace el modelo y lo optimice para las instancias Inf1. + +Para obtener mรกs informaciรณn sobre las caracterรญsticas, herramientas, tutoriales de ejemplo y รบltimas actualizaciones del AWS Neuron SDK, consulta [la documentaciรณn de AWS NeuronSDK](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/trainer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/trainer.md new file mode 100644 index 0000000000000000000000000000000000000000..4455521f5317c1f1d4feb1e48170edd311996d71 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/trainer.md @@ -0,0 +1,410 @@ + + +# El Trainer + +El [`Trainer`] es un bucle completo de entrenamiento y evaluaciรณn para modelos de PyTorch implementado en la biblioteca Transformers. Solo necesitas pasarle las piezas necesarias para el entrenamiento (modelo, tokenizador, conjunto de datos, funciรณn de evaluaciรณn, hiperparรกmetros de entrenamiento, etc.), y la clase [`Trainer`] se encarga del resto. Esto facilita comenzar a entrenar mรกs rรกpido sin tener que escribir manualmente tu propio bucle de entrenamiento. Pero al mismo tiempo, [`Trainer`] es muy personalizable y ofrece una gran cantidad de opciones de entrenamiento para que puedas adaptarlo a tus necesidades exactas de entrenamiento. + + + +Ademรกs de la clase [`Trainer`], Transformers tambiรฉn proporciona una clase [`Seq2SeqTrainer`] para tareas de secuencia a secuencia como traducciรณn o resumen. Tambiรฉn estรก la clase [~trl.SFTTrainer] de la biblioteca [TRL](https://hf.co/docs/trl) que envuelve la clase [`Trainer`] y estรก optimizada para entrenar modelos de lenguaje como Llama-2 y Mistral con tรฉcnicas autoregresivas. [`~trl.SFTTrainer`] tambiรฉn admite funciones como el empaquetado de secuencias, LoRA, cuantizaciรณn y DeepSpeed para escalar eficientemente a cualquier tamaรฑo de modelo. + +
+ +Siรฉntete libre de consultar [la referencia de API](./main_classes/trainer) para estas otras clases tipo [`Trainer`] para aprender mรกs sobre cuรกndo usar cada una. En general, [`Trainer`] es la opciรณn mรกs versรกtil y es apropiada para una amplia gama de tareas. [`Seq2SeqTrainer`] estรก diseรฑado para tareas de secuencia a secuencia y [`~trl.SFTTrainer`] estรก diseรฑado para entrenar modelos de lenguaje. + +
+ +Antes de comenzar, asegรบrate de tener instalado [Accelerate](https://hf.co/docs/accelerate), una biblioteca para habilitar y ejecutar el entrenamiento de PyTorch en entornos distribuidos. + +```bash +pip install accelerate + +# upgrade +pip install accelerate --upgrade +``` +Esta guรญa proporciona una visiรณn general de la clase [`Trainer`]. + +## Uso bรกsico + +[`Trainer`] incluye todo el cรณdigo que encontrarรญas en un bucle de entrenamiento bรกsico: +1. Realiza un paso de entrenamiento para calcular la pรฉrdida +2. Calcula los gradientes con el mรฉtodo [~accelerate.Accelerator.backward] +3. Actualiza los pesos basados en los gradientes +4. Repite este proceso hasta alcanzar un nรบmero predeterminado de รฉpocas + +La clase [`Trainer`] abstrae todo este cรณdigo para que no tengas que preocuparte por escribir manualmente un bucle de entrenamiento cada vez o si estรกs empezando con PyTorch y el entrenamiento. Solo necesitas proporcionar los componentes esenciales requeridos para el entrenamiento, como un modelo y un conjunto de datos, y la clase [`Trainer`] maneja todo lo demรกs. + +Si deseas especificar opciones de entrenamiento o hiperparรกmetros, puedes encontrarlos en la clase [`TrainingArguments`]. Por ejemplo, vamos a definir dรณnde guardar el modelo en output_dir y subir el modelo al Hub despuรฉs del entrenamiento con `push_to_hub=True`. + +```py +from transformers import TrainingArguments + +training_args = TrainingArguments( + output_dir="your-model", + learning_rate=2e-5, + per_device_train_batch_size=16, + per_device_eval_batch_size=16, + num_train_epochs=2, + weight_decay=0.01, + eval_strategy="epoch", + save_strategy="epoch", + load_best_model_at_end=True, + push_to_hub=True, +) +``` + +Pase `training_args` al [`Trainer`] con un modelo, un conjunto de datos o algo para preprocesar el conjunto de datos (dependiendo en el tipo de datos pueda ser un tokenizer, extractor de caracteristicas o procesor del imagen), un recopilador de datos y una funciรณn para calcular las mรฉtricas que desea rastrear durante el entrenamiento. + +Finalmente, llame [`~Trainer.train`] para empezar entrenamiento! + +```py +from transformers import Trainer + +trainer = Trainer( + model=model, + args=training_args, + train_dataset=dataset["train"], + eval_dataset=dataset["test"], + processing_class=tokenizer, + data_collator=data_collator, + compute_metrics=compute_metrics, +) + +trainer.train() +``` + +### Los puntos de control + +La clase [`Trainer`] guarda los puntos de control del modelo en el directorio especificado en el parรกmetro `output_dir` de [`TrainingArguments`]. Encontrarรกs los puntos de control guardados en una subcarpeta checkpoint-000 donde los nรบmeros al final corresponden al paso de entrenamiento. Guardar puntos de control es รบtil para reanudar el entrenamiento mรกs tarde. + +```py +# resume from latest checkpoint +trainer.train(resume_from_checkpoint=True) + +# resume from specific checkpoint saved in output directory +trainer.train(resume_from_checkpoint="your-model/checkpoint-1000") +``` + +Puedes guardar tus puntos de control (por defecto, el estado del optimizador no se guarda) en el Hub configurando `push_to_hub=True` en [`TrainingArguments`] para confirmar y enviarlos. Otras opciones para decidir cรณmo se guardan tus puntos de control estรกn configuradas en el parรกmetro [`hub_strategy`](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.hub_strategy): + +* hub_strategy="checkpoint" envรญa el รบltimo punto de control a una subcarpeta llamada "last-checkpoint" desde la cual puedes reanudar el entrenamiento. + +* hub_strategy="all_checkpoints" envรญa todos los puntos de control al directorio definido en `output_dir` (verรกs un punto de control por carpeta en tu repositorio de modelos). + +Cuando reanudas el entrenamiento desde un punto de control, el [`Trainer`] intenta mantener los estados de los generadores de nรบmeros aleatorios (RNG) de Python, NumPy y PyTorch iguales a como estaban cuando se guardรณ el punto de control. Pero debido a que PyTorch tiene varias configuraciones predeterminadas no determinรญsticas, no se garantiza que los estados de RNG sean los mismos. Si deseas habilitar la plena determinismo, echa un vistazo a la guรญa ["Controlling sources of randomness"](https://pytorch.org/docs/stable/notes/randomness#controlling-sources-of-randomness) para aprender quรฉ puedes habilitar para hacer que tu entrenamiento sea completamente determinista. Sin embargo, ten en cuenta que al hacer ciertas configuraciones deterministas, el entrenamiento puede ser mรกs lento. + +## Personaliza el Trainer + +Si bien la clase [`Trainer`] estรก diseรฑada para ser accesible y fรกcil de usar, tambiรฉn ofrece mucha capacidad de personalizaciรณn para usuarios mรกs aventureros. Muchos de los mรฉtodos del [`Trainer`] pueden ser subclasificados y sobrescritos para admitir la funcionalidad que deseas, sin tener que reescribir todo el bucle de entrenamiento desde cero para adaptarlo. Estos mรฉtodos incluyen: + +* [~Trainer.get_train_dataloader] crea un entrenamiento de DataLoader +* [~Trainer.get_eval_dataloader] crea una evaluaciรณn DataLoader +* [~Trainer.get_test_dataloader] crea una prueba de DataLoader +* [~Trainer.log] anota la informaciรณn de los objetos varios que observa el entrenamiento +* [~Trainer.create_optimizer_and_scheduler] crea un optimizador y la tasa programada de aprendizaje si no lo pasaron enย __init__; estos pueden ser personalizados independientes con [~Trainer.create_optimizer] y [~Trainer.create_scheduler] respectivamente +* [~Trainer.compute_loss] computa la pรฉrdida en lote con las aportes del entrenamiento +* [~Trainer.training_step] realiza el paso del entrenamiento +* [~Trainer.prediction_step] realiza la predicciรณn y paso de prueba +* [~Trainer.evaluate] evalua el modelo y da las metricas evaluativas +* [~Trainer.predict] hace las predicciones (con las metricas si hay etiquetas disponibles) en lote de prueba + +Por ejemplo, si deseas personalizar el mรฉtodo [`~Trainer.compute_loss`] para usar una pรฉrdida ponderada en su lugar, puedes hacerlo de la siguiente manera: + +```py +from torch import nn +from transformers import Trainer + +class CustomTrainer(Trainer): + def compute_loss(self, model, inputs, return_outputs=False): + labels = inputs.pop("labels") + # forward pass + outputs = model(**inputs) + logits = outputs.get("logits") + # compute custom loss for 3 labels with different weights + loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 2.0, 3.0], device=model.device)) + loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1)) + return (loss, outputs) if return_outputs else loss +``` +### Callbacks + +Otra opciรณn para personalizar el [`Trainer`] es utilizar [callbacks](callbacks). Los callbacks *no cambian nada* en el bucle de entrenamiento. Inspeccionan el estado del bucle de entrenamiento y luego ejecutan alguna acciรณn (detenciรณn anticipada, registro de resultados, etc.) segรบn el estado. En otras palabras, un callback no puede usarse para implementar algo como una funciรณn de pรฉrdida personalizada y necesitarรกs subclasificar y sobrescribir el mรฉtodo [`~Trainer.compute_loss`] para eso. + +Por ejemplo, si deseas agregar un callback de detenciรณn anticipada al bucle de entrenamiento despuรฉs de 10 pasos. + +```py +from transformers import TrainerCallback + +class EarlyStoppingCallback(TrainerCallback): + def __init__(self, num_steps=10): + self.num_steps = num_steps + + def on_step_end(self, args, state, control, **kwargs): + if state.global_step >= self.num_steps: + return {"should_training_stop": True} + else: + return {} + +``` +Luego, pรกsalo al parรกmetro `callback` del [`Trainer`]: + +```py +from transformers import Trainer + +trainer = Trainer( + model=model, + args=training_args, + train_dataset=dataset["train"], + eval_dataset=dataset["test"], + processing_class=tokenizer, + data_collator=data_collator, + compute_metrics=compute_metrics, + callback=[EarlyStoppingCallback()], +) +``` + +## Logging + + + +Comprueba el API referencia [logging](./main_classes/logging) para mas informaciรณn sobre los niveles differentes de logging. + + + +El [`Trainer`] estรก configurado a `logging.INFO` de forma predeterminada el cual informa errores, advertencias y otra informaciรณn basica. Un [`Trainer`] rรฉplica - en entornos distributos - estรก configurado a `logging.WARNING` el cual solamente informa errores y advertencias. Puedes cambiar el nivel de logging con los parametros [`log_level`](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.log_level) y [`log_level_replica`](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.log_level_replica) en [`TrainingArguments`]. + +Para configurar el nivel de registro para cada nodo, usa el parรกmetro [`log_on_each_node`](https://huggingface.co/docs/transformers/main/en/main_classes/trainer#transformers.TrainingArguments.log_on_each_node) para determinar si deseas utilizar el nivel de registro en cada nodo o solo en el nodo principal. + + + +[`Trainer`] establece el nivel de registro por separado para cada nodo en el mรฉtodo [`Trainer.init`], por lo que es posible que desees considerar establecer esto antes si estรกs utilizando otras funcionalidades de Transformers antes de crear el objeto [`Trainer`]. + + + +Por ejemplo, para establecer que tu cรณdigo principal y los mรณdulos utilicen el mismo nivel de registro segรบn cada nodo: + +```py +logger = logging.getLogger(__name__) + +logging.basicConfig( + format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", + datefmt="%m/%d/%Y %H:%M:%S", + handlers=[logging.StreamHandler(sys.stdout)], +) + +log_level = training_args.get_process_log_level() +logger.setLevel(log_level) +datasets.utils.logging.set_verbosity(log_level) +transformers.utils.logging.set_verbosity(log_level) + +trainer = Trainer(...) +``` + + + +Usa diferentes combinaciones de `log_level` y `log_level_replica` para configurar quรฉ se registra en cada uno de los nodos. + +```bash +my_app.py ... --log_level warning --log_level_replica error +``` + + + + +Agrega el parรกmetro `log_on_each_node 0` para entornos multi-nodo. + +```bash +my_app.py ... --log_level warning --log_level_replica error --log_on_each_node 0 + +# set to only report errors +my_app.py ... --log_level error --log_level_replica error --log_on_each_node 0 +``` + + + + +## NEFTune + +[NEFTune](https://hf.co/papers/2310.05914) es una tรฉcnica que puede mejorar el rendimiento al agregar ruido a los vectores de incrustaciรณn durante el entrenamiento. Para habilitarlo en [`Trainer`], establece el parรกmetro `neftune_noise_alpha` en [`TrainingArguments`] para controlar cuรกnto ruido se agrega. + +```py +from transformers import TrainingArguments, Trainer + +training_args = TrainingArguments(..., neftune_noise_alpha=0.1) +trainer = Trainer(..., args=training_args) +``` + +NEFTune se desactiva despuรฉs del entrenamiento para restaurar la capa de incrustaciรณn original y evitar cualquier comportamiento inesperado. + +## Accelerate y Trainer + +La clase [`Trainer`] estรก impulsada por [Accelerate](https://hf.co/docs/accelerate), una biblioteca para entrenar fรกcilmente modelos de PyTorch en entornos distribuidos con soporte para integraciones como [FullyShardedDataParallel (FSDP)](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/) y [DeepSpeed](https://www.deepspeed.ai/). + + + +Aprende mรกs sobre las estrategias de fragmentaciรณn FSDP, descarga de CPU y mรกs con el [`Trainer`] en la guรญa [Paralela de Datos Completamente Fragmentados](fsdp). + + + +Para usar Accelerate con [`Trainer`], ejecuta el comando [`accelerate.config`](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-config) para configurar el entrenamiento para tu entorno de entrenamiento. Este comando crea un `config_file.yaml` que se utilizarรก cuando inicies tu script de entrenamiento. Por ejemplo, algunas configuraciones de ejemplo que puedes configurar son: + + + + +```yml +compute_environment: LOCAL_MACHINE +distributed_type: MULTI_GPU +downcast_bf16: 'no' +gpu_ids: all +machine_rank: 0 #change rank as per the node +main_process_ip: 192.168.20.1 +main_process_port: 9898 +main_training_function: main +mixed_precision: fp16 +num_machines: 2 +num_processes: 8 +rdzv_backend: static +same_network: true +tpu_env: [] +tpu_use_cluster: false +tpu_use_sudo: false +use_cpu: false +``` + + + +```yml +compute_environment: LOCAL_MACHINE +distributed_type: FSDP +downcast_bf16: 'no' +fsdp_config: + fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP + fsdp_backward_prefetch_policy: BACKWARD_PRE + fsdp_forward_prefetch: true + fsdp_offload_params: false + fsdp_sharding_strategy: 1 + fsdp_state_dict_type: FULL_STATE_DICT + fsdp_sync_module_states: true + fsdp_transformer_layer_cls_to_wrap: BertLayer + fsdp_use_orig_params: true +machine_rank: 0 +main_training_function: main +mixed_precision: bf16 +num_machines: 1 +num_processes: 2 +rdzv_backend: static +same_network: true +tpu_env: [] +tpu_use_cluster: false +tpu_use_sudo: false +use_cpu: false +``` + + + +```yml +compute_environment: LOCAL_MACHINE +deepspeed_config: + deepspeed_config_file: /home/user/configs/ds_zero3_config.json + zero3_init_flag: true +distributed_type: DEEPSPEED +downcast_bf16: 'no' +machine_rank: 0 +main_training_function: main +num_machines: 1 +num_processes: 4 +rdzv_backend: static +same_network: true +tpu_env: [] +tpu_use_cluster: false +tpu_use_sudo: false +use_cpu: false +``` + + + +```yml +compute_environment: LOCAL_MACHINE +deepspeed_config: + gradient_accumulation_steps: 1 + gradient_clipping: 0.7 + offload_optimizer_device: cpu + offload_param_device: cpu + zero3_init_flag: true + zero_stage: 2 +distributed_type: DEEPSPEED +downcast_bf16: 'no' +machine_rank: 0 +main_training_function: main +mixed_precision: bf16 +num_machines: 1 +num_processes: 4 +rdzv_backend: static +same_network: true +tpu_env: [] +tpu_use_cluster: false +tpu_use_sudo: false +use_cpu: false + +``` + + + + + +El comando [`accelerate_launch`](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-launch) es la forma recomendada de lanzar tu script de entrenamiento en un sistema distribuido con Accelerate y [`Trainer`] con los parรกmetros especificados en `config_file.yaml`. Este archivo se guarda en la carpeta de cachรฉ de Accelerate y se carga automรกticamente cuando ejecutas `accelerate_launch`. + +Por ejemplo, para ejecutar el script de entrenamiento [`run_glue.py`](https://github.com/huggingface/transformers/blob/f4db565b695582891e43a5e042e5d318e28f20b8/examples/pytorch/text-classification/run_glue.py#L4) con la configuraciรณn de FSDP: + +```bash +accelerate launch \ + ./examples/pytorch/text-classification/run_glue.py \ + --model_name_or_path bert-base-cased \ + --task_name $TASK_NAME \ + --do_train \ + --do_eval \ + --max_seq_length 128 \ + --per_device_train_batch_size 16 \ + --learning_rate 5e-5 \ + --num_train_epochs 3 \ + --output_dir /tmp/$TASK_NAME/ \ + --overwrite_output_dir +``` + +Tambiรฉn puedes especificar los parรกmetros del archivo config_file.yaml directamente en la lรญnea de comandos: + +```bash +accelerate launch --num_processes=2 \ + --use_fsdp \ + --mixed_precision=bf16 \ + --fsdp_auto_wrap_policy=TRANSFORMER_BASED_WRAP \ + --fsdp_transformer_layer_cls_to_wrap="BertLayer" \ + --fsdp_sharding_strategy=1 \ + --fsdp_state_dict_type=FULL_STATE_DICT \ + ./examples/pytorch/text-classification/run_glue.py + --model_name_or_path bert-base-cased \ + --task_name $TASK_NAME \ + --do_train \ + --do_eval \ + --max_seq_length 128 \ + --per_device_train_batch_size 16 \ + --learning_rate 5e-5 \ + --num_train_epochs 3 \ + --output_dir /tmp/$TASK_NAME/ \ + --overwrite_output_dir +``` + +Consulta el tutorial [Lanzamiento de tus scripts con Accelerate](https://huggingface.co/docs/accelerate/basic_tutorials/launch) para obtener mรกs informaciรณn sobre `accelerate_launch` y las configuraciones personalizadas. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/training.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/training.md new file mode 100644 index 0000000000000000000000000000000000000000..f10f49d08997ac5f37b763c6b5f3cb284e400c88 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/es/training.md @@ -0,0 +1,371 @@ + + +# Fine-tuning a un modelo pre-entrenado + +[[open-in-colab]] + +El uso de un modelo pre-entrenado tiene importantes ventajas. Reduce los costos de computaciรณn, la huella de carbono y te permite utilizar modelos de รบltima generaciรณn sin tener que entrenar uno desde cero. + +* Fine-tuning a un modelo pre-entrenado con ๐Ÿค— Transformers [`Trainer`]. +* Fine-tuning a un modelo pre-entrenado en TensorFlow con Keras. +* Fine-tuning a un modelo pre-entrenado en PyTorch nativo. + + + +## Prepara un dataset + + + +Antes de aplicar fine-tuning a un modelo pre-entrenado, descarga un dataset y prepรกralo para el entrenamiento. El tutorial anterior nos enseรฑรณ cรณmo procesar los datos para el entrenamiento, y ahora es la oportunidad de poner a prueba estas habilidades. + +Comienza cargando el dataset de [Yelp Reviews](https://huggingface.co/datasets/yelp_review_full): + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("yelp_review_full") +>>> dataset[100] +{'label': 0, + 'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'} +``` + +Como ya sabes, necesitas un tokenizador para procesar el texto e incluir una estrategia para el padding y el truncamiento para manejar cualquier longitud de secuencia variable. Para procesar tu dataset en un solo paso, utiliza el mรฉtodo de ๐Ÿค— Datasets map para aplicar una funciรณn de preprocesamiento sobre todo el dataset: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") + + +>>> def tokenize_function(examples): +... return tokenizer(examples["text"], padding="max_length", truncation=True) + + +>>> tokenized_datasets = dataset.map(tokenize_function, batched=True) +``` + +Si lo deseas, puedes crear un subconjunto mรกs pequeรฑo del dataset completo para aplicarle fine-tuning y asรญ reducir el tiempo. + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + + + +## Fine-tuning con `Trainer` + + + +๐Ÿค— Transformers proporciona una clase [`Trainer`] optimizada para el entrenamiento de modelos de ๐Ÿค— Transformers, haciendo mรกs fรกcil el inicio del entrenamiento sin necesidad de escribir manualmente tu propio ciclo. La API del [`Trainer`] soporta una amplia gama de opciones de entrenamiento y caracterรญsticas como el logging, el gradient accumulation y el mixed precision. + +Comienza cargando tu modelo y especifica el nรบmero de labels previstas. A partir del [Card Dataset](https://huggingface.co/datasets/yelp_review_full#data-fields) de Yelp Review, que como ya sabemos tiene 5 labels: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + + + +Verรกs una advertencia acerca de que algunos de los pesos pre-entrenados no estรกn siendo utilizados y que algunos pesos estรกn siendo inicializados al azar. No te preocupes, esto es completamente normal. +El head/cabezal pre-entrenado del modelo BERT se descarta y se sustituye por un head de clasificaciรณn inicializado aleatoriamente. Puedes aplicar fine-tuning a este nuevo head del modelo en tu tarea de clasificaciรณn de secuencias haciendo transfer learning del modelo pre-entrenado. + + + +### Hiperparรกmetros de entrenamiento + +A continuaciรณn, crea una clase [`TrainingArguments`] que contenga todos los hiperparรกmetros que puedes ajustar asรญ como los indicadores para activar las diferentes opciones de entrenamiento. Para este tutorial puedes empezar con los [hiperparรกmetros](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments) de entrenamiento por defecto, pero siรฉntete libre de experimentar con ellos para encontrar tu configuraciรณn รณptima. + +Especifica dรณnde vas a guardar los checkpoints de tu entrenamiento: + +```py +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments(output_dir="test_trainer") +``` + +### Mรฉtricas + +El [`Trainer`] no evalรบa automรกticamente el rendimiento del modelo durante el entrenamiento. Tendrรกs que pasarle a [`Trainer`] una funciรณn para calcular y hacer un reporte de las mรฉtricas. La biblioteca de ๐Ÿค— Datasets proporciona una funciรณn de [`accuracy`](https://huggingface.co/metrics/accuracy) simple que puedes cargar con la funciรณn `load_metric` (ver este [tutorial](https://huggingface.co/docs/datasets/metrics) para mรกs informaciรณn): + +```py +>>> import numpy as np +>>> from datasets import load_metric + +>>> metric = load_metric("accuracy") +``` + +Define la funciรณn `compute` en `metric` para calcular el accuracy de tus predicciones. Antes de pasar tus predicciones a `compute`, necesitas convertir las predicciones a logits (recuerda que todos los modelos de ๐Ÿค— Transformers devuelven logits). + +```py +>>> def compute_metrics(eval_pred): +... logits, labels = eval_pred +... predictions = np.argmax(logits, axis=-1) +... return metric.compute(predictions=predictions, references=labels) +``` + +Si quieres controlar tus mรฉtricas de evaluaciรณn durante el fine-tuning, especifica el parรกmetro `eval_strategy` en tus argumentos de entrenamiento para que el modelo tenga en cuenta la mรฉtrica de evaluaciรณn al final de cada รฉpoca: + +```py +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments(output_dir="test_trainer", eval_strategy="epoch") +``` + +### Trainer + +Crea un objeto [`Trainer`] con tu modelo, argumentos de entrenamiento, datasets de entrenamiento y de prueba, y tu funciรณn de evaluaciรณn: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +A continuaciรณn, aplica fine-tuning a tu modelo llamando [`~transformers.Trainer.train`]: + +```py +>>> trainer.train() +``` + + + +## Fine-tuning con Keras + + + +Los modelos de ๐Ÿค— Transformers tambiรฉn permiten realizar el entrenamiento en TensorFlow con la API de Keras. Solo es necesario hacer algunos cambios antes de hacer fine-tuning. + +### Convierte el dataset al formato de TensorFlow + +El [`DefaultDataCollator`] junta los tensores en un batch para que el modelo se entrene en รฉl. Asegรบrate de especificar `return_tensors` para devolver los tensores de TensorFlow: + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +[`Trainer`] utiliza [`DataCollatorWithPadding`] por defecto por lo que no es necesario especificar explรญcitamente un intercalador de datos (data collator, en inglรฉs). + + + +A continuaciรณn, convierte los datasets tokenizados en datasets de TensorFlow con el mรฉtodo [`to_tf_dataset`](https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.to_tf_dataset). Especifica tus entradas en `columns` y tu etiqueta en `label_cols`: + +```py +>>> tf_train_dataset = small_train_dataset.to_tf_dataset( +... columns=["attention_mask", "input_ids", "token_type_ids"], +... label_cols="labels", +... shuffle=True, +... collate_fn=data_collator, +... batch_size=8, +... ) + +>>> tf_validation_dataset = small_eval_dataset.to_tf_dataset( +... columns=["attention_mask", "input_ids", "token_type_ids"], +... label_cols="labels", +... shuffle=False, +... collate_fn=data_collator, +... batch_size=8, +... ) +``` + +### Compila y ajusta + +Carguemos un modelo TensorFlow con el nรบmero esperado de labels: + +```py +>>> import tensorflow as tf +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + +A continuaciรณn, compila y aplica fine-tuning a tu modelo con [`fit`](https://keras.io/api/models/model_training_apis/) como lo harรญas con cualquier otro modelo de Keras: + +```py +>>> model.compile( +... optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5), +... loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), +... metrics=tf.metrics.SparseCategoricalAccuracy(), +... ) + +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3) +``` + + + +## Fine-tune en PyTorch nativo + + + +El [`Trainer`] se encarga del ciclo de entrenamiento y permite aplicar fine-tuning a un modelo en una sola lรญnea de cรณdigo. Para los que prefieran escribir su propio ciclo de entrenamiento, tambiรฉn pueden aplicar fine-tuning a un modelo de ๐Ÿค— Transformers en PyTorch nativo. + +En este punto, es posible que necesites reiniciar tu notebook o ejecutar el siguiente cรณdigo para liberar algo de memoria: + +```py +del model +del pytorch_model +del trainer +torch.cuda.empty_cache() +``` + +A continuaciรณn, haremos un post-procesamiento manual al `tokenized_dataset` y asรญ prepararlo para el entrenamiento. + +1. Elimina la columna de `text` porque el modelo no acepta texto en crudo como entrada: + + ```py + >>> tokenized_datasets = tokenized_datasets.remove_columns(["text"]) + ``` + +2. Cambia el nombre de la columna de `label` a `labels` porque el modelo espera que el argumento se llame `labels`: + + ```py + >>> tokenized_datasets = tokenized_datasets.rename_column("label", "labels") + ``` + +3. Establece el formato del dataset para devolver tensores PyTorch en lugar de listas: + + ```py + >>> tokenized_datasets.set_format("torch") + ``` + +A continuaciรณn, crea un subconjunto mรกs pequeรฑo del dataset como se ha mostrado anteriormente para acelerar el fine-tuning: + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + +### DataLoader + +Crea un `DataLoader` para tus datasets de entrenamiento y de prueba para poder iterar sobre batches de datos: + +```py +>>> from torch.utils.data import DataLoader + +>>> train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8) +>>> eval_dataloader = DataLoader(small_eval_dataset, batch_size=8) +``` + +Carga tu modelo con el nรบmero de labels previstas: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + +### Optimiza y programa el learning rate + +Crea un optimizador y el learning rate para aplicar fine-tuning al modelo. Vamos a utilizar el optimizador [`AdamW`](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) de PyTorch: + +```py +>>> from torch.optim import AdamW + +>>> optimizer = AdamW(model.parameters(), lr=5e-5) +``` + +Crea el learning rate desde el [`Trainer`]: + +```py +>>> from transformers import get_scheduler + +>>> num_epochs = 3 +>>> num_training_steps = num_epochs * len(train_dataloader) +>>> lr_scheduler = get_scheduler( +... name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps +... ) +``` + +Por รบltimo, especifica el `device` o entorno de ejecuciรณn para utilizar una GPU si tienes acceso a una. De lo contrario, el entrenamiento en una CPU puede llevarte varias horas en lugar de un par de minutos. + +```py +>>> import torch + +>>> device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +>>> model.to(device) +``` + + + +Consigue acceso gratuito a una GPU en la nube si es que no tienes este recurso de forma local con un notebook alojado en [Colaboratory](https://colab.research.google.com/) o [SageMaker StudioLab](https://studiolab.sagemaker.aws/). + + + +Genial, ยกahora podemos entrenar! ๐Ÿฅณ + +### Ciclo de entrenamiento + +Para hacer un seguimiento al progreso del entrenamiento, utiliza la biblioteca [tqdm](https://tqdm.github.io/) para aรฑadir una barra de progreso sobre el nรบmero de pasos de entrenamiento: + +```py +>>> from tqdm.auto import tqdm + +>>> progress_bar = tqdm(range(num_training_steps)) + +>>> model.train() +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... outputs = model(**batch) +... loss = outputs.loss +... loss.backward() + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +### Mรฉtricas + +De la misma manera que necesitas aรฑadir una funciรณn de evaluaciรณn al [`Trainer`], necesitas hacer lo mismo cuando escribas tu propio ciclo de entrenamiento. Pero en lugar de calcular y reportar la mรฉtrica al final de cada รฉpoca, esta vez acumularรกs todos los batches con [`add_batch`](https://huggingface.co/docs/datasets/package_reference/main_classes?highlight=add_batch#datasets.Metric.add_batch) y calcularรกs la mรฉtrica al final. + +```py +>>> metric = load_metric("accuracy") +>>> model.eval() +>>> for batch in eval_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... with torch.no_grad(): +... outputs = model(**batch) + +... logits = outputs.logits +... predictions = torch.argmax(logits, dim=-1) +... metric.add_batch(predictions=predictions, references=batch["labels"]) + +>>> metric.compute() +``` + + + +## Recursos adicionales + +Para mรกs ejemplos de fine-tuning consulta: + +- [๐Ÿค— Transformers Examples](https://github.com/huggingface/transformers/tree/main/examples) incluye scripts + para entrenar tareas comunes de NLP en PyTorch y TensorFlow. + +- [๐Ÿค— Transformers Notebooks](notebooks) contiene varios notebooks sobre cรณmo aplicar fine-tuning a un modelo para tareas especรญficas en PyTorch y TensorFlow. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_config.py b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_config.py new file mode 100644 index 0000000000000000000000000000000000000000..f3f59bf5202b3fdefbbee649a16dc9ef61aa0411 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_config.py @@ -0,0 +1,14 @@ +# docstyle-ignore +INSTALL_CONTENT = """ +# Installation de Transformers +! pip install transformers datasets evaluate accelerate +# Pour installer ร  partir du code source au lieu de la derniรจre version, commentez la commande ci-dessus et dรฉcommentez la suivante. +# ! pip install git+https://github.com/huggingface/transformers.git +""" + +notebook_first_cells = [{"type": "code", "content": INSTALL_CONTENT}] +black_avoid_patterns = { + "{processor_class}": "FakeProcessorClass", + "{model_class}": "FakeModelClass", + "{object_class}": "FakeObjectClass", +} diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_toctree.yml b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_toctree.yml new file mode 100644 index 0000000000000000000000000000000000000000..3b85bcab8284f6ee51190c1176521c0199c7e3d6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/_toctree.yml @@ -0,0 +1,34 @@ +- sections: + - local: index + title: ๐Ÿค— Transformers + - local: quicktour + title: Visite rapide + - local: installation + title: Installation + title: Dรฉmarrer +- sections: + - local: tutoriel_pipeline + title: Pipelines pour l'infรฉrence + - local: autoclass_tutorial + title: Chargement d'instances prรฉ-entraรฎnรฉes avec une AutoClass + - local: in_translation + title: Prรฉparation des donnรฉes + - local: in_translation + title: Fine-tune un modรจle prรฉ-entraรฎnรฉ + - local: run_scripts_fr + title: Entraรฎnement avec un script + - local: in_translation + title: Entraรฎnement distribuรฉ avec ๐Ÿค— Accelerate + - local: in_translation + title: Chargement et entraรฎnement des adaptateurs avec ๐Ÿค— PEFT + - local: in_translation + title: Partager un modรจle + - local: in_translation + title: Gรฉnรฉration avec LLMs + title: Tutoriels +- sections: + - local: task_summary + title: Ce que ๐Ÿค— Transformers peut faire + - local: tasks_explained + title: Comment ๐Ÿค— Transformers rรฉsout ces tรขches + title: Guides conceptuels diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/autoclass_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/autoclass_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..1f3baac07ce69993d49dc597b03a2d76f53add8a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/autoclass_tutorial.md @@ -0,0 +1,186 @@ + + +# Chargement d'instances prรฉ-entraรฎnรฉes avec une AutoClass + +Avec autant d'architectures Transformer diffรฉrentes, il peut รชtre difficile d'en crรฉer une pour votre ensemble de poids (aussi appelรฉs "weights" ou "checkpoint" en anglais). Dans l'idรฉe de crรฉer une librairie facile, simple et flexible ร  utiliser, ๐Ÿค— Transformers fournit une `AutoClass` qui infรจre et charge automatiquement l'architecture correcte ร  partir d'un ensemble de poids donnรฉ. La fonction `from_pretrained()` vous permet de charger rapidement un modรจle prรฉ-entraรฎnรฉ pour n'importe quelle architecture afin que vous n'ayez pas ร  consacrer du temps et des ressources ร  l'entraรฎnement d'un modรจle ร  partir de zรฉro. Produire un tel code indรฉpendant d'un ensemble de poids signifie que si votre code fonctionne pour un ensemble de poids, il fonctionnera avec un autre ensemble - tant qu'il a รฉtรฉ entraรฎnรฉ pour une tรขche similaire - mรชme si l'architecture est diffรฉrente. + + + +Rappel, l'architecture fait rรฉfรฉrence au squelette du modรจle et l'ensemble de poids contient les poids pour une architecture donnรฉe. Par exemple, [BERT](https://huggingface.co/google-bert/bert-base-uncased) est une architecture, tandis que `google-bert/bert-base-uncased` est un ensemble de poids. Le terme modรจle est gรฉnรฉral et peut signifier soit architecture soit ensemble de poids. + + + +Dans ce tutoriel, vous apprendrez ร : + + * Charger un tokenizer prรฉ-entraรฎnรฉ. + * Charger un processeur d'image prรฉ-entraรฎnรฉ. + * Charger un extracteur de caractรฉristiques prรฉ-entraรฎnรฉ. + * Charger un processeur prรฉ-entraรฎnรฉ. + * Charger un modรจle prรฉ-entraรฎnรฉ. + +## AutoTokenizer + +Quasiment toutes les tรขches de traitement du langage (NLP) commencent avec un tokenizer. Un tokenizer convertit votre texte initial dans un format qui peut รชtre traitรฉ par le modรจle. + +Chargez un tokenizer avec [`AutoTokenizer.from_pretrained`]: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") +``` + +Puis, transformez votre texte initial comme montrรฉ ci-dessous: + +```py +>>> sequence = "In a hole in the ground there lived a hobbit." +>>> print(tokenizer(sequence)) +{'input_ids': [101, 1999, 1037, 4920, 1999, 1996, 2598, 2045, 2973, 1037, 7570, 10322, 4183, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +## AutoImageProcessor + +Pour les tรขches de vision, un processeur d'image traite l'image pour la formater correctment. + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +``` + +## AutoBackbone + +
+ +
Un backbone Swin avec plusieurs รฉtapes pour produire une carte de caractรฉristiques.
+
+ +[`AutoBackbone`] vous permet d'utiliser des modรจles prรฉ-entraรฎnรฉs comme backbones pour obtenir des cartes de caractรฉristiques ร  partir de diffรฉrentes รฉtapes du backbone. Vous devez spรฉcifier l'un des paramรจtres suivants dans [`~PretrainedConfig.from_pretrained`] : + +* `out_indices` est l'index de la couche dont vous souhaitez obtenir la carte de caractรฉristiques +* `out_features` est le nom de la couche dont vous souhaitez obtenir la carte de caractรฉristiques + +Ces paramรจtres peuvent รชtre utilisรฉs de maniรจre interchangeable, mais si vous utilisez les deux, assurez-vous qu'ils sont alignรฉs l'un avec l'autre ! Si vous ne passez aucun de ces paramรจtres, le backbone renvoie la carte de caractรฉristiques de la derniรจre couche. + +
+ +
Une carte de caractรฉristiques de la premiรจre รฉtape du backbone. La partition de patch fait rรฉfรฉrence ร  la tige du modรจle.
+
+ +Par exemple, dans le diagramme ci-dessus, pour renvoyer la carte de caractรฉristiques de la premiรจre รฉtape du backbone Swin, vous pouvez dรฉfinir `out_indices=(1,)` : + +```py +>>> from transformers import AutoImageProcessor, AutoBackbone +>>> import torch +>>> from PIL import Image +>>> import requests +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> processor = AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224") +>>> model = AutoBackbone.from_pretrained("microsoft/swin-tiny-patch4-window7-224", out_indices=(1,)) + +>>> inputs = processor(image, return_tensors="pt") +>>> outputs = model(**inputs) +>>> feature_maps = outputs.feature_maps +``` + +Vous pouvez maintenant accรฉder ร  l'objet `feature_maps` de la premiรจre รฉtape du backbone : + + +```py +>>> list(feature_maps[0].shape) +[1, 96, 56, 56] +``` + +## AutoFeatureExtractor + +Pour les tรขches audio, un extracteur de caractรฉristiques (aussi appelรฉs "features" en anglais) traite le signal audio pour le formater correctement. + +Chargez un extracteur de caractรฉristiques avec [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained( +... "ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" +... ) +``` + +## AutoProcessor + +Les tรขches multimodales nรฉcessitent un processeur qui combine deux types d'outils de prรฉtraitement. Par exemple, le modรจle [LayoutLMV2](model_doc/layoutlmv2) nรฉcessite un processeur d'image pour traiter les images et un tokenizer pour traiter le texte ; un processeur combine les deux. + +Chargez un processeur avec [`AutoProcessor.from_pretrained`]: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") +``` + +## AutoModel + + + +Enfin, les classes `AutoModelFor` vous permettent de charger un modรจle prรฉ-entraรฎnรฉ pour une tรขche donnรฉe (voir [ici](model_doc/auto) pour une liste complรจte des tรขches disponibles). Par exemple, chargez un modรจle pour la classification de sรฉquence avec [`AutoModelForSequenceClassification.from_pretrained`]: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Rรฉutilisez facilement le mรชme ensemble de poids pour charger une architecture pour une tรขche diffรฉrente : + +```py +>>> from transformers import AutoModelForTokenClassification + +>>> model = AutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +Pour les modรจles PyTorch, la fonction `from_pretrained()` utilise `torch.load()` qui utilise `pickle` en interne et est connu pour รชtre non sรฉcurisรฉ. En gรฉnรฉral, ne chargez jamais un modรจle qui pourrait provenir d'une source non fiable, ou qui pourrait avoir รฉtรฉ altรฉrรฉ. Ce risque de sรฉcuritรฉ est partiellement attรฉnuรฉ pour les modรจles hรฉbergรฉs publiquement sur le Hugging Face Hub, qui sont [scannรฉs pour les logiciels malveillants](https://huggingface.co/docs/hub/security-malware) ร  chaque modification. Consultez la [documentation du Hub](https://huggingface.co/docs/hub/security) pour connaรฎtre les meilleures pratiques comme la [vรฉrification des modifications signรฉes](https://huggingface.co/docs/hub/security-gpg#signing-commits-with-gpg) avec GPG. + +Les points de contrรดle TensorFlow et Flax ne sont pas concernรฉs, et peuvent รชtre chargรฉs dans des architectures PyTorch en utilisant les arguments `from_tf` et `from_flax` de la fonction `from_pretrained` pour contourner ce problรจme. + + + +En gรฉnรฉral, nous recommandons d'utiliser les classes `AutoTokenizer` et `AutoModelFor` pour charger des instances prรฉ-entraรฎnรฉes de tokenizers et modรจles respectivement. Cela vous permettra de charger la bonne architecture ร  chaque fois. Dans le prochain [tutoriel](preprocessing), vous apprenez ร  utiliser un tokenizer, processeur d'image, extracteur de caractรฉristiques et processeur pour prรฉ-traiter un jeu de donnรฉes pour le fine-tuning. + + +Enfin, les classes `TFAutoModelFor` vous permettent de charger un modรจle prรฉ-entraรฎnรฉ pour une tรขche donnรฉe (voir [ici](model_doc/auto) pour une liste complรจte des tรขches disponibles). Par exemple, chargez un modรจle pour la classification de sรฉquence avec [`TFAutoModelForSequenceClassification.from_pretrained`]: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Rรฉutilisez facilement le mรชme ensemble de poids pour charger une architecture pour une tรขche diffรฉrente : + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +En gรฉnรฉral, nous recommandons d'utiliser les classes `AutoTokenizer` et `TFAutoModelFor` pour charger des instances prรฉ-entraรฎnรฉes de tokenizers et modรจles respectivement. Cela vous permettra de charger la bonne architecture ร  chaque fois. Dans le prochain [tutoriel](preprocessing), vous apprenez ร  utiliser un tokenizer, processeur d'image, extracteur de caractรฉristiques et processeur pour prรฉ-traiter un jeu de donnรฉes pour le fine-tuning. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/in_translation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/in_translation.md new file mode 100644 index 0000000000000000000000000000000000000000..910559ef6c9a0af10f6991c5464f52d4659f3426 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/in_translation.md @@ -0,0 +1,5 @@ + + +# Traduction en cours. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/index.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/index.md new file mode 100644 index 0000000000000000000000000000000000000000..61ca795cf99530ab54bf42f67420eb97db913a0f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/index.md @@ -0,0 +1,412 @@ + + +# ๐Ÿค— Transformers + +Apprentissage automatique de pointe pour [PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/), et [JAX](https://jax.readthedocs.io/en/latest/). + +๐Ÿค— Transformers fournit des API et des outils pour tรฉlรฉcharger et entraรฎner facilement des modรจles prรฉ-entraรฎnรฉs de pointe. L'utilisation de modรจles prรฉ-entraรฎnรฉs peut rรฉduire vos coรปts de calcul, votre empreinte carbone, et vous faire รฉconomiser le temps et les ressources nรฉcessaires pour entraรฎner un modรจle ร  partir de zรฉro. Ces modรจles prennent en charge des tรขches courantes dans diffรฉrentes modalitรฉs, telles que : + +๐Ÿ“ **Traitement automatique des langues**: classification de texte, reconnaissance d'entitรฉs, systรจme de question-rรฉponse, modรจle de langage, gรฉnรฉration de rรฉsumรฉ, traduction, question ร  choix multiples et gรฉnรฉration de texte.
+๐Ÿ–ผ๏ธ **Vision par ordinateur**: classification d'image, dรฉtection d'objet et segmentation.
+๐Ÿ—ฃ๏ธ **Audio**: reconnaissance automatique de la parole et classification audio.
+๐Ÿ™ **Multimodalitรฉ**: systรจme de question-rรฉponse avec des tableaux ou images, reconnaissance optique de caractรจres, extraction d'information depuis des documents scannรฉs et classification de vidรฉo. + +๐Ÿค— Transformers prend en charge l'interopรฉrabilitรฉ entre PyTorch, TensorFlow et JAX. Cela permet d'utiliser un framework diffรฉrent ร  chaque รฉtape de la vie d'un modรจle, par exemple entraรฎner un modรจle en trois lignes de code avec un framework, et le charger pour l'infรฉrence avec un autre. Les modรจles peuvent รฉgalement รชtre exportรฉs dans un format comme ONNX et TorchScript pour รชtre dรฉployรฉs dans des environnements de production. + +Rejoignez la communautรฉ grandissante sur le [Hub](https://huggingface.co/models), le [forum](https://discuss.huggingface.co/) ou [Discord](https://discord.com/invite/JfAtkvEtRb) dรจs aujourd'hui ! + +## Si vous cherchez un support personnalisรฉ de l'รฉquipe Hugging Face + + + HuggingFace Expert Acceleration Program + + +## Contenu + +La documentation est organisรฉe en 5 parties: + +- **DEMARRER** propose une visite rapide de la bibliothรจque et des instructions d'installation pour รชtre opรฉrationnel. +- **TUTORIELS** excellent point de dรฉpart pour les dรฉbutants. Cette section vous aidera ร  acquรฉrir les compรฉtences de base dont vous avez besoin pour commencer ร  utiliser la bibliothรจque. +- **GUIDES D'UTILISATION** pour diffรฉrentes tรขches comme par exemple le finetuning d'un modรจle prรฉ-entraรฎnรฉ pour la classification de texte ou comment crรฉer et partager votre propre modรจle. +- **GUIDES CONCEPTUELS** pour plus de discussions et d'explications sur les concepts et les idรฉes sous-jacentes aux modรจles, aux tรขches et ร  la philosophie de conception de ๐Ÿค— Transformers. +- **API** dรฉcrit toutes les classes et fonctions : + + - **CLASSES PRINCIPALES** dรฉtaille les classes les plus importantes comme la configuration, le modรจle, le tokenizer et le pipeline.. + - **MODELES** dรฉtaille les classes et les fonctions propres ร  chaque modรจle de la bibliothรจque. + - **UTILITAIRES INTERNES** dรฉtaille les classes et fonctions utilitaires utilisรฉes en interne. + +### Modรจles supportรฉs + + + +1. **[ALBERT](model_doc/albert)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. +1. **[ALIGN](model_doc/align)** (from Google Research) released with the paper [Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://huggingface.co/papers/2102.05918) by Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, Tom Duerig. +1. **[AltCLIP](model_doc/altclip)** (from BAAI) released with the paper [AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities](https://huggingface.co/papers/2211.06679) by Chen, Zhongzhi and Liu, Guang and Zhang, Bo-Wen and Ye, Fulong and Yang, Qinghong and Wu, Ledell. +1. **[Audio Spectrogram Transformer](model_doc/audio-spectrogram-transformer)** (from MIT) released with the paper [AST: Audio Spectrogram Transformer](https://huggingface.co/papers/2104.01778) by Yuan Gong, Yu-An Chung, James Glass. +1. **[BART](model_doc/bart)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://huggingface.co/papers/1910.13461) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer. +1. **[BARThez](model_doc/barthez)** (from ร‰cole polytechnique) released with the paper [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) by Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis. +1. **[BARTpho](model_doc/bartpho)** (from VinAI Research) released with the paper [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701) by Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen. +1. **[BEiT](model_doc/beit)** (from Microsoft) released with the paper [BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) by Hangbo Bao, Li Dong, Furu Wei. +1. **[BERT](model_doc/bert)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova. +1. **[BERT For Sequence Generation](model_doc/bert-generation)** (from Google) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[BERTweet](model_doc/bertweet)** (from VinAI Research) released with the paper [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) by Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen. +1. **[BigBird-Pegasus](model_doc/bigbird_pegasus)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[BigBird-RoBERTa](model_doc/big_bird)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[BioGpt](model_doc/biogpt)** (from Microsoft Research AI4Science) released with the paper [BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) by Renqian Luo, Liai Sun, Yingce Xia, Tao Qin, Sheng Zhang, Hoifung Poon and Tie-Yan Liu. +1. **[BiT](model_doc/bit)** (from Google AI) released with the paper [Big Transfer (BiT): General Visual Representation Learning](https://huggingface.co/papers/1912.11370) by Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai, Joan Puigcerver, Jessica Yung, Sylvain Gelly, Neil Houlsby. +1. **[Blenderbot](model_doc/blenderbot)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BlenderbotSmall](model_doc/blenderbot-small)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BLIP](model_doc/blip)** (from Salesforce) released with the paper [BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation](https://huggingface.co/papers/2201.12086) by Junnan Li, Dongxu Li, Caiming Xiong, Steven Hoi. +1. **[BLOOM](model_doc/bloom)** (from BigScience workshop) released by the [BigScience Workshop](https://bigscience.huggingface.co/). +1. **[BORT](model_doc/bort)** (from Alexa) released with the paper [Optimal Subarchitecture Extraction For BERT](https://huggingface.co/papers/2010.10499) by Adrian de Wynter and Daniel J. Perry. +1. **[BridgeTower](model_doc/bridgetower)** (from Harbin Institute of Technology/Microsoft Research Asia/Intel Labs) released with the paper [BridgeTower: Building Bridges Between Encoders in Vision-Language Representation Learning](https://huggingface.co/papers/2206.08657) by Xiao Xu, Chenfei Wu, Shachar Rosenman, Vasudev Lal, Wanxiang Che, Nan Duan. +1. **[ByT5](model_doc/byt5)** (from Google Research) released with the paper [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel. +1. **[CamemBERT](model_doc/camembert)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suรกrez*, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la Clergerie, Djamรฉ Seddah and Benoรฎt Sagot. +1. **[CANINE](model_doc/canine)** (from Google Research) released with the paper [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://huggingface.co/papers/2103.06874) by Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. +1. **[Chinese-CLIP](model_doc/chinese_clip)** (from OFA-Sys) released with the paper [Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese](https://huggingface.co/papers/2211.01335) by An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou. +1. **[CLIP](model_doc/clip)** (from OpenAI) released with the paper [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. +1. **[CLIPSeg](model_doc/clipseg)** (from University of Gรถttingen) released with the paper [Image Segmentation Using Text and Image Prompts](https://huggingface.co/papers/2112.10003) by Timo Lรผddecke and Alexander Ecker. +1. **[CodeGen](model_doc/codegen)** (from Salesforce) released with the paper [A Conversational Paradigm for Program Synthesis](https://huggingface.co/papers/2203.13474) by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong. +1. **[Conditional DETR](model_doc/conditional_detr)** (from Microsoft Research Asia) released with the paper [Conditional DETR for Fast Training Convergence](https://huggingface.co/papers/2108.06152) by Depu Meng, Xiaokang Chen, Zejia Fan, Gang Zeng, Houqiang Li, Yuhui Yuan, Lei Sun, Jingdong Wang. +1. **[ConvBERT](model_doc/convbert)** (from YituTech) released with the paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan. +1. **[ConvNeXT](model_doc/convnext)** (from Facebook AI) released with the paper [A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. +1. **[ConvNeXTV2](model_doc/convnextv2)** (from Facebook AI) released with the paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. +1. **[CPM](model_doc/cpm)** (from Tsinghua University) released with the paper [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) by Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun. +1. **[CTRL](model_doc/ctrl)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher. +1. **[CvT](model_doc/cvt)** (from Microsoft) released with the paper [CvT: Introducing Convolutions to Vision Transformers](https://huggingface.co/papers/2103.15808) by Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang. +1. **[Data2Vec](model_doc/data2vec)** (from Facebook) released with the paper [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) by Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli. +1. **[DeBERTa](model_doc/deberta)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[DeBERTa-v2](model_doc/deberta-v2)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[Decision Transformer](model_doc/decision_transformer)** (from Berkeley/Facebook/Google) released with the paper [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) by Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. +1. **[Deformable DETR](model_doc/deformable_detr)** (from SenseTime Research) released with the paper [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://huggingface.co/papers/2010.04159) by Xizhou Zhu, Weijie Su, Lewei Lu, Bin Li, Xiaogang Wang, Jifeng Dai. +1. **[DeiT](model_doc/deit)** (from Facebook) released with the paper [Training data-efficient image transformers & distillation through attention](https://huggingface.co/papers/2012.12877) by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervรฉ Jรฉgou. +1. **[DETA](model_doc/deta)** (from The University of Texas at Austin) released with the paper [NMS Strikes Back](https://huggingface.co/papers/2212.06137) by Jeffrey Ouyang-Zhang, Jang Hyun Cho, Xingyi Zhou, Philipp Krรคhenbรผhl. +1. **[DETR](model_doc/detr)** (from Facebook) released with the paper [End-to-End Object Detection with Transformers](https://huggingface.co/papers/2005.12872) by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko. +1. **[DialoGPT](model_doc/dialogpt)** (from Microsoft Research) released with the paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan. +1. **[DiNAT](model_doc/dinat)** (from SHI Labs) released with the paper [Dilated Neighborhood Attention Transformer](https://huggingface.co/papers/2209.15001) by Ali Hassani and Humphrey Shi. +1. **[DistilBERT](model_doc/distilbert)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://huggingface.co/papers/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers-research-projects/tree/main/distillation) and a German version of DistilBERT. +1. **[DiT](model_doc/dit)** (from Microsoft Research) released with the paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://huggingface.co/papers/2203.02378) by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei. +1. **[Donut](model_doc/donut)** (from NAVER), released together with the paper [OCR-free Document Understanding Transformer](https://huggingface.co/papers/2111.15664) by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park. +1. **[DPR](model_doc/dpr)** (from Facebook) released with the paper [Dense Passage Retrieval for Open-Domain Question Answering](https://huggingface.co/papers/2004.04906) by Vladimir Karpukhin, Barlas OฤŸuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih. +1. **[DPT](master/model_doc/dpt)** (from Intel Labs) released with the paper [Vision Transformers for Dense Prediction](https://huggingface.co/papers/2103.13413) by Renรฉ Ranftl, Alexey Bochkovskiy, Vladlen Koltun. +1. **[EfficientFormer](model_doc/efficientformer)** (from Snap Research) released with the paper [EfficientFormer: Vision Transformers at MobileNetSpeed](https://huggingface.co/papers/2206.01191) by Yanyu Li, Geng Yuan, Yang Wen, Ju Hu, Georgios Evangelidis, Sergey Tulyakov, Yanzhi Wang, Jian Ren. +1. **[ELECTRA](model_doc/electra)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://huggingface.co/papers/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning. +1. **[EncoderDecoder](model_doc/encoder-decoder)** (from Google Research) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[ERNIE](model_doc/ernie)** (from Baidu) released with the paper [ERNIE: Enhanced Representation through Knowledge Integration](https://huggingface.co/papers/1904.09223) by Yu Sun, Shuohuan Wang, Yukun Li, Shikun Feng, Xuyi Chen, Han Zhang, Xin Tian, Danxiang Zhu, Hao Tian, Hua Wu. +1. **[ESM](model_doc/esm)** (from Meta AI) are transformer protein language models. **ESM-1b** was released with the paper [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118) by Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus. **ESM-1v** was released with the paper [Language models enable zero-shot prediction of the effects of mutations on protein function](https://doi.org/10.1101/2021.07.09.450648) by Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rives. **ESM-2 and ESMFold** were released with the paper [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902) by Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives. +1. **[FastSpeech2Conformer](model_doc/fastspeech2_conformer)** (from ESPnet) released with the paper [Recent Developments On Espnet Toolkit Boosted By Conformer](https://huggingface.co/papers/2010.13956) by Pengcheng Guo, Florian Boyer, Xuankai Chang, Tomoki Hayashi, Yosuke Higuchi, Hirofumi Inaguma, Naoyuki Kamo, Chenda Li, Daniel Garcia-Romero, Jiatong Shi, Jing Shi, Shinji Watanabe, Kun Wei, Wangyou Zhang, and Yuekai Zhang. +1. **[FLAN-T5](model_doc/flan-t5)** (from Google AI) released in the repository [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints) by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei +1. **[FlauBERT](model_doc/flaubert)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://huggingface.co/papers/1912.05372) by Hang Le, Loรฏc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoรฎt Crabbรฉ, Laurent Besacier, Didier Schwab. +1. **[FLAVA](model_doc/flava)** (from Facebook AI) released with the paper [FLAVA: A Foundational Language And Vision Alignment Model](https://huggingface.co/papers/2112.04482) by Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, and Douwe Kiela. +1. **[FNet](model_doc/fnet)** (from Google Research) released with the paper [FNet: Mixing Tokens with Fourier Transforms](https://huggingface.co/papers/2105.03824) by James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. +1. **[Funnel Transformer](model_doc/funnel)** (from CMU/Google Brain) released with the paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://huggingface.co/papers/2006.03236) by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le. +1. **[GIT](model_doc/git)** (from Microsoft Research) released with the paper [GIT: A Generative Image-to-text Transformer for Vision and Language](https://huggingface.co/papers/2205.14100) by Jianfeng Wang, Zhengyuan Yang, Xiaowei Hu, Linjie Li, Kevin Lin, Zhe Gan, Zicheng Liu, Ce Liu, Lijuan Wang. +1. **[GLPN](model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://huggingface.co/papers/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim. +1. **[GPT](model_doc/openai-gpt)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://openai.com/research/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever. +1. **[GPT Neo](model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy. +1. **[GPT NeoX](model_doc/gpt_neox)** (from EleutherAI) released with the paper [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://huggingface.co/papers/2204.06745) by Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach +1. **[GPT NeoX Japanese](model_doc/gpt_neox_japanese)** (from ABEJA) released by Shinya Otani, Takayoshi Makabe, Anuj Arora, and Kyo Hattori. +1. **[GPT-2](model_doc/gpt2)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://openai.com/research/better-language-models/) by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever. +1. **[GPT-J](model_doc/gptj)** (from EleutherAI) released in the repository [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) by Ben Wang and Aran Komatsuzaki. +1. **[GPT-Sw3](model_doc/gpt-sw3)** (from AI-Sweden) released with the paper [Lessons Learned from GPT-SW3: Building the First Large-Scale Generative Language Model for Swedish](http://www.lrec-conf.org/proceedings/lrec2022/pdf/2022.lrec-1.376.pdf) by Ariel Ekgren, Amaru Cuba Gyllensten, Evangelia Gogoulou, Alice Heiman, Severine Verlinden, Joey ร–hman, Fredrik Carlsson, Magnus Sahlgren. +1. **[Graphormer](model_doc/graphormer)** (from Microsoft) released with the paper [Do Transformers Really Perform Bad for Graph Representation?](https://huggingface.co/papers/2106.05234) by Chengxuan Ying, Tianle Cai, Shengjie Luo, Shuxin Zheng, Guolin Ke, Di He, Yanming Shen, Tie-Yan Liu. +1. **[GroupViT](model_doc/groupvit)** (from UCSD, NVIDIA) released with the paper [GroupViT: Semantic Segmentation Emerges from Text Supervision](https://huggingface.co/papers/2202.11094) by Jiarui Xu, Shalini De Mello, Sifei Liu, Wonmin Byeon, Thomas Breuel, Jan Kautz, Xiaolong Wang. +1. **[Hubert](model_doc/hubert)** (from Facebook) released with the paper [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://huggingface.co/papers/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed. +1. **[I-BERT](model_doc/ibert)** (from Berkeley) released with the paper [I-BERT: Integer-only BERT Quantization](https://huggingface.co/papers/2101.01321) by Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer. +1. **[ImageGPT](model_doc/imagegpt)** (from OpenAI) released with the paper [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) by Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. +1. **[Jukebox](model_doc/jukebox)** (from OpenAI) released with the paper [Jukebox: A Generative Model for Music](https://huggingface.co/papers/2005.00341) by Prafulla Dhariwal, Heewoo Jun, Christine Payne, Jong Wook Kim, Alec Radford, Ilya Sutskever. +1. **[LayoutLM](model_doc/layoutlm)** (from Microsoft Research Asia) released with the paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://huggingface.co/papers/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou. +1. **[LayoutLMv2](model_doc/layoutlmv2)** (from Microsoft Research Asia) released with the paper [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://huggingface.co/papers/2012.14740) by Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. +1. **[LayoutLMv3](model_doc/layoutlmv3)** (from Microsoft Research Asia) released with the paper [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://huggingface.co/papers/2204.08387) by Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei. +1. **[LayoutXLM](model_doc/layoutxlm)** (from Microsoft Research Asia) released with the paper [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://huggingface.co/papers/2104.08836) by Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei. +1. **[LED](model_doc/led)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LeViT](model_doc/levit)** (from Meta AI) released with the paper [LeViT: A Vision Transformer in ConvNet's Clothing for Faster Inference](https://huggingface.co/papers/2104.01136) by Ben Graham, Alaaeldin El-Nouby, Hugo Touvron, Pierre Stock, Armand Joulin, Hervรฉ Jรฉgou, Matthijs Douze. +1. **[LiLT](model_doc/lilt)** (from South China University of Technology) released with the paper [LiLT: A Simple yet Effective Language-Independent Layout Transformer for Structured Document Understanding](https://huggingface.co/papers/2202.13669) by Jiapeng Wang, Lianwen Jin, Kai Ding. +1. **[Longformer](model_doc/longformer)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LongT5](model_doc/longt5)** (from Google AI) released with the paper [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://huggingface.co/papers/2112.07916) by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang. +1. **[LUKE](model_doc/luke)** (from Studio Ousia) released with the paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://huggingface.co/papers/2010.01057) by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto. +1. **[LXMERT](model_doc/lxmert)** (from UNC Chapel Hill) released with the paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://huggingface.co/papers/1908.07490) by Hao Tan and Mohit Bansal. +1. **[M-CTC-T](model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://huggingface.co/papers/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert. +1. **[M2M100](model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://huggingface.co/papers/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin. +1. **[MarianMT](model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jรถrg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team. +1. **[MarkupLM](model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://huggingface.co/papers/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei. +1. **[Mask2Former](model_doc/mask2former)** (from FAIR and UIUC) released with the paper [Masked-attention Mask Transformer for Universal Image Segmentation](https://huggingface.co/papers/2112.01527) by Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. +1. **[MaskFormer](model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://huggingface.co/papers/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. +1. **[mBART](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://huggingface.co/papers/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer. +1. **[mBART-50](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://huggingface.co/papers/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan. +1. **[Megatron-BERT](model_doc/megatron-bert)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. +1. **[Megatron-GPT2](model_doc/megatron_gpt2)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. +1. **[mLUKE](model_doc/mluke)** (from Studio Ousia) released with the paper [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://huggingface.co/papers/2110.08151) by Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka. +1. **[MobileBERT](model_doc/mobilebert)** (from CMU/Google Brain) released with the paper [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://huggingface.co/papers/2004.02984) by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou. +1. **[MobileNetV1](model_doc/mobilenet_v1)** (from Google Inc.) released with the paper [MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications](https://huggingface.co/papers/1704.04861) by Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam. +1. **[MobileNetV2](model_doc/mobilenet_v2)** (from Google Inc.) released with the paper [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://huggingface.co/papers/1801.04381) by Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen. +1. **[MobileViT](model_doc/mobilevit)** (from Apple) released with the paper [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://huggingface.co/papers/2110.02178) by Sachin Mehta and Mohammad Rastegari. +1. **[MPNet](model_doc/mpnet)** (from Microsoft Research) released with the paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://huggingface.co/papers/2004.09297) by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu. +1. **[MT5](model_doc/mt5)** (from Google AI) released with the paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://huggingface.co/papers/2010.11934) by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel. +1. **[MVP](model_doc/mvp)** (from RUC AI Box) released with the paper [MVP: Multi-task Supervised Pre-training for Natural Language Generation](https://huggingface.co/papers/2206.12131) by Tianyi Tang, Junyi Li, Wayne Xin Zhao and Ji-Rong Wen. +1. **[NAT](model_doc/nat)** (from SHI Labs) released with the paper [Neighborhood Attention Transformer](https://huggingface.co/papers/2204.07143) by Ali Hassani, Steven Walton, Jiachen Li, Shen Li, and Humphrey Shi. +1. **[Nezha](model_doc/nezha)** (from Huawei Noahโ€™s Ark Lab) released with the paper [NEZHA: Neural Contextualized Representation for Chinese Language Understanding](https://huggingface.co/papers/1909.00204) by Junqiu Wei, Xiaozhe Ren, Xiaoguang Li, Wenyong Huang, Yi Liao, Yasheng Wang, Jiashu Lin, Xin Jiang, Xiao Chen and Qun Liu. +1. **[NLLB](model_doc/nllb)** (from Meta) released with the paper [No Language Left Behind: Scaling Human-Centered Machine Translation](https://huggingface.co/papers/2207.04672) by the NLLB team. +1. **[Nystrรถmformer](model_doc/nystromformer)** (from the University of Wisconsin - Madison) released with the paper [Nystrรถmformer: A Nystrรถm-Based Algorithm for Approximating Self-Attention](https://huggingface.co/papers/2102.03902) by Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh. +1. **[OneFormer](model_doc/oneformer)** (from SHI Labs) released with the paper [OneFormer: One Transformer to Rule Universal Image Segmentation](https://huggingface.co/papers/2211.06220) by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. +1. **[OPT](master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://huggingface.co/papers/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al. +1. **[OWL-ViT](model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://huggingface.co/papers/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby. +1. **[Pegasus](model_doc/pegasus)** (from Google) released with the paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://huggingface.co/papers/1912.08777) by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu. +1. **[PEGASUS-X](model_doc/pegasus_x)** (from Google) released with the paper [Investigating Efficiently Extending Transformers for Long Input Summarization](https://huggingface.co/papers/2208.04347) by Jason Phang, Yao Zhao, and Peter J. Liu. +1. **[Perceiver IO](model_doc/perceiver)** (from Deepmind) released with the paper [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://huggingface.co/papers/2107.14795) by Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hรฉnaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, Joรฃo Carreira. +1. **[PhoBERT](model_doc/phobert)** (from VinAI Research) released with the paper [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) by Dat Quoc Nguyen and Anh Tuan Nguyen. +1. **[PLBart](model_doc/plbart)** (from UCLA NLP) released with the paper [Unified Pre-training for Program Understanding and Generation](https://huggingface.co/papers/2103.06333) by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang. +1. **[PoolFormer](model_doc/poolformer)** (from Sea AI Labs) released with the paper [MetaFormer is Actually What You Need for Vision](https://huggingface.co/papers/2111.11418) by Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng. +1. **[ProphetNet](model_doc/prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. +1. **[QDQBert](model_doc/qdqbert)** (from NVIDIA) released with the paper [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://huggingface.co/papers/2004.09602) by Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius Micikevicius. +1. **[RAG](model_doc/rag)** (from Facebook) released with the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://huggingface.co/papers/2005.11401) by Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Kรผttler, Mike Lewis, Wen-tau Yih, Tim Rocktรคschel, Sebastian Riedel, Douwe Kiela. +1. **[REALM](model_doc/realm.html)** (from Google Research) released with the paper [REALM: Retrieval-Augmented Language Model Pre-Training](https://huggingface.co/papers/2002.08909) by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang. +1. **[Reformer](model_doc/reformer)** (from Google Research) released with the paper [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) by Nikita Kitaev, ลukasz Kaiser, Anselm Levskaya. +1. **[RegNet](model_doc/regnet)** (from META Platforms) released with the paper [Designing Network Design Space](https://huggingface.co/papers/2003.13678) by Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollรกr. +1. **[RemBERT](model_doc/rembert)** (from Google Research) released with the paper [Rethinking embedding coupling in pre-trained language models](https://huggingface.co/papers/2010.12821) by Hyung Won Chung, Thibault Fรฉvry, Henry Tsai, M. Johnson, Sebastian Ruder. +1. **[ResNet](model_doc/resnet)** (from Microsoft Research) released with the paper [Deep Residual Learning for Image Recognition](https://huggingface.co/papers/1512.03385) by Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. +1. **[RoBERTa](model_doc/roberta)** (from Facebook), released together with the paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. +1. **[RoBERTa-PreLayerNorm](model_doc/roberta-prelayernorm)** (from Facebook) released with the paper [fairseq: A Fast, Extensible Toolkit for Sequence Modeling](https://huggingface.co/papers/1904.01038) by Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, Michael Auli. +1. **[RoCBert](model_doc/roc_bert)** (from WeChatAI) released with the paper [RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining](https://aclanthology.org/2022.acl-long.65.pdf) by HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou. +1. **[RoFormer](model_doc/roformer)** (from ZhuiyiTechnology), released together with the paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://huggingface.co/papers/2104.09864) by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu. +1. **[SegFormer](model_doc/segformer)** (from NVIDIA) released with the paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://huggingface.co/papers/2105.15203) by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo. +1. **[SEW](model_doc/sew)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SEW-D](model_doc/sew_d)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SpeechT5](model_doc/speecht5)** (from Microsoft Research) released with the paper [SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing](https://huggingface.co/papers/2110.07205) by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. +1. **[SpeechToTextTransformer](model_doc/speech_to_text)** (from Facebook), released together with the paper [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://huggingface.co/papers/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. +1. **[SpeechToTextTransformer2](model_doc/speech_to_text_2)** (from Facebook), released together with the paper [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://huggingface.co/papers/2104.06678) by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. +1. **[Splinter](model_doc/splinter)** (from Tel Aviv University), released together with the paper [Few-Shot Question Answering by Pretraining Span Selection](https://huggingface.co/papers/2101.00438) by Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. +1. **[SqueezeBERT](model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://huggingface.co/papers/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer. +1. **[Swin Transformer](model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. +1. **[Swin Transformer V2](model_doc/swinv2)** (from Microsoft) released with the paper [Swin Transformer V2: Scaling Up Capacity and Resolution](https://huggingface.co/papers/2111.09883) by Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo. +1. **[Swin2SR](model_doc/swin2sr)** (from University of Wรผrzburg) released with the paper [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://huggingface.co/papers/2209.11345) by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte. +1. **[SwitchTransformers](model_doc/switch_transformers)** (from Google) released with the paper [Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity](https://huggingface.co/papers/2101.03961) by William Fedus, Barret Zoph, Noam Shazeer. +1. **[T5](model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://huggingface.co/papers/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. +1. **[T5v1.1](model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. +1. **[Table Transformer](model_doc/table-transformer)** (from Microsoft Research) released with the paper [PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents](https://huggingface.co/papers/2110.00061) by Brandon Smock, Rohith Pesala, Robin Abraham. +1. **[TAPAS](model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://huggingface.co/papers/2004.02349) by Jonathan Herzig, Paweล‚ Krzysztof Nowak, Thomas Mรผller, Francesco Piccinno and Julian Martin Eisenschlos. +1. **[TAPEX](model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://huggingface.co/papers/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou. +1. **[Time Series Transformer](model_doc/time_series_transformer)** (from HuggingFace). +1. **[TimeSformer](model_doc/timesformer)** (from Facebook) released with the paper [Is Space-Time Attention All You Need for Video Understanding?](https://huggingface.co/papers/2102.05095) by Gedas Bertasius, Heng Wang, Lorenzo Torresani. +1. **[Trajectory Transformer](model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://huggingface.co/papers/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine +1. **[Transformer-XL](model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://huggingface.co/papers/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. +1. **[TrOCR](model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://huggingface.co/papers/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei. +1. **[UL2](model_doc/ul2)** (from Google Research) released with the paper [Unifying Language Learning Paradigms](https://huggingface.co/papers/2205.05131v1) by Yi Tay, Mostafa Dehghani, Vinh Q. Tran, Xavier Garcia, Dara Bahri, Tal Schuster, Huaixiu Steven Zheng, Neil Houlsby, Donald Metzler +1. **[UniSpeech](model_doc/unispeech)** (from Microsoft Research) released with the paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://huggingface.co/papers/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang. +1. **[UniSpeechSat](model_doc/unispeech-sat)** (from Microsoft Research) released with the paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://huggingface.co/papers/2110.05752) by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu. +1. **[UPerNet](model_doc/upernet)** (from Peking University) released with the paper [Unified Perceptual Parsing for Scene Understanding](https://huggingface.co/papers/1807.10221) by Tete Xiao, Yingcheng Liu, Bolei Zhou, Yuning Jiang, Jian Sun. +1. **[VAN](model_doc/van)** (from Tsinghua University and Nankai University) released with the paper [Visual Attention Network](https://huggingface.co/papers/2202.09741) by Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu. +1. **[VideoMAE](model_doc/videomae)** (from Multimedia Computing Group, Nanjing University) released with the paper [VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training](https://huggingface.co/papers/2203.12602) by Zhan Tong, Yibing Song, Jue Wang, Limin Wang. +1. **[ViLT](model_doc/vilt)** (from NAVER AI Lab/Kakao Enterprise/Kakao Brain) released with the paper [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://huggingface.co/papers/2102.03334) by Wonjae Kim, Bokyung Son, Ildoo Kim. +1. **[Vision Transformer (ViT)](model_doc/vit)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. +1. **[VisualBERT](model_doc/visual_bert)** (from UCLA NLP) released with the paper [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://huggingface.co/papers/1908.03557) by Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang. +1. **[ViT Hybrid](model_doc/vit_hybrid)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. +1. **[ViTMAE](model_doc/vit_mae)** (from Meta AI) released with the paper [Masked Autoencoders Are Scalable Vision Learners](https://huggingface.co/papers/2111.06377) by Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollรกr, Ross Girshick. +1. **[ViTMSN](model_doc/vit_msn)** (from Meta AI) released with the paper [Masked Siamese Networks for Label-Efficient Learning](https://huggingface.co/papers/2204.07141) by Mahmoud Assran, Mathilde Caron, Ishan Misra, Piotr Bojanowski, Florian Bordes, Pascal Vincent, Armand Joulin, Michael Rabbat, Nicolas Ballas. +1. **[Wav2Vec2](model_doc/wav2vec2)** (from Facebook AI) released with the paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://huggingface.co/papers/2006.11477) by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli. +1. **[Wav2Vec2-Conformer](model_doc/wav2vec2-conformer)** (from Facebook AI) released with the paper [FAIRSEQ S2T: Fast Speech-to-Text Modeling with FAIRSEQ](https://huggingface.co/papers/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Sravya Popuri, Dmytro Okhonko, Juan Pino. +1. **[Wav2Vec2Phoneme](model_doc/wav2vec2_phoneme)** (from Facebook AI) released with the paper [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://huggingface.co/papers/2109.11680) by Qiantong Xu, Alexei Baevski, Michael Auli. +1. **[WavLM](model_doc/wavlm)** (from Microsoft Research) released with the paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://huggingface.co/papers/2110.13900) by Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei. +1. **[Whisper](model_doc/whisper)** (from OpenAI) released with the paper [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf) by Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever. +1. **[X-CLIP](model_doc/xclip)** (from Microsoft Research) released with the paper [Expanding Language-Image Pretrained Models for General Video Recognition](https://huggingface.co/papers/2208.02816) by Bolin Ni, Houwen Peng, Minghao Chen, Songyang Zhang, Gaofeng Meng, Jianlong Fu, Shiming Xiang, Haibin Ling. +1. **[XGLM](model_doc/xglm)** (From Facebook AI) released with the paper [Few-shot Learning with Multilingual Language Models](https://huggingface.co/papers/2112.10668) by Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O'Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li. +1. **[XLM](model_doc/xlm)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://huggingface.co/papers/1901.07291) by Guillaume Lample and Alexis Conneau. +1. **[XLM-ProphetNet](model_doc/xlm-prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. +1. **[XLM-RoBERTa](model_doc/xlm-roberta)** (from Facebook AI), released together with the paper [Unsupervised Cross-lingual Representation Learning at Scale](https://huggingface.co/papers/1911.02116) by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmรกn, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov. +1. **[XLM-RoBERTa-XL](model_doc/xlm-roberta-xl)** (from Facebook AI), released together with the paper [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://huggingface.co/papers/2105.00572) by Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau. +1. **[XLNet](model_doc/xlnet)** (from Google/CMU) released with the paper [โ€‹XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://huggingface.co/papers/1906.08237) by Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. +1. **[XLS-R](model_doc/xls_r)** (from Facebook AI) released with the paper [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://huggingface.co/papers/2111.09296) by Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli. +1. **[XLSR-Wav2Vec2](model_doc/xlsr_wav2vec2)** (from Facebook AI) released with the paper [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://huggingface.co/papers/2006.13979) by Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli. +1. **[YOLOS](model_doc/yolos)** (from Huazhong University of Science & Technology) released with the paper [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://huggingface.co/papers/2106.00666) by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu. +1. **[YOSO](model_doc/yoso)** (from the University of Wisconsin - Madison) released with the paper [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://huggingface.co/papers/2111.09714) by Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh. + + +### Frameworks compatibles + +Le tableau ci-dessous reprรฉsente la prise en charge actuelle dans la bibliothรจque pour chacun de ces modรจles, qu'ils aient ou non un tokenizer Python (appelรฉ "slow"). Un tokenizer rapide ("fast") soutenu par la bibliothรจque ๐Ÿค— Tokenizers, qu'ils aient un support en Jax (via Flax), PyTorch, et/ou TensorFlow. + + + +| Modรจle | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | +|:-----------------------------:|:--------------:|:--------------:|:---------------:|:------------------:|:------------:| +| ALBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| AltCLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| Audio Spectrogram Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| BART | โœ… | โœ… | โœ… | โœ… | โœ… | +| BEiT | โŒ | โŒ | โœ… | โŒ | โœ… | +| BERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| Bert Generation | โœ… | โŒ | โœ… | โŒ | โŒ | +| BigBird | โœ… | โœ… | โœ… | โŒ | โœ… | +| BigBird-Pegasus | โŒ | โŒ | โœ… | โŒ | โŒ | +| BioGpt | โœ… | โŒ | โœ… | โŒ | โŒ | +| BiT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Blenderbot | โœ… | โœ… | โœ… | โœ… | โœ… | +| BlenderbotSmall | โœ… | โœ… | โœ… | โœ… | โœ… | +| BLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| BLOOM | โŒ | โœ… | โœ… | โŒ | โŒ | +| BridgeTower | โŒ | โŒ | โœ… | โŒ | โŒ | +| CamemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| CANINE | โœ… | โŒ | โœ… | โŒ | โŒ | +| Chinese-CLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| CLIP | โœ… | โœ… | โœ… | โœ… | โœ… | +| CLIPSeg | โŒ | โŒ | โœ… | โŒ | โŒ | +| CodeGen | โœ… | โœ… | โœ… | โŒ | โŒ | +| Conditional DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| ConvBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ConvNeXT | โŒ | โŒ | โœ… | โœ… | โŒ | +| CTRL | โœ… | โŒ | โœ… | โœ… | โŒ | +| CvT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Data2VecAudio | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecText | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecVision | โŒ | โŒ | โœ… | โœ… | โŒ | +| DeBERTa | โœ… | โœ… | โœ… | โœ… | โŒ | +| DeBERTa-v2 | โœ… | โœ… | โœ… | โœ… | โŒ | +| Decision Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Deformable DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeiT | โŒ | โŒ | โœ… | โœ… | โŒ | +| DETA | โŒ | โŒ | โœ… | โŒ | โŒ | +| DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DiNAT | โŒ | โŒ | โœ… | โŒ | โŒ | +| DistilBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| DonutSwin | โŒ | โŒ | โœ… | โŒ | โŒ | +| DPR | โœ… | โœ… | โœ… | โœ… | โŒ | +| DPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| EfficientFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ELECTRA | โœ… | โœ… | โœ… | โœ… | โœ… | +| Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| ERNIE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ESM | โœ… | โŒ | โœ… | โœ… | โŒ | +| FairSeq Machine-Translation | โœ… | โŒ | โœ… | โŒ | โŒ | +| FastSpeech2Conformer | โœ… | โŒ | โœ… | โŒ | โŒ | +| FlauBERT | โœ… | โŒ | โœ… | โœ… | โŒ | +| FLAVA | โŒ | โŒ | โœ… | โŒ | โŒ | +| FNet | โœ… | โœ… | โœ… | โŒ | โŒ | +| Funnel Transformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| GIT | โŒ | โŒ | โœ… | โŒ | โŒ | +| GLPN | โŒ | โŒ | โœ… | โŒ | โŒ | +| GPT Neo | โŒ | โŒ | โœ… | โŒ | โœ… | +| GPT NeoX | โŒ | โœ… | โœ… | โŒ | โŒ | +| GPT NeoX Japanese | โœ… | โŒ | โœ… | โŒ | โŒ | +| GPT-J | โŒ | โŒ | โœ… | โœ… | โœ… | +| GPT-Sw3 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Graphormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| GroupViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Hubert | โŒ | โŒ | โœ… | โœ… | โŒ | +| I-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ImageGPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Jukebox | โœ… | โŒ | โœ… | โŒ | โŒ | +| LayoutLM | โœ… | โœ… | โœ… | โœ… | โŒ | +| LayoutLMv2 | โœ… | โœ… | โœ… | โŒ | โŒ | +| LayoutLMv3 | โœ… | โœ… | โœ… | โœ… | โŒ | +| LED | โœ… | โœ… | โœ… | โœ… | โŒ | +| LeViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| LiLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Longformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| LongT5 | โŒ | โŒ | โœ… | โŒ | โœ… | +| LUKE | โœ… | โŒ | โœ… | โŒ | โŒ | +| LXMERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| M-CTC-T | โŒ | โŒ | โœ… | โŒ | โŒ | +| M2M100 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Marian | โœ… | โŒ | โœ… | โœ… | โœ… | +| MarkupLM | โœ… | โœ… | โœ… | โŒ | โŒ | +| Mask2Former | โŒ | โŒ | โœ… | โŒ | โŒ | +| MaskFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| MaskFormerSwin | โŒ | โŒ | โŒ | โŒ | โŒ | +| mBART | โœ… | โœ… | โœ… | โœ… | โœ… | +| Megatron-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| MobileNetV1 | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileNetV2 | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| MPNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| MT5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| MVP | โœ… | โœ… | โœ… | โŒ | โŒ | +| NAT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Nezha | โŒ | โŒ | โœ… | โŒ | โŒ | +| Nystrรถmformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OneFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OpenAI GPT | โœ… | โœ… | โœ… | โœ… | โŒ | +| OpenAI GPT-2 | โœ… | โœ… | โœ… | โœ… | โœ… | +| OPT | โŒ | โŒ | โœ… | โœ… | โœ… | +| OWL-ViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Pegasus | โœ… | โœ… | โœ… | โœ… | โœ… | +| PEGASUS-X | โŒ | โŒ | โœ… | โŒ | โŒ | +| Perceiver | โœ… | โŒ | โœ… | โŒ | โŒ | +| PLBart | โœ… | โŒ | โœ… | โŒ | โŒ | +| PoolFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| QDQBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| RAG | โœ… | โŒ | โœ… | โœ… | โŒ | +| REALM | โœ… | โœ… | โœ… | โŒ | โŒ | +| Reformer | โœ… | โœ… | โœ… | โŒ | โŒ | +| RegNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ResNet | โŒ | โŒ | โœ… | โœ… | โŒ | +| RetriBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| RoBERTa-PreLayerNorm | โŒ | โŒ | โœ… | โœ… | โœ… | +| RoCBert | โœ… | โŒ | โœ… | โŒ | โŒ | +| RoFormer | โœ… | โœ… | โœ… | โœ… | โœ… | +| SegFormer | โŒ | โŒ | โœ… | โœ… | โŒ | +| SEW | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW-D | โŒ | โŒ | โœ… | โŒ | โŒ | +| Speech Encoder decoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| Speech2Text | โœ… | โŒ | โœ… | โœ… | โŒ | +| Speech2Text2 | โœ… | โŒ | โŒ | โŒ | โŒ | +| SpeechT5 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Splinter | โœ… | โœ… | โœ… | โŒ | โŒ | +| SqueezeBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| Swin Transformer | โŒ | โŒ | โœ… | โœ… | โŒ | +| Swin Transformer V2 | โŒ | โŒ | โœ… | โŒ | โŒ | +| Swin2SR | โŒ | โŒ | โœ… | โŒ | โŒ | +| SwitchTransformers | โŒ | โŒ | โœ… | โŒ | โŒ | +| T5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Table Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| TAPAS | โœ… | โŒ | โœ… | โœ… | โŒ | +| Time Series Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| TimeSformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Trajectory Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Transformer-XL | โœ… | โŒ | โœ… | โœ… | โŒ | +| TrOCR | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeech | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeechSat | โŒ | โŒ | โœ… | โŒ | โŒ | +| UPerNet | โŒ | โŒ | โœ… | โŒ | โŒ | +| VAN | โŒ | โŒ | โœ… | โŒ | โŒ | +| VideoMAE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Vision Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| VisionTextDualEncoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| VisualBERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViT | โŒ | โŒ | โœ… | โœ… | โœ… | +| ViT Hybrid | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViTMAE | โŒ | โŒ | โœ… | โœ… | โŒ | +| ViTMSN | โŒ | โŒ | โœ… | โŒ | โŒ | +| Wav2Vec2 | โœ… | โŒ | โœ… | โœ… | โœ… | +| Wav2Vec2-Conformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| WavLM | โŒ | โŒ | โœ… | โŒ | โŒ | +| Whisper | โœ… | โŒ | โœ… | โœ… | โŒ | +| X-CLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| XGLM | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM | โœ… | โŒ | โœ… | โœ… | โŒ | +| XLM-ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| XLM-RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM-RoBERTa-XL | โŒ | โŒ | โœ… | โŒ | โŒ | +| XLNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| YOLOS | โŒ | โŒ | โœ… | โŒ | โŒ | +| YOSO | โŒ | โŒ | โœ… | โŒ | โŒ | + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/installation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/installation.md new file mode 100644 index 0000000000000000000000000000000000000000..da3e585df7aee38ae672fa46c6913b867c3d5c2f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/installation.md @@ -0,0 +1,258 @@ + + +# Installation + +Installez ๐Ÿค— Transformers pour n'importe quelle librairie d'apprentissage profond avec laquelle vous avez l'habitude de travaillez, configurez votre cache et configurez ๐Ÿค— Transformers pour un usage hors ligne (facultatif). + +๐Ÿค— Transformers est testรฉ avec Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+ et Flax. +Consulter les instructions d'installation ci-dessous pour la librairie d'apprentissage profond que vous utilisez: + + * Instructions d'installation pour [PyTorch](https://pytorch.org/get-started/locally/). + * Instructions d'installation pour [TensorFlow 2.0](https://www.tensorflow.org/install/pip). + * Instructions d'installation pour [Flax](https://flax.readthedocs.io/en/latest/). + +## Installation avec pip + +Vous devriez installer ๐Ÿค— Transformers dans un [environnement virtuel](https://docs.python.org/3/library/venv.html). +Si vous n'รชtes pas ร  l'aise avec les environnements virtuels, consultez ce [guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). +Utiliser un environnement virtuel permet de facilement gรฉrer diffรฉrents projets et d'รฉviter des erreurs de compatibilitรฉ entre les diffรฉrentes dรฉpendances. + +Commencez par crรฉer un environnement virtuel dans l'espace de travail de votre projet : + +```bash +python -m venv .env +``` + +Activez l'environnement virtuel. Sur Linux ou MacOs : + +```bash +source .env/bin/activate +``` + +Activez l'environnement virtuel sur Windows : + +```bash +.env/Scripts/activate +``` + +Maintenant, ๐Ÿค— Transformers peut รชtre installรฉ avec la commande suivante : + +```bash +pip install transformers +``` + +Pour une utilisation avec CPU seulement, ๐Ÿค— Transformers et la librairie d'apprentissage profond de votre choix peuvent รชtre installรฉs en une seule ligne. +Par exemple, installez ๐Ÿค— Transformers et PyTorch avec la commande suivante : + +```bash +pip install 'transformers[torch]' +``` + +๐Ÿค— Transformers et TensorFlow 2.0 : + +```bash +pip install 'transformers[tf-cpu]' +``` + + + +Pour les architectures mac M1 / ARM + +Vous devez installer les outils suivants avant d'installer TensorFLow 2.0 + +```bash +brew install cmake +brew install pkg-config +``` + + + +๐Ÿค— Transformers et Flax : + +```bash +pip install 'transformers[flax]' +``` + +Vรฉrifiez que ๐Ÿค— Transformers a bien รฉtรฉ installรฉ avec la commande suivante. La commande va tรฉlรฉcharger un modรจle prรฉ-entraรฎnรฉ : + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" +``` + +Le label et score sont ensuite affichรฉs : + +```bash +[{'label': 'POSITIVE', 'score': 0.9998704791069031}] +``` + +## Installation depuis le code source + +Installez ๐Ÿค— Transformers depuis le code source avec la commande suivante : + +```bash +pip install git+https://github.com/huggingface/transformers +``` + +Cette commande installe la version depuis la branche `main` au lieu de la derniรจre version stable. La version de la branche `main` est utile pour avoir les derniers dรฉveloppements. Par exemple, si un bug a รฉtรฉ rรฉsolu depuis la derniรจre version stable mais n'a pas encore รฉtรฉ publiรฉ officiellement. Cependant, cela veut aussi dire que la version de la branche `main` n'est pas toujours stable. Nous nous efforรงons de maintenir la version de la branche `main` opรฉrationnelle, et la plupart des problรจmes sont gรฉnรฉralement rรฉsolus en l'espace de quelques heures ou d'un jour. Si vous recontrez un problรจme, n'hรฉsitez pas ร  crรฉer une [Issue](https://github.com/huggingface/transformers/issues) pour que l'on puisse trouver une solution au plus vite ! + +Vรฉrifiez que ๐Ÿค— Transformers a bien รฉtรฉ installรฉ avec la commande suivante : + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" +``` + +## Installation modifiable + +Vous aurez besoin d'une installation modifiable si vous le souhaitez : + + * Utiliser la version de la branche `main` du code source. + * Contribuer ร  ๐Ÿค— Transformers et vouler tester vos modifications du code source. + +Clonez le projet et installez ๐Ÿค— Transformers avec les commandes suivantes : + +```bash +git clone https://github.com/huggingface/transformers.git +cd transformers +pip install -e . +``` + +Ces commandes crรฉent des liens entre le dossier oรน le projet a รฉtรฉ clonรฉ et les chemins de vos librairies Python. Python regardera maintenant dans le dossier que vous avez clonรฉ en plus des dossiers oรน sont installรฉes vos autres librairies. Par exemple, si vos librairies Python sont installรฉes dans `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python cherchera aussi dans le dossier oรน vous avez clonรฉ : `~/transformers/`. + + + +Vous devez garder le dossier `transformers` si vous voulez continuer d'utiliser la librairie. + + + +Maintenant, vous pouvez facilement mettre ร  jour votre clone avec la derniรจre version de ๐Ÿค— Transformers en utilisant la commande suivante : + +```bash +cd ~/transformers/ +git pull +``` + +Votre environnement Python utilisera la version de la branche `main` lors de la prochaine exรฉcution. + +## Installation avec conda + +Installation via le canal `conda-forge` de conda : + +```bash +conda install conda-forge::transformers +``` + +## Configuration du cache + +Les modรจles prรฉ-entraรฎnรฉs sont tรฉlรฉchargรฉs et mis en cache localement dans le dossier suivant : `~/.cache/huggingface/hub`. C'est le dossier par dรฉfaut donnรฉ par la variable d'environnement `TRANSFORMERS_CACHE`. Sur Windows, le dossier par dรฉfaut est `C:\Users\nom_utilisateur\.cache\huggingface\hub`. Vous pouvez modifier les variables d'environnement indiquรฉes ci-dessous - par ordre de prioritรฉ - pour spรฉcifier un dossier de cache diffรฉrent : + +1. Variable d'environnement (par dรฉfaut) : `HF_HUB_CACHE` ou `TRANSFORMERS_CACHE`. +2. Variable d'environnement : `HF_HOME`. +3. Variable d'environnement : `XDG_CACHE_HOME` + `/huggingface`. + + + +๐Ÿค— Transformers utilisera les variables d'environnement `PYTORCH_TRANSFORMERS_CACHE` ou `PYTORCH_PRETRAINED_BERT_CACHE` si vous utilisez une version prรฉcรฉdente de cette librairie et avez dรฉfini ces variables d'environnement, sauf si vous spรฉcifiez la variable d'environnement `TRANSFORMERS_CACHE`. + + + +## Mode hors ligne + +๐Ÿค— Transformers peut fonctionner dans un environnement cloisonnรฉ ou hors ligne en n'utilisant que des fichiers locaux. Dรฉfinissez la variable d'environnement `HF_HUB_OFFLINE=1` pour activer ce mode. + + + +Ajoutez [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/) ร  votre processus d'entraรฎnement hors ligne en dรฉfinissant la variable d'environnement `HF_DATASETS_OFFLINE=1`. + + + +```bash +HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +Le script devrait maintenant s'exรฉcuter sans rester en attente ou attendre une expiration, car il n'essaiera pas de tรฉlรฉcharger des modรจle sur le Hub. + +Vous pouvez aussi รฉviter de tรฉlรฉcharger un modรจle ร  chaque appel de la fonction [`~PreTrainedModel.from_pretrained`] en utilisant le paramรจtre [local_files_only]. Seuls les fichiers locaux sont chargรฉs lorsque ce paramรจtre est activรฉ (c.-ร -d. `local_files_only=True`) : + +```py +from transformers import T5Model + +model = T5Model.from_pretrained("./path/to/local/directory", local_files_only=True) +``` + +### Rรฉcupรฉrer des modรจles et des tokenizers pour une utilisation hors ligne + +Une autre option pour utiliser ๐Ÿค— Transformers hors ligne est de tรฉlรฉcharger les fichiers ร  l'avance, puis d'utiliser les chemins locaux lorsque vous en avez besoin en mode hors ligne. Il existe trois faรงons de faire cela : + + * Tรฉlรฉchargez un fichier via l'interface utilisateur sur le [Model Hub](https://huggingface.co/models) en cliquant sur l'icรดne โ†“. + + ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) + + * Utilisez les fonctions [`PreTrainedModel.from_pretrained`] et [`PreTrainedModel.save_pretrained`] : + + 1. Tรฉlรฉchargez vos fichiers ร  l'avance avec [`PreTrainedModel.from_pretrained`]: + + ```py + >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + + >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") + >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") + ``` + + 2. Sauvegardez les fichiers dans un dossier de votre choix avec [`PreTrainedModel.save_pretrained`]: + + ```py + >>> tokenizer.save_pretrained("./your/path/bigscience_t0") + >>> model.save_pretrained("./your/path/bigscience_t0") + ``` + + 3. Maintenant, lorsque vous รชtes hors ligne, rechargez vos fichiers avec [`PreTrainedModel.from_pretrained`] depuis le dossier oรน vous les avez sauvegardรฉs : + + ```py + >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") + >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") + ``` + + * Tรฉlรฉchargez des fichiers de maniรจre automatique avec la librairie [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) : + + 1. Installez la librairie `huggingface_hub` dans votre environnement virtuel : + + ```bash + python -m pip install huggingface_hub + ``` + + 2. Utilisez la fonction [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) pour tรฉlรฉcharger un fichier vers un chemin de votre choix. Par exemple, la commande suivante tรฉlรฉcharge le fichier `config.json` du modรจle [T0](https://huggingface.co/bigscience/T0_3B) vers le chemin de votre choix : + + ```py + >>> from huggingface_hub import hf_hub_download + + >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0") + ``` + +Une fois que votre fichier est tรฉlรฉchargรฉ et cachรฉ localement, spรฉcifiez son chemin local pour le charger et l'utiliser : + +```py +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") +``` + + + +Consultez la section [How to download files from the Hub (Comment tรฉlรฉcharger des fichiers depuis le Hub)](https://huggingface.co/docs/hub/how-to-downstream) pour plus de dรฉtails sur le tรฉlรฉchargement de fichiers stockรฉs sur le Hub. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..dcf21562316d5d1d1991a098f0ef039b5b62372d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/quicktour.md @@ -0,0 +1,550 @@ + + +# Visite rapide + +[[open-in-colab]] + +Soyez opรฉrationnel avec ๐Ÿค— Transformers ! Que vous soyez un dรฉveloppeur ou un utilisateur lambda, cette visite rapide vous aidera ร  dรฉmarrer et vous montrera comment utiliser le [`pipeline`] pour l'infรฉrence, charger un modรจle prรฉ-entraรฎnรฉ et un prรฉprocesseur avec une [AutoClass](./model_doc/auto), et entraรฎner rapidement un modรจle avec PyTorch ou TensorFlow. Si vous รชtes un dรฉbutant, nous vous recommandons de consulter nos tutoriels ou notre [cours](https://huggingface.co/course/chapter1/1) suivant pour des explications plus approfondies des concepts prรฉsentรฉs ici. + +Avant de commencer, assurez-vous que vous avez installรฉ toutes les bibliothรจques nรฉcessaires : + +```bash +!pip install transformers datasets evaluate accelerate +``` + +Vous aurez aussi besoin d'installer votre bibliothรจque d'apprentissage profond favorite : + + + + +```bash +pip install torch +``` + + + +```bash +pip install tensorflow +``` + + + +## Pipeline + + + +Le [`pipeline`] est le moyen le plus simple d'utiliser un modรจle prรฉ-entraรฎnรฉ pour l'infรฉrence. Vous pouvez utiliser le [`pipeline`] prรชt ร  l'emploi pour de nombreuses tรขches dans diffรฉrentes modalitรฉs. Consultez le tableau ci-dessous pour connaรฎtre les tรขches prises en charge : + +| **Tรขche** | **Description** | **Modalitรฉ** | **Identifiant du pipeline** | +|------------------------------|--------------------------------------------------------------------------------------------------------------|----------------------|-----------------------------------------------| +| Classification de texte | Attribue une catรฉgorie ร  une sรฉquence de texte donnรฉe | Texte | pipeline(task="sentiment-analysis") | +| Gรฉnรฉration de texte | Gรฉnรจre du texte ร  partir d'une consigne donnรฉe | Texte | pipeline(task="text-generation") | +| Reconnaissance de token nommรฉ | Attribue une catรฉgorie ร  chaque token dans une sรฉquence (personnes, organisation, localisation, etc.) | Texte | pipeline(task="ner") | +| Question rรฉponse | Extrait une rรฉponse du texte en fonction du contexte et d'une question | Texte | pipeline(task="question-answering") | +| Prรฉdiction de token masquรฉ | Prรฉdit correctement le token masquรฉ dans une sรฉquence | Texte | pipeline(task="fill-mask") | +| Gรฉnรฉration de rรฉsumรฉ | Gรฉnรจre un rรฉsumรฉ d'une sรฉquence de texte donnรฉe ou d'un document | Texte | pipeline(task="summarization") | +| Traduction | Traduit du texte d'un langage ร  un autre | Texte | pipeline(task="translation") | +| Classification d'image | Attribue une catรฉgorie ร  une image | Image | pipeline(task="image-classification") | +| Segmentation d'image | Attribue une catรฉgorie ร  chaque pixel d'une image (supporte la segmentation sรฉmantique, panoptique et d'instance) | Image | pipeline(task="image-segmentation") | +| Dรฉtection d'objets | Prรฉdit les dรฉlimitations et catรฉgories d'objets dans une image | Image | pipeline(task="object-detection") | +| Classification d'audio | Attribue une catรฉgorie ร  un fichier audio | Audio | pipeline(task="audio-classification") | +| Reconnaissance automatique de la parole | Extrait le discours d'un fichier audio en texte | Audio | pipeline(task="automatic-speech-recognition") | +| Question rรฉponse visuels | Etant donnรฉes une image et une question, rรฉpond correctement ร  une question sur l'image | Modalitรฉs multiples | pipeline(task="vqa") | + +Commencez par crรฉer une instance de [`pipeline`] et spรฉcifiez la tรขche pour laquelle vous souhaitez l'utiliser. Vous pouvez utiliser le [`pipeline`] pour n'importe laquelle des tรขches mentionnรฉes dans le tableau prรฉcรฉdent. Pour obtenir une liste complรจte des tรขches prises en charge, consultez la documentation de l'[API pipeline](./main_classes/pipelines). Dans ce guide, nous utiliserons le [`pipeline`] pour l'analyse des sentiments ร  titre d'exemple : + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("sentiment-analysis") +``` + +Le [`pipeline`] tรฉlรฉcharge et stocke en cache un [modรจle prรฉ-entraรฎnรฉ](https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english) et un tokenizer par dรฉfaut pour l'analyse des sentiments. Vous pouvez maintenant utiliser le `classifier` sur le texte de votre choix : + +```py +>>> classifier("We are very happy to show you the ๐Ÿค— Transformers library.") +[{'label': 'POSITIVE', 'score': 0.9998}] +``` + +Si vous voulez classifier plus qu'un texte, donnez une liste de textes au [`pipeline`] pour obtenir une liste de dictionnaires en retour : + +```py +>>> results = classifier(["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."]) +>>> for result in results: +... print(f"label: {result['label']}, avec le score de: {round(result['score'], 4)}") +label: POSITIVE, avec le score de: 0.9998 +label: NEGATIVE, avec le score de: 0.5309 +``` + +Le [`pipeline`] peut aussi itรฉrer sur un jeu de donnรฉes entier pour n'importe quelle tรขche. Prenons par exemple la reconnaissance automatique de la parole : + +```py +>>> import torch +>>> from transformers import pipeline + +>>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h") +``` + +Chargez un jeu de donnรฉes audio (voir le ๐Ÿค— Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart#audio) pour plus de dรฉtails) sur lequel vous souhaitez itรฉrer. Pour cet exemple, nous chargeons le jeu de donnรฉes [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) : + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT +``` + +Vous devez vous assurer que le taux d'รฉchantillonnage de l'ensemble de donnรฉes correspond au taux d'รฉchantillonnage sur lequel [`facebook/wav2vec2-base-960h`](https://huggingface.co/facebook/wav2vec2-base-960h) a รฉtรฉ entraรฎnรฉ : + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate)) +``` + +Les fichiers audio sont automatiquement chargรฉs et rรฉรฉchantillonnรฉs lors de l'appel de la colonne `"audio"`. +Extrayez les tableaux de formes d'ondes brutes des quatre premiers รฉchantillons et passez-les comme une liste au pipeline : + +```py +>>> result = speech_recognizer(dataset[:4]["audio"]) +>>> print([d["text"] for d in result]) +['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FODING HOW I'D SET UP A JOIN TO HET WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE AP SO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AND I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I THURN A JOIN A COUNT'] +``` + +Pour les ensembles de donnรฉes plus importants oรน les entrรฉes sont volumineuses (comme dans les domaines de la parole ou de la vision), utilisez plutรดt un gรฉnรฉrateur au lieu d'une liste pour charger toutes les entrรฉes en mรฉmoire. Pour plus d'informations, consultez la documentation de l'[API pipeline](./main_classes/pipelines). + +### Utiliser une autre modรจle et tokenizer dans le pipeline + +Le [`pipeline`] peut รชtre utilisรฉ avec n'importe quel modรจle du [Hub](https://huggingface.co/models), ce qui permet d'adapter facilement le [`pipeline`] ร  d'autres cas d'utilisation. Par exemple, si vous souhaitez un modรจle capable de traiter du texte franรงais, utilisez les filtres du Hub pour trouver un modรจle appropriรฉ. Le premier rรฉsultat renvoie un [modรจle BERT](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment) multilingue finetunรฉ pour l'analyse des sentiments que vous pouvez utiliser pour le texte franรงais : + +```py +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +``` + + + +Utilisez [`AutoModelForSequenceClassification`] et [`AutoTokenizer`] pour charger le modรจle prรฉ-entraรฎnรฉ et le tokenizer adaptรฉ (plus de dรฉtails sur une `AutoClass` dans la section suivante) : + +```py +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + +Utilisez [`TFAutoModelForSequenceClassification`] et [`AutoTokenizer`] pour charger le modรจle prรฉ-entraรฎnรฉ et le tokenizer adaptรฉ (plus de dรฉtails sur une `TFAutoClass` dans la section suivante) : + +```py +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + +Spรฉcifiez le modรจle et le tokenizer dans le [`pipeline`], et utilisez le `classifier` sur le texte en franรงais : + +```py +>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) +>>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") +[{'label': '5 stars', 'score': 0.7273}] +``` + +Si vous ne parvenez pas ร  trouver un modรจle adaptรฉ ร  votre cas d'utilisation, vous devrez finetuner un modรจle prรฉ-entraรฎnรฉ sur vos donnรฉes. Jetez un coup d'ล“il ร  notre [tutoriel sur le finetuning](./training) pour apprendre comment faire. Enfin, aprรจs avoir finetunรฉ votre modรจle prรฉ-entraรฎnรฉ, pensez ร  [partager](./model_sharing) le modรจle avec la communautรฉ sur le Hub afin de dรฉmocratiser l'apprentissage automatique pour tous ! ๐Ÿค— + +## AutoClass + + + +Les classes [`AutoModelForSequenceClassification`] et [`AutoTokenizer`] fonctionnent ensemble pour crรฉer un [`pipeline`] comme celui que vous avez utilisรฉ ci-dessus. Une [AutoClass](./model_doc/auto) est un raccourci qui rรฉcupรจre automatiquement l'architecture d'un modรจle prรฉ-entraรฎnรฉ ร  partir de son nom ou de son emplacement. Il vous suffit de sรฉlectionner l'`AutoClass` appropriรฉe ร  votre tรขche et la classe de prรฉtraitement qui lui est associรฉe. + +Reprenons l'exemple de la section prรฉcรฉdente et voyons comment vous pouvez utiliser l'`AutoClass` pour reproduire les rรฉsultats du [`pipeline`]. + +### AutoTokenizer + +Un tokenizer est chargรฉ de prรฉtraiter le texte pour en faire un tableau de chiffres qui servira d'entrรฉe ร  un modรจle. De nombreuses rรจgles rรฉgissent le processus de tokenisation, notamment la maniรจre de diviser un mot et le niveau auquel les mots doivent รชtre divisรฉs (pour en savoir plus sur la tokenisation, consultez le [rรฉsumรฉ](./tokenizer_summary)). La chose la plus importante ร  retenir est que vous devez instancier un tokenizer avec le mรชme nom de modรจle pour vous assurer que vous utilisez les mรชmes rรจgles de tokenisation que celles avec lesquelles un modรจle a รฉtรฉ prรฉ-entraรฎnรฉ. + +Chargez un tokenizer avec [`AutoTokenizer`] : + +```py +>>> from transformers import AutoTokenizer + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + +Passez votre texte au tokenizer : + +```py +>>> encoding = tokenizer("We are very happy to show you the ๐Ÿค— Transformers library.") +>>> print(encoding) +{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +Le tokenizer retourne un dictionnaire contenant : + +* [input_ids](./glossary#input-ids): la reprรฉsentation numรฉrique des tokens. +* [attention_mask](.glossary#attention-mask): indique quels tokens doivent faire l'objet d'une attention particuliรจre (plus particuliรจrement les tokens de remplissage). + +Un tokenizer peut รฉgalement accepter une liste de textes, et remplir et tronquer le texte pour retourner un รฉchantillon de longueur uniforme : + + + + +```py +>>> pt_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="pt", +... ) +``` + + + +```py +>>> tf_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="tf", +... ) +``` + + + + + +Consultez le tutoriel [prรฉtraitement](./preprocessing) pour plus de dรฉtails sur la tokenisation, et sur la maniรจre d'utiliser un [`AutoImageProcessor`], un [`AutoFeatureExtractor`] et un [`AutoProcessor`] pour prรฉtraiter les images, l'audio et les contenus multimodaux. + + + +### AutoModel + + + +๐Ÿค— Transformers fournit un moyen simple et unifiรฉ de charger des instances prรฉ-entraรฎnรฉes. Cela signifie que vous pouvez charger un [`AutoModel`] comme vous chargeriez un [`AutoTokenizer`]. La seule diffรฉrence est de sรฉlectionner l'[`AutoModel`] appropriรฉ pour la tรขche. Pour une classification de texte (ou de sรฉquence de textes), vous devez charger [`AutoModelForSequenceClassification`] : + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +Voir le [rรฉsumรฉ de la tรขche](./task_summary) pour vรฉrifier si elle est prise en charge par une classe [`AutoModel`]. + + + +Maintenant, passez votre รฉchantillon d'entrรฉes prรฉtraitรฉes directement au modรจle. Il vous suffit de dรฉcompresser le dictionnaire en ajoutant `**` : + +```py +>>> pt_outputs = pt_model(**pt_batch) +``` + +Le modรจle produit les activations finales dans l'attribut `logits`. Appliquez la fonction softmax aux `logits` pour rรฉcupรฉrer les probabilitรฉs : + +```py +>>> from torch import nn + +>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) +>>> print(pt_predictions) +tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], + [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=) +``` + + +๐Ÿค— Transformers fournit un moyen simple et unifiรฉ de charger des instances prรฉ-entraรฎnรฉs. Cela signifie que vous pouvez charger un [`TFAutoModel`] comme vous chargeriez un [`AutoTokenizer`]. La seule diffรฉrence est de sรฉlectionner le [`TFAutoModel`] appropriรฉ pour la tรขche. Pour une classification de texte (ou de sรฉquence de textes), vous devez charger [`TFAutoModelForSequenceClassification`] : + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +Voir le [rรฉsumรฉ de la tรขche](./task_summary) pour vรฉrifier si elle est prise en charge par une classe [`AutoModel`]. + + + +Passez maintenant votre รฉchantillon d'entrรฉes prรฉtraitรฉes directement au modรจle en passant les clรฉs du dictionnaire directement aux tensors : + +```py +>>> tf_outputs = tf_model(tf_batch) +``` + +Le modรจle produit les activations finales dans l'attribut `logits`. Appliquez la fonction softmax aux `logits` pour rรฉcupรฉrer les probabilitรฉs : + +```py +>>> import tensorflow as tf + +>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) +>>> tf_predictions # doctest: +IGNORE_RESULT +``` + + + + + +Tous les modรจles ๐Ÿค— Transformers (PyTorch ou TensorFlow) produisent les tensors *avant* la fonction d'activation finale (comme softmax) car la fonction d'activation finale est souvent fusionnรฉe avec le calcul de la perte. Les structures produites par le modรจle sont des classes de donnรฉes spรฉciales, de sorte que leurs attributs sont autocomplรฉtรฉs dans un environnement de dรฉveloppement. Les structures produites par le modรจle se comportent comme un tuple ou un dictionnaire (vous pouvez les indexer avec un entier, une tranche ou une chaรฎne), auquel cas les attributs qui sont None sont ignorรฉs. + + + +### Sauvegarder un modรจle + + + +Une fois que votre modรจle est finetunรฉ, vous pouvez le sauvegarder avec son tokenizer en utilisant [`PreTrainedModel.save_pretrained`] : + +```py +>>> pt_save_directory = "./pt_save_pretrained" +>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT +>>> pt_model.save_pretrained(pt_save_directory) +``` + +Lorsque vous voulez rรฉutiliser le modรจle, rechargez-le avec [`PreTrainedModel.from_pretrained`] : + +```py +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") +``` + + +Une fois que votre modรจle est finetunรฉ, vous pouvez le sauvegarder avec son tokenizer en utilisant [`TFPreTrainedModel.save_pretrained`] : + +```py +>>> tf_save_directory = "./tf_save_pretrained" +>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT +>>> tf_model.save_pretrained(tf_save_directory) +``` + +Lorsque vous voulez rรฉutiliser le modรจle, rechargez-le avec [`TFPreTrainedModel.from_pretrained`] : + +```py +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") +``` + + + +Une fonctionnalitรฉ particuliรจrement cool ๐Ÿค— Transformers est la possibilitรฉ d'enregistrer un modรจle et de le recharger en tant que modรจle PyTorch ou TensorFlow. Le paramรจtre `from_pt` ou `from_tf` permet de convertir le modรจle d'un framework ร  l'autre : + + + + +```py +>>> from transformers import AutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) +``` + + + +```py +>>> from transformers import TFAutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) +``` + + + +## Constructions de modรจles personnalisรฉs + +Vous pouvez modifier la configuration du modรจle pour changer la faรงon dont un modรจle est construit. La configuration spรฉcifie les attributs d'un modรจle, tels que le nombre de couches ou de tรชtes d'attention. Vous partez de zรฉro lorsque vous initialisez un modรจle ร  partir d'une configuration personnalisรฉe. Les attributs du modรจle sont initialisรฉs de maniรจre alรฉatoire et vous devrez entraรฎner le modรจle avant de pouvoir l'utiliser pour obtenir des rรฉsultats significatifs. + +Commencez par importer [`AutoConfig`], puis chargez le modรจle prรฉ-entraรฎnรฉ que vous voulez modifier. Dans [`AutoConfig.from_pretrained`], vous pouvez spรฉcifier l'attribut que vous souhaitez modifier, tel que le nombre de tรชtes d'attention : + +```py +>>> from transformers import AutoConfig + +>>> my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12) +``` + + + +Crรฉez un modรจle personnalisรฉ ร  partir de votre configuration avec [`AutoModel.from_config`] : + +```py +>>> from transformers import AutoModel + +>>> my_model = AutoModel.from_config(my_config) +``` + + +Crรฉez un modรจle personnalisรฉ ร  partir de votre configuration avec [`TFAutoModel.from_config`] : + +```py +>>> from transformers import TFAutoModel + +>>> my_model = TFAutoModel.from_config(my_config) +``` + + + +Consultez le guide [Crรฉer une architecture personnalisรฉe](./create_a_model) pour plus d'informations sur la crรฉation de configurations personnalisรฉes. + +## Trainer - une boucle d'entraรฎnement optimisรฉe par PyTorch + +Tous les modรจles sont des [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) standard, vous pouvez donc les utiliser dans n'importe quelle boucle d'entraรฎnement typique. Bien que vous puissiez รฉcrire votre propre boucle d'entraรฎnement, ๐Ÿค— Transformers fournit une classe [`Trainer`] pour PyTorch, qui contient la boucle d'entraรฎnement de base et ajoute des fonctionnalitรฉs supplรฉmentaires comme l'entraรฎnement distribuรฉ, la prรฉcision mixte, et plus encore. + +En fonction de votre tรขche, vous passerez gรฉnรฉralement les paramรจtres suivants ร  [`Trainer`] : + +1. Un [`PreTrainedModel`] ou un [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module): + + ```py + >>> from transformers import AutoModelForSequenceClassification + + >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. [`TrainingArguments`] contient les hyperparamรจtres du modรจle que vous pouvez changer comme le taux d'apprentissage, la taille de l'รฉchantillon, et le nombre d'รฉpoques pour s'entraรฎner. Les valeurs par dรฉfaut sont utilisรฉes si vous ne spรฉcifiez pas d'hyperparamรจtres d'apprentissage : + + ```py + >>> from transformers import TrainingArguments + + >>> training_args = TrainingArguments( + ... output_dir="path/to/save/folder/", + ... learning_rate=2e-5, + ... per_device_train_batch_size=8, + ... per_device_eval_batch_size=8, + ... num_train_epochs=2, + ... ) + ``` + +3. Une classe de prรฉtraitement comme un tokenizer, un processeur d'images ou un extracteur de caractรฉristiques : + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +4. Chargez un jeu de donnรฉes : + + ```py + >>> from datasets import load_dataset + + >>> dataset = load_dataset("rotten_tomatoes") # doctest: +IGNORE_RESULT + ``` + +5. Crรฉez une fonction qui transforme le texte du jeu de donnรฉes en token : + + ```py + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) + ``` + + Puis appliquez-la ร  l'intรฉgralitรฉ du jeu de donnรฉes avec [`~datasets.Dataset.map`]: + + ```py + >>> dataset = dataset.map(tokenize_dataset, batched=True) + ``` + +6. Un [`DataCollatorWithPadding`] pour crรฉer un รฉchantillon d'exemples ร  partir de votre jeu de donnรฉes : + + ```py + >>> from transformers import DataCollatorWithPadding + + >>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer) + ``` + +Maintenant, rassemblez tous ces รฉlรฉments dans un [`Trainer`] : + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=dataset["train"], +... eval_dataset=dataset["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) # doctest: +SKIP +``` + +Une fois que vous รชtes prรชt, appelez la fonction [`~Trainer.train`] pour commencer l'entraรฎnement : + +```py +>>> trainer.train() # doctest: +SKIP +``` + + + +Pour les tรขches - comme la traduction ou la gรฉnรฉration de rรฉsumรฉ - qui utilisent un modรจle sรฉquence ร  sรฉquence, utilisez plutรดt les classes [`Seq2SeqTrainer`] et [`Seq2SeqTrainingArguments`]. + + + +Vous pouvez personnaliser le comportement de la boucle d'apprentissage en redรฉfinissant les mรฉthodes ร  l'intรฉrieur de [`Trainer`]. Cela vous permet de personnaliser des caractรฉristiques telles que la fonction de perte, l'optimiseur et le planificateur. Consultez la documentation de [`Trainer`] pour savoir quelles mรฉthodes peuvent รชtre redรฉfinies. + +L'autre moyen de personnaliser la boucle d'apprentissage est d'utiliser les [Callbacks](./main_classes/callback). Vous pouvez utiliser les callbacks pour intรฉgrer d'autres bibliothรจques et inspecter la boucle d'apprentissage afin de suivre la progression ou d'arrรชter l'apprentissage plus tรดt. Les callbacks ne modifient rien dans la boucle d'apprentissage elle-mรชme. Pour personnaliser quelque chose comme la fonction de perte, vous devez redรฉfinir le [`Trainer`] ร  la place. + +## Entraรฎnement avec TensorFlow + +Tous les modรจles sont des modรจles standard [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) afin qu'ils puissent รชtre entraรฎnรฉs avec TensorFlow avec l'API [Keras](https://keras.io/). ๐Ÿค— Transformers fournit la fonction [`~TFPreTrainedModel.prepare_tf_dataset`] pour charger facilement votre jeu de donnรฉes comme un `tf.data.Dataset` afin que vous puissiez commencer l'entraรฎnement immรฉdiatement avec les fonctions [`compile`](https://keras.io/api/models/model_training_apis/#compile-method) et [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) de Keras. + +1. Vous commencez avec un modรจle [`TFPreTrainedModel`] ou [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) : + + ```py + >>> from transformers import TFAutoModelForSequenceClassification + + >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. Une classe de prรฉtraitement comme un tokenizer, un processeur d'images ou un extracteur de caractรฉristiques : + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +3. Crรฉez une fonction qui transforme le texte du jeu de donnรฉes en token : + + ```py + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) # doctest: +SKIP + ``` + +4. Appliquez le tokenizer ร  l'ensemble du jeu de donnรฉes avec [`~datasets.Dataset.map`] et passez ensuite le jeu de donnรฉes et le tokenizer ร  [`~TFPreTrainedModel.prepare_tf_dataset`]. Vous pouvez รฉgalement modifier la taille de l'รฉchantillon et mรฉlanger le jeu de donnรฉes ici si vous le souhaitez : + + ```py + >>> dataset = dataset.map(tokenize_dataset) # doctest: +SKIP + >>> tf_dataset = model.prepare_tf_dataset( + ... dataset, batch_size=16, shuffle=True, tokenizer=tokenizer + ... ) # doctest: +SKIP + ``` + +5. Une fois que vous รชtes prรชt, appelez les fonctions `compile` et `fit` pour commencer l'entraรฎnement : + + ```py + >>> from tensorflow.keras.optimizers import Adam + + >>> model.compile(optimizer=Adam(3e-5)) + >>> model.fit(dataset) # doctest: +SKIP + ``` + +## Et aprรจs ? + +Maintenant que vous avez terminรฉ la visite rapide de ๐Ÿค— Transformers, consultez nos guides et apprenez ร  faire des choses plus spรฉcifiques comme crรฉer un modรจle personnalisรฉ, finetuner un modรจle pour une tรขche, et comment entraรฎner un modรจle avec un script. Si vous souhaitez en savoir plus sur les concepts fondamentaux de ๐Ÿค— Transformers, jetez un ล“il ร  nos guides conceptuels ! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/run_scripts_fr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/run_scripts_fr.md new file mode 100644 index 0000000000000000000000000000000000000000..561f9f0470053e53049c2e12450fed07ac2b906b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/run_scripts_fr.md @@ -0,0 +1,355 @@ + + +# Entraรฎner avec un script + +En plus des [notebooks](./notebooks) de ๐Ÿค— Transformers, il existe รฉgalement des exemples de scripts dรฉmontrant comment entraรฎner un modรจle pour une tรขche avec [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch), [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow) ou [JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax). + + +Vous trouverez รฉgalement des scripts que nous avons utilisรฉ dans nos [projets de recherche](https://github.com/huggingface/transformers-research-projects/) et des [exemples "legacy"](https://github.com/huggingface/transformers/tree/main/examples/legacy) qui sont des contributions de la communautรฉ. Ces scripts ne sont pas activement maintenus et nรฉcessitent une version spรฉcifique de ๐Ÿค— Transformers qui sera probablement incompatible avec la derniรจre version de la librairie. + +Les exemples de scripts ne sont pas censรฉs fonctionner immรฉdiatement pour chaque problรจme, et il se peut que vous ayez besoin d'adapter le script au problรจme que vous essayez de rรฉsoudre. Pour vous aider dans cette tรขche, la plupart des scripts exposent entiรจrement la maniรจre dont les donnรฉes sont prรฉtraitรฉes, vous permettant de les modifier selon vos besoins. + +Pour toute fonctionnalitรฉ que vous souhaitez implรฉmenter dans un script d'exemple, veuillez en discuter sur le [forum](https://discuss.huggingface.co/) ou dans une [issue](https://github.com/huggingface/transformers/issues) avant de soumettre une Pull Request. Bien que nous acceptions les corrections de bugs, il est peu probable que nous fusionnions une Pull Request (opรฉration "merge" dans Git) ajoutant plus de fonctionnalitรฉs au dรฉtriment de la lisibilitรฉ. + +Ce guide vous montrera comment exรฉcuter un script d'entraรฎnement de rรฉsumรฉ en exemple avec [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) et [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization). Tous les exemples sont censรฉs fonctionner avec les deux frameworks, sauf indication contraire. + +## Configuration + +Pour exรฉcuter avec succรจs la derniรจre version des scripts d'exemple, vous devez **installer ๐Ÿค— Transformers ร  partir du code source** dans un nouvel environnement virtuel : + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` + +Pour les versions plus anciennes des exemples de scripts, cliquez sur le bouton ci-dessous : + +
+ Exemples pour les anciennes versions de Transformers ๐Ÿค— + +
+ +Ensuite, changez votre clone actuel de ๐Ÿค— Transformers pour une version spรฉcifique, comme par exemple v3.5.1 : + +```bash +git checkout tags/v3.5.1 +``` + +Aprรจs avoir configurรฉ la bonne version de la librairie, accรฉdez au dossier d'exemple de votre choix et installez les prรฉrequis spรฉcifiques ร  l'exemple. + +```bash +pip install -r requirements.txt +``` + +## Exรฉcuter un script + + + + +Le script d'exemple tรฉlรฉcharge et prรฉtraite un jeu de donnรฉes ร  partir de la bibliothรจque ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Ensuite, le script affine un ensemble de donnรฉes ร  l'aide de [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) sur une architecture qui prend en charge la tรขche de rรฉsumรฉ. L'exemple suivant montre comment ajuster le modรจle [T5-small](https://huggingface.co/google-t5/t5-small) sur les donnรฉes [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). Le modรจle T5 nรฉcessite un argument supplรฉmentaire `source_prefix` en raison de la faรงon dont il a รฉtรฉ entraรฎnรฉ. Cette invite permet ร  T5 de savoir qu'il s'agit d'une tรขche de rรฉsumรฉ. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + + +Le script d'exemple tรฉlรฉcharge et prรฉtraite un jeu de donnรฉes ร  partir de la bibliothรจque ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Ensuite, le script ajuste un modรจle ร  l'aide de Keras sur une architecture qui prend en charge la tรขche de rรฉsumรฉ. L'exemple suivant montre comment ajuster le modรจle [T5-small](https://huggingface.co/google-t5/t5-small) sur le jeu de donnรฉes [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). Le modรจle T5 nรฉcessite un argument supplรฉmentaire source_prefix en raison de la faรงon dont il a รฉtรฉ entraรฎnรฉ. Cette invite permet ร  T5 de savoir qu'il s'agit d'une tรขche de rรฉsumรฉ. + +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Entraรฎnement distribuรฉ et prรฉcision mixte + +[Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) prend en charge l'entraรฎnement distribuรฉ et la prรฉcision mixte, ce qui signifie que vous pouvez รฉgalement les utiliser dans un script. Pour activer ces deux fonctionnalitรฉs : + +- Ajoutez l'argument fp16 pour activer la prรฉcision mixte. +- Dรฉfinissez le nombre de GPU ร  utiliser avec l'argument `nproc_per_node`. + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +Les scripts TensorFlow utilisent une Strategie en Miroir [`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy) pour l'entraรฎnement distribuรฉ, et vous n'avez pas besoin d'ajouter d'arguments supplรฉmentaires au script d'entraรฎnement. Le script TensorFlow utilisera plusieurs GPU par dรฉfaut s'ils sont disponibles. + +## Exรฉcuter un script sur un TPU + + + + +Les unitรฉs de traitement de tenseurs (UTT) (TPU) sont spรฉcialement conรงues pour accรฉlรฉrer les performances. PyTorch prend en charge les TPU avec le compilateur de deep learning [XLA](https://www.tensorflow.org/xla). Pour utiliser un TPU, lancez le script xla_spawn.py et utilisez l'argument num_cores pour dรฉfinir le nombre de cล“urs TPU que vous souhaitez utilise + +```bash +python xla_spawn.py --num_cores 8 \ + summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +Les scripts TensorFlow utilisent une [`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy) pour l'entraรฎnement sur TPU. Pour utiliser un TPU, passez le nom de la ressource TPU ร  l'argument tpu. + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Exรฉcuter un script avec ๐Ÿค— Accelerate + +๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) est une bibliothรจque uniquement pour PyTorch qui offre une mรฉthode unifiรฉe pour entraรฎner un modรจle sur plusieurs types de configurations (CPU uniquement, plusieurs GPU, TPU) tout en maintenant une visibilitรฉ complรจte sur la boucle d'entraรฎnement PyTorch. Assurez-vous que vous avez installรฉ ๐Ÿค— Accelerate si ce n'est pas dรฉjร  le cas. + +> Note : Comme Accelerate est en dรฉveloppement rapide, la version git d'accelerate doit รชtre installรฉe pour exรฉcuter les scripts. +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +Au lieu du script `run_summarization.py`, vous devez utiliser le script `run_summarization_no_trainer.py`. Les scripts compatibles avec ๐Ÿค— Accelerate auront un fichier `task_no_trainer.py` dans le dossier. Commencez par exรฉcuter la commande suivante pour crรฉer et enregistrer un fichier de configuration. + +```bash +accelerate config +``` + +Testez votre configuration pour vous assurer qu'elle est correctement configurรฉe : + +```bash +accelerate test +``` + +Maintenant, vous รชtes prรชt ร  lancer l'entraรฎnement : + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization +``` + +## Utiliser un jeu de donnรฉes personnalisรฉ + +Le script de rรฉsumรฉ prend en charge les jeux de donnรฉes personnalisรฉs tant qu'ils sont au format CSV ou JSON Line. Lorsque vous utilisez votre propre jeu de donnรฉes, vous devez spรฉcifier plusieurs arguments supplรฉmentaires : + +- `train_file` et `validation_file` spรฉcifient le chemin vers vos fichiers d'entraรฎnement et de validation. +- `text_column` est le texte d'entrรฉe ร  rรฉsumer. +- `summary_column` est le texte cible ร  produire. + +Un exemple de script de rรฉsumรฉ utilisant un ensemble de donnรฉes personnalisรฉ ressemblerait ร  ceci : + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate +``` + +## Tester un script +Il est souvent judicieux d'exรฉcuter votre script sur un plus petit nombre d'exemples de jeu de donnรฉes pour s'assurer que tout fonctionne comme prรฉvu avant de s'engager sur un jeu de donnรฉes complet qui pourrait prendre des heures ร  traiter. Utilisez les arguments suivants pour tronquer le jeu de donnรฉes ร  un nombre maximal d'รฉchantillons : + +- `max_train_samples` +- `max_eval_samples` +- `max_predict_samples` + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +Tous les scripts d'exemple ne prennent pas en charge l'argument `max_predict_samples`. Si vous n'รชtes pas sรปr que votre script prenne en charge cet argument, ajoutez l'argument `-h` pour vรฉrifier. + +```bash +examples/pytorch/summarization/run_summarization.py -h +``` + +## Reprendre l'entraรฎnement ร  partir d'un point de contrรดle + +Une autre option utile est de reprendre l'entraรฎnement ร  partir d'un point de contrรดle prรฉcรฉdent. Cela vous permettra de reprendre lร  oรน vous vous รฉtiez arrรชtรฉ sans recommencer si votre entraรฎnement est interrompu. Il existe deux mรฉthodes pour reprendre l'entraรฎnement ร  partir d'un point de contrรดle. + +La premiรจre mรฉthode utilise l'argument `output_dir previous_output_dir` pour reprendre l'entraรฎnement ร  partir du dernier point de contrรดle stockรฉ dans `output_dir`. Dans ce cas, vous devez supprimer l'argument `overwrite_output_dir`. + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --output_dir previous_output_dir \ + --predict_with_generate +``` + +La seconde mรฉthode utilise l'argument `resume_from_checkpoint path_to_specific_checkpoint` pour reprendre l'entraรฎnement ร  partir d'un dossier de point de contrรดle spรฉcifique. + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate +``` + +## Partage ton modรจle + +Tous les scripts peuvent tรฉlรฉcharger votre modรจle final sur le Model Hub. Assurez-vous que vous รชtes connectรฉ ร  Hugging Face avant de commencer : + +```bash +hf auth login +``` + +Ensuite, ajoutez l'argument `push_to_hub` au script. Cet argument crรฉera un dรฉpรดt avec votre nom d'utilisateur Hugging Face et le nom du dossier spรฉcifiรฉ dans `output_dir`. + + +Pour donner un nom spรฉcifique ร  votre dรฉpรดt, utilisez l'argument `push_to_hub_model_id` pour l'ajouter. Le dรฉpรดt sera automatiquement listรฉ sous votre namespace. + +L'exemple suivant montre comment tรฉlรฉcharger un modรจle avec un nom de dรฉpรดt spรฉcifique : + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/task_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/task_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..f730264f95e86fe8bb44cbb338f72e48af607125 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/task_summary.md @@ -0,0 +1,341 @@ + + +# Ce que ๐Ÿค— Transformers peut faire + +๐Ÿค— Transformers est une bibliothรจque de modรจles prรฉentraรฎnรฉs ร  la pointe de la technologie pour les tรขches de traitement du langage naturel (NLP), de vision par ordinateur et de traitement audio et de la parole. Non seulement la bibliothรจque contient des modรจles Transformer, mais elle inclut รฉgalement des modรจles non-Transformer comme des rรฉseaux convolutionnels modernes pour les tรขches de vision par ordinateur. Si vous regardez certains des produits grand public les plus populaires aujourd'hui, comme les smartphones, les applications et les tรฉlรฉviseurs, il est probable qu'une technologie d'apprentissage profond soit derriรจre. Vous souhaitez supprimer un objet de fond d'une photo prise avec votre smartphone ? C'est un exemple de tรขche de segmentation panoptique (ne vous inquiรฉtez pas si vous ne savez pas encore ce que cela signifie, nous le dรฉcrirons dans les sections suivantes !). + +Cette page fournit un aperรงu des diffรฉrentes tรขches de traitement de la parole et de l'audio, de vision par ordinateur et de NLP qui peuvent รชtre rรฉsolues avec la bibliothรจque ๐Ÿค— Transformers en seulement trois lignes de code ! + +## Audio + +Les tรขches de traitement audio et de la parole sont lรฉgรจrement diffรฉrentes des autres modalitรฉs principalement parce que l'audio en tant que donnรฉe d'entrรฉe est un signal continu. Contrairement au texte, un signal audio brut ne peut pas discrรฉtisรฉ de la maniรจre dont une phrase peut รชtre divisรฉe en mots. Pour contourner cela, le signal audio brut est gรฉnรฉralement รฉchantillonnรฉ ร  intervalles rรฉguliers. Si vous prenez plus d'รฉchantillons dans un intervalle, le taux d'รฉchantillonnage est plus รฉlevรฉ et l'audio ressemble davantage ร  la source audio originale. + +Les approches prรฉcรฉdentes prรฉtraitaient l'audio pour en extraire des caractรฉristiques utiles. Il est maintenant plus courant de commencer les tรขches de traitement audio et de la parole en donnant directement le signal audio brut ร  un encodeur de caractรฉristiques (*feature encoder* en anglais) pour extraire une reprรฉsentation de l'audio. Cela correspond ร  l'รฉtape de prรฉtraitement et permet au modรจle d'apprendre les caractรฉristiques les plus essentielles du signal. + +### Classification audio + +La classification audio est une tรขche qui consiste ร  attribuer une classe, parmi un ensemble de classes prรฉdรฉfini, ร  un audio. La classification audio englobe de nombreuses applications spรฉcifiques, dont certaines incluent : + +* la classification d'environnements sonores : attribuer une classe (catรฉgorie) ร  l'audio pour indiquer l'environnement associรฉ, tel que "bureau", "plage" ou "stade". +* la dรฉtection d'รฉvรฉnements sonores : รฉtiqueter l'audio avec une รฉtiquette d'รฉvรฉnement sonore ("klaxon de voiture", "appel de baleine", "verre brisรฉ") +* l'identification d'รฉlรฉments sonores : attribuer des tags (*รฉtiquettes* en franรงais) ร  l'audio pour marquer des sons spรฉcifiques, comme "chant des oiseaux" ou "identification du locuteur lors d'une rรฉunion". +* la classification musicale : attribuer un genre ร  la musique, comme "metal", "hip-hop" ou "country". + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="audio-classification", model="superb/hubert-base-superb-er") +>>> preds = classifier("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4532, 'label': 'hap'}, + {'score': 0.3622, 'label': 'sad'}, + {'score': 0.0943, 'label': 'neu'}, + {'score': 0.0903, 'label': 'ang'}] +``` + +### Reconnaissance vocale + +La reconnaissance vocale (*Automatic Speech Recognition* ou ASR en anglais) transcrit la parole en texte. C'est l'une des tรขches audio les plus courantes en partie parce que la parole est une forme de communication la plus naturelle pour nous, humains. Aujourd'hui, les systรจmes ASR sont intรฉgrรฉs dans des produits technologiques "intelligents" comme les enceintes, les tรฉlรฉphones et les voitures. Il est dรฉsormais possible de demander ร  nos assistants virtuels de jouer de la musique, de dรฉfinir des rappels et de nous indiquer la mรฉtรฉo. + +Mais l'un des principaux dรฉfis auxquels les architectures Transformer contribuent ร  rรฉsoudre est celui des langues ร  faibles ressources, c'est-ร -dire des langues pour lesquelles il existe peu de donnรฉes รฉtiquetรฉes. En prรฉentraรฎnant sur de grandes quantitรฉs de donnรฉes vocales d'un autre language plus ou moins similaire, le rรฉglage fin (*fine-tuning* en anglais) du modรจle avec seulement une heure de donnรฉes vocales รฉtiquetรฉes dans une langue ร  faibles ressources peut tout de mรชme produire des rรฉsultats de haute qualitรฉ comparรฉs aux systรจmes ASR prรฉcรฉdents entraรฎnรฉs sur 100 fois plus de donnรฉes รฉtiquetรฉes. + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-small") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +## Vision par ordinateur + +L'une des premiรจres rรฉussites en vision par ordinateur a รฉtรฉ la reconnaissance des numรฉros de code postal ร  l'aide d'un [rรฉseau de neurones convolutionnel (CNN)](glossary#convolution). Une image est composรฉe de pixels, chacun ayant une valeur numรฉrique, ce qui permet de reprรฉsenter facilement une image sous forme de matrice de valeurs de pixels. Chaque combinaison de valeurs de pixels correspond aux couleurs d'une image. + +Il existe deux approches principales pour rรฉsoudre les tรขches de vision par ordinateur : + +1. Utiliser des convolutions pour apprendre les caractรฉristiques hiรฉrarchiques d'une image, des dรฉtails de bas niveau aux รฉlรฉments abstraits de plus haut niveau. +2. Diviser l'image en morceaux (*patches* en anglais) et utiliser un Transformer pour apprendre progressivement comment chaque morceau est liรฉ aux autres pour former l'image complรจte. Contrairement ร  l'approche ascendante des CNNs, cette mรฉthode ressemble ร  un processus oรน l'on dรฉmarre avec une image floue pour ensuite la mettre au point petit ร  petit. + +### Classification d'images + +La classification d'images consiste ร  attribuer une classe, parmi un ensemble de classes prรฉdรฉfini, ร  toute une image. Comme pour la plupart des tรขches de classification, les cas d'utilisation pratiques sont nombreux, notamment : + +- Santรฉ : classification d'images mรฉdicales pour dรฉtecter des maladies ou surveiller l'รฉtat de santรฉ des patients. +- Environnement : classification d'images satellites pour suivre la dรฉforestation, aider ร  la gestion des terres ou dรฉtecter les incendies de forรชt. +- Agriculture : classification d'images de cultures pour surveiller la santรฉ des plantes ou des images satellites pour analyser l'utilisation des terres. +- ร‰cologie : classification d'images d'espรจces animales ou vรฉgรฉtales pour suivre les populations fauniques ou les espรจces menacรฉes. + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="image-classification") +>>> preds = classifier( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.4335, 'label': 'lynx, catamount'} +{'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'} +{'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'} +{'score': 0.0239, 'label': 'Egyptian cat'} +{'score': 0.0229, 'label': 'tiger cat'} +``` + +### Dรฉtection d'objets + +La dรฉtection d'objets, ร  la diffรฉrence de la classification d'images, identifie plusieurs objets dans une image ainsi que leurs positions, gรฉnรฉralement dรฉfinies par des boรฎtes englobantes (*bounding boxes* en anglais). Voici quelques exemples d'applications : + +- Vรฉhicules autonomes : dรฉtection des objets de la circulation, tels que les vรฉhicules, piรฉtons et feux de signalisation. +- Tรฉlรฉdรฉtection : surveillance des catastrophes, planification urbaine et prรฉvisions mรฉtรฉorologiques. +- Dรฉtection de dรฉfauts : identification des fissures ou dommages structurels dans les bรขtiments, ainsi que des dรฉfauts de fabrication. + +```py +>>> from transformers import pipeline + +>>> detector = pipeline(task="object-detection") +>>> preds = detector( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"], "box": pred["box"]} for pred in preds] +>>> preds +[{'score': 0.9865, + 'label': 'cat', + 'box': {'xmin': 178, 'ymin': 154, 'xmax': 882, 'ymax': 598}}] +``` + +### Segmentation d'images + +La segmentation d'images est une tรขche qui consiste ร  attribuer une classe ร  chaque pixel d'une image, ce qui la rend plus prรฉcise que la dรฉtection d'objets, qui se limite aux boรฎtes englobantes (*bounding boxes* en anglais). Elle permet ainsi de dรฉtecter les objets ร  la prรฉcision du pixel. Il existe plusieurs types de segmentation d'images : + +- Segmentation d'instances : en plus de classifier un objet, elle identifie chaque instance distincte d'un mรชme objet (par exemple, "chien-1", "chien-2"). +- Segmentation panoptique : combine segmentation sรฉmantique et segmentation d'instances, attribuant ร  chaque pixel une classe sรฉmantique **et** une instance spรฉcifique. + +Ces techniques sont utiles pour les vรฉhicules autonomes, qui doivent cartographier leur environnement pixel par pixel pour naviguer en toute sรฉcuritรฉ autour des piรฉtons et des vรฉhicules. Elles sont รฉgalement prรฉcieuses en imagerie mรฉdicale, oรน la prรฉcision au niveau des pixels permet de dรฉtecter des anomalies cellulaires ou des caractรฉristiques d'organes. Dans le commerce en ligne, la segmentation est utilisรฉe pour des essayages virtuels de vรชtements ou des expรฉriences de rรฉalitรฉ augmentรฉe, en superposant des objets virtuels sur des images du monde rรฉel via la camรฉra. + +```py +>>> from transformers import pipeline + +>>> segmenter = pipeline(task="image-segmentation") +>>> preds = segmenter( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.9879, 'label': 'LABEL_184'} +{'score': 0.9973, 'label': 'snow'} +{'score': 0.9972, 'label': 'cat'} +``` + +### Estimation de la profondeur + +L'estimation de la profondeur consiste ร  prรฉdire la distance de chaque pixel d'une image par rapport ร  la camรฉra. Cette tรขche est cruciale pour comprendre et reconstruire des scรจnes rรฉelles. Par exemple, pour les voitures autonomes, il est essentiel de dรฉterminer la distance des objets tels que les piรฉtons, les panneaux de signalisation et les autres vรฉhicules pour รฉviter les collisions. L'estimation de la profondeur permet รฉgalement de crรฉer des modรจles 3D ร  partir d'images 2D, ce qui est utile pour gรฉnรฉrer des reprรฉsentations dรฉtaillรฉes de structures biologiques ou de bรขtiments. + +Il existe deux principales approches pour estimer la profondeur : + +- Stรฉrรฉo : la profondeur est estimรฉe en comparant deux images d'une mรชme scรจne prises sous des angles lรฉgรจrement diffรฉrents. +- Monoculaire : la profondeur est estimรฉe ร  partir d'une seule image. + +```py +>>> from transformers import pipeline + +>>> depth_estimator = pipeline(task="depth-estimation") +>>> preds = depth_estimator( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +``` + +## Traitement du langage naturel + +Les tรขches de traitement du langage naturel (*Natural Language Processing* ou *NLP* en anglais) sont courantes car le texte est une forme naturelle de communication pour nous. Pour qu'un modรจle puisse traiter le texte, celui-ci doit รชtre *tokenisรฉ*, c'est-ร -dire divisรฉ en mots ou sous-mots appelรฉs "*tokens*", puis converti en nombres. Ainsi, une sรฉquence de texte peut รชtre reprรฉsentรฉe comme une sรฉquence de nombres, qui peut ensuite รชtre utilisรฉe comme donnรฉes d'entrรฉe pour un modรจle afin de rรฉsoudre diverses tรขches de traitement du langage naturel. + +### Classification de texte + +La classification de texte attribue une classe ร  une sรฉquence de texte (au niveau d'une phrase, d'un paragraphe ou d'un document) ร  partir d'un ensemble de classes prรฉdรฉfini. Voici quelques applications pratiques : + +- **Analyse des sentiments** : รฉtiqueter le texte avec une polaritรฉ telle que `positive` ou `nรฉgative`, ce qui aide ร  la prise de dรฉcision dans des domaines comme la politique, la finance et le marketing. +- **Classification de contenu** : organiser et filtrer les informations en attribuant des *tags* sur des sujets spรฉcifiques, comme `mรฉtรฉo`, `sports` ou `finance`, dans les flux d'actualitรฉs et les rรฉseaux sociaux. + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="sentiment-analysis") +>>> preds = classifier("Hugging Face is the best thing since sliced bread!") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.9991, 'label': 'POSITIVE'}] +``` + +### Classification des tokens + +Dans les tรขches de traitement du language naturel, le texte est d'abord prรฉtraitรฉ en le sรฉparant en mots ou sous-mots individuels, appelรฉs *[tokens](glossary#token)*. La classification des tokens attribue une classe ร  chaque token ร  partir d'un ensemble de classes prรฉdรฉfini. + +Voici deux types courants de classification des tokens : + +- **Reconnaissance d'entitรฉs nommรฉes (*Named Entity Recognition* ou *NER* en anglais)** : รฉtiqueter un token selon une catรฉgorie d'entitรฉ, telle qu'organisation, personne, lieu ou date. La NER est particuliรจrement utilisรฉe dans les contextes biomรฉdicaux pour identifier des gรจnes, des protรฉines et des noms de mรฉdicaments. +- **ร‰tiquetage des parties du discours (*Part of Speech* ou *POS* en anglais)** : รฉtiqueter un token en fonction de sa partie du discours, comme nom, verbe ou adjectif. Le POS est utile pour les systรจmes de traduction afin de comprendre comment deux mots identiques peuvent avoir des rรดles grammaticaux diffรฉrents (par exemple, "banque" comme nom versus "banque" comme verbe). + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="ner") +>>> preds = classifier("Hugging Face is a French company based in New York City.") +>>> preds = [ +... { +... "entity": pred["entity"], +... "score": round(pred["score"], 4), +... "index": pred["index"], +... "word": pred["word"], +... "start": pred["start"], +... "end": pred["end"], +... } +... for pred in preds +... ] +>>> print(*preds, sep="\n") +{'entity': 'I-ORG', 'score': 0.9968, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2} +{'entity': 'I-ORG', 'score': 0.9293, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7} +{'entity': 'I-ORG', 'score': 0.9763, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12} +{'entity': 'I-MISC', 'score': 0.9983, 'index': 6, 'word': 'French', 'start': 18, 'end': 24} +{'entity': 'I-LOC', 'score': 0.999, 'index': 10, 'word': 'New', 'start': 42, 'end': 45} +{'entity': 'I-LOC', 'score': 0.9987, 'index': 11, 'word': 'York', 'start': 46, 'end': 50} +{'entity': 'I-LOC', 'score': 0.9992, 'index': 12, 'word': 'City', 'start': 51, 'end': 55} +``` + +### Rรฉponse ร  des questions - (*Question Answering*) + +La rรฉponse ร  des questions (*Question Answering* ou *QA* en anglais) est une tรขche de traitement du language naturel qui consiste ร  fournir une rรฉponse ร  une question, parfois avec l'aide d'un contexte (domaine ouvert) et d'autres fois sans contexte (domaine fermรฉ). Cette tรขche intervient lorsqu'on interroge un assistant virtuel, par exemple pour savoir si un restaurant est ouvert. Elle est รฉgalement utilisรฉe pour le support client, technique, et pour aider les moteurs de recherche ร  fournir des informations pertinentes. + +Il existe deux types courants de rรฉponse ร  des questions : + +- **Extractive** : pour une question donnรฉe et un contexte fourni, la rรฉponse est extraite directement du texte du contexte par le modรจle. +- **Abstractive** : pour une question donnรฉe et un contexte, la rรฉponse est gรฉnรฉrรฉe ร  partir du contexte. Cette approche utilise le [`Text2TextGenerationPipeline`] plutรดt que le [`QuestionAnsweringPipeline`] montrรฉ ci-dessous. + + +```py +>>> from transformers import pipeline + +>>> question_answerer = pipeline(task="question-answering") +>>> preds = question_answerer( +... question="What is the name of the repository?", +... context="The name of the repository is huggingface/transformers", +... ) +>>> print( +... f"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}" +... ) +score: 0.9327, start: 30, end: 54, answer: huggingface/transformers +``` + +### Rรฉsumรฉ de texte - (*Summarization*) + +Le rรฉsumรฉ de text consiste ร  crรฉer une version plus courte d'un texte tout en conservant l'essentiel du sens du document original. C'est une tรขche de sรฉquence ร  sรฉquence qui produit un texte plus condensรฉ ร  partir du texte initial. Cette technique est utile pour aider les lecteurs ร  saisir rapidement les points clรฉs de longs documents, comme les projets de loi, les documents juridiques et financiers, les brevets, et les articles scientifiques. + +Il existe deux types courants de summarization : + +- **Extractive** : identifier et extraire les phrases les plus importantes du texte original. +- **Abstractive** : gรฉnรฉrer un rรฉsumรฉ qui peut inclure des mots nouveaux non prรฉsents dans le texte d'origine. Le [`SummarizationPipeline`] utilise l'approche abstractive. + +```py +>>> from transformers import pipeline + +>>> summarizer = pipeline(task="summarization") +>>> summarizer( +... "In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. In the former task our best model outperforms even all previously reported ensembles." +... ) +[{'summary_text': ' The Transformer is the first sequence transduction model based entirely on attention . It replaces the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention . For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers .'}] +``` + +### Traduction + +La traduction convertit un texte d'une langue ร  une autre. Elle facilite la communication entre personnes de diffรฉrentes langues, permet de toucher des audiences plus larges et peut aussi servir d'outil d'apprentissage pour ceux qui apprennent une nouvelle langue. Comme le rรฉsumรฉ de texte, la traduction est une tรขche de sรฉquence ร  sรฉquence, oรน le modรจle reรงoit une sรฉquence d'entrรฉe (un texte est ici vu comme une sรฉquence de mots, ou plus prรฉcisรฉment de tokens) et produit une sรฉquence de sortie dans la langue cible. + +Initialement, les modรจles de traduction รฉtaient principalement monolingues, mais il y a eu rรฉcemment un intรฉrรชt croissant pour les modรจles multilingues capables de traduire entre plusieurs paires de langues. + +```py +>>> from transformers import pipeline + +>>> text = "translate English to French: Hugging Face is a community-based open-source platform for machine learning." +>>> translator = pipeline(task="translation", model="google-t5/t5-small") +>>> translator(text) +[{'translation_text': "Hugging Face est une tribune communautaire de l'apprentissage des machines."}] +``` + +### Modรฉlisation du langage + +La modรฉlisation du langage consiste ร  prรฉdire un mot dans un texte. Cette tรขche est devenue trรจs populaire en traitement du language naturel, car un modรจle de langage prรฉentraรฎnรฉ sur cette tรขche peut ensuite รชtre ajustรฉ (*finetuned*) pour accomplir de nombreuses autres tรขches. Rรฉcemment, les grands modรจles de langage (LLMs) ont suscitรฉ beaucoup d'intรฉrรชt pour leur capacitรฉ ร  apprendre avec peu ou pas de donnรฉes spรฉcifiques ร  une tรขche, ce qui leur permet de rรฉsoudre des problรจmes pour lesquels ils n'ont pas รฉtรฉ explicitement entraรฎnรฉs. Ces modรจles peuvent gรฉnรฉrer du texte fluide et convaincant, bien qu'il soit important de vรฉrifier leur prรฉcision. + +Il existe deux types de modรฉlisation du langage : + +- **Causale** : le modรจle prรฉdit le token suivant dans une sรฉquence, avec les tokens futurs masquรฉs. + + ```py + >>> from transformers import pipeline + + >>> prompt = "Hugging Face is a community-based open-source platform for machine learning." + >>> generator = pipeline(task="text-generation") + >>> generator(prompt) # doctest: +SKIP + ``` + +- **Masquรฉe** : le modรจle prรฉdit un token masquรฉ dans une sรฉquence en ayant accรจs ร  tous les autres tokens de la sรฉquence (passรฉ et futur). + + ```py + >>> text = "Hugging Face is a community-based open-source for machine learning." + >>> fill_mask = pipeline(task="fill-mask") + >>> preds = fill_mask(text, top_k=1) + >>> preds = [ + ... { + ... "score": round(pred["score"], 4), + ... "token": pred["token"], + ... "token_str": pred["token_str"], + ... "sequence": pred["sequence"], + ... } + ... for pred in preds + ... ] + >>> preds + [{'score': 0.2236, + 'token': 1761, + 'token_str': ' platform', + 'sequence': 'Hugging Face is a community-based open-source platform for machine learning.'}] + ``` + +## Multimodal + +Les tรขches multimodales nรฉcessitent qu'un modรจle traite plusieurs types de donnรฉes (texte, image, audio, vidรฉo) pour rรฉsoudre un problรจme spรฉcifique. Par exemple, la gรฉnรฉration de lรฉgendes pour les images est une tรขche multimodale oรน le modรจle prend une image en entrรฉe et produit une sรฉquence de texte dรฉcrivant l'image ou ses propriรฉtรฉs. + +Bien que les modรจles multimodaux traitent divers types de donnรฉes, ils convertissent toutes ces donnรฉes en *embeddings* (vecteurs ou listes de nombres contenant des informations significatives). Pour des tรขches comme la gรฉnรฉration de lรฉgendes pour les images, le modรจle apprend les relations entre les *embeddings* d'images et ceux de texte. + +### Rรฉponse ร  des questions sur des documents - (*Document Question Answering*) + +La rรฉponse ร  des questions sur des documents consiste ร  rรฉpondre ร  des questions en langage naturel en utilisant un document comme rรฉfรฉrence. Contrairement ร  la rรฉponse ร  des questions au niveau des tokens, qui prend du texte en entrรฉe, cette tรขche prend une image d'un document ainsi qu'une question concernant ce document, et fournit une rรฉponse. Elle est utile pour analyser des donnรฉes structurรฉes et extraire des informations clรฉes. Par exemple, ร  partir d'un reรงu, on peut extraire des informations telles que le montant total et le change dรป. + +```py +>>> from transformers import pipeline +>>> from PIL import Image +>>> import requests + +>>> url = "https://huggingface.co/datasets/hf-internal-testing/example-documents/resolve/main/jpeg_images/2.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> doc_question_answerer = pipeline("document-question-answering", model="magorshunov/layoutlm-invoices") +>>> preds = doc_question_answerer( +... question="What is the total amount?", +... image=image, +... ) +>>> preds +[{'score': 0.8531, 'answer': '17,000', 'start': 4, 'end': 4}] +``` + +En espรฉrant que cette page vous ait donnรฉ plus d'informations sur les diffรฉrents types de tรขches dans chaque modalitรฉ et l'importance pratique de chacune d'elles. Dans la [section suivante](tasks_explained), vous dรฉcouvrirez **comment** ๐Ÿค— Transformers fonctionne pour rรฉsoudre ces tรขches. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tasks_explained.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tasks_explained.md new file mode 100644 index 0000000000000000000000000000000000000000..775f8f4ff7fb22152781d7f85a40ae45c3fb8984 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tasks_explained.md @@ -0,0 +1,294 @@ + + +# Comment ๐Ÿค— Transformers rรฉsout ces tรขches + +Dans [Ce que ๐Ÿค— Transformers peut faire](task_summary), vous avez dรฉcouvert les tรขches de traitement du langage naturel (NLP), de traitement de la parole et de l'audio, de vision par ordinateur, ainsi que certaines de leurs applications importantes. Cette page se penche sur la maniรจre dont les modรจles rรฉsolvent ces tรขches et explique les processus en arriรจre-plan. Bien que diffรฉrents modรจles puissent utiliser diverses techniques ou approches innovantes, les modรจles Transformer suivent gรฉnรฉralement une idรฉe commune. Grรขce ร  leur architecture flexible, la plupart des modรจles sont basรฉs sur un encodeur, un dรฉcodeur ou une combinaison encodeur-dรฉcodeur. En plus des modรจles Transformer, notre bibliothรจque comprend รฉgalement des rรฉseaux de neurones convolutifs (CNN), qui restent utilisรฉs pour les tรขches de vision par ordinateur. Nous expliquerons aussi le fonctionnement d'un CNN moderne. + +Voici comment diffรฉrents modรจles rรฉsolvent des tรขches spรฉcifiques : + +- [Wav2Vec2](model_doc/wav2vec2) pour la classification audio et la reconnaissance vocale (*ASR* en anglais) +- [Vision Transformer (ViT)](model_doc/vit) et [ConvNeXT](model_doc/convnext) pour la classification d'images +- [DETR](model_doc/detr) pour la dรฉtection d'objets +- [Mask2Former](model_doc/mask2former) pour la segmentation d'images +- [GLPN](model_doc/glpn) pour l'estimation de la profondeur +- [BERT](model_doc/bert) pour les tรขches de traitement du language naturel telles que la classification de texte, la classification des tokens et la rรฉponse ร  des questions utilisant un encodeur +- [GPT2](model_doc/gpt2) pour les tรขches de traitement du language naturel telles que la gรฉnรฉration de texte utilisant un dรฉcodeur +- [BART](model_doc/bart) pour les tรขches de traitement du language naturel telles que le rรฉsumรฉ de texte et la traduction utilisant un encodeur-dรฉcodeur + + + +Avant de poursuivre, il est utile d'avoir quelques connaissances de base sur l'architecture des Transformers. Comprendre le fonctionnement des encodeurs, des dรฉcodeurs et du mรฉcanisme d'attention vous aidera ร  saisir comment les diffรฉrents modรจles Transformer fonctionnent. Si vous dรฉbutez ou avez besoin d'un rappel, consultez notre [cours](https://huggingface.co/course/chapter1/4?fw=pt) pour plus d'informations ! + + + +## Paroles et audio + +[Wav2Vec2](model_doc/wav2vec2) est un modรจle auto-supervisรฉ qui est prรฉentraรฎnรฉ sur des donnรฉes de parole non รฉtiquetรฉes et ajustรฉ sur des donnรฉes รฉtiquetรฉes pour des tรขches telles que la classification audio et la reconnaissance vocale (ASR). + +
+ +
+ +Ce modรจle comporte quatre composants principaux : + +1. **Encodeur de caractรฉristiques** (*feature encoder*): Il prend le signal audio brut, le normalise pour avoir une moyenne nulle et une variance unitaire, et le convertit en une sรฉquence de vecteurs de caractรฉristiques, chacun reprรฉsentant une durรฉe de 20 ms. + +2. **Module de quantification** (*quantization module*): Les vecteurs de caractรฉristiques sont passรฉs ร  ce module pour apprendre des unitรฉs de parole discrรจtes. Chaque vecteur est associรฉ ร  un *codebook* (une collection de mots-clรฉs), et l'unitรฉ de parole la plus reprรฉsentative est sรฉlectionnรฉe parmi celles du codebook et transmise au modรจle. + +3. **Rรฉseau de contexte** (*context network*): Environ la moitiรฉ des vecteurs de caractรฉristiques sont masquรฉs alรฉatoirement. Les vecteurs masquรฉs sont ensuite envoyรฉs ร  un *rรฉseau de contexte*, qui est un encodeur qui ajoute des embeddings positionnels relatifs. + +4. **Tรขche contrastive** (*contrastive task*): Le rรฉseau de contexte est prรฉentraรฎnรฉ avec une tรขche contrastive. Le modรจle doit prรฉdire la vรฉritable unitรฉ de parole quantifiรฉe ร  partir de la prรฉdiction masquรฉe parmi un ensemble de fausses, ce qui pousse le modรจle ร  trouver l'unitรฉ de parole quantifiรฉe la plus proche de la prรฉdiction. + +Une fois prรฉentraรฎnรฉ, wav2vec2 peut รชtre ajustรฉ sur vos propres donnรฉes pour des tรขches comme la classification audio ou la reconnaissance automatique de la parole ! + +### Classification audio + +Pour utiliser le modรจle prรฉentraรฎnรฉ pour la classification audio, ajoutez une tรชte de classification de sรฉquence au-dessus du modรจle Wav2Vec2 de base. Cette tรชte de classification est une couche linรฉaire qui reรงoit les รฉtats cachรฉs (*hidden states*) de l'encodeur. Ces รฉtats cachรฉs, qui reprรฉsentent les caractรฉristiques apprises de chaque trame audio, peuvent avoir des longueurs variables. Pour obtenir un vecteur de longueur fixe, les รฉtats cachรฉs sont d'abord regroupรฉs, puis transformรฉs en logits correspondant aux รฉtiquettes de classe. La perte d'entropie croisรฉe est calculรฉe entre les logits et la cible pour dรฉterminer la classe la plus probable. + +Prรชt ร  vous lancer dans la classification audio ? Consultez notre [guide complet de classification audio](tasks/audio_classification) pour apprendre ร  ajuster Wav2Vec2 et ร  l'utiliser pour l'infรฉrence ! + +### Reconnaissance vocale + +Pour utiliser le modรจle prรฉentraรฎnรฉ pour la reconnaissance vocale, ajoutez une tรชte de modรฉlisation du langage au-dessus du modรจle Wav2Vec2 de base pour la [classification temporelle connexionniste (CTC)](glossary#connectionist-temporal-classification-ctc). Cette tรชte de modรฉlisation du langage est une couche linรฉaire qui prend les รฉtats cachรฉs (*hidden states*) de l'encodeur et les convertit en logits. Chaque logit correspond ร  une classe de token (le nombre de tokens provient du vocabulaire de la tรขche). La perte CTC est calculรฉe entre les logits et les cibles (*targets*) pour identifier la sรฉquence de tokens la plus probable, qui est ensuite dรฉcodรฉe en transcription. + +Prรชt ร  vous lancer dans la reconnaissance automatique de la parole ? Consultez notre [guide complet de reconnaissance automatique de la parole](tasks/asr) pour apprendre ร  ajuster Wav2Vec2 et ร  l'utiliser pour l'infรฉrence ! + +## Vision par ordinateur + +Il existe deux faรงons d'aborder les tรขches de vision par ordinateur : + +1. **Diviser une image en une sรฉquence de patches** et les traiter en parallรจle avec un Transformer. +2. **Utiliser un CNN moderne**, comme [ConvNeXT](model_doc/convnext), qui repose sur des couches convolutionnelles mais adopte des conceptions de rรฉseau modernes. + + + +Une troisiรจme approche combine les Transformers avec des convolutions (par exemple, [Convolutional Vision Transformer](model_doc/cvt) ou [LeViT](model_doc/levit)). Nous ne discuterons pas de ces approches ici, car elles mรฉlangent simplement les deux approches que nous examinons. + + + +ViT et ConvNeXT sont couramment utilisรฉs pour la classification d'images. Pour d'autres tรขches de vision par ordinateur comme la dรฉtection d'objets, la segmentation et l'estimation de la profondeur, nous examinerons respectivement DETR, Mask2Former et GLPN, qui sont mieux adaptรฉs ร  ces tรขches. + +### Classification d'images + +ViT et ConvNeXT peuvent tous deux รชtre utilisรฉs pour la classification d'images ; la principale diffรฉrence rรฉside dans leurs approches : ViT utilise un mรฉcanisme d'attention tandis que ConvNeXT repose sur des convolutions. + +#### Transformer + +[ViT](model_doc/vit) remplace entiรจrement les convolutions par une architecture Transformer pure. Si vous รชtes dรฉjร  familiarisรฉ avec le Transformer original, vous trouverez que ViT suit des principes similaires, mais adaptรฉs pour traiter les images comme des sรฉquences de patches. + +
+ +
+ +Le principal changement introduit par ViT concerne la faรงon dont les images sont fournies ร  un Transformer : + +1. **Tokenisation des images** : L'image est divisรฉe en patches carrรฉs non chevauchants, chacun รฉtant transformรฉ en un vecteur ou *embedding de patch*. Ces embeddings de patch sont gรฉnรฉrรฉs ร  partir d'une couche convolutionnelle 2D pour adapter les dimensions d'entrรฉe (par exemple, 768 valeurs pour chaque embedding de patch). Si vous avez une image de 224x224 pixels, elle peut รชtre divisรฉe en 196 patches de 16x16 pixels. Ainsi, une image est "tokenisรฉe" en une sรฉquence de patches. + +2. **Token `[CLS]`** : Un *embedding apprenables* spรฉcial, appelรฉ token `[CLS]`, est ajoutรฉ au dรฉbut des embeddings de patch, similaire ร  BERT. L'รฉtat cachรฉ final du token `[CLS]` est utilisรฉ comme entrรฉe pour la tรชte de classification attachรฉe, tandis que les autres sorties sont ignorรฉes. Ce token aide le modรจle ร  encoder une reprรฉsentation globale de l'image. + +3. **Embeddings de position** : Pour que le modรจle comprenne l'ordre des patches, des *embeddings de position* sont ajoutรฉs aux embeddings de patch. Ces embeddings de position, รฉgalement apprenables et de la mรชme taille que les embeddings de patch, permettent au modรจle de saisir la structure spatiale de l'image. + +4. **Classification** : Les embeddings, enrichis des embeddings de position, sont ensuite traitรฉs par l'encodeur Transformer. La sortie associรฉe au token `[CLS]` est passรฉe ร  une tรชte de perceptron multicouche (MLP) pour la classification. La tรชte MLP convertit cette sortie en logits pour chaque รฉtiquette de classe, et la perte d'entropie croisรฉe est calculรฉe pour dรฉterminer la classe la plus probable. + +Prรชt ร  vous essayer ร  la classification d'images ? Consultez notre [guide complet de classification d'images](tasks/image_classification) pour apprendre ร  ajuster ViT et ร  l'utiliser pour l'infรฉrence ! + +#### CNN + + + +Cette section explique briรจvement les convolutions, mais il serait utile d'avoir une comprรฉhension prรฉalable de la faรงon dont elles modifient la forme et la taille d'une image. Si vous n'รชtes pas familier avec les convolutions, consultez le [chapitre sur les rรฉseaux de neurones convolutionnels](https://github.com/fastai/fastbook/blob/master/13_convolutions.ipynb) du livre fastai ! + + + +[ConvNeXT](model_doc/convnext) est une architecture CNN qui adopte des conceptions de rรฉseau modernes pour amรฉliorer les performances. Cependant, les convolutions restent au cล“ur du modรจle. D'un point de vue gรฉnรฉral, une [convolution](glossary#convolution) est une opรฉration oรน une matrice plus petite (*noyau*) est multipliรฉe par une petite fenรชtre de pixels de l'image. Elle calcule certaines caractรฉristiques ร  partir de cette fenรชtre, comme une texture particuliรจre ou la courbure d'une ligne. Ensuite, elle se dรฉplace vers la fenรชtre suivante de pixels ; la distance parcourue par la convolution est appelรฉe le *stride*. + +
+ +
+ +Une convolution de base sans padding ni stride, tirรฉe de Un guide des calculs de convolution pour l'apprentissage profond. + +Vous pouvez alimenter la sortie d'une couche convolutionnelle ร  une autre couche convolutionnelle. ร€ chaque couche successive, le rรฉseau apprend des caractรฉristiques de plus en plus complexes et abstraites, telles que des objets spรฉcifiques comme des hot-dogs ou des fusรฉes. Entre les couches convolutionnelles, il est courant d'ajouter des couches de pooling pour rรฉduire la dimensionnalitรฉ et rendre le modรจle plus robuste aux variations de position des caractรฉristiques. + +
+ +
+ +ConvNeXT modernise un CNN de cinq maniรจres : + +1. **Modification du nombre de blocs** : ConvNeXT utilise une approche similaire ร  ViT en "patchifiant" l'image avec un stride plus grand et une taille de noyau correspondante, divisant ainsi l'image en patches non chevauchants. + +2. **Couche de goulot d'รฉtranglement** (*bottleneck layer*) : Cette couche rรฉduit puis restaure le nombre de canaux pour accรฉlรฉrer les convolutions 1x1, permettant une plus grande profondeur du rรฉseau. Un goulot d'รฉtranglement inversรฉ augmente d'abord le nombre de canaux avant de les rรฉduire, optimisant ainsi l'utilisation de la mรฉmoire. + +3. **Convolution en profondeur** (*depthwise convolution*): Remplace la convolution 3x3 traditionnelle par une convolution appliquรฉe ร  chaque canal d'entrรฉe sรฉparรฉment, amรฉliorant ainsi la largeur du rรฉseau et ses performances. + +4. **Augmentation de la taille du noyau** : ConvNeXT utilise un noyau de 7x7 pour imiter le champ rรฉceptif global de ViT, ce qui permet de capturer des informations sur une plus grande partie de l'image. + +5. **Changements de conception des couches** : Le modรจle adopte des modifications inspirรฉes des Transformers, telles que moins de couches d'activation et de normalisation, l'utilisation de GELU au lieu de ReLU, et LayerNorm plutรดt que BatchNorm. + +La sortie des blocs de convolution est ensuite passรฉe ร  une tรชte de classification, qui convertit les sorties en logits et calcule la perte d'entropie croisรฉe pour dรฉterminer l'รฉtiquette la plus probable. + +### Object detection + +[DETR](model_doc/detr), *DEtection TRansformer*, est un modรจle de dรฉtection d'objets de bout en bout qui combine un CNN avec un encodeur-dรฉcodeur Transformer. + +
+ +
+ +Dรฉcomposons le fonctionnement de DETR (DEtection TRansformer) pour la dรฉtection d'objets : + +1. **Extraction des caractรฉristiques avec le CNN** : Un CNN prรฉentraรฎnรฉ, appelรฉ *backbone*, prend une image et gรฉnรจre une carte de caractรฉristiques (*feature map*) ร  basse rรฉsolution. Une convolution 1x1 est ensuite appliquรฉe pour rรฉduire la dimensionnalitรฉ et crรฉer une nouvelle carte de caractรฉristiques qui reprรฉsente des abstractions de plus haut niveau de l'image. Cette derniรจre est ensuite aplatie en une sรฉquence de vecteurs de caractรฉristiques, qui sont combinรฉs avec des embeddings positionnels. + +2. **Traitement avec l'encodeur et le dรฉcodeur** : Les vecteurs de caractรฉristiques sont passรฉs ร  l'encodeur, qui apprend les reprรฉsentations de l'image avec ses couches d'attention. Les รฉtats cachรฉs de l'encodeur sont ensuite combinรฉs avec des *objects queries* dans le dรฉcodeur. Ces *objects queries* sont des embeddings appris qui se concentrent sur diffรฉrentes rรฉgions de l'image et sont mis ร  jour ร  chaque couche d'attention. Les รฉtats cachรฉs du dรฉcodeur sont utilisรฉs pour prรฉdire les coordonnรฉes de la boรฎte englobante (*bounding box*) et le label de la classe pour chaque objet query, ou `pas d'objet` si aucun objet n'est dรฉtectรฉ. + +3. **Perte de correspondance bipartite** : Lors de l'entraรฎnement, DETR utilise une *perte de correspondance bipartite* pour comparer un nombre fixe de prรฉdictions avec un ensemble fixe de labels de vรฉritรฉ terrain. Si le nombre de labels de vรฉritรฉ terrain est infรฉrieur au nombre de *N* labels, ils sont complรฉtรฉs avec une classe `pas d'objet`. Cette fonction de perte encourage DETR ร  trouver une correspondance un ร  un entre les prรฉdictions et les labels de vรฉritรฉ terrain. Si les boรฎtes englobantes ou les labels de classe ne sont pas corrects, une perte est encourue. De mรชme, si DETR prรฉdit un objet inexistant, il est pรฉnalisรฉ. Cela encourage DETR ร  trouver d'autres objets dans l'image au lieu de se concentrer sur un seul objet trรจs proรฉminent. + +Une tรชte de dรฉtection d'objets est ajoutรฉe au-dessus de DETR pour trouver le label de la classe et les coordonnรฉes de la boรฎte englobante. Cette tรชte de dรฉtection d'objets comprend deux composants : une couche linรฉaire pour transformer les รฉtats cachรฉs du dรฉcodeur en logits sur les labels de classe, et un MLP pour prรฉdire la boรฎte englobante. + +Prรชt ร  essayer la dรฉtection d'objets ? Consultez notre guide complet sur la [dรฉtection d'objets](tasks/object_detection) pour apprendre ร  affiner DETR et ร  l'utiliser pour l'infรฉrence ! + +### Segmentation d'image + +[Mask2Former](model_doc/mask2former) est une architecture polyvalente conรงue pour traiter tous les types de tรขches de segmentation d'image. Contrairement aux modรจles de segmentation traditionnels, qui sont gรฉnรฉralement spรฉcialisรฉs dans des sous-tรขches spรฉcifiques comme la segmentation d'instances, sรฉmantique ou panoptique, Mask2Former aborde chaque tรขche comme un problรจme de *classification de masques*. Cette approche regroupe les pixels en *N* segments et prรฉdit pour chaque image *N* masques ainsi que leur รฉtiquette de classe correspondante. Dans cette section, nous vous expliquerons le fonctionnement de Mask2Former et vous aurez la possibilitรฉ d'effectuer un rรฉglage fin (*fine-tuning*) de SegFormer ร  la fin. + +
+ +
+ +Il y a trois composants principaux dans Mask2Former : + +1. Un [backbone Swin](model_doc/swin) qui prend une image en entrรฉe et gรฉnรจre une carte de caractรฉristiques (*feature map*) ร  basse rรฉsolution aprรจs trois convolutions successives de 3x3. + +2. Cette carte de caractรฉristiques est ensuite envoyรฉe ร  un *dรฉcodeur de pixels*, qui augmente progressivement la rรฉsolution des caractรฉristiques pour obtenir des embeddings par pixel en haute rรฉsolution. Le dรฉcodeur de pixels produit des caractรฉristiques multi-รฉchelles, comprenant des rรฉsolutions de 1/32, 1/16, et 1/8 de l'image originale. + +3. Les cartes de caractรฉristiques ร  diffรฉrentes รฉchelles sont successivement traitรฉes par une couche de dรฉcodeur Transformer, permettant de capturer les petits objets ร  partir des caractรฉristiques haute rรฉsolution. Le point central de Mask2Former est le mรฉcanisme de *masquage d'attention* dans le dรฉcodeur. Contrairement ร  l'attention croisรฉe, qui peut se concentrer sur l'ensemble de l'image, l'attention masquรฉe se focalise uniquement sur certaines zones spรฉcifiques. Cette approche est plus rapide et amรฉliore les performances en permettant au modรจle de se concentrer sur les dรฉtails locaux de l'image. + +4. ร€ l'instar de [DETR](tasks_explained#object-detection), Mask2Former utilise รฉgalement des requรชtes d'objet apprises, qu'il combine avec les caractรฉristiques de l'image du dรฉcodeur de pixels pour faire des prรฉdictions globales (c'est-ร -dire, `รฉtiquette de classe`, `prรฉdiction de masque`). Les รฉtats cachรฉs du dรฉcodeur sont passรฉs dans une couche linรฉaire pour รชtre transformรฉs en logits correspondant aux รฉtiquettes de classe. La perte d'entropie croisรฉe est alors calculรฉe entre les logits et l'รฉtiquette de classe pour dรฉterminer la plus probable. + + Les prรฉdictions de masque sont gรฉnรฉrรฉes en combinant les embeddings de pixels avec les รฉtats cachรฉs finaux du dรฉcodeur. La perte d'entropie croisรฉe sigmoรฏde et la perte de Dice sont calculรฉes entre les logits et le masque de vรฉritรฉ terrain pour dรฉterminer le masque le plus probable. + +Prรชt ร  vous lancer dans la dรฉtection d'objets ? Consultez notre [guide complet sur la segmentation d'image](tasks/semantic_segmentation) pour apprendre ร  affiner SegFormer et l'utiliser pour l'infรฉrence ! + +### Estimation de la profondeur + +[GLPN](model_doc/glpn), *Global-Local Path Network*, est un Transformer pour l'estimation de profondeur qui combine un encodeur [SegFormer](model_doc/segformer) avec un dรฉcodeur lรฉger. + +
+ +
+1. Comme avec ViT, une image est divisรฉe en une sรฉquence de patches, mais ces patches sont plus petits. Cette approche est particuliรจrement adaptรฉe aux tรขches de prรฉdiction dense telles que la segmentation ou l'estimation de profondeur. Les patches d'image sont transformรฉs en embeddings (voir la section [classification d'image](#image-classification) pour plus de dรฉtails sur la crรฉation des embeddings), puis envoyรฉs ร  l'encodeur. + +2. L'encodeur traite les embeddings de patches ร  travers plusieurs blocs d'encodeur. Chaque bloc comprend des couches d'attention et de Mix-FFN, conรงues pour fournir des informations positionnelles. ร€ la fin de chaque bloc, une couche de *fusion de patches* crรฉe des reprรฉsentations hiรฉrarchiques. Les caractรฉristiques des groupes de patches voisins sont concatรฉnรฉes, et une couche linรฉaire est appliquรฉe pour rรฉduire le nombre de patches ร  une rรฉsolution de 1/4. Ce processus est rรฉpรฉtรฉ dans les blocs suivants jusqu'ร  obtenir des caractรฉristiques d'image avec des rรฉsolutions de 1/8, 1/16, et 1/32. + +3. Un dรฉcodeur lรฉger prend la derniรจre carte de caractรฉristiques (ร  l'รฉchelle 1/32) de l'encodeur et l'agrandit ร  l'รฉchelle 1/16. Ensuite, cette caractรฉristique passe par un module de *Fusion de Caractรฉristiques Sรฉlective (SFF)*, qui sรฉlectionne et combine les caractรฉristiques locales et globales ร  partir d'une carte d'attention pour chaque caractรฉristique, puis l'agrandit ร  1/8. Ce processus est rรฉpรฉtรฉ jusqu'ร  ce que les caractรฉristiques dรฉcodรฉes aient la mรชme taille que l'image originale. La sortie est ensuite traitรฉe par deux couches de convolution, suivies d'une activation sigmoรฏde pour prรฉdire la profondeur de chaque pixel. + +## Traitement du langage naturel + +Le Transformer a รฉtรฉ initialement conรงu pour la traduction automatique, et depuis, il est devenu pratiquement l'architecture par dรฉfaut pour rรฉsoudre toutes les tรขches de traitement du langage naturel (NLP). Certaines tรขches se prรชtent bien ร  la structure d'encodeur du Transformer, tandis que d'autres sont mieux adaptรฉes au dรฉcodeur. D'autres tรขches encore utilisent ร  la fois la structure encodeur-dรฉcodeur du Transformer. + +### Classification de texte + +[BERT](model_doc/bert) est un modรจle basรฉ uniquement sur l'encodeur, qui a รฉtรฉ le premier ร  intรฉgrer efficacement la bidirectionnalitรฉ profonde pour obtenir des reprรฉsentations plus riches du texte en tenant compte des mots en amont et en aval. + +1. BERT utilise la tokenisation [WordPiece](tokenizer_summary#wordpiece) pour gรฉnรฉrer des embeddings de tokens ร  partir du texte. Pour diffรฉrencier une seule phrase d'une paire de phrases, un token spรฉcial `[SEP]` est ajoutรฉ. De plus, un token spรฉcial `[CLS]` est placรฉ au dรฉbut de chaque sรฉquence de texte. La sortie finale associรฉe au token `[CLS]` est utilisรฉe comme entrรฉe pour la tรชte de classification des tรขches. BERT ajoute รฉgalement un embedding de segment pour indiquer si un token appartient ร  la premiรจre ou ร  la deuxiรจme phrase dans une paire. + +2. BERT est prรฉentraรฎnรฉ avec deux objectifs : le masquage de mots (masked language modeling) et la prรฉdiction de la phrase suivante. Pour le masquage de mots, un pourcentage des tokens d'entrรฉe est masquรฉ alรฉatoirement, et le modรจle doit prรฉdire ces mots. Cela permet de surmonter le problรจme de la bidirectionnalitรฉ, oรน le modรจle pourrait autrement tricher en voyant tous les mots et en "prรฉdire" le mot suivant. Les รฉtats cachรฉs finaux des tokens masquรฉs sont passรฉs ร  un rรฉseau feedforward avec une fonction softmax sur le vocabulaire pour prรฉdire le mot masquรฉ. + + Le deuxiรจme objectif de prรฉentraรฎnement est la prรฉdiction de la phrase suivante. Le modรจle doit dรฉterminer si la phrase B suit la phrase A. Dans la moitiรฉ des cas, la phrase B est la phrase suivante, et dans l'autre moitiรฉ, elle est alรฉatoire. Cette prรฉdiction (phrase suivante ou non) est envoyรฉe ร  un rรฉseau feedforward avec une softmax sur les deux classes (`IsNext` et `NotNext`). + +3. Les embeddings d'entrรฉe sont traitรฉs par plusieurs couches d'encodeur pour produire des รฉtats cachรฉs finaux. + +Pour utiliser le modรจle prรฉentraรฎnรฉ pour la classification de texte, ajoutez une tรชte de classification de sรฉquence au-dessus du modรจle BERT de base. Cette tรชte est une couche linรฉaire qui prend les รฉtats cachรฉs finaux et les transforme en logits. La perte d'entropie croisรฉe est ensuite calculรฉe entre les logits et les cibles pour dรฉterminer l'รฉtiquette la plus probable. + +Prรชt ร  essayer la classification de texte ? Consultez notre [guide complet sur la classification de texte](tasks/sequence_classification) pour apprendre ร  effectuer un rรฉglagle fin (*fine-tuning*) de DistilBERT et l'utiliser pour l'infรฉrence ! + +### Classification de tokens + +Pour utiliser BERT dans des tรขches de classification de tokens, comme la reconnaissance d'entitรฉs nommรฉes (NER), ajoutez une tรชte de classification de tokens au-dessus du modรจle BERT de base. Cette tรชte est une couche linรฉaire qui prend les รฉtats cachรฉs finaux et les transforme en logits. La perte d'entropie croisรฉe est ensuite calculรฉe entre les logits et les labels de chaque token pour dรฉterminer l'รฉtiquette la plus probable. + +Prรชt ร  essayer la classification de tokens ? Consultez notre [guide complet sur la classification de tokens](tasks/token_classification) pour dรฉcouvrir comment effectuer un rรฉglagle fin (*fine-tuning*) de DistilBERT et l'utiliser pour l'infรฉrence ! + +### Rรฉponse aux questions - (*Question Answering*) + +Pour utiliser BERT pour la rรฉponse aux questions, ajoutez une tรชte de classification de span au-dessus du modรจle BERT de base. Cette tรชte est une couche linรฉaire qui transforme les รฉtats cachรฉs finaux en logits pour les positions de dรฉbut et de fin du `span` correspondant ร  la rรฉponse. La perte d'entropie croisรฉe est calculรฉe entre les logits et les positions rรฉelles pour dรฉterminer le span de texte le plus probable en tant que rรฉponse. + +Prรชt ร  essayer la rรฉponse aux questions ? Consultez notre [guide complet sur la rรฉponse aux questions](tasks/question_answering) pour dรฉcouvrir comment effectuer un rรฉglagle fin (*fine-tuning*) de DistilBERT et l'utiliser pour l'infรฉrence ! + + + +๐Ÿ’ก Une fois BERT prรฉentraรฎnรฉ, il est incroyablement facile de lโ€™adapter ร  diverses tรขches ! Il vous suffit dโ€™ajouter une tรชte spรฉcifique au modรจle prรฉentraรฎnรฉ pour transformer les รฉtats cachรฉs en la sortie souhaitรฉe. + + + +### Gรฉnรฉration de texte + +[GPT-2](model_doc/gpt2) est un modรจle basรฉ uniquement sur le dรฉcodeur, prรฉentraรฎnรฉ sur une grande quantitรฉ de texte. Il peut gรฉnรฉrer du texte convaincant (bien que parfois inexact !) ร  partir d'une invite et accomplir d'autres tรขches de NLP, comme la rรฉponse aux questions, mรชme s'il n'a pas รฉtรฉ spรฉcifiquement entraรฎnรฉ pour ces tรขches. + +
+ +
+ +1. GPT-2 utilise le [byte pair encoding (BPE)](tokenizer_summary#bytepair-encoding-bpe) pour tokeniser les mots et gรฉnรฉrer des embeddings de tokens. Des encodages positionnels sont ajoutรฉs pour indiquer la position de chaque token dans la sรฉquence. Les embeddings d'entrรฉe passent ร  travers plusieurs blocs de dรฉcodeur pour produire des รฉtats cachรฉs finaux. Chaque bloc de dรฉcodeur utilise une couche d'*attention masquรฉe*, ce qui signifie que GPT-2 ne peut pas se concentrer sur les tokens futurs et est uniquement autorisรฉ ร  se focaliser sur les tokens ร  gauche dans le texte. Cela diffรจre du token [`mask`] de BERT, car ici, dans l'attention masquรฉe, un masque d'attention est utilisรฉ pour attribuer un score de `0` aux tokens futurs. + +2. La sortie du dรฉcodeur est ensuite envoyรฉe ร  une tรชte de modรฉlisation du langage, qui effectue une transformation linรฉaire pour convertir les รฉtats cachรฉs en logits. L'รฉtiquette est le token suivant dans la sรฉquence, obtenue en dรฉcalant les logits vers la droite d'une position. La perte d'entropie croisรฉe est calculรฉe entre les logits dรฉcalรฉs et les รฉtiquettes pour dรฉterminer le token suivant le plus probable. + +L'objectif de prรฉentraรฎnement de GPT-2 est basรฉ sur la [modรฉlisation du langage causale](glossary#causal-language-modeling), qui consiste ร  prรฉdire le mot suivant dans une sรฉquence. Cette approche rend GPT-2 particuliรจrement efficace pour les tรขches de gรฉnรฉration de texte. + +Prรชt ร  essayer la gรฉnรฉration de texte ? Consultez notre [guide complet sur la modรฉlisation du langage causale](tasks/language_modeling#causal-language-modeling) pour dรฉcouvrir comment effectuer un rรฉglagle fin (*fine-tuning*) de DistilGPT-2 et l'utiliser pour l'infรฉrence ! + + + +Pour plus d'informations sur la gรฉnรฉration de texte, consultez le guide sur les [stratรฉgies de gรฉnรฉration de texte](generation_strategies) ! + + + +### Rรฉsumรฉ de texte + +Les modรจles encodeur-dรฉcodeur tels que [BART](model_doc/bart) et [T5](model_doc/t5) sont conรงus pour les tรขches de rรฉsumรฉ en mode sรฉquence-ร -sรฉquence. Dans cette section, nous expliquerons le fonctionnement de BART, puis vous aurez l'occasion de dรฉcouvrir comment rรฉaliser un rรฉglagle fin (*fine-tuning*) de T5. + +
+ +
+ +1. L'architecture de l'encodeur de BART est trรจs similaire ร  celle de BERT, acceptant des embeddings de tokens et des embeddings positionnels du texte. BART est prรฉentraรฎnรฉ en corrompant l'entrรฉe et en la reconstruisant avec le dรฉcodeur. Contrairement ร  d'autres encodeurs utilisant des stratรฉgies de corruption spรฉcifiques, BART peut appliquer divers types de corruption, parmi lesquelles la stratรฉgie de *text infilling* est la plus efficace. Dans le text infilling, plusieurs segments de texte sont remplacรฉs par un **seul** token [`mask`]. Cette approche est cruciale car elle force le modรจle ร  prรฉdire les tokens masquรฉs et ร  estimer le nombre de tokens manquants. Les embeddings d'entrรฉe et les spans masquรฉs sont passรฉs ร  l'encodeur pour produire des รฉtats cachรฉs finaux. Contrairement ร  BERT, BART ne comporte pas de rรฉseau feedforward final pour prรฉdire un mot. + +2. La sortie de l'encodeur est transmise au dรฉcodeur, qui doit prรฉdire ร  la fois les tokens masquรฉs et les tokens non corrompus. Ce contexte supplรฉmentaire aide le dรฉcodeur ร  restaurer le texte original. La sortie du dรฉcodeur est ensuite envoyรฉe ร  une tรชte de modรฉlisation du langage, qui transforme les รฉtats cachรฉs en logits. La perte d'entropie croisรฉe est calculรฉe entre les logits et l'รฉtiquette, qui est simplement le token dรฉcalรฉ vers la droite. + +Prรชt ร  essayer le rรฉsumรฉ ? Consultez notre [guide complet sur le rรฉsumรฉ](tasks/summarization) pour apprendre ร  effectuer un rรฉglage fin (*fine-tuning*) de T5 et l'utiliser pour l'infรฉrence ! + + + +Pour plus d'informations sur la gรฉnรฉration de texte, consultez le guide sur les [stratรฉgies de gรฉnรฉration de texte](generation_strategies) ! + + + +### Traduction + +La traduction est un autre exemple de tรขche sรฉquence-ร -sรฉquence, ce qui signifie qu'un modรจle encodeur-dรฉcodeur comme [BART](model_doc/bart) ou [T5](model_doc/t5) peut รชtre utilisรฉ pour cette tรขche. Nous expliquerons ici comment BART fonctionne pour la traduction, puis vous pourrez dรฉcouvrir comment affiner T5. + +BART adapte le modรจle ร  la traduction en ajoutant un encodeur sรฉparรฉ, initialisรฉ alรฉatoirement, pour mapper la langue source en une entrรฉe qui peut รชtre dรฉcodรฉe dans la langue cible. Les embeddings de cet encodeur sont ensuite passรฉs ร  l'encodeur prรฉentraรฎnรฉ au lieu des embeddings de mots originaux. L'encodeur source est entraรฎnรฉ en mettant ร  jour l'encodeur source, les embeddings positionnels et les embeddings d'entrรฉe avec la perte d'entropie croisรฉe provenant de la sortie du modรจle. Les paramรจtres du modรจle sont figรฉs lors de cette premiรจre รฉtape, et tous les paramรจtres du modรจle sont entraรฎnรฉs ensemble lors de la deuxiรจme รฉtape. + +BART a รฉtรฉ suivi par une version multilingue, mBART, qui est spรฉcifiquement conรงue pour la traduction et prรฉentraรฎnรฉe sur de nombreuses langues diffรฉrentes. + +Prรชt ร  essayer la traduction ? Consultez notre [guide complet sur la traduction](tasks/translation) pour apprendre ร  affiner T5 et l'utiliser pour l'infรฉrence ! + + + +Pour plus d'informations sur la gรฉnรฉration de texte, consultez le guide sur les [stratรฉgies de gรฉnรฉration de texte](generation_strategies) ! + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tutoriel_pipeline.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tutoriel_pipeline.md new file mode 100644 index 0000000000000000000000000000000000000000..d398f2c0f0f51b8cdb3c350a1c431f1a2a6d3de3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/fr/tutoriel_pipeline.md @@ -0,0 +1,313 @@ + + +# Pipelines pour l'infรฉrence + +L'objet [`pipeline`] rend simple l'utilisation de n'importe quel modรจle du [Hub](https://huggingface.co/models) pour l'infรฉrence sur n'importe quelle langue, tรขches de vision par ordinateur, d'audio et multimodales. Mรชme si vous n'avez pas d'expรฉrience avec une modalitรฉ spรฉcifique ou si vous n'รชtes pas familier avec le code ci-dessous des modรจles, vous pouvez toujours les utiliser pour l'infรฉrence avec la [`pipeline`] ! Ce tutoriel vous apprendra ร  : + +* Utiliser un [`pipeline`] pour l'infรฉrence. +* Utiliser un tokenizer ou modรจle spรฉcifique. +* Utiliser un [`pipeline`] pour des tรขches audio, de vision et multimodales. + + + +Consultez la documentation du [`pipeline`] pour une liste complรจte des tรขches prises en charge et des paramรจtres disponibles. + + + +## Utilisation du pipeline + +Bien que chaque tรขche ait son propre [`pipeline`], il est plus simple d'utiliser le [`pipeline`] gรฉnรฉrale qui inclut tous les pipelines spรฉcifiques aux diffรฉrentes tรขches. Cette approche charge automatiquement un modรจle par dรฉfaut et une classe de prรฉtraitement adaptรฉe ร  votre tรขche, simplifiant ainsi votre utilisation. Prenons l'exemple de l'utilisation du [`pipeline`] pour la reconnaissance automatique de la parole (ASR) ou de la transcription de la parole en texte. + +1. Commencez par crรฉer un [`pipeline`] et spรฉcifiez la tรขche d'infรฉrence : + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition") +``` + +2. Passez votre entrรฉe au [`pipeline`]. Dans le cas de la reconnaissance vocale, il s'agit d'un fichier audio : + +```py +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'} +``` + +Pas le rรฉsultat que vous aviez en tรชte ? Consultez certains des [modรจles de reconnaissance vocale automatique les plus tรฉlรฉchargรฉs](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=trending) +sur le Hub pour voir si vous pouvez obtenir une meilleure transcription. + +Essayons le modรจle [Whisper large-v2](https://huggingface.co/openai/whisper-large) de OpenAI. Whisper a รฉtรฉ publiรฉ 2 ans aprรจs Wav2Vec2 et a รฉtรฉ entraรฎnรฉ sur prรจs de 10 fois plus de donnรฉes. En tant que tel, il surpasse Wav2Vec2 sur la plupart des benchmarks en aval. Il a รฉgalement l'avantage supplรฉmentaire de prรฉdire la ponctuation et la casse, ce qui n'est pas possible avec Wav2Vec2. + +Essayons-le ici pour voir comment il fonctionne : + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +Maintenant, ce rรฉsultat semble plus prรฉcis ! Pour une comparaison approfondie entre Wav2Vec2 et Whisper, consultez le [cours Audio Transformers](https://huggingface.co/learn/audio-course/chapter5/asr_models). +Nous vous encourageons vraiment ร  consulter le Hub pour des modรจles dans diffรฉrentes langues, des modรจles spรฉcialisรฉs dans votre domaine, et plus encore. +Vous pouvez consulter et comparer les rรฉsultats des modรจles directement depuis votre navigateur sur le Hub pour voir s'ils conviennent ou gรจrent mieux les cas particuliers que d'autres. +Et si vous ne trouvez pas de modรจle pour votre cas d'utilisation, vous pouvez toujours commencer ร  [entraรฎner](training) le vรดtre ! + +Si vous avez plusieurs entrรฉes, vous pouvez passer votre entrรฉe sous forme de liste : + +```py +transcriber( + [ + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", + ] +) +``` + +Les pipelines sont excellents pour l'expรฉrimentation car passer d'un modรจle ร  un autre est trivial ; cependant, il existe des moyens de les optimiser pour des charges de travail plus importantes que l'expรฉrimentation. Consultez les guides suivants qui expliquent comment itรฉrer sur des ensembles de donnรฉes complets ou utiliser des pipelines dans un serveur web : +de la documentation : +* [Utilisation des pipelines sur un ensemble de donnรฉes](#using-pipelines-on-a-dataset) +* [Utilisation des pipelines pour un serveur web](./pipeline_webserver) + +## Paramรจtres + +[`pipeline`] prend en charge de nombreux paramรจtres ; certains sont spรฉcifiques ร  la tรขche et d'autres sont gรฉnรฉraux pour tous les pipelines. +En gรฉnรฉral, vous pouvez spรฉcifier les paramรจtres oรน vous le souhaitez : + +```py +transcriber = pipeline(model="openai/whisper-large-v2", my_parameter=1) + +out = transcriber(...) # This will use `my_parameter=1`. +out = transcriber(..., my_parameter=2) # This will override and use `my_parameter=2`. +out = transcriber(...) # This will go back to using `my_parameter=1`. +``` + +Voyons 3 paramรจtres importants : + +### Device + +Si vous utilisez `device=n`, le pipeline met automatiquement le modรจle sur l'appareil spรฉcifiรฉ. +Cela fonctionnera que vous utilisiez PyTorch ou Tensorflow. + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0) +``` + +Si le modรจle est trop grand pour un seul GPU et que vous utilisez PyTorch, vous pouvez dรฉfinir `device_map="auto"` pour dรฉterminer automatiquement comment charger et stocker les poids du modรจle. L'utilisation de l'argument `device_map` nรฉcessite le package ๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) : + +```bash +pip install --upgrade accelerate +``` + +Le code suivant charge et stocke automatiquement les poids du modรจle sur plusieurs appareils : + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device_map="auto") +``` + +Notez que si `device_map="auto"` est passรฉ, il n'est pas nรฉcessaire d'ajouter l'argument `device=device` lors de l'instanciation de votre `pipeline` car vous pourriez rencontrer des comportements inattendus ! + +### Batch size + +Par dรฉfaut, les pipelines ne feront pas d'infรฉrence en batch pour des raisons expliquรฉes en dรฉtail [ici](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching). La raison est que le batching n'est pas nรฉcessairement plus rapide, et peut en fait รชtre beaucoup plus lent dans certains cas. + +Mais si cela fonctionne dans votre cas d'utilisation, vous pouvez utiliser : + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0, batch_size=2) +audio_filenames = [f"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/{i}.flac" for i in range(1, 5)] +texts = transcriber(audio_filenames) +``` + +Cela exรฉcute le pipeline sur les 4 fichiers audio fournis, mais les passera par batch de 2 au modรจle (qui est sur un GPU, oรน le batching est plus susceptible d'aider) sans nรฉcessiter de code supplรฉmentaire de votre part. +La sortie doit toujours correspondre ร  ce que vous auriez reรงu sans batching. Il s'agit uniquement d'un moyen de vous aider ร  obtenir plus de vitesse avec un pipeline. + +Les pipelines peuvent รฉgalement attรฉnuer certaines des complexitรฉs du batching car, pour certains pipelines, un seul รฉlรฉment (comme un long fichier audio) doit รชtre divisรฉ en plusieurs parties pour รชtre traitรฉ par un modรจle. Le pipeline effectue ce [*batching par morceaux*](./main_classes/pipelines#pipeline-chunk-batching) pour vous. + +### Paramรจtres spรฉcifiques ร  la tรขche + +Toutes les tรขches fournissent des paramรจtres spรฉcifiques ร  la tรขche qui permettent une flexibilitรฉ et des options supplรฉmentaires pour vous aider ร  accomplir votre travail. +Par exemple, la mรฉthode [`transformers.AutomaticSpeechRecognitionPipeline.__call__`] dispose d'un paramรจtre `return_timestamps` qui semble prometteur pour le sous-titrage des vidรฉos : + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2", return_timestamps=True) +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.', 'chunks': [{'timestamp': (0.0, 11.88), 'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its'}, {'timestamp': (11.88, 12.38), 'text': ' creed.'}]} +``` + +Comme vous pouvez le voir, le modรจle a infรฉrรฉ le texte et a รฉgalement indiquรฉ **quand** les diffรฉrentes phrases ont รฉtรฉ prononcรฉes. + +Il existe de nombreux paramรจtres disponibles pour chaque tรขche, alors consultez la rรฉfรฉrence API de chaque tรขche pour voir ce que vous pouvez ajuster ! +Par exemple, le [`~transformers.AutomaticSpeechRecognitionPipeline`] dispose d'un paramรจtre `chunk_length_s` qui est utile pour travailler sur des fichiers audio trรจs longs (par exemple, le sous-titrage de films entiers ou de vidรฉos d'une heure) qu'un modรจle ne peut gรฉnรฉralement pas gรฉrer seul : + +```python +>>> transcriber = pipeline(model="openai/whisper-large-v2", chunk_length_s=30) +>>> transcriber("https://huggingface.co/datasets/reach-vb/random-audios/resolve/main/ted_60.wav") +{'text': " So in college, I was a government major, which means I had to write a lot of papers. Now, when a normal student writes a paper, they might spread the work out a little like this. So, you know. You get started maybe a little slowly, but you get enough done in the first week that with some heavier days later on, everything gets done and things stay civil. And I would want to do that like that. That would be the plan. I would have it all ready to go, but then actually the paper would come along, and then I would kind of do this. And that would happen every single paper. But then came my 90-page senior thesis, a paper you're supposed to spend a year on. I knew for a paper like that, my normal workflow was not an option, it was way too big a project. So I planned things out and I decided I kind of had to go something like this. This is how the year would go. So I'd start off light and I'd bump it up"} +``` + +Si vous ne trouvez pas un paramรจtre qui vous aiderait vraiment, n'hรฉsitez pas ร  [le demander](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml) ! + +## Utilisation des pipelines sur un ensemble de donnรฉes + +Le pipeline peut รฉgalement exรฉcuter des infรฉrences sur un grand ensemble de donnรฉes. Le moyen le plus simple que nous recommandons pour cela est d'utiliser un itรฉrateur : + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + + +pipe = pipeline(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipe(data()): + generated_characters += len(out[0]["generated_text"]) +``` + + +L'itรฉrateur `data()` gรฉnรจre chaque rรฉsultat, et le pipeline reconnaรฎt automatiquement que l'entrรฉe est itรฉrable et commencera ร  rรฉcupรฉrer les donnรฉes tout en continuant ร  les traiter sur le GPU (cela utilise [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader) sous le capot). +C'est important car vous n'avez pas besoin d'allouer de mรฉmoire pour l'ensemble de donnรฉes complet et vous pouvez alimenter le GPU aussi rapidement que possible. + +ร‰tant donnรฉ que le lotissement pourrait accรฉlรฉrer les choses, il peut รชtre utile d'essayer de rรฉgler le paramรจtre `batch_size` ici. + +La faรงon la plus simple d'itรฉrer sur un ensemble de donnรฉes est d'en charger un depuis ๐Ÿค— [Datasets](https://github.com/huggingface/datasets) : + +```py +# KeyDataset is a util that will just output the item we're interested in. +from transformers.pipelines.pt_utils import KeyDataset +from datasets import load_dataset + +pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) +dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") + +for out in pipe(KeyDataset(dataset, "audio")): + print(out) +``` + +## Utilisation des pipelines pour un serveur web + + +Crรฉer un moteur d'infรฉrence est un sujet complexe qui mรฉrite sa propre page. + + +[Lien](./pipeline_webserver) + +## Pipeline de vision + +Utiliser un [`pipeline`] pour les tรขches de vision est pratiquement identique. + +Spรฉcifiez votre tรขche et passez votre image au classificateur. L'image peut รชtre un lien, un chemin local ou une image encodรฉe en base64. Par exemple, quelle espรจce de chat est montrรฉe ci-dessous ? + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(model="google/vit-base-patch16-224") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` + + +## Pipeline de texte + +Utiliser un [`pipeline`] pour les tรขches de NLP est pratiquement identique. + +```py +>>> from transformers import pipeline + +>>> # This model is a `zero-shot-classification` model. +>>> # It will classify text, except you are free to choose any label you might imagine +>>> classifier = pipeline(model="facebook/bart-large-mnli") +>>> classifier( +... "I have a problem with my iphone that needs to be resolved asap!!", +... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], +... ) +{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]} +``` + + +## Pipeline multimodal + +Le [`pipeline`] prend en charge plus d'une modalitรฉ. Par exemple, une tรขche de rรฉponse ร  des questions visuelles (VQA) combine texte et image. N'hรฉsitez pas ร  utiliser n'importe quel lien d'image que vous aimez et une question que vous souhaitez poser ร  propos de l'image. L'image peut รชtre une URL ou un chemin local vers l'image. + +Par exemple, si vous utilisez cette [image de facture](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png) : + +```py +>>> from transformers import pipeline + +>>> vqa = pipeline(model="impira/layoutlm-document-qa") +>>> output = vqa( +... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", +... question="What is the invoice number?", +... ) +>>> output[0]["score"] = round(output[0]["score"], 3) +>>> output +[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}] +``` + + + +Pour exรฉcuter l'exemple ci-dessus, vous devez avoir [`pytesseract`](https://pypi.org/project/pytesseract/) installรฉ en plus de ๐Ÿค— Transformers : + +```bash +sudo apt install -y tesseract-ocr +pip install pytesseract +``` + + + +## Utilisation de `pipeline` sur de grands modรจles avec ๐Ÿค— `accelerate` : + +Vous pouvez facilement exรฉcuter `pipeline` sur de grands modรจles en utilisant ๐Ÿค— `accelerate` ! Assurez-vous d'abord d'avoir installรฉ `accelerate` avec `pip install accelerate`. + +Chargez d'abord votre modรจle en utilisant `device_map="auto"` ! Nous utiliserons `facebook/opt-1.3b` pour notre exemple. + +```py +# pip install accelerate +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto") +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` +Vous pouvez รฉgalement passer des modรจles chargรฉs en 8 bits si vous installez `bitsandbytes` et ajoutez l'argument `load_in_8bit=True` +Notez que vous pouvez remplacer le point de contrรดle par n'importe quel modรจle. + +```py +# pip install accelerate bitsandbytes +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}) +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +## Crรฉation de dรฉmonstrations web ร  partir de pipelines avec `gradio` + +Hugging Face prenant en charge le chargement de grands modรจles, comme BLOOM. +Les pipelines sont automatiquement pris en charge dans [Gradio](https://github.com/gradio-app/gradio/), une bibliothรจque qui facilite la crรฉation d'applications d'apprentissage automatique belles et conviviales sur le web. Tout d'abord, assurez-vous que Gradio est installรฉ : + +``` +pip install gradio +``` + +Ensuite, vous pouvez crรฉer une dรฉmonstration web autour d'un pipeline de classification d'images (ou tout autre pipeline) en une seule ligne de code en appelant la fonction [`Interface.from_pipeline`](https://www.gradio.app/docs/interface#interface-from-pipeline) de Gradio pour lancer le pipeline. Cela crรฉe une interface intuitive de glisser-dรฉposer dans votre navigateur : + +```py +from transformers import pipeline +import gradio as gr + +pipe = pipeline("image-classification", model="google/vit-base-patch16-224") + +gr.Interface.from_pipeline(pipe).launch() +``` + +![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/panda-classification.png) + + +Par dรฉfaut, la dรฉmonstration web s'exรฉcute sur un serveur local. Si vous souhaitez la partager avec d'autres, vous pouvez gรฉnรฉrer un lien public temporaire en dรฉfinissant `share=True` dans `launch()`. Vous pouvez รฉgalement hรฉberger votre dรฉmonstration sur [Hugging Face Spaces](https://huggingface.co/spaces) pour obtenir un lien permanent. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/_toctree.yml b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/_toctree.yml new file mode 100644 index 0000000000000000000000000000000000000000..72759457a5c83a57620984bce73695b989f41cba --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/_toctree.yml @@ -0,0 +1,7 @@ +- sections: + - local: pipeline_tutorial + title: เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เฅ‡ เคธเคพเคฅ เค…เคจเฅเคฎเคพเคจ เคšเคฒเคพเคเค + - local: accelerate + title: ๐Ÿค— Accelerate เค•เฅ‡ เคธเคพเคฅ เคตเคฟเคคเคฐเคฟเคค เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคธเฅ‡เคŸ เค•เคฐเฅ‡เค‚ + - local: tflite + title: TFLite เคฎเฅ‡เค‚ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเฅ‡เค‚ \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/accelerate.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/accelerate.md new file mode 100644 index 0000000000000000000000000000000000000000..3d568217a1299b179d28804660bf3a8124bbaa45 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/accelerate.md @@ -0,0 +1,136 @@ + + +# เคตเคฟเคคเคฐเคฟเคค เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ‡ เคธเคพเคฅ ๐Ÿค— Accelerate + +เคœเฅˆเคธเฅ‡-เคœเฅˆเคธเฅ‡ เคฎเฅ‰เคกเคฒ เคฌเคกเคผเฅ‡ เคนเฅ‹เคคเฅ‡ เคนเฅˆเค‚, เคธเคฎเคพเคจเคพเค‚เคคเคฐเคคเคพ เคธเฅ€เคฎเคฟเคค เคนเคพเคฐเฅเคกเคตเฅ‡เคฏเคฐ เคชเคฐ เคฌเคกเคผเฅ‡ เคฎเฅ‰เคกเคฒ เค•เฅ‹ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฟเคค เค•เคฐเคจเฅ‡ เค”เคฐ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ€ เค—เคคเคฟ เค•เฅ‹ เค•เคˆ เค†เคฆเฅ‡เคถเฅ‹เค‚ เค•เฅ‡ เค†เค•เคพเคฐ เคฎเฅ‡เค‚ เคคเฅ‡เคœ เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคเค• เคฐเคฃเคจเฅ€เคคเคฟ เค•เฅ‡ เคฐเฅ‚เคช เคฎเฅ‡เค‚ เค‰เคญเคฐเฅ€ เคนเฅˆเฅค เคนเค—เคฟเค‚เค— เคซเฅ‡เคธ เคฎเฅ‡เค‚, เคนเคฎเคจเฅ‡ เค‰เคชเคฏเฅ‹เค—เค•เคฐเฅเคคเคพเค“เค‚ เค•เฅ‹ เค•เคฟเคธเฅ€ เคญเฅ€ เคชเฅเคฐเค•เคพเคฐ เค•เฅ‡ เคตเคฟเคคเคฐเคฟเคค เคธเฅ‡เคŸเค…เคช เคชเคฐ ๐Ÿค— เคŸเฅเคฐเคพเค‚เคธเคซเคพเคฐเฅเคฎเคฐเฅเคธ เคฎเฅ‰เคกเคฒ เค•เฅ‹ เค†เคธเคพเคจเฅ€ เคธเฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฟเคค เค•เคฐเคจเฅ‡ เคฎเฅ‡เค‚ เคฎเคฆเคฆ เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) เคชเฅเคธเฅเคคเค•เคพเคฒเคฏ เคฌเคจเคพเคฏเคพ เคนเฅˆ, เคšเคพเคนเฅ‡ เคตเคน เคเค• เคฎเคถเฅ€เคจ เคชเคฐ เค•เคˆ GPU เคนเฅ‹เค‚ เคฏเคพ เค•เคˆ เคฎเคถเฅ€เคจเฅ‹เค‚ เคฎเฅ‡เค‚ เค•เคˆ GPUเฅค เค‡เคธ เคŸเฅเคฏเฅ‚เคŸเฅ‹เคฐเคฟเคฏเคฒ เคฎเฅ‡เค‚, เคœเคพเคจเฅ‡เค‚ เค•เคฟ เค…เคชเคจเฅ‡ เคฎเฅ‚เคฒ PyTorch เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคฒเฅ‚เคช เค•เฅ‹ เค•เฅˆเคธเฅ‡ เค…เคจเฅเค•เฅ‚เคฒเคฟเคค เค•เคฟเคฏเคพ เคœเคพเค เคคเคพเค•เคฟ เคตเคฟเคคเคฐเคฟเคค เคตเคพเคคเคพเคตเคฐเคฃ เคฎเฅ‡เค‚ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคธเค•เฅเคทเคฎ เคนเฅ‹ เคธเค•เฅ‡เฅค + +## เคธเฅ‡เคŸเค…เคช + +๐Ÿค— Accelerate เคธเฅเคฅเคพเคชเคฟเคค เค•เคฐเค•เฅ‡ เคถเฅเคฐเฅ‚ เค•เคฐเฅ‡เค‚: + +```bash +pip install accelerate +``` + +เคซเคฟเคฐ เคเค• [`~accelerate.Accelerator`] เค‘เคฌเฅเคœเฅ‡เค•เฅเคŸ เค†เคฏเคพเคค เค•เคฐเฅ‡เค‚ เค”เคฐ เคฌเคจเคพเคเค‚เฅค [`~accelerate.Accelerator`] เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เค†เคชเค•เฅ‡ เคตเคฟเคคเคฐเคฟเคค เคธเฅ‡เคŸเค…เคช เค•เฅ‡ เคชเฅเคฐเค•เคพเคฐ เค•เคพ เคชเคคเคพ เคฒเค—เคพเคเค—เคพ เค”เคฐ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ‡ เคฒเคฟเค เคธเคญเฅ€ เค†เคตเคถเฅเคฏเค• เค˜เคŸเค•เฅ‹เค‚ เค•เฅ‹ เคชเฅเคฐเคพเคฐเค‚เคญ เค•เคฐเฅ‡เค—เคพเฅค เค†เคชเค•เฅ‹ เค…เคชเคจเฅ‡ เคฎเฅ‰เคกเคฒ เค•เฅ‹ เค•เคฟเคธเฅ€ เคกเคฟเคตเคพเค‡เคธ เคชเคฐ เคธเฅเคชเคทเฅเคŸ เคฐเฅ‚เคช เคธเฅ‡ เคฐเค–เคจเฅ‡ เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เคจเคนเฅ€เค‚ เคนเฅˆเฅค + +```py +>>> from accelerate import Accelerator + +>>> accelerator = Accelerator() +``` + +## เคคเฅ‡เคœเฅ€ เคฒเคพเคจเฅ‡ เค•เฅ€ เคคเฅˆเคฏเคพเคฐเฅ€ + +เค…เค—เคฒเคพ เค•เคฆเคฎ เคธเคญเฅ€ เคชเฅเคฐเคพเคธเค‚เค—เคฟเค• เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคตเคธเฅเคคเฅเค“เค‚ เค•เฅ‹ [`~accelerate.Accelerator.prepare`] เคตเคฟเคงเคฟ เคฎเฅ‡เค‚ เคชเคพเคธ เค•เคฐเคจเคพ เคนเฅˆเฅค เค‡เคธเคฎเฅ‡เค‚ เค†เคชเค•เฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค”เคฐ เคฎเฅ‚เคฒเฅเคฏเคพเค‚เค•เคจ DataLoaders, เคเค• เคฎเฅ‰เคกเคฒ เค”เคฐ เคเค• เค‘เคชเฅเคŸเคฟเคฎเคพเค‡เคœเคผเคฐ เคถเคพเคฎเคฟเคฒ เคนเฅˆเค‚: + +```py +>>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( +... train_dataloader, eval_dataloader, model, optimizer +... ) +``` + +## เคฌเฅˆเค•เคตเคฐเฅเคก + +เค…เค‚เคคเคฟเคฎ เคœเฅ‹เคกเคผ เคฏเคน เคนเฅˆ เค•เคฟ เค†เคชเค•เฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคฒเฅ‚เคช เคฎเฅ‡เค‚ เคธเคพเคฎเคพเคจเฅเคฏ `loss.backward()` เค•เฅ‹ ๐Ÿค— Accelerate เค•เฅ‡ [`~accelerate.Accelerator.backward`] เคตเคฟเคงเคฟ เคธเฅ‡ เคฌเคฆเคฒเฅ‡เค‚: + +```py +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... outputs = model(**batch) +... loss = outputs.loss +... accelerator.backward(loss) + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +เคœเฅˆเคธเคพ เค•เคฟ เค†เคช เคจเคฟเคฎเฅเคจเคฒเคฟเค–เคฟเคค เค•เฅ‹เคก เคฎเฅ‡เค‚ เคฆเฅ‡เค– เคธเค•เคคเฅ‡ เคนเฅˆเค‚, เค†เคชเค•เฅ‹ เคตเคฟเคคเคฐเคฟเคค เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคธเค•เฅเคทเคฎ เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เค…เคชเคจเฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคฒเฅ‚เคช เคฎเฅ‡เค‚ เค•เฅ‡เคตเคฒ เคšเคพเคฐ เค…เคคเคฟเคฐเคฟเค•เฅเคค เค•เฅ‹เคก เค•เฅ€ เคชเค‚เค•เฅเคคเคฟเคฏเคพเค เคœเฅ‹เคกเคผเคจเฅ‡ เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เคนเฅˆ! + +```diff ++ from accelerate import Accelerator + from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + ++ accelerator = Accelerator() + + model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) + optimizer = AdamW(model.parameters(), lr=3e-5) + +- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +- model.to(device) + ++ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ++ train_dataloader, eval_dataloader, model, optimizer ++ ) + + num_epochs = 3 + num_training_steps = num_epochs * len(train_dataloader) + lr_scheduler = get_scheduler( + "linear", + optimizer=optimizer, + num_warmup_steps=0, + num_training_steps=num_training_steps + ) + + progress_bar = tqdm(range(num_training_steps)) + + model.train() + for epoch in range(num_epochs): + for batch in train_dataloader: +- batch = {k: v.to(device) for k, v in batch.items()} + outputs = model(**batch) + loss = outputs.loss +- loss.backward() ++ accelerator.backward(loss) + + optimizer.step() + lr_scheduler.step() + optimizer.zero_grad() + progress_bar.update(1) +``` + +## เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ + +เคเค• เคฌเคพเคฐ เคœเคฌ เค†เคชเคจเฅ‡ เคชเฅเคฐเคพเคธเค‚เค—เคฟเค• เค•เฅ‹เคก เค•เฅ€ เคชเค‚เค•เฅเคคเคฟเคฏเคพเค เคœเฅ‹เคกเคผ เคฆเฅ€ เคนเฅˆเค‚, เคคเฅ‹ เค…เคชเคจเฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ‹ เคธเฅเค•เฅเคฐเคฟเคชเฅเคŸ เคฏเคพ เค•เฅ‹เคฒเฅˆเคฌเฅ‹เคฐเฅ‡เคŸเคฐเฅ€ เคœเฅˆเคธเฅ‡ เคจเฅ‹เคŸเคฌเฅเค• เคฎเฅ‡เค‚ เคฒเฅ‰เคจเฅเคš เค•เคฐเฅ‡เค‚เฅค + +### เคธเฅเค•เฅเคฐเคฟเคชเฅเคŸ เค•เฅ‡ เคธเคพเคฅ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ + +เคฏเคฆเคฟ เค†เคช เคธเฅเค•เฅเคฐเคฟเคชเฅเคŸ เคธเฅ‡ เค…เคชเคจเคพ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เคšเคฒเคพ เคฐเคนเฅ‡ เคนเฅˆเค‚, เคคเฅ‹ เคเค• เค•เฅ‰เคจเฅเคซเคผเคฟเค—เคฐเฅ‡เคถเคจ เคซเคผเคพเค‡เคฒ เคฌเคจเคพเคจเฅ‡ เค”เคฐ เคธเคนเฅ‡เคœเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคจเคฟเคฎเฅเคจเคฒเคฟเค–เคฟเคค เค•เคฎเคพเค‚เคก เคšเคฒเคพเคเค: + +```bash +accelerate config +``` + +เคซเคฟเคฐ เค…เคชเคจเฅ‡ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ‹ เค‡เคธ เคคเคฐเคน เคฒเฅ‰เคจเฅเคš เค•เคฐเฅ‡เค‚: + +```bash +accelerate launch train.py +``` + +### เคจเฅ‹เคŸเคฌเฅเค• เค•เฅ‡ เคธเคพเคฅ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ + +๐Ÿค— Accelerate เคเค• เคจเฅ‹เคŸเคฌเฅเค• เคฎเฅ‡เค‚ เคญเฅ€ เคšเคฒ เคธเค•เคคเคพ เคนเฅˆ เคฏเคฆเคฟ เค†เคช Colaboratory เค•เฅ‡ TPU เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเฅ‡ เค•เฅ€ เคฏเฅ‹เคœเคจเคพ เคฌเคจเคพ เคฐเคนเฅ‡ เคนเฅˆเค‚เฅค เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ เค•เฅ‡ เคฒเคฟเค เคœเคฟเคฎเฅเคฎเฅ‡เคฆเคพเคฐ เคธเคญเฅ€ เค•เฅ‹เคก เค•เฅ‹ เคเค• เคซเคผเค‚เค•เฅเคถเคจ เคฎเฅ‡เค‚ เคฒเคชเฅ‡เคŸเฅ‡เค‚, เค”เคฐ เค‡เคธเฅ‡ [`~accelerate.notebook_launcher`] เคฎเฅ‡เค‚ เคชเคพเคธ เค•เคฐเฅ‡เค‚: + +```py +>>> from accelerate import notebook_launcher + +>>> notebook_launcher(training_function) +``` + +๐Ÿค— Accelerate เค”เคฐ เค‡เคธเค•เฅ€ เคธเคฎเฅƒเคฆเฅเคง เคธเฅเคตเคฟเคงเคพเค“เค‚ เค•เฅ‡ เคฌเคพเคฐเฅ‡ เคฎเฅ‡เค‚ เค…เคงเคฟเค• เคœเคพเคจเค•เคพเคฐเฅ€ เค•เฅ‡ เคฒเคฟเค, [เคฆเคธเฅเคคเคพเคตเฅ‡เคœเคผเฅ€เค•เคฐเคฃ](https://huggingface.co/docs/accelerate) เคฆเฅ‡เค–เฅ‡เค‚เฅค diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..d20d5d617a972763eb864992ee63fd32887fdd9a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/pipeline_tutorial.md @@ -0,0 +1,319 @@ + + +# เค…เคจเฅเคฎเคพเคจ เค•เฅ‡ เคฒเคฟเค เคชเคพเค‡เคชเคฒเคพเค‡เคจ + +[`pipeline`] เค•เคฟเคธเฅ€ เคญเฅ€ เคญเคพเคทเคพ, เค•เค‚เคชเฅเคฏเฅ‚เคŸเคฐ เคฆเฅƒเคทเฅเคŸเคฟ, เคญเคพเคทเคฃ เค”เคฐ เคฎเคฒเฅเคŸเฅ€เคฎเฅ‰เคกเคฒ เค•เคพเคฐเฅเคฏเฅ‹เค‚ เคชเคฐ เค…เคจเฅเคฎเคพเคจ เคฒเค—เคพเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค [Hub](https://huggingface.co/models) เคธเฅ‡ เค•เคฟเคธเฅ€ เคญเฅ€ เคฎเฅ‰เคกเคฒ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ เค†เคธเคพเคจ เคฌเคจเคพเคคเคพ เคนเฅˆเฅค เคญเคฒเฅ‡ เคนเฅ€ เค†เคชเค•เฅ‡ เคชเคพเคธ เค•เคฟเคธเฅ€ เคตเคฟเคถเคฟเคทเฅเคŸ เคคเฅŒเคฐ-เคคเคฐเฅ€เค•เฅ‡ เค•เคพ เค…เคจเฅเคญเคต เคจ เคนเฅ‹ เคฏเคพ เค†เคช เคฎเฅ‰เคกเคฒเฅ‹เค‚ เค•เฅ‡ เคชเฅ€เค›เฅ‡ เค…เค‚เคคเคฐเฅเคจเคฟเคนเคฟเคค เค•เฅ‹เคก เคธเฅ‡ เคชเคฐเคฟเคšเคฟเคค เคจ เคนเฅ‹เค‚, เคซเคฟเคฐ เคญเฅ€ เค†เคช [`pipeline`] เค•เฅ‡ เค…เคจเฅเคฎเคพเคจ เค•เฅ‡ เคฒเคฟเค เค‰เคจเค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚! เคฏเคน เคŸเฅเคฏเฅ‚เคŸเฅ‹เคฐเคฟเคฏเคฒ เค†เคชเค•เฅ‹ เคฏเฅ‡ เคธเคฟเค–เคพเคเค—เคพ: + +* เค…เคจเฅเคฎเคพเคจ เค•เฅ‡ เคฒเคฟเค [`pipeline`] เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเฅ‡เค‚เฅค +* เคเค• เคตเคฟเคถเคฟเคทเฅเคŸ เคŸเฅ‹เค•เคจเคจเคพเค‡เคœเคผเคฐ เคฏเคพ เคฎเฅ‰เคกเคฒ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเฅ‡เค‚เฅค +* เค‘เคกเคฟเคฏเฅ‹, เคตเคฟเคœเคผเคจ เค”เคฐ เคฎเคฒเฅเคŸเฅ€เคฎเฅ‰เคกเคฒ เค•เคพเคฐเฅเคฏเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค [`pipeline`] เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเฅ‡เค‚เฅค + + + +เคธเคฎเคฐเฅเคฅเคฟเคค เค•เคพเคฐเฅเคฏเฅ‹เค‚ เค”เคฐ เค‰เคชเคฒเคฌเฅเคง เคฎเคพเคชเคฆเค‚เคกเฅ‹เค‚ เค•เฅ€ เคชเฅ‚เคฐเฅ€ เคธเฅ‚เคšเฅ€ เค•เฅ‡ เคฒเคฟเค [`pipeline`] เคฆเคธเฅเคคเคพเคตเฅ‡เคœเคผ เคชเคฐ เคเค• เคจเคœเคผเคฐ เคกเคพเคฒเฅ‡เค‚เฅค + + + +## เคชเคพเค‡เคชเคฒเคพเค‡เคจ เค•เคพ เค‰เคชเคฏเฅ‹เค— + +เคœเคฌเค•เคฟ เคชเฅเคฐเคคเฅเคฏเฅ‡เค• เค•เคพเคฐเฅเคฏ เคฎเฅ‡เค‚ เคเค• เคธเค‚เคฌเคฆเฅเคง [`pipeline`] เคนเฅ‹เคคเคพ เคนเฅˆ, เคธเคพเคฎเคพเคจเฅเคฏ [`pipeline`] เค…เคฎเฅ‚เคฐเฅเคค เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ เค†เคธเคพเคจ เคนเฅ‹เคคเคพ เคนเฅˆ เคœเคฟเคธเคฎเฅ‡เค‚ เคถเคพเคฎเคฟเคฒ เคนเฅ‹เคคเคพ เคนเฅˆ +เคธเคญเฅ€ เค•เคพเคฐเฅเคฏ-เคตเคฟเคถเคฟเคทเฅเคŸ เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‡เค‚เฅค [`pipeline`] เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เคเค• เคกเคฟเคซเคผเฅ‰เคฒเฅเคŸ เคฎเฅ‰เคกเคฒ เค”เคฐ เคธเค•เฅเคทเคฎ เคชเฅเคฐเฅ€เคชเฅเคฐเฅ‹เคธเฅ‡เคธเคฟเค‚เค— เค•เฅเคฒเคพเคธ เคฒเฅ‹เคก เค•เคฐเคคเคพ เคนเฅˆ +เค†เคชเค•เฅ‡ เค•เคพเคฐเฅเคฏ เค•เฅ‡ เคฒเคฟเค เค…เคจเฅเคฎเคพเคจ เค•เคพ. เค†เค‡เค เคธเฅเคตเคšเคพเคฒเคฟเคค เคตเคพเค•เฅ เคชเคนเคšเคพเคจ (เคเคเคธเค†เคฐ) เค•เฅ‡ เคฒเคฟเค [`pipeline`] เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเฅ‡ เค•เคพ เค‰เคฆเคพเคนเคฐเคฃ เคฒเฅ‡เค‚, เคฏเคพ +เคตเคพเค•เฅ-เคธเฅ‡-เคชเคพเค . + + +1. เคเค• [`pipeline`] เคฌเคจเคพเค•เคฐ เคชเฅเคฐเคพเคฐเค‚เคญ เค•เคฐเฅ‡เค‚ เค”เคฐ เค…เคจเฅเคฎเคพเคจ เค•เคพเคฐเฅเคฏ เคจเคฟเคฐเฅเคฆเคฟเคทเฅเคŸ เค•เคฐเฅ‡เค‚: + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition") +``` + +2. เค…เคชเคจเคพ เค‡เคจเคชเฅเคŸ [`pipeline`] เคชเคฐ เคญเฅ‡เคœเฅ‡เค‚เฅค เคตเคพเค•เฅ เคชเคนเคšเคพเคจ เค•เฅ‡ เคฎเคพเคฎเคฒเฅ‡ เคฎเฅ‡เค‚, เคฏเคน เคเค• เค‘เคกเคฟเคฏเฅ‹ เค‡เคจเคชเฅเคŸ เคซเคผเคพเค‡เคฒ เคนเฅˆ: + +```py +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'} +``` + +เค•เฅเคฏเคพ เคตเคน เคชเคฐเคฟเคฃเคพเคฎ เคจเคนเฅ€เค‚ เคœเฅ‹ เค†เคชเค•เฅ‡ เคฎเคจ เคฎเฅ‡เค‚ เคฅเคพ? เค•เฅเค› [เคธเคฌเคธเฅ‡ เค…เคงเคฟเค• เคกเคพเค‰เคจเคฒเฅ‹เคก เค•เคฟเค เค—เค เคธเฅเคตเคšเคพเคฒเคฟเคค เคตเคพเค•เฅ เคชเคนเคšเคพเคจ เคฎเฅ‰เคกเคฒ](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=trending) เคฆเฅ‡เค–เฅ‡เค‚ +เคฏเคน เคฆเฅ‡เค–เคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคนเคฌ เคชเคฐ เคœเคพเคเค‚ เค•เคฟ เค•เฅเคฏเคพ เค†เคชเค•เฅ‹ เคฌเฅ‡เคนเคคเคฐ เคŸเฅเคฐเคพเค‚เคธเฅเค•เฅเคฐเคฟเคชเฅเคถเคจ เคฎเคฟเคฒ เคธเค•เคคเคพ เคนเฅˆเฅค + +เค†เค‡เค OpenAI เคธเฅ‡ [เคตเฅเคนเคฟเคธเฅเคชเคฐ เคฒเคพเคฐเฅเคœ-v2](https://huggingface.co/openai/whisper-large) เคฎเฅ‰เคกเคฒ เค†เคœเคผเคฎเคพเคเค‚เฅค เคตเฅเคนเคฟเคธเฅเคชเคฐ เคœเคพเคฐเฅ€ เค•เคฟเคฏเคพ เค—เคฏเคพ +Wav2Vec2 เค•เฅ€ เคคเฅเคฒเคจเคพ เคฎเฅ‡เค‚ 2 เคธเคพเคฒ เคฌเคพเคฆ, เค”เคฐ เคฒเค—เคญเค— 10 เค—เฅเคจเคพ เค…เคงเคฟเค• เคกเฅ‡เคŸเคพ เคชเคฐ เคชเฅเคฐเคถเคฟเค•เฅเคทเคฟเคค เค•เคฟเคฏเคพ เค—เคฏเคพ เคฅเคพเฅค เค‡เคธ เคชเฅเคฐเค•เคพเคฐ, เคฏเคน เค…เคงเคฟเค•เคพเค‚เคถ เคกเคพเค‰เคจเคธเฅเคŸเฅเคฐเฅ€เคฎ เคชเคฐ Wav2Vec2 เค•เฅ‹ เคฎเคพเคค เคฆเฅ‡เคคเคพ เคนเฅˆ +เคฌเฅ‡เค‚เคšเคฎเคพเคฐเฅเค•. เค‡เคธเคฎเฅ‡เค‚ เคตเคฟเคฐเคพเคฎ เคšเคฟเคนเฅเคจ เค”เคฐ เค†เคตเคฐเคฃ เค•เฅ€ เคญเคตเคฟเคทเฅเคฏเคตเคพเคฃเฅ€ เค•เคฐเคจเฅ‡ เค•เคพ เค…เคคเคฟเคฐเคฟเค•เฅเคค เคฒเคพเคญ เคญเฅ€ เคนเฅˆ, เคœเคฟเคจเคฎเฅ‡เค‚ เคธเฅ‡ เค•เฅ‹เคˆ เคญเฅ€ เคธเค‚เคญเคต เคจเคนเฅ€เค‚ เคนเฅˆ +Wav2Vec2. + +เค†เค‡เค เค‡เคธเฅ‡ เคฏเคนเคพเค‚ เค†เคœเคผเคฎเคพเค•เคฐ เคฆเฅ‡เค–เฅ‡เค‚ เค•เคฟ เคฏเคน เค•เฅˆเคธเคพ เคชเฅเคฐเคฆเคฐเฅเคถเคจ เค•เคฐเคคเคพ เคนเฅˆ: + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +เค…เคฌ เคฏเคน เคชเคฐเคฟเคฃเคพเคฎ เค…เคงเคฟเค• เคธเคŸเฅ€เค• เคฆเคฟเค–เคคเคพ เคนเฅˆ! Wav2Vec2 เคฌเคจเคพเคฎ เคตเฅเคนเคฟเคธเฅเคชเคฐ เคชเคฐ เค—เคนเคจ เคคเฅเคฒเคจเคพ เค•เฅ‡ เคฒเคฟเค, [เค‘เคกเคฟเคฏเฅ‹ เคŸเฅเคฐเคพเค‚เคธเคซเฅ‰เคฐเฅเคฎเคฐเฅเคธ เค•เฅ‹เคฐเฅเคธ](https://huggingface.co/learn/audio-course/chapter5/asr_models) เคฆเฅ‡เค–เฅ‡เค‚เฅค +เคนเคฎ เคตเคพเคธเฅเคคเคต เคฎเฅ‡เค‚ เค†เคชเค•เฅ‹ เคตเคฟเคญเคฟเคจเฅเคจ เคญเคพเคทเคพเค“เค‚ เคฎเฅ‡เค‚ เคฎเฅ‰เคกเคฒ, เค†เคชเค•เฅ‡ เค•เฅเคทเฅ‡เคคเฅเคฐ เคฎเฅ‡เค‚ เคตเคฟเคถเฅ‡เคทเฅ€เค•เฅƒเคค เคฎเฅ‰เคกเคฒ เค”เคฐ เคฌเคนเฅเคค เค•เฅเค› เค•เฅ‡ เคฒเคฟเค เคนเคฌ เค•เฅ€ เคœเคพเค‚เคš เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคชเฅเคฐเฅ‹เคคเฅเคธเคพเคนเคฟเคค เค•เคฐเคคเฅ‡ เคนเฅˆเค‚เฅค +เค†เคช เคนเคฌ เคชเคฐ เคธเฅ€เคงเฅ‡ เค…เคชเคจเฅ‡ เคฌเฅเคฐเคพเค‰เคœเคผเคฐ เคธเฅ‡ เคฎเฅ‰เคกเคฒ เคชเคฐเคฟเคฃเคพเคฎเฅ‹เค‚ เค•เฅ€ เคœเคพเค‚เคš เค”เคฐ เคคเฅเคฒเคจเคพ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚ เค•เคฟ เคฏเคน เคซเคฟเคŸ เคฌเฅˆเค เคคเคพ เคนเฅˆ เคฏเคพ เคจเคนเฅ€เค‚ +เค…เคจเฅเคฏ เคฎเคพเคฎเคฒเฅ‹เค‚ เค•เฅ€ เคคเฅเคฒเคจเคพ เคฎเฅ‡เค‚ เค•เฅ‹เคจเฅ‡ เค•เฅ‡ เคฎเคพเคฎเคฒเฅ‹เค‚ เค•เฅ‹ เคฌเฅ‡เคนเคคเคฐ เคขเค‚เค— เคธเฅ‡ เคธเค‚เคญเคพเคฒเคคเคพ เคนเฅˆเฅค +เค”เคฐ เคฏเคฆเคฟ เค†เคชเค•เฅ‹ เค…เคชเคจเฅ‡ เค‰เคชเคฏเฅ‹เค— เค•เฅ‡ เคฎเคพเคฎเคฒเฅ‡ เค•เฅ‡ เคฒเคฟเค เค•เฅ‹เคˆ เคฎเฅ‰เคกเคฒ เคจเคนเฅ€เค‚ เคฎเคฟเคฒเคคเคพ เคนเฅˆ, เคคเฅ‹ เค†เคช เคนเคฎเฅ‡เคถเคพ เค…เคชเคจเคพ เค–เฅเคฆ เค•เคพ [เคชเฅเคฐเคถเคฟเค•เฅเคทเคฃ](training) เคถเฅเคฐเฅ‚ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚! + +เคฏเคฆเคฟ เค†เคชเค•เฅ‡ เคชเคพเคธ เค•เคˆ เค‡เคจเคชเฅเคŸ เคนเฅˆเค‚, เคคเฅ‹ เค†เคช เค…เคชเคจเฅ‡ เค‡เคจเคชเฅเคŸ เค•เฅ‹ เคเค• เคธเฅ‚เคšเฅ€ เค•เฅ‡ เคฐเฅ‚เคช เคฎเฅ‡เค‚ เคชเคพเคธ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚: + +```py +transcriber( + [ + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", + ] +) +``` + +เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‡เค‚ เคชเฅเคฐเคฏเฅ‹เค— เค•เฅ‡ เคฒเคฟเค เคฌเคนเฅเคค เค…เคšเฅเค›เฅ€ เคนเฅˆเค‚ เค•เฅเคฏเฅ‹เค‚เค•เคฟ เคเค• เคฎเฅ‰เคกเคฒ เคธเฅ‡ เคฆเฅ‚เคธเคฐเฅ‡ เคฎเฅ‰เคกเคฒ เคชเคฐ เคธเฅเคตเคฟเคš เค•เคฐเคจเคพ เคฎเคพเคฎเฅ‚เคฒเฅ€ เค•เคพเคฎ เคนเฅˆ; เคนเคพเคฒเคพเคเค•เคฟ, เคชเฅเคฐเคฏเฅ‹เค— เค•เฅ€ เคคเฅเคฒเคจเคพ เคฎเฅ‡เค‚ เคฌเคกเคผเฅ‡ เค•เคพเคฐเฅเคฏเคญเคพเคฐ เค•เฅ‡ เคฒเคฟเค เค‰เคจเฅเคนเฅ‡เค‚ เค…เคจเฅเค•เฅ‚เคฒเคฟเคค เค•เคฐเคจเฅ‡ เค•เฅ‡ เค•เฅเค› เคคเคฐเฅ€เค•เฅ‡ เคนเฅˆเค‚เฅค เคธเค‚เคชเฅ‚เคฐเฅเคฃ เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เคชเคฐ เคชเฅเคจเคฐเคพเคตเฅƒเคคเฅเคคเคฟ เค•เคฐเคจเฅ‡ เคฏเคพ เคตเฅ‡เคฌเคธเคฐเฅเคตเคฐ เคฎเฅ‡เค‚ เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฌเคพเคฐเฅ‡ เคฎเฅ‡เค‚ เคจเคฟเคฎเฅเคจเคฒเคฟเค–เคฟเคค เคฎเคพเคฐเฅเค—เคฆเคฐเฅเคถเคฟเค•เคพเคเค เคฆเฅ‡เค–เฅ‡เค‚: +เคฆเคธเฅเคคเคพเคตเฅ‡เคœเคผเฅ‹เค‚ เคฎเฅ‡เค‚ เคธเฅ‡: +* [เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เคชเคฐ เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ](#using-pipelines-on-a-dataset) +* [เคตเฅ‡เคฌเคธเคฐเฅเคตเคฐ เค•เฅ‡ เคฒเคฟเค เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ](./pipeline_webserver) + +## เคชเฅเคฐเคพเคšเคฒ + +[`pipeline`] เค•เคˆ เคฎเคพเคชเคฆเค‚เคกเฅ‹เค‚ เค•เคพ เคธเคฎเคฐเฅเคฅเคจ เค•เคฐเคคเคพ เคนเฅˆ; เค•เฅเค› เค•เคพเคฐเฅเคฏ เคตเคฟเคถเคฟเคทเฅเคŸ เคนเฅˆเค‚, เค”เคฐ เค•เฅเค› เคธเคญเฅ€ เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค เคธเคพเคฎเคพเคจเฅเคฏ เคนเฅˆเค‚เฅค +เคธเคพเคฎเคพเคจเฅเคฏ เคคเฅŒเคฐ เคชเคฐ, เค†เคช เค…เคชเคจเฅ€ เค‡เคšเฅเค›เคพเคจเฅเคธเคพเคฐ เค•เคนเฅ€เค‚ เคญเฅ€ เคชเฅˆเคฐเคพเคฎเฅ€เคŸเคฐ เคจเคฟเคฐเฅเคฆเคฟเคทเฅเคŸ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", my_parameter=1) + +out = transcriber(...) # This will use `my_parameter=1`. +out = transcriber(..., my_parameter=2) # This will override and use `my_parameter=2`. +out = transcriber(...) # This will go back to using `my_parameter=1`. +``` + +เค†เค‡เค 3 เคฎเคนเคคเฅเคตเคชเฅ‚เคฐเฅเคฃ เคฌเคพเคคเฅ‹เค‚ เคชเคฐ เค—เฅŒเคฐ เค•เคฐเฅ‡เค‚: + +### เค‰เคชเค•เคฐเคฃ + +เคฏเคฆเคฟ เค†เคช `device=0` เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคคเฅ‡ เคนเฅˆเค‚, เคคเฅ‹ เคชเคพเค‡เคชเคฒเคพเค‡เคจ เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เคฎเฅ‰เคกเคฒ เค•เฅ‹ เคจเคฟเคฐเฅเคฆเคฟเคทเฅเคŸ เคกเคฟเคตเคพเค‡เคธ เคชเคฐ เคกเคพเคฒ เคฆเฅ‡เคคเฅ€ เคนเฅˆเฅค +เคฏเคน เค‡เคธ เคชเคฐ เคงเฅเคฏเคพเคจ เคฆเคฟเค เคฌเคฟเคจเคพ เค•เคพเคฎ เค•เคฐเฅ‡เค—เคพ เค•เคฟ เค†เคช PyTorch เคฏเคพ Tensorflow เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐ เคฐเคนเฅ‡ เคนเฅˆเค‚ เคฏเคพ เคจเคนเฅ€เค‚เฅค + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0) +``` + +เคฏเคฆเคฟ เคฎเฅ‰เคกเคฒ เคเค•เคฒ GPU เค•เฅ‡ เคฒเคฟเค เคฌเคนเฅเคค เคฌเคกเคผเคพ เคนเฅˆ เค”เคฐ เค†เคช PyTorch เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐ เคฐเคนเฅ‡ เคนเฅˆเค‚, เคคเฅ‹ เค†เคช `device_map="auto"` เค•เฅ‹ เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เคธเฅ‡เคŸ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚ +เคจเคฟเคฐเฅเคงเคพเคฐเคฟเคค เค•เคฐเฅ‡เค‚ เค•เคฟ เคฎเฅ‰เคกเคฒ เคตเคœเคผเคจ เค•เฅ‹ เค•เฅˆเคธเฅ‡ เคฒเฅ‹เคก เค”เคฐ เคธเค‚เค—เฅเคฐเคนเฅ€เคค เค•เคฟเคฏเคพ เคœเคพเคเฅค `device_map` เคคเคฐเฅเค• เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค ๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เคนเฅ‹เคคเฅ€ เคนเฅˆ +เคชเฅˆเค•เฅ‡เคŸ: + +```bash +pip install --upgrade accelerate +``` + +เคจเคฟเคฎเฅเคจเคฒเคฟเค–เคฟเคค เค•เฅ‹เคก เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เคธเคญเฅ€ เคกเคฟเคตเคพเค‡เคธเฅ‹เค‚ เคฎเฅ‡เค‚ เคฎเฅ‰เคกเคฒ เคญเคพเคฐ เค•เฅ‹ เคฒเฅ‹เคก เค”เคฐ เคธเค‚เค—เฅเคฐเคนเฅ€เคค เค•เคฐเคคเคพ เคนเฅˆ: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device_map="auto") +``` + +เคงเฅเคฏเคพเคจ เคฆเฅ‡เค‚ เค•เคฟ เคฏเคฆเคฟ `device_map='auto'` เคชเคพเคฐเคฟเคค เคนเฅ‹ เค—เคฏเคพ เคนเฅˆ, เคคเฅ‹ เค…เคชเคจเฅ€ `pipeline` เค•เฅ‹ เคšเคพเคฒเฅ‚ เค•เคฐเคคเฅ‡ เคธเคฎเคฏ `device=device` เคคเคฐเฅเค• เคœเฅ‹เคกเคผเคจเฅ‡ เค•เฅ€ เค•เฅ‹เคˆ เค†เคตเคถเฅเคฏเค•เคคเคพ เคจเคนเฅ€เค‚ เคนเฅˆ เค•เฅเคฏเฅ‹เค‚เค•เคฟ เค†เคชเค•เฅ‹ เค•เฅเค› เค…เคชเฅเคฐเคคเฅเคฏเคพเคถเคฟเคค เคตเฅเคฏเคตเคนเคพเคฐ เค•เคพ เคธเคพเคฎเคจเคพ เค•เคฐเคจเคพ เคชเคกเคผ เคธเค•เคคเคพ เคนเฅˆ! + +### เคฌเฅˆเคš เค•เคพ เค†เค•เคพเคฐ + +เคกเคฟเคซเคผเฅ‰เคฒเฅเคŸ เคฐเฅ‚เคช เคธเฅ‡, เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‡เค‚ [เคฏเคนเคพเค‚](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching) เคตเคฟเคธเฅเคคเคพเคฐ เคธเฅ‡ เคฌเคคเคพเค เค—เค เค•เคพเคฐเคฃเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค เคฌเฅˆเคš เค…เคจเฅเคฎเคพเคจ เคจเคนเฅ€เค‚ เคฒเค—เคพเคเค‚เค—เฅ€เฅค เค‡เคธเค•เคพ เค•เคพเคฐเคฃ เคฏเคน เคนเฅˆ เค•เคฟ เคฌเฅˆเคšเคฟเค‚เค— เค†เคตเคถเฅเคฏเค• เคฐเฅ‚เคช เคธเฅ‡ เคคเฅ‡เคœเคผ เคจเคนเฅ€เค‚ เคนเฅˆ, เค”เคฐ เคตเคพเคธเฅเคคเคต เคฎเฅ‡เค‚ เค•เฅเค› เคฎเคพเคฎเคฒเฅ‹เค‚ เคฎเฅ‡เค‚ เค•เคพเคซเฅ€ เคงเฅ€เคฎเฅ€ เคนเฅ‹ เคธเค•เคคเฅ€ เคนเฅˆเฅค + +เคฒเฅ‡เค•เคฟเคจ เค…เค—เคฐ เคฏเคน เค†เคชเค•เฅ‡ เค‰เคชเคฏเฅ‹เค— เค•เฅ‡ เคฎเคพเคฎเคฒเฅ‡ เคฎเฅ‡เค‚ เค•เคพเคฎ เค•เคฐเคคเคพ เคนเฅˆ, เคคเฅ‹ เค†เคช เค‡เคธเค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚: + +```py +transcriber = pipeline(model="openai/whisper-large-v2", device=0, batch_size=2) +audio_filenames = [f"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/{i}.flac" for i in range(1, 5)] +texts = transcriber(audio_filenames) +``` + +เคฏเคน เคชเฅเคฐเคฆเคพเคจ เค•เฅ€ เค—เคˆ 4 เค‘เคกเคฟเคฏเฅ‹ เคซเคพเค‡เคฒเฅ‹เค‚ เคชเคฐ เคชเคพเค‡เคชเคฒเคพเค‡เคจ เคšเคฒเคพเคคเคพ เคนเฅˆ, เคฒเฅ‡เค•เคฟเคจ เคฏเคน เค‰เคจเฅเคนเฅ‡เค‚ 2 เค•เฅ‡ เคฌเฅˆเคš เคฎเฅ‡เค‚ เคชเคพเคธ เค•เคฐเฅ‡เค—เคพ +เค†เคชเคธเฅ‡ เค•เคฟเคธเฅ€ เค”เคฐ เค•เฅ‹เคก เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เค•เฅ‡ เคฌเคฟเคจเคพ เคฎเฅ‰เคกเคฒ (เคœเฅ‹ เคเค• เคœเฅ€เคชเฅ€เคฏเฅ‚ เคชเคฐ เคนเฅˆ, เคœเคนเคพเค‚ เคฌเฅˆเคšเคฟเค‚เค— เคธเฅ‡ เคฎเคฆเคฆ เคฎเคฟเคฒเคจเฅ‡ เค•เฅ€ เค…เคงเคฟเค• เคธเค‚เคญเคพเคตเคจเคพ เคนเฅˆ) เคชเคฐ เคœเคพเคเค‚เฅค +เค†เค‰เคŸเคชเฅเคŸ เคนเคฎเฅ‡เคถเคพ เค‰เคธเฅ€ เคธเฅ‡ เคฎเฅ‡เคฒ เค–เคพเคจเคพ เคšเคพเคนเคฟเค เคœเฅ‹ เค†เคชเค•เฅ‹ เคฌเฅˆเคšเคฟเค‚เค— เค•เฅ‡ เคฌเคฟเคจเคพ เคชเฅเคฐเคพเคชเฅเคค เคนเฅเค† เคนเฅ‹เค—เคพเฅค เค‡เคธเค•เคพ เค‰เคฆเฅเคฆเฅ‡เคถเฅเคฏ เค•เฅ‡เคตเคฒ เคชเคพเค‡เคชเคฒเคพเค‡เคจ เคธเฅ‡ เค…เคงเคฟเค• เค—เคคเคฟ เคชเฅเคฐเคพเคชเฅเคค เค•เคฐเคจเฅ‡ เคฎเฅ‡เค‚ เค†เคชเค•เฅ€ เคธเคนเคพเคฏเคคเคพ เค•เคฐเคจเคพ เคนเฅˆเฅค + +เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‡เค‚ เคฌเฅˆเคšเคฟเค‚เค— เค•เฅ€ เค•เฅเค› เคœเคŸเคฟเคฒเคคเคพเค“เค‚ เค•เฅ‹ เคญเฅ€ เค•เคฎ เค•เคฐ เคธเค•เคคเฅ€ เคนเฅˆเค‚ เค•เฅเคฏเฅ‹เค‚เค•เคฟ, เค•เฅเค› เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค, เคเค• เคเค•เคฒ เค†เค‡เคŸเคฎ (เคœเฅˆเคธเฅ‡ เคเค• เคฒเค‚เคฌเฅ€ เค‘เคกเคฟเคฏเฅ‹ เคซเคผเคพเค‡เคฒ) เค•เฅ‹ เคเค• เคฎเฅ‰เคกเคฒ เคฆเฅเคตเคพเคฐเคพ เคธเค‚เคธเคพเคงเคฟเคค เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เค•เคˆ เคญเคพเค—เฅ‹เค‚ เคฎเฅ‡เค‚ เคตเคฟเคญเคพเคœเคฟเคค เค•เคฐเคจเฅ‡ เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เคนเฅ‹เคคเฅ€ เคนเฅˆเฅค เคชเคพเค‡เคชเคฒเคพเค‡เคจ เค†เคชเค•เฅ‡ เคฒเคฟเค เคฏเคน [*chunk batching*](./main_classes/pipelines#pipeline-chunk-batching) เค•เคฐเคคเฅ€ เคนเฅˆเฅค + +### เค•เคพเคฐเฅเคฏ เคตเคฟเคถเคฟเคทเฅเคŸ เคชเฅเคฐเคพเคšเคฒ + +เคธเคญเฅ€ เค•เคพเคฐเฅเคฏ เค•เคพเคฐเฅเคฏ เคตเคฟเคถเคฟเคทเฅเคŸ เคชเฅเคฐเคพเคšเคฒ เคชเฅเคฐเคฆเคพเคจ เค•เคฐเคคเฅ‡ เคนเฅˆเค‚ เคœเฅ‹ เค†เคชเค•เฅ‹ เค…เคชเคจเคพ เค•เคพเคฎ เคชเฅ‚เคฐเคพ เค•เคฐเคจเฅ‡ เคฎเฅ‡เค‚ เคฎเคฆเคฆ เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เค…เคคเคฟเคฐเคฟเค•เฅเคค เคฒเคšเฅ€เคฒเฅ‡เคชเคจ เค”เคฐ เคตเคฟเค•เคฒเฅเคชเฅ‹เค‚ เค•เฅ€ เค…เคจเฅเคฎเคคเคฟ เคฆเฅ‡เคคเฅ‡ เคนเฅˆเค‚เฅค +เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, [`transformers.AutomaticSpeechRecognitionPipeline.__call__`] เคตเคฟเคงเคฟ เคฎเฅ‡เค‚ เคเค• `return_timestamps` เคชเฅเคฐเคพเคšเคฒ เคนเฅˆ เคœเฅ‹ เคตเฅ€เคกเคฟเคฏเฅ‹ เค‰เคชเคถเฅ€เคฐเฅเคทเค• เค•เฅ‡ เคฒเคฟเค เค†เคถเคพเคœเคจเค• เคฒเค—เคคเคพ เคนเฅˆ: + + +```py +>>> transcriber = pipeline(model="openai/whisper-large-v2", return_timestamps=True) +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.', 'chunks': [{'timestamp': (0.0, 11.88), 'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its'}, {'timestamp': (11.88, 12.38), 'text': ' creed.'}]} +``` + +เคœเฅˆเคธเคพ เค•เคฟ เค†เคช เคฆเฅ‡เค– เคธเค•เคคเฅ‡ เคนเฅˆเค‚, เคฎเฅ‰เคกเคฒ เคจเฅ‡ เคชเคพเค  เค•เคพ เค…เคจเฅเคฎเคพเคจ เคฒเค—เคพเคฏเคพ เค”เคฐ **when** เคตเคฟเคญเคฟเคจเฅเคจ เคตเคพเค•เฅเคฏเฅ‹เค‚ เค•เคพ เค‰เคšเฅเคšเคพเคฐเคฃ เค•เคฟเคฏเคพ เค—เคฏเคพ เคคเฅ‹ เค†เค‰เคŸเคชเฅเคŸ เคญเฅ€ เคฆเคฟเคฏเคพเฅค + +เคชเฅเคฐเคคเฅเคฏเฅ‡เค• เค•เคพเคฐเฅเคฏ เค•เฅ‡ เคฒเคฟเค เค•เคˆ เคชเฅเคฐเคพเคšเคฒ เค‰เคชเคฒเคฌเฅเคง เคนเฅˆเค‚, เค‡เคธเคฒเคฟเค เคฏเคน เคฆเฅ‡เค–เคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เค•เคฟ เค†เคช เค•เคฟเคธเค•เฅ‡ เคธเคพเคฅ เค›เฅ‡เคกเคผเค›เคพเคกเคผ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚, เคชเฅเคฐเคคเฅเคฏเฅ‡เค• เค•เคพเคฐเฅเคฏ เค•เคพ API เคธเค‚เคฆเคฐเฅเคญ เคฆเฅ‡เค–เฅ‡เค‚! +เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, [`~transformers.AutomaticSpeechRecognitionPipeline`] เคฎเฅ‡เค‚ เคเค• `chunk_length_s` เคชเฅเคฐเคพเคšเคฒ เคนเฅˆ เคœเฅ‹ เคธเคนเคพเคฏเค• เคนเฅˆ +เคตเคพเคธเฅเคคเคต เคฎเฅ‡เค‚ เคฒเค‚เคฌเฅ€ เค‘เคกเคฟเคฏเฅ‹ เคซเคผเคพเค‡เคฒเฅ‹เค‚ เคชเคฐ เค•เคพเคฎ เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค (เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, เคธเค‚เคชเฅ‚เคฐเฅเคฃ เคซเคฟเคฒเฅเคฎเฅ‹เค‚ เคฏเคพ เค˜เค‚เคŸเฅ‡-เคฒเค‚เคฌเฅ‡ เคตเฅ€เคกเคฟเคฏเฅ‹ เค•เฅ‹ เค‰เคชเคถเฅ€เคฐเฅเคทเค• เคฆเฅ‡เคจเคพ) เคœเฅ‹ เค†เคฎเคคเฅŒเคฐ เคชเคฐ เคเค• เคฎเฅ‰เคกเคฒ เคนเฅ‹เคคเคพ เคนเฅˆ +เค…เคชเคจเฅ‡ เค†เคช เคธเค‚เคญเคพเคฒ เคจเคนเฅ€เค‚ เคธเค•เคคเคพ: + +```python +>>> transcriber = pipeline(model="openai/whisper-large-v2", chunk_length_s=30, return_timestamps=True) +>>> transcriber("https://huggingface.co/datasets/sanchit-gandhi/librispeech_long/resolve/main/audio.wav") +{'text': " Chapter 16. I might have told you of the beginning of this liaison in a few lines, but I wanted you to see every step by which we came. I, too, agree to whatever Marguerite wished, Marguerite to be unable to live apart from me. It was the day after the evening... +``` + +เคฏเคฆเคฟ เค†เคชเค•เฅ‹ เค•เฅ‹เคˆ เคเคธเคพ เคชเฅˆเคฐเคพเคฎเฅ€เคŸเคฐ เคจเคนเฅ€เค‚ เคฎเคฟเคฒ เคฐเคนเคพ เคนเฅˆ เคœเฅ‹ เคตเคพเคธเฅเคคเคต เคฎเฅ‡เค‚ เค†เคชเค•เฅ€ เคฎเคฆเคฆ เค•เคฐเฅ‡เค—เคพ, เคคเฅ‹ เคฌเฅ‡เคเคฟเคเค• [เค…เคจเฅเคฐเฅ‹เคง เค•เคฐเฅ‡เค‚](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml)! + + +## เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เคชเคฐ เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ + +เคชเคพเค‡เคชเคฒเคพเค‡เคจ เคฌเคกเคผเฅ‡ เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เคชเคฐ เคญเฅ€ เค…เคจเฅเคฎเคพเคจ เคšเคฒเคพ เคธเค•เคคเฅ€ เคนเฅˆเฅค เคเคธเคพ เค•เคฐเคจเฅ‡ เค•เคพ เคธเคฌเคธเฅ‡ เค†เคธเคพเคจ เคคเคฐเฅ€เค•เคพ เคนเคฎ เคเค• เคชเฅเคจเคฐเคพเคตเคฐเฅเคคเค• เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเฅ‡ เค•เฅ€ เคธเคฒเคพเคน เคฆเฅ‡เคคเฅ‡ เคนเฅˆเค‚: + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + + +pipe = pipeline(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipe(data()): + generated_characters += len(out[0]["generated_text"]) +``` + +เคชเฅเคจเคฐเคพเคตเคฐเฅเคคเค• `data()` เคชเฅเคฐเคคเฅเคฏเฅ‡เค• เคชเคฐเคฟเคฃเคพเคฎ เค”เคฐ เคชเคพเค‡เคชเคฒเคพเค‡เคจ เคธเฅเคตเคšเคพเคฒเคฟเคค เคฐเฅ‚เคช เคธเฅ‡ เค‰เคคเฅเคชเคจเฅเคจ เค•เคฐเคคเคพ เคนเฅˆ +เคชเคนเคšเคพเคจเคคเคพ เคนเฅˆ เค•เคฟ เค‡เคจเคชเฅเคŸ เคชเฅเคจเคฐเคพเคตเคฐเฅเคคเคจเฅ€เคฏ เคนเฅˆ เค”เคฐ เคกเฅ‡เคŸเคพ เคชเฅเคฐเคพเคชเฅเคค เค•เคฐเคจเคพ เคถเฅเคฐเฅ‚ เค•เคฐ เคฆเฅ‡เค—เคพ +เคฏเคน เค‡เคธเฅ‡ GPU เคชเคฐ เคชเฅเคฐเฅ‹เคธเฅ‡เคธ เค•เคฐเคจเคพ เคœเคพเคฐเฅ€ เคฐเค–เคคเคพ เคนเฅˆ (เคฏเคน เคนเฅเคก เค•เฅ‡ เคคเคนเคค [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader) เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคคเคพ เคนเฅˆ)เฅค +เคฏเคน เคฎเคนเคคเฅเคตเคชเฅ‚เคฐเฅเคฃ เคนเฅˆ เค•เฅเคฏเฅ‹เค‚เค•เคฟ เค†เคชเค•เฅ‹ เคธเค‚เคชเฅ‚เคฐเฅเคฃ เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เค•เฅ‡ เคฒเคฟเค เคฎเฅ‡เคฎเฅ‹เคฐเฅ€ เค†เคตเค‚เคŸเคฟเคค เค•เคฐเคจเฅ‡ เค•เฅ€ เค†เคตเคถเฅเคฏเค•เคคเคพ เคจเคนเฅ€เค‚ เคนเฅˆ +เค”เคฐ เค†เคช เคœเคฟเคคเคจเฅ€ เคœเคฒเฅเคฆเฅ€ เคนเฅ‹ เคธเค•เฅ‡ GPU เค•เฅ‹ เคซเฅ€เคก เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚เฅค + +เคšเฅ‚เค‚เค•เคฟ เคฌเฅˆเคšเคฟเค‚เค— เคธเฅ‡ เคšเฅ€เคœเคผเฅ‡เค‚ เคคเฅ‡เคœเคผ เคนเฅ‹ เคธเค•เคคเฅ€ เคนเฅˆเค‚, เค‡เคธเคฒเคฟเค เคฏเคนเคพเค‚ `batch_size` เคชเฅเคฐเคพเคšเคฒ เค•เฅ‹ เคŸเฅเคฏเฅ‚เคจ เค•เคฐเคจเฅ‡ เค•เคพ เคชเฅเคฐเคฏเคพเคธ เค•เคฐเคจเคพ เค‰เคชเคฏเฅ‹เค—เฅ€ เคนเฅ‹ เคธเค•เคคเคพ เคนเฅˆเฅค + +เค•เคฟเคธเฅ€ เคกเฅ‡เคŸเคพเคธเฅ‡เคŸ เคชเคฐ เคชเฅเคจเคฐเคพเคตเฅƒเคคเคฟ เค•เคฐเคจเฅ‡ เค•เคพ เคธเคฌเคธเฅ‡ เคธเคฐเคฒ เคคเคฐเฅ€เค•เคพ เคฌเคธ เคเค• เค•เฅ‹ ๐Ÿค— [Dataset](https://github.com/huggingface/datasets/) เคธเฅ‡ เคฒเฅ‹เคก เค•เคฐเคจเคพ เคนเฅˆ: + +```py +# KeyDataset is a util that will just output the item we're interested in. +from transformers.pipelines.pt_utils import KeyDataset +from datasets import load_dataset + +pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) +dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") + +for out in pipe(KeyDataset(dataset, "audio")): + print(out) +``` + + +## เคตเฅ‡เคฌเคธเคฐเฅเคตเคฐ เค•เฅ‡ เคฒเคฟเค เคชเคพเค‡เคชเคฒเคพเค‡เคจเฅ‹เค‚ เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ + + +เคเค• เค…เคจเฅเคฎเคพเคจ เค‡เค‚เคœเคจ เคฌเคจเคพเคจเคพ เคเค• เคœเคŸเคฟเคฒ เคตเคฟเคทเคฏ เคนเฅˆ เคœเฅ‹ เค…เคชเคจเฅ‡ เค†เคช เคฎเฅ‡เค‚ เค‰เคชเคฏเฅเค•เฅเคค เคนเฅˆ +เคชเฅƒเคทเฅเค เฅค + + +[Link](./pipeline_webserver) + +## เคตเคฟเคœเคผเคจ เคชเคพเค‡เคชเคฒเคพเค‡เคจ + +เคฆเฅƒเคทเฅเคŸเคฟ เค•เคพเคฐเฅเคฏเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค [`pipeline`] เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ เคตเฅเคฏเคพเคตเคนเคพเคฐเคฟเค• เคฐเฅ‚เคช เคธเฅ‡ เคธเคฎเคพเคจ เคนเฅˆเฅค + +เค…เคชเคจเคพ เค•เคพเคฐเฅเคฏ เคจเคฟเคฐเฅเคฆเคฟเคทเฅเคŸ เค•เคฐเฅ‡เค‚ เค”เคฐ เค…เคชเคจเฅ€ เค›เคตเคฟ เค•เฅเคฒเคพเคธเคฟเคซเคพเคฏเคฐเคฟเคฏเคฐ เค•เฅ‹ เคญเฅ‡เคœเฅ‡เค‚เฅค เค›เคตเคฟ เคเค• เคฒเคฟเค‚เค•, เคเค• เคธเฅเคฅเคพเคจเฅ€เคฏ เคชเคฅ เคฏเคพ เคฌเฅ‡เคธ64-เคเคจเฅเค•เฅ‹เคกเฅ‡เคก เค›เคตเคฟ เคนเฅ‹ เคธเค•เคคเฅ€ เคนเฅˆเฅค เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, เคฌเคฟเคฒเฅเคฒเฅ€ เค•เฅ€ เค•เฅŒเคจ เคธเฅ€ เคชเฅเคฐเคœเคพเคคเคฟ เคจเฅ€เคšเฅ‡ เคฆเคฟเค–เคพเคˆ เค—เคˆ เคนเฅˆ? + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(model="google/vit-base-patch16-224") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` + +## เคชเคพเค  เคชเคพเค‡เคชเคฒเคพเค‡เคจ + +NLP เค•เคพเคฐเฅเคฏเฅ‹เค‚ เค•เฅ‡ เคฒเคฟเค [`pipeline`] เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ เคตเฅเคฏเคพเคตเคนเคพเคฐเคฟเค• เคฐเฅ‚เคช เคธเฅ‡ เคธเคฎเคพเคจ เคนเฅˆเฅค + +```py +>>> from transformers import pipeline + +>>> # This model is a `zero-shot-classification` model. +>>> # It will classify text, except you are free to choose any label you might imagine +>>> classifier = pipeline(model="facebook/bart-large-mnli") +>>> classifier( +... "I have a problem with my iphone that needs to be resolved asap!!", +... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], +... ) +{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]} +``` + +## เคฌเคนเฅเคตเคฟเคง เคชเคพเค‡เคชเคฒเคพเค‡เคจ + +[`pipeline`] เคเค• เคธเฅ‡ เค…เคงเคฟเค• เคคเฅŒเคฐ-เคคเคฐเฅ€เค•เฅ‹เค‚ เค•เคพ เคธเคฎเคฐเฅเคฅเคจ เค•เคฐเคคเฅ€ เคนเฅˆเฅค เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, เคเค• เคฆเฅƒเคถเฅเคฏ เคชเฅเคฐเคถเฅเคจ เค‰เคคเฅเคคเคฐ (VQA) เค•เคพเคฐเฅเคฏ เคชเคพเค  เค”เคฐ เค›เคตเคฟ เค•เฅ‹ เคœเฅ‹เคกเคผเคคเคพ เคนเฅˆเฅค เค…เคชเคจเฅ€ เคชเคธเค‚เคฆ เค•เฅ‡ เค•เคฟเคธเฅ€ เคญเฅ€ เค›เคตเคฟ เคฒเคฟเค‚เค• เค”เคฐ เค›เคตเคฟ เค•เฅ‡ เคฌเคพเคฐเฅ‡ เคฎเฅ‡เค‚ เค•เฅ‹เคˆ เคชเฅเคฐเคถเฅเคจ เคชเฅ‚เค›เคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคธเฅเคตเคคเค‚เคคเฅเคฐ เคฎเคนเคธเฅ‚เคธ เค•เคฐเฅ‡เค‚เฅค เค›เคตเคฟ เคเค• URL เคฏเคพ เค›เคตเคฟ เค•เคพ เคธเฅเคฅเคพเคจเฅ€เคฏ เคชเคฅ เคนเฅ‹ เคธเค•เคคเฅ€ เคนเฅˆเฅค + +เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, เคฏเคฆเคฟ เค†เคช เค‡เคธ [invoice image](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png) เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคคเฅ‡ เคนเฅˆเค‚: + +```py +>>> from transformers import pipeline + +>>> vqa = pipeline(model="impira/layoutlm-document-qa") +>>> output = vqa( +... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", +... question="What is the invoice number?", +... ) +>>> output[0]["score"] = round(output[0]["score"], 3) +>>> output +[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}] +``` + + + +เคŠเคชเคฐ เคฆเคฟเค เค—เค เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‹ เคšเคฒเคพเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เค†เคชเค•เฅ‹ ๐Ÿค— เคŸเฅเคฐเคพเค‚เคธเคซเฅ‰เคฐเฅเคฎเคฐ เค•เฅ‡ เค…เคฒเคพเคตเคพ [`pytesseract`](https://pypi.org/project/pytesseract/) เค‡เค‚เคธเฅเคŸเฅ‰เคฒ เค•เคฐเคจเคพ เคนเฅ‹เค—เคพ: + +```bash +sudo apt install -y tesseract-ocr +pip install pytesseract +``` + + + +## ๐Ÿค— `เคคเฅเคตเคฐเคฃ` เค•เฅ‡ เคธเคพเคฅ เคฌเคกเคผเฅ‡ เคฎเฅ‰เคกเคฒเฅ‹เค‚ เคชเคฐ `pipeline` เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคจเคพ: + +เค†เคช ๐Ÿค— `accelerate` เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเค•เฅ‡ เคฌเคกเคผเฅ‡ เคฎเฅ‰เคกเคฒเฅ‹เค‚ เคชเคฐ เค†เคธเคพเคจเฅ€ เคธเฅ‡ `pipeline` เคšเคฒเคพ เคธเค•เคคเฅ‡ เคนเฅˆเค‚! เคชเคนเคฒเฅ‡ เคธเฅเคจเคฟเคถเฅเคšเคฟเคค เค•เคฐเฅ‡เค‚ เค•เคฟ เค†เคชเคจเฅ‡ `accelerate` เค•เฅ‹ `pip install accelerate` เค•เฅ‡ เคธเคพเคฅ เค‡เค‚เคธเฅเคŸเฅ‰เคฒ เค•เคฟเคฏเคพ เคนเฅˆเฅค + +เคธเคฌเคธเฅ‡ เคชเคนเคฒเฅ‡ `device_map='auto'` เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเค•เฅ‡ เค…เคชเคจเคพ เคฎเฅ‰เคกเคฒ เคฒเฅ‹เคก เค•เคฐเฅ‡เค‚! เคนเคฎ เค…เคชเคจเฅ‡ เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค `facebook/opt-1.3b` เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเฅ‡เค‚เค—เฅ‡เฅค + +```py +# pip install accelerate +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto") +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +เคฏเคฆเคฟ เค†เคช `bitsandbytes` เค‡เค‚เคธเฅเคŸเฅ‰เคฒ เค•เคฐเคคเฅ‡ เคนเฅˆเค‚ เค”เคฐ `load_in_8bit=True` เคคเคฐเฅเค• เคœเฅ‹เคกเคผเคคเฅ‡ เคนเฅˆเค‚ เคคเฅ‹ เค†เคช 8-เคฌเคฟเคŸ เคฒเฅ‹เคกเฅ‡เคก เคฎเฅ‰เคกเคฒ เคญเฅ€ เคชเคพเคธ เค•เคฐ เคธเค•เคคเฅ‡ เคนเฅˆเค‚ + +```py +# pip install accelerate bitsandbytes +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}) +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +เคงเฅเคฏเคพเคจ เคฆเฅ‡เค‚ เค•เคฟ เค†เคช เคšเฅ‡เค•เคชเฅ‰เค‡เค‚เคŸ เค•เฅ‹ เค•เคฟเคธเฅ€ เคญเฅ€ เคนเค—เคฟเค‚เค— เคซเฅ‡เคธ เคฎเฅ‰เคกเคฒ เคธเฅ‡ เคฌเคฆเคฒ เคธเค•เคคเฅ‡ เคนเฅˆเค‚ เคœเฅ‹ BLOOM เคœเฅˆเคธเฅ‡ เคฌเคกเคผเฅ‡ เคฎเฅ‰เคกเคฒ เคฒเฅ‹เคกเคฟเค‚เค— เค•เคพ เคธเคฎเคฐเฅเคฅเคจ เค•เคฐเคคเคพ เคนเฅˆ! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/tflite.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/tflite.md new file mode 100644 index 0000000000000000000000000000000000000000..5a84bed94266db0de5e8fa81cfcd7c1cb2a5b643 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/hi/tflite.md @@ -0,0 +1,55 @@ + + +# TFLite เคฎเฅ‡เค‚ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเฅ‡เค‚ + +[TensorFlow Lite](https://www.tensorflow.org/lite/guide) เคเค• เคนเคฒเฅเค•เคพ เคขเคพเค‚เคšเคพ เคนเฅˆ เคœเฅ‹ เคฎเคถเฅ€เคจ เคฒเคฐเฅเคจเคฟเค‚เค— เคฎเฅ‰เคกเคฒ เค•เฅ‹ เคธเค‚เคธเคพเคงเคจ-เคธเฅ€เคฎเคฟเคค เค‰เคชเค•เคฐเคฃเฅ‹เค‚, เคœเฅˆเคธเฅ‡ เคฎเฅ‹เคฌเคพเค‡เคฒ เคซเฅ‹เคจ, เคเคฎเฅเคฌเฅ‡เคกเฅ‡เคก เคธเคฟเคธเฅเคŸเคฎ เค”เคฐ เค‡เค‚เคŸเคฐเคจเฅ‡เคŸ เค‘เคซ เคฅเคฟเค‚เค—เฅเคธ (IoT) เค‰เคชเค•เคฐเคฃเฅ‹เค‚ เคชเคฐ เคคเฅˆเคจเคพเคค เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคนเฅˆเฅค TFLite เค•เฅ‹ เค‡เคจ เค‰เคชเค•เคฐเคฃเฅ‹เค‚ เคชเคฐ เคธเฅ€เคฎเคฟเคค เค—เคฃเคจเคพเคคเฅเคฎเค• เคถเค•เฅเคคเคฟ, เคฎเฅ‡เคฎเฅ‹เคฐเฅ€ เค”เคฐ เคŠเคฐเฅเคœเคพ เค–เคชเคค เค•เฅ‡ เคธเคพเคฅ เคฎเฅ‰เคกเคฒ เค•เฅ‹ เค•เฅเคถเคฒเคคเคพ เคธเฅ‡ เค‘เคชเฅเคŸเคฟเคฎเคพเค‡เคœเคผ เค”เคฐ เคšเคฒเคพเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค เคกเคฟเคœเคผเคพเค‡เคจ เค•เคฟเคฏเคพ เค—เคฏเคพ เคนเฅˆเฅค เคเค• TensorFlow Lite เคฎเฅ‰เคกเคฒ เค•เฅ‹ เคเค• เคตเคฟเคถเฅ‡เคท เค•เฅเคถเคฒ เคชเฅ‹เคฐเฅเคŸเฅ‡เคฌเคฒ เคชเฅเคฐเคพเคฐเฅ‚เคช เคฎเฅ‡เค‚ เคฆเคฐเฅเคถเคพเคฏเคพ เคœเคพเคคเคพ เคนเฅˆ เคœเคฟเคธเฅ‡ `.tflite` เคซเคผเคพเค‡เคฒ เคเค•เฅเคธเคŸเฅ‡เค‚เคถเคจ เคฆเฅเคตเคพเคฐเคพ เคชเคนเคšเคพเคจเคพ เคœเคพเคคเคพ เคนเฅˆเฅค + +๐Ÿค— Optimum เคฎเฅ‡เค‚ `exporters.tflite` เคฎเฅ‰เคกเฅเคฏเฅ‚เคฒ เค•เฅ‡ เคฎเคพเคงเฅเคฏเคฎ เคธเฅ‡ ๐Ÿค— Transformers เคฎเฅ‰เคกเคฒ เค•เฅ‹ TFLite เคฎเฅ‡เค‚ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเคจเฅ‡ เค•เฅ€ เค•เคพเคฐเฅเคฏเค•เฅเคทเคฎเคคเคพ เคนเฅˆเฅค เคธเคฎเคฐเฅเคฅเคฟเคค เคฎเฅ‰เคกเคฒ เค†เคฐเฅเค•เคฟเคŸเฅ‡เค•เฅเคšเคฐ เค•เฅ€ เคธเฅ‚เคšเฅ€ เค•เฅ‡ เคฒเคฟเค, เค•เฅƒเคชเคฏเคพ [๐Ÿค— Optimum เคฆเคธเฅเคคเคพเคตเฅ‡เคœเคผ](https://huggingface.co/docs/optimum/exporters/tflite/overview) เคฆเฅ‡เค–เฅ‡เค‚เฅค + +TFLite เคฎเฅ‡เค‚ เคเค• เคฎเฅ‰เคกเคฒ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค, เค†เคตเคถเฅเคฏเค• เคจเคฟเคฐเฅเคญเคฐเคคเคพเคเค เคธเฅเคฅเคพเคชเคฟเคค เค•เคฐเฅ‡เค‚: + +```bash +pip install optimum[exporters-tf] +``` + +เคธเคญเฅ€ เค‰เคชเคฒเคฌเฅเคง เคคเคฐเฅเค•เฅ‹เค‚ เค•เฅ€ เคœเคพเค‚เคš เค•เคฐเคจเฅ‡ เค•เฅ‡ เคฒเคฟเค, [๐Ÿค— Optimum เคฆเคธเฅเคคเคพเคตเฅ‡เคœเคผ](https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model) เคฆเฅ‡เค–เฅ‡เค‚, +เคฏเคพ เค•เคฎเคพเค‚เคก เคฒเคพเค‡เคจ เคฎเฅ‡เค‚ เคฎเคฆเคฆ เคฆเฅ‡เค–เฅ‡เค‚: + +```bash +optimum-cli export tflite --help +``` + +เคฏเคฆเคฟ เค†เคช ๐Ÿค— Hub เคธเฅ‡ เคเค• เคฎเฅ‰เคกเคฒ เค•เคพ เคšเฅ‡เค•เคชเฅ‰เค‡เค‚เคŸ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเคจเคพ เคšเคพเคนเคคเฅ‡ เคนเฅˆเค‚, เค‰เคฆเคพเคนเคฐเคฃ เค•เฅ‡ เคฒเคฟเค, `google-bert/bert-base-uncased`, เคจเคฟเคฎเฅเคจเคฒเคฟเค–เคฟเคค เค•เคฎเคพเค‚เคก เคšเคฒเคพเคเค: + +```bash +optimum-cli export tflite --model google-bert/bert-base-uncased --sequence_length 128 bert_tflite/ +``` + +เค†เคชเค•เฅ‹ เคชเฅเคฐเค—เคคเคฟ เค•เฅ‹ เคฆเคฐเฅเคถเคพเคคเฅ‡ เคนเฅเค เคฒเฅ‰เค— เคฆเคฟเค–เคพเคˆ เคฆเฅ‡เค‚เค—เฅ‡ เค”เคฐ เคฏเคน เคฆเคฟเค–เคพเคเค‚เค—เฅ‡ เค•เคฟ เคชเคฐเคฟเคฃเคพเคฎเคธเฅเคตเคฐเฅ‚เคช `model.tflite` เค•เคนเคพเค เคธเคนเฅ‡เคœเคพ เค—เคฏเคพ เคนเฅˆ, เคœเฅˆเคธเฅ‡: + +```bash +Validating TFLite model... + -[โœ“] TFLite model output names match reference model (logits) + - Validating TFLite Model output "logits": + -[โœ“] (1, 128, 30522) matches (1, 128, 30522) + -[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05) +The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05: +- logits: max diff = 5.817413330078125e-05. + The exported model was saved at: bert_tflite +``` + +เค‰เคชเคฐเฅ‹เค•เฅเคค เค‰เคฆเคพเคนเคฐเคฃ ๐Ÿค— Hub เคธเฅ‡ เคเค• เคšเฅ‡เค•เคชเฅ‰เค‡เค‚เคŸ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเคจเฅ‡ เค•เฅ‹ เคฆเคฐเฅเคถเคพเคคเคพ เคนเฅˆเฅค เคœเคฌ เคเค• เคธเฅเคฅเคพเคจเฅ€เคฏ เคฎเฅ‰เคกเคฒ เคจเคฟเคฐเฅเคฏเคพเคค เค•เคฐเคคเฅ‡ เคนเฅˆเค‚, เคคเฅ‹ เคชเคนเคฒเฅ‡ เคธเฅเคจเคฟเคถเฅเคšเคฟเคค เค•เคฐเฅ‡เค‚ เค•เคฟ เค†เคชเคจเฅ‡ เคฎเฅ‰เคกเคฒ เค•เฅ‡ เคตเคœเคผเคจ เค”เคฐ เคŸเฅ‹เค•เคจเคพเค‡เคœเคผเคฐ เคซเคผเคพเค‡เคฒเฅ‹เค‚ เค•เฅ‹ เคเค• เคนเฅ€ เคจเคฟเคฐเฅเคฆเฅ‡เคถเคฟเค•เคพ (`local_path`) เคฎเฅ‡เค‚ เคธเคนเฅ‡เคœเคพ เคนเฅˆเฅค CLI เค•เคพ เค‰เคชเคฏเฅ‹เค— เค•เคฐเคคเฅ‡ เคธเคฎเคฏ, เคšเฅ‡เค•เคชเฅ‰เค‡เค‚เคŸ เคจเคพเคฎ เค•เฅ‡ เคฌเคœเคพเคฏ `model` เคคเคฐเฅเค• เคฎเฅ‡เค‚ `local_path` เคชเคพเคธ เค•เคฐเฅ‡เค‚เฅค diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_config.py b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_config.py new file mode 100644 index 0000000000000000000000000000000000000000..72b362f9a7230a3aa8fa5ef72747bf56cec5f6b8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_config.py @@ -0,0 +1,15 @@ +# docstyle-ignore +INSTALL_CONTENT = """ +# Installazione di Transformers +! pip install transformers datasets evaluate accelerate +# Per installare dalla fonte invece dell'ultima versione rilasciata, commenta il comando sopra e +# rimuovi la modalitร  commento al comando seguente. +# ! pip install git+https://github.com/huggingface/transformers.git +""" + +notebook_first_cells = [{"type": "code", "content": INSTALL_CONTENT}] +black_avoid_patterns = { + "{processor_class}": "FakeProcessorClass", + "{model_class}": "FakeModelClass", + "{object_class}": "FakeObjectClass", +} diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_toctree.yml b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_toctree.yml new file mode 100644 index 0000000000000000000000000000000000000000..47d90f9a9a85c985d2233a61e1eac1f398536272 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/_toctree.yml @@ -0,0 +1,71 @@ +- sections: + - local: index + title: ๐Ÿค— Transformers + - local: quicktour + title: Tour rapido + - local: installation + title: Installazione + title: Iniziare +- sections: + - local: pipeline_tutorial + title: Pipeline per l'inferenza + - local: autoclass_tutorial + title: Carica istanze pre-allenate con AutoClass + - local: preprocessing + title: Preprocess + - local: training + title: Fine-tuning di un modello pre-addestrato + - local: accelerate + title: Allenamento distribuito con ๐Ÿค— Accelerate + - local: model_sharing + title: Condividere un modello + title: Esercitazione +- sections: + - local: create_a_model + title: Crea un'architettura personalizzata + - local: custom_models + title: Condividere modelli personalizzati + - local: run_scripts + title: Addestramento con script + - local: multilingual + title: Modelli multilingua per l'inferenza + - local: converting_tensorflow_models + title: Convertire modelli tensorflow + - local: serialization + title: Esporta modelli Transformers + - local: perf_train_cpu + title: Addestramento efficiente su CPU + - local: perf_train_cpu_many + title: Addestramento efficiente su multiple CPU + - local: perf_train_tpu + title: Addestramento su TPU + - local: perf_train_special + title: Addestramento su Hardware Specializzato + - local: perf_infer_cpu + title: Inferenza Efficiente su CPU + - local: perf_infer_gpu_one + title: Inferenza su una GPU + - local: perf_infer_gpu_many + title: Inferenza Efficiente su GPU Multiple + - local: perf_infer_special + title: Inferenza su Hardware Specializzato + - local: big_models + title: Istanziare un big model + - local: migration + title: Passaggio da pacchetti precedenti + - local: debugging + title: Debugging + title: Guide pratiche +- sections: + - local: add_new_pipeline + title: Come aggiungere una pipeline a ๐Ÿค— Transformers? + - local: add_new_model + title: Come aggiungere un modello a ๐Ÿค— Transformers? + - local: perf_hardware + title: Hardware ottimizzato per l'addestramento + - local: community + title: Risorse della comunitร  + - local: pr_checks + title: Controlli su una Pull Request + title: Guide How-to + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/accelerate.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/accelerate.md new file mode 100644 index 0000000000000000000000000000000000000000..3114613a9a7994c0c326b5deb6da428f5be9ab3a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/accelerate.md @@ -0,0 +1,136 @@ + + +# Allenamento distribuito con ๐Ÿค— Accelerate + +La parallelizzazione รจ emersa come strategia per allenare modelli sempre piรน grandi su hardware limitato e accelerarne la velocitร  di allenamento di diversi ordini di magnitudine. In Hugging Face, abbiamo creato la libreria [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) per aiutarti ad allenare in modo semplice un modello ๐Ÿค— Transformers su qualsiasi tipo di configurazione distribuita, sia che si tratti di piรน GPU su una sola macchina o di piรน GPU su piรน macchine. In questo tutorial, imparerai come personalizzare il training loop nativo di PyTorch per consentire l'addestramento in un ambiente distribuito. + +## Configurazione + +Inizia installando ๐Ÿค— Accelerate: + +```bash +pip install accelerate +``` + +Poi importa e crea un oggetto [`Accelerator`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator). `Accelerator` rileverร  automaticamente il tuo setup distribuito e inizializzerร  tutte le componenti necessarie per l'allenamento. Non dovrai allocare esplicitamente il tuo modello su un device. + +```py +>>> from accelerate import Accelerator + +>>> accelerator = Accelerator() +``` + +## Preparati ad accelerare + +Il prossimo passo รจ quello di passare tutti gli oggetti rilevanti per l'allenamento al metodo [`prepare`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator.prepare). Questo include i tuoi DataLoaders per l'allenamento e per la valutazione, un modello e un ottimizzatore: + +```py +>>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( +... train_dataloader, eval_dataloader, model, optimizer +... ) +``` + +## Backward + +Infine, sostituisci il tipico metodo `loss.backward()` nel tuo loop di allenamento con il metodo [`backward`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator.backward) di ๐Ÿค— Accelerate: + +```py +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... outputs = model(**batch) +... loss = outputs.loss +... accelerator.backward(loss) + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +Come puoi vedere nel seguente codice, hai solo bisogno di aggiungere quattro righe in piรน di codice al tuo training loop per abilitare l'allenamento distribuito! + +```diff ++ from accelerate import Accelerator + from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + ++ accelerator = Accelerator() + + model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) + optimizer = AdamW(model.parameters(), lr=3e-5) + +- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +- model.to(device) + ++ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ++ train_dataloader, eval_dataloader, model, optimizer ++ ) + + num_epochs = 3 + num_training_steps = num_epochs * len(train_dataloader) + lr_scheduler = get_scheduler( + "linear", + optimizer=optimizer, + num_warmup_steps=0, + num_training_steps=num_training_steps + ) + + progress_bar = tqdm(range(num_training_steps)) + + model.train() + for epoch in range(num_epochs): + for batch in train_dataloader: +- batch = {k: v.to(device) for k, v in batch.items()} + outputs = model(**batch) + loss = outputs.loss +- loss.backward() ++ accelerator.backward(loss) + + optimizer.step() + lr_scheduler.step() + optimizer.zero_grad() + progress_bar.update(1) +``` + +## Allenamento + +Una volta che hai aggiunto le righe di codice rilevanti, lancia il tuo allenamento in uno script o in un notebook come Colaboratory. + +### Allenamento con uno script + +Se stai eseguendo il tuo allenamento da uno script, esegui il comando seguente per creare e salvare un file di configurazione: + +```bash +accelerate config +``` + +Poi lancia il tuo allenamento con: + +```bash +accelerate launch train.py +``` + +### Allenamento con un notebook + +La libreria ๐Ÿค— Accelerate puรฒ anche essere utilizzata in un notebook se stai pianificando di utilizzare le TPU di Colaboratory. Inserisci tutto il codice legato all'allenamento in una funzione, e passala al `notebook_launcher`: + +```py +>>> from accelerate import notebook_launcher + +>>> notebook_launcher(training_function) +``` + +Per maggiori informazioni relative a ๐Ÿค— Accelerate e le sue numerose funzionalitร , fai riferimento alla [documentazione](https://huggingface.co/docs/accelerate). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_model.md new file mode 100644 index 0000000000000000000000000000000000000000..1cd5da18c64545a1ae5ba6a343c7a79951487fd5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_model.md @@ -0,0 +1,780 @@ + + +# Come aggiungere un modello a ๐Ÿค— Transformers? + +Aggiungere un nuovo modello รฉ spesso difficile e richiede una profonda conoscenza della libreria ๐Ÿค— Transformers e anche +della repository originale del modello. A Hugging Face cerchiamo di dare alla community sempre piรบ poteri per aggiungere +modelli independentemente. Quindi, per alcuni nuovi modelli che la community vuole aggiungere a ๐Ÿค— Transformers, abbiamo +creato una specifica *call-for-model-addition* che spiega passo dopo passo come aggiungere il modello richiesto. Con +questo *call-for-model-addition* vogliamo insegnare a volenterosi e esperti collaboratori della community come implementare +un modello in ๐Ÿค— Transformers. + +Se questo รฉ qualcosa che puรฒ interessarvi, siete liberi di controllare l'attuale โ€œcalls-for-model-additionโ€ [qui](https://github.com/huggingface/transformers/tree/main/templates/adding_a_new_model/open_model_proposals/README.md) +e contattarci. + +Se il modello sarร  selezionato, allora potrete lavorare insieme a un membro di Hugging Face per integrare il modello in ๐Ÿค— +Transformers. Cosรฌ facendo, ci guadagnerai in una comprensione totale, sia teorica che pratica, del modello proposto. Inoltre, +sarai l'artefice di un importante contributo open-source a ๐Ÿค— Transformers. Durante l'implementazione avrai l'opportunitร  di: + +- ottenere piรน comprensione delle best practices in open-source +- capire i principi di design di una della librerie NLP piรน popolari +- capire come efficientemente testare complessi modelli NLP +- capire come integrare utilit Python come `black`, `ruff`, `make fix-copies` in una libreria per garantire sempre di avere un codice leggibile e pulito + +Siamo anche contenti se vuoi aggiungere un modello che non puรฒ essere trovato nella cartella โ€œcalls-for-model-additionโ€. +Le seguenti sezioni spiegano in dettaglio come aggiungere un nuovo modello. Puรฒ anche essere molto utile controllare modelli +giร  aggiunti [qui](https://github.com/huggingface/transformers/pulls?q=is%3Apr+label%3A%22PR+for+Model+Addition%22+is%3Aclosed), +per capire se richiamano il modello che vorreste aggiungere. + +Per cominciare, vediamo una panoramica general della libreria Transformers. + +## Panoramica generale su ๐Ÿค— Transformers + +Prima di tutto, vediamo in generale ๐Ÿค— Transformers. ๐Ÿค— Transformers รฉ una libreria molto strutturata, quindi +puร  essere che a volte ci sia un disaccordo con alcune filosofie della libreria o scelte di design. Dalla nostra esperienza, +tuttavia, abbiamo trovato che le scelte fondamentali di design della libreria sono cruciali per usare ๐Ÿค— Transformers efficacemente +su larga scala, mantenendo i costi a un livello accettabile. + +Un buon primo punto di partenza per capire al meglio la libreria รฉ leggere la [documentazione sulla nostra filosofia](filosofia) +Da qui, ci sono alcune scelte sul modo di lavorare che cerchiamo di applicare a tutti i modelli: + +- La composizione รฉ generalmente favorita sulla sovra-astrazione +- Duplicare il codice non รฉ sempre male, soprattutto se migliora notevolmente la leggibilitร  e accessibilitร  del modello +- Tutti i files creati per il nuovo modello devono il piu possibile "compatti". Questo vuol dire che quando qualcuno leggerรก il codice +di uno specifico modello, potrรก vedere solo il corrispettivo file `modeling_....py` senza avere multiple dipendenze. + + +La cosa piรบ importante, รฉ che consideriamo la libreria non solo un mezzo per dare un prodotto, *per esempio* dare la possibilitร  +di usare BERT per inferenza, ma รฉ anche il prodotto reale che noi vogliamo migliorare sempre piรน. Quindi, quando aggiungi +un modello, non sei solo la persona che userร  il modello, ma rappresenti anche tutti coloro che leggeranno, +cercheranno di capire e modificare il tuo modello. + +Tenendo questi principi in mente, immergiamoci nel design generale della libreria. + +### Panoramica sui modelli + +Per aggiungere con successo un modello, รฉ importante capire l'interazione tra il tuo modello e la sua configurazione, +[`PreTrainedModel`], e [`PretrainedConfig`]. Per dare un esempio, chiameremo il modello da aggiungere a ๐Ÿค— Transformers +`BrandNewBert`. + +Diamo un'occhiata: + + + +Come potete vedere, ci basiamo sull'ereditarietร  in ๐Ÿค— Transformers, tenendo perรฒ il livello di astrazione a un minimo +assoluto. Non ci sono mai piรน di due livelli di astrazione per ogni modello nella libreria. `BrandNewBertModel` eredita +da `BrandNewBertPreTrainedModel` che, a sua volta, eredita da [`PreTrainedModel`] - semplice no? +Come regola generale, vogliamo essere sicuri che un nuovo modello dipenda solo da [`PreTrainedModel`]. Le funzionalitร  +importanti che sono automaticamente conferite a ogni nuovo modello sono [`~PreTrainedModel.from_pretrained`] +e [`~PreTrainedModel.save_pretrained`], che sono usate per serializzazione e deserializzazione. Tutte le altre importanti +funzionalitร , come ad esempio `BrandNewBertModel.forward` devono essere definite completamente nel nuovo script +`modeling_brand_new_bert.py`. Inoltre, vogliamo essere sicuri che un modello con uno specifico head layer, come +`BrandNewBertForMaskedLM` non erediti da `BrandNewBertModel`, ma piuttosto usi `BrandNewBertModel` +come componente che puรฒ essere chiamata nel passaggio forward per mantenere il livello di astrazione basso. Ogni +nuovo modello richieste una classe di configurazione, chiamata `BrandNewBertConfig`. Questa configurazione รฉ sempre +mantenuta come un attributo in [`PreTrainedModel`], e quindi puรฒ essere accessibile tramite l'attributo `config` +per tutte le classi che ereditano da `BrandNewBertPreTrainedModel`: + +```python +model = BrandNewBertModel.from_pretrained("brandy/brand_new_bert") +model.config # il modello ha accesso al suo config +``` + +Analogamente al modello, la configurazione eredita le funzionalitร  base di serializzazione e deserializzazione da +[`PretrainedConfig`]. ร‰ da notare che la configurazione e il modello sono sempre serializzati in due formati differenti - +il modello รฉ serializzato in un file *pytorch_model.bin* mentre la configurazione con *config.json*. Chiamando +[`~PreTrainedModel.save_pretrained`] automaticamente chiamerร  [`~PretrainedConfig.save_pretrained`], cosicchรฉ sia il +modello che la configurazione siano salvati. + + +### Stile per il codice + +Quando codifichi un nuovo modello, tieni presente che Transformers ha una sua struttura di fondo come libreria, perciรฒ +ci sono alcuni fatti da considerare su come scrivere un codice :-) + +1. Il forward pass del tuo modello dev'essere scritto completamente nel file del modello, mentre dev'essere indipendente + da altri modelli nella libreria. Se vuoi riutilizzare un blocco di codice da un altro modello, copia e incolla il codice con un commento `# Copied from` in cima al codice (guarda [qui](https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/models/roberta/modeling_roberta.py#L160) + per un ottimo esempio). +2. Il codice dev'essere interamente comprensibile, anche da persone che non parlano in inglese. Questo significa che le + variabili devono avere un nome descrittivo e bisogna evitare abbreviazioni. Per esempio, `activation` รฉ molto meglio + che `act`. Le variabili con una lettera sono da evitare fortemente, almeno che non sia per un indce in un for loop. +3. Generamente รฉ meglio avere un codice esplicito e piรบ lungo che un codice corto e magico. +4. Evita di subclassare `nn.Sequential` in Pytorch, puoi subclassare `nn.Module` e scrivere il forward pass, cosicchรฉ + chiunque puรฒ effettuare debug sul tuo codice, aggiungendo print o breaking points. +5. La tua function-signature dev'essere type-annoted. Per il resto, รฉ meglio preferire variabili con un nome accettabile + piuttosto che annotazioni per aumentare la comprensione e leggibilitร  del codice. + +### Panoramica sui tokenizers + +Questa sezione sarร  creata al piu presto :-( + +## Aggiungere un modello a ๐Ÿค— Transformers passo dopo passo + +Ci sono differenti modi per aggiungere un modello a Hugging Face. Qui trovi una lista di blog posts da parte della community su come aggiungere un modello: + +1. [Aggiungere GPT2](https://medium.com/huggingface/from-tensorflow-to-pytorch-265f40ef2a28) scritto da [Thomas](https://huggingface.co/thomwolf) +2. [Aggiungere WMT19 MT](https://huggingface.co/blog/porting-fsmt) scritto da [Stas](https://huggingface.co/stas) + +Per esperienza, possiamo dirti che quando si aggiunge un modello รฉ meglio tenere a mente le seguenti considerazioni: + +- Non sfondare una porta giรก aperta! La maggior parte del codice che aggiungerai per un nuovo modello ๐Ÿค— Transformers + esiste giร  da qualche parte in ๐Ÿค— Transformers. Prendi un po' di tempo per trovare codici simili in modelli e tokenizers esistenti e fare un copia-incolla. Ricorda che [grep](https://www.gnu.org/software/grep/) e [rg](https://github.com/BurntSushi/ripgrep) sono tuoi buoni amici. Inoltre, ricorda che puรณ essere molto probabile che il tokenizer per il tuo modello sia basato sull'implementazione di un altro modello, e il codice del tuo modello stesso su un altro ancora. *Per esempio* il modello FSMT รฉ basato su BART, mentre il tokenizer di FSMT รฉ basato su XLM. +- Ricorda che qui รฉ piu una sfida ingegneristica che scientifica. Spendi piรบ tempo per create un efficiente ambiente di debugging piuttosto che cercare di capire tutti gli aspetti teorici dell'articolo del modello. +- Chiedi aiuto se sei in panne! I modelli sono la parte principale di ๐Ÿค— Transformers, perciรฒ qui a Hugging Face siamo piรน che contenti di aiutarti in ogni passo per aggiungere il tuo modello. Non esitare a chiedere se vedi che non riesci a progredire. + +Di seguito, diamo una ricetta generale per aiutare a portare un modello in ๐Ÿค— Transformers. + +La lista seguente รฉ un sommario di tutto quello che รฉ stato fatto per aggiungere un modello, e puรฒ essere usata come To-Do List: + +- 1. โ˜ (Opzionale) Capire gli aspetti teorici del modello +- 2. โ˜ Preparare l'ambiente dev per transformers +- 3. โ˜ Preparare l'ambiente debugging della repository originale +- 4. โ˜ Create uno script che gestisca con successo il forward pass usando la repository originale e checkpoint +- 5. โ˜ Aggiungere con successo lo scheletro del modello a Transformers +- 6. โ˜ Convertire i checkpoint original a Transformers checkpoint +- 7. โ˜ Effettuare con successo la forward pass in Transformers, di modo che dia un output identico al checkpoint originale +- 8. โ˜ Finire i tests per il modello in Transformers +- 9. โ˜ Aggiungere con successo Tokenizer in Transformers +- 10. โ˜ Testare e provare gli integration tests da capo a fine +- 11. โ˜ Completare i docs +- 12. โ˜ Caricare i moedl weights all'hub +- 13. โ˜ Sottomettere una pull request +- 14. โ˜ (Opzionale) Aggiungere un notebook con una demo + +Per cominciare di solito consigliamo `BrandNewBert`, partendo dalla teoria, di modo da avere una buona comprensione della teoria generale. TUttavia, se preferisci imparare l'aspetto teorico del modello mentre *lavori* sul modello รฉ ok immergersi direttamente nel codice di `BrandNewBert`. Questa opzione puรณ essere buona se le tue skills ingegneristiche sono meglio che quelle teoriche, o se il paper `BrandNewBert` ti dรก problemi, o se semplicemente ti piace programmare piรบ che leggere articoli scientifici. + +### 1. (Opzionale) Aspetti teorici di BrandNewBert + +Allora con calma, prendi un po' di tempo per leggere l'articolo su *BrandNewBert* . Sicuramente, alcune sezioni dell'articolo sono molto complesse, ma non preoccuparti! L'obiettivo non รฉ avere una compresione immensa della teoria alla base, ma estrarre le informazioni necessarie per re-implementare con successo il modello in ๐Ÿค— Transformers. Quindi, non impazzire sugli aspetti teorici, ma piuttosto focalizzati su quelli pratici, ossia: + +- Che tipo di modello รฉ *brand_new_bert*? ร‰ solo un encoder in stile BERT? O tipo decoder come GPT2? O encoder e decoder stile BART? Dai un'occhiata a [model_summary](model_summary) se non sei famigliare con le differenze tra questi modelli +- Quali sono le applicazioni di *brand_new_bert*? Classificazione di testo? Generazione di testo? O per tasks del genere seq2seq? +- Quali sono le nuove aggiunte al modello che lo rendono diverso da BERT/GPT-2/BART? +- Quali modelli estistenti in [๐Ÿค— Transformers models](https://huggingface.co/transformers/#contents) sono molto simili a *brand_new_bert*? +- Che tipo di tokenizer si usa in questo caso? Un sentencepiece tokenizer? O un word piece tokenizer? Il tokenizer รฉ lo stesso di BERT o BART? + +Una volta che senti che hai avuto una bella overview dell'architettura del modello, puoi scrivere senza problemi al team di Hugging Face per ogni domanda che tu hai. Questo puรณ includere domande sull'architettura del modello, o sull'attention layer, etc. Saremo molto felici di aiutarti :) + + +### 2. Prepare il tuo ambiente + +1. Forka la [repository](https://github.com/huggingface/transformers) cliccando sul tasto โ€˜Fork' nella pagina della repository. Questo crea una copia del codice nel tuo account GitHub + +2. Clona il tuo fork `transfomers` sul tuo dico locale, e aggiungi la repository base come remota: + +```bash +git clone https://github.com/[your Github handle]/transformers.git +cd transformers +git remote add upstream https://github.com/huggingface/transformers.git +``` + + +3. Crea un ambiente di sviluppo, per esempio tramite questo comando: + +```bash +python -m venv .env +source .env/bin/activate +pip install -e ".[dev]" +``` + +quindi torna alla directory principale: + +```bash +cd .. +``` + + +4. Attenzione, raccomandiamo di aggiungere la versione di PyTorch di *brand_new_bert* a Transfomers. Per installare PyTorch, basta seguire queste istruzioni https://pytorch.org/get-started/locally/. + +**Nota bene:** Non c'รฉ bisogno di installare o avere installato CUDA. Il nuovo modello puรฒ funzionare senza problemi su una CPU. + + +5. Per trasferire *brand_new_bert* To port *brand_new_bert* avrai bisogno anche accesso alla sua repository originale: + +```bash +git clone https://github.com/org_that_created_brand_new_bert_org/brand_new_bert.git +cd brand_new_bert +pip install -e . +``` + +Ok, ora hai un ambiente di sviluppo per portare *brand_new_bert* in ๐Ÿค— Transformers. + + +### 3.-4. Provare un pretrained checkpoint usando la repo originale + +Per cominciare, comincerai a lavorare sulla repo originale di *brand_new_bert*. Come spesso accade, l'implementazione originale รฉ molto sullo stile "ricerca". Questo significa che a volte la documentazione non รฉ al top, magari manca qualche cosa e il codice puรณ essere difficile da capire. Tuttavia, questa รฉ e dev'essere la motivazione per reimplementare *brand_new_bert*. In Hugging Face, uno degli obiettivi principali รฉ di *mettere le persone sulle spalle dei giganti*, il che si traduce, in questo contesto, di prendere un modello funzionante e riscriverlo e renderlo il piรบ possibile **accessibile, user-friendly, e leggibile**. Questa รฉ la top motivazione per re-implementare modelli in ๐Ÿค— Transformers - cercare di creare nuove complesse tecnologie NLP accessibili a **chiunque**. + +Riuscire a far girare il modello pretrained originale dalla repository ufficiale รฉ spesso il passo **piu arduo**. Dalla nostra esperienza, รฉ molto importante spendere un p' di tempo per diventare familiari con il codice base originale. Come test, prova a capire i seguenti punti: + +- Dove si trovano i pretrained weights? +- Come caricare i pretrained weights nel modello corrispondente? +- Come girare un tokenizer independentemente dal modello? +- Prova a tracciare un singolo forward pass, cosicchรฉ potrai sapere che classi e funzioni sono richieste per un semplice forward pass. Di solito, dovrai reimplementare queste funzioni e basta +- Prova a localizzare i componenti importanti del modello: Dove si trova la classe del modello? Ci sono sotto classi nel modello *per esempio* EngoderModel, DecoderMOdel? Dove si trova il self-attention layer? Ci sono molteplici differenti layer di attention, *per esempio * *self-attention*, *cross-attention*...? +- Come puoi fare debug sul modello nell'ambiente originale della repo? Devi aggiungere dei *print* o puoi usare *ipdb* come debugger interattivo, o vabene anche un IDE efficiente per debug come PyCharm? + +ร‰ molto importante che prima di cominciare a trasferire il modello nuovo tu spenda tempo a fare debug del codice originale in maniera **efficiente**! Inoltre, ricorda che tutta la library รฉ open-soruce, quindi non temere di aprire issue o fare una pull request nella repo originale. Tutti coloro che mantengono la repository saranno piรบ che felici di avere qualcuno che guarda e gioca con i loro codici! + +A questo punto, sta a te decidere quale ambiente per debug vuoi usare. Noi consilgiamo di evitare setup con GPU, che potrebbero costare assai, lavorare su una CPU puรณ essere un ottimo punto di partenza per indagare la repository originale e per cominciare a scrivere il codice per ๐Ÿค— Transformers. Solo alla fine, quando il modello รฉ stato portato con successo in ๐Ÿค— Transformers, allora si potrรก verificare il suo funzionamento su GPU. + +In generale ci sono due possibili ambienti di debug per il testare il modello originale: + +- [Jupyter notebooks](https://jupyter.org/) / [google colab](https://colab.research.google.com/notebooks/intro.ipynb) +- Scripts locali in Python + +Il vantaggio dei Jupyter notebooks รฉ la possibilitร  di eseguire cella per cella, il che puรฒ essere utile per decomporre tutte le componenti logiche, cosi da a vere un ciclo di debug piรน rapido, siccome si possono salvare i risultati da steps intermedi. Inoltre, i notebooks spesso sono molto facili da condividere con altri contributors, il che puรฒ essere molto utile se vuoi chiedere aiuto al team di Hugging Face. Se sei famigliare con Jupyter notebooks allora racommandiamo di lavorare in questa maniera. + +Ovviamente se non siete abituati a lavorare con i notebook, questo puรฒ essere uno svantaggio nell'usare questa tecnologia, sprecando un sacco di tempo per setup e portare tutto al nuovo ambiente, siccome non potreste neanche usare dei tools di debug come `ipdb`. + +Per ogni pratica code-base, รฉ sempre meglio come primo step caricare un **piccolo** checkpoint pretrained e cercare di riprodurre un singolo forward pass usando un vettore fittizio di IDs fatti da numeri interi. Un esempio per uno script simile, in pseudocodice รฉ: + +```python +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = [0, 4, 5, 2, 3, 7, 9] # vector of input ids +original_output = model.predict(input_ids) +``` + +Per quanto riguarda la strategia di debugging, si puรฒ scegliere tra: + +- Decomporre il modello originario in piccole componenenti e testare ognuna di esse +- Decomporre il modello originario nel *tokenizer* originale e nel *modello* originale, testare un forward pass su questi, +e usare dei print statement o breakpoints intermedi per verificare + +Ancora una volta, siete liberi di scegliere quale strategia sia ottimale per voi. Spesso una strategia รฉ piu +avvantaggiosa di un'altra, ma tutto dipende dall'code-base originario. + +Se il code-base vi permette di decomporre il modello in piccole sub-componenenti, *per esempio* se il code-base +originario puรฒ essere facilmente testato in eager mode, allora vale la pena effettuare un debugging di questo genere. +Ricordate che ci sono dei vantaggi nel decidere di prendere la strada piu impegnativa sin da subito: + +- negli stage piu finali, quando bisognerร  comparare il modello originario all'implementazione in Hugging Face, potrete verificare +automaticamente ogni componente, individualmente, di modo che ci sia una corrispondenza 1:1 +- avrete l'opportunitร  di decomporre un problema molto grande in piccoli passi, cosรฌ da strutturare meglio il vostro lavoro +- separare il modello in componenti logiche vi aiuterร  ad avere un'ottima overview sul design del modello, quindi una migliore +comprensione del modello stesso +- verso gli stage finali i test fatti componente per componente vi aiuterร  ad essere sicuri di non andare avanti e indietro +nell'implementazione, cosรฌ da continuare la modifica del codice senza interruzione + +Un ottimo esempio di come questo puรฒ essere fatto รฉ dato da [Lysandre](https://gist.github.com/LysandreJik/db4c948f6b4483960de5cbac598ad4ed) +per il modello ELECTRA + +Tuttavia, se il code-base originale รฉ molto complesso o le componenti intermedie possono essere testate solo in tramite +compilazione, potrebbe richiedere parecchio tempo o addirittura essere impossibile separare il modello in piccole sotto-componenti. +Un buon esempio รฉ [MeshTensorFlow di T5](https://github.com/tensorflow/mesh/tree/master/mesh_tensorflow). Questa libreria +รฉ molto complessa e non offre un metodo semplice di decomposizione in sotto-componenti. Per simili librerie, potrete fare +affidamento ai print statements. + +In ogni caso, indipendentemente da quale strategia scegliete, la procedura raccomandata รฉ di cominciare a fare debug dal +primo layer al layer finale. +ร‰ consigliato recuperare gli output dai layers, tramite print o sotto-componenti, nel seguente ordine: + +1. Recuperare gli IDs di input dati al modello +2. Recuperare i word embeddings +3. Recuperare l'input del primo Transformer layer +4. Recuperare l'output del primo Transformer layer +5. Recuperare l'output dei seguenti `n - 1` Transformer layers +6. Recuperare l'output dell'intero BrandNewBert Model + +Gli IDs in input dovrebbero essere un arrary di interi, *per esempio* `input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19]` + +Gli output dei seguenti layer di solito dovrebbero essere degli array di float multi-dimensionali come questo: + +``` +[[ + [-0.1465, -0.6501, 0.1993, ..., 0.1451, 0.3430, 0.6024], + [-0.4417, -0.5920, 0.3450, ..., -0.3062, 0.6182, 0.7132], + [-0.5009, -0.7122, 0.4548, ..., -0.3662, 0.6091, 0.7648], + ..., + [-0.5613, -0.6332, 0.4324, ..., -0.3792, 0.7372, 0.9288], + [-0.5416, -0.6345, 0.4180, ..., -0.3564, 0.6992, 0.9191], + [-0.5334, -0.6403, 0.4271, ..., -0.3339, 0.6533, 0.8694]]], +``` + +Ci aspettiamo che ogni modello aggiunto a ๐Ÿค— Transformers passi con successo un paio di test d'integrazione. Questo +significa che il modello originale e la sua implementazione in ๐Ÿค— Transformers abbiano lo stesso output con una precisione +di 0.001! Siccome รฉ normale che lo stesso esatto modello, scritto in librerie diverse, possa dare output leggermente +diversi, la tolleranza accettata รฉ 1e-3 (0.001). Ricordate che i due modelli devono dare output quasi identici. Dunque, +รฉ molto conveniente comparare gli output intermedi di ๐Ÿค— Transformers molteplici volte con gli output intermedi del +modello originale di *brand_new_bert*. Di seguito vi diamo alcuni consigli per avere un ambiente di debug il piu efficiente +possibile: + +- Trovate la migliore strategia per fare debug dei risultati intermedi. Per esempio, รฉ la repository originale scritta in PyTorch? +Se si, molto probabilmente dovrete dedicare un po' di tempo per scrivere degli script piu lunghi, cosรฌ da decomporre il +modello originale in piccole sotto-componenti, in modo da poter recuperare i valori intermedi. Oppure, la repo originale +รฉ scritta in Tensorflow 1? Se รฉ cosรฌ dovrete fare affidamento ai print di Tensorflow [tf.print](https://www.tensorflow.org/api_docs/python/tf/print) +per avere i valori intermedi. Altro caso, la repo รฉ scritta in Jax? Allora assicuratevi che il modello non sia in **jit** +quanto testate il foward pass, *per esempio* controllate [questo link](https://github.com/google/jax/issues/196). +- Usate i piรน piccoli pretrained checkpoint che potete trovare. Piu piccolo รฉ il checkpoint, piu velocemente sarร  il vostro +ciclo di debug. Non รฉ efficiente avere un pretrained model cosรฌ gigante che per il forward pass impieghi piu di 10 secondi. +Nel caso in cui i checkpoints siano molto grandi, e non si possa trovare di meglio, allora รฉ buona consuetudine ricorrere +a fare un dummy model nel nuovo ambiente, con weights inizializzati random e salvare quei weights per comprare la versione ๐Ÿค— Transformers +con il vostro modello +- Accertatevi di usare la via piu semplice per chiamare il forward pass nella repo originale. Sarebbe opportuno trovare +la funzione originaria che chiami **solo** un singolo forward pass, *per esempio* questa funzione spesso viene chiamata +`predict`, `evaluate`, `forward` o `__call__`. Siate sicuri di non fare debug su una funzione che chiami `forward` molteplici +volte, *per esempio* per generare testo, come `autoregressive_sample`, `generate`. +- Cercate di separare la tokenization dal forward pass del modello. Se la repo originaria mostra esempio dove potete dare +come input una stringa, provate a cercare dove nella forward call la stringa viene cambiata in input ids e cominciate il +debug da questo punto. Questo vi garantisce un ottimo punto di partenza per scrivere un piccolo script personale dove dare +gli input al modello, anziche delle stringhe in input. +- Assicuratevi che il debugging **non** sia in training mode. Spesso questo potra il modello a dare degli output random, per +via dei molteplici dropout layers. Assicuratevi che il forward pass nell'ambiente di debug sia **deterministico**, cosicche +i dropout non siano usati. Alternativamente, potete usare *transformers.utils.set_seed* se la vecchia e nuova implementazione +sono nello stesso framework. + +La seguente sezione vi da ulteriori dettagli e accorgimenti su come potete fare tutto questo per *brand_new_bert*. + + +### 5.-14. Trasferire BrandNewBert in ๐Ÿค— Transformers + +Allora cominciamo ad aggiungere un nuovo codice in ๐Ÿค— Transformers. Andate nel vostro fork clone di ๐Ÿค— Transformers: + + +```bash +cd transformers +``` + +Nel caso speciale in cui stiate aggiungendo un modello, la cui architettura sia identica a una di un modello giร  esistente, +dovrete solo aggiugnere uno script di conversione, come descritto [qui](#write-a-conversion-script). +In questo caso, potete riutilizzare l'intera architettura del modello gia esistente. + +Se questo non รฉ il caso, cominciamo con il generare un nuovo modello. Ti consigliamo di utilizzare il seguente script per aggiungere un modello a partire da +un modello esistente: + +```bash +transformers add-new-model-like +``` + +Ti verrร  richiesto con un questionario di compilare le informazioni di base del tuo modello. + +**Aprire una Pull Request in main huggingface/transformers repo** + +Prime di cominciare ad adattare il codice automaticamente generato, aprite una nuova PR come "Work in progress (WIP)", +*per esempio* "[WIP] Aggiungere *brand_new_bert*", cosicchรฉ il team di Hugging Face possa lavorare al vostro fianco nell' +integrare il modello in ๐Ÿค— Transformers. + +Questi sarebbero gli step generali da seguire: + +1. Creare un branch dal main branch con un nome descrittivo + +```bash +git checkout -b add_brand_new_bert +``` + +2. Commit del codice automaticamente generato + +```bash +git add . +git commit +``` + +3. Fare fetch e rebase del main esistente + +```bash +git fetch upstream +git rebase upstream/main +``` + +4. Push dei cambiamenti al proprio account: + +```bash +git push -u origin a-descriptive-name-for-my-changes +``` + +5. Una volte che siete soddisfatti dei nuovi cambiamenti, andate sulla webpage del vostro fork su GitHub. Cliccate "Pull request". +Assiuratevi di aggiungere alcuni membri di Hugging Face come reviewers, nel riguardo alla destra della pagina della PR, cosicche il team +Hugging Face verrร  notificato anche per i futuri cambiamenti. + +6. Cambiare la PR a draft, cliccando su "Convert to draft" alla destra della pagina della PR + +Da quel punto in poi, ricordate di fare commit di ogni progresso e cambiamento, cosicche venga mostrato nella PR. Inoltre, +ricordatevi di tenere aggiornato il vostro lavoro con il main esistente: + +```bash +git fetch upstream +git merge upstream/main +``` + +In generale, tutte le domande che avrete riguardo al modello o l'implementazione dovranno essere fatte nella vostra PR +e discusse/risolte nella PR stessa. In questa maniera, il team di Hugging Face sarร  sempre notificato quando farete commit +di un nuovo codice o se avrete qualche domanda. ร‰ molto utile indicare al team di Hugging Face il codice a cui fate riferimento +nella domanda, cosicche il team potra facilmente capire il problema o la domanda. + +Per fare questo andate sulla tab "Files changed", dove potrete vedere tutti i vostri cambiamenti al codice, andate sulla linea +dove volete chiedere una domanda, e cliccate sul simbolo "+" per aggiungere un commento. Ogni volta che una domanda o problema +รฉ stato risolto, cliccate sul bottone "Resolve". + +In questa stessa maniera, Hugging Face aprirร  domande o commenti nel rivedere il vostro codice. Mi raccomando, chiedete piรน +domande possibili nella pagina della vostra PR. Se avete domande molto generali, non molto utili per il pubblico, siete liberi +di chiedere al team Hugging Face direttamente su slack o email. + + +**5. Adattare i codici per brand_new_bert** + +Per prima cosa, ci focalizzeremo sul modello e non sui tokenizer. Tutto il codice relative dovrebbe trovarsi in +`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` e +`src/transformers/models/brand_new_bert/configuration_brand_new_bert.py`. + +Ora potete finalmente cominciare il codice :). Il codice generato in +`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` avrร  sia la stessa architettura di BERT se รฉ un +modello encoder-only o BART se รฉ encoder-decoder. A questo punto, ricordatevi cio che avete imparato all'inizio, riguardo +agli aspetti teorici del modello: *In che maniera il modello che sto implmementando รฉ diverso da BERT o BART?*. Implementare +questi cambi spesso vuol dire cambiare il layer *self-attention*, l'ordine dei layer di normalizzazione e cosรฌ via... +Ancora una volta ripetiamo, รฉ molto utile vedere architetture simili di modelli gia esistenti in Transformers per avere +un'idea migliore su come implementare il modello. + +**Notate** che a questo punto non dovete avere subito un codice tutto corretto o pulito. Piuttosto, รฉ consigliato cominciare con un +codice poco pulito, con copia-incolla del codice originale in `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` +fino a che non avrete tutto il codice necessario. In base alla nostra esperienza, รฉ molto meglio aggiungere una prima bozza +del codice richiesto e poi correggere e migliorare iterativamente. L'unica cosa essenziale che deve funzionare qui รฉ la seguente +instanza: + +```python +from transformers import BrandNewBertModel, BrandNewBertConfig + +model = BrandNewBertModel(BrandNewBertConfig()) +``` + +Questo comando creerร  un modello con i parametri di default definiti in `BrandNewBergConfig()` e weights random. Questo garantisce +che `init()` di tutte le componenti funzioni correttamente. + + +**6. Scrivere uno script di conversione** + +Il prossimo step รฉ scrivere uno script per convertire il checkpoint che avete usato per fare debug su *brand_new_berts* nella +repo originale in un checkpoint per la nuova implementazione di *brand_new_bert* in ๐Ÿค— Transformers. Non รฉ consigliato scrivere +lo script di conversione da zero, ma piuttosto cercate e guardate script gia esistenti in ๐Ÿค— Transformers, cosรฌ da trovarne +uno simile al vostro modello. Di solito basta fare una copia di uno script gia esistente e adattarlo al vostro caso. +Non esistate a chiedre al team di Hugging Face a riguardo. + +- Se state convertendo un modello da TensorFlow a PyTorch, un ottimo inizio รฉ vedere [questo script di conversione per BERT](https://github.com/huggingface/transformers/blob/7acfa95afb8194f8f9c1f4d2c6028224dbed35a2/src/transformers/models/bert/modeling_bert.py#L91) +- Se state convertendo un modello da PyTorch a PyTorch, [lo script di conversione di BART puรฒ esservi utile](https://github.com/huggingface/transformers/blob/main/src/transformers/models/bart/convert_bart_original_pytorch_checkpoint_to_pytorch.py) + +Qui di seguito spiegheremo come i modelli PyTorch salvano i weights per ogni layer e come i nomi dei layer sono definiti. In PyTorch, +il nomde del layer รฉ definito dal nome della class attribute che date al layer. Definiamo un modello dummy in PyTorch, +chiamato `SimpleModel`: + +```python +from torch import nn + + +class SimpleModel(nn.Module): + def __init__(self): + super().__init__() + self.dense = nn.Linear(10, 10) + self.intermediate = nn.Linear(10, 10) + self.layer_norm = nn.LayerNorm(10) +``` +Ora possiamo creare un'instanza di questa definizione di modo da inizializzare a random weights: `dense`, `intermediate`, `layer_norm`. +Possiamo usare print per vedere l'architettura del modello: + +```python +model = SimpleModel() + +print(model) +``` + +Da cui si ottiene: + +``` +SimpleModel( + (dense): Linear(in_features=10, out_features=10, bias=True) + (intermediate): Linear(in_features=10, out_features=10, bias=True) + (layer_norm): LayerNorm((10,), eps=1e-05, elementwise_affine=True) +) +``` + +Si puรฒ vedere come i nomi dei layers siano definiti dal nome della class attribute in PyTorch. I valori dei weights di uno +specifico layer possono essere visualizzati: + + +```python +print(model.dense.weight.data) +``` + +ad esempio: + +``` +tensor([[-0.0818, 0.2207, -0.0749, -0.0030, 0.0045, -0.1569, -0.1598, 0.0212, + -0.2077, 0.2157], + [ 0.1044, 0.0201, 0.0990, 0.2482, 0.3116, 0.2509, 0.2866, -0.2190, + 0.2166, -0.0212], + [-0.2000, 0.1107, -0.1999, -0.3119, 0.1559, 0.0993, 0.1776, -0.1950, + -0.1023, -0.0447], + [-0.0888, -0.1092, 0.2281, 0.0336, 0.1817, -0.0115, 0.2096, 0.1415, + -0.1876, -0.2467], + [ 0.2208, -0.2352, -0.1426, -0.2636, -0.2889, -0.2061, -0.2849, -0.0465, + 0.2577, 0.0402], + [ 0.1502, 0.2465, 0.2566, 0.0693, 0.2352, -0.0530, 0.1859, -0.0604, + 0.2132, 0.1680], + [ 0.1733, -0.2407, -0.1721, 0.1484, 0.0358, -0.0633, -0.0721, -0.0090, + 0.2707, -0.2509], + [-0.1173, 0.1561, 0.2945, 0.0595, -0.1996, 0.2988, -0.0802, 0.0407, + 0.1829, -0.1568], + [-0.1164, -0.2228, -0.0403, 0.0428, 0.1339, 0.0047, 0.1967, 0.2923, + 0.0333, -0.0536], + [-0.1492, -0.1616, 0.1057, 0.1950, -0.2807, -0.2710, -0.1586, 0.0739, + 0.2220, 0.2358]]). +``` + +Nello script di conversione, dovreste riempire quei valori di inizializzazione random con gli stessi weights del corrispondente +layer nel checkpoint. *Per esempio* + +```python +# retrieve matching layer weights, e.g. by +# recursive algorithm +layer_name = "dense" +pretrained_weight = array_of_dense_layer + +model_pointer = getattr(model, "dense") + +model_pointer.weight.data = torch.from_numpy(pretrained_weight) +``` + +Cosรฌ facendo, dovete verificare che ogni inizializzazione random di un peso del modello PyTorch e il suo corrispondente peso nel pretrained checkpoint +siano esattamente gli stessi e uguali in **dimensione/shape e nome**. Per fare questo, รฉ **necessario** aggiungere un `assert` +per la dimensione/shape e nome: + +```python +assert ( + model_pointer.weight.shape == pretrained_weight.shape +), f"Pointer shape of random weight {model_pointer.shape} and array shape of checkpoint weight {pretrained_weight.shape} mismatched" +``` + +Inoltre, dovrete fare il print sia dei nomi che dei weights per essere sicuri che siano gli stessi: + +```python +logger.info(f"Initialize PyTorch weight {layer_name} from {pretrained_weight.name}") +``` + +Se la dimensione o il nome non sono uguali, probabilmente avete sbagliato ad assegnare il peso nel checkpoint o nel layer costrutture di + ๐Ÿค— Transformers. + +Una dimensione sbagliata puรฒ essere dovuta ad un errore nei parameteri in `BrandNewBertConfig()`. Tuttavia, puรฒ essere anche +che l'implementazione del layer in PyTorch richieda di fare una transposizione della matrice dei weights. + +Infine, controllate **tutti** che tutti i weights inizializzati e fate print di tutti i weights del checkpoint che non sono stati +usati per l'inizializzazione, di modo da essere sicuri che il modello sia correttamente convertito. ร‰ normale che ci siano +errori nel test di conversione, fai per un errore in `BrandNewBertConfig()`, o un errore nell'architettura in ๐Ÿค— Transformers, +o un bug in `init()`. + +Questo step dev'essere fatto tramite iterazioni fino a che non si raggiungano gli stessi valori per i weights. Una volta che +il checkpoint รฉ stato correttamente caricato in ๐Ÿค— Transformers, potete salvare il modello in una cartella di vostra scelta +`/path/to/converted/checkpoint/folder` che contenga sia +`pytorch_model.bin` che `config.json`: + +```python +model.save_pretrained("/path/to/converted/checkpoint/folder") +``` + + +**7. Implementare il forward pass** + +Una volta che i weights pretrained sono stati correttamente caricati in ๐Ÿค— Transformers, dovrete assicurarvi che il forward pass +sia correttamente implementato. [Qui](#3-4-provare-un-pretrained-checkpoint-usando-la-repo-originale), avete give creato e provato +uno script che testi il forward pass del modello usando la repo originaria. Ora dovrete fare lo stesso con uno script analogo +usando l'implementazione in ๐Ÿค— Transformers anzichรฉ l'originale. Piu o meno lo script dovrebbe essere: + +```python +model = BrandNewBertModel.from_pretrained("/path/to/converted/checkpoint/folder") +input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19] +output = model(input_ids).last_hidden_states +``` + +Di solito l'output da ๐Ÿค— Transformers non รฉ uguale uguale all'output originario, sopratto la prima volta. Non vi abbattete - +รฉ normale! Prima di tutto assicuratevi che non ci siano errori o che non vengano segnalati degli errori nella forward pass. +Spesso capita che ci siano dimensioni sbagliate o data type sbagliati, *ad esempio* `torch.long` anziche `torch.float32`. +Non esistate a chiedere al team Hugging Face! + +Nella parte finale assicuratevi che l'implementazione ๐Ÿค— Transformers funzioni correttamente cosi da testare che gli output +siano equivalenti a una precisione di `1e-3`. Controllate che `outputs.shape` siano le stesse tra ๐Ÿค— Transformers e l'implementazione +originaria. Poi, controllate che i valori in output siano identici. Questa รฉ sicuramente la parte piรน difficile, qui una serie +di errori comuni quando gli output non sono uguali: + +- Alcuni layers non sono stati aggiunti, *ad esempio* un *activation* layer non รฉ stato aggiunto, o ci si รฉ scordati di una connessione +- La matrice del word embedding non รฉ stata ripareggiata +- Ci sono degli embeddings posizionali sbagliati perchรฉ l'implementazione originaria ha un offset +- Il dropout รฉ in azione durante il forward pass. Per sistemare questo errore controllate che *model.training = False* e che +il dropout non sia stato attivato nel forward pass, * per esempio * passate *self.training* a [PyTorch's functional dropout](https://pytorch.org/docs/stable/nn.functional.html?highlight=dropout#torch.nn.functional.dropout) + +La miglior maniera per sistemare il problema รฉ di vedere all'implementazione originaria del forward pass e in ๐Ÿค— Transformers +fianco a fianco e vedere se ci sono delle differenze. In teoria, con debug e print degli output intermedie di entrambe le +implementazioni nel forward pass nell'esatta posizione del network dovrebbe aiutarvi a vedere dove ci sono differenze tra +i due frameworks. Come prima mossa controllate che `input_ids` siano identici in entrambi gli scripts. Da lรฌ andate fino +all'ultimo layer. Potrete notare una differenza tra le due implementazioni a quel punto. + +Una volta che lo stesso output รฉ stato ragguingi, verificate gli output con `torch.allclose(original_output, output, atol=1e-3)`. +A questo punto se รฉ tutto a posto: complimenti! Le parti seguenti saranno una passeggiata ๐Ÿ˜Š. + + +**8. Aggiungere i test necessari per il modello** + +A questo punto avete aggiunto con successo il vostro nuovo modello. Tuttavia, รฉ molto probabile che il modello non sia +del tutto ok con il design richiesto. Per essere sicuri che l'implementazione sia consona e compatibile con ๐Ÿค— Transformers รฉ +necessario implementare dei tests. Il Cookiecutter dovrebbe fornire automaticamente dei file per test per il vostro modello, +di solito nella folder `tests/test_modeling_brand_new_bert.py`. Provate questo per verificare l'ok nei test piu comuni: + +```bash +pytest tests/test_modeling_brand_new_bert.py +``` + +Una volta sistemati i test comuni, bisogna assicurarsi che il vostro lavoro sia correttamente testato cosicchรจ: + +- a) La community puo capire in maniera semplice il vostro lavoro controllando tests specifici del modello *brand_new_bert*, +- b) Implementazioni future del vostro modello non rompano alcune feature importante del modello. + +Per prima cosa agguingete dei test d'integrazione. Questi sono essenziali perche fanno la stessa funzione degli scripts di +debug usati precedentemente. Un template per questi tests esiste gia nel Cookiecutter ed รฉ sotto il nome di `BrandNewBertModelIntegrationTests`, +voi dovrete solo completarlo. Una volta che questi tests sono OK, provate: + +```bash +RUN_SLOW=1 pytest -sv tests/test_modeling_brand_new_bert.py::BrandNewBertModelIntegrationTests +``` + + + +Nel caso siate su Windows, sostituite `RUN_SLOW=1` con `SET RUN_SLOW=1` + + + +Di seguito, tutte le features che sono utili e necessarire per *brand_new_bert* devono essere testate in test separati, +contenuti in `BrandNewBertModelTester`/ `BrandNewBertModelTest`. spesso la gente si scorda questi test, ma ricordate che sono utili per: + + +- Aiuta gli utenti a capire il vostro codice meglio, richiamando l'attenzione su queste nuove features +- Developers e contributors futuri potranno velocemente testare nuove implementazioni del modello testanto questi casi speciali. + + +**9. Implementare il tokenizer** + +A questo punto avremo bisogno un tokenizer per *brand_new_bert*. Di solito il tokenizer รฉ uguale ad altri modelli in ๐Ÿค— Transformers. + +ร‰ importante che troviate il file con il tokenizer originale e che lo carichiate in ๐Ÿค— Transformers. + +Per controllare che il tokenizer funzioni in modo corretto, create uno script nella repo originaria che riceva come input +una stringa e ritorni gli `input_ids`. Piu o meno questo potrebbe essere il codice: + +```python +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = model.tokenize(input_str) +``` + +Potrebbe richiedere un po' di tempo, ma guardate ancora alla repo originaria per trovare la funzione corretta del tokenizer. +A volte capita di dover riscrivere il tokenizer nella repo originaria, di modo da avere come output gli `input_ids`. +A quel punto uno script analogo รฉ necessario in ๐Ÿค— Transformers: + +```python +from transformers import BrandNewBertTokenizer + +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." + +tokenizer = BrandNewBertTokenizer.from_pretrained("/path/to/tokenizer/folder/") + +input_ids = tokenizer(input_str).input_ids +``` + +Una volta che `input_ids` sono uguali, bisogna aggiungere un test per il tokenizer. + +Il file test per tokenizer di *brand_new_brand* dovrebbe avere un paio di hard-coded test d'integrazione. + + +**10. Test end-to-end** + +Ora che avete il tokenizer, dovrete aggiungere dei test d'integrazione per l'intero workflow in `tests/test_modeling_brand_new_bert.py` in ๐Ÿค— Transformer. +Questi test devono mostrare che un significante campione text-to-text funzioni come ci si aspetta nell'implementazione di ๐Ÿค— Transformers. +*Per esempio* potreste usare dei source-to-target-translation, o un sommario di un articolo, o un domanda-risposta e cosi via. +Se nessuno dei checkpoints รฉ stato ultra parametrizzato per task simili, allora i tests per il modello sono piu che sufficienti. +Nello step finale dovete assicurarvi che il modello sia totalmente funzionale, e consigliamo anche di provare a testare su GPU. +Puo succedere che ci si scordi un `.to(self.device)` ad esempio. Se non avete accesso a GPU, il team Hugging Face puo provvedere +a testare questo aspetto per voi. + +**11. Aggiungere una Docstring** + +Siete quasi alla fine! L'ultima cosa rimasta รฉ avere una bella docstring e una pagina doc. Il Cookiecutter dovrebbe provvedere giร  +un template chiamato `docs/source/model_doc/brand_new_bert.rst`, che dovrete compilare. La prima cosa che un utente farร  +per usare il vostro modello sarร  dare una bella lettura al doc. Quindi proponete una documentazione chiara e concisa. ร‰ molto +utile per la community avere anche delle *Tips* per mostrare come il modello puo' essere usato. Non esitate a chiedere a Hugging Face +riguardo alle docstirng. + +Quindi, assicuratevi che la docstring sia stata aggiunta a `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`. +Assicuratevi che la docstring sia corretta e che includa tutti i necessari input e output. Abbiamo una guida dettagliata per +scrivere la documentazione e docstring. + + +**Rifattorizzare il codice** + +Perfetto! Ora che abbiamo tutto per *brand_new_bert* controllate che lo stile del codice sia ok: + +```bash +make style +``` + +E che il codice passi i quality check: + +```bash +make quality +``` + +A volte capita che manchino delle informazioninella docstring o alcuni nomi sbagliati, questo farร  fallire i tests sopra. +Ripetiamo: chiedete pure a Hugging Face, saremo lieti di aiutarvi. + +Per ultimo, fare del refactoring del codice una volta che รฉ stato creato. + +Avete finito con il codice, congratulazioni! ๐ŸŽ‰ Siete fantasticiiiiiii! ๐Ÿ˜Ž + +**12. Caricare il modello sul model hub** + +In questa ultima parte dovrete convertire e caricare il modello, con tutti i checkpoints, nel model hub e aggiungere una +model card per ogni checkpoint caricato. Leggete la nostra guida [Model sharing and uploading Page](model_sharing) per +avere familiaritร  con l'hub. Di solito in questa parte lavorate a fianco di Hugging face per decidere un nome che sia ok +per ogni checkpoint, per ottenere i permessi necessari per caricare il modello nell'organizzazione dell'autore di *brand_new_bert*. +Il metodo `push_to_hub`, presente in tutti i modelli `transformers`, รฉ una maniera rapida e indolore per caricare il vostro checkpoint sull'hub: + +```python +brand_new_bert.push_to_hub( + repo_path_or_name="brand_new_bert", + # Uncomment the following line to push to an organization + # organization="", + commit_message="Add model", + use_temp_dir=True, +) +``` + +Vale la pena spendere un po' di tempo per creare una model card ad-hoc per ogni checkpoint. Le model cards dovrebbero +suggerire le caratteristiche specifiche del checkpoint, *per esempio* su che dataset il checkpoint รฉ stato pretrained o fine-tuned. +O che su che genere di task il modello lavoro? E anche buona pratica includere del codice su come usare il modello correttamente. + + +**13. (Opzionale) Aggiungere un notebook** + +ร‰ molto utile aggiungere un notebook, che dimostri in dettaglio come *brand_new_bert* si utilizzi per fare inferenza e/o +fine-tuned su specifiche task. Non รฉ una cosa obbligatoria da avere nella vostra PR, ma รฉ molto utile per la community. + +**14. Sottomettere la PR** + +L'ultimissimo step! Ovvero il merge della PR nel main. Di solito il team Hugging face a questo punto vi avrร  gia aiutato, +ma รฉ ok prendere un po' di tempo per pulire la descirzione e commenti nel codice. + + +### Condividete il vostro lavoro!! + +ร‰ ora tempo di prendere un po' di credito dalla communitร  per il vostro lavoro! Caricare e implementare un nuovo modello +รฉ un grandissimo contributo per Transformers e l'intera community NLP. Il codice e la conversione dei modelli pre-trained sara +sicuramente utilizzato da centinaia o migliaia di sviluppatori e ricercatori. Siate fieri e orgogliosi di condividere il vostro +traguardo con l'intera community :) + +** Avete create un altro modello che รฉ super facile da usare per tutti quanti nella community! ๐Ÿคฏ** diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_pipeline.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_pipeline.md new file mode 100644 index 0000000000000000000000000000000000000000..fcc4da1899a2b1d7ff44e9b0075fded4e271f390 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/add_new_pipeline.md @@ -0,0 +1,246 @@ + + +# Come creare una pipeline personalizzata? + +In questa guida, scopriremo come creare una pipeline personalizzata e condividerla sull' [Hub](https://hf.co/models) o aggiungerla nella libreria +Transformers. + +Innanzitutto, รจ necessario decidere gli input grezzi che la pipeline sarร  in grado di accettare. Possono essere strings, raw bytes, +dictionaries o qualsiasi cosa sia l'input desiderato piรน probabile. Cerca di mantenere questi input il piรน possibile in Python +in quanto facilita la compatibilitร  (anche con altri linguaggi tramite JSON). Questi saranno gli `inputs` della +pipeline (`preprocess`). + +Poi definire gli `outputs`. Stessa strategia degli `inputs`. Piรน รจ seplice e meglio รจ. Questi saranno gli output del metodo +`postprocess`. + +Si parte ereditando la classe base `Pipeline`. con i 4 metodi che bisogna implementare `preprocess`, +`_forward`, `postprocess` e `_sanitize_parameters`. + + +```python +from transformers import Pipeline + + +class MyPipeline(Pipeline): + def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "maybe_arg" in kwargs: + preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"] + return preprocess_kwargs, {}, {} + + def preprocess(self, inputs, maybe_arg=2): + model_input = Tensor(inputs["input_ids"]) + return {"model_input": model_input} + + def _forward(self, model_inputs): + # model_inputs == {"model_input": model_input} + outputs = self.model(**model_inputs) + # Maybe {"logits": Tensor(...)} + return outputs + + def postprocess(self, model_outputs): + best_class = model_outputs["logits"].softmax(-1) + return best_class +``` + +La struttura di questa suddivisione consiste nel supportare in modo relativamente continuo CPU/GPU, supportando allo stesso tempo l'esecuzione di +pre/postelaborazione sulla CPU su thread diversi. + +`preprocess` prenderร  gli input originariamente definiti e li trasformerร  in qualcosa di alimentabile dal modello. Potrebbe +contenere piรน informazioni e di solito รจ un `Dict`. + +`_forward` รจ il dettaglio dell'implementazione e non รจ destinato a essere chiamato direttamente. `forward` รจ il metodo preferito per assicurarsi che tutto funzioni correttamente perchรจ contiene delle slavaguardie. Se qualcosa รจ +รจ collegato a un modello reale, appartiene al metodo `_forward`, tutto il resto รจ nel preprocess/postprocess. + +`postprocess` prende l'otput di `_forward` e lo trasforma nell'output finale che era stato deciso in precedenza. + +`_sanitize_parameters` esiste per consentire agli utenti di passare i parametri ogni volta che desiderano sia a inizialization time `pipeline(...., maybe_arg=4)` che al call time `pipe = pipeline(...); output = pipe(...., maybe_arg=4)`. + +`_sanitize_parameters` ritorna 3 dicts di kwargs che vengono passati direttamente a `preprocess`, +`_forward` e `postprocess`. Non riempire nulla se il chiamante non ha chiamato con alcun parametro aggiuntivo. Questo +consente di mantenere gli argomenti predefiniti nella definizione della funzione, che รจ sempre piรน "naturale". + +Un esempio classico potrebbe essere l'argomento `top_k` nel post processing dei classification tasks. + +```python +>>> pipe = pipeline("my-new-task") +>>> pipe("This is a test") +[{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}, {"label": "3-star", "score": 0.05} +{"label": "4-star", "score": 0.025}, {"label": "5-star", "score": 0.025}] + +>>> pipe("This is a test", top_k=2) +[{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}] +``` + +In order to achieve that, we'll update our `postprocess` method with a default parameter to `5`. and edit +`_sanitize_parameters` to allow this new parameter. + + +```python +def postprocess(self, model_outputs, top_k=5): + best_class = model_outputs["logits"].softmax(-1) + # Add logic to handle top_k + return best_class + + +def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "maybe_arg" in kwargs: + preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"] + + postprocess_kwargs = {} + if "top_k" in kwargs: + postprocess_kwargs["top_k"] = kwargs["top_k"] + return preprocess_kwargs, {}, postprocess_kwargs +``` + +Cercare di mantenere gli input/output molto semplici e idealmente serializzabili in JSON, in quanto ciรฒ rende l'uso della pipeline molto facile +senza richiedere agli utenti di comprendere nuovi tipi di oggetti. รˆ anche relativamente comune supportare molti tipi di argomenti +per facilitarne l'uso (ad esempio file audio, possono essere nomi di file, URL o byte puri). + +## Aggiungilo alla lista dei tasks supportati + +Per registrar il tuo `new-task` alla lista dei tasks supportati, devi aggiungerlo al `PIPELINE_REGISTRY`: + +```python +from transformers.pipelines import PIPELINE_REGISTRY + +PIPELINE_REGISTRY.register_pipeline( + "new-task", + pipeline_class=MyPipeline, + pt_model=AutoModelForSequenceClassification, +) +``` + +Puoi specificare il modello di default che desideri, in questo caso dovrebbe essere accompagnato da una revisione specifica (che puรฒ essere il nome di un branch o l'hash di un commit, in questo caso abbiamo preso `"abcdef"`) e anche dal type: + +```python +PIPELINE_REGISTRY.register_pipeline( + "new-task", + pipeline_class=MyPipeline, + pt_model=AutoModelForSequenceClassification, + default={"pt": ("user/awesome_model", "abcdef")}, + type="text", # current support type: text, audio, image, multimodal +) +``` + +## Condividi la tua pipeline sull'Hub + +Per condividere la tua pipeline personalizzata sull'Hub, devi solo salvare il codice della tua sottoclasse `Pipeline` in un file +python. Per esempio, supponiamo di voler utilizzare una pipeline personalizzata per la classificazione delle coppie di frasi come la seguente: + +```py +import numpy as np + +from transformers import Pipeline + + +def softmax(outputs): + maxes = np.max(outputs, axis=-1, keepdims=True) + shifted_exp = np.exp(outputs - maxes) + return shifted_exp / shifted_exp.sum(axis=-1, keepdims=True) + + +class PairClassificationPipeline(Pipeline): + def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "second_text" in kwargs: + preprocess_kwargs["second_text"] = kwargs["second_text"] + return preprocess_kwargs, {}, {} + + def preprocess(self, text, second_text=None): + return self.tokenizer(text, text_pair=second_text, return_tensors=self.framework) + + def _forward(self, model_inputs): + return self.model(**model_inputs) + + def postprocess(self, model_outputs): + logits = model_outputs.logits[0].numpy() + probabilities = softmax(logits) + + best_class = np.argmax(probabilities) + label = self.model.config.id2label[best_class] + score = probabilities[best_class].item() + logits = logits.tolist() + return {"label": label, "score": score, "logits": logits} +``` + +L'implementazione รจ agnostica al framework, e lavorerร  sia con modelli PyTorch che con TensorFlow. Se l'abbiamo salvato in un file chiamato `pair_classification.py`, puรฒ essere successivamente importato e registrato in questo modo: + +```py +from pair_classification import PairClassificationPipeline +from transformers.pipelines import PIPELINE_REGISTRY +from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification + +PIPELINE_REGISTRY.register_pipeline( + "pair-classification", + pipeline_class=PairClassificationPipeline, + pt_model=AutoModelForSequenceClassification, + tf_model=TFAutoModelForSequenceClassification, +) +``` + +Una volta fatto, possiamo usarla con un modello pretrained. L'istanza `sgugger/finetuned-bert-mrpc` รจ stata +fine-tuned sul dataset MRPC, che classifica le coppie di frasi come parafrasi o no. + +```py +from transformers import pipeline + +classifier = pipeline("pair-classification", model="sgugger/finetuned-bert-mrpc") +``` + +Successivamente possiamo condividerlo sull'Hub usando il metodo `push_to_hub` + +```py +classifier.push_to_hub("test-dynamic-pipeline") +``` + +Questo codice copierร  il file dove รจ stato definitp `PairClassificationPipeline` all'interno della cartella `"test-dynamic-pipeline"`, +insieme al salvataggio del modello e del tokenizer della pipeline, prima di pushare il tutto nel repository +`{your_username}/test-dynamic-pipeline`. Dopodichรฉ chiunque potrร  utilizzarlo, purchรฉ fornisca l'opzione +`trust_remote_code=True`: + +```py +from transformers import pipeline + +classifier = pipeline(model="{your_username}/test-dynamic-pipeline", trust_remote_code=True) +``` + +## Aggiungere la pipeline a Transformers + +Se vuoi contribuire con la tua pipeline a Transformers, dovrai aggiungere un modulo nel sottomodulo `pipelines` +con il codice della tua pipeline, quindi aggiungilo all'elenco dei tasks definiti in `pipelines/__init__.py`. + +Poi hai bisogno di aggiungere i test. Crea un nuovo file `tests/test_pipelines_MY_PIPELINE.py` con esempi ed altri test. + +La funzione `run_pipeline_test` sarร  molto generica e su piccoli modelli casuali su ogni possibile +architettura, come definito da `model_mapping` e `tf_model_mapping`. + +Questo รจ molto importante per testare la compatibilitร  futura, nel senso che se qualcuno aggiunge un nuovo modello di +`XXXForQuestionAnswering` allora il test della pipeline tenterร  di essere eseguito su di esso. Poichรฉ i modelli sono casuali, รจ +รจ impossibile controllare i valori effettivi, per questo esiste un aiuto `ANY` che tenterร  solamente di far corrispondere l'output della pipeline TYPE. + +Hai anche *bisogno* di implementare 2 (idealmente 4) test. + +- `test_small_model_pt` : Definire 1 piccolo modello per questa pipeline (non importa se i risultati non hanno senso) + e testare i risultati della pipeline. I risultati dovrebbero essere gli stessi di `test_small_model_tf`. +- `test_small_model_tf` : Definire 1 piccolo modello per questa pipeline (non importa se i risultati non hanno senso) + e testare i risultati della pipeline. I risultati dovrebbero essere gli stessi di `test_small_model_pt`. +- `test_large_model_pt` (`optional`): Testare la pipeline su una pipeline reale in cui i risultati dovrebbero avere + senso. Questi test sono lenti e dovrebbero essere contrassegnati come tali. In questo caso l'obiettivo รจ mostrare la pipeline e assicurarsi che non ci siano derive nelle versioni future +- `test_large_model_tf` (`optional`): Testare la pipeline su una pipeline reale in cui i risultati dovrebbero avere + senso. Questi test sono lenti e dovrebbero essere contrassegnati come tali. In questo caso l'obiettivo รจ mostrare la pipeline e assicurarsi + che non ci siano derive nelle versioni future \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/autoclass_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/autoclass_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..edb96528e705eacebdf5281e35f7ce620bd3d8bd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/autoclass_tutorial.md @@ -0,0 +1,123 @@ + + +# Carica istanze pre-allenate con AutoClass + +Con cosรฌ tante architetture Transformer differenti, puรฒ essere sfidante crearne una per il tuo checkpoint. Come parte della filosofia centrale di ๐Ÿค— Transformers per rendere la libreria facile, semplice e flessibile da utilizzare, una `AutoClass` inferisce e carica automaticamente l'architettura corretta da un dato checkpoint. Il metodo `from_pretrained` ti permette di caricare velocemente un modello pre-allenato per qualsiasi architettura, cosรฌ non devi utilizzare tempo e risorse per allenare un modello da zero. Produrre questo codice agnostico ai checkpoint significa che se il tuo codice funziona per un checkpoint, funzionerร  anche per un altro checkpoint, purchรฉ sia stato allenato per un compito simile, anche se l'architettura รจ differente. + + + +Ricorda, con architettura ci si riferisce allo scheletro del modello e con checkpoint ai pesi di una determinata architettura. Per esempio, [BERT](https://huggingface.co/google-bert/bert-base-uncased) รจ un'architettura, mentre `google-bert/bert-base-uncased` รจ un checkpoint. Modello รจ un termine generale che puรฒ significare sia architettura che checkpoint. + + + +In questo tutorial, imparerai a: + +* Caricare un tokenizer pre-allenato. +* Caricare un estrattore di caratteristiche (feature extractor, in inglese) pre-allenato. +* Caricare un processore pre-allenato. +* Caricare un modello pre-allenato. + +## AutoTokenizer + +Quasi tutti i compiti di NLP iniziano con un tokenizer. Un tokenizer converte il tuo input in un formato che possa essere elaborato dal modello. + +Carica un tokenizer con [`AutoTokenizer.from_pretrained`]: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("FacebookAI/xlm-roberta-base") +``` + +Poi tokenizza il tuo input come mostrato in seguito: + +```py +>>> sequenza = "In un buco nel terreno viveva uno Hobbit." +>>> print(tokenizer(sequenza)) +{'input_ids': [0, 360, 51, 373, 587, 1718, 54644, 22597, 330, 3269, 2291, 22155, 18, 5, 2], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +## AutoFeatureExtractor + +Per compiti inerenti a audio e video, un feature extractor processa il segnale audio o l'immagine nel formato di input corretto. + +Carica un feature extractor con [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained( +... "ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" +... ) +``` + +## AutoProcessor + +Compiti multimodali richiedono un processore che combini i due tipi di strumenti di elaborazione. Per esempio, il modello [LayoutLMV2](model_doc/layoutlmv2) richiede un feature extractor per gestire le immagine e un tokenizer per gestire il testo; un processore li combina entrambi. + +Carica un processore con [`AutoProcessor.from_pretrained`]: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") +``` + +## AutoModel + + + +Infine, le classi `AutoModelFor` ti permettono di caricare un modello pre-allenato per un determinato compito (guarda [qui](model_doc/auto) per una lista completa di compiti presenti). Per esempio, carica un modello per la classificazione di sequenze con [`AutoModelForSequenceClassification.from_pretrained`]: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Semplicemente utilizza lo stesso checkpoint per caricare un'architettura per un task differente: + +```py +>>> from transformers import AutoModelForTokenClassification + +>>> model = AutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Generalmente, raccomandiamo di utilizzare la classe `AutoTokenizer` e la classe `AutoModelFor` per caricare istanze pre-allenate dei modelli. Questo ti assicurerร  di aver caricato la corretta architettura ogni volta. Nel prossimo [tutorial](preprocessing), imparerai come utilizzare il tokenizer, il feature extractor e il processore per elaborare un dataset per il fine-tuning. + + + +Infine, le classi `TFAutoModelFor` ti permettono di caricare un modello pre-allenato per un determinato compito (guarda [qui](model_doc/auto) per una lista completa di compiti presenti). Per esempio, carica un modello per la classificazione di sequenze con [`TFAutoModelForSequenceClassification.from_pretrained`]: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Semplicemente utilizza lo stesso checkpoint per caricare un'architettura per un task differente: + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Generalmente, raccomandiamo di utilizzare la classe `AutoTokenizer` e la classe `TFAutoModelFor` per caricare istanze pre-allenate dei modelli. Questo ti assicurerร  di aver caricato la corretta architettura ogni volta. Nel prossimo [tutorial](preprocessing), imparerai come utilizzare il tokenizer, il feature extractor e il processore per elaborare un dataset per il fine-tuning. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/big_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/big_models.md new file mode 100644 index 0000000000000000000000000000000000000000..6a5c346dec890fb29b2f91a22dfe609229d8b1b8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/big_models.md @@ -0,0 +1,123 @@ + + +# Istanziare un big model + +Quando vuoi utilizzare un modello preaddestrato (pretrained) molto grande, una sfida รจ minimizzare l'uso della RAM. Il workflow classico +in PyTorch รจ: + +1. Crea il tuo modello con pesi casuali (random weights). +2. Carica i tuoi pesi preaddestrati. +3. Inserisci i pesi preaddestrati nel tuo modello casuale. + +I passi 1 e 2 una versione completa del modello in memoria, in molti casi non รจ un problema, ma se il modello inizia a pesare diversi GigaBytes, queste due copie possono sturare la nostra RAM. Ancora peggio, se stai usando `torch.distributed` per seguire l'addestramento (training) in distribuito, ogni processo caricherร  il modello preaddestrato e memorizzerร  queste due copie nella RAM. + + + +Nota che il modello creato casualmente รจ inizializzato con tensori "vuoti", che occupano spazio in memoria ma senza riempirlo (quindi i valori casuali sono quelli che si trovavano in questa porzione di memoria in un determinato momento). L'inizializzazione casuale che segue la distribuzione appropriata per il tipo di modello/parametri istanziato (come la distribuzione normale per le istanze) รจ eseguito solo dopo il passaggio 3 sui pesi non inizializzati, per essere piรน rapido possibile! + + + +In questa guida, esploreremo le soluzioni che Transformers offre per affrontare questo problema. C'รจ da tenere in conto che questa รจ un'area in cui si sta attualmente sviluppando, quindi le API spiegate qui possono variare velocemente in futuro. + +## Checkpoints condivisi + +Dalla versione 4.18.0, i checkpoints dei modelli che occupano piรน di 10GB di spazio vengono automaticamente frammentati in piรน parti. Per quanto riguarda la possibilitร  di avere un unico checkpoint quando si utilizza `model.save_pretrained(save_dir)`, si hanno diversi checkpoint parziali (ognuno con dimensione < 10GB) e un indice che mappa i nomi dei parametri ai file in cui sono memorizzati. + +Puoi controllare la dimensione massima dopo la frammentazione con il parametro `max_shard_size`, nel prossimo esempio, useremo modelli di dimensioni normali con frammenti di piccoli dimensioni: prendiamo un modello BERT classico. + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained("google-bert/bert-base-cased") +``` + +Se tu salvi usando [`~PreTrainedModel.save_pretrained`], avrai una nuova cartella con due file: il config del modello e i suoi pesi: + +```py +>>> import os +>>> import tempfile + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir) +... print(sorted(os.listdir(tmp_dir))) +['config.json', 'pytorch_model.bin'] +``` + +Adesso usiamo una dimensione massima di frammentazione di 200MB: + +```py +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... print(sorted(os.listdir(tmp_dir))) +['config.json', 'pytorch_model-00001-of-00003.bin', 'pytorch_model-00002-of-00003.bin', 'pytorch_model-00003-of-00003.bin', 'pytorch_model.bin.index.json'] +``` + +In aggiunta alla configurazione del modello, vediamo tre differenti file dei pesi, e un file `index.json` che รจ il nostro indice. Un checkpoint puรฒ essere ricaricato totalmente usando il metodo [`~PreTrainedModel.from_pretrained`]: + +```py +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... new_model = AutoModel.from_pretrained(tmp_dir) +``` + +Il vantaggio principale di applicare questo metodo per modelli grandi รจ che durante il passo 2 del workflow illustrato in precedenza, ogni frammento del checkpoint viene caricato dopo il precedente, limitando l'utilizzo della RAM alla dimensione del modello piรน la dimensione del frammento piรน grande. + +Dietro le quinte, il file indice รจ utilizzato per determinare quali chiavi sono nel checkpoint, e dove i corrispondenti pesi sono memorizzati. Possiamo caricare l'indice come un qualsiasi json e ottenere un dizionario: + +```py +>>> import json + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... with open(os.path.join(tmp_dir, "pytorch_model.bin.index.json"), "r") as f: +... index = json.load(f) + +>>> print(index.keys()) +dict_keys(['metadata', 'weight_map']) +``` + +I metadati consistono solo nella dimensione totale del modello per ora. Abbiamo in programma di aggiungere altre informazioni in futuro: + +```py +>>> index["metadata"] +{'total_size': 433245184} +``` + +La mappa dei pesi รจ la parte principale di questo indice, che mappa ogni nome dei parametri (si trova solitamente nei modelli PyTorch come `state_dict`) al file in cui รจ memorizzato: + +```py +>>> index["weight_map"] +{'embeddings.LayerNorm.bias': 'pytorch_model-00001-of-00003.bin', + 'embeddings.LayerNorm.weight': 'pytorch_model-00001-of-00003.bin', + ... +``` + +Se vuoi caricare direttamente un checkpoint frammentato in un modello senza usare [`~PreTrainedModel.from_pretrained`] (come si farebbe con `model.load_state_dict()` per un checkpoint completo) devi usare [`~modeling_utils.load_sharded_checkpoint`]: + +```py +>>> from transformers.modeling_utils import load_sharded_checkpoint + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... load_sharded_checkpoint(model, tmp_dir) +``` + +## Caricamento low memory + +Frammentare i checkpoint l'utilizzo di memoria al passo 2 del workflow citato in precedenza, ma per utilizzare questo modello in un ambiente con poca memoria, consigliamo di utilizzare i nostri strumenti basati sulla libreria Accelerate. + +Per ulteriori informazioni, leggere la seguente guida: [Large model loading using Accelerate](./main_classes/model#large-model-loading) \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/community.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/community.md new file mode 100644 index 0000000000000000000000000000000000000000..92f6698a9a89bb8243ea84e3ab5698d2e8374f90 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/community.md @@ -0,0 +1,68 @@ + + +# Comunitร  + +Questa pagina raggruppa le risorse sviluppate dalla comunitร  riguardo ๐Ÿค— Transformers. + +## Risorse della comunitร : + +| Risorsa | Descrizione | Autore | +|:----------|:-------------|------:| +| [Glossario delle Flashcards di Transformers](https://www.darigovresearch.com/huggingface-transformers-glossary-flashcards) | Un insieme di flashcards basate sul [glossario della documentazione di Transformers](glossary), creato in un formato tale da permettere un facile apprendimento e revisione usando [Anki](https://apps.ankiweb.net/), un'applicazione open-source e multi-piattaforma, specificatamente progettata per ricordare informazioni nel lungo termine. Guarda questo [video introduttivo su come usare le flashcards](https://www.youtube.com/watch?v=Dji_h7PILrw). | [Darigov Research](https://www.darigovresearch.com/) | + +## Notebook della comunitร : + +| Notebook | Descrizione | Autore | | +|:----------|:-------------|:-------------|------:| +| [Fine-tuning di un Transformer pre-addestrato, al fine di generare testi di canzoni](https://github.com/AlekseyKorshuk/huggingartists) | Come generare testi di canzoni nello stile del vostro artista preferito attraverso il fine-tuning di un modello GPT-2. | [Aleksey Korshuk](https://github.com/AlekseyKorshuk) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AlekseyKorshuk/huggingartists/blob/master/huggingartists-demo.ipynb) | +| [Addestramento di T5 in Tensorflow 2](https://github.com/snapthat/TF-T5-text-to-text) | Come addestrare T5 per qualsiasi attivitร  usando Tensorflow 2. Questo notebook mostra come risolvere l'attivitร  di "Question Answering" usando Tensorflow 2 e SQUAD. | [Muhammad Harris](https://github.com/HarrisDePerceptron) |[![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/snapthat/TF-T5-text-to-text/blob/master/snapthatT5/notebooks/TF-T5-Datasets%20Training.ipynb) | +| [Addestramento di T5 con TPU](https://github.com/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb) | Come addestrare T5 su SQUAD con Transformers e NLP. | [Suraj Patil](https://github.com/patil-suraj) |[![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb#scrollTo=QLGiFCDqvuil) | +| [Fine-tuning di T5 per la classificazione e scelta multipla](https://github.com/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | Come effettuare il fine-tuning di T5 per le attivitร  di classificazione a scelta multipla - usando un formato testo-a-testo - con PyTorch Lightning. | [Suraj Patil](https://github.com/patil-suraj) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | +| [Fine-tuning di DialoGPT su nuovi dataset e lingue](https://github.com/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | Come effettuare il fine-tuning di un modello DialoGPT su un nuovo dataset per chatbots conversazionali open-dialog. | [Nathan Cooper](https://github.com/ncoop57) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | +| [Modellamento di una lunga sequenza con Reformer](https://github.com/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | Come addestrare su sequenze di lunghezza fino a 500 mila token con Reformer. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | +| [Fine-tuning di BART per riassumere testi](https://github.com/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb) | Come effettuare il fine-tuning di BART per riassumere testi con fastai usando blurr. | [Wayde Gilliam](https://ohmeow.com/) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb) | +| [Fine-tuning di un Transformer pre-addestrato su tweet](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | Come generare tweet nello stile del tuo account Twitter preferito attraverso il fine-tuning di un modello GPT-2. | [Boris Dayma](https://github.com/borisdayma) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | +| [Ottimizzazione di modelli ๐Ÿค— Hugging Face con Weights & Biases](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | Un tutorial completo che mostra l'integrazione di W&B con Hugging Face. | [Boris Dayma](https://github.com/borisdayma) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | +| [Longformer pre-addestrato](https://github.com/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | Come costruire una versione "long" degli esistenti modelli pre-addestrati. | [Iz Beltagy](https://beltagy.net) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | +| [Fine-tuning di Longformer per QA](https://github.com/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | Come effettuare il fine-tuning di un modello longformer per un task di QA.| [Suraj Patil](https://github.com/patil-suraj) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | +| [Valutazione di modelli con ๐Ÿค—NLP](https://github.com/patrickvonplaten/notebooks/blob/master/How_to_evaluate_Longformer_on_TriviaQA_using_NLP.ipynb) | Come valutare longformer su TriviaQA con `NLP`. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1m7eTGlPmLRgoPkkA7rkhQdZ9ydpmsdLE?usp=sharing) | +| [Fine-tuning di T5 per Sentiment Span Extraction](https://github.com/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | Come effettuare il fine-tuning di T5 per la sentiment span extraction - usando un formato testo-a-testo - con PyTorch Lightning. | [Lorenzo Ampil](https://github.com/enzoampil) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | +| [Fine-tuning di DistilBert per la classificazione multi-classe](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb) | Come effettuare il fine-tuning di DistilBert per la classificazione multi-classe con PyTorch. | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb)| +|[Fine-tuning di BERT per la classificazione multi-etichetta](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)|Come effettuare il fine-tuning di BERT per la classificazione multi-etichetta con PyTorch. |[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)| +|[Accelerazione del fine-tuning con il Dynamic Padding / Bucketing](https://github.com/ELS-RD/transformers-notebook/blob/master/Divide_Hugging_Face_Transformers_training_time_by_2_or_more.ipynb)| Come velocizzare il fine-tuning di un fattore 2X usando il dynamic padding / bucketing. |[Michael Benesty](https://github.com/pommedeterresautee) |[![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1CBfRU1zbfu7-ijiOqAAQUA-RJaxfcJoO?usp=sharing)| +|[Pre-addestramento di Reformer per Masked Language Modeling](https://github.com/patrickvonplaten/notebooks/blob/master/Reformer_For_Masked_LM.ipynb)| Come addestrare un modello Reformer usando livelli di self-attention bi-direzionali.| [Patrick von Platen](https://github.com/patrickvonplaten) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1tzzh0i8PgDQGV3SMFUGxM7_gGae3K-uW?usp=sharing)| +|[Espansione e fine-tuning di Sci-BERT](https://github.com/lordtt13/word-embeddings/blob/master/COVID-19%20Research%20Data/COVID-SciBERT.ipynb)| Come incrementare il vocabolario di un modello SciBERT - pre-addestrato da AllenAI sul dataset CORD - e crearne una pipeline. | [Tanmay Thakur](https://github.com/lordtt13) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1rqAR40goxbAfez1xvF3hBJphSCsvXmh8)| +|[Fine-tuning di BlenderBotSmall per riassumere testi usando Trainer API](https://github.com/lordtt13/transformers-experiments/blob/master/Custom%20Tasks/fine-tune-blenderbot_small-for-summarization.ipynb)| Come effettuare il fine-tuning di BlenderBotSmall per riassumere testi su un dataset personalizzato, usando Trainer API. | [Tanmay Thakur](https://github.com/lordtt13) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19Wmupuls7mykSGyRN_Qo6lPQhgp56ymq?usp=sharing)| +|[Fine-tuning di Electra e interpretazione con Integrated Gradients](https://github.com/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb) | Come effettuare il fine-tuning di Electra per l'analisi dei sentimenti e intepretare le predizioni con Captum Integrated Gradients. | [Eliza Szczechla](https://elsanns.github.io) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb)| +|[Fine-tuning di un modello GPT-2 non inglese con la classe Trainer](https://github.com/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb) | Come effettuare il fine-tuning di un modello GPT-2 non inglese con la classe Trainer. | [Philipp Schmid](https://www.philschmid.de) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb)| +|[Fine-tuning di un modello DistilBERT per la classficazione multi-etichetta](https://github.com/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb) | Come effettuare il fine-tuning di un modello DistilBERT per l'attivitร  di classificazione multi-etichetta. | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb)| +|[Fine-tuning di ALBERT per la classifcazione di coppie di frasi](https://github.com/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb) | Come effettuare il fine-tuning di un modello ALBERT - o un altro modello BERT-based - per l'attivitร  di classificazione di coppie di frasi. | [Nadir El Manouzi](https://github.com/NadirEM) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb)| +|[Fine-tuning di Roberta per l'analisi di sentimenti](https://github.com/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb) | Come effettuare il fine-tuning di un modello Roberta per l'analisi di sentimenti. | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb)| +|[Valutazione di modelli che generano domande](https://github.com/flexudy-pipe/qugeev) | Quanto sono accurante le risposte alle domande generate dal tuo modello transformer seq2seq? | [Pascal Zoleko](https://github.com/zolekode) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bpsSqCQU-iw_5nNoRm_crPq6FRuJthq_?usp=sharing)| +|[Classificazione di testo con DistilBERT e Tensorflow](https://github.com/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb) | Come effettuare il fine-tuning di DistilBERT per la classificazione di testo in TensorFlow. | [Peter Bayerle](https://github.com/peterbayerle) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb)| +|[Utilizzo di BERT per riassumere testi con un modello Encoder-Decoder su CNN/Dailymail](https://github.com/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb) | Come avviare "a caldo" un *EncoderDecoderModel* attraverso l'utilizzo di un checkpoint *google-bert/bert-base-uncased* per riassumere testi su CNN/Dailymail. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Aprilo in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)| +|[Utilizzo di RoBERTa per riassumere testi con un modello Encoder-Decoder su BBC XSum](https://github.com/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb) | Come avviare "a caldo" un *EncoderDecoderModel* (condiviso) attraverso l'utilizzo di un checkpoint *FacebookAI/roberta-base* per riassumere testi su BBC/XSum. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb)| +|[Fine-tuning di TAPAS su Sequential Question Answering (SQA)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) | Come effettuare il fine-tuning di un modello *TapasForQuestionAnswering* attraverso l'utilizzo di un checkpoint *tapas-base* sul dataset Sequential Question Answering (SQA). | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb)| +|[Valutazione di TAPAS su Table Fact Checking (TabFact)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb) | Come valutare un modello *TapasForSequenceClassification* - fine-tuned con un checkpoint *tapas-base-finetuned-tabfact* - usando una combinazione delle librerie ๐Ÿค— datasets e ๐Ÿค— transformers. | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb)| +|[Fine-tuning di mBART per la traduzione](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb) | Come effettuare il fine-tuning di mBART usando Seq2SeqTrainer per la traduzione da hindi a inglese.| [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)| +|[Fine-tuning di LayoutLM su FUNSD (un dataset per la comprensione della forma)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb) | Come effettuare il fine-tuning di un modello *LayoutLMForTokenClassification* sul dataset FUNSD per l'estrazione di informazioni da documenti scannerizzati.| [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb)| +|[Fine-tuning di DistilGPT2 e generazione di testo](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb) | Come effettuare il fine-tuning di DistilGPT2 e generare testo. | [Aakash Tripathi](https://github.com/tripathiaakash) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb)| +|[Fine-tuning di LED fino a 8 mila token](https://github.com/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb) | Come effettuare il fine-tuning di LED su PubMed per riassumere "lunghi" testi. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb)| +|[Valutazione di LED su Arxiv](https://github.com/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb) | Come valutare efficacemente LED sull'attivitร  di riassumere "lunghi" testi. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb)| +|[Fine-tuning di LayoutLM su RVL-CDIP, un dataset per la classificazione di documenti (immagini)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb) | Come effettuare il fine-tuning di un modello *LayoutLMForSequenceClassification* sul dataset RVL-CDIP per la classificazione di documenti scannerizzati. | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb)| +|[Decodifica Wav2Vec2 CTC con variazioni di GPT2](https://github.com/voidful/huggingface_notebook/blob/main/xlsr_gpt.ipynb) | Come decodificare sequenze CTC, variate da modelli di linguaggio. | [Eric Lam](https://github.com/voidful) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing) +|[Fine-tuning di BART per riassumere testi in due lingue con la classe Trainer](https://github.com/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb) | Come effettuare il fine-tuning di BART per riassumere testi in due lingue usando la classe Trainer. | [Eliza Szczechla](https://github.com/elsanns) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)| +|[Valutazione di Big Bird su Trivia QA](https://github.com/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb) | Come valutare BigBird su question answering di "lunghi" documenti attraverso Trivia QA. | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb)| +| [Creazione di sottotitoli per video usando Wav2Vec2](https://github.com/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | Come creare sottotitoli per qualsiasi video di YouTube trascrivendo l'audio con Wav2Vec. | [Niklas Muennighoff](https://github.com/Muennighoff) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | +| [Fine-tuning di Vision Transformer su CIFAR-10 usando PyTorch Lightning](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | Come effettuare il fine-tuning di Vision Transformer (ViT) su CIFAR-10 usando HuggingFace Transformers, Datasets e PyTorch Lightning.| [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | +| [Fine-tuning di Vision Transformer su CIFAR-10 usando ๐Ÿค— Trainer](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | Come effettuare il fine-tuning di Vision Transformer (ViT) su CIFAR-10 usando HuggingFace Transformers, Datasets e ๐Ÿค— Trainer. | [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | +| [Valutazione di LUKE su Open Entity, un dataset di entity typing](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | Come valutare un modello *LukeForEntityClassification* sul dataset Open Entity. | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | +| [Valutazione di LUKE su TACRED, un dataset per l'estrazione di relazioni](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | Come valutare un modello *LukeForEntityPairClassification* sul dataset TACRED. | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | +| [Valutazione di LUKE su CoNLL-2003, un importante benchmark NER](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | Come valutare un modello *LukeForEntitySpanClassification* sul dataset CoNLL-2003. | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | +| [Valutazione di BigBird-Pegasus su dataset PubMed](https://github.com/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | Come valutare un modello *BigBirdPegasusForConditionalGeneration* su dataset PubMed. | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | +| [Classificazione di emozioni dal discorso con Wav2Vec2](https://github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | Come utilizzare un modello pre-addestrato Wav2Vec2 per la classificazione di emozioni sul dataset MEGA. | [Mehrdad Farahani](https://github.com/m3hrdadfi) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | +| [Rilevamento oggetti in un'immagine con DETR](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | Come usare un modello addestrato *DetrForObjectDetection* per rilevare oggetti in un'immagine e visualizzare l'attention. | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | +| [Fine-tuning di DETR su un dataset personalizzato per rilevare oggetti](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | Come effettuare fine-tuning di un modello *DetrForObjectDetection* su un dataset personalizzato per rilevare oggetti. | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | +| [Fine-tuning di T5 per Named Entity Recognition](https://github.com/ToluClassics/Notebooks/blob/main/T5_Ner_Finetuning.ipynb) | Come effettuare fine-tunining di *T5* per un'attivitร  di Named Entity Recognition. | [Ogundepo Odunayo](https://github.com/ToluClassics) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1obr78FY_cBmWY5ODViCmzdY6O1KB65Vc?usp=sharing) | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/converting_tensorflow_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/converting_tensorflow_models.md new file mode 100644 index 0000000000000000000000000000000000000000..dace244fa6dd069aab5e61432aa9c3387f0408e4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/converting_tensorflow_models.md @@ -0,0 +1,144 @@ + + +# Convertire checkpoint di Tensorflow + +รˆ disponibile un'interfaccia a linea di comando per convertire gli originali checkpoint di Bert/GPT/GPT-2/Transformer-XL/XLNet/XLM +in modelli che possono essere caricati utilizzando i metodi `from_pretrained` della libreria. + + + +A partire dalla versione 2.3.0 lo script di conversione รจ parte di transformers CLI (**transformers**), disponibile in ogni installazione +di transformers >=2.3.0. + +La seguente documentazione riflette il formato dei comandi di **transformers convert**. + + + +## BERT + +Puoi convertire qualunque checkpoint Tensorflow di BERT (in particolare +[i modeli pre-allenati rilasciati da Google](https://github.com/google-research/bert#pre-trained-models)) +in un file di salvataggio Pytorch utilizzando lo script +[convert_bert_original_tf_checkpoint_to_pytorch.py](https://github.com/huggingface/transformers/tree/main/src/transformers/models/bert/convert_bert_original_tf_checkpoint_to_pytorch.py). + +Questo CLI prende come input un checkpoint di Tensorflow (tre files che iniziano con `bert_model.ckpt`) ed il relativo +file di configurazione (`bert_config.json`), crea un modello Pytorch per questa configurazione, carica i pesi dal +checkpoint di Tensorflow nel modello di Pytorch e salva il modello che ne risulta in un file di salvataggio standard di Pytorch che +puรฒ essere importato utilizzando `from_pretrained()` (vedi l'esempio nel +[quicktour](quicktour) , [run_glue.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification/run_glue.py) ). + +Devi soltanto lanciare questo script di conversione **una volta** per ottenere un modello Pytorch. Dopodichรจ, potrai tralasciare +il checkpoint di Tensorflow (i tre files che iniziano con `bert_model.ckpt`), ma assicurati di tenere il file di configurazione +(`bert_config.json`) ed il file di vocabolario (`vocab.txt`) in quanto queste componenti sono necessarie anche per il modello di Pytorch. + +Per lanciare questo specifico script di conversione avrai bisogno di un'installazione di Tensorflow e di Pytorch +(`pip install tensorflow`). Il resto della repository richiede soltanto Pytorch. + +Questo รจ un esempio del processo di conversione per un modello `BERT-Base Uncased` pre-allenato: + +```bash +export BERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12 +transformers convert --model_type bert \ + --tf_checkpoint $BERT_BASE_DIR/bert_model.ckpt \ + --config $BERT_BASE_DIR/bert_config.json \ + --pytorch_dump_output $BERT_BASE_DIR/pytorch_model.bin +``` + +Puoi scaricare i modelli pre-allenati di Google per la conversione [qua](https://github.com/google-research/bert#pre-trained-models). + +## ALBERT + +Per il modello ALBERT, converti checkpoint di Tensoflow in Pytorch utilizzando lo script +[convert_albert_original_tf_checkpoint_to_pytorch.py](https://github.com/huggingface/transformers/tree/main/src/transformers/models/albert/convert_albert_original_tf_checkpoint_to_pytorch.py). + +Il CLI prende come input un checkpoint di Tensorflow (tre files che iniziano con `model.ckpt-best`) e i relativi file di +configurazione (`albert_config.json`), dopodichรจ crea e salva un modello Pytorch. Per lanciare questa conversione +avrai bisogno di un'installazione di Tensorflow e di Pytorch. + +Ecco un esempio del procedimento di conversione di un modello `ALBERT Base` pre-allenato: + +```bash +export ALBERT_BASE_DIR=/path/to/albert/albert_base +transformers convert --model_type albert \ + --tf_checkpoint $ALBERT_BASE_DIR/model.ckpt-best \ + --config $ALBERT_BASE_DIR/albert_config.json \ + --pytorch_dump_output $ALBERT_BASE_DIR/pytorch_model.bin +``` + +Puoi scaricare i modelli pre-allenati di Google per la conversione [qui](https://github.com/google-research/albert#pre-trained-models). + +## OpenAI GPT + +Ecco un esempio del processo di conversione di un modello OpenAI GPT pre-allenato, assumendo che il tuo checkpoint di NumPy +sia salvato nello stesso formato dei modelli pre-allenati OpenAI (vedi [qui](https://github.com/openai/finetune-transformer-lm)): +```bash +export OPENAI_GPT_CHECKPOINT_FOLDER_PATH=/path/to/openai/pretrained/numpy/weights +transformers convert --model_type gpt \ + --tf_checkpoint $OPENAI_GPT_CHECKPOINT_FOLDER_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--config OPENAI_GPT_CONFIG] \ + [--finetuning_task_name OPENAI_GPT_FINETUNED_TASK] \ +``` + +## OpenAI GPT-2 + +Ecco un esempio del processo di conversione di un modello OpenAI GPT-2 pre-allenato (vedi [qui](https://github.com/openai/gpt-2)): + +```bash +export OPENAI_GPT2_CHECKPOINT_PATH=/path/to/openai-community/gpt2/pretrained/weights +transformers convert --model_type gpt2 \ + --tf_checkpoint $OPENAI_GPT2_CHECKPOINT_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--config OPENAI_GPT2_CONFIG] \ + [--finetuning_task_name OPENAI_GPT2_FINETUNED_TASK] +``` + +## XLNet + +Ecco un esempio del processo di conversione di un modello XLNet pre-allenato: + +```bash +export TRANSFO_XL_CHECKPOINT_PATH=/path/to/xlnet/checkpoint +export TRANSFO_XL_CONFIG_PATH=/path/to/xlnet/config +transformers convert --model_type xlnet \ + --tf_checkpoint $TRANSFO_XL_CHECKPOINT_PATH \ + --config $TRANSFO_XL_CONFIG_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT \ + [--finetuning_task_name XLNET_FINETUNED_TASK] \ +``` + +## XLM + +Ecco un esempio del processo di conversione di un modello XLM pre-allenato: + +```bash +export XLM_CHECKPOINT_PATH=/path/to/xlm/checkpoint +transformers convert --model_type xlm \ + --tf_checkpoint $XLM_CHECKPOINT_PATH \ + --pytorch_dump_output $PYTORCH_DUMP_OUTPUT + [--config XML_CONFIG] \ + [--finetuning_task_name XML_FINETUNED_TASK] +``` + +## T5 + +Ecco un esempio del processo di conversione di un modello T5 pre-allenato: + +```bash +export T5=/path/to/t5/uncased_L-12_H-768_A-12 +transformers convert --model_type t5 \ + --tf_checkpoint $T5/t5_model.ckpt \ + --config $T5/t5_config.json \ + --pytorch_dump_output $T5/pytorch_model.bin +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/create_a_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/create_a_model.md new file mode 100644 index 0000000000000000000000000000000000000000..caacf4fadc5db6dd6f69e6a23afaeca21009a499 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/create_a_model.md @@ -0,0 +1,361 @@ + + +# Crea un'architettura personalizzata + +Una [`AutoClass`](model_doc/auto) deduce automaticamente il modello dell'architettura e scarica la configurazione e i pesi pre-allenati. Generalmente, noi consigliamo di usare un `AutoClass` per produrre un codice indipendente dal checkpoint. Ma gli utenti che desiderano un controllo maggiore su parametri specifici del modello possono creare un modello ๐Ÿค— Transformers personalizzato da poche classi base. Questo potrebbe essere particolarmente utile per qualunque persona sia interessata nel studiare, allenare o sperimentare con un modello ๐Ÿค— Transformers. In questa guida, approfondisci la creazione di un modello personalizzato senza `AutoClass`. Impara come: + +- Caricare e personalizzare una configurazione del modello. +- Creare un'architettura modello. +- Creare un tokenizer lento e veloce per il testo. +- Creare un estrattore di caratteristiche per attivitร  riguardanti audio o immagini. +- Creare un processore per attivitร  multimodali. + +## Configurazione + +Una [configurazione](main_classes/configuration) si riferisce agli attributi specifici di un modello. Ogni configurazione del modello ha attributi diversi; per esempio, tutti i modelli npl hanno questi attributi in comune `hidden_size`, `num_attention_heads`, `num_hidden_layers` e `vocab_size`. Questi attributi specificano il numero di attention heads o strati nascosti con cui costruire un modello. + +Dai un'occhiata piรน da vicino a [DistilBERT](model_doc/distilbert) accedendo a [`DistilBertConfig`] per ispezionare i suoi attributi: + +```py +>>> from transformers import DistilBertConfig + +>>> config = DistilBertConfig() +>>> print(config) +DistilBertConfig { + "activation": "gelu", + "attention_dropout": 0.1, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +[`DistilBertConfig`] mostra tutti gli attributi predefiniti usati per costruire una base [`DistilBertModel`]. Tutti gli attributi sono personalizzabili, creando uno spazio per sperimentare. Per esempio, puoi configurare un modello predefinito per: + +- Provare un funzione di attivazione diversa con il parametro `activation`. +- Utilizzare tasso di drop out piรน elevato per le probalitร  di attention con il parametro `attention_dropout`. + +```py +>>> my_config = DistilBertConfig(activation="relu", attention_dropout=0.4) +>>> print(my_config) +DistilBertConfig { + "activation": "relu", + "attention_dropout": 0.4, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +Nella funzione [`~PretrainedConfig.from_pretrained`] possono essere modificati gli attributi del modello pre-allenato: + +```py +>>> my_config = DistilBertConfig.from_pretrained("distilbert/distilbert-base-uncased", activation="relu", attention_dropout=0.4) +``` + +Quando la configurazione del modello ti soddisfa, la puoi salvare con [`~PretrainedConfig.save_pretrained`]. Il file della tua configurazione รจ memorizzato come file JSON nella save directory specificata: + +```py +>>> my_config.save_pretrained(save_directory="./your_model_save_path") +``` + +Per riutilizzare la configurazione del file, caricalo con [`~PretrainedConfig.from_pretrained`]: + +```py +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +``` + + + +Puoi anche salvare il file di configurazione come dizionario oppure come la differenza tra gli attributi della tua configurazione personalizzata e gli attributi della configurazione predefinita! Guarda la documentazione [configuration](main_classes/configuration) per piรน dettagli. + + + +## Modello + +Il prossimo passo e di creare [modello](main_classes/models). Il modello - vagamente riferito anche come architettura - definisce cosa ogni strato deve fare e quali operazioni stanno succedendo. Attributi come `num_hidden_layers` provenienti dalla configurazione sono usati per definire l'architettura. Ogni modello condivide la classe base [`PreTrainedModel`] e alcuni metodi comuni come il ridimensionamento degli input embeddings e la soppressione delle self-attention heads . Inoltre, tutti i modelli sono la sottoclasse di [`torch.nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html), [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) o [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html). Cio significa che i modelli sono compatibili con l'uso di ciascun di framework. + + + +Carica gli attributi della tua configurazione personalizzata nel modello: + +```py +>>> from transformers import DistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +>>> model = DistilBertModel(my_config) +``` + +Questo crea modelli con valori casuali invece di pesi pre-allenati. Non sarai in grado di usare questo modello per niente di utile finchรฉ non lo alleni. L'allenamento รจ un processo costoso e che richiede tempo . Generalmente รจ meglio usare un modello pre-allenato per ottenere risultati migliori velocemente, utilizzando solo una frazione delle risorse neccesarie per l'allenamento. + +Crea un modello pre-allenato con [`~PreTrainedModel.from_pretrained`]: + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Quando carichi pesi pre-allenati, la configurazione del modello predefinito รจ automaticamente caricata se il modello รจ fornito da ๐Ÿค— Transformers. Tuttavia, puoi ancora sostituire gli attributi - alcuni o tutti - di configurazione del modello predefinito con i tuoi se lo desideri: + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + +Carica gli attributi di configurazione personalizzati nel modello: + +```py +>>> from transformers import TFDistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +>>> tf_model = TFDistilBertModel(my_config) +``` + + +Questo crea modelli con valori casuali invece di pesi pre-allenati. Non sarai in grado di usare questo modello per niente di utile finchรฉ non lo alleni. L'allenamento รจ un processo costoso e che richiede tempo . Generalmente รจ meglio usare un modello pre-allenato per ottenere risultati migliori velocemente, utilizzando solo una frazione delle risorse neccesarie per l'allenamento. + +Crea un modello pre-allenoto con [`~TFPreTrainedModel.from_pretrained`]: + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Quando carichi pesi pre-allenati, la configurazione del modello predefinito รจ automaticamente caricato se il modello รจ fornito da ๐Ÿค— Transformers. Tuttavia, puoi ancora sostituire gli attributi - alcuni o tutti - di configurazione del modello predefinito con i tuoi se lo desideri: + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + + + +### Model head + +A questo punto, hai un modello DistilBERT base i cui output sono gli *hidden states* (in italiano stati nascosti). Gli stati nascosti sono passati come input a un model head per produrre l'output finale. ๐Ÿค— Transformers fornisce un model head diverso per ogni attivitร  fintanto che il modello supporta l'attivitร  (i.e., non puoi usare DistilBERT per un attivitร  sequence-to-sequence come la traduzione). + + + +Per esempio, [`DistilBertForSequenceClassification`] รจ un modello DistilBERT base con una testa di classificazione per sequenze. La sequenza di classificazione head รจ uno strato lineare sopra gli output ragruppati. + +```py +>>> from transformers import DistilBertForSequenceClassification + +>>> model = DistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Riutilizza facilmente questo checkpoint per un'altra attivitร  passando ad un model head differente. Per un attivitร  di risposta alle domande, utilizzerai il model head [`DistilBertForQuestionAnswering`]. La head per compiti di question answering รจ simile alla classificazione di sequenza head tranne per il fatto che รจ uno strato lineare sopra l'output degli stati nascosti (hidden states in inglese) + +```py +>>> from transformers import DistilBertForQuestionAnswering + +>>> model = DistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + +Per esempio, [`TFDistilBertForSequenceClassification`] รจ un modello DistilBERT base con classificazione di sequenza head. La classificazione di sequenza head รจ uno strato lineare sopra gli output raggruppati. + +```py +>>> from transformers import TFDistilBertForSequenceClassification + +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Riutilizza facilmente questo checkpoint per un altra attivitร  passando ad un modello head diverso. Per un attivitร  di risposta alle domande, utilizzerai il model head [`TFDistilBertForQuestionAnswering`]. Il head di risposta alle domande รจ simile alla sequenza di classificazione head tranne per il fatto che รจ uno strato lineare sopra l'output degli stati nascosti (hidden states in inglese) + +```py +>>> from transformers import TFDistilBertForQuestionAnswering + +>>> tf_model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +## Tokenizer + +L'ultima classe base di cui hai bisogno prima di utilizzare un modello per i dati testuali รจ un [tokenizer](main_classes/tokenizer) per convertire il testo grezzo in tensori. Ci sono due tipi di tokenizer che puoi usare con ๐Ÿค— Transformers: + +- [`PreTrainedTokenizer`]: un'implementazione Python di un tokenizer. +- [`PreTrainedTokenizerFast`]: un tokenizer dalla nostra libreria [๐Ÿค— Tokenizer](https://huggingface.co/docs/tokenizers/python/latest/) basata su Rust. Questo tipo di tokenizer รจ significativamente piรน veloce, specialmente durante la batch tokenization, grazie alla sua implementazione Rust. Il tokenizer veloce offre anche metodi aggiuntivi come *offset mapping* che associa i token alle loro parole o caratteri originali. + +Entrambi i tokenizer supportano metodi comuni come la codifica e la decodifica, l'aggiunta di nuovi token e la gestione di token speciali. + + + +Non tutti i modelli supportano un tokenizer veloce. Dai un'occhiata a questo [tabella](index#supported-frameworks) per verificare se un modello ha il supporto per tokenizer veloce. + + + +Se hai addestrato il tuo tokenizer, puoi crearne uno dal tuo file *vocabolario*: + +```py +>>> from transformers import DistilBertTokenizer + +>>> my_tokenizer = DistilBertTokenizer(vocab_file="my_vocab_file.txt", do_lower_case=False, padding_side="left") +``` + +รˆ importante ricordare che il vocabolario di un tokenizer personalizzato sarร  diverso dal vocabolario generato dal tokenizer di un modello preallenato. รˆ necessario utilizzare il vocabolario di un modello preallenato se si utilizza un modello preallenato, altrimenti gli input non avranno senso. Crea un tokenizer con il vocabolario di un modello preallenato con la classe [`DistilBertTokenizer`]: + +```py +>>> from transformers import DistilBertTokenizer + +>>> slow_tokenizer = DistilBertTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +Crea un tokenizer veloce con la classe [`DistilBertTokenizerFast`]: + +```py +>>> from transformers import DistilBertTokenizerFast + +>>> fast_tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +Per l'impostazione predefinita, [`AutoTokenizer`] proverร  a caricare un tokenizer veloce. Puoi disabilitare questo comportamento impostando `use_fast=False` in `from_pretrained`. + + + +## Estrattore Di Feature + +Un estrattore di caratteristiche (feature in inglese) elabora input audio o immagini. Eredita dalla classe [`~feature_extraction_utils.FeatureExtractionMixin`] base e puรฒ anche ereditare dalla classe [`ImageFeatureExtractionMixin`] per l'elaborazione delle caratteristiche dell'immagine o dalla classe [`SequenceFeatureExtractor`] per l'elaborazione degli input audio. + +A seconda che tu stia lavorando a un'attivitร  audio o visiva, crea un estrattore di caratteristiche associato al modello che stai utilizzando. Ad esempio, crea un [`ViTFeatureExtractor`] predefinito se stai usando [ViT](model_doc/vit) per la classificazione delle immagini: + +```py +>>> from transformers import ViTFeatureExtractor + +>>> vit_extractor = ViTFeatureExtractor() +>>> print(vit_extractor) +ViTFeatureExtractor { + "do_normalize": true, + "do_resize": true, + "feature_extractor_type": "ViTFeatureExtractor", + "image_mean": [ + 0.5, + 0.5, + 0.5 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": 2, + "size": 224 +} +``` + + + +Se non stai cercando alcuna personalizzazione, usa il metodo `from_pretrained` per caricare i parametri di default dell'estrattore di caratteristiche di un modello. + + + +Modifica uno qualsiasi dei parametri [`ViTFeatureExtractor`] per creare il tuo estrattore di caratteristiche personalizzato: + +```py +>>> from transformers import ViTFeatureExtractor + +>>> my_vit_extractor = ViTFeatureExtractor(resample="PIL.Image.BOX", do_normalize=False, image_mean=[0.3, 0.3, 0.3]) +>>> print(my_vit_extractor) +ViTFeatureExtractor { + "do_normalize": false, + "do_resize": true, + "feature_extractor_type": "ViTFeatureExtractor", + "image_mean": [ + 0.3, + 0.3, + 0.3 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": "PIL.Image.BOX", + "size": 224 +} +``` + +Per gli input audio, puoi creare un [`Wav2Vec2FeatureExtractor`] e personalizzare i parametri in modo simile: + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> w2v2_extractor = Wav2Vec2FeatureExtractor() +>>> print(w2v2_extractor) +Wav2Vec2FeatureExtractor { + "do_normalize": true, + "feature_extractor_type": "Wav2Vec2FeatureExtractor", + "feature_size": 1, + "padding_side": "right", + "padding_value": 0.0, + "return_attention_mask": false, + "sampling_rate": 16000 +} +``` + +## Processore + +Per modelli che supportano attivitร  multimodali, ๐Ÿค— Transformers offre una classe di processore che racchiude comodamente un estrattore di caratteristiche e un tokenizer in un unico oggetto. Ad esempio, utilizziamo [`Wav2Vec2Processor`] per un'attivitร  di riconoscimento vocale automatico (ASR). ASR trascrive l'audio in testo, quindi avrai bisogno di un estrattore di caratteristiche e di un tokenizer. + +Crea un estrattore di feature per gestire gli input audio: + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> feature_extractor = Wav2Vec2FeatureExtractor(padding_value=1.0, do_normalize=True) +``` + +Crea un tokenizer per gestire gli input di testo: + +```py +>>> from transformers import Wav2Vec2CTCTokenizer + +>>> tokenizer = Wav2Vec2CTCTokenizer(vocab_file="my_vocab_file.txt") +``` + +Combinare l'estrattore di caratteristiche e il tokenizer in [`Wav2Vec2Processor`]: + +```py +>>> from transformers import Wav2Vec2Processor + +>>> processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer) +``` + +Con due classi di base - configurazione e modello - e una classe di preelaborazione aggiuntiva (tokenizer, estrattore di caratteristiche o processore), puoi creare qualsiasi modello supportato da ๐Ÿค— Transformers. Ognuna di queste classi base รจ configurabile, consentendoti di utilizzare gli attributi specifici che desideri. รˆ possibile impostare facilmente un modello per l'addestramento o modificare un modello preallenato esistente per la messa a punto. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/custom_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/custom_models.md new file mode 100644 index 0000000000000000000000000000000000000000..5f3d4cade0073f07bae794d9a9c006baed4db47b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/custom_models.md @@ -0,0 +1,359 @@ + + +# Condividere modelli personalizzati +La libreria ๐Ÿค— Transformers รจ studiata per essere facilmente estendibile. Il codice di ogni modello รจ interamente +situato in una sottocartella del repository senza alcuna astrazione, perciรฒ puoi facilmente copiare il file di un +modello e modificarlo in base ai tuoi bisogni. + +Se stai scrivendo un nuovo modello, potrebbe essere piรน semplice iniziare da zero. In questo tutorial, ti mostreremo +come scrivere un modello personalizzato e la sua configurazione in modo che possa essere utilizzato allโ€™interno di +Transformers, e come condividerlo con la community (assieme al relativo codice) cosรฌ che tutte le persone possano usarlo, anche +se non presente nella libreria ๐Ÿค— Transformers. + +Illustriamo tutto questo su un modello ResNet, avvolgendo la classe ResNet della +[libreria timm](https://github.com/rwightman/pytorch-image-models) in un [`PreTrainedModel`]. + +## Scrivere una configurazione personalizzata +Prima di iniziare a lavorare al modello, scriviamone la configurazione. La configurazione di un modello รจ un oggetto +che contiene tutte le informazioni necessarie per la build del modello. Come vedremo nella prossima sezione, il +modello puรฒ soltanto essere inizializzato tramite `config`, per cui dovremo rendere tale oggetto piรน completo possibile. + +Nel nostro esempio, prenderemo un paio di argomenti della classe ResNet che potremmo voler modificare. +Configurazioni differenti ci daranno quindi i differenti possibili tipi di ResNet. Salveremo poi questi argomenti, +dopo averne controllato la validitร . + +```python +from transformers import PretrainedConfig +from typing import List + + +class ResnetConfig(PretrainedConfig): + model_type = "resnet" + + def __init__( + self, + block_type="bottleneck", + layers: list[int] = [3, 4, 6, 3], + num_classes: int = 1000, + input_channels: int = 3, + cardinality: int = 1, + base_width: int = 64, + stem_width: int = 64, + stem_type: str = "", + avg_down: bool = False, + **kwargs, + ): + if block_type not in ["basic", "bottleneck"]: + raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.") + if stem_type not in ["", "deep", "deep-tiered"]: + raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.") + + self.block_type = block_type + self.layers = layers + self.num_classes = num_classes + self.input_channels = input_channels + self.cardinality = cardinality + self.base_width = base_width + self.stem_width = stem_width + self.stem_type = stem_type + self.avg_down = avg_down + super().__init__(**kwargs) +``` + +Le tre cose piรน importanti da ricordare quando scrivi le tue configurazioni sono le seguenti: +- Devi ereditare da `Pretrainedconfig`, +- Il metodo `__init__` del tuo `Pretrainedconfig` deve accettare i kwargs, +- I `kwargs` devono essere passati alla superclass `__init__` + +Lโ€™ereditร  รจ importante per assicurarsi di ottenere tutte le funzionalitร  della libreria ๐Ÿค— transformers, +mentre gli altri due vincoli derivano dal fatto che un `Pretrainedconfig` ha piรน campi di quelli che stai settando. +Quando ricarichi una config da un metodo `from_pretrained`, questi campi devono essere accettati dalla tua config e +poi inviati alla superclasse. + +Definire un `model_type` per la tua configurazione (qua `model_type = โ€œresnetโ€`) non รจ obbligatorio, a meno che tu +non voglia registrare il modello con le classi Auto (vedi l'ultima sezione). + +Una volta completato, puoi facilmente creare e salvare la tua configurazione come faresti con ogni altra configurazione +di modelli della libreria. Ecco come possiamo creare la config di un resnet50d e salvarlo: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d_config.save_pretrained("custom-resnet") +``` + +Questo salverร  un file chiamato `config.json` all'interno della cartella `custom-resnet`. Potrai poi ricaricare la tua +config con il metodo `from_pretrained`. + +```py +resnet50d_config = ResnetConfig.from_pretrained("custom-resnet") +``` + +Puoi anche usare qualunque altro metodo della classe [`PretrainedConfig`], come [`~PretrainedConfig.push_to_hub`] +per caricare direttamente la tua configurazione nell'hub. + +## Scrivere un modello personalizzato + +Ora che abbiamo la nostra configurazione ResNet, possiamo continuare a scrivere il modello. In realtร , ne scriveremo +due: uno che estrae le features nascoste da una batch di immagini (come [`BertModel`]) e uno che รจ utilizzabile per +la classificazione di immagini (come [`BertModelForSequenceClassification`]). + +Come abbiamo menzionato in precedenza, scriveremo soltanto un wrapper del modello, per mantenerlo semplice ai fini di +questo esempio. L'unica cosa che dobbiamo fare prima di scrivere questa classe รจ una mappatura fra i tipi di blocco e +le vere classi dei blocchi. Successivamente il modello รจ definito tramite la configurazione, passando tutto quanto alla +classe `ResNet`. + +```py +from transformers import PreTrainedModel +from timm.models.resnet import BasicBlock, Bottleneck, ResNet +from .configuration_resnet import ResnetConfig + + +BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} + + +class ResnetModel(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor): + return self.model.forward_features(tensor) +``` + +Per il modello che classificherร  le immagini, cambiamo soltanto il metodo forward: + +```py +import torch + + +class ResnetModelForImageClassification(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor, labels=None): + logits = self.model(tensor) + if labels is not None: + loss = torch.nn.functional.cross_entropy(logits, labels) + return {"loss": loss, "logits": logits} + return {"logits": logits} +``` + +Nota come, in entrambi i casi, ereditiamo da `PreTrainedModel` e chiamiamo l'inizializzazione della superclasse +con il metodo `config` (un po' come quando scrivi un normale `torch.nn.Module`). La riga che imposta la `config_class` +non รจ obbligatoria, a meno che tu non voglia registrare il modello con le classi Auto (vedi l'ultima sezione). + + + +Se il tuo modello รจ molto simile a un modello all'interno della libreria, puoi ri-usare la stessa configurazione di quel modello. + + + +Puoi fare in modo che il tuo modello restituisca in output qualunque cosa tu voglia, ma far restituire un dizionario +come abbiamo fatto per `ResnetModelForImageClassification`, con la funzione di perdita inclusa quando vengono passate le labels, +renderร  il tuo modello direttamente utilizzabile all'interno della classe [`Trainer`]. Utilizzare altri formati di output va bene +se hai in progetto di utilizzare un tuo loop di allenamento, o se utilizzerai un'altra libreria per l'addestramento. + +Ora che abbiamo la classe del nostro modello, creiamone uno: + +```py +resnet50d = ResnetModelForImageClassification(resnet50d_config) +``` + +Ribadiamo, puoi usare qualunque metodo dei [`PreTrainedModel`], come [`~PreTrainedModel.save_pretrained`] o +[`~PreTrainedModel.push_to_hub`]. Utilizzeremo quest'ultimo nella prossima sezione, e vedremo come caricare i pesi del +modello assieme al codice del modello stesso. Ma prima, carichiamo alcuni pesi pre-allenati all'interno del nostro modello. + +Nel tuo caso specifico, probabilmente allenerai il tuo modello sui tuoi dati. Per velocizzare in questo tutorial, +utilizzeremo la versione pre-allenata del resnet50d. Dato che il nostro modello รจ soltanto un wrapper attorno a quel modello, +sarร  facile trasferirne i pesi: + +```py +import timm + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +Vediamo adesso come assicurarci che quando facciamo [`~PreTrainedModel.save_pretrained`] o [`~PreTrainedModel.push_to_hub`], +il codice del modello venga salvato. + +## Inviare il codice all'Hub + + + +Questa API รจ sperimentale e potrebbe avere alcuni cambiamenti nei prossimi rilasci. + + + +Innanzitutto, assicurati che il tuo modello sia completamente definito in un file `.py`. Puรฒ sfruttare import relativi +ad altri file, purchรจ questi siano nella stessa directory (non supportiamo ancora sotto-moduli per questa funzionalitร ). +Per questo esempio, definiremo un file `modeling_resnet.py` e un file `configuration_resnet.py` in una cartella dell'attuale +working directory chiamata `resnet_model`. Il file configuration contiene il codice per `ResnetConfig` e il file modeling +contiene il codice di `ResnetModel` e `ResnetModelForImageClassification`. + +``` +. +โ””โ”€โ”€ resnet_model + โ”œโ”€โ”€ __init__.py + โ”œโ”€โ”€ configuration_resnet.py + โ””โ”€โ”€ modeling_resnet.py +``` + +Il file `__init__.py` puรฒ essere vuoto, serve solo perchรจ Python capisca che `resnet_model` puรฒ essere utilizzato come un modulo. + + + +Se stai copiando i file relativi alla modellazione della libreria, dovrai sostituire tutti gli import relativi in cima al file con import del + pacchetto `transformers`. + + + +Nota che puoi ri-utilizzare (o usare come sottoclassi) un modello/configurazione esistente. + +Per condividere il tuo modello con la community, segui questi passi: prima importa il modello ResNet e la sua configurazione +dai nuovi file creati: + +```py +from resnet_model.configuration_resnet import ResnetConfig +from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification +``` + +Dopodichรจ dovrai dire alla libreria che vuoi copiare i file con il codice di quegli oggetti quando utilizzi il metodo +`save_pretrained` e registrarli in modo corretto con una Auto classe (specialmente per i modelli). Utilizza semplicemente: + +```py +ResnetConfig.register_for_auto_class() +ResnetModel.register_for_auto_class("AutoModel") +ResnetModelForImageClassification.register_for_auto_class("AutoModelForImageClassification") +``` + +Nota che non c'รจ bisogno di specificare una Auto classe per la configurazione (c'รจ solo una Auto classe per le configurazioni, +[`AutoConfig`], ma รจ diversa per i modelli). Il tuo modello personalizato potrebbe essere utilizzato per diverse tasks, +per cui devi specificare quale delle classi Auto รจ quella corretta per il tuo modello. + +Successivamente, creiamo i modelli e la config come abbiamo fatto in precedenza: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d = ResnetModelForImageClassification(resnet50d_config) + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +Adesso, per inviare il modello all'Hub, assicurati di aver effettuato l'accesso. Lancia dal tuo terminale: + +```bash +hf auth login +``` + +O da un notebook: + +```py +from huggingface_hub import notebook_login + +notebook_login() +``` + +Potrai poi inviare il tutto sul tuo profilo (o di un'organizzazione di cui fai parte) in questo modo: + +```py +resnet50d.push_to_hub("custom-resnet50d") +``` + +Oltre ai pesi del modello e alla configurazione in formato json, questo ha anche copiato i file `.py` modeling e +configuration all'interno della cartella `custom-resnet50d` e ha caricato i risultati sull'Hub. Puoi controllare +i risultati in questa [model repo](https://huggingface.co/sgugger/custom-resnet50d). + +Puoi controllare il tutorial di condivisione [tutorial di condivisione](model_sharing) per piรน informazioni sul +metodo con cui inviare all'Hub. + +## Usare un modello con codice personalizzato + +Puoi usare ogni configurazione, modello o tokenizer con file di codice personalizzati nella sua repository +con le classi Auto e il metodo `from_pretrained`. Tutti i files e il codice caricati sull'Hub sono scansionati da malware +(fai riferimento alla documentazione [Hub security](https://huggingface.co/docs/hub/security#malware-scanning) per piรน informazioni), +ma dovresti comunque assicurarti dell'affidabilitร  del codice e dell'autore per evitare di eseguire codice dannoso sulla tua macchina. +Imposta `trust_remote_code=True` per usare un modello con codice personalizzato: + +```py +from transformers import AutoModelForImageClassification + +model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True) +``` + +Inoltre, raccomandiamo fortemente di passare un hash del commit come `revision` per assicurarti che le autrici o gli autori del modello +non abbiano modificato il codice con alcune nuove righe dannose (a meno che non ti fidi completamente della fonte): + +```py +commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292" +model = AutoModelForImageClassification.from_pretrained( + "sgugger/custom-resnet50d", trust_remote_code=True, revision=commit_hash +) +``` + +Nota che quando cerchi la storia dei commit della repo del modello sull'Hub, c'รจ un bottone con cui facilmente copiare il +commit hash di ciascun commit. + +## Registrare un modello con codice personalizzato nelle classi Auto + +Se stai scrivendo una libreria che estende ๐Ÿค— Transformers, potresti voler estendere le classi Auto per includere il tuo modello. +Questo รจ diverso dall'inviare codice nell'Hub: gli utenti dovranno importare la tua libreria per ottenere il modello personalizzato +(anzichรจ scaricare automaticamente il modello dall'Hub). + +Finchรจ il tuo file di configurazione ha un attributo `model_type` diverso dai model types esistenti, e finchรจ le tue +classi modello hanno i corretti attributi `config_class`, potrai semplicemente aggiungerli alle classi Auto come segue: + +```py +from transformers import AutoConfig, AutoModel, AutoModelForImageClassification + +AutoConfig.register("resnet", ResnetConfig) +AutoModel.register(ResnetConfig, ResnetModel) +AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) +``` + +Nota che il primo argomento utilizzato quando registri la configurazione di un modello personalizzato con [`AutoConfig`] +deve corrispondere al `model_type` della tua configurazione personalizzata, ed il primo argomento utilizzato quando +registri i tuoi modelli personalizzati in una qualunque classe Auto del modello deve corrispondere alla `config_class` +di quei modelli. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/debugging.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/debugging.md new file mode 100644 index 0000000000000000000000000000000000000000..5c1dab51bd11793a62eabc916c7bbf7e95209934 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/debugging.md @@ -0,0 +1,318 @@ + + +# Debugging + +## Debug dei problemi di rete multi-GPU + +Quando addestri o fai inferenza con `DistributedDataParallel` e GPU multiple, se si verificano problemi di intercomunicazione tra processi e/o nodi, puoi utilizzare il seguente script per diagnosticare i problemi della rete. + +```bash +wget https://raw.githubusercontent.com/huggingface/transformers/main/scripts/distributed/torch-distributed-gpu-test.py +``` + +Per esempio per testare come 2 GPU interagiscono fai: + +```bash +python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` + +Se entrambi i processi sono in grado di comunicare tra loro e di allocare la memoria della GPU, ciascuno di essi stamperร  lo stato OK. + +Per piรน GPU o nodi adatta gli argumenti nello script. + +All'interno dello script di diagnostica troverai molti altri dettagli e anche una guida per eseguirlo in ambiente SLURM. + +Un livello di debug superiore รจ aggiungere la variabile d'ambiente `NCCL_DEBUG=INFO` come di seguito: + +```bash +NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` + +In questo modo si scaricano molte informazioni di debug relative a NCCL, che puoi cercare online in caso di problemi. Oppure, se non hai la sicurezza di come interpretare l'output, puoi condividere il file di log in una Issue. + +## Rilevamento di Underflow e Overflow + + + +Questa funzionalitร  al momento รจ disponibile solo per PyTorch. + + + + + +Per addestramento multi-GPU richiede DDP (`torch.distributed.launch`). + + + + + +Questa funzionalitร  puรฒ essere usata con modelli basati su `nn.Module`. + + + +Se inizi a ottenere `loss=NaN` o il modello presenta qualche altro comportamento anomalo a causa di valori `inf` o `nan` in +attivazioni o nei pesi, รจ necessario scoprire dove si verifica il primo underflow o overflow e cosa lo ha determinato. Fortunatamente +รจ possibile farlo facilmente attivando un modulo speciale che effettuerร  il rilevamento automaticamente. + +Se stai usando [`Trainer`], hai bisogno di aggiungere solo: + +```bash +--debug underflow_overflow +``` + +ai normali argomenti della riga di comando, o passa `debug="underflow_overflow"` quando viene creato l'oggetto +[`TrainingArguments`]. + +Se stai usando il tuo ciclo di allenamento o un altro trainer, puoi ottenere lo stesso risultato con: + +```python +from .debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model) +``` + +[`~debug_utils.DebugUnderflowOverflow`] inserisce dei ganci nel modello che dopo ogni chiamata +testeranno le variabili di ingresso e di uscita e anche i pesi del modulo corrispondente. Non appena viene rilevato `inf` o +o `nan` in almeno un elemento delle attivazioni o dei pesi, il programma lo notifica e stampa un rapporto come il seguente (questo รจ stato rilevato con `google/mt5-small` sotto fp16 mixed precision): + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata + encoder.block.1.layer.1.DenseReluDense.dropout Dropout +0.00e+00 2.57e+02 input[0] +0.00e+00 2.85e+02 output +[...] + encoder.block.2.layer.0 T5LayerSelfAttention +6.78e-04 3.15e+03 input[0] +2.65e-04 3.42e+03 output[0] + None output[1] +2.25e-01 1.00e+04 output[2] + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.dropout Dropout +0.00e+00 8.76e+03 input[0] +0.00e+00 9.74e+03 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +L'output di esempio รจ stato tagliato al centro per brevitร . + +La seconda colonna mostra il valore dell'elemento piรน grande in assoluto,cosรฌ se osserviamo da vicino gli ultimi istanti, +input e output sono nel range di `1e4`. Questo addestramento รจ stato eseguito con una mixed precision fp16 e l'ultimo passo usciva fuori (sotto `fp16` il valore piรน grande prima di `inf` รจ `64e3`). Per evitare overflows sotto `fp16` le attivazionioni devono rimanere molto al di sotto di `1e4`, perchรฉ `1e4 * 1e4 = 1e8` quindi qualsiasi moltiplicazione di matrice con grandi attivazioni porterร  a una condizione di overflow numerico. + +All'inizio della traccia รจ possibile scoprire a quale lotto si รจ verificato il problema (questo `Detected inf/nan during batch_number=0` significa che il problema si รจ verificato nel primo lotto). + +Ogni frame segnalato inizia dichiarando la voce completamente qualificata per il modulo corrispondente per il quale il frame รจ stato segnalato. +Se osserviamo il seguente frame: + +``` + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output +``` + +Questo, `encoder.block.2.layer.1.layer_norm` indica che si tratta di un layer norm nel primo layer, del secondo blocco dell'encoder. E le chiamata specifica di `forward` รจ `T5LayerNorm`. + +Osserviamo gli ultimi frame del report: + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata +[...] + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +L'ultimo frame report per la funzione `Dropout.forward` con la prima voce per l'unico input e la seconda per l'unico output. Si puรฒ notare che รจ stato richiamato da un attibuto `dropout` dentro la classe `DenseReluDense`. Si puรฒ notare che ciรฒ รจ avvenuto durante il primo strato, del 2ยฐ blocco, durante il primissimo lotto. Infine, gli elementi di input piรน grandi in assoluto sono stati `6.27e+04` e l'equivalente per l'output era `inf`. + +Puoi vedere qui, che `T5DenseGatedGeluDense.forward` risulta in output activations, il cui valore massimo assoluto era circa 62,7K, che รจ molto vicino al limite massimo di 64K di fp16. Nel prossimo frame abbiamo `Dropout` che rinormalizza i pesi, dopo aver azzerato alcuni elementi, il che spinge il valore massimo assoluto a piรน di 64K e si verifica un overflow.(`inf`). + +Come puoi notare, รจ nei frames precedenti che occorre esaminare quando i numeri iniziano a diventare molto grandi per i valori fp16. + +Confrontiamo il report al codice `models/t5/modeling_t5.py`: + +```python +class T5DenseGatedGeluDense(nn.Module): + def __init__(self, config): + super().__init__() + self.wi_0 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wi_1 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wo = nn.Linear(config.d_ff, config.d_model, bias=False) + self.dropout = nn.Dropout(config.dropout_rate) + self.gelu_act = ACT2FN["gelu_new"] + + def forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states +``` + +Ora รจ facile vedere la chiamata `dropout`, e tutte le chiamate precedenti. + +Poichรฉ il rilevamento avviene in un avanzamento (forward hook in eng.), i rapporti vengono creati immeditamente dopo ogni rientro da `forward` (forward returns in eng.). + +Tornando al rapporto completo, per agire e risolvere il problema, dobbiamo andare qualche frame piรน in alto, dove i numeri hanno iniziato a salire, e probabilmente passare alla modalitร  `fp32`, in modo che i numeri non trabocchino quando vengono moltiplicati o sommati. Naturalmente, potrebbero esserci altre soluzioni. Per esempio, potremmo spegnere temporanemante `amp` se รจ abilitato, successivamente spostare `forward` in un helper wrapper, come: + +```python +def _forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states + + +import torch + + +def forward(self, hidden_states): + if torch.is_autocast_enabled(): + with torch.cuda.amp.autocast(enabled=False): + return self._forward(hidden_states) + else: + return self._forward(hidden_states) +``` + +Poichรฉ il rilevatore automatico riporta solo gli ingressi e le uscite di fotogrammi completi, una volta che si sa dove cercare, si puรฒ +analizzare anche le fasi intermedie di una specifica funzione `forward`. In alcuni casi puoi usare la funzione di supporto `detect_overflow` per indirizzare il rilevatore dove preferisci, ad esempio: + +```python +from debug_utils import detect_overflow + + +class T5LayerFF(nn.Module): + [...] + + def forward(self, hidden_states): + forwarded_states = self.layer_norm(hidden_states) + detect_overflow(forwarded_states, "after layer_norm") + forwarded_states = self.DenseReluDense(forwarded_states) + detect_overflow(forwarded_states, "after DenseReluDense") + return hidden_states + self.dropout(forwarded_states) +``` + +Si puรฒ vedere che abbiamo aggiunto 2 di questi e ora teniamo traccia se `inf` o `nan` per `forwarded_states` รจ stato rilevato +da qualche parte. + +In realtร , il rilevatore li riporta giร , perchรฉ ciascuna delle chiamate nell'esempio precedente รจ un `nn.Module`, ma +diciamo che se avessimo dei calcoli diretti locali, questo รจ il modo in cui lo faremmo. + +Inoltre, se si istanzia il debugger nel proprio codice, รจ possibile modificare il numero di fotogrammi stampati rispetto a +predefinito, ad esempio.: + +```python +from .debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model, max_frames_to_save=100) +``` + +### Tracciamento della mistura assoluta del lotto specifico e del valore massimo + +La stessa classe di debug puรฒ essere utilizzata per il tracciamento per-batch con la funzione di rilevamento di underflow/overflow disattivata. + +Supponiamo di voler osservare i valori minimi e massimi assoluti per tutti gli ingredienti di ogni chiamata `forward` di un dato lotto. +lotto, e che lo si voglia fare solo per i lotti 1 e 3. Si istanzia questa classe come: + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3]) +``` + +Ora i batch completi 1 e 3 saranno tracciati utilizzando lo stesso formato del rilevatore di underflow/overflow. + +I batches sono 0-indexed. + +Questo รจ utile se si sa che il programma inizia a comportarsi male dopo un certo numero di batch, in modo da poter avanzare velocemente fino a quell'area. +direttamente a quell'area. Ecco un esempio di output troncato per questa configurazione: + +``` + *** Starting batch number=1 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.47e+04 input[0] +5.36e-05 7.92e+02 output +[...] + decoder.dropout Dropout +1.60e-07 2.27e+01 input[0] +0.00e+00 2.52e+01 output + decoder T5Stack + not a tensor output + lm_head Linear +1.01e-06 7.92e+02 weight +0.00e+00 1.11e+00 input[0] +6.06e-02 8.39e+01 output + T5ForConditionalGeneration + not a tensor output + + *** Starting batch number=3 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.78e+04 input[0] +5.36e-05 7.92e+02 output +[...] +``` + +Qui verrร  scaricato un numero enorme di fotogrammi, tanti quanti sono le chiamate in avanti nel modello, quindi puรฒ essere o non essere quello che volete, ma a volte puรฒ essere piรน utile usarlo di un classico debugger. Per esempio, se il problema inizia a verificarsi a partire dal lotto numero 150. Quindi รจ possibile scaricare le tracce dei lotti 149 e 150 e confrontare i punti in cui i numeri hanno iniziato a divergere. + +รˆ inoltre possibile specificare il numero di batch dopo il quale interrompere l'addestramento, con: + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3], abort_after_batch_num=3) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/index.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/index.md new file mode 100644 index 0000000000000000000000000000000000000000..a0c2523966140a2a0c3175f19da8693bff1475a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/index.md @@ -0,0 +1,300 @@ + + +# ๐Ÿค— Transformers + +Machine Learning allo stato dell'arte per PyTorch, TensorFlow e JAX. + +๐Ÿค— Transformers fornisce delle API per scaricare in modo semplice e allenare modelli pre-allenati allo stato dell'arte. L'utilizzo di modelli pre-allenati puรฒ ridurre i tuoi costi computazionali, l'impatto ambientale, e farti risparmiare il tempo che utilizzeresti per allenare un modello da zero. I modelli possono essere utilizzati in diverse modalitร  come ad esempio: + +* ๐Ÿ“ Testo: classificazione del testo, estrazione delle informazioni, rispondere a domande, riassumere, traduzione e generazione del testo in piรน di 100 lingue. +* ๐Ÿ–ผ๏ธ Immagini: classificazione di immagini, rilevazione di oggetti e segmentazione. +* ๐Ÿ—ฃ๏ธ Audio: riconoscimento vocale e classificazione dell'audio. +* ๐Ÿ™ Multimodale: rispondere a domande inerenti dati tabulari, riconoscimento ottico dei caratteri, estrazione di informazioni a partire da documenti scannerizzati, classificazione di video e risposta visuale a domande. + +La nostra libreria supporta un'integrazione perfetta tra tre delle librerie per il deep learning piรน popolari: [PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/) e [JAX](https://jax.readthedocs.io/en/latest/). Allena il tuo modello in tre righe di codice in un framework, e caricalo per l'inferenza in un altro. + +Ogni architettura di ๐Ÿค— Transformers รจ definita in un modulo Python indipendente cosรฌ da poter essere personalizzata in modo semplice per la ricerca e gli esperimenti. + +## Se stai cercando supporto personalizzato dal team di Hugging Face + + +HuggingFace Expert Acceleration Program + + +## Contenuti + +La documentazione รจ organizzata in cinque parti: + +- **INIZIARE** contiene un tour rapido e le istruzioni di installazione per cominciare ad utilizzare ๐Ÿค— Transformers. +- **TUTORIALS** รจ un buon posto da cui iniziare se per te la nostra libreria รจ nuova. Questa sezione ti aiuterร  ad acquisire le competenze basilari di cui hai bisogno per iniziare ad utilizzare ๐Ÿค— Transformers. +- **GUIDE PRATICHE** ti mostrerร  come raggiungere obiettivi specifici come fare fine-tuning di un modello pre-allenato per la modellizzazione del linguaggio o come creare una testa per un modello personalizzato. +- **GUIDE CONCETTUALI** fornisce discussioni e spiegazioni dei concetti sottostanti alle idee dietro ai modelli, compiti, e la filosofia di progettazione di ๐Ÿค— Transformers. +- **API** descrive ogni classe e funzione, raggruppate in: + - **CLASSI PRINCIPALI** per le classi principali che espongono le API importanti della libreria. + - **MODELLI** per le classi e le funzioni relative ad ogni modello implementato all'interno della libreria. + - **HELPERS INTERNI** per le classi e le funzioni che utilizziamo internamente. + +La libreria attualmente contiene implementazioni in JAX, PyTorch e TensorFlow, pesi di modelli pre-allenati, script di utilizzo e strumenti di conversione per i seguenti modelli. + +### Modelli supportati + + + +1. **[ALBERT](model_doc/albert)** (da Google Research e l'Istituto Tecnologico di Chicago) rilasciato con il paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942), da Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. +1. **[ALIGN](model_doc/align)** (from Google Research) rilasciato con il paper [Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://huggingface.co/papers/2102.05918) da Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, Tom Duerig. +1. **[BART](model_doc/bart)** (da Facebook) rilasciato con il paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://huggingface.co/papers/1910.13461) da Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov e Luke Zettlemoyer. +1. **[BARThez](model_doc/barthez)** (da politecnico di ร‰cole) rilasciato con il paper [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) da Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis. +1. **[BARTpho](model_doc/bartpho)** (da VinAI Research) rilasciato con il paper [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701) da Nguyen Luong Tran, Duong Minh Le e Dat Quoc Nguyen. +1. **[BEiT](model_doc/beit)** (da Microsoft) rilasciato con il paper [BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) da Hangbo Bao, Li Dong, Furu Wei. +1. **[BERT](model_doc/bert)** (da Google) rilasciato con il paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) da Jacob Devlin, Ming-Wei Chang, Kenton Lee e Kristina Toutanova. +1. **[BERTweet](model_doc/bertweet)** (da VinAI Research) rilasciato con il paper [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) da Dat Quoc Nguyen, Thanh Vu e Anh Tuan Nguyen. +1. **[BERT For Sequence Generation](model_doc/bert-generation)** (da Google) rilasciato con il paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) da Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[BigBird-RoBERTa](model_doc/big_bird)** (da Google Research) rilasciato con il paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) da Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[BigBird-Pegasus](model_doc/bigbird_pegasus)** (v Google Research) rilasciato con il paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) da Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[Blenderbot](model_doc/blenderbot)** (da Facebook) rilasciato con il paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) da Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BlenderbotSmall](model_doc/blenderbot-small)** (da Facebook) rilasciato con il paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) da Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BORT](model_doc/bort)** (da Alexa) rilasciato con il paper [Optimal Subarchitecture Extraction For BERT](https://huggingface.co/papers/2010.10499) da Adrian de Wynter e Daniel J. Perry. +1. **[ByT5](model_doc/byt5)** (da Google Research) rilasciato con il paper [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) da Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel. +1. **[CamemBERT](model_doc/camembert)** (da Inria/Facebook/Sorbonne) rilasciato con il paper [CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) da Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suรกrez*, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la Clergerie, Djamรฉ Seddah e Benoรฎt Sagot. +1. **[CANINE](model_doc/canine)** (da Google Research) rilasciato con il paper [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://huggingface.co/papers/2103.06874) da Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. +1. **[ConvNeXT](model_doc/convnext)** (da Facebook AI) rilasciato con il paper [A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) da Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. +1. **[ConvNeXTV2](model_doc/convnextv2)** (da Facebook AI) rilasciato con il paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) da Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. +1. **[CLIP](model_doc/clip)** (da OpenAI) rilasciato con il paper [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) da Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. +1. **[ConvBERT](model_doc/convbert)** (da YituTech) rilasciato con il paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) da Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan. +1. **[CPM](model_doc/cpm)** (dalla Universitร  di Tsinghua) rilasciato con il paper [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) da Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun. +1. **[CTRL](model_doc/ctrl)** (da Salesforce) rilasciato con il paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) da Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong e Richard Socher. +1. **[CvT](model_doc/cvt)** (da Microsoft) rilasciato con il paper [CvT: Introducing Convolutions to Vision Transformers](https://huggingface.co/papers/2103.15808) da Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang. +1. **[Data2Vec](model_doc/data2vec)** (da Facebook) rilasciato con il paper [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) da Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli. +1. **[DeBERTa](model_doc/deberta)** (da Microsoft) rilasciato con il paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) da Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[DeBERTa-v2](model_doc/deberta-v2)** (da Microsoft) rilasciato con il paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) da Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[Decision Transformer](model_doc/decision_transformer)** (da Berkeley/Facebook/Google) rilasciato con il paper [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) da Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. +1. **[DiT](model_doc/dit)** (da Microsoft Research) rilasciato con il paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://huggingface.co/papers/2203.02378) da Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei. +1. **[DeiT](model_doc/deit)** (da Facebook) rilasciato con il paper [Training data-efficient image transformers & distillation through attention](https://huggingface.co/papers/2012.12877) da Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervรฉ Jรฉgou. +1. **[DETR](model_doc/detr)** (da Facebook) rilasciato con il paper [End-to-End Object Detection with Transformers](https://huggingface.co/papers/2005.12872) da Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko. +1. **[DialoGPT](model_doc/dialogpt)** (da Microsoft Research) rilasciato con il paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) da Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan. +1. **[DistilBERT](model_doc/distilbert)** (da HuggingFace), rilasciato assieme al paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://huggingface.co/papers/1910.01108) da Victor Sanh, Lysandre Debut e Thomas Wolf. La stessa tecnica รจ stata applicata per comprimere GPT2 in [DistilGPT2](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), RoBERTa in [DistilRoBERTa](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), Multilingual BERT in [DistilmBERT](https://github.com/huggingface/transformers-research-projects/tree/main/distillation) and a German version of DistilBERT. +1. **[DPR](model_doc/dpr)** (da Facebook) rilasciato con il paper [Dense Passage Retrieval for Open-Domain Question Answering](https://huggingface.co/papers/2004.04906) da Vladimir Karpukhin, Barlas OฤŸuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, e Wen-tau Yih. +1. **[DPT](master/model_doc/dpt)** (da Intel Labs) rilasciato con il paper [Vision Transformers for Dense Prediction](https://huggingface.co/papers/2103.13413) da Renรฉ Ranftl, Alexey Bochkovskiy, Vladlen Koltun. +1. **[EfficientNet](model_doc/efficientnet)** (from Google Research) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://huggingface.co/papers/1905.11946) by Mingxing Tan and Quoc V. Le. +1. **[EncoderDecoder](model_doc/encoder-decoder)** (da Google Research) rilasciato con il paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) da Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[ELECTRA](model_doc/electra)** (da Google Research/Stanford University) rilasciato con il paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://huggingface.co/papers/2003.10555) da Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning. +1. **[FlauBERT](model_doc/flaubert)** (da CNRS) rilasciato con il paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://huggingface.co/papers/1912.05372) da Hang Le, Loรฏc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoรฎt Crabbรฉ, Laurent Besacier, Didier Schwab. +1. **[FLAVA](model_doc/flava)** (da Facebook AI) rilasciato con il paper [FLAVA: A Foundational Language And Vision Alignment Model](https://huggingface.co/papers/2112.04482) da Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, e Douwe Kiela. +1. **[FNet](model_doc/fnet)** (da Google Research) rilasciato con il paper [FNet: Mixing Tokens with Fourier Transforms](https://huggingface.co/papers/2105.03824) da James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. +1. **[Funnel Transformer](model_doc/funnel)** (da CMU/Google Brain) rilasciato con il paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://huggingface.co/papers/2006.03236) da Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le. +1. **[GLPN](model_doc/glpn)** (da KAIST) rilasciato con il paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://huggingface.co/papers/2201.07436) da Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim. +1. **[GPT](model_doc/openai-gpt)** (da OpenAI) rilasciato con il paper [Improving Language Understanding by Generative Pre-Training](https://openai.com/research/language-unsupervised/) da Alec Radford, Karthik Narasimhan, Tim Salimans e Ilya Sutskever. +1. **[GPT-2](model_doc/gpt2)** (da OpenAI) rilasciato con il paper [Language Models are Unsupervised Multitask Learners](https://openai.com/research/better-language-models/) da Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei e Ilya Sutskever. +1. **[GPT-J](model_doc/gptj)** (da EleutherAI) rilasciato nel repository [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) da Ben Wang e Aran Komatsuzaki. +1. **[GPT Neo](model_doc/gpt_neo)** (da EleutherAI) rilasciato nel repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) da Sid Black, Stella Biderman, Leo Gao, Phil Wang e Connor Leahy. +1. **[GPT NeoX](model_doc/gpt_neox)** (da EleutherAI) rilasciato con il paper [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://huggingface.co/papers/2204.06745) da Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach +1. **[Hubert](model_doc/hubert)** (da Facebook) rilasciato con il paper [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://huggingface.co/papers/2106.07447) da Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed. +1. **[I-BERT](model_doc/ibert)** (da Berkeley) rilasciato con il paper [I-BERT: Integer-only BERT Quantization](https://huggingface.co/papers/2101.01321) da Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer. +1. **[ImageGPT](model_doc/imagegpt)** (da OpenAI) rilasciato con il paper [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) da Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. +1. **[LayoutLM](model_doc/layoutlm)** (da Microsoft Research Asia) rilasciato con il paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://huggingface.co/papers/1912.13318) da Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou. +1. **[LayoutLMv2](model_doc/layoutlmv2)** (da Microsoft Research Asia) rilasciato con il paper [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://huggingface.co/papers/2012.14740) da Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. +1. **[LayoutLMv3](model_doc/layoutlmv3)** (da Microsoft Research Asia) rilasciato con il paper [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://huggingface.co/papers/2204.08387) da Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei. +1. **[LayoutXLM](model_doc/layoutlxlm)** (da Microsoft Research Asia) rilasciato con il paper [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://huggingface.co/papers/2104.08836) da Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei. +1. **[LED](model_doc/led)** (da AllenAI) rilasciato con il paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) da Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[Longformer](model_doc/longformer)** (da AllenAI) rilasciato con il paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) da Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LUKE](model_doc/luke)** (da Studio Ousia) rilasciato con il paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://huggingface.co/papers/2010.01057) da Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto. +1. **[mLUKE](model_doc/mluke)** (da Studio Ousia) rilasciato con il paper [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://huggingface.co/papers/2110.08151) da Ryokan Ri, Ikuya Yamada, e Yoshimasa Tsuruoka. +1. **[LXMERT](model_doc/lxmert)** (da UNC Chapel Hill) rilasciato con il paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://huggingface.co/papers/1908.07490) da Hao Tan e Mohit Bansal. +1. **[M2M100](model_doc/m2m_100)** (da Facebook) rilasciato con il paper [Beyond English-Centric Multilingual Machine Translation](https://huggingface.co/papers/2010.11125) da Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin. +1. **[MarianMT](model_doc/marian)** Modello di machine learning per le traduzioni allenato utilizzando i dati [OPUS](http://opus.nlpl.eu/) di Jรถrg Tiedemann. Il [Framework Marian](https://marian-nmt.github.io/) รจ stato sviluppato dal Microsoft Translator Team. +1. **[Mask2Former](model_doc/mask2former)** (da FAIR e UIUC) rilasciato con il paper [Masked-attention Mask Transformer for Universal Image Segmentation](https://huggingface.co/papers/2112.01527) da Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. +1. **[MaskFormer](model_doc/maskformer)** (da Meta e UIUC) rilasciato con il paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://huggingface.co/papers/2107.06278) da Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. +1. **[MBart](model_doc/mbart)** (da Facebook) rilasciato con il paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://huggingface.co/papers/2001.08210) da Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer. +1. **[MBart-50](model_doc/mbart)** (da Facebook) rilasciato con il paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://huggingface.co/papers/2008.00401) da Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan. +1. **[Megatron-BERT](model_doc/megatron-bert)** (da NVIDIA) rilasciato con il paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) da Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper e Bryan Catanzaro. +1. **[Megatron-GPT2](model_doc/megatron_gpt2)** (da NVIDIA) rilasciato con il paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) da Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper e Bryan Catanzaro. +1. **[MPNet](model_doc/mpnet)** (da Microsoft Research) rilasciato con il paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://huggingface.co/papers/2004.09297) da Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu. +1. **[MT5](model_doc/mt5)** (da Google AI) rilasciato con il paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://huggingface.co/papers/2010.11934) da Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel. +1. **[Nystrรถmformer](model_doc/nystromformer)** (dalla Universitร  del Wisconsin - Madison) rilasciato con il paper [Nystrรถmformer: A Nystrรถm-Based Algorithm for Approximating Self-Attention](https://huggingface.co/papers/2102.03902) da Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh. +1. **[OneFormer](model_doc/oneformer)** (da SHI Labs) rilasciato con il paper [OneFormer: One Transformer to Rule Universal Image Segmentation](https://huggingface.co/papers/2211.06220) da Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. +1. **[OPT](master/model_doc/opt)** (da Meta AI) rilasciato con il paper [OPT: Open Pre-trained Transformer Language Models](https://huggingface.co/papers/2205.01068) da Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al. +1. **[Pegasus](model_doc/pegasus)** (da Google) rilasciato con il paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://huggingface.co/papers/1912.08777) da Jingqing Zhang, Yao Zhao, Mohammad Saleh e Peter J. Liu. +1. **[Perceiver IO](model_doc/perceiver)** (da Deepmind) rilasciato con il paper [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://huggingface.co/papers/2107.14795) da Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hรฉnaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, Joรฃo Carreira. +1. **[PhoBERT](model_doc/phobert)** (da VinAI Research) rilasciato con il paper [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) da Dat Quoc Nguyen e Anh Tuan Nguyen. +1. **[PLBart](model_doc/plbart)** (da UCLA NLP) rilasciato con il paper [Unified Pre-training for Program Understanding and Generation](https://huggingface.co/papers/2103.06333) da Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang. +1. **[PoolFormer](model_doc/poolformer)** (da Sea AI Labs) rilasciato con il paper [MetaFormer is Actually What You Need for Vision](https://huggingface.co/papers/2111.11418) da Yu, Weihao e Luo, Mi e Zhou, Pan e Si, Chenyang e Zhou, Yichen e Wang, Xinchao e Feng, Jiashi e Yan, Shuicheng. +1. **[ProphetNet](model_doc/prophetnet)** (da Microsoft Research) rilasciato con il paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) da Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang e Ming Zhou. +1. **[QDQBert](model_doc/qdqbert)** (da NVIDIA) rilasciato con il paper [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://huggingface.co/papers/2004.09602) da Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev e Paulius Micikevicius. +1. **[REALM](model_doc/realm.html)** (da Google Research) rilasciato con il paper [REALM: Retrieval-Augmented Language Model Pre-Training](https://huggingface.co/papers/2002.08909) da Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat e Ming-Wei Chang. +1. **[Reformer](model_doc/reformer)** (da Google Research) rilasciato con il paper [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) da Nikita Kitaev, ลukasz Kaiser, Anselm Levskaya. +1. **[RemBERT](model_doc/rembert)** (da Google Research) rilasciato con il paper [Rethinking embedding coupling in pre-trained language models](https://huggingface.co/papers/2010.12821) da Hyung Won Chung, Thibault Fรฉvry, Henry Tsai, M. Johnson, Sebastian Ruder. +1. **[RegNet](model_doc/regnet)** (da META Platforms) rilasciato con il paper [Designing Network Design Space](https://huggingface.co/papers/2003.13678) da Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollรกr. +1. **[ResNet](model_doc/resnet)** (da Microsoft Research) rilasciato con il paper [Deep Residual Learning for Image Recognition](https://huggingface.co/papers/1512.03385) da Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. +1. **[RoBERTa](model_doc/roberta)** (da Facebook), rilasciato assieme al paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692) da Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. +1. **[RoFormer](model_doc/roformer)** (da ZhuiyiTechnology), rilasciato assieme al paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://huggingface.co/papers/2104.09864) da Jianlin Su e Yu Lu e Shengfeng Pan e Bo Wen e Yunfeng Liu. +1. **[SegFormer](model_doc/segformer)** (da NVIDIA) rilasciato con il paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://huggingface.co/papers/2105.15203) da Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo. +1. **[SEW](model_doc/sew)** (da ASAPP) rilasciato con il paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) da Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SEW-D](model_doc/sew_d)** (da ASAPP) rilasciato con il paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) da Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SpeechToTextTransformer](model_doc/speech_to_text)** (da Facebook), rilasciato assieme al paper [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://huggingface.co/papers/2010.05171) da Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. +1. **[SpeechToTextTransformer2](model_doc/speech_to_text_2)** (da Facebook), rilasciato assieme al paper [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://huggingface.co/papers/2104.06678) da Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. +1. **[Splinter](model_doc/splinter)** (dalla Universitร  di Tel Aviv), rilasciato assieme al paper [Few-Shot Question Answering by Pretraining Span Selection](https://huggingface.co/papers/2101.00438) da Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. +1. **[SqueezeBert](model_doc/squeezebert)** (da Berkeley) rilasciato con il paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://huggingface.co/papers/2006.11316) da Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, e Kurt W. Keutzer. +1. **[Swin Transformer](model_doc/swin)** (da Microsoft) rilasciato con il paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030) da Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. +1. **[T5](model_doc/t5)** (da Google AI) rilasciato con il paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://huggingface.co/papers/1910.10683) da Colin Raffel e Noam Shazeer e Adam Roberts e Katherine Lee e Sharan Narang e Michael Matena e Yanqi Zhou e Wei Li e Peter J. Liu. +1. **[T5v1.1](model_doc/t5v1.1)** (da Google AI) rilasciato nel repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) da Colin Raffel e Noam Shazeer e Adam Roberts e Katherine Lee e Sharan Narang e Michael Matena e Yanqi Zhou e Wei Li e Peter J. Liu. +1. **[TAPAS](model_doc/tapas)** (da Google AI) rilasciato con il paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://huggingface.co/papers/2004.02349) da Jonathan Herzig, Paweล‚ Krzysztof Nowak, Thomas Mรผller, Francesco Piccinno e Julian Martin Eisenschlos. +1. **[TAPEX](model_doc/tapex)** (da Microsoft Research) rilasciato con il paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://huggingface.co/papers/2107.07653) da Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou. +1. **[Trajectory Transformer](model_doc/trajectory_transformers)** (dall'Universitร  della California a Berkeley) rilasciato con il paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://huggingface.co/papers/2106.02039) da Michael Janner, Qiyang Li, Sergey Levine +1. **[Transformer-XL](model_doc/transfo-xl)** (da Google/CMU) rilasciato con il paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://huggingface.co/papers/1901.02860) da Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. +1. **[TrOCR](model_doc/trocr)** (da Microsoft), rilasciato assieme al paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://huggingface.co/papers/2109.10282) da Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei. +1. **[UniSpeech](model_doc/unispeech)** (da Microsoft Research) rilasciato con il paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://huggingface.co/papers/2101.07597) da Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang. +1. **[UniSpeechSat](model_doc/unispeech-sat)** (da Microsoft Research) rilasciato con il paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://huggingface.co/papers/2110.05752) da Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu. +1. **[VAN](model_doc/van)** (dalle Universitร  di Tsinghua e Nankai) rilasciato con il paper [Visual Attention Network](https://huggingface.co/papers/2202.09741) da Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu. +1. **[ViLT](model_doc/vilt)** (da NAVER AI Lab/Kakao Enterprise/Kakao Brain) rilasciato con il paper [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://huggingface.co/papers/2102.03334) da Wonjae Kim, Bokyung Son, Ildoo Kim. +1. **[Vision Transformer (ViT)](model_doc/vit)** (da Google AI) rilasciato con il paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) da Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. +1. **[ViTMAE](model_doc/vit_mae)** (da Meta AI) rilasciato con il paper [Masked Autoencoders Are Scalable Vision Learners](https://huggingface.co/papers/2111.06377) da Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollรกr, Ross Girshick. +1. **[VisualBERT](model_doc/visual_bert)** (da UCLA NLP) rilasciato con il paper [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://huggingface.co/papers/1908.03557) da Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang. +1. **[WavLM](model_doc/wavlm)** (da Microsoft Research) rilasciato con il paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://huggingface.co/papers/2110.13900) da Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei. +1. **[Wav2Vec2](model_doc/wav2vec2)** (da Facebook AI) rilasciato con il paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://huggingface.co/papers/2006.11477) da Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli. +1. **[Wav2Vec2Phoneme](model_doc/wav2vec2_phoneme)** (da Facebook AI) rilasciato con il paper [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://huggingface.co/papers/2109.11680) da Qiantong Xu, Alexei Baevski, Michael Auli. +1. **[XGLM](model_doc/xglm)** (da Facebook AI) rilasciato con il paper [Few-shot Learning with Multilingual Language Models](https://huggingface.co/papers/2112.10668) da Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O'Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li. +1. **[XLM](model_doc/xlm)** (v Facebook) rilasciato assieme al paper [Cross-lingual Language Model Pretraining](https://huggingface.co/papers/1901.07291) da Guillaume Lample e Alexis Conneau. +1. **[XLM-ProphetNet](model_doc/xlm-prophetnet)** (da Microsoft Research) rilasciato con il paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) da Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang e Ming Zhou. +1. **[XLM-RoBERTa](model_doc/xlm-roberta)** (da Facebook AI), rilasciato assieme al paper [Unsupervised Cross-lingual Representation Learning at Scale](https://huggingface.co/papers/1911.02116) da Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmรกn, Edouard Grave, Myle Ott, Luke Zettlemoyer e Veselin Stoyanov. +1. **[XLM-RoBERTa-XL](model_doc/xlm-roberta-xl)** (da Facebook AI), rilasciato assieme al paper [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://huggingface.co/papers/2105.00572) da Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau. +1. **[XLNet](model_doc/xlnet)** (da Google/CMU) rilasciato con il paper [โ€‹XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://huggingface.co/papers/1906.08237) da Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. +1. **[XLSR-Wav2Vec2](model_doc/xlsr_wav2vec2)** (da Facebook AI) rilasciato con il paper [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://huggingface.co/papers/2006.13979) da Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli. +1. **[XLS-R](model_doc/xls_r)** (da Facebook AI) rilasciato con il paper [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://huggingface.co/papers/2111.09296) da Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli. +1. **[YOLOS](model_doc/yolos)** (dalla Universitร  della scienza e tecnologia di Huazhong) rilasciato con il paper [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://huggingface.co/papers/2106.00666) da Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu. +1. **[YOSO](model_doc/yoso)** (dall'Universitร  del Wisconsin - Madison) rilasciato con il paper [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://huggingface.co/papers/2111.09714) da Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh. + + +### Framework supportati + +La tabella seguente rappresenta il supporto attuale nella libreria per ognuno di questi modelli, si puรฒ identificare se questi hanno un Python +tokenizer (chiamato "slow"). Un tokenizer "fast" supportato dalla libreria ๐Ÿค— Tokenizers, e se hanno supporto in Jax (via Flax), PyTorch, e/o TensorFlow. + + + +| Model | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | +|:---------------------------:|:--------------:|:--------------:|:---------------:|:------------------:|:------------:| +| ALBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| BART | โœ… | โœ… | โœ… | โœ… | โœ… | +| BEiT | โŒ | โŒ | โœ… | โŒ | โœ… | +| BERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| Bert Generation | โœ… | โŒ | โœ… | โŒ | โŒ | +| BigBird | โœ… | โœ… | โœ… | โŒ | โœ… | +| BigBirdPegasus | โŒ | โŒ | โœ… | โŒ | โŒ | +| Blenderbot | โœ… | โœ… | โœ… | โœ… | โœ… | +| BlenderbotSmall | โœ… | โœ… | โœ… | โœ… | โœ… | +| CamemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| Canine | โœ… | โŒ | โœ… | โŒ | โŒ | +| CLIP | โœ… | โœ… | โœ… | โœ… | โœ… | +| ConvBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ConvNext | โŒ | โŒ | โœ… | โœ… | โŒ | +| CTRL | โœ… | โŒ | โœ… | โœ… | โŒ | +| CvT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecAudio | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecText | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecVision | โŒ | โŒ | โœ… | โœ… | โŒ | +| DeBERTa | โœ… | โœ… | โœ… | โœ… | โŒ | +| DeBERTa-v2 | โœ… | โœ… | โœ… | โœ… | โŒ | +| Decision Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeiT | โŒ | โŒ | โœ… | โŒ | โŒ | +| DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DistilBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| DPR | โœ… | โœ… | โœ… | โœ… | โŒ | +| DPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ELECTRA | โœ… | โœ… | โœ… | โœ… | โœ… | +| Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| FairSeq Machine-Translation | โœ… | โŒ | โœ… | โŒ | โŒ | +| FlauBERT | โœ… | โŒ | โœ… | โœ… | โŒ | +| Flava | โŒ | โŒ | โœ… | โŒ | โŒ | +| FNet | โœ… | โœ… | โœ… | โŒ | โŒ | +| Funnel Transformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| GLPN | โŒ | โŒ | โœ… | โŒ | โŒ | +| GPT Neo | โŒ | โŒ | โœ… | โŒ | โœ… | +| GPT NeoX | โŒ | โœ… | โœ… | โŒ | โŒ | +| GPT-J | โŒ | โŒ | โœ… | โœ… | โœ… | +| Hubert | โŒ | โŒ | โœ… | โœ… | โŒ | +| I-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ImageGPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| LayoutLM | โœ… | โœ… | โœ… | โœ… | โŒ | +| LayoutLMv2 | โœ… | โœ… | โœ… | โŒ | โŒ | +| LayoutLMv3 | โœ… | โœ… | โœ… | โœ… | โŒ | +| LED | โœ… | โœ… | โœ… | โœ… | โŒ | +| Longformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| LUKE | โœ… | โŒ | โœ… | โŒ | โŒ | +| LXMERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| M2M100 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Marian | โœ… | โŒ | โœ… | โœ… | โœ… | +| MaskFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| mBART | โœ… | โœ… | โœ… | โœ… | โœ… | +| MegatronBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| MPNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| mT5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Nystromformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OpenAI GPT | โœ… | โœ… | โœ… | โœ… | โŒ | +| OpenAI GPT-2 | โœ… | โœ… | โœ… | โœ… | โœ… | +| OPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Pegasus | โœ… | โœ… | โœ… | โœ… | โœ… | +| Perceiver | โœ… | โŒ | โœ… | โŒ | โŒ | +| PLBart | โœ… | โŒ | โœ… | โŒ | โŒ | +| PoolFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| QDQBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| RAG | โœ… | โŒ | โœ… | โœ… | โŒ | +| Realm | โœ… | โœ… | โœ… | โŒ | โŒ | +| Reformer | โœ… | โœ… | โœ… | โŒ | โŒ | +| RegNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ResNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RetriBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| RoFormer | โœ… | โœ… | โœ… | โœ… | โœ… | +| SegFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW-D | โŒ | โŒ | โœ… | โŒ | โŒ | +| Speech Encoder decoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| Speech2Text | โœ… | โŒ | โœ… | โœ… | โŒ | +| Speech2Text2 | โœ… | โŒ | โŒ | โŒ | โŒ | +| Splinter | โœ… | โœ… | โœ… | โŒ | โŒ | +| SqueezeBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| Swin | โŒ | โŒ | โœ… | โœ… | โŒ | +| T5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| TAPAS | โœ… | โŒ | โœ… | โœ… | โŒ | +| Trajectory Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Transformer-XL | โœ… | โŒ | โœ… | โœ… | โŒ | +| TrOCR | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeech | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeechSat | โŒ | โŒ | โœ… | โŒ | โŒ | +| VAN | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Vision Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| VisionTextDualEncoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| VisualBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViT | โŒ | โŒ | โœ… | โœ… | โœ… | +| ViTMAE | โŒ | โŒ | โœ… | โœ… | โŒ | +| Wav2Vec2 | โœ… | โŒ | โœ… | โœ… | โœ… | +| Wav2Vec2-Conformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| WavLM | โŒ | โŒ | โœ… | โŒ | โŒ | +| XGLM | โœ… | โœ… | โœ… | โŒ | โœ… | +| XLM | โœ… | โŒ | โœ… | โœ… | โŒ | +| XLM-RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM-RoBERTa-XL | โŒ | โŒ | โœ… | โŒ | โŒ | +| XLMProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| XLNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| YOLOS | โŒ | โŒ | โœ… | โŒ | โŒ | +| YOSO | โŒ | โŒ | โœ… | โŒ | โŒ | + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/installation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/installation.md new file mode 100644 index 0000000000000000000000000000000000000000..a4f444c1eb0c4cbb71218c7a7f66ffe7ff230456 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/installation.md @@ -0,0 +1,239 @@ + + +# Installazione + +Installa ๐Ÿค— Transformers per qualsiasi libreria di deep learning con cui stai lavorando, imposta la tua cache, e opzionalmente configura ๐Ÿค— Transformers per l'esecuzione offline. + +๐Ÿค— Transformers รจ testato su Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, e Flax. Segui le istruzioni di installazione seguenti per la libreria di deep learning che stai utilizzando: + +* [PyTorch](https://pytorch.org/get-started/locally/) istruzioni di installazione. +* [TensorFlow 2.0](https://www.tensorflow.org/install/pip) istruzioni di installazione. +* [Flax](https://flax.readthedocs.io/en/latest/) istruzioni di installazione. + +## Installazione con pip + +Puoi installare ๐Ÿค— Transformers in un [ambiente virtuale](https://docs.python.org/3/library/venv.html). Se non sei familiare con gli ambienti virtuali in Python, dai un'occhiata a questa [guida](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). Un ambiente virtuale rende piรน semplice la gestione di progetti differenti, evitando problemi di compatibilitร  tra dipendenze. + +Inizia creando un ambiente virtuale nella directory del tuo progetto: + +```bash +python -m venv .env +``` + +Attiva l'ambiente virtuale: + +```bash +source .env/bin/activate +``` + +Ora puoi procedere con l'installazione di ๐Ÿค— Transformers eseguendo il comando seguente: + +```bash +pip install transformers +``` + +Per il solo supporto della CPU, puoi installare facilmente ๐Ÿค— Transformers e una libreria di deep learning in solo una riga. Ad esempio, installiamo ๐Ÿค— Transformers e PyTorch con: + +```bash +pip install transformers[torch] +``` + +๐Ÿค— Transformers e TensorFlow 2.0: + +```bash +pip install transformers[tf-cpu] +``` + +๐Ÿค— Transformers e Flax: + +```bash +pip install transformers[flax] +``` + +Infine, verifica se ๐Ÿค— Transformers รจ stato installato in modo appropriato eseguendo il seguente comando. Questo scaricherร  un modello pre-allenato: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" +``` + +Dopodichรฉ stampa l'etichetta e il punteggio: + +```bash +[{'label': 'POSITIVE', 'score': 0.9998704791069031}] +``` + +## Installazione dalla fonte + +Installa ๐Ÿค— Transformers dalla fonte con il seguente comando: + +```bash +pip install git+https://github.com/huggingface/transformers +``` + +Questo comando installa la versione `main` piรน attuale invece dell'ultima versione stabile. Questo รจ utile per stare al passo con gli ultimi sviluppi. Ad esempio, se un bug รจ stato sistemato da quando รจ uscita l'ultima versione ufficiale ma non รจ stata ancora rilasciata una nuova versione. Tuttavia, questo significa che questa versione `main` puรฒ non essere sempre stabile. Ci sforziamo per mantenere la versione `main` operativa, e la maggior parte dei problemi viene risolta in poche ore o in un giorno. Se riscontri un problema, per favore apri una [Issue](https://github.com/huggingface/transformers/issues) cosรฌ possiamo sistemarlo ancora piรน velocemente! + +Controlla se ๐Ÿค— Transformers รจ stata installata in modo appropriato con il seguente comando: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" +``` + +## Installazione modificabile + +Hai bisogno di un'installazione modificabile se vuoi: + +* Usare la versione `main` del codice dalla fonte. +* Contribuire a ๐Ÿค— Transformers e hai bisogno di testare i cambiamenti nel codice. + +Clona il repository e installa ๐Ÿค— Transformers con i seguenti comandi: + +```bash +git clone https://github.com/huggingface/transformers.git +cd transformers +pip install -e . +``` + +Questi comandi collegheranno la cartella in cui รจ stato clonato il repository e i path delle librerie Python. Python guarderร  ora all'interno della cartella clonata, oltre ai normali path delle librerie. Per esempio, se i tuoi pacchetti Python sono installati tipicamente in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python cercherร  anche nella cartella clonata: `~/transformers/`. + + + +Devi tenere la cartella `transformers` se vuoi continuare ad utilizzare la libreria. + + + +Ora puoi facilmente aggiornare il tuo clone all'ultima versione di ๐Ÿค— Transformers con il seguente comando: + +```bash +cd ~/transformers/ +git pull +``` + +Il tuo ambiente Python troverร  la versione `main` di ๐Ÿค— Transformers alla prossima esecuzione. + +## Installazione con conda + +Installazione dal canale conda `conda-forge`: + +```bash +conda install conda-forge::transformers +``` + +## Impostazione della cache + +I modelli pre-allenati sono scaricati e memorizzati localmente nella cache in: `~/.cache/huggingface/transformers/`. Questa รจ la directory di default data dalla variabile d'ambiente della shell `TRANSFORMERS_CACHE`. Su Windows, la directory di default รจ data da `C:\Users\username\.cache\huggingface\transformers`. Puoi cambiare le variabili d'ambiente della shell indicate in seguito, in ordine di prioritร , per specificare una directory differente per la cache: + +1. Variabile d'ambiente della shell (default): `TRANSFORMERS_CACHE`. +2. Variabile d'ambiente della shell: `HF_HOME` + `transformers/`. +3. Variabile d'ambiente della shell: `XDG_CACHE_HOME` + `/huggingface/transformers`. + + + +๐Ÿค— Transformers utilizzerร  le variabili d'ambiente della shell `PYTORCH_TRANSFORMERS_CACHE` o `PYTORCH_PRETRAINED_BERT_CACHE` se si proviene da un'iterazione precedente di questa libreria e sono state impostate queste variabili d'ambiente, a meno che non si specifichi la variabile d'ambiente della shell `TRANSFORMERS_CACHE`. + + + +## Modalitร  Offline + +๐Ÿค— Transformers puรฒ essere eseguita in un ambiente firewalled o offline utilizzando solo file locali. Imposta la variabile d'ambiente `HF_HUB_OFFLINE=1` per abilitare questo comportamento. + + + +Aggiungi [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/) al tuo flusso di lavoro offline di training impostando la variabile d'ambiente `HF_DATASETS_OFFLINE=1`. + + + +Ad esempio, in genere si esegue un programma su una rete normale, protetta da firewall per le istanze esterne, con il seguente comando: + +```bash +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +Esegui lo stesso programma in un'istanza offline con: + +```bash +HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +Lo script viene ora eseguito senza bloccarsi o attendere il timeout, perchรฉ sa di dover cercare solo file locali. + +### Ottenere modelli e tokenizer per l'uso offline + +Un'altra opzione per utilizzare offline ๐Ÿค— Transformers รจ scaricare i file in anticipo, e poi puntare al loro path locale quando hai la necessitร  di utilizzarli offline. Ci sono tre modi per fare questo: + +* Scarica un file tramite l'interfaccia utente sul [Model Hub](https://huggingface.co/models) premendo sull'icona โ†“. + + ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) + +* Utilizza il flusso [`PreTrainedModel.from_pretrained`] e [`PreTrainedModel.save_pretrained`]: + + 1. Scarica i tuoi file in anticipo con [`PreTrainedModel.from_pretrained`]: + + ```py + >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + + >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") + >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") + ``` + + 2. Salva i tuoi file in una directory specificata con [`PreTrainedModel.save_pretrained`]: + + ```py + >>> tokenizer.save_pretrained("./il/tuo/path/bigscience_t0") + >>> model.save_pretrained("./il/tuo/path/bigscience_t0") + ``` + + 3. Ora quando sei offline, carica i tuoi file con [`PreTrainedModel.from_pretrained`] dalla directory specificata: + + ```py + >>> tokenizer = AutoTokenizer.from_pretrained("./il/tuo/path/bigscience_t0") + >>> model = AutoModel.from_pretrained("./il/tuo/path/bigscience_t0") + ``` + +* Scarica in maniera programmatica i file con la libreria [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub): + + 1. Installa la libreria `huggingface_hub` nel tuo ambiente virtuale: + + ```bash + python -m pip install huggingface_hub + ``` + + 2. Utilizza la funzione [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) per scaricare un file in un path specifico. Per esempio, il seguente comando scarica il file `config.json` dal modello [T0](https://huggingface.co/bigscience/T0_3B) nel path che desideri: + + ```py + >>> from huggingface_hub import hf_hub_download + + >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./il/tuo/path/bigscience_t0") + ``` + +Una volta che il tuo file รจ scaricato e salvato in cache localmente, specifica il suo path locale per caricarlo e utilizzarlo: + +```py +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("./il/tuo/path/bigscience_t0/config.json") +``` + + + +Fai riferimento alla sezione [How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream) per avere maggiori dettagli su come scaricare modelli presenti sull Hub. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/migration.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/migration.md new file mode 100644 index 0000000000000000000000000000000000000000..07d31705784e7fa626a9aadf011aaf5a88e96e09 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/migration.md @@ -0,0 +1,313 @@ + + +# Migrazione da pacchetti precedenti + +## Migrazione da transformers `v3.x` a `v4.x` + +Un paio di modifiche sono state introdotte nel passaggio dalla versione 3 alla versione 4. Di seguito รจ riportato un riepilogo delle +modifiche previste: + +#### 1. AutoTokenizer e pipeline ora utilizzano tokenizer veloci (rust) per impostazione predefinita. + +I tokenizer python e rust hanno all'incirca le stesse API, ma i tokenizer rust hanno un set di funzionalitร  piรน completo. + +Ciรฒ introduce due modifiche sostanziali: +- La gestione dei token in overflow tra i tokenizer Python e Rust รจ diversa. +- I tokenizers di rust non accettano numeri interi nei metodi di codifica. + +##### Come ottenere lo stesso comportamento di v3.x in v4.x + +- Le pipeline ora contengono funzionalitร  aggiuntive pronte all'uso. Vedi la [pipeline di classificazione dei token con il flag `grouped_entities`](main_classes/pipelines#transformers.TokenClassificationPipeline). +- Gli auto-tokenizer ora restituiscono tokenizer rust. Per ottenere invece i tokenizer python, l'utente deve usare il flag `use_fast` impostandolo `False`: + +Nella versione `v3.x`: +```py +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") +``` +per ottenere lo stesso nella versione `v4.x`: +```py +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased", use_fast=False) +``` + +#### 2. SentencePiece รจ stato rimosso dalle dipendenze richieste + +Il requisito sulla dipendenza SentencePiece รจ stato rimosso da `setup.py`. รˆ stato fatto per avere un canale su anaconda cloud senza basarsi su `conda-forge`. Ciรฒ significa che i tokenizer che dipendono dalla libreria SentencePiece non saranno disponibili con un'installazione standard di `transformers`. + +Ciรฒ include le versioni **lente** di: +- `XLNetTokenizer` +- `AlbertTokenizer` +- `CamembertTokenizer` +- `MBartTokenizer` +- `PegasusTokenizer` +- `T5Tokenizer` +- `ReformerTokenizer` +- `XLMRobertaTokenizer` + +##### Come ottenere lo stesso comportamento della v3.x nella v4.x + +Per ottenere lo stesso comportamento della versione `v3.x`, devi installare anche `sentencepiece`: + +Nella versione `v3.x`: +```bash +pip install transformers +``` +per ottenere lo stesso nella versione `v4.x`: +```bash +pip install transformers[sentencepiece] +``` +o +```bash +pip install transformers stentencepiece +``` +#### 3. L'architettura delle repo รจ stato aggiornata in modo che ogni modello abbia la propria cartella + +Con lโ€™aggiunta di nuovi modelli, il numero di file nella cartella `src/transformers` continua a crescere e diventa piรน difficile navigare e capire. Abbiamo fatto la scelta di inserire ogni modello e i file che lo accompagnano nelle proprie sottocartelle. + +Si tratta di una modifica sostanziale in quanto l'importazione di layer intermedi utilizzando direttamente il modulo di un modello deve essere eseguita tramite un percorso diverso. + +##### Come ottenere lo stesso comportamento della v3.x nella v4.x + +Per ottenere lo stesso comportamento della versione `v3.x`, devi aggiornare il percorso utilizzato per accedere ai layer. + +Nella versione `v3.x`: +```bash +from transformers.modeling_bert import BertLayer +``` +per ottenere lo stesso nella versione `v4.x`: +```bash +from transformers.models.bert.modeling_bert import BertLayer +``` + +#### 4. Impostare l'argomento `return_dict` su `True` per impostazione predefinita + +L'[argomento `return_dict`](main_classes/output) abilita la restituzione di oggetti python dict-like contenenti gli output del modello, invece delle tuple standard. Questo oggetto รจ self-documented poichรฉ le chiavi possono essere utilizzate per recuperare valori, comportandosi anche come una tupla e gli utenti possono recuperare oggetti per indexing o slicing. + +Questa รจ una modifica sostanziale poichรฉ la tupla non puรฒ essere decompressa: `value0, value1 = outputs` non funzionerร . + +##### Come ottenere lo stesso comportamento della v3.x nella v4.x + +Per ottenere lo stesso comportamento della versione `v3.x`, specifica l'argomento `return_dict` come `False`, sia nella configurazione del modello che nel passaggio successivo. + +Nella versione `v3.x`: +```bash +model = BertModel.from_pretrained("google-bert/bert-base-cased") +outputs = model(**inputs) +``` +per ottenere lo stesso nella versione `v4.x`: +```bash +model = BertModel.from_pretrained("google-bert/bert-base-cased") +outputs = model(**inputs, return_dict=False) +``` +o +```bash +model = BertModel.from_pretrained("google-bert/bert-base-cased", return_dict=False) +outputs = model(**inputs) +``` + +#### 5. Rimozione di alcuni attributi deprecati + +Gli attributi sono stati rimossi se deprecati da almeno un mese. L'elenco completo degli attributi obsoleti รจ disponibile in [#8604](https://github.com/huggingface/transformers/pull/8604). + +Ecco un elenco di questi attributi/metodi/argomenti e quali dovrebbero essere le loro sostituzioni: + +In diversi modelli, le etichette diventano coerenti con gli altri modelli: +- `masked_lm_labels` diventa `labels` in `AlbertForMaskedLM` e `AlbertForPreTraining`. +- `masked_lm_labels` diventa `labels` in `BertForMaskedLM` e `BertForPreTraining`. +- `masked_lm_labels` diventa `labels` in `DistilBertForMaskedLM`. +- `masked_lm_labels` diventa `labels` in `ElectraForMaskedLM`. +- `masked_lm_labels` diventa `labels` in `LongformerForMaskedLM`. +- `masked_lm_labels` diventa `labels` in `MobileBertForMaskedLM`. +- `masked_lm_labels` diventa `labels` in `RobertaForMaskedLM`. +- `lm_labels` diventa `labels` in `BartForConditionalGeneration`. +- `lm_labels` diventa `labels` in `GPT2DoubleHeadsModel`. +- `lm_labels` diventa `labels` in `OpenAIGPTDoubleHeadsModel`. +- `lm_labels` diventa `labels` in `T5ForConditionalGeneration`. + +In diversi modelli, il meccanismo di memorizzazione nella cache diventa coerente con gli altri: +- `decoder_cached_states` diventa `past_key_values` in tutti i modelli BART-like, FSMT e T5. +- `decoder_past_key_values` diventa `past_key_values` in tutti i modelli BART-like, FSMT e T5. +- `past` diventa `past_key_values` in tutti i modelli CTRL. +- `past` diventa `past_key_values` in tutti i modelli GPT-2. + +Per quanto riguarda le classi tokenizer: +- L'attributo tokenizer `max_len` diventa `model_max_length`. +- L'attributo tokenizer `return_lengths` diventa `return_length`. +- L'argomento di codifica del tokenizer `is_pretokenized` diventa `is_split_into_words`. + +Per quanto riguarda la classe `Trainer`: +- L'argomento `tb_writer` di `Trainer` รจ stato rimosso in favore della funzione richiamabile `TensorBoardCallback(tb_writer=...)`. +- L'argomento `prediction_loss_only` di `Trainer` รจ stato rimosso in favore dell'argomento di classe `args.prediction_loss_only`. +- L'attributo `data_collator` di `Trainer` sarร  richiamabile. +- Il metodo `_log` di `Trainer` รจ deprecato a favore di `log`. +- Il metodo `_training_step` di `Trainer` รจ deprecato a favore di `training_step`. +- Il metodo `_prediction_loop` di `Trainer` รจ deprecato a favore di `prediction_loop`. +- Il metodo `is_local_master` di `Trainer` รจ deprecato a favore di `is_local_process_zero`. +- Il metodo `is_world_master` di `Trainer` รจ deprecato a favore di `is_world_process_zero`. + +Per quanto riguarda la classe `TrainingArguments`: +- L'argomento `evaluate_during_training` di `TrainingArguments` รจ deprecato a favore di `eval_strategy`. + +Per quanto riguarda il modello Transfo-XL: +- L'attributo di configurazione `tie_weight` di Transfo-XL diventa `tie_words_embeddings`. +- Il metodo di modellazione `reset_length` di Transfo-XL diventa `reset_memory_length`. + +Per quanto riguarda le pipeline: +- L'argomento `topk` di `FillMaskPipeline` diventa `top_k`. + + + +## Passaggio da pytorch-transformers a ๐Ÿค— Transformers + +Ecco un breve riepilogo di ciรฒ a cui prestare attenzione durante il passaggio da `pytorch-transformers` a ๐Ÿค— Transformers. + +### Lโ€™ordine posizionale di alcune parole chiave di input dei modelli (`attention_mask`, `token_type_ids`...) รจ cambiato + +Per usare Torchscript (vedi #1010, #1204 e #1195) l'ordine specifico delle **parole chiave di input** di alcuni modelli (`attention_mask`, `token_type_ids`...) รจ stato modificato. + +Se inizializzavi i modelli usando parole chiave per gli argomenti, ad esempio `model(inputs_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)`, questo non dovrebbe causare alcun cambiamento. + +Se inizializzavi i modelli con input posizionali per gli argomenti, ad esempio `model(inputs_ids, attention_mask, token_type_ids)`, potrebbe essere necessario ricontrollare l'ordine esatto degli argomenti di input. + +## Migrazione da pytorch-pretrained-bert + +Ecco un breve riepilogo di ciรฒ a cui prestare attenzione durante la migrazione da `pytorch-pretrained-bert` a ๐Ÿค— Transformers + +### I modelli restituiscono sempre `tuple` + +La principale modifica di rilievo durante la migrazione da `pytorch-pretrained-bert` a ๐Ÿค— Transformers รจ che il metodo dei modelli di previsione dร  sempre una `tupla` con vari elementi a seconda del modello e dei parametri di configurazione. + +Il contenuto esatto delle tuple per ciascun modello รจ mostrato in dettaglio nelle docstring dei modelli e nella [documentazione](https://huggingface.co/transformers/). + +In quasi tutti i casi, andrร  bene prendendo il primo elemento dell'output come quello che avresti precedentemente utilizzato in `pytorch-pretrained-bert`. + +Ecco un esempio di conversione da `pytorch-pretrained-bert` + a ๐Ÿค— Transformers per un modello di classificazione `BertForSequenceClassification`: + +```python +# Carichiamo il nostro modello +model = BertForSequenceClassification.from_pretrained("google-bert/bert-base-uncased") + +# Se usavi questa riga in pytorch-pretrained-bert : +loss = model(input_ids, labels=labels) + +# Ora usa questa riga in ๐Ÿค— Transformers per estrarre la perdita dalla tupla di output: +outputs = model(input_ids, labels=labels) +loss = outputs[0] + +# In ๐Ÿค— Transformers puoi anche avere accesso ai logit: +loss, logits = outputs[:2] + +# Ed anche agli attention weight se configuri il modello per restituirli (e anche altri output, vedi le docstring e la documentazione) +model = BertForSequenceClassification.from_pretrained(" google-bert/bert-base-uncased", output_attentions=True) +outputs = model(input_ids, labels=labels) +loss, logits, attentions = outputs +``` + +### Serializzazione + +Modifica sostanziale nel metodo `from_pretrained()`: + +1. I modelli sono ora impostati in modalitร  di valutazione in maniera predefinita quando usi il metodo `from_pretrained()`. Per addestrarli non dimenticare di riportarli in modalitร  di addestramento (`model.train()`) per attivare i moduli di dropout. + +2. Gli argomenti aggiuntivi `*inputs` e `**kwargs` forniti al metodo `from_pretrained()` venivano passati direttamente al metodo `__init__()` della classe sottostante del modello. Ora sono usati per aggiornare prima l'attributo di configurazione del modello, che puรฒ non funzionare con le classi del modello derivate costruite basandosi sui precedenti esempi di `BertForSequenceClassification`. Piรน precisamente, gli argomenti posizionali `*inputs` forniti a `from_pretrained()` vengono inoltrati direttamente al metodo `__init__()` del modello mentre gli argomenti keyword `**kwargs` (i) che corrispondono agli attributi della classe di configurazione, vengono utilizzati per aggiornare tali attributi (ii) che non corrispondono ad alcun attributo della classe di configurazione, vengono inoltrati al metodo `__init__()`. + +Inoltre, sebbene non si tratti di una modifica sostanziale, i metodi di serializzazione sono stati standardizzati e probabilmente dovresti passare al nuovo metodo `save_pretrained(save_directory)` se prima usavi qualsiasi altro metodo di serializzazione. + +Ecco un esempio: + +```python +### Carichiamo un modello e un tokenizer +model = BertForSequenceClassification.from_pretrained("google-bert/bert-base-uncased") +tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") + +### Facciamo fare alcune cose al nostro modello e tokenizer +# Es: aggiungiamo nuovi token al vocabolario e agli embending del nostro modello +tokenizer.add_tokens(["[SPECIAL_TOKEN_1]", "[SPECIAL_TOKEN_2]"]) +model.resize_token_embeddings(len(tokenizer)) +# Alleniamo il nostro modello +train(model) + +### Ora salviamo il nostro modello e il tokenizer in una cartella +model.save_pretrained("./my_saved_model_directory/") +tokenizer.save_pretrained("./my_saved_model_directory/") + +### Ricarichiamo il modello e il tokenizer +model = BertForSequenceClassification.from_pretrained("./my_saved_model_directory/") +tokenizer = BertTokenizer.from_pretrained("./my_saved_model_directory/") +``` + +### Ottimizzatori: BertAdam e OpenAIAdam ora sono AdamW, lo scheduling รจ quello standard PyTorch + +I due ottimizzatori precedenti inclusi, `BertAdam` e `OpenAIAdam`, sono stati sostituiti da un singolo `AdamW` che presenta alcune differenze: + +- implementa solo la correzione del weights decay, +- lo scheduling ora รจ esterno (vedi sotto), +- anche il gradient clipping ora รจ esterno (vedi sotto). + +Il nuovo ottimizzatore `AdamW` corrisponde alle API di `Adam` di PyTorch e ti consente di utilizzare metodi PyTorch o apex per lo scheduling e il clipping. + +Lo scheduling รจ ora standard [PyTorch learning rate schedulers](https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate) e non fanno piรน parte dell'ottimizzatore. + +Ecco un esempio di linear warmup e decay con `BertAdam` e con `AdamW`: + +```python +# Parametri: +lr = 1e-3 +max_grad_norm = 1.0 +num_training_steps = 1000 +num_warmup_steps = 100 +warmup_proportion = float( num_warmup_steps) / float(num_training_steps) # 0.1 + +### In precedenza l'ottimizzatore BertAdam veniva istanziato in questo modo: +optimizer = BertAdam( + model.parameters(), + lr=lr, + schedule="warmup_linear", + warmup=warmup_proportion, + num_training_steps=num_training_steps, +) +### e usato in questo modo: +for batch in train_data: + loss = model(batch) + loss.backward() + optimizer.step() + +### In ๐Ÿค— Transformers, ottimizzatore e schedule sono divisi e usati in questo modo: +optimizer = AdamW( + model.parameters(), lr=lr, correct_bias=False +) # Per riprodurre il comportamento specifico di BertAdam impostare correct_bias=False +scheduler = get_linear_schedule_with_warmup( + optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps +) # PyTorch scheduler +### e va usato cosรฌ: +for batch in train_data: + loss = model(batch) + loss.backward() + torch.nn.utils.clip_grad_norm_( + model.parameters(), max_grad_norm + ) # Gradient clipping non รจ piรน in AdamW (quindi puoi usare amp senza problemi) + optimizer.step() + scheduler.step() +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/model_sharing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/model_sharing.md new file mode 100644 index 0000000000000000000000000000000000000000..c6efa717efb82401c89b6d0ae70a4ca873423632 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/model_sharing.md @@ -0,0 +1,238 @@ + + +# Condividi un modello + +Gli ultimi due tutorial ti hanno mostrato come puoi fare fine-tuning di un modello con PyTorch, Keras e ๐Ÿค— Accelerate per configurazioni distribuite. Il prossimo passo รจ quello di condividere il tuo modello con la community! In Hugging Face, crediamo nella condivisione della conoscenza e delle risorse in modo da democratizzare l'intelligenza artificiale per chiunque. Ti incoraggiamo a considerare di condividere il tuo modello con la community per aiutare altre persone a risparmiare tempo e risorse. + +In questo tutorial, imparerai due metodi per la condivisione di un modello trained o fine-tuned nel [Model Hub](https://huggingface.co/models): + +- Condividi in modo programmatico i tuoi file nell'Hub. +- Trascina i tuoi file nell'Hub mediante interfaccia grafica. + + + + + +Per condividere un modello con la community, hai bisogno di un account su [huggingface.co](https://huggingface.co/join). Puoi anche unirti ad un'organizzazione esistente o crearne una nuova. + + + +## Caratteristiche dei repository + +Ogni repository nel Model Hub si comporta come un tipico repository di GitHub. I nostri repository offrono il versionamento, la cronologia dei commit, e la possibilitร  di visualizzare le differenze. + +Il versionamento all'interno del Model Hub รจ basato su git e [git-lfs](https://git-lfs.github.com/). In altre parole, puoi trattare un modello come un unico repository, consentendo un maggiore controllo degli accessi e maggiore scalabilitร . Il controllo delle versioni consente *revisions*, un metodo per appuntare una versione specifica di un modello con un hash di commit, un tag o un branch. + +Come risultato, puoi caricare una specifica versione di un modello con il parametro `revision`: + +```py +>>> model = AutoModel.from_pretrained( +... "julien-c/EsperBERTo-small", revision="4c77982" # nome di un tag, di un branch, o commit hash +... ) +``` + +Anche i file possono essere modificati facilmente in un repository ed รจ possibile visualizzare la cronologia dei commit e le differenze: + +![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png) + +## Configurazione + +Prima di condividere un modello nell'Hub, hai bisogno delle tue credenziali di Hugging Face. Se hai accesso ad un terminale, esegui il seguente comando nell'ambiente virtuale in cui รจ installata la libreria ๐Ÿค— Transformers. Questo memorizzerร  il tuo token di accesso nella cartella cache di Hugging Face (di default `~/.cache/`): + +```bash +hf auth login +``` + +Se stai usando un notebook come Jupyter o Colaboratory, assicurati di avere la libreria [`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library) installata. Questa libreria ti permette di interagire in maniera programmatica con l'Hub. + +```bash +pip install huggingface_hub +``` + +Utilizza `notebook_login` per accedere all'Hub, e segui il link [qui](https://huggingface.co/settings/token) per generare un token con cui effettuare il login: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Converti un modello per tutti i framework + +Per assicurarti che il tuo modello possa essere utilizzato da persone che lavorano con un framework differente, ti raccomandiamo di convertire e caricare il tuo modello sia con i checkpoint di PyTorch che con quelli di TensorFlow. Anche se รจ possibile caricare il modello da un framework diverso, se si salta questo passaggio, il caricamento sarร  piรน lento perchรฉ ๐Ÿค— Transformers ha bisogno di convertire i checkpoint al momento. + +Convertire un checkpoint per un altro framework รจ semplice. Assicurati di avere PyTorch e TensorFlow installati (vedi [qui](installation) per le istruzioni d'installazione), e poi trova il modello specifico per il tuo compito nell'altro framework. + + + +Specifica `from_tf=True` per convertire un checkpoint da TensorFlow a PyTorch: + +```py +>>> pt_model = DistilBertForSequenceClassification.from_pretrained( +... "path/verso/il-nome-magnifico-che-hai-scelto", from_tf=True +... ) +>>> pt_model.save_pretrained("path/verso/il-nome-magnifico-che-hai-scelto") +``` + + +Specifica `from_pt=True` per convertire un checkpoint da PyTorch a TensorFlow: + +```py +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained( +... "path/verso/il-nome-magnifico-che-hai-scelto", from_pt=True +... ) +``` + +Poi puoi salvare il tuo nuovo modello in TensorFlow con il suo nuovo checkpoint: + +```py +>>> tf_model.save_pretrained("path/verso/il-nome-magnifico-che-hai-scelto") +``` + + +Se un modello รจ disponibile in Flax, puoi anche convertire un checkpoint da PyTorch a Flax: + +```py +>>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( +... "path/verso/il-nome-magnifico-che-hai-scelto", from_pt=True +... ) +``` + + + +## Condividi un modello durante il training + + + + + +Condividere un modello nell'Hub รจ tanto semplice quanto aggiungere un parametro extra o un callback. Ricorda dal [tutorial sul fine-tuning](training), la classe [`TrainingArguments`] รจ dove specifichi gli iperparametri e le opzioni addizionali per l'allenamento. Una di queste opzioni di training include l'abilitร  di condividere direttamente un modello nell'Hub. Imposta `push_to_hub=True` in [`TrainingArguments`]: + +```py +>>> training_args = TrainingArguments(output_dir="il-mio-bellissimo-modello", push_to_hub=True) +``` + +Passa gli argomenti per il training come di consueto al [`Trainer`]: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +Dopo aver effettuato il fine-tuning del tuo modello, chiama [`~transformers.Trainer.push_to_hub`] sul [`Trainer`] per condividere il modello allenato nell'Hub. ๐Ÿค— Transformers aggiungerร  in modo automatico persino gli iperparametri, i risultati del training e le versioni del framework alla scheda del tuo modello (model card, in inglese)! + +```py +>>> trainer.push_to_hub() +``` + + +Condividi un modello nell'Hub con [`PushToHubCallback`]. Nella funzione [`PushToHubCallback`], aggiungi: + +- Una directory di output per il tuo modello. +- Un tokenizer. +- L'`hub_model_id`, che รจ il tuo username sull'Hub e il nome del modello. + +```py +>>> from transformers import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="./il_path_dove_salvare_il_tuo_modello", +... tokenizer=tokenizer, +... hub_model_id="il-tuo-username/il-mio-bellissimo-modello", +... ) +``` + +Aggiungi il callback a [`fit`](https://keras.io/api/models/model_training_apis/), e ๐Ÿค— Transformers caricherร  il modello allenato nell'Hub: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback) +``` + + + +## Utilizzare la funzione `push_to_hub` + +Puoi anche chiamare `push_to_hub` direttamente sul tuo modello per caricarlo nell'Hub. + +Specifica il nome del tuo modello in `push_to_hub`: + +```py +>>> pt_model.push_to_hub("il-mio-bellissimo-modello") +``` + +Questo crea un repository sotto il proprio username con il nome del modello `il-mio-bellissimo-modello`. Ora chiunque puรฒ caricare il tuo modello con la funzione `from_pretrained`: + +```py +>>> from transformers import AutoModel + +>>> model = AutoModel.from_pretrained("il-tuo-username/il-mio-bellissimo-modello") +``` + +Se fai parte di un'organizzazione e vuoi invece condividere un modello sotto il nome dell'organizzazione, aggiungi il parametro `organization`: + +```py +>>> pt_model.push_to_hub("il-mio-bellissimo-modello", organization="la-mia-fantastica-org") +``` + +La funzione `push_to_hub` puรฒ essere anche utilizzata per aggiungere altri file al repository del modello. Per esempio, aggiungi un tokenizer ad un repository di un modello: + +```py +>>> tokenizer.push_to_hub("il-mio-bellissimo-modello") +``` + +O magari potresti voler aggiungere la versione di TensorFlow del tuo modello PyTorch a cui hai fatto fine-tuning: + +```py +>>> tf_model.push_to_hub("il-mio-bellissimo-modello") +``` + +Ora quando navighi nel tuo profilo Hugging Face, dovresti vedere il tuo repository del modello appena creato. Premendo sulla scheda **Files** vengono visualizzati tutti i file caricati nel repository. + +Per maggiori dettagli su come creare e caricare file ad un repository, fai riferimento alla documentazione [qui](https://huggingface.co/docs/hub/how-to-upstream). + +## Carica un modello utilizzando l'interfaccia web + +Chi preferisce un approccio senza codice puรฒ caricare un modello tramite l'interfaccia web dell'hub. Visita [huggingface.co/new](https://huggingface.co/new) per creare un nuovo repository: + +![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png) + +Da qui, aggiungi alcune informazioni sul tuo modello: + +- Seleziona il/la **owner** del repository. Puoi essere te o qualunque organizzazione di cui fai parte. +- Scegli un nome per il tuo modello, il quale sarร  anche il nome del repository. +- Scegli se il tuo modello รจ pubblico o privato. +- Specifica la licenza utilizzata per il tuo modello. + +Ora premi sulla scheda **Files** e premi sul pulsante **Add file** per caricare un nuovo file al tuo repository. Trascina poi un file per caricarlo e aggiungere un messaggio di commit. + +![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png) + +## Aggiungi una scheda del modello + +Per assicurarti che chiunque possa comprendere le abilitร , limitazioni, i potenziali bias e le considerazioni etiche del tuo modello, per favore aggiungi una scheda del modello (model card, in inglese) al tuo repository. La scheda del modello รจ definita nel file `README.md`. Puoi aggiungere una scheda del modello: + +* Creando manualmente e caricando un file `README.md`. +* Premendo sul pulsante **Edit model card** nel repository del tuo modello. + +Dai un'occhiata alla [scheda del modello](https://huggingface.co/distilbert/distilbert-base-uncased) di DistilBert per avere un buon esempio del tipo di informazioni che una scheda di un modello deve includere. Per maggiori dettagli legati ad altre opzioni che puoi controllare nel file `README.md`, come l'impatto ambientale o widget di esempio, fai riferimento alla documentazione [qui](https://huggingface.co/docs/hub/models-cards). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/multilingual.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/multilingual.md new file mode 100644 index 0000000000000000000000000000000000000000..e9e85beec1d96642c72fd88b4c4c09929d95f7d0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/multilingual.md @@ -0,0 +1,178 @@ + + +# Modelli multilingue per l'inferenza + +[[open-in-colab]] + +Ci sono diversi modelli multilingue in ๐Ÿค— Transformers, e il loro utilizzo per l'inferenza differisce da quello dei modelli monolingua. Non *tutti* gli utilizzi dei modelli multilingue sono perรฒ diversi. Alcuni modelli, come [google-bert/bert-base-multilingual-uncased](https://huggingface.co/google-bert/bert-base-multilingual-uncased), possono essere usati come un modello monolingua. Questa guida ti mostrerร  come utilizzare modelli multilingue che utilizzano un modo diverso per fare l'inferenza. + +## XLM + +XLM ha dieci diversi checkpoint, di cui solo uno รจ monolingua. I nove checkpoint rimanenti possono essere suddivisi in due categorie: i checkpoint che utilizzano i language embeddings e quelli che non li utilizzano. + +### XLM con language embeddings + +I seguenti modelli XLM utilizzano gli embeddings linguistici per specificare la lingua utilizzata per l'inferenza: + +- `FacebookAI/xlm-mlm-ende-1024` (Modellazione mascherata del linguaggio (Masked language modeling, in inglese), Inglese-Tedesco) +- `FacebookAI/xlm-mlm-enfr-1024` (Modellazione mascherata del linguaggio, Inglese-Francese) +- `FacebookAI/xlm-mlm-enro-1024` (Modellazione mascherata del linguaggio, Inglese-Rumeno) +- `FacebookAI/xlm-mlm-xnli15-1024` (Modellazione mascherata del linguaggio, lingue XNLI) +- `FacebookAI/xlm-mlm-tlm-xnli15-1024` (Modellazione mascherata del linguaggio + traduzione, lingue XNLI) +- `FacebookAI/xlm-clm-enfr-1024` (Modellazione causale del linguaggio, Inglese-Francese) +- `FacebookAI/xlm-clm-ende-1024` (Modellazione causale del linguaggio, Inglese-Tedesco) + +Gli embeddings linguistici sono rappresentati come un tensore delle stesse dimensioni dell' `input_ids` passato al modello. I valori in questi tensori dipendono dal linguaggio usato e sono identificati dagli attributi `lang2id` e `id2lang` del tokenizer. + +In questo esempio, carica il checkpoint `FacebookAI/xlm-clm-enfr-1024` (Modellazione causale del linguaggio, Inglese-Francese): + +```py +>>> import torch +>>> from transformers import XLMTokenizer, XLMWithLMHeadModel + +>>> tokenizer = XLMTokenizer.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +>>> model = XLMWithLMHeadModel.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +``` + +L'attributo `lang2id` del tokenizer mostra il linguaggio del modello e il suo ids: + +```py +>>> print(tokenizer.lang2id) +{'en': 0, 'fr': 1} +``` + +Poi, crea un esempio di input: + +```py +>>> input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) # batch size of 1 +``` + +Imposta l'id del linguaggio a `"en"` e usalo per definire il language embedding. Il language embedding รจ un tensore riempito con `0` perchรฉ questo รจ il language id per l'inglese. Questo tensore dovrebbe avere la stessa dimensione di `input_ids`. + +```py +>>> language_id = tokenizer.lang2id["en"] # 0 +>>> langs = torch.tensor([language_id] * input_ids.shape[1]) # torch.tensor([0, 0, 0, ..., 0]) + +>>> # We reshape it to be of size (batch_size, sequence_length) +>>> langs = langs.view(1, -1) # is now of shape [1, sequence_length] (we have a batch size of 1) +``` + +Adesso puoi inserire `input_ids` e language embedding nel modello: + +```py +>>> outputs = model(input_ids, langs=langs) +``` + +Lo script [run_generation.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-generation/run_generation.py) puรฒ generare testo tramite i language embeddings usando i checkpoints `xlm-clm`. + +### XLM senza language embeddings + +I seguenti modelli XLM non richiedono l'utilizzo dei language embeddings per fare inferenza: + +- `FacebookAI/xlm-mlm-17-1280` (Modellazione mascherata del linguaggio, 17 lingue) +- `FacebookAI/xlm-mlm-100-1280` (Modellazione mascherata del linguaggio, 100 lingue) + +Questi modelli sono utilizzati per rappresentazioni generiche di frasi, a differenza dei precedenti checkpoints XML. + +## BERT + +Il seguente modello BERT puรฒ essere usato per compiti multilingue: + +- `google-bert/bert-base-multilingual-uncased` (Modellazione mascherata del linguaggio + Previsione della prossima frase, 102 lingue) +- `google-bert/bert-base-multilingual-cased` (Modellazione mascherata del linguaggio + Previsione della prossima frase, 104 lingue) + +Questi modelli non richiedono language embeddings per fare inferenza. Riescono ad identificare il linguaggio dal contesto e inferire di conseguenza. + +## XLM-RoBERTa + +Il seguente modello XLM-RoBERTa puรฒ essere usato per compiti multilingue: + +- `FacebookAI/xlm-roberta-base` (Modellazione mascherata del linguaggio, 100 lingue) +- `FacebookAI/xlm-roberta-large` (Modellazione mascherata del linguaggio, 100 lingue) + +XLM-RoBERTa รจ stato addestrato su 2.5TB di dati CommonCrawl appena creati e puliti in 100 lingue. Offre notevoli vantaggi rispetto ai modelli multilingue rilasciati in precedenza, come mBERT o XLM, in compiti come la classificazione, l'etichettatura delle sequenze e la risposta alle domande. + +## M2M100 + +Il seguente modello M2M100 puรฒ essere usato per compiti multilingue: + +- `facebook/m2m100_418M` (Traduzione) +- `facebook/m2m100_1.2B` (Traduzione) + +In questo esempio, carica il checkpoint `facebook/m2m100_418M` per tradurre dal cinese all'inglese. Puoi impostare la lingua di partenza nel tokenizer: + +```py +>>> from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> chinese_text = "ไธ่ฆๆ’ๆ‰‹ๅทซๅธซ็š„ไบ‹ๅ‹™, ๅ› ็‚บไป–ๅ€‘ๆ˜ฏๅพฎๅฆ™็š„, ๅพˆๅฟซๅฐฑๆœƒ็™ผๆ€’." + +>>> tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="zh") +>>> model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M") +``` + +Applica il tokenizer al testo: + +```py +>>> encoded_zh = tokenizer(chinese_text, return_tensors="pt") +``` + +M2M100 forza l'id della lingua obiettivo come primo token generato per tradurre nella lingua obiettivo. Imposta il parametro `forced_bos_token_id` a `en` nel metodo `generate` per tradurre in inglese: + +```py +>>> generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +'Do not interfere with the matters of the witches, because they are delicate and will soon be angry.' +``` + +## MBart + +Il seguente modello MBart puรฒ essere usato per compiti multilingue: + +- `facebook/mbart-large-50-one-to-many-mmt` (Traduzione automatica multilingue uno-a-molti, 50 lingue) +- `facebook/mbart-large-50-many-to-many-mmt` (Traduzione automatica multilingue molti-a-molti, 50 lingue) +- `facebook/mbart-large-50-many-to-one-mmt` (Traduzione automatica multilingue molti-a-uno, 50 lingue) +- `facebook/mbart-large-50` (Traduzione multilingue, 50 lingue) +- `facebook/mbart-large-cc25` + +In questo esempio, carica il checkpoint `facebook/mbart-large-50-many-to-many-mmt` per tradurre dal finlandese all'inglese. Puoi impostare la lingua di partenza nel tokenizer: + +```py +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> fi_text = "ร„lรค sekaannu velhojen asioihin, sillรค ne ovat hienovaraisia ja nopeasti vihaisia." + +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="fi_FI") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/mbart-large-50-many-to-many-mmt") +``` + +Applica il tokenizer sul testo: + +```py +>>> encoded_en = tokenizer(en_text, return_tensors="pt") +``` + +MBart forza l'id della lingua obiettivo come primo token generato per tradurre nella lingua obiettivo. Imposta il parametro `forced_bos_token_id` a `en` nel metodo `generate` per tradurre in inglese: + +```py +>>> generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id("en_XX")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +"Don't interfere with the wizard's affairs, because they are subtle, will soon get angry." +``` + +Se stai usando il checkpoint `facebook/mbart-large-50-many-to-one-mmt`, non hai bisogno di forzare l'id della lingua obiettivo come primo token generato altrimenti l'uso รจ lo stesso. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_hardware.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_hardware.md new file mode 100644 index 0000000000000000000000000000000000000000..946dcb3238d057def0a73a86e7f483610effafa4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_hardware.md @@ -0,0 +1,155 @@ + + + +# Hardware ottimizzato per l'addestramento + +L'hardware utilizzato per eseguire l'addestramento del modello e l'inferenza puรฒ avere un grande effetto sulle prestazioni. Per un analisi approfondita delle GPUs, assicurati di dare un'occhiata all'eccellente [blog post](https://timdettmers.com/2020/09/07/which-gpu-for-deep-learning/) di Tim Dettmer. + +Diamo un'occhiata ad alcuni consigli pratici per la configurazione della GPU. + +## GPU +Quando si addestrano modelli piรน grandi ci sono essenzialmente tre opzioni: +- GPUs piu' grandi +- Piu' GPUs +- Piu' CPU e piu' NVMe (scaricato da [DeepSpeed-Infinity](main_classes/deepspeed#nvme-support)) + +Iniziamo dal caso in cui ci sia una singola GPU. + +### Potenza e Raffreddamento + +Se hai acquistato una costosa GPU di fascia alta, assicurati di darle la potenza corretta e un raffreddamento sufficiente. + +**Potenza**: + +Alcune schede GPU consumer di fascia alta hanno 2 e talvolta 3 prese di alimentazione PCI-E a 8 pin. Assicurati di avere tanti cavi PCI-E a 8 pin indipendenti da 12 V collegati alla scheda quante sono le prese. Non utilizzare le 2 fessure a un'estremitร  dello stesso cavo (noto anche come cavo a spirale). Cioรจ se hai 2 prese sulla GPU, vuoi 2 cavi PCI-E a 8 pin che vanno dall'alimentatore alla scheda e non uno che abbia 2 connettori PCI-E a 8 pin alla fine! In caso contrario, non otterrai tutte le prestazioni ufficiali. + +Ciascun cavo di alimentazione PCI-E a 8 pin deve essere collegato a una guida da 12 V sul lato dell'alimentatore e puรฒ fornire fino a 150 W di potenza. + +Alcune altre schede possono utilizzare connettori PCI-E a 12 pin e questi possono fornire fino a 500-600 W di potenza. + +Le schede di fascia bassa possono utilizzare connettori a 6 pin, che forniscono fino a 75 W di potenza. + +Inoltre vuoi un alimentatore (PSU) di fascia alta che abbia una tensione stabile. Alcuni PSU di qualitร  inferiore potrebbero non fornire alla scheda la tensione stabile di cui ha bisogno per funzionare al massimo. + +E ovviamente l'alimentatore deve avere abbastanza Watt inutilizzati per alimentare la scheda. + +**Raffreddamento**: + +Quando una GPU si surriscalda, inizierร  a rallentare e non fornirร  le prestazioni mssimali e potrebbe persino spegnersi se diventasse troppo calda. + +รˆ difficile dire l'esatta temperatura migliore a cui aspirare quando una GPU รจ molto caricata, ma probabilmente qualsiasi cosa al di sotto di +80ยฐC va bene, ma piรน bassa รจ meglio - forse 70-75ยฐC รจ un intervallo eccellente in cui trovarsi. รˆ probabile che il rallentamento inizi a circa 84-90ยฐC. Ma oltre alla limitazione delle prestazioni, una temperatura molto elevata prolungata รจ probabile che riduca la durata di una GPU. + +Diamo quindi un'occhiata a uno degli aspetti piรน importanti quando si hanno piรน GPU: la connettivitร . + +### Connettivitร  multi-GPU + +Se utilizzi piรน GPU, il modo in cui le schede sono interconnesse puรฒ avere un enorme impatto sul tempo totale di allenamento. Se le GPU si trovano sullo stesso nodo fisico, puoi eseguire: + +```bash +nvidia-smi topo -m +``` + +e ti dirร  come sono interconnesse le GPU. Su una macchina con doppia GPU e collegata a NVLink, molto probabilmente vedrai qualcosa del tipo: + +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X NV2 0-23 N/A +GPU1 NV2 X 0-23 N/A +``` + +su una macchina diversa senza NVLink potremmo vedere: + +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X PHB 0-11 N/A +GPU1 PHB X 0-11 N/A +``` + +Il rapporto include questa legenda: + +``` + X = Self + SYS = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI) + NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node + PHB = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU) + PXB = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge) + PIX = Connection traversing at most a single PCIe bridge + NV# = Connection traversing a bonded set of # NVLinks +``` + +Quindi il primo rapporto `NV2` ci dice che le GPU sono interconnesse con 2 NVLinks e nel secondo report `PHB` abbiamo una tipica configurazione PCIe+Bridge a livello di consumatore. + +Controlla che tipo di connettivitร  hai sulla tua configurazione. Alcuni di questi renderanno la comunicazione tra le carte piรน veloce (es. NVLink), altri piรน lenta (es. PHB). + +A seconda del tipo di soluzione di scalabilitร  utilizzata, la velocitร  di connettivitร  potrebbe avere un impatto maggiore o minore. Se le GPU devono sincronizzarsi raramente, come in DDP, l'impatto di una connessione piรน lenta sarร  meno significativo. Se le GPU devono scambiarsi messaggi spesso, come in ZeRO-DP, una connettivitร  piรน veloce diventa estremamente importante per ottenere un addestramento piรน veloce. + +#### NVlink + +[NVLink](https://en.wikipedia.org/wiki/NVLink) รจ un collegamento di comunicazione a corto raggio multilinea seriale basato su cavo sviluppato da Nvidia. + +Ogni nuova generazione fornisce una larghezza di banda piรน veloce, ad es. ecco una citazione da [Nvidia Ampere GA102 GPU Architecture](https://www.nvidia.com/content/dam/en-zz/Solutions/geforce/ampere/pdf/NVIDIA-ampere-GA102-GPU-Architecture-Whitepaper-V1.pdf): + +> Third-Generation NVLinkยฎ +> GA102 GPUs utilize NVIDIAโ€™s third-generation NVLink interface, which includes four x4 links, +> with each link providing 14.0625 GB/sec bandwidth in each direction between two GPUs. Four +> links provide 56.25 GB/sec bandwidth in each direction, and 112.5 GB/sec total bandwidth +> between two GPUs. Two RTX 3090 GPUs can be connected together for SLI using NVLink. +> (Note that 3-Way and 4-Way SLI configurations are not supported.) + +Quindi piรน `X` si ottiene nel rapporto di `NVX` nell'output di `nvidia-smi topo -m`, meglio รจ. La generazione dipenderร  dall'architettura della tua GPU. + +Confrontiamo l'esecuzione di un training del modello di linguaggio openai-community/gpt2 su un piccolo campione di wikitext + +I risultati sono: + + +| NVlink | Time | +| ----- | ---: | +| Y | 101s | +| N | 131s | + + +Puoi vedere che NVLink completa l'addestramento circa il 23% piรน velocemente. Nel secondo benchmark utilizziamo `NCCL_P2P_DISABLE=1` per dire alle GPU di non utilizzare NVLink. + +Ecco il codice benchmark completo e gli output: + +```bash +# DDP w/ NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train \ +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 101.9003, 'train_samples_per_second': 1.963, 'epoch': 0.69} + +# DDP w/o NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 NCCL_P2P_DISABLE=1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 131.4367, 'train_samples_per_second': 1.522, 'epoch': 0.69} +``` + +Hardware: 2x TITAN RTX 24GB each + NVlink with 2 NVLinks (`NV2` in `nvidia-smi topo -m`) +Software: `pytorch-1.8-to-be` + `cuda-11.0` / `transformers==4.3.0.dev0` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..5bf48e4737d9412b7d3ca257709451f80d3928ad --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_cpu.md @@ -0,0 +1,79 @@ + + +# Inferenza Efficiente su CPU + +Questa guida si concentra sull'inferenza di modelli di grandi dimensioni in modo efficiente sulla CPU. + +## `BetterTransformer` per inferenza piรน rapida + +Abbiamo integrato di recente `BetterTransformer` per fare inferenza piรน rapidamente con modelli per testi, immagini e audio. Visualizza la documentazione sull'integrazione [qui](https://huggingface.co/docs/optimum/bettertransformer/overview) per maggiori dettagli. + +## PyTorch JIT-mode (TorchScript) + +TorchScript รจ un modo di creare modelli serializzabili e ottimizzabili da codice PyTorch. Ogni programma TorchScript puรฒ esere salvato da un processo Python e caricato in un processo dove non ci sono dipendenze Python. +Comparandolo con l'eager mode di default, jit mode in PyTorch normalmente fornisce prestazioni migliori per l'inferenza del modello da parte di metodologie di ottimizzazione come la operator fusion. + +Per una prima introduzione a TorchScript, vedi la Introduction to [PyTorch TorchScript tutorial](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html#tracing-modules). + +### IPEX Graph Optimization con JIT-mode + +Intelยฎ Extension per PyTorch fornnisce ulteriori ottimizzazioni in jit mode per i modelli della serie Transformers. Consigliamo vivamente agli utenti di usufruire dei vantaggi di Intelยฎ Extension per PyTorch con jit mode. Alcuni operator patterns usati fequentemente dai modelli Transformers models sono giร  supportati in Intelยฎ Extension per PyTorch con jit mode fusions. Questi fusion patterns come Multi-head-attention fusion, Concat Linear, Linear+Add, Linear+Gelu, Add+LayerNorm fusion and etc. sono abilitati e hanno buone performance. I benefici della fusion รจ fornito agli utenti in modo trasparente. In base alle analisi, il ~70% dei problemi piรน popolari in NLP question-answering, text-classification, and token-classification possono avere benefici sulle performance grazie ai fusion patterns sia per Float32 precision che per BFloat16 Mixed precision. + +Vedi maggiori informazioni per [IPEX Graph Optimization](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/graph_optimization.html). + +#### Installazione di IPEX + +I rilasci di IPEX seguono PyTorch, verifica i vari approcci per [IPEX installation](https://intel.github.io/intel-extension-for-pytorch/). + +### Utilizzo del JIT-mode + +Per abilitare JIT-mode in Trainer per evaluation e prediction, devi aggiungere `jit_mode_eval` negli argomenti di Trainer. + + + +per PyTorch >= 1.14.0. JIT-mode potrebe giovare a qualsiasi modello di prediction e evaluaion visto che il dict input รจ supportato in jit.trace + +per PyTorch < 1.14.0. JIT-mode potrebbe giovare ai modelli il cui ordine dei parametri corrisponde all'ordine delle tuple in ingresso in jit.trace, come i modelli per question-answering. +Nel caso in cui l'ordine dei parametri seguenti non corrisponda all'ordine delle tuple in ingresso in jit.trace, come nei modelli di text-classification, jit.trace fallirร  e lo cattureremo con una eccezione al fine di renderlo un fallback. Il logging รจ usato per notificare gli utenti. + + + +Trovi un esempo con caso d'uso in [Transformers question-answering](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) + +- Inference using jit mode on CPU: + +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--jit_mode_eval 
+ +- Inference with IPEX using jit mode on CPU: + +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--use_ipex \
+--jit_mode_eval
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..b78cb34e1d6d813f28a41f98a367f5574cab547b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_many.md @@ -0,0 +1,28 @@ + + +# Inferenza Efficiente su GPU Multiple + +Questo documento contiene informazioni su come fare inferenza in maniera efficiente su GPU multiple. + + + +Nota: Un setup con GPU multiple puรฒ utilizzare la maggior parte delle strategie descritte nella [sezione con GPU singola](./perf_infer_gpu_one). Tuttavia, รจ necessario conoscere delle tecniche semplici che possono essere utilizzate per un risultato migliore. + + + +## `BetterTransformer` per inferenza piรน rapida + +Abbiamo recentemente integrato `BetterTransformer` per inferenza piรน rapida su multi-GPU per modelli su testo, immagini e audio. Controlla il documento con queste integrazioni [qui](https://huggingface.co/docs/optimum/bettertransformer/overview) per maggiori dettagli. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..5339d72d4c9d23e0c0ed5521de94863c8a15a02a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_gpu_one.md @@ -0,0 +1,112 @@ + + +# Inferenza efficiente su GPU singola + +Questo documento sarร  presto completato con informazioni su come effetture l'inferenza su una singola GPU. Nel frattempo รจ possibile consultare [la guida per l'addestramento su una singola GPU](perf_train_gpu_one) e [la guida per l'inferenza su CPU](perf_infer_cpu). + +## `BetterTransformer` per l'inferenza piรน veloce + +Abbiamo recentemente integrato `BetterTransformer` per velocizzare l'inferenza su GPU per modelli di testo, immagini e audio. Per maggiori dettagli, consultare la documentazione su questa integrazione [qui](https://huggingface.co/docs/optimum/bettertransformer/overview). + +## Integrazione di `bitsandbytes` per Int8 mixed-precision matrix decomposition + + + +Nota che questa funzione puรฒ essere utilizzata anche nelle configurazioni multi GPU. + + + +Dal paper [`LLM.int8() : 8-bit Matrix Multiplication for Transformers at Scale`](https://huggingface.co/papers/2208.07339), noi supportiamo l'integrazione di Hugging Face per tutti i modelli dell'Hub con poche righe di codice. +Il metodo `nn.Linear` riduce la dimensione di 2 per i pesi `float16` e `bfloat16` e di 4 per i pesi `float32`, con un impatto quasi nullo sulla qualitร , operando sugli outlier in half-precision. + +![HFxbitsandbytes.png](https://cdn-uploads.huggingface.co/production/uploads/1659861207959-62441d1d9fdefb55a0b7d12c.png) + +Il metodo Int8 mixed-precision matrix decomposition funziona separando la moltiplicazione tra matrici in due flussi: (1) una matrice di flusso di outlier di caratteristiche sistematiche moltiplicata in fp16, (2) in flusso regolare di moltiplicazione di matrici int8 (99,9%). Con questo metodo, รจ possibile effettutare inferenza int8 per modelli molto grandi senza degrado predittivo. +Per maggiori dettagli sul metodo, consultare il [paper](https://huggingface.co/papers/2208.07339) o il nostro [blogpost sull'integrazione](https://huggingface.co/blog/hf-bitsandbytes-integration). + +![MixedInt8.gif](https://cdn-uploads.huggingface.co/production/uploads/1660567469965-62441d1d9fdefb55a0b7d12c.gif) + +Nota che รจ necessaria una GPU per eseguire modelli di tipo mixed-8bit, poichรฉ i kernel sono stati compilati solo per le GPU. Prima di utilizzare questa funzione, assicurarsi di disporre di memoria sufficiente sulla GPU per memorizzare un quarto del modello (o la metร  se i pesi del modello sono in mezza precisione). +Di seguito sono riportate alcune note per aiutarvi a utilizzare questo modulo, oppure seguite le dimostrazioni su [Google colab](#colab-demos). + +### Requisiti + +- Se si dispone di `bitsandbytes<0.37.0`, assicurarsi di eseguire su GPU NVIDIA che supportano tensor cores a 8 bit (Turing, Ampere o architetture piรน recenti - ad esempio T4, RTX20s RTX30s, A40-A100). Per `bitsandbytes>=0.37.0`, tutte le GPU dovrebbero essere supportate. +- Installare la versione corretta di `bitsandbytes` eseguendo: +`pip install bitsandbytes>=0.31.5`. +- Installare `accelerate` +`pip install accelerate>=0.12.0` + +### Esecuzione di modelli mixed-Int8 - configurazione per singola GPU + +Dopo aver installato le librerie necessarie, per caricare il tuo modello mixed 8-bit รจ il seguente: + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +Per la generazione di testo, si consiglia di: + +* utilizzare il metodo `generate()` del modello invece della funzione `pipeline()`. Sebbene l'inferenza sia possibile con la funzione `pipeline()`, essa non รจ ottimizzata per i modelli mixed-8bit e sarร  piรน lenta rispetto all'uso del metodo `generate()`. Inoltre, alcune strategie di campionamento, come il campionamento nucleaus, non sono supportate dalla funzione `pipeline()` per i modelli mixed-8bit. +* collocare tutti gli ingressi sullo stesso dispositivo del modello. + +Ecco un semplice esempio: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +tokenizer = AutoTokenizer.from_pretrained(model_name) +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) + +text = "Hello, my llama is cute" +inputs = tokenizer(prompt, return_tensors="pt").to("cuda") +generated_ids = model.generate(**inputs) +outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +``` + + +### Esecuzione di modelli mixed-8bit - configurazione multi GPU + +Usare il seguente modo caricare il modello mixed-8bit su piรน GPU (stesso comando della configurazione a GPU singola): +```py +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` +Puoi controllare la RAM della GPU che si vuole allocare su ogni GPU usando `accelerate`. Utilizzare l'argomento `max_memory` come segue: + +```py +max_memory_mapping = {0: "1GB", 1: "2GB"} +model_name = "bigscience/bloom-3b" +model_8bit = AutoModelForCausalLM.from_pretrained( + model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping +) +``` +In questo esempio, la prima GPU utilizzerร  1 GB di memoria e la seconda 2 GB. + +### Colab demos + +Con questo metodo รจ possibile inferire modelli che prima non era possibile inferire su Google Colab. +Guardate la demo per l'esecuzione di T5-11b (42GB in fp32)! Utilizzo la quantizzazione a 8 bit su Google Colab: + +[![Open In Colab: T5-11b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing) + +Oppure questa demo di BLOOM-3B: + +[![Open In Colab: BLOOM-3b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_special.md new file mode 100644 index 0000000000000000000000000000000000000000..3e2c0a5c288e379f4aa029bc2cbf6d3d72ea260f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_infer_special.md @@ -0,0 +1,18 @@ + + +# Inferenza su Hardware Specializzato + +Questo documento sarร  completato a breve con la documentazione per l'inferenza su hardware specializzato. Nel frattempo puoi controllare [la guida per fare inferenza sulle CPU](perf_infer_cpu). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..25ee63997b5ebee5471041ae5e065fff9fc592f7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu.md @@ -0,0 +1,69 @@ + + +# Addestramento efficiente su CPU + +Questa guida si concentra su come addestrare in maniera efficiente grandi modelli su CPU. + +## Mixed precision con IPEX + +IPEX รจ ottimizzato per CPU con AVX-512 o superiore, e funziona per le CPU con solo AVX2. Pertanto, si prevede che le prestazioni saranno piรน vantaggiose per le CPU Intel con AVX-512 o superiori, mentre le CPU con solo AVX2 (ad esempio, le CPU AMD o le CPU Intel piรน vecchie) potrebbero ottenere prestazioni migliori con IPEX, ma non sono garantite. IPEX offre ottimizzazioni delle prestazioni per l'addestramento della CPU sia con Float32 che con BFloat16. L'uso di BFloat16 รจ l'argomento principale delle seguenti sezioni. + +Il tipo di dati a bassa precisione BFloat16 รจ stato supportato in modo nativo su 3rd Generation Xeonยฎ Scalable Processors (aka Cooper Lake) con AVX512 e sarร  supportata dalla prossima generazione di Intelยฎ Xeonยฎ Scalable Processors con Intelยฎ Advanced Matrix Extensions (Intelยฎ AMX) instruction set con prestazioni ulteriormente migliorate. L'Auto Mixed Precision per il backende della CPU รจ stato abilitato da PyTorch-1.10. allo stesso tempo, il supporto di Auto Mixed Precision con BFloat16 per CPU e l'ottimizzazione degli operatori BFloat16 รจ stata abilitata in modo massiccio in Intelยฎ Extension per PyTorch, and parzialmente aggiornato al branch master di PyTorch. Gli utenti possono ottenere prestazioni migliori ed users experience con IPEX Auto Mixed Precision.. + +Vedi informazioni piรน dettagliate su [Auto Mixed Precision](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/amp.html). + +### Installazione di IPEX: + +Il rilascio di IPEX segue quello di PyTorch, da installare via pip: + +| PyTorch Version | IPEX version | +| :---------------: | :----------: | +| 1.13 | 1.13.0+cpu | +| 1.12 | 1.12.300+cpu | +| 1.11 | 1.11.200+cpu | +| 1.10 | 1.10.100+cpu | + +```bash +pip install intel_extension_for_pytorch== -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +Vedi altri approcci per [IPEX installation](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/installation.html). + +### Utilizzo nel Trainer + +Per abilitare la auto mixed precision con IPEX in Trainer, l'utende dovrebbe aggiungere `use_ipex`, `bf16` e `no_cuda` negli argomenti del comando di addestramento. + +Vedi un sempio di un caso d'uso [Transformers question-answering](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) + +- Training with IPEX using BF16 auto mixed precision on CPU: + +
 python run_qa.py \
+--model_name_or_path google-bert/bert-base-uncased \
+--dataset_name squad \
+--do_train \
+--do_eval \
+--per_device_train_batch_size 12 \
+--learning_rate 3e-5 \
+--num_train_epochs 2 \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/debug_squad/ \
+--use_ipex \
+--bf16 --no_cuda
+ +### Esempi pratici + +Blog: [Accelerating PyTorch Transformers with Intel Sapphire Rapids](https://huggingface.co/blog/intel-sapphire-rapids) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..c1f8833829ac3bc0df0481f4a64c6102538d27dd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_cpu_many.md @@ -0,0 +1,141 @@ + + +# Addestramento effciente su multiple CPU + +Quando l'addestramento su una singola CPU รจ troppo lento, possiamo usare CPU multiple. Quasta guida si concentra su DDP basato su PyTorch abilitando l'addetramento distribuito su CPU in maniera efficiente. + +## Intelยฎ oneCCL Bindings per PyTorch + +[Intelยฎ oneCCL](https://github.com/oneapi-src/oneCCL) (collective communications library) รจ una libreria per l'addestramento efficiente del deep learning in distribuito e implementa collettivi come allreduce, allgather, alltoall. Per maggiori informazioni su oneCCL, fai riferimento a [oneCCL documentation](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html) e [oneCCL specification](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html). + +Il modulo `oneccl_bindings_for_pytorch` (`torch_ccl` precedentemente alla versione 1.12) implementa PyTorch C10D ProcessGroup API e puรฒ essere caricato dinamicamente com external ProcessGroup e funziona solo su piattaforma Linux al momento. + +Qui trovi informazioni piรน dettagliate per [oneccl_bind_pt](https://github.com/intel/torch-ccl). + +### Intelยฎ oneCCL Bindings per l'installazione PyTorch: + +I file wheel sono disponibili per le seguenti versioni di Python: + +| Extension Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | Python 3.10 | +| :---------------: | :--------: | :--------: | :--------: | :--------: | :---------: | +| 1.13.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.100 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.11.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.10.0 | โˆš | โˆš | โˆš | โˆš | | + +```bash +pip install oneccl_bind_pt=={pytorch_version} -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +dove `{pytorch_version}` deve essere la tua versione di PyTorch, per l'stanza 1.13.0. +Verifica altri approcci per [oneccl_bind_pt installation](https://github.com/intel/torch-ccl). +Le versioni di oneCCL e PyTorch devono combaciare. + + + +oneccl_bindings_for_pytorch 1.12.0 prebuilt wheel does not work with PyTorch 1.12.1 (it is for PyTorch 1.12.0) +PyTorch 1.12.1 should work with oneccl_bindings_for_pytorch 1.12.100 + + + +## Intelยฎ MPI library + +Usa questa implementazione basata su standard MPI per fornire una architettura flessibile, efficiente, scalabile su cluster per Intelยฎ. Questo componente รจ parte di Intelยฎ oneAPI HPC Toolkit. + +oneccl_bindings_for_pytorch รจ installato insieme al set di strumenti MPI. Necessitร  di reperire l'ambiente prima di utilizzarlo. + +per Intelยฎ oneCCL >= 1.12.0 + +```bash +oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") +source $oneccl_bindings_for_pytorch_path/env/setvars.sh +``` + +per Intelยฎ oneCCL con versione < 1.12.0 + +```bash +torch_ccl_path=$(python -c "import torch; import torch_ccl; import os; print(os.path.abspath(os.path.dirname(torch_ccl.__file__)))") +source $torch_ccl_path/env/setvars.sh +``` + +#### Installazione IPEX: + +IPEX fornisce ottimizzazioni delle prestazioni per l'addestramento della CPU sia con Float32 che con BFloat16; puoi fare riferimento a [single CPU section](./perf_train_cpu). + +Il seguente "Utilizzo in Trainer" prende come esempio mpirun nella libreria Intelยฎ MPI. + +## Utilizzo in Trainer + +Per abilitare l'addestramento distribuito multi CPU nel Trainer con il ccl backend, gli utenti devono aggiungere **`--ddp_backend ccl`** negli argomenti del comando. + +Vediamo un esempio per il [question-answering example](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) + +Il seguente comando abilita due processi sul nodo Xeon, con un processo in esecuzione per ogni socket. Le variabili OMP_NUM_THREADS/CCL_WORKER_COUNT possono essere impostate per una prestazione ottimale. + +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=127.0.0.1 + mpirun -n 2 -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex +``` + +Il seguente comando abilita l'addestramento per un totale di quattro processi su due Xeon (node0 e node1, prendendo node0 come processo principale), ppn (processes per node) รจ impostato a 2, on un processo in esecuzione per ogni socket. Le variabili OMP_NUM_THREADS/CCL_WORKER_COUNT possono essere impostate per una prestazione ottimale. + +In node0, รจ necessario creare un file di configurazione che contenga gli indirizzi IP di ciascun nodo (per esempio hostfile) e passare il percorso del file di configurazione come parametro. + +```shell script + cat hostfile + xxx.xxx.xxx.xxx #node0 ip + xxx.xxx.xxx.xxx #node1 ip +``` + +A questo punto, esegui il seguente comando nel nodo0 e **4DDP** sarร  abilitato in node0 e node1 con BF16 auto mixed precision: + +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip + mpirun -f hostfile -n 4 -ppn 2 \ + -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex \ + --bf16 +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_special.md new file mode 100644 index 0000000000000000000000000000000000000000..23ba034e8e2d310dafc1ff8e2eaf6b93467b634f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_special.md @@ -0,0 +1,25 @@ + + +# Addestramento su Hardware Specializzato + + + + Nota: Molte delle strategie introdotte nella [sezione sulla GPU singola](perf_train_gpu_one) (come mixed precision training o gradient accumulation) e [sezione multi-GPU](perf_train_gpu_many) sono generiche e applicabili all'addestramento di modelli in generale quindi assicurati di dargli un'occhiata prima di immergerti in questa sezione. + + + +Questo documento sarร  presto completato con informazioni su come effettuare la formazione su hardware specializzato. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_tpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_tpu.md new file mode 100644 index 0000000000000000000000000000000000000000..663f83c499cba41e8910916b861380cc9072daae --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/perf_train_tpu.md @@ -0,0 +1,24 @@ + + +# Addestramento su TPU + + + + Nota: Molte delle strategie introdotte nella [sezione sulla GPU singola](perf_train_gpu_one) (come mixed precision training o gradient accumulation) e [sezione multi-GPU](perf_train_gpu_many) sono generiche e applicabili all'addestramento di modelli in generale quindi assicurati di dargli un'occhiata prima di immergerti in questa sezione. + + + +Questo documento sarร  presto completato con informazioni su come effettuare la formazione su TPU. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..87f3166623b05a31beecbae1893dc5bc6d12192a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pipeline_tutorial.md @@ -0,0 +1,152 @@ + + +# Pipeline per l'inferenza + +La [`pipeline`] rende semplice usare qualsiasi modello dal [Model Hub](https://huggingface.co/models) per fare inferenza su diversi compiti come generazione del testo, segmentazione di immagini e classificazione di audio. Anche se non hai esperienza con una modalitร  specifica o non comprendi bene il codice che alimenta i modelli, รจ comunque possibile utilizzarli con l'opzione [`pipeline`]! Questa esercitazione ti insegnerร  a: + +* Usare una [`pipeline`] per fare inferenza. +* Usare uno specifico tokenizer o modello. +* Usare una [`pipeline`] per compiti che riguardano audio e video. + + + +Dai un'occhiata alla documentazione di [`pipeline`] per una lista completa dei compiti supportati. + + + +## Utilizzo della Pipeline + +Nonostante ogni compito abbia una [`pipeline`] associata, รจ piรน semplice utilizzare l'astrazione generica della [`pipeline`] che contiene tutte quelle specifiche per ogni mansione. La [`pipeline`] carica automaticamente un modello predefinito e un tokenizer in grado di fare inferenza per il tuo compito. + +1. Inizia creando una [`pipeline`] e specificando il compito su cui fare inferenza: + +```py +>>> from transformers import pipeline + +>>> generator = pipeline(task="text-generation") +``` + +2. Inserisci il testo in input nella [`pipeline`]: + +```py +>>> generator( +... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone" +... ) # doctest: +SKIP +[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Seven for the Iron-priests at the door to the east, and thirteen for the Lord Kings at the end of the mountain'}] +``` + +Se hai piรน di un input, inseriscilo in una lista: + +```py +>>> generator( +... [ +... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone", +... "Nine for Mortal Men, doomed to die, One for the Dark Lord on his dark throne", +... ] +... ) # doctest: +SKIP +``` + +Qualsiasi parametro addizionale per il tuo compito puรฒ essere incluso nella [`pipeline`]. La mansione `text-generation` ha un metodo [`~generation.GenerationMixin.generate`] con diversi parametri per controllare l'output. Ad esempio, se desideri generare piรน di un output, utilizza il parametro `num_return_sequences`: + +```py +>>> generator( +... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone", +... num_return_sequences=2, +... ) # doctest: +SKIP +``` + +### Scegliere modello e tokenizer + +La [`pipeline`] accetta qualsiasi modello dal [Model Hub](https://huggingface.co/models). Ci sono tag nel Model Hub che consentono di filtrare i modelli per attivitร . Una volta che avrai scelto il modello appropriato, caricalo usando la corrispondente classe `AutoModelFor` e [`AutoTokenizer`]. Ad esempio, carica la classe [`AutoModelForCausalLM`] per un compito di causal language modeling: + +```py +>>> from transformers import AutoTokenizer, AutoModelForCausalLM + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +Crea una [`pipeline`] per il tuo compito, specificando il modello e il tokenizer che hai caricato: + +```py +>>> from transformers import pipeline + +>>> generator = pipeline(task="text-generation", model=model, tokenizer=tokenizer) +``` + +Inserisci il testo di input nella [`pipeline`] per generare del testo: + +```py +>>> generator( +... "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone" +... ) # doctest: +SKIP +[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, Seven for the Dragon-lords (for them to rule in a world ruled by their rulers, and all who live within the realm'}] +``` + +## Audio pipeline + +La flessibilitร  della [`pipeline`] fa si che possa essere estesa ad attivitร  sugli audio. + +Per esempio, classifichiamo le emozioni in questo clip audio: + +```py +>>> from datasets import load_dataset +>>> import torch + +>>> torch.manual_seed(42) # doctest: +IGNORE_RESULT +>>> ds = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation") +>>> audio_file = ds[0]["audio"]["path"] +``` + +Trova un modello per la [classificazione audio](https://huggingface.co/models?pipeline_tag=audio-classification) sul Model Hub per eseguire un compito di riconoscimento automatico delle emozioni e caricalo nella [`pipeline`]: + +```py +>>> from transformers import pipeline + +>>> audio_classifier = pipeline( +... task="audio-classification", model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" +... ) +``` + +Inserisci il file audio nella [`pipeline`]: + +```py +>>> preds = audio_classifier(audio_file) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.1315, 'label': 'calm'}, {'score': 0.1307, 'label': 'neutral'}, {'score': 0.1274, 'label': 'sad'}, {'score': 0.1261, 'label': 'fearful'}, {'score': 0.1242, 'label': 'happy'}] +``` + +## Vision pipeline + +Infine, usare la [`pipeline`] per le attivitร  sulle immagini รจ praticamente la stessa cosa. + +Specifica la tua attivitร  e inserisci l'immagine nel classificatore. L'immagine puรฒ essere sia un link che un percorso sul tuo pc in locale. Per esempio, quale specie di gatto รจ raffigurata qui sotto? + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(task="image-classification") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pr_checks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pr_checks.md new file mode 100644 index 0000000000000000000000000000000000000000..caa5fe32965bde77bb52065d94f5f829a432091f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/pr_checks.md @@ -0,0 +1,135 @@ + + +# Controlli su una Pull Request + +Quando apri una pull request sui ๐Ÿค— Transformers, vengono eseguiti un discreto numero di controlli per assicurarsi che la patch che stai aggiungendo non stia rompendo qualcosa di esistente. Questi controlli sono di quattro tipi: +- test regolari +- costruzione della documentazione +- stile del codice e della documentazione +- coerenza generale del repository + +In questo documento, cercheremo di spiegare quali sono i vari controlli e le loro ragioni, oltre a spiegare come eseguire il debug locale se uno di essi fallisce sulla tua PR. + +Nota che tutti richiedono un'installazione dev: + +```bash +pip install transformers[dev] +``` + +o un'installazione modificabile: + +```bash +pip install -e .[dev] +``` + +all'interno del repo Transformers. + +## Tests + +Tutti i job che iniziano con `ci/circleci: run_tests_` eseguono parti della suite di test dei Transformers. Ognuno di questi job si concentra su una parte della libreria in un determinato ambiente: per esempio `ci/circleci: run_tests_pipelines_tf` esegue il test delle pipeline in un ambiente in cui รจ installato solo TensorFlow. + +Nota che per evitare di eseguire i test quando non ci sono cambiamenti reali nei moduli che si stanno testando, ogni volta viene eseguita solo una parte della suite di test: viene eseguita una utility per determinare le differenze nella libreria tra prima e dopo la PR (ciรฒ che GitHub mostra nella scheda "Files changes") e sceglie i test che sono stati impattati dalla diff. Questa utility puรฒ essere eseguita localmente con: + +```bash +python utils/tests_fetcher.py +``` + +dalla root del repo Transformers. Di seguito ciรฒ che farร : + +1. Controlla per ogni file nel diff se le modifiche sono nel codice o solo nei commenti o nelle docstrings. Vengono mantenuti solo i file con modifiche reali al codice. +2. Costruisce una mappa interna che fornisce per ogni file del codice sorgente della libreria tutti i file su cui ha un impatto ricorsivo. Si dice che il modulo A ha un impatto sul modulo B se il modulo B importa il modulo A. Per l'impatto ricorsivo, abbiamo bisogno di una catena di moduli che va dal modulo A al modulo B in cui ogni modulo importa il precedente. +3. Applica questa mappa ai file raccolti nel passaggio 1, si ottiene l'elenco dei file del modello interessati dalla PR. +4. Mappa ciascuno di questi file con i corrispondenti file di test e ottiene l'elenco dei test da eseguire. + +Quando esegui lo script in locale, dovresti ottenere la stampa dei risultati dei passi 1, 3 e 4 e quindi sapere quali test sono stati eseguiti. Lo script creerร  anche un file chiamato `test_list.txt` che contiene l'elenco dei test da eseguire e che puoi eseguire localmente con il seguente comando: + +```bash +python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) +``` + +Nel caso in cui qualcosa sia sfuggito, l'intera suite di test viene eseguita quotidianamente. + +## Build della documentazione + +Il job `ci/circleci: build_doc` esegue una build della documentazione per assicurarsi che tutto sia a posto una volta che la PR รจ stata unita. Se questo passaggio fallisce, puoi controllare localmente entrando nella cartella `docs` del repo Transformers e digitare + +```bash +make html +``` + +Sphinx non รจ noto per i suoi messaggi di errore chiari, quindi potrebbe essere necessario che provi alcune cose per trovare davvero la fonte dell'errore. + +## Stile del codice e della documentazione + +La formattazione del codice viene applicata a tutti i file sorgenti, agli esempi e ai test usando `black` e `isort`. Abbiamo anche uno strumento personalizzato che si occupa della formattazione delle docstring e dei file `rst` (`utils/style_doc.py`), cosรฌ come dell'ordine dei lazy imports eseguiti nei file `__init__.py` dei Transformers (`utils/custom_init_isort.py`). Tutto questo puรฒ essere lanciato eseguendo + +```bash +make style +``` + +I controlli della CI sono applicati all'interno del controllo `ci/circleci: check_code_quality`. Esegue anche `flake8`, che dร  un'occhiata di base al codice e si lamenta se trova una variabile non definita o non utilizzata. Per eseguire questo controllo localmente, usare + +```bash +make quality +``` + +Questa operazione puรฒ richiedere molto tempo, quindi per eseguire la stessa operazione solo sui file modificati nel branch corrente, eseguire + +```bash +make fixup +``` + +Quest'ultimo comando eseguirร  anche tutti i controlli aggiuntivi per la consistenza del repository. Diamogli un'occhiata. + +## Coerenza del repository + +All'interno sono raggruppati tutti i test per assicurarsi che la tua PR lasci il repository in un buono stato ed รจ eseguito dal controllo `ci/circleci: check_repository_consistency`. Puoi eseguire localmente questo controllo eseguendo quanto segue: + +```bash +make repo-consistency +``` + +Questo verifica che: + +- Tutti gli oggetti aggiunti all'init sono documentati (eseguito da `utils/check_repo.py`) +- Tutti i file `__init__.py` hanno lo stesso contenuto nelle loro due sezioni (eseguito da `utils/check_inits.py`) +- Tutto il codice identificato come copia da un altro modulo รจ coerente con l'originale (eseguito da `utils/check_copies.py`) +- Le traduzioni dei README e l'indice della documentazione hanno lo stesso elenco di modelli del README principale (eseguito da `utils/check_copies.py`) +- Le tabelle autogenerate nella documentazione sono aggiornate (eseguito da `utils/check_table.py`) +- La libreria ha tutti gli oggetti disponibili anche se non tutte le dipendenze opzionali sono installate (eseguito da `utils/check_dummies.py`) + +Se questo controllo fallisce, le prime due voci richiedono una correzione manuale, mentre le ultime quattro possono essere corrette automaticamente per te eseguendo il comando + +```bash +make fix-copies +``` + +Ulteriori controlli riguardano le PR che aggiungono nuovi modelli, principalmente che: + +- Tutti i modelli aggiunti sono in un Auto-mapping (eseguita da `utils/check_repo.py`) + +- Tutti i modelli sono testati correttamente (eseguito da `utils/check_repo.py`) + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/preprocessing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/preprocessing.md new file mode 100644 index 0000000000000000000000000000000000000000..6d7bc5b2e3df7e05a92f7b806fd470c457be1c03 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/preprocessing.md @@ -0,0 +1,491 @@ + + +# Preprocess + +[[open-in-colab]] + +Prima di poter usare i dati in un modello, bisogna processarli in un formato accettabile per quest'ultimo. Un modello non comprende il testo grezzo, le immagini o l'audio. Bisogna convertire questi input in numeri e assemblarli all'interno di tensori. In questa esercitazione, tu potrai: + +* Preprocessare dati testuali con un tokenizer. +* Preprocessare immagini o dati audio con un estrattore di caratteristiche. +* Preprocessare dati per attivitร  multimodali mediante un processore. + +## NLP + + + +Lo strumento principale per processare dati testuali รจ un [tokenizer](main_classes/tokenizer). Un tokenizer inizia separando il testo in *tokens* secondo una serie di regole. I tokens sono convertiti in numeri, questi vengono utilizzati per costruire i tensori di input del modello. Anche altri input addizionali se richiesti dal modello vengono aggiunti dal tokenizer. + + + +Se stai pensando si utilizzare un modello preaddestrato, รจ importante utilizzare il tokenizer preaddestrato associato. Questo assicura che il testo sia separato allo stesso modo che nel corpus usato per l'addestramento, e venga usata la stessa mappatura tokens-to-index (solitamente indicato come il *vocabolario*) come nel preaddestramento. + + + +Iniziamo subito caricando un tokenizer preaddestrato con la classe [`AutoTokenizer`]. Questo scarica il *vocabolario* usato quando il modello รจ stato preaddestrato. + +### Tokenize + +Carica un tokenizer preaddestrato con [`AutoTokenizer.from_pretrained`]: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") +``` + +Poi inserisci le tue frasi nel tokenizer: + +```py +>>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.") +>>> print(encoded_input) +{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +Il tokenizer restituisce un dizionario contenente tre oggetti importanti: + +* [input_ids](glossary#input-ids) sono gli indici che corrispondono ad ogni token nella frase. +* [attention_mask](glossary#attention-mask) indicata se un token deve essere elaborato o no. +* [token_type_ids](glossary#token-type-ids) identifica a quale sequenza appartiene un token se รจ presente piรน di una sequenza. + +Si possono decodificare gli `input_ids` per farsi restituire l'input originale: + +```py +>>> tokenizer.decode(encoded_input["input_ids"]) +'[CLS] Do not meddle in the affairs of wizards, for they are subtle and quick to anger. [SEP]' +``` + +Come si puรฒ vedere, il tokenizer aggiunge due token speciali - `CLS` e `SEP` (classificatore e separatore) - alla frase. Non tutti i modelli hanno bisogno dei token speciali, ma se servono, il tokenizer li aggiungerร  automaticamente. + +Se ci sono piรน frasi che vuoi processare, passale come una lista al tokenizer: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_inputs = tokenizer(batch_sentences) +>>> print(encoded_inputs) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1]]} +``` + +### Pad + +Questo รจ un argomento importante. Quando processi un insieme di frasi potrebbero non avere tutte la stessa lunghezza. Questo รจ un problema perchรจ i tensori, in input del modello, devono avere dimensioni uniformi. Il padding รจ una strategia per assicurarsi che i tensori siano rettangolari aggiungendo uno speciale *padding token* alle frasi piรน corte. + +Imposta il parametro `padding` a `True` per imbottire le frasi piรน corte nel gruppo in modo che combacino con la massima lunghezza presente: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + +Nota che il tokenizer aggiunge alle sequenze degli `0` perchรจ sono troppo corte! + +### Truncation + +L'altra faccia della medaglia รจ che avolte le sequenze possono essere troppo lunghe per essere gestite dal modello. In questo caso, avrai bisogno di troncare la sequenza per avere una lunghezza minore. + +Imposta il parametro `truncation` a `True` per troncare una sequenza alla massima lunghezza accettata dal modello: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + +### Costruire i tensori + +Infine, vuoi che il tokenizer restituisca i tensori prodotti dal modello. + +Imposta il parametro `return_tensors` su `pt` per PyTorch, o `tf` per TensorFlow: + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch, padding=True, truncation=True, return_tensors="pt") +>>> print(encoded_input) +{'input_ids': tensor([[ 101, 153, 7719, 21490, 1122, 1114, 9582, 1623, 102], + [ 101, 5226, 1122, 9649, 1199, 2610, 1236, 102, 0]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 0]])} +===PT-TF-SPLIT=== +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch, padding=True, truncation=True, return_tensors="tf") +>>> print(encoded_input) +{'input_ids': , + 'token_type_ids': , + 'attention_mask': } +``` + +## Audio + +Gli input audio sono processati in modo differente rispetto al testo, ma l'obiettivo rimane lo stesso: creare sequenze numeriche che il modello puรฒ capire. Un [estrattore di caratteristiche](main_classes/feature_extractor) รจ progettato con lo scopo preciso di estrarre caratteristiche da immagini o dati audio grezzi e convertirli in tensori. Prima di iniziare, installa ๐Ÿค— Datasets per caricare un dataset audio e sperimentare: + +```bash +pip install datasets +``` + +Carica il dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) (vedi il ๐Ÿค— [Datasets tutorial](https://huggingface.co/docs/datasets/load_hub) per avere maggiori dettagli su come caricare un dataset): + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +Accedi al primo elemento della colonna `audio` per dare uno sguardo all'input. Richiamando la colonna `audio` sarร  caricato automaticamente e ricampionato il file audio: + +```py +>>> dataset[0]["audio"] +{'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414, + 0. , 0. ], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', + 'sampling_rate': 8000} +``` + +Questo restituisce tre oggetti: + +* `array` รจ il segnale vocale caricato - e potenzialmente ricampionato - come vettore 1D. +* `path` il percorso del file audio. +* `sampling_rate` si riferisce al numero di campioni del segnale vocale misurati al secondo. + +### Ricampionamento + +Per questo tutorial, puoi usare il modello [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base). Come puoi vedere dalla model card, il modello Wav2Vec2 รจ preaddestrato su un campionamento vocale a 16kHz.รˆ importante che la frequenza di campionamento dei tuoi dati audio combaci con la frequenza di campionamento del dataset usato per preaddestrare il modello. Se la frequenza di campionamento dei tuoi dati non รจ uguale dovrai ricampionare i tuoi dati audio. + +Per esempio, il dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ha una frequenza di campionamento di 8000kHz. Utilizzando il modello Wav2Vec2 su questo dataset, alzala a 16kHz: + +```py +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +>>> dataset[0]["audio"] +{'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414, + 0. , 0. ], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', + 'sampling_rate': 8000} +``` + +1. Usa il metodo di ๐Ÿค— Datasets' [`cast_column`](https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.cast_column) per alzare la frequenza di campionamento a 16kHz: + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +2. Carica il file audio: + +```py +>>> dataset[0]["audio"] +{'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ..., + 3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', + 'sampling_rate': 16000} +``` + +Come puoi notare, la `sampling_rate` adesso รจ 16kHz! + +### Feature extractor + +Il prossimo passo รจ caricare un estrattore di caratteristiche per normalizzare e fare padding sull'input. Quando applichiamo il padding sui dati testuali, uno `0` รจ aggiunto alle sequenze piรน brevi. La stessa idea si applica ai dati audio, l'estrattore di caratteristiche per gli audio aggiungerร  uno `0` - interpretato come silenzio - agli `array`. + +Carica l'estrattore delle caratteristiche con [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +Inserisci l' `array` audio nell'estrattore delle caratteristiche. Noi raccomandiamo sempre di aggiungere il parametro `sampling_rate` nell'estrattore delle caratteristiche per correggere meglio qualche errore, dovuto ai silenzi, che potrebbe verificarsi. + +```py +>>> audio_input = [dataset[0]["audio"]["array"]] +>>> feature_extractor(audio_input, sampling_rate=16000) +{'input_values': [array([ 3.8106556e-04, 2.7506407e-03, 2.8015103e-03, ..., + 5.6335266e-04, 4.6588284e-06, -1.7142107e-04], dtype=float32)]} +``` + +### Pad e truncate + +Come per il tokenizer, puoi applicare le operazioni padding o truncation per manipolare sequenze di variabili a lotti. Dai uno sguaro alla lunghezza delle sequenze di questi due campioni audio: + +```py +>>> dataset[0]["audio"]["array"].shape +(173398,) + +>>> dataset[1]["audio"]["array"].shape +(106496,) +``` + +Come puoi vedere, il primo campione ha una sequenza piรน lunga del secondo. Crea una funzione che preprocesserร  il dataset. Specifica una lunghezza massima del campione, e l'estrattore di features si occuperร  di riempire o troncare la sequenza per coincidervi: + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, +... sampling_rate=16000, +... padding=True, +... max_length=100000, +... truncation=True, +... ) +... return inputs +``` + +Applica la funzione ai primi esempi nel dataset: + +```py +>>> processed_dataset = preprocess_function(dataset[:5]) +``` + +Adesso guarda la lunghezza dei campioni elaborati: + +```py +>>> processed_dataset["input_values"][0].shape +(100000,) + +>>> processed_dataset["input_values"][1].shape +(100000,) +``` + +La lunghezza dei campioni adesso coincide con la massima lunghezza impostata nelle funzione. + +## Vision + +Un estrattore di caratteristiche si puรฒ usare anche per processare immagini e per compiti di visione. Ancora una volta, l'obiettivo รจ convertire l'immagine grezza in un lotto di tensori come input. + +Carica il dataset [food101](https://huggingface.co/datasets/food101) per questa esercitazione. Usa il parametro `split` di ๐Ÿค— Datasets per caricare solo un piccolo campione dal dataset di addestramento poichรจ il set di dati รจ molto grande: + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("food101", split="train[:100]") +``` + +Secondo passo, dai uno sguardo alle immagini usando la caratteristica [`Image`](https://huggingface.co/docs/datasets/package_reference/main_classes.html?highlight=image#datasets.Image) di ๐Ÿค— Datasets: + +```py +>>> dataset[0]["image"] +``` + +![vision-preprocess-tutorial.png](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png) + +### Feature extractor + +Carica l'estrattore di caratteristiche [`AutoFeatureExtractor.from_pretrained`]: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224") +``` + +### Data augmentation + +Per le attivitร  di visione, รจ usuale aggiungere alcuni tipi di data augmentation alle immagini come parte del preprocessing. Puoi aggiungere augmentations con qualsiasi libreria che preferisci, ma in questa esercitazione, userai il modulo [`transforms`](https://pytorch.org/vision/stable/transforms.html) di torchvision. + +1. Normalizza l'immagine e usa [`Compose`](https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html) per concatenare alcune trasformazioni - [`RandomResizedCrop`](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html) e [`ColorJitter`](https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html) - insieme: + +```py +>>> from torchvision.transforms import Compose, Normalize, RandomResizedCrop, ColorJitter, ToTensor + +>>> normalize = Normalize(mean=feature_extractor.image_mean, std=feature_extractor.image_std) +>>> _transforms = Compose( +... [RandomResizedCrop(feature_extractor.size), ColorJitter(brightness=0.5, hue=0.5), ToTensor(), normalize] +... ) +``` + +2. Il modello accetta [`pixel_values`](model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values) come input. Questo valore รจ generato dall'estrattore di caratteristiche. Crea una funzione che genera `pixel_values` dai transforms: + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(image.convert("RGB")) for image in examples["image"]] +... return examples +``` + +3. Poi utilizza ๐Ÿค— Datasets [`set_transform`](https://huggingface.co/docs/datasets/process#format-transform)per applicare al volo la trasformazione: + +```py +>>> dataset.set_transform(transforms) +``` + +4. Adesso quando accedi all'immagine, puoi notare che l'estrattore di caratteristiche ha aggiunto `pixel_values` allo schema di input: + +```py +>>> dataset[0]["image"] +{'image': , + 'label': 6, + 'pixel_values': tensor([[[ 0.0353, 0.0745, 0.1216, ..., -0.9922, -0.9922, -0.9922], + [-0.0196, 0.0667, 0.1294, ..., -0.9765, -0.9843, -0.9922], + [ 0.0196, 0.0824, 0.1137, ..., -0.9765, -0.9686, -0.8667], + ..., + [ 0.0275, 0.0745, 0.0510, ..., -0.1137, -0.1216, -0.0824], + [ 0.0667, 0.0824, 0.0667, ..., -0.0588, -0.0745, -0.0980], + [ 0.0353, 0.0353, 0.0431, ..., -0.0039, -0.0039, -0.0588]], + + [[ 0.2078, 0.2471, 0.2863, ..., -0.9451, -0.9373, -0.9451], + [ 0.1608, 0.2471, 0.3098, ..., -0.9373, -0.9451, -0.9373], + [ 0.2078, 0.2706, 0.3020, ..., -0.9608, -0.9373, -0.8275], + ..., + [-0.0353, 0.0118, -0.0039, ..., -0.2392, -0.2471, -0.2078], + [ 0.0196, 0.0353, 0.0196, ..., -0.1843, -0.2000, -0.2235], + [-0.0118, -0.0039, -0.0039, ..., -0.0980, -0.0980, -0.1529]], + + [[ 0.3961, 0.4431, 0.4980, ..., -0.9216, -0.9137, -0.9216], + [ 0.3569, 0.4510, 0.5216, ..., -0.9059, -0.9137, -0.9137], + [ 0.4118, 0.4745, 0.5216, ..., -0.9137, -0.8902, -0.7804], + ..., + [-0.2314, -0.1922, -0.2078, ..., -0.4196, -0.4275, -0.3882], + [-0.1843, -0.1686, -0.2000, ..., -0.3647, -0.3804, -0.4039], + [-0.1922, -0.1922, -0.1922, ..., -0.2941, -0.2863, -0.3412]]])} +``` + +Di seguito come si vede l'immagine dopo la fase di preprocessing. Come ci si aspetterebbe dalle trasformazioni applicate, l'immagine รจ stata ritagliata in modo casuale e le proprietร  del colore sono diverse. + +```py +>>> import numpy as np +>>> import matplotlib.pyplot as plt + +>>> img = dataset[0]["pixel_values"] +>>> plt.imshow(img.permute(1, 2, 0)) +``` + +![preprocessed_image](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png) + +## Multimodal + +Per attivitร  multimodali userai una combinazione di tutto quello che hai imparato poco fa e applicherai le tue competenze alla comprensione automatica del parlato (Automatic Speech Recognition - ASR). Questo significa che avrai bisogno di: + +* Un estrattore delle caratteristiche per processare i dati audio. +* Il Tokenizer per processare i testi. + +Ritorna sul datasere [LJ Speech](https://huggingface.co/datasets/lj_speech): + +```py +>>> from datasets import load_dataset + +>>> lj_speech = load_dataset("lj_speech", split="train") +``` + +Visto che sei interessato solo alle colonne `audio` e `text`, elimina tutte le altre: + +```py +>>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"]) +``` + +Adesso guarda le colonne `audio` e `text`: + +```py +>>> lj_speech[0]["audio"] +{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., + 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 22050} + +>>> lj_speech[0]["text"] +'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition' +``` + +Ricorda dalla sezione precedente sull'elaborazione dei dati audio, tu dovresti sempre [ricampionare](preprocessing#audio) la frequenza di campionamento dei tuoi dati audio per farla coincidere con quella del dataset usato dal modello preaddestrato: + +```py +>>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +### Processor + +Un processor combina un estrattore di caratteristiche e un tokenizer. Carica un processor con [`AutoProcessor.from_pretrained`]: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h") +``` + +1. Crea una funzione che processi i dati audio in `input_values`, e tokenizza il testo in `labels`. Questi sono i tuoi input per il modello: + +```py +>>> def prepare_dataset(example): +... audio = example["audio"] + +... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) + +... return example +``` + +2. Applica la funzione `prepare_dataset` ad un campione: + +```py +>>> prepare_dataset(lj_speech[0]) +``` + +Nota che il processor ha aggiunto `input_values` e `labels`. La frequenza di campionamento รจ stata corretta riducendola a 16kHz. + +Fantastico, ora dovresti essere in grado di preelaborare i dati per qualsiasi modalitร  e persino di combinare modalitร  diverse! Nella prossima esercitazione, impareremo a mettere a punto un modello sui dati appena pre-elaborati. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..f0291a6167715a860af1dad07d41ada4d667a405 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/quicktour.md @@ -0,0 +1,401 @@ + + +# Quick tour + +[[open-in-colab]] + +Entra in azione con ๐Ÿค— Transformers! Inizia utilizzando [`pipeline`] per un'inferenza veloce, carica un modello pre-allenato e un tokenizer con una [AutoClass](./model_doc/auto) per risolvere i tuoi compiti legati a testo, immagini o audio. + + + +Tutti gli esempi di codice presenti in questa documentazione hanno un pulsante in alto a sinistra che permette di selezionare tra PyTorch e TensorFlow. Se +questo non รจ presente, ci si aspetta che il codice funzioni per entrambi i backend senza alcun cambiamento. + + + +## Pipeline + +[`pipeline`] รจ il modo piรน semplice per utilizzare un modello pre-allenato per un dato compito. + + + +La [`pipeline`] supporta molti compiti comuni: + +**Testo**: +* Analisi del Sentimento (Sentiment Analysis, in inglese): classifica la polaritร  di un testo dato. +* Generazione del Testo (Text Generation, in inglese): genera del testo a partire da un dato input. +* Riconoscimento di Entitร  (Name Entity Recognition o NER, in inglese): etichetta ogni parola con l'entitร  che questa rappresenta (persona, data, luogo, ecc.). +* Rispondere a Domande (Question answering, in inglese): estrae la risposta da un contesto, dato del contesto e una domanda. +* Riempimento di Maschere (Fill-mask, in inglese): riempie gli spazi mancanti in un testo che ha parole mascherate. +* Riassumere (Summarization, in inglese): genera una sintesi di una lunga sequenza di testo o di un documento. +* Traduzione (Translation, in inglese): traduce un testo in un'altra lingua. +* Estrazione di Caratteristiche (Feature Extraction, in inglese): crea un tensore che rappresenta un testo. + +**Immagini**: +* Classificazione di Immagini (Image Classification, in inglese): classifica un'immagine. +* Segmentazione di Immagini (Image Segmentation, in inglese): classifica ogni pixel di un'immagine. +* Rilevazione di Oggetti (Object Detection, in inglese): rileva oggetti all'interno di un'immagine. + +**Audio**: +* Classificazione di Audio (Audio Classification, in inglese): assegna un'etichetta ad un segmento di audio dato. +* Riconoscimento Vocale Automatico (Automatic Speech Recognition o ASR, in inglese): trascrive il contenuto di un audio dato in un testo. + + + +Per maggiori dettagli legati alla [`pipeline`] e ai compiti ad essa associati, fai riferimento alla documentazione [qui](./main_classes/pipelines). + + + +### Utilizzo della Pipeline + +Nel seguente esempio, utilizzerai la [`pipeline`] per l'analisi del sentimento. + +Installa le seguenti dipendenze se non lo hai giร  fatto: + + + + +```bash +pip install torch +``` + + + +```bash +pip install tensorflow +``` + + + +Importa [`pipeline`] e specifica il compito che vuoi completare: + +```py +>>> from transformers import pipeline + +>>> classificatore = pipeline("sentiment-analysis", model="MilaNLProc/feel-it-italian-sentiment") +``` + +La pipeline scarica e salva il [modello pre-allenato](https://huggingface.co/MilaNLProc/feel-it-italian-sentiment) e il tokenizer per l'analisi del sentimento. Se non avessimo scelto un modello, la pipeline ne avrebbe scelto uno di default. Ora puoi utilizzare il `classifier` sul tuo testo obiettivo: + +```py +>>> classificatore("Siamo molto felici di mostrarti la libreria ๐Ÿค— Transformers.") +[{'label': 'positive', 'score': 0.9997}] +``` + +Per piรน di una frase, passa una lista di frasi alla [`pipeline`] la quale restituirร  una lista di dizionari: + +```py +>>> risultati = classificatore( +... ["Siamo molto felici di mostrarti la libreria ๐Ÿค— Transformers.", "Speriamo te non la odierai."] +... ) +>>> for risultato in risultati: +... print(f"etichetta: {risultato['label']}, con punteggio: {round(risultato['score'], 4)}") +etichetta: positive, con punteggio: 0.9998 +etichetta: negative, con punteggio: 0.9998 +``` + +La [`pipeline`] puรฒ anche iterare su un dataset intero. Inizia installando la libreria [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/): + +```bash +pip install datasets +``` + +Crea una [`pipeline`] con il compito che vuoi risolvere e con il modello che vuoi utilizzare. + +```py +>>> import torch +>>> from transformers import pipeline + +>>> riconoscitore_vocale = pipeline( +... "automatic-speech-recognition", model="radiogroup-crits/wav2vec2-xls-r-1b-italian-doc4lm-5gram" +... ) +``` + +Poi, carica un dataset (vedi ๐Ÿค— Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart) per maggiori dettagli) sul quale vuoi iterare. Per esempio, carichiamo il dataset [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14): + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="it-IT", split="train") # doctest: +IGNORE_RESULT +``` + +Dobbiamo assicurarci che la frequenza di campionamento del set di dati corrisponda alla frequenza di campionamento con cui รจ stato addestrato `radiogroup-crits/wav2vec2-xls-r-1b-italian-doc4lm-5gram`. + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=riconoscitore_vocale.feature_extractor.sampling_rate)) +``` + +I file audio vengono caricati automaticamente e ri-campionati quando chiamiamo la colonna "audio". +Estraiamo i vettori delle forme d'onda grezze delle prime 4 osservazioni e passiamoli come lista alla pipeline: + +```py +>>> risultato = riconoscitore_vocale(dataset[:4]["audio"]) +>>> print([d["text"] for d in risultato]) +['dovrei caricare dei soldi sul mio conto corrente', 'buongiorno e senza vorrei depositare denaro sul mio conto corrente come devo fare per cortesia', 'sรฌ salve vorrei depositare del denaro sul mio conto', 'e buon pomeriggio vorrei depositare dei soldi sul mio conto bancario volleo sapere come posso fare se e posso farlo online ed un altro conto o andandoo tramite bancomut'] +``` + +Per un dataset piรน grande dove gli input sono di dimensione maggiore (come nel parlato/audio o nella visione), dovrai passare un generatore al posto di una lista che carica tutti gli input in memoria. Guarda la [documentazione della pipeline](./main_classes/pipelines) per maggiori informazioni. + +### Utilizzare un altro modello e tokenizer nella pipeline + +La [`pipeline`] puรฒ ospitare qualsiasi modello del [Model Hub](https://huggingface.co/models), rendendo semplice l'adattamento della [`pipeline`] per altri casi d'uso. Per esempio, se si vuole un modello capace di trattare testo in francese, usa i tag presenti nel Model Hub in modo da filtrare per ottenere un modello appropriato. Il miglior risultato filtrato restituisce un modello multi-lingua [BERT model](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment) fine-tuned per l'analisi del sentimento. Ottimo, utilizziamo questo modello! + +```py +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +``` + + + +Usa [`AutoModelForSequenceClassification`] e [`AutoTokenizer`] per caricare il modello pre-allenato e il suo tokenizer associato (maggiori informazioni su una `AutoClass` in seguito): + +```py +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + +Usa [`TFAutoModelForSequenceClassification`] e [`AutoTokenizer`] per caricare il modello pre-allenato e il suo tokenizer associato (maggiori informazioni su una `TFAutoClass` in seguito): + +```py +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + +Poi puoi specificare il modello e il tokenizer nella [`pipeline`], e applicare il `classifier` sul tuo testo obiettivo: + +```py +>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) +>>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") +[{'label': '5 stars', 'score': 0.7273}] +``` + +Se non riesci a trovare un modello per il tuo caso d'uso, dovrai fare fine-tuning di un modello pre-allenato sui tuoi dati. Dai un'occhiata al nostro tutorial [fine-tuning tutorial](./training) per imparare come. Infine, dopo che hai completato il fine-tuning del tuo modello pre-allenato, considera per favore di condividerlo (vedi il tutorial [qui](./model_sharing)) con la comunitร  sul Model Hub per democratizzare l'NLP! ๐Ÿค— + +## AutoClass + + + +Al suo interno, le classi [`AutoModelForSequenceClassification`] e [`AutoTokenizer`] lavorano assieme per dare potere alla [`pipeline`]. Una [AutoClass](./model_doc/auto) รจ una scorciatoia che automaticamente recupera l'architettura di un modello pre-allenato a partire dal suo nome o path. Hai solo bisogno di selezionare la `AutoClass` appropriata per il tuo compito e il suo tokenizer associato con [`AutoTokenizer`]. + +Ritorniamo al nostro esempio e vediamo come puoi utilizzare la `AutoClass` per replicare i risultati della [`pipeline`]. + +### AutoTokenizer + +Un tokenizer รจ responsabile dell'elaborazione del testo in modo da trasformarlo in un formato comprensibile dal modello. Per prima cosa, il tokenizer dividerร  il testo in parole chiamate *token*. Ci sono diverse regole che governano il processo di tokenizzazione, tra cui come dividere una parola e a quale livello (impara di piรน sulla tokenizzazione [qui](./tokenizer_summary)). La cosa piรน importante da ricordare comunque รจ che hai bisogno di inizializzare il tokenizer con lo stesso nome del modello in modo da assicurarti che stai utilizzando le stesse regole di tokenizzazione con cui il modello รจ stato pre-allenato. + +Carica un tokenizer con [`AutoTokenizer`]: + +```py +>>> from transformers import AutoTokenizer + +>>> nome_del_modello = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tokenizer = AutoTokenizer.from_pretrained(nome_del_modello) +``` + +Dopodichรฉ, il tokenizer converte i token in numeri in modo da costruire un tensore come input del modello. Questo รจ conosciuto come il *vocabolario* del modello. + +Passa il tuo testo al tokenizer: + +```py +>>> encoding = tokenizer("Siamo molto felici di mostrarti la libreria ๐Ÿค— Transformers.") +>>> print(encoding) +{'input_ids': [101, 56821, 10132, 14407, 13019, 13007, 10120, 47201, 10330, 10106, 91686, 100, 58263, 119, 102], +'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], +'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +Il tokenizer restituirร  un dizionario contenente: + +* [input_ids](./glossary#input-ids): rappresentazioni numeriche dei tuoi token. +* [attention_mask](.glossary#attention-mask): indica quali token devono essere presi in considerazione. + +Come con la [`pipeline`], il tokenizer accetterร  una lista di input. In piรน, il tokenizer puรฒ anche completare (pad, in inglese) e troncare il testo in modo da restituire un lotto (batch, in inglese) di lunghezza uniforme: + + + +```py +>>> pt_batch = tokenizer( +... ["Siamo molto felici di mostrarti la libreria ๐Ÿค— Transformers.", "Speriamo te non la odierai."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="pt", +... ) +``` + + +```py +>>> tf_batch = tokenizer( +... ["Siamo molto felici di mostrarti la libreria ๐Ÿค— Transformers.", "Speriamo te non la odierai."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="tf", +... ) +``` + + + +Leggi il tutorial sul [preprocessing](./preprocessing) per maggiori dettagli sulla tokenizzazione. + +### AutoModel + + + +๐Ÿค— Transformers fornisce un metodo semplice e unificato per caricare istanze pre-allenate. Questo significa che puoi caricare un [`AutoModel`] come caricheresti un [`AutoTokenizer`]. L'unica differenza รจ selezionare l'[`AutoModel`] corretto per il compito di interesse. Dato che stai facendo classificazione di testi, o sequenze, carica [`AutoModelForSequenceClassification`]: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +Guarda il [task summary](./task_summary) per sapere quale classe di [`AutoModel`] utilizzare per quale compito. + + + +Ora puoi passare il tuo lotto di input pre-processati direttamente al modello. Devi solo spacchettare il dizionario aggiungendo `**`: + +```py +>>> pt_outputs = pt_model(**pt_batch) +``` + +Il modello produrrร  le attivazioni finali nell'attributo `logits`. Applica la funzione softmax a `logits` per ottenere le probabilitร : + +```py +>>> from torch import nn + +>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) +>>> print(pt_predictions) +tensor([[0.0041, 0.0037, 0.0203, 0.2005, 0.7713], + [0.3766, 0.3292, 0.1832, 0.0558, 0.0552]], grad_fn=) +``` + + +๐Ÿค— Transformers fornisce un metodo semplice e unificato per caricare istanze pre-allenate. Questo significa che puoi caricare un [`TFAutoModel`] come caricheresti un [`AutoTokenizer`]. L'unica differenza รจ selezionare il [`TFAutoModel`] corretto per il compito di interesse. Dato che stai facendo classificazione di testi, o sequenze, carica [`TFAutoModelForSequenceClassification`]: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> nome_del_modello = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(nome_del_modello) +``` + + + +Guarda il [task summary](./task_summary) per sapere quale classe di [`AutoModel`] utilizzare per quale compito. + + + +Ora puoi passare il tuo lotto di input pre-processati direttamente al modello passando le chiavi del dizionario al tensore: + +```py +>>> tf_outputs = tf_model(tf_batch) +``` + +Il modello produrrร  le attivazioni finali nell'attributo `logits`. Applica la funzione softmax a `logits` per ottenere le probabilitร : +```py +>>> import tensorflow as tf + +>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) +>>> tf_predictions # doctest: +IGNORE_RESULT +``` + + + + + +Tutti i modelli di ๐Ÿค— Transformers (PyTorch e TensorFlow) restituiscono i tensori *prima* della funzione finale +di attivazione (come la softmax) perchรฉ la funzione di attivazione finale viene spesso unita a quella di perdita. + + + +I modelli sono [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) o [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) standard cosรฌ puoi utilizzarli all'interno del tuo training loop usuale. Tuttavia, per rendere le cose piรน semplici, ๐Ÿค— Transformers fornisce una classe [`Trainer`] per PyTorch che aggiunge delle funzionalitร  per l'allenamento distribuito, precisione mista, e altro ancora. Per TensorFlow, puoi utilizzare il metodo `fit` di [Keras](https://keras.io/). Fai riferimento al [tutorial per il training](./training) per maggiori dettagli. + + + +Gli output del modello di ๐Ÿค— Transformers sono delle dataclasses speciali in modo che i loro attributi vengano auto-completati all'interno di un IDE. +Gli output del modello si comportano anche come una tupla o un dizionario (ad esempio, puoi indicizzare con un intero, una slice o una stringa) nel qual caso gli attributi che sono `None` vengono ignorati. + + + +### Salva un modello + + + +Una volta completato il fine-tuning del tuo modello, puoi salvarlo con il suo tokenizer utilizzando [`PreTrainedModel.save_pretrained`]: + +```py +>>> pt_save_directory = "./pt_save_pretrained" +>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT +>>> pt_model.save_pretrained(pt_save_directory) +``` + +Quando desideri utilizzare il tuo modello nuovamente, puoi ri-caricarlo con [`PreTrainedModel.from_pretrained`]: + +```py +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") +``` + + +Una volta completato il fine-tuning del tuo modello, puoi salvarlo con il suo tokenizer utilizzando [`TFPreTrainedModel.save_pretrained`]: + +```py +>>> tf_save_directory = "./tf_save_pretrained" +>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT +>>> tf_model.save_pretrained(tf_save_directory) +``` + +Quando desideri utilizzare il tuo modello nuovamente, puoi ri-caricarlo con [`TFPreTrainedModel.from_pretrained`]: + +```py +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") +``` + + + +Una caratteristica particolarmente interessante di ๐Ÿค— Transformers รจ la sua abilitร  di salvare un modello e ri-caricarlo sia come modello di PyTorch che di TensorFlow. I parametri `from_pt` o `from_tf` possono convertire un modello da un framework all'altro: + + + + +```py +>>> from transformers import AutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) +``` + + + +```py +>>> from transformers import TFAutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/run_scripts.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/run_scripts.md new file mode 100644 index 0000000000000000000000000000000000000000..71ccf0eed52bdb154822c980f0415dd46192d816 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/run_scripts.md @@ -0,0 +1,351 @@ + + +# Addestramento con script + +Insieme ai [notebooks](./notebooks) ๐Ÿค— Transformers, ci sono anche esempi di script che dimostrano come addestrare un modello per un task con [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch), [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow), o [JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax). + +Troverai anche script che abbiamo usato nei nostri [progetti di ricerca](https://github.com/huggingface/transformers-research-projects/) e [precedenti esempi](https://github.com/huggingface/transformers/tree/main/examples/legacy) a cui contribuisce per lo piรน la comunitร . Questi script non sono attivamente mantenuti e richiedono una specifica versione di ๐Ÿค— Transformers che sarร  molto probabilmente incompatibile con l'ultima versione della libreria. + +Non รจ dato per scontato che gli script di esempio funzionino senza apportare modifiche per ogni problema, bensรฌ potrebbe essere necessario adattare lo script al tuo caso specifico. Per aiutarti in ciรฒ, la maggioranza degli script espone le modalitร  di pre-processamento dei dati, consentendoti di modificare lo script come preferisci. + +Per qualsiasi feature che vorresti implementare in uno script d'esempio, per favore discutine nel [forum](https://discuss.huggingface.co/) o in un'[issue](https://github.com/huggingface/transformers/issues) prima di inviare una Pull Request. Mentre accogliamo con piacere la correzione di bug, รจ piรน improbabile che faremo la stessa con una PR che aggiunge funzionalitร  sacrificando la leggibilitร . + +Questa guida ti mostrerร  come eseguire uno script di esempio relativo al task di summarization in [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) e [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization). Tutti gli esempi funzioneranno con entrambi i framework a meno che non sia specificato altrimenti. + +## Installazione + +Per eseguire con successo l'ultima versione degli script di esempio, devi **installare ๐Ÿค— Transformers dalla fonte** in un nuovo ambiente virtuale: + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` +Per le precedenti versioni degli script di esempio, clicca sul pulsante di seguito: + +
+ Esempi per versioni precedenti di ๐Ÿค— Transformers + +
+ +Successivamente, cambia la tua attuale copia di ๐Ÿค— Transformers specificandone la versione, ad esempio v3.5.1: + +```bash +git checkout tags/v3.5.1 +``` + + Dopo aver configurato correttamente la versione della libreria, naviga nella cartella degli esempi di tua scelta e installa i requisiti: + +```bash +pip install -r requirements.txt +``` + +## Esegui uno script + + + + +Lo script di esempio scarica e pre-processa un dataset dalla libreria ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Successivamente, lo script esegue il fine-tuning su un dataset usando il [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) su un'architettura che supporta la summarization. Il seguente esempio mostra come eseguire il fine-tuning di [T5-small](https://huggingface.co/google-t5/t5-small) sul dataset [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). Il modello T5 richiede un parametro addizionale `source_prefix` a causa del modo in cui รจ stato addestrato. Questo prefisso permette a T5 di sapere che si tratta di un task di summarization. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +Lo script di esempio scarica e pre-processa un dataset dalla libreria ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/). Successivamente, lo script esegue il fine-tuning su un dataset usando Keras su un'architettura che supporta la summarization. Il seguente esempio mostra come eseguire il fine-tuning di [T5-small](https://huggingface.co/google-t5/t5-small) sul dataset [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail). Il modello T5 richiede un parametro addizionale `source_prefix` a causa del modo in cui รจ stato addestrato. Questo prefisso permette a T5 di sapere che si tratta di un task di summarization. + +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Addestramento distribuito e precisione mista + +Il [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) supporta l'addestramento distribuito e la precisione mista, che significa che puoi anche usarla in uno script. Per abilitare entrambe le funzionalitร : + +- Aggiunto l'argomento `fp16` per abilitare la precisione mista. +- Imposta un numero di GPU da usare con l'argomento `nproc_per_node`. + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +Gli script TensorFlow utilizzano una [`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy) per il training distribuito e non devi aggiungere alcun argomento addizionale allo script di training. Lo script TensorFlow userร  multiple GPU in modo predefinito se quest'ultime sono disponibili: + +## Esegui uno script su TPU + + + +Le Tensor Processing Units (TPU) sono state progettate per migliorare le prestazioni. PyTorch supporta le TPU con il compilatore per deep learning [XLA](https://www.tensorflow.org/xla) (guarda [questo link](https://github.com/pytorch/xla/blob/master/README.md) per maggiori dettagli). Per usare una TPU, avvia lo script `xla_spawn.py` e usa l'argomento `num_cores` per impostare il numero di core TPU che intendi usare. + +```bash +python xla_spawn.py --num_cores 8 \ + summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +Le Tensor Processing Units (TPU) sono state progettate per migliorare le prestazioni. Gli script TensorFlow utilizzano una [`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy) per eseguire l'addestramento su TPU. Per usare una TPU, passa il nome della risorsa TPU all'argomento `tpu`. + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Esegui uno script con ๐Ÿค— Accelerate + +๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) รจ una libreria compatibile solo con PyTorch che offre un metodo unificato per addestrare modelli su diverse tipologie di configurazioni (CPU, multiple GPU, TPU) mantenendo una completa visibilitร  rispetto al ciclo di training di PyTorch. Assicurati di aver effettuato l'installazione di ๐Ÿค— Accelerate, nel caso non lo avessi fatto: + +> Nota: dato che Accelerate รจ in rapido sviluppo, รจ necessario installare la versione proveniente da git per eseguire gli script: +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +Invece che usare lo script `run_summarization.py`, devi usare lo script `run_summarization_no_trainer.py`. Gli script supportati in ๐Ÿค— Accelerate avranno un file chiamato `task_no_trainer.py` nella rispettiva cartella. Per iniziare, esegui il seguente comando per creare e salvare un file di configurazione: + +```bash +accelerate config +``` + +Testa la tua configurazione per assicurarti della sua correttezza: + +```bash +accelerate test +``` + +Ora sei pronto per avviare l'addestramento: + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization +``` + +## Uso di un dataset personalizzato + +Lo script di summarization supporta dataset personalizzati purchรฉ siano file CSV o JSON Line. Quando usi il tuo dataset, devi specificare diversi argomenti aggiuntivi: + +- `train_file` e `validation_file` specificano dove si trovano i file di addestramento e validazione. +- `text_column` รจ il file di input da riassumere. +- `summary_column` รจ il file di destinazione per l'output. + +Uno script di summarization usando un dataset personalizzato sarebbe simile a questo: + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate +``` + +## Testare uno script + +รˆ spesso una buona idea avviare il tuo script su un numero inferiore di esempi tratti dal dataset, per assicurarti che tutto funzioni come previsto prima di eseguire lo script sull'intero dataset, che potrebbe necessitare di ore. Usa i seguenti argomenti per limitare il dataset ad un massimo numero di esempi: + +- `max_train_samples` +- `max_eval_samples` +- `max_predict_samples` + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +Non tutti gli esempi di script supportano l'argomento `max_predict_samples`. Se non sei sicuro circa il supporto di questo argomento da parte del tuo script, aggiungi l'argomento `-h` per controllare: + +```bash +examples/pytorch/summarization/run_summarization.py -h +``` + +## Riavviare addestramento da un checkpoint + +Un'altra utile opzione รจ riavviare un addestramento da un checkpoint precedente. Questo garantirร  che tu possa riprendere da dove hai interrotto senza ricominciare se l'addestramento viene interrotto. Ci sono due metodi per riavviare l'addestramento da un checkpoint: + +Il primo metodo usa l'argomento `output_dir previous_output_dir` per riavviare l'addestramento dall'ultima versione del checkpoint contenuto in `output_dir`. In questo caso, dovresti rimuovere `overwrite_output_dir`: + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --output_dir previous_output_dir \ + --predict_with_generate +``` + +Il secondo metodo usa l'argomento `resume_from_checkpoint path_to_specific_checkpoint` per riavviare un addestramento da una specifica cartella di checkpoint. + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate +``` + +## Condividi il tuo modello + +Tutti gli script possono caricare il tuo modello finale al [Model Hub](https://huggingface.co/models). Prima di iniziare, assicurati di aver effettuato l'accesso su Hugging Face: + +```bash +hf auth login +``` + +Poi, aggiungi l'argomento `push_to_hub` allo script. Questo argomento consentirร  di creare un repository con il tuo username Hugging Face e la cartella specificata in `output_dir`. + +Per dare uno specifico nome al repository, usa l'argomento `push_to_hub_model_id`. Il repository verrร  automaticamente elencata sotto al tuo namespace. + +Il seguente esempio mostra come caricare un modello specificando il nome del repository: + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/serialization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/serialization.md new file mode 100644 index 0000000000000000000000000000000000000000..974aee0d81cae0425fbd6a6fa3052dd5c2c25282 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/serialization.md @@ -0,0 +1,676 @@ + + +# Esporta modelli ๐Ÿค— Transformers + +Se devi implementare ๐Ÿค— modelli Transformers in ambienti di produzione, noi +consigliamo di esportarli in un formato serializzato che puรฒ essere caricato ed eseguito +su runtime e hardware specializzati. In questa guida ti mostreremo come farlo +esporta ๐Ÿค— Modelli Transformers in due formati ampiamente utilizzati: ONNX e TorchScript. + +Una volta esportato, un modello puรฒ essere ottimizato per l'inferenza tramite tecniche come +la quantizzazione e soppressione. Se sei interessato a ottimizzare i tuoi modelli per l'esecuzione +con la massima efficienza, dai un'occhiata a [๐Ÿค— Optimum +library](https://github.com/huggingface/optimum). + +## ONNX + +Il progetto [ONNX (Open Neural Network eXchange)](http://onnx.ai) Il progetto onnx รจ un open +standard che definisce un insieme comune di operatori e un formato di file comune a +rappresentano modelli di deep learning in un'ampia varietร  di framework, tra cui +PyTorch e TensorFlow. Quando un modello viene esportato nel formato ONNX, questi +operatori sono usati per costruire un grafico computazionale (often called an +_intermediate representation_) che rappresenta il flusso di dati attraverso la +rete neurale. + +Esponendo un grafico con operatori e tipi di dati standardizzati, ONNX rende +piรน facile passare da un framework all'altro. Ad esempio, un modello allenato in PyTorch puรฒ +essere esportato in formato ONNX e quindi importato in TensorFlow (e viceversa). + +๐Ÿค— Transformers fornisce un pacchetto `transformers.onnx` che ti consente di +convertire i checkpoint del modello in un grafico ONNX sfruttando gli oggetti di configurazione. +Questi oggetti di configurazione sono giร  pronti per una serie di architetture di modelli, +e sono progettati per essere facilmente estensibili ad altre architetture. + +Le configurazioni pronte includono le seguenti architetture: + + + +- ALBERT +- BART +- BEiT +- BERT +- BigBird +- BigBird-Pegasus +- Blenderbot +- BlenderbotSmall +- CamemBERT +- ConvBERT +- Data2VecText +- Data2VecVision +- DeiT +- DistilBERT +- ELECTRA +- FlauBERT +- GPT Neo +- GPT-J +- I-BERT +- LayoutLM +- M2M100 +- Marian +- mBART +- MobileBERT +- OpenAI GPT-2 +- Perceiver +- PLBart +- RoBERTa +- RoFormer +- SqueezeBERT +- T5 +- ViT +- XLM +- XLM-RoBERTa +- XLM-RoBERTa-XL + +Nelle prossime due sezioni, ti mostreremo come: + +* Esporta un modello supportato usando il pacchetto `transformers.onnx`. +* Esporta un modello personalizzato per un'architettura non supportata. + +### Esportazione di un modello in ONNX + +Per esportare un modello ๐Ÿค— Transformers in ONNX, dovrai prima installarne alcune +dipendenze extra: + +```bash +pip install transformers[onnx] +``` + +Il pacchetto `transformers.onnx` puรฒ essere usato come modulo Python: + +```bash +python -m transformers.onnx --help + +usage: Hugging Face Transformers ONNX exporter [-h] -m MODEL [--feature {causal-lm, ...}] [--opset OPSET] [--atol ATOL] output + +positional arguments: + output Path indicating where to store generated ONNX model. + +optional arguments: + -h, --help show this help message and exit + -m MODEL, --model MODEL + Model ID on huggingface.co or path on disk to load model from. + --feature {causal-lm, ...} + The type of features to export the model with. + --opset OPSET ONNX opset version to export the model with. + --atol ATOL Absolute difference tolerance when validating the model. +``` + +L'esportazione di un checkpoint utilizzando una configurazione giร  pronta puรฒ essere eseguita come segue: + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased onnx/ +``` + +che dovrebbe mostrare i seguenti log: + +```bash +Validating ONNX model... + -[โœ“] ONNX model output names match reference model ({'last_hidden_state'}) + - Validating ONNX Model output "last_hidden_state": + -[โœ“] (2, 8, 768) matches (2, 8, 768) + -[โœ“] all values close (atol: 1e-05) +All good, model saved at: onnx/model.onnx +``` + +Questo esporta un grafico ONNX del checkpoint definito dall'argomento `--model`. +In questo esempio รจ `distilbert/distilbert-base-uncased`, ma puรฒ essere qualsiasi checkpoint +Hugging Face Hub o uno memorizzato localmente. + +Il file risultante `model.onnx` puรฒ quindi essere eseguito su uno dei [tanti +acceleratori](https://onnx.ai/supported-tools.html#deployModel) che supportano il +lo standard ONNX. Ad esempio, possiamo caricare ed eseguire il modello con [ONNX +Runtime](https://onnxruntime.ai/) come segue: + +```python +>>> from transformers import AutoTokenizer +>>> from onnxruntime import InferenceSession + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> session = InferenceSession("onnx/model.onnx") +>>> # ONNX Runtime expects NumPy arrays as input +>>> inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="np") +>>> outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs)) +``` + +I nomi di output richiesti (cioรจ `["last_hidden_state"]`) possono essere ottenuti +dando un'occhiata alla configurazione ONNX di ogni modello. Ad esempio, per +DistilBERT abbiamo: + +```python +>>> from transformers.models.distilbert import DistilBertConfig, DistilBertOnnxConfig + +>>> config = DistilBertConfig() +>>> onnx_config = DistilBertOnnxConfig(config) +>>> print(list(onnx_config.outputs.keys())) +["last_hidden_state"] +``` + +Il processo รจ identico per i checkpoint TensorFlow sull'hub. Ad esempio, noi +possiamo esportare un checkpoint TensorFlow puro da [Keras +organizzazione](https://huggingface.co/keras-io) come segue: + +```bash +python -m transformers.onnx --model=keras-io/transformers-qa onnx/ +``` + +Per esportare un modello memorizzato localmente, devi disporre dei pesi del modello +e file tokenizer memorizzati in una directory. Ad esempio, possiamo caricare e salvare un +checkpoint come segue: + + + +```python +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> # Load tokenizer and PyTorch weights form the Hub +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +>>> # Save to disk +>>> tokenizer.save_pretrained("local-pt-checkpoint") +>>> pt_model.save_pretrained("local-pt-checkpoint") +``` + +Una volta salvato il checkpoint, possiamo esportarlo su ONNX puntando l'argomento `--model` +del pacchetto `transformers.onnx` nella directory desiderata: + +```bash +python -m transformers.onnx --model=local-pt-checkpoint onnx/ +``` + + +```python +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> # Load tokenizer and TensorFlow weights from the Hub +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +>>> # Save to disk +>>> tokenizer.save_pretrained("local-tf-checkpoint") +>>> tf_model.save_pretrained("local-tf-checkpoint") +``` + +Once the checkpoint is saved, we can export it to ONNX by pointing the `--model` +argument of the `transformers.onnx` package to the desired directory: + +```bash +python -m transformers.onnx --model=local-tf-checkpoint onnx/ +``` + + + +### Selezione delle caratteristiche per diverse topologie di modello + +Ogni configurazione giร  pronta viene fornita con una serie di _caratteristiche_ che ti consentono di +esportare modelli per diversi tipi di topologie o attivitร . Come mostrato nella tabella +di seguito, ogni caratteristica รจ associata a una diversa Auto Class: + +| Caratteristica | Auto Class | +| ------------------------------------ | ------------------------------------ | +| `causal-lm`, `causal-lm-with-past` | `AutoModelForCausalLM` | +| `default`, `default-with-past` | `AutoModel` | +| `masked-lm` | `AutoModelForMaskedLM` | +| `question-answering` | `AutoModelForQuestionAnswering` | +| `seq2seq-lm`, `seq2seq-lm-with-past` | `AutoModelForSeq2SeqLM` | +| `sequence-classification` | `AutoModelForSequenceClassification` | +| `token-classification` | `AutoModelForTokenClassification` | + +Per ciascuna configurazione, puoi trovare l'elenco delle funzionalitร  supportate tramite il +`FeaturesManager`. Ad esempio, per DistilBERT abbiamo: + +```python +>>> from transformers.onnx.features import FeaturesManager + +>>> distilbert_features = list(FeaturesManager.get_supported_features_for_model_type("distilbert").keys()) +>>> print(distilbert_features) +["default", "masked-lm", "causal-lm", "sequence-classification", "token-classification", "question-answering"] +``` + +Puoi quindi passare una di queste funzionalitร  all'argomento `--feature` nel +pacchetto `transformers.onnx`. Ad esempio, per esportare un modello di classificazione del testo +possiamo scegliere un modello ottimizzato dall'Hub ed eseguire: + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased-finetuned-sst-2-english \ + --feature=sequence-classification onnx/ +``` + +che visualizzerร  i seguenti registri: + +```bash +Validating ONNX model... + -[โœ“] ONNX model output names match reference model ({'logits'}) + - Validating ONNX Model output "logits": + -[โœ“] (2, 2) matches (2, 2) + -[โœ“] all values close (atol: 1e-05) +All good, model saved at: onnx/model.onnx +``` + +Puoi notare che in questo caso, i nomi di output del modello ottimizzato sono +`logits` invece di `last_hidden_state` che abbiamo visto con il +checkpoint `distilbert/distilbert-base-uncased` precedente. Questo รจ previsto dal +modello ottimizato visto che ha una testa di e. + + + +Le caratteristiche che hanno un suffisso `wtih-past` (ad es. `causal-lm-with-past`) +corrispondono a topologie di modello con stati nascosti precalcolati (chiave e valori +nei blocchi di attenzione) che possono essere utilizzati per la decodifica autoregressiva veloce. + + + + +### Esportazione di un modello per un'architettura non supportata + +Se desideri esportare un modello la cui architettura non รจ nativamente supportata dalla +libreria, ci sono tre passaggi principali da seguire: + +1. Implementare una configurazione ONNX personalizzata. +2. Esportare il modello in ONNX. +3. Convalidare gli output di PyTorch e dei modelli esportati. + +In questa sezione, vedremo come DistilBERT รจ stato implementato per mostrare cosa รจ +coinvolto in ogni passaggio. + +#### Implementazione di una configurazione ONNX personalizzata + +Iniziamo con l'oggetto di configurazione ONNX. Forniamo tre classi +astratte da cui ereditare, a seconda del tipo di archittettura +del modello che desideri esportare: + +* I modelli basati su encoder ereditano da [`~onnx.config.OnnxConfig`] +* I modelli basati su decoder ereditano da [`~onnx.config.OnnxConfigWithPast`] +* I modelli encoder-decoder ereditano da[`~onnx.config.OnnxSeq2SeqConfigWithPast`] + + + +Un buon modo per implementare una configurazione ONNX personalizzata รจ guardare l'implementazione +esistente nel file `configuration_.py` di un'architettura simile. + + + +Poichรฉ DistilBERT รจ un modello basato su encoder, la sua configurazione eredita da +`OnnxConfig`: + +```python +>>> from typing import Mapping, OrderedDict +>>> from transformers.onnx import OnnxConfig + + +>>> class DistilBertOnnxConfig(OnnxConfig): +... @property +... def inputs(self) -> Mapping[str, Mapping[int, str]]: +... return OrderedDict( +... [ +... ("input_ids", {0: "batch", 1: "sequence"}), +... ("attention_mask", {0: "batch", 1: "sequence"}), +... ] +... ) +``` + +Ogni oggetto di configurazione deve implementare la proprietร  `inputs` e restituire una +mappatura, dove ogni chiave corrisponde a un input previsto e ogni valore +indica l'asse di quell'input. Per DistilBERT, possiamo vedere che sono richiesti +due input: `input_ids` e `attention_mask`. Questi inputs hanno la stessa forma di +`(batch_size, sequence_length)` per questo motivo vediamo gli stessi assi usati nella +configurazione. + + + +Puoi notare che la proprietร  `inputs` per `DistilBertOnnxConfig` restituisce un +`OrdinatoDict`. Ciรฒ garantisce che gli input corrispondano alla loro posizione +relativa all'interno del metodo `PreTrainedModel.forward()` durante il tracciamento del grafico. +Raccomandiamo di usare un `OrderedDict` per le proprietร  `inputs` e `outputs` +quando si implementano configurazioni ONNX personalizzate. + + + +Dopo aver implementato una configurazione ONNX, รจ possibile istanziarla +fornendo alla configurazione del modello base come segue: + +```python +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased") +>>> onnx_config = DistilBertOnnxConfig(config) +``` + +L'oggetto risultante ha diverse proprietร  utili. Ad esempio รจ possibile visualizzare il +Set operatore ONNX che verrร  utilizzato durante l'esportazione: + +```python +>>> print(onnx_config.default_onnx_opset) +11 +``` + +รˆ inoltre possibile visualizzare gli output associati al modello come segue: + +```python +>>> print(onnx_config.outputs) +OrderedDict([("last_hidden_state", {0: "batch", 1: "sequence"})]) +``` + +Puoi notare che la proprietร  degli output segue la stessa struttura degli input; esso +restituisce un `OrderedDict` di output con nome e le loro forme. La struttura di output +รจ legato alla scelta della funzione con cui viene inizializzata la configurazione. +Per impostazione predefinita, la configurazione ONNX viene inizializzata con la funzione 'predefinita' +che corrisponde all'esportazione di un modello caricato con la classe `AutoModel`. Se tu +desideri esportare una topologia di modello diversa, รจ sufficiente fornire una funzionalitร  diversa a +l'argomento `task` quando inizializzi la configurazione ONNX. Ad esempio, se +volevamo esportare DistilBERT con una testa di classificazione per sequenze, potremmo +usare: + +```python +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased") +>>> onnx_config_for_seq_clf = DistilBertOnnxConfig(config, task="sequence-classification") +>>> print(onnx_config_for_seq_clf.outputs) +OrderedDict([('logits', {0: 'batch'})]) +``` + + + +Tutte le proprietร  e i metodi di base associati a [`~onnx.config.OnnxConfig`] e le +altre classi di configurazione possono essere sovrascritte se necessario. Guarda +[`BartOnnxConfig`] per un esempio avanzato. + + + +#### Esportazione del modello + +Una volta implementata la configurazione ONNX, il passaggio successivo consiste nell'esportare il +modello. Qui possiamo usare la funzione `export()` fornita dal +pacchetto `transformers.onnx`. Questa funzione prevede la configurazione ONNX, insieme +con il modello base e il tokenizer e il percorso per salvare il file esportato: + +```python +>>> from pathlib import Path +>>> from transformers.onnx import export +>>> from transformers import AutoTokenizer, AutoModel + +>>> onnx_path = Path("model.onnx") +>>> model_ckpt = "distilbert/distilbert-base-uncased" +>>> base_model = AutoModel.from_pretrained(model_ckpt) +>>> tokenizer = AutoTokenizer.from_pretrained(model_ckpt) + +>>> onnx_inputs, onnx_outputs = export(tokenizer, base_model, onnx_config, onnx_config.default_onnx_opset, onnx_path) +``` + +Gli `onnx_inputs` e `onnx_outputs` restituiti dalla funzione `export()` sono +liste di chiavi definite nelle proprietร  di `input` e `output` della +configurazione. Una volta esportato il modello, puoi verificare che il modello sia ben +formato come segue: + +```python +>>> import onnx + +>>> onnx_model = onnx.load("model.onnx") +>>> onnx.checker.check_model(onnx_model) +``` + + + +Se il tuo modello รจ piรน largo di 2 GB, vedrai che molti file aggiuntivi sono +creati durante l'esportazione. Questo รจ _previsto_ perchรฉ ONNX utilizza [Protocol +Buffer](https://developers.google.com/protocol-buffers/) per memorizzare il modello e +questi hanno un limite di dimensione 2 GB. Vedi la [Documentazione +ONNX](https://github.com/onnx/onnx/blob/master/docs/ExternalData.md) +per istruzioni su come caricare modelli con dati esterni. + + + +#### Convalida degli output del modello + +Il passaggio finale consiste nel convalidare gli output dal modello di base e quello esportato +corrispondere entro una soglia di tolleranza assoluta. Qui possiamo usare la +Funzione `validate_model_outputs()` fornita dal pacchetto `transformers.onnx` +come segue: + +```python +>>> from transformers.onnx import validate_model_outputs + +>>> validate_model_outputs( +... onnx_config, tokenizer, base_model, onnx_path, onnx_outputs, onnx_config.atol_for_validation +... ) +``` + +Questa funzione usa il metodo `OnnxConfig.generate_dummy_inputs()` per generare +input per il modello di base e quello esportato e la tolleranza assoluta puรฒ essere +definita nella configurazione. Generalmente troviamo una corrispondenza numerica nell'intervallo da 1e-6 +a 1e-4, anche se รจ probabile che qualsiasi cosa inferiore a 1e-3 vada bene. + +### Contribuire con una nuova configurazione a ๐Ÿค— Transformers + +Stiamo cercando di espandere l'insieme di configurazioni giร  pronte e di accettare +contributi della community! Se vuoi contribuire con la tua aggiunta +nella libreria, dovrai: + +* Implementare la configurazione ONNX nella corrispondente `configuration file +_.py` +* Includere l'architettura del modello e le funzioni corrispondenti in [`~onnx.features.FeatureManager`] +* Aggiungere la tua architettura del modello ai test in `test_onnx_v2.py` + +Scopri come stato contribuito la configurazione per [IBERT](https://github.com/huggingface/transformers/pull/14868/files) per +avere un'idea di cosa รจ coinvolto. + +## TorchScript + + + +Questo รจ l'inizio dei nostri esperimenti con TorchScript e stiamo ancora esplorando le sue capacitร  con +modelli con variable-input-size. รˆ una nostra prioritร  e approfondiremo le nostre analisi nelle prossime versioni, +con piรน esempi di codici, un'implementazione piรน flessibile e benchmark che confrontano i codici basati su Python con quelli compilati con +TorchScript. + + + +Secondo la documentazione di Pytorch: "TorchScript รจ un modo per creare modelli serializzabili e ottimizzabili da codice +Pytorch". I due moduli di Pytorch [JIT e TRACE](https://pytorch.org/docs/stable/jit.html) consentono allo sviluppatore di esportare +il loro modello da riutilizzare in altri programmi, come i programmi C++ orientati all'efficienza. + +Abbiamo fornito un'interfaccia che consente l'esportazione di modelli ๐Ÿค— Transformers in TorchScript in modo che possano essere riutilizzati +in un ambiente diverso rispetto a un programma Python basato su Pytorch. Qui spieghiamo come esportare e utilizzare i nostri modelli utilizzando +TorchScript. + +Esportare un modello richiede due cose: + +- Un passaggio in avanti con input fittizzi. +- Istanziazione del modello con flag `torchscript`. + +Queste necessitร  implicano diverse cose a cui gli sviluppatori dovrebbero prestare attenzione. Questi dettagli mostrati sotto. + +### Flag TorchScript e pesi legati + +Questo flag รจ necessario perchรฉ la maggior parte dei modelli linguistici in questo repository hanno pesi legati tra il loro +strato "Embedding" e lo strato "Decoding". TorchScript non consente l'esportazione di modelli che hanno pesi +legati, quindi รจ necessario prima slegare e clonare i pesi. + +Ciรฒ implica che i modelli istanziati con il flag `torchscript` hanno il loro strato `Embedding` e strato `Decoding` +separato, il che significa che non dovrebbero essere addestrati in futuro. L'allenamento de-sincronizza i due +strati, portando a risultati inaspettati. + +Questo non รจ il caso per i modelli che non hanno una testa del modello linguistico, poichรฉ quelli non hanno pesi legati. Questi modelli +puรฒ essere esportato in sicurezza senza il flag `torchscript`. + +### Input fittizi e standard lengths + +Gli input fittizzi sono usati per fare un modello passaggio in avanti . Mentre i valori degli input si propagano attraverso i strati, +Pytorch tiene traccia delle diverse operazioni eseguite su ciascun tensore. Queste operazioni registrate vengono quindi utilizzate per +creare la "traccia" del modello. + +La traccia viene creata relativamente alle dimensioni degli input. รˆ quindi vincolato dalle dimensioni dell'input +fittizio e non funzionerร  per altre lunghezze di sequenza o dimensioni batch. Quando si proverร  con una dimensione diversa, ci sarร  errore +come: + +`La dimensione espansa del tensore (3) deve corrispondere alla dimensione esistente (7) nella dimensione non singleton 2` + +will be raised. Si consiglia pertanto di tracciare il modello con una dimensione di input fittizia grande almeno quanto il piรน grande +input che verrร  fornito al modello durante l'inferenza. รˆ possibile eseguire il padding per riempire i valori mancanti. Il modello +sarร  tracciato con una grande dimensione di input, tuttavia, anche le dimensioni della diverse matrici saranno grandi, +risultando in piรน calcoli. + +Si raccomanda di prestare attenzione al numero totale di operazioni eseguite su ciascun input e di seguire da vicino le prestazioni +durante l'esportazione di modelli di sequenza-lunghezza variabili. + +### Usare TorchSscript in Python + +Di seguito รจ riportato un esempio, che mostra come salvare, caricare modelli e come utilizzare la traccia per l'inferenza. + +#### Salvare un modello + +Questo frammento di codice mostra come usare TorchScript per esportare un `BertModel`. Qui il `BertModel` รจ istanziato secondo +una classe `BertConfig` e quindi salvato su disco con il nome del file `traced_bert.pt` + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch + +enc = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") + +# Tokenizing input text +text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" +tokenized_text = enc.tokenize(text) + +# Masking one of the input tokens +masked_index = 8 +tokenized_text[masked_index] = "[MASK]" +indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) +segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] + +# Creating a dummy input +tokens_tensor = torch.tensor([indexed_tokens]) +segments_tensors = torch.tensor([segments_ids]) +dummy_input = [tokens_tensor, segments_tensors] + +# Initializing the model with the torchscript flag +# Flag set to True even though it is not necessary as this model does not have an LM Head. +config = BertConfig( + vocab_size_or_config_json_file=32000, + hidden_size=768, + num_hidden_layers=12, + num_attention_heads=12, + intermediate_size=3072, + torchscript=True, +) + +# Instantiating the model +model = BertModel(config) + +# The model needs to be in evaluation mode +model.eval() + +# If you are instantiating the model with *from_pretrained* you can also easily set the TorchScript flag +model = BertModel.from_pretrained("google-bert/bert-base-uncased", torchscript=True) + +# Creating the trace +traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) +torch.jit.save(traced_model, "traced_bert.pt") +``` + +#### Caricare un modello + +Questo frammento di codice mostra come caricare il `BertModel` che era stato precedentemente salvato su disco con il nome `traced_bert.pt`. +Stiamo riutilizzando il `dummy_input` precedentemente inizializzato. + +```python +loaded_model = torch.jit.load("traced_bert.pt") +loaded_model.eval() + +all_encoder_layers, pooled_output = loaded_model(*dummy_input) +``` + +#### Utilizzare un modello tracciato per l'inferenza + +Usare il modello tracciato per l'inferenza รจ semplice come usare il suo metodo dunder `__call__`: + +```python +traced_model(tokens_tensor, segments_tensors) +``` + +### Implementare modelli HuggingFace TorchScript su AWS utilizzando Neuron SDK + +AWS ha introdotto [Amazon EC2 Inf1](https://aws.amazon.com/ec2/instance-types/inf1/) +famiglia di istanze per l'inferenza di machine learning a basso costo e ad alte prestazioni nel cloud. +Le istanze Inf1 sono alimentate dal chip AWS Inferentia, un acceleratore hardware personalizzato, +specializzato in carichi di lavoro di inferenza di deep learning. +[AWS Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#) +รจ l'SDK per Inferentia che supporta il tracciamento e l'ottimizzazione dei modelli transformers per +distribuzione su Inf1. L'SDK Neuron fornisce: + + +1. API di facile utilizzo con una riga di modifica del codice per tracciare e ottimizzare un modello TorchScript per l'inferenza nel cloud. +2. Ottimizzazioni delle prestazioni pronte all'uso per [miglioramento dei costi-prestazioni](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/>) +3. Supporto per i modelli di trasformatori HuggingFace costruiti con [PyTorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html) + o [TensorFlow](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html). + +#### Implicazioni + +Modelli Transformers basati su architettura [BERT (Bidirectional Encoder Representations from Transformers)](https://huggingface.co/docs/transformers/main/model_doc/bert), +o sue varianti come [distilBERT](https://huggingface.co/docs/transformers/main/model_doc/distilbert) +e [roBERTa](https://huggingface.co/docs/transformers/main/model_doc/roberta) +funzioneranno meglio su Inf1 per attivitร  non generative come la question answering estrattive, +Classificazione della sequenza, Classificazione dei token. In alternativa, generazione di testo +le attivitร  possono essere adattate per essere eseguite su Inf1, secondo questo [tutorial AWS Neuron MarianMT](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html). +Ulteriori informazioni sui modelli che possono essere convertiti fuori dagli schemi su Inferentia possono essere +trovati nella [sezione Model Architecture Fit della documentazione Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia). + +#### Dipendenze + +L'utilizzo di AWS Neuron per convertire i modelli richiede le seguenti dipendenze e l'ambiente: + +* A [Neuron SDK environment](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide), + which comes pre-configured on [AWS Deep Learning AMI](https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html). + +#### Convertire un modello per AWS Neuron + +Usando lo stesso script come in [Usando TorchScipt in Python](https://huggingface.co/docs/transformers/main/en/serialization#using-torchscript-in-python) +per tracciare un "BertModel", importi l'estensione del framework `torch.neuron` per accedere +i componenti di Neuron SDK tramite un'API Python. + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch +import torch.neuron +``` +E modificare solo la riga di codice di traccia + +Da: + +```python +torch.jit.trace(model, [tokens_tensor, segments_tensors]) +``` + +A: + +```python +torch.neuron.trace(model, [token_tensor, segments_tensors]) +``` + +Questa modifica consente a Neuron SDK di tracciare il modello e ottimizzarlo per l'esecuzione nelle istanze Inf1. + +Per ulteriori informazioni sulle funzionalitร , gli strumenti, i tutorial di esempi e gli ultimi aggiornamenti di AWS Neuron SDK, +consultare la [documentazione AWS NeuronSDK](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/training.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/training.md new file mode 100644 index 0000000000000000000000000000000000000000..a65b23e1cb98b27ca1aca7afe0e50d30efac69c0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/it/training.md @@ -0,0 +1,376 @@ + + +# Fine-tuning di un modello pre-addestrato + +[[open-in-colab]] + +Ci sono benefici significativi nell'usare un modello pre-addestrato. Si riducono i costi computazionali, l'impronta di carbonio e ti consente di usare modelli stato dell'arte senza doverli addestrare da zero. ๐Ÿค— Transformers consente l'accesso a migliaia di modelli pre-addestrati per un'ampia gamma di compiti. Quando usi un modello pre-addestrato, lo alleni su un dataset specifico per il tuo compito. Questo รจ conosciuto come fine-tuning, una tecnica di addestramento incredibilmente potente. In questa esercitazione, potrai fare il fine-tuning di un modello pre-addestrato, con un framework di deep learning a tua scelta: + +* Fine-tuning di un modello pre-addestrato con ๐Ÿค— Transformers [`Trainer`]. +* Fine-tuning di un modello pre-addestrato in TensorFlow con Keras. +* Fine-tuning di un modello pre-addestrato con PyTorch. + + + +## Preparare un dataset + + + +Prima di poter fare il fine-tuning di un modello pre-addestrato, scarica un dataset e preparalo per l'addestramento. La precedente esercitazione ti ha mostrato come processare i dati per l'addestramento e adesso hai l'opportunitร  di metterti alla prova! + +Inizia caricando il dataset [Yelp Reviews](https://huggingface.co/datasets/yelp_review_full): + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("yelp_review_full") +>>> dataset["train"][100] +{'label': 0, + 'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'} +``` + +Come giร  sai, hai bisogno di un tokenizer per processare il testo e includere una strategia di padding e truncation per gestire sequenze di lunghezza variabile. Per processare il dataset in un unico passo, usa il metodo [`map`](https://huggingface.co/docs/datasets/process#map) di ๐Ÿค— Datasets che applica la funzione di preprocessing all'intero dataset: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") + + +>>> def tokenize_function(examples): +... return tokenizer(examples["text"], padding="max_length", truncation=True) + + +>>> tokenized_datasets = dataset.map(tokenize_function, batched=True) +``` + +Se vuoi, puoi creare un sottoinsieme piรน piccolo del dataset per il fine-tuning cosรฌ da ridurre il tempo necessario: + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + + + +## Addestramento + + + + + +๐Ÿค— Transformers mette a disposizione la classe [`Trainer`] ottimizzata per addestrare modelli ๐Ÿค— Transformers, rendendo semplice iniziare l'addestramento senza scrivere manualmente il tuo ciclo di addestramento. L'API [`Trainer`] supporta un'ampia gamma di opzioni e funzionalitร  di addestramento come logging, gradient accumulation e mixed precision. + +Inizia caricando il tuo modello e specificando il numero di etichette (labels) attese. Nel dataset Yelp Review [dataset card](https://huggingface.co/datasets/yelp_review_full#data-fields), sai che ci sono cinque etichette: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + + + +Potresti vedere un warning dato che alcuni dei pesi pre-addestrati non sono stati utilizzati e altri pesi sono stati inizializzati casualmente. Non preoccuparti, รจ completamente normale! L'head pre-addestrata del modello BERT viene scartata e rimpiazzata da una classification head inizializzata casualmente. Farai il fine-tuning di questa nuova head del modello sul tuo compito di classificazione, trasferendogli la conoscenza del modello pre-addestrato. + + + +### Iperparametri per il training + +Successivamente, crea una classe [`TrainingArguments`] contenente tutti gli iperparametri che si possono regore nonchรฉ le variabili per attivare le differenti opzioni di addestramento. Per questa esercitazione puoi iniziare con gli [iperparametri](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments) di ddestramento predefiniti, ma sentiti libero di sperimentare per trovare la configurazione ottimale per te. + +Specifica dove salvare i checkpoints del tuo addestramento: + +```py +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments(output_dir="test_trainer") +``` + +### Metriche + +[`Trainer`] non valuta automaticamente le performance del modello durante l'addestramento. Dovrai passare a [`Trainer`] una funzione che calcola e restituisce le metriche. La libreria ๐Ÿค— Datasets mette a disposizione una semplice funzione [`accuracy`](https://huggingface.co/metrics/accuracy) che puoi caricare con la funzione `load_metric` (guarda questa [esercitazione](https://huggingface.co/docs/datasets/metrics) per maggiori informazioni): + +```py +>>> import numpy as np +>>> from datasets import load_metric + +>>> metric = load_metric("accuracy") +``` + +Richiama `compute` su `metric` per calcolare l'accuratezza delle tue previsioni. Prima di passare le tue previsioni a `compute`, hai bisogno di convertirle in logits (ricorda che tutti i modelli ๐Ÿค— Transformers restituiscono logits): + +```py +>>> def compute_metrics(eval_pred): +... logits, labels = eval_pred +... predictions = np.argmax(logits, axis=-1) +... return metric.compute(predictions=predictions, references=labels) +``` + +Se preferisci monitorare le tue metriche di valutazione durante il fine-tuning, specifica il parametro `eval_strategy` nei tuoi training arguments per restituire le metriche di valutazione ad ogni epoca di addestramento: + +```py +>>> from transformers import TrainingArguments, Trainer + +>>> training_args = TrainingArguments(output_dir="test_trainer", eval_strategy="epoch") +``` + +### Trainer + +Crea un oggetto [`Trainer`] col tuo modello, training arguments, dataset di training e test, e funzione di valutazione: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +Poi metti a punto il modello richiamando [`~transformers.Trainer.train`]: + +```py +>>> trainer.train() +``` + + + + + + +I modelli ๐Ÿค— Transformers supportano anche l'addestramento in TensorFlow usando l'API di Keras. + +### Convertire dataset nel formato per TensorFlow + +Il [`DefaultDataCollator`] assembla tensori in lotti su cui il modello si addestrerร . Assicurati di specificare di restituire tensori per TensorFlow in `return_tensors`: + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +[`Trainer`] usa [`DataCollatorWithPadding`] in maniera predefinita in modo da non dover specificare esplicitamente un collettore di dati. + + + +Successivamente, converti i datasets tokenizzati in TensorFlow datasets con il metodo [`to_tf_dataset`](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.to_tf_dataset). Specifica il tuo input in `columns` e le tue etichette in `label_cols`: + +```py +>>> tf_train_dataset = small_train_dataset.to_tf_dataset( +... columns=["attention_mask", "input_ids", "token_type_ids"], +... label_cols=["labels"], +... shuffle=True, +... collate_fn=data_collator, +... batch_size=8, +... ) + +>>> tf_validation_dataset = small_eval_dataset.to_tf_dataset( +... columns=["attention_mask", "input_ids", "token_type_ids"], +... label_cols=["labels"], +... shuffle=False, +... collate_fn=data_collator, +... batch_size=8, +... ) +``` + +### Compilazione e addestramento + +Carica un modello TensorFlow col numero atteso di etichette: + +```py +>>> import tensorflow as tf +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + +Poi compila e fai il fine-tuning del tuo modello usando [`fit`](https://keras.io/api/models/model_training_apis/) come faresti con qualsiasi altro modello di Keras: + +```py +>>> model.compile( +... optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5), +... loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), +... metrics=tf.metrics.SparseCategoricalAccuracy(), +... ) + +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3) +``` + + + + + +## Addestramento in PyTorch nativo + + + + + +[`Trainer`] si occupa del ciclo di addestramento e ti consente di mettere a punto un modello con una sola riga di codice. Per chi preferisse scrivere un proprio ciclo di addestramento personale, puoi anche fare il fine-tuning di un modello ๐Ÿค— Transformers in PyTorch nativo. + +A questo punto, potresti avere bisogno di riavviare il tuo notebook o eseguire il seguente codice per liberare un po' di memoria: + +```py +del model +del pytorch_model +del trainer +torch.cuda.empty_cache() +``` + +Successivamente, postprocessa manualmente il `tokenized_dataset` per prepararlo ad essere allenato. + +1. Rimuovi la colonna `text` perchรฉ il modello non accetta testo grezzo come input: + + ```py + >>> tokenized_datasets = tokenized_datasets.remove_columns(["text"]) + ``` + +2. Rinomina la colonna `label` in `labels` perchรฉ il modello si aspetta che questo argomento si chiami `labels`: + + ```py + >>> tokenized_datasets = tokenized_datasets.rename_column("label", "labels") + ``` + +3. Imposta il formato del dataset per farti restituire tensori di PyTorch all'interno delle liste: + + ```py + >>> tokenized_datasets.set_format("torch") + ``` + +Poi crea un piccolo sottocampione del dataset come visto precedentemente per velocizzare il fine-tuning: + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + +### DataLoader + +Crea un `DataLoader` per i tuoi datasets di train e test cosรฌ puoi iterare sui lotti di dati: + +```py +>>> from torch.utils.data import DataLoader + +>>> train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8) +>>> eval_dataloader = DataLoader(small_eval_dataset, batch_size=8) +``` + +Carica il tuo modello con il numero atteso di etichette: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + +### Ottimizzatore e learning rate scheduler + +Crea un ottimizzatore e il learning rate scheduler per fare il fine-tuning del modello. Usa l'ottimizzatore [`AdamW`](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) di PyTorch: + +```py +>>> from torch.optim import AdamW + +>>> optimizer = AdamW(model.parameters(), lr=5e-5) +``` + +Crea il learning rate scheduler predefinito da [`Trainer`]: + +```py +>>> from transformers import get_scheduler + +>>> num_epochs = 3 +>>> num_training_steps = num_epochs * len(train_dataloader) +>>> lr_scheduler = get_scheduler( +... name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps +... ) +``` + +Infine specifica come `device` da usare una GPU se ne hai una. Altrimenti, l'addestramento su una CPU puรฒ richiedere diverse ore invece di un paio di minuti. + +```py +>>> import torch + +>>> device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +>>> model.to(device) +``` + + + +Ottieni l'accesso gratuito a una GPU sul cloud se non ne possiedi una usando un notebook sul web come [Colaboratory](https://colab.research.google.com/) o [SageMaker StudioLab](https://studiolab.sagemaker.aws/). + + + +Ottimo, adesso possiamo addestrare! ๐Ÿฅณ + +### Training loop + +Per tenere traccia dei tuoi progressi durante l'addestramento, usa la libreria [tqdm](https://tqdm.github.io/) per aggiungere una progress bar sopra il numero dei passi di addestramento: + +```py +>>> from tqdm.auto import tqdm + +>>> progress_bar = tqdm(range(num_training_steps)) + +>>> model.train() +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... outputs = model(**batch) +... loss = outputs.loss +... loss.backward() + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +### Metriche + +Proprio come รจ necessario aggiungere una funzione di valutazione del [`Trainer`], รจ necessario fare lo stesso quando si scrive il proprio ciclo di addestramento. Ma invece di calcolare e riportare la metrica alla fine di ogni epoca, questa volta accumulerai tutti i batch con [`add_batch`](https://huggingface.co/docs/datasets/package_reference/main_classes?highlight=add_batch#datasets.Metric.add_batch) e calcolerai la metrica alla fine. + +```py +>>> metric = load_metric("accuracy") +>>> model.eval() +>>> for batch in eval_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... with torch.no_grad(): +... outputs = model(**batch) + +... logits = outputs.logits +... predictions = torch.argmax(logits, dim=-1) +... metric.add_batch(predictions=predictions, references=batch["labels"]) + +>>> metric.compute() +``` + + + + + +## Altre risorse + +Per altri esempi sul fine-tuning, fai riferimento a: + +- [๐Ÿค— Transformers Examples](https://github.com/huggingface/transformers/tree/main/examples) include scripts per addestrare compiti comuni di NLP in PyTorch e TensorFlow. + +- [๐Ÿค— Transformers Notebooks](https://github.com/huggingface/notebooks) contiene diversi notebooks su come mettere a punto un modello per compiti specifici in PyTorch e TensorFlow. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/_toctree.yml b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/_toctree.yml new file mode 100644 index 0000000000000000000000000000000000000000..a8a01dbd9cd4e4de1a7e868934c1cb5c4ff8d9ab --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/_toctree.yml @@ -0,0 +1,386 @@ +- sections: + - local: index + title: ๐Ÿค— Transformers + - local: quicktour + title: ใ‚ฏใ‚คใƒƒใ‚ฏใƒ„ใ‚ขใƒผ + - local: installation + title: ใ‚คใƒณใ‚นใƒˆใƒผใƒซ + title: Get started +- sections: + - local: pipeline_tutorial + title: ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ + - local: autoclass_tutorial + title: AutoClass ใ‚’ไฝฟ็”จใ—ใฆ็งปๆคๅฏ่ƒฝใชใ‚ณใƒผใƒ‰ใ‚’ไฝœๆˆใ™ใ‚‹ + - local: preprocessing + title: ใƒ‡ใƒผใ‚ฟใฎๅ‰ๅ‡ฆ็† + - local: training + title: ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ + - local: run_scripts + title: ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ + - local: accelerate + title: ๐Ÿค— Accelerate ใ‚’ไฝฟ็”จใ—ใฆๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ + - local: peft + title: ๐Ÿค— PEFT ใ‚’ไฝฟ็”จใ—ใฆใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ + - local: model_sharing + title: ใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ + - local: llm_tutorial + title: LLM ใ‚’ไฝฟ็”จใ—ใŸ็”Ÿๆˆ + title: Tutorials +- sections: + - isExpanded: false + sections: + - local: tasks/token_classification + title: ใƒˆใƒผใ‚ฏใƒณใฎๅˆ†้กž + - local: tasks/question_answering + title: ่ณช็–‘ๅฟœ็ญ” + - local: tasks/language_modeling + title: ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + - local: tasks/masked_language_modeling + title: ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + - local: tasks/translation + title: ็ฟป่จณ + - local: tasks/summarization + title: ่ฆ็ด„ + - local: tasks/multiple_choice + title: ่ค‡ๆ•ฐใฎ้ธๆŠž่‚ข + title: ่‡ช็„ถ่จ€่ชžๅ‡ฆ็† + - isExpanded: false + sections: + - local: tasks/audio_classification + title: ้Ÿณๅฃฐใฎๅˆ†้กž + - local: tasks/asr + title: ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ + title: ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช + - isExpanded: false + sections: + - local: tasks/image_classification + title: ็”ปๅƒๅˆ†้กž + - local: tasks/semantic_segmentation + title: ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ + - local: tasks/video_classification + title: ใƒ“ใƒ‡ใ‚ชใฎๅˆ†้กž + - local: tasks/object_detection + title: ็‰ฉไฝ“ๆคœๅ‡บ + - local: tasks/zero_shot_object_detection + title: ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็‰ฉไฝ“ๆคœๅ‡บ + - local: tasks/zero_shot_image_classification + title: ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กž + - local: tasks/monocular_depth_estimation + title: ๆทฑใ•ใฎๆŽจๅฎš + - local: tasks/image_to_image + title: ็”ปๅƒใ‹ใ‚‰็”ปๅƒใธ + - local: tasks/knowledge_distillation_for_image_classification + title: ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใŸใ‚ใฎ็Ÿฅ่ญ˜ใฎ่’ธ็•™ + title: ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณ + - isExpanded: false + sections: + - local: tasks/image_captioning + title: ็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ + - local: tasks/document_question_answering + title: ๆ–‡ๆ›ธใฎ่ณชๅ•ใธใฎๅ›ž็ญ” + - local: tasks/visual_question_answering + title: ่ฆ–่ฆš็š„ใช่ณชๅ•ใธใฎๅ›ž็ญ” + - local: tasks/text-to-speech + title: ใƒ†ใ‚ญใ‚นใƒˆ่ชญใฟไธŠใ’ + title: ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ + - isExpanded: false + sections: + - local: generation_strategies + title: ็”Ÿๆˆๆˆฆ็•ฅใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ™ใ‚‹ + title: ไธ–ไปฃ + - isExpanded: false + sections: + - local: tasks/idefics + title: IDEFICS ใ‚’ไฝฟ็”จใ—ใŸใ‚คใƒกใƒผใ‚ธ ใ‚ฟใ‚นใ‚ฏ + - local: tasks/prompting + title: LLM ใƒ—ใƒญใƒณใƒ—ใƒˆ ใ‚ฌใ‚คใƒ‰ + title: ใƒ—ใƒญใƒณใƒ—ใƒˆ + title: Task Guides +- sections: + - local: fast_tokenizers + title: ๐Ÿค— ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ้ซ˜้€Ÿใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ + - local: multilingual + title: ๅคš่จ€่ชžใƒขใƒ‡ใƒซใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ + - local: create_a_model + title: ใƒขใƒ‡ใƒซๅ›บๆœ‰ใฎ API ใ‚’ไฝฟ็”จใ™ใ‚‹ + - local: custom_models + title: ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ + - local: chat_templating + title: ใƒใƒฃใƒƒใƒˆใƒขใƒ‡ใƒซใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ + - local: serialization + title: ONNX ใธใฎใ‚จใ‚ฏใ‚นใƒใƒผใƒˆ + - local: tflite + title: TFLite ใธใฎใ‚จใ‚ฏใ‚นใƒใƒผใƒˆ + - local: torchscript + title: ใƒˆใƒผใƒใ‚นใ‚ฏใƒชใƒ—ใƒˆใธใฎใ‚จใ‚ฏใ‚นใƒใƒผใƒˆ + - local: community + title: ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใƒชใ‚ฝใƒผใ‚น + - local: troubleshooting + title: ใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐ + title: ้–‹็™บ่€…ใ‚ฌใ‚คใƒ‰ +- sections: + - local: performance + title: ๆฆ‚่ฆ + - sections: + - local: perf_train_gpu_one + title: ๅ˜ไธ€ใฎ GPU ใงๅŠน็އ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใจใƒ„ใƒผใƒซ + - local: perf_train_gpu_many + title: ่ค‡ๆ•ฐใฎ GPU ใจไธฆๅˆ—ๅ‡ฆ็† + - local: perf_train_cpu + title: CPU ใงใฎๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ + - local: perf_train_cpu_many + title: ๅˆ†ๆ•ฃCPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ + - local: perf_train_tpu + title: TPU ใซ้–ขใ™ใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ + - local: perf_train_tpu_tf + title: TensorFlow ใ‚’ไฝฟ็”จใ—ใŸ TPU ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ + - local: perf_train_special + title: ็‰นๆฎŠใชใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซ้–ขใ™ใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ + - local: perf_hardware + title: ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ข + - local: hpo_train + title: Trainer API ใ‚’ไฝฟ็”จใ—ใŸใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟๆคœ็ดข + title: ๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏ + - sections: + - local: perf_infer_cpu + title: CPUใงใฎๆŽจ่ซ– + - local: perf_infer_gpu_one + title: 1 ใคใฎ GPU ใงใฎๆŽจ่ซ– + - local: perf_infer_gpu_many + title: ๅคšใใฎ GPU ใงใฎๆŽจ่ซ– + - local: perf_infer_special + title: ็‰นๆฎŠใชใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใฎๆŽจ่ซ– + title: ๆŽจ่ซ–ใฎๆœ€้ฉๅŒ– + - local: big_models + title: ๅคงใใชใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ– + - local: tf_xla + title: TensorFlowใƒขใƒ‡ใƒซใฎXLA็ตฑๅˆ + - local: perf_torch_compile + title: torch.compile()ใ‚’ไฝฟ็”จใ—ใŸๆŽจ่ซ–ใฎๆœ€้ฉๅŒ– + title: ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃ +- sections: + - local: add_new_model + title: ๐Ÿค— Transformersใซใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๆ–นๆณ• + - local: testing + title: ใƒ†ใ‚นใƒˆ + - local: pr_checks + title: ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใฎใƒใ‚งใƒƒใ‚ฏ + title: ่ฒข็Œฎใ™ใ‚‹ +- sections: + - local: philosophy + title: ใƒ•ใ‚ฃใƒญใ‚ฝใƒ•ใ‚ฃใƒผ + - local: glossary + title: ็”จ่ชž้›† + - local: task_summary + title: ๐Ÿค— TransformersใฎๆฉŸ่ƒฝ + - local: tasks_explained + title: ๐Ÿค— TransformersใŒใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ๆ–นๆณ• + - local: model_summary + title: Transformerใƒขใƒ‡ใƒซใƒ•ใ‚กใƒŸใƒชใƒผ + - local: tokenizer_summary + title: ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎๆฆ‚่ฆ + - local: attention + title: ๆณจๆ„ๆฉŸๆง‹ + - local: pad_truncation + title: ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ + - local: bertology + title: BERTology + - local: perplexity + title: ๅ›บๅฎš้•ทใƒขใƒ‡ใƒซใฎใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃ + - local: pipeline_webserver + title: Webใ‚ตใƒผใƒใƒผๆŽจ่ซ–็”จใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ + - local: model_memory_anatomy + title: ใƒขใƒ‡ใƒซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่งฃๅ‰–ๅญฆ + title: ใ‚ณใƒณใ‚ปใƒ—ใƒใƒฅใ‚ขใƒซใ‚ฌใ‚คใƒ‰ +- sections: + - sections: + - local: model_doc/auto + title: Auto Classes + - local: main_classes/callback + title: ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ + - local: main_classes/configuration + title: ๆง‹ๆˆ + - local: main_classes/data_collator + title: ใƒ‡ใƒผใ‚ฟ็…งๅˆ่€… + - local: main_classes/keras_callbacks + title: Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ + - local: main_classes/logging + title: ใƒญใ‚ฎใƒณใ‚ฐ + - local: main_classes/model + title: ใƒขใƒ‡ใƒซ + - local: main_classes/text_generation + title: ใƒ†ใ‚ญใ‚นใƒˆใฎ็”Ÿๆˆ + - local: main_classes/onnx + title: ONNX + - local: main_classes/optimizer_schedules + title: ๆœ€้ฉๅŒ– + - local: main_classes/output + title: ใƒขใƒ‡ใƒซใฎๅ‡บๅŠ› + - local: main_classes/pipelines + title: ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ + - local: main_classes/processors + title: ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผ + - local: main_classes/quantization + title: ้‡ๅญๅŒ– + - local: main_classes/tokenizer + title: ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ + - local: main_classes/trainer + title: ใƒˆใƒฌใƒผใƒŠใƒผ + - local: main_classes/deepspeed + title: ใƒ‡ใ‚ฃใƒผใƒ—ใ‚นใƒ”ใƒผใƒ‰ใฎ็ตฑๅˆ + - local: main_classes/feature_extractor + title: ็‰นๅพดๆŠฝๅ‡บๅ™จ + - local: main_classes/image_processor + title: ็”ปๅƒๅ‡ฆ็†ใƒ—ใƒญใ‚ปใƒƒใ‚ต + title: ไธป่ฆใชใ‚ฏใƒฉใ‚น + - sections: + - isExpanded: false + sections: + - local: model_doc/albert + title: ALBERT + - local: model_doc/bart + title: BART + - local: model_doc/barthez + title: BARThez + - local: model_doc/bartpho + title: BARTpho + - local: model_doc/bert + title: BERT + - local: model_doc/bert-generation + title: BertGeneration + - local: model_doc/bert-japanese + title: BertJapanese + - local: model_doc/bertweet + title: Bertweet + - local: model_doc/big_bird + title: BigBird + - local: model_doc/bigbird_pegasus + title: BigBirdPegasus + - local: model_doc/biogpt + title: BioGpt + - local: model_doc/blenderbot + title: Blenderbot + - local: model_doc/blenderbot-small + title: Blenderbot Small + - local: model_doc/bloom + title: BLOOM + - local: model_doc/bort + title: BORT + - local: model_doc/byt5 + title: ByT5 + - local: model_doc/camembert + title: CamemBERT + - local: model_doc/canine + title: CANINE + - local: model_doc/codegen + title: CodeGen + - local: model_doc/code_llama + title: CodeLlama + - local: model_doc/convbert + title: ConvBERT + - local: model_doc/cpm + title: CPM + - local: model_doc/cpmant + title: CPMANT + - local: model_doc/ctrl + title: CTRL + - local: model_doc/deberta + title: DeBERTa + - local: model_doc/deberta-v2 + title: DeBERTa-v2 + - local: model_doc/dialogpt + title: DialoGPT + title: ๆ–‡็ซ ใƒขใƒ‡ใƒซ + - isExpanded: false + sections: + - local: model_doc/beit + title: BEiT + - local: model_doc/bit + title: BiT + - local: model_doc/conditional_detr + title: Conditional DETR + - local: model_doc/convnext + title: ConvNeXT + - local: model_doc/convnextv2 + title: ConvNeXTV2 + - local: model_doc/cvt + title: CvT + - local: model_doc/deformable_detr + title: Deformable DETR + - local: model_doc/deit + title: DeiT + - local: model_doc/deta + title: DETA + - local: model_doc/detr + title: DETR + - local: model_doc/dinat + title: DiNAT + title: ใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซ + - isExpanded: false + sections: + - local: model_doc/audio-spectrogram-transformer + title: Audio Spectrogram Transformer + - local: model_doc/bark + title: Bark + - local: model_doc/clap + title: CLAP + title: ้Ÿณๅฃฐใƒขใƒ‡ใƒซ + - isExpanded: false + sections: + - local: model_doc/align + title: ALIGN + - local: model_doc/altclip + title: AltCLIP + - local: model_doc/blip + title: BLIP + - local: model_doc/blip-2 + title: BLIP-2 + - local: model_doc/bridgetower + title: BridgeTower + - local: model_doc/bros + title: BROS + - local: model_doc/chinese_clip + title: Chinese-CLIP + - local: model_doc/clip + title: CLIP + - local: model_doc/clipseg + title: CLIPSeg + - local: model_doc/clvp + title: CLVP + - local: model_doc/data2vec + title: Data2Vec + - local: model_doc/deplot + title: DePlot + title: ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใƒขใƒ‡ใƒซ + - isExpanded: false + sections: + - local: model_doc/decision_transformer + title: Decision Transformer + title: ๅผทๅŒ–ๅญฆ็ฟ’ใƒขใƒ‡ใƒซ + - isExpanded: false + sections: + - local: model_doc/autoformer + title: Autoformer + title: ๆ™‚็ณปๅˆ—ใƒขใƒ‡ใƒซ + title: ใƒขใƒ‡ใƒซ + - sections: + - local: internal/modeling_utils + title: ใ‚ซใ‚นใ‚ฟใƒ ใƒฌใ‚คใƒคใƒผใจใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/pipelines_utils + title: ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/tokenization_utils + title: ใƒˆ=ใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/trainer_utils + title: ใƒˆใƒฌใƒผใƒŠใƒผ็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/generation_utils + title: ็™บ้›ป็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/image_processing_utils + title: ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ต็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/audio_utils + title: ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅ‡ฆ็†็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + - local: internal/file_utils + title: ไธ€่ˆฌๅ…ฌๅ…ฑไบ‹ๆฅญ + - local: internal/time_series_utils + title: ๆ™‚็ณปๅˆ—็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + title: ๅ†…้ƒจใƒ˜ใƒซใƒ‘ใƒผ + title: API diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/accelerate.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/accelerate.md new file mode 100644 index 0000000000000000000000000000000000000000..73e45b9cd3c5ecbbf950698cdb7a3dd34d6f2f92 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/accelerate.md @@ -0,0 +1,136 @@ + + +# ๐Ÿค— Accelerate ใ‚’็”จใ„ใŸๅˆ†ๆ•ฃๅญฆ็ฟ’ + +ใƒขใƒ‡ใƒซใŒๅคงใใใชใ‚‹ใซใคใ‚Œใฆใ€้™ใ‚‰ใ‚ŒใŸใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใ‚’่จ“็ทดใ—ใ€่จ“็ทด้€Ÿๅบฆใ‚’ๅคงๅน…ใซไธŠๆ˜‡ใ•ใ›ใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใจใ—ใฆไธฆๅˆ—ๅ‡ฆ็†ใŒๆตฎไธŠใ—ใฆใใพใ—ใŸใ€‚1ๅฐใฎใƒžใ‚ทใƒณใซ่ค‡ๆ•ฐใฎGPUใŒใ‚ใฃใฆใ‚‚ใ€่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณใซใพใŸใŒใ‚‹่ค‡ๆ•ฐใฎGPUใŒใ‚ใฃใฆใ‚‚ใ€ใ‚ใ‚‰ใ‚†ใ‚‹ใ‚ฟใ‚คใƒ—ใฎๅˆ†ๆ•ฃๅ‡ฆ็†ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ไธŠใงใƒฆใƒผใ‚ถใƒผใŒ็ฐกๅ˜ใซ ๐Ÿค— Transformers ใƒขใƒ‡ใƒซใ‚’่จ“็ทดใงใใ‚‹ใ‚ˆใ†ใซใ€ Hugging Face ใงใฏ [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝœๆˆใ—ใพใ—ใŸใ€‚ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€PyTorch ใฎ่จ“็ทดใƒซใƒผใƒ—ใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ—ใฆใ€ๅˆ†ๆ•ฃๅ‡ฆ็†็’ฐๅขƒใงใฎ่จ“็ทดใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆๅญฆใณใพใ™ใ€‚ + +## ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ— + +ใฏใ˜ใ‚ใซ ๐Ÿค— Accelerate ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ—ใ‚‡ใ†: + +```bash +pip install accelerate +``` + +ใใ—ใŸใ‚‰ใ‚คใƒณใƒใƒผใƒˆใ—ใฆ [`~accelerate.Accelerator`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚[`~accelerate.Accelerator`] ใฏๅˆ†ๆ•ฃๅ‡ฆ็†ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’่‡ชๅ‹•็š„ใซๆคœๅ‡บใ—ใ€่จ“็ทดใฎใŸใ‚ใซๅฟ…่ฆใชๅ…จใฆใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ใƒ‡ใƒใ‚คใ‚นใซๆ˜Ž็คบ็š„ใซ้…็ฝฎใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +```py +>>> from accelerate import Accelerator + +>>> accelerator = Accelerator() +``` + +## Accelerate ใ™ใ‚‹ๆบ–ๅ‚™ใ‚’ใ—ใพใ—ใ‚‡ใ† + +ๆฌกใซใ€้–ข้€ฃใ™ใ‚‹ๅ…จใฆใฎ่จ“็ทดใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ [`~accelerate.Accelerator.prepare`] ใƒกใ‚ฝใƒƒใƒ‰ใซๆธกใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€่จ“็ทดใจ่ฉ•ไพกใใ‚Œใžใ‚ŒใฎDataloaderใ€ใƒขใƒ‡ใƒซใ€optimizer ใŒๅซใพใ‚Œใพใ™: + +```py +>>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( +... train_dataloader, eval_dataloader, model, optimizer +... ) +``` + +## Backward + +ๆœ€ๅพŒใซ่จ“็ทดใƒซใƒผใƒ—ๅ†…ใฎ `loss.backward()` ใ‚’ ๐Ÿค— Accelerate ใฎ [`~accelerate.Accelerator.backward`] ใƒกใ‚ฝใƒƒใƒ‰ใง็ฝฎใๆ›ใˆใพใ™๏ผš + +```py +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... outputs = model(**batch) +... loss = outputs.loss +... accelerator.backward(loss) + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใง็ขบ่ชใงใใ‚‹้€šใ‚Šใ€่จ“็ทดใƒซใƒผใƒ—ใซ4่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๅˆ†ๆ•ฃๅญฆ็ฟ’ใŒๅฏ่ƒฝใงใ™๏ผ + +```diff ++ from accelerate import Accelerator + from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + ++ accelerator = Accelerator() + + model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) + optimizer = AdamW(model.parameters(), lr=3e-5) + +- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +- model.to(device) + ++ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ++ train_dataloader, eval_dataloader, model, optimizer ++ ) + + num_epochs = 3 + num_training_steps = num_epochs * len(train_dataloader) + lr_scheduler = get_scheduler( + "linear", + optimizer=optimizer, + num_warmup_steps=0, + num_training_steps=num_training_steps + ) + + progress_bar = tqdm(range(num_training_steps)) + + model.train() + for epoch in range(num_epochs): + for batch in train_dataloader: +- batch = {k: v.to(device) for k, v in batch.items()} + outputs = model(**batch) + loss = outputs.loss +- loss.backward() ++ accelerator.backward(loss) + + optimizer.step() + lr_scheduler.step() + optimizer.zero_grad() + progress_bar.update(1) +``` + +## ่จ“็ทดใ™ใ‚‹ + +้–ข้€ฃใ™ใ‚‹ใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ—ใŸใ‚‰ใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใพใŸใฏ Colaboratory ใชใฉใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใง่จ“็ทดใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚ + +### ใ‚นใ‚ฏใƒชใƒ—ใƒˆใง่จ“็ทดใ™ใ‚‹ + +ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‹ใ‚‰่จ“็ทดใ‚’ใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใƒปไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใซไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„: + +```bash +accelerate config +``` + +ใใ—ใฆๆฌกใฎใ‚ˆใ†ใซใ—ใฆ่จ“็ทดใ‚’้–‹ๅง‹ใ—ใพใ™: + +```bash +accelerate launch train.py +``` + +### ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใง่จ“็ทดใ™ใ‚‹ + +Colaboratory ใฎ TPU ใฎๅˆฉ็”จใ‚’ใŠ่€ƒใˆใฎๅ ดๅˆใ€๐Ÿค— Accelerate ใฏใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏไธŠใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚่จ“็ทดใซๅฟ…่ฆใชๅ…จใฆใฎใ‚ณใƒผใƒ‰ใ‚’้–ขๆ•ฐใซๅซใ‚ใ€[`~accelerate.notebook_launcher`] ใซๆธกใ—ใฆใใ ใ•ใ„: + +```py +>>> from accelerate import notebook_launcher + +>>> notebook_launcher(training_function) +``` + +๐Ÿค— Accelerate ใจ่ฑŠๅฏŒใชๆฉŸ่ƒฝใซใคใ„ใฆใ‚‚ใฃใจ็Ÿฅใ‚ŠใŸใ„ๆ–นใฏ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/accelerate)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/add_new_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/add_new_model.md new file mode 100644 index 0000000000000000000000000000000000000000..000e4fd8592431c1d14629dd96e736e0f336fa79 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/add_new_model.md @@ -0,0 +1,749 @@ + + +# How to add a model to ๐Ÿค— Transformers? + +๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใฎ่ฒข็Œฎ่€…ใฎใŠใ‹ใ’ใงๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’ๆไพ›ใงใใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ +ใ—ใ‹ใ—ใ€ใ“ใ‚Œใฏ้›ฃใ—ใ„ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใงใ‚ใ‚Šใ€๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใจๅฎŸ่ฃ…ใ™ใ‚‹ใƒขใƒ‡ใƒซใซใคใ„ใฆใฎๆทฑใ„็Ÿฅ่ญ˜ใŒๅฟ…่ฆใงใ™ใ€‚ +Hugging Faceใงใฏใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใฎๅคšใใฎไบบใ€…ใซ็ฉๆฅต็š„ใซใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅŠ›ใ‚’ไธŽใˆใ‚ˆใ†ใจๅŠชๅŠ›ใ—ใฆใŠใ‚Šใ€ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใ‚’ใพใจใ‚ใฆใ€PyTorchใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใ‚’่ชฌๆ˜Žใ—ใพใ™๏ผˆ[PyTorchใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„](https://pytorch.org/get-started/locally/)๏ผ‰ใ€‚ + +ใ“ใฎ้Ž็จ‹ใงใ€ไปฅไธ‹ใฎใ“ใจใ‚’ๅญฆใณใพใ™๏ผš + +- ใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใฎใƒ™ใ‚นใƒˆใƒ—ใƒฉใ‚ฏใƒ†ใ‚ฃใ‚นใซ้–ขใ™ใ‚‹ๆดžๅฏŸ +- ๆœ€ใ‚‚ไบบๆฐ—ใฎใ‚ใ‚‹ๆทฑๅฑคๅญฆ็ฟ’ใƒฉใ‚คใƒ–ใƒฉใƒชใฎ่จญ่จˆๅŽŸๅ‰‡ใ‚’็†่งฃใ™ใ‚‹ +- ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซใƒ†ใ‚นใƒˆใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถ +- `black`ใ€`ruff`ใ€ใŠใ‚ˆใณ`make fix-copies`ใชใฉใฎPythonใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’็ตฑๅˆใ—ใฆใ€ใ‚ฏใƒชใƒผใƒณใง่ชญใฟใ‚„ใ™ใ„ใ‚ณใƒผใƒ‰ใ‚’็ขบไฟใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถ + +Hugging Faceใƒใƒผใƒ ใฎใƒกใƒณใƒใƒผใŒใ‚ตใƒใƒผใƒˆใ‚’ๆไพ›ใ™ใ‚‹ใฎใงใ€ไธ€ไบบใผใฃใกใซใชใ‚‹ใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ๐Ÿค— โค๏ธ + +ใ•ใ‚ใ€ๅง‹ใ‚ใพใ—ใ‚‡ใ†๏ผ๐Ÿค— Transformersใง่ฆ‹ใŸใ„ใƒขใƒ‡ใƒซใซใคใ„ใฆใฎ[New model addition](https://github.com/huggingface/transformers/issues/new?assignees=&labels=New+model&template=new-model-addition.yml)ใฎใ‚คใ‚ทใƒฅใƒผใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ +็‰นๅฎšใฎใƒขใƒ‡ใƒซใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใซ็‰นใซใ“ใ ใ‚ใ‚ŠใŒใชใ„ๅ ดๅˆใ€[New model label](https://github.com/huggingface/transformers/labels/New%20model)ใงๆœชๅ‰ฒใ‚Šๅฝ“ใฆใฎใƒขใƒ‡ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใŒใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใฆใ€ใใ‚Œใซๅ–ใ‚Š็ต„ใ‚€ใ“ใจใŒใงใใพใ™ใ€‚ + +ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใŸใ‚‰ใ€ๆœ€ๅˆใฎใ‚นใƒ†ใƒƒใƒ—ใฏ๐Ÿค— Transformersใ‚’ใ‚ˆใ็†่งฃใ™ใ‚‹ใ“ใจใงใ™๏ผ + +## General overview of ๐Ÿค— Transformers + +ใพใšใ€๐Ÿค— Transformersใฎไธ€่ˆฌ็š„ใชๆฆ‚่ฆใ‚’ๆŠŠๆกใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚๐Ÿค— Transformersใฏ้žๅธธใซๆ„่ฆ‹ใŒๅˆ†ใ‹ใ‚Œใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใงใ™ใฎใงใ€ +ใƒฉใ‚คใƒ–ใƒฉใƒชใฎๅ“ฒๅญฆใ‚„่จญ่จˆ้ธๆŠžใซใคใ„ใฆๅŒๆ„ใงใใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€็งใŸใกใฎ็ตŒ้จ“ใ‹ใ‚‰ใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎๅŸบๆœฌ็š„ใช่จญ่จˆ้ธๆŠžใจๅ“ฒๅญฆใฏใ€ +๐Ÿค— Transformersใ‚’ๅŠน็އ็š„ใซใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ—ใ€้ฉๅˆ‡ใชใƒฌใƒ™ใƒซใงไฟๅฎˆใ‚ณใ‚นใƒˆใ‚’ๆŠ‘ใˆใ‚‹ใŸใ‚ใซไธๅฏๆฌ ใงใ™ใ€‚ + +ใƒฉใ‚คใƒ–ใƒฉใƒชใฎ็†่งฃใ‚’ๆทฑใ‚ใ‚‹ใŸใ‚ใฎ่‰ฏใ„ๅ‡บ็™บ็‚นใฏใ€[ๅ“ฒๅญฆใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](philosophy)ใ‚’่ชญใ‚€ใ“ใจใงใ™ใ€‚ +็งใŸใกใฎไฝœๆฅญๆ–นๆณ•ใฎ็ตๆžœใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใซ้ฉ็”จใ—ใ‚ˆใ†ใจใ™ใ‚‹ใ„ใใคใ‹ใฎ้ธๆŠž่‚ขใŒใ‚ใ‚Šใพใ™๏ผš + +- ไธ€่ˆฌ็š„ใซใ€ๆŠฝ่ฑกๅŒ–ใ‚ˆใ‚Šใ‚‚ๆง‹ๆˆใŒๅ„ชๅ…ˆใ•ใ‚Œใพใ™ใ€‚ +- ใ‚ณใƒผใƒ‰ใฎ้‡่ค‡ใฏใ€่ชญใฟใ‚„ใ™ใ•ใ‚„ใ‚ขใ‚ฏใ‚ปใ‚นๅฏ่ƒฝๆ€งใ‚’ๅคงๅน…ใซๅ‘ไธŠใ•ใ›ใ‚‹ๅ ดๅˆใ€ๅฟ…ใšใ—ใ‚‚ๆ‚ชใ„ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +- ใƒขใƒ‡ใƒซใƒ•ใ‚กใ‚คใƒซใฏใงใใ‚‹ใ ใ‘่‡ชๅทฑๅฎŒ็ต็š„ใงใ‚ใ‚‹ในใใงใ€็‰นๅฎšใฎใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใ‚’่ชญใ‚€้š›ใซใฏใ€็†ๆƒณ็š„ใซใฏ่ฉฒๅฝ“ใ™ใ‚‹`modeling_....py`ใƒ•ใ‚กใ‚คใƒซใฎใฟใ‚’่ฆ‹ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +็งใŸใกใฎๆ„่ฆ‹ใงใฏใ€ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฎใ‚ณใƒผใƒ‰ใฏๅ˜ใชใ‚‹่ฃฝๅ“ใ‚’ๆไพ›ใ™ใ‚‹ๆ‰‹ๆฎตใ ใ‘ใงใชใใ€*ไพ‹ใˆใฐใ€ๆŽจ่ซ–ใฎใŸใ‚ใซBERTใ‚’ไฝฟ็”จใ™ใ‚‹่ƒฝๅŠ›*ใชใฉใฎ่ฃฝๅ“ใใฎใ‚‚ใฎ. + +### Overview of models + +ใƒขใƒ‡ใƒซใ‚’ๆญฃๅธธใซ่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใซใฏใ€ใƒขใƒ‡ใƒซใจใใฎ่จญๅฎšใ€[`PreTrainedModel`]ใ€ใŠใ‚ˆใณ[`PretrainedConfig`]ใฎ็›ธไบ’ไฝœ็”จใ‚’็†่งฃใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ +ไพ‹็คบ็š„ใช็›ฎ็š„ใงใ€๐Ÿค— Transformersใซ่ฟฝๅŠ ใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’ใ€ŒBrandNewBertใ€ใจๅ‘ผใณใพใ™ใ€‚ + +ไปฅไธ‹ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผš + + + +ใ”่ฆงใฎใ‚ˆใ†ใซใ€๐Ÿค— Transformersใงใฏ็ถ™ๆ‰ฟใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใŒใ€ๆŠฝ่ฑกๅŒ–ใฎใƒฌใƒ™ใƒซใ‚’ๆœ€ๅฐ้™ใซไฟใฃใฆใ„ใพใ™ใ€‚ +ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใฉใฎใƒขใƒ‡ใƒซใซใ‚‚ใ€ๆŠฝ่ฑกๅŒ–ใฎใƒฌใƒ™ใƒซใŒ2ใคใ‚’่ถ…ใˆใ‚‹ใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +`BrandNewBertModel` ใฏ `BrandNewBertPreTrainedModel` ใ‚’็ถ™ๆ‰ฟใ—ใ€ใ•ใ‚‰ใซ[`PreTrainedModel`]ใ‚’็ถ™ๆ‰ฟใ—ใฆใ„ใพใ™ใ€‚ +ใ“ใ‚Œใ ใ‘ใงใ™ใ€‚ +ไธ€่ˆฌ็š„ใชใƒซใƒผใƒซใจใ—ใฆใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใฏ[`PreTrainedModel`]ใซใฎใฟไพๅญ˜ใ™ใ‚‹ใ‚ˆใ†ใซใ—ใŸใ„ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚ +ใ™ในใฆใฎๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใซ่‡ชๅ‹•็š„ใซๆไพ›ใ•ใ‚Œใ‚‹้‡่ฆใชๆฉŸ่ƒฝใฏใ€[`~PreTrainedModel.from_pretrained`]ใŠใ‚ˆใณ +[`~PreTrainedModel.save_pretrained`]ใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฏใ‚ทใƒชใ‚ขใƒฉใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใจใƒ‡ใ‚ทใƒชใ‚ขใƒฉใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +`BrandNewBertModel.forward`ใชใฉใฎไป–ใฎ้‡่ฆใชๆฉŸ่ƒฝใฏใ€ๆ–ฐใ—ใ„ใ€Œmodeling_brand_new_bert.pyใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใงๅฎŒๅ…จใซๅฎš็พฉใ•ใ‚Œใ‚‹ในใใงใ™ใ€‚ +ๆฌกใซใ€็‰นๅฎšใฎใƒ˜ใƒƒใƒ‰ใƒฌใ‚คใƒคใƒผใ‚’ๆŒใคใƒขใƒ‡ใƒซ๏ผˆใŸใจใˆใฐ `BrandNewBertForMaskedLM` ๏ผ‰ใŒ `BrandNewBertModel` ใ‚’็ถ™ๆ‰ฟใ™ใ‚‹ใฎใงใฏใชใใ€ +ๆŠฝ่ฑกๅŒ–ใฎใƒฌใƒ™ใƒซใ‚’ไฝŽใไฟใคใŸใ‚ใซใ€ใใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใง `BrandNewBertModel` ใ‚’ๅ‘ผใณๅ‡บใ™ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใจใ—ใฆไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใŸใ„ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚ +ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใซใฏๅธธใซ `BrandNewBertConfig` ใจใ„ใ†่จญๅฎšใ‚ฏใƒฉใ‚นใŒๅฟ…่ฆใงใ™ใ€‚ใ“ใฎ่จญๅฎšใฏๅธธใซ[`PreTrainedModel`]ใฎๅฑžๆ€งใจใ—ใฆไฟๅญ˜ใ•ใ‚Œใ€ +ใ—ใŸใŒใฃใฆใ€`BrandNewBertPreTrainedModel`ใ‹ใ‚‰็ถ™ๆ‰ฟใ™ใ‚‹ใ™ในใฆใฎใ‚ฏใƒฉใ‚นใง`config`ๅฑžๆ€งใ‚’ไป‹ใ—ใฆใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ + +```python +model = BrandNewBertModel.from_pretrained("brandy/brand_new_bert") +model.config # model has access to its config +``` + +ใƒขใƒ‡ใƒซใจๅŒๆง˜ใซใ€่จญๅฎšใฏ[`PretrainedConfig`]ใ‹ใ‚‰ๅŸบๆœฌ็š„ใชใ‚ทใƒชใ‚ขใƒซๅŒ–ใŠใ‚ˆใณ้€†ใ‚ทใƒชใ‚ขใƒซๅŒ–ใฎๆฉŸ่ƒฝใ‚’็ถ™ๆ‰ฟใ—ใฆใ„ใพใ™ใ€‚ๆณจๆ„ใ™ในใใฏใ€่จญๅฎšใจใƒขใƒ‡ใƒซใฏๅธธใซ2ใคใฎ็•ฐใชใ‚‹ๅฝขๅผใซใ‚ทใƒชใ‚ขใƒซๅŒ–ใ•ใ‚Œใ‚‹ใ“ใจใงใ™ - ใƒขใƒ‡ใƒซใฏ*pytorch_model.bin*ใƒ•ใ‚กใ‚คใƒซใซใ€่จญๅฎšใฏ*config.json*ใƒ•ใ‚กใ‚คใƒซใซใ‚ทใƒชใ‚ขใƒซๅŒ–ใ•ใ‚Œใพใ™ใ€‚[`~PreTrainedModel.save_pretrained`]ใ‚’ๅ‘ผใณๅ‡บใ™ใจใ€่‡ชๅ‹•็š„ใซ[`~PretrainedConfig.save_pretrained`]ใ‚‚ๅ‘ผใณๅ‡บใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใจ่จญๅฎšใฎไธกๆ–นใŒไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚ + +### Code style + +ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹้š›ใซใฏใ€Transformersใฏๆ„่ฆ‹ใŒใ‚ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใงใ‚ใ‚Šใ€ใ‚ณใƒผใƒ‰ใฎๆ›ธใๆ–นใซ้–ขใ—ใฆใ„ใใคใ‹ใฎ็‹ฌ่‡ชใฎ่€ƒใˆๆ–นใŒใ‚ใ‚Šใพใ™ :-) + +1. ใƒขใƒ‡ใƒซใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฏใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใซๅฎŒๅ…จใซ่จ˜่ฟฐใ•ใ‚Œใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎไป–ใฎใƒขใƒ‡ใƒซใจใฏๅฎŒๅ…จใซ็‹ฌ็ซ‹ใ—ใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไป–ใฎใƒขใƒ‡ใƒซใ‹ใ‚‰ใƒ–ใƒญใƒƒใ‚ฏใ‚’ๅ†ๅˆฉ็”จใ—ใŸใ„ๅ ดๅˆใ€ใ‚ณใƒผใƒ‰ใ‚’ใ‚ณใƒ”ใƒผใ—ใฆใƒˆใƒƒใƒ—ใซ`# Copied from`ใ‚ณใƒกใƒณใƒˆใ‚’ไป˜ใ‘ใฆ่ฒผใ‚Šไป˜ใ‘ใพใ™๏ผˆ่‰ฏใ„ไพ‹ใฏ[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/models/roberta/modeling_roberta.py#L160)ใ€ใ‚ณใƒ”ใƒผใซ้–ขใ™ใ‚‹่ฉณ็ดฐใชใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏ[ใ“ใ“](pr_checks#check-copies)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚ +2. ใ‚ณใƒผใƒ‰ใฏๅฎŒๅ…จใซ็†่งฃๅฏ่ƒฝใงใชใ‘ใ‚Œใฐใชใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏ่จ˜่ฟฐ็š„ใชๅค‰ๆ•ฐๅใ‚’้ธๆŠžใ—ใ€็œ็•ฅๅฝขใ‚’้ฟใ‘ใ‚‹ในใใงใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€`act`ใงใฏใชใ`activation`ใŒๅฅฝใพใ‚Œใพใ™ใ€‚1ๆ–‡ๅญ—ใฎๅค‰ๆ•ฐๅใฏใ€forใƒซใƒผใƒ—ๅ†…ใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงใชใ„้™ใ‚Šใ€ๅผทใ้žๆŽจๅฅจใงใ™ใ€‚ +3. ใ‚ˆใ‚Šไธ€่ˆฌ็š„ใซใ€้ญ”ๆณ•ใฎใ‚ˆใ†ใช็Ÿญใ„ใ‚ณใƒผใƒ‰ใ‚ˆใ‚Šใ‚‚้•ทใใฆๆ˜Ž็คบ็š„ใชใ‚ณใƒผใƒ‰ใ‚’ๅฅฝใฟใพใ™ใ€‚ +4. PyTorchใงใฏ`nn.Sequential`ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ›ใšใซใ€`nn.Module`ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ—ใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’่จ˜่ฟฐใ—ใ€ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ไป–ใฎไบบใŒ็ฐกๅ˜ใซใƒ‡ใƒใƒƒใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใƒ—ใƒชใƒณใƒˆใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚„ใƒ–ใƒฌใƒผใ‚ฏใƒใ‚คใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใฆใƒ‡ใƒใƒƒใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +5. ้–ขๆ•ฐใฎใ‚ทใ‚ฐใƒใƒใƒฃใฏๅž‹ใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ไป˜ใ‘ใ‚‹ในใใงใ™ใ€‚ใใฎไป–ใฎ้ƒจๅˆ†ใซ้–ขใ—ใฆใฏใ€ๅž‹ใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใ‚ˆใ‚Šใ‚‚่‰ฏใ„ๅค‰ๆ•ฐๅใŒ่ชญใฟใ‚„ใ™ใ็†่งฃใ—ใ‚„ใ™ใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +### Overview of tokenizers + +ใพใ ๅฎŒไบ†ใ—ใฆใ„ใพใ›ใ‚“ :-( ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏ่ฟ‘ๆ—ฅไธญใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™๏ผ + +## Step-by-step recipe to add a model to ๐Ÿค— Transformers + +ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๆ–นๆณ•ใฏไบบใใ‚Œใžใ‚Œ็•ฐใชใ‚‹ใŸใ‚ใ€ไป–ใฎใ‚ณใƒณใƒˆใƒชใƒ“ใƒฅใƒผใ‚ฟใƒผใŒ๐Ÿค— Transformersใซใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹้š›ใฎ่ฆ็ด„ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใŒ้žๅธธใซๅฝน็ซ‹ใคๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใฏใ€ไป–ใฎใ‚ณใƒณใƒˆใƒชใƒ“ใƒฅใƒผใ‚ฟใƒผใŒ๐Ÿค— Transformersใซใƒขใƒ‡ใƒซใ‚’ใƒใƒผใƒˆใ™ใ‚‹้š›ใฎใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใƒ–ใƒญใ‚ฐๆŠ•็จฟใฎใƒชใ‚นใƒˆใงใ™ใ€‚ + +1. [GPT2ใƒขใƒ‡ใƒซใฎใƒใƒผใƒ†ใ‚ฃใƒณใ‚ฐ](https://medium.com/huggingface/from-tensorflow-to-pytorch-265f40ef2a28) by [Thomas](https://huggingface.co/thomwolf) +2. [WMT19 MTใƒขใƒ‡ใƒซใฎใƒใƒผใƒ†ใ‚ฃใƒณใ‚ฐ](https://huggingface.co/blog/porting-fsmt) by [Stas](https://huggingface.co/stas) + +็ตŒ้จ“ใ‹ใ‚‰่จ€ใˆใ‚‹ใ“ใจใฏใ€ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹้š›ใซๆœ€ใ‚‚้‡่ฆใชใ“ใจใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + +- ่ปŠ่ผชใฎๅ†็™บๆ˜Žใ‚’ใ—ใชใ„ใงใใ ใ•ใ„๏ผๆ–ฐใ—ใ„๐Ÿค— Transformersใƒขใƒ‡ใƒซใฎใŸใ‚ใซ่ฟฝๅŠ ใ™ใ‚‹ใ‚ณใƒผใƒ‰ใฎใปใจใ‚“ใฉใฏใ™ใงใซ๐Ÿค— Transformersๅ†…ใฎใฉใ“ใ‹ใซๅญ˜ๅœจใ—ใฆใ„ใพใ™ใ€‚้กžไผผใ—ใŸๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใ‚„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซใ€ใ„ใใคใ‹ใฎๆ™‚้–“ใ‚’ใ‹ใ‘ใฆๆŽขใ™ใ“ใจใŒ้‡่ฆใงใ™ใ€‚[grep](https://www.gnu.org/software/grep/)ใจ[rg](https://github.com/BurntSushi/ripgrep)ใฏใ‚ใชใŸใฎๅ‹้”ใงใ™ใ€‚ใƒขใƒ‡ใƒซใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏ1ใคใฎใƒขใƒ‡ใƒซๅฎŸ่ฃ…ใซๅŸบใฅใ„ใฆใ„ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ใƒขใƒ‡ใƒซใฎใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚ณใƒผใƒ‰ใฏๅˆฅใฎๅฎŸ่ฃ…ใซๅŸบใฅใ„ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ไพ‹ใˆใฐใ€FSMTใฎใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚ณใƒผใƒ‰ใฏBARTใซๅŸบใฅใ„ใฆใŠใ‚Šใ€FSMTใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚ณใƒผใƒ‰ใฏXLMใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ +- ใ“ใ‚Œใฏ็ง‘ๅญฆ็š„ใช่ชฒ้กŒใ‚ˆใ‚Šใ‚‚ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฎ่ชฒ้กŒใงใ™ใ€‚ใƒขใƒ‡ใƒซใฎ่ซ–ๆ–‡ใฎ็†่ซ–็š„ใชๅด้ขใ‚’ใ™ในใฆ็†่งฃใ—ใ‚ˆใ†ใจใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚ใ€ๅŠน็އ็š„ใชใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ในใใงใ™ใ€‚ +- ่กŒใ่ฉฐใพใฃใŸๅ ดๅˆใฏๅŠฉใ‘ใ‚’ๆฑ‚ใ‚ใฆใใ ใ•ใ„๏ผใƒขใƒ‡ใƒซใฏ๐Ÿค— Transformersใฎใ‚ณใ‚ขใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใงใ‚ใ‚Šใ€Hugging Faceใงใฏใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใฎๅ„ใ‚นใƒ†ใƒƒใƒ—ใงใŠๆ‰‹ไผใ„ใ™ใ‚‹ใฎใ‚’ๅ–œใ‚“ใงใ„ใพใ™ใ€‚้€ฒ่กŒใŒใชใ„ใ“ใจใซๆฐ—ไป˜ใ„ใŸๅ ดๅˆใฏใ€้€ฒๅฑ•ใ—ใฆใ„ใชใ„ใ“ใจใ‚’ๆฐ—ใซใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +ไปฅไธ‹ใงใฏใ€๐Ÿค— Transformersใซใƒขใƒ‡ใƒซใ‚’ใƒใƒผใƒˆใ™ใ‚‹้š›ใซๆœ€ใ‚‚ๅฝน็ซ‹ใคใจ่€ƒใˆใ‚‰ใ‚Œใ‚‹ไธ€่ˆฌ็š„ใชใƒฌใ‚ทใƒ”ใ‚’ๆไพ›ใ—ใ‚ˆใ†ใจใ—ใฆใ„ใพใ™ใ€‚ + +ๆฌกใฎใƒชใ‚นใƒˆใฏใ€ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใซ่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ใ™ในใฆใฎใ“ใจใฎ่ฆ็ด„ใงใ‚ใ‚Šใ€To-Doใƒชใ‚นใƒˆใจใ—ใฆไฝฟ็”จใงใใพใ™๏ผš + +- โ˜ ๏ผˆใ‚ชใƒ—ใ‚ทใƒงใƒณ๏ผ‰ใƒขใƒ‡ใƒซใฎ็†่ซ–็š„ใชๅด้ขใ‚’็†่งฃใ—ใพใ—ใŸ +- โ˜ ๐Ÿค— Transformersใฎ้–‹็™บ็’ฐๅขƒใ‚’ๆบ–ๅ‚™ใ—ใพใ—ใŸ +- โ˜ ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใฎใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ—ใพใ—ใŸ +- โ˜ `forward()` ใƒ‘ใ‚นใ‚’ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงๆญฃๅธธใซๅฎŸ่กŒใ™ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝœๆˆใ—ใพใ—ใŸ +- โ˜ ใƒขใƒ‡ใƒซใฎ้ชจๆ ผใ‚’๐Ÿค— Transformersใซๆญฃๅธธใซ่ฟฝๅŠ ใ—ใพใ—ใŸ +- โ˜ ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’๐Ÿค— Transformersใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซๆญฃๅธธใซๅค‰ๆ›ใ—ใพใ—ใŸ +- โ˜ ๐Ÿค— TransformersใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ `forward()` ใƒ‘ใ‚นใ‚’ๆญฃๅธธใซๅฎŸ่กŒใ—ใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจๅŒไธ€ใฎๅ‡บๅŠ›ใ‚’ๅพ—ใพใ—ใŸ +- โ˜ ๐Ÿค— Transformersใงใฎใƒขใƒ‡ใƒซใƒ†ใ‚นใƒˆใ‚’ๅฎŒไบ†ใ—ใพใ—ใŸ +- โ˜ ๐Ÿค— Transformersใซใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ๆญฃๅธธใซ่ฟฝๅŠ ใ—ใพใ—ใŸ +- โ˜ ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎ็ตฑๅˆใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ—ใŸ +- โ˜ ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅฎŒๆˆใ•ใ›ใพใ—ใŸ +- โ˜ ใƒขใƒ‡ใƒซใฎใ‚ฆใ‚งใ‚คใƒˆใ‚’Hubใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ—ใŸ +- โ˜ ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’ๆๅ‡บใ—ใพใ—ใŸ +- โ˜ ๏ผˆใ‚ชใƒ—ใ‚ทใƒงใƒณ๏ผ‰ใƒ‡ใƒขใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸ + +ใพใšใ€้€šๅธธใ€`BrandNewBert`ใฎ็†่ซ–็š„ใช็†่งฃใ‚’ๆทฑใ‚ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใŸใ ใ—ใ€ใ‚‚ใ—ใƒขใƒ‡ใƒซใฎ็†่ซ–็š„ใชๅด้ขใ‚’ใ€ŒๅฎŸๅ‹™ไธญใซ็†่งฃใ™ใ‚‹ใ€ๆ–นใŒๅฅฝใพใ—ใ„ๅ ดๅˆใ€`BrandNewBert`ใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใซ็›ดๆŽฅใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใฎใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ“ใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใฏใ€ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฎใ‚นใ‚ญใƒซใŒ็†่ซ–็š„ใชใ‚นใ‚ญใƒซใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ +`BrandNewBert`ใฎ่ซ–ๆ–‡ใ‚’็†่งฃใ™ใ‚‹ใฎใซ่‹ฆๅŠดใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใพใŸใฏ็ง‘ๅญฆ็š„ใช่ซ–ๆ–‡ใ‚’่ชญใ‚€ใ‚ˆใ‚Šใ‚‚ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐใ‚’ๆฅฝใ—ใ‚“ใงใ„ใ‚‹ๅ ดๅˆใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ + +### 1. (Optional) Theoretical aspects of BrandNewBert + +BrandNewBertใฎ่ซ–ๆ–‡ใŒใ‚ใ‚‹ๅ ดๅˆใ€ใใฎ่ชฌๆ˜Žใ‚’่ชญใ‚€ใŸใ‚ใฎๆ™‚้–“ใ‚’ๅ–ใ‚‹ในใใงใ™ใ€‚่ซ–ๆ–‡ใฎไธญใซใฏ็†่งฃใŒ้›ฃใ—ใ„้ƒจๅˆ†ใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใใฎๅ ดๅˆใงใ‚‚ๅฟƒ้…ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚็›ฎๆจ™ใฏ่ซ–ๆ–‡ใฎๆทฑใ„็†่ซ–็š„็†่งฃใ‚’ๅพ—ใ‚‹ใ“ใจใงใฏใชใใ€ +๐Ÿค— Transformersใงใƒขใƒ‡ใƒซใ‚’ๅŠนๆžœ็š„ใซๅ†ๅฎŸ่ฃ…ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชๆƒ…ๅ ฑใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใŸใ ใ—ใ€็†่ซ–็š„ใชๅด้ขใซใ‚ใพใ‚Šๅคšใใฎๆ™‚้–“ใ‚’ใ‹ใ‘ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไปฃใ‚ใ‚Šใซใ€ๅฎŸ่ทต็š„ใชๅด้ขใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ—ใ‚‡ใ†ใ€‚ๅ…ทไฝ“็š„ใซใฏๆฌกใฎ็‚นใงใ™๏ผš + +- *brand_new_bert*ใฏใฉใฎ็จฎ้กžใฎใƒขใƒ‡ใƒซใงใ™ใ‹๏ผŸ BERTใฎใ‚ˆใ†ใชใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎใฟใฎใƒขใƒ‡ใƒซใงใ™ใ‹๏ผŸ GPT2ใฎใ‚ˆใ†ใชใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎใฟใฎใƒขใƒ‡ใƒซใงใ™ใ‹๏ผŸ BARTใฎใ‚ˆใ†ใชใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ-ใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใงใ™ใ‹๏ผŸ + [model_summary](model_summary)ใ‚’ๅ‚็…งใ—ใฆใ€ใ“ใ‚Œใ‚‰ใฎ้•ใ„ใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +- *brand_new_bert*ใฎๅฟœ็”จๅˆ†้‡Žใฏไฝ•ใงใ™ใ‹๏ผŸ ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใงใ™ใ‹๏ผŸ ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใงใ™ใ‹๏ผŸ Seq2Seqใ‚ฟใ‚นใ‚ฏใ€ไพ‹ใˆใฐ่ฆ็ด„ใงใ™ใ‹๏ผŸ +- ใƒขใƒ‡ใƒซใ‚’BERT/GPT-2/BARTใจใฏ็•ฐใชใ‚‹ใ‚‚ใฎใซใ™ใ‚‹ๆ–ฐใ—ใ„ๆฉŸ่ƒฝใฏไฝ•ใงใ™ใ‹๏ผŸ +- ๆ—ขๅญ˜ใฎ[๐Ÿค— Transformersใƒขใƒ‡ใƒซ](https://huggingface.co/transformers/#contents)ใฎไธญใง*brand_new_bert*ใซๆœ€ใ‚‚ไผผใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใฏใฉใ‚Œใงใ™ใ‹๏ผŸ +- ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎ็จฎ้กžใฏไฝ•ใงใ™ใ‹๏ผŸ SentencePieceใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใงใ™ใ‹๏ผŸ WordPieceใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใงใ™ใ‹๏ผŸ BERTใ‚„BARTใงไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅŒใ˜ใงใ™ใ‹๏ผŸ + +ใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎ่‰ฏใ„ๆฆ‚่ฆใ‚’ๅพ—ใŸใจๆ„Ÿใ˜ใŸใ‚‰ใ€Hugging Faceใƒใƒผใƒ ใซ่ณชๅ•ใ‚’้€ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใ“ใ‚Œใซใฏใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€ๆณจๆ„ๅฑคใชใฉใซ้–ขใ™ใ‚‹่ณชๅ•ใŒๅซใพใ‚Œใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +็งใŸใกใฏๅ–œใ‚“ใงใŠๆ‰‹ไผใ„ใ—ใพใ™ใ€‚ + +### 2. Next prepare your environment + +1. ใƒชใƒใ‚ธใƒˆใƒชใฎใƒšใƒผใ‚ธใงใ€ŒForkใ€ใƒœใ‚ฟใƒณใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใ€[ใƒชใƒใ‚ธใƒˆใƒช](https://github.com/huggingface/transformers)ใ‚’ใƒ•ใ‚ฉใƒผใ‚ฏใ—ใพใ™ใ€‚ + ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ณใƒผใƒ‰ใฎใ‚ณใƒ”ใƒผใŒGitHubใƒฆใƒผใ‚ถใƒผใ‚ขใ‚ซใ‚ฆใƒณใƒˆใฎไธ‹ใซไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ + +2. ใƒญใƒผใ‚ซใƒซใƒ‡ใ‚ฃใ‚นใ‚ฏใซใ‚ใ‚‹`transformers`ใƒ•ใ‚ฉใƒผใ‚ฏใ‚’ใ‚ฏใƒญใƒผใƒณใ—ใ€ใƒ™ใƒผใ‚นใƒชใƒใ‚ธใƒˆใƒชใ‚’ใƒชใƒขใƒผใƒˆใจใ—ใฆ่ฟฝๅŠ ใ—ใพใ™๏ผš + +```bash +git clone https://github.com/[your Github handle]/transformers.git +cd transformers +git remote add upstream https://github.com/huggingface/transformers.git +``` + +```bash +python -m venv .env +source .env/bin/activate +pip install -e ".[dev]" +``` + +3. ้–‹็™บ็’ฐๅขƒใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใŸใ‚ใซใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +```bash +python -m venv .env +source .env/bin/activate +pip install -e ".[dev]" +``` + +ใŠไฝฟใ„ใฎOSใซๅฟœใ˜ใฆใ€ใŠใ‚ˆใณTransformersใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใฎไพๅญ˜้–ขไฟ‚ใฎๆ•ฐใŒๅข—ใˆใฆใ„ใ‚‹ใŸใ‚ใ€ใ“ใฎใ‚ณใƒžใƒณใƒ‰ใงใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +ใใฎๅ ดๅˆใฏใ€ไฝœๆฅญใ—ใฆใ„ใ‚‹Deep Learningใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ๏ผˆPyTorchใ€TensorFlowใ€ใŠใ‚ˆใณ/ใพใŸใฏFlax๏ผ‰ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pip install -e ".[quality]" +``` + +ใ“ใ‚Œใฏใปใจใ‚“ใฉใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซใฏๅๅˆ†ใงใ‚ใ‚‹ใฏใšใงใ™ใ€‚ใใฎๅพŒใ€่ฆชใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซๆˆปใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```bash +cd .. +``` + +4. Transformersใซ*brand_new_bert*ใฎPyTorchใƒใƒผใ‚ธใƒงใƒณใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚PyTorchใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ + https://pytorch.org/get-started/locally/ ใฎๆŒ‡็คบใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ + + **ๆณจๆ„:** CUDAใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’CPUใงๅ‹•ไฝœใ•ใ›ใ‚‹ใ“ใจใงๅๅˆ†ใงใ™ใ€‚ + +5. *brand_new_bert*ใ‚’็งปๆคใ™ใ‚‹ใซใฏใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใธใฎใ‚ขใ‚ฏใ‚ปใ‚นใ‚‚ๅฟ…่ฆใงใ™ใ€‚ + +```bash +git clone https://github.com/org_that_created_brand_new_bert_org/brand_new_bert.git +cd brand_new_bert +pip install -e . +``` + + +*brand_new_bert*ใ‚’๐Ÿค— Transformersใซใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใฎ้–‹็™บ็’ฐๅขƒใ‚’่จญๅฎšใ—ใพใ—ใŸใ€‚ + +### 3.-4. Run a pretrained checkpoint using the original repository + +ๆœ€ๅˆใซใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎ*brand_new_bert*ใƒชใƒใ‚ธใƒˆใƒชใงไฝœๆฅญใ—ใพใ™ใ€‚้€šๅธธใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎๅฎŸ่ฃ…ใฏ้žๅธธใซใ€Œ็ ”็ฉถ็š„ใ€ใงใ‚ใ‚Šใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใŒไธ่ถณใ—ใฆใ„ใŸใ‚Šใ€ใ‚ณใƒผใƒ‰ใŒ็†่งฃใ—ใซใใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ“ใ‚ŒใŒ*brand_new_bert*ใ‚’ๅ†ๅฎŸ่ฃ…ใ™ใ‚‹ๅ‹•ๆฉŸใจใชใ‚‹ในใใงใ™ใ€‚Hugging Faceใงใฏใ€ไธป่ฆใช็›ฎๆจ™ใฎ1ใคใŒใ€ๅ‹•ไฝœใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’ๅ–ใ‚Šใ€ใใ‚Œใ‚’ใงใใ‚‹ใ ใ‘**ใ‚ขใ‚ฏใ‚ปใ‚นๅฏ่ƒฝใงใƒฆใƒผใ‚ถใƒผใƒ•ใƒฌใƒณใƒ‰ใƒชใƒผใง็พŽใ—ใ„**ใ‚‚ใฎใซๆ›ธใ็›ดใ™ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใฏใ€๐Ÿค— Transformersใซใƒขใƒ‡ใƒซใ‚’ๅ†ๅฎŸ่ฃ…ใ™ใ‚‹ๆœ€ใ‚‚้‡่ฆใชๅ‹•ๆฉŸใงใ™ - ่ค‡้›‘ใชๆ–ฐใ—ใ„NLPๆŠ€่ก“ใ‚’**่ชฐใซใงใ‚‚**ใ‚ขใ‚ฏใ‚ปใ‚นๅฏ่ƒฝใซใ—ใ‚ˆใ†ใจใ™ใ‚‹่ฉฆใฟใงใ™ใ€‚ + +ใพใšใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใซๅ…ฅใ‚Š่พผใ‚€ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใ‚‹ในใใงใ™ใ€‚ + +ๅ…ฌๅผใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใงๆญฃๅธธใซๅฎŸ่กŒใ™ใ‚‹ใ“ใจใฏใ€้€šๅธธใ€**ๆœ€ใ‚‚ๅ›ฐ้›ฃใช**ใ‚นใƒ†ใƒƒใƒ—ใงใ™ใ€‚ +็งใŸใกใฎ็ตŒ้จ“ใ‹ใ‚‰ใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใซๆ…ฃใ‚Œใ‚‹ใฎใซๆ™‚้–“ใ‚’ใ‹ใ‘ใ‚‹ใ“ใจใŒ้žๅธธใซ้‡่ฆใงใ™ใ€‚ไปฅไธ‹ใฎใ“ใจใ‚’็†่งฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +- ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใฉใ“ใง่ฆ‹ใคใ‘ใ‚‹ใ‹๏ผŸ +- ๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒซใซไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฏ๏ผŸ +- ใƒขใƒ‡ใƒซใ‹ใ‚‰็‹ฌ็ซ‹ใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฏ๏ผŸ +- 1ใคใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’่ฟฝ่ทกใ—ใฆใ€ๅ˜็ด”ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใซๅฟ…่ฆใชใ‚ฏใƒฉใ‚นใจ้–ขๆ•ฐใŒใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚้€šๅธธใ€ใ“ใ‚Œใ‚‰ใฎ้–ขๆ•ฐใ ใ‘ใ‚’ๅ†ๅฎŸ่ฃ…ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ใƒขใƒ‡ใƒซใฎ้‡่ฆใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’็‰นๅฎšใงใใ‚‹ใ“ใจ๏ผšใƒขใƒ‡ใƒซใฎใ‚ฏใƒฉใ‚นใฏใฉใ“ใซใ‚ใ‚Šใพใ™ใ‹๏ผŸใƒขใƒ‡ใƒซใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใ€*ไพ‹* EncoderModelใ€DecoderModelใŒใ‚ใ‚Šใพใ™ใ‹๏ผŸ่‡ชๅทฑๆณจๆ„ใƒฌใ‚คใƒคใƒผใฏใฉใ“ใซใ‚ใ‚Šใพใ™ใ‹๏ผŸ่ค‡ๆ•ฐใฎ็•ฐใชใ‚‹ๆณจๆ„ใƒฌใ‚คใƒคใƒผใ€*ไพ‹* *่‡ชๅทฑๆณจๆ„*ใ€*ใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ*ใชใฉใŒๅญ˜ๅœจใ—ใพใ™ใ‹๏ผŸ +- ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใฎ็’ฐๅขƒใงใƒขใƒ‡ใƒซใ‚’ใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใฏ๏ผŸ*print*ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ€*ipdb*ใฎใ‚ˆใ†ใชๅฏพ่ฉฑๅž‹ใƒ‡ใƒใƒƒใ‚ฌใ‚’ไฝฟ็”จใงใใ‚‹ใ‹ใ€PyCharmใฎใ‚ˆใ†ใชๅŠน็އ็š„ใชIDEใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ‹๏ผŸ + +้‡่ฆใชใฎใฏใ€ใƒใƒผใƒ†ใ‚ฃใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใงใ‚ณใƒผใƒ‰ใ‚’**ๅŠน็އ็š„ใซ**ใƒ‡ใƒใƒƒใ‚ฐใงใใ‚‹ใ“ใจใงใ™๏ผใพใŸใ€ใ“ใ‚Œใฏใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใƒฉใ‚คใƒ–ใƒฉใƒชใงไฝœๆฅญใ—ใฆใ„ใ‚‹ใ“ใจใ‚’่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใงใ‚ณใƒผใƒ‰ใ‚’่ชฟในใ‚‹่ชฐใ‹ใ‚’ๆญ“่ฟŽใ™ใ‚‹ใŸใ‚ใซใ€ๅ•้กŒใ‚’ใ‚ชใƒผใƒ—ใƒณใซใ—ใŸใ‚Šใ€ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้€ไฟกใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใ‚’ใŸใ‚ใ‚‰ใ‚ใชใ„ใงใใ ใ•ใ„ใ€‚ใ“ใฎใƒชใƒใ‚ธใƒˆใƒชใฎใƒกใƒณใƒ†ใƒŠใƒผใฏใ€ๅฝผใ‚‰ใฎใ‚ณใƒผใƒ‰ใ‚’่ชฟในใฆใใ‚Œใ‚‹ไบบใซๅฏพใ—ใฆ้žๅธธใซๅ–œใ‚“ใงใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใงใ™๏ผ + +ใ“ใฎๆฎต้šŽใงใฏใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒขใƒ‡ใƒซใฎใƒ‡ใƒใƒƒใ‚ฐใซใฉใฎใ‚ˆใ†ใช็’ฐๅขƒใจๆˆฆ็•ฅใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใฏใ€ใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ๆœ€ๅˆใซใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใซ้–ขใ™ใ‚‹ใ‚ณใƒผใƒ‰ใ‚’ใƒ‡ใƒใƒƒใ‚ฐใงใใ‚‹ใ“ใจใŒ้žๅธธใซ้‡่ฆใงใ™ใ€‚ใพใŸใ€GPU็’ฐๅขƒใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใฏใŠๅ‹งใ‚ใ—ใพใ›ใ‚“ใ€‚ใพใšใ€CPUไธŠใงไฝœๆฅญใ—ใ€ใƒขใƒ‡ใƒซใŒใ™ใงใซ๐Ÿค— Transformersใซๆญฃๅธธใซใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ๆœ€ๅพŒใซใ€ใƒขใƒ‡ใƒซใŒGPUไธŠใงใ‚‚ๆœŸๅพ…้€šใ‚Šใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’ๆคœ่จผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไธ€่ˆฌ็š„ใซใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒขใƒ‡ใƒซใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎ2ใคใฎใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใŒใ‚ใ‚Šใพใ™๏ผš + +- [Jupyter notebooks](https://jupyter.org/) / [google colab](https://colab.research.google.com/notebooks/intro.ipynb) +- ใƒญใƒผใ‚ซใƒซใชPythonใ‚นใ‚ฏใƒชใƒ—ใƒˆใ€‚ + +JupyterใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏใ€ใ‚ปใƒซใ”ใจใซๅฎŸ่กŒใงใใ‚‹ใŸใ‚ใ€่ซ–็†็š„ใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’ใ‚ˆใ‚Šๅˆ†ๅ‰ฒใ—ใ€ไธญ้–“็ตๆžœใ‚’ไฟๅญ˜ใงใใ‚‹ใŸใ‚ใ€ใƒ‡ใƒใƒƒใ‚ฐใ‚ตใ‚คใ‚ฏใƒซใŒ้€Ÿใใชใ‚‹ใจใ„ใ†ๅˆฉ็‚นใŒใ‚ใ‚Šใพใ™ใ€‚ใพใŸใ€ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏไป–ใฎๅ…ฑๅŒไฝœๆฅญ่€…ใจ็ฐกๅ˜ใซๅ…ฑๆœ‰ใงใใ‚‹ใ“ใจใŒๅคšใใ€Hugging Faceใƒใƒผใƒ ใซๅŠฉใ‘ใ‚’ๆฑ‚ใ‚ใ‚‹ๅ ดๅˆใซ้žๅธธใซๅฝน็ซ‹ใคๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚JupyterใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใซ็ฒพ้€šใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใใ‚Œ + + +```python +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = [0, 4, 5, 2, 3, 7, 9] # vector of input ids +original_output = model.predict(input_ids) +``` + +ใƒ‡ใƒใƒƒใ‚ฐๆˆฆ็•ฅใซใคใ„ใฆใฏใ€้€šๅธธใ€ใ„ใใคใ‹ใฎ้ธๆŠž่‚ขใŒใ‚ใ‚Šใพใ™๏ผš + +- ๅ…ƒใฎใƒขใƒ‡ใƒซใ‚’ๅคšใใฎๅฐใ•ใชใƒ†ใ‚นใƒˆๅฏ่ƒฝใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†่งฃใ—ใ€ใใ‚Œใžใ‚Œใซๅฏพใ—ใฆๅ‰ๆ–นใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ—ใฆๆคœ่จผใ—ใพใ™ +- ๅ…ƒใฎใƒขใƒ‡ใƒซใ‚’ๅ…ƒใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅ…ƒใฎใƒขใƒ‡ใƒซใซใฎใฟๅˆ†่งฃใ—ใ€ใใ‚Œใ‚‰ใซๅฏพใ—ใฆๅ‰ๆ–นใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ—ใ€ๆคœ่จผใฎใŸใ‚ใซไธญ้–“ใฎใƒ—ใƒชใƒณใƒˆใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใพใŸใฏใƒ–ใƒฌใƒผใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ + +ๅ†ๅบฆใ€ใฉใฎๆˆฆ็•ฅใ‚’้ธๆŠžใ™ใ‚‹ใ‹ใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใซไพๅญ˜ใ™ใ‚‹ใ“ใจใŒๅคšใใ€ๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใซๅฟœใ˜ใฆไธ€ๆ–นใพใŸใฏไป–ๆ–นใŒๆœ‰ๅˆฉใชใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใŒใƒขใƒ‡ใƒซใ‚’ๅฐใ•ใชใ‚ตใƒ–ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†่งฃใงใใ‚‹ๅ ดๅˆใ€*ไพ‹ใˆใฐ*ๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใŒ็ฐกๅ˜ใซใ‚คใƒผใ‚ฌใƒผใƒขใƒผใƒ‰ใงๅฎŸ่กŒใงใใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚’่กŒใ†ไพกๅ€คใŒ้€šๅธธใ‚ใ‚Šใพใ™ใ€‚ๆœ€ๅˆใ‹ใ‚‰ใ‚ˆใ‚Š้›ฃใ—ใ„ๆ–นๆณ•ใ‚’้ธๆŠžใ™ใ‚‹ใ“ใจใซใฏใ„ใใคใ‹ใฎ้‡่ฆใชๅˆฉ็‚นใŒใ‚ใ‚Šใพใ™๏ผš + +- ๅพŒใงๅ…ƒใฎใƒขใƒ‡ใƒซใ‚’๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใจๆฏ”่ผƒใ™ใ‚‹้š›ใซใ€ๅ„ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒๅฏพๅฟœใ™ใ‚‹๐Ÿค— TransformersๅฎŸ่ฃ…ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใจไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’่‡ชๅ‹•็š„ใซๆคœ่จผใงใใ‚‹ใŸใ‚ใ€่ฆ–่ฆš็š„ใชๆฏ”่ผƒใซไพๅญ˜ใ›ใšใซๆธˆใฟใพใ™ +- ๅคงใใชๅ•้กŒใ‚’ๅฐใ•ใชๅ•้กŒใซๅˆ†่งฃใ™ใ‚‹ใ€ใคใพใ‚Šๅ€‹ใ€…ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎใฟใ‚’ใƒใƒผใƒ†ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅ•้กŒใซๅˆ†ๅ‰ฒใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใ€ไฝœๆฅญใ‚’ๆง‹้€ ๅŒ–ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ +- ใƒขใƒ‡ใƒซใ‚’่ซ–็†็š„ใชๆ„ๅ‘ณใฎใ‚ใ‚‹ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใงใ€ใƒขใƒ‡ใƒซใฎ่จญ่จˆใ‚’ใ‚ˆใ‚Šใ‚ˆใ็†่งฃใ—ใ‚„ใ™ใใ—ใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Šใ‚ˆใ็†่งฃใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ +- ๅพŒใงใ€ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ”ใจใฎใƒ†ใ‚นใƒˆใ‚’่กŒใ†ใ“ใจใงใ€ใ‚ณใƒผใƒ‰ใ‚’ๅค‰ๆ›ดใ—็ถšใ‘ใ‚‹้š›ใซใƒชใ‚ฐใƒฌใƒƒใ‚ทใƒงใƒณใŒ็™บ็”Ÿใ—ใชใ„ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ + +[Lysandreใฎ](https://gist.github.com/LysandreJik/db4c948f6b4483960de5cbac598ad4ed) ELECTRAใฎ็ตฑๅˆใƒใ‚งใƒƒใ‚ฏใฏใ€ใ“ใ‚ŒใŒใฉใฎใ‚ˆใ†ใซ่กŒใ‚ใ‚Œใ‚‹ใ‹ใฎ่‰ฏใ„ไพ‹ใงใ™ใ€‚ + +ใŸใ ใ—ใ€ๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใŒ้žๅธธใซ่ค‡้›‘ใงใ€ไธญ้–“ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใƒขใƒผใƒ‰ใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ—ใ‹่จฑๅฏใ—ใชใ„ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใ‚’ๅฐใ•ใชใƒ†ใ‚นใƒˆๅฏ่ƒฝใชใ‚ตใƒ–ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†่งฃใ™ใ‚‹ใ“ใจใŒๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใ™ใŽใ‚‹ใ‹ใ€ไธๅฏ่ƒฝใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ +่‰ฏใ„ไพ‹ใฏ[T5ใฎMeshTensorFlow](https://github.com/tensorflow/mesh/tree/master/mesh_tensorflow)ใƒฉใ‚คใƒ–ใƒฉใƒชใงใ‚ใ‚Šใ€้žๅธธใซ่ค‡้›‘ใงใƒขใƒ‡ใƒซใ‚’ใ‚ตใƒ–ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†่งฃใ™ใ‚‹็ฐกๅ˜ใชๆ–นๆณ•ใ‚’ๆไพ›ใ—ใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎใ‚ˆใ†ใชใƒฉใ‚คใƒ–ใƒฉใƒชใงใฏใ€้€šๅธธใ€ใƒ—ใƒชใƒณใƒˆใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’ๆคœ่จผใ™ใ‚‹ใ“ใจใซไพๅญ˜ใ—ใพใ™ใ€‚ + +ใฉใฎๆˆฆ็•ฅใ‚’้ธๆŠžใ—ใฆใ‚‚ใ€ๆŽจๅฅจใ•ใ‚Œใ‚‹ๆ‰‹้ †ใฏ้€šๅธธๅŒใ˜ใงใ€ๆœ€ๅˆใฎใƒฌใ‚คใƒคใƒผใ‹ใ‚‰ใƒ‡ใƒใƒƒใ‚ฐใ‚’้–‹ๅง‹ใ—ใ€ๆœ€ๅพŒใฎใƒฌใ‚คใƒคใƒผใ‹ใ‚‰ใƒ‡ใƒใƒƒใ‚ฐใ‚’่กŒใ†ในใใงใ™ใ€‚ + +้€šๅธธใ€ไปฅไธ‹ใฎ้ †ๅบใงๆฌกใฎใƒฌใ‚คใƒคใƒผใ‹ใ‚‰ใฎๅ‡บๅŠ›ใ‚’ๅ–ๅพ—ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™๏ผš + +1. ใƒขใƒ‡ใƒซใซๆธกใ•ใ‚ŒใŸๅ…ฅๅŠ›IDใ‚’ๅ–ๅพ—ใ™ใ‚‹ +2. ๅ˜่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ๅ–ๅพ—ใ™ใ‚‹ +3. ๆœ€ๅˆใฎTransformerใƒฌใ‚คใƒคใƒผใฎๅ…ฅๅŠ›ใ‚’ๅ–ๅพ—ใ™ใ‚‹ +4. ๆœ€ๅˆใฎTransformerใƒฌใ‚คใƒคใƒผใฎๅ‡บๅŠ›ใ‚’ๅ–ๅพ—ใ™ใ‚‹ +5. ๆฌกใฎn - 1ใคใฎTransformerใƒฌใ‚คใƒคใƒผใฎๅ‡บๅŠ›ใ‚’ๅ–ๅพ—ใ™ใ‚‹ +6. BrandNewBertใƒขใƒ‡ใƒซๅ…จไฝ“ใฎๅ‡บๅŠ›ใ‚’ๅ–ๅพ—ใ™ใ‚‹ + +ๅ…ฅๅŠ›IDใฏๆ•ดๆ•ฐใฎ้…ๅˆ—ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€*ไพ‹๏ผš* `input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19]` ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฎใƒฌใ‚คใƒคใƒผใฎๅ‡บๅŠ›ใฏๅคšๆฌกๅ…ƒใฎๆตฎๅ‹•ๅฐๆ•ฐ็‚น้…ๅˆ—ใงใ‚ใ‚‹ใ“ใจใŒๅคšใใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผš + + +``` +[[ + [-0.1465, -0.6501, 0.1993, ..., 0.1451, 0.3430, 0.6024], + [-0.4417, -0.5920, 0.3450, ..., -0.3062, 0.6182, 0.7132], + [-0.5009, -0.7122, 0.4548, ..., -0.3662, 0.6091, 0.7648], + ..., + [-0.5613, -0.6332, 0.4324, ..., -0.3792, 0.7372, 0.9288], + [-0.5416, -0.6345, 0.4180, ..., -0.3564, 0.6992, 0.9191], + [-0.5334, -0.6403, 0.4271, ..., -0.3339, 0.6533, 0.8694]]], +``` + +๐Ÿค— Transformersใซ่ฟฝๅŠ ใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏใ€็ตฑๅˆใƒ†ใ‚นใƒˆใ‚’ๆ•ฐๅ›žๅˆๆ ผใ™ใ‚‹ใ“ใจใŒๆœŸๅพ…ใ•ใ‚ŒใฆใŠใ‚Šใ€ๅ…ƒใฎใƒขใƒ‡ใƒซใจ๐Ÿค— Transformersใงๅ†ๅฎŸ่ฃ…ใ•ใ‚ŒใŸใƒใƒผใ‚ธใƒงใƒณใŒใ€0.001ใฎ็ฒพๅบฆใพใงใพใฃใŸใๅŒใ˜ๅ‡บๅŠ›ใ‚’ๆไพ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +็•ฐใชใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงๅŒใ˜ใƒขใƒ‡ใƒซใ‚’ๆ›ธใ„ใŸๅ ดๅˆใ€ใ‚ใšใ‹ใซ็•ฐใชใ‚‹ๅ‡บๅŠ›ใ‚’่ฟ”ใ™ใ“ใจใŒๆญฃๅธธใงใ‚ใ‚‹ใŸใ‚ใ€่ชคๅทฎ่จฑๅฎนๅ€คใจใ—ใฆ1e-3๏ผˆ0.001๏ผ‰ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใฆใ„ใพใ™ใ€‚ใƒขใƒ‡ใƒซใŒใปใผๅŒใ˜ๅ‡บๅŠ›ใ‚’่ฟ”ใ™ใ ใ‘ใงใฏไธๅๅˆ†ใงใ€ใปใผๅŒไธ€ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใใฎใŸใ‚ใ€๐Ÿค— Transformersใƒใƒผใ‚ธใƒงใƒณใฎไธญ้–“ๅ‡บๅŠ›ใ‚’ๅ…ƒใฎ*brand_new_bert*ใฎๅฎŸ่ฃ…ใฎไธญ้–“ๅ‡บๅŠ›ใจ่ค‡ๆ•ฐๅ›žใซใ‚ใŸใฃใฆๆฏ”่ผƒใ™ใ‚‹ใ“ใจใซใชใ‚‹ใงใ—ใ‚‡ใ†ใ€‚ใใฎ้š›ใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใฎ**ๅŠน็އ็š„ใช**ใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใŒ้žๅธธใซ้‡่ฆใงใ™ใ€‚ไปฅไธ‹ใฏใ€ใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใ‚’ใงใใ‚‹ใ ใ‘ๅŠน็އ็š„ใซใ™ใ‚‹ใŸใ‚ใฎใ‚ขใƒ‰ใƒใ‚คใ‚นใงใ™ใ€‚ + +- ไธญ้–“็ตๆžœใ‚’ใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹ๆœ€้ฉใชๆ–นๆณ•ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ€‚ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใฏPyTorchใงๆ›ธใ‹ใ‚Œใฆใ„ใพใ™ใ‹๏ผŸใใฎๅ ดๅˆใ€ๅ…ƒใฎใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใ‚ตใƒ–ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๅˆ†่งฃใ—ใฆไธญ้–“ๅ€คใ‚’ๅ–ๅพ—ใ™ใ‚‹้•ทใ„ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๆ›ธใใ“ใจใŒใŠใใ‚‰ใ้ฉๅˆ‡ใงใ™ใ€‚ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใŒTensorflow 1ใงๆ›ธใ‹ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€[tf.print](https://www.tensorflow.org/api_docs/python/tf/print)ใชใฉใฎTensorFlowใฎใƒ—ใƒชใƒณใƒˆๆ“ไฝœใ‚’ไฝฟ็”จใ—ใฆไธญ้–“ๅ€คใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใŒJaxใงๆ›ธใ‹ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎๅฎŸ่กŒๆ™‚ใซใƒขใƒ‡ใƒซใŒ**jittedใ•ใ‚Œใฆใ„ใชใ„**ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ไพ‹๏ผš[ใ“ใฎใƒชใƒณใ‚ฏ](https://github.com/google/jax/issues/196)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ€‚ +- ไฝฟ็”จๅฏ่ƒฝใชๆœ€ๅฐใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒๅฐใ•ใ„ใปใฉใ€ใƒ‡ใƒใƒƒใ‚ฐใ‚ตใ‚คใ‚ฏใƒซใŒ้€Ÿใใชใ‚Šใพใ™ใ€‚ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใŒใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใซ10็ง’ไปฅไธŠใ‹ใ‹ใ‚‹ๅ ดๅˆใ€ๅŠน็އ็š„ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚้žๅธธใซๅคงใใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ—ใ‹ๅˆฉ็”จใงใใชใ„ๅ ดๅˆใ€ๆ–ฐใ—ใ„็’ฐๅขƒใงใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใ‚ฆใ‚งใ‚คใƒˆใ‚’ๆŒใคใƒ€ใƒŸใƒผใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใ€ใใ‚Œใ‚‰ใฎใ‚ฆใ‚งใ‚คใƒˆใ‚’๐Ÿค— Transformersใƒใƒผใ‚ธใƒงใƒณใฎใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ™ใ‚‹ๆ–นใŒ่‰ฏใ„ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +- ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใงใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅ‘ผใณๅ‡บใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚็†ๆƒณ็š„ใซใฏใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใง**ๅ˜ไธ€ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚น**ใ‚’ๅ‘ผใณๅ‡บใ™้–ขๆ•ฐใ‚’่ฆ‹ใคใ‘ใŸใ„ใงใ™ใ€‚ใ“ใ‚Œใฏ้€šๅธธใ€Œpredictใ€ใ€ใ€Œevaluateใ€ใ€ใ€Œforwardใ€ใ€ใ€Œ__call__ใ€ใจๅ‘ผใฐใ‚Œใพใ™ใ€‚่ค‡ๆ•ฐๅ›žใ€Œforwardใ€ใ‚’ๅ‘ผใณๅ‡บใ™้–ขๆ•ฐใ‚’ใƒ‡ใƒใƒƒใ‚ฐใ—ใŸใใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไพ‹๏ผšใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซใ€Œautoregressive_sampleใ€ใ€ใ€Œgenerateใ€ใจๅ‘ผใฐใ‚Œใ‚‹้–ขๆ•ฐใ€‚ +- ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใจใƒขใƒ‡ใƒซใฎใ€Œใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใ€ใƒ‘ใ‚นใ‚’ๅˆ†้›ขใ—ใ‚ˆใ†ใจใ—ใฆใใ ใ•ใ„ใ€‚ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใŒๅ…ฅๅŠ›ๆ–‡ๅญ—ๅˆ—ใ‚’ๅ…ฅๅŠ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ไพ‹ใ‚’็คบใ™ๅ ดๅˆใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใ‚ณใƒผใƒซๅ†…ใงๆ–‡ๅญ—ๅˆ—ๅ…ฅๅŠ›ใŒๅ…ฅๅŠ›IDใซๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅ ดๆ‰€ใ‚’็‰นๅฎšใ—ใ€ใ“ใฎใƒใ‚คใƒณใƒˆใ‹ใ‚‰้–‹ๅง‹ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’่‡ชๅˆ†ใงๆ›ธใใ‹ใ€ๅ…ฅๅŠ›ๆ–‡ๅญ—ๅˆ—ใงใฏใชใๅ…ฅๅŠ›IDใ‚’็›ดๆŽฅๅ…ฅๅŠ›ใงใใ‚‹ใ‚ˆใ†ใซๅ…ƒใฎใ‚ณใƒผใƒ‰ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +- ใƒ‡ใƒใƒƒใ‚ฐใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ๅ†…ใฎใƒขใƒ‡ใƒซใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒขใƒผใƒ‰ใงใฏใชใ„ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒขใƒผใƒ‰ใงใฏใ€ใƒขใƒ‡ใƒซๅ†…ใฎ่ค‡ๆ•ฐใฎใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆใƒฌใ‚คใƒคใƒผใฎใŸใ‚ใซใƒฉใƒณใƒ€ใƒ ใชๅ‡บๅŠ›ใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‡ใƒใƒƒใ‚ฐ็’ฐๅขƒใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใŒ**ๆฑบๅฎš่ซ–็š„**ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€ใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆใƒฌใ‚คใƒคใƒผใŒไฝฟ็”จใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใพใŸใฏใ€ๆ–ฐใ—ใ„ๅฎŸ่ฃ…ใŒๅŒใ˜ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏๅ†…ใซใ‚ใ‚‹ๅ ดๅˆใ€*transformers.utils.set_seed*ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + +ไปฅไธ‹ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€*brand_new_bert*ใซใคใ„ใฆใ“ใ‚Œใ‚’ๅ…ทไฝ“็š„ใซใฉใฎใ‚ˆใ†ใซ่กŒใ†ใ‹ใซใคใ„ใฆใฎ่ฉณ็ดฐ/ใƒ’ใƒณใƒˆใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +### 5.-14. Port BrandNewBert to ๐Ÿค— Transformers + +ๆฌกใซใ€ใคใ„ใซๆ–ฐใ—ใ„ใ‚ณใƒผใƒ‰ใ‚’๐Ÿค— Transformersใซ่ฟฝๅŠ ใงใใพใ™ใ€‚๐Ÿค— Transformersใฎใƒ•ใ‚ฉใƒผใ‚ฏใฎใ‚ฏใƒญใƒผใƒณใซ็งปๅ‹•ใ—ใฆใใ ใ•ใ„๏ผš + +```bash +cd transformers +``` + +็‰นๅˆฅใชใ‚ฑใƒผใ‚นใจใ—ใฆใ€ๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใจๅฎŒๅ…จใซไธ€่‡ดใ™ใ‚‹ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅ ดๅˆใ€ +[ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณ](#write-a-conversion-script)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€ๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™ใ€‚ +ใ“ใฎๅ ดๅˆใ€ๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใฎๅฎŒๅ…จใชใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๅ†ๅˆฉ็”จใงใใพใ™ใ€‚ + + +ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใฎ็”Ÿๆˆใ‚’้–‹ๅง‹ใ—ใพใ—ใ‚‡ใ†ใ€‚ ๆฌกใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€ไปฅไธ‹ใ‹ใ‚‰ๅง‹ใพใ‚‹ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซ: + +```bash +transformers add-new-model-like +``` + +ใƒขใƒ‡ใƒซใฎๅŸบๆœฌๆƒ…ๅ ฑใ‚’ๅ…ฅๅŠ›ใ™ใ‚‹ใŸใ‚ใฎใ‚ขใƒณใ‚ฑใƒผใƒˆใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +**ไธป่ฆใช huggingface/transformers ใƒชใƒใ‚ธใƒˆใƒชใงใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ** + +่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใ‚’้ฉๅฟœใ—ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€๐Ÿค— Transformers ใซใ€Œไฝœๆฅญไธญ๏ผˆWIP๏ผ‰ใ€ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใใ‚ฟใ‚คใƒŸใƒณใ‚ฐใงใ™ใ€‚ +ไพ‹๏ผšใ€Œ[WIP] *brand_new_bert* ใ‚’่ฟฝๅŠ ใ€ใชใฉใงใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒฆใƒผใ‚ถใƒผใจ Hugging Face ใƒใƒผใƒ ใŒ๐Ÿค— Transformers ใซใƒขใƒ‡ใƒซใ‚’็ตฑๅˆใ™ใ‚‹ไฝœๆฅญใ‚’ไธฆ่กŒใ—ใฆ่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ + +ไปฅไธ‹ใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +1. ใƒกใ‚คใƒณใƒ–ใƒฉใƒณใƒใ‹ใ‚‰ๅˆ†ใ‹ใ‚Šใ‚„ใ™ใ„ๅๅ‰ใฎใƒ–ใƒฉใƒณใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```bash +git checkout -b add_brand_new_bert +``` + +2. ่‡ชๅ‹•็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใ‚’ใ‚ณใƒŸใƒƒใƒˆใ—ใฆใใ ใ•ใ„: + +```bash +git add . +git commit +``` + +3. ็พๅœจใฎ main ใƒ–ใƒฉใƒณใƒใซใƒ•ใ‚งใƒƒใƒใ—ใฆใƒชใƒ™ใƒผใ‚น + +```bash +git fetch upstream +git rebase upstream/main +``` + +4. ๅค‰ๆ›ดใ‚’ใ‚ใชใŸใฎใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + +```bash +git push -u origin a-descriptive-name-for-my-changes +``` + +5. ๆบ€่ถณใ—ใŸใ‚‰ใ€GitHubไธŠใฎใƒ•ใ‚ฉใƒผใ‚ฏใฎใ‚ฆใ‚งใƒ–ใƒšใƒผใ‚ธใซ็งปๅ‹•ใ—ใพใ™ใ€‚[ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆ]ใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใพใ™ใ€‚ๅฐ†ๆฅใฎๅค‰ๆ›ดใซๅ‚™ใˆใฆใ€Hugging Face ใƒใƒผใƒ ใฎใƒกใƒณใƒใƒผใฎGitHubใƒใƒณใƒ‰ใƒซใ‚’ใƒฌใƒ“ใƒฅใ‚ขใƒผใจใ—ใฆ่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„ใ€‚ + +6. GitHubใฎใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚ฆใ‚งใƒ–ใƒšใƒผใ‚ธใฎๅณๅดใซใ‚ใ‚‹ใ€Œใƒ‰ใƒฉใƒ•ใƒˆใซๅค‰ๆ›ใ€ใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใ€PRใ‚’ใƒ‰ใƒฉใƒ•ใƒˆใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใงใฏใ€้€ฒๆ—ใŒใ‚ใฃใŸๅ ดๅˆใฏๅธธใซไฝœๆฅญใ‚’ใ‚ณใƒŸใƒƒใƒˆใ—ใ€ใƒ—ใƒƒใ‚ทใƒฅใ—ใฆใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใซ่กจ็คบใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ใ•ใ‚‰ใซใ€ๅฎšๆœŸ็š„ใซใƒกใ‚คใƒณใ‹ใ‚‰ใฎๆœ€ๆ–ฐใฎๅค‰ๆ›ดใ‚’ๅ–ใ‚Š่พผใ‚€ใŸใ‚ใซใ€ๆฌกใฎใ‚ˆใ†ใซ่กŒใ†ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„๏ผš + +```bash +git fetch upstream +git merge upstream/main +``` + +ไธ€่ˆฌ็š„ใซใ€ใƒขใƒ‡ใƒซใ‚„ๅฎŸ่ฃ…ใซ้–ขใ™ใ‚‹่ณชๅ•ใฏPull Request (PR) ใง่กŒใ„ใ€PRๅ†…ใง่ญฐ่ซ–ใ—ใ€่งฃๆฑบใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€Hugging Face ใƒใƒผใƒ ใฏๆ–ฐใ—ใ„ใ‚ณใƒผใƒ‰ใ‚’ใ‚ณใƒŸใƒƒใƒˆใ™ใ‚‹้š›ใ‚„่ณชๅ•ใŒใ‚ใ‚‹ๅ ดๅˆใซๅธธใซ้€š็Ÿฅใ‚’ๅ—ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +่ณชๅ•ใ‚„ๅ•้กŒใŒ่งฃๆฑบใ•ใ‚ŒใŸ้š›ใซใ€ๅ•้กŒใ‚„่ณชๅ•ใŒ็†่งฃใ•ใ‚Œใ‚„ใ™ใ„ใ‚ˆใ†ใซใ€Hugging Face ใƒใƒผใƒ ใซใ‚ณใƒผใƒ‰ใ‚’ๆŒ‡ๆ‘˜ใ™ใ‚‹ใ“ใจใŒ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ“ใฎใŸใ‚ใซใฏใ€ใ€ŒFiles changedใ€ใ‚ฟใƒ–ใซ็งปๅ‹•ใ—ใฆใ™ในใฆใฎๅค‰ๆ›ดใ‚’่กจ็คบใ—ใ€่ณชๅ•ใ—ใŸใ„่กŒใซ็งปๅ‹•ใ—ใฆใ€Œ+ใ€ใ‚ทใƒณใƒœใƒซใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใ‚ณใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +่ณชๅ•ใ‚„ๅ•้กŒใŒ่งฃๆฑบใ•ใ‚ŒใŸๅ ดๅˆใฏใ€ไฝœๆˆใ•ใ‚ŒใŸใ‚ณใƒกใƒณใƒˆใฎใ€ŒResolveใ€ใƒœใ‚ฟใƒณใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใงใใพใ™ใ€‚ + +ๅŒๆง˜ใซใ€Hugging Face ใƒใƒผใƒ ใฏใ‚ณใƒผใƒ‰ใ‚’ใƒฌใƒ“ใƒฅใƒผใ™ใ‚‹้š›ใซใ‚ณใƒกใƒณใƒˆใ‚’้–‹ใใพใ™ใ€‚ +PRไธŠใงใฎใปใจใ‚“ใฉใฎ่ณชๅ•ใฏGitHubไธŠใง่กŒใ†ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ไธ€่ˆฌ็š„ใช่ณชๅ•ใซ้–ขใ—ใฆใฏใ€ๅ…ฌใซใฏใ‚ใพใ‚Šๅฝน็ซ‹ใŸใชใ„่ณชๅ•ใซใคใ„ใฆใฏใ€Slackใ‚„ใƒกใƒผใƒซใงHugging Face ใƒใƒผใƒ ใซ้€ฃ็ตกใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +**5. ็”Ÿๆˆใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚ณใƒผใƒ‰ใ‚’"brand_new_bert"ใซ้ฉๅฟœใ•ใ›ใ‚‹** + +ๆœ€ๅˆใซใ€ใƒขใƒ‡ใƒซ่‡ชไฝ“ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใฏๆฐ—ใซใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ +้–ข้€ฃใ™ใ‚‹ใ‚ณใƒผใƒ‰ใฏใ€็”Ÿๆˆใ•ใ‚ŒใŸใƒ•ใ‚กใ‚คใƒซ`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`ใŠใ‚ˆใณ`src/transformers/models/brand_new_bert/configuration_brand_new_bert.py`ใง่ฆ‹ใคใ‹ใ‚‹ใฏใšใงใ™ใ€‚ + +ใ•ใฆใ€ใคใ„ใซใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅง‹ใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ :smile:ใ€‚ +`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`ใซใ‚ใ‚‹็”Ÿๆˆใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใฏใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎใฟใฎใƒขใƒ‡ใƒซใงใ‚ใ‚ŒใฐBERTใจๅŒใ˜ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆŒใฃใฆใ„ใ‚‹ใ‹ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ-ใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใงใ‚ใ‚ŒใฐBARTใจๅŒใ˜ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆŒใฃใฆใ„ใ‚‹ใฏใšใงใ™ใ€‚ +ใ“ใฎๆฎต้šŽใงใฏใ€ใƒขใƒ‡ใƒซใฎ็†่ซ–็š„ใชๅด้ขใซใคใ„ใฆๅญฆใ‚“ใ ใ“ใจใ‚’ๆ€ใ„ๅ‡บใ™ในใใงใ™ใ€‚ใคใพใ‚Šใ€ใ€Œใ“ใฎใƒขใƒ‡ใƒซใฏBERTใพใŸใฏBARTใจใฉใฎใ‚ˆใ†ใซ็•ฐใชใ‚‹ใฎใ‹๏ผŸใ€ใจใ„ใ†ใ“ใจใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎๅค‰ๆ›ดใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใŒใ€ใ“ใ‚Œใฏ้€šๅธธใ€ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒฌใ‚คใƒคใƒผใ€ๆญฃ่ฆๅŒ–ใƒฌใ‚คใƒคใƒผใฎ้ †ๅบใชใฉใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ๅ†ใณใ€ใ‚ใชใŸใฎใƒขใƒ‡ใƒซใŒใฉใฎใ‚ˆใ†ใซๅฎŸ่ฃ…ใ•ใ‚Œใ‚‹ในใใ‹ใ‚’ใ‚ˆใ‚Š่‰ฏใ็†่งฃใ™ใ‚‹ใŸใ‚ใซใ€Transformersๅ†…ใซๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใฎ้กžไผผใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’่ฆ‹ใ‚‹ใ“ใจใŒๅฝน็ซ‹ใคใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎๆ™‚็‚นใงใฏใ€ใ‚ณใƒผใƒ‰ใŒๅฎŒๅ…จใซๆญฃ็ขบใพใŸใฏใ‚ฏใƒชใƒผใƒณใงใ‚ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ‚€ใ—ใ‚ใ€ใพใšใฏๅฟ…่ฆใชใ‚ณใƒผใƒ‰ใฎๆœ€ๅˆใฎ*ใ‚ฏใƒชใƒผใƒณใงใชใ„*ใ‚ณใƒ”ใƒผ๏ผ†ใƒšใƒผใ‚นใƒˆใƒใƒผใ‚ธใƒงใƒณใ‚’ +`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`ใซ่ฟฝๅŠ ใ—ใ€ๅฟ…่ฆใชใ‚ณใƒผใƒ‰ใŒใ™ในใฆ่ฟฝๅŠ ใ•ใ‚Œใฆใ„ใ‚‹ใจๆ„Ÿใ˜ใ‚‹ใพใงๆ”นๅ–„/ไฟฎๆญฃใ‚’ๅๅพฉ็š„ใซ่กŒใ†ใ“ใจใŒใŠๅ‹งใ‚ใงใ™ใ€‚ +็งใŸใกใฎ็ตŒ้จ“ใ‹ใ‚‰ใ€ๅฟ…่ฆใชใ‚ณใƒผใƒ‰ใฎๆœ€ๅˆใฎใƒใƒผใ‚ธใƒงใƒณใ‚’่ฟ…้€Ÿใซ่ฟฝๅŠ ใ—ใ€ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ชฌๆ˜Žใ™ใ‚‹ๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ‚ณใƒผใƒ‰ใ‚’็นฐใ‚Š่ฟ”ใ—ๆ”นๅ–„/ไฟฎๆญฃใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ‚ใ‚‹ใ“ใจใŒๅคšใ„ใงใ™ใ€‚ +ใ“ใฎๆ™‚็‚นใงๅ‹•ไฝœใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใฎใฏใ€๐Ÿค— Transformersใฎ"brand_new_bert"ใฎๅฎŸ่ฃ…ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใงใใ‚‹ใ“ใจใ ใ‘ใงใ™ใ€‚ใคใพใ‚Šใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใŒๆฉŸ่ƒฝใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```python +from transformers import BrandNewBertModel, BrandNewBertConfig + +model = BrandNewBertModel(BrandNewBertConfig()) +``` + +ไธŠ่จ˜ใฎใ‚ณใƒžใƒณใƒ‰ใฏใ€`BrandNewBertConfig()` ใงๅฎš็พฉใ•ใ‚ŒใŸใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใซๅพ“ใฃใฆใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใ€ +ใ™ในใฆใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎ `init()` ใƒกใ‚ฝใƒƒใƒ‰ใŒๆญฃๅธธใซๅ‹•ไฝœใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + +ใ™ในใฆใฎใƒฉใƒณใƒ€ใƒ ใชๅˆๆœŸๅŒ–ใฏใ€`BrandnewBertPreTrainedModel` ใ‚ฏใƒฉใ‚นใฎ `_init_weights` ใƒกใ‚ฝใƒƒใƒ‰ใง่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€่จญๅฎšๅค‰ๆ•ฐใซไพๅญ˜ใ™ใ‚‹ใ™ในใฆใฎใƒชใƒผใƒ•ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใฏใ€BERT ใฎ `_init_weights` ใƒกใ‚ฝใƒƒใƒ‰ใฎไพ‹ใงใ™๏ผš + +```py +def _init_weights(self, module): + """Initialize the weights""" + if isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.bias is not None: + module.bias.data.zero_() + elif isinstance(module, nn.Embedding): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.padding_idx is not None: + module.weight.data[module.padding_idx].zero_() + elif isinstance(module, nn.LayerNorm): + module.bias.data.zero_() + module.weight.data.fill_(1.0) +``` + +็‰นๅฎšใฎใƒขใ‚ธใƒฅใƒผใƒซใซ็‰นๅˆฅใชๅˆๆœŸๅŒ–ใŒๅฟ…่ฆใชๅ ดๅˆใ€ใ‚ซใ‚นใ‚ฟใƒ ใ‚นใ‚ญใƒผใƒ ใ‚’ใ•ใ‚‰ใซๆŒใคใ“ใจใŒใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ +`Wav2Vec2ForPreTraining`ใงใฏใ€ๆœ€ๅพŒใฎ2ใคใฎ็ทšๅฝขๅฑคใซใฏ้€šๅธธใฎPyTorchใฎ`nn.Linear`ใฎๅˆๆœŸๅŒ–ใŒๅฟ…่ฆใงใ™ใŒใ€ +ไป–ใฎใ™ในใฆใฎๅฑคใฏไธŠ่จ˜ใฎใ‚ˆใ†ใชๅˆๆœŸๅŒ–ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏไปฅไธ‹ใฎใ‚ˆใ†ใซใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™๏ผš + +```py +def _init_weights(self, module): + """Initialize the weights""" + if isinstance(module, Wav2Vec2ForPreTraining): + module.project_hid.reset_parameters() + module.project_q.reset_parameters() + module.project_hid._is_hf_initialized = True + module.project_q._is_hf_initialized = True + elif isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.bias is not None: + module.bias.data.zero_() +``` + +`_is_hf_initialized`ใƒ•ใƒฉใ‚ฐใฏใ€ใ‚ตใƒ–ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไธ€ๅบฆใ ใ‘ๅˆๆœŸๅŒ–ใ™ใ‚‹ใ“ใจใ‚’็ขบๅฎŸใซใ™ใ‚‹ใŸใ‚ใซๅ†…้ƒจใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +`module.project_q`ใจ`module.project_hid`ใฎใŸใ‚ใซใใ‚Œใ‚’`True`ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใงใ€ +ใ‚ซใ‚นใ‚ฟใƒ ๅˆๆœŸๅŒ–ใŒๅพŒใงไธŠๆ›ธใใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใ€`_init_weights`้–ขๆ•ฐใŒใใ‚Œใ‚‰ใซ้ฉ็”จใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +**6. ๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๆ›ธใ** + +ๆฌกใซใ€*brand_new_bert* ใฎๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใงใƒ‡ใƒใƒƒใ‚ฐใซไฝฟ็”จใ—ใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ€ๆ–ฐใ—ใไฝœๆˆใ—ใŸ ๐Ÿค— Transformers ๅฎŸ่ฃ…ใฎ *brand_new_bert* ใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซๅค‰ๆ›ใงใใ‚‹ๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๆ›ธใๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ใ‚ผใƒญใ‹ใ‚‰ๆ›ธใใ“ใจใฏใŠๅ‹งใ‚ใ•ใ‚Œใพใ›ใ‚“ใŒใ€ไปฃใ‚ใ‚Šใซ ๐Ÿค— Transformers ใงๆ—ขใซๅญ˜ๅœจใ™ใ‚‹้กžไผผใฎใƒขใƒ‡ใƒซใ‚’ๅŒใ˜ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงๅค‰ๆ›ใ—ใŸใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’่ชฟในใ‚‹ใ“ใจใŒ่‰ฏใ„ใงใ—ใ‚‡ใ†ใ€‚ +้€šๅธธใ€ๆ—ขๅญ˜ใฎๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ใ‚ณใƒ”ใƒผใ—ใฆใ€่‡ชๅˆ†ใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซใ‚ใšใ‹ใซ้ฉๅฟœใ•ใ›ใ‚‹ใ“ใจใงๅๅˆ†ใงใ™ใ€‚ +Hugging Face ใƒใƒผใƒ ใซๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใซ้กžไผผใ—ใŸๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๆ•™ใˆใฆใ‚‚ใ‚‰ใ†ใ“ใจใ‚‚่บŠ่บ‡ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +- TensorFlowใ‹ใ‚‰PyTorchใซใƒขใƒ‡ใƒซใ‚’็งปๆคใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€่‰ฏใ„ๅ‡บ็™บ็‚นใฏBERTใฎๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ [here](https://github.com/huggingface/transformers/blob/7acfa95afb8194f8f9c1f4d2c6028224dbed35a2/src/transformers/models/bert/modeling_bert.py#L91) +- PyTorchใ‹ใ‚‰PyTorchใซใƒขใƒ‡ใƒซใ‚’็งปๆคใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€่‰ฏใ„ๅ‡บ็™บ็‚นใฏBARTใฎๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ [here](https://github.com/huggingface/transformers/blob/main/src/transformers/models/bart/convert_bart_original_pytorch_checkpoint_to_pytorch.py) + +ไปฅไธ‹ใงใฏใ€PyTorchใƒขใƒ‡ใƒซใŒๅฑคใฎ้‡ใฟใ‚’ใฉใฎใ‚ˆใ†ใซไฟๅญ˜ใ—ใ€ๅฑคใฎๅๅ‰ใ‚’ๅฎš็พฉใ™ใ‚‹ใ‹ใซใคใ„ใฆ็ฐกๅ˜ใซ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +PyTorchใงใฏใ€ๅฑคใฎๅๅ‰ใฏๅฑคใซไธŽใˆใ‚‹ใ‚ฏใƒฉใ‚นๅฑžๆ€งใฎๅๅ‰ใซใ‚ˆใฃใฆๅฎš็พฉใ•ใ‚Œใพใ™ใ€‚ +PyTorchใง `SimpleModel` ใจใ„ใ†ใƒ€ใƒŸใƒผใƒขใƒ‡ใƒซใ‚’ๅฎš็พฉใ—ใพใ—ใ‚‡ใ†๏ผš + +```python +from torch import nn + + +class SimpleModel(nn.Module): + def __init__(self): + super().__init__() + self.dense = nn.Linear(10, 10) + self.intermediate = nn.Linear(10, 10) + self.layer_norm = nn.LayerNorm(10) +``` + +ใ“ใ‚Œใงใ€ใ“ใฎใƒขใƒ‡ใƒซๅฎš็พฉใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใ€`dense`ใ€`intermediate`ใ€`layer_norm`ใฎใ™ในใฆใฎ้‡ใฟใ‚’ใƒฉใƒณใƒ€ใƒ ใช้‡ใฟใงๅŸ‹ใ‚ใŸใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใงใใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใ‚’ๅฐๅˆทใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +model = SimpleModel() + +print(model) +``` +ใ“ใ‚Œใฏไปฅไธ‹ใ‚’ๅ‡บๅŠ›ใ—ใพใ™๏ผš + +``` +SimpleModel( + (dense): Linear(in_features=10, out_features=10, bias=True) + (intermediate): Linear(in_features=10, out_features=10, bias=True) + (layer_norm): LayerNorm((10,), eps=1e-05, elementwise_affine=True) +) +``` + +ๅฑคใฎๅๅ‰ใฏPyTorchใฎใ‚ฏใƒฉใ‚นๅฑžๆ€งใฎๅๅ‰ใซใ‚ˆใฃใฆๅฎš็พฉใ•ใ‚Œใฆใ„ใพใ™ใ€‚็‰นๅฎšใฎๅฑคใฎ้‡ใฟๅ€คใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + + +```python +print(model.dense.weight.data) +``` + +ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซ + +``` +tensor([[-0.0818, 0.2207, -0.0749, -0.0030, 0.0045, -0.1569, -0.1598, 0.0212, + -0.2077, 0.2157], + [ 0.1044, 0.0201, 0.0990, 0.2482, 0.3116, 0.2509, 0.2866, -0.2190, + 0.2166, -0.0212], + [-0.2000, 0.1107, -0.1999, -0.3119, 0.1559, 0.0993, 0.1776, -0.1950, + -0.1023, -0.0447], + [-0.0888, -0.1092, 0.2281, 0.0336, 0.1817, -0.0115, 0.2096, 0.1415, + -0.1876, -0.2467], + [ 0.2208, -0.2352, -0.1426, -0.2636, -0.2889, -0.2061, -0.2849, -0.0465, + 0.2577, 0.0402], + [ 0.1502, 0.2465, 0.2566, 0.0693, 0.2352, -0.0530, 0.1859, -0.0604, + 0.2132, 0.1680], + [ 0.1733, -0.2407, -0.1721, 0.1484, 0.0358, -0.0633, -0.0721, -0.0090, + 0.2707, -0.2509], + [-0.1173, 0.1561, 0.2945, 0.0595, -0.1996, 0.2988, -0.0802, 0.0407, + 0.1829, -0.1568], + [-0.1164, -0.2228, -0.0403, 0.0428, 0.1339, 0.0047, 0.1967, 0.2923, + 0.0333, -0.0536], + [-0.1492, -0.1616, 0.1057, 0.1950, -0.2807, -0.2710, -0.1586, 0.0739, + 0.2220, 0.2358]]). +``` + +ใ‚นใ‚ฏใƒชใƒ—ใƒˆๅ†…ใฎๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใงใฏใ€ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’ใ€ๅฏพๅฟœใ™ใ‚‹ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆๅ†…ใฎๆญฃ็ขบใช้‡ใฟใงๅŸ‹ใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซ็ฟป่จณใ—ใพใ™๏ผš + + +```python +# retrieve matching layer weights, e.g. by +# recursive algorithm +layer_name = "dense" +pretrained_weight = array_of_dense_layer + +model_pointer = getattr(model, "dense") + +model_pointer.weight.data = torch.from_numpy(pretrained_weight) +``` + +PyTorchใƒขใƒ‡ใƒซใฎๅ„ใƒฉใƒณใƒ€ใƒ ๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใจๅฏพๅฟœใ™ใ‚‹ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใŒ +**ๅฝข็Šถใจๅๅ‰ใฎไธกๆ–น**ใงๆญฃ็ขบใซไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใ‚’่กŒใ†ใŸใ‚ใซใ€ๅฝข็Šถใซๅฏพใ™ใ‚‹assertใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใฎๅๅ‰ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ“ใจใŒ +**ๅฟ…่ฆไธๅฏๆฌ **ใงใ™ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎใ‚ˆใ†ใชใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + + +```python +assert ( + model_pointer.weight.shape == pretrained_weight.shape +), f"Pointer shape of random weight {model_pointer.shape} and array shape of checkpoint weight {pretrained_weight.shape} mismatched" +``` + +ใพใŸใ€ไธกๆ–นใฎ้‡ใฟใฎๅๅ‰ใ‚’ๅฐๅˆทใ—ใฆใ€ไธ€่‡ดใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + +```python +logger.info(f"Initialize PyTorch weight {layer_name} from {pretrained_weight.name}") +``` + +ใ‚‚ใ—ๅฝข็ŠถใพใŸใฏๅๅ‰ใฎใ„ใšใ‚Œใ‹ใŒไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ€ใŠใใ‚‰ใ่ชคใฃใฆ๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใƒฌใ‚คใƒคใƒผใซ้–“้•ใฃใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใฆใ—ใพใฃใŸๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +่ชคใฃใŸๅฝข็Šถใฏใ€ใŠใใ‚‰ใ`BrandNewBertConfig()`ใงใฎ่จญๅฎšใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒใ€ๅค‰ๆ›ใ—ใŸใ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงไฝฟ็”จใ•ใ‚ŒใŸใ‚‚ใฎใจๆญฃ็ขบใซไธ€่‡ดใ—ใชใ„ใŸใ‚ใงใ™ใ€‚ +ใŸใ ใ—ใ€PyTorchใฎใƒฌใ‚คใƒคใƒผใฎๅฎŸ่ฃ…ใซใ‚ˆใฃใฆใฏใ€้‡ใฟใ‚’ไบ‹ๅ‰ใซ่ปข็ฝฎใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€ๅพŒใซใ€**ใ™ในใฆ**ใฎๅฟ…่ฆใช้‡ใฟใŒๅˆๆœŸๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€ๅˆๆœŸๅŒ–ใซไฝฟ็”จใ•ใ‚Œใชใ‹ใฃใŸใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใ‚’่กจ็คบใ—ใฆใ€ใƒขใƒ‡ใƒซใŒๆญฃใ—ใๅค‰ๆ›ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ๅค‰ๆ›ใƒˆใƒฉใ‚คใ‚ขใƒซใŒ่ชคใฃใŸๅฝข็Šถใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใพใŸใฏ่ชคใฃใŸๅๅ‰ๅ‰ฒใ‚Šๅฝ“ใฆใงๅคฑๆ•—ใ™ใ‚‹ใฎใฏๅฎŒๅ…จใซๆญฃๅธธใงใ™ใ€‚ +ใ“ใ‚ŒใฏใŠใใ‚‰ใใ€`BrandNewBertConfig()`ใง่ชคใฃใŸใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ไฝฟ็”จใ—ใŸใ‹ใ€๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใซ่ชคใฃใŸใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒใ‚ใ‚‹ใ‹ใ€๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใฎ1ใคใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎ`init()`้–ขๆ•ฐใซใƒใ‚ฐใŒใ‚ใ‚‹ใ‹ใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใฎ1ใคใ‚’่ปข็ฝฎใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ใ“ใฎใ‚นใƒ†ใƒƒใƒ—ใฏใ€ไปฅๅ‰ใฎใ‚นใƒ†ใƒƒใƒ—ใจ็นฐใ‚Š่ฟ”ใ™ในใใงใ™ใ€‚ใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ้‡ใฟใŒๆญฃใ—ใ๐Ÿค— Transformersใƒขใƒ‡ใƒซใซ่ชญใฟ่พผใพใ‚Œใ‚‹ใพใง็นฐใ‚Š่ฟ”ใ™ในใใงใ™ใ€‚ +๐Ÿค— TransformersๅฎŸ่ฃ…ใซๆญฃใ—ใใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’่ชญใฟ่พผใ‚“ใ ๅพŒใ€้ธๆŠžใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ใƒผใซใƒขใƒ‡ใƒซใ‚’ไฟๅญ˜ใงใใพใ™ `/path/to/converted/checkpoint/folder`ใ€‚ใ“ใฎใƒ•ใ‚ฉใƒซใƒ€ใซใฏ`pytorch_model.bin`ใƒ•ใ‚กใ‚คใƒซใจ`config.json`ใƒ•ใ‚กใ‚คใƒซใฎไธกๆ–นใŒๅซใพใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ + + +```python +model.save_pretrained("/path/to/converted/checkpoint/folder") +``` + +**7. ้ †ไผๆ’ญ๏ผˆforward pass๏ผ‰ใฎๅฎŸ่ฃ…** + +๐Ÿค— TransformersๅฎŸ่ฃ…ใงไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ๆญฃใ—ใ่ชญใฟ่พผใ‚“ใ ๅพŒใ€้ †ไผๆ’ญใŒๆญฃใ—ใๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚[ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใ‚’็†่งฃใ™ใ‚‹](#3-4-run-a-pretrained-checkpoint-using-the-original-repository)ใงใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎ้ †ไผๆ’ญใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ใ™ใงใซไฝœๆˆใ—ใพใ—ใŸใ€‚ไปŠๅบฆใฏใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใฎไปฃใ‚ใ‚Šใซ๐Ÿค— TransformersๅฎŸ่ฃ…ใ‚’ไฝฟ็”จใ—ใฆ้กžไผผใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + +```python +model = BrandNewBertModel.from_pretrained("/path/to/converted/checkpoint/folder") +input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19] +output = model(input_ids).last_hidden_states +``` + +๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใจๅ…ƒใฎใƒขใƒ‡ใƒซใฎๅฎŸ่ฃ…ใŒๆœ€ๅˆใฎๅฎŸ่กŒใงๅฎŒๅ…จใซๅŒใ˜ๅ‡บๅŠ›ใ‚’ๆไพ›ใ—ใชใ„ใ‹ใ€ +ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใงใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒ้žๅธธใซ้ซ˜ใ„ใงใ™ใ€‚ๅคฑๆœ›ใ—ใชใ„ใงใใ ใ•ใ„ - ใ“ใ‚Œใฏไบˆๆƒณใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใงใ™๏ผ +ใพใšใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใŒใ‚จใƒฉใƒผใ‚’ใ‚นใƒญใƒผใ—ใชใ„ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +้–“้•ใฃใŸๆฌกๅ…ƒใŒไฝฟ็”จใ•ใ‚Œใ€*ๆฌกๅ…ƒใฎไธไธ€่‡ด*ใ‚จใƒฉใƒผใ‚„ใ€่ชคใฃใŸใƒ‡ใƒผใ‚ฟๅž‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒไฝฟ็”จใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ +ไพ‹ใˆใฐใ€`torch.long`ใงใฏใชใ`torch.float32`ใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚็‰นๅฎšใฎใ‚จใƒฉใƒผใ‚’่งฃๆฑบใงใใชใ„ๅ ดๅˆใฏใ€ +Hugging Faceใƒใƒผใƒ ใซๅŠฉใ‘ใ‚’ๆฑ‚ใ‚ใ‚‹ใ“ใจใ‚’่บŠ่บ‡ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +๐Ÿค— TransformersๅฎŸ่ฃ…ใŒๆญฃใ—ใๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆœ€็ต‚็š„ใช้ƒจๅˆ†ใฏใ€ๅ‡บๅŠ›ใŒ`1e-3`ใฎ็ฒพๅบฆใงๅŒ็ญ‰ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใพใšใ€ๅ‡บๅŠ›ใฎๅฝข็ŠถใŒๅŒไธ€ใงใ‚ใ‚‹ใ“ใจใ€ใคใพใ‚Šใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎ๐Ÿค— TransformersๅฎŸ่ฃ…ใจๅ…ƒใฎๅฎŸ่ฃ…ใฎไธกๆ–นใง`outputs.shape`ใŒๅŒใ˜ๅ€คใ‚’็”Ÿๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๆฌกใซใ€ๅ‡บๅŠ›ๅ€คใŒๅŒไธ€ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใฏๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹้š›ใฎๆœ€ใ‚‚้›ฃใ—ใ„้ƒจๅˆ†ใฎ1ใคใงใ™ใ€‚ +ๅ‡บๅŠ›ใŒๅŒไธ€ใงใชใ„็†็”ฑใฎไธ€่ˆฌ็š„ใช้–“้•ใ„ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™ใ€‚ + +- ไธ€้ƒจใฎใƒฌใ‚คใƒคใƒผใŒ่ฟฝๅŠ ใ•ใ‚Œใฆใ„ใชใ„ใ€ใคใพใ‚Š*ๆดปๆ€งๅŒ–*ใƒฌใ‚คใƒคใƒผใŒ่ฟฝๅŠ ใ•ใ‚Œใฆใ„ใชใ„ใ‹ใ€ใƒชใ‚ถใƒใƒซๆŽฅ็ถšใŒๅฟ˜ใ‚Œใ‚‰ใ‚Œใฆใ„ใ‚‹ +- ๅ˜่ชžๅŸ‹ใ‚่พผใฟ่กŒๅˆ—ใŒ็ตใฐใ‚Œใฆใ„ใชใ„ +- ใ‚ชใƒชใ‚ธใƒŠใƒซใฎๅฎŸ่ฃ…ใŒใ‚ชใƒ•ใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใŸใ‚ใ€่ชคใฃใŸไฝ็ฝฎๅŸ‹ใ‚่พผใฟใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ +- ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นไธญใซใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆใŒ้ฉ็”จใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚’ไฟฎๆญฃใ™ใ‚‹ใซใฏใ€*model.trainingใŒFalse*ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นไธญใซ่ชคใฃใฆใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆใƒฌใ‚คใƒคใƒผใŒใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +*ใคใพใ‚Š* [PyTorchใฎfunctional dropout](https://pytorch.org/docs/stable/nn.functional.html?highlight=dropout#torch.nn.functional.dropout)ใซ*model.training*ใ‚’ๆธกใ—ใพใ™ใ€‚ + +ๅ•้กŒใ‚’ไฟฎๆญฃใ™ใ‚‹ๆœ€่‰ฏใฎๆ–นๆณ•ใฏใ€้€šๅธธใ€ๅ…ƒใฎๅฎŸ่ฃ…ใจ๐Ÿค— TransformersๅฎŸ่ฃ…ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ไธฆในใฆ่กจ็คบใ—ใ€้•ใ„ใŒใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +็†ๆƒณ็š„ใซใฏใ€ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎไธกๆ–นใฎๅฎŸ่ฃ…ใฎไธญ้–“ๅ‡บๅŠ›ใ‚’ใƒ‡ใƒใƒƒใ‚ฐ/ใƒ—ใƒชใƒณใƒˆใ‚ขใ‚ฆใƒˆใ—ใฆใ€๐Ÿค— TransformersๅฎŸ่ฃ…ใŒๅ…ƒใฎๅฎŸ่ฃ…ใจ็•ฐใชใ‚‹ๅ‡บๅŠ›ใ‚’็คบใ™ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏๅ†…ใฎๆญฃ็ขบใชไฝ็ฝฎใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ๆœ€ๅˆใซใ€ไธกๆ–นใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎใƒใƒผใƒ‰ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚ŒใŸ`input_ids`ใŒๅŒไธ€ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +ๆฌกใซใ€`input_ids`ใฎๆœ€ๅˆใฎๅค‰ๆ›๏ผˆ้€šๅธธใ€ๅ˜่ชžๅŸ‹ใ‚่พผใฟ๏ผ‰ใฎๅ‡บๅŠ›ใŒๅŒไธ€ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +ใใฎๅพŒใ€ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎๆœ€ๅพŒใฎใƒฌใ‚คใƒคใƒผใพใงไฝœๆฅญใ‚’้€ฒใ‚ใพใ™ใ€‚ +ใ„ใšใ‚Œใ‹ใฎๆ™‚็‚นใงใ€2ใคใฎๅฎŸ่ฃ…้–“ใง้•ใ„ใŒใ‚ใ‚‹ใ“ใจใซๆฐ—ไป˜ใใฏใšใงใ€ใใ‚Œใซใ‚ˆใ‚Š๐Ÿค— TransformersๅฎŸ่ฃ…ใฎใƒใ‚ฐใฎๅ ดๆ‰€ใŒ็‰นๅฎšใ•ใ‚Œใพใ™ใ€‚ +็ตŒ้จ“ไธŠใ€ๅ…ƒใฎๅฎŸ่ฃ…ใจ๐Ÿค— TransformersๅฎŸ่ฃ…ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎๅŒใ˜ไฝ็ฝฎใซๅคšใใฎใƒ—ใƒชใƒณใƒˆใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใ€ +ไธญ้–“ใƒ—ใƒฌใ‚ผใƒณใƒ†ใƒผใ‚ทใƒงใƒณใงๅŒใ˜ๅ€คใ‚’็คบใ™ใƒ—ใƒชใƒณใƒˆใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’ๆฎต้šŽ็š„ใซๅ‰Š้™คใ™ใ‚‹ใฎใŒใ‚ทใƒณใƒ—ใƒซใ‹ใคๅŠนๆžœ็š„ใชๆ–นๆณ•ใงใ™ใ€‚ + +ไธกๆ–นใฎๅฎŸ่ฃ…ใŒๅŒใ˜ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ใ“ใจใซ่‡ชไฟกใ‚’ๆŒใฃใฆใ„ใ‚‹ๅ ดๅˆใ€`torch.allclose(original_output, output, atol=1e-3)`ใ‚’ไฝฟ็”จใ—ใฆๅ‡บๅŠ›ใ‚’็ขบ่ชใ™ใ‚‹ใจใ€ๆœ€ใ‚‚้›ฃใ—ใ„้ƒจๅˆ†ใŒๅฎŒไบ†ใ—ใพใ™๏ผ +ใŠใ‚ใงใจใ†ใ”ใ–ใ„ใพใ™ - ๅฎŒไบ†ใ™ใ‚‹ไฝœๆฅญใฏ็ฐกๅ˜ใชใ‚‚ใฎใซใชใ‚‹ใฏใšใงใ™ ๐Ÿ˜Šใ€‚ + +**8. ๅฟ…่ฆใชใ™ในใฆใฎใƒขใƒ‡ใƒซใƒ†ใ‚นใƒˆใ‚’่ฟฝๅŠ ** + +ใ“ใฎๆ™‚็‚นใงใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใŒๆญฃๅธธใซ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ +ใŸใ ใ—ใ€ใƒขใƒ‡ใƒซใŒใพใ ๅฟ…่ฆใช่จญ่จˆใซๅฎŒๅ…จใซๆบ–ๆ‹ ใ—ใฆใ„ใชใ„ๅฏ่ƒฝๆ€งใŒ้žๅธธใซ้ซ˜ใ„ใงใ™ใ€‚ +๐Ÿค— TransformersใจๅฎŒๅ…จใซไบ’ๆ›ๆ€งใŒใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ใ™ในใฆใฎไธ€่ˆฌ็š„ใชใƒ†ใ‚นใƒˆใŒใƒ‘ใ‚นใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +CookiecutterใฏใŠใใ‚‰ใใƒขใƒ‡ใƒซ็”จใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใ‚’่‡ชๅ‹•็š„ใซ่ฟฝๅŠ ใ—ใฆใ„ใ‚‹ใฏใšใงใ€ใŠใใ‚‰ใๅŒใ˜ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซ`tests/models/brand_new_bert/test_modeling_brand_new_bert.py`ใจใ—ใฆๅญ˜ๅœจใ—ใพใ™ใ€‚ +ใ“ใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใ‚’ๅฎŸ่กŒใ—ใฆใ€ใ™ในใฆใฎไธ€่ˆฌ็š„ใชใƒ†ใ‚นใƒˆใŒใƒ‘ใ‚นใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pytest tests/models/brand_new_bert/test_modeling_brand_new_bert.py +``` + +ใ™ในใฆใฎไธ€่ˆฌ็š„ใชใƒ†ใ‚นใƒˆใ‚’ไฟฎๆญฃใ—ใŸใ‚‰ใ€ไปŠๅบฆใฏๅฎŸ่กŒใ—ใŸใ™ในใฆใฎ็ด ๆ™ดใ‚‰ใ—ใ„ไฝœๆฅญใŒ้ฉๅˆ‡ใซใƒ†ใ‚นใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใŒ้žๅธธใซ้‡่ฆใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ + +- a) ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใฏ*brand_new_bert*ใฎ็‰นๅฎšใฎใƒ†ใ‚นใƒˆใ‚’่ฆ‹ใ‚‹ใ“ใจใงใ€ใ‚ใชใŸใฎไฝœๆฅญใ‚’็ฐกๅ˜ใซ็†่งฃใงใใพใ™ใ€‚ +- b) ใƒขใƒ‡ใƒซใธใฎๅฐ†ๆฅใฎๅค‰ๆ›ดใŒใƒขใƒ‡ใƒซใฎ้‡่ฆใชๆฉŸ่ƒฝใ‚’ๅฃŠใ•ใชใ„ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ใพใšใ€็ตฑๅˆใƒ†ใ‚นใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎ็ตฑๅˆใƒ†ใ‚นใƒˆใฏใ€ๅŸบๆœฌ็š„ใซใฏใƒ‡ใƒใƒƒใ‚ฐใ‚นใ‚ฏใƒชใƒ—ใƒˆใจๅŒใ˜ใ“ใจใ‚’่กŒใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใƒ†ใ‚นใƒˆใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏCookiecutterใซใ‚ˆใฃใฆๆ—ขใซ่ฟฝๅŠ ใ•ใ‚ŒใฆใŠใ‚Šใ€ใ€ŒBrandNewBertModelIntegrationTestsใ€ใจๅ‘ผใฐใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎใƒ†ใ‚นใƒˆใ‚’่จ˜ๅ…ฅใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใŒๅˆๆ ผใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +RUN_SLOW=1 pytest -sv tests/models/brand_new_bert/test_modeling_brand_new_bert.py::BrandNewBertModelIntegrationTests +``` + + + +Windowsใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€`RUN_SLOW=1`ใ‚’`SET RUN_SLOW=1`ใซ็ฝฎใๆ›ใˆใฆใใ ใ•ใ„ใ€‚ + + + +ๆฌกใซใ€*brand_new_bert*ใซ็‰นๆœ‰ใฎใ™ในใฆใฎ็‰นๅพดใฏใ€ๅˆฅๅ€‹ใฎใƒ†ใ‚นใƒˆๅ†…ใง่ฟฝๅŠ ใ•ใ‚Œใ‚‹ในใใงใ™ใ€‚ +`BrandNewBertModelTester`/`BrandNewBertModelTest`ใฎไธ‹ใซใ€‚ใ“ใฎ้ƒจๅˆ†ใฏใ‚ˆใๅฟ˜ใ‚Œใ‚‰ใ‚Œใพใ™ใŒใ€2ใคใฎ็‚นใง้žๅธธใซๅฝน็ซ‹ใกใพใ™๏ผš + +- ใƒขใƒ‡ใƒซใฎ่ฟฝๅŠ ไธญใซ็ฒๅพ—ใ—ใŸ็Ÿฅ่ญ˜ใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซไผใˆใ€*brand_new_bert*ใฎ็‰นๅˆฅใชๆฉŸ่ƒฝใŒใฉใฎใ‚ˆใ†ใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’็คบใ™ใ“ใจใซใ‚ˆใฃใฆใ€็Ÿฅ่ญ˜ใฎๅ…ฑๆœ‰ใ‚’ๆ”ฏๆดใ—ใพใ™ใ€‚ +- ๅฐ†ๆฅใฎ่ฒข็Œฎ่€…ใฏใ€ใ“ใ‚Œใ‚‰ใฎ็‰นๅˆฅใชใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใงใƒขใƒ‡ใƒซใธใฎๅค‰ๆ›ดใ‚’่ฟ…้€Ÿใซใƒ†ใ‚นใƒˆใงใใพใ™ใ€‚ + +**9. ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎๅฎŸ่ฃ…** + +ๆฌกใซใ€*brand_new_bert*ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚้€šๅธธใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏ๐Ÿค— Transformersใฎๆ—ขๅญ˜ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅŒ็ญ‰ใ‹้žๅธธใซไผผใฆใ„ใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒๆญฃใ—ใๅ‹•ไฝœใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใฏใ€ใพใšใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชๅ†…ใงๆ–‡ๅญ—ๅˆ—ใ‚’ๅ…ฅๅŠ›ใ—ใ€`input_ids`ใ‚’่ฟ”ใ™ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใ“ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผˆ็–‘ไผผใ‚ณใƒผใƒ‰ใง็คบใ—ใพใ™๏ผ‰๏ผš + +```python +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = model.tokenize(input_str) +``` + +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใ‚’่ฉณใ—ใ่ชฟๆŸปใ—ใ€ๆญฃใ—ใ„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎ้–ขๆ•ฐใ‚’่ฆ‹ใคใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใพใŸใฏใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใฎใ‚ฏใƒญใƒผใƒณใ‚’ๅค‰ๆ›ดใ—ใฆใ€`input_ids`ใ ใ‘ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใ‚’ไฝฟ็”จใ—ใŸๆฉŸ่ƒฝ็š„ใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝœๆˆใ—ใŸๅพŒใ€ +๐Ÿค— Transformersๅ‘ใ‘ใฎ้กžไผผใ—ใŸใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ไปฅไธ‹ใฎใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ในใใงใ™๏ผš + +```python +from transformers import BrandNewBertTokenizer + +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." + +tokenizer = BrandNewBertTokenizer.from_pretrained("/path/to/tokenizer/folder/") + +input_ids = tokenizer(input_str).input_ids +``` + +`input_ids`ใŒๅŒใ˜ๅ€คใ‚’็”Ÿๆˆใ—ใŸๅ ดๅˆใ€ๆœ€็ต‚ใ‚นใƒ†ใƒƒใƒ—ใจใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใ‚‚่ฟฝๅŠ ใ™ใ‚‹ในใใงใ™ใ€‚ + +*brand_new_bert*ใฎใƒขใƒ‡ใƒซใƒณใ‚ฐใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใจๅŒๆง˜ใซใ€*brand_new_bert*ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใซใฏใ€ใ„ใใคใ‹ใฎใƒใƒผใƒ‰ใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็ตฑๅˆใƒ†ใ‚นใƒˆใŒๅซใพใ‚Œใ‚‹ในใใงใ™ใ€‚ + +**10. ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰็ตฑๅˆใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒ** + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’่ฟฝๅŠ ใ—ใŸๅพŒใ€`๐Ÿค— Transformers`ๅ†…ใฎ`tests/models/brand_new_bert/test_modeling_brand_new_bert.py`ใซ +ใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎไธกๆ–นใ‚’ไฝฟ็”จใ™ใ‚‹ใ„ใใคใ‹ใฎใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰็ตฑๅˆใƒ†ใ‚นใƒˆใ‚‚่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใฎใ‚ˆใ†ใชใƒ†ใ‚นใƒˆใฏใ€๐Ÿค— TransformersใฎๅฎŸ่ฃ…ใŒๆœŸๅพ…ใฉใŠใ‚ŠใซๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็คบใ™ในใใงใ™ใ€‚ +ๆ„ๅ‘ณใฎใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆๅฏพใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ตใƒณใƒ—ใƒซใŒๅซใพใ‚Œใพใ™ใ€‚ๆœ‰็”จใชใƒ†ใ‚ญใ‚นใƒˆๅฏพใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ตใƒณใƒ—ใƒซใซใฏใ€ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใธใฎ็ฟป่จณใƒšใ‚ขใ€่จ˜ไบ‹ใ‹ใ‚‰่ฆ็ด„ใธใฎใƒšใ‚ขใ€่ณชๅ•ใ‹ใ‚‰ๅ›ž็ญ”ใธใฎใƒšใ‚ขใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ +ใƒใƒผใƒˆใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใ‚ฟใ‚นใ‚ฏใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ†ใ‚นใƒˆใซไพๅญ˜ใ™ใ‚‹ใ ใ‘ใงๅๅˆ†ใงใ™ใ€‚ +ใƒขใƒ‡ใƒซใŒๅฎŒๅ…จใซๆฉŸ่ƒฝใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ใ™ในใฆใฎใƒ†ใ‚นใƒˆใ‚’GPUไธŠใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎๅ†…้ƒจใƒ†ใƒณใ‚ฝใƒซใซ`.to(self.device)`ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใฎใ‚’ๅฟ˜ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ใใฎใ‚ˆใ†ใชใƒ†ใ‚นใƒˆใงใฏใ‚จใƒฉใƒผใŒ่กจ็คบใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ +GPUใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใชใ„ๅ ดๅˆใ€Hugging Faceใƒใƒผใƒ ใŒไปฃใ‚ใ‚Šใซใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ + +**11. ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎ่ฟฝๅŠ ** + +ใ“ใ‚Œใงใ€*brand_new_bert*ใฎๅฟ…่ฆใชใ™ในใฆใฎๆฉŸ่ƒฝใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸ - ใปใผๅฎŒไบ†ใงใ™๏ผๆฎ‹ใ‚Šใฎ่ฟฝๅŠ ใ™ในใใ“ใจใฏใ€่‰ฏใ„ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใจใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใƒšใƒผใ‚ธใงใ™ใ€‚ +CookiecutterใŒ`docs/source/model_doc/brand_new_bert.md`ใจใ„ใ†ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใƒ•ใ‚กใ‚คใƒซใ‚’่ฟฝๅŠ ใ—ใฆใ„ใ‚‹ใฏใšใงใ€ใ“ใ‚Œใ‚’่จ˜ๅ…ฅใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎใƒฆใƒผใ‚ถใƒผใฏ้€šๅธธใ€ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใพใšใ“ใฎใƒšใƒผใ‚ธใ‚’่ฆ‹ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏ็†่งฃใ—ใ‚„ใ™ใ็ฐกๆฝ”ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไฝฟ็”จๆ–นๆณ•ใ‚’็คบใ™ใŸใ‚ใซใ„ใใคใ‹ใฎ*Tips*ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใฏใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซใจใฃใฆ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใซ้–ขใ—ใฆใฏใ€Hugging Faceใƒใƒผใƒ ใซๅ•ใ„ๅˆใ‚ใ›ใ‚‹ใ“ใจใ‚’ใŸใ‚ใ‚‰ใ‚ใชใ„ใงใใ ใ•ใ„ใ€‚ + +ๆฌกใซใ€`src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`ใซ่ฟฝๅŠ ใ•ใ‚ŒใŸใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณๆ–‡ๅญ—ๅˆ—ใŒๆญฃใ—ใ„ใ“ใจใ€ใŠใ‚ˆใณใ™ในใฆใฎๅฟ…่ฆใชๅ…ฅๅŠ›ใŠใ‚ˆใณๅ‡บๅŠ›ใ‚’ๅซใ‚“ใงใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎๆ›ธใๆ–นใจใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณๆ–‡ๅญ—ๅˆ—ใฎใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใซใคใ„ใฆ่ฉณ็ดฐใชใ‚ฌใ‚คใƒ‰ใŒ[ใ“ใกใ‚‰](writing-documentation)ใซใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏ้€šๅธธใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจใƒขใƒ‡ใƒซใฎๆœ€ๅˆใฎๆŽฅ่งฆ็‚นใงใ‚ใ‚‹ใŸใ‚ใ€ใ‚ณใƒผใƒ‰ใจๅŒใ˜ใใ‚‰ใ„ๆณจๆ„ๆทฑใๆ‰ฑใ†ในใใงใ‚ใ‚‹ใ“ใจใ‚’ๅธธใซๅฟต้ ญใซ็ฝฎใ„ใฆใใ ใ•ใ„ใ€‚ + +**ใ‚ณใƒผใƒ‰ใฎใƒชใƒ•ใ‚กใ‚ฏใ‚ฟใƒชใƒณใ‚ฐ** + +็ด ๆ™ดใ‚‰ใ—ใ„ใ€ใ“ใ‚Œใง*brand_new_bert*ใซๅฟ…่ฆใชใ™ในใฆใฎใ‚ณใƒผใƒ‰ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ +ใ“ใฎๆ™‚็‚นใงใ€ๆฌกใฎใ‚ˆใ†ใชใƒใƒ†ใƒณใ‚ทใƒฃใƒซใชใ‚ณใƒผใƒ‰ใ‚นใ‚ฟใ‚คใƒซใฎ่ชคใ‚Šใ‚’่จ‚ๆญฃใ™ใ‚‹ใŸใ‚ใซไปฅไธ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```bash +make style +``` + +ใ‚ใชใŸใฎใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚นใ‚ฟใ‚คใƒซใŒๅ“่ณชใƒใ‚งใƒƒใ‚ฏใ‚’ใƒ‘ใ‚นใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„: + +```bash +make quality +``` + +๐Ÿค— Transformersใฎ้žๅธธใซๅŽณๆ ผใชใƒ‡ใ‚ถใ‚คใƒณใƒ†ใ‚นใƒˆใซใฏใ€ใพใ ๅˆๆ ผใ—ใฆใ„ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ„ใใคใ‹ใฎไป–ใฎใƒ†ใ‚นใƒˆใŒๅญ˜ๅœจใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใ“ใ‚Œใฏใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆๆ–‡ๅญ—ๅˆ—ใซๆƒ…ๅ ฑใŒไธ่ถณใ—ใฆใ„ใ‚‹ใ‹ใ€ๅๅ‰ใŒ้–“้•ใฃใฆใ„ใ‚‹ใ“ใจใŒๅŽŸๅ› ใงใ‚ใ‚‹ใ“ใจใŒๅคšใ„ใงใ™ใ€‚Hugging Faceใƒใƒผใƒ ใฏใ€ใ“ใ“ใง่ฉฐใพใฃใฆใ„ใ‚‹ๅ ดๅˆใซใฏๅฟ…ใšๅŠฉใ‘ใฆใใ‚Œใ‚‹ใงใ—ใ‚‡ใ†ใ€‚ + +ๆœ€ๅพŒใซใ€ใ‚ณใƒผใƒ‰ใŒๆญฃใ—ใๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใŸๅพŒใ€ใ‚ณใƒผใƒ‰ใ‚’ใƒชใƒ•ใ‚กใ‚ฏใ‚ฟใƒชใƒณใ‚ฐใ™ใ‚‹ใฎใฏๅธธใซ่‰ฏใ„ใ‚ขใ‚คใƒ‡ใ‚ขใงใ™ใ€‚ +ใ™ในใฆใฎใƒ†ใ‚นใƒˆใŒใƒ‘ใ‚นใ—ใŸไปŠใ€่ฟฝๅŠ ใ—ใŸใ‚ณใƒผใƒ‰ใ‚’ๅ†ๅบฆ็ขบ่ชใ—ใฆใƒชใƒ•ใ‚กใ‚ฏใ‚ฟใƒชใƒณใ‚ฐใ‚’่กŒใ†ใฎใฏ่‰ฏใ„ใ‚ฟใ‚คใƒŸใƒณใ‚ฐใงใ™ใ€‚ + +ใ“ใ‚Œใงใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฎ้ƒจๅˆ†ใฏๅฎŒไบ†ใ—ใพใ—ใŸใ€ใŠใ‚ใงใจใ†ใ”ใ–ใ„ใพใ™๏ผ ๐ŸŽ‰ ใ‚ใชใŸใฏ็ด ๆ™ดใ‚‰ใ—ใ„ใงใ™๏ผ ๐Ÿ˜Ž + +**12. ใƒขใƒ‡ใƒซใ‚’ใƒขใƒ‡ใƒซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰** + +ๆœ€ๅพŒใฎใƒ‘ใƒผใƒˆใงใฏใ€ใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒขใƒ‡ใƒซใƒใƒ–ใซๅค‰ๆ›ใ—ใฆใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใ€ๅ„ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใƒขใƒ‡ใƒซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใƒใƒ–ใฎๆฉŸ่ƒฝใซใคใ„ใฆ่ฉณใ—ใใฏใ€[Model sharing and uploading Page](model_sharing)ใ‚’่ชญใ‚“ใง็†่งฃใงใใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€*brand_new_bert*ใฎ่‘—่€…็ต„็น”ใฎไธ‹ใซใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใงใใ‚‹ใ‚ˆใ†ใซๅฟ…่ฆใชใ‚ขใ‚ฏใ‚ปใ‚นๆจฉใ‚’ๅ–ๅพ—ใ™ใ‚‹ใŸใ‚ใซใ€Hugging Faceใƒใƒผใƒ ใจๅ”ๅŠ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +`transformers`ใฎใ™ในใฆใฎใƒขใƒ‡ใƒซใซๅญ˜ๅœจใ™ใ‚‹`push_to_hub`ใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹่ฟ…้€Ÿใ‹ใคๅŠน็އ็š„ใชๆ–นๆณ•ใงใ™ใ€‚ +ไปฅไธ‹ใซใ€ๅฐ‘ใ—ใฎใ‚ณใƒผใƒ‰ใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’็คบใ—ใพใ™๏ผš + +```python +brand_new_bert.push_to_hub("brand_new_bert") +# Uncomment the following line to push to an organization. +# brand_new_bert.push_to_hub("/brand_new_bert") +``` + +ๅ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซ้ฉๅˆ‡ใชใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’ไฝœๆˆใ™ใ‚‹ไพกๅ€คใŒใ‚ใ‚Šใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใฏใ€ใ“ใฎ็‰นๅฎšใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ็‰นๆ€งใ‚’ใƒใ‚คใƒฉใ‚คใƒˆใ™ใ‚‹ในใใงใ™ใ€‚ไพ‹ใˆใฐใ€ใ“ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใฉใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไบ‹ๅ‰ๅญฆ็ฟ’/ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‹ใ€ใฉใฎใ‚ˆใ†ใชไธ‹ๆตใ‚ฟใ‚นใ‚ฏใงใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ในใใ‹ใ‚’็คบใ™ในใใงใ™ใ€‚ใพใŸใ€ใƒขใƒ‡ใƒซใฎๆญฃใ—ใ„ไฝฟ็”จๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใ‚ณใƒผใƒ‰ใ‚‚ๅซใ‚ใ‚‹ในใใงใ™ใ€‚ + +**13.๏ผˆใ‚ชใƒ—ใ‚ทใƒงใƒณ๏ผ‰ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎ่ฟฝๅŠ ** + +*brand_new_bert*ใ‚’ๆŽจ่ซ–ใพใŸใฏไธ‹ๆตใ‚ฟใ‚นใ‚ฏใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใซใฉใฎใ‚ˆใ†ใซ่ฉณ็ดฐใซไฝฟ็”จใงใใ‚‹ใ‹ใ‚’็คบใ™ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใฏ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ใ“ใ‚Œใฏใ‚ใชใŸใฎPRใ‚’ใƒžใƒผใ‚ธใ™ใ‚‹ใŸใ‚ใซๅฟ…้ ˆใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซใจใฃใฆ้žๅธธใซๆœ‰็”จใงใ™ใ€‚ + +**14. ๅฎŒๆˆใ—ใŸPRใฎๆๅ‡บ** + +ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ๆœ€ๅพŒใฎใ‚นใƒ†ใƒƒใƒ—ใซ็งปๅ‹•ใ—ใ€PRใ‚’ใƒกใ‚คใƒณใƒ–ใƒฉใƒณใƒใซใƒžใƒผใ‚ธใ—ใพใ—ใ‚‡ใ†ใ€‚้€šๅธธใ€Hugging Faceใƒใƒผใƒ ใฏใ“ใฎๆ™‚็‚นใงๆ—ขใซใ‚ใชใŸใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใฏใšใงใ™ใŒใ€PRใซ่‰ฏใ„่ชฌๆ˜Žใ‚’่ฟฝๅŠ ใ—ใ€ใ‚ณใƒผใƒ‰ใซใ‚ณใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใฆใ€ใƒฌใƒ“ใƒฅใ‚ขใƒผใซ็‰นๅฎšใฎ่จญ่จˆใฎ้ธๆŠž่‚ขใ‚’ๆŒ‡ๆ‘˜ใ—ใŸใ„ๅ ดๅˆใฏใ‚ณใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚‚ไพกๅ€คใŒใ‚ใ‚Šใพใ™ใ€‚ + +### Share your work!! + +ใ•ใ‚ใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใ‹ใ‚‰ใ‚ใชใŸใฎไฝœๆฅญใซๅฏพใ™ใ‚‹่ฉ•ไพกใ‚’ๅพ—ใ‚‹ๆ™‚ใŒๆฅใพใ—ใŸ๏ผใƒขใƒ‡ใƒซใฎ่ฟฝๅŠ ใ‚’ๅฎŒไบ†ใ™ใ‚‹ใ“ใจใฏใ€TransformersใŠใ‚ˆใณNLPใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซใจใฃใฆ้‡่ฆใช่ฒข็Œฎใงใ™ใ€‚ใ‚ใชใŸใฎใ‚ณใƒผใƒ‰ใจใƒใƒผใƒˆใ•ใ‚ŒใŸไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฏใ€ไฝ•็™พไบบใ€ไฝ•ๅƒไบบใจใ„ใ†้–‹็™บ่€…ใ‚„็ ”็ฉถ่€…ใซใ‚ˆใฃใฆ็ขบๅฎŸใซไฝฟ็”จใ•ใ‚Œใ‚‹ใงใ—ใ‚‡ใ†ใ€‚ใ‚ใชใŸใฎไป•ไบ‹ใซ่ช‡ใ‚Šใ‚’ๆŒใกใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจใ‚ใชใŸใฎๆˆๆžœใ‚’ๅ…ฑๆœ‰ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +**ใ‚ใชใŸใฏใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใฎ่ชฐใงใ‚‚็ฐกๅ˜ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ๅˆฅใฎใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ—ใŸ๏ผ ๐Ÿคฏ** diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/attention.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/attention.md new file mode 100644 index 0000000000000000000000000000000000000000..4c452ffa42229936883510abd652bd5e5ab0f4e8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/attention.md @@ -0,0 +1,52 @@ + + +# Attention mechanism + +ใปใจใ‚“ใฉใฎTransformerใƒขใƒ‡ใƒซใฏใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ่กŒๅˆ—ใŒๆญฃๆ–นๅฝขใงใ‚ใ‚‹ใจใ„ใ†ๆ„ๅ‘ณใงๅฎŒๅ…จใชใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€้•ทใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ‰ฑใ†ๅ ดๅˆใซ่จˆ็ฎ—ใฎใƒœใƒˆใƒซใƒใƒƒใ‚ฏใจใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚Longformerใ‚„Reformerใฏใ€ใ‚ˆใ‚ŠๅŠน็އ็š„ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ่กŒๅˆ—ใฎใ‚นใƒ‘ใƒผใ‚นใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝฟ็”จใ—ใ‚ˆใ†ใจใ™ใ‚‹ใƒขใƒ‡ใƒซใงใ™ใ€‚ + +## LSH attention + +[Reformer](model_doc/reformer)ใฏLSH๏ผˆๅฑ€ๆ‰€็š„ใซๆ•ฃๅœจใƒใƒƒใ‚ทใƒฅ๏ผ‰ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚น(QK^t)ใงใฏใ€่กŒๅˆ—QK^tใฎไธญใง๏ผˆใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚นๆฌกๅ…ƒใง๏ผ‰ๆœ€ใ‚‚ๅคงใใช่ฆ็ด ใฎใฟใŒๆœ‰็”จใชๅฏ„ไธŽใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ๅ„ใ‚ฏใ‚จใƒชqใซใคใ„ใฆใ€ใ‚ฏใ‚จใƒชqใซ่ฟ‘ใ„ใ‚ญใƒผkใฎใฟใ‚’่€ƒๆ…ฎใงใใพใ™ใ€‚ +qใจkใŒ่ฟ‘ใ„ใ‹ใฉใ†ใ‹ใ‚’ๆฑบๅฎšใ™ใ‚‹ใŸใ‚ใซใ€ใƒใƒƒใ‚ทใƒฅ้–ขๆ•ฐใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใ‚นใ‚ฏใฏๅค‰ๆ›ดใ•ใ‚Œใ€็พๅœจใฎใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏๅŒ–ใ—ใพใ™๏ผˆๆœ€ๅˆใฎไฝ็ฝฎใ‚’้™คใ๏ผ‰ใ€‚ +ใชใœใชใ‚‰ใ€ใใ‚Œใฏใ‚ฏใ‚จใƒชใจใ‚ญใƒผใŒ็ญ‰ใ—ใ„๏ผˆใคใพใ‚Š้žๅธธใซไผผใฆใ„ใ‚‹๏ผ‰ใ‚ฏใ‚จใƒชใจใ‚ญใƒผใ‚’ๆไพ›ใ™ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ +ใƒใƒƒใ‚ทใƒฅใฏๅคšๅฐ‘ใƒฉใƒณใƒ€ใƒ ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใŸใ‚ใ€ๅฎŸ้š›ใซใฏใ„ใใคใ‹ใฎใƒใƒƒใ‚ทใƒฅ้–ขๆ•ฐใŒไฝฟ็”จใ•ใ‚Œ๏ผˆn_roundsใƒ‘ใƒฉใƒกใƒผใ‚ฟใงๆฑบๅฎšใ•ใ‚Œใพใ™๏ผ‰ใ€ใใ‚Œใ‚‰ใŒๅนณๅ‡ๅŒ–ใ•ใ‚Œใพใ™ใ€‚ + +## Local attention + +[Longformer](model_doc/longformer)ใฏใƒญใƒผใ‚ซใƒซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ใ—ใฐใ—ใฐใ€ใƒญใƒผใ‚ซใƒซใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ๏ผˆไพ‹๏ผšๅทฆๅณใฎ2ใคใฎใƒˆใƒผใ‚ฏใƒณใฏไฝ•ใงใ™ใ‹๏ผŸ๏ผ‰ใฏใ€็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใซๅฏพใ—ใฆ่กŒๅ‹•ใ‚’่ตทใ“ใ™ใฎใซๅๅˆ†ใงใ™ใ€‚ +ใพใŸใ€ๅฐใ•ใชใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใ‚’ๆŒใคใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒฌใ‚คใƒคใƒผใ‚’็ฉใฟ้‡ใญใ‚‹ใ“ใจใงใ€ๆœ€ๅพŒใฎใƒฌใ‚คใƒคใƒผใฏใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๅ†…ใฎใƒˆใƒผใ‚ฏใƒณใ ใ‘ใงใชใใ€ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๅ†…ใฎใƒˆใƒผใ‚ฏใƒณใ‚’่ถ…ใˆใฆๅ—ๅฎน้‡Žใ‚’ๆŒใคใ‚ˆใ†ใซใชใ‚Šใ€ๆ–‡ๅ…จไฝ“ใฎ่กจ็พใ‚’ๆง‹็ฏ‰ใงใใพใ™ใ€‚ + +ไธ€้ƒจใฎไบ‹ๅ‰้ธๆŠžใ•ใ‚ŒใŸๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณใซใฏใ‚ฐใƒญใƒผใƒใƒซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚‚ไธŽใˆใ‚‰ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎๅฐ‘ๆ•ฐใฎใƒˆใƒผใ‚ฏใƒณใซๅฏพใ—ใฆใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ่กŒๅˆ—ใฏใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ€ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏๅฏพ็งฐ็š„ใงใ™ใ€‚ +ไป–ใฎใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใฏใ€ใ“ใ‚Œใ‚‰ใฎ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™๏ผˆใƒญใƒผใ‚ซใƒซใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๅ†…ใฎใƒˆใƒผใ‚ฏใƒณใซๅŠ ใˆใฆ๏ผ‰ใ€‚ +ใ“ใ‚Œใฏใ€่ซ–ๆ–‡ใฎๅ›ณ2dใซ็คบใ•ใ‚ŒใฆใŠใ‚Šใ€ไปฅไธ‹ใฏใ‚ตใƒณใƒ—ใƒซใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใ‚นใ‚ฏใงใ™๏ผš + +
+ +
+ + +## Other tricks + +### Axial positional encodings + +[Reformer](model_doc/reformer)ใฏ่ปธๆ–นๅ‘ใฎไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ไผ็ตฑ็š„ใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใงใฏใ€ไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐEใฏใ‚ตใ‚คใ‚บใŒ \\(l\\) ร— \\(d\\) ใฎ่กŒๅˆ—ใงใ€\\(l\\) ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใ€\\(d\\) ใฏ้š ใ‚Œ็Šถๆ…‹ใฎๆฌกๅ…ƒใงใ™ใ€‚้žๅธธใซ้•ทใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ‰ฑใ†ๅ ดๅˆใ€ใ“ใฎ่กŒๅˆ—ใฏ้žๅธธใซๅคงใใใ€GPUไธŠใงๅคง้‡ใฎใ‚นใƒšใƒผใ‚นใ‚’ๅ ๆœ‰ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚’็ทฉๅ’Œใ™ใ‚‹ใŸใ‚ใซใ€่ปธๆ–นๅ‘ใฎไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฏใ€ใ“ใฎๅคงใใช่กŒๅˆ—Eใ‚’2ใคใฎๅฐใ•ใช่กŒๅˆ—E1ใจE2ใซๅˆ†่งฃใ—ใพใ™ใ€‚ใใ‚Œใžใ‚Œใฎ่กŒๅˆ—ใฏใ‚ตใ‚คใ‚บ \\(l_{1} \times d_{1}\\) ใŠใ‚ˆใณ \\(l_{2} \times d_{2}\\) ใ‚’ๆŒใกใ€ \\(l_{1} \times l_{2} = l\\) ใŠใ‚ˆใณ \\(d_{1} + d_{2} = d\\) ใจใ„ใ†ๆกไปถใ‚’ๆบ€ใŸใ—ใพใ™๏ผˆ้•ทใ•ใฎ็ฉใ‚’่€ƒใˆใ‚‹ใจใ€ใ“ใ‚ŒใŒใฏใ‚‹ใ‹ใซๅฐใ•ใใชใ‚Šใพใ™๏ผ‰ใ€‚่กŒๅˆ—Eๅ†…ใฎๆ™‚ๅˆป \\(j\\) ใฎๅŸ‹ใ‚่พผใฟใฏใ€E1ๅ†…ใฎๆ™‚ๅˆป \\(j \% l1\\) ใฎๅŸ‹ใ‚่พผใฟใจE2ๅ†…ใฎๆ™‚ๅˆป \\(j // l1\\) ใฎๅŸ‹ใ‚่พผใฟใ‚’้€ฃ็ตใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/autoclass_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/autoclass_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..f8fbeaa221f6aa34e41d0718a4933e06445a2bce --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/autoclass_tutorial.md @@ -0,0 +1,161 @@ + + +# AutoClassใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ + +ใ•ใพใ–ใพใชTransformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใ€่‡ชๅˆ†ใฎใ‚ฟใ‚นใ‚ฏใซๅˆใฃใŸใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใฎใฏ้›ฃใ—ใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ +๐Ÿค— Transformersใฎใ‚ณใ‚ขๅ“ฒๅญฆใฎไธ€็’ฐใจใ—ใฆใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใ‚„ใ™ใใ€ใ‚ทใƒณใƒ—ใƒซใงๆŸ”่ปŸใซใ™ใ‚‹ใŸใ‚ใซใ€ +`AutoClass`ใฏไธŽใˆใ‚‰ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ๆญฃใ—ใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’่‡ชๅ‹•็š„ใซๆŽจ่ซ–ใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +`from_pretrained()`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’็ด ๆ—ฉใใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ผใƒญใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซๆ™‚้–“ใจใƒชใ‚ฝใƒผใ‚นใ‚’่ฒปใ‚„ใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ“ใฎ็จฎใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซไพๅญ˜ใ—ใชใ„ใ‚ณใƒผใƒ‰ใ‚’็”Ÿๆˆใ™ใ‚‹ใ“ใจใฏใ€ +ใ‚ณใƒผใƒ‰ใŒ1ใคใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงๅ‹•ไฝœใ™ใ‚Œใฐใ€ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒ็•ฐใชใฃใฆใ„ใฆใ‚‚ใ€ๅŒใ˜ใ‚ฟใ‚นใ‚ฏใซๅ‘ใ‘ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅ ดๅˆใฏๅˆฅใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงใ‚‚ๅ‹•ไฝœใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + + + +ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใƒขใƒ‡ใƒซใฎ้ชจๆ ผใ‚’ๆŒ‡ใ—ใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ็‰นๅฎšใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎ้‡ใฟใงใ™ใ€‚ +ใŸใจใˆใฐใ€[BERT](https://huggingface.co/google-bert/bert-base-uncased)ใฏใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใ‚ใ‚Šใ€`google-bert/bert-base-uncased`ใฏใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงใ™ใ€‚ +ใƒขใƒ‡ใƒซใฏใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใพใŸใฏใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎใฉใกใ‚‰ใ‚’ๆŒ‡ใ™ไธ€่ˆฌ็š„ใช็”จ่ชžใงใ™ใ€‚ + + + +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ไปฅไธ‹ใ‚’ๅญฆ็ฟ’ใ—ใพใ™๏ผš + +* ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ +* ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ +* ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟ็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ +* ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ +* ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ + +## AutoTokenizer + +ใปใจใ‚“ใฉใฎNLPใ‚ฟใ‚นใ‚ฏใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใงๅง‹ใพใ‚Šใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใงๅ‡ฆ็†ใงใใ‚‹ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +[`AutoTokenizer.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") +``` + + +ๆฌกใซใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซๅ…ฅๅŠ›ใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ—ใพใ™๏ผš + +```py +>>> sequence = "In a hole in the ground there lived a hobbit." +>>> print(tokenizer(sequence)) +{'input_ids': [101, 1999, 1037, 4920, 1999, 1996, 2598, 2045, 2973, 1037, 7570, 10322, 4183, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +## AutoImageProcessor + +ใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใŒ็”ปๅƒใ‚’ๆญฃใ—ใ„ๅ…ฅๅŠ›ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +``` + +## AutoFeatureExtractor + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใŒใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชไฟกๅทใ‚’ๆญฃใ—ใ„ๅ…ฅๅŠ›ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +[`AutoFeatureExtractor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆ็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™. + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained( +... "ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" +... ) +``` + +## AutoProcessor + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€2ใคใฎๅ‰ๅ‡ฆ็†ใƒ„ใƒผใƒซใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใƒ—ใƒญใ‚ปใƒƒใ‚ตใŒๅฟ…่ฆใงใ™ใ€‚ใŸใจใˆใฐใ€ +[LayoutLMV2](model_doc/layoutlmv2)ใƒขใƒ‡ใƒซใฏ็”ปๅƒใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใจใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒๅฟ…่ฆใงใ™ใ€‚ +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ“ใ‚Œใ‚‰ใฎไธกๆ–นใ‚’็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ + +[`AutoProcessor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") +``` + +## AutoModel + + + +ๆœ€ๅพŒใซใ€`AutoModelFor`ใ‚ฏใƒฉใ‚นใฏ็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผˆไฝฟ็”จๅฏ่ƒฝใชใ‚ฟใ‚นใ‚ฏใฎๅฎŒๅ…จใชไธ€่ฆงใซใคใ„ใฆใฏ[ใ“ใกใ‚‰](model_doc/auto)ใ‚’ๅ‚็…ง๏ผ‰ใ€‚ +ใŸใจใˆใฐใ€[`AutoModelForSequenceClassification.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กž็”จใฎใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅ†ๅˆฉ็”จใ—ใฆ็•ฐใชใ‚‹ใ‚ฟใ‚นใ‚ฏใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> from transformers import AutoModelForTokenClassification + +>>> model = AutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +PyTorchใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€ `from_pretrained()`ใƒกใ‚ฝใƒƒใƒ‰ใฏๅ†…้ƒจใง`torch.load()`ใ‚’ไฝฟ็”จใ—ใ€ๅ†…้ƒจ็š„ใซใฏ`pickle`ใ‚’ไฝฟ็”จใ—ใฆใŠใ‚Šใ€ใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใฎๅ•้กŒใŒ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ +ไธ€่ˆฌ็š„ใซใฏใ€ไฟก้ ผๆ€งใฎใชใ„ใ‚ฝใƒผใ‚นใ‹ใ‚‰ๅ–ๅพ—ใ—ใŸๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใƒขใƒ‡ใƒซใ‚„ๆ”นใ–ใ‚“ใ•ใ‚ŒใŸๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ +ใ“ใฎใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใƒชใ‚นใ‚ฏใฏใ€`Hugging Face Hub`ใงใƒ›ใ‚นใƒˆใ•ใ‚Œใฆใ„ใ‚‹ๅ…ฌ้–‹ใƒขใƒ‡ใƒซใซๅฏพใ—ใฆ้ƒจๅˆ†็š„ใซ็ทฉๅ’Œใ•ใ‚ŒใฆใŠใ‚Šใ€ๅ„ใ‚ณใƒŸใƒƒใƒˆใงใƒžใƒซใ‚ฆใ‚งใ‚ขใฎใ‚นใ‚ญใƒฃใƒณใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ +GPGใ‚’ไฝฟ็”จใ—ใŸ็ฝฒๅๆธˆใฟใ‚ณใƒŸใƒƒใƒˆใฎๆคœ่จผใชใฉใฎใƒ™ใ‚นใƒˆใƒ—ใƒฉใ‚ฏใƒ†ใ‚ฃใ‚นใซใคใ„ใฆใฏใ€Hubใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +TensorFlowใŠใ‚ˆใณFlaxใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซใฏๅฝฑ้ŸฟใŒใชใใ€`from_pretrained`ใƒกใ‚ฝใƒƒใƒ‰ใฎ`from_tf`ใŠใ‚ˆใณ`from_flax`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆPyTorchใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃๅ†…ใงใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + + + +ไธ€่ˆฌ็š„ใซใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซ`AutoTokenizer`ใ‚ฏใƒฉใ‚นใจ`AutoModelFor`ใ‚ฏใƒฉใ‚นใฎไฝฟ็”จใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅธธใซๆญฃใ—ใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ +ๆฌกใฎ[tutorial](preprocessing)ใงใฏใ€ๆ–ฐใ—ใใƒญใƒผใƒ‰ใ—ใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ€ใŠใ‚ˆใณใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใณใพใ™ใ€‚ + + +ๆœ€ๅพŒใซใ€`TFAutoModelFor`ใ‚ฏใƒฉใ‚นใฏ็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผˆไฝฟ็”จๅฏ่ƒฝใชใ‚ฟใ‚นใ‚ฏใฎๅฎŒๅ…จใชไธ€่ฆงใซใคใ„ใฆใฏใ“ใกใ‚‰ใ‚’ๅ‚็…ง๏ผ‰ใ€‚ +ใŸใจใˆใฐใ€[`TFAutoModelForSequenceClassification.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กž็”จใฎใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅ†ๅˆฉ็”จใ—ใฆ็•ฐใชใ‚‹ใ‚ฟใ‚นใ‚ฏใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ไธ€่ˆฌ็š„ใซใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซ`AutoTokenizer`ใ‚ฏใƒฉใ‚นใจ`TFAutoModelFor`ใ‚ฏใƒฉใ‚นใฎไฝฟ็”จใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅธธใซๆญฃใ—ใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ +ๆฌกใฎ[tutorial](preproccesing)ใงใฏใ€ๆ–ฐใ—ใใƒญใƒผใƒ‰ใ—ใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ€ใŠใ‚ˆใณใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใณใพใ™ใ€‚ + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/bertology.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/bertology.md new file mode 100644 index 0000000000000000000000000000000000000000..5287354cb2f148fe4b1133816b19b85e43f93061 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/bertology.md @@ -0,0 +1,34 @@ + + + +# BERTology + +ๅคง่ฆๆจกใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ€ไพ‹ใˆใฐBERTใฎๅ†…้ƒจๅ‹•ไฝœใ‚’่ชฟๆŸปใ™ใ‚‹็ ”็ฉถ้ ˜ๅŸŸใŒๆ€ฅๆˆ้•ทใ—ใฆใ„ใพใ™๏ผˆใ“ใ‚Œใ‚’ใ€ŒBERTologyใ€ใจใ‚‚ๅ‘ผใณใพใ™๏ผ‰ใ€‚ใ“ใฎๅˆ†้‡Žใฎ่‰ฏใ„ไพ‹ใฏไปฅไธ‹ใงใ™๏ผš + +- BERT Rediscovers the Classical NLP Pipeline by Ian Tenney, Dipanjan Das, Ellie Pavlick: + [่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/1905.05950) +- Are Sixteen Heads Really Better than One? by Paul Michel, Omer Levy, Graham Neubig: [่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/1905.10650) +- What Does BERT Look At? An Analysis of BERT's Attention by Kevin Clark, Urvashi Khandelwal, Omer Levy, Christopher D. Manning: [่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/1906.04341) +- CAT-probing: A Metric-based Approach to Interpret How Pre-trained Models for Programming Language Attend Code Structure: [่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/2210.04633) + +ใ“ใฎๆ–ฐใ—ใ„ๅˆ†้‡Žใฎ็™บๅฑ•ใ‚’ๆ”ฏๆดใ™ใ‚‹ใŸใ‚ใซใ€BERT/GPT/GPT-2ใƒขใƒ‡ใƒซใซใ„ใใคใ‹ใฎ่ฟฝๅŠ ๆฉŸ่ƒฝใ‚’็ต„ใฟ่พผใฟใ€ไบบใ€…ใŒๅ†…้ƒจ่กจ็พใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใ‚‰ใฎๆฉŸ่ƒฝใฏใ€ไธปใซPaul Michelๆฐใฎๅ„ชใ‚ŒใŸ็ ”็ฉถ๏ผˆ[่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/1905.10650)๏ผ‰ใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ๅ…ทไฝ“็š„ใซใฏใ€ไปฅไธ‹ใฎๆฉŸ่ƒฝใŒๅซใพใ‚Œใฆใ„ใพใ™๏ผš + +- BERT/GPT/GPT-2ใฎใ™ในใฆใฎ้š ใ‚Œ็Šถๆ…‹ใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- BERT/GPT/GPT-2ใฎๅ„ใƒ˜ใƒƒใƒ‰ใฎๆณจๆ„้‡ใฟใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ +- ใƒ˜ใƒƒใƒ‰ใฎๅ‡บๅŠ›ๅ€คใจๅ‹พ้…ใ‚’ๅ–ๅพ—ใ—ใ€ใƒ˜ใƒƒใƒ‰ใฎ้‡่ฆๆ€งใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ—ใ€[่ซ–ๆ–‡ใƒชใƒณใ‚ฏ](https://huggingface.co/papers/1905.10650)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใƒ˜ใƒƒใƒ‰ใ‚’ๅ‰Šๆธ›ใงใใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎๆฉŸ่ƒฝใ‚’็†่งฃใ—ใ€ไฝฟ็”จใ™ใ‚‹ใฎใ‚’ๆ”ฏๆดใ™ใ‚‹ใŸใ‚ใซใ€็‰นๅฎšใฎใ‚ตใƒณใƒ—ใƒซใ‚นใ‚ฏใƒชใƒ—ใƒˆใ€Œ[bertology.py](https://github.com/huggingface/transformers-research-projects/tree/main/bertology/run_bertology.py)ใ€ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ใ“ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€GLUEใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‹ใ‚‰ๆƒ…ๅ ฑใ‚’ๆŠฝๅ‡บใ—ใ€ใƒ˜ใƒƒใƒ‰ใ‚’ๅ‰Šๆธ›ใ™ใ‚‹ๅฝนๅ‰ฒใ‚’ๆžœใŸใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/big_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/big_models.md new file mode 100644 index 0000000000000000000000000000000000000000..78852dc4374cceafdded1d91a69a0ae04e3d90ea --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/big_models.md @@ -0,0 +1,130 @@ + + +# Instantiating a big model + +้žๅธธใซๅคง่ฆๆจกใชไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€RAMใฎไฝฟ็”จ้‡ใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‹ใ“ใจใฏ่ชฒ้กŒใฎ1ใคใงใ™ใ€‚้€šๅธธใฎPyTorchใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +1. ใƒฉใƒณใƒ€ใƒ ใช้‡ใฟใ‚’ๆŒใคใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +2. ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +3. ใ“ใ‚Œใ‚‰ใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒฉใƒณใƒ€ใƒ ใชใƒขใƒ‡ใƒซใซ้…็ฝฎใ—ใพใ™ใ€‚ + +ใ‚นใƒ†ใƒƒใƒ—1ใจ2ใฎไธกๆ–นใŒใƒกใƒขใƒชใซใƒขใƒ‡ใƒซใฎๅฎŒๅ…จใชใƒใƒผใ‚ธใƒงใƒณใ‚’ๅฟ…่ฆใจใ—ใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใฏๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใŒๆ•ฐใ‚ฎใ‚ฌใƒใ‚คใƒˆใซใชใ‚‹ใจใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใ‚ณใƒ”ใƒผใ‚’RAMใ‹ใ‚‰ๆŽ’้™คใ™ใ‚‹ใ“ใจใŒใงใใชใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ•ใ‚‰ใซๆ‚ชใ„ใ“ใจใซใ€ๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใซ`torch.distributed`ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ๅ„ใƒ—ใƒญใ‚ปใ‚นใฏไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใ‚ณใƒ”ใƒผใ‚’RAMใซไฟๅญ˜ใ—ใพใ™ใ€‚ + + + +ใƒฉใƒณใƒ€ใƒ ใซไฝœๆˆใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€ใƒกใƒขใƒชๅ†…ใซใ€Œ็ฉบใฎใ€ใƒ†ใƒณใ‚ฝใƒซใงๅˆๆœŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒฉใƒณใƒ€ใƒ ใชๅ€คใฏใ€ใƒกใƒขใƒชใฎ็‰นๅฎšใฎใƒใƒฃใƒณใ‚ฏใซใ‚ใฃใŸใ‚‚ใฎใ‚’ไฝฟ็”จใ—ใพใ™๏ผˆใ—ใŸใŒใฃใฆใ€ใƒฉใƒณใƒ€ใƒ ใชๅ€คใฏใใฎๆ™‚็‚นใงใฎใƒกใƒขใƒชใƒใƒฃใƒณใ‚ฏๅ†…ใฎๅ€คใงใ™๏ผ‰ใ€‚ใƒขใƒ‡ใƒซ/ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ็จฎ้กžใซ้ฉใ—ใŸๅˆ†ๅธƒ๏ผˆใŸใจใˆใฐใ€ๆญฃ่ฆๅˆ†ๅธƒ๏ผ‰ใซๅพ“ใ†ใƒฉใƒณใƒ€ใƒ ใชๅˆๆœŸๅŒ–ใฏใ€ใ‚นใƒ†ใƒƒใƒ—3ใงๅˆๆœŸๅŒ–ใ•ใ‚Œใฆใ„ใชใ„้‡ใฟใซๅฏพใ—ใฆใ€ใงใใ‚‹ใ ใ‘้ซ˜้€ŸใซๅฎŸ่กŒใ•ใ‚Œใพใ™๏ผ + + + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€TransformersใŒใ“ใฎๅ•้กŒใซๅฏพๅ‡ฆใ™ใ‚‹ใŸใ‚ใซๆไพ›ใ™ใ‚‹ใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใ‚’ๆŽขใ‚Šใพใ™ใ€‚ใชใŠใ€ใ“ใ‚Œใฏ็พๅœจใ‚‚้–‹็™บใŒ้€ฒ่กŒไธญใฎๅˆ†้‡Žใงใ‚ใ‚Šใ€ๅฐ†ๆฅใ€ใ“ใ“ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹APIใŒใ‚ใšใ‹ใซๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +## Sharded checkpoints + +ใƒใƒผใ‚ธใƒงใƒณ4.18.0ใ‹ใ‚‰ใ€10GBใ‚’่ถ…ใˆใ‚‹ใ‚ตใ‚คใ‚บใฎใƒขใƒ‡ใƒซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ่‡ชๅ‹•็š„ใซ่ค‡ๆ•ฐใฎๅฐใ•ใช้ƒจๅˆ†ใซๅˆ†ๅ‰ฒใ•ใ‚Œใพใ™ใ€‚`model.save_pretrained(save_dir)`ใ‚’ๅฎŸ่กŒใ™ใ‚‹้š›ใซ1ใคใฎๅ˜ไธ€ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆŒใคไปฃใ‚ใ‚Šใซใ€ใ„ใใคใ‹ใฎ้ƒจๅˆ†็š„ใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ๏ผˆใใ‚Œใžใ‚Œใฎใ‚ตใ‚คใ‚บใŒ<10GB๏ผ‰ใจใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟๅใ‚’ใใ‚Œใ‚‰ใŒๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใ‚กใ‚คใƒซใซใƒžใƒƒใƒ—ใ™ใ‚‹ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใŒ็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ + +`max_shard_size`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๅ‰ใฎๆœ€ๅคงใ‚ตใ‚คใ‚บใ‚’ๅˆถๅพกใงใใ‚‹ใŸใ‚ใ€ไพ‹ใจใ—ใฆ้€šๅธธใ‚ตใ‚คใ‚บใฎใƒขใƒ‡ใƒซใจๅฐใ•ใชใ‚ทใƒฃใƒผใƒ‰ใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅพ“ๆฅใฎBERTใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +```py +from transformers import AutoModel + +model = AutoModel.from_pretrained("google-bert/bert-base-cased") +``` + +ใ‚‚ใ—[`~PreTrainedModel.save_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆไฟๅญ˜ใ™ใ‚‹ๅ ดๅˆใ€ๆ–ฐใ—ใ„ใƒ•ใ‚ฉใƒซใƒ€ใŒ2ใคใฎใƒ•ใ‚กใ‚คใƒซใ‚’ๅซใ‚€ๅฝขใงไฝœๆˆใ•ใ‚Œใพใ™: ใƒขใƒ‡ใƒซใฎ่จญๅฎšๆƒ…ๅ ฑใจใใฎ้‡ใฟๆƒ…ๅ ฑใงใ™ใ€‚ + +```py +>>> import os +>>> import tempfile + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir) +... print(sorted(os.listdir(tmp_dir))) +['config.json', 'pytorch_model.bin'] +``` + +ๆœ€ๅคงใ‚ทใƒฃใƒผใƒ‰ใ‚ตใ‚คใ‚บใ‚’200MBใซ่จญๅฎšใ—ใพใ™๏ผš + +```py +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... print(sorted(os.listdir(tmp_dir))) +['config.json', 'pytorch_model-00001-of-00003.bin', 'pytorch_model-00002-of-00003.bin', 'pytorch_model-00003-of-00003.bin', 'pytorch_model.bin.index.json'] +``` + +ใƒขใƒ‡ใƒซใฎ่จญๅฎšใฎไธŠใซใ€3ใคใฎ็•ฐใชใ‚‹้‡ใฟใƒ•ใ‚กใ‚คใƒซใจใ€`index.json`ใƒ•ใ‚กใ‚คใƒซใŒ่ฆ‹ใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏ็งใŸใกใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงใ™ใ€‚ +ใ“ใฎใ‚ˆใ†ใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€[`~PreTrainedModel.from_pretrained`]ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆๅฎŒๅ…จใซๅ†ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... new_model = AutoModel.from_pretrained(tmp_dir) +``` + +ไธป่ฆใชๅˆฉ็‚นใฏใ€ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€ไธŠ่จ˜ใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใฎใ‚นใƒ†ใƒƒใƒ—2ใซใŠใ„ใฆใ€ๅ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎใ‚ทใƒฃใƒผใƒ‰ใŒๅ‰ใฎใ‚ทใƒฃใƒผใƒ‰ใฎๅพŒใซใƒญใƒผใƒ‰ใ•ใ‚Œใ€RAMใฎใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใจๆœ€ๅคงใฎใ‚ทใƒฃใƒผใƒ‰ใฎใ‚ตใ‚คใ‚บใ‚’ๅˆใ‚ใ›ใŸใ‚‚ใฎใซๅˆถ้™ใงใใ‚‹ใ“ใจใงใ™ใ€‚ + +ๅ†…้ƒจใงใฏใ€ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใƒ•ใ‚กใ‚คใƒซใŒไฝฟ็”จใ•ใ‚Œใ€ใฉใฎใ‚ญใƒผใŒใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซๅญ˜ๅœจใ—ใ€ๅฏพๅฟœใ™ใ‚‹้‡ใฟใŒใฉใ“ใซๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’ๅˆคๆ–ญใ—ใพใ™ใ€‚ใ“ใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฏ้€šๅธธใฎJSONใƒ•ใ‚กใ‚คใƒซใฎใ‚ˆใ†ใซ่ชญใฟ่พผใ‚€ใ“ใจใŒใงใใ€่พžๆ›ธใจใ—ใฆๅ–ๅพ—ใงใใพใ™ใ€‚ + + +```py +>>> import json + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... with open(os.path.join(tmp_dir, "pytorch_model.bin.index.json"), "r") as f: +... index = json.load(f) + +>>> print(index.keys()) +dict_keys(['metadata', 'weight_map']) +``` + +ใƒกใ‚ฟใƒ‡ใƒผใ‚ฟใซใฏ็พๆ™‚็‚นใงใฏใƒขใƒ‡ใƒซใฎ็ทใ‚ตใ‚คใ‚บใฎใฟใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +ๅฐ†ๆฅ็š„ใซใฏไป–ใฎๆƒ…ๅ ฑใ‚’่ฟฝๅŠ ใ™ใ‚‹ไบˆๅฎšใงใ™๏ผš + +```py +>>> index["metadata"] +{'total_size': 433245184} +``` + +้‡ใฟใƒžใƒƒใƒ—ใฏใ“ใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฎไธป่ฆใช้ƒจๅˆ†ใงใ‚ใ‚Šใ€ๅ„ใƒ‘ใƒฉใƒกใƒผใ‚ฟๅ๏ผˆ้€šๅธธใฏPyTorchใƒขใƒ‡ใƒซใฎ`state_dict`ใง่ฆ‹ใคใ‹ใ‚‹ใ‚‚ใฎ๏ผ‰ใ‚’ใใฎๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใ‚กใ‚คใƒซใซใƒžใƒƒใƒ—ใ—ใพใ™๏ผš + +```py +>>> index["weight_map"] +{'embeddings.LayerNorm.bias': 'pytorch_model-00001-of-00003.bin', + 'embeddings.LayerNorm.weight': 'pytorch_model-00001-of-00003.bin', + ... +``` + +็›ดๆŽฅใƒขใƒ‡ใƒซๅ†…ใง[`~PreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ›ใšใซใ€ +ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใŸใ„ๅ ดๅˆ๏ผˆใƒ•ใƒซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎๅ ดๅˆใซ`model.load_state_dict()`ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซ่กŒใ†ๆ–นๆณ•๏ผ‰ใ€[`~modeling_utils.load_sharded_checkpoint`]ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + + +```py +>>> from transformers.modeling_utils import load_sharded_checkpoint + +>>> with tempfile.TemporaryDirectory() as tmp_dir: +... model.save_pretrained(tmp_dir, max_shard_size="200MB") +... load_sharded_checkpoint(model, tmp_dir) +``` + + +## Low memory loading + +ใ‚ทใƒฃใƒผใƒ‰ใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ไธŠ่จ˜ใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใฎใ‚นใƒ†ใƒƒใƒ—2ใซใŠใ‘ใ‚‹ใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใŒใ€ +ไฝŽใƒกใƒขใƒชใฎ็’ฐๅขƒใงใใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใ€Accelerateใƒฉใ‚คใƒ–ใƒฉใƒชใซๅŸบใฅใ„ใŸๅฝ“็คพใฎใƒ„ใƒผใƒซใ‚’ๆดป็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎใ‚ฌใ‚คใƒ‰ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผš[Accelerateใ‚’ไฝฟ็”จใ—ใŸๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟ](./main_classes/model#large-model-loading) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/chat_templating.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/chat_templating.md new file mode 100644 index 0000000000000000000000000000000000000000..ebe0a68fd42cca8aedf5c0166c88e7fa49e0069e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/chat_templating.md @@ -0,0 +1,249 @@ + + +# Chat Templates + +## Introduction + +LLM๏ผˆLanguage Model๏ผ‰ใฎใพใ™ใพใ™ไธ€่ˆฌ็š„ใชไฝฟ็”จไบ‹ไพ‹ใฎ1ใคใฏใ€Œใƒใƒฃใƒƒใƒˆใ€ใงใ™ใ€‚ +ใƒใƒฃใƒƒใƒˆใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใงใฏใ€้€šๅธธใฎ่จ€่ชžใƒขใƒ‡ใƒซใฎใ‚ˆใ†ใซๅ˜ไธ€ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚นใƒˆใƒชใƒณใ‚ฐใ‚’็ถ™็ถšใ™ใ‚‹ใฎใงใฏใชใใ€ใƒขใƒ‡ใƒซใฏ1ใคไปฅไธŠใฎใ€Œใƒกใƒƒใ‚ปใƒผใ‚ธใ€ใ‹ใ‚‰ใชใ‚‹ไผš่ฉฑใ‚’็ถ™็ถšใ—ใพใ™ใ€‚ +ๅ„ใƒกใƒƒใ‚ปใƒผใ‚ธใซใฏใ€Œใƒญใƒผใƒซใ€ใจใƒกใƒƒใ‚ปใƒผใ‚ธใƒ†ใ‚ญใ‚นใƒˆใŒๅซใพใ‚Œใพใ™ใ€‚ + +ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใซใ€ใ“ใ‚Œใ‚‰ใฎใƒญใƒผใƒซใฏใƒฆใƒผใ‚ถใƒผใ‹ใ‚‰ใฎใƒกใƒƒใ‚ปใƒผใ‚ธใซใฏใ€Œใƒฆใƒผใ‚ถใƒผใ€ใ€ใƒขใƒ‡ใƒซใ‹ใ‚‰ใฎใƒกใƒƒใ‚ปใƒผใ‚ธใซใฏใ€Œใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใ€ใŒๅ‰ฒใ‚Šๅฝ“ใฆใ‚‰ใ‚Œใพใ™ใ€‚ +ไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ€Œใ‚ทใ‚นใƒ†ใƒ ใ€ใƒญใƒผใƒซใ‚‚ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ +ใ‚ทใ‚นใƒ†ใƒ ใƒกใƒƒใ‚ปใƒผใ‚ธใฏ้€šๅธธไผš่ฉฑใฎ้–‹ๅง‹ๆ™‚ใซ้€ไฟกใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฎๅ‹•ไฝœๆ–นๆณ•ใซ้–ขใ™ใ‚‹ๆŒ‡็คบใŒๅซใพใ‚Œใพใ™ใ€‚ + +ใ™ในใฆใฎ่จ€่ชžใƒขใƒ‡ใƒซใ€ใƒใƒฃใƒƒใƒˆ็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅซใ‚€ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏใ€ใƒˆใƒผใ‚ฏใƒณใฎใƒชใƒ‹ใ‚ขใ‚ทใƒผใ‚ฑใƒณใ‚นใงๅ‹•ไฝœใ—ใ€ใƒญใƒผใƒซใซ็‰นๆœ‰ใฎ็‰นๅˆฅใชๅ‡ฆ็†ใ‚’ๆŒใกใพใ›ใ‚“ใ€‚ +ใคใพใ‚Šใ€ใƒญใƒผใƒซๆƒ…ๅ ฑใฏ้€šๅธธใ€ใƒกใƒƒใ‚ปใƒผใ‚ธ้–“ใซๅˆถๅพกใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ—ใฆๆณจๅ…ฅใ•ใ‚Œใ€ใƒกใƒƒใ‚ปใƒผใ‚ธใฎๅขƒ็•Œใจ้–ข้€ฃใ™ใ‚‹ใƒญใƒผใƒซใ‚’็คบใ™ใ“ใจใงๆไพ›ใ•ใ‚Œใพใ™ใ€‚ + +ๆฎ‹ๅฟตใชใŒใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใฎไฝฟ็”จๆ–นๆณ•ใซใคใ„ใฆใฏ๏ผˆใพใ ๏ผ๏ผ‰ๆจ™ๆบ–ใŒๅญ˜ๅœจใ›ใšใ€็•ฐใชใ‚‹ใƒขใƒ‡ใƒซใฏใƒใƒฃใƒƒใƒˆ็”จใฎใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ‚„ๅˆถๅพกใƒˆใƒผใ‚ฏใƒณใŒๅคงใใ็•ฐใชใ‚‹ๅฝขๅผใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใ“ใ‚Œใฏใƒฆใƒผใ‚ถใƒผใซใจใฃใฆๅฎŸ้š›ใฎๅ•้กŒใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๆญฃใ—ใ„ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใชใ„ใจใ€ใƒขใƒ‡ใƒซใฏๅ…ฅๅŠ›ใซๆททไนฑใ—ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๆœฌๆฅใ‚ˆใ‚Šใ‚‚้ฅใ‹ใซไฝŽไธ‹ใ—ใพใ™ใ€‚ +ใ“ใ‚ŒใŒใ€Œใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ€ใŒ่งฃๆฑบใ—ใ‚ˆใ†ใจใ™ใ‚‹ๅ•้กŒใงใ™ใ€‚ + +ใƒใƒฃใƒƒใƒˆไผš่ฉฑใฏ้€šๅธธใ€ๅ„่พžๆ›ธใŒใ€Œใƒญใƒผใƒซใ€ใจใ€Œใ‚ณใƒณใƒ†ใƒณใƒ„ใ€ใฎใ‚ญใƒผใ‚’ๅซใฟใ€ๅ˜ไธ€ใฎใƒใƒฃใƒƒใƒˆใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’่กจใ™ใƒชใ‚นใƒˆใจใ—ใฆ่กจ็พใ•ใ‚Œใพใ™ใ€‚ +ใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎไผš่ฉฑใ‚’ๅ˜ไธ€ใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ๅฏ่ƒฝใชใ‚ทใƒผใ‚ฑใƒณใ‚นใซใฉใฎใ‚ˆใ†ใซใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ™ใ‚‹ใ‹ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹Jinjaใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ๅซใ‚€ๆ–‡ๅญ—ๅˆ—ใงใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจใ“ใฎๆƒ…ๅ ฑใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒๆœŸๅพ…ใ™ใ‚‹ๅฝขๅผใฎๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +ใ•ใฃใใใ€`BlenderBot` ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸไพ‹ใ‚’็คบใ—ใฆๅ…ทไฝ“็š„ใซใ—ใพใ—ใ‚‡ใ†ใ€‚`BlenderBot` ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏ้žๅธธใซใ‚ทใƒณใƒ—ใƒซใงใ€ใปใจใ‚“ใฉใŒๅฏพ่ฉฑใฎใƒฉใ‚ฆใƒณใƒ‰้–“ใซ็ฉบ็™ฝใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + + +```python +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") + +>>> chat = [ +... {"role": "user", "content": "Hello, how are you?"}, +... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, +... {"role": "user", "content": "I'd like to show off how chat templating works!"}, +... ] + +>>> tokenizer.apply_chat_template(chat, tokenize=False) +" Hello, how are you? I'm doing great. How can I help you today? I'd like to show off how chat templating works!" +``` + +ๆŒ‡ๅฎšใ•ใ‚ŒใŸ้€šใ‚Šใ€ใƒใƒฃใƒƒใƒˆๅ…จไฝ“ใŒๅ˜ไธ€ใฎๆ–‡ๅญ—ๅˆ—ใซใพใจใ‚ใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ่จญๅฎšใงใ‚ใ‚‹ใ€Œtokenize=Trueใ€ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ +ใใฎๆ–‡ๅญ—ๅˆ—ใ‚‚ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ‚ˆใ‚Š่ค‡้›‘ใชใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒๅฎŸ้š›ใซใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ +ใ€Œmeta-llama/Llama-2-7b-chat-hfใ€ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ใŸใ ใ—ใ€ใ“ใฎใƒขใƒ‡ใƒซใฏใ‚ฒใƒผใƒˆไป˜ใใ‚ขใ‚ฏใ‚ปใ‚นใ‚’ๆŒใฃใฆใŠใ‚Šใ€ +ใ“ใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅ ดๅˆใฏ[ใƒชใƒใ‚ธใƒˆใƒชใงใ‚ขใ‚ฏใ‚ปใ‚นใ‚’ใƒชใ‚ฏใ‚จใ‚นใƒˆ](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +>> from transformers import AutoTokenizer +>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf") + +>> chat = [ +... {"role": "user", "content": "Hello, how are you?"}, +... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, +... {"role": "user", "content": "I'd like to show off how chat templating works!"}, +... ] + +>> tokenizer.use_default_system_prompt = False +>> tokenizer.apply_chat_template(chat, tokenize=False) +"[INST] Hello, how are you? [/INST] I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" +``` + +ไปŠๅ›žใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏๅˆถๅพกใƒˆใƒผใ‚ฏใƒณ [INST] ใจ [/INST] ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใ‚‰ใฏใƒฆใƒผใ‚ถใƒผใƒกใƒƒใ‚ปใƒผใ‚ธใฎ้–‹ๅง‹ใจ็ต‚ไบ†ใ‚’็คบใ™ใŸใ‚ใฎใ‚‚ใฎใงใ™๏ผˆใŸใ ใ—ใ€ใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใƒกใƒƒใ‚ปใƒผใ‚ธใซใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“๏ผ๏ผ‰ + +## How do chat templates work? + +ใƒขใƒ‡ใƒซใฎใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใ€`tokenizer.chat_template`ๅฑžๆ€งใซๆ ผ็ดใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใใฎใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒไปฃใ‚ใ‚Šใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚`BlenderBot`ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†: + +```python + +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") + +>>> tokenizer.chat_template +"{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}" +``` + + +ใ“ใ‚Œใฏๅฐ‘ใ—ๆŠ‘ๅœง็š„ใงใ™ใญใ€‚ๅฏ่ชญๆ€งใ‚’้ซ˜ใ‚ใ‚‹ใŸใ‚ใซใ€ๆ–ฐใ—ใ„่กŒใจใ‚คใƒณใƒ‡ใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใพใ—ใ‚‡ใ†ใ€‚ +ๅ„ใƒ–ใƒญใƒƒใ‚ฏใฎ็›ดๅ‰ใฎ็ฉบ็™ฝใจใ€ใƒ–ใƒญใƒƒใ‚ฏใฎ็›ดๅพŒใฎๆœ€ๅˆใฎๆ”น่กŒใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงJinjaใฎ `trim_blocks` ใŠใ‚ˆใณ `lstrip_blocks` ใƒ•ใƒฉใ‚ฐใ‚’ไฝฟ็”จใ—ใฆๅ‰Š้™คใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚คใƒณใƒ‡ใƒณใƒˆใจๆ”น่กŒใ‚’ๅซใ‚€ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ๆ›ธใ„ใฆใ‚‚ๆญฃๅธธใซๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ ' ' }} + {% endif %} + {{ message['content'] }} + {% if not loop.last %} + {{ ' ' }} + {% endif %} +{% endfor %} +{{ eos_token }} +``` + +ใ“ใ‚ŒใŒๅˆใ‚ใฆ่ฆ‹ใ‚‹ๆ–นใธใ€ใ“ใ‚Œใฏ[Jinjaใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ](https://jinja.palletsprojects.com/en/3.1.x/templates/)ใงใ™ใ€‚ +Jinjaใฏใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใฎใ‚ทใƒณใƒ—ใƒซใชใ‚ณใƒผใƒ‰ใ‚’่จ˜่ฟฐใงใใ‚‹ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ่จ€่ชžใงใ™ใ€‚ๅคšใใฎ็‚นใงใ€ใ‚ณใƒผใƒ‰ใจ +ๆง‹ๆ–‡ใฏPythonใซไผผใฆใ„ใพใ™ใ€‚็ด”็ฒ‹ใชPythonใงใฏใ€ใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚‹ใงใ—ใ‚‡ใ†๏ผš + +```python +for idx, message in enumerate(messages): + if message['role'] == 'user': + print(' ') + print(message['content']) + if not idx == len(messages) - 1: # Check for the last message in the conversation + print(' ') +print(eos_token) +``` + +ๅฎŸ้š›ใซใ€ใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏๆฌกใฎ3ใคใฎใ“ใจใ‚’่กŒใ„ใพใ™๏ผš +1. ๅ„ใƒกใƒƒใ‚ปใƒผใ‚ธใซๅฏพใ—ใฆใ€ใƒกใƒƒใ‚ปใƒผใ‚ธใŒใƒฆใƒผใ‚ถใƒผใƒกใƒƒใ‚ปใƒผใ‚ธใงใ‚ใ‚‹ๅ ดๅˆใ€ใใ‚Œใฎๅ‰ใซ็ฉบ็™ฝใ‚’่ฟฝๅŠ ใ—ใ€ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏไฝ•ใ‚‚่กจ็คบใ—ใพใ›ใ‚“ใ€‚ +2. ใƒกใƒƒใ‚ปใƒผใ‚ธใฎๅ†…ๅฎนใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +3. ใƒกใƒƒใ‚ปใƒผใ‚ธใŒๆœ€ๅพŒใฎใƒกใƒƒใ‚ปใƒผใ‚ธใงใชใ„ๅ ดๅˆใ€ใใฎๅพŒใซ2ใคใฎใ‚นใƒšใƒผใ‚นใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ๆœ€ๅพŒใฎใƒกใƒƒใ‚ปใƒผใ‚ธใฎๅพŒใซใฏEOSใƒˆใƒผใ‚ฏใƒณใ‚’่กจ็คบใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใฏ้žๅธธใซใ‚ทใƒณใƒ—ใƒซใชใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใงใ™ใ€‚ๅˆถๅพกใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ—ใชใ„ใ—ใ€ใƒขใƒ‡ใƒซใซๅฏพใ™ใ‚‹ๆŒ‡็คบใ‚’ไผใˆใ‚‹ไธ€่ˆฌ็š„ใชๆ–นๆณ•ใงใ‚ใ‚‹ใ€Œใ‚ทใ‚นใƒ†ใƒ ใ€ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ใ€‚ +ใŸใ ใ—ใ€Jinjaใฏใ“ใ‚Œใ‚‰ใฎใ“ใจใ‚’่กŒใ†ใŸใ‚ใฎๅคšใใฎๆŸ”่ปŸๆ€งใ‚’ๆไพ›ใ—ใฆใ„ใพใ™๏ผ +LLaMAใŒใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ™ใ‚‹ๆ–นๆณ•ใซ้กžไผผใ—ใŸๅ…ฅๅŠ›ใ‚’ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ™ใ‚‹ใŸใ‚ใฎJinjaใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ† +๏ผˆๅฎŸ้š›ใฎLLaMAใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ทใ‚นใƒ†ใƒ ใƒกใƒƒใ‚ปใƒผใ‚ธใฎๅ‡ฆ็†ใ‚„ใ€ไธ€่ˆฌ็š„ใชใ‚ทใ‚นใƒ†ใƒ ใƒกใƒƒใ‚ปใƒผใ‚ธใฎๅ‡ฆ็†ใŒ่‹ฅๅนฒ็•ฐใชใ‚‹ใŸใ‚ใ€ +ๅฎŸ้š›ใฎใ‚ณใƒผใƒ‰ใงใฏใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ—ใชใ„ใงใใ ใ•ใ„๏ผ๏ผ‰ + + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ bos_token + '[INST] ' + message['content'] + ' [/INST]' }} + {% elif message['role'] == 'system' %} + {{ '<>\\n' + message['content'] + '\\n<>\\n\\n' }} + {% elif message['role'] == 'assistant' %} + {{ ' ' + message['content'] + ' ' + eos_token }} + {% endif %} +{% endfor %} +``` + +้ก˜ใ‚ใใฐใ€ๅฐ‘ใ—่ฆ‹ใคใ‚ใฆใ„ใŸใ ใ‘ใ‚Œใฐใ€ใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒไฝ•ใ‚’่กŒใฃใฆใ„ใ‚‹ใ‹ใŒใ‚ใ‹ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใ€ๅ„ใƒกใƒƒใ‚ปใƒผใ‚ธใฎใ€Œๅฝนๅ‰ฒใ€ใซๅŸบใฅใ„ใฆ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒˆใƒผใ‚ฏใƒณใฏใ€ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’้€ไฟกใ—ใŸไบบใ‚’่กจใ™ใ‚‚ใฎใงใ™ใ€‚ +ใƒฆใƒผใ‚ถใƒผใ€ใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใ€ใŠใ‚ˆใณใ‚ทใ‚นใƒ†ใƒ ใƒกใƒƒใ‚ปใƒผใ‚ธใฏใ€ใใ‚Œใ‚‰ใŒๅซใพใ‚Œใ‚‹ใƒˆใƒผใ‚ฏใƒณใซใ‚ˆใฃใฆใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆๆ˜Ž็ขบใซๅŒบๅˆฅใ•ใ‚Œใพใ™ใ€‚ + + +## How do I create a chat template? + +็ฐกๅ˜ใงใ™ใ€‚ๅ˜็ด”ใซJinjaใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ๆ›ธใ„ใฆใ€`tokenizer.chat_template`ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +ไป–ใฎใƒขใƒ‡ใƒซใ‹ใ‚‰ๆ—ขๅญ˜ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ๅง‹็‚นใซใ—ใฆใ€ๅฟ…่ฆใซๅฟœใ˜ใฆ็ทจ้›†ใ™ใ‚‹ใจไพฟๅˆฉใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผ +ไพ‹ใˆใฐใ€ไธŠ่จ˜ใฎLLaMAใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ๅ–ใฃใฆใ€ใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใƒกใƒƒใ‚ปใƒผใ‚ธใซ"[ASST]"ใจ"[/ASST]"ใ‚’่ฟฝๅŠ ใงใใพใ™ใ€‚ + +``` +{% for message in messages %} + {% if message['role'] == 'user' %} + {{ bos_token + '[INST] ' + message['content'].strip() + ' [/INST]' }} + {% elif message['role'] == 'system' %} + {{ '<>\\n' + message['content'].strip() + '\\n<>\\n\\n' }} + {% elif message['role'] == 'assistant' %} + {{ '[ASST] ' + message['content'] + ' [/ASST]' + eos_token }} + {% endif %} +{% endfor %} +``` + +ๆฌกใซใ€ๅ˜ใซ`tokenizer.chat_template`ๅฑžๆ€งใ‚’่จญๅฎšใ—ใฆใใ ใ•ใ„ใ€‚ +ๆฌกๅ›žใ€[`~PreTrainedTokenizer.apply_chat_template`]ใ‚’ไฝฟ็”จใ™ใ‚‹้š›ใซใ€ๆ–ฐใ—ใ„ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒไฝฟ็”จใ•ใ‚Œใพใ™๏ผ +ใ“ใฎๅฑžๆ€งใฏ`tokenizer_config.json`ใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ•ใ‚Œใ‚‹ใŸใ‚ใ€[`~utils.PushToHubMixin.push_to_hub`]ใ‚’ไฝฟ็”จใ—ใฆ +ๆ–ฐใ—ใ„ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’Hubใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใ€ใฟใ‚“ใชใŒๆญฃใ—ใ„ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใงใใพใ™๏ผ + +```python +template = tokenizer.chat_template +template = template.replace("SYS", "SYSTEM") # Change the system token +tokenizer.chat_template = template # Set the new template +tokenizer.push_to_hub("model_name") # Upload your new template to the Hub! +``` + +[`~PreTrainedTokenizer.apply_chat_template`] ใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ใ‚ใชใŸใฎใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซ `TextGenerationPipeline` ใ‚ฏใƒฉใ‚นใซใ‚ˆใฃใฆๅ‘ผใณๅ‡บใ•ใ‚Œใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ๆญฃใ—ใ„ใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’่จญๅฎšใ™ใ‚‹ใจใ€ใ‚ใชใŸใฎใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซ [`TextGenerationPipeline`] ใจไบ’ๆ›ๆ€งใŒใ‚ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + +## What are "default" templates? + +ใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฎๅฐŽๅ…ฅๅ‰ใซใ€ใƒใƒฃใƒƒใƒˆใฎๅ‡ฆ็†ใฏใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใƒฌใƒ™ใƒซใงใƒใƒผใƒ‰ใ‚ณใƒผใƒ‰ใ•ใ‚Œใฆใ„ใพใ—ใŸใ€‚ +ๅพŒๆ–นไบ’ๆ›ๆ€งใฎใŸใ‚ใซใ€ใ“ใฎใ‚ฏใƒฉใ‚นๅ›บๆœ‰ใฎๅ‡ฆ็†ใ‚’ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใจใ—ใฆไฟๆŒใ—ใ€ใ‚ฏใƒฉใ‚นใƒฌใƒ™ใƒซใง่จญๅฎšใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใซใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใŸใ ใ—ใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใ€ +`TextGenerationPipeline`ใ‚ฏใƒฉใ‚นใ‚„`apply_chat_template`ใชใฉใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ‚ฏใƒฉใ‚นใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€`tokenizer.default_chat_template`ๅฑžๆ€งใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใ‚Œใฏใ€ๅพŒๆ–นไบ’ๆ›ๆ€งใฎใŸใ‚ใซ็ด”็ฒ‹ใซ่กŒใฃใฆใ„ใ‚‹ใ“ใจใงใ€ๆ—ขๅญ˜ใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใ‚’ๅฃŠใ•ใชใ„ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใซใจใฃใฆใ‚ฏใƒฉใ‚นใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒ้ฉๅˆ‡ใงใ‚ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ—ใฆ +`chat_template`ๅฑžๆ€งใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒฆใƒผใ‚ถใƒผใซใจใฃใฆ +ใƒขใƒ‡ใƒซใŒใƒใƒฃใƒƒใƒˆ็”จใซๆญฃใ—ใๆง‹ๆˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒๆ˜Ž็ขบใซใชใ‚Šใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒๅค‰ๆ›ดใ•ใ‚ŒใŸใ‚Šๅปƒๆญขใ•ใ‚ŒใŸๅ ดๅˆใซๅ‚™ใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## What template should I use? + +ใ™ใงใซใƒใƒฃใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ—ใ‘ใŸใƒขใƒ‡ใƒซใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’่จญๅฎšใ™ใ‚‹ๅ ดๅˆใ€ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒขใƒ‡ใƒซใŒ่ฆ‹ใŸใƒกใƒƒใ‚ปใƒผใ‚ธใฎใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใจใพใฃใŸใไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใใ†ใงใชใ„ๅ ดๅˆใ€ๆ€ง่ƒฝใฎไฝŽไธ‹ใ‚’็ตŒ้จ“ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใงใ™ใ€‚ใ“ใ‚Œใฏใƒขใƒ‡ใƒซใ‚’ใ•ใ‚‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ๅŒๆง˜ใงใ™ - ใƒใƒฃใƒƒใƒˆใƒˆใƒผใ‚ฏใƒณใ‚’ไธ€ๅฎšใซไฟใคใจใ€ใŠใใ‚‰ใๆœ€้ซ˜ใฎๆ€ง่ƒฝใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใฏใƒˆใƒผใ‚ฏใƒณๅŒ–ใจ้žๅธธใซ้กžไผผใ—ใฆใŠใ‚Šใ€้€šๅธธใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซไฝฟ็”จใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณๅŒ–ใจๆญฃ็ขบใซไธ€่‡ดใ™ใ‚‹ๅ ดๅˆใซใ€ๆŽจ่ซ–ใพใŸใฏใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใฎ้š›ใซๆœ€่‰ฏใฎๆ€ง่ƒฝใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +ไธ€ๆ–นใ€ใ‚ผใƒญใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ‹ใ€ใƒใƒฃใƒƒใƒˆใฎใŸใ‚ใซใƒ™ใƒผใ‚น่จ€่ชžใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€้ฉๅˆ‡ใชใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’้ธๆŠžใ™ใ‚‹่‡ช็”ฑๅบฆใŒใ‚ใ‚Šใพใ™ใ€‚ +LLM๏ผˆLanguage Model๏ผ‰ใฏใ•ใพใ–ใพใชๅ…ฅๅŠ›ๅฝขๅผใ‚’ๅ‡ฆ็†ใงใใ‚‹ใปใฉใ‚นใƒžใƒผใƒˆใงใ™ใ€‚ใ‚ฏใƒฉใ‚นๅ›บๆœ‰ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใŒใชใ„ใƒขใƒ‡ใƒซ็”จใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใ€ไธ€่ˆฌ็š„ใชใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซๅฏพใ—ใฆ่‰ฏใ„ๆŸ”่ปŸใช้ธๆŠž่‚ขใงใ™ใ€‚ +ใ“ใ‚Œใฏใ€`ChatMLใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆ`ใซๅพ“ใฃใŸใ‚‚ใฎใงใ€ๅคšใใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + +``` +{% for message in messages %} + {{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}} +{% endfor %} +``` + +If you like this one, here it is in one-liner form, ready to copy into your code: + +```python +tokenizer.chat_template = "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}" +``` + +ใ“ใฎใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏใ€ๅ„ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ใ€Œ``ใ€ใƒˆใƒผใ‚ฏใƒณใงๅ›ฒใฟใ€ๅฝนๅ‰ฒใ‚’ๆ–‡ๅญ—ๅˆ—ใจใ—ใฆๅ˜็ด”ใซ่จ˜่ฟฐใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงไฝฟ็”จใ™ใ‚‹ๅฝนๅ‰ฒใซๅฏพใ™ใ‚‹ๆŸ”่ปŸๆ€งใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ๅ‡บๅŠ›ใฏไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +``` +<|im_start|>system +You are a helpful chatbot that will do its best not to say anything so stupid that people tweet about it.<|im_end|> +<|im_start|>user +How are you?<|im_end|> +<|im_start|>assistant +I'm doing great!<|im_end|> +``` + +ใ€Œใƒฆใƒผใ‚ถใƒผใ€ใ€ใ€Œใ‚ทใ‚นใƒ†ใƒ ใ€ใ€ใŠใ‚ˆใณใ€Œใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใ€ใฎๅฝนๅ‰ฒใฏใ€ใƒใƒฃใƒƒใƒˆใฎๆจ™ๆบ–ใงใ™ใ€‚ +็‰นใซใ€`TextGenerationPipeline`ใจใฎ้€ฃๆบใ‚’ใ‚นใƒ ใƒผใ‚บใซ่กŒใ†ๅ ดๅˆใซใฏใ€ใ“ใ‚Œใ‚‰ใฎๅฝนๅ‰ฒใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใ‚Œใ‚‰ใฎๅฝนๅ‰ฒใซๅˆถ็ด„ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใฏ้žๅธธใซๆŸ”่ปŸใงใ€ไปปๆ„ใฎๆ–‡ๅญ—ๅˆ—ใ‚’ๅฝนๅ‰ฒใจใ—ใฆไฝฟ็”จใงใใพใ™ใ€‚ + +## I want to use chat templates! How should I get started? + +ใƒใƒฃใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’ๆŒใฃใฆใ„ใ‚‹ๅ ดๅˆใ€ใใฎใƒขใƒ‡ใƒซใฎ`tokenizer.chat_template`ๅฑžๆ€งใ‚’่จญๅฎšใ—ใ€[`~PreTrainedTokenizer.apply_chat_template`]ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚นใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใฏใƒขใƒ‡ใƒซใฎๆ‰€ๆœ‰่€…ใงใชใ„ๅ ดๅˆใงใ‚‚้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใŒ็ฉบใฎใƒใƒฃใƒƒใƒˆใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใพใŸใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฏใƒฉใ‚นใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ +ใ“ใฎๅฑžๆ€งใ‚’้ฉๅˆ‡ใซ่จญๅฎšใงใใ‚‹ใ‚ˆใ†ใซ[ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆ](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)ใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ + +ไธ€ๅบฆๅฑžๆ€งใŒ่จญๅฎšใ•ใ‚Œใ‚Œใฐใ€ใใ‚ŒใงๅฎŒไบ†ใงใ™๏ผ `tokenizer.apply_chat_template`ใฏใ€ใใฎใƒขใƒ‡ใƒซใซๅฏพใ—ใฆๆญฃใ—ใๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€ +`TextGenerationPipeline` ใชใฉใฎๅ ดๆ‰€ใงใ‚‚่‡ชๅ‹•็š„ใซใ‚ตใƒใƒผใƒˆใ•ใ‚Œใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใŒใ“ใฎๅฑžๆ€งใ‚’ๆŒใคใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใงใ€ใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใƒขใƒ‡ใƒซใฎๅ…จใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใŒใใฎใƒ•ใƒซใƒ‘ใƒฏใƒผใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใฎไธไธ€่‡ดใฏใ“ใฎๅˆ†้‡Žใซๆ‚ฉใฟ็ถšใ‘ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซ้ป™ใฃใฆๅฝฑ้Ÿฟใ‚’ไธŽใˆใฆใใพใ—ใŸใ€‚ใใ‚Œใ‚’็ต‚ใ‚ใ‚‰ใ›ใ‚‹ๆ™‚ใŒๆฅใพใ—ใŸ๏ผ + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/community.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/community.md new file mode 100644 index 0000000000000000000000000000000000000000..ffe28d042d237e6ddfaeec7d554b690d140a7411 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/community.md @@ -0,0 +1,69 @@ + + +# Community + +ใ“ใฎใƒšใƒผใ‚ธใฏใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซใ‚ˆใฃใฆ้–‹็™บใ•ใ‚ŒใŸ๐Ÿค— Transformersใซ้–ขใ™ใ‚‹ใƒชใ‚ฝใƒผใ‚นใ‚’ใพใจใ‚ใŸใ‚‚ใฎใงใ™ใ€‚ + +## Community resources: + +| ใƒชใ‚ฝใƒผใ‚น | ่ชฌๆ˜Ž | ไฝœ่€… | +|:----------|:-------------|------:| +| [Hugging Face Transformers Glossary Flashcards](https://www.darigovresearch.com/huggingface-transformers-glossary-flashcards) | [Transformers Docs Glossary](glossary)ใซๅŸบใฅใ„ใŸใƒ•ใƒฉใƒƒใ‚ทใƒฅใ‚ซใƒผใƒ‰ใ‚ปใƒƒใƒˆใงใ™ใ€‚ใ“ใฎใ‚ปใƒƒใƒˆใฏใ€้•ทๆœŸใฎ็Ÿฅ่ญ˜ๅฎš็€ใ‚’็‰นใซ่€ƒๆ…ฎใ—ใฆ่จญ่จˆใ•ใ‚ŒใŸใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใฎใ‚ฏใƒญใ‚นใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ใ‚ขใƒ—ใƒชใงใ‚ใ‚‹[Anki](https://apps.ankiweb.net/)ใ‚’ไฝฟ็”จใ—ใฆ็ฐกๅ˜ใซๅญฆ็ฟ’/ๅพฉ็ฟ’ใงใใ‚‹ๅฝขๅผใซใชใฃใฆใ„ใพใ™ใ€‚[ใƒ•ใƒฉใƒƒใ‚ทใƒฅใ‚ซใƒผใƒ‰ใฎไฝฟ็”จๆ–นๆณ•ใซ้–ขใ™ใ‚‹็ดนไป‹ใƒ“ใƒ‡ใ‚ชใฏใ“ใกใ‚‰](https://www.youtube.com/watch?v=Dji_h7PILrw)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ | [Darigov Research](https://www.darigovresearch.com/) | + +## Community notebooks: + +| ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ | ่ชฌๆ˜Ž | ่‘—่€… | | +|:----------|:-------------|:-------------|------:| +| [ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎTransformerใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆๆญŒ่ฉžใ‚’็”Ÿๆˆ](https://github.com/AlekseyKorshuk/huggingartists) | GPT-2ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใŠๆฐ—ใซๅ…ฅใ‚Šใฎใ‚ขใƒผใƒ†ใ‚ฃใ‚นใƒˆใฎใ‚นใ‚ฟใ‚คใƒซใงๆญŒ่ฉžใ‚’็”Ÿๆˆใ™ใ‚‹ๆ–นๆณ• | [Aleksey Korshuk](https://github.com/AlekseyKorshuk) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AlekseyKorshuk/huggingartists/blob/master/huggingartists-demo.ipynb) | +| [Tensorflow 2ใงT5ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/snapthat/TF-T5-text-to-text) | Tensorflow 2ใ‚’ไฝฟ็”จใ—ใฆไปปๆ„ใฎใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆT5ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ€‚ใ“ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏTensorflow 2ใ‚’ไฝฟ็”จใ—ใฆSQUADใงๅฎŸ่ฃ…ใ•ใ‚ŒใŸ่ณชๅ•ใจๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ | [Muhammad Harris](https://github.com/HarrisDePerceptron) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/snapthat/TF-T5-text-to-text/blob/master/snapthatT5/notebooks/TF-T5-Datasets%20Training.ipynb) | +| [TPUใงT5ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb) | TransformersใจNlpใ‚’ไฝฟ็”จใ—ใฆSQUADใงT5ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Suraj Patil](https://github.com/patil-suraj) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb#scrollTo=QLGiFCDqvuil) | +| [ๅˆ†้กžใจๅคš่‚ข้ธๆŠžใฎใŸใ‚ใซT5ใ‚’ๅพฎ่ชฟๆ•ด](https://github.com/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | PyTorch Lightningใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆๅฏพใƒ†ใ‚ญใ‚นใƒˆๅฝขๅผใงT5ใ‚’ๅˆ†้กžใจๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏใซๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• | [Suraj Patil](https://github.com/patil-suraj) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | +| [ๆ–ฐใ—ใ„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใจ่จ€่ชžใงDialoGPTใ‚’ๅพฎ่ชฟๆ•ด](https://github.com/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | DialoGPTใƒขใƒ‡ใƒซใ‚’ๆ–ฐใ—ใ„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ‚ชใƒผใƒ—ใƒณใƒ€ใ‚คใ‚ขใƒญใ‚ฐไผš่ฉฑ็”จใฎๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• | [Nathan Cooper](https://github.com/ncoop57) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | +| [Reformerใ‚’ไฝฟ็”จใ—ใŸ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใƒขใƒ‡ใƒชใƒณใ‚ฐ](https://github.com/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | Reformerใ‚’ไฝฟ็”จใ—ใฆ500,000ใƒˆใƒผใ‚ฏใƒณใพใงใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | +| [่ฆ็ด„ใฎใŸใ‚ใซBARTใ‚’ๅพฎ่ชฟๆ•ด](https://github.com/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb) | Blurrใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„ใฎใŸใ‚ใซBARTใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• | [Wayde Gilliam](https://ohmeow.com/) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb) | +| [ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎTransformerใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆ่ชฐใ‹ใฎใƒ„ใ‚คใƒผใƒˆใ‚’็”Ÿๆˆ](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | GPT-2ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใŠๆฐ—ใซๅ…ฅใ‚ŠใฎTwitterใ‚ขใ‚ซใ‚ฆใƒณใƒˆใฎใ‚นใ‚ฟใ‚คใƒซใงใƒ„ใ‚คใƒผใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ๆ–นๆณ• | [Boris Dayma](https://github.com/borisdayma) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | +| [๐Ÿค— Hugging Faceใƒขใƒ‡ใƒซใ‚’Weights & Biasesใงๆœ€้ฉๅŒ–](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | Hugging FaceใจWeights & Biasesใฎ็ตฑๅˆใ‚’็คบใ™ๅฎŒๅ…จใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ | [Boris Dayma](https://github.com/borisdayma) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | +| [Longformerใฎไบ‹ๅ‰ๅญฆ็ฟ’](https://github.com/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | ๆ—ขๅญ˜ใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใ€Œ้•ทใ„ใ€ใƒใƒผใ‚ธใƒงใƒณใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ๆ–นๆณ• | [Iz Beltagy](https://beltagy.net) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | +| [QAใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซLongformerใ‚’ๅพฎ่ชฟๆ•ด](https://github.com/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | QAใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซLongformerใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• | [Suraj Patil](https://github.com/patil-suraj) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | +| [๐Ÿค—nlpใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎ่ฉ•ไพก](https://github.com/patrickvonplaten/notebooks/blob/master/How_to_evaluate_Longformer_on_TriviaQA_using_NLP.ipynb) | `nlp`ใ‚’ไฝฟ็”จใ—ใฆTriviaQAใงLongformerใ‚’่ฉ•ไพกใ™ใ‚‹ๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1m7eTGlPmLRgoPkkA7rkhQdZ9ydpmsdLE?usp=sharing) | +| [ๆ„Ÿๆƒ…ใ‚นใƒ‘ใƒณๆŠฝๅ‡บใฎใŸใ‚ใซT5ใ‚’ๅพฎ่ชฟๆ•ด](https://github.com/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | PyTorch Lightningใ‚’ไฝฟ็”จใ—ใฆๆ„Ÿๆƒ…ใ‚นใƒ‘ใƒณๆŠฝๅ‡บใฎใŸใ‚ใซT5ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• | [Lorenzo Ampil](https://github.com/enzoampil) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | +| [DistilBertใ‚’ใƒžใƒซใƒใ‚ฏใƒฉใ‚นๅˆ†้กžใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb) | PyTorchใ‚’ไฝฟ็”จใ—ใฆDistilBertใ‚’ใƒžใƒซใƒใ‚ฏใƒฉใ‚นๅˆ†้กžใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb)| +|[BERTใ‚’ใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)|PyTorchใ‚’ไฝฟ็”จใ—ใฆBERTใ‚’ใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)| +|[T5ใ‚’่ฆ็ด„ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb)|PyTorchใ‚’ไฝฟ็”จใ—ใฆT5ใ‚’่ฆ็ด„ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใ€WandBใงๅฎŸ้จ“ใ‚’ใƒˆใƒฉใƒƒใ‚ญใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Abhishek Kumar Mishra](https://github.com/abhimishra91) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb)| +|[ใƒ€ใ‚คใƒŠใƒŸใƒƒใ‚ฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ/ใƒใ‚ฑใƒƒใƒ†ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆTransformersใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–](https://github.com/ELS-RD/transformers-notebook/blob/master/Divide_Hugging_Face_Transformers_training_time_by_2_or_more.ipynb)|ใƒ€ใ‚คใƒŠใƒŸใƒƒใ‚ฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ/ใƒใ‚ฑใƒƒใƒ†ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ‚’2ๅ€้ซ˜้€ŸๅŒ–ใ™ใ‚‹ๆ–นๆณ•|[Michael Benesty](https://github.com/pommedeterresautee) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1CBfRU1zbfu7-ijiOqAAQUA-RJaxfcJoO?usp=sharing)| +|[ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎใŸใ‚ใฎReformerใฎไบ‹ๅ‰ๅญฆ็ฟ’](https://github.com/patrickvonplaten/notebooks/blob/master/Reformer_For_Masked_LM.ipynb)|ๅŒๆ–นๅ‘ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒฌใ‚คใƒคใƒผใ‚’ๅ‚™ใˆใŸReformerใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•|[Patrick von Platen](https://github.com/patrickvonplaten) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1tzzh0i8PgDQGV3SMFUGxM7_gGae3K-uW?usp=sharing)| +|[Sci-BERTใ‚’ๆ‹กๅผตใ—ใฆใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/lordtt13/word-embeddings/blob/master/COVID-19%20Research%20Data/COVID-SciBERT.ipynb)|AllenAIใฎCORDใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎSciBERTใƒขใƒ‡ใƒซใฎ่ชžๅฝ™ใ‚’ๆ‹กๅผตใ—ใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณๅŒ–ใ™ใ‚‹ๆ–นๆณ•|[Tanmay Thakur](https://github.com/lordtt13) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1rqAR40goxbAfez1xvF3hBJphSCsvXmh8)| +|[Trainer APIใ‚’ไฝฟ็”จใ—ใฆBlenderBotSmallใ‚’่ฆ็ด„ใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/lordtt13/transformers-experiments/blob/master/Custom%20Tasks/fine-tune-blenderbot_small-for-summarization.ipynb)|ใ‚ซใ‚นใ‚ฟใƒ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงBlenderBotSmallใ‚’่ฆ็ด„ใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ€Trainer APIใ‚’ไฝฟ็”จ|[Tanmay Thakur](https://github.com/lordtt13) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19Wmupuls7mykSGyRN_Qo6lPQhgp56ymq?usp=sharing)| +|[Electraใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใฆCaptum Integrated Gradientsใง่งฃ้‡ˆ](https://github.com/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb) |Electraใ‚’ๆ„Ÿๆƒ…ๅˆ†ๆžใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใ€Captum Integrated Gradientsใงไบˆๆธฌใ‚’่งฃ้‡ˆใ™ใ‚‹ๆ–นๆณ•|[Eliza Szczechla](https://elsanns.github.io) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb)| +|[Trainerใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆ้ž่‹ฑ่ชžใฎGPT-2ใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb) |Trainerใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆ้ž่‹ฑ่ชžใฎGPT-2ใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Philipp Schmid](https://www.philschmid.de) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb)| +|[DistilBERTใƒขใƒ‡ใƒซใ‚’ใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb) |DistilBERTใƒขใƒ‡ใƒซใ‚’ใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Dhaval Taunk](https://github.com/DhavalTaunk08) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb)| +|[ALBERTใ‚’ๆ–‡ใƒšใ‚ขๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb) |ALBERTใƒขใƒ‡ใƒซใพใŸใฏไป–ใฎBERTใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใ‚’ๆ–‡ใƒšใ‚ขๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Nadir El Manouzi](https://github.com/NadirEM) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb)| +|[RoBERTaใ‚’ๆ„Ÿๆƒ…ๅˆ†ๆžใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb) |RoBERTaใƒขใƒ‡ใƒซใ‚’ๆ„Ÿๆƒ…ๅˆ†ๆžใฎใŸใ‚ใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•|[Dhaval Taunk](https://github.com/DhavalTaunk08) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb)| +|[่ณชๅ•็”Ÿๆˆใƒขใƒ‡ใƒซใฎ่ฉ•ไพก](https://github.com/flexudy-pipe/qugeev) | seq2seqใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆ็”Ÿๆˆใ•ใ‚ŒใŸ่ณชๅ•ใฎๅ›ž็ญ”ใฎๆญฃ็ขบใ•ใ‚’่ฉ•ไพกใ™ใ‚‹ๆ–นๆณ• | [Pascal Zoleko](https://github.com/zolekode) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bpsSqCQU-iw_5nNoRm_crPq6FRuJthq_?usp=sharing)| +|[DistilBERTใจTensorflowใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅˆ†้กž](https://github.com/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb) | TensorFlowใงใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใฎใŸใ‚ใซDistilBERTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Peter Bayerle](https://github.com/peterbayerle) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb)| +|[CNN/Dailymailใงใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผ่ฆ็ด„ใซBERTใ‚’ๆดป็”จ](https://github.com/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb) | *google-bert/bert-base-uncased* ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆCNN/Dailymailใฎ่ฆ็ด„ใฎใŸใ‚ใซ *EncoderDecoderModel* ใ‚’ใ‚ฆใ‚ฉใƒผใƒ ใ‚นใ‚ฟใƒผใƒˆใ™ใ‚‹ๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)| +|[BBC XSumใงใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผ่ฆ็ด„ใซRoBERTaใ‚’ๆดป็”จ](https://github.com/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb) | *FacebookAI/roberta-base* ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆBBC/XSumใฎ่ฆ็ด„ใฎใŸใ‚ใฎๅ…ฑๆœ‰ *EncoderDecoderModel* ใ‚’ใ‚ฆใ‚ฉใƒผใƒ ใ‚นใ‚ฟใƒผใƒˆใ™ใ‚‹ๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb)| +|[TAPASใ‚’ใ‚ทใƒผใ‚ฑใƒณใ‚ทใƒฃใƒซ่ณชๅ•ๅฟœ็ญ”๏ผˆSQA๏ผ‰ใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) | ใ‚ทใƒผใ‚ฑใƒณใ‚ทใƒฃใƒซ่ณชๅ•ๅฟœ็ญ”๏ผˆSQA๏ผ‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง *tapas-base* ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆ *TapasForQuestionAnswering* ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb)| +|[TabFactใงTAPASใ‚’่ฉ•ไพก](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb) | *tapas-base-finetuned-tabfact* ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ *TapasForSequenceClassification* ใ‚’่ฉ•ไพกใ™ใ‚‹ๆ–นๆณ•ใ€๐Ÿค— datasets ใจ ๐Ÿค— transformers ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จ | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb)| +|[็ฟป่จณใฎใŸใ‚ใฎmBARTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb) | Seq2SeqTrainerใ‚’ไฝฟ็”จใ—ใฆHindiใ‹ใ‚‰Englishใธใฎ็ฟป่จณใฎใŸใ‚ใซmBARTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)| +|[FUNSD๏ผˆใƒ•ใ‚ฉใƒผใƒ ็†่งฃใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ๏ผ‰ใงLayoutLMใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb) | ใ‚นใ‚ญใƒฃใƒณใ•ใ‚ŒใŸใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‹ใ‚‰ใฎๆƒ…ๅ ฑๆŠฝๅ‡บใฎใŸใ‚ใซFUNSDใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง *LayoutLMForTokenClassification* ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb)| +| [DistilGPT2ใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใจใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb) | DistilGPT2ใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใจใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆ–นๆณ• | [Aakash Tripathi](https://github.com/tripathiaakash) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb)| +| [ๆœ€ๅคง8Kใƒˆใƒผใ‚ฏใƒณใงใฎLEDใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb) | ใƒญใƒณใ‚ฐใƒฌใƒณใ‚ธ่ฆ็ด„ใฎใŸใ‚ใฎpubmedใงLEDใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb)| +| [ArxivใงใฎLEDใฎ่ฉ•ไพก](https://github.com/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb) | ใƒญใƒณใ‚ฐใƒฌใƒณใ‚ธ่ฆ็ด„ใฎใŸใ‚ใฎLEDใฎๅŠนๆžœ็š„ใช่ฉ•ไพกๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb)| +| [RVL-CDIP๏ผˆๆ–‡ๆ›ธ็”ปๅƒๅˆ†้กžใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ๏ผ‰ใงใฎLayoutLMใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb) | ใ‚นใ‚ญใƒฃใƒณใ•ใ‚ŒใŸๆ–‡ๆ›ธใฎๅˆ†้กžใฎใŸใ‚ใฎRVL-CDIPใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*LayoutLMForSequenceClassification*ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/nielsrogge) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb)| +| [Wav2Vec2 CTCใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใจGPT2ใฎ่ชฟๆ•ด](https://github.com/voidful/huggingface_notebook/blob/main/xlsr_gpt.ipynb) | ่จ€่ชžใƒขใƒ‡ใƒซใฎ่ชฟๆ•ดใ‚’ไผดใ†CTCใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆ–นๆณ• | [Eric Lam](https://github.com/voidful) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing)| +| [Trainerใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใŸ2่จ€่ชžใฎ่ฆ็ด„็”จใซBARTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb) | ใƒˆใƒฌใƒผใƒŠใƒผใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆ2ใคใฎ่จ€่ชžใงใฎ่ฆ็ด„็”จใซBARTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Eliza Szczechla](https://github.com/elsanns) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)| +| [PubMedใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงBigBirdใฎ่ฉ•ไพก](https://github.com/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb) | Trivia QAใฎ้•ทใ„ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ่ณชๅ•ๅฟœ็ญ”ใงBigBirdใฎ่ฉ•ไพกๆ–นๆณ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb)| +| [Wav2Vec2ใ‚’ไฝฟ็”จใ—ใฆใƒ“ใƒ‡ใ‚ชใฎๅญ—ๅน•ใ‚’ไฝœๆˆใ™ใ‚‹](https://github.com/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | Wav2Vecใงใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚’่ปข่จ˜ใ—ใฆไปปๆ„ใฎใƒ“ใƒ‡ใ‚ชใ‹ใ‚‰YouTubeใฎๅญ—ๅน•ใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ• | [Niklas Muennighoff](https://github.com/Muennighoff) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | +| [PyTorch Lightningใ‚’ไฝฟ็”จใ—ใŸCIFAR-10ใงใฎVision Transformerใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | HuggingFace Transformersใ€Datasetsใ€ใŠใ‚ˆใณPyTorch Lightningใ‚’ไฝฟ็”จใ—ใฆCIFAR-10ใงVision Transformer๏ผˆViT๏ผ‰ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/nielsrogge) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | +| [๐Ÿค— Trainerใ‚’ไฝฟ็”จใ—ใŸCIFAR-10ใงใฎVision Transformerใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | HuggingFace Transformersใ€Datasetsใ€ใŠใ‚ˆใณ๐Ÿค— Trainerใ‚’ไฝฟ็”จใ—ใฆCIFAR-10ใงVision Transformer๏ผˆViT๏ผ‰ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/nielsrogge) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | +| [Open Entityใ€ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใ‚ฟใ‚คใƒ”ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงLUKEใฎ่ฉ•ไพก](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | Open Entityใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*LukeForEntityClassification*ใฎ่ฉ•ไพกๆ–นๆณ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | +| [TACREDใ€้–ขไฟ‚ๆŠฝๅ‡บใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงLUKEใฎ่ฉ•ไพก](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | TACREDใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*LukeForEntityPairClassification*ใฎ่ฉ•ไพกๆ–นๆณ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | +| [CoNLL-2003ใ€้‡่ฆใชNERใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงLUKEใฎ่ฉ•ไพก](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | CoNLL-2003ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*LukeForEntitySpanClassification*ใฎ่ฉ•ไพกๆ–นๆณ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | +| [PubMedใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงBigBird-Pegasusใฎ่ฉ•ไพก](https://github.com/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | PubMedใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*BigBirdPegasusForConditionalGeneration*ใฎ่ฉ•ไพกๆ–นๆณ• | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | +| [Wav2Vec2ใ‚’ไฝฟ็”จใ—ใŸใ‚นใƒ”ใƒผใƒใ‚จใƒขใƒผใ‚ทใƒงใƒณๅˆ†้กž](https://github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | MEGAใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฎๆ„Ÿๆƒ…ๅˆ†้กžใฎใŸใ‚ใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟWav2Vec2ใƒขใƒ‡ใƒซใฎๅˆฉ็”จๆ–นๆณ• | [Mehrdad Farahani](https://github.com/m3hrdadfi) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | +| [DETRใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใ™ใ‚‹](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ*DetrForObjectDetection*ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใ—ใ€ๆณจๆ„ใ‚’ๅฏ่ฆ–ๅŒ–ใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/NielsRogge) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | +| [ใ‚ซใ‚นใ‚ฟใƒ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงDETRใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | ใ‚ซใ‚นใ‚ฟใƒ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง*DetrForObjectDetection*ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Niels Rogge](https://github.com/NielsRogge) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | +| [Named Entity RecognitionใฎใŸใ‚ใซT5ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐ](https://github.com/ToluClassics/Notebooks/blob/main/T5_Ner_Finetuning.ipynb) | Named Entity Recognitionใ‚ฟใ‚นใ‚ฏใงT5ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ• | [Ogundepo Odunayo](https://github.com/ToluClassics) | [![Colabใง้–‹ใ](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1obr78FY_cBmWY5ODViCmzdY6O1KB65Vc?usp=sharing) | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/create_a_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/create_a_model.md new file mode 100644 index 0000000000000000000000000000000000000000..fdb23f98e7b1077631f034290f69718421749e1a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/create_a_model.md @@ -0,0 +1,420 @@ + + +# Create a custom architecture + +[`AutoClass`](model_doc/auto)ใฏใ€ใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’่‡ชๅ‹•็š„ใซๆŽจ่ซ–ใ—ใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ่จญๅฎšใจ้‡ใฟใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ไธ€่ˆฌ็š„ใซใฏใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซไพๅญ˜ใ—ใชใ„ใ‚ณใƒผใƒ‰ใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซ`AutoClass`ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใŸใ ใ—ใ€็‰นๅฎšใฎใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใซๅฏพใ™ใ‚‹ๅˆถๅพกใ‚’ใ‚ˆใ‚Š่ฉณ็ดฐใซ่กŒใ„ใŸใ„ใƒฆใƒผใ‚ถใƒผใฏใ€ใ„ใใคใ‹ใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใ‹ใ‚‰ใ‚ซใ‚นใ‚ฟใƒ ๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’็ ”็ฉถใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€ใพใŸใฏๅฎŸ้จ“ใ™ใ‚‹่ˆˆๅ‘ณใŒใ‚ใ‚‹ใƒฆใƒผใ‚ถใƒผใซ็‰นใซๅฝน็ซ‹ใคใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€`AutoClass`ใ‚’ไฝฟ็”จใ—ใชใ„ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใฎไฝœๆˆใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆใณใพใ™๏ผš + +- ใƒขใƒ‡ใƒซใฎ่จญๅฎšใ‚’ใƒญใƒผใƒ‰ใŠใ‚ˆใณใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ™ใ‚‹ใ€‚ +- ใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝœๆˆใ™ใ‚‹ใ€‚ +- ใƒ†ใ‚ญใ‚นใƒˆ็”จใฎ้…ใ„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจ้ซ˜้€Ÿใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝœๆˆใ™ใ‚‹ใ€‚ +- ใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏ็”จใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝœๆˆใ™ใ‚‹ใ€‚ +- ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ฟใ‚นใ‚ฏ็”จใฎ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ไฝœๆˆใ™ใ‚‹ใ€‚ +- ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏ็”จใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝœๆˆใ™ใ‚‹ใ€‚ + +## Configuration + +[่จญๅฎš](main_classes/configuration)ใฏใ€ใƒขใƒ‡ใƒซใฎ็‰นๅฎšใฎๅฑžๆ€งใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ๅ„ใƒขใƒ‡ใƒซใฎ่จญๅฎšใซใฏ็•ฐใชใ‚‹ๅฑžๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ใ™ในใฆใฎNLPใƒขใƒ‡ใƒซใซใฏใ€`hidden_size`ใ€`num_attention_heads`ใ€`num_hidden_layers`ใ€ใŠใ‚ˆใณ`vocab_size`ๅฑžๆ€งใŒๅ…ฑ้€šใ—ใฆใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅฑžๆ€งใฏใ€ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใฎๆณจๆ„ใƒ˜ใƒƒใƒ‰ใฎๆ•ฐใ‚„้š ใ‚Œๅฑคใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +[DistilBERT](model_doc/distilbert)ใ‚’ใ‚ˆใ‚Š่ฉณใ—ใ่ชฟในใ‚‹ใŸใ‚ใซใ€[`DistilBertConfig`]ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใใฎๅฑžๆ€งใ‚’่ชฟในใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```py +>>> from transformers import DistilBertConfig + +>>> config = DistilBertConfig() +>>> print(config) +DistilBertConfig { + "activation": "gelu", + "attention_dropout": 0.1, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +[`DistilBertConfig`]ใฏใ€ๅŸบๆœฌใฎ[`DistilBertModel`]ใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅฑžๆ€งใ‚’่กจ็คบใ—ใพใ™ใ€‚ +ใ™ในใฆใฎๅฑžๆ€งใฏใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บๅฏ่ƒฝใงใ€ๅฎŸ้จ“ใฎใŸใ‚ใฎใ‚นใƒšใƒผใ‚นใ‚’ๆไพ›ใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ—ใฆไปฅไธ‹ใฎใ‚ˆใ†ใชใ“ใจใŒใงใใพใ™๏ผš + +- `activation`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใง็•ฐใชใ‚‹ๆดปๆ€งๅŒ–้–ขๆ•ฐใ‚’่ฉฆใ™ใ€‚ +- `attention_dropout`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใงๆณจๆ„็ขบ็އใฎ้ซ˜ใ„ใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆ็އใ‚’ไฝฟ็”จใ™ใ‚‹ใ€‚ + + +```py +>>> my_config = DistilBertConfig(activation="relu", attention_dropout=0.4) +>>> print(my_config) +DistilBertConfig { + "activation": "relu", + "attention_dropout": 0.4, + "dim": 768, + "dropout": 0.1, + "hidden_dim": 3072, + "initializer_range": 0.02, + "max_position_embeddings": 512, + "model_type": "distilbert", + "n_heads": 12, + "n_layers": 6, + "pad_token_id": 0, + "qa_dropout": 0.1, + "seq_classif_dropout": 0.2, + "sinusoidal_pos_embds": false, + "transformers_version": "4.16.2", + "vocab_size": 30522 +} +``` + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎๅฑžๆ€งใฏใ€[`~PretrainedConfig.from_pretrained`] ้–ขๆ•ฐใงๅค‰ๆ›ดใงใใพใ™๏ผš + +```py +>>> my_config = DistilBertConfig.from_pretrained("distilbert/distilbert-base-uncased", activation="relu", attention_dropout=0.4) +``` + +Once you are satisfied with your model configuration, you can save it with [`PretrainedConfig.save_pretrained`]. Your configuration file is stored as a JSON file in the specified save directory. + +```py +>>> my_config.save_pretrained(save_directory="./your_model_save_path") +``` + +่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ๅ†ๅˆฉ็”จใ™ใ‚‹ใซใฏใ€[`~PretrainedConfig.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใใ‚Œใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json") +``` + + + +ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’่พžๆ›ธใจใ—ใฆไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‚‚ใ€ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆๅฑžๆ€งใจใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๆง‹ๆˆๅฑžๆ€งใฎ้•ใ„ใ ใ‘ใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผ่ฉณ็ดฐใซใคใ„ใฆใฏ[configuration](main_classes/configuration)ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +## Model + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใฏใ€[ใƒขใƒ‡ใƒซ](main_classes/models)ใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซ๏ผˆใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใ‚‚็ทฉใ่จ€ใ‚ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ‰ใฏใ€ๅ„ใƒฌใ‚คใƒคใƒผใŒไฝ•ใ‚’ใ—ใฆใ„ใ‚‹ใ‹ใ€ใฉใฎๆ“ไฝœใŒ่กŒใ‚ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๆง‹ๆˆใ‹ใ‚‰ใฎ `num_hidden_layers` ใฎใ‚ˆใ†ใชๅฑžๆ€งใฏใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๅฎš็พฉใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏ [`PreTrainedModel`] ใ‚’ใƒ™ใƒผใ‚นใ‚ฏใƒฉใ‚นใจใ—ใ€ๅ…ฅๅŠ›ๅŸ‹ใ‚่พผใฟใฎใƒชใ‚ตใ‚คใ‚บใ‚„ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒ˜ใƒƒใƒ‰ใฎใƒ—ใƒซใƒผใƒ‹ใƒณใ‚ฐใชใฉใ€ๅ…ฑ้€šใฎใƒกใ‚ฝใƒƒใƒ‰ใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ +ใ•ใ‚‰ใซใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏ [`torch.nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html)ใ€[`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)ใ€ใพใŸใฏ [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html) ใฎใ„ใšใ‚Œใ‹ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใคใพใ‚Šใ€ใƒขใƒ‡ใƒซใฏใใ‚Œใžใ‚Œใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎไฝฟ็”จๆณ•ใจไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ใƒขใƒ‡ใƒซใซใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆๅฑžๆ€งใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> from transformers import DistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json") +>>> model = DistilBertModel(my_config) +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎ้‡ใฟใงใฏใชใใƒฉใƒณใƒ€ใƒ ใชๅ€คใ‚’ๆŒใคใƒขใƒ‡ใƒซใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ่กŒใ‚ใ‚Œใ‚‹ใพใงใ€ใพใ ๆœ‰็”จใชใ‚‚ใฎใจใ—ใฆไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ‚ณใ‚นใƒˆใจๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ™ใ€‚ +้€šๅธธใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฟ…่ฆใชใƒชใ‚ฝใƒผใ‚นใฎไธ€้ƒจใ—ใ‹ไฝฟ็”จใ›ใšใ€ใ‚ˆใ‚Š้€Ÿใใ‚ˆใ‚Š่‰ฏใ„็ตๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใซไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒ่‰ฏใ„ใงใ—ใ‚‡ใ†ใ€‚ + +[`~PreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™๏ผš + + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹้š›ใ€ใƒขใƒ‡ใƒซใŒ๐Ÿค— Transformersใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซ่จญๅฎšใŒ่‡ชๅ‹•็š„ใซใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซ่จญๅฎšๅฑžๆ€งใฎไธ€้ƒจใพใŸใฏใ™ในใฆใ‚’็‹ฌ่‡ชใฎใ‚‚ใฎใง็ฝฎใๆ›ใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```py +>>> model = DistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + +ใƒขใƒ‡ใƒซใซใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšๅฑžๆ€งใ‚’ใƒญใƒผใƒ‰ใ—ใฆใใ ใ•ใ„๏ผš + +```py +>>> from transformers import TFDistilBertModel + +>>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") +>>> tf_model = TFDistilBertModel(my_config) +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใงใฏใชใใƒฉใƒณใƒ€ใƒ ใชๅ€คใ‚’ๆŒใคใƒขใƒ‡ใƒซใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ +ใ“ใฎใƒขใƒ‡ใƒซใ‚’ๆœ‰็”จใช็›ฎ็š„ใซใฏใพใ ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ‚ณใ‚นใƒˆใŒใ‹ใ‹ใ‚Šใ€ๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ™ใ€‚ +ไธ€่ˆฌ็š„ใซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฟ…่ฆใชใƒชใ‚ฝใƒผใ‚นใฎไธ€้ƒจใ—ใ‹ไฝฟ็”จใ›ใšใซใ€ใ‚ˆใ‚Š้€Ÿใๅ„ชใ‚ŒใŸ็ตๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใซไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒ่‰ฏใ„ใงใ—ใ‚‡ใ†ใ€‚ + +[`~TFPreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™๏ผš + + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹้š›ใ€ใƒขใƒ‡ใƒซใŒ๐Ÿค— Transformersใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซๆง‹ๆˆใŒ่‡ชๅ‹•็š„ใซใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใงใ‚ใ‚Œใฐใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซๆง‹ๆˆๅฑžๆ€งใฎไธ€้ƒจใพใŸใฏใ™ในใฆใ‚’็‹ฌ่‡ชใฎใ‚‚ใฎใง็ฝฎใๆ›ใˆใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผš + +```py +>>> tf_model = TFDistilBertModel.from_pretrained("distilbert/distilbert-base-uncased", config=my_config) +``` + + + + +### Model heads + +ใ“ใฎๆ™‚็‚นใงใ€ใƒ™ใƒผใ‚นใฎDistilBERTใƒขใƒ‡ใƒซใŒใ‚ใ‚Šใ€ใ“ใ‚Œใฏ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚้š ใ‚ŒใŸ็Šถๆ…‹ใฏใƒขใƒ‡ใƒซใฎใƒ˜ใƒƒใƒ‰ใธใฎๅ…ฅๅŠ›ใจใ—ใฆๆธกใ•ใ‚Œใ€ๆœ€็ต‚็š„ใชๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚๐Ÿค— Transformersใฏใ€ใƒขใƒ‡ใƒซใŒใใฎใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹้™ใ‚Šใ€ๅ„ใ‚ฟใ‚นใ‚ฏใซๅฏพๅฟœใ™ใ‚‹็•ฐใชใ‚‹ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใ‚’ๆไพ›ใ—ใพใ™๏ผˆใคใพใ‚Šใ€DistilBERTใ‚’็ฟป่จณใฎใ‚ˆใ†ใชใ‚ทใƒผใ‚ฑใƒณใ‚นๅฏพใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“๏ผ‰ใ€‚ + + + +ใŸใจใˆใฐใ€[`DistilBertForSequenceClassification`]ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใคใƒ™ใƒผใ‚นใฎDistilBERTใƒขใƒ‡ใƒซใงใ™ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏใ€ใƒ—ใƒผใƒซใ•ใ‚ŒใŸๅ‡บๅŠ›ใฎไธŠใซใ‚ใ‚‹็ทšๅฝขๅฑคใงใ™ใ€‚ + +```py +>>> from transformers import DistilBertForSequenceClassification + +>>> model = DistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ๆ–ฐใ—ใ„ใ‚ฟใ‚นใ‚ฏใซใ“ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ฐกๅ˜ใซๅ†ๅˆฉ็”จใ™ใ‚‹ใซใฏใ€็•ฐใชใ‚‹ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใซๅˆ‡ใ‚Šๆ›ฟใˆใพใ™ใ€‚ +่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€[`DistilBertForQuestionAnswering`] ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +่ณชๅ•ๅฟœ็ญ”ใƒ˜ใƒƒใƒ‰ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใจ้กžไผผใ—ใฆใ„ใพใ™ใŒใ€้š ใ‚Œ็Šถๆ…‹ใฎๅ‡บๅŠ›ใฎไธŠใซ็ทšๅฝขๅฑคใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> from transformers import DistilBertForQuestionAnswering + +>>> model = DistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +ไพ‹ใˆใฐใ€[`TFDistilBertForSequenceClassification`]ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใคใƒ™ใƒผใ‚นใฎDistilBERTใƒขใƒ‡ใƒซใงใ™ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏใ€ใƒ—ใƒผใƒซใ•ใ‚ŒใŸๅ‡บๅŠ›ใฎไธŠใซใ‚ใ‚‹็ทšๅฝขๅฑคใงใ™ใ€‚ + +```py +>>> from transformers import TFDistilBertForSequenceClassification + +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ๅˆฅใฎใ‚ฟใ‚นใ‚ฏใซใ“ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ฐกๅ˜ใซๅ†ๅˆฉ็”จใ™ใ‚‹ใ“ใจใŒใงใใ€็•ฐใชใ‚‹ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ใ ใ‘ใงใ™ใ€‚ +่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€[`TFDistilBertForQuestionAnswering`]ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +่ณชๅ•ๅฟœ็ญ”ใƒ˜ใƒƒใƒ‰ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใจไผผใฆใ„ใพใ™ใŒใ€้š ใ‚Œ็Šถๆ…‹ใฎๅ‡บๅŠ›ใฎไธŠใซ็ทšๅฝขๅฑคใŒใ‚ใ‚‹ใ ใ‘ใงใ™ใ€‚ + + +```py +>>> from transformers import TFDistilBertForQuestionAnswering + +>>> tf_model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +## Tokenizer + +ใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚’ใƒขใƒ‡ใƒซใงไฝฟ็”จใ™ใ‚‹ๅ‰ใซๅฟ…่ฆใชๆœ€ๅพŒใฎใƒ™ใƒผใ‚นใ‚ฏใƒฉใ‚นใฏใ€็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](main_classes/tokenizer)ใงใ™ใ€‚ +๐Ÿค— Transformersใงไฝฟ็”จใงใใ‚‹2ใคใฎใ‚ฟใ‚คใƒ—ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒใ‚ใ‚Šใพใ™๏ผš + +- [`PreTrainedTokenizer`]: ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎPythonๅฎŸ่ฃ…ใงใ™ใ€‚ +- [`PreTrainedTokenizerFast`]: Rustใƒ™ใƒผใ‚นใฎ[๐Ÿค— Tokenizer](https://huggingface.co/docs/tokenizers/python/latest/)ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใงใ™ใ€‚ +ใ“ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใ‚ฟใ‚คใƒ—ใฏใ€ใใฎRustๅฎŸ่ฃ…ใซใ‚ˆใ‚Šใ€็‰นใซใƒใƒƒใƒใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณไธญใซ้ซ˜้€Ÿใงใ™ใ€‚ +้ซ˜้€Ÿใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ƒใฎๅ˜่ชžใพใŸใฏๆ–‡ๅญ—ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ™ใ‚‹*ใ‚ชใƒ•ใ‚ปใƒƒใƒˆใƒžใƒƒใƒ”ใƒณใ‚ฐ*ใชใฉใฎ่ฟฝๅŠ ใƒกใ‚ฝใƒƒใƒ‰ใ‚‚ๆไพ›ใ—ใพใ™ใ€‚ + +ไธกๆ–นใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ใ‚จใƒณใ‚ณใƒผใƒ‰ใจใƒ‡ใ‚ณใƒผใƒ‰ใ€ๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใฎ่ฟฝๅŠ ใ€็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใฎ็ฎก็†ใชใฉใ€ๅ…ฑ้€šใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ + + + +ใ™ในใฆใฎใƒขใƒ‡ใƒซใŒ้ซ˜้€Ÿใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใƒขใƒ‡ใƒซใŒ้ซ˜้€Ÿใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€ใ“ใฎ[่กจ](index#supported-frameworks)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +็‹ฌ่‡ชใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใŸๅ ดๅˆใ€*ใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใƒผ*ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝœๆˆใงใใพใ™ใ€‚ + +```py +>>> from transformers import DistilBertTokenizer + +>>> my_tokenizer = DistilBertTokenizer(vocab_file="my_vocab_file.txt", do_lower_case=False, padding_side="left") +``` + +ใ‚ซใ‚นใ‚ฟใƒ ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‹ใ‚‰็”Ÿๆˆใ•ใ‚Œใ‚‹่ชžๅฝ™ใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒ็”Ÿๆˆใ™ใ‚‹่ชžๅฝ™ใจใฏ็•ฐใชใ‚‹ใ“ใจใ‚’่ฆšใˆใฆใŠใใ“ใจใฏ้‡่ฆใงใ™ใ€‚ +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎ่ชžๅฝ™ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใใ†ใ—ใชใ„ใจใ€ๅ…ฅๅŠ›ใŒๆ„ๅ‘ณใ‚’ใชใ•ใชใใชใ‚Šใพใ™ใ€‚ +[`DistilBertTokenizer`]ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎ่ชžๅฝ™ใ‚’ๆŒใคใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝœๆˆใ—ใพใ™: + + +```py +>>> from transformers import DistilBertTokenizer + +>>> slow_tokenizer = DistilBertTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +[`DistilBertTokenizerFast`]ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆ้ซ˜้€Ÿใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```py +>>> from transformers import DistilBertTokenizerFast + +>>> fast_tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert/distilbert-base-uncased") +``` + + + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€[`AutoTokenizer`]ใฏ้ซ˜้€Ÿใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’่ชญใฟ่พผใ‚‚ใ†ใจใ—ใพใ™ใ€‚`from_pretrained`ๅ†…ใง`use_fast=False`ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใงใ€ใ“ใฎๅ‹•ไฝœใ‚’็„กๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + + +## Image Processor + +็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใƒ“ใ‚ธใƒงใƒณๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏๅŸบๆœฌใ‚ฏใƒฉใ‚น [`~image_processing_utils.ImageProcessingMixin`] ใ‚’็ถ™ๆ‰ฟใ—ใฆใ„ใพใ™ใ€‚ + +ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ไฝฟ็”จใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใŸใจใˆใฐใ€็”ปๅƒๅˆ†้กžใซ[ViT](model_doc/vit)ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ [`ViTImageProcessor`] ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> from transformers import ViTImageProcessor + +>>> vit_extractor = ViTImageProcessor() +>>> print(vit_extractor) +ViTImageProcessor { + "do_normalize": true, + "do_resize": true, + "image_processor_type": "ViTImageProcessor", + "image_mean": [ + 0.5, + 0.5, + 0.5 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": 2, + "size": 224 +} +``` + + + +ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ‚’ๅฟ…่ฆใจใ—ใชใ„ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€ๅ˜็ด”ใซ`from_pretrained`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + + +[`ViTImageProcessor`]ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅค‰ๆ›ดใ—ใฆใ€ใ‚ซใ‚นใ‚ฟใƒ ใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝœๆˆใงใใพใ™๏ผš + + +```py +>>> from transformers import ViTImageProcessor + +>>> my_vit_extractor = ViTImageProcessor(resample="PIL.Image.BOX", do_normalize=False, image_mean=[0.3, 0.3, 0.3]) +>>> print(my_vit_extractor) +ViTImageProcessor { + "do_normalize": false, + "do_resize": true, + "image_processor_type": "ViTImageProcessor", + "image_mean": [ + 0.3, + 0.3, + 0.3 + ], + "image_std": [ + 0.5, + 0.5, + 0.5 + ], + "resample": "PIL.Image.BOX", + "size": 224 +} +``` + +## Feature Extractor + +ใƒ•ใ‚ฃใƒผใƒใƒฃใƒผๆŠฝๅ‡บๅ™จใฏ้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏๅŸบๆœฌ็š„ใช [`~feature_extraction_utils.FeatureExtractionMixin`] ใ‚ฏใƒฉใ‚นใ‹ใ‚‰็ถ™ๆ‰ฟใ•ใ‚Œใ€้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎ [`SequenceFeatureExtractor`] ใ‚ฏใƒฉใ‚นใ‹ใ‚‰ใ‚‚็ถ™ๆ‰ฟใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸใƒ•ใ‚ฃใƒผใƒใƒฃใƒผๆŠฝๅ‡บๅ™จใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€้Ÿณๅฃฐๅˆ†้กžใซ [Wav2Vec2](model_doc/wav2vec2) ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ [`Wav2Vec2FeatureExtractor`] ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> w2v2_extractor = Wav2Vec2FeatureExtractor() +>>> print(w2v2_extractor) +Wav2Vec2FeatureExtractor { + "do_normalize": true, + "feature_extractor_type": "Wav2Vec2FeatureExtractor", + "feature_size": 1, + "padding_side": "right", + "padding_value": 0.0, + "return_attention_mask": false, + "sampling_rate": 16000 +} +``` + + + +ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ‚’่กŒใ‚ใชใ„ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็‰นๅพดๆŠฝๅ‡บๅ™จใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€ๅ˜ใซ `from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + + +[`Wav2Vec2FeatureExtractor`] ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅค‰ๆ›ดใ—ใฆใ€ใ‚ซใ‚นใ‚ฟใƒ ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ไฝœๆˆใงใใพใ™: + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> w2v2_extractor = Wav2Vec2FeatureExtractor(sampling_rate=8000, do_normalize=False) +>>> print(w2v2_extractor) +Wav2Vec2FeatureExtractor { + "do_normalize": false, + "feature_extractor_type": "Wav2Vec2FeatureExtractor", + "feature_size": 1, + "padding_side": "right", + "padding_value": 0.0, + "return_attention_mask": false, + "sampling_rate": 8000 +} +``` + +## Processor + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใซๅฏพใ—ใฆใ€๐Ÿค— Transformersใฏไพฟๅˆฉใชใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ +ใ“ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚ฏใƒฉใ‚นใฏใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ‚„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใชใฉใฎๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’ไพฟๅˆฉใซใƒฉใƒƒใƒ—ใ—ใ€ๅ˜ไธ€ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ็ตๅˆใ—ใพใ™ใ€‚ +ใŸใจใˆใฐใ€่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ‚ฟใ‚นใ‚ฏ๏ผˆASR๏ผ‰็”จใซ[`Wav2Vec2Processor`]ใ‚’ไฝฟ็”จใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +ASRใฏ้Ÿณๅฃฐใ‚’ใƒ†ใ‚ญใ‚นใƒˆใซ่ปขๅ†™ใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚Šใ€้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒๅฟ…่ฆใงใ™ใ€‚ + +้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```py +>>> from transformers import Wav2Vec2FeatureExtractor + +>>> feature_extractor = Wav2Vec2FeatureExtractor(padding_value=1.0, do_normalize=True) +``` + +ใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝœๆˆใ—ใพใ™: + +```py +>>> from transformers import Wav2Vec2CTCTokenizer + +>>> tokenizer = Wav2Vec2CTCTokenizer(vocab_file="my_vocab_file.txt") +``` + +[`Wav2Vec2Processor`]ใง็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’็ต„ใฟๅˆใ‚ใ›ใพใ™๏ผš + + +```py +>>> from transformers import Wav2Vec2Processor + +>>> processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer) +``` + +ไบŒใคใฎๅŸบๆœฌใ‚ฏใƒฉใ‚น - ่จญๅฎšใจใƒขใƒ‡ใƒซ - ใŠใ‚ˆใณ่ฟฝๅŠ ใฎๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚น๏ผˆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพดๆŠฝๅ‡บๅ™จใ€ใพใŸใฏใƒ—ใƒญใ‚ปใƒƒใ‚ต๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€๐Ÿค— Transformers ใŒใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใฎใ„ใšใ‚Œใ‹ใ‚’ไฝœๆˆใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใฏ่จญๅฎšๅฏ่ƒฝใงใ€ๅฟ…่ฆใช็‰นๆ€งใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซ็ฐกๅ˜ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ—ใŸใ‚Šใ€ๆ—ขๅญ˜ใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/custom_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/custom_models.md new file mode 100644 index 0000000000000000000000000000000000000000..737f5fd36d0337288302f1e41dc3795665c671af --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/custom_models.md @@ -0,0 +1,336 @@ + + +# Sharing custom models + +๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€็ฐกๅ˜ใซๆ‹กๅผตใงใใ‚‹ใ‚ˆใ†ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏใƒชใƒใ‚ธใƒˆใƒชใฎ็‰นๅฎšใฎใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใซๅฎŒๅ…จใซใ‚ณใƒผใƒ‰ๅŒ–ใ•ใ‚ŒใฆใŠใ‚Šใ€ๆŠฝ่ฑกๅŒ–ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ณใƒ”ใƒผใ—ใฆ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒ็ฐกๅ˜ใงใ™ใ€‚ + +ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’ๆ›ธใ„ใฆใ„ใ‚‹ๅ ดๅˆใ€ใ‚ผใƒญใ‹ใ‚‰ๅง‹ใ‚ใ‚‹ๆ–นใŒ็ฐกๅ˜ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใจใใฎ่จญๅฎšใ‚’ใฉใฎใ‚ˆใ†ใซๆ›ธใใ€Transformersๅ†…ใงไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใ€ใ‚ณใƒผใƒ‰ใซไพๅญ˜ใ™ใ‚‹ๅ…ฑๅŒไฝ“ใจๅ…ฑๆœ‰ใ™ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใƒฉใ‚คใƒ–ใƒฉใƒชใซๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใงใ‚‚ใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚’ๅฎŸ่จผใ™ใ‚‹ใŸใ‚ใซใ€[timmใƒฉใ‚คใƒ–ใƒฉใƒช](https://github.com/rwightman/pytorch-image-models)ใฎResNetใ‚ฏใƒฉใ‚นใ‚’[`PreTrainedModel`]ใซใƒฉใƒƒใƒ—ใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆใ€ResNetใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +## Writing a custom configuration + +ใƒขใƒ‡ใƒซใซๅ–ใ‚Š็ต„ใ‚€ๅ‰ใซใ€ใพใšใใฎ่จญๅฎšใ‚’ๆ›ธใใพใ—ใ‚‡ใ†ใ€‚ใƒขใƒ‡ใƒซใฎ่จญๅฎšใฏใ€ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใ™ในใฆใฎๆƒ…ๅ ฑใ‚’ๅซใ‚€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ฆ‹ใ‚‹ใ‚ˆใ†ใซใ€ใƒขใƒ‡ใƒซใฏๅˆๆœŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซ`config`ใ—ใ‹ๅ—ใ‘ๅ–ใ‚‹ใ“ใจใŒใงใใชใ„ใŸใ‚ใ€ใใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒใงใใ‚‹ใ ใ‘ๅฎŒๅ…จใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎไพ‹ใงใฏใ€ResNetใ‚ฏใƒฉใ‚นใฎใ„ใใคใ‹ใฎๅผ•ๆ•ฐใ‚’ๅ–ๅพ—ใ—ใ€่ชฟๆ•ดใ—ใŸใ„ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใจใ—ใพใ™ใ€‚็•ฐใชใ‚‹่จญๅฎšใฏใ€็•ฐใชใ‚‹ใ‚ฟใ‚คใƒ—ใฎResNetใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใใฎๅพŒใ€ใ“ใ‚Œใ‚‰ใฎๅผ•ๆ•ฐใ‚’็ขบ่ชใ—ใŸๅพŒใ€ใใ‚Œใ‚‰ใฎๅผ•ๆ•ฐใ‚’ๅ˜ใซๆ ผ็ดใ—ใพใ™ใ€‚ + +```python +from transformers import PretrainedConfig +from typing import List + + +class ResnetConfig(PretrainedConfig): + model_type = "resnet" + + def __init__( + self, + block_type="bottleneck", + layers: list[int] = [3, 4, 6, 3], + num_classes: int = 1000, + input_channels: int = 3, + cardinality: int = 1, + base_width: int = 64, + stem_width: int = 64, + stem_type: str = "", + avg_down: bool = False, + **kwargs, + ): + if block_type not in ["basic", "bottleneck"]: + raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.") + if stem_type not in ["", "deep", "deep-tiered"]: + raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.") + + self.block_type = block_type + self.layers = layers + self.num_classes = num_classes + self.input_channels = input_channels + self.cardinality = cardinality + self.base_width = base_width + self.stem_width = stem_width + self.stem_type = stem_type + self.avg_down = avg_down + super().__init__(**kwargs) +``` + +้‡่ฆใชใ“ใจใ‚’3ใค่ฆšใˆใฆใŠใในใใƒใ‚คใƒณใƒˆใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš +- `PretrainedConfig` ใ‚’็ถ™ๆ‰ฟใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ใ‚ใชใŸใฎ `PretrainedConfig` ใฎ `__init__` ใฏไปปๆ„ใฎ kwargs ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ใ“ใ‚Œใ‚‰ใฎ `kwargs` ใฏ่ฆชใ‚ฏใƒฉใ‚นใฎ `__init__` ใซๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +็ถ™ๆ‰ฟใฏใ€๐Ÿค— Transformers ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใ™ในใฆใฎๆฉŸ่ƒฝใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใงใ™ใ€‚ไป–ใฎ2ใคใฎๅˆถ็ด„ใฏใ€ +`PretrainedConfig` ใŒ่จญๅฎšใ—ใฆใ„ใ‚‹ใƒ•ใ‚ฃใƒผใƒซใƒ‰ไปฅๅค–ใซใ‚‚ๅคšใใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๆŒใฃใฆใ„ใ‚‹ใ“ใจใ‹ใ‚‰ๆฅใฆใ„ใพใ™ใ€‚ +`from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใง่จญๅฎšใ‚’ๅ†ใƒญใƒผใƒ‰ใ™ใ‚‹ๅ ดๅˆใ€ใ“ใ‚Œใ‚‰ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏใ‚ใชใŸใฎ่จญๅฎšใซๅ—ใ‘ๅ…ฅใ‚Œใ‚‰ใ‚Œใ€ +ใใฎๅพŒใ€่ฆชใ‚ฏใƒฉใ‚นใซ้€ไฟกใ•ใ‚Œใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +่จญๅฎšใฎ `model_type` ใ‚’ๅฎš็พฉใ™ใ‚‹ใ“ใจ๏ผˆใ“ใ“ใงใฏ `model_type="resnet"`๏ผ‰ใฏใ€ +่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใซใƒขใƒ‡ใƒซใ‚’็™ป้Œฒใ—ใŸใ„ๅ ดๅˆใ‚’้™คใ„ใฆใฏๅฟ…้ ˆใงใฏใ‚ใ‚Šใพใ›ใ‚“๏ผˆๆœ€ๅพŒใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…ง๏ผ‰ใ€‚ + +ใ“ใ‚Œใงใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎไป–ใฎใƒขใƒ‡ใƒซ่จญๅฎšใจๅŒๆง˜ใซใ€่จญๅฎšใ‚’็ฐกๅ˜ใซไฝœๆˆใ—ใฆไฟๅญ˜ใงใใพใ™ใ€‚ +ไปฅไธ‹ใฏใ€resnet50d ่จญๅฎšใ‚’ไฝœๆˆใ—ใฆไฟๅญ˜ใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใงใ™๏ผš + + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d_config.save_pretrained("custom-resnet") +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€`custom-resnet` ใƒ•ใ‚ฉใƒซใƒ€ๅ†…ใซ `config.json` ใจใ„ใ†ๅๅ‰ใฎใƒ•ใ‚กใ‚คใƒซใŒไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚ใใฎๅพŒใ€`from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆๆง‹ๆˆใ‚’ๅ†ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + + +```py +resnet50d_config = ResnetConfig.from_pretrained("custom-resnet") +``` + +ใพใŸใ€[`PretrainedConfig`] ใ‚ฏใƒฉใ‚นใฎไป–ใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใŸใจใˆใฐใ€[`~PretrainedConfig.push_to_hub`] ใ‚’ไฝฟ็”จใ—ใฆใ€่จญๅฎšใ‚’็›ดๆŽฅ Hub ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +## Writing a custom model + +ResNet ใฎ่จญๅฎšใŒใงใใŸใฎใงใ€ใƒขใƒ‡ใƒซใ‚’ๆ›ธใๅง‹ใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๅฎŸ้š›ใซใฏ2ใคใฎใƒขใƒ‡ใƒซใ‚’ๆ›ธใใพใ™ใ€‚1ใคใฏใƒใƒƒใƒใฎ็”ปๅƒใ‹ใ‚‰้š ใ‚ŒใŸ็‰นๅพดใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใƒขใƒ‡ใƒซ๏ผˆ[`BertModel`] ใฎใ‚ˆใ†ใชใ‚‚ใฎ๏ผ‰ใงใ€ใ‚‚ใ†1ใคใฏ็”ปๅƒๅˆ†้กžใซ้ฉใ—ใŸใƒขใƒ‡ใƒซ๏ผˆ[`BertForSequenceClassification`] ใฎใ‚ˆใ†ใชใ‚‚ใฎ๏ผ‰ใงใ™ใ€‚ + +ๅ‰่ฟฐใ—ใŸใ‚ˆใ†ใซใ€ใ“ใฎไพ‹ใ‚’ใ‚ทใƒณใƒ—ใƒซใซไฟใคใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใฎ็ทฉใ„ใƒฉใƒƒใƒ‘ใƒผใฎใฟใ‚’ๆ›ธใใพใ™ใ€‚ใ“ใฎใ‚ฏใƒฉใ‚นใ‚’ๆ›ธใๅ‰ใซ่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ๅ”ฏไธ€ใฎใ“ใจใฏใ€ใƒ–ใƒญใƒƒใ‚ฏใ‚ฟใ‚คใƒ—ใจๅฎŸ้š›ใฎใƒ–ใƒญใƒƒใ‚ฏใ‚ฏใƒฉใ‚นใฎ้–“ใฎใƒžใƒƒใƒ—ใงใ™ใ€‚ใใฎๅพŒใ€ใ™ในใฆใ‚’ `ResNet` ใ‚ฏใƒฉใ‚นใซๆธกใ—ใฆ่จญๅฎšใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ๅฎš็พฉใ—ใพใ™๏ผš + +```py +from transformers import PreTrainedModel +from timm.models.resnet import BasicBlock, Bottleneck, ResNet +from .configuration_resnet import ResnetConfig + + +BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} + + +class ResnetModel(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor): + return self.model.forward_features(tensor) +``` + +็”ปๅƒใ‚’ๅˆ†้กžใ™ใ‚‹ใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€forwardใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ ใ‘ใงใ™๏ผš + +```py +import torch + + +class ResnetModelForImageClassification(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor, labels=None): + logits = self.model(tensor) + if labels is not None: + loss = torch.nn.functional.cross_entropy(logits, labels) + return {"loss": loss, "logits": logits} + return {"logits": logits} +``` + +ไธกๆ–นใฎๅ ดๅˆใ€`PreTrainedModel`ใ‹ใ‚‰็ถ™ๆ‰ฟใ—ใ€`config`ใ‚’ไฝฟ็”จใ—ใฆใ‚นใƒผใƒ‘ใƒผใ‚ฏใƒฉใ‚นใฎๅˆๆœŸๅŒ–ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™๏ผˆ้€šๅธธใฎ`torch.nn.Module`ใ‚’ๆ›ธใใจใใฎใ‚ˆใ†ใชๆ„Ÿใ˜ใงใ™๏ผ‰ใ€‚ +`config_class`ใ‚’่จญๅฎšใ™ใ‚‹่กŒใฏๅฟ…้ ˆใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€๏ผˆๆœ€ๅพŒใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…ง๏ผ‰ใ€ใƒขใƒ‡ใƒซใ‚’่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใซ็™ป้Œฒใ—ใŸใ„ๅ ดๅˆใซไฝฟ็”จใงใใพใ™ใ€‚ + + + +ใƒขใƒ‡ใƒซใŒใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใƒขใƒ‡ใƒซใจ้žๅธธใซไผผใฆใ„ใ‚‹ๅ ดๅˆใ€ใ“ใฎใƒขใƒ‡ใƒซใจๅŒใ˜ๆง‹ๆˆใ‚’ๅ†ๅˆฉ็”จใงใใพใ™ใ€‚ + + + +ใƒขใƒ‡ใƒซใŒ่ฟ”ใ™ๅ†…ๅฎนใฏไฝ•ใงใ‚‚ๆง‹ใ„ใพใ›ใ‚“ใŒใ€ใƒฉใƒ™ใƒซใŒๆธกใ•ใ‚Œใ‚‹ใจใใซๆๅคฑใ‚’ๅซใ‚€่พžๆ›ธใ‚’่ฟ”ใ™๏ผˆ`ResnetModelForImageClassification`ใฎใ‚ˆใ†ใซ่กŒใฃใŸใ‚‚ใฎ๏ผ‰ใจใ€ +ใƒขใƒ‡ใƒซใ‚’[`Trainer`]ใ‚ฏใƒฉใ‚นๅ†…ใง็›ดๆŽฅไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใพใŸใฏไป–ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ไบˆๅฎšใงใ‚ใ‚‹้™ใ‚Šใ€ +ๅˆฅใฎๅ‡บๅŠ›ๅฝขๅผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ•ใฆใ€ใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใŒใงใใŸใฎใงใ€1ใคไฝœๆˆใ—ใพใ—ใ‚‡ใ†๏ผš + +```py +resnet50d = ResnetModelForImageClassification(resnet50d_config) +``` + +ๅ†ๅบฆใ€[`PreTrainedModel`]ใฎใ„ใšใ‚Œใ‹ใฎใƒกใ‚ฝใƒƒใƒ‰ใ€ไพ‹ใˆใฐ[`~PreTrainedModel.save_pretrained`]ใ‚„ +[`~PreTrainedModel.push_to_hub`]ใชใฉใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ใ‚ณใƒผใƒ‰ใจไธ€็ท’ใซ +Hugging Face Hub ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ™ใ€‚ +ใ—ใ‹ใ—ใ€ใพใšใฏใƒขใƒ‡ใƒซๅ†…ใซไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +็‹ฌ่‡ชใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใงใฏใ€ใŠใใ‚‰ใ็‹ฌ่‡ชใฎใƒ‡ใƒผใ‚ฟใงใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใซใชใ‚‹ใงใ—ใ‚‡ใ†ใ€‚ +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ‚นใƒ”ใƒผใƒ‰ใ‚ขใƒƒใƒ—ใฎใŸใ‚ใซใ€resnet50dใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +็งใŸใกใฎใƒขใƒ‡ใƒซใฏใใ‚Œใ‚’ใƒฉใƒƒใƒ—ใ™ใ‚‹ใ ใ‘ใชใฎใงใ€ใ“ใ‚Œใ‚‰ใฎ้‡ใฟใ‚’่ปข้€ใ™ใ‚‹ใฎใฏ็ฐกๅ˜ใงใ™๏ผš + + +```py +import timm + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +ใ•ใฆใ€[`~PreTrainedModel.save_pretrained`]ใพใŸใฏ[`~PreTrainedModel.push_to_hub`]ใ‚’ๅฎŸ่กŒใ—ใŸใจใใซใ€ +ใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใŒไฟๅญ˜ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +## Sending the code to the Hub + + + +ใ“ใฎAPIใฏๅฎŸ้จ“็š„ใงใ‚ใ‚Šใ€ๆฌกใฎใƒชใƒชใƒผใ‚นใงใ‚ใšใ‹ใชๅค‰ๆ›ดใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ + + + +ใพใšใ€ใƒขใƒ‡ใƒซใŒ`.py`ใƒ•ใ‚กใ‚คใƒซใซๅฎŒๅ…จใซๅฎš็พฉใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ใƒ•ใ‚กใ‚คใƒซใฏ็›ธๅฏพใ‚คใƒณใƒใƒผใƒˆใ‚’ไป–ใฎใƒ•ใ‚กใ‚คใƒซใซไพๅญ˜ใงใใพใ™ใŒใ€ใ™ในใฆใฎใƒ•ใ‚กใ‚คใƒซใŒๅŒใ˜ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซใ‚ใ‚‹้™ใ‚Š๏ผˆใพใ ใ“ใฎๆฉŸ่ƒฝใงใฏใ‚ตใƒ–ใƒขใ‚ธใƒฅใƒผใƒซใฏใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“๏ผ‰ใ€ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ“ใฎไพ‹ใงใฏใ€็พๅœจใฎไฝœๆฅญใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชๅ†…ใซๅๅ‰ใŒใ€Œresnet_modelใ€ใฎใƒ•ใ‚ฉใƒซใƒ€ใ‚’ไฝœๆˆใ—ใ€ใใฎไธญใซ`modeling_resnet.py`ใƒ•ใ‚กใ‚คใƒซใจ`configuration_resnet.py`ใƒ•ใ‚กใ‚คใƒซใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ +ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใซใฏ`ResnetConfig`ใฎใ‚ณใƒผใƒ‰ใŒๅซใพใ‚Œใ€ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใซใฏ`ResnetModel`ใจ`ResnetModelForImageClassification`ใฎใ‚ณใƒผใƒ‰ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + + +``` +. +โ””โ”€โ”€ resnet_model + โ”œโ”€โ”€ __init__.py + โ”œโ”€โ”€ configuration_resnet.py + โ””โ”€โ”€ modeling_resnet.py +``` + +`__init__.py`ใฏ็ฉบใงใ‚ใฃใฆใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚PythonใŒ`resnet_model`ใ‚’ใƒขใ‚ธใƒฅใƒผใƒซใจใ—ใฆๆคœๅ‡บใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซๅญ˜ๅœจใ—ใพใ™ใ€‚ + + + +ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ณใƒ”ใƒผใ™ใ‚‹ๅ ดๅˆใ€ใƒ•ใ‚กใ‚คใƒซใฎๅ…ˆ้ ญใซใ‚ใ‚‹ใ™ในใฆใฎ็›ธๅฏพใ‚คใƒณใƒใƒผใƒˆใ‚’`transformers`ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‹ใ‚‰ใ‚คใƒณใƒใƒผใƒˆใซ็ฝฎใๆ›ใˆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ๆ—ขๅญ˜ใฎ่จญๅฎšใ‚„ใƒขใƒ‡ใƒซใ‚’ๅ†ๅˆฉ็”จ๏ผˆใพใŸใฏใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–๏ผ‰ใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใŸใ‚ใซใ€ๆฌกใฎๆ‰‹้ †ใซๅพ“ใฃใฆใใ ใ•ใ„๏ผšใพใšใ€ๆ–ฐใ—ใไฝœๆˆใ—ใŸใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ResNetใƒขใƒ‡ใƒซใจ่จญๅฎšใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใพใ™๏ผš + + +```py +from resnet_model.configuration_resnet import ResnetConfig +from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification +``` + +ๆฌกใซใ€`save_pretrained`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ“ใ‚Œใ‚‰ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใ‚ณใƒผใƒ‰ใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ณใƒ”ใƒผใ—ใ€็‰นๅฎšใฎAutoใ‚ฏใƒฉใ‚น๏ผˆ็‰นใซใƒขใƒ‡ใƒซใฎๅ ดๅˆ๏ผ‰ใซๆญฃใ—ใ็™ป้Œฒใ™ใ‚‹ใ‚ˆใ†ใƒฉใ‚คใƒ–ใƒฉใƒชใซๆŒ‡็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฌกใฎใ‚ˆใ†ใซๅฎŸ่กŒใ—ใพใ™๏ผš + +```py +ResnetConfig.register_for_auto_class() +ResnetModel.register_for_auto_class("AutoModel") +ResnetModelForImageClassification.register_for_auto_class("AutoModelForImageClassification") +``` + +ๆณจๆ„: ่จญๅฎšใซใคใ„ใฆใฏ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“๏ผˆ่จญๅฎš็”จใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏ1ใคใ—ใ‹ใชใใ€[`AutoConfig`]ใงใ™๏ผ‰ใŒใ€ +ใƒขใƒ‡ใƒซใซใคใ„ใฆใฏ็•ฐใชใ‚Šใพใ™ใ€‚ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใฏๅคšใใฎ็•ฐใชใ‚‹ใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ +ใƒขใƒ‡ใƒซใŒๆญฃ็ขบใช่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฎใ†ใกใฉใ‚Œใซ้ฉใ—ใฆใ„ใ‚‹ใ‹ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆฌกใซใ€ๅ‰่ฟฐใฎใ‚ˆใ†ใซ่จญๅฎšใจใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†๏ผš + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d = ResnetModelForImageClassification(resnet50d_config) + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +ใƒขใƒ‡ใƒซใ‚’Hubใซ้€ไฟกใ™ใ‚‹ใซใฏใ€ใƒญใ‚ฐใ‚คใƒณใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ใ‚ฟใƒผใƒŸใƒŠใƒซใงๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™๏ผš + +```bash +hf auth login +``` + +ใพใŸใฏใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‹ใ‚‰๏ผš + +```py +from huggingface_hub import notebook_login + +notebook_login() +``` + +ๆฌกใซใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใฆใ€็‹ฌ่‡ชใฎๅๅ‰็ฉบ้–“ใซใƒ—ใƒƒใ‚ทใƒฅใงใใพใ™๏ผˆใพใŸใฏใ€ใƒกใƒณใƒใƒผใงใ‚ใ‚‹็ต„็น”ใซใƒ—ใƒƒใ‚ทใƒฅใงใใพใ™๏ผ‰๏ผš + +```py +resnet50d.push_to_hub("custom-resnet50d") +``` + +ใƒขใƒ‡ใƒชใƒณใ‚ฐใฎ้‡ใฟใจJSONๅฝขๅผใฎๆง‹ๆˆใซๅŠ ใˆใฆใ€ใ“ใฎใƒ•ใ‚ฉใƒซใƒ€ใƒผใ€Œcustom-resnet50dใ€ๅ†…ใฎใƒขใƒ‡ใƒชใƒณใ‚ฐใŠใ‚ˆใณๆง‹ๆˆใ€Œ.pyใ€ใƒ•ใ‚กใ‚คใƒซใ‚‚ใ‚ณใƒ”ใƒผใ•ใ‚Œใ€็ตๆžœใฏHubใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใพใ—ใŸใ€‚็ตๆžœใฏใ“ใฎ[model repo](https://huggingface.co/sgugger/custom-resnet50d)ใง็ขบ่ชใงใใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Hubใธใฎใƒ—ใƒƒใ‚ทใƒฅๆ–นๆณ•](model_sharing)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Using a model with custom code + +่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใจ `from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒชใƒใ‚ธใƒˆใƒชๅ†…ใฎใ‚ซใ‚นใ‚ฟใƒ ใ‚ณใƒผใƒ‰ใƒ•ใ‚กใ‚คใƒซใจๅ…ฑใซไปปๆ„ใฎๆง‹ๆˆใ€ใƒขใƒ‡ใƒซใ€ใพใŸใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ Hubใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒ•ใ‚กใ‚คใƒซใจใ‚ณใƒผใƒ‰ใฏใƒžใƒซใ‚ฆใ‚งใ‚ขใฎใ‚นใ‚ญใƒฃใƒณใŒๅฎŸๆ–ฝใ•ใ‚Œใพใ™๏ผˆ่ฉณ็ดฐใฏ[Hubใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃ](https://huggingface.co/docs/hub/security#malware-scanning)ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€ใ—ใ‹ใ—ใ€ไพ็„ถใจใ—ใฆๆ‚ชๆ„ใฎใ‚ใ‚‹ใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ—ใชใ„ใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใ‚ณใƒผใƒ‰ใจไฝœ่€…ใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +`trust_remote_code=True` ใ‚’่จญๅฎšใ—ใฆใ‚ซใ‚นใ‚ฟใƒ ใ‚ณใƒผใƒ‰ใ‚’ๆŒใคใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™๏ผš + + +```py +from transformers import AutoModelForImageClassification + +model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True) +``` + +ใ‚ณใƒŸใƒƒใƒˆใƒใƒƒใ‚ทใƒฅใ‚’ใ€Œrevisionใ€ใจใ—ใฆๆธกใ™ใ“ใจใ‚‚ๅผทใๆŽจๅฅจใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฎไฝœ่€…ใŒใ‚ณใƒผใƒ‰ใ‚’ๆ‚ชๆ„ใฎใ‚ใ‚‹ๆ–ฐใ—ใ„่กŒใงๆ›ดๆ–ฐใ—ใชใ‹ใฃใŸใ“ใจใ‚’็ขบ่ชใงใใพใ™๏ผˆใƒขใƒ‡ใƒซใฎไฝœ่€…ใ‚’ๅฎŒๅ…จใซไฟก้ ผใ—ใฆใ„ใ‚‹ๅ ดๅˆใ‚’้™คใใพใ™๏ผ‰ใ€‚ + + +```py +commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292" +model = AutoModelForImageClassification.from_pretrained( + "sgugger/custom-resnet50d", trust_remote_code=True, revision=commit_hash +) +``` + +ใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชใฎใ‚ณใƒŸใƒƒใƒˆๅฑฅๆญดใ‚’ใƒ–ใƒฉใ‚ฆใ‚ธใƒณใ‚ฐใ™ใ‚‹้š›ใซใฏใ€ไปปๆ„ใฎใ‚ณใƒŸใƒƒใƒˆใฎใ‚ณใƒŸใƒƒใƒˆใƒใƒƒใ‚ทใƒฅใ‚’็ฐกๅ˜ใซใ‚ณใƒ”ใƒผใงใใ‚‹ใƒœใ‚ฟใƒณใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Registering a model with custom code to the auto classes + +๐Ÿค— Transformersใ‚’ๆ‹กๅผตใ™ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝœๆˆใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใ‚’ๅซใ‚ใ‚‹ใŸใ‚ใซ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใ‚’ๆ‹กๅผตใ—ใŸใ„ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใฏใ‚ณใƒผใƒ‰ใ‚’Hubใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ“ใจใจใฏ็•ฐใชใ‚Šใ€ใƒฆใƒผใ‚ถใƒผใฏใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใ‚’ๅ–ๅพ—ใ™ใ‚‹ใŸใ‚ใซใ‚ใชใŸใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ +๏ผˆHubใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚ณใƒผใƒ‰ใ‚’่‡ชๅ‹•็š„ใซใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ใฎใจใฏๅฏพ็…ง็š„ใงใ™๏ผ‰ใ€‚ + +ๆง‹ๆˆใซๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใ‚ฟใ‚คใƒ—ใจ็•ฐใชใ‚‹ `model_type` ๅฑžๆ€งใŒใ‚ใ‚‹้™ใ‚Šใ€ใพใŸใ‚ใชใŸใฎใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใŒ้ฉๅˆ‡ใช `config_class` ๅฑžๆ€งใ‚’ๆŒใฃใฆใ„ใ‚‹้™ใ‚Šใ€ +ๆฌกใฎใ‚ˆใ†ใซใใ‚Œใ‚‰ใ‚’่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใซ่ฟฝๅŠ ใงใใพใ™๏ผš + +```py +from transformers import AutoConfig, AutoModel, AutoModelForImageClassification + +AutoConfig.register("resnet", ResnetConfig) +AutoModel.register(ResnetConfig, ResnetModel) +AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) +``` + +ๆณจๆ„: `AutoConfig` ใซใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšใ‚’็™ป้Œฒใ™ใ‚‹้š›ใฎๆœ€ๅˆใฎๅผ•ๆ•ฐใฏใ€ใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšใฎ `model_type` ใจไธ€่‡ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใพใŸใ€ไปปๆ„ใฎ่‡ชๅ‹•ใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใซใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใ‚’็™ป้Œฒใ™ใ‚‹้š›ใฎๆœ€ๅˆใฎๅผ•ๆ•ฐใฏใ€ใใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฎ `config_class` ใจไธ€่‡ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/fast_tokenizers.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/fast_tokenizers.md new file mode 100644 index 0000000000000000000000000000000000000000..d97e59ed0ed25344e26508f868b1dbe317437242 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/fast_tokenizers.md @@ -0,0 +1,73 @@ + + +# Use tokenizers from ๐Ÿค— Tokenizers + +[`PreTrainedTokenizerFast`]ใฏ[๐Ÿค— Tokenizers](https://huggingface.co/docs/tokenizers)ใƒฉใ‚คใƒ–ใƒฉใƒชใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚๐Ÿค— Tokenizersใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ๅ–ๅพ—ใ—ใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€้žๅธธใซ็ฐกๅ˜ใซ๐Ÿค— Transformersใซใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +ๅ…ทไฝ“็š„ใชๅ†…ๅฎนใซๅ…ฅใ‚‹ๅ‰ใซใ€ใพใšใฏใ„ใใคใ‹ใฎ่กŒใงใƒ€ใƒŸใƒผใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†๏ผš + + +```python +>>> from tokenizers import Tokenizer +>>> from tokenizers.models import BPE +>>> from tokenizers.trainers import BpeTrainer +>>> from tokenizers.pre_tokenizers import Whitespace + +>>> tokenizer = Tokenizer(BPE(unk_token="[UNK]")) +>>> trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]) + +>>> tokenizer.pre_tokenizer = Whitespace() +>>> files = [...] +>>> tokenizer.train(files, trainer) +``` + +็งใŸใกใฏไปŠใ€ๅฎš็พฉใ—ใŸใƒ•ใ‚กใ‚คใƒซใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚’ใƒฉใƒณใ‚ฟใ‚คใƒ ใงๅผ•ใ็ถšใไฝฟ็”จใ™ใ‚‹ใ‹ใ€ +ๅฐ†ๆฅใฎๅ†ๅˆฉ็”จใฎใŸใ‚ใซJSONใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## Loading directly from the tokenizer object + +๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใงใ“ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใฉใฎใ‚ˆใ†ใซๆดป็”จใงใใ‚‹ใ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚[`PreTrainedTokenizerFast`]ใ‚ฏใƒฉใ‚นใฏใ€ +*tokenizer*ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅผ•ๆ•ฐใจใ—ใฆๅ—ใ‘ๅ…ฅใ‚Œใ€็ฐกๅ˜ใซใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer) +``` + +ใ“ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏใ€๐Ÿค— Transformers ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒๅ…ฑๆœ‰ใ™ใ‚‹ใ™ในใฆใฎใƒกใ‚ฝใƒƒใƒ‰ใจไธ€็ท’ใซไฝฟ็”จใงใใพใ™๏ผ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใƒšใƒผใ‚ธ](main_classes/tokenizer)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## Loading from a JSON file + +JSONใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’่ชญใฟ่พผใ‚€ใซใฏใ€ใพใšใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†๏ผš + +```python +>>> tokenizer.save("tokenizer.json") +``` + +ใ“ใฎใƒ•ใ‚กใ‚คใƒซใ‚’ไฟๅญ˜ใ—ใŸใƒ‘ใ‚นใฏใ€`PreTrainedTokenizerFast` ใฎๅˆๆœŸๅŒ–ใƒกใ‚ฝใƒƒใƒ‰ใซ `tokenizer_file` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆๆธกใ™ใ“ใจใŒใงใใพใ™๏ผš + + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json") +``` + +ใ“ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏใ€๐Ÿค— Transformers ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒๅ…ฑๆœ‰ใ™ใ‚‹ใ™ในใฆใฎใƒกใ‚ฝใƒƒใƒ‰ใจไธ€็ท’ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸ๏ผ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใƒšใƒผใ‚ธ](main_classes/tokenizer)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/generation_strategies.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/generation_strategies.md new file mode 100644 index 0000000000000000000000000000000000000000..a93ef3d364409737638d382b371a5b0d11f5a3e4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/generation_strategies.md @@ -0,0 +1,349 @@ + + +# Text generation strategies + +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฏใ€ใ‚ชใƒผใƒ—ใƒณใ‚จใƒณใƒ‰ใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ€่ฆ็ด„ใ€็ฟป่จณใชใฉใ€ๅคšใใฎ่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏใซไธๅฏๆฌ ใงใ™ใ€‚ใพใŸใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡บๅŠ›ใจใ™ใ‚‹ใ•ใพใ–ใพใชๆททๅœจใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซใ‚‚ๅฝฑ้Ÿฟใ‚’ไธŽใˆใฆใŠใ‚Šใ€ไพ‹ใˆใฐ้Ÿณๅฃฐใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎๅค‰ๆ›ใ‚„็”ปๅƒใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎๅค‰ๆ›ใชใฉใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใ‚‹ใ„ใใคใ‹ใฎใƒขใƒ‡ใƒซใซใฏใ€GPT2ใ€XLNetใ€OpenAI GPTใ€CTRLใ€TransformerXLใ€XLMใ€Bartใ€T5ใ€GITใ€WhisperใŒๅซใพใ‚Œใพใ™ใ€‚ + +[`~generation.GenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€็•ฐใชใ‚‹ใ‚ฟใ‚นใ‚ฏใฎใƒ†ใ‚ญใ‚นใƒˆๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ใ„ใใคใ‹ใฎไพ‹ใ‚’ใ”็ดนไป‹ใ—ใพใ™๏ผš +* [ใƒ†ใ‚ญใ‚นใƒˆ่ฆ็ด„](./tasks/summarization#inference) +* [็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ](./model_doc/git#transformers.GitForCausalLM.forward.example) +* [้Ÿณๅฃฐใฎ่ปข่จ˜](./model_doc/whisper#transformers.WhisperForConditionalGeneration.forward.example) + +generateใƒกใ‚ฝใƒƒใƒ‰ใธใฎๅ…ฅๅŠ›ใฏใ€ใƒขใƒ‡ใƒซใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใซไพๅญ˜ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅ…ฅๅŠ›ใฏใ€AutoTokenizerใ‚„AutoProcessorใชใฉใฎใƒขใƒ‡ใƒซใฎใƒ—ใƒชใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚ฏใƒฉใ‚นใซใ‚ˆใฃใฆ่ฟ”ใ•ใ‚Œใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎใƒ—ใƒชใƒ—ใƒญใ‚ปใƒƒใ‚ตใŒ่ค‡ๆ•ฐใฎ็จฎ้กžใฎๅ…ฅๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ๅ ดๅˆใฏใ€ใ™ในใฆใฎๅ…ฅๅŠ›ใ‚’generate()ใซๆธกใ—ใพใ™ใ€‚ๅ„ใƒขใƒ‡ใƒซใฎใƒ—ใƒชใƒ—ใƒญใ‚ปใƒƒใ‚ตใซใคใ„ใฆใฎ่ฉณ็ดฐใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒซใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใง็ขบ่ชใงใใพใ™ใ€‚ + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใฎใƒˆใƒผใ‚ฏใƒณใฎ้ธๆŠžใƒ—ใƒญใ‚ปใ‚นใฏใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใ€`generate()`ใƒกใ‚ฝใƒƒใƒ‰ใŒไฝฟ็”จใ™ใ‚‹ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใงใใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใฏใ€่จ“็ทดๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅ€คใ‚’ๅค‰ๆ›ดใ—ใพใ›ใ‚“ใŒใ€็”Ÿๆˆใ•ใ‚Œใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎๅ“่ณชใซ้ก•่‘—ใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆๅ†…ใฎ็นฐใ‚Š่ฟ”ใ—ใ‚’ๆธ›ๅฐ‘ใ•ใ›ใ€ใ‚ˆใ‚Šไธ€่ฒซๆ€งใฎใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏไปฅไธ‹ใฎๅ†…ๅฎนใŒ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™๏ผš +* ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ่จญๅฎš +* ไธ€่ˆฌ็š„ใชใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใจใใฎไธป่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟ +* ๐Ÿค— Hubใฎใ‚ใชใŸใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใƒขใƒ‡ใƒซใจใ‚ซใ‚นใ‚ฟใƒ ็”Ÿๆˆ่จญๅฎšใฎไฟๅญ˜ใจๅ…ฑๆœ‰ + +## Default text generation configuration + +ใƒขใƒ‡ใƒซใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฏใ€ใใฎ็”Ÿๆˆ่จญๅฎšใงๅฎš็พฉใ•ใ‚Œใฆใ„ใพใ™ใ€‚[`pipeline`] ๅ†…ใงๆŽจ่ซ–ใซไบ‹ๅ‰่จ“็ทดใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹้š›ใซใฏใ€ใƒขใƒ‡ใƒซใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”Ÿๆˆ่จญๅฎšใ‚’ๅ†…้ƒจใง้ฉ็”จใ™ใ‚‹ `PreTrainedModel.generate()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ่จญๅฎšใฏใ€ใƒขใƒ‡ใƒซใซใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšใŒไฟๅญ˜ใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใซใ‚‚ไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ๆ˜Ž็คบ็š„ใซ่ชญใฟ่พผใ‚€ๅ ดๅˆใ€ใใ‚Œใซไป˜ๅฑžใ™ใ‚‹็”Ÿๆˆ่จญๅฎšใ‚’ `model.generation_config` ใ‚’ไป‹ใ—ใฆ็ขบ่ชใงใใพใ™ใ€‚ + +```python +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +>>> model.generation_config +GenerationConfig { + "bos_token_id": 50256, + "eos_token_id": 50256, +} +``` + +`model.generation_config` ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใจใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”Ÿๆˆ่จญๅฎšใ‹ใ‚‰็•ฐใชใ‚‹ๅ€คใฎใฟใŒ่กจ็คบใ•ใ‚Œใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ€คใฏใƒชใ‚นใƒˆใ•ใ‚Œใพใ›ใ‚“ใ€‚ + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”Ÿๆˆ่จญๅฎšใงใฏใ€ๅ‡บๅŠ›ใฎใ‚ตใ‚คใ‚บใฏๅ…ฅๅŠ›ใƒ—ใƒญใƒณใƒ—ใƒˆใจใฎ็ต„ใฟๅˆใ‚ใ›ใงๆœ€ๅคง20ใƒˆใƒผใ‚ฏใƒณใซๅˆถ้™ใ•ใ‚ŒใฆใŠใ‚Šใ€ใƒชใ‚ฝใƒผใ‚นๅˆถ้™ใซ้”ใ—ใชใ„ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฏ่ฒชๆฌฒๆŽข็ดขใงใ€ๆœ€ใ‚‚็ขบ็އใฎ้ซ˜ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใจใ—ใฆ้ธๆŠžใ™ใ‚‹ๆœ€ใ‚‚ๅ˜็ด”ใชใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใงใ™ใ€‚ๅคšใใฎใ‚ฟใ‚นใ‚ฏใ‚„ๅฐใ•ใชๅ‡บๅŠ›ใ‚ตใ‚คใ‚บใฎๅ ดๅˆใ€ใ“ใ‚Œใฏใ†ใพใๆฉŸ่ƒฝใ—ใพใ™ใ€‚ใŸใ ใ—ใ€้•ทใ„ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅ ดๅˆใ€่ฒชๆฌฒๆŽข็ดขใฏ้ซ˜ๅบฆใซ็นฐใ‚Š่ฟ”ใ•ใ‚Œใ‚‹็ตๆžœใ‚’็”Ÿๆˆใ—ๅง‹ใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Customize text generation + +`generate` ใƒกใ‚ฝใƒƒใƒ‰ใซ็›ดๆŽฅใƒ‘ใƒฉใƒกใƒผใ‚ฟใจใใฎๅ€คใ‚’ๆธกใ™ใ“ใจใงใ€`generation_config` ใ‚’ไธŠๆ›ธใใงใใพใ™ใ€‚ + +```python +>>> my_model.generate(**inputs, num_beams=4, do_sample=True) # doctest: +SKIP +``` + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใŒใปใจใ‚“ใฉใฎใ‚ฟใ‚นใ‚ฏใงใ†ใพใๆฉŸ่ƒฝใ™ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ใ„ใใคใ‹ใฎ่จญๅฎšใ‚’ๅพฎ่ชฟๆ•ดใงใใพใ™ใ€‚ไธ€่ˆฌ็š„ใซ่ชฟๆ•ดใ•ใ‚Œใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™๏ผš + +- `max_new_tokens`: ็”Ÿๆˆใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใฎๆœ€ๅคงๆ•ฐใ€‚ใคใพใ‚Šใ€ๅ‡บๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใ‚ตใ‚คใ‚บใงใ‚ใ‚Šใ€ใƒ—ใƒญใƒณใƒ—ใƒˆๅ†…ใฎใƒˆใƒผใ‚ฏใƒณใฏๅซใพใ‚Œใพใ›ใ‚“ใ€‚ +- `num_beams`: 1ใ‚ˆใ‚Šใ‚‚ๅคงใใชใƒ“ใƒผใƒ ๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใงใ€่ฒชๆฌฒๆคœ็ดขใ‹ใ‚‰ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใฎๆˆฆ็•ฅใงใฏใ€ๅ„ๆ™‚้–“ใ‚นใƒ†ใƒƒใƒ—ใงใ„ใใคใ‹ใฎไปฎ่ชฌใ‚’่ฉ•ไพกใ—ใ€ๆœ€็ต‚็š„ใซๅ…จไฝ“ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพใ™ใ‚‹ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใ‚’ๆŒใคไปฎ่ชฌใ‚’้ธๆŠžใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅˆๆœŸใฎ็ขบ็އใŒไฝŽใ„ใƒˆใƒผใ‚ฏใƒณใงๅง‹ใพใ‚‹้ซ˜็ขบ็އใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒ่ฒชๆฌฒๆคœ็ดขใซใ‚ˆใฃใฆ็„ก่ฆ–ใ•ใ‚Œใ‚‹ใ“ใจใŒใชใใชใ‚Šใพใ™ใ€‚ +- `do_sample`: ใ“ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`True`ใซ่จญๅฎšใ™ใ‚‹ใจใ€ๅคš้ …ๅˆ†ๅธƒใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒๅคš้ …ๅˆ†ๅธƒใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ€Top-Kใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ€Top-pใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใชใฉใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใŒๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๆˆฆ็•ฅใฏใ€ๅ„ๆˆฆ็•ฅๅ›บๆœ‰ใฎ่ชฟๆ•ดใ‚’ๅซใ‚€ๅ˜่ชžๅฝ™ๅ…จไฝ“ใฎ็ขบ็އๅˆ†ๅธƒใ‹ใ‚‰ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’้ธๆŠžใ—ใพใ™ใ€‚ +- `num_return_sequences`: ๅ„ๅ…ฅๅŠ›ใซๅฏพใ—ใฆ่ฟ”ใ™ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ€™่ฃœใฎๆ•ฐใ€‚ใ“ใ‚Œใฏใ€่ค‡ๆ•ฐใฎใ‚ทใƒผใ‚ฑใƒณใ‚นๅ€™่ฃœใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅ๏ผˆใƒ“ใƒผใƒ ใ‚ตใƒผใƒใ‚„ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใฎใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณใชใฉ๏ผ‰ใซใฎใฟ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚่ฒชๆฌฒๆคœ็ดขใ‚„ๅฏพ็…ง็š„ใชๆคœ็ดขใชใฉใ€ๅ˜ไธ€ใฎๅ‡บๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’่ฟ”ใ™ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใงใฏไฝฟ็”จใงใใพใ›ใ‚“ใ€‚ + +## Save a custom decoding strategy with your model + +็‰นๅฎšใฎ็”Ÿๆˆๆง‹ๆˆใง่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ—ใŸใ„ๅ ดๅˆใ€ไปฅไธ‹ใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใงใใพใ™๏ผš +* [`GenerationConfig`] ใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ™ใ‚‹ +* ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ +* [`GenerationConfig.save_pretrained`] ใ‚’ไฝฟ็”จใ—ใฆ็”Ÿๆˆๆง‹ๆˆใ‚’ไฟๅญ˜ใ—ใ€`config_file_name` ๅผ•ๆ•ฐใ‚’็ฉบใซใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ +* `push_to_hub` ใ‚’ `True` ใซ่จญๅฎšใ—ใฆใ€ๆง‹ๆˆใ‚’ใƒขใƒ‡ใƒซใฎใƒชใƒใ‚ธใƒˆใƒชใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ + +```python +>>> from transformers import AutoModelForCausalLM, GenerationConfig + +>>> model = AutoModelForCausalLM.from_pretrained("my_account/my_model") # doctest: +SKIP +>>> generation_config = GenerationConfig( +... max_new_tokens=50, do_sample=True, top_k=50, eos_token_id=model.config.eos_token_id +... ) +>>> generation_config.save_pretrained("my_account/my_model", push_to_hub=True) # doctest: +SKIP +``` + +1ใคใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซ่ค‡ๆ•ฐใฎ็”Ÿๆˆ่จญๅฎšใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‚‚ใงใใ€[`GenerationConfig.save_pretrained`] ใฎ `config_file_name` +ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅพŒใง [`GenerationConfig.from_pretrained`] ใงใ“ใ‚Œใ‚‰ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€1ใคใฎใƒขใƒ‡ใƒซใซๅฏพใ—ใฆ่ค‡ๆ•ฐใฎ็”Ÿๆˆ่จญๅฎšใ‚’ไฟๅญ˜ใ—ใŸใ„ๅ ดๅˆใซไพฟๅˆฉใงใ™ +๏ผˆไพ‹๏ผšใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใŸใ‚ฏใƒชใ‚จใ‚คใƒ†ใ‚ฃใƒ–ใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ็”จใฎ1ใคใจใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใ‚’ไฝฟ็”จใ—ใŸ่ฆ็ด„็”จใฎ1ใค๏ผ‰ใ€‚ใƒขใƒ‡ใƒซใซ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใซใฏใ€้ฉๅˆ‡ใช Hub ๆจฉ้™ใŒๅฟ…่ฆใงใ™ใ€‚ + + +```python +>>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig + +>>> tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-small") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-small") + +>>> translation_generation_config = GenerationConfig( +... num_beams=4, +... early_stopping=True, +... decoder_start_token_id=0, +... eos_token_id=model.config.eos_token_id, +... pad_token=model.config.pad_token_id, +... ) + +>>> # Tip: add `push_to_hub=True` to push to the Hub +>>> translation_generation_config.save_pretrained("/tmp", "translation_generation_config.json") + +>>> # You could then use the named generation config file to parameterize generation +>>> generation_config = GenerationConfig.from_pretrained("/tmp", "translation_generation_config.json") +>>> inputs = tokenizer("translate English to French: Configuration files are easy to use!", return_tensors="pt") +>>> outputs = model.generate(**inputs, generation_config=generation_config) +>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['Les fichiers de configuration sont faciles ร  utiliser!'] +``` + +## Streaming + +`generate()` ใฏใ€ใใฎ `streamer` ๅ…ฅๅŠ›ใ‚’ไป‹ใ—ใฆใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚`streamer` ๅ…ฅๅŠ›ใฏใ€ๆฌกใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆŒใคใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใจไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ™๏ผš`put()` ใจ `end()`ใ€‚ๅ†…้ƒจ็š„ใซใฏใ€`put()` ใฏๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ€`end()` ใฏใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎ็ต‚ไบ†ใ‚’ใƒ•ใƒฉใ‚ฐไป˜ใ‘ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + + +ใ‚นใƒˆใƒชใƒผใƒžใƒผใ‚ฏใƒฉใ‚นใฎAPIใฏใพใ ้–‹็™บไธญใงใ‚ใ‚Šใ€ๅฐ†ๆฅๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ๅฎŸ้š›ใซใฏใ€ใ•ใพใ–ใพใช็›ฎ็š„ใซๅฏพใ—ใฆ็‹ฌ่‡ชใฎใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐใ‚ฏใƒฉใ‚นใ‚’ไฝœๆˆใงใใพใ™๏ผใพใŸใ€ไฝฟ็”จใงใใ‚‹ๅŸบๆœฌ็š„ใชใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐใ‚ฏใƒฉใ‚นใ‚‚็”จๆ„ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไพ‹ใˆใฐใ€[`TextStreamer`] ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆใ€`generate()` ใฎๅ‡บๅŠ›ใ‚’็”ป้ขใซๅ˜่ชžใ”ใจใซใ‚นใƒˆใƒชใƒผใƒ ใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer + +>>> tok = AutoTokenizer.from_pretrained("openai-community/gpt2") +>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") +>>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") +>>> streamer = TextStreamer(tok) + +>>> # Despite returning the usual output, the streamer will also print the generated text to stdout. +>>> _ = model.generate(**inputs, streamer=streamer, max_new_tokens=20) +An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven, +``` + +## Decoding strategies + +็‰นๅฎšใฎ `generate()` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ็ต„ใฟๅˆใ‚ใ›ใ€ใใ—ใฆๆœ€็ต‚็š„ใซ `generation_config` ใฏใ€็‰นๅฎšใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚ใ“ใฎใ‚ณใƒณใ‚ปใƒ—ใƒˆใŒๆ–ฐใ—ใ„ๅ ดๅˆใ€[ใ“ใฎใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://huggingface.co/blog/how-to-generate)ใ‚’่ชญใ‚€ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใฎใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆใงใฏใ€ไธ€่ˆฌ็š„ใชใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใŒใฉใฎใ‚ˆใ†ใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใŒ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใ“ใงใฏใ€ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๅˆถๅพกใ™ใ‚‹ใ„ใใคใ‹ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’็คบใ—ใ€ใใ‚Œใ‚‰ใ‚’ใฉใฎใ‚ˆใ†ใซไฝฟ็”จใงใใ‚‹ใ‹ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +### Greedy Search + +[`generate`] ใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง่ฒชๆฌฒๆŽข็ดขใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใ€ๆœ‰ๅŠนใซใ™ใ‚‹ใŸใ‚ใซใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆธกใ™ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟ `num_beams` ใŒ 1 ใซ่จญๅฎšใ•ใ‚Œใ€`do_sample=False` ใงใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "I look forward to" +>>> checkpoint = "distilbert/distilgpt2" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> outputs = model.generate(**inputs) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['I look forward to seeing you all again!\n\n\n\n\n\n\n\n\n\n\n'] +``` + +### Contrastive search + +ใ‚ณใƒณใƒˆใƒฉใ‚นใƒ†ใ‚ฃใƒ–ๆคœ็ดขใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฏใ€2022ๅนดใฎ่ซ–ๆ–‡[A Contrastive Framework for Neural Text Generation](https://huggingface.co/papers/2202.06417)ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ“ใ‚Œใฏใ€้žๅๅพฉ็š„ใงใ‚ใ‚ŠใชใŒใ‚‰ไธ€่ฒซๆ€งใฎใ‚ใ‚‹้•ทใ„ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซๅ„ชใ‚ŒใŸ็ตๆžœใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ‚ณใƒณใƒˆใƒฉใ‚นใƒ†ใ‚ฃใƒ–ๆคœ็ดขใฎๅ‹•ไฝœๅŽŸ็†ใ‚’ๅญฆใถใซใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://huggingface.co/blog/introducing-csearch)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ +ใ‚ณใƒณใƒˆใƒฉใ‚นใƒ†ใ‚ฃใƒ–ๆคœ็ดขใฎๅ‹•ไฝœใ‚’ๆœ‰ๅŠนใซใ—ใ€ๅˆถๅพกใ™ใ‚‹2ใคใฎไธป่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€Œpenalty_alphaใ€ใจใ€Œtop_kใ€ใงใ™๏ผš + +```python +>>> from transformers import AutoTokenizer, AutoModelForCausalLM + +>>> checkpoint = "openai-community/gpt2-large" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> prompt = "Hugging Face Company is" +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> outputs = model.generate(**inputs, penalty_alpha=0.6, top_k=4, max_new_tokens=100) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Hugging Face Company is a family owned and operated business. We pride ourselves on being the best +in the business and our customer service is second to none.\n\nIf you have any questions about our +products or services, feel free to contact us at any time. We look forward to hearing from you!'] +``` + +### Multinomial sampling + +ๅธธใซๆœ€้ซ˜็ขบ็އใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใจใ—ใฆ้ธๆŠžใ™ใ‚‹่ฒชๆฌฒๆคœ็ดขใจใฏ็•ฐใชใ‚Šใ€ๅคš้ …ๅˆ†ๅธƒใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ๏ผˆใพใŸใฏ็ฅ–ๅ…ˆใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใจใ‚‚ๅ‘ผใฐใ‚Œใพใ™๏ผ‰ใฏใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹่ชžๅฝ™ๅ…จไฝ“ใฎ็ขบ็އๅˆ†ๅธƒใซๅŸบใฅใ„ใฆๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ใƒฉใƒณใƒ€ใƒ ใซ้ธๆŠžใ—ใพใ™ใ€‚ใ‚ผใƒญไปฅๅค–ใฎ็ขบ็އใ‚’ๆŒใคใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใซใฏ้ธๆŠžใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใ€ใ“ใ‚Œใซใ‚ˆใ‚Š็นฐใ‚Š่ฟ”ใ—ใฎใƒชใ‚นใ‚ฏใŒๆธ›ๅฐ‘ใ—ใพใ™ใ€‚ + +ๅคš้ …ๅˆ†ๅธƒใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`do_sample=True` ใŠใ‚ˆใณ `num_beams=1` ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + +```python +>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed +>>> set_seed(0) # For reproducibility + +>>> checkpoint = "openai-community/gpt2-large" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> prompt = "Today was an amazing day because" +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> outputs = model.generate(**inputs, do_sample=True, num_beams=1, max_new_tokens=100) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Today was an amazing day because when you go to the World Cup and you don\'t, or when you don\'t get invited, +that\'s a terrible feeling."'] +``` + +### Beam-search decoding + +่ฒชๆฌฒๆŽข็ดขใจใฏ็•ฐใชใ‚Šใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฏๅ„ๆ™‚้–“ใ‚นใƒ†ใƒƒใƒ—ใงใ„ใใคใ‹ใฎไปฎ่ชฌใ‚’ไฟๆŒใ—ใ€ๆœ€็ต‚็š„ใซใ‚ทใƒผใ‚ฑใƒณใ‚นๅ…จไฝ“ใงๆœ€ใ‚‚็ขบ็އใŒ้ซ˜ใ„ไปฎ่ชฌใ‚’้ธๆŠžใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่ฒชๆฌฒๆŽข็ดขใงใฏ็„ก่ฆ–ใ•ใ‚Œใฆใ—ใพใ†ๅˆๆœŸใƒˆใƒผใ‚ฏใƒณใฎ็ขบ็އใŒไฝŽใ„้ซ˜็ขบ็އใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็‰นๅฎšใ™ใ‚‹ๅˆฉ็‚นใŒใ‚ใ‚Šใพใ™ใ€‚ + + + + + +ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฎๅ‹•ไฝœใ‚’[ใ“ใฎใ‚คใƒณใ‚ฟใƒฉใ‚ฏใƒ†ใ‚ฃใƒ–ใƒ‡ใƒข](https://huggingface.co/spaces/m-ric/beam_search_visualizer)ใง็ขบ่ชใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆ–‡็ซ ใ‚’ๅ…ฅๅŠ›ใ—ใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ใ„ใ˜ใ‚‹ใ“ใจใงใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใƒ“ใƒผใƒ ใŒใฉใฎใ‚ˆใ†ใซๅค‰ๅŒ–ใ™ใ‚‹ใ‹ใ‚’็Ÿฅใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ใ“ใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`num_beams`๏ผˆ่ฟฝ่ทกใ™ใ‚‹ไปฎ่ชฌใฎๆ•ฐ๏ผ‰ใ‚’1ใ‚ˆใ‚Šใ‚‚ๅคงใใชๅ€คใซๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "It is astonishing how one can" +>>> checkpoint = "openai-community/gpt2-medium" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, max_new_tokens=50) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['It is astonishing how one can have such a profound impact on the lives of so many people in such a short period of +time."\n\nHe added: "I am very proud of the work I have been able to do in the last few years.\n\n"I have'] +``` + +### Beam-search multinomial sampling + +ใใฎๅๅ‰ใ‹ใ‚‰ใ‚‚ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซใ€ใ“ใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฏใƒ“ใƒผใƒ ใ‚ตใƒผใƒใจๅคš้ …ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ„ใพใ™ใ€‚ใ“ใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€`num_beams` ใ‚’1ใ‚ˆใ‚Šๅคงใใชๅ€คใซ่จญๅฎšใ—ใ€`do_sample=True` ใ‚’่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, set_seed +>>> set_seed(0) # For reproducibility + +>>> prompt = "translate English to German: The house is wonderful." +>>> checkpoint = "google-t5/t5-small" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, do_sample=True) +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Das Haus ist wunderbar.' +``` + +### Diverse beam search decoding + +ๅคšๆง˜ใชใƒ“ใƒผใƒ ใ‚ตใƒผใƒใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใฏใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒๆˆฆ็•ฅใฎๆ‹กๅผตใงใ‚ใ‚Šใ€้ธๆŠž่‚ขใ‹ใ‚‰ใ‚ˆใ‚Šๅคšๆง˜ใชใƒ“ใƒผใƒ ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็”Ÿๆˆใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใ“ใฎไป•็ต„ใฟใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models](https://huggingface.co/papers/1610.02424) ใ‚’ใ”ๅ‚็…งใใ ใ•ใ„ใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใซใฏใ€`num_beams`ใ€`num_beam_groups`ใ€ใŠใ‚ˆใณ `diversity_penalty` ใจใ„ใ†3ใคใฎไธป่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใพใ™ใ€‚ๅคšๆง˜ๆ€งใƒšใƒŠใƒซใƒ†ใ‚ฃใฏใ€ๅ‡บๅŠ›ใŒใ‚ฐใƒซใƒผใƒ—ใ”ใจใซ็•ฐใชใ‚‹ใ“ใจใ‚’ไฟ่จผใ—ใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใฏๅ„ใ‚ฐใƒซใƒผใƒ—ๅ†…ใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + +```python +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + +>>> checkpoint = "google/pegasus-xsum" +>>> prompt = ( +... "The Permaculture Design Principles are a set of universal design principles " +... "that can be applied to any location, climate and culture, and they allow us to design " +... "the most efficient and sustainable human habitation and food production systems. " +... "Permaculture is a design system that encompasses a wide variety of disciplines, such " +... "as ecology, landscape design, environmental science and energy conservation, and the " +... "Permaculture design principles are drawn from these various disciplines. Each individual " +... "design principle itself embodies a complete conceptual framework based on sound " +... "scientific principles. When we bring all these separate principles together, we can " +... "create a design system that both looks at whole systems, the parts that these systems " +... "consist of, and how those parts interact with each other to create a complex, dynamic, " +... "living system. Each design principle serves as a tool that allows us to integrate all " +... "the separate parts of a design, referred to as elements, into a functional, synergistic, " +... "whole system, where the elements harmoniously interact and work together in the most " +... "efficient way possible." +... ) + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, num_beam_groups=5, max_new_tokens=30, diversity_penalty=1.0) +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'The Design Principles are a set of universal design principles that can be applied to any location, climate and +culture, and they allow us to design the' +``` + +### Assisted Decoding + +ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฏใ€ไธŠ่จ˜ใฎใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๅค‰ๆ›ดใ—ใŸใ‚‚ใฎใงใ€ๅŒใ˜ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ๏ผˆ็†ๆƒณ็š„ใซใฏใฏใ‚‹ใ‹ใซๅฐใ•ใชใƒขใƒ‡ใƒซ๏ผ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ„ใใคใ‹ใฎๅ€™่ฃœใƒˆใƒผใ‚ฏใƒณใ‚’่ฒชๆฌฒใซ็”Ÿๆˆใ™ใ‚‹ใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใใฎๅพŒใ€ไธป่ฆใชใƒขใƒ‡ใƒซใฏๅ€™่ฃœใƒˆใƒผใ‚ฏใƒณใ‚’1ใคใฎๅ‰ๅ‘ใใƒ‘ใ‚นใงๆคœ่จผใ—ใ€ใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใ‚’้ซ˜้€ŸๅŒ–ใ—ใพใ™ใ€‚็พๅœจใ€ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใงใฏ่ฒชๆฌฒๆคœ็ดขใจใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใฎใฟใŒใ‚ตใƒใƒผใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ใƒใƒƒใƒๅ…ฅๅŠ›ใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://huggingface.co/blog/assisted-generation) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`assistant_model` ๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใง่จญๅฎšใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ€ใ•ใพใ–ใพใชใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ไธป่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซ้ซ˜ๅบฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฏ [`generate`] ใƒกใ‚ฝใƒƒใƒ‰ใซๅญ˜ๅœจใ—ใ€[`generate`] ใƒกใ‚ฝใƒƒใƒ‰ใฎๅ‹•ไฝœใ‚’ใ•ใ‚‰ใซๅˆถๅพกใงใใพใ™ใ€‚ไฝฟ็”จๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๅฎŒๅ…จใชใƒชใ‚นใƒˆใซใคใ„ใฆใฏใ€[APIใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](./main_classes/text_generation) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "Alice and Bob" +>>> checkpoint = "EleutherAI/pythia-1.4b-deduped" +>>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) +>>> outputs = model.generate(**inputs, assistant_model=assistant_model) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Alice and Bob are sitting in a bar. Alice is drinking a beer and Bob is drinking a'] +``` + +ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใงใฏ `temperature` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ๅคš้ …ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใจๅŒๆง˜ใซใƒฉใƒณใƒ€ใƒ ๆ€งใ‚’ๅˆถๅพกใงใใพใ™ใ€‚ใŸใ ใ—ใ€ใ‚ขใ‚ทใ‚นใƒˆใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใงใฏใ€ๆธฉๅบฆใ‚’ไฝŽใใ™ใ‚‹ใ“ใจใง้…ๅปถใฎๆ”นๅ–„ใซๅฝน็ซ‹ใกใพใ™ใ€‚ + + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed +>>> set_seed(42) # For reproducibility + +>>> prompt = "Alice and Bob" +>>> checkpoint = "EleutherAI/pythia-1.4b-deduped" +>>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) +>>> outputs = model.generate(**inputs, assistant_model=assistant_model, do_sample=True, temperature=0.5) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Alice and Bob are going to the same party. It is a small party, in a small'] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/glossary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/glossary.md new file mode 100644 index 0000000000000000000000000000000000000000..775bffdd0c694e01976a8ec427452dce75610f5a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/glossary.md @@ -0,0 +1,444 @@ + + +# Glossary + +ใ“ใฎ็”จ่ชž้›†ใฏใ€ไธ€่ˆฌ็š„ใชๆฉŸๆขฐๅญฆ็ฟ’ใจ ๐Ÿค— ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎ็”จ่ชžใ‚’ๅฎš็พฉใ—ใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ใ‚ˆใ‚Š็†่งฃใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +## A + +### attention mask + +ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒžใ‚นใ‚ฏใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹้š›ใซไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎๅผ•ๆ•ฐใงใ™ใ€‚ + + + +ใ“ใฎๅผ•ๆ•ฐใฏใ€ใƒขใƒ‡ใƒซใซใฉใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆณจ่ฆ–ใ™ในใใ‹ใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆณจ่ฆ–ใ—ใชใ„ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +ไพ‹ใˆใฐใ€ๆฌกใฎ2ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’่€ƒใˆใฆใฟใฆใใ ใ•ใ„๏ผš + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence_a = "This is a short sequence." +>>> sequence_b = "This is a rather long sequence. It is at least longer than the sequence A." + +>>> encoded_sequence_a = tokenizer(sequence_a)["input_ids"] +>>> encoded_sequence_b = tokenizer(sequence_b)["input_ids"] +``` + +The encoded versions have different lengths: + +```python +>>> len(encoded_sequence_a), len(encoded_sequence_b) +(8, 19) +``` + +ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใ‚‰ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใใฎใพใพๅŒใ˜ใƒ†ใƒณใ‚ฝใƒซใซ้…็ฝฎใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ๆœ€ๅˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฏใ€ +2็•ช็›ฎใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใซๅˆใ‚ใ›ใฆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใพใŸใฏใ€2็•ช็›ฎใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฏใ€ๆœ€ๅˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ +้•ทใ•ใซๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€ๅˆใฎๅ ดๅˆใ€IDใฎใƒชใ‚นใƒˆใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงๆ‹กๅผตใ•ใ‚Œใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใƒชใ‚นใƒˆใ‚’ๆธกใ—ใ€ๆฌกใฎใ‚ˆใ†ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ‚ˆใ†ใซ +ไพ้ ผใงใใพใ™: + + +```python +>>> padded_sequences = tokenizer([sequence_a, sequence_b], padding=True) +``` + +0sใŒ่ฟฝๅŠ ใ•ใ‚Œใฆใ€ๆœ€ๅˆใฎๆ–‡ใŒ2็•ช็›ฎใฎๆ–‡ใจๅŒใ˜้•ทใ•ใซใชใ‚‹ใฎใŒใ‚ใ‹ใ‚Šใพใ™๏ผš + +```python +>>> padded_sequences["input_ids"] +[[101, 1188, 1110, 170, 1603, 4954, 119, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [101, 1188, 1110, 170, 1897, 1263, 4954, 119, 1135, 1110, 1120, 1655, 2039, 1190, 1103, 4954, 138, 119, 102]] +``` + +ใ“ใ‚Œใฏใ€PyTorchใพใŸใฏTensorFlowใงใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใงใใพใ™ใ€‚ๆณจๆ„ใƒžใ‚นใ‚ฏใฏใ€ใƒขใƒ‡ใƒซใŒใใ‚Œใ‚‰ใซๆณจๆ„ใ‚’ๆ‰•ใ‚ใชใ„ใ‚ˆใ†ใซใ€ๅŸ‹ใ‚่พผใพใ‚ŒใŸใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฎไฝ็ฝฎใ‚’็คบใ™ใƒใ‚คใƒŠใƒชใƒ†ใƒณใ‚ฝใƒซใงใ™ใ€‚[`BertTokenizer`]ใงใฏใ€`1`ใฏๆณจๆ„ใ‚’ๆ‰•ใ†ๅฟ…่ฆใŒใ‚ใ‚‹ๅ€คใ‚’็คบใ—ใ€`0`ใฏๅŸ‹ใ‚่พผใพใ‚ŒใŸๅ€คใ‚’็คบใ—ใพใ™ใ€‚ใ“ใฎๆณจๆ„ใƒžใ‚นใ‚ฏใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒ่ฟ”ใ™่พžๆ›ธใฎใ‚ญใƒผใ€Œattention_maskใ€ใฎไธ‹ใซใ‚ใ‚Šใพใ™ใ€‚ + + + +```python +>>> padded_sequences["attention_mask"] +[[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] +``` + +### autoencoding models + +[ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซ](#encoder-models) ใŠใ‚ˆใณ [ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](#masked-language-modeling-mlm) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### autoregressive models + +[ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](#causal-language-modeling) ใŠใ‚ˆใณ [ใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซ](#decoder-models) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## B + +### backbone + +ใƒใƒƒใ‚ฏใƒœใƒผใƒณใฏใ€็”Ÿใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚„็‰นๅพดใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆๅŸ‹ใ‚่พผใฟใจๅฑค๏ผ‰ใงใ™ใ€‚้€šๅธธใ€็‰นๅพดใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ใŸใ‚ใซ [ใƒ˜ใƒƒใƒ‰](#head) ใซๆŽฅ็ถšใ•ใ‚ŒใฆใŠใ‚Šใ€ไบˆๆธฌใ‚’่กŒใ„ใพใ™ใ€‚ใŸใจใˆใฐใ€[`ViTModel`] ใฏ็‰นๅฎšใฎใƒ˜ใƒƒใƒ‰ใŒไธŠใซใชใ„ใƒใƒƒใ‚ฏใƒœใƒผใƒณใงใ™ใ€‚ไป–ใฎใƒขใƒ‡ใƒซใ‚‚ [`VitModel`] ใ‚’ใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆไฝฟ็”จใงใใพใ™ใ€ไพ‹ใˆใฐ [DPT](model_doc/dpt) ใงใ™ใ€‚ + +## C + +### causal language modeling + +ใƒขใƒ‡ใƒซใŒใƒ†ใ‚ญใ‚นใƒˆใ‚’้ †็•ชใซ่ชญใฟใ€ๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚้€šๅธธใ€ใƒขใƒ‡ใƒซใฏๆ–‡ๅ…จไฝ“ใ‚’่ชญใฟๅ–ใ‚Šใพใ™ใŒใ€็‰นๅฎšใฎใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใงๆœชๆฅใฎใƒˆใƒผใ‚ฏใƒณใ‚’้š ใ™ใŸใ‚ใซใƒขใƒ‡ใƒซๅ†…ใงใƒžใ‚นใ‚ฏใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +### channel + +ใ‚ซใƒฉใƒผ็”ปๅƒใฏใ€่ตคใ€็ท‘ใ€้’๏ผˆRGB๏ผ‰ใฎ3ใคใฎใƒใƒฃใƒใƒซใฎๅ€คใฎ็ต„ใฟๅˆใ‚ใ›ใ‹ใ‚‰ๆˆใ‚Š็ซ‹ใฃใฆใŠใ‚Šใ€ใ‚ฐใƒฌใƒผใ‚นใ‚ฑใƒผใƒซ็”ปๅƒใฏ1ใคใฎใƒใƒฃใƒใƒซใ—ใ‹ๆŒใกใพใ›ใ‚“ใ€‚๐Ÿค— Transformers ใงใฏใ€ใƒใƒฃใƒใƒซใฏ็”ปๅƒใฎใƒ†ใƒณใ‚ฝใƒซใฎๆœ€ๅˆใพใŸใฏๆœ€ๅพŒใฎๆฌกๅ…ƒใซใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผš[`n_channels`, `height`, `width`] ใพใŸใฏ [`height`, `width`, `n_channels`]ใ€‚ + +### connectionist temporal classification (CTC) + +ๅ…ฅๅŠ›ใจๅ‡บๅŠ›ใŒๆญฃ็ขบใซใฉใฎใ‚ˆใ†ใซๆ•ดๅˆ—ใ™ใ‚‹ใ‹ใ‚’ๆญฃ็ขบใซ็Ÿฅใ‚‰ใชใใฆใ‚‚ใƒขใƒ‡ใƒซใ‚’ๅญฆ็ฟ’ใ•ใ›ใ‚‹ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ€‚CTC ใฏใ€็‰นๅฎšใฎๅ…ฅๅŠ›ใซๅฏพใ—ใฆใ™ในใฆใฎๅฏ่ƒฝใชๅ‡บๅŠ›ใฎๅˆ†ๅธƒใ‚’่จˆ็ฎ—ใ—ใ€ใใฎไธญใ‹ใ‚‰ๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ๅ‡บๅŠ›ใ‚’้ธๆŠžใ—ใพใ™ใ€‚CTC ใฏใ€ใ‚นใƒ”ใƒผใ‚ซใƒผใฎ็•ฐใชใ‚‹็™บ่ฉฑ้€Ÿๅบฆใชใฉใ€ใ•ใพใ–ใพใช็†็”ฑใง้ŸณๅฃฐใŒใƒˆใƒฉใƒณใ‚นใ‚ฏใƒชใƒ—ใƒˆใจๅฎŒๅ…จใซๆ•ดๅˆใ—ใชใ„ๅ ดๅˆใซใ€้Ÿณๅฃฐ่ช่ญ˜ใ‚ฟใ‚นใ‚ฏใงไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +### convolution + +ใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎไธ€็จฎใงใ€ๅ…ฅๅŠ›่กŒๅˆ—ใŒ่ฆ็ด ใ”ใจใซๅฐใ•ใช่กŒๅˆ—๏ผˆใ‚ซใƒผใƒใƒซใพใŸใฏใƒ•ใ‚ฃใƒซใ‚ฟใƒผ๏ผ‰ใจไน—็ฎ—ใ•ใ‚Œใ€ๅ€คใŒๆ–ฐใ—ใ„่กŒๅˆ—ใซๅˆ่จˆใ•ใ‚Œใ‚‹ใƒฌใ‚คใƒคใƒผใฎใ‚ฟใ‚คใƒ—ใ€‚ใ“ใ‚Œใฏๅ…ฅๅŠ›่กŒๅˆ—ๅ…จไฝ“ใซๅฏพใ—ใฆ็นฐใ‚Š่ฟ”ใ•ใ‚Œใ‚‹็•ณใฟ่พผใฟๆ“ไฝœใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใ€ๅ„ๆ“ไฝœใฏๅ…ฅๅŠ›่กŒๅˆ—ใฎ็•ฐใชใ‚‹ใ‚ปใ‚ฐใƒกใƒณใƒˆใซ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆCNN๏ผ‰ใฏใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใงไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## D + +### decoder input IDs + +ใ“ใฎๅ…ฅๅŠ›ใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใซ็‰นๆœ‰ใงใ‚ใ‚Šใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใซไพ›็ตฆใ•ใ‚Œใ‚‹ๅ…ฅๅŠ›IDใ‚’ๅซใฟใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅ…ฅๅŠ›ใฏใ€็ฟป่จณใ‚„่ฆ็ด„ใชใฉใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใƒ„ใƒผใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใ•ใ‚Œใ€้€šๅธธใ€ๅ„ใƒขใƒ‡ใƒซใซๅ›บๆœ‰ใฎๆ–นๆณ•ใงๆง‹็ฏ‰ใ•ใ‚Œใพใ™ใ€‚ + +ใปใจใ‚“ใฉใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซ๏ผˆBARTใ€T5๏ผ‰ใฏใ€`labels` ใ‹ใ‚‰็‹ฌ่‡ชใซ `decoder_input_ids` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ“ใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใงใฏใ€`labels` ใ‚’ๆธกใ™ใ“ใจใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ‡ฆ็†ใ™ใ‚‹ๅ„ชใ‚ŒใŸๆ–นๆณ•ใงใ™ใ€‚ + +ใ‚ทใƒผใ‚ฑใƒณใ‚นใƒ„ใƒผใ‚ทใƒผใ‚ฑใƒณใ‚นใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใŠใ‘ใ‚‹ใ“ใ‚Œใ‚‰ใฎๅ…ฅๅŠ›IDใฎๅ‡ฆ็†ๆ–นๆณ•ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ๅ„ใƒขใƒ‡ใƒซใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### decoder models + +ใ‚ชใƒผใƒˆใƒชใ‚ฐใƒฌใƒƒใ‚ทใƒงใƒณใƒขใƒ‡ใƒซใจใ‚‚ๅ‘ผใฐใ‚Œใ€ใƒขใƒ‡ใƒซใŒใƒ†ใ‚ญใ‚นใƒˆใ‚’้ †็•ชใซ่ชญใฟใ€ๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฟใ‚นใ‚ฏ๏ผˆๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ๏ผ‰ใซ้–ขไธŽใ—ใพใ™ใ€‚้€šๅธธใ€ใƒขใƒ‡ใƒซใฏๆ–‡ๅ…จไฝ“ใ‚’่ชญใฟๅ–ใ‚Šใ€็‰นๅฎšใฎใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใงๆœชๆฅใฎใƒˆใƒผใ‚ฏใƒณใ‚’้š ใ™ใƒžใ‚นใ‚ฏใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ + + + + +### deep learning (DL) + +ใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚’ไฝฟ็”จใ™ใ‚‹ๆฉŸๆขฐๅญฆ็ฟ’ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใ€่ค‡ๆ•ฐใฎๅฑคใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ + +## E + +### encoder models + +ใ‚ชใƒผใƒˆใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใƒขใƒ‡ใƒซใจใ—ใฆใ‚‚็Ÿฅใ‚‰ใ‚ŒใฆใŠใ‚Šใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฏๅ…ฅๅŠ›๏ผˆใƒ†ใ‚ญใ‚นใƒˆใ‚„็”ปๅƒใชใฉ๏ผ‰ใ‚’ใ€ๅŸ‹ใ‚่พผใฟใจๅ‘ผใฐใ‚Œใ‚‹็ฐก็•ฅๅŒ–ใ•ใ‚ŒใŸๆ•ฐๅ€ค่กจ็พใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฏใ€ใ—ใฐใ—ใฐ[ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ๏ผˆ#masked-language-modeling-mlm๏ผ‰](#masked-language-modeling-mlm)ใชใฉใฎๆŠ€่ก“ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ€ๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎไธ€้ƒจใ‚’ใƒžใ‚นใ‚ฏใ—ใ€ใƒขใƒ‡ใƒซใซใ‚ˆใ‚Šๆ„ๅ‘ณใฎใ‚ใ‚‹่กจ็พใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใŒๅผทๅˆถใ•ใ‚Œใพใ™ใ€‚ + + + +## F + +### feature extraction + +็”Ÿใƒ‡ใƒผใ‚ฟใ‚’ใ‚ˆใ‚Šๆƒ…ๅ ฑ่ฑŠใ‹ใงๆฉŸๆขฐๅญฆ็ฟ’ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใซใจใฃใฆๆœ‰็”จใช็‰นๅพดใฎใ‚ปใƒƒใƒˆใซ้ธๆŠžใŠใ‚ˆใณๅค‰ๆ›ใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใ€‚็‰นๅพดๆŠฝๅ‡บใฎไพ‹ใซใฏใ€็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ˜่ชžๅŸ‹ใ‚่พผใฟใซๅค‰ๆ›ใ—ใŸใ‚Šใ€็”ปๅƒ/ใƒ“ใƒ‡ใ‚ชใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ใ‚จใƒƒใ‚ธใ‚„ๅฝข็Šถใชใฉใฎ้‡่ฆใช็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใŒๅซใพใ‚Œใพใ™ใ€‚ + +### feed forward chunking + +ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผๅ†…ใฎๅ„ๆฎ‹ๅทฎๆณจๆ„ใƒ–ใƒญใƒƒใ‚ฏใงใฏใ€้€šๅธธใ€่‡ชๅทฑๆณจๆ„ๅฑคใฎๅพŒใซ2ใคใฎใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅฑคใŒ็ถšใใพใ™ใ€‚ +ใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅฑคใฎไธญ้–“ๅŸ‹ใ‚่พผใฟใ‚ตใ‚คใ‚บใฏใ€ใƒขใƒ‡ใƒซใฎ้š ใ‚ŒใŸใ‚ตใ‚คใ‚บใ‚ˆใ‚Šใ‚‚ๅคงใใ„ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™๏ผˆใŸใจใˆใฐใ€`google-bert/bert-base-uncased`ใฎๅ ดๅˆ๏ผ‰ใ€‚ + +ๅ…ฅๅŠ›ใ‚ตใ‚คใ‚บใŒ `[batch_sizeใ€sequence_length]` ใฎๅ ดๅˆใ€ไธญ้–“ใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅŸ‹ใ‚่พผใฟ `[batch_sizeใ€sequence_lengthใ€config.intermediate_size]` ใ‚’ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใƒกใƒขใƒชใฏใ€ใƒกใƒขใƒชใฎๅคง้ƒจๅˆ†ใ‚’ๅ ใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚[Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451)ใฎ่‘—่€…ใฏใ€่จˆ็ฎ—ใŒ `sequence_length` ๆฌกๅ…ƒใซไพๅญ˜ใ—ใชใ„ใŸใ‚ใ€ไธกๆ–นใฎใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅฑคใฎๅ‡บๅŠ›ๅŸ‹ใ‚่พผใฟ `[batch_sizeใ€config.hidden_size]_0ใ€...ใ€[batch_sizeใ€config.hidden_size]_n` ใ‚’ๅ€‹ๅˆฅใซ่จˆ็ฎ—ใ—ใ€ๅพŒใง `[batch_sizeใ€sequence_lengthใ€config.hidden_size]` ใซ้€ฃ็ตใ™ใ‚‹ใ“ใจใฏๆ•ฐๅญฆ็š„ใซ็ญ‰ไพกใงใ‚ใ‚‹ใจๆฐ—ไป˜ใใพใ—ใŸใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅข—ๅŠ ใ—ใŸ่จˆ็ฎ—ๆ™‚้–“ใจใƒกใƒขใƒชไฝฟ็”จ้‡ใฎใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใŒ็”Ÿใ˜ใพใ™ใŒใ€ๆ•ฐๅญฆ็š„ใซ็ญ‰ไพกใช็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +[`apply_chunking_to_forward`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€`chunk_size` ใฏไธฆๅˆ—ใซ่จˆ็ฎ—ใ•ใ‚Œใ‚‹ๅ‡บๅŠ›ๅŸ‹ใ‚่พผใฟใฎๆ•ฐใ‚’ๅฎš็พฉใ—ใ€ใƒกใƒขใƒชใจๆ™‚้–“ใฎ่ค‡้›‘ใ•ใฎใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚`chunk_size` ใŒ 0 ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใฎใƒใƒฃใƒณใ‚ญใƒณใ‚ฐใฏ่กŒใ‚ใ‚Œใพใ›ใ‚“ใ€‚ + +### finetuned models + +ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใฏใ€ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅ–ใ‚Šใ€ใใฎ้‡ใฟใ‚’ๅ›บๅฎšใ—ใ€ๆ–ฐใ—ใ่ฟฝๅŠ ใ•ใ‚ŒใŸ[model head](#head)ใงๅ‡บๅŠ›ใƒฌใ‚คใƒคใƒผใ‚’็ฝฎใๆ›ใˆใ‚‹ๅฝขๅผใฎ่ปข็งปๅญฆ็ฟ’ใงใ™ใ€‚ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใฏๅฏพ่ฑกใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Fine-tune a pretrained model](https://huggingface.co/docs/transformers/training) ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ๅ‚็…งใ—ใฆใ€๐Ÿค— Transformersใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใ‚’ๅญฆใณใพใ—ใ‚‡ใ†ใ€‚ + +## H + +### head + +ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใฏใ€ใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎๆœ€ๅพŒใฎใƒฌใ‚คใƒคใƒผใ‚’ๆŒ‡ใ—ใ€็”Ÿใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใฆ็•ฐใชใ‚‹ๆฌกๅ…ƒใซๅฐ„ๅฝฑใ—ใพใ™ใ€‚ๅ„ใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆ็•ฐใชใ‚‹ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐ๏ผš + + * [`GPT2ForSequenceClassification`] ใฏใ€ใƒ™ใƒผใ‚นใฎ[`GPT2Model`]ใฎไธŠใซใ‚ใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰๏ผˆ็ทšๅฝขๅฑค๏ผ‰ใงใ™ใ€‚ + * [`ViTForImageClassification`] ใฏใ€ใƒ™ใƒผใ‚นใฎ[`ViTModel`]ใฎ`CLS`ใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚้š ใ‚ŒใŸ็Šถๆ…‹ใฎไธŠใซใ‚ใ‚‹็”ปๅƒๅˆ†้กžใƒ˜ใƒƒใƒ‰๏ผˆ็ทšๅฝขๅฑค๏ผ‰ใงใ™ใ€‚ + * [`Wav2Vec2ForCTC`] ใฏใ€[CTC](#connectionist-temporal-classification-ctc)ใ‚’ๆŒใคใƒ™ใƒผใ‚นใฎ[`Wav2Vec2Model`]ใฎ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ˜ใƒƒใƒ‰ใงใ™ใ€‚ + +## I + +### image patch + +ใƒ“ใ‚ธใƒงใƒณใƒ™ใƒผใ‚นใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใฏใ€็”ปๅƒใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใ€ใใ‚Œใ‚‰ใ‚’็ทšๅฝขใซๅŸ‹ใ‚่พผใฟใ€ใƒขใƒ‡ใƒซใซใ‚ทใƒผใ‚ฑใƒณใ‚นใจใ—ใฆๆธกใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎ + +### inference + +ๆŽจ่ซ–ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸๅพŒใซๆ–ฐใ—ใ„ใƒ‡ใƒผใ‚ฟใงใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ™ใ€‚ ๐Ÿค— Transformers ใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆใฏใ€[ๆŽจ่ซ–ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ](https://huggingface.co/docs/transformers/pipeline_tutorial) ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### input IDs + +ๅ…ฅๅŠ›IDใฏใ€ใƒขใƒ‡ใƒซใธใฎๅ…ฅๅŠ›ใจใ—ใฆๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎไธญใงๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชใ‚‚ใฎใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใƒˆใƒผใ‚ฏใƒณใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐๅ€ค่กจ็พใงใ™ใ€‚ + + + +ๅ„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏ็•ฐใชใ‚‹ๆ–นๆณ•ใงๅ‹•ไฝœใ—ใพใ™ใŒใ€ๅŸบๆœฌ็š„ใชใƒกใ‚ซใƒ‹ใ‚บใƒ ใฏๅŒใ˜ใงใ™ใ€‚ไปฅไธ‹ใฏBERTใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใŸไพ‹ใงใ™ใ€‚BERTใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏ[WordPiece](https://huggingface.co/papers/1609.08144)ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ™ใ€‚ + + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence = "A Titan RTX has 24GB of VRAM" +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ่ชžๅฝ™ใงไฝฟ็”จๅฏ่ƒฝใชใƒˆใƒผใ‚ฏใƒณใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```python +>>> tokenized_sequence = tokenizer.tokenize(sequence) +``` + +ใƒˆใƒผใ‚ฏใƒณใฏๅ˜่ชžใพใŸใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ใงใ™ใ€‚ ใŸใจใˆใฐใ€ใ“ใ“ใงใฏ "VRAM" ใฏใƒขใƒ‡ใƒซใฎ่ชžๅฝ™ใซๅซใพใ‚Œใฆใ„ใชใ‹ใฃใŸใŸใ‚ใ€"V"ใ€"RA"ใ€"M" ใซๅˆ†ๅ‰ฒใ•ใ‚Œใพใ—ใŸใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒˆใƒผใ‚ฏใƒณใŒๅˆฅใ€…ใฎๅ˜่ชžใงใฏใชใใ€ๅŒใ˜ๅ˜่ชžใฎไธ€้ƒจใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ™ใŸใ‚ใซใ€"RA" ใจ "M" ใซใฏใƒ€ใƒ–ใƒซใƒใƒƒใ‚ทใƒฅใฎใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ + +```python +>>> print(tokenized_sequence) +['A', 'Titan', 'R', '##T', '##X', 'has', '24', '##GB', 'of', 'V', '##RA', '##M'] +``` + +ใ“ใ‚Œใ‚‰ใฎใƒˆใƒผใ‚ฏใƒณใฏใ€ใƒขใƒ‡ใƒซใŒ็†่งฃใงใใ‚‹ใ‚ˆใ†ใซIDใซๅค‰ๆ›ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๆ–‡ใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซ็›ดๆŽฅไพ›็ตฆใ—ใฆ่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใฎใŸใ‚ใซ[๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers)ใฎRustๅฎŸ่ฃ…ใ‚’ๆดป็”จใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> inputs = tokenizer(sequence) +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒซใŒๆญฃใ—ใๅ‹•ไฝœใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใ™ในใฆใฎๅผ•ๆ•ฐใ‚’ๅซใ‚€่พžๆ›ธใ‚’่ฟ”ใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฏใ€ใ‚ญใƒผ `input_ids` ใฎไธ‹ใซใ‚ใ‚Šใพใ™ใ€‚ + +```python +>>> encoded_sequence = inputs["input_ids"] +>>> print(encoded_sequence) +[101, 138, 18696, 155, 1942, 3190, 1144, 1572, 13745, 1104, 159, 9664, 2107, 102] +``` + +ๆณจๆ„๏ผšใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€้–ข้€ฃใ™ใ‚‹ใƒขใƒ‡ใƒซใŒใใ‚Œใ‚‰ใ‚’ๅฟ…่ฆใจใ™ใ‚‹ๅ ดๅˆใซ่‡ชๅ‹•็š„ใซใ€Œ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ€ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใ€ใƒขใƒ‡ใƒซใŒๆ™‚ๆŠ˜ไฝฟ็”จใ™ใ‚‹็‰นๅˆฅใชIDใงใ™ใ€‚ + +ๅ‰ใฎIDใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ๅ ดๅˆใ€ + + +```python +>>> decoded_sequence = tokenizer.decode(encoded_sequence) +``` + +็งใŸใกใฏ่ฆ‹ใพใ™ + +```python +>>> print(decoded_sequence) +[CLS] A Titan RTX has 24GB of VRAM [SEP] +``` + +ใ“ใ‚Œใฏ[`BertModel`]ใŒใใฎๅ…ฅๅŠ›ใ‚’ๆœŸๅพ…ใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ + + +## L + +### Labels + +ใƒฉใƒ™ใƒซใฏใ€ใƒขใƒ‡ใƒซใŒๆๅคฑใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซๆธกใ™ใ“ใจใŒใงใใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎๅผ•ๆ•ฐใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒฉใƒ™ใƒซใฏใ€ใƒขใƒ‡ใƒซใฎไบˆๆธฌใฎๆœŸๅพ…ๅ€คใงใ‚ใ‚‹ในใใงใ™ใ€‚ใƒขใƒ‡ใƒซใฏใ€้€šๅธธใฎๆๅคฑใ‚’ไฝฟ็”จใ—ใฆใ€ใใฎไบˆๆธฌใจๆœŸๅพ…ๅ€ค๏ผˆใƒฉใƒ™ใƒซ๏ผ‰ใจใฎ้–“ใฎๆๅคฑใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒฉใƒ™ใƒซใฏใƒขใƒ‡ใƒซใฎใƒ˜ใƒƒใƒ‰ใซๅฟœใ˜ใฆ็•ฐใชใ‚Šใพใ™ใ€‚ใŸใจใˆใฐ๏ผš + +- ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒขใƒ‡ใƒซ๏ผˆ[`BertForSequenceClassification`]๏ผ‰ใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ใƒใƒƒใƒๅ†…ใฎๅ„ๅ€คใŒใ‚ทใƒผใ‚ฑใƒณใ‚นๅ…จไฝ“ใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ +- ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใƒขใƒ‡ใƒซ๏ผˆ[`BertForTokenClassification`]๏ผ‰ใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size, seq_length)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ๅ„ๅ€คใŒๅ„ๅ€‹ใ€…ใฎใƒˆใƒผใ‚ฏใƒณใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ +- ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎๅ ดๅˆ๏ผˆ[`BertForMaskedLM`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size, seq_length)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ๅ„ๅ€คใŒๅ„ๅ€‹ใ€…ใฎใƒˆใƒผใ‚ฏใƒณใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ใ“ใ“ใงใฎใƒฉใƒ™ใƒซใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใฎใƒˆใƒผใ‚ฏใƒณIDใงใ‚ใ‚Šใ€ไป–ใฎใƒˆใƒผใ‚ฏใƒณใซใฏ้€šๅธธ -100 ใชใฉใฎๅ€คใŒ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ +- ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใฎใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆ๏ผˆ[`BartForConditionalGeneration`]ใ€[`MBartForConditionalGeneration`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size, tgt_seq_length)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ๅ„ๅ€คใŒๅ„ๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸใ‚ฟใƒผใ‚ฒใƒƒใƒˆใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพๅฟœใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใ€BARTใจT5ใฎไธกๆ–นใฏ้ฉๅˆ‡ใช `decoder_input_ids` ใจใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใ‚นใ‚ฏใ‚’ๅ†…้ƒจใง็”Ÿๆˆใ—ใพใ™ใ€‚้€šๅธธใ€ใ“ใ‚Œใ‚‰ใ‚’ๆไพ›ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’ๅˆฉ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใซใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ +- ็”ปๅƒๅˆ†้กžใƒขใƒ‡ใƒซใฎๅ ดๅˆ๏ผˆ[`ViTForImageClassification`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ใƒใƒƒใƒๅ†…ใฎๅ„ๅ€คใŒๅ„ๅ€‹ใ€…ใฎ็”ปๅƒใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ +- ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒขใƒ‡ใƒซใฎๅ ดๅˆ๏ผˆ[`SegformerForSemanticSegmentation`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size, height, width)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ใƒใƒƒใƒๅ†…ใฎๅ„ๅ€คใŒๅ„ๅ€‹ใ€…ใฎใƒ”ใ‚ฏใ‚ปใƒซใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ +- ็‰ฉไฝ“ๆคœๅ‡บใƒขใƒ‡ใƒซใฎๅ ดๅˆ๏ผˆ[`DetrForObjectDetection`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๅ„ๅ€‹ใ€…ใฎ็”ปๅƒใฎไบˆๆธฌใƒฉใƒ™ใƒซใจๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎๆ•ฐใซๅฏพๅฟœใ™ใ‚‹ `class_labels` ใจ `boxes` ใ‚ญใƒผใ‚’ๆŒใค่พžๆ›ธใฎใƒชใ‚นใƒˆใ‚’ๆœŸๅพ…ใ—ใพใ™ใ€‚ +- ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใƒขใƒ‡ใƒซใฎๅ ดๅˆ๏ผˆ[`Wav2Vec2ForCTC`]๏ผ‰ใ€ใƒขใƒ‡ใƒซใฏๆฌกๅ…ƒใŒ `(batch_size, target_length)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๆœŸๅพ…ใ—ใ€ๅ„ๅ€คใŒๅ„ๅ€‹ใ€…ใฎใƒˆใƒผใ‚ฏใƒณใฎไบˆๆธฌใƒฉใƒ™ใƒซใซๅฏพๅฟœใ—ใพใ™ใ€‚ + + + +ๅ„ใƒขใƒ‡ใƒซใฎใƒฉใƒ™ใƒซใฏ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚‹ใŸใ‚ใ€ๅธธใซๅ„ใƒขใƒ‡ใƒซใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’็ขบ่ชใ—ใฆใ€ใใ‚Œใ‚‰ใฎ็‰นๅฎšใฎใƒฉใƒ™ใƒซใซ้–ขใ™ใ‚‹่ฉณ็ดฐๆƒ…ๅ ฑใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผ + + + +ใƒ™ใƒผใ‚นใƒขใƒ‡ใƒซ๏ผˆ[`BertModel`]๏ผ‰ใฏใƒฉใƒ™ใƒซใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใ‚‰ใฏใƒ™ใƒผใ‚นใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใงใ‚ใ‚Šใ€ๅ˜ใซ็‰นๅพดใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ + + +### large language models (LLM) + +ๅคง้‡ใฎใƒ‡ใƒผใ‚ฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅค‰ๆ›ๅ™จ่จ€่ชžใƒขใƒ‡ใƒซ๏ผˆGPT-3ใ€BLOOMใ€OPT๏ผ‰ใ‚’ๆŒ‡ใ™ไธ€่ˆฌ็š„ใช็”จ่ชžใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏ้€šๅธธใ€ๅคšใใฎๅญฆ็ฟ’ๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใฃใฆใ„ใพใ™๏ผˆใŸใจใˆใฐใ€GPT-3ใฎๅ ดๅˆใ€1750ๅ„„ๅ€‹๏ผ‰ใ€‚ + +## M + +### masked language modeling (MLM) + +ใƒขใƒ‡ใƒซใฏใƒ†ใ‚ญใ‚นใƒˆใฎ็ ดๆใƒใƒผใ‚ธใƒงใƒณใ‚’่ฆ‹ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฟใ‚นใ‚ฏใงใ€้€šๅธธใฏใƒฉใƒณใƒ€ใƒ ใซไธ€้ƒจใฎใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ญใƒณใ‚ฐใ—ใฆๅ…ƒใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +### multimodal + +ใƒ†ใ‚ญใ‚นใƒˆใจๅˆฅใฎ็จฎ้กžใฎๅ…ฅๅŠ›๏ผˆใŸใจใˆใฐ็”ปๅƒ๏ผ‰ใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ + +## N + +### Natural language generation (NLG) + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹้–ข้€ฃใ™ใ‚‹ใ™ในใฆใฎใ‚ฟใ‚นใ‚ฏ๏ผˆใŸใจใˆใฐใ€[Transformersใงๆ›ธใ](https://transformer.huggingface.co/)ใ€็ฟป่จณใชใฉ๏ผ‰ใ€‚ + +### Natural language processing (NLP) + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ‰ฑใ†ๆ–นๆณ•ใ‚’ไธ€่ˆฌ็š„ใซ่กจ็พใ—ใŸใ‚‚ใฎใงใ™ใ€‚ + +### Natural language understanding (NLU) + +ใƒ†ใ‚ญใ‚นใƒˆๅ†…ใซไฝ•ใŒใ‚ใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹้–ข้€ฃใ™ใ‚‹ใ™ในใฆใฎใ‚ฟใ‚นใ‚ฏ๏ผˆใŸใจใˆใฐใ€ใƒ†ใ‚ญใ‚นใƒˆๅ…จไฝ“ใฎๅˆ†้กžใ€ๅ€‹ใ€…ใฎๅ˜่ชžใฎๅˆ†้กžใชใฉ๏ผ‰ใ€‚ + +## P + +### pipeline + +๐Ÿค— Transformersใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€ใƒ‡ใƒผใ‚ฟใฎๅ‰ๅ‡ฆ็†ใจๅค‰ๆ›ใ‚’็‰นๅฎšใฎ้ †ๅบใงๅฎŸ่กŒใ—ใฆใƒ‡ใƒผใ‚ฟใ‚’ๅ‡ฆ็†ใ—ใ€ใƒขใƒ‡ใƒซใ‹ใ‚‰ไบˆๆธฌใ‚’่ฟ”ใ™ไธ€้€ฃใฎใ‚นใƒ†ใƒƒใƒ—ใ‚’ๆŒ‡ใ™ๆŠฝ่ฑกๅŒ–ใงใ™ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซ่ฆ‹ใ‚‰ใ‚Œใ‚‹ใ„ใใคใ‹ใฎใ‚นใƒ†ใƒผใ‚ธใฎไพ‹ใซใฏใ€ใƒ‡ใƒผใ‚ฟใฎๅ‰ๅ‡ฆ็†ใ€็‰นๅพดๆŠฝๅ‡บใ€ๆญฃ่ฆๅŒ–ใชใฉใŒใ‚ใ‚Šใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๆŽจ่ซ–ใฎใŸใ‚ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ](https://huggingface.co/docs/transformers/pipeline_tutorial)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### pixel values + +ใƒขใƒ‡ใƒซใซๆธกใ•ใ‚Œใ‚‹็”ปๅƒใฎๆ•ฐๅ€ค่กจ็พใฎใƒ†ใƒณใ‚ฝใƒซใงใ™ใ€‚ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใฏใ€ๅฝข็ŠถใŒ [`ใƒใƒƒใƒใ‚ตใ‚คใ‚บ`, `ใƒใƒฃใƒใƒซๆ•ฐ`, `้ซ˜ใ•`, `ๅน…`] ใฎ่กŒๅˆ—ใงใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‹ใ‚‰็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ + +### pooling + +่กŒๅˆ—ใ‚’ๅฐใ•ใช่กŒๅˆ—ใซ็ธฎๅฐใ™ใ‚‹ๆ“ไฝœใงใ€ใƒ—ใƒผใƒซๅฏพ่ฑกใฎๆฌกๅ…ƒใฎๆœ€ๅคงๅ€คใพใŸใฏๅนณๅ‡ๅ€คใ‚’ๅ–ใ‚‹ใ“ใจใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ใƒ—ใƒผใƒชใƒณใ‚ฐใƒฌใ‚คใƒคใƒผใฏไธ€่ˆฌ็š„ใซ็•ณใฟ่พผใฟใƒฌใ‚คใƒคใƒผใฎ้–“ใซ่ฆ‹ใ‚‰ใ‚Œใ€็‰นๅพด่กจ็พใ‚’ใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ + +### position IDs + +ใƒˆใƒผใ‚ฏใƒณใ”ใจใฎไฝ็ฝฎใŒๅŸ‹ใ‚่พผใพใ‚Œใฆใ„ใ‚‹RNNใจใฏ็•ฐใชใ‚Šใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏๅ„ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚’ๆŠŠๆกใ—ใฆใ„ใพใ›ใ‚“ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒขใƒ‡ใƒซใฏใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚’่ญ˜ๅˆฅใ™ใ‚‹ใŸใ‚ใซไฝ็ฝฎID๏ผˆ`position_ids`๏ผ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใ™ใ€‚ใƒขใƒ‡ใƒซใซ `position_ids` ใŒๆธกใ•ใ‚Œใชใ„ๅ ดๅˆใ€IDใฏ่‡ชๅ‹•็š„ใซ็ตถๅฏพ็š„ใชไฝ็ฝฎๅŸ‹ใ‚่พผใฟใจใ—ใฆไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ + +็ตถๅฏพ็š„ใชไฝ็ฝฎๅŸ‹ใ‚่พผใฟใฏ็ฏ„ๅ›ฒ `[0ใ€config.max_position_embeddings - 1]` ใ‹ใ‚‰้ธๆŠžใ•ใ‚Œใพใ™ใ€‚ไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ€ๆญฃๅผฆๆณขไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚„็›ธๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใชใฉใ€ไป–ใฎใ‚ฟใ‚คใƒ—ใฎไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + + +### preprocessing + +็”Ÿใƒ‡ใƒผใ‚ฟใ‚’ๆฉŸๆขฐๅญฆ็ฟ’ใƒขใƒ‡ใƒซใง็ฐกๅ˜ใซๅ‡ฆ็†ใงใใ‚‹ๅฝขๅผใซๆบ–ๅ‚™ใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ไพ‹ใˆใฐใ€ใƒ†ใ‚ญใ‚นใƒˆใฏ้€šๅธธใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใซใ‚ˆใฃใฆๅ‰ๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ไป–ใฎๅ…ฅๅŠ›ใ‚ฟใ‚คใƒ—ใซๅฏพใ™ใ‚‹ๅ‰ๅ‡ฆ็†ใฎๅ…ทไฝ“็š„ใชๆ–นๆณ•ใ‚’็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€[Preprocess](https://huggingface.co/docs/transformers/preprocessing) ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +### pretrained model + +ใ‚ใ‚‹ใƒ‡ใƒผใ‚ฟ๏ผˆใŸใจใˆใฐใ€Wikipediaๅ…จไฝ“ใชใฉ๏ผ‰ใงไบ‹ๅ‰ใซๅญฆ็ฟ’ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใงใ™ใ€‚ไบ‹ๅ‰ๅญฆ็ฟ’ใฎๆ–นๆณ•ใซใฏใ€่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šใฎ็›ฎ็š„ใŒๅซใพใ‚Œใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’่ชญใฟๅ–ใ‚Šใ€ๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ—ใ‚ˆใ†ใจใ™ใ‚‹ใ‚‚ใฎ๏ผˆ[ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](#causal-language-modeling)ใ‚’ๅ‚็…ง๏ผ‰ใ‚„ใ€ไธ€้ƒจใฎๅ˜่ชžใ‚’ใƒžใ‚นใ‚ฏใ—ใ€ใใ‚Œใ‚‰ใ‚’ไบˆๆธฌใ—ใ‚ˆใ†ใจใ™ใ‚‹ใ‚‚ใฎ๏ผˆ[ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](#masked-language-modeling-mlm)ใ‚’ๅ‚็…ง๏ผ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +้Ÿณๅฃฐใจใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใซใฏ็‹ฌ่‡ชใฎไบ‹ๅ‰ๅญฆ็ฟ’ใฎ็›ฎ็š„ใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€Wav2Vec2ใฏ้Ÿณๅฃฐใƒขใƒ‡ใƒซใงใ€ใƒขใƒ‡ใƒซใซๅฏพใ—ใฆใ€Œ็œŸใฎใ€้Ÿณๅฃฐ่กจ็พใ‚’ๅฝใฎ้Ÿณๅฃฐ่กจ็พใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰่ญ˜ๅˆฅใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅฏพๆฏ”็š„ใชใ‚ฟใ‚นใ‚ฏใงไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไธ€ๆ–นใ€BEiTใฏใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใงใ€ไธ€้ƒจใฎ็”ปๅƒใƒ‘ใƒƒใƒใ‚’ใƒžใ‚นใ‚ฏใ—ใ€ใƒขใƒ‡ใƒซใซใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒ‘ใƒƒใƒใ‚’ไบˆๆธฌใ•ใ›ใ‚‹ใ‚ฟใ‚นใ‚ฏ๏ผˆใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎ็›ฎ็š„ใจไผผใฆใ„ใพใ™๏ผ‰ใงไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## R + +### recurrent neural network (RNN) + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซๅฑคใ‚’ใƒซใƒผใƒ—ใ•ใ›ใ‚‹ใƒขใƒ‡ใƒซใฎไธ€็จฎใงใ™ใ€‚ + +### representation learning + +็”Ÿใƒ‡ใƒผใ‚ฟใฎๆ„ๅ‘ณใฎใ‚ใ‚‹่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๆฉŸๆขฐๅญฆ็ฟ’ใฎใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใงใ™ใ€‚่กจ็พๅญฆ็ฟ’ใฎๆŠ€่ก“ใฎไธ€้ƒจใซใฏๅ˜่ชžๅŸ‹ใ‚่พผใฟใ€ใ‚ชใƒผใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€Generative Adversarial Networks๏ผˆGANs๏ผ‰ใชใฉใŒใ‚ใ‚Šใพใ™ใ€‚ + +## S + +### sampling rate + +็ง’ใ”ใจใซๅ–ใ‚‰ใ‚Œใ‚‹ใ‚ตใƒณใƒ—ใƒซ๏ผˆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชไฟกๅทใชใฉ๏ผ‰ใฎๆ•ฐใ‚’ใƒ˜ใƒซใƒ„ๅ˜ไฝใงๆธฌๅฎšใ—ใŸใ‚‚ใฎใงใ™ใ€‚ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใฏ้Ÿณๅฃฐใชใฉใฎ้€ฃ็ถšไฟกๅทใ‚’้›ขๆ•ฃๅŒ–ใ™ใ‚‹็ตๆžœใงใ™ใ€‚ + +### self-attention + +ๅ…ฅๅŠ›ใฎๅ„่ฆ็ด ใฏใ€ใฉใฎไป–ใฎ่ฆ็ด ใซๆณจๆ„ใ‚’ๆ‰•ใ†ในใใ‹ใ‚’ๆคœๅ‡บใ—ใพใ™ใ€‚ + +### self-supervised learning + +ใƒขใƒ‡ใƒซใŒใƒฉใƒ™ใƒซใฎใชใ„ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰่‡ชๅˆ†่‡ช่บซใฎๅญฆ็ฟ’็›ฎๆจ™ใ‚’ไฝœๆˆใ™ใ‚‹ๆฉŸๆขฐๅญฆ็ฟ’ๆŠ€่ก“ใฎใ‚ซใƒ†ใ‚ดใƒชใงใ™ใ€‚ใ“ใ‚Œใฏ[ๆ•™ๅธซใชใ—ๅญฆ็ฟ’](#unsupervised-learning)ใ‚„[ๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’](#supervised-learning)ใจใฏ็•ฐใชใ‚Šใ€ๅญฆ็ฟ’ใƒ—ใƒญใ‚ปใ‚นใฏใƒฆใƒผใ‚ถใƒผใ‹ใ‚‰ใฏๆ˜Ž็คบ็š„ใซใฏ็›ฃ็ฃใ•ใ‚Œใฆใ„ใชใ„็‚นใŒ็•ฐใชใ‚Šใพใ™ใ€‚ + +่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใฎ1ใคใฎไพ‹ใฏ[ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](#masked-language-modeling-mlm)ใงใ€ใƒขใƒ‡ใƒซใซใฏไธ€้ƒจใฎใƒˆใƒผใ‚ฏใƒณใŒๅ‰Š้™คใ•ใ‚ŒใŸๆ–‡ใŒไธŽใˆใ‚‰ใ‚Œใ€ๆฌ ่ฝใ—ใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซๅญฆ็ฟ’ใ—ใพใ™ใ€‚ + +### semi-supervised learning + +ใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใฎๅฐ‘้‡ใจใƒฉใƒ™ใƒซใฎใชใ„ใƒ‡ใƒผใ‚ฟใฎๅคง้‡ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใƒขใƒ‡ใƒซใฎ็ฒพๅบฆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ๅบƒ็ฏ„ใชๆฉŸๆขฐๅญฆ็ฟ’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆŠ€่ก“ใฎใ‚ซใƒ†ใ‚ดใƒชใงใ™ใ€‚[ๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’](#supervised-learning)ใ‚„[ๆ•™ๅธซใชใ—ๅญฆ็ฟ’](#unsupervised-learning)ใจใฏ็•ฐใชใ‚Šใ€ๅŠๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใฎใ‚ขใƒ—ใƒญใƒผใƒใฎ1ใคใฏใ€Œใ‚ปใƒซใƒ•ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€ใงใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใฏใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ€ๆฌกใซใƒฉใƒ™ใƒซใฎใชใ„ใƒ‡ใƒผใ‚ฟใงไบˆๆธฌใ‚’่กŒใ„ใพใ™ใ€‚ใƒขใƒ‡ใƒซใŒๆœ€ใ‚‚่‡ชไฟกใ‚’ๆŒใฃใฆไบˆๆธฌใ™ใ‚‹้ƒจๅˆ†ใŒใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซ่ฟฝๅŠ ใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฎๅ†ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +### sequence-to-sequence (seq2seq) + +ๅ…ฅๅŠ›ใ‹ใ‚‰ๆ–ฐใ—ใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็”Ÿๆˆใ™ใ‚‹ใƒขใƒ‡ใƒซใงใ™ใ€‚็ฟป่จณใƒขใƒ‡ใƒซใ‚„่ฆ็ด„ใƒขใƒ‡ใƒซ๏ผˆ[Bart](model_doc/bart)ใ‚„[T5](model_doc/t5)ใชใฉ๏ผ‰ใชใฉใŒใ“ใ‚Œใซ่ฉฒๅฝ“ใ—ใพใ™ใ€‚ + +### stride + +[็•ณใฟ่พผใฟ](#convolution)ใพใŸใฏ[ใƒ—ใƒผใƒชใƒณใ‚ฐ](#pooling)ใซใŠใ„ใฆใ€ใ‚นใƒˆใƒฉใ‚คใƒ‰ใฏใ‚ซใƒผใƒใƒซใŒ่กŒๅˆ—ไธŠใง็งปๅ‹•ใ™ใ‚‹่ท้›ขใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ใ‚นใƒˆใƒฉใ‚คใƒ‰ใŒ1ใฎๅ ดๅˆใ€ใ‚ซใƒผใƒใƒซใฏ1ใƒ”ใ‚ฏใ‚ปใƒซใšใค็งปๅ‹•ใ—ใ€ใ‚นใƒˆใƒฉใ‚คใƒ‰ใŒ2ใฎๅ ดๅˆใ€ใ‚ซใƒผใƒใƒซใฏ2ใƒ”ใ‚ฏใ‚ปใƒซใšใค็งปๅ‹•ใ—ใพใ™ใ€‚ + +### supervised learning + +ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใฎไธ€ใคใงใ€็›ดๆŽฅใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎๆ€ง่ƒฝใ‚’ไฟฎๆญฃใ—ๆŒ‡ๅฐŽใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใซไพ›็ตฆใ•ใ‚Œใ€ใใฎไบˆๆธฌใŒๆ—ข็Ÿฅใฎใƒฉใƒ™ใƒซใจๆฏ”่ผƒใ•ใ‚Œใพใ™ใ€‚ใƒขใƒ‡ใƒซใฏไบˆๆธฌใŒใฉใ‚Œใ ใ‘่ชคใฃใฆใ„ใŸใ‹ใซๅŸบใฅใ„ใฆ้‡ใฟใ‚’ๆ›ดๆ–ฐใ—ใ€ใƒ—ใƒญใ‚ปใ‚นใฏใƒขใƒ‡ใƒซใฎๆ€ง่ƒฝใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ใŸใ‚ใซ็นฐใ‚Š่ฟ”ใ•ใ‚Œใพใ™ใ€‚ + +## T + +### token + +ๆ–‡ใฎไธ€้ƒจใงใ‚ใ‚Šใ€้€šๅธธใฏๅ˜่ชžใงใ™ใŒใ€ใ‚ตใƒ–ใƒฏใƒผใƒ‰๏ผˆไธ€่ˆฌ็š„ใงใชใ„ๅ˜่ชžใฏใ—ใฐใ—ใฐใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ‰ใพใŸใฏๅฅ่ชญ็‚นใฎ่จ˜ๅทใงใ‚ใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +### token Type IDs + +ไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ€ๆ–‡ใฎใƒšใ‚ขใฎๅˆ†้กžใ‚„่ณชๅ•ๅฟœ็ญ”ใ‚’่กŒใ†ใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ + + + +ใ“ใ‚Œใซใฏ็•ฐใชใ‚‹2ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ˜ไธ€ใฎใ€Œinput_idsใ€ใ‚จใƒณใƒˆใƒชใซ็ตๅˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€้€šๅธธใฏๅˆ†้กžๅญ๏ผˆ`[CLS]`๏ผ‰ใ‚„ๅŒบๅˆ‡ใ‚Š่จ˜ๅท๏ผˆ`[SEP]`๏ผ‰ใชใฉใฎ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใฎๅŠฉใ‘ใ‚’ๅ€Ÿใ‚ŠใฆๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ไพ‹ใˆใฐใ€BERTใƒขใƒ‡ใƒซใฏๆฌกใฎใ‚ˆใ†ใซ2ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นๅ…ฅๅŠ›ใ‚’ๆง‹็ฏ‰ใ—ใพใ™๏ผš + +ๆ—ฅๆœฌ่ชž่จณใ‚’ๆไพ›ใ—ใฆใ„ใŸใ ใใŸใ„ใงใ™ใ€‚Markdownๅฝขๅผใง่จ˜่ฟฐใ—ใฆใใ ใ•ใ„ใ€‚ + + +```python +>>> # [CLS] SEQUENCE_A [SEP] SEQUENCE_B [SEP] +``` + +ๆˆ‘ใ€…ใฏใ€ๅ‰่ฟฐใฎใ‚ˆใ†ใซใ€2ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’2ใคใฎๅผ•ๆ•ฐใจใ—ใฆ `tokenizer` ใซๆธกใ™ใ“ใจใงใ€ใ“ใฎใ‚ˆใ†ใชๆ–‡ใ‚’่‡ชๅ‹•็š„ใซ็”Ÿๆˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผˆไปฅๅ‰ใฎใ‚ˆใ†ใซใƒชใ‚นใƒˆใงใฏใชใ๏ผ‰ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซ๏ผš + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") +>>> sequence_a = "HuggingFace is based in NYC" +>>> sequence_b = "Where is HuggingFace based?" + +>>> encoded_dict = tokenizer(sequence_a, sequence_b) +>>> decoded = tokenizer.decode(encoded_dict["input_ids"]) +``` + +ใ“ใ‚Œใซๅฏพๅฟœใ™ใ‚‹ใ‚ณใƒผใƒ‰ใฏไปฅไธ‹ใงใ™๏ผš + +```python +>>> print(decoded) +[CLS] HuggingFace is based in NYC [SEP] Where is HuggingFace based? [SEP] +``` + +ไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏใ€1ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใฉใ“ใง็ต‚ใ‚ใ‚Šใ€ๅˆฅใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใฉใ“ใงๅง‹ใพใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹ใฎใซๅๅˆ†ใชๆƒ…ๅ ฑใŒๅ‚™ใ‚ใฃใฆใ„ใพใ™ใ€‚ใŸใ ใ—ใ€BERTใชใฉใฎไป–ใฎใƒขใƒ‡ใƒซใงใฏใ€ใƒˆใƒผใ‚ฏใƒณใ‚ฟใ‚คใƒ—ID๏ผˆใ‚ปใ‚ฐใƒกใƒณใƒˆIDใจใ‚‚ๅ‘ผใฐใ‚Œใ‚‹๏ผ‰ใ‚‚ไฝฟ็”จใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซๅ†…ใฎ2ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’่ญ˜ๅˆฅใ™ใ‚‹ใƒใ‚คใƒŠใƒชใƒžใ‚นใ‚ฏใจใ—ใฆ่กจใ•ใ‚Œใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ใ“ใฎใƒžใ‚นใ‚ฏใ‚’ใ€Œtoken_type_idsใ€ใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```python +>>> encoded_dict["token_type_ids"] +[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1] +``` + +ๆœ€ๅˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ€ใคใพใ‚Š่ณชๅ•ใฎใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ใ€Œใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ€ใฏใ€ใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใŒใ€Œ0ใ€ใง่กจใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไธ€ๆ–นใ€2็•ช็›ฎใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ€่ณชๅ•ใซๅฏพๅฟœใ™ใ‚‹ใ‚‚ใฎใฏใ€ใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใŒใ€Œ1ใ€ใง่กจใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ไธ€้ƒจใฎใƒขใƒ‡ใƒซใ€ไพ‹ใˆใฐ [`XLNetModel`] ใฎใ‚ˆใ†ใซใ€่ฟฝๅŠ ใฎใƒˆใƒผใ‚ฏใƒณใŒใ€Œ2ใ€ใง่กจใ•ใ‚Œใพใ™ใ€‚ + + +### transfer learning + +ไบ‹ๅ‰ใซๅญฆ็ฟ’ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅ–ใ‚Šใ€ใใ‚Œใ‚’ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซ้ฉๅฟœใ•ใ›ใ‚‹ๆŠ€่ก“ใ€‚ใ‚ผใƒญใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’่จ“็ทดใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ๆ—ขๅญ˜ใฎใƒขใƒ‡ใƒซใ‹ใ‚‰ๅพ—ใŸ็Ÿฅ่ญ˜ใ‚’ๅ‡บ็™บ็‚นใจใ—ใฆๆดป็”จใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šๅญฆ็ฟ’ใƒ—ใƒญใ‚ปใ‚นใŒๅŠ ้€Ÿใ—ใ€ๅฟ…่ฆใช่จ“็ทดใƒ‡ใƒผใ‚ฟใฎ้‡ใŒๆธ›ๅฐ‘ใ—ใพใ™ใ€‚ + +### transformer + +่‡ชๅทฑๆณจๆ„ใƒ™ใƒผใ‚นใฎๆทฑๅฑคๅญฆ็ฟ’ใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ + +## U + +### unsupervised learning + +ใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใŒใƒฉใƒ™ใƒซไป˜ใ‘ใ•ใ‚Œใฆใ„ใชใ„ใƒขใƒ‡ใƒซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅฝขๆ…‹ใ€‚ๆ•™ๅธซใชใ—ๅญฆ็ฟ’ใฎๆŠ€่ก“ใฏใ€ใ‚ฟใ‚นใ‚ฏใซๅฝน็ซ‹ใคใƒ‘ใ‚ฟใƒผใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซใƒ‡ใƒผใ‚ฟๅˆ†ๅธƒใฎ็ตฑ่จˆๆƒ…ๅ ฑใ‚’ๆดป็”จใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/hpo_train.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/hpo_train.md new file mode 100644 index 0000000000000000000000000000000000000000..ff82d7138b033ae5fb434cef6e1ef64b37f1ddd9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/hpo_train.md @@ -0,0 +1,144 @@ + + +# Hyperparameter Search using Trainer API + +๐Ÿค— Transformersใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹[`Trainer`]ใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใ€็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๆ‰‹ๅ‹•ใง่จ˜่ฟฐใ›ใšใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใŒ็ฐกๅ˜ใซใชใ‚Šใพใ™ใ€‚[`Trainer`]ใฏใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆคœ็ดขใฎAPIใ‚‚ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€ใใ‚Œใ‚’ไพ‹็คบใ—ใพใ™ใ€‚ + +## Hyperparameter Search backend + +[`Trainer`]ใฏ็พๅœจใ€4ใคใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆคœ็ดขใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™๏ผš +[optuna](https://optuna.org/)ใ€[sigopt](https://sigopt.com/)ใ€[raytune](https://docs.ray.io/en/latest/tune/index.html)ใ€ใŠใ‚ˆใณ[wandb](https://wandb.ai/site/sweeps)ใ€‚ + +ใ“ใ‚Œใ‚‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆคœ็ดขใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +```bash +pip install optuna/sigopt/wandb/ray[tune] +``` + +## How to enable Hyperparameter search in example + +ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆคœ็ดขใ‚นใƒšใƒผใ‚นใ‚’ๅฎš็พฉใ—ใ€็•ฐใชใ‚‹ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใซใฏ็•ฐใชใ‚‹ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใŒๅฟ…่ฆใงใ™ใ€‚ + +Sigoptใฎๅ ดๅˆใ€sigopt [object_parameter](https://docs.sigopt.com/ai-module-api-references/api_reference/objects/object_parameter) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใใ‚Œใฏไปฅไธ‹ใฎใ‚ˆใ†ใชใ‚‚ใฎใงใ™๏ผš +```py +>>> def sigopt_hp_space(trial): +... return [ +... {"bounds": {"min": 1e-6, "max": 1e-4}, "name": "learning_rate", "type": "double"}, +... { +... "categorical_values": ["16", "32", "64", "128"], +... "name": "per_device_train_batch_size", +... "type": "categorical", +... }, +... ] +``` + + +Optunaใซ้–ขใ—ใฆใฏใ€[object_parameter](https://optuna.readthedocs.io/en/stable/tutorial/10_key_features/002_configurations.html#sphx-glr-tutorial-10-key-features-002-configurations-py)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +```py +>>> def optuna_hp_space(trial): +... return { +... "learning_rate": trial.suggest_float("learning_rate", 1e-6, 1e-4, log=True), +... "per_device_train_batch_size": trial.suggest_categorical("per_device_train_batch_size", [16, 32, 64, 128]), +... } +``` + +Optunaใฏใ€ๅคš็›ฎ็š„ใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟๆœ€้ฉๅŒ–๏ผˆHPO๏ผ‰ใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ `hyperparameter_search` ใง `direction` ใ‚’ๆธกใ—ใ€่ค‡ๆ•ฐใฎ็›ฎ็š„้–ขๆ•ฐๅ€คใ‚’่ฟ”ใ™ใŸใ‚ใฎ็‹ฌ่‡ชใฎ `compute_objective` ใ‚’ๅฎš็พฉใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ Pareto Front๏ผˆ`list[BestRun]`๏ผ‰ใฏ `hyperparameter_search` ใง่ฟ”ใ•ใ‚Œใ€[test_trainer](https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py) ใฎใƒ†ใ‚นใƒˆใ‚ฑใƒผใ‚น `TrainerHyperParameterMultiObjectOptunaIntegrationTest` ใ‚’ๅ‚็…งใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + +```py +>>> best_trials = trainer.hyperparameter_search( +... direction=["minimize", "maximize"], +... backend="optuna", +... hp_space=optuna_hp_space, +... n_trials=20, +... compute_objective=compute_objective, +... ) +``` + +Ray Tuneใซ้–ขใ—ใฆใ€[object_parameter](https://docs.ray.io/en/latest/tune/api/search_space.html)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +```py +>>> def ray_hp_space(trial): +... return { +... "learning_rate": tune.loguniform(1e-6, 1e-4), +... "per_device_train_batch_size": tune.choice([16, 32, 64, 128]), +... } +``` + +Wandbใซใคใ„ใฆใฏใ€[object_parameter](https://docs.wandb.ai/guides/sweeps/configuration)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ใ“ใ‚Œใฏไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + +```py +>>> def wandb_hp_space(trial): +... return { +... "method": "random", +... "metric": {"name": "objective", "goal": "minimize"}, +... "parameters": { +... "learning_rate": {"distribution": "uniform", "min": 1e-6, "max": 1e-4}, +... "per_device_train_batch_size": {"values": [16, 32, 64, 128]}, +... }, +... } +``` + +`model_init` ้–ขๆ•ฐใ‚’ๅฎš็พฉใ—ใ€ใใ‚Œใ‚’ [`Trainer`] ใซๆธกใ™ไพ‹ใ‚’็คบใ—ใพใ™๏ผš + + +```py +>>> def model_init(trial): +... return AutoModelForSequenceClassification.from_pretrained( +... model_args.model_name_or_path, +... from_tf=bool(".ckpt" in model_args.model_name_or_path), +... config=config, +... cache_dir=model_args.cache_dir, +... revision=model_args.model_revision, +... token=True if model_args.use_auth_token else None, +... ) +``` + +[`Trainer`] ใ‚’ `model_init` ้–ขๆ•ฐใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ†ใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใŠใ‚ˆใณ่ฉ•ไพก้–ขๆ•ฐใจๅ…ฑใซไฝœๆˆใ—ใฆใใ ใ•ใ„: + + +```py +>>> trainer = Trainer( +... model=None, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... processing_class=tokenizer, +... model_init=model_init, +... data_collator=data_collator, +... ) +``` + +ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๆŽข็ดขใ‚’ๅ‘ผใณๅ‡บใ—ใ€ๆœ€่‰ฏใฎใƒˆใƒฉใ‚คใ‚ขใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใฏ `"optuna"` / `"sigopt"` / `"wandb"` / `"ray"` ใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๆ–นๅ‘ใฏ `"minimize"` ใพใŸใฏ `"maximize"` ใงใ‚ใ‚Šใ€็›ฎๆจ™ใ‚’ใ‚ˆใ‚Šๅคงใใใ™ใ‚‹ใ‹ๅฐใ•ใใ™ใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +`compute_objective` ้–ขๆ•ฐใ‚’็‹ฌ่‡ชใซๅฎš็พฉใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ๅฎš็พฉใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ `compute_objective` ใŒๅ‘ผใณๅ‡บใ•ใ‚Œใ€F1ใชใฉใฎ่ฉ•ไพกใƒกใƒˆใƒชใƒƒใ‚ฏใฎๅˆ่จˆใŒ็›ฎๆจ™ๅ€คใจใ—ใฆ่ฟ”ใ•ใ‚Œใพใ™ใ€‚ + + +```py +>>> best_trial = trainer.hyperparameter_search( +... direction="maximize", +... backend="optuna", +... hp_space=optuna_hp_space, +... n_trials=20, +... compute_objective=compute_objective, +... ) +``` + +## Hyperparameter search For DDP finetune +็พๅœจใ€DDP๏ผˆDistributed Data Parallel๏ผ‰ใฎใŸใ‚ใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆคœ็ดขใฏใ€Optuna ใจ SigOpt ใซๅฏพใ—ใฆๆœ‰ๅŠนใซใชใฃใฆใ„ใพใ™ใ€‚ใƒฉใƒณใ‚ฏใ‚ผใƒญใƒ—ใƒญใ‚ปใ‚นใฎใฟใŒๆคœ็ดขใƒˆใƒฉใ‚คใ‚ขใƒซใ‚’็”Ÿๆˆใ—ใ€ไป–ใฎใƒฉใƒณใ‚ฏใซๅผ•ๆ•ฐใ‚’ๆธกใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/index.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/index.md new file mode 100644 index 0000000000000000000000000000000000000000..1fdd300b7682c0c02a733fa01b45537fe2cec36d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/index.md @@ -0,0 +1,399 @@ + + +# ๐Ÿค— Transformers + +[PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/), [JAX](https://jax.readthedocs.io/en/latest/)ใฎใŸใ‚ใฎๆœ€ๅ…ˆ็ซฏๆฉŸๆขฐๅญฆ็ฟ’ใ€‚ + +๐Ÿค— Transformers ใฏๆœ€ๅ…ˆ็ซฏใฎๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’็ฐกๅ˜ใซใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆๅญฆ็ฟ’ใ™ใ‚‹APIใจใƒ„ใƒผใƒซใ‚’ๆไพ›ใ—ใพใ™ใ€‚ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใง่จˆ็ฎ—ใ‚ณใ‚นใƒˆใจไบŒ้…ธๅŒ–็‚ญ็ด ใฎๆŽ’ๅ‡บ้‡ใ‚’ๅ‰Šๆธ›ใงใใ€ใพใŸใ‚ผใƒญใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใซ่ฆๆฑ‚ใ•ใ‚Œใ‚‹ๆ™‚้–“ใจใƒชใ‚ฝใƒผใ‚นใ‚’็ฏ€็ด„ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏไปฅไธ‹ใฎใ‚ˆใ†ใช็•ฐใชใ‚‹ใƒขใƒ€ใƒชใƒ†ใ‚ฃใซใŠใ‘ใ‚‹ไธ€่ˆฌ็š„ใชใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™: + +๐Ÿ“ **่‡ช็„ถ่จ€่ชžๅ‡ฆ็†**: ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ€ ๅ›บๆœ‰่กจ็พๆŠฝๅ‡บใ€ ่ณชๅ•ๅฟœ็ญ”ใ€ ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€ ๆ–‡็ซ ่ฆ็ด„ใ€ ๆฉŸๆขฐ็ฟป่จณใ€ ่ค‡ๆ•ฐ้ธๆŠžใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ€‚
+๐Ÿ–ผ๏ธ **ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณ**: ็”ปๅƒๅˆ†้กžใ€ ็‰ฉไฝ“ๆคœๅ‡บใ€ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€‚
+๐Ÿ—ฃ๏ธ **้Ÿณๅฃฐ**: ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ€้Ÿณๅฃฐๅˆ†้กžใ€‚
+๐Ÿ™ **ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ**: ใƒ†ใƒผใƒ–ใƒซ่ณชๅ•ๅฟœ็ญ”ใ€ ๅ…‰ๅญฆๆ–‡ๅญ—่ช่ญ˜(OCR)ใ€ ใ‚นใ‚ญใƒฃใƒณใ•ใ‚ŒใŸใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‹ใ‚‰ใฎๆƒ…ๅ ฑๆŠฝๅ‡บใ€ ๅ‹•็”ปๅˆ†้กžใ€ visual question answering(่ฆ–่ฆš็š„่ณชๅ•ๅฟœ็ญ”)ใ€‚ + +๐Ÿค— Transformers ใฏPyTorch, TensorFlow, JAX้–“ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ็›ธไบ’้‹็”จๆ€งใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ ใ“ใ‚Œใฏใƒขใƒ‡ใƒซใฎๅ„ๆฎต้šŽใง็•ฐใชใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’ไฝฟใ†ใŸใ‚ใฎๆŸ”่ปŸๆ€งใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ‚ใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใง3่กŒใฎใ‚ณใƒผใƒ‰ใงใƒขใƒ‡ใƒซใ‚’ๅญฆ็ฟ’ใ—ใ€ๅˆฅใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงๆŽจ่ซ–ใฎใŸใ‚ใซใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใŒๅฏ่ƒฝใงใ™ใ€‚ใพใŸใ€ๆœฌ็•ช็’ฐๅขƒใฎใƒ‡ใƒ—ใƒญใ‚คใฎใŸใ‚ใซใƒขใƒ‡ใƒซใ‚’ONNXใ‚„TorchScriptใฎใ‚ˆใ†ใชๅฝขๅผใงใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ + +[Hub](https://huggingface.co/models), [forum](https://discuss.huggingface.co/), [Discord](https://discord.com/invite/JfAtkvEtRb)ใงๆˆ้•ทไธญใฎใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซไปŠๆ—ฅๅ‚ๅŠ ใ—ใพใ—ใ‚‡ใ†๏ผ + +## Hugging Faceใƒใƒผใƒ ใซใ‚ˆใ‚‹ใ‚ซใ‚นใ‚ฟใƒ ใ‚ตใƒใƒผใƒˆใ‚’ใ”ๅธŒๆœ›ใฎๅ ดๅˆ + + + HuggingFace Expert Acceleration Program + + +## ็›ฎๆฌก + +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏไปฅไธ‹ใฎ5ใคใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงๆง‹ๆˆใ•ใ‚Œใฆใ„ใพใ™: + +- **ใฏใ˜ใ‚ใซ** ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใ‚ฏใ‚คใƒƒใ‚ฏใƒ„ใ‚ขใƒผใจใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟใ„ๅง‹ใ‚ใ‚‹ใŸใ‚ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ +- **ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ** ใฏใ€ๅˆๅฟƒ่€…ใŒๅง‹ใ‚ใ‚‹ใฎใซๆœ€้ฉใชๅ ดๆ‰€ใงใ™ใ€‚ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟใ„ๅง‹ใ‚ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชๅŸบๆœฌ็š„ใชใ‚นใ‚ญใƒซใ‚’็ฟ’ๅพ—ใงใใพใ™ใ€‚ +- **HOW-TOใ‚ฌใ‚คใƒ‰** ใฏใ€่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎใŸใ‚ใซๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’finetuningใ™ใ‚‹ใ“ใจใ‚„ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใฎไฝœๆˆใจๅ…ฑๆœ‰ใฎๆ–นๆณ•ใชใฉใจใ„ใฃใŸ็‰นๅฎšใฎ็›ฎๆจ™ใ‚’้”ๆˆใ™ใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ +- **ใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚ฌใ‚คใƒ‰** ใฏใ€ใƒขใƒ‡ใƒซใ‚„ใ‚ฟใ‚นใ‚ฏใ€ใใ—ใฆ ๐Ÿค— Transformersใฎ่จญ่จˆๆ€ๆƒณใฎ่ƒŒๆ™ฏใซใ‚ใ‚‹ๅŸบๆœฌ็š„ใซใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚„่€ƒใˆๆ–นใซใคใ„ใฆใ‚ˆใ‚Šๆทฑใ่€ƒๅฏŸใ—่งฃ่ชฌใ—ใฆใ„ใพใ™ใ€‚ +- **API** ๅ…จใฆใฎใ‚ฏใƒฉใ‚นใจ้–ขๆ•ฐใ‚’่ชฌๆ˜Žใ—ใพใ™: + + - **MAIN CLASSES** ใฏใ€configuration, model, tokenizer, pipelineใจใ„ใฃใŸๆœ€ใ‚‚้‡่ฆใชใ‚ฏใƒฉใ‚นใซใคใ„ใฆ่ฉณ็ดฐใซ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ + - **MODELS** ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใงๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใ‚‹ใใ‚Œใžใ‚Œใฎใƒขใƒ‡ใƒซใซ้–ข้€ฃใ—ใŸใ‚ฏใƒฉใ‚นใจ้–ขๆ•ฐใ‚’่ฉณ็ดฐใซ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ + - **INTERNAL HELPERS** ใฏใ€ๅ†…้ƒจใงไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚ฏใƒฉใ‚นใ‚„้–ขๆ•ฐใ‚’่ฉณ็ดฐใซ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ + +### ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซ + + + +1. **[ALBERT](https://huggingface.co/docs/transformers/model_doc/albert)** (Google Research and the Toyota Technological Institute at Chicago ใ‹ใ‚‰) Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942) +1. **[AltCLIP](https://huggingface.co/docs/transformers/main/model_doc/altclip)** (BAAI ใ‹ใ‚‰) Chen, Zhongzhi and Liu, Guang and Zhang, Bo-Wen and Ye, Fulong and Yang, Qinghong and Wu, Ledell ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities](https://huggingface.co/papers/2211.06679) +1. **[Audio Spectrogram Transformer](https://huggingface.co/docs/transformers/model_doc/audio-spectrogram-transformer)** (MIT ใ‹ใ‚‰) Yuan Gong, Yu-An Chung, James Glass ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [AST: Audio Spectrogram Transformer](https://huggingface.co/papers/2104.01778) +1. **[BART](https://huggingface.co/docs/transformers/model_doc/bart)** (Facebook ใ‹ใ‚‰) Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://huggingface.co/papers/1910.13461) +1. **[BARThez](https://huggingface.co/docs/transformers/model_doc/barthez)** (ร‰cole polytechnique ใ‹ใ‚‰) Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) +1. **[BARTpho](https://huggingface.co/docs/transformers/model_doc/bartpho)** (VinAI Research ใ‹ใ‚‰) Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701) +1. **[BEiT](https://huggingface.co/docs/transformers/model_doc/beit)** (Microsoft ใ‹ใ‚‰) Hangbo Bao, Li Dong, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) +1. **[BERT](https://huggingface.co/docs/transformers/model_doc/bert)** (Google ใ‹ใ‚‰) Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) +1. **[BERT For Sequence Generation](https://huggingface.co/docs/transformers/model_doc/bert-generation)** (Google ใ‹ใ‚‰) Sascha Rothe, Shashi Narayan, Aliaksei Severyn ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) +1. **[BERTweet](https://huggingface.co/docs/transformers/model_doc/bertweet)** (VinAI Research ใ‹ใ‚‰) Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) +1. **[BigBird-Pegasus](https://huggingface.co/docs/transformers/model_doc/bigbird_pegasus)** (Google Research ใ‹ใ‚‰) Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) +1. **[BigBird-RoBERTa](https://huggingface.co/docs/transformers/model_doc/big_bird)** (Google Research ใ‹ใ‚‰) Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) +1. **[BioGpt](https://huggingface.co/docs/transformers/main/model_doc/biogpt)** (Microsoft Research AI4Science ใ‹ใ‚‰) Renqian Luo, Liai Sun, Yingce Xia, Tao Qin, Sheng Zhang, Hoifung Poon and Tie-Yan Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) +1. **[BiT](https://huggingface.co/docs/transformers/main/model_doc/bit)** (Google AI ใ‹ใ‚‰) Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai, Joan Puigcerver, Jessica Yung, Sylvain Gelly, Neil ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Big Transfer (BiT)](https://huggingface.co/papers/1912.11370)Houlsby. +1. **[Blenderbot](https://huggingface.co/docs/transformers/model_doc/blenderbot)** (Facebook ใ‹ใ‚‰) Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) +1. **[BlenderbotSmall](https://huggingface.co/docs/transformers/model_doc/blenderbot-small)** (Facebook ใ‹ใ‚‰) Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) +1. **[BLIP](https://huggingface.co/docs/transformers/main/model_doc/blip)** (Salesforce ใ‹ใ‚‰) Junnan Li, Dongxu Li, Caiming Xiong, Steven Hoi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation](https://huggingface.co/papers/2201.12086) +1. **[BLOOM](https://huggingface.co/docs/transformers/model_doc/bloom)** (BigScience workshop ใ‹ใ‚‰) [BigScience Workshop](https://bigscience.huggingface.co/) ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚Œใพใ—ใŸ. +1. **[BORT](https://huggingface.co/docs/transformers/model_doc/bort)** (Alexa ใ‹ใ‚‰) Adrian de Wynter and Daniel J. Perry ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Optimal Subarchitecture Extraction For BERT](https://huggingface.co/papers/2010.10499) +1. **[ByT5](https://huggingface.co/docs/transformers/model_doc/byt5)** (Google Research ใ‹ใ‚‰) Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) +1. **[CamemBERT](https://huggingface.co/docs/transformers/model_doc/camembert)** (Inria/Facebook/Sorbonne ใ‹ใ‚‰) Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suรกrez*, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la Clergerie, Djamรฉ Seddah and Benoรฎt Sagot ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) +1. **[CANINE](https://huggingface.co/docs/transformers/model_doc/canine)** (Google Research ใ‹ใ‚‰) Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://huggingface.co/papers/2103.06874) +1. **[Chinese-CLIP](https://huggingface.co/docs/transformers/model_doc/chinese_clip)** (OFA-Sys ใ‹ใ‚‰) An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese](https://huggingface.co/papers/2211.01335) +1. **[CLIP](https://huggingface.co/docs/transformers/model_doc/clip)** (OpenAI ใ‹ใ‚‰) Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) +1. **[CLIPSeg](https://huggingface.co/docs/transformers/model_doc/clipseg)** (University of Gรถttingen ใ‹ใ‚‰) Timo Lรผddecke and Alexander Ecker ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Image Segmentation Using Text and Image Prompts](https://huggingface.co/papers/2112.10003) +1. **[CodeGen](https://huggingface.co/docs/transformers/model_doc/codegen)** (Salesforce ใ‹ใ‚‰) Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [A Conversational Paradigm for Program Synthesis](https://huggingface.co/papers/2203.13474) +1. **[Conditional DETR](https://huggingface.co/docs/transformers/model_doc/conditional_detr)** (Microsoft Research Asia ใ‹ใ‚‰) Depu Meng, Xiaokang Chen, Zejia Fan, Gang Zeng, Houqiang Li, Yuhui Yuan, Lei Sun, Jingdong Wang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Conditional DETR for Fast Training Convergence](https://huggingface.co/papers/2108.06152) +1. **[ConvBERT](https://huggingface.co/docs/transformers/model_doc/convbert)** (YituTech ใ‹ใ‚‰) Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) +1. **[ConvNeXT](https://huggingface.co/docs/transformers/model_doc/convnext)** (Facebook AI ใ‹ใ‚‰) Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) +1. **[ConvNeXTV2](model_doc/convnextv2)** (from Facebook AI) released with the paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. +1. **[CPM](https://huggingface.co/docs/transformers/model_doc/cpm)** (Tsinghua University ใ‹ใ‚‰) Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) +1. **[CTRL](https://huggingface.co/docs/transformers/model_doc/ctrl)** (Salesforce ใ‹ใ‚‰) Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) +1. **[CvT](https://huggingface.co/docs/transformers/model_doc/cvt)** (Microsoft ใ‹ใ‚‰) Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [CvT: Introducing Convolutions to Vision Transformers](https://huggingface.co/papers/2103.15808) +1. **[Data2Vec](https://huggingface.co/docs/transformers/model_doc/data2vec)** (Facebook ใ‹ใ‚‰) Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) +1. **[DeBERTa](https://huggingface.co/docs/transformers/model_doc/deberta)** (Microsoft ใ‹ใ‚‰) Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) +1. **[DeBERTa-v2](https://huggingface.co/docs/transformers/model_doc/deberta-v2)** (Microsoft ใ‹ใ‚‰) Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) +1. **[Decision Transformer](https://huggingface.co/docs/transformers/model_doc/decision_transformer)** (Berkeley/Facebook/Google ใ‹ใ‚‰) Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) +1. **[Deformable DETR](https://huggingface.co/docs/transformers/model_doc/deformable_detr)** (SenseTime Research ใ‹ใ‚‰) Xizhou Zhu, Weijie Su, Lewei Lu, Bin Li, Xiaogang Wang, Jifeng Dai ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://huggingface.co/papers/2010.04159) +1. **[DeiT](https://huggingface.co/docs/transformers/model_doc/deit)** (Facebook ใ‹ใ‚‰) Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervรฉ Jรฉgou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Training data-efficient image transformers & distillation through attention](https://huggingface.co/papers/2012.12877) +1. **[DETR](https://huggingface.co/docs/transformers/model_doc/detr)** (Facebook ใ‹ใ‚‰) Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [End-to-End Object Detection with Transformers](https://huggingface.co/papers/2005.12872) +1. **[DialoGPT](https://huggingface.co/docs/transformers/model_doc/dialogpt)** (Microsoft Research ใ‹ใ‚‰) Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) +1. **[DiNAT](https://huggingface.co/docs/transformers/model_doc/dinat)** (SHI Labs ใ‹ใ‚‰) Ali Hassani and Humphrey Shi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Dilated Neighborhood Attention Transformer](https://huggingface.co/papers/2209.15001) +1. **[DistilBERT](https://huggingface.co/docs/transformers/model_doc/distilbert)** (HuggingFace ใ‹ใ‚‰), Victor Sanh, Lysandre Debut and Thomas Wolf. ๅŒใ˜ๆ‰‹ๆณ•ใง GPT2, RoBERTa ใจ Multilingual BERT ใฎๅœง็ธฎใ‚’่กŒใ„ใพใ—ใŸ.ๅœง็ธฎใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใใ‚Œใžใ‚Œ [DistilGPT2](https://github.com/huggingface/transformers-research-projects/tree/main/distillation)ใ€[DistilRoBERTa](https://github.com/huggingface/transformers-research-projects/tree/main/distillation)ใ€[DistilmBERT](https://github.com/huggingface/transformers-research-projects/tree/main/distillation) ใจๅไป˜ใ‘ใ‚‰ใ‚Œใพใ—ใŸ. ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://huggingface.co/papers/1910.01108) +1. **[DiT](https://huggingface.co/docs/transformers/model_doc/dit)** (Microsoft Research ใ‹ใ‚‰) Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [DiT: Self-supervised Pre-training for Document Image Transformer](https://huggingface.co/papers/2203.02378) +1. **[Donut](https://huggingface.co/docs/transformers/model_doc/donut)** (NAVER ใ‹ใ‚‰), Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [OCR-free Document Understanding Transformer](https://huggingface.co/papers/2111.15664) +1. **[DPR](https://huggingface.co/docs/transformers/model_doc/dpr)** (Facebook ใ‹ใ‚‰) Vladimir Karpukhin, Barlas OฤŸuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Dense Passage Retrieval for Open-Domain Question Answering](https://huggingface.co/papers/2004.04906) +1. **[DPT](https://huggingface.co/docs/transformers/master/model_doc/dpt)** (Intel Labs ใ‹ใ‚‰) Renรฉ Ranftl, Alexey Bochkovskiy, Vladlen Koltun ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Vision Transformers for Dense Prediction](https://huggingface.co/papers/2103.13413) +1. **[EfficientNet](https://huggingface.co/docs/transformers/model_doc/efficientnet)** (from Google Research) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://huggingface.co/papers/1905.11946) by Mingxing Tan and Quoc V. Le. +1. **[ELECTRA](https://huggingface.co/docs/transformers/model_doc/electra)** (Google Research/Stanford University ใ‹ใ‚‰) Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://huggingface.co/papers/2003.10555) +1. **[EncoderDecoder](https://huggingface.co/docs/transformers/model_doc/encoder-decoder)** (Google Research ใ‹ใ‚‰) Sascha Rothe, Shashi Narayan, Aliaksei Severyn ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) +1. **[ERNIE](https://huggingface.co/docs/transformers/model_doc/ernie)** (Baidu ใ‹ใ‚‰) Yu Sun, Shuohuan Wang, Yukun Li, Shikun Feng, Xuyi Chen, Han Zhang, Xin Tian, Danxiang Zhu, Hao Tian, Hua Wu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ERNIE: Enhanced Representation through Knowledge Integration](https://huggingface.co/papers/1904.09223) +1. **[ESM](https://huggingface.co/docs/transformers/model_doc/esm)** (Meta AI ใ‹ใ‚‰) ใฏใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ—ใƒญใƒ†ใ‚คใƒณ่จ€่ชžใƒขใƒ‡ใƒซใงใ™. **ESM-1b** ใฏ Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118). **ESM-1v** ใฏ Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rivesใ€€ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Language models enable zero-shot prediction of the effects of mutations on protein function](https://doi.org/10.1101/2021.07.09.450648). **ESM-2** ใจใ€€**ESMFold** ใฏ Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902) +1. **[FLAN-T5](https://huggingface.co/docs/transformers/model_doc/flan-t5)** (Google AI ใ‹ใ‚‰) Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸใƒฌใƒใ‚ธใƒˆใƒชใƒผ [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints) Le, and Jason Wei +1. **[FlauBERT](https://huggingface.co/docs/transformers/model_doc/flaubert)** (CNRS ใ‹ใ‚‰) Hang Le, Loรฏc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoรฎt Crabbรฉ, Laurent Besacier, Didier Schwab ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [FlauBERT: Unsupervised Language Model Pre-training for French](https://huggingface.co/papers/1912.05372) +1. **[FLAVA](https://huggingface.co/docs/transformers/model_doc/flava)** (Facebook AI ใ‹ใ‚‰) Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, and Douwe Kiela ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [FLAVA: A Foundational Language And Vision Alignment Model](https://huggingface.co/papers/2112.04482) +1. **[FNet](https://huggingface.co/docs/transformers/model_doc/fnet)** (Google Research ใ‹ใ‚‰) James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [FNet: Mixing Tokens with Fourier Transforms](https://huggingface.co/papers/2105.03824) +1. **[Funnel Transformer](https://huggingface.co/docs/transformers/model_doc/funnel)** (CMU/Google Brain ใ‹ใ‚‰) Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://huggingface.co/papers/2006.03236) +1. **[GIT](https://huggingface.co/docs/transformers/main/model_doc/git)** (Microsoft Research ใ‹ใ‚‰) Jianfeng Wang, Zhengyuan Yang, Xiaowei Hu, Linjie Li, Kevin Lin, Zhe Gan, Zicheng Liu, Ce Liu, Lijuan Wang. ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡ [GIT: A Generative Image-to-text Transformer for Vision and Language](https://huggingface.co/papers/2205.14100) +1. **[GLPN](https://huggingface.co/docs/transformers/model_doc/glpn)** (KAIST ใ‹ใ‚‰) Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://huggingface.co/papers/2201.07436) +1. **[GPT](https://huggingface.co/docs/transformers/model_doc/openai-gpt)** (OpenAI ใ‹ใ‚‰) Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Improving Language Understanding by Generative Pre-Training](https://openai.com/research/language-unsupervised/) +1. **[GPT Neo](https://huggingface.co/docs/transformers/model_doc/gpt_neo)** (EleutherAI ใ‹ใ‚‰) Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸใƒฌใƒใ‚ธใƒˆใƒชใƒผ : [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) +1. **[GPT NeoX](https://huggingface.co/docs/transformers/model_doc/gpt_neox)** (EleutherAI ใ‹ใ‚‰) Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://huggingface.co/papers/2204.06745) +1. **[GPT NeoX Japanese](https://huggingface.co/docs/transformers/model_doc/gpt_neox_japanese)** (ABEJA ใ‹ใ‚‰) Shinya Otani, Takayoshi Makabe, Anuj Arora, and Kyo Hattori ใ‹ใ‚‰ใƒชใƒชใƒผใ‚น. +1. **[GPT-2](https://huggingface.co/docs/transformers/model_doc/gpt2)** (OpenAI ใ‹ใ‚‰) Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Language Models are Unsupervised Multitask Learners](https://openai.com/research/better-language-models/) +1. **[GPT-J](https://huggingface.co/docs/transformers/model_doc/gptj)** (EleutherAI ใ‹ใ‚‰) Ben Wang and Aran Komatsuzaki ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸใƒฌใƒใ‚ธใƒˆใƒชใƒผ [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) +1. **[GPT-Sw3](https://huggingface.co/docs/transformers/main/model_doc/gpt-sw3)** (AI-Sweden ใ‹ใ‚‰) Ariel Ekgren, Amaru Cuba Gyllensten, Evangelia Gogoulou, Alice Heiman, Severine Verlinden, Joey ร–hman, Fredrik Carlsson, Magnus Sahlgren ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Lessons Learned from GPT-SW3: Building the First Large-Scale Generative Language Model for Swedish](http://www.lrec-conf.org/proceedings/lrec2022/pdf/2022.lrec-1.376.pdf) +1. **[GroupViT](https://huggingface.co/docs/transformers/model_doc/groupvit)** (UCSD, NVIDIA ใ‹ใ‚‰) Jiarui Xu, Shalini De Mello, Sifei Liu, Wonmin Byeon, Thomas Breuel, Jan Kautz, Xiaolong Wang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [GroupViT: Semantic Segmentation Emerges from Text Supervision](https://huggingface.co/papers/2202.11094) +1. **[Hubert](https://huggingface.co/docs/transformers/model_doc/hubert)** (Facebook ใ‹ใ‚‰) Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://huggingface.co/papers/2106.07447) +1. **[I-BERT](https://huggingface.co/docs/transformers/model_doc/ibert)** (Berkeley ใ‹ใ‚‰) Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [I-BERT: Integer-only BERT Quantization](https://huggingface.co/papers/2101.01321) +1. **[ImageGPT](https://huggingface.co/docs/transformers/model_doc/imagegpt)** (OpenAI ใ‹ใ‚‰) Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) +1. **[Jukebox](https://huggingface.co/docs/transformers/model_doc/jukebox)** (OpenAI ใ‹ใ‚‰) Prafulla Dhariwal, Heewoo Jun, Christine Payne, Jong Wook Kim, Alec Radford, Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Jukebox: A Generative Model for Music](https://huggingface.co/papers/2005.00341) +1. **[LayoutLM](https://huggingface.co/docs/transformers/model_doc/layoutlm)** (Microsoft Research Asia ใ‹ใ‚‰) Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://huggingface.co/papers/1912.13318) +1. **[LayoutLMv2](https://huggingface.co/docs/transformers/model_doc/layoutlmv2)** (Microsoft Research Asia ใ‹ใ‚‰) Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://huggingface.co/papers/2012.14740) +1. **[LayoutLMv3](https://huggingface.co/docs/transformers/model_doc/layoutlmv3)** (Microsoft Research Asia ใ‹ใ‚‰) Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://huggingface.co/papers/2204.08387) +1. **[LayoutXLM](https://huggingface.co/docs/transformers/model_doc/layoutxlm)** (Microsoft Research Asia ใ‹ใ‚‰) Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://huggingface.co/papers/2104.08836) +1. **[LED](https://huggingface.co/docs/transformers/model_doc/led)** (AllenAI ใ‹ใ‚‰) Iz Beltagy, Matthew E. Peters, Arman Cohan ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) +1. **[LeViT](https://huggingface.co/docs/transformers/model_doc/levit)** (Meta AI ใ‹ใ‚‰) Ben Graham, Alaaeldin El-Nouby, Hugo Touvron, Pierre Stock, Armand Joulin, Hervรฉ Jรฉgou, Matthijs Douze ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LeViT: A Vision Transformer in ConvNet's Clothing for Faster Inference](https://huggingface.co/papers/2104.01136) +1. **[LiLT](https://huggingface.co/docs/transformers/model_doc/lilt)** (South China University of Technology ใ‹ใ‚‰) Jiapeng Wang, Lianwen Jin, Kai Ding ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LiLT: A Simple yet Effective Language-Independent Layout Transformer for Structured Document Understanding](https://huggingface.co/papers/2202.13669) +1. **[Longformer](https://huggingface.co/docs/transformers/model_doc/longformer)** (AllenAI ใ‹ใ‚‰) Iz Beltagy, Matthew E. Peters, Arman Cohan ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) +1. **[LongT5](https://huggingface.co/docs/transformers/model_doc/longt5)** (Google AI ใ‹ใ‚‰) Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://huggingface.co/papers/2112.07916) +1. **[LUKE](https://huggingface.co/docs/transformers/model_doc/luke)** (Studio Ousia ใ‹ใ‚‰) Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://huggingface.co/papers/2010.01057) +1. **[LXMERT](https://huggingface.co/docs/transformers/model_doc/lxmert)** (UNC Chapel Hill ใ‹ใ‚‰) Hao Tan and Mohit Bansal ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://huggingface.co/papers/1908.07490) +1. **[M-CTC-T](https://huggingface.co/docs/transformers/model_doc/mctct)** (Facebook ใ‹ใ‚‰) Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://huggingface.co/papers/2111.00161) +1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (Facebook ใ‹ใ‚‰) Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Beyond English-Centric Multilingual Machine Translation](https://huggingface.co/papers/2010.11125) +1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** Jรถrg Tiedemann ใ‹ใ‚‰. [OPUS](http://opus.nlpl.eu/) ใ‚’ไฝฟใ„ใชใŒใ‚‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ "Machine translation" (ใƒžใ‚ทใƒณใƒˆใƒฉใƒณใ‚นใƒฌใƒผใ‚ทใƒงใƒณ) ใƒขใƒ‡ใƒซ. [Marian Framework](https://marian-nmt.github.io/) ใฏMicrosoft Translator Teamใ€€ใŒ็พๅœจ้–‹็™บไธญใงใ™. +1. **[MarkupLM](https://huggingface.co/docs/transformers/model_doc/markuplm)** (Microsoft Research Asia ใ‹ใ‚‰) Junlong Li, Yiheng Xu, Lei Cui, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://huggingface.co/papers/2110.08518) +1. **[Mask2Former](https://huggingface.co/docs/transformers/main/model_doc/mask2former)** (FAIR and UIUC ใ‹ใ‚‰) Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡ [Masked-attention Mask Transformer for Universal Image Segmentation](https://huggingface.co/papers/2112.01527) +1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (Meta and UIUC ใ‹ใ‚‰) Bowen Cheng, Alexander G. Schwing, Alexander Kirillov ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://huggingface.co/papers/2107.06278) +1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (Facebook ใ‹ใ‚‰) Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Multilingual Denoising Pre-training for Neural Machine Translation](https://huggingface.co/papers/2001.08210) +1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (Facebook ใ‹ใ‚‰) Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://huggingface.co/papers/2008.00401) +1. **[Megatron-BERT](https://huggingface.co/docs/transformers/model_doc/megatron-bert)** (NVIDIA ใ‹ใ‚‰) Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) +1. **[Megatron-GPT2](https://huggingface.co/docs/transformers/model_doc/megatron_gpt2)** (NVIDIA ใ‹ใ‚‰) Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) +1. **[mLUKE](https://huggingface.co/docs/transformers/model_doc/mluke)** (Studio Ousia ใ‹ใ‚‰) Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://huggingface.co/papers/2110.08151) +1. **[MobileBERT](https://huggingface.co/docs/transformers/model_doc/mobilebert)** (CMU/Google Brain ใ‹ใ‚‰) Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://huggingface.co/papers/2004.02984) +1. **[MobileNetV1](https://huggingface.co/docs/transformers/model_doc/mobilenet_v1)** (Google Inc. ใ‹ใ‚‰) Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications](https://huggingface.co/papers/1704.04861) +1. **[MobileNetV2](https://huggingface.co/docs/transformers/model_doc/mobilenet_v2)** (Google Inc. ใ‹ใ‚‰) Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://huggingface.co/papers/1801.04381) +1. **[MobileViT](https://huggingface.co/docs/transformers/model_doc/mobilevit)** (Apple ใ‹ใ‚‰) Sachin Mehta and Mohammad Rastegari ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://huggingface.co/papers/2110.02178) +1. **[MPNet](https://huggingface.co/docs/transformers/model_doc/mpnet)** (Microsoft Research ใ‹ใ‚‰) Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MPNet: Masked and Permuted Pre-training for Language Understanding](https://huggingface.co/papers/2004.09297) +1. **[MT5](https://huggingface.co/docs/transformers/model_doc/mt5)** (Google AI ใ‹ใ‚‰) Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [mT5: A massively multilingual pre-trained text-to-text transformer](https://huggingface.co/papers/2010.11934) +1. **[MVP](https://huggingface.co/docs/transformers/model_doc/mvp)** (RUC AI Box ใ‹ใ‚‰) Tianyi Tang, Junyi Li, Wayne Xin Zhao and Ji-Rong Wen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MVP: Multi-task Supervised Pre-training for Natural Language Generation](https://huggingface.co/papers/2206.12131) +1. **[NAT](https://huggingface.co/docs/transformers/model_doc/nat)** (SHI Labs ใ‹ใ‚‰) Ali Hassani, Steven Walton, Jiachen Li, Shen Li, and Humphrey Shi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Neighborhood Attention Transformer](https://huggingface.co/papers/2204.07143) +1. **[Nezha](https://huggingface.co/docs/transformers/model_doc/nezha)** (Huawei Noahโ€™s Ark Lab ใ‹ใ‚‰) Junqiu Wei, Xiaozhe Ren, Xiaoguang Li, Wenyong Huang, Yi Liao, Yasheng Wang, Jiashu Lin, Xin Jiang, Xiao Chen and Qun Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [NEZHA: Neural Contextualized Representation for Chinese Language Understanding](https://huggingface.co/papers/1909.00204) +1. **[NLLB](https://huggingface.co/docs/transformers/model_doc/nllb)** (Meta ใ‹ใ‚‰) the NLLB team ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [No Language Left Behind: Scaling Human-Centered Machine Translation](https://huggingface.co/papers/2207.04672) +1. **[Nystrรถmformer](https://huggingface.co/docs/transformers/model_doc/nystromformer)** (the University of Wisconsin - Madison ใ‹ใ‚‰) Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Nystrรถmformer: A Nystrรถm-Based Algorithm for Approximating Self-Attention](https://huggingface.co/papers/2102.03902) +1. **[OneFormer](https://huggingface.co/docs/transformers/main/model_doc/oneformer)** (SHI Labs ใ‹ใ‚‰) Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [OneFormer: One Transformer to Rule Universal Image Segmentation](https://huggingface.co/papers/2211.06220) +1. **[OPT](https://huggingface.co/docs/transformers/master/model_doc/opt)** (Meta AI ใ‹ใ‚‰) Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [OPT: Open Pre-trained Transformer Language Models](https://huggingface.co/papers/2205.01068) +1. **[OWL-ViT](https://huggingface.co/docs/transformers/model_doc/owlvit)** (Google AI ใ‹ใ‚‰) Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Simple Open-Vocabulary Object Detection with Vision Transformers](https://huggingface.co/papers/2205.06230) +1. **[Pegasus](https://huggingface.co/docs/transformers/model_doc/pegasus)** (Google ใ‹ใ‚‰) Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://huggingface.co/papers/1912.08777) +1. **[PEGASUS-X](https://huggingface.co/docs/transformers/model_doc/pegasus_x)** (Google ใ‹ใ‚‰) Jason Phang, Yao Zhao, and Peter J. Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Investigating Efficiently Extending Transformers for Long Input Summarization](https://huggingface.co/papers/2208.04347) +1. **[Perceiver IO](https://huggingface.co/docs/transformers/model_doc/perceiver)** (Deepmind ใ‹ใ‚‰) Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hรฉnaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, Joรฃo Carreira ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://huggingface.co/papers/2107.14795) +1. **[PhoBERT](https://huggingface.co/docs/transformers/model_doc/phobert)** (VinAI Research ใ‹ใ‚‰) Dat Quoc Nguyen and Anh Tuan Nguyen ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) +1. **[PLBart](https://huggingface.co/docs/transformers/model_doc/plbart)** (UCLA NLP ใ‹ใ‚‰) Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Unified Pre-training for Program Understanding and Generation](https://huggingface.co/papers/2103.06333) +1. **[PoolFormer](https://huggingface.co/docs/transformers/model_doc/poolformer)** (Sea AI Labs ใ‹ใ‚‰) Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [MetaFormer is Actually What You Need for Vision](https://huggingface.co/papers/2111.11418) +1. **[ProphetNet](https://huggingface.co/docs/transformers/model_doc/prophetnet)** (Microsoft Research ใ‹ใ‚‰) Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) +1. **[QDQBert](https://huggingface.co/docs/transformers/model_doc/qdqbert)** (NVIDIA ใ‹ใ‚‰) Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius Micikevicius ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://huggingface.co/papers/2004.09602) +1. **[RAG](https://huggingface.co/docs/transformers/model_doc/rag)** (Facebook ใ‹ใ‚‰) Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Kรผttler, Mike Lewis, Wen-tau Yih, Tim Rocktรคschel, Sebastian Riedel, Douwe Kiela ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://huggingface.co/papers/2005.11401) +1. **[REALM](https://huggingface.co/docs/transformers/model_doc/realm.html)** (Google Research ใ‹ใ‚‰) Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [REALM: Retrieval-Augmented Language Model Pre-Training](https://huggingface.co/papers/2002.08909) +1. **[Reformer](https://huggingface.co/docs/transformers/model_doc/reformer)** (Google Research ใ‹ใ‚‰) Nikita Kitaev, ลukasz Kaiser, Anselm Levskaya ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) +1. **[RegNet](https://huggingface.co/docs/transformers/model_doc/regnet)** (META Platforms ใ‹ใ‚‰) Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollรกr ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Designing Network Design Space](https://huggingface.co/papers/2003.13678) +1. **[RemBERT](https://huggingface.co/docs/transformers/model_doc/rembert)** (Google Research ใ‹ใ‚‰) Hyung Won Chung, Thibault Fรฉvry, Henry Tsai, M. Johnson, Sebastian Ruder ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Rethinking embedding coupling in pre-trained language models](https://huggingface.co/papers/2010.12821) +1. **[ResNet](https://huggingface.co/docs/transformers/model_doc/resnet)** (Microsoft Research ใ‹ใ‚‰) Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Deep Residual Learning for Image Recognition](https://huggingface.co/papers/1512.03385) +1. **[RoBERTa](https://huggingface.co/docs/transformers/model_doc/roberta)** (Facebook ใ‹ใ‚‰), Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692) +1. **[RoBERTa-PreLayerNorm](https://huggingface.co/docs/transformers/main/model_doc/roberta-prelayernorm)** (Facebook ใ‹ใ‚‰) Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [fairseq: A Fast, Extensible Toolkit for Sequence Modeling](https://huggingface.co/papers/1904.01038) +1. **[RoCBert](https://huggingface.co/docs/transformers/main/model_doc/roc_bert)** (WeChatAI ใ‹ใ‚‰) HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining](https://aclanthology.org/2022.acl-long.65.pdf) +1. **[RoFormer](https://huggingface.co/docs/transformers/model_doc/roformer)** (ZhuiyiTechnology ใ‹ใ‚‰), Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://huggingface.co/papers/2104.09864) +1. **[SegFormer](https://huggingface.co/docs/transformers/model_doc/segformer)** (NVIDIA ใ‹ใ‚‰) Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://huggingface.co/papers/2105.15203) +1. **[SEW](https://huggingface.co/docs/transformers/model_doc/sew)** (ASAPP ใ‹ใ‚‰) Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) +1. **[SEW-D](https://huggingface.co/docs/transformers/model_doc/sew_d)** (ASAPP ใ‹ใ‚‰) Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) +1. **[SpeechToTextTransformer](https://huggingface.co/docs/transformers/model_doc/speech_to_text)** (Facebook ใ‹ใ‚‰), Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://huggingface.co/papers/2010.05171) +1. **[SpeechToTextTransformer2](https://huggingface.co/docs/transformers/model_doc/speech_to_text_2)** (Facebook ใ‹ใ‚‰), Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://huggingface.co/papers/2104.06678) +1. **[Splinter](https://huggingface.co/docs/transformers/model_doc/splinter)** (Tel Aviv University ใ‹ใ‚‰), Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Few-Shot Question Answering by Pretraining Span Selection](https://huggingface.co/papers/2101.00438) +1. **[SqueezeBERT](https://huggingface.co/docs/transformers/model_doc/squeezebert)** (Berkeley ใ‹ใ‚‰) Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://huggingface.co/papers/2006.11316) +1. **[Swin Transformer](https://huggingface.co/docs/transformers/model_doc/swin)** (Microsoft ใ‹ใ‚‰) Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030) +1. **[Swin Transformer V2](https://huggingface.co/docs/transformers/model_doc/swinv2)** (Microsoft ใ‹ใ‚‰) Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Swin Transformer V2: Scaling Up Capacity and Resolution](https://huggingface.co/papers/2111.09883) +1. **[Swin2SR](https://huggingface.co/docs/transformers/main/model_doc/swin2sr)** (University of Wรผrzburg ใ‹ใ‚‰) Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://huggingface.co/papers/2209.11345) +1. **[SwitchTransformers](https://huggingface.co/docs/transformers/main/model_doc/switch_transformers)** (Google ใ‹ใ‚‰) William Fedus, Barret Zoph, Noam Shazeer ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity](https://huggingface.co/papers/2101.03961) +1. **[T5](https://huggingface.co/docs/transformers/model_doc/t5)** (Google AI ใ‹ใ‚‰) Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://huggingface.co/papers/1910.10683) +1. **[T5v1.1](https://huggingface.co/docs/transformers/model_doc/t5v1.1)** (Google AI ใ‹ใ‚‰) Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸใƒฌใƒใ‚ธใƒˆใƒชใƒผ [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) +1. **[Table Transformer](https://huggingface.co/docs/transformers/model_doc/table-transformer)** (Microsoft Research ใ‹ใ‚‰) Brandon Smock, Rohith Pesala, Robin Abraham ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents](https://huggingface.co/papers/2110.00061) +1. **[TAPAS](https://huggingface.co/docs/transformers/model_doc/tapas)** (Google AI ใ‹ใ‚‰) Jonathan Herzig, Paweล‚ Krzysztof Nowak, Thomas Mรผller, Francesco Piccinno and Julian Martin Eisenschlos ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://huggingface.co/papers/2004.02349) +1. **[TAPEX](https://huggingface.co/docs/transformers/model_doc/tapex)** (Microsoft Research ใ‹ใ‚‰) Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://huggingface.co/papers/2107.07653) +1. **[Time Series Transformer](https://huggingface.co/docs/transformers/model_doc/time_series_transformer)** (HuggingFace ใ‹ใ‚‰). +1. **[TimeSformer](https://huggingface.co/docs/transformers/main/model_doc/timesformer)** (Facebook ใ‹ใ‚‰) Gedas Bertasius, Heng Wang, Lorenzo Torresani ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Is Space-Time Attention All You Need for Video Understanding?](https://huggingface.co/papers/2102.05095) +1. **[Trajectory Transformer](https://huggingface.co/docs/transformers/model_doc/trajectory_transformers)** (the University of California at Berkeley ใ‹ใ‚‰) Michael Janner, Qiyang Li, Sergey Levine ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://huggingface.co/papers/2106.02039) +1. **[Transformer-XL](https://huggingface.co/docs/transformers/model_doc/transfo-xl)** (Google/CMU ใ‹ใ‚‰) Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://huggingface.co/papers/1901.02860) +1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (Microsoft ใ‹ใ‚‰), Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://huggingface.co/papers/2109.10282) +1. **[UL2](https://huggingface.co/docs/transformers/model_doc/ul2)** (Google Research ใ‹ใ‚‰) Yi Tay, Mostafa Dehghani, Vinh Q ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Unifying Language Learning Paradigms](https://huggingface.co/papers/2205.05131v1) Tran, Xavier Garcia, Dara Bahri, Tal Schuster, Huaixiu Steven Zheng, Neil Houlsby, Donald Metzler +1. **[UniSpeech](https://huggingface.co/docs/transformers/model_doc/unispeech)** (Microsoft Research ใ‹ใ‚‰) Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://huggingface.co/papers/2101.07597) +1. **[UniSpeechSat](https://huggingface.co/docs/transformers/model_doc/unispeech-sat)** (Microsoft Research ใ‹ใ‚‰) Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://huggingface.co/papers/2110.05752) +1. **[UPerNet](https://huggingface.co/docs/transformers/main/model_doc/upernet)** (Peking University ใ‹ใ‚‰) Tete Xiao, Yingcheng Liu, Bolei Zhou, Yuning Jiang, Jian Sun. ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡ [Unified Perceptual Parsing for Scene Understanding](https://huggingface.co/papers/1807.10221) +1. **[VAN](https://huggingface.co/docs/transformers/model_doc/van)** (Tsinghua University and Nankai University ใ‹ใ‚‰) Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Visual Attention Network](https://huggingface.co/papers/2202.09741) +1. **[VideoMAE](https://huggingface.co/docs/transformers/model_doc/videomae)** (Multimedia Computing Group, Nanjing University ใ‹ใ‚‰) Zhan Tong, Yibing Song, Jue Wang, Limin Wang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training](https://huggingface.co/papers/2203.12602) +1. **[ViLT](https://huggingface.co/docs/transformers/model_doc/vilt)** (NAVER AI Lab/Kakao Enterprise/Kakao Brain ใ‹ใ‚‰) Wonjae Kim, Bokyung Son, Ildoo Kim ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://huggingface.co/papers/2102.03334) +1. **[Vision Transformer (ViT)](https://huggingface.co/docs/transformers/model_doc/vit)** (Google AI ใ‹ใ‚‰) Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) +1. **[VisualBERT](https://huggingface.co/docs/transformers/model_doc/visual_bert)** (UCLA NLP ใ‹ใ‚‰) Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://huggingface.co/papers/1908.03557) +1. **[ViT Hybrid](https://huggingface.co/docs/transformers/main/model_doc/vit_hybrid)** (Google AI ใ‹ใ‚‰) Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) +1. **[ViTMAE](https://huggingface.co/docs/transformers/model_doc/vit_mae)** (Meta AI ใ‹ใ‚‰) Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollรกr, Ross Girshick ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Masked Autoencoders Are Scalable Vision Learners](https://huggingface.co/papers/2111.06377) +1. **[ViTMSN](https://huggingface.co/docs/transformers/model_doc/vit_msn)** (Meta AI ใ‹ใ‚‰) Mahmoud Assran, Mathilde Caron, Ishan Misra, Piotr Bojanowski, Florian Bordes, Pascal Vincent, Armand Joulin, Michael Rabbat, Nicolas Ballas ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Masked Siamese Networks for Label-Efficient Learning](https://huggingface.co/papers/2204.07141) +1. **[Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/wav2vec2)** (Facebook AI ใ‹ใ‚‰) Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://huggingface.co/papers/2006.11477) +1. **[Wav2Vec2-Conformer](https://huggingface.co/docs/transformers/model_doc/wav2vec2-conformer)** (Facebook AI ใ‹ใ‚‰) Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Sravya Popuri, Dmytro Okhonko, Juan Pino ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [FAIRSEQ S2T: Fast Speech-to-Text Modeling with FAIRSEQ](https://huggingface.co/papers/2010.05171) +1. **[Wav2Vec2Phoneme](https://huggingface.co/docs/transformers/model_doc/wav2vec2_phoneme)** (Facebook AI ใ‹ใ‚‰) Qiantong Xu, Alexei Baevski, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://huggingface.co/papers/2109.11680) +1. **[WavLM](https://huggingface.co/docs/transformers/model_doc/wavlm)** (Microsoft Research ใ‹ใ‚‰) Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://huggingface.co/papers/2110.13900) +1. **[Whisper](https://huggingface.co/docs/transformers/model_doc/whisper)** (OpenAI ใ‹ใ‚‰) Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf) +1. **[X-CLIP](https://huggingface.co/docs/transformers/model_doc/xclip)** (Microsoft Research ใ‹ใ‚‰) Bolin Ni, Houwen Peng, Minghao Chen, Songyang Zhang, Gaofeng Meng, Jianlong Fu, Shiming Xiang, Haibin Ling ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Expanding Language-Image Pretrained Models for General Video Recognition](https://huggingface.co/papers/2208.02816) +1. **[XGLM](https://huggingface.co/docs/transformers/model_doc/xglm)** (From Facebook AI) Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O'Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Few-shot Learning with Multilingual Language Models](https://huggingface.co/papers/2112.10668) +1. **[XLM](https://huggingface.co/docs/transformers/model_doc/xlm)** (Facebook ใ‹ใ‚‰) Guillaume Lample and Alexis Conneau ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Cross-lingual Language Model Pretraining](https://huggingface.co/papers/1901.07291) +1. **[XLM-ProphetNet](https://huggingface.co/docs/transformers/model_doc/xlm-prophetnet)** (Microsoft Research ใ‹ใ‚‰) Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) +1. **[XLM-RoBERTa](https://huggingface.co/docs/transformers/model_doc/xlm-roberta)** (Facebook AI ใ‹ใ‚‰), Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmรกn, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Unsupervised Cross-lingual Representation Learning at Scale](https://huggingface.co/papers/1911.02116) +1. **[XLM-RoBERTa-XL](https://huggingface.co/docs/transformers/model_doc/xlm-roberta-xl)** (Facebook AI ใ‹ใ‚‰), Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://huggingface.co/papers/2105.00572) +1. **[XLNet](https://huggingface.co/docs/transformers/model_doc/xlnet)** (Google/CMU ใ‹ใ‚‰) Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [โ€‹XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://huggingface.co/papers/1906.08237) +1. **[XLS-R](https://huggingface.co/docs/transformers/model_doc/xls_r)** (Facebook AI ใ‹ใ‚‰) Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://huggingface.co/papers/2111.09296) +1. **[XLSR-Wav2Vec2](https://huggingface.co/docs/transformers/model_doc/xlsr_wav2vec2)** (Facebook AI ใ‹ใ‚‰) Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://huggingface.co/papers/2006.13979) +1. **[YOLOS](https://huggingface.co/docs/transformers/model_doc/yolos)** (Huazhong University of Science & Technology ใ‹ใ‚‰) Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://huggingface.co/papers/2106.00666) +1. **[YOSO](https://huggingface.co/docs/transformers/model_doc/yoso)** (the University of Wisconsin - Madison ใ‹ใ‚‰) Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh ใ‹ใ‚‰ๅ…ฌ้–‹ใ•ใ‚ŒใŸ็ ”็ฉถ่ซ–ๆ–‡: [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://huggingface.co/papers/2111.09714) + + +### ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ + +ไปฅไธ‹ใฎใƒ†ใƒผใƒ–ใƒซใฏใใ‚Œใžใ‚Œใฎใƒขใƒ‡ใƒซใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚"slow"ใจๅ‘ผใฐใ‚Œใ‚‹Pythonใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€๐Ÿค— Tokenizers ใƒฉใ‚คใƒ–ใƒฉใƒชใซใ‚ˆใ‚‹"fast"ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€PyTorch, TensorFlow, Flaxใฎ5ใคใฎใใ‚Œใžใ‚ŒใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + + + +| Model | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | +|:-----------------------------:|:--------------:|:--------------:|:---------------:|:------------------:|:------------:| +| ALBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| AltCLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| Audio Spectrogram Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| BART | โœ… | โœ… | โœ… | โœ… | โœ… | +| BEiT | โŒ | โŒ | โœ… | โŒ | โœ… | +| BERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| Bert Generation | โœ… | โŒ | โœ… | โŒ | โŒ | +| BigBird | โœ… | โœ… | โœ… | โŒ | โœ… | +| BigBird-Pegasus | โŒ | โŒ | โœ… | โŒ | โŒ | +| BioGpt | โœ… | โŒ | โœ… | โŒ | โŒ | +| BiT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Blenderbot | โœ… | โœ… | โœ… | โœ… | โœ… | +| BlenderbotSmall | โœ… | โœ… | โœ… | โœ… | โœ… | +| BLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| BLOOM | โŒ | โœ… | โœ… | โŒ | โŒ | +| CamemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| CANINE | โœ… | โŒ | โœ… | โŒ | โŒ | +| Chinese-CLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| CLIP | โœ… | โœ… | โœ… | โœ… | โœ… | +| CLIPSeg | โŒ | โŒ | โœ… | โŒ | โŒ | +| CodeGen | โœ… | โœ… | โœ… | โŒ | โŒ | +| Conditional DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| ConvBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ConvNeXT | โŒ | โŒ | โœ… | โœ… | โŒ | +| CTRL | โœ… | โŒ | โœ… | โœ… | โŒ | +| CvT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Data2VecAudio | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecText | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecVision | โŒ | โŒ | โœ… | โœ… | โŒ | +| DeBERTa | โœ… | โœ… | โœ… | โœ… | โŒ | +| DeBERTa-v2 | โœ… | โœ… | โœ… | โœ… | โŒ | +| Decision Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Deformable DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeiT | โŒ | โŒ | โœ… | โœ… | โŒ | +| DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DiNAT | โŒ | โŒ | โœ… | โŒ | โŒ | +| DistilBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| DonutSwin | โŒ | โŒ | โœ… | โŒ | โŒ | +| DPR | โœ… | โœ… | โœ… | โœ… | โŒ | +| DPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ELECTRA | โœ… | โœ… | โœ… | โœ… | โœ… | +| Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| ERNIE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ESM | โœ… | โŒ | โœ… | โœ… | โŒ | +| FairSeq Machine-Translation | โœ… | โŒ | โœ… | โŒ | โŒ | +| FlauBERT | โœ… | โŒ | โœ… | โœ… | โŒ | +| FLAVA | โŒ | โŒ | โœ… | โŒ | โŒ | +| FNet | โœ… | โœ… | โœ… | โŒ | โŒ | +| Funnel Transformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| GIT | โŒ | โŒ | โœ… | โŒ | โŒ | +| GLPN | โŒ | โŒ | โœ… | โŒ | โŒ | +| GPT Neo | โŒ | โŒ | โœ… | โŒ | โœ… | +| GPT NeoX | โŒ | โœ… | โœ… | โŒ | โŒ | +| GPT NeoX Japanese | โœ… | โŒ | โœ… | โŒ | โŒ | +| GPT-J | โŒ | โŒ | โœ… | โœ… | โœ… | +| GPT-Sw3 | โœ… | โœ… | โœ… | โœ… | โœ… | +| GroupViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Hubert | โŒ | โŒ | โœ… | โœ… | โŒ | +| I-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ImageGPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Jukebox | โœ… | โŒ | โœ… | โŒ | โŒ | +| LayoutLM | โœ… | โœ… | โœ… | โœ… | โŒ | +| LayoutLMv2 | โœ… | โœ… | โœ… | โŒ | โŒ | +| LayoutLMv3 | โœ… | โœ… | โœ… | โœ… | โŒ | +| LED | โœ… | โœ… | โœ… | โœ… | โŒ | +| LeViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| LiLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Longformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| LongT5 | โŒ | โŒ | โœ… | โŒ | โœ… | +| LUKE | โœ… | โŒ | โœ… | โŒ | โŒ | +| LXMERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| M-CTC-T | โŒ | โŒ | โœ… | โŒ | โŒ | +| M2M100 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Marian | โœ… | โŒ | โœ… | โœ… | โœ… | +| MarkupLM | โœ… | โœ… | โœ… | โŒ | โŒ | +| Mask2Former | โŒ | โŒ | โœ… | โŒ | โŒ | +| MaskFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| MaskFormerSwin | โŒ | โŒ | โŒ | โŒ | โŒ | +| mBART | โœ… | โœ… | โœ… | โœ… | โœ… | +| Megatron-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| MobileNetV1 | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileNetV2 | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| MPNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| MT5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| MVP | โœ… | โœ… | โœ… | โŒ | โŒ | +| NAT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Nezha | โŒ | โŒ | โœ… | โŒ | โŒ | +| Nystrรถmformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OpenAI GPT | โœ… | โœ… | โœ… | โœ… | โŒ | +| OpenAI GPT-2 | โœ… | โœ… | โœ… | โœ… | โœ… | +| OPT | โŒ | โŒ | โœ… | โœ… | โœ… | +| OWL-ViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Pegasus | โœ… | โœ… | โœ… | โœ… | โœ… | +| PEGASUS-X | โŒ | โŒ | โœ… | โŒ | โŒ | +| Perceiver | โœ… | โŒ | โœ… | โŒ | โŒ | +| PLBart | โœ… | โŒ | โœ… | โŒ | โŒ | +| PoolFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| QDQBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| RAG | โœ… | โŒ | โœ… | โœ… | โŒ | +| REALM | โœ… | โœ… | โœ… | โŒ | โŒ | +| Reformer | โœ… | โœ… | โœ… | โŒ | โŒ | +| RegNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ResNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RetriBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| RoBERTa-PreLayerNorm | โŒ | โŒ | โœ… | โœ… | โœ… | +| RoCBert | โœ… | โŒ | โœ… | โŒ | โŒ | +| RoFormer | โœ… | โœ… | โœ… | โœ… | โœ… | +| SegFormer | โŒ | โŒ | โœ… | โœ… | โŒ | +| SEW | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW-D | โŒ | โŒ | โœ… | โŒ | โŒ | +| Speech Encoder decoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| Speech2Text | โœ… | โŒ | โœ… | โœ… | โŒ | +| Speech2Text2 | โœ… | โŒ | โŒ | โŒ | โŒ | +| Splinter | โœ… | โœ… | โœ… | โŒ | โŒ | +| SqueezeBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| Swin Transformer | โŒ | โŒ | โœ… | โœ… | โŒ | +| Swin Transformer V2 | โŒ | โŒ | โœ… | โŒ | โŒ | +| Swin2SR | โŒ | โŒ | โœ… | โŒ | โŒ | +| SwitchTransformers | โŒ | โŒ | โœ… | โŒ | โŒ | +| T5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Table Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| TAPAS | โœ… | โŒ | โœ… | โœ… | โŒ | +| Time Series Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| TimeSformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Trajectory Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Transformer-XL | โœ… | โŒ | โœ… | โœ… | โŒ | +| TrOCR | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeech | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeechSat | โŒ | โŒ | โœ… | โŒ | โŒ | +| UPerNet | โŒ | โŒ | โœ… | โŒ | โŒ | +| VAN | โŒ | โŒ | โœ… | โŒ | โŒ | +| VideoMAE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Vision Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| VisionTextDualEncoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| VisualBERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViT | โŒ | โŒ | โœ… | โœ… | โœ… | +| ViT Hybrid | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViTMAE | โŒ | โŒ | โœ… | โœ… | โŒ | +| ViTMSN | โŒ | โŒ | โœ… | โŒ | โŒ | +| Wav2Vec2 | โœ… | โŒ | โœ… | โœ… | โœ… | +| Wav2Vec2-Conformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| WavLM | โŒ | โŒ | โœ… | โŒ | โŒ | +| Whisper | โœ… | โŒ | โœ… | โœ… | โŒ | +| X-CLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| XGLM | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM | โœ… | โŒ | โœ… | โœ… | โŒ | +| XLM-ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| XLM-RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM-RoBERTa-XL | โŒ | โŒ | โœ… | โŒ | โŒ | +| XLNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| YOLOS | โŒ | โŒ | โœ… | โŒ | โŒ | +| YOSO | โŒ | โŒ | โœ… | โŒ | โŒ | + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/installation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/installation.md new file mode 100644 index 0000000000000000000000000000000000000000..cb4919caf50571b95ffd9bacd6292c0fdf5d6294 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/installation.md @@ -0,0 +1,244 @@ + + +# ใ‚คใƒณใ‚นใƒˆใƒผใƒซ + +ไฝฟ็”จใ—ใฆใ„ใ‚‹Deep Learningใƒฉใ‚คใƒ–ใƒฉใƒชใซๅฏพใ—ใฆใ€๐Ÿค— Transformersใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚’่จญๅฎšใ€ใใ—ใฆใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ‚ชใƒ•ใƒฉใ‚คใƒณใงๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซ ๐Ÿค— Transformersใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + +๐Ÿค— TransformersใฏPython 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, Flaxใงๅ‹•ไฝœ็ขบ่ชใ—ใฆใ„ใพใ™ใ€‚ ไฝฟ็”จใ—ใฆใ„ใ‚‹Deep Learningใƒฉใ‚คใƒ–ใƒฉใƒชใซๅˆใ‚ใ›ใฆใ€ไปฅไธ‹ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ–นๆณ•ใซๅพ“ใฃใฆใใ ใ•ใ„: + +* [PyTorch](https://pytorch.org/get-started/locally/)ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใ€‚ +* [TensorFlow 2.0](https://www.tensorflow.org/install/pip)ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใ€‚ +* [Flax](https://flax.readthedocs.io/en/latest/)ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใ€‚ + +## pipใงใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ + +๐Ÿค— Transformersใ‚’[ไปฎๆƒณ็’ฐๅขƒ](https://docs.python.org/3/library/venv.html)ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ‚‚ใ—ใ€Pythonใฎไปฎๆƒณ็’ฐๅขƒใซ้ฆดๆŸ“ใฟใŒใชใ„ๅ ดๅˆใฏใ€ใ“ใฎ[ใ‚ฌใ‚คใƒ‰](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ไปฎๆƒณ็’ฐๅขƒใซใ‚ˆใฃใฆ็•ฐใชใ‚‹ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใฎ็ฎก็†ใŒใ‚ˆใ‚Š็ฐกๅ˜ใซใชใ‚Šใ€ไพๅญ˜้–ขไฟ‚้–“ใฎไบ’ๆ›ๆ€งใฎๅ•้กŒใ‚’ๅ›ž้ฟใงใใพใ™ใ€‚ + +ใพใšใ€ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซไปฎๆƒณ็’ฐๅขƒใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†: + +```bash +python -m venv .env +``` + +ไปฎๆƒณ็’ฐๅขƒใ‚’่ตทๅ‹•ใ—ใพใ—ใ‚‡ใ†ใ€‚LinuxใจMacOsใฎๅ ดๅˆใฏไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใง่ตทๅ‹•ใ—ใพใ™: + +```bash +source .env/bin/activate +``` +Windowsใงไปฎๆƒณ็’ฐๅขƒใ‚’่ตทๅ‹•ใ—ใพใ™ + +```bash +.env/Scripts/activate +``` + +ใ“ใ‚Œใงใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใง๐Ÿค— Transformersใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸ: + +```bash +pip install transformers +``` + +CPUๅฏพๅฟœใฎใฟๅฟ…่ฆใชๅ ดๅˆใ€๐Ÿค— TransformersใจDeep Learningใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’1่กŒใงใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใ‚‹ใ‚ˆใ†ใซใชใฃใฆใ„ใฆไพฟๅˆฉใงใ™ใ€‚ไพ‹ใˆใฐใ€๐Ÿค— TransformersใจPyTorchใ‚’ไปฅไธ‹ใฎใ‚ˆใ†ใซไธ€็ท’ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™: + +```bash +pip install transformers[torch] +``` + +๐Ÿค— TransformersใจTensorFlow 2.0: + +```bash +pip install transformers[tf-cpu] +``` + +๐Ÿค— TransformersใจFlax: + +```bash +pip install transformers[flax] +``` + +ๆœ€ๅพŒใซใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใง๐Ÿค— TransformersใŒๆญฃใ—ใใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใŒใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" +``` + +ใใฎๅพŒใ€ใƒฉใƒ™ใƒซใจใ‚นใ‚ณใ‚ขใŒๅ‡บๅŠ›ใ•ใ‚Œใพใ™: + +```bash +[{'label': 'POSITIVE', 'score': 0.9998704791069031}] +``` + +## ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ + +ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใงใ‚ฝใƒผใ‚นใ‹ใ‚‰๐Ÿค— Transformersใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™: + +```bash +pip install git+https://github.com/huggingface/transformers +``` + +ใ“ใฎใ‚ณใƒžใƒณใƒ‰ใฏๆœ€ๆ–ฐใฎๅฎ‰ๅฎš็‰ˆใงใฏใชใใ€้–‹็™บใซใŠใ‘ใ‚‹ๆœ€ๆ–ฐใฎ`main`ใƒใƒผใ‚ธใƒงใƒณใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚`main`ใƒใƒผใ‚ธใƒงใƒณใฏๆœ€ๆ–ฐใฎ้–‹็™บ็Šถๆณใซๅฏพๅฟœใ™ใ‚‹ใฎใซไพฟๅˆฉใงใ™ใ€‚ไพ‹ใˆใฐใ€ๆœ€ๅพŒใฎๅ…ฌๅผใƒชใƒชใƒผใ‚นไปฅ้™ใซใƒใ‚ฐใŒไฟฎๆญฃใ•ใ‚ŒใŸใŒใ€ๆ–ฐใ—ใ„ใƒชใƒชใƒผใ‚นใŒใพใ ๅฑ•้–‹ใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใชใฉใงใ™ใ€‚ใ—ใ‹ใ—ใ€ใ“ใ‚Œใฏ`main`ใƒใƒผใ‚ธใƒงใƒณใŒๅธธใซๅฎ‰ๅฎšใ—ใฆใ„ใ‚‹ใจใฏ้™ใ‚‰ใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚็งใŸใกใฏ`main`ใƒใƒผใ‚ธใƒงใƒณใฎ้‹็”จใ‚’็ถญๆŒใ™ใ‚‹ใ‚ˆใ†ๅŠชใ‚ใ€ใปใจใ‚“ใฉใฎๅ•้กŒใฏ้€šๅธธใ€ๆ•ฐๆ™‚้–“ใ‹ใ‚‰1ๆ—ฅไปฅๅ†…ใซ่งฃๆฑบใ•ใ‚Œใพใ™ใ€‚ใ‚‚ใ—ๅ•้กŒใซ้ญ้‡ใ—ใŸๅ ดๅˆใฏใ€ใ‚ˆใ‚Šๆ—ฉใไฟฎๆญฃใงใใ‚‹ใ‚ˆใ†ใซ[Issue](https://github.com/huggingface/transformers/issues)ใ‚’ไฝœๆˆใ—ใฆใใ ใ•ใ„๏ผ + +ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใ€๐Ÿค— TransformersใŒๆญฃใ—ใใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใพใ™: + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" +``` + +## ็ทจ้›†ๅฏ่ƒฝใชใ‚คใƒณใ‚นใƒˆใƒผใƒซ + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€็ทจ้›†ๅฏ่ƒฝใชใ‚คใƒณใ‚นใƒˆใƒผใƒซใ‚’ใ—ใพใ™: + +* ใ‚ฝใƒผใ‚นใ‚ณใƒผใƒ‰ใฎ`main`ใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝฟใ„ใพใ™ใ€‚ +* ๐Ÿค— Transformersใซใ‚ณใƒณใƒˆใƒชใƒ“ใƒฅใƒผใƒˆใ—ใ€ใ‚ณใƒผใƒ‰ใฎๅค‰ๆ›ดใ‚’ใƒ†ใ‚นใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใงใƒฌใƒใ‚ธใƒˆใƒชใ‚’ใ‚ฏใƒญใƒผใƒณใ—ใฆใ€๐Ÿค— Transformersใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™: + +```bash +git clone https://github.com/huggingface/transformers.git +cd transformers +pip install -e . +``` + +ไธŠ่จ˜ใฎใ‚ณใƒžใƒณใƒ‰ใฏใ€ใƒฌใƒใ‚ธใƒˆใƒชใ‚’ใ‚ฏใƒญใƒผใƒณใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ใจPythonใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใƒ‘ใ‚นใ‚’ใƒชใƒณใ‚ฏใ—ใพใ™ใ€‚Pythonใฏ้€šๅธธใฎใƒฉใ‚คใƒ–ใƒฉใƒชใƒ‘ใ‚นใซๅŠ ใˆใฆใ€ใ‚ใชใŸใŒใ‚ฏใƒญใƒผใƒณใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ใฎไธญใ‚‚่ฆ‹ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€Pythonใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใŒ้€šๅธธใ€`~/anaconda3/envs/main/lib/python3.7/site-packages/`ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€Pythonใฏใ‚ฏใƒญใƒผใƒณใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ใ‚‚ๆคœ็ดขใ™ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™: `~/transformers/`. + + + +ใƒฉใ‚คใƒ–ใƒฉใƒชใƒผใ‚’ไฝฟใ„็ถšใ‘ใŸใ„ๅ ดๅˆใฏใ€transformersใƒ•ใ‚ฉใƒซใƒ€ใƒผใ‚’ไฟๆŒใ—ใคใฅใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ใ“ใ‚Œใงใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใง็ฐกๅ˜ใซใ‚ฏใƒญใƒผใƒณใ‚’๐Ÿค— Transformersใฎๆœ€ๆ–ฐ็‰ˆใซๆ›ดๆ–ฐใงใใพใ™: + +```bash +cd ~/transformers/ +git pull +``` + +Python็’ฐๅขƒใฏๆฌกๅ›žใฎๅฎŸ่กŒๆ™‚ใซ๐Ÿค— Transformersใฎ`main`ใƒใƒผใ‚ธใƒงใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +## condaใงใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ + +`conda-forge`ใฎcondaใƒใƒฃใƒณใƒใƒซใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™: + +```bash +conda install conda-forge::transformers +``` + +## ใ‚ญใƒฃใƒƒใ‚ทใƒฅใฎ่จญๅฎš + +ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฏใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œใ€ใƒญใƒผใ‚ซใƒซใซใ‚ญใƒฃใƒƒใ‚ทใƒฅใ•ใ‚Œใพใ™: `~/.cache/huggingface/hub`. ใ“ใ‚Œใฏใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐ`TRANSFORMERS_CACHE`ใงๆŒ‡ๅฎšใ•ใ‚Œใ‚‹ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใงใ™ใ€‚Windowsใงใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฏ`C:\Users\username\.cache\huggingface\hub`ใซใชใฃใฆใ„ใพใ™ใ€‚็•ฐใชใ‚‹ใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใŸใ‚ใซใ€ไปฅไธ‹ใฎใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใŒๅฏ่ƒฝใงใ™ใ€‚ๅ„ชๅ…ˆๅบฆใฏไปฅไธ‹ใฎ้ †็•ชใซๅฏพๅฟœใ—ใพใ™: + +1. ใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐ (ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆ): `HF_HUB_CACHE` ใพใŸใฏ `TRANSFORMERS_CACHE`. +2. ใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐ: `HF_HOME`. +3. ใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐ: `XDG_CACHE_HOME` + `/huggingface`. + + + +ใ‚‚ใ—ใ€ไปฅๅ‰ใฎใƒใƒผใ‚ธใƒงใƒณใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ„ใŸไบบใงใ€`PYTORCH_TRANSFORMERS_CACHE`ใพใŸใฏ`PYTORCH_PRETRAINED_BERT_CACHE`ใ‚’่จญๅฎšใ—ใฆใ„ใŸๅ ดๅˆใ€ใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐ`TRANSFORMERS_CACHE`ใ‚’ๆŒ‡ๅฎšใ—ใชใ„้™ใ‚Š๐Ÿค— Transformersใฏใ“ใ‚Œใ‚‰ใฎใ‚ทใ‚งใƒซ็’ฐๅขƒๅค‰ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + + + +## ใ‚ชใƒ•ใƒฉใ‚คใƒณใƒขใƒผใƒ‰ + +๐Ÿค— Transformersใฏใƒญใƒผใ‚ซใƒซใƒ•ใ‚กใ‚คใƒซใฎใฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใƒ•ใ‚กใ‚คใ‚ขใ‚ฆใ‚ฉใƒผใƒซใ‚„ใ‚ชใƒ•ใƒฉใ‚คใƒณใฎ็’ฐๅขƒใงใ‚‚ๅ‹•ไฝœใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใฎๅ‹•ไฝœใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใŸใ‚ใซใฏใ€็’ฐๅขƒๅค‰ๆ•ฐ`HF_HUB_OFFLINE=1`ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + + + +็’ฐๅขƒๅค‰ๆ•ฐ`HF_DATASETS_OFFLINE=1`ใ‚’่จญๅฎšใ—ใ€ใ‚ชใƒ•ใƒฉใ‚คใƒณใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใซ[๐Ÿค— Datasets](https://huggingface.co/docs/datasets/)ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + + + +ไพ‹ใˆใฐใ€ๅค–้ƒจใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซๅฏพใ—ใฆใƒ•ใ‚กใ‚คใ‚ขใ‚ฆใ‚ฉใƒผใƒซใงไฟ่ญทใ•ใ‚ŒใŸ้€šๅธธใฎใƒใƒƒใƒˆใƒฏใƒผใ‚ฏไธŠใงใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅ ดๅˆใ€้€šๅธธไปฅไธ‹ใฎใ‚ˆใ†ใชใ‚ณใƒžใƒณใƒ‰ใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™: + +```bash +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +ใ‚ชใƒ•ใƒฉใ‚คใƒณใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใงใ“ใฎๅŒใ˜ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ—ใพใ™: + +```bash +HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +ใ“ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ใƒญใƒผใ‚ซใƒซใƒ•ใ‚กใ‚คใƒซใฎใฟใ‚’ๆคœ็ดขใ™ใ‚‹ใ“ใจใŒๅˆ†ใ‹ใฃใฆใ„ใ‚‹ใฎใงใ€ใƒใƒณใ‚ฐใ‚ขใƒƒใƒ—ใ—ใŸใ‚Šใ‚ฟใ‚คใƒ ใ‚ขใ‚ฆใƒˆใ‚’ๅพ…ใฃใŸใ‚Šใ™ใ‚‹ใ“ใจใชใๅฎŸ่กŒใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ + +### ใ‚ชใƒ•ใƒฉใ‚คใƒณใงไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใƒขใƒ‡ใƒซใ‚„ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ๅ–ๅพ—ใ™ใ‚‹ + +ใ‚ชใƒ•ใƒฉใ‚คใƒณใง๐Ÿค— Transformersใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚‚ใ†1ใคใฎๆ–นๆณ•ใฏใ€ๅ‰ใ‚‚ใฃใฆใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆใŠใใ€ใ‚ชใƒ•ใƒฉใ‚คใƒณใงไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใจใใซใใฎใƒญใƒผใ‚ซใƒซใƒ‘ใ‚นใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใฏ3ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™: + +* [Model Hub](https://huggingface.co/models)ใฎใƒฆใƒผใ‚ถใƒผใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นไธŠใ‹ใ‚‰โ†“ใ‚ขใ‚คใ‚ณใƒณใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ€‚ + + ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) + +* [`PreTrainedModel.from_pretrained`]ใŠใ‚ˆใณ[`PreTrainedModel.save_pretrained`]ใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•: + + 1. [`PreTrainedModel.from_pretrained`]ใงๅ‰ใ‚‚ใฃใฆใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใพใ™: + + ```py + >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + + >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") + >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") + ``` + + 2. [`PreTrainedModel.save_pretrained`]ใงๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซใƒ•ใ‚กใ‚คใƒซใ‚’ไฟๅญ˜ใ—ใฆใŠใใพใ™: + + ```py + >>> tokenizer.save_pretrained("./your/path/bigscience_t0") + >>> model.save_pretrained("./your/path/bigscience_t0") + ``` + + 3. ใ‚ชใƒ•ใƒฉใ‚คใƒณใซใ‚ใ‚‹ๆ™‚ใ€[`PreTrainedModel.from_pretrained`]ใซๆŒ‡ๅฎšใ—ใŸใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‹ใ‚‰ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒชใƒญใƒผใƒ‰ใ—ใพใ™: + + ```py + >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") + >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") + ``` + +* ใƒ—ใƒญใ‚ฐใƒฉใƒ ็š„ใซ[huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub)ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’็”จใ„ใฆใ€ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•: + + 1. ไปฎๆƒณ็’ฐๅขƒใซ`huggingface_hub`ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™: + + ```bash + python -m pip install huggingface_hub + ``` + + 2. ๆŒ‡ๅฎšใฎใƒ‘ใ‚นใซใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซใ€[`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub)้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใงใ€[T0](https://huggingface.co/bigscience/T0_3B)ใƒขใƒ‡ใƒซใฎ`config.json`ใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ๅฎšใฎใƒ‘ใ‚นใซใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใงใใพใ™: + + ```py + >>> from huggingface_hub import hf_hub_download + + >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0") + ``` + +ใƒ•ใ‚กใ‚คใƒซใŒใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œใ€ใƒญใƒผใ‚ซใƒซใซใ‚ญใƒฃใƒƒใ‚ทใƒฅใ•ใ‚ŒใŸใ‚‰ใ€ใใฎใƒญใƒผใ‚ซใƒซใƒ‘ใ‚นใ‚’ๆŒ‡ๅฎšใ—ใฆใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆไฝฟ็”จใ—ใพใ™: + +```py +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") +``` + + + +Hubใซไฟๅญ˜ใ•ใ‚Œใฆใ„ใ‚‹ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream)ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/audio_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/audio_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..967c716cd2f93ce104c4f6ebcedc97a9bee79963 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/audio_utils.md @@ -0,0 +1,39 @@ + + +# `FeatureExtractor` ็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€*็Ÿญๆ™‚้–“ใƒ•ใƒผใƒชใ‚จๅค‰ๆ›* ใ‚„ *ใƒญใ‚ฐ ใƒกใƒซ ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ * ใชใฉใฎไธ€่ˆฌ็š„ใชใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ไฝฟ็”จใ—ใฆ็”Ÿใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‹ใ‚‰็‰นๅˆฅใช็‰นๅพดใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช [`FeatureExtractor`] ใงไฝฟ็”จใงใใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใ‚ณใƒผใƒ‰ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +## ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅค‰ๆ› + +[[autodoc]] audio_utils.hertz_to_mel + +[[autodoc]] audio_utils.mel_to_hertz + +[[autodoc]] audio_utils.mel_filter_bank + +[[autodoc]] audio_utils.optimal_fft_length + +[[autodoc]] audio_utils.window_function + +[[autodoc]] audio_utils.spectrogram + +[[autodoc]] audio_utils.power_to_db + +[[autodoc]] audio_utils.amplitude_to_db diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/file_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/file_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..51a025bfc67f17f6b463294a04ba7f6111d9e77e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/file_utils.md @@ -0,0 +1,49 @@ + + +# ไธ€่ˆฌ็š„ใชใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€ใƒ•ใ‚กใ‚คใƒซ `utils.py` ใซใ‚ใ‚‹ Transformers ใฎไธ€่ˆฌ็š„ใชใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใ™ในใฆใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใงไธ€่ˆฌ็š„ใชใ‚ณใƒผใƒ‰ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +## ๅˆ—ๆŒ™ๅž‹ใจๅๅ‰ไป˜ใใ‚ฟใƒ—ใƒซ + +[[autodoc]] utils.ExplicitEnum + +[[autodoc]] utils.PaddingStrategy + +[[autodoc]] utils.TensorType + +## ็‰นๅˆฅใชใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใƒผ + +[[autodoc]] utils.add_start_docstrings + +[[autodoc]] utils.add_start_docstrings_to_model_forward + +[[autodoc]] utils.add_end_docstrings + +[[autodoc]] utils.add_code_sample_docstrings + +[[autodoc]] utils.replace_return_docstrings + +## ็‰นๆฎŠใชใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃ + +[[autodoc]] utils.cached_property + +## ใใฎไป–ใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +[[autodoc]] utils._LazyModule diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/generation_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/generation_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..1a5cc1dec079584f471ff3179c868971957ac855 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/generation_utils.md @@ -0,0 +1,336 @@ + + +# ็™บ้›ป็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€[`~generation.GenerationMixin.generate`] ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ + +[`~generation.GenerationMixin.generate`] ใฎๅ‡บๅŠ›ใฏใ€ๆฌกใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใงใ™ใ€‚ +[`~utils.ModelOutput`]ใ€‚ใ“ใฎๅ‡บๅŠ›ใฏใ€่ฟ”ใ•ใ‚ŒใŸใ™ในใฆใฎๆƒ…ๅ ฑใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟๆง‹้€ ใงใ™ใ€‚ +[`~generation.GenerationMixin.generate`] ใซใ‚ˆใฃใฆไฝœๆˆใ•ใ‚Œใพใ™ใŒใ€ใ‚ฟใƒ—ใƒซใพใŸใฏ่พžๆ›ธใจใ—ใฆใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ + +ไปฅไธ‹ใซไพ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +```python +from transformers import GPT2Tokenizer, GPT2LMHeadModel + +tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2") +model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2") + +inputs = tokenizer("Hello, my dog is cute and ", return_tensors="pt") +generation_output = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) +``` + +`generation_output` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏใ€ใงใใ‚‹้™ใ‚Š [`~generation.GenerateDecoderOnlyOutput`] ใงใ™ใ€‚ +ไปฅไธ‹ใฎใใฎใ‚ฏใƒฉใ‚นใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใฏใ€ๆฌกใฎๅฑžๆ€งใŒใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + +- `sequences`: ็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚น +- `scores` (ใ‚ชใƒ—ใ‚ทใƒงใƒณ): ๅ„็”Ÿๆˆใ‚นใƒ†ใƒƒใƒ—ใฎ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใƒ˜ใƒƒใƒ‰ใฎไบˆๆธฌใ‚นใ‚ณใ‚ข +- `hidden_โ€‹โ€‹states` (ใ‚ชใƒ—ใ‚ทใƒงใƒณ): ็”Ÿๆˆใ‚นใƒ†ใƒƒใƒ—ใ”ใจใฎใƒขใƒ‡ใƒซใฎ้š ใ‚ŒใŸ็Šถๆ…‹ +- `attentions` (ใ‚ชใƒ—ใ‚ทใƒงใƒณ): ็”Ÿๆˆใ‚นใƒ†ใƒƒใƒ—ใ”ใจใฎใƒขใƒ‡ใƒซใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฎ้‡ใฟ + +ใ“ใ“ใงใฏใ€`output_scores=True`ใ‚’ๆธกใ—ใŸใฎใง `scores` ใŒใ‚ใ‚Šใพใ™ใŒใ€`hidden_โ€‹โ€‹states` ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +`attentions` ใฏใ€`output_hidden_โ€‹โ€‹states=True`ใพใŸใฏ`output_attentions=True`ใ‚’ๆธกใ•ใชใ‹ใฃใŸใŸใ‚ใงใ™ใ€‚ + +้€šๅธธใจๅŒใ˜ใ‚ˆใ†ใซๅ„ๅฑžๆ€งใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ใใฎๅฑžๆ€งใŒใƒขใƒ‡ใƒซใ‹ใ‚‰่ฟ”ใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใฏใ€ +ใฏใ€Œใชใ—ใ€ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ“ใ“ใงใ€ใŸใจใˆใฐ`generation_output.scores`ใฏใ€็”Ÿๆˆใ•ใ‚ŒใŸใ™ในใฆใฎไบˆๆธฌใ‚นใ‚ณใ‚ขใงใ™ใ€‚ +่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎใƒ˜ใƒƒใƒ‰ใงใ‚ใ‚Šใ€`generation_output.attentions`ใฏ`None`ใงใ™ใ€‚ + +`generation_output` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚ฟใƒ—ใƒซใจใ—ใฆไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`None` ๅ€คใ‚’ๆŒใŸใชใ„ๅฑžๆ€งใฎใฟใŒไฟๆŒใ•ใ‚Œใพใ™ใ€‚ +ใŸใจใˆใฐใ€ใ“ใ“ใซใฏ 2 ใคใฎ่ฆ็ด ใ€`loss`ใ€ๆฌกใซ`logits`ใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +generation_output[:2] +``` + +ใŸใจใˆใฐใ€ใ‚ฟใƒ—ใƒซ `(generation_output.sequences,generation_output.scores)` ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +`generation_output` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่พžๆ›ธใจใ—ใฆไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`None` ใ‚’ๆŒใŸใชใ„ๅฑžๆ€งใฎใฟใŒไฟๆŒใ•ใ‚Œใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€ใŸใจใˆใฐใ€`sequences`ใจ`scores`ใจใ„ใ† 2 ใคใฎใ‚ญใƒผใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใ“ใงใฏใ™ในใฆใฎๅ‡บๅŠ›ใ‚ฟใ‚คใƒ—ใ‚’ๆ–‡ๆ›ธๅŒ–ใ—ใพใ™ใ€‚ + +### PyTorch + +[[autodoc]] generation.GenerateDecoderOnlyOutput + +[[autodoc]] generation.GenerateEncoderDecoderOutput + +[[autodoc]] generation.GenerateBeamDecoderOnlyOutput + +[[autodoc]] generation.GenerateBeamEncoderDecoderOutput + +### TensorFlow + +[[autodoc]] generation.TFGreedySearchEncoderDecoderOutput + +[[autodoc]] generation.TFGreedySearchDecoderOnlyOutput + +[[autodoc]] generation.TFSampleEncoderDecoderOutput + +[[autodoc]] generation.TFSampleDecoderOnlyOutput + +[[autodoc]] generation.TFBeamSearchEncoderDecoderOutput + +[[autodoc]] generation.TFBeamSearchDecoderOnlyOutput + +[[autodoc]] generation.TFBeamSampleEncoderDecoderOutput + +[[autodoc]] generation.TFBeamSampleDecoderOnlyOutput + +[[autodoc]] generation.TFContrastiveSearchEncoderDecoderOutput + +[[autodoc]] generation.TFContrastiveSearchDecoderOnlyOutput + +### FLAX + +[[autodoc]] generation.FlaxSampleOutput + +[[autodoc]] generation.FlaxGreedySearchOutput + +[[autodoc]] generation.FlaxBeamSearchOutput + +## LogitsProcessor + +[`LogitsProcessor`] ใ‚’ไฝฟ็”จใ—ใฆใ€่จ€่ชžใƒขใƒ‡ใƒซใฎใƒ˜ใƒƒใƒ‰ใฎไบˆๆธฌใ‚นใ‚ณใ‚ขใ‚’ๅค‰ๆ›ดใงใใพใ™ใ€‚ +ไธ–ไปฃใ€‚ + +### PyTorch + +[[autodoc]] AlternatingCodebooksLogitsProcessor + - __call__ + +[[autodoc]] ClassifierFreeGuidanceLogitsProcessor + - __call__ + +[[autodoc]] EncoderNoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] EncoderRepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] EpsilonLogitsWarper + - __call__ + +[[autodoc]] EtaLogitsWarper + - __call__ + +[[autodoc]] ExponentialDecayLengthPenalty + - __call__ + +[[autodoc]] ForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] ForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] HammingDiversityLogitsProcessor + - __call__ + +[[autodoc]] InfNanRemoveLogitsProcessor + - __call__ + +[[autodoc]] LogitNormalization + - __call__ + +[[autodoc]] LogitsProcessor + - __call__ + +[[autodoc]] LogitsProcessorList + - __call__ + +[[autodoc]] MinLengthLogitsProcessor + - __call__ + +[[autodoc]] MinNewTokensLengthLogitsProcessor + - __call__ + +[[autodoc]] NoBadWordsLogitsProcessor + - __call__ + +[[autodoc]] NoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] PrefixConstrainedLogitsProcessor + - __call__ + +[[autodoc]] RepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] SequenceBiasLogitsProcessor + - __call__ + +[[autodoc]] SuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] SuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] TemperatureLogitsWarper + - __call__ + +[[autodoc]] TopKLogitsWarper + - __call__ + +[[autodoc]] TopPLogitsWarper + - __call__ + +[[autodoc]] TypicalLogitsWarper + - __call__ + +[[autodoc]] UnbatchedClassifierFreeGuidanceLogitsProcessor + - __call__ + +[[autodoc]] WhisperTimeStampLogitsProcessor + - __call__ + +### TensorFlow + +[[autodoc]] TFForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] TFForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] TFForceTokensLogitsProcessor + - __call__ + +[[autodoc]] TFLogitsProcessor + - __call__ + +[[autodoc]] TFLogitsProcessorList + - __call__ + +[[autodoc]] TFLogitsWarper + - __call__ + +[[autodoc]] TFMinLengthLogitsProcessor + - __call__ + +[[autodoc]] TFNoBadWordsLogitsProcessor + - __call__ + +[[autodoc]] TFNoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] TFRepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] TFSuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] TFSuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] TFTemperatureLogitsWarper + - __call__ + +[[autodoc]] TFTopKLogitsWarper + - __call__ + +[[autodoc]] TFTopPLogitsWarper + - __call__ + +### FLAX + +[[autodoc]] FlaxForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] FlaxForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] FlaxForceTokensLogitsProcessor + - __call__ + +[[autodoc]] FlaxLogitsProcessor + - __call__ + +[[autodoc]] FlaxLogitsProcessorList + - __call__ + +[[autodoc]] FlaxLogitsWarper + - __call__ + +[[autodoc]] FlaxMinLengthLogitsProcessor + - __call__ + +[[autodoc]] FlaxSuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] FlaxSuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] FlaxTemperatureLogitsWarper + - __call__ + +[[autodoc]] FlaxTopKLogitsWarper + - __call__ + +[[autodoc]] FlaxTopPLogitsWarper + - __call__ + +[[autodoc]] FlaxWhisperTimeStampLogitsProcessor + - __call__ + +## StoppingCriteria + +[`StoppingCriteria`] ใ‚’ไฝฟ็”จใ—ใฆใ€(EOS ใƒˆใƒผใ‚ฏใƒณไปฅๅค–ใฎ) ็”Ÿๆˆใ‚’ๅœๆญขใ™ใ‚‹ใ‚ฟใ‚คใƒŸใƒณใ‚ฐใ‚’ๅค‰ๆ›ดใงใใพใ™ใ€‚ใ“ใ‚Œใฏ PyTorch ๅฎŸ่ฃ…ใงใฎใฟๅˆฉ็”จๅฏ่ƒฝใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] StoppingCriteria + - __call__ + +[[autodoc]] StoppingCriteriaList + - __call__ + +[[autodoc]] MaxLengthCriteria + - __call__ + +[[autodoc]] MaxTimeCriteria + - __call__ + +## Constraints + +[`Constraint`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€็”Ÿๆˆๆ™‚ใซๅ‡บๅŠ›ใซ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใพใŸใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใŒๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซๅผทๅˆถใงใใพใ™ใ€‚ใ“ใ‚Œใฏ PyTorch ๅฎŸ่ฃ…ใงใฎใฟๅˆฉ็”จๅฏ่ƒฝใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] Constraint + +[[autodoc]] PhrasalConstraint + +[[autodoc]] DisjunctiveConstraint + +[[autodoc]] ConstraintListState + +## BeamSearch + +[[autodoc]] BeamScorer + - process + - finalize + +[[autodoc]] BeamSearchScorer + - process + - finalize + +[[autodoc]] ConstrainedBeamSearchScorer + - process + - finalize + +## Streamers + +[[autodoc]] TextStreamer + +[[autodoc]] TextIteratorStreamer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/image_processing_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/image_processing_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..917f86875f481260c0d86c32f8ef25bc6b129d2a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/image_processing_utils.md @@ -0,0 +1,48 @@ + + +# ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ต็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใƒผ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไธปใซๆฉŸ่ƒฝ็š„ใชใ‚‚ใฎใงใ™ใ€‚ +็”ปๅƒใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅค‰ๆ›ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใ‚ณใƒผใƒ‰ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +## Image Transformations + +[[autodoc]] image_transforms.center_crop + +[[autodoc]] image_transforms.center_to_corners_format + +[[autodoc]] image_transforms.corners_to_center_format + +[[autodoc]] image_transforms.id_to_rgb + +[[autodoc]] image_transforms.normalize + +[[autodoc]] image_transforms.pad + +[[autodoc]] image_transforms.rgb_to_id + +[[autodoc]] image_transforms.rescale + +[[autodoc]] image_transforms.resize + +[[autodoc]] image_transforms.to_pil_image + +## ImageProcessingMixin + +[[autodoc]] image_processing_utils.ImageProcessingMixin diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/modeling_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/modeling_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..6e8335623a0153d9d15d07d92a8eb75a3a9c0e9d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/modeling_utils.md @@ -0,0 +1,66 @@ + + +# ใ‚ซใ‚นใ‚ฟใƒ ใƒฌใ‚คใƒคใƒผใจใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒฌใ‚คใƒคใƒผใจใ€ใƒขใƒ‡ใƒชใƒณใ‚ฐใซๆไพ›ใ•ใ‚Œใ‚‹ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใ‚’็ ”็ฉถใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + + +## Pytorch custom modules + +[[autodoc]] pytorch_utils.Conv1D + +## PyTorch Helper Functions + +[[autodoc]] pytorch_utils.apply_chunking_to_forward + +[[autodoc]] pytorch_utils.find_pruneable_heads_and_indices + +[[autodoc]] pytorch_utils.prune_layer + +[[autodoc]] pytorch_utils.prune_conv1d_layer + +[[autodoc]] pytorch_utils.prune_linear_layer + +## TensorFlow custom layers + +[[autodoc]] modeling_tf_utils.TFConv1D + +[[autodoc]] modeling_tf_utils.TFSequenceSummary + +## TensorFlow loss functions + +[[autodoc]] modeling_tf_utils.TFCausalLanguageModelingLoss + +[[autodoc]] modeling_tf_utils.TFMaskedLanguageModelingLoss + +[[autodoc]] modeling_tf_utils.TFMultipleChoiceLoss + +[[autodoc]] modeling_tf_utils.TFQuestionAnsweringLoss + +[[autodoc]] modeling_tf_utils.TFSequenceClassificationLoss + +[[autodoc]] modeling_tf_utils.TFTokenClassificationLoss + +## TensorFlow Helper Functions + +[[autodoc]] modeling_tf_utils.get_initializer + +[[autodoc]] modeling_tf_utils.keras_serializable + +[[autodoc]] modeling_tf_utils.shape_list diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/pipelines_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/pipelines_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..833c98c4d0dc18cab391e620cea02a4d5a041a9b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/pipelines_utils.md @@ -0,0 +1,44 @@ + + +# ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ็”จใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใŒใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๆไพ›ใ™ใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใ‚’็ ”็ฉถใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + + +## Argument handling + +[[autodoc]] pipelines.ArgumentHandler + +[[autodoc]] pipelines.ZeroShotClassificationArgumentHandler + +[[autodoc]] pipelines.QuestionAnsweringArgumentHandler + +## Data format + +[[autodoc]] pipelines.PipelineDataFormat + +[[autodoc]] pipelines.CsvPipelineDataFormat + +[[autodoc]] pipelines.JsonPipelineDataFormat + +[[autodoc]] pipelines.PipedPipelineDataFormat + +## Utilities + +[[autodoc]] pipelines.PipelineException diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/time_series_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/time_series_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..9355ea090e14584b32f67e61a84f185cc1bc5821 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/time_series_utils.md @@ -0,0 +1,29 @@ + + +# ๆ™‚็ณปๅˆ—ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€ๆ™‚็ณปๅˆ—ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใซไฝฟ็”จใงใใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใจใ‚ฏใƒฉใ‚นใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ๆ™‚็ณปๅˆ—ใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใ‚’็ ”็ฉถใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใพใŸใฏๅˆ†ๆ•ฃๅ‡บๅŠ›ใ‚ฏใƒฉใ‚นใฎใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใซ่ฟฝๅŠ ใ—ใŸใ„ๅ ดๅˆใซใฎใฟๅฝน็ซ‹ใกใพใ™ใ€‚ + +## Distributional Output + +[[autodoc]] time_series_utils.NormalOutput + +[[autodoc]] time_series_utils.StudentTOutput + +[[autodoc]] time_series_utils.NegativeBinomialOutput \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/tokenization_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/tokenization_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..8e36e4149e278415d7a11e7195234ea2e2bfaa04 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/tokenization_utils.md @@ -0,0 +1,42 @@ + + +# Utilities for Tokenizers + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซใ‚ˆใฃใฆไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐ (ไธปใซใ‚ฏใƒฉใ‚น) ใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ +[`~tokenization_utils_base.PreTrainedTokenizerBase`] ้–“ใฎๅ…ฑ้€šใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ +[`PreTrainedTokenizer`] ใจ [`PreTrainedTokenizerFast`] ใŠใ‚ˆใณใƒŸใƒƒใ‚ฏใ‚นใ‚คใƒณ +[`~tokenization_utils_base.SpecialTokensMixin`]ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎใ‚ณใƒผใƒ‰ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +## PreTrainedTokenizerBase + +[[autodoc]] tokenization_utils_base.PreTrainedTokenizerBase + - __call__ + - all + +## SpecialTokensMixin + +[[autodoc]] tokenization_utils_base.SpecialTokensMixin + +## Enums and namedtuples + +[[autodoc]] tokenization_utils_base.TruncationStrategy + +[[autodoc]] tokenization_utils_base.CharSpan + +[[autodoc]] tokenization_utils_base.TokenSpan diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/trainer_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/trainer_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..dddfc7dc55ee12827447217f0e65e53dac0652e7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/internal/trainer_utils.md @@ -0,0 +1,49 @@ + + +# ใƒˆใƒฌใƒผใƒŠใƒผ็”จใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ + +ใ“ใฎใƒšใƒผใ‚ธใซใฏใ€[`Trainer`] ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ้–ขๆ•ฐใŒใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใปใจใ‚“ใฉใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใƒˆใƒฌใƒผใƒŠใƒผใฎใ‚ณใƒผใƒ‰ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +## Utilities + +[[autodoc]] EvalPrediction + +[[autodoc]] IntervalStrategy + +[[autodoc]] enable_full_determinism + +[[autodoc]] set_seed + +[[autodoc]] torch_distributed_zero_first + +## Callbacks internals + +[[autodoc]] trainer_callback.CallbackHandler + +## Distributed Evaluation + +[[autodoc]] trainer_pt_utils.DistributedTensorGatherer + +## Trainer Argument Parser + +[[autodoc]] HfArgumentParser + +## Debug Utilities + +[[autodoc]] debug_utils.DebugUnderflowOverflow diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/llm_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/llm_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..671382f8c13806c2eefdd1ea20f711d1434394ae --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/llm_tutorial.md @@ -0,0 +1,223 @@ + + +# Generation with LLMs + +[[open-in-colab]] + +LLMใ€ใพใŸใฏLarge Language Models๏ผˆๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซ๏ผ‰ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎ้ตใจใชใ‚‹่ฆ็ด ใงใ™ใ€‚่ฆใ™ใ‚‹ใซใ€ใ“ใ‚Œใ‚‰ใฏๅคง่ฆๆจกใชไบ‹ๅ‰่จ“็ทดๆธˆใฟใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใงใ€ไธŽใˆใ‚‰ใ‚ŒใŸๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใซๅŸบใฅใ„ใฆๆฌกใฎๅ˜่ชž๏ผˆใพใŸใฏใ€ใ‚ˆใ‚Šๆญฃ็ขบใซใฏใƒˆใƒผใ‚ฏใƒณ๏ผ‰ใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซ่จ“็ทดใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณใ‚’1ใคใšใคไบˆๆธฌใ™ใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใ‚’ๅ‘ผใณๅ‡บใ™ใ ใ‘ใงใฏๆ–ฐใ—ใ„ๆ–‡ใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซไฝ•ใ‹ใ‚ˆใ‚Š็ฒพๅทงใชใ“ใจใ‚’ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚่‡ชๅทฑๅ›žๅธฐ็”Ÿๆˆใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +่‡ชๅทฑๅ›žๅธฐ็”Ÿๆˆใฏใ€ๆŽจ่ซ–ๆ™‚ใฎๆ‰‹็ถšใใงใ€ใ„ใใคใ‹ใฎๅˆๆœŸๅ…ฅๅŠ›ใ‚’ไธŽใˆใŸ็Šถๆ…‹ใงใ€ใƒขใƒ‡ใƒซใ‚’ๅๅพฉ็š„ใซๅ‘ผใณๅ‡บใ™ๆ‰‹ๆณ•ใงใ™ใ€‚๐Ÿค— Transformersใงใฏใ€ใ“ใ‚Œใฏ[`~generation.GenerationMixin.generate`]ใƒกใ‚ฝใƒƒใƒ‰ใซใ‚ˆใฃใฆๅ‡ฆ็†ใ•ใ‚Œใ€ใ“ใ‚Œใฏ็”Ÿๆˆ่ƒฝๅŠ›ใ‚’ๆŒใคใ™ในใฆใฎใƒขใƒ‡ใƒซใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ไปฅไธ‹ใฎใ“ใจใ‚’็คบใ—ใพใ™๏ผš + +* LLMใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ๆ–นๆณ• +* ไธ€่ˆฌ็š„ใช่ฝใจใ—็ฉดใ‚’ๅ›ž้ฟใ™ใ‚‹ๆ–นๆณ• +* LLMใ‚’ๆœ€ๅคง้™ใซๆดป็”จใ™ใ‚‹ใŸใ‚ใฎๆฌกใฎใ‚นใƒ†ใƒƒใƒ— + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผš + + +```bash +pip install transformers bitsandbytes>=0.39.0 -q +``` + +## Generate text + +[ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](tasks/language_modeling)ใฎใŸใ‚ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใ€ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใฎ็ขบ็އๅˆ†ๅธƒใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +
+ +
"Forward pass of an LLM"
+
+ + +LLM๏ผˆLanguage Model๏ผ‰ใซใ‚ˆใ‚‹่‡ชๅทฑๅ›žๅธฐ็”Ÿๆˆใฎ้‡่ฆใชๅด้ขใฎ1ใคใฏใ€ใ“ใฎ็ขบ็އๅˆ†ๅธƒใ‹ใ‚‰ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’้ธๆŠžใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ใ“ใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€ๆฌกใฎใ‚คใƒ†ใƒฌใƒผใ‚ทใƒงใƒณใฎใŸใ‚ใฎใƒˆใƒผใ‚ฏใƒณใŒๅพ—ใ‚‰ใ‚Œใ‚‹้™ใ‚Šใ€ไฝ•ใงใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ใ“ใ‚Œใฏใ€็ขบ็އๅˆ†ๅธƒใ‹ใ‚‰ๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’้ธๆŠžใ™ใ‚‹ใ ใ‘ใฎใ‚ทใƒณใƒ—ใƒซใชๆ–นๆณ•ใ‹ใ‚‰ใ€็ตๆžœใฎๅˆ†ๅธƒใ‹ใ‚‰ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซๆ•ฐใ€…ใฎๅค‰ๆ›ใ‚’้ฉ็”จใ™ใ‚‹ใปใฉ่ค‡้›‘ใชๆ–นๆณ•ใพใงใ€ใ‚ใ‚‰ใ‚†ใ‚‹ๆ–นๆณ•ใŒ่€ƒใˆใ‚‰ใ‚Œใพใ™ใ€‚ + + + +
+ +
"Autoregressive generation iteratively selects the next token from a probability distribution to generate text"
+
+ +ไธŠ่จ˜ใฎใƒ—ใƒญใ‚ปใ‚นใฏใ€ใ‚ใ‚‹ๅœๆญขๆกไปถใŒๆบ€ใŸใ•ใ‚Œใ‚‹ใพใงๅๅพฉ็š„ใซ็นฐใ‚Š่ฟ”ใ•ใ‚Œใพใ™ใ€‚็†ๆƒณ็š„ใซใฏใ€ๅœๆญขๆกไปถใฏใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆๆŒ‡็คบใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฏ็ต‚ไบ†ใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผˆ`EOS`๏ผ‰ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ‚ฟใ‚คใƒŸใƒณใ‚ฐใ‚’ๅญฆ็ฟ’ใ™ในใใงใ™ใ€‚ใ“ใ‚ŒใŒใใ†ใงใชใ„ๅ ดๅˆใ€็”Ÿๆˆใฏใ‚ใ‚‰ใ‹ใ˜ใ‚ๅฎš็พฉใ•ใ‚ŒใŸๆœ€ๅคง้•ทใซ้”ใ—ใŸใจใใซๅœๆญขใ—ใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒณ้ธๆŠžใ‚นใƒ†ใƒƒใƒ—ใจๅœๆญขๆกไปถใ‚’้ฉๅˆ‡ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใฏใ€ใƒขใƒ‡ใƒซใŒใ‚ฟใ‚นใ‚ฏใงๆœŸๅพ…ใฉใŠใ‚ŠใซๆŒฏใ‚‹่ˆžใ†ใŸใ‚ใซ้‡่ฆใงใ™ใ€‚ใใ‚ŒใŒใ€ๅ„ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸ [`~generation.GenerationConfig`] ใƒ•ใ‚กใ‚คใƒซใŒใ‚ใ‚‹็†็”ฑใงใ‚ใ‚Šใ€ใ“ใ‚Œใซใฏๅ„ชใ‚ŒใŸใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”Ÿๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟๅŒ–ใŒๅซใพใ‚Œใ€ใƒขใƒ‡ใƒซใจไธ€็ท’ใซ่ชญใฟ่พผใพใ‚Œใพใ™ใ€‚ + +ใ‚ณใƒผใƒ‰ใซใคใ„ใฆ่ฉฑใ—ใพใ—ใ‚‡ใ†๏ผ + + + +ๅŸบๆœฌ็š„ใชLLMใฎไฝฟ็”จใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใ€้ซ˜ใƒฌใƒ™ใƒซใฎ [`Pipeline`](pipeline_tutorial) ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใŒ่‰ฏใ„ๅ‡บ็™บ็‚นใงใ™ใ€‚ใŸใ ใ—ใ€LLMใฏใ—ใฐใ—ใฐ้‡ๅญๅŒ–ใ‚„ใƒˆใƒผใ‚ฏใƒณ้ธๆŠžใ‚นใƒ†ใƒƒใƒ—ใฎ็ดฐใ‹ใ„ๅˆถๅพกใชใฉใฎ้ซ˜ๅบฆใชๆฉŸ่ƒฝใŒๅฟ…่ฆใงใ‚ใ‚Šใ€ใ“ใ‚Œใฏ [`~generation.GenerationMixin.generate`] ใ‚’ไป‹ใ—ใฆๆœ€่‰ฏใซ่กŒใ‚ใ‚Œใพใ™ใ€‚LLMใจใฎ่‡ชๅทฑๅ›žๅธฐ็”Ÿๆˆใฏใƒชใ‚ฝใƒผใ‚นใŒๅคšใๅฟ…่ฆใงใ‚ใ‚Šใ€้ฉๅˆ‡ใชใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใฎใŸใ‚ใซGPUใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + + +ใพใšใ€ใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained( +... "openlm-research/open_llama_7b", device_map="auto", load_in_4bit=True +... ) +``` + +`from_pretrained` ๅ‘ผใณๅ‡บใ—ใง2ใคใฎใƒ•ใƒฉใ‚ฐใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„๏ผš + +- `device_map` ใฏใƒขใƒ‡ใƒซใ‚’ใ‚ใชใŸใฎGPUใซ็งปๅ‹•ใ•ใ›ใพใ™ +- `load_in_4bit` ใฏ[4ใƒ“ใƒƒใƒˆใฎๅ‹•็š„้‡ๅญๅŒ–](main_classes/quantization)ใ‚’้ฉ็”จใ—ใฆใƒชใ‚ฝใƒผใ‚น่ฆไปถใ‚’ๅคงๅน…ใซๅ‰Šๆธ›ใ—ใพใ™ + +ใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹ไป–ใฎๆ–นๆณ•ใ‚‚ใ‚ใ‚Šใพใ™ใŒใ€ใ“ใ‚ŒใฏLLMใ‚’ๅง‹ใ‚ใ‚‹ใŸใ‚ใฎ่‰ฏใ„ๅŸบๆบ–ใงใ™ใ€‚ + +ๆฌกใซใ€[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](tokenizer_summary)ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b") +>>> model_inputs = tokenizer(["A list of colors: red, blue"], return_tensors="pt").to(model.device) +``` + + +`model_inputs` ๅค‰ๆ•ฐใฏใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใจใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใ‚นใ‚ฏใ‚’ไฟๆŒใ—ใฆใ„ใพใ™ใ€‚ [`~generation.GenerationMixin.generate`] ใฏใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใ‚นใ‚ฏใŒๆธกใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใงใ‚‚ใ€ๆœ€ๅ–„ใฎๅŠชๅŠ›ใ‚’ใ—ใฆใใ‚Œใ‚’ๆŽจๆธฌใ—ใ‚ˆใ†ใจใ—ใพใ™ใŒใ€ใงใใ‚‹้™ใ‚Šๆธกใ™ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ๆœ€้ฉใช็ตๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ๆœ€ๅพŒใซใ€[`~generation.GenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ—ใฆ็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ๅ–ๅพ—ใ—ใ€ใใ‚Œใ‚’่กจ็คบใ™ใ‚‹ๅ‰ใซใƒ†ใ‚ญใ‚นใƒˆใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A list of colors: red, blue, green, yellow, black, white, and brown' +``` + +ใ“ใ‚ŒใงๅฎŒไบ†ใงใ™๏ผใ‚ใšใ‹ใชใ‚ณใƒผใƒ‰่กŒๆ•ฐใงใ€LLM๏ผˆLarge Language Model๏ผ‰ใฎใƒ‘ใƒฏใƒผใ‚’ๆดป็”จใงใใพใ™ใ€‚ + +## Common pitfalls + +[็”Ÿๆˆๆˆฆ็•ฅ](generation_strategies)ใฏใŸใใ•ใ‚“ใ‚ใ‚Šใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ€คใŒใ‚ใชใŸใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉใ—ใฆใ„ใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ๅ‡บๅŠ›ใŒๆœŸๅพ…้€šใ‚Šใงใชใ„ๅ ดๅˆใ€ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใช่ฝใจใ—็ฉดใจใใฎๅ›ž้ฟๆ–นๆณ•ใฎใƒชใ‚นใƒˆใ‚’ไฝœๆˆใ—ใพใ—ใŸใ€‚ + +```py +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b") +>>> tokenizer.pad_token = tokenizer.eos_token # Llama has no pad token by default +>>> model = AutoModelForCausalLM.from_pretrained( +... "openlm-research/open_llama_7b", device_map="auto", load_in_4bit=True +... ) +``` + +### Generated output is too short/long + +[`~generation.GenerationConfig`] ใƒ•ใ‚กใ‚คใƒซใงๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€`generate` ใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงๆœ€ๅคงใง 20 ใƒˆใƒผใ‚ฏใƒณใพใง่ฟ”ใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฏ `generate` ใ‚ณใƒผใƒซใง `max_new_tokens` ใ‚’ๆ‰‹ๅ‹•ใง่จญๅฎšใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่ฟ”ใ•ใ‚Œใ‚‹ๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใฎๆœ€ๅคงๆ•ฐใ‚’ๅˆถๅพกใงใใพใ™ใ€‚LLM๏ผˆๆญฃ็ขบใซใฏใ€[ใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จใƒขใƒ‡ใƒซ](https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt)๏ผ‰ใ‚‚ๅ‡บๅŠ›ใฎไธ€้ƒจใจใ—ใฆๅ…ฅๅŠ›ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’่ฟ”ใ™ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> model_inputs = tokenizer(["A sequence of numbers: 1, 2"], return_tensors="pt").to(model.device) + +>>> # By default, the output will contain up to 20 tokens +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A sequence of numbers: 1, 2, 3, 4, 5' + +>>> # Setting `max_new_tokens` allows you to control the maximum length +>>> generated_ids = model.generate(**model_inputs, max_new_tokens=50) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A sequence of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,' +``` + +### Incorrect generation mode + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ [`~generation.GenerationConfig`] ใƒ•ใ‚กใ‚คใƒซใงๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„้™ใ‚Šใ€`generate` ใฏๅ„ใ‚คใƒ†ใƒฌใƒผใ‚ทใƒงใƒณใงๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’้ธๆŠžใ—ใพใ™๏ผˆ่ฒชๆฌฒใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ๏ผ‰ใ€‚ใ‚ฟใ‚นใ‚ฏใซๅฟœใ˜ใฆใ€ใ“ใ‚Œใฏๆœ›ใพใ—ใใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆใ‚„ใ‚จใƒƒใ‚ปใ‚คใฎใ‚ˆใ†ใชๅ‰ต้€ ็š„ใชใ‚ฟใ‚นใ‚ฏใงใฏใ€ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใŒๆœ‰็›Šใงใ™ใ€‚ไธ€ๆ–นใ€้Ÿณๅฃฐใฎ่ปขๅ†™ใ‚„็ฟป่จณใฎใ‚ˆใ†ใชๅ…ฅๅŠ›ใซๅŸบใฅใใ‚ฟใ‚นใ‚ฏใงใฏใ€่ฒชๆฌฒใƒ‡ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใŒๆœ‰็›Šใงใ™ใ€‚`do_sample=True` ใงใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใงใใพใ™ใ€‚ใ“ใฎใƒˆใƒ”ใƒƒใ‚ฏใซใคใ„ใฆใฎ่ฉณ็ดฐใฏใ€ใ“ใฎ[ใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://huggingface.co/blog/how-to-generate)ใงๅญฆใถใ“ใจใŒใงใใพใ™ใ€‚ + +```py +>>> # Set seed or reproducibility -- you don't need this unless you want full reproducibility +>>> from transformers import set_seed +>>> set_seed(0) + +>>> model_inputs = tokenizer(["I am a cat."], return_tensors="pt").to(model.device) + +>>> # LLM + greedy decoding = repetitive, boring output +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'I am a cat. I am a cat. I am a cat. I am a cat' + +>>> # With sampling, the output becomes more creative! +>>> generated_ids = model.generate(**model_inputs, do_sample=True) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'I am a cat.\nI just need to be. I am always.\nEvery time' +``` + +### Wrong padding side + +LLM๏ผˆLarge Language Models๏ผ‰ใฏ[ใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จ](https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt)ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใ‚ใ‚Šใ€ๅ…ฅๅŠ›ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’็นฐใ‚Š่ฟ”ใ—ๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ๅ…ฅๅŠ›ใŒๅŒใ˜้•ทใ•ใงใชใ„ๅ ดๅˆใ€ใใ‚Œใ‚‰ใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚LLMใฏใƒ‘ใƒƒใƒ‰ใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰ใฎ็ถšใใ‚’ๅญฆ็ฟ’ใ—ใฆใ„ใชใ„ใŸใ‚ใ€ๅ…ฅๅŠ›ใฏๅทฆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใพใŸใ€็”Ÿๆˆใซๅฏพใ—ใฆๆณจ็›ฎใƒžใ‚นใ‚ฏใ‚’ๆธกใ—ๅฟ˜ใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„๏ผ + + +```py +>>> # The tokenizer initialized above has right-padding active by default: the 1st sequence, +>>> # which is shorter, has padding on the right side. Generation fails. +>>> model_inputs = tokenizer( +... ["1, 2, 3", "A, B, C, D, E"], padding=True, return_tensors="pt" +... ).to(model.device) +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids[0], skip_special_tokens=True)[0] +'' + +>>> # With left-padding, it works as expected! +>>> tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b", padding_side="left") +>>> tokenizer.pad_token = tokenizer.eos_token # Llama has no pad token by default +>>> model_inputs = tokenizer( +... ["1, 2, 3", "A, B, C, D, E"], padding=True, return_tensors="pt" +... ).to(model.device) +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'1, 2, 3, 4, 5, 6,' +``` + +## Further resources + +ใ‚ชใƒผใƒˆใƒชใ‚ฐใƒฌใƒƒใ‚ทใƒ–็”Ÿๆˆใƒ—ใƒญใ‚ปใ‚นใฏๆฏ”่ผƒ็š„็ฐกๅ˜ใงใ™ใŒใ€LLMใ‚’ๆœ€ๅคง้™ใซๆดป็”จใ™ใ‚‹ใ“ใจใฏๅคšใใฎ่ฆ็ด ใŒ็ตกใ‚€ใŸใ‚ใ€ๆŒ‘ๆˆฆ็š„ใช่ฉฆใฟใจใชใ‚Šใพใ™ใ€‚LLMใฎไฝฟ็”จใจ็†่งฃใ‚’ใ•ใ‚‰ใซๆทฑใ‚ใ‚‹ใŸใ‚ใฎๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใซใคใ„ใฆใฏไปฅไธ‹ใฎใƒชใ‚ฝใƒผใ‚นใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +### Advanced generate usage + +1. [ใ‚ฌใ‚คใƒ‰](generation_strategies)๏ผš็•ฐใชใ‚‹็”Ÿๆˆๆ–นๆณ•ใ‚’ๅˆถๅพกใ™ใ‚‹ๆ–นๆณ•ใ€็”Ÿๆˆๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใฎ่จญๅฎšๆ–นๆณ•ใ€ๅ‡บๅŠ›ใฎใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐๆ–นๆณ•ใซใคใ„ใฆใฎใ‚ฌใ‚คใƒ‰; +2. [`~generation.GenerationConfig`]ใ€[`~generation.GenerationMixin.generate`]ใ€ใŠใ‚ˆใณ[็”Ÿๆˆ้–ข้€ฃใ‚ฏใƒฉใ‚น](internal/generation_utils)ใซ้–ขใ™ใ‚‹APIใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใ€‚ + +### LLM leaderboards + +1. [Open LLM ใƒชใƒผใƒ€ใƒผใƒœใƒผใƒ‰](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)๏ผšใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใƒขใƒ‡ใƒซใฎๅ“่ณชใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใŸใƒชใƒผใƒ€ใƒผใƒœใƒผใƒ‰; +2. [Open LLM-Perf ใƒชใƒผใƒ€ใƒผใƒœใƒผใƒ‰](https://huggingface.co/spaces/optimum/llm-perf-leaderboard)๏ผšLLMใฎใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใŸใƒชใƒผใƒ€ใƒผใƒœใƒผใƒ‰ใ€‚ + +### Latency and throughput + +1. [ใ‚ฌใ‚คใƒ‰](main_classes/quantization)๏ผšใƒ€ใ‚คใƒŠใƒŸใƒƒใ‚ฏใ‚ฏใ‚ชใƒณใ‚ฟใ‚คใ‚บใซ้–ขใ™ใ‚‹ใ‚ฌใ‚คใƒ‰ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใƒกใƒขใƒช่ฆไปถใ‚’ๅЇ็š„ใซๅ‰Šๆธ›ใ™ใ‚‹ๆ–นๆณ•ใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +### Related libraries + +1. [`text-generation-inference`](https://github.com/huggingface/text-generation-inference)๏ผšLLM็”จใฎๆœฌ็•ชๅ‘ใ‘ใ‚ตใƒผใƒใƒผ; +2. [`optimum`](https://github.com/huggingface/optimum)๏ผš็‰นๅฎšใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใƒ‡ใƒใ‚คใ‚นๅ‘ใ‘ใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸ๐Ÿค— Transformersใฎๆ‹กๅผตใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/callback.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/callback.md new file mode 100644 index 0000000000000000000000000000000000000000..f1cb357f7eb8f49f27b8285ec0a9a53734c3f43a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/callback.md @@ -0,0 +1,139 @@ + + + +# ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏๆ•ฐ + +ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใฏใ€PyTorch ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใฎๅ‹•ไฝœใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใงใใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใ‚’ๆคœๆŸปใงใใ‚‹ [`Trainer`] (ใ“ใฎๆฉŸ่ƒฝใฏ TensorFlow ใซใฏใพใ ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ›ใ‚“) +็Šถๆ…‹ใ‚’็ขบ่ชใ— (้€ฒๆ—ใƒฌใƒใƒผใƒˆใ€TensorBoard ใพใŸใฏไป–ใฎ ML ใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ใธใฎใƒญใ‚ฐ่จ˜้Œฒใชใฉ)ใ€ๆฑบๅฎšใ‚’ไธ‹ใ—ใพใ™ (ๅˆๆœŸๆฎต้šŽใชใฉ)ใ€‚ +ๅœๆญขไธญ๏ผ‰ใ€‚ + +ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใฏใ€่ฟ”ใ•ใ‚Œใ‚‹ [`TrainerControl`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’้™คใ‘ใฐใ€ใ€Œ่ชญใฟๅ–ใ‚Šๅฐ‚็”จใ€ใฎใ‚ณใƒผใƒ‰้ƒจๅˆ†ใงใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ๅ†…ใงใฏไฝ•ใ‚‚ๅค‰ๆ›ดใงใใพใ›ใ‚“ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใฎๅค‰ๆ›ดใŒๅฟ…่ฆใชใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใฎๅ ดๅˆใฏใ€ๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[`Trainer`] ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ—ใ€ๅฟ…่ฆใชใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ—ใพใ™ (ไพ‹ใซใคใ„ใฆใฏใ€[trainer](trainer) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚ + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€`TrainingArguments.report_to` ใฏ `"all"` ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€[`Trainer`] ใฏๆฌกใฎใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +- [`DefaultFlowCallback`] ใฏใ€ใƒญใ‚ฐ่จ˜้Œฒใ€ไฟๅญ˜ใ€่ฉ•ไพกใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ‹•ไฝœใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ +- [`PrinterCallback`] ใพใŸใฏ [`ProgressCallback`] ใง้€ฒ่กŒ็Šถๆณใ‚’่กจ็คบใ—ใ€ + ใƒญใ‚ฐ (ๆœ€ๅˆใฎใƒญใ‚ฐใฏใ€[`TrainingArguments`] ใ‚’้€šใ˜ใฆ tqdm ใ‚’้žใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใ™ใ‚‹ๅ ดๅˆใซไฝฟ็”จใ•ใ‚Œใ€ใใ†ใงใชใ„ๅ ดๅˆใซไฝฟ็”จใ•ใ‚Œใพใ™) + 2็•ช็›ฎใงใ™)ใ€‚ +- [`~integrations.TensorBoardCallback`] (PyTorch >= 1.4 ใ‚’ไป‹ใ—ใฆ) tensorboard ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ๅ ดๅˆ + ใพใŸใฏใƒ†ใƒณใ‚ฝใƒซใƒœใƒผใƒ‰X๏ผ‰ใ€‚ +- [`~integrations.WandbCallback`] [wandb](https://www.wandb.com/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.CometCallback`] [comet_ml](https://www.comet.com/site/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [mlflow](https://www.mlflow.org/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใฏ [`~integrations.MLflowCallback`]ใ€‚ +- [`~integrations.NeptuneCallback`] [neptune](https://neptune.ai/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.AzureMLCallback`] [azureml-sdk](https://pypi.org/project/azureml-sdk/) ใฎๅ ดๅˆ + ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- [`~integrations.CodeCarbonCallback`] [codecarbon](https://pypi.org/project/codecarbon/) ใฎๅ ดๅˆ + ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- [`~integrations.ClearMLCallback`] [clearml](https://github.com/allegroai/clearml) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.DagsHubCallback`] [dagshub](https://dagshub.com/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.FlyteCallback`] [flyte](https://flyte.org/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.DVCLiveCallback`] [dvclive](https://www.dvc.org/doc/dvclive) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ +- [`~integrations.SwanLabCallback`] [swanlab](http://swanlab.cn/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€‚ + +ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใŒใ€ไป˜้šใ™ใ‚‹็ตฑๅˆใ‚’ไฝฟ็”จใ—ใŸใใชใ„ๅ ดๅˆใฏใ€`TrainingArguments.report_to` ใ‚’ใ€ไฝฟ็”จใ—ใŸใ„็ตฑๅˆใฎใฟใฎใƒชใ‚นใƒˆใซๅค‰ๆ›ดใงใใพใ™ (ไพ‹: `["azure_ml", "wandb"]`) ใ€‚ + +ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๅฎŸ่ฃ…ใ™ใ‚‹ใƒกใ‚คใƒณใ‚ฏใƒฉใ‚นใฏ [`TrainerCallback`] ใงใ™ใ€‚ใใ‚Œใฏใ€ +[`TrainingArguments`] ใฏ [`Trainer`] ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ€ใใ‚Œใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ +[`TrainerState`] ใ‚’ไป‹ใ—ใฆใƒˆใƒฌใƒผใƒŠใƒผใฎๅ†…้ƒจ็Šถๆ…‹ใ‚’ๅ–ๅพ—ใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ไธŠใงใ„ใใคใ‹ใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ +[`TrainerControl`]ใ€‚ + +## ๅˆฉ็”จๅฏ่ƒฝใชใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ + +ใƒฉใ‚คใƒ–ใƒฉใƒชใงๅˆฉ็”จๅฏ่ƒฝใช [`TrainerCallback`] ใฎใƒชใ‚นใƒˆใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +[[autodoc]] integrations.CometCallback + - setup + +[[autodoc]] DefaultFlowCallback + +[[autodoc]] PrinterCallback + +[[autodoc]] ProgressCallback + +[[autodoc]] EarlyStoppingCallback + +[[autodoc]] integrations.TensorBoardCallback + +[[autodoc]] integrations.WandbCallback + - setup + +[[autodoc]] integrations.MLflowCallback + - setup + +[[autodoc]] integrations.AzureMLCallback + +[[autodoc]] integrations.CodeCarbonCallback + +[[autodoc]] integrations.NeptuneCallback + +[[autodoc]] integrations.ClearMLCallback + +[[autodoc]] integrations.DagsHubCallback + +[[autodoc]] integrations.FlyteCallback + +[[autodoc]] integrations.DVCLiveCallback + - setup + +[[autodoc]] integrations.SwanLabCallback + - setup + +## TrainerCallback + +[[autodoc]] TrainerCallback + +ไปฅไธ‹ใฏใ€ใ‚ซใ‚นใ‚ฟใƒ  ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ PyTorch [`Trainer`] ใซ็™ป้Œฒใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใงใ™ใ€‚ + +```python +class MyCallback(TrainerCallback): + "A callback that prints a message at the beginning of training" + + def on_train_begin(self, args, state, control, **kwargs): + print("Starting training") + + +trainer = Trainer( + model, + args, + train_dataset=train_dataset, + eval_dataset=eval_dataset, + callbacks=[MyCallback], # We can either pass the callback class this way or an instance of it (MyCallback()) +) +``` + +ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’็™ป้Œฒใ™ใ‚‹ๅˆฅใฎๆ–นๆณ•ใฏใ€ๆฌกใฎใ‚ˆใ†ใซ `trainer.add_callback()` ใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใงใ™ใ€‚ + +```python +trainer = Trainer(...) +trainer.add_callback(MyCallback) +# Alternatively, we can pass an instance of the callback class +trainer.add_callback(MyCallback()) +``` + +## TrainerState + +[[autodoc]] TrainerState + +## TrainerControl + +[[autodoc]] TrainerControl + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/configuration.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/configuration.md new file mode 100644 index 0000000000000000000000000000000000000000..7fab5269e2049f61c8c83859a19b218d247cebb5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/configuration.md @@ -0,0 +1,31 @@ + + +๏ผƒ ๆง‹ๆˆ + +ๅŸบๆœฌใ‚ฏใƒฉใ‚น [`PretrainedConfig`] ใฏใ€่จญๅฎšใ‚’ใƒญใƒผใƒ‰/ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใฎไธ€่ˆฌ็š„ใชใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ +ใƒญใƒผใ‚ซใƒซ ใƒ•ใ‚กใ‚คใƒซใพใŸใฏใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‹ใ‚‰ใ€ใพใŸใฏใƒฉใ‚คใƒ–ใƒฉใƒช (ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚ŒใŸ) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซๆง‹ๆˆใ‹ใ‚‰ +HuggingFace ใฎ AWS S3 ใƒชใƒใ‚ธใƒˆใƒชใ‹ใ‚‰)ใ€‚ + +ๅ„ๆดพ็”Ÿๆง‹ๆˆใ‚ฏใƒฉใ‚นใฏใƒขใƒ‡ใƒซๅ›บๆœ‰ใฎๅฑžๆ€งใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ใ™ในใฆใฎๆง‹ๆˆใ‚ฏใƒฉใ‚นใซๅญ˜ๅœจใ™ใ‚‹ๅ…ฑ้€šใฎๅฑžๆ€งใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +`hidden_โ€‹โ€‹size`ใ€`num_attention_heads`ใ€ใŠใ‚ˆใณ `num_hidden_โ€‹โ€‹layers`ใ€‚ใƒ†ใ‚ญใ‚นใƒˆ ใƒขใƒ‡ใƒซใฏใ•ใ‚‰ใซไปฅไธ‹ใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ +`vocab_size`ใ€‚ + +## PretrainedConfig + +[[autodoc]] PretrainedConfig + - push_to_hub + - all diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/data_collator.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/data_collator.md new file mode 100644 index 0000000000000000000000000000000000000000..c37f1aeef4d1ccb17446da900b39904b92d1d68b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/data_collator.md @@ -0,0 +1,67 @@ + + +# ใƒ‡ใƒผใ‚ฟ็…งๅˆ่€… + +ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ่ฆ็ด ใฎใƒชใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใ—ใฆใƒใƒƒใƒใ‚’ๅฝขๆˆใ™ใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎ่ฆ็ด ใฏใ€ +`train_dataset` ใพใŸใฏ `eval_dataset` ใฎ่ฆ็ด ใจๅŒใ˜ๅž‹ใ€‚ + +ใƒใƒƒใƒใ‚’ๆง‹็ฏ‰ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆ่€…ใฏไฝ•ใ‚‰ใ‹ใฎๅ‡ฆ็† (ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใชใฉ) ใ‚’้ฉ็”จใ™ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใใฎใ†ใกใฎใ„ใใคใ‹ใฏ๏ผˆ +[`DataCollatโ€‹โ€‹orForLanguageModeling`]) ใƒฉใƒณใƒ€ใƒ ใชใƒ‡ใƒผใ‚ฟๆ‹กๅผต (ใƒฉใƒณใƒ€ใƒ  ใƒžใ‚นใ‚ญใƒณใ‚ฐใชใฉ) ใ‚‚้ฉ็”จใ—ใพใ™ +ๅฝขๆˆใ•ใ‚ŒใŸใƒใƒƒใƒไธŠใงใ€‚ + +ไฝฟ็”จไพ‹ใฏใ€[ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](../examples) ใพใŸใฏ [ใ‚ตใƒณใƒ—ใƒซ ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](../notebooks) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Default data collator + +[[autodoc]] data.data_collator.default_data_collator + +## DefaultDataCollator + +[[autodoc]] data.data_collator.DefaultDataCollator + +## DataCollatorWithPadding + +[[autodoc]] data.data_collator.DataCollatorWithPadding + +## DataCollatorForTokenClassification + +[[autodoc]] data.data_collator.DataCollatorForTokenClassification + +## DataCollatorForSeq2Seq + +[[autodoc]] data.data_collator.DataCollatorForSeq2Seq + +## DataCollatorForLanguageModeling + +[[autodoc]] data.data_collator.DataCollatorForLanguageModeling + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens + +## DataCollatorForWholeWordMask + +[[autodoc]] data.data_collator.DataCollatorForWholeWordMask + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens + +## DataCollatorForPermutationLanguageModeling + +[[autodoc]] data.data_collator.DataCollatorForPermutationLanguageModeling + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/deepspeed.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/deepspeed.md new file mode 100644 index 0000000000000000000000000000000000000000..b0bc6610876baef0d40251e6b34a06ad773e29d4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/deepspeed.md @@ -0,0 +1,2254 @@ + + +# DeepSpeed Integration + +[DeepSpeed](https://github.com/deepspeedai/DeepSpeed) ใฏใ€[ZeRO ่ซ–ๆ–‡](https://huggingface.co/papers/1910.02054) ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ™ในใฆใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚็พๅœจใ€ๆฌกใฎใ‚‚ใฎใ‚’ๅฎŒๅ…จใซใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ + +1. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 1) +2. ๅ‹พ้…ๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2) +3. ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๅˆ†ๅ‰ฒ (ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3) +4. ใ‚ซใ‚นใ‚ฟใƒ ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅ‡ฆ็† +5. ไธ€้€ฃใฎ้ซ˜้€Ÿ CUDA ๆ‹กๅผตใƒ™ใƒผใ‚นใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ +6. CPU ใŠใ‚ˆใณ NVMe ใธใฎ ZeRO ใ‚ชใƒ•ใƒญใƒผใƒ‰ + +ZeRO-Offload ใซใฏ็‹ฌ่‡ชใฎๅฐ‚็”จใƒšใƒผใƒ‘ใƒผใŒใ‚ใ‚Šใพใ™: [ZeRO-Offload: Democratizing Billion-Scale Model Training](https://huggingface.co/papers/2101.06840)ใ€‚ NVMe ใ‚ตใƒใƒผใƒˆใซใคใ„ใฆใฏใ€่ซ–ๆ–‡ [ZeRO-Infinity: Breaking the GPU Memory Wall for Extreme Scale Deep Learning](https://huggingface.co/papers/2104.07857)ใ€‚ + +DeepSpeed ZeRO-2 ใฏใ€ใใฎๆฉŸ่ƒฝใŒๆŽจ่ซ–ใซใฏๅฝนใซ็ซ‹ใŸใชใ„ใŸใ‚ใ€ไธปใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใฟใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +DeepSpeed ZeRO-3 ใฏใ€ๅทจๅคงใชใƒขใƒ‡ใƒซใ‚’่ค‡ๆ•ฐใฎ GPU ใซใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ +ๅ˜ไธ€ใฎ GPU ใงใฏไธๅฏ่ƒฝใงใ™ใ€‚ + +๐Ÿค— Transformers ใฏใ€2 ใคใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ไป‹ใ—ใฆ [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) ใ‚’็ตฑๅˆใ—ใพใ™ใ€‚ + +1. [`Trainer`] ใซใ‚ˆใ‚‹ใ‚ณใ‚ข DeepSpeed ๆฉŸ่ƒฝใฎ็ตฑๅˆใ€‚ไฝ•ใงใ‚‚ใ‚„ใฃใฆใใ‚Œใ‚‹ใ‚ฟใ‚คใƒ—ใงใ™ + ็ตฑๅˆใฎๅ ดๅˆ - ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใ ใ‘ใงใ€ไป–ใซไฝ•ใ‚‚ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใŸใ„ใฆใ„ใฎ + ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ“ใฎๆฉŸ่ƒฝใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ +2. [`Trainer`] ใ‚’ไฝฟ็”จใ›ใšใ€DeepSpeed ใ‚’็ตฑๅˆใ—ใŸ็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒŠใƒผใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆ + `from_pretrained` ใ‚„ `from_config` ใชใฉใฎใ‚ณใ‚ขๆฉŸ่ƒฝใซใฏใ€้‡่ฆใชๆฉŸ่ƒฝใฎ็ตฑๅˆใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ไปฅ้™ใฎ `zero.Init`ใชใฉใฎ DeepSpeed ใฎ้ƒจๅˆ†ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ๆดป็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ใŠ่ชญใฟใใ ใ•ใ„ใ€‚ + [้žใƒˆใƒฌใƒผใƒŠใƒผ DeepSpeed ็ตฑๅˆ](#nontrainer-deepspeed-integration)ใ€‚ + +็ตฑๅˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚‚ใฎ: + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผš + +1. DeepSpeed ZeRO ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ZeRO-Infinity (CPU ใŠใ‚ˆใณ NVME ใ‚ชใƒ•ใƒญใƒผใƒ‰) ใ‚’ไฝฟ็”จใ—ใฆๅฎŒๅ…จใช ZeRO ใ‚นใƒ†ใƒผใ‚ธ 1ใ€2ใ€ใŠใ‚ˆใณ 3 ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +ๆŽจ่ซ–๏ผš + +1. DeepSpeed ZeRO Inference ใฏใ€ZeRO-Infinity ใซใ‚ˆใ‚‹ ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๅŒใ˜ ZeRO ใƒ—ใƒญใƒˆใ‚ณใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ + ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใจ lr ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใฏไฝฟ็”จใ›ใšใ€ใ‚นใƒ†ใƒผใ‚ธ 3 ใฎใฟใŒ้–ข้€ฃใ—ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + [ใ‚ผใƒญๆŽจ่ซ–](#zero-inference)ใ€‚ + +DeepSpeed Inference ใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€Tensor Parallelism ใฎไปฃใ‚ใ‚Šใซ Tensor Parallelism ใ‚’ไฝฟ็”จใ™ใ‚‹ใพใฃใŸใ็•ฐใชใ‚‹ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใงใ™ใ€‚ +ZeRO (่ฟ‘ๆ—ฅๅ…ฌ้–‹)ใ€‚ + + + + +## Trainer Deepspeed Integration + + + + +### Installation + +pypi ็ตŒ็”ฑใงใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ +```bash +pip install deepspeed +``` + +ใพใŸใฏ`tansformers`, `extras`็ตŒ็”ฑ: + +```bash +pip install transformers[deepspeed] +``` + +ใพใŸใฏใ€[DeepSpeed ใฎ GitHub ใƒšใƒผใ‚ธ](https://github.com/deepspeedai/DeepSpeed#installation) ใง่ฉณ็ดฐใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +[้ซ˜ๅบฆใชใ‚คใƒณใ‚นใƒˆใƒผใƒซ](https://www.deepspeed.ai/tutorials/advanced-install/)ใ€‚ + +ใใ‚Œใงใ‚‚ใƒ“ใƒซใƒ‰ใซ่‹ฆๅŠดใ™ใ‚‹ๅ ดๅˆใฏใ€ใพใš [CUDA ๆ‹กๅผตๆฉŸ่ƒฝใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ ใƒŽใƒผใƒˆ](trainer#cuda-extension-installation-notes) ใ‚’ๅฟ…ใš่ชญใ‚“ใงใใ ใ•ใ„ใ€‚ + +ๆ‹กๅผตๆฉŸ่ƒฝใ‚’ไบ‹ๅ‰ใƒ“ใƒซใƒ‰ใ›ใšใ€ๅฎŸ่กŒๆ™‚ใซๆ‹กๅผตๆฉŸ่ƒฝใŒใƒ“ใƒซใƒ‰ใ•ใ‚Œใ‚‹ใ“ใจใซไพๅญ˜ใ—ใฆใŠใ‚Šใ€ไธŠ่จ˜ใฎ่งฃๆฑบ็ญ–ใ‚’ใ™ในใฆ่ฉฆใ—ใŸๅ ดๅˆ +ใใ‚ŒใŒๅฝนใซ็ซ‹ใŸใชใ‹ใฃใŸๅ ดๅˆใ€ๆฌกใซ่ฉฆใ™ในใใ“ใจใฏใ€ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅ‰ใซใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไบ‹ๅ‰ใซใƒ“ใƒซใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +DeepSpeed ใฎใƒญใƒผใ‚ซใƒซ ใƒ“ใƒซใƒ‰ใ‚’ไฝœๆˆใ™ใ‚‹ใซใฏ: + +```bash +git clone https://github.com/deepspeedai/DeepSpeed/ +cd DeepSpeed +rm -rf build +TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 pip install . \ +--global-option="build_ext" --global-option="-j8" --no-cache -v \ +--disable-pip-version-check 2>&1 | tee build.log +``` + +NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ไธŠ่จ˜ใฎๆ‰‹้ †ใซ`DS_BUILD_AIO=1`ใ‚’ๅซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ (ใพใŸใ€ +*libaio-dev* ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™)ใ€‚ + +`TORCH_CUDA_ARCH_LIST` ใ‚’็ทจ้›†ใ—ใฆใ€ไฝฟ็”จใ™ใ‚‹ GPU ใ‚ซใƒผใƒ‰ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ณใƒผใƒ‰ใ‚’ๆŒฟๅ…ฅใ—ใพใ™ใ€‚ใ™ในใฆใ‚’ไปฎๅฎšใ™ใ‚‹ใจ +ใ‚ใชใŸใฎใ‚ซใƒผใƒ‰ใฏๅŒใ˜ใงใ€ๆฌกใฎๆ–นๆณ•ใงใ‚ขใƒผใƒใ‚’ๅ–ๅพ—ใงใใพใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES=0 python -c "import torch; print(torch.cuda.get_device_capability())" +``` + +ใ—ใŸใŒใฃใฆใ€`8, 6`ใ‚’ๅ–ๅพ—ใ—ใŸๅ ดๅˆใฏใ€`TORCH_CUDA_ARCH_LIST="8.6"`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚่ค‡ๆ•ฐใฎ็•ฐใชใ‚‹ใ‚ซใƒผใƒ‰ใ‚’ใŠๆŒใกใฎๅ ดๅˆใฏใ€ใ™ในใฆใ‚’ใƒชใ‚นใƒˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™ +ใใ‚Œใ‚‰ใฎใ†ใกใ€`TORCH_CUDA_ARCH_LIST="6.1;8.6"`ใŒๅฅฝใใงใ™ + +่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณใงๅŒใ˜ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใƒใ‚คใƒŠใƒช ใƒ›ใ‚คใƒผใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```bash +git clone https://github.com/deepspeedai/DeepSpeed/ +cd DeepSpeed +rm -rf build +TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 \ +python setup.py build_ext -j8 bdist_wheel +``` + +`dist/deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl`ใฎใ‚ˆใ†ใชใ‚‚ใฎใŒ็”Ÿๆˆใ•ใ‚Œใ‚‹ใฎใงใ€ใ“ใ‚Œใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™ +`pip install deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl`ใจใ—ใฆใƒญใƒผใ‚ซใƒซใพใŸใฏไป–ใฎใƒžใ‚ทใƒณใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ + +็นฐใ‚Š่ฟ”ใ—ใพใ™ใŒใ€`TORCH_CUDA_ARCH_LIST`ใ‚’ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ + +NVIDIA GPU ใฎๅฎŒๅ…จใชใƒชใ‚นใƒˆใจใ€ใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ **ใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐๆฉŸ่ƒฝ** (ใ“ใฎ่จ˜ไบ‹ใฎ Arch ใจๅŒใ˜) ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ) [ใ“ใ“](https://developer.nvidia.com/cuda-gpus)ใ€‚ + +ไปฅไธ‹ใ‚’ไฝฟ็”จใ—ใฆใ€pytorch ใŒๆง‹็ฏ‰ใ•ใ‚ŒใŸใ‚ขใƒผใƒใ‚’็ขบ่ชใงใใพใ™ใ€‚ + +```bash +python -c "import torch; print(torch.cuda.get_arch_list())" +``` + +ใ“ใ“ใงใฏใ€ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ GPU ใฎ 1 ใคใฎใ‚ขใƒผใƒใ‚’่ฆ‹ใคใ‘ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€GPU 0 ใฎๅ ดๅˆ: + +```bash +CUDA_VISIBLE_DEVICES=0 python -c "import torch; \ +print(torch.cuda.get_device_properties(torch.device('cuda')))" +``` + +ๅ‡บๅŠ›ใŒๆฌกใฎๅ ดๅˆ: + +```bash +_CudaDeviceProperties(name='GeForce RTX 3090', major=8, minor=6, total_memory=24268MB, multi_processor_count=82) +``` + +ใใ†ใ™ใ‚Œใฐใ€ใ“ใฎใ‚ซใƒผใƒ‰ใฎใ‚ขใƒผใƒใŒ`8.6`ใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +`TORCH_CUDA_ARCH_LIST` ใ‚’ๅฎŒๅ…จใซ็œ็•ฅใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใใ†ใ™ใ‚Œใฐใ€ใƒ“ใƒซใƒ‰ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใŒ่‡ชๅ‹•็š„ใซใ‚ฏใ‚จใƒชใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +ใƒ“ใƒซใƒ‰ใŒ่กŒใ‚ใ‚Œใ‚‹ GPU ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ใ“ใ‚Œใฏใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใƒžใ‚ทใƒณใฎ GPU ใจไธ€่‡ดใ™ใ‚‹ๅ ดๅˆใ‚‚ใ‚ใ‚Œใฐใ€ไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ +็›ฎ็š„ใฎใ‚ขใƒผใƒใ‚’ๆ˜Ž็คบ็š„ใซๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +ๆๆกˆใ•ใ‚ŒใŸใ“ใจใ‚’ใ™ในใฆ่ฉฆใ—ใฆใ‚‚ใพใ ใƒ“ใƒซใƒ‰ใฎๅ•้กŒใŒ็™บ็”Ÿใ™ใ‚‹ๅ ดๅˆใฏใ€GitHub ใฎๅ•้กŒใซ้€ฒใ‚“ใงใใ ใ•ใ„ใ€‚ +[ใƒ‡ใ‚ฃใƒผใƒ—ใ‚นใƒ”ใƒผใƒ‰](https://github.com/deepspeedai/DeepSpeed/issues)ใ€ + + + +### Deployment with multiple GPUs + +DeepSpeed ็ตฑๅˆใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ใซใฏใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’่ชฟๆ•ดใ—ใฆๆ–ฐใ—ใ„ๅผ•ๆ•ฐ `--deepspeed ds_config.json` ใ‚’ๅซใ‚ใพใ™ใ€‚ใ“ใ“ใงใ€`ds_config.json` ใฏ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ + [ใ“ใกใ‚‰](https://www.deepspeed.ai/docs/config-json/)ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒ•ใ‚กใ‚คใƒซๅใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ + DeepSpeed ใฎ`add_config_arguments`ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’ไฝฟ็”จใ—ใฆใ€ๅฟ…่ฆใชใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’ใ‚ณใƒผใƒ‰ใซ่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[DeepSpeed ใฎๅผ•ๆ•ฐ่งฃๆž](https://deepspeed.readthedocs.io/en/latest/initialize.html#argument-parsing) ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใ“ใง้ธๆŠžใ—ใŸใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ pytorch ใƒฉใƒณใƒใƒฃใƒผใ‚’ๅผ•ใ็ถšใไฝฟ็”จใงใใพใ™ใ€‚ + +```bash +torch.distributed.run --nproc_per_node=2 your_program.py --deepspeed ds_config.json +``` + +ใพใŸใฏใ€`deepspeed`ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```bash +deepspeed --num_gpus=2 your_program.py --deepspeed ds_config.json +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€ๅผ•ๆ•ฐใฏๅŒใ˜ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใปใจใ‚“ใฉใฎใƒ‹ใƒผใ‚บใงใฏใฉใกใ‚‰ใงใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚ใฎ +ใ•ใพใ–ใพใชใƒŽใƒผใƒ‰ใจ GPU ใ‚’ๆง‹ๆˆใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใกใ‚‰](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ—ใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€`--num_gpus`ใƒ•ใƒฉใ‚ฐใ‚’็œ็•ฅใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ DeepSpeed ใง`run_translation.py`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไพ‹ใงใ™ใ€‚ + +```bash +deepspeed examples/pytorch/translation/run_translation.py \ +--deepspeed tests/deepspeed/ds_config_zero3.json \ +--model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ +--output_dir output_dir --overwrite_output_dir --fp16 \ +--do_train --max_train_samples 500 --num_train_epochs 1 \ +--dataset_name wmt16 --dataset_config "ro-en" \ +--source_lang en --target_lang ro +``` + +DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซใฏใ€`--deepspeed --deepspeed_config ds_config.json`ใŒ่กจ็คบใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +DeepSpeed ้–ข้€ฃใฎๅผ•ๆ•ฐใŒ 2 ใคใ‚ใ‚Šใพใ™ใŒใ€็ฐกๅ˜ใซใ™ใ‚‹ใŸใ‚ใงใ‚ใ‚Šใ€ๅ‡ฆ็†ใ™ในใๅผ•ๆ•ฐใŒใ™ใงใซ้žๅธธใซๅคšใ„ใŸใ‚ใงใ™ใ€‚ +ใ“ใฎ 2 ใคใ‚’ 1 ใคใฎๅผ•ๆ•ฐใซ็ตๅˆใ—ใพใ—ใŸใ€‚ + +ๅฎŸ้š›ใฎไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€ใ“ใฎ [ๆŠ•็จฟ](https://github.com/huggingface/transformers/issues/8771#issuecomment-759248400) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + + +### Deployment with one GPU + +1 ใคใฎ GPU ใง DeepSpeed ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ใซใฏใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ‚’ๆฌกใฎใ‚ˆใ†ใซ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```bash +deepspeed --num_gpus=1 examples/pytorch/translation/run_translation.py \ +--deepspeed tests/deepspeed/ds_config_zero2.json \ +--model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ +--output_dir output_dir --overwrite_output_dir --fp16 \ +--do_train --max_train_samples 500 --num_train_epochs 1 \ +--dataset_name wmt16 --dataset_config "ro-en" \ +--source_lang en --target_lang ro +``` + +ใ“ใ‚Œใฏ่ค‡ๆ•ฐใฎ GPU ใฎๅ ดๅˆใจใปใผๅŒใ˜ใงใ™ใŒใ€ใ“ใ“ใงใฏใ€DeepSpeed ใซ 1 ใคใฎ GPU ใ ใ‘ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆ˜Ž็คบ็š„ใซๆŒ‡็คบใ—ใพใ™ใ€‚ +`--num_gpus=1`ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€DeepSpeed ใฏๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒŽใƒผใƒ‰ไธŠใง่ช่ญ˜ใงใใ‚‹ใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ—ใพใ™ใ€‚่ตทๅ‹•ใ™ใ‚‹ GPU ใŒ 1 ใคใ ใ‘ใฎๅ ดๅˆ +ใฎๅ ดๅˆใ€ใ“ใฎๅผ•ๆ•ฐใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆฌกใฎ [ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใงใฏใ€ใƒฉใƒณใƒใƒฃใƒผ ใ‚ชใƒ—ใ‚ทใƒงใƒณใซใคใ„ใฆ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ + +1 ใคใฎ GPU ใ ใ‘ใง DeepSpeed ใ‚’ไฝฟ็”จใ—ใŸใ„ใฎใฏใชใœใงใ™ใ‹? + +1. ไธ€้ƒจใฎ่จˆ็ฎ—ใจใƒกใƒขใƒชใ‚’ใƒ›ใ‚นใƒˆใฎ CPU ใจ RAM ใซๅง”ไปปใงใใ‚‹ ZeRO ใ‚ชใƒ•ใƒญใƒผใƒ‰ๆฉŸ่ƒฝใ‚’ๅ‚™ใˆใฆใ„ใ‚‹ใŸใ‚ใ€ + ใƒขใƒ‡ใƒซใฎใƒ‹ใƒผใ‚บใซๅˆใ‚ใ›ใฆใ‚ˆใ‚Šๅคšใใฎ GPU ใƒชใ‚ฝใƒผใ‚นใ‚’ๆฎ‹ใ—ใฆใŠใใพใ™ใ€‚ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ€ใพใŸใฏ้žๅธธใซๅคงใใชใƒขใƒ‡ใƒซใฎใƒ•ใ‚ฃใƒƒใƒ†ใ‚ฃใƒณใ‚ฐใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ + ๆ™ฎ้€šใฏๅˆใ‚ใชใ„ใงใ—ใ‚‡ใ†ใ€‚ +2. ใ‚นใƒžใƒผใƒˆใช GPU ใƒกใƒขใƒช็ฎก็†ใ‚ทใ‚นใƒ†ใƒ ใ‚’ๆไพ›ใ—ใ€ใƒกใƒขใƒชใฎๆ–ญ็‰‡ๅŒ–ใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใพใ™ใ€‚ + ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใจใƒ‡ใƒผใ‚ฟ ใƒใƒƒใƒใ€‚ + +ๆฌกใซๆง‹ๆˆใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใŒใ€ๅ˜ไธ€ใฎ GPU ใงๅคงๅน…ใชๆ”นๅ–„ใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใฎ้ตใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใซๅฐ‘ใชใใจใ‚‚ๆฌกใฎๆง‹ๆˆใŒๅฟ…่ฆใงใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 2e8, + "reduce_scatter": true, + "reduce_bucket_size": 2e8, + "overlap_comm": true, + "contiguous_gradients": true + } +} +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚„ใใฎไป–ใฎ้‡่ฆใชๆฉŸ่ƒฝใŒๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใƒใƒƒใƒ•ใ‚ก ใ‚ตใ‚คใ‚บใ‚’่ฉฆใ—ใฆใฟใ‚‹ใจใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎใƒ‡ใ‚ฃใ‚นใ‚ซใƒƒใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใ‚ฟใ‚คใƒ—ใฎใƒ‡ใƒ—ใƒญใ‚คใƒกใƒณใƒˆใฎๅฎŸ้š›็š„ใชไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€ใ“ใฎ [ๆŠ•็จฟ](https://github.com/huggingface/transformers/issues/8771#issuecomment-759176685) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใง่ฉณใ—ใ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€CPU ใŠใ‚ˆใณ NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅ‚™ใˆใŸ ZeRO-3 ใ‚’่ฉฆใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +ใƒŽใƒผใƒˆ๏ผš + +- GPU 0 ใจใฏ็•ฐใชใ‚‹็‰นๅฎšใฎ GPU ใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€`CUDA_VISIBLE_DEVICES` ใ‚’ไฝฟ็”จใ—ใฆๅˆถ้™ใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ + ๅˆฉ็”จๅฏ่ƒฝใช GPU ใฎ่กจ็คบ็ฏ„ๅ›ฒใ€‚ไปฃใ‚ใ‚Šใซใ€ๆฌกใฎๆง‹ๆ–‡ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + ```bash + deepspeed --include localhost:1 examples/pytorch/translation/run_translation.py ... + ``` + + ใ“ใฎไพ‹ใงใฏใ€DeepSpeed ใซ GPU 1 (2 ็•ช็›ฎใฎ GPU) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใ—ใพใ™ใ€‚ + + + +### ่ค‡ๆ•ฐใฎใƒŽใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใŸใƒ‡ใƒ—ใƒญใ‚คใƒกใƒณใƒˆ + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆƒ…ๅ ฑใฏ DeepSpeed ็ตฑๅˆใซๅ›บๆœ‰ใฎใ‚‚ใฎใงใฏใชใใ€ใ‚ใ‚‰ใ‚†ใ‚‹ใƒžใƒซใƒใƒŽใƒผใƒ‰ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใซ้ฉ็”จใงใใพใ™ใ€‚ใŸใ ใ—ใ€DeepSpeed ใฏใ€SLURM ็’ฐๅขƒใงใชใ„้™ใ‚Šใ€ไป–ใฎใƒฉใƒณใƒใƒฃใƒผใ‚ˆใ‚Šใ‚‚ไฝฟใ„ใ‚„ใ™ใ„`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใใ‚Œใžใ‚Œ 8 GPU ใ‚’ๅ‚™ใˆใŸ 2 ใคใฎใƒŽใƒผใƒ‰ใŒใ‚ใ‚‹ใจไปฎๅฎšใ—ใพใ™ใ€‚ใพใŸใ€ๆœ€ๅˆใฎใƒŽใƒผใƒ‰ใซใฏ `ssh hostname1` ใ‚’ไฝฟ็”จใ—ใฆใ€2 ็•ช็›ฎใฎใƒŽใƒผใƒ‰ใซใฏ `ssh hostname2` ใ‚’ไฝฟ็”จใ—ใฆๆŽฅ็ถšใงใใพใ™ใ€‚ไธกๆ–นใจใ‚‚ใƒ‘ใ‚นใƒฏใƒผใƒ‰ใชใ—ใงใƒญใƒผใ‚ซใƒซใฎ ssh ็ตŒ็”ฑใง็›ธไบ’ใซๆŽฅ็ถšใงใใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใฎใƒ›ใ‚นใƒˆ (ใƒŽใƒผใƒ‰) ๅใ‚’ใ€ไฝœๆฅญใ—ใฆใ„ใ‚‹ๅฎŸ้š›ใฎใƒ›ใ‚นใƒˆๅใซๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +#### The torch.distributed.run launcher + + +ใŸใจใˆใฐใ€`torch.distributed.run` ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```bash +python -m torch.distributed.run --nproc_per_node=8 --nnode=2 --node_rank=0 --master_addr=hostname1 \ +--master_port=9901 your_program.py --deepspeed ds_config.json +``` + +ๅ„ใƒŽใƒผใƒ‰ใซ SSH ใงๆŽฅ็ถšใ—ใ€ใใ‚Œใžใ‚ŒใฎใƒŽใƒผใƒ‰ใงๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆ€ฅใๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒฉใƒณใƒใƒฃใƒผใฏไธกๆ–นใฎใƒŽใƒผใƒ‰ใŒๅŒๆœŸใ™ใ‚‹ใพใงๅพ…ๆฉŸใ—ใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[torchrun](https://pytorch.org/docs/stable/elastic/run.html) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใกใชใฟใซใ€ใ“ใ‚Œใฏ pytorch ใฎๆ•ฐใƒใƒผใ‚ธใƒงใƒณๅ‰ใฎ`torch.distributed.launch`ใ‚’็ฝฎใๆ›ใˆใŸใƒฉใƒณใƒใƒฃใƒผใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +#### ใƒ‡ใ‚ฃใƒผใƒ—ใ‚นใƒ”ใƒผใƒ‰ ใƒฉใƒณใƒใƒฃใƒผ + +ไปฃใ‚ใ‚Šใซ`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใพใš`hostfile`ใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +``` +hostname1 slots=8 +hostname2 slots=8 +``` + +ใใ—ใฆใ€ๆฌกใฎใ‚ˆใ†ใซ่ตทๅ‹•ใงใใพใ™ใ€‚ + +```bash +deepspeed --num_gpus 8 --num_nodes 2 --hostfile hostfile --master_addr hostname1 --master_port=9901 \ +your_program.py --deepspeed ds_config.json +``` + +`torch.distributed.run`ใƒฉใƒณใƒใƒฃใƒผใจใฏ็•ฐใชใ‚Šใ€`deepspeed`ใฏไธกๆ–นใฎใƒŽใƒผใƒ‰ใงใ“ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’่‡ชๅ‹•็š„ใซ่ตทๅ‹•ใ—ใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒชใ‚ฝใƒผใ‚นๆง‹ๆˆ (ใƒžใƒซใƒใƒŽใƒผใƒ‰)](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +#### Launching in a SLURM environment + +SLURM ็’ฐๅขƒใงใฏใ€ๆฌกใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ไปฅไธ‹ใฏใ€็‰นๅฎšใฎ SLURM ็’ฐๅขƒใซ้ฉๅˆใ•ใ›ใ‚‹ใŸใ‚ใซๅฟ…่ฆใช slurm ใ‚นใ‚ฏใƒชใƒ—ใƒˆ `launch.slurm` ใงใ™ใ€‚ + +```bash +#SBATCH --job-name=test-nodes # name +#SBATCH --nodes=2 # nodes +#SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! +#SBATCH --cpus-per-task=10 # number of cores per tasks +#SBATCH --gres=gpu:8 # number of gpus +#SBATCH --time 20:00:00 # maximum execution time (HH:MM:SS) +#SBATCH --output=%x-%j.out # output file name + +export GPUS_PER_NODE=8 +export MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) +export MASTER_PORT=9901 + +srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ + --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ + --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ +your_program.py --deepspeed ds_config.json' +``` + +ใ‚ใจใฏๅฎŸ่กŒใ‚’ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ +```bash +sbatch launch.slurm +``` + +#### Use of Non-shared filesystem + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€DeepSpeed ใฏใƒžใƒซใƒใƒŽใƒผใƒ‰็’ฐๅขƒใŒๅ…ฑๆœ‰ใ‚นใƒˆใƒฌใƒผใ‚ธใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๆƒณๅฎšใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚ŒใŒๅฝ“ใฆใฏใพใ‚‰ใšใ€ๅ„ใƒŽใƒผใƒ‰ใŒใƒญใƒผใ‚ซใƒซ ใƒ•ใ‚กใ‚คใƒซใ‚ทใ‚นใƒ†ใƒ ใ—ใ‹ๅ‚็…งใงใใชใ„ๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’่ชฟๆ•ดใ—ใฆ [`checkpoint`_section](https://www.deepspeed.ai/docs/config-json/#) ใ‚’ๅซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ชใƒ—ใ‚ทใƒงใƒณ) ใ‚’ๆฌกใฎ่จญๅฎšใงๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + + +```json +{ + "checkpoint": { + "use_node_local_storage": true + } +} +``` + +ใ‚ใ‚‹ใ„ใฏใ€[`Trainer`] ใฎ `--save_on_each_node` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใ€ไธŠ่จ˜ใฎ่จญๅฎšใฏ่‡ชๅ‹•็š„ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ + + + +### Deployment in Notebooks + +ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎใ‚ปใƒซใ‚’ใ‚นใ‚ฏใƒชใƒ—ใƒˆใจใ—ใฆๅฎŸ่กŒใ™ใ‚‹ๅ ดๅˆใฎๅ•้กŒใฏใ€ไพๅญ˜ใ™ใ‚‹้€šๅธธใฎ`deepspeed`ใƒฉใƒณใƒใƒฃใƒผใŒใชใ„ใ“ใจใงใ™ใ€‚ +็‰นๅฎšใฎ่จญๅฎšใงใฏใ€ใใ‚Œใ‚’ใ‚จใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +GPU ใ‚’ 1 ใคใ ใ‘ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏๅ†…ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ณใƒผใƒ‰ใ‚’่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```python +# DeepSpeed requires a distributed environment even when only one process is used. +# This emulates a launcher in the notebook +import os + +os.environ["MASTER_ADDR"] = "localhost" +os.environ["MASTER_PORT"] = "9994" # modify if RuntimeError: Address already in use +os.environ["RANK"] = "0" +os.environ["LOCAL_RANK"] = "0" +os.environ["WORLD_SIZE"] = "1" + +# Now proceed as normal, plus pass the deepspeed config file +training_args = TrainingArguments(..., deepspeed="ds_config_zero3.json") +trainer = Trainer(...) +trainer.train() +``` + +ๆณจ: `...` ใฏใ€้–ขๆ•ฐใซๆธกใ™้€šๅธธใฎๅผ•ๆ•ฐใ‚’่กจใ—ใพใ™ใ€‚ + +่ค‡ๆ•ฐใฎ GPU ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€DeepSpeed ใŒๅ‹•ไฝœใ™ใ‚‹ใซใฏใƒžใƒซใƒใƒ—ใƒญใ‚ปใ‚น็’ฐๅขƒใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใคใพใ‚Šใ€ใ‚ใชใŸใฏๆŒใฃใฆใ„ใพใ™ +ใใฎ็›ฎ็š„ใงใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใŒใ€ใ“ใ‚Œใฏใ€ๆ็คบใ•ใ‚ŒใŸๅˆ†ๆ•ฃ็’ฐๅขƒใ‚’ใ‚จใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆใฏๅฎŸ็พใงใใพใ›ใ‚“ใ€‚ +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๅ†’้ ญใงใ€‚ + +็พๅœจใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใซใใฎๅ ดใงๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใŸใ„ๅ ดๅˆใฏใ€ๅฐ‚็”จใฎ +ใ‚ปใƒซใฎๅ†…ๅฎน: + +```python no-style +%%bash +cat <<'EOT' > ds_config_zero3.json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "steps_per_print": 2000, + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", + "wall_clock_breakdown": false +} +EOT +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใŒใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎใ‚ปใƒซใงใฏใชใ้€šๅธธใฎใƒ•ใ‚กใ‚คใƒซใซใ‚ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใฆ`deepspeed`ใ‚’้€šๅธธใฉใŠใ‚Š่ตทๅ‹•ใงใใพใ™ใ€‚ +็ดฐ่ƒžใ‹ใ‚‰ใฎใ‚ทใ‚งใƒซใ€‚ใŸใจใˆใฐใ€`run_translation.py` ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซ่ตทๅ‹•ใ—ใพใ™ใ€‚ + +```python no-style +!git clone https://github.com/huggingface/transformers +!cd transformers; deepspeed examples/pytorch/translation/run_translation.py ... +``` + +ใพใŸใฏใ€`%%bash` ใƒžใ‚ธใƒƒใ‚ฏใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ‚ทใ‚งใƒซ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎ่ค‡ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่จ˜่ฟฐใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python no-style +%%bash + +git clone https://github.com/huggingface/transformers +cd transformers +deepspeed examples/pytorch/translation/run_translation.py ... +``` + +ใใฎใ‚ˆใ†ใชๅ ดๅˆใ€ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆœ€ๅˆใซ็คบใ—ใŸใ‚ณใƒผใƒ‰ใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ๆณจ: `%%bash` ใƒžใ‚ธใƒƒใ‚ฏใฏๅ„ชใ‚Œใฆใ„ใพใ™ใŒใ€็พๆ™‚็‚นใงใฏๅ‡บๅŠ›ใ‚’ใƒใƒƒใƒ•ใ‚กใƒชใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใ€ใƒ—ใƒญใ‚ปใ‚นใŒ็ต‚ไบ†ใ™ใ‚‹ใพใงใƒญใ‚ฐใฏ่กจ็คบใ•ใ‚Œใพใ›ใ‚“ใ€‚ +ๅฎŒไบ†ใ—ใพใ™ใ€‚ + + + +### Configuration + +่จญๅฎšใƒ•ใ‚กใ‚คใƒซใงไฝฟ็”จใงใใ‚‹ DeepSpeed ่จญๅฎšใ‚ชใƒ—ใ‚ทใƒงใƒณใฎๅฎŒๅ…จใชใ‚ฌใ‚คใƒ‰ใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[ๆฌกใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/) ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ•ใพใ–ใพใชๅฎŸ้š›ใฎใƒ‹ใƒผใ‚บใซๅฏพๅฟœใ™ใ‚‹ๆ•ฐๅใฎ DeepSpeed ๆง‹ๆˆไพ‹ใ‚’ [DeepSpeedExamples](https://github.com/deepspeedai/DeepSpeedExamples)ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใƒชใƒใ‚ธใƒˆใƒช: + +```bash +git clone https://github.com/deepspeedai/DeepSpeedExamples +cd DeepSpeedExamples +find . -name '*json' +``` + +ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚’็ถšใ‘ใฆใ€Lamb ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ๆง‹ๆˆใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ใจใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆฌกใฎไธญใ‹ใ‚‰ๆคœ็ดขใงใใพใ™ +`.json` ใƒ•ใ‚กใ‚คใƒซใฎไพ‹: + +```bash +grep -i Lamb $(find . -name '*json') +``` + +ใ•ใ‚‰ใซใ„ใใคใ‹ใฎไพ‹ใŒ [ใƒกใ‚คใƒณ ใƒชใƒใ‚ธใƒˆใƒช](https://github.com/deepspeedai/DeepSpeed) ใซใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +DeepSpeed ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ๅธธใซ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€ไธ€้ƒจใฎๆง‹ๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใซใฏ +ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณ็ตŒ็”ฑใง่จญๅฎšใ—ใพใ™ใ€‚ๅพฎๅฆ™ใช้•ใ„ใซใคใ„ใฆใฏใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใง่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใŒใฉใฎใ‚ˆใ†ใชใ‚‚ใฎใ‹ใ‚’็†่งฃใ™ใ‚‹ใŸใ‚ใซใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ +ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ็Šถๆ…‹ใฎ CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅซใฟใ€`AdamW`ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจ`WarmupLR`ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ไฝฟ็”จใ—ใ€ๆททๅˆใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ +`--fp16` ใŒๆธกใ•ใ‚ŒใŸๅ ดๅˆใฎ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ: + + +```json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 2e8, + "overlap_comm": true, + "reduce_scatter": true, + "reduce_bucket_size": 2e8, + "contiguous_gradients": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", +} +``` + +ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€DeepSpeed ใฏ [`Trainer`] ใ‹ใ‚‰ๅ—ใ‘ๅ–ใฃใŸ่จญๅฎšใ‚’ใƒญใ‚ฐใซ่จ˜้Œฒใ—ใพใ™ใ€‚ +ใ‚ณใƒณใ‚ฝใƒผใƒซใซๆธกใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆœ€็ต‚็š„ใซใฉใฎใ‚ˆใ†ใช่จญๅฎšใŒๆธกใ•ใ‚ŒใŸใฎใ‹ใ‚’ๆญฃ็ขบใซ็ขบ่ชใงใใพใ™ใ€‚ + + + +### Passing Configuration + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใง่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซใ€้€šๅธธใ€DeepSpeed ่จญๅฎšใฏ json ใƒ•ใ‚กใ‚คใƒซใธใฎใƒ‘ใ‚นใจใ—ใฆๆธกใ•ใ‚Œใพใ™ใŒใ€ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่จญๅฎšใซใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณ ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใ‚คใ‚นใ‚’ไฝฟ็”จใ›ใšใ€ไปฃใ‚ใ‚Šใซใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +[`Trainer`] via [`TrainingArguments`] ใใฎๅพŒใ€`deepspeed` ๅผ•ๆ•ฐใซใคใ„ใฆใฏๆฌกใฎใ“ใจใŒใงใใพใ™ +ใƒใ‚นใƒˆใ•ใ‚ŒใŸ `dict` ใ‚’ๆธกใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใใฎๅ ดใงๆง‹ๆˆใ‚’ไฝœๆˆใงใใ€ใใ‚Œใ‚’ๆ›ธใ่พผใ‚€ๅฟ…่ฆใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +[`TrainingArguments`] ใซๆธกใ™ๅ‰ใซใƒ•ใ‚กใ‚คใƒซ ใ‚ทใ‚นใƒ†ใƒ ใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ + +่ฆ็ด„ใ™ใ‚‹ใจใ€ๆฌกใฎใ“ใจใŒใงใใพใ™ใ€‚ + +```python +TrainingArguments(..., deepspeed="/path/to/ds_config.json") +``` + +ใพใŸใฏ๏ผš + +```python +ds_config_dict = dict(scheduler=scheduler_params, optimizer=optimizer_params) +TrainingArguments(..., deepspeed=ds_config_dict) +``` + + + + +### Shared Configuration + + + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๅฟ…่ชญใงใ™ + + + +[`Trainer`] ใจ DeepSpeed ใฎไธกๆ–นใŒๆญฃใ—ใๆฉŸ่ƒฝใ™ใ‚‹ใซใฏใ€ใ„ใใคใ‹ใฎ่จญๅฎšๅ€คใŒๅฟ…่ฆใงใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ๆคœๅ‡บใŒๅ›ฐ้›ฃใชใ‚จใƒฉใƒผใซใคใชใŒใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ๅฎš็พฉใฎ็ซถๅˆใ‚’้˜ฒใใŸใ‚ใซใ€ใใ‚Œใ‚‰ใ‚’ๆง‹ๆˆใ™ใ‚‹ใ“ใจใซใ—ใพใ—ใŸใ€‚ +[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐ็ตŒ็”ฑใ€‚ + +ใ•ใ‚‰ใซใ€ไธ€้ƒจใฎๆง‹ๆˆๅ€คใฏใƒขใƒ‡ใƒซใฎๆง‹ๆˆใซๅŸบใฅใ„ใฆ่‡ชๅ‹•็š„ใซๅฐŽๅ‡บใ•ใ‚Œใพใ™ใ€‚ +่ค‡ๆ•ฐใฎๅ€คใ‚’ๆ‰‹ๅ‹•ใง่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚[`Trainer`] ใซๅคง้ƒจๅˆ†ใ‚’ไปปใ›ใ‚‹ใฎใŒๆœ€ๅ–„ใงใ™ +ใฎ่จญๅฎšใ‚’่กŒใ„ใพใ™ใ€‚ + +ใ—ใŸใŒใฃใฆใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใงใฏใ€็‰นๅˆฅใช่จญๅฎšๅ€ค `auto` ใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚’่จญๅฎšใ™ใ‚‹ใจใ€ +ๆญฃใ—ใ„ๅ€คใพใŸใฏๆœ€ใ‚‚ๅŠน็އ็š„ใชๅ€คใซ่‡ชๅ‹•็š„ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚’็„ก่ฆ–ใ™ใ‚‹ใ“ใจใ‚’่‡ช็”ฑใซ้ธๆŠžใ—ใฆใใ ใ•ใ„ +ๆŽจๅฅจไบ‹้ …ใ‚’ๅ‚็…งใ—ใ€ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ—ใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๆฌกใฎ็‚นใซๅๅˆ†ๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +[`Trainer`] ๅผ•ๆ•ฐใจ DeepSpeed ่จญๅฎšใฏไธ€่‡ดใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€ๅŒใ˜ใ‚‚ใฎใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ‹ +ๅญฆ็ฟ’็އใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ€ใพใŸใฏๅ‹พ้…็ดฏ็ฉ่จญๅฎš?ใ“ใ‚Œใ‚‰ใŒไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏ้žๅธธใซๅคฑๆ•—ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ +ๆ–นๆณ•ใ‚’ๆคœๅ‡บใ™ใ‚‹ใฎใŒ้›ฃใ—ใ„ใ€‚ใ‚ใชใŸใฏ่ญฆๅ‘Šใ‚’ๅ—ใ‘ใพใ—ใŸใ€‚ + +DeepSpeed ใฎใฟใซๅ›บๆœ‰ใฎๅ€คใ‚„ใ€ใใ‚Œใซๅˆใ‚ใ›ใฆๆ‰‹ๅ‹•ใง่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ€คใŒไป–ใซใ‚‚่ค‡ๆ•ฐใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ใชใŸใฎ่ฆๆœ›ใ€‚ + +็‹ฌ่‡ชใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใงใ€DeepSpeed ๆง‹ๆˆใ‚’ใƒžใ‚นใ‚ฟใƒผใจใ—ใฆๅค‰ๆ›ดใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใใ‚ŒใซๅŸบใฅใ„ใฆ [`TrainingArguments`] ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ๆ‰‹้ †ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +1. ใƒžใ‚นใ‚ฟใƒผๆง‹ๆˆใจใ—ใฆไฝฟ็”จใ™ใ‚‹ DeepSpeed ๆง‹ๆˆใ‚’ไฝœๆˆใพใŸใฏใƒญใƒผใƒ‰ใ—ใพใ™ +2. ใ“ใ‚Œใ‚‰ใฎๅ€คใซๅŸบใฅใ„ใฆ [`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ—ใพใ™ + +`scheduler.params.total_num_steps`ใชใฉใฎไธ€้ƒจใฎๅ€คใฏๆฌกใฎใ‚ˆใ†ใซ่จˆ็ฎ—ใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +`train` ไธญใซ [`Trainer`] ใ‚’ๅฎŸ่กŒใ—ใพใ™ใŒใ€ใ‚‚ใกใ‚ใ‚“่‡ชๅˆ†ใง่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + +### ZeRO + +[Zero Redundancy Optimizer (ZeRO)](https://www.deepspeed.ai/tutorials/zero/) ใฏใ€DeepSpeed ใฎไธปๅŠ›่ฃฝๅ“ใงใ™ใ€‚ใใ‚Œ +3 ใคใฎ็•ฐใชใ‚‹ใƒฌใƒ™ใƒซ (ๆฎต้šŽ) ใฎๆœ€้ฉๅŒ–ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ๆœ€ๅˆใฎใ‚‚ใฎใฏใ€ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใฎ่ฆณ็‚นใ‹ใ‚‰ใฏใ‚ใพใ‚Š่ˆˆๅ‘ณๆทฑใ„ใ‚‚ใฎใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ—ใŸใŒใฃใฆใ€ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ‚นใƒ†ใƒผใ‚ธ 2 ใจ 3 ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ใ‚นใƒ†ใƒผใ‚ธ 3 ใฏใ€ๆœ€ๆ–ฐใฎ ZeRO-Infinity ใฎ่ฟฝๅŠ ใซใ‚ˆใฃใฆใ•ใ‚‰ใซๆ”นๅ–„ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใฎ `zero_optimization` ใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๆœ€ใ‚‚้‡่ฆใช้ƒจๅˆ†ใงใ™ ([docs](https://www.deepspeed.ai/docs/config-json/#zero-optimizations-for-fp16-training))ใ€‚ใ“ใ“ใงๅฎš็พฉใ—ใพใ™ +ใฉใฎ ZeRO ใ‚นใƒ†ใƒผใ‚ธใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€ใใ—ใฆใใ‚Œใ‚‰ใ‚’ใฉใฎใ‚ˆใ†ใซๆง‹ๆˆใ™ใ‚‹ใ‹ใ€‚ๅ„ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ่ชฌๆ˜Žใฏใ€ +DeepSpeed ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ€‚ + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏใ€DeepSpeed ่จญๅฎšใ‚’ไป‹ใ—ใฆใฎใฟ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ - [`Trainer`] ใŒๆไพ›ใ—ใพใ™ +ๅŒ็ญ‰ใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ๆณจ: ็พๅœจใ€DeepSpeed ใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๅใ‚’ๆคœ่จผใ—ใชใ„ใŸใ‚ใ€ใ‚นใƒšใƒซใ‚’้–“้•ใˆใ‚‹ใจใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆ่จญๅฎšใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ใ‚นใƒšใƒซใŒ้–“้•ใฃใฆใ„ใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€‚ DeepSpeed ใ‚จใƒณใ‚ธใƒณใฎ่ตทๅ‹•ใƒญใ‚ฐ ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’่ฆ‹ใฆใ€ใใฎๅ€คใ‚’็ขบ่ชใงใใพใ™ใ€‚ +ไฝฟ็”จใ™ใ‚‹ใคใ‚‚ใ‚Šใงใ™ใ€‚ + + + +#### ZeRO-2 Config + +ไปฅไธ‹ใฏใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ใฎๆง‹ๆˆไพ‹ใงใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 5e8, + "overlap_comm": true, + "reduce_scatter": true, + "reduce_bucket_size": 5e8, + "contiguous_gradients": true + } +} +``` + +**ๆ€ง่ƒฝ่ชฟๆ•ด๏ผš** + +- `offload_optimizer` ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใจใ€GPU RAM ใฎไฝฟ็”จ้‡ใŒๅ‰Šๆธ›ใ•ใ‚Œใพใ™ (`"stage": 2` ใŒๅฟ…่ฆใงใ™) +- `"overlap_comm": true` ใฏใ€GPU RAM ไฝฟ็”จ้‡ใฎๅข—ๅŠ ใจใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใ—ใฆใ€้…ๅปถใ‚’ใ™ในใฆๅ‰Šๆธ›ใ—ใพใ™ใ€‚ `overlap_comm`ใฏ 4.5x ใ‚’ไฝฟ็”จใ—ใพใ™ + `allgather_bucket_size`ใจ`reduce_bucket_size`ใฎๅ€คใ€‚ใ—ใŸใŒใฃใฆใ€5e8 ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€9GB ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ + ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆ (`5e8 x 2Bytes x 2 x 4.5`)ใ€‚ใ—ใŸใŒใฃใฆใ€8GB ไปฅไธ‹ใฎ RAM ใ‚’ๆญ่ผ‰ใ—ใŸ GPU ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ + OOM ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`2e8`็จ‹ๅบฆใซๆธ›ใ‚‰ใ™ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใใ‚Œใซใฏ 3.6GB ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ใ‚„ใ‚ŠใŸใใชใ‚‹ใงใ—ใ‚‡ใ† + OOM ใซ้”ใ—ๅง‹ใ‚ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ใ‚ˆใ‚Šๅคงๅฎน้‡ใฎ GPU ใงใ‚‚ๅŒๆง˜ใงใ™ใ€‚ +- ใ“ใ‚Œใ‚‰ใฎใƒใƒƒใƒ•ใ‚กใ‚’ๆธ›ใ‚‰ใ™ใจใ€ใ‚ˆใ‚Šๅคšใใฎ GPU RAM ใ‚’ๅˆฉ็”จใ™ใ‚‹ใŸใ‚ใซ้€šไฟก้€Ÿๅบฆใ‚’็Š ็‰ฒใซใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ใƒใƒƒใƒ•ใ‚กใ‚ตใ‚คใ‚บใŒๅฐใ•ใ„ใปใฉใ€ + ้€šไฟกใŒ้…ใใชใ‚Šใ€ไป–ใฎใ‚ฟใ‚นใ‚ฏใงไฝฟ็”จใงใใ‚‹ GPU RAM ใŒๅข—ใˆใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒๅคงใใ„ๅ ดๅˆใฏใ€ + ้‡่ฆใชใฎใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ใ‚’ๅฐ‘ใ—้…ใ‚‰ใ›ใ‚‹ใ“ใจใฏ่‰ฏใ„ใƒˆใƒฌใƒผใƒ‰ใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ•ใ‚‰ใซใ€`deepspeed==0.4.4`ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใงๆœ‰ๅŠนใซใงใใ‚‹ๆ–ฐใ—ใ„ใ‚ชใƒ—ใ‚ทใƒงใƒณ`round_robin_gradients`ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ + +```json +{ + "zero_optimization": { + "round_robin_gradients": true + } +} +``` + +ใ“ใ‚Œใฏใ€ใใ‚็ดฐใ‹ใ„ๅ‹พ้…ใƒ‘ใƒผใƒ†ใ‚ฃใ‚ทใƒงใƒ‹ใƒณใ‚ฐใซใ‚ˆใฃใฆใƒฉใƒณใ‚ฏ้–“ใฎ CPU ใƒกใƒขใƒชใธใฎๅ‹พ้…ใ‚ณใƒ”ใƒผใ‚’ไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใ€CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฎใ‚นใƒ†ใƒผใ‚ธ 2 ๆœ€้ฉๅŒ–ใงใ™ใ€‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅˆฉ็‚นใฏใ€ๅ‹พ้…็ดฏ็ฉใ‚นใƒ†ใƒƒใƒ— (ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—้–“ใฎใ‚ณใƒ”ใƒผใฎๅข—ๅŠ ) ใพใŸใฏ GPU ๆ•ฐ (ไธฆๅˆ—ๅ‡ฆ็†ใฎๅข—ๅŠ ) ใซๅฟœใ˜ใฆๅข—ๅŠ ใ—ใพใ™ใ€‚ + + + +#### ZeRO-3 Config + +ไปฅไธ‹ใฏใ€ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใฎๆง‹ๆˆไพ‹ใงใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + } +} +``` + +ใƒขใƒ‡ใƒซใพใŸใฏใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใŒ GPU ใƒกใƒขใƒชใซ้ฉๅˆใ›ใšใ€CPU ใŒๆœชไฝฟ็”จใงใ‚ใ‚‹ใŸใ‚ใซ OOM ใŒ็™บ็”Ÿใ—ใฆใ„ใ‚‹ๅ ดๅˆ +`"device": "cpu"` ใ‚’ไฝฟ็”จใ—ใฆใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ CPU ใƒกใƒขใƒชใซใƒกใƒขใƒชใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใ€ใ“ใฎๅˆถ้™ใŒ่งฃๆฑบใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +CPU ใƒกใƒขใƒชใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใŸใใชใ„ๅ ดๅˆใฏใ€`device`ใ‚จใƒณใƒˆใƒชใซ`cpu`ใฎไปฃใ‚ใ‚Šใซ`none`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ชใƒ•ใƒญใƒผใƒ‰ๅ…ˆ +NVMe ใซใคใ„ใฆใฏๅพŒใปใฉ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ๅ›บๅฎšใƒกใƒขใƒชใฏใ€`pin_memory`ใ‚’`true`ใซ่จญๅฎšใ™ใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใซใ‚ˆใ‚Šใ€ๆฌกใฎใ‚ˆใ†ใชใ‚ณใ‚นใƒˆใ‚’ใ‹ใ‘ใฆใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ไป–ใฎใƒ—ใƒญใ‚ปใ‚นใŒไฝฟ็”จใงใใ‚‹ใƒกใƒขใƒชใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ใƒ”ใƒณ็•™ใ‚ใ•ใ‚ŒใŸใƒกใƒขใƒชใฏใ€ใใ‚Œใ‚’่ฆๆฑ‚ใ—ใŸ็‰นๅฎšใฎใƒ—ใƒญใ‚ปใ‚นใฎใŸใ‚ใซ็ขบไฟใ•ใ‚Œใพใ™ใ€‚ +้€šๅธธใ€้€šๅธธใฎ CPU ใƒกใƒขใƒชใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซ้ซ˜้€Ÿใซใ‚ขใ‚ฏใ‚ปใ‚นใ•ใ‚Œใพใ™ใ€‚ + +**ๆ€ง่ƒฝ่ชฟๆ•ด๏ผš** + +- `stage3_max_live_parameters`: `1e9` +- `stage3_max_reuse_distance`: `1e9` + +OOM ใซ้”ใ—ใŸๅ ดๅˆใฏใ€ใ€Œstage3_max_live_parametersใ€ใจใ€Œstage3_max_reuse_ distanceใ€ใ‚’ๆธ›ใ‚‰ใ—ใพใ™ใ€‚ๅฝฑ้Ÿฟใฏๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‰ใ‚Œใ‚‹ใฏใšใงใ™ +ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅฎŸ่กŒใ—ใชใ„้™ใ‚Šใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซๅฝฑ้Ÿฟใ—ใพใ™ใ€‚ `1e9`ใฏ็ด„ 2GB ใ‚’ๆถˆ่ฒปใ—ใพใ™ใ€‚่จ˜ๆ†ถใ‚’ๅ…ฑๆœ‰ใ—ใฆใ„ใ‚‹ใฎใฏใ€ +`stage3_max_live_parameters` ใจ `stage3_max_reuse_distance` ใชใฎใงใ€ๅŠ ็ฎ—ใ•ใ‚Œใ‚‹ใ‚‚ใฎใงใฏใชใใ€ๅˆ่จˆใง 2GB ใซใชใ‚Šใพใ™ใ€‚ + +`stage3_max_live_parameters` ใฏใ€็‰นๅฎšใฎๆ™‚็‚นใง GPU ไธŠใซไฟๆŒใ™ใ‚‹ๅฎŒๅ…จใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใฎไธŠ้™ใงใ™ใ€‚ +ๆ™‚้–“ใ€‚ ใ€Œๅ†ๅˆฉ็”จ่ท้›ขใ€ใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฐ†ๆฅใ„ใคๅ†ใณไฝฟ็”จใ•ใ‚Œใ‚‹ใ‹ใ‚’ๅˆคๆ–ญใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ™ใ‚‹ๆŒ‡ๆจ™ใงใ™ใ€‚ +`stage3_max_reuse_ distance`ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’็ ดๆฃ„ใ™ใ‚‹ใ‹ไฟๆŒใ™ใ‚‹ใ‹ใ‚’ๆฑบๅฎšใ—ใพใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ +่ฟ‘ใ„ๅฐ†ๆฅใซๅ†ใณไฝฟ็”จใ•ใ‚Œใ‚‹ไบˆๅฎš (`stage3_max_reuse_distance`ๆœชๆบ€) ใชใฎใงใ€้€šไฟกใ‚’ๆธ›ใ‚‰ใ™ใŸใ‚ใซไฟๆŒใ—ใพใ™ใ€‚ +ใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใ€‚ใ“ใ‚Œใฏใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆœ‰ๅŠนใซใ—ใฆใ„ใ‚‹ๅ ดๅˆใซ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ๅ†่จˆ็ฎ—ใŒ่กŒใ‚ใ‚Œใ€ +backward ใฏๅ˜ไธ€ใƒฌใ‚คใƒคใƒผ็ฒ’ๅบฆใ‚’ๆธกใ—ใ€ๅพŒๆ–นๅ†่จˆ็ฎ—ใพใงใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ‰ๆ–นๅ†่จˆ็ฎ—ใซไฟๆŒใ—ใŸใ„ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚ + +ๆฌกใฎๆง‹ๆˆๅ€คใฏใ€ใƒขใƒ‡ใƒซใฎ้ž่กจ็คบใ‚ตใ‚คใ‚บใซใ‚ˆใฃใฆ็•ฐใชใ‚Šใพใ™ใ€‚ + +- `reduce_bucket_size`: `hidden_size*hidden_size` +- `stage3_prefetch_bucket_size`: `0.9 * hidden_size * hidden_size` +- `stage3_param_persistence_threshold`: `10 * hidden_size` + +ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใ‚‰ใฎๅ€คใ‚’ `auto` ใซ่จญๅฎšใ™ใ‚‹ใจใ€[`Trainer`] ใŒๆŽจๅฅจใ•ใ‚Œใ‚‹ๅ€คใ‚’่‡ชๅ‹•็š„ใซๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ +ไพกๅ€ค่ฆณใ€‚ใŸใ ใ—ใ€ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +`stage3_gather_16bit_weights_on_model_save` ใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๆ™‚ใซใƒขใƒ‡ใƒซ fp16 ใฎ้‡ใฟ็ตฑๅˆใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ๅคงใใ„ +ใƒขใƒ‡ใƒซใจ่ค‡ๆ•ฐใฎ GPU ใฎๅ ดๅˆใ€ใ“ใ‚Œใฏใƒกใƒขใƒชใจ้€Ÿๅบฆใฎไธกๆ–นใฎ็‚นใง้ซ˜ไพกใชๆ“ไฝœใงใ™ใ€‚็พๅœจๅฟ…้ ˆใจใชใฃใฆใ„ใ‚‹ใฎใฏใ€ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ไบˆๅฎšใงใ™ใ€‚ใ“ใฎๅˆถ้™ใ‚’ๅ–ใ‚Š้™คใใ€ใ‚ˆใ‚Šไพฟๅˆฉใซใ™ใ‚‹ไปŠๅพŒใฎใ‚ขใƒƒใƒ—ใƒ‡ใƒผใƒˆใซๆณจ็›ฎใ—ใฆใใ ใ•ใ„ใ€‚ +ใƒ•ใƒฌใ‚ญใ‚ทใƒ–ใƒซใ€‚ + +ZeRO-2 ๆง‹ๆˆใ‹ใ‚‰็งป่กŒใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€`allgather_partitions`ใ€`allgather_bucket_size`ใ€ใŠใ‚ˆใณ +`reduce_scatter`่จญๅฎšใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏ ZeRO-3 ใงใฏไฝฟ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใ‚‰ใ‚’่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ—ใฆใŠใใจใ€ +็„ก่ฆ–ใ•ใ‚Œใ‚‹ใ€‚ + +- `sub_group_size`: `1e9` + + +`sub_group_size` ใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎใ‚นใƒ†ใƒƒใƒ—ไธญใซใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒๆ›ดๆ–ฐใ•ใ‚Œใ‚‹็ฒ’ๅบฆใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +`sub_group_size` ใฎใƒใ‚ฑใƒƒใƒˆใซใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ•ใ‚Œใ€ๅ„ใƒใ‚ฑใƒƒใƒˆใฏไธ€ๅบฆใซ 1 ใคใšใคๆ›ดๆ–ฐใ•ใ‚Œใพใ™ใ€‚ NVMeใ‚ชใƒ•ใƒญใƒผใƒ‰ใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ +ใ—ใŸใŒใฃใฆใ€ZeRO-Infinity ใฎ `sub_group_size`ใฏใ€ใƒขใƒ‡ใƒซใฎ็Šถๆ…‹ใŒ CPU ใซๅ‡บๅ…ฅใ‚Šใ™ใ‚‹็ฒ’ๅบฆใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ +ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚นใƒ†ใƒƒใƒ—ไธญใซ NVMe ใ‹ใ‚‰ใƒกใƒขใƒชใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎ CPU ใƒกใƒขใƒชไธ่ถณใŒ้˜ฒๆญขใ•ใ‚Œใพใ™ใ€‚ + +NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฏใ€`sub_group_size`ใ‚’ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใฎ *1e9* ใฎใพใพใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ +ๆฌกใฎๅ ดๅˆใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€ค: + +1. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—ไธญใซ OOM ใŒ็™บ็”Ÿใ™ใ‚‹: `sub_group_size` ใ‚’ๆธ›ใ‚‰ใ—ใฆใ€ไธ€ๆ™‚ใƒใƒƒใƒ•ใ‚กใƒผใฎใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ +2. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใ‚นใƒ†ใƒƒใƒ—ใซๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚`sub_group_size`ใ‚’ๅข—ใ‚„ใ—ใฆใ€ๅธฏๅŸŸๅน…ใฎไฝฟ็”จ็އใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ + ใƒ‡ใƒผใ‚ฟใƒใƒƒใƒ•ใ‚กใฎๅข—ๅŠ ใ€‚ + +#### ZeRO-0 Config + +ใ‚นใƒ†ใƒผใ‚ธ 0 ใจ 1 ใฏใ‚ใฃใŸใซไฝฟ็”จใ•ใ‚Œใชใ„ใŸใ‚ใ€ๆœ€ๅพŒใซใƒชใ‚นใƒˆใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ‚นใƒ†ใƒผใ‚ธ 0 ใงใฏใ€ใ™ในใฆใฎใ‚ฟใ‚คใƒ—ใฎใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’็„กๅŠนใซใ—ใ€DDP ใจใ—ใฆ DeepSpeed ใฎใฟใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใงใ‚ชใƒณใซใงใใพใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 0 + } +} +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ไป–ใซไฝ•ใ‚‚ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใชใใ€ๅŸบๆœฌ็š„ใซ ZeRO ใŒ็„กๅŠนใซใชใ‚Šใพใ™ใ€‚ + +#### ZeRO-1 Config + +ใ‚นใƒ†ใƒผใ‚ธ 1 ใฏใ€ใ‚นใƒ†ใƒผใ‚ธ 2 ใ‹ใ‚‰ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’้™คใ„ใŸใ‚‚ใฎใงใ™ใ€‚ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใ‚’ใ‚ทใƒฃใƒผใƒ‰ๅŒ–ใ™ใ‚‹ใ ใ‘ใงใ€ๅ‡ฆ็†ใ‚’ๅฐ‘ใ—้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ„ใคใงใ‚‚่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 1 + } +} +``` + + + +### NVMe Support + +ZeRO-Infinity ใฏใ€GPU ใจ CPU ใƒกใƒขใƒชใ‚’ NVMe ใƒกใƒขใƒชใงๆ‹กๅผตใ™ใ‚‹ใ“ใจใงใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ใŠใ‹ใ’ใง +ใ‚นใƒžใƒผใƒˆ ใƒ‘ใƒผใƒ†ใ‚ฃใ‚ทใƒงใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณใ‚ฟใ‚คใƒชใƒณใ‚ฐ ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใฏใ€ๅ„ GPU ใŒ้žๅธธใซๅฐ‘้‡ใฎใƒ‡ใƒผใ‚ฟใ‚’้€ๅ—ไฟกใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ชใƒ•ใƒญใƒผใƒ‰ใซใ‚ˆใ‚Šใ€ๆœ€ๆ–ฐใฎ NVMe ใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅˆฉ็”จใงใใ‚‹ๅˆ่จˆใƒกใƒขใƒช ใƒ—ใƒผใƒซใ‚’ใ•ใ‚‰ใซๅคงใใใ™ใ‚‹ใฎใซ้ฉใ—ใฆใ„ใ‚‹ใ“ใจใŒๅˆคๆ˜Žใ—ใพใ—ใŸใ€‚ +ใƒ—ใƒญใ‚ปใ‚นใ€‚ ZeRO-Infinity ใซใฏใ€ZeRO-3 ใŒๆœ‰ๅŠนใซใชใฃใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆฌกใฎ่จญๅฎšไพ‹ใงใฏใ€NVMe ใŒใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎไธกๆ–นใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```json +{ + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "nvme", + "nvme_path": "/local_nvme", + "pin_memory": true, + "buffer_count": 4, + "fast_init": false + }, + "offload_param": { + "device": "nvme", + "nvme_path": "/local_nvme", + "pin_memory": true, + "buffer_count": 5, + "buffer_size": 1e8, + "max_in_cpu": 1e9 + }, + "aio": { + "block_size": 262144, + "queue_depth": 32, + "thread_count": 1, + "single_submit": false, + "overlap_events": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, +} +``` + +ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎไธกๆ–นใ‚’ NVMe ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‹ใ€ใฉใกใ‚‰ใ‹ 1 ใคใ ใ‘ใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‹ใ€ใพใฃใŸใใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใชใ„ใ‹ใ‚’้ธๆŠžใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ๆฌกใฎๅ ดๅˆ +ๅˆฉ็”จๅฏ่ƒฝใช CPU ใƒกใƒขใƒชใŒๅคง้‡ใซใ‚ใ‚‹ๅ ดๅˆใฏใ€้ซ˜้€Ÿใซใชใ‚‹ใŸใ‚ใ€ๅฟ…ใš CPU ใƒกใƒขใƒชใฎใฟใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใฆใใ ใ•ใ„ (ใƒ’ใƒณใƒˆ: +*"device": "CPU"*)ใ€‚ + +[ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹](https://www.deepspeed.ai/docs/config-json/#optimizer-offloading) ใจ [ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ](https://www.deepspeed.ai/docs/config-json/#parameter-offloading)ใ€‚ + +`nvme_path`ใŒๅฎŸ้š›ใซ NVMe ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚NVMe ใฏ้€šๅธธใฎใƒใƒผใƒ‰ใƒ‰ใƒฉใ‚คใƒ–ใพใŸใฏ SSD ใงๅ‹•ไฝœใ—ใพใ™ใŒใ€ +ใฏใ‚‹ใ‹ใซ้…ใใชใ‚Šใพใ™ใ€‚้ซ˜้€Ÿใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ๆœ€ๆ–ฐใฎ NVMe ่ปข้€้€Ÿๅบฆใ‚’ๅฟต้ ญใซ็ฝฎใ„ใฆ่จญ่จˆใ•ใ‚Œใพใ—ใŸ (ใ“ใฎๆ™‚็‚นใงใฏ +ๆ›ธใ่พผใฟใงใฏใ€่ชญใฟๅ–ใ‚Šๆœ€ๅคง 3.5 GB/็ง’ใ€ๆ›ธใ่พผใฟๆœ€ๅคง 3 GB/็ง’ใฎใƒ”ใƒผใ‚ฏ้€ŸๅบฆใŒๅพ—ใ‚‰ใ‚Œใพใ™)ใ€‚ + +ๆœ€้ฉใช`aio`ๆง‹ๆˆใƒ–ใƒญใƒƒใ‚ฏใ‚’่ฆ‹ใคใ‘ใ‚‹ใซใฏใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จญๅฎšใงใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[ใ“ใ“ใง่ชฌๆ˜Ž](https://github.com/deepspeedai/DeepSpeed/issues/998)ใ€‚ + + + + +#### ZeRO-2 vs ZeRO-3 Performance + +ZeRO-3 ใฏใ€ไป–ใฎใ™ในใฆใŒๅŒใ˜ใ‚ˆใ†ใซๆง‹ๆˆใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ZeRO-2 ใ‚ˆใ‚Šใ‚‚้…ใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๅ‰่€…ใฏๅŽ้›†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ +ZeRO-2 ใฎๆฉŸ่ƒฝใซๅŠ ใˆใฆใƒขใƒ‡ใƒซใฎ้‡ใฟไป˜ใ‘ใ‚’่กŒใ„ใพใ™ใ€‚ ZeRO-2 ใŒใƒ‹ใƒผใ‚บใ‚’ๆบ€ใŸใ—ใ€ๆ•ฐๅ€‹ใฎ GPU ใ‚’่ถ…ใˆใฆๆ‹กๅผตใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ๅ ดๅˆ +ใใ†ใ™ใ‚Œใฐใ€ใใ‚Œใซๅ›บๅŸทใ™ใ‚‹ใ“ใจใ‚’้ธๆŠžใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ ZeRO-3 ใซใ‚ˆใ‚Šใ€ใฏใ‚‹ใ‹ใซ้ซ˜ใ„ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃๅฎน้‡ใŒๅฏ่ƒฝใซใชใ‚‹ใ“ใจใ‚’็†่งฃใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ +ใ‚นใƒ”ใƒผใƒ‰ใ‚’็Š ็‰ฒใซใ—ใฆใ€‚ + +ZeRO-3 ใฎๆง‹ๆˆใ‚’่ชฟๆ•ดใ—ใฆใ€ZeRO-2 ใซ่ฟ‘ใฅใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +- `stage3_param_persistence_threshold` ใ‚’้žๅธธใซๅคงใใชๆ•ฐๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€`6 * hidden_โ€‹โ€‹size * hidden_โ€‹โ€‹size` ใฎใ‚ˆใ†ใซใ€ๆœ€ๅคงโ€‹โ€‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚ˆใ‚Šใ‚‚ๅคงใใใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ GPU ใซไฟๆŒใ•ใ‚Œใพใ™ใ€‚ +- ZeRO-2 ใซใฏใใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใŒใชใ„ใŸใ‚ใ€`offload_params` ใ‚’ใ‚ชใƒ•ใซใ—ใพใ™ใ€‚ + +ๅค‰ๆ›ดใ—ใชใใฆใ‚‚ใ€`offload_params`ใ‚’ใ‚ชใƒ•ใซใ™ใ‚‹ใ ใ‘ใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +`stage3_param_persistence_threshold`ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใ‚‰ใฎๅค‰ๆ›ดใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใซๅฝฑ้Ÿฟใ—ใพใ™ใ€‚ใใ‚Œใง +ใ“ใ‚Œใ‚‰ใฏใ€ใƒ‹ใƒผใ‚บใซๅฟœใ˜ใฆใ€ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใจๅผ•ใๆ›ใˆใซ้€Ÿๅบฆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + + + +#### ZeRO-2 Example + +ไปฅไธ‹ใฏใ€ๅฎŒๅ…จใช ZeRO-2 ่‡ชๅ‹•ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ `ds_config_zero2.json` ใงใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 2e8, + "overlap_comm": true, + "reduce_scatter": true, + "reduce_bucket_size": 2e8, + "contiguous_gradients": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "steps_per_print": 2000, + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", + "wall_clock_breakdown": false +} +``` + +ไปฅไธ‹ใฏใ€ๆ‰‹ๅ‹•ใง่จญๅฎšใ•ใ‚ŒใŸๅฎŒๅ…จใช ZeRO-2 ใฎใ™ในใฆใŒๆœ‰ๅŠนใชๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ใ“ใ“ใงใฏไธปใซใ€ๅ…ธๅž‹็š„ใชใ‚‚ใฎใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ +ๅ€คใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใŒใ€่ค‡ๆ•ฐใฎ`auto`่จญๅฎšใŒๅซใพใ‚Œใ‚‹ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": true, + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": 3e-5, + "betas": [0.8, 0.999], + "eps": 1e-8, + "weight_decay": 3e-7 + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": 0, + "warmup_max_lr": 3e-5, + "warmup_num_steps": 500 + } + }, + + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 2e8, + "overlap_comm": true, + "reduce_scatter": true, + "reduce_bucket_size": 2e8, + "contiguous_gradients": true + }, + + "steps_per_print": 2000, + "wall_clock_breakdown": false +} +``` + + + +#### ZeRO-3 Example + +ไปฅไธ‹ใฏใ€ๅฎŒๅ…จใช ZeRO-3 ่‡ชๅ‹•ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ`ds_config_zero3.json`ใงใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "steps_per_print": 2000, + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", + "wall_clock_breakdown": false +} +``` + +ไปฅไธ‹ใฏใ€ๆ‰‹ๅ‹•ใง่จญๅฎšใ•ใ‚ŒใŸๅฎŒๅ…จใช ZeRO-3 ใฎใ™ในใฆใŒๆœ‰ๅŠนใชๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใ™ใ€‚ใ“ใ“ใงใฏไธปใซใ€ๅ…ธๅž‹็š„ใชใ‚‚ใฎใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ +ๅ€คใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใŒใ€่ค‡ๆ•ฐใฎ`auto`่จญๅฎšใŒๅซใพใ‚Œใ‚‹ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": true, + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": 3e-5, + "betas": [0.8, 0.999], + "eps": 1e-8, + "weight_decay": 3e-7 + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": 0, + "warmup_max_lr": 3e-5, + "warmup_num_steps": 500 + } + }, + + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": 1e6, + "stage3_prefetch_bucket_size": 0.94e6, + "stage3_param_persistence_threshold": 1e4, + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, + + "steps_per_print": 2000, + "wall_clock_breakdown": false +} +``` + +#### How to Choose Which ZeRO Stage and Offloads To Use For Best Performance + +ใ“ใ‚Œใงใ€ใ•ใพใ–ใพใชๆฎต้šŽใŒใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸใ€‚ใฉใกใ‚‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ใฉใฎใ‚ˆใ†ใซๆฑบๅฎšใ™ใ‚Œใฐใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ‹?ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใ“ใฎ่ณชๅ•ใซ็ญ”ใˆใฆใ„ใใพใ™ใ€‚ + +ไธ€่ˆฌใซใ€ๆฌกใฎใ“ใจใŒๅฝ“ใฆใฏใพใ‚Šใพใ™ใ€‚ + +- ้€Ÿๅบฆใฎ็‚น๏ผˆๅทฆใฎๆ–นใŒๅณใ‚ˆใ‚Š้€Ÿใ„๏ผ‰ + +ใ‚นใƒ†ใƒผใ‚ธ 0 (DDP) > ใ‚นใƒ†ใƒผใ‚ธ 1 > ใ‚นใƒ†ใƒผใ‚ธ 2 > ใ‚นใƒ†ใƒผใ‚ธ 2 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ > ใ‚นใƒ†ใƒผใ‚ธ 3 > ใ‚นใƒ†ใƒผใ‚ธ 3 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ + +- GPU ใƒกใƒขใƒชใฎไฝฟ็”จ็Šถๆณ (ๅณใฏๅทฆใ‚ˆใ‚Šใ‚‚ GPU ใƒกใƒขใƒชๅŠน็އใŒ้ซ˜ใ„) + +ใ‚นใƒ†ใƒผใ‚ธ 0 (DDP) < ใ‚นใƒ†ใƒผใ‚ธ 1 < ใ‚นใƒ†ใƒผใ‚ธ 2 < ใ‚นใƒ†ใƒผใ‚ธ 2 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ < ใ‚นใƒ†ใƒผใ‚ธ 3 < ใ‚นใƒ†ใƒผใ‚ธ 3 + ใ‚ชใƒ•ใƒญใƒผใƒ‰ + +ใ—ใŸใŒใฃใฆใ€ๆœ€ๅฐ้™ใฎๆ•ฐใฎ GPU ใซๅŽใพใ‚ŠใชใŒใ‚‰ๆœ€้€ŸใฎๅฎŸ่กŒใ‚’ๅฎŸ็พใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใƒ—ใƒญใ‚ปใ‚นใซๅพ“ใ†ใ“ใจใŒใงใใพใ™ใ€‚ๆœ€ใ‚‚้€Ÿใ„ใ‚ขใƒ—ใƒญใƒผใƒใ‹ใ‚‰้–‹ๅง‹ใ—ใ€GPU OOM ใซ้™ฅใฃใŸๅ ดๅˆใฏใ€ๆฌกใซ้…ใ„ใ‚ขใƒ—ใƒญใƒผใƒใซ้€ฒใฟใพใ™ใŒใ€ใ“ใ‚Œใซใ‚ˆใ‚Šไฝฟ็”จใ•ใ‚Œใ‚‹ GPU ใƒกใƒขใƒชใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ใชใฉใชใฉใ€‚ + +ใพใšใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ 1 ใซ่จญๅฎšใ—ใพใ™ (ๅฟ…่ฆใชๆœ‰ๅŠนใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใซๅฏพใ—ใฆใ€ใ„ใคใงใ‚‚ๅ‹พ้…็ดฏ็ฉใ‚’ไฝฟ็”จใงใใพใ™)ใ€‚ + +1. `--gradient_checkpointing 1` (HF Trainer) ใพใŸใฏ็›ดๆŽฅ `model.gradient_checkpointing_enable()` ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ - OOM ใฎๅ ดๅˆ +2. ๆœ€ๅˆใซ ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 ใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ OOMใฎๅ ดๅˆ +3. ZeRO ใ‚นใƒ†ใƒผใ‚ธ 2 + `offload_optimizer` ใ‚’่ฉฆใ—ใพใ™ - OOM ใฎๅ ดๅˆ +4. ZeRO ใ‚นใƒ†ใƒผใ‚ธ 3 ใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ - OOM ใฎๅ ดๅˆ +5. `cpu` ใซๅฏพใ—ใฆ `offload_param` ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ - OOM ใฎๅ ดๅˆ +6. OOM ใฎๅ ดๅˆใฏใ€`cpu`ใซๅฏพใ—ใฆ`offload_optimizer`ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ + +7. ใใ‚Œใงใ‚‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บ 1 ใซ้ฉๅˆใ—ใชใ„ๅ ดๅˆใฏใ€ใพใšใ•ใพใ–ใพใชใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใ‚’็ขบ่ชใ—ใ€ๅฏ่ƒฝใงใ‚ใ‚Œใฐๅ€คใ‚’ไธ‹ใ’ใพใ™ใ€‚ใŸใจใˆใฐใ€`generate`ใ‚’ไฝฟ็”จใ—ใ€ๅบƒใ„ๆคœ็ดขใƒ“ใƒผใƒ ใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฏใ€ๅคง้‡ใฎใƒกใƒขใƒชใ‚’ๆถˆ่ฒปใ™ใ‚‹ใŸใ‚ใ€ๆคœ็ดขใƒ“ใƒผใƒ ใ‚’็‹ญใใ—ใพใ™ใ€‚ + +8. fp32 ใงใฏๅฟ…ใšๆททๅˆๅŠ็ฒพๅบฆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใคใพใ‚Šใ€Ampere ไปฅไธŠใฎ GPU ใงใฏ bf16ใ€ๅคใ„ GPU ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใฏ fp16 ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +9. ใใ‚Œใงใ‚‚ OOM ใ‚’่กŒใ†ๅ ดๅˆใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ‹ใ€ZeRO-Infinity ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใคใพใ‚Šใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ `offload_param` ใจ `offload_optimizer` ใ‚’ `nvme` ใซๅˆ‡ใ‚Šๆ›ฟใˆใพใ™ใ€‚้žๅธธใซ้ซ˜้€Ÿใช nvme ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚้€ธ่ฉฑใจใ—ใฆใ€ZeRO-Infinity ใ‚’ไฝฟ็”จใ—ใฆๅฐใ•ใช GPU ใง BLOOM-176B ใ‚’ๆŽจ่ซ–ใ™ใ‚‹ใ“ใจใŒใงใใพใ—ใŸใŒใ€้žๅธธใซ้…ใ‹ใฃใŸใงใ™ใ€‚ใงใ‚‚ใ€ใ†ใพใใ„ใใพใ—ใŸ๏ผ + +ใ‚‚ใกใ‚ใ‚“ใ€ๆœ€ใ‚‚ GPU ใƒกใƒขใƒชๅŠน็އใฎ้ซ˜ใ„ๆง‹ๆˆใ‹ใ‚‰ๅง‹ใ‚ใฆใ€ๅพŒใ‹ใ‚‰้€†ใซ้€ฒใ‚€ใ“ใจใงใ€ใ“ใ‚Œใ‚‰ใฎๆ‰‹้ †ใ‚’้€†ใซๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ‚ใ‚‹ใ„ใฏไบŒ็ญ‰ๅˆ†ใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +OOM ใ‚’ๅผ•ใ่ตทใ“ใ•ใชใ„ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บ 1 ใ‚’ๅ–ๅพ—ใ—ใŸใ‚‰ใ€ๅฎŸๅŠนใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๆธฌๅฎšใ—ใพใ™ใ€‚ + +ๆฌกใซใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ใงใใ‚‹ใ ใ‘ๅคงใใใ—ใฆใฟใพใ™ใ€‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใŒๅคงใใ„ใปใฉใ€ไน—็ฎ—ใ™ใ‚‹่กŒๅˆ—ใŒๅทจๅคงใชๅ ดๅˆใซ GPU ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๆœ€้ซ˜ใซใชใ‚‹ใŸใ‚ใ€GPU ใฎๅŠน็އใŒๅ‘ไธŠใ—ใพใ™ใ€‚ + +ใ“ใ“ใงใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใ‚ฒใƒผใƒ ใŒๅง‹ใพใ‚Šใพใ™ใ€‚ไธ€้ƒจใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ๆฉŸ่ƒฝใ‚’ใ‚ชใƒ•ใซใ™ใ‚‹ใ‹ใ€ZeRO ๆฎต้šŽใงใ‚นใƒ†ใƒƒใƒ—ใƒ€ใ‚ฆใƒณใ—ใฆใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ๅข—ๆธ›ใ—ใฆใ€ๅฎŸๅŠนใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ†ๅบฆๆธฌๅฎšใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆบ€่ถณใ™ใ‚‹ใพใงๆด—ใ„ๆตใ—ใ€็นฐใ‚Š่ฟ”ใ—ใพใ™ใ€‚ + +ๆฐธ้ ใซใ“ใ‚Œใซ่ฒปใ‚„ใ™ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€3 ใ‹ๆœˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใซ้–ขใ—ใฆๆœ€ใ‚‚ๅŠนๆžœ็š„ใช่จญๅฎšใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซๆ•ฐๆ—ฅใ‹ใ‘ใฆใใ ใ•ใ„ใ€‚ใใฎใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใ‚ณใ‚นใƒˆใŒๆœ€ๅฐ้™ใซใชใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ˆใ‚Šๆ—ฉใๅฎŒไบ†ใงใใพใ™ใ€‚็พๅœจใฎ็›ฎใพใใ‚‹ใ—ใๅค‰ๅŒ–ใ™ใ‚‹ ML ใฎไธ–็•Œใงใฏใ€ไฝ•ใ‹ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใฎใซใ•ใ‚‰ใซ 1 ใ‹ๆœˆใ‹ใ‹ใ‚‹ๅ ดๅˆใ€็ตถๅฅฝใฎๆฉŸไผšใ‚’้€ƒใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ใ“ใ‚Œใฏ็งใŒๆ„่ฆ‹ใ‚’ๅ…ฑๆœ‰ใ—ใฆใ„ใ‚‹ใ ใ‘ใงใ‚ใ‚Šใ€ๆฑบใ—ใฆใ‚ใชใŸใ‚’ๆ€ฅใ‹ใใ†ใจใ—ใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ BLOOM-176B ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ€ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใซ 2 ๆ—ฅ้–“่ฒปใ‚„ใ—ใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ 90 TFLOP ใ‹ใ‚‰ 150 TFLOP ใซๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ—ใŸใ€‚ใ“ใฎๅ–ใ‚Š็ต„ใฟใซใ‚ˆใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ใ‚’ 1 ใ‹ๆœˆไปฅไธŠ็ฏ€็ด„ใงใใพใ—ใŸใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒกใƒขใฏไธปใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒขใƒผใƒ‰็”จใซๆ›ธใ‹ใ‚ŒใŸใ‚‚ใฎใงใ™ใŒใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใฏๆŽจ่ซ–ใซใ‚‚้ฉ็”จใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ใŸใจใˆใฐใ€ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใฎใฟๅฝน็ซ‹ใคใŸใ‚ใ€ๆŽจ่ซ–ไธญใฏไฝ•ใ‚‚่กŒใ‚ใ‚Œใพใ›ใ‚“ใ€‚ใ•ใ‚‰ใซใ€ใƒžใƒซใƒ GPU ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใฆใ„ใฆใ€[DeepSpeed-Inference](https://www.deepspeed.ai/tutorials/inference-tutorial/)ใ€[Accelerate](https://ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚น.co/blog/bloom-inference-pytorch-scripts) ใฏๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๆไพ›ใ™ใ‚‹ใฏใšใงใ™ใ€‚ + + +ใใฎไป–ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น้–ข้€ฃใฎ็ฐกๅ˜ใชใƒกใƒข: +- ไฝ•ใ‹ใ‚’ๆœ€ๅˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๅธธใซ 16 ใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ๅฝข็Šถใฎใƒ†ใƒณใ‚ฝใƒซ (้š ใ‚ŒใŸใ‚ตใ‚คใ‚บใชใฉ) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใซใคใ„ใฆใฏใ€ๅฐ‘ใชใใจใ‚‚ 2 ใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ GPU ใ‹ใ‚‰ใ•ใ‚‰ใซ้ซ˜ใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅผ•ใๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขๅ›บๆœ‰ใฎ [ๆณขใจใ‚ฟใ‚คใƒซใฎ้‡ๅญๅŒ–](https://developer.nvidia.com/blog/optimizing-gpu-performance-tensor-cores/) ใฎๅฏๅˆ†ๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +### Activation Checkpointing or Gradient Checkpointing + +ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๅŒใ˜ๆ–นๆณ•่ซ–ใ‚’ๆŒ‡ใ™ 2 ใคใฎ็•ฐใชใ‚‹็”จ่ชžใงใ™ใ€‚ใจใฆใ‚‚ใ‚„ใ‚„ใ“ใ—ใ„ใงใ™ใŒใ€ใ“ใ‚“ใชๆ„Ÿใ˜ใงใ™ใ€‚ + +ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€้€Ÿๅบฆใ‚’ GPU ใƒกใƒขใƒชใจๅผ•ใๆ›ใˆใซใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€GPU OOM ใ‚’ๅ…‹ๆœใ—ใŸใ‚Šใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ๅข—ใ‚„ใ™ใ“ใจใŒใงใใ€ๅคšใใฎๅ ดๅˆใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใซใคใชใŒใ‚Šใพใ™ใ€‚ + +HF Transformers ใƒขใƒ‡ใƒซใฏใ€DeepSpeed ใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซใคใ„ใฆไฝ•ใ‚‚็Ÿฅใ‚‰ใชใ„ใŸใ‚ใ€DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใงใใฎๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ—ใ‚ˆใ†ใจใ—ใฆใ‚‚ใ€ไฝ•ใ‚‚่ตทใ“ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ—ใŸใŒใฃใฆใ€ใ“ใฎ้žๅธธใซๆœ‰็›ŠใชๆฉŸ่ƒฝใ‚’ๆดป็”จใ™ใ‚‹ใซใฏ 2 ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. HF Transformers ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€`model.gradient_checkpointing_enable()` ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ‹ใ€HF ใƒˆใƒฌใƒผใƒŠใƒผใง `--gradient_checkpointing` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ“ใ‚ŒใŒ่‡ชๅ‹•็š„ใซๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใใ“ใงไฝฟใ‚ใ‚Œใ‚‹ใฎใŒ `torch.utils.checkpoint` ใงใ™ใ€‚ +2. ็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใ€DeepSpeed ใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€[ใใ“ใง่ฆๅฎšใ•ใ‚Œใฆใ„ใ‚‹ API](https://deepspeed.readthedocs.io/en/latest/activation-checkpointing.html) ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ HF Transformers ใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€`torch.utils.checkpoint` ใ‚’ DeepSpeed ใฎ API ใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ๅพŒ่€…ใฏใ€้ †ๆ–นๅ‘ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ๅ†่จˆ็ฎ—ใ™ใ‚‹ไปฃใ‚ใ‚Šใซ CPU ใƒกใƒขใƒชใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ใ‚ˆใ‚ŠๆŸ”่ปŸใงใ™ใ€‚ + +### Optimizer and Scheduler + +`offload_optimizer`ใ‚’ๆœ‰ๅŠนใซใ—ใชใ„้™ใ‚Šใ€DeepSpeed ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใจ HuggingFace ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใงใใพใ™ใ€‚ +ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ (HuggingFace ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใจ DeepSpeed ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็ต„ใฟๅˆใ‚ใ›ใ‚’้™คใ): + +| Combos | HF Scheduler | DS Scheduler | +|:-------------|:-------------|:-------------| +| HF Optimizer | Yes | Yes | +| DS Optimizer | No | Yes | + +`offload_optimizer`ใŒๆœ‰ๅŠนใชๅ ดๅˆใ€CPU ใจ +GPU ๅฎŸ่ฃ… (LAMB ใ‚’้™คใ)ใ€‚ + + + + +#### Optimizer + +DeepSpeed ใฎไธปใชใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฏใ€Adamใ€AdamWใ€OneBitAdamใ€Lamb ใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ ZeRO ใงๅพนๅบ•็š„ใซใƒ†ใ‚นใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ +ใ—ใŸใŒใฃใฆใ€ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ไป–ใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ใ€Œtorchใ€ใ‹ใ‚‰ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใฏใงใใพใ™ใ€‚ๅฎŒๅ…จใชใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏ [ใ“ใกใ‚‰](https://www.deepspeed.ai/docs/config-json/#optimizer-parameters) ใซใ‚ใ‚Šใพใ™ใ€‚ + +่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `optimizer` ใ‚จใƒณใƒˆใƒชใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€[`Trainer`] ใฏ +่‡ชๅ‹•็š„ใซ`AdamW`ใซ่จญๅฎšใ•ใ‚Œใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸๅ€คใพใŸใฏๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ๅผ•ๆ•ฐ: `--learning_rate`ใ€`--adam_beta1`ใ€`--adam_beta2`ใ€`--adam_epsilon`ใ€ใŠใ‚ˆใณ `--weight_decay`ใ€‚ + +ไปฅไธ‹ใฏใ€`AdamW`ใฎ่‡ชๅ‹•ๆง‹ๆˆใ•ใ‚ŒใŸ`optimizer`ใ‚จใƒณใƒˆใƒชใฎไพ‹ใงใ™ใ€‚ + +```json +{ + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + } +} +``` + +ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซใ‚ˆใฃใฆๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซๅ†…ใฎๅ€คใŒ่จญๅฎšใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใฏ 1 ใคใ‚ใ‚‹ใŸใ‚ใงใ™ +ๅ€คใฎๆฑบๅฎš็š„ใชใ‚ฝใƒผใ‚นใ‚’ๆไพ›ใ—ใ€ใŸใจใˆใฐๅญฆ็ฟ’็އใŒๆฌกใฎใ‚ˆใ†ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซใ€่ฆ‹ใคใ‘ใซใใ„ใ‚จใƒฉใƒผใ‚’ๅ›ž้ฟใ—ใพใ™ใ€‚ +ใ•ใพใ–ใพใชๅ ดๆ‰€ใงใ•ใพใ–ใพใชไพกๅ€ค่ฆณใ€‚ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒซใƒผใƒซใ€‚ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ•ใ‚Œใ‚‹ๅ€คใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- `lr` ใจ `--learning_rate` ใฎๅ€ค +- `betas` ใจ `--adam_beta1 --adam_beta2` ใฎๅ€ค +- `eps` ใจ `--adam_epsilon` ใฎๅ€ค +- `weight_decay` ใจ `--weight_decay` ใฎๅ€ค + +ใ—ใŸใŒใฃใฆใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใงๅ…ฑๆœ‰ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ + +ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "optimizer": { + "type": "AdamW", + "params": { + "lr": 0.001, + "betas": [0.8, 0.999], + "eps": 1e-8, + "weight_decay": 3e-7 + } + } +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + +ไธŠ่จ˜ใซใƒชใ‚นใƒˆใ•ใ‚Œใฆใ„ใชใ„ๅˆฅใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ใƒˆใƒƒใƒ—ใƒฌใƒ™ใƒซใฎๆง‹ๆˆใซ่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```json +{ + "zero_allow_untested_optimizer": true +} +``` + +`AdamW`ใจๅŒๆง˜ใซใ€ๅ…ฌๅผใซใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ไป–ใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ๆง‹ๆˆใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ็•ฐใชใ‚‹่จญๅฎšๅ€คใ‚’ๆŒใคๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ไพ‹ใˆใฐAdam ใฎๅ ดๅˆใฏใ€`weight_decay`ใ‚’`0.01`ไป˜่ฟ‘ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ•ใ‚‰ใซใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฏใ€Deepspeed ใฎ CPU Adam ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจไฝต็”จใ™ใ‚‹ใจๆœ€ใ‚‚ๅŠนๆžœ็š„ใซๆฉŸ่ƒฝใ—ใพใ™ใ€‚ `deepspeed==0.8.3` ใชใฎใงใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ใงๅˆฅใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ไปฅไธ‹ใ‚‚่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```json +{ + "zero_force_ds_cpu_optimizer": false +} +``` + +ๆœ€ไธŠไฝใฎๆง‹ๆˆใซ็งป่กŒใ—ใพใ™ใ€‚ + + + + +#### Scheduler + + +DeepSpeed ใฏใ€`LRRangeTest`ใ€`OneCycle`ใ€`WarmupLR`ใ€ใŠใ‚ˆใณ`WarmupDecayLR`ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ๅฎŒๅ…จใช +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏ[ใ“ใ“](https://www.deepspeed.ai/docs/config-json/#scheduler-parameters)ใงใ™ใ€‚ + +ใ“ใ“ใงใฏใ€๐Ÿค— Transformers ใจ DeepSpeed ใฎ้–“ใงใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใŒ้‡่ค‡ใ™ใ‚‹ๅ ดๆ‰€ใ‚’็คบใ—ใพใ™ใ€‚ + +- `--lr_scheduler_type constant_with_warmup` ็ตŒ็”ฑใฎ `WarmupLR` +- `--lr_scheduler_type Linear` ใ‚’ไป‹ใ—ใŸ `WarmupDecayLR`ใ€‚ใ“ใ‚Œใฏ `--lr_scheduler_type` ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + ใ—ใŸใŒใฃใฆใ€ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€ใ“ใ‚ŒใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง่จญๅฎšใ•ใ‚Œใ‚‹ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใซใชใ‚Šใพใ™ใ€‚ + +่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `scheduler` ใ‚จใƒณใƒˆใƒชใ‚’่จญๅฎšใ—ใชใ„ๅ ดๅˆใ€[`Trainer`] ใฏ +`--lr_scheduler_type`ใ€`--learning_rate`ใ€ใŠใ‚ˆใณ `--warmup_steps` ใพใŸใฏ `--warmup_ratio` ใฎๅ€คใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +๐Ÿค— ใใ‚Œใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒใƒผใ‚ธใƒงใƒณใ€‚ + +ไปฅไธ‹ใฏใ€`WarmupLR`ใฎ่‡ชๅ‹•ๆง‹ๆˆใ•ใ‚ŒใŸ`scheduler`ใ‚จใƒณใƒˆใƒชใฎไพ‹ใงใ™ใ€‚ + +```json +{ + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + } +} +``` + +*"auto"* ใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€[`Trainer`] ๅผ•ๆ•ฐใฏ่จญๅฎšใซๆญฃใ—ใ„ๅ€คใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +ใƒ•ใ‚กใ‚คใƒซใ€‚ใ“ใ‚Œใฏใ€ๅ€คใฎๆฑบๅฎš็š„ใชใ‚ฝใƒผใ‚นใŒ 1 ใคใ‚ใ‚‹ใ“ใจใจใ€ใŸใจใˆใฐๆฌกใฎใ‚ˆใ†ใชๅ ดๅˆใซ่ฆ‹ใคใ‘ใซใใ„ใ‚จใƒฉใƒผใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใงใ™ใ€‚ +ๅญฆ็ฟ’็އใฏใ€ๅ ดๆ‰€ใ”ใจใซ็•ฐใชใ‚‹ๅ€คใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใฎใƒซใƒผใƒซใ€‚่จญๅฎšใ•ใ‚Œใ‚‹ๅ€คใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- `warmup_min_lr` ใฎๅ€คใฏ `0` ใงใ™ใ€‚ +- `warmup_max_lr` ใจ `--learning_rate` ใฎๅ€คใ€‚ +- `warmup_num_steps` ใจ `--warmup_steps` ใฎๅ€ค (ๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ)ใ€‚ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏ `--warmup_ratio` ใ‚’ไฝฟ็”จใ—ใพใ™ + ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใƒ†ใƒƒใƒ—ใฎๆ•ฐใ‚’ไน—็ฎ—ใ—ใ€ๅˆ‡ใ‚ŠไธŠใ’ใพใ™ใ€‚ +- `total_num_steps` ใซใฏ `--max_steps` ใฎๅ€คใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏๅฎŸ่กŒๆ™‚ใซ่‡ชๅ‹•็š„ใซๅฐŽๅ‡บใ•ใ‚Œใพใ™ใ€‚ + ็’ฐๅขƒใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใ‚คใ‚บใ€ใŠใ‚ˆใณใใฎไป–ใฎใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐ ( + `WarmupDecayLR`)ใ€‚ + +ใ‚‚ใกใ‚ใ‚“ใ€ๆง‹ๆˆๅ€คใฎไธ€้ƒจใพใŸใฏใ™ในใฆใ‚’ๅผ•ใ็ถ™ใ„ใงใ€่‡ชๅˆ†ใง่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": 0, + "warmup_max_lr": 0.001, + "warmup_num_steps": 1000 + } + } +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + +ใŸใจใˆใฐใ€`WarmupDecayLR`ใฎๅ ดๅˆใฏใ€ๆฌกใฎใ‚จใƒณใƒˆใƒชใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +```json +{ + "scheduler": { + "type": "WarmupDecayLR", + "params": { + "last_batch_iteration": -1, + "total_num_steps": "auto", + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + } +} +``` + +`total_num_steps`ใ€`warmup_max_lr`ใ€`warmup_num_steps`ใ€ใŠใ‚ˆใณ `total_num_steps` ใฏใƒญใƒผใƒ‰ๆ™‚ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ + + + +### fp32 Precision + +Deepspeed ใฏใ€ๅฎŒๅ…จใช fp32 ใจ fp16 ใฎๆททๅˆ็ฒพๅบฆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +fp16 ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๅฟ…่ฆใชใƒกใƒขใƒชใŒๅคงๅน…ใซๅ‰Šๆธ›ใ•ใ‚Œใ€้€ŸๅบฆใŒๅ‘ไธŠใ™ใ‚‹ใŸใ‚ใ€ +ไฝฟ็”จใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใŒใ“ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒขใƒผใƒ‰ใง้ฉๅˆ‡ใซๅ‹•ไฝœใ—ใชใ„ๅ ดๅˆใฏใ€ไฝฟ็”จใ—ใชใ„ๆ–นใŒใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ€‚้€šๅธธใ“ใ‚Œ +ใƒขใƒ‡ใƒซใŒ fp16 ๆททๅˆ็ฒพๅบฆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใซ็™บ็”Ÿใ—ใพใ™ (ใŸใจใˆใฐใ€ใ“ใ‚Œใฏ bf16 ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅ ดๅˆใซใ‚ˆใ็™บ็”Ÿใ—ใพใ™) +ใƒขใƒ‡ใƒซ๏ผ‰ใ€‚ใ“ใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใงใฏใ€ใ‚ชใƒผใƒใƒผใƒ•ใƒญใƒผใพใŸใฏใ‚ขใƒณใƒ€ใƒผใƒ•ใƒญใƒผใŒ็™บ็”Ÿใ—ใ€`NaN`ๆๅคฑใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚ŒใŒใ‚ใชใŸใฎๅ ดๅˆใฏใ€ไฝฟ็”จใ—ใŸใ„ใจๆ€ใ†ใงใ—ใ‚‡ใ† +ๅฎŒๅ…จใช fp32 ใƒขใƒผใƒ‰ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ fp16 ๆททๅˆ็ฒพๅบฆใƒขใƒผใƒ‰ใ‚’ๆฌกใฎใ‚ˆใ†ใซๆ˜Ž็คบ็š„ใซ็„กๅŠนใซใ—ใพใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": false, + } +} +``` + +Ampere ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ ใƒ™ใƒผใ‚นใฎ GPU ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€pytorch ใƒใƒผใ‚ธใƒงใƒณ 1.7 ไปฅ้™ใฏ่‡ชๅ‹•็š„ใซ ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅˆ‡ใ‚Šๆ›ฟใ‚ใ‚Šใพใ™ใ€‚ +ไธ€้ƒจใฎๆ“ไฝœใงใฏใฏใ‚‹ใ‹ใซๅŠน็އ็š„ใช tf32 ๅฝขๅผใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€็ตๆžœใฏไพ็„ถใจใ—ใฆ fp32 ใซใชใ‚Šใพใ™ใ€‚่ฉณ็ดฐใจ +ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใคใ„ใฆใฏใ€[Ampere ใƒ‡ใƒใ‚คใ‚นไธŠใฎ TensorFloat-32(TF32)](https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ๆ–‡ๆ›ธใซใฏไปฅไธ‹ใŒๅซใพใ‚Œใพใ™ +ไฝ•ใ‚‰ใ‹ใฎ็†็”ฑใงใ“ใฎ่‡ชๅ‹•ๅค‰ๆ›ใ‚’ไฝฟ็”จใ—ใŸใใชใ„ๅ ดๅˆใฏใ€ใ“ใฎ่‡ชๅ‹•ๅค‰ๆ›ใ‚’็„กๅŠนใซใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +๐Ÿค— ใƒˆใƒฌใƒผใƒŠใƒผใงใฏใ€`--tf32` ใ‚’ไฝฟ็”จใ—ใฆๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€`--tf32 0` ใพใŸใฏ `--no_tf32` ใ‚’ไฝฟ็”จใ—ใฆ็„กๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€PyTorch ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + + +### Automatic Mixed Precision + +pytorch ใฎใ‚ˆใ†ใช AMP ใฎๆ–นๆณ•ใพใŸใฏ apex ใฎใ‚ˆใ†ใชๆ–นๆณ•ใง่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +### fp16 + +fp16 (float16) ใ‚’่จญๅฎšใ—ใฆ pytorch AMP ใฎใ‚ˆใ†ใชใƒขใƒผใƒ‰ใ‚’่จญๅฎšใ™ใ‚‹ใซใฏ: + +```json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + } +} +``` + +[`Trainer`] ใฏใ€ใฎๅ€คใซๅŸบใฅใ„ใฆใใ‚Œใ‚’่‡ชๅ‹•็š„ใซๆœ‰ๅŠนใพใŸใฏ็„กๅŠนใซใ—ใพใ™ใ€‚ +`args.fp16_backend`ใ€‚ๆฎ‹ใ‚Šใฎ่จญๅฎšๅ€คใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--fp16 --fp16_backend amp`ใพใŸใฏ`--fp16_full_eval`ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆœ‰ๅŠน/็„กๅŠนใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": true, + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + } +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + +ใ“ใ‚ŒใŒ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/#fp16-training-options)ใงใ™ใ€‚ + +### BF16 + +fp16 ใฎไปฃใ‚ใ‚Šใซ bf16 (bfloat16) ใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€ๆฌกใฎๆง‹ๆˆใ‚ปใ‚ฏใ‚ทใƒงใƒณใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +```json +{ + "bf16": { + "enabled": "auto" + } +} +``` + +bf16 ใฏ fp32 ใจๅŒใ˜ใƒ€ใ‚คใƒŠใƒŸใƒƒใ‚ฏ ใƒฌใƒณใ‚ธใ‚’ๅ‚™ใˆใฆใ„ใ‚‹ใŸใ‚ใ€ๆๅคฑใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--bf16` ใพใŸใฏ `--bf16_full_eval` ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆœ‰ๅŠน/็„กๅŠนใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "bf16": { + "enabled": true + } +} +``` + + + +`deepspeed==0.6.0`ใฎๆ™‚็‚นใงใฏใ€bf16 ใ‚ตใƒใƒผใƒˆใฏๆ–ฐใ—ใๅฎŸ้จ“็š„ใชใ‚‚ใฎใงใ™ใ€‚ + +bf16 ใŒๆœ‰ๅŠนใช็Šถๆ…‹ใง [ๅ‹พ้…็ดฏ็ฉ](#gradient-accumulation) ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€bf16 ใงๅ‹พ้…ใŒ็ดฏ็ฉใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅฝขๅผใฎ็ฒพๅบฆใŒไฝŽใ„ใŸใ‚ใ€ใ“ใ‚ŒใฏๅธŒๆœ›ใฉใŠใ‚Šใงใฏใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๆๅคฑใฎใ‚ใ‚‹่“„็ฉใซใคใชใŒใ‚Šใพใ™ใ€‚ + +ใ“ใฎๅ•้กŒใ‚’ไฟฎๆญฃใ—ใ€ใ‚ˆใ‚Š้ซ˜็ฒพๅบฆใฎ `dtype` (fp16 ใพใŸใฏ fp32) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆไพ›ใ™ใ‚‹ใŸใ‚ใฎไฝœๆฅญใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ + + + + +### NCCL Collectives + +่จ“็ทดไฝ“ๅˆถใฎ`dtype`ใŒใ‚ใ‚Šใ€ใ•ใพใ–ใพใชๅ‰Šๆธ›ใ‚„ๅŽ้›†/ๅˆ†ๆ•ฃๆ“ไฝœใชใฉใฎใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณ้›†ๅˆไฝ“ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅˆฅใฎ`dtype`ใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ™ในใฆใฎๅŽ้›†/ๅˆ†ๆ•ฃๆ“ไฝœใฏใ€ใƒ‡ใƒผใ‚ฟใŒๅซใพใ‚Œใฆใ„ใ‚‹ใฎใจๅŒใ˜ `dtype` ใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ใŸใ‚ใ€bf16 ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไฝ“ๅˆถใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใฏ bf16 ใงๅŽ้›†ใ•ใ‚Œใพใ™ใ€‚ๅŽ้›†ใฏๆๅคฑใฎใชใ„ๆ“ไฝœใงใ™ใ€‚ + +ใ•ใพใ–ใพใชใƒชใƒ‡ใƒฅใƒผใ‚นๆ“ไฝœใฏ้žๅธธใซๆๅคฑใŒๅคงใใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€่ค‡ๆ•ฐใฎ GPU ้–“ใงๅ‹พ้…ใŒๅนณๅ‡ๅŒ–ใ•ใ‚Œใ‚‹ๅ ดๅˆใ€้€šไฟกใŒ fp16 ใพใŸใฏ bf16 ใง่กŒใ‚ใ‚Œใ‚‹ๅ ดๅˆใ€็ตๆžœใฏๆๅคฑใŒๅคšใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚่ค‡ๆ•ฐใฎๆ•ฐๅ€คใ‚’ไฝŽ็ฒพๅบฆใงใ‚ขใƒ‰ใƒใ‚ฟใ‚คใ‚บใ™ใ‚‹ใจ็ตๆžœใฏๆญฃ็ขบใงใฏใชใ„ใŸใ‚ใงใ™ใ€‚ ใ€‚ bf16 ใงใฏ fp16 ใ‚ˆใ‚Šใ‚‚็ฒพๅบฆใŒไฝŽใ„ใŸใ‚ใ€ใ•ใ‚‰ใซใใ†ใงใ™ใ€‚้€šๅธธใฏ้žๅธธใซๅฐใ•ใ„ grad ใ‚’ๅนณๅ‡ใ™ใ‚‹้š›ใฎๆๅคฑใŒๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‰ใ‚Œใ‚‹ใŸใ‚ใ€fp16 ใงๅๅˆ†ใงใ‚ใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ๅŠ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใฏ fp16 ใŒใƒชใƒ€ใ‚ฏใ‚ทใƒงใƒณๆผ”็ฎ—ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ๅฎŒๅ…จใซๅˆถๅพกใงใใ€ๅฟ…่ฆใซๅฟœใ˜ใฆๅฐใ•ใชใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใฆใ€ใƒชใƒ€ใ‚ฏใ‚ทใƒงใƒณใŒ็ดฏ็ฉ dtype ใจใ—ใฆ fp32 ใ‚’ไฝฟ็”จใ—ใ€็ตๆžœใฎๆบ–ๅ‚™ใŒใงใใŸๅ ดๅˆใซใฎใฟๅŠ็ฒพๅบฆ `dtype` ใซใƒ€ใ‚ฆใƒณใ‚ญใƒฃใ‚นใƒˆใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใงใ™ใ€‚ + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใซใฏใ€ๆ–ฐใ—ใ„ๆง‹ๆˆใ‚จใƒณใƒˆใƒชใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + +```json +{ + "communication_data_type": "fp32" +} +``` + +ใ“ใฎ่จ˜ไบ‹ใฎๅŸท็ญ†ๆ™‚็‚นใงใฎๆœ‰ๅŠนใชๅ€คใฏใ€"fp16"ใ€"bfp16"ใ€"fp32"ใงใ™ใ€‚ + +ๆณจ: ใ‚นใƒ†ใƒผใ‚ธ ใ‚ผใƒญ 3 ใซใฏใ€bf16 ้€šไฟกใ‚ฟใ‚คใƒ—ใซ้–ขใ™ใ‚‹ใƒใ‚ฐใŒใ‚ใ‚Šใ€`deepspeed==0.8.1`ใงไฟฎๆญฃใ•ใ‚Œใพใ—ใŸใ€‚ + +### apex + +apex AMP ใฎใ‚ˆใ†ใชใƒขใƒผใƒ‰ ใ‚ปใƒƒใƒˆใ‚’่จญๅฎšใ™ใ‚‹ใซใฏ: + +```json +"amp": { + "enabled": "auto", + "opt_level": "auto" +} +``` + +[`Trainer`] ใฏ `args.fp16_backend` ใฎๅ€คใซๅŸบใฅใ„ใฆ่‡ชๅ‹•็š„ใซ่จญๅฎšใ—ใพใ™ใ€‚ +`args.fp16_opt_level`ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใฏใ€`--fp16 --fp16_backend apex --fp16_opt_level 01`ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใŒๆธกใ•ใ‚Œใ‚‹ใจๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ + +ใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๆง‹ๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "amp": { + "enabled": true, + "opt_level": "O1" + } +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + +ใ“ใ‚Œใฏ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/docs/config-json/#automatic-mixed-precision-amp-training-options)ใงใ™ใ€‚ + + + +### Batch Size + +ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’่จญๅฎšใ™ใ‚‹ใซใฏใ€ๆฌกใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + + +```json +{ + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto" +} +``` + +[`Trainer`] ใฏ่‡ชๅ‹•็š„ใซ `train_micro_batch_size_per_gpu` ใ‚’ๆฌกใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ +`args.per_device_train_batch_size`ใจ`train_batch_size`ใ‚’`args.world_size * args.per_device_train_batch_size * args.gradient_accumulation_steps`ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ + +ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "train_batch_size": 12, + "train_micro_batch_size_per_gpu": 4 +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + + + +### Gradient Accumulation + +ๅ‹พ้…็ดฏ็ฉใ‚ปใƒƒใƒˆใ‚’ๆง‹ๆˆใ™ใ‚‹ใซใฏ: + +```json +{ + "gradient_accumulation_steps": "auto" +} +``` + +[`Trainer`] ใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚’ `args.gradient_accumulation_steps` ใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ + +ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "gradient_accumulation_steps": 3 +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + + + +### Gradient Clipping + +ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ฐใƒฉใƒ‡ใƒผใ‚ทใƒงใƒณ ใ‚ฏใƒชใƒƒใƒ”ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใ‚’ๆง‹ๆˆใ™ใ‚‹ใซใฏ: + +```json +{ + "gradient_clipping": "auto" +} +``` + +[`Trainer`] ใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚’ `args.max_grad_norm` ใฎๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ + +ๅ€คใ‚’ๆ˜Ž็คบ็š„ใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```json +{ + "gradient_clipping": 1.0 +} +``` + +ใŸใ ใ—ใ€[`Trainer`] ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใจ DeepSpeed ใ‚’่‡ชๅˆ†ใงๅŒๆœŸใ™ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ +ๆง‹ๆˆใ€‚ + + + +### Getting The Model Weights Out + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็ถ™็ถšใ—ใ€DeepSpeed ใฎไฝฟ็”จใ‚’ๅ†้–‹ใ™ใ‚‹้™ใ‚Šใ€ไฝ•ใ‚‚ๅฟƒ้…ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ DeepSpeed ใ‚นใƒˆใ‚ข +fp32 ใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ ใƒ•ใ‚กใ‚คใƒซๅ†…ใฎใƒžใ‚นใ‚ฟใƒผใฎ้‡ใฟใ€‚ใ“ใ‚Œใฏ `global_step*/*optim_states.pt` (ใ“ใ‚Œใฏ glob +ใƒ‘ใ‚ฟใƒผใƒณ)ใ€้€šๅธธใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎไธ‹ใซไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚ + +**FP16 ใ‚ฆใ‚งใ‚คใƒˆ:** + +ใƒขใƒ‡ใƒซใ‚’ ZeRO-2 ใงไฟๅญ˜ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ๅซใ‚€้€šๅธธใฎ `pytorch_model.bin` ใƒ•ใ‚กใ‚คใƒซใŒไฝœๆˆใ•ใ‚Œใพใ™ใŒใ€ +ใ“ใ‚Œใ‚‰ใฏ้‡ใฟใฎ fp16 ใƒใƒผใ‚ธใƒงใƒณใซใ™ใŽใพใ›ใ‚“ใ€‚ + +ZeRO-3 ใงใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒ่ค‡ๆ•ฐใฎ GPU ใซๅˆ†ๅ‰ฒใ•ใ‚Œใ‚‹ใŸใ‚ใ€็Šถๆณใฏใ•ใ‚‰ใซ่ค‡้›‘ใซใชใ‚Šใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€fp16 ใ‚’ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใฎ `Trainer` ใ‚’ๅ–ๅพ—ใ™ใ‚‹ใซใฏใ€`"stage3_gather_16bit_weights_on_model_save": true` ใŒๅฟ…่ฆใงใ™ใ€‚ +้‡ใฟใฎใƒใƒผใ‚ธใƒงใƒณใ€‚ใ“ใฎ่จญๅฎšใŒ`False`ใฎๅ ดๅˆใ€`pytorch_model.bin`ใฏไฝœๆˆใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง DeepSpeed ใฎ `state_dict` ใซๅฎŸ้š›ใฎ้‡ใฟใงใฏใชใใƒ—ใƒฌใƒผใ‚นใƒ›ใƒซใƒ€ใƒผใŒๅซใพใ‚Œใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใฎ `state_dict` ใ‚’ไฟๅญ˜ใ—ใŸๅ ดๅˆใ€ใƒญใƒผใƒ‰ใ—็›ดใ™ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ + +```json +{ + "zero_optimization": { + "stage3_gather_16bit_weights_on_model_save": true + } +} +``` + +**FP32 ้‡้‡:** + +fp16 ใ‚ฆใ‚งใ‚คใƒˆใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ใฎใซ้ฉใ—ใฆใ„ใพใ™ใŒใ€ใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใŒๅฎŒไบ†ใ—ใ€ใใ‚Œใ‚’ +[ใƒขใƒ‡ใƒซ ใƒใƒ–](https://huggingface.co/models) ใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใ‹ใ€fp32 ใ‚’ๅ…ฅๆ‰‹ใ—ใŸใ„ใจๆ€ใ‚ใ‚Œใ‚‹ไป–ใฎไบบใซๆธกใ—ใพใ™ใ€‚ +้‡ใฟใ€‚ใ“ใ‚Œใฏๅคง้‡ใฎใƒกใƒขใƒชใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ‚ใ‚‹ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ่กŒใ†ในใใงใฏใชใ„ใฎใŒ็†ๆƒณ็š„ใงใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅฎŒไบ†ๅพŒใซใ‚ชใƒ•ใƒฉใ‚คใƒณใงๅฎŸ่กŒใ™ใ‚‹ใฎใŒๆœ€้ฉใงใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใ€็ฉบใ CPU ใŒๅๅˆ†ใซใ‚ใ‚‹ๅ ดๅˆใฏใ€ +ๅŒใ˜ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใงๅฎŸ่กŒใงใใ‚‹ใ“ใจใ‚’ๆ€ใ„ๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ไธกๆ–นใฎใ‚ขใƒ—ใƒญใƒผใƒใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + + +**ใƒฉใ‚คใƒ– FP32 ใ‚ฆใ‚งใ‚คใƒˆ ใƒชใ‚ซใƒใƒช:** + +ใƒขใƒ‡ใƒซใŒๅคงใใใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็ต‚ไบ†ๆ™‚ใซ็ฉบใ CPU ใƒกใƒขใƒชใŒใปใจใ‚“ใฉๆฎ‹ใฃใฆใ„ใชใ„ๅ ดๅˆใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏๆฉŸ่ƒฝใ—ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๅฐ‘ใชใใจใ‚‚ 1 ใคใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใฆใ„ใฆใ€ๆœ€ๆ–ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ + +```python +from transformers.trainer_utils import get_last_checkpoint +from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint + +checkpoint_dir = get_last_checkpoint(trainer.args.output_dir) +fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) +``` + +`--load_best_model_at_end` class:*~transformers.TrainingArguments* ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆ (ๆœ€้ฉใชใƒขใƒ‡ใƒซใ‚’่ฟฝ่ทกใ™ใ‚‹ใŸใ‚) +ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ)ใ€ๆœ€ๅˆใซๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚’ๆ˜Ž็คบ็š„ใซไฟๅญ˜ใ—ใฆใ‹ใ‚‰ใ€ไธŠ่จ˜ใจๅŒใ˜ใ“ใจใ‚’่กŒใ†ใ“ใจใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็ต‚ไบ†ใงใใพใ™ใ€‚ + +```python +from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint + +checkpoint_dir = os.path.join(trainer.args.output_dir, "checkpoint-final") +trainer.deepspeed.save_checkpoint(checkpoint_dir) +fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) +``` + + + +`load_state_dict_from_zero_checkpoint` ใŒๅฎŸ่กŒใ•ใ‚Œใ‚‹ใจใ€`model` ใฏใ‚‚ใฏใ‚„ไฝฟ็”จใงใใชใใชใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +ๅŒใ˜ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฎ DeepSpeed ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ€‚ใคใพใ‚Šใ€deepspeed ใ‚จใƒณใ‚ธใƒณใ‚’ๅ†ๅˆๆœŸๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +`model.load_state_dict(state_dict)` ใฏใใ“ใ‹ใ‚‰ใ™ในใฆใฎ DeepSpeed ใƒžใ‚ธใƒƒใ‚ฏใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใฏๆœ€ๅพŒใซใฎใฟๅฎŸ่กŒใ—ใฆใใ ใ•ใ„ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆง˜ๅญใ€‚ + + + + +ใ‚‚ใกใ‚ใ‚“ใ€class:*~transformers.Trainer* ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใชใใ€ไธŠ่จ˜ใฎไพ‹ใ‚’็‹ฌ่‡ชใฎใ‚‚ใฎใซ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒŠใƒผใ€‚ + +ไฝ•ใ‚‰ใ‹ใฎ็†็”ฑใงใ•ใ‚‰ใซๆ”น่‰ฏใ—ใŸใ„ๅ ดๅˆใฏใ€้‡ใฟใฎ fp32 `state_dict` ใ‚’ๆŠฝๅ‡บใ—ใฆ้ฉ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ๆฌกใฎไพ‹ใซ็คบใ™ใ‚ˆใ†ใซใ€ใ“ใ‚Œใ‚‰ใฏ่‡ชๅˆ†ใงไฝœๆˆใ—ใพใ™ใ€‚ + +```python +from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint + +state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu +model = model.cpu() +model.load_state_dict(state_dict) +``` + +**ใ‚ชใƒ•ใƒฉใ‚คใƒณ FP32 ใ‚ฆใ‚งใ‚คใƒˆ ใƒชใ‚ซใƒใƒช:** + +DeepSpeed ใฏ็‰นๅˆฅใชๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆ`zero_to_fp32.py`ใ‚’ไฝœๆˆใ—ใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎๆœ€ไธŠไฝใซ้…็ฝฎใ—ใพใ™ใ€‚ +ใƒ•ใ‚ฉใƒซใƒ€ใ€‚ใ“ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ„ใคใงใ‚‚้‡ใฟใ‚’ๆŠฝๅ‡บใงใใพใ™ใ€‚ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ‚นใ‚ฟใƒณใƒ‰ใ‚ขใƒญใƒณใชใฎใงใ€ใ‚‚ใ†ๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ๆŠฝๅ‡บใ‚’่กŒใ†ใŸใ‚ใฎ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใพใŸใฏ `Trainer` ใŒๅฟ…่ฆใงใ™ใ€‚ + +ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใƒ•ใ‚ฉใƒซใƒ€ใƒผใŒๆฌกใฎใ‚ˆใ†ใซใชใฃใฆใ„ใ‚‹ใจใ—ใพใ™ใ€‚ + +```bash +$ ls -l output_dir/checkpoint-1/ +-rw-rw-r-- 1 stas stas 1.4K Mar 27 20:42 config.json +drwxrwxr-x 2 stas stas 4.0K Mar 25 19:52 global_step1/ +-rw-rw-r-- 1 stas stas 12 Mar 27 13:16 latest +-rw-rw-r-- 1 stas stas 827K Mar 27 20:42 optimizer.pt +-rw-rw-r-- 1 stas stas 231M Mar 27 20:42 pytorch_model.bin +-rw-rw-r-- 1 stas stas 623 Mar 27 20:42 scheduler.pt +-rw-rw-r-- 1 stas stas 1.8K Mar 27 20:42 special_tokens_map.json +-rw-rw-r-- 1 stas stas 774K Mar 27 20:42 spiece.model +-rw-rw-r-- 1 stas stas 1.9K Mar 27 20:42 tokenizer_config.json +-rw-rw-r-- 1 stas stas 339 Mar 27 20:42 trainer_state.json +-rw-rw-r-- 1 stas stas 2.3K Mar 27 20:42 training_args.bin +-rwxrw-r-- 1 stas stas 5.5K Mar 27 13:16 zero_to_fp32.py* +``` + +ใ“ใฎไพ‹ใงใฏใ€DeepSpeed ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผ *global_step1* ใŒ 1 ใคใ ใ‘ใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€FP32ใ‚’ๅ†ๆง‹็ฏ‰ใ™ใ‚‹ใซใฏ +้‡ใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ ใ‘ใงใ™: + +```bash +python zero_to_fp32.py . pytorch_model.bin +``` + +ใ“ใ‚Œใ ใ‚ˆใ€‚ `pytorch_model.bin`ใซใฏใ€่ค‡ๆ•ฐใฎ GPU ใ‹ใ‚‰็ตฑๅˆใ•ใ‚ŒใŸๅฎŒๅ…จใช fp32 ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ZeRO-2 ใพใŸใฏ ZeRO-3 ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +`python zero_to_fp32.py -h` ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€ไฝฟ็”จๆ–นๆณ•ใฎ่ฉณ็ดฐใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ใƒ•ใ‚กใ‚คใƒซ`latest`ใฎๅ†…ๅฎนใ‚’ไฝฟ็”จใ—ใฆ deepspeed ใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผใ‚’่‡ชๅ‹•ๆคœๅ‡บใ—ใพใ™ใ€‚ +ไพ‹ใซใฏ`global_step1`ใŒๅซใพใ‚Œใพใ™ใ€‚ + +ๆณจ: ็พๅœจใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซใฏๆœ€็ต‚็š„ใช fp32 ใƒขใƒ‡ใƒซใฎ้‡ใฟใฎ 2 ๅ€ใฎไธ€่ˆฌ RAM ใŒๅฟ…่ฆใงใ™ใ€‚ + +### ZeRO-3 ใจ Infinity Nuances + +ZeRO-3 ใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆฉŸ่ƒฝใฎ็‚นใง ZeRO-2 ใจใฏๅคงใใ็•ฐใชใ‚Šใพใ™ใ€‚ + +ZeRO-Infinity ใฏ ZeRO-3 ใ‚’ใ•ใ‚‰ใซๆ‹กๅผตใ—ใ€NVMe ใƒกใƒขใƒชใ‚„ใใฎไป–ใฎ่ค‡ๆ•ฐใฎ้€Ÿๅบฆใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใฎๅ‘ไธŠใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใซ็‰นๅˆฅใชๅค‰ๆ›ดใ‚’ๅŠ ใˆใ‚‹ๅฟ…่ฆใŒใชใใฆใ‚‚ๆญฃๅธธใซๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใ‚ใ‚‰ใ‚†ใ‚‹ๅŠชๅŠ›ใŒๆ‰•ใ‚ใ‚Œใฆใใพใ—ใŸใŒใ€็‰นๅฎšใฎ็‚นใงใฏ +็Šถๆณใซใ‚ˆใฃใฆใฏใ€ๆฌกใฎๆƒ…ๅ ฑใŒๅฟ…่ฆใซใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +#### Constructing Massive Models + + +DeepSpeed/ZeRO-3 ใฏใ€ๆ—ขๅญ˜ใฎ RAM ใซๅŽใพใ‚‰ใชใ„ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ๆ•ฐๅ…†ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคใƒขใƒ‡ใƒซใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ใใฎใ‚ˆใ†ใชๅ ดๅˆใ€ +ใพใŸใ€ๅˆๆœŸๅŒ–ใ‚’ใ‚ˆใ‚Š้ซ˜้€ŸใซๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใฏใ€*deepspeed.zero.Init()* ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ +ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ ใƒžใƒใƒผใ‚ธใƒฃใƒผ (้–ขๆ•ฐใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใƒผใงใ‚‚ใ‚ใ‚Šใพใ™)ใ€‚ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +```python +from transformers import T5ForConditionalGeneration, T5Config +import deepspeed + +with deepspeed.zero.Init(): + config = T5Config.from_pretrained("google-t5/t5-small") + model = T5ForConditionalGeneration(config) +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใ€`model_class.from_pretrained` ใฏๆฌกใฎๆกไปถใ‚’ๆบ€ใŸใ™้™ใ‚Šใ“ใฎๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ +`is_deepspeed_zero3_enabled()` ใฏ `True` ใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ็พๅœจใ€ +[`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (ๆธกใ•ใ‚ŒใŸ DeepSpeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใซ ZeRO-3 ๆง‹ๆˆใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ) +ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ€‚ใ—ใŸใŒใฃใฆใ€ๅ‘ผใณๅ‡บใ—ใฎๅ‰ใซ** [`TrainingArguments`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +`from_pretrained`ใ€‚่€ƒใˆใ‚‰ใ‚Œใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎไพ‹ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```python +from transformers import AutoModel, Trainer, TrainingArguments + +training_args = TrainingArguments(..., deepspeed=ds_config) +model = AutoModel.from_pretrained("google-t5/t5-small") +trainer = Trainer(model=model, args=training_args, ...) +``` + +ๅ…ฌๅผใฎใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใฆใ€ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ `--deepspeed ds_config.json` ใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ +ZeRO-3 ่จญๅฎšใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใจใ€ใ“ใ‚ŒใŒใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎ่จ˜่ฟฐๆ–นๆณ•ใงใ‚ใ‚‹ใŸใ‚ใ€ใ™ในใฆใŒใ™ใงใซๅฎŒไบ†ใ—ใฆใ„ใพใ™ใ€‚ + +ๆณจ: ใƒขใƒ‡ใƒซใฎ fp16 ้‡ใฟใŒๅ˜ไธ€ใฎ GPU ใฎใƒกใƒขใƒชใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆใฏใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎๆ–นๆณ•ใจใใฎไป–ใฎ้–ข้€ฃๆฉŸ่ƒฝใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎๆง‹็ฏ‰](https://deepspeed.readthedocs.io/en/latest/zero3.html#constructing-massive-models) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใพใŸใ€fp16 ใงไบ‹ๅ‰่จ“็ทดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใฏใ€`from_pretrained` ใซไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +`torch_dtype=torch.float16`ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[from_pretrained-torch-dtype](#from_pretrained-torch-dtype) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +#### Gathering Parameters + +่ค‡ๆ•ฐใฎ GPU ไธŠใฎ ZeRO-3 ใงใฏใ€็พๅœจใฎ GPU ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใชใ„้™ใ‚Šใ€ๅ˜ไธ€ใฎ GPU ใŒใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ๅฎŸ่กŒๅฑคใ€‚ใ—ใŸใŒใฃใฆใ€ใ™ในใฆใฎใƒฌใ‚คใƒคใƒผใฎใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใซไธ€ๅบฆใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใใ‚Œใ‚’่กŒใ†ใŸใ‚ใฎ็‰นๅฎšใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ +ใปใจใ‚“ใฉใฎๅ ดๅˆใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅฟ…่ฆใชๅ ดๅˆใฏใ€[ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅŽ้›†](https://deepspeed.readthedocs.io/en/latest/zero3.html#manual-parameter-coordination) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใŸใ ใ—ใ€ใ„ใใคใ‹ใฎๅ ดๆ‰€ใงๅ†…้ƒจ็š„ใซไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใใฎไพ‹ใฎ 1 ใคใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใงใ™ใ€‚ +`from_pretrained`ใ€‚ไธ€ๅบฆใซ 1 ใคใฎใƒฌใ‚คใƒคใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใ€ๅ‚ๅŠ ใ—ใฆใ„ใ‚‹ใ™ในใฆใฎ GPU ใซๅณๅบงใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ +ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใฏใ€ใƒกใƒขใƒชใฎ้–ขไฟ‚ใงใ€1 ใคใฎ GPU ใซใƒญใƒผใƒ‰ใ—ใฆใ‹ใ‚‰่ค‡ๆ•ฐใฎ GPU ใซๅˆ†ๆ•ฃใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ +ๅˆถ้™ใ€‚ + +ใพใŸใ€ZeRO-3 ใงใฏใ€็‹ฌ่‡ชใฎใ‚ณใƒผใƒ‰ใ‚’ไฝœๆˆใ—ใ€ๆฌกใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ้‡ใฟใŒ็™บ็”Ÿใ™ใ‚‹ใจใ—ใพใ™ใ€‚ + +```python +tensor([1.0], device="cuda:0", dtype=torch.float16, requires_grad=True) +``` + +`tensor([1.])` ใซใ‚นใƒˆใƒฌใ‚นใ‚’ๆ„Ÿใ˜ใŸๅ ดๅˆใ€ใพใŸใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎใ‚ตใ‚คใ‚บใŒ `1` ใงใ‚ใ‚‹ใจใ„ใ†ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆ +ใ‚ˆใ‚Šๅคงใใชๅคšๆฌกๅ…ƒๅฝข็Šถใ€‚ใ“ใ‚Œใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒๅˆ†ๅ‰ฒใ•ใ‚ŒใฆใŠใ‚Šใ€่กจ็คบใ•ใ‚Œใ‚‹ใฎใฏ ZeRO-3 ใƒ—ใƒฌใƒผใ‚นใƒ›ใƒซใƒ€ใƒผใงใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + + + + +### ZeRO Inference + +ZeRO Inference ใฏใ€ZeRO-3 Training ใจๅŒใ˜ๆง‹ๆˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใง +ๅฎŸ้š›ใ€ๅŒใ˜ใ‚‚ใฎใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๅ…ฑๆœ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚‰ใ‚’่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซๆฎ‹ใ™ใ“ใจใŒใงใใพใ™ใ€‚ๅฝผใ‚‰ใฏใŸใ ใใ†ใชใ‚‹ใ ใ‚ใ† +็„ก่ฆ–ใ•ใ‚Œใพใ—ใŸใ€‚ + +ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏใ€้€šๅธธใฎ [`TrainingArguments`] ๅผ•ๆ•ฐใ‚’ๆธกใ™ใ ใ‘ใงใ™ใ€‚ไพ‹ใˆใฐ๏ผš + +```bash +deepspeed --num_gpus=2 your_program.py --do_eval --deepspeed ds_config.json +``` + +ๅ”ฏไธ€้‡่ฆใชใ“ใจใฏใ€ZeRO-2 ใซใฏไฝ•ใฎๅˆฉ็‚นใ‚‚ใชใ„ใŸใ‚ใ€ZeRO-3 ๆง‹ๆˆใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ +ZeRO-3 ใฎใฟใŒใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ™ใ‚‹ใฎใซๅฏพใ—ใ€ZeRO-1 ใฏๅ‹พ้…ใจใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใ‚’ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ๅˆฉ็”จๅฏ่ƒฝใชใ™ในใฆใฎ GPU ใ‚’ใƒ‡ใƒ—ใƒญใ‚คใ™ใ‚‹ DeepSpeed ใง`run_translation.py`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไพ‹ใงใ™ใ€‚ + + +```bash +deepspeed examples/pytorch/translation/run_translation.py \ +--deepspeed tests/deepspeed/ds_config_zero3.json \ +--model_name_or_path google-t5/t5-small --output_dir output_dir \ +--do_eval --max_eval_samples 50 --warmup_steps 50 \ +--max_source_length 128 --val_max_target_length 128 \ +--overwrite_output_dir --per_device_eval_batch_size 4 \ +--predict_with_generate --dataset_config "ro-en" --fp16 \ +--source_lang en --target_lang ro --dataset_name wmt16 \ +--source_prefix "translate English to Romanian: " +``` + +ๆŽจ่ซ–ใฎใŸใ‚ใซใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใจๅ‹พ้…ใซใ‚ˆใฃใฆไฝฟ็”จใ•ใ‚Œใ‚‹่ฟฝๅŠ ใฎๅคงใใชใƒกใƒขใƒชใฏๅฟ…่ฆใชใ„ใŸใ‚ใ€ +ใฏใ‚‹ใ‹ใซๅคงใใชใƒใƒƒใƒใ‚„ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’ๅŒใ˜ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซ้ฉๅˆใงใใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ•ใ‚‰ใซใ€DeepSpeed ใฏ็พๅœจใ€Deepspeed-Inference ใจๅ‘ผใฐใ‚Œใ‚‹้–ข้€ฃ่ฃฝๅ“ใ‚’้–‹็™บใ—ใฆใ„ใพใ™ใŒใ€ใ“ใ‚Œใจใฏไฝ•ใฎ้–ขไฟ‚ใ‚‚ใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ZeRO ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใซๆบ–ๆ‹ ใ—ใฆใ„ใพใ™ใŒใ€ไปฃใ‚ใ‚Šใซใƒ†ใƒณใ‚ฝใƒซไธฆๅˆ—ๅ‡ฆ็†ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ˜ไธ€ใฎ GPU ใซๅŽใพใ‚‰ใชใ„ใƒขใƒ‡ใƒซใ‚’ใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ +็พๅœจ้–‹็™บไธญใงใ™ใ€‚่ฃฝๅ“ใŒๅฎŒๆˆใ—ใŸใ‚‰็ตฑๅˆใ‚’ๆไพ›ใ™ใ‚‹ไบˆๅฎšใงใ™ใ€‚ + + +### Memory Requirements + +Deepspeed ZeRO ใฏใƒกใƒขใƒชใ‚’ CPU (ใŠใ‚ˆใณ NVMe) ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใ‚‹ใŸใ‚ใ€ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฏใ€ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ GPU ใฎๆ•ฐใซๅฟœใ˜ใฆๅฟ…่ฆใช CPU ใŠใ‚ˆใณ GPU ใƒกใƒขใƒชใฎ้‡ใ‚’็Ÿฅใ‚‹ใ“ใจใŒใงใใ‚‹ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +ๅ˜ไธ€ใฎ GPU ใง `bigscience/T0_3B`ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใƒกใƒขใƒชใฎ้‡ใ‚’่ฆ‹็ฉใ‚‚ใฃใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```bash +$ python -c 'from transformers import AutoModel; \ +from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live; \ +model = AutoModel.from_pretrained("bigscience/T0_3B"); \ +estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=1, num_nodes=1)' +[...] +Estimated memory needed for params, optim states and gradients for a: +HW: Setup with 1 node, 1 GPU per node. +SW: Model with 2783M total params, 65M largest layer params. + per CPU | per GPU | Options + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=1 + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=0 + 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=1 + 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=0 + 0.37GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=1 + 15.56GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=0 +``` + +ใ—ใŸใŒใฃใฆใ€ๅ˜ไธ€ใฎ 80 GB GPU ใง CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใชใ—ใงๆญ่ผ‰ใ™ใ‚‹ใ“ใจใ‚‚ใ€ๅฐใ•ใช 8 GB GPU ใงใ‚‚ๆœ€ๅคง 60 GB ใฎ CPU ใƒกใƒขใƒชใŒๅฟ…่ฆใซใชใ‚‹ใ“ใจใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ (ใ“ใ‚Œใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใ€ใŠใ‚ˆใณๅ‹พ้…ใฎใŸใ‚ใฎใƒกใƒขใƒชใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚cuda ใ‚ซใƒผใƒใƒซใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ€ใŠใ‚ˆใณไธ€ๆ™‚ใƒกใƒขใƒชใซใฏใ‚‚ใ†ๅฐ‘ใ—ๅคšใใฎใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™ใ€‚) + +ๆฌกใซใ€ใ‚ณใ‚นใƒˆใจ้€Ÿๅบฆใฎใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใซใชใ‚Šใพใ™ใ€‚ใ‚ˆใ‚Šๅฐใ•ใ„ GPU ใ‚’่ณผๅ…ฅใพใŸใฏใƒฌใƒณใ‚ฟใƒซใ—ใŸๆ–นใŒๅฎ‰ใใชใ‚Šใพใ™ (Deepspeed ZeRO ใงใฏ่ค‡ๆ•ฐใฎ GPU ใ‚’ไฝฟ็”จใงใใ‚‹ใŸใ‚ใ€GPU ใฎๆ•ฐใ‚’ๆธ›ใ‚‰ใ™ใ“ใจใ‚‚ใงใใพใ™)ใ€‚ใ—ใ‹ใ—ใ€ใใฎๅ ดๅˆใฏ้…ใใชใ‚Šใพใ™ใ€‚ใใฎใŸใ‚ใ€ไฝ•ใ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹้€Ÿๅบฆใ‚’ๆฐ—ใซใ—ใชใใฆใ‚‚ใ€้€ŸๅบฆใฎไฝŽไธ‹ใฏ GPU ใฎไฝฟ็”จๆ™‚้–“ใซ็›ดๆŽฅๅฝฑ้Ÿฟใ—ใ€ใ‚ณใ‚นใƒˆใŒๅข—ๅคงใ™ใ‚‹ใŸใ‚ใ€ใฉใ‚ŒใŒๆœ€ใ‚‚ๅŠนๆžœ็š„ใ‹ใ‚’ๅฎŸ้จ“ใ—ใฆๆฏ”่ผƒใ—ใฆใใ ใ•ใ„ใ€‚ + +ๅๅˆ†ใช GPU ใƒกใƒขใƒชใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใ™ในใฆใŒ้ซ˜้€Ÿใซใชใ‚‹ใŸใ‚ใ€CPU/NVMe ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’ๅฟ…ใš็„กๅŠนใซใ—ใฆใใ ใ•ใ„ใ€‚ + +ใŸใจใˆใฐใ€2 ใคใฎ GPU ใซๅฏพใ—ใฆๅŒใ˜ใ“ใจใ‚’็นฐใ‚Š่ฟ”ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```bash +$ python -c 'from transformers import AutoModel; \ +from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live; \ +model = AutoModel.from_pretrained("bigscience/T0_3B"); \ +estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=2, num_nodes=1)' +[...] +Estimated memory needed for params, optim states and gradients for a: +HW: Setup with 1 node, 2 GPUs per node. +SW: Model with 2783M total params, 65M largest layer params. + per CPU | per GPU | Options + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=1 + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=0 + 62.23GB | 2.84GB | offload_param=none, offload_optimizer=cpu , zero_init=1 + 62.23GB | 2.84GB | offload_param=none, offload_optimizer=cpu , zero_init=0 + 0.74GB | 23.58GB | offload_param=none, offload_optimizer=none, zero_init=1 + 31.11GB | 23.58GB | offload_param=none, offload_optimizer=none, zero_init=0 + +``` + +ใ—ใŸใŒใฃใฆใ€ใ“ใ“ใงใฏใ€CPU ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ›ใšใซ 2x 32GB ไปฅไธŠใฎ GPU ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒกใƒขใƒชๆŽจๅฎšใƒ„ใƒผใƒซ](https://deepspeed.readthedocs.io/en/latest/memory.html) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +### Filing Issues + + +ใ“ใ“ใงใฏใ€ๅ•้กŒใฎ็œŸ็›ธใ‚’ใ™ใใซ่งฃๆ˜Žใ—ใ€ไฝœๆฅญใฎใƒ–ใƒญใƒƒใ‚ฏใ‚’่งฃ้™คใงใใ‚‹ใ‚ˆใ†ใ€ๅ•้กŒใ‚’ๅ ฑๅ‘Šใ™ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ใƒฌใƒใƒผใƒˆใซใฏๅฟ…ใšๆฌกใฎๅ†…ๅฎนใ‚’ๅซใ‚ใฆใใ ใ•ใ„ใ€‚ + +1. ใƒฌใƒใƒผใƒˆๅ†…ใฎๅฎŒๅ…จใช Deepspeed ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ + +2. [`Trainer`] ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใ€ใพใŸใฏ + ใƒˆใƒฌใƒผใƒŠใƒผใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’่‡ชๅˆ†ใงใ‚นใ‚ฏใƒชใƒ—ใƒˆไฝœๆˆใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€[`TrainingArguments`] ๅผ•ๆ•ฐใ€‚ใ—ใชใ„ใงใใ ใ•ใ„ + [`TrainingArguments`] ใซใฏ็„ก้–ขไฟ‚ใชใ‚จใƒณใƒˆใƒชใŒๅคšๆ•ฐๅซใพใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใƒ€ใƒณใƒ—ใ—ใพใ™ใ€‚ + +3. ๆฌกใฎๅ‡บๅŠ›: + + ```bash + python -c 'import torch; print(f"torch: {torch.__version__}")' + python -c 'import transformers; print(f"transformers: {transformers.__version__}")' + python -c 'import deepspeed; print(f"deepspeed: {deepspeed.__version__}")' + ``` + +4. ๅฏ่ƒฝใงใ‚ใ‚Œใฐใ€ๅ•้กŒใ‚’ๅ†็พใงใใ‚‹ Google Colab ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใธใฎใƒชใƒณใ‚ฏใ‚’ๅซใ‚ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใ‚’ไฝฟใˆใพใ™ + [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/stas00/porting/blob/master/transformers/deepspeed/DeepSpeed_on_colab_CLI.ipynb) ใจใ—ใฆ + ๅ‡บ็™บ็‚นใ€‚ + +5. ไธๅฏ่ƒฝใงใชใ„้™ใ‚Šใ€ใ‚ซใ‚นใ‚ฟใƒ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฏใชใใ€ๅธธใซไฝฟ็”จใงใใ‚‹ๆจ™ๆบ–ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + +6. ๅฏ่ƒฝใงใ‚ใ‚Œใฐใ€ๆ—ขๅญ˜ใฎ [ใ‚ตใƒณใƒ—ใƒซ](https://github.com/huggingface/transformers/tree/main/examples/pytorch) ใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใ—ใฆๅ•้กŒใ‚’ๅ†็พใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +- Deepspeed ใŒๅ•้กŒใฎๅŽŸๅ› ใงใฏใชใ„ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ + + ๆๅ‡บใ•ใ‚ŒใŸๅ•้กŒใฎไธ€้ƒจใฏใ€Deepspeed ใจใฏ็„ก้–ขไฟ‚ใงใ‚ใ‚‹ใ“ใจใŒๅˆคๆ˜Žใ—ใพใ—ใŸใ€‚ใใ‚Œใฏใ€Deepspeed ใŒใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‹ใ‚‰ๅ‰Š้™คใ•ใ‚ŒใŸๅพŒใงใ™ใ€‚ + ๅ•้กŒใฏใพใ ๆฎ‹ใฃใฆใ„ใŸใ€‚ + + ใ—ใŸใŒใฃใฆใ€ๅฎŒๅ…จใซๆ˜Ž็™ฝใงใชใ„ๅ ดๅˆใฏใ€DeepSpeed ้–ข้€ฃใฎๅ•้กŒใงใ™ใ€‚ + ไพ‹ๅค–ใŒ็™บ็”Ÿใ—ใ€DeepSpeed ใƒขใ‚ธใƒฅใƒผใƒซใŒ้–ขไฟ‚ใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใพใšใ€DeepSpeed ใ‚’ๅซใพใชใ„ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’ๅ†ใƒ†ใ‚นใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ + ๅ•้กŒใŒ่งฃๆฑบใ—ใชใ„ๅ ดๅˆใซใฎใฟใ€Deepspeed ใซใคใ„ใฆ่จ€ๅŠใ—ใ€ๅฟ…่ฆใช่ฉณ็ดฐใ‚’ใ™ในใฆๆไพ›ใ—ใฆใใ ใ•ใ„ใ€‚ + +- ๅ•้กŒใŒ็ตฑๅˆ้ƒจๅˆ†ใงใฏใชใ DeepSpeed ใ‚ณใ‚ขใซใ‚ใ‚‹ใ“ใจใŒๆ˜Žใ‚‰ใ‹ใชๅ ดๅˆใฏใ€ๅ•้กŒใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ + [Deepspeed](https://github.com/deepspeedai/DeepSpeed/) ใ‚’็›ดๆŽฅไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ˆใใ‚ใ‹ใ‚‰ใชใ„ๅ ดๅˆใงใ‚‚ใ€ใ”ๅฎ‰ๅฟƒใใ ใ•ใ„ใ€‚ + ใฉใกใ‚‰ใฎๅ•้กŒใƒˆใƒฉใƒƒใ‚ซใƒผใงใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆŠ•็จฟใ•ใ‚ŒใŸใ‚‰ใใ‚Œใ‚’ๅˆคๆ–ญใ—ใ€ๆฌกใฎๅ ดๅˆใฏๅˆฅใฎๅ•้กŒใƒˆใƒฉใƒƒใ‚ซใƒผใซใƒชใƒ€ใ‚คใƒฌใ‚ฏใƒˆใ—ใพใ™ใ€‚ + ใใ†ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ€‚ + + +### Troubleshooting + +#### the `deepspeed` process gets killed at startup without a traceback + +`deepspeed`ใƒ—ใƒญใ‚ปใ‚นใŒ่ตทๅ‹•ๆ™‚ใซใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใชใ—ใงๅผทๅˆถ็ต‚ไบ†ใ•ใ‚ŒใŸๅ ดๅˆใ€ใใ‚Œใฏ้€šๅธธใ€ใƒ—ใƒญใ‚ฐใƒฉใƒ ใŒ่ฉฆ่กŒใ—ใŸใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ใ‚ทใ‚นใƒ†ใƒ ใŒๆŒใฃใฆใ„ใ‚‹ใ‚ˆใ‚Šใ‚‚ๅคšใใฎ CPU ใƒกใƒขใƒชใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ใ‹ใ€ใƒ—ใƒญใ‚ปใ‚นใŒๅ‰ฒใ‚Šๅฝ“ใฆใ‚’่จฑๅฏใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€OS ใ‚ซใƒผใƒใƒซใŒใใ‚Œใ‚’ๅผทๅˆถ็ต‚ไบ†ใ—ใพใ™ใ€‚ +ใƒ—ใƒญใ‚ปใ‚นใ€‚ใ“ใ‚Œใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซ `offload_optimizer` ใพใŸใฏ `offload_param` ใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใŸใ‚ใงใ™ใ€‚ +ใฉใกใ‚‰ใ‚‚`cpu`ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ‚ˆใ†ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใพใ™ใ€‚ NVMe ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎ็’ฐๅขƒใงๅฎŸ่กŒใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏ NVMe ใธใฎใ‚ชใƒ•ใƒญใƒผใƒ‰ใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ +ใ‚ผใƒญ-3ใ€‚ [็‰นๅฎšใฎใƒขใƒ‡ใƒซใซๅฟ…่ฆใชใƒกใƒขใƒช้‡ใ‚’่ฆ‹็ฉใ‚‚ใ‚‹]ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™(https://deepspeed.readthedocs.io/en/latest/memory.html)ใ€‚ + +#### training and/or eval/predict loss is `NaN` + +ใ“ใ‚Œใฏใ€bf16 ๆททๅˆ็ฒพๅบฆใƒขใƒผใƒ‰ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅ–ๅพ—ใ—ใ€ใใ‚Œใ‚’ fp16 (ๆททๅˆ็ฒพๅบฆใฎๆœ‰็„กใซใ‹ใ‹ใ‚ใ‚‰ใš) ใงไฝฟ็”จใ—ใ‚ˆใ†ใจใ—ใŸๅ ดๅˆใซใ‚ˆใ็™บ็”Ÿใ—ใพใ™ใ€‚ TPU ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใ€ใŠใ‚ˆใณๅคšใใฎๅ ดๅˆใ€Google ใซใ‚ˆใฃใฆใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€ใ“ใฎใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใ•ใ‚Œใพใ™ (ใŸใจใˆใฐใ€ใปใผใ™ในใฆใฎ t5 ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซ)ใ€‚ใ“ใ“ใงใฎ่งฃๆฑบ็ญ–ใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใŒใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ๅ ดๅˆ (TPUใ€Ampere GPU ไปฅ้™)ใ€fp32 ใพใŸใฏ bf16 ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + } +} +``` + +ใƒญใ‚ฐใซใฏใ€Deepspeed ใŒๆฌกใฎใ‚ˆใ†ใซ`OVERFLOW!`ใ‚’ๅ ฑๅ‘Šใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +``` +0%| | 0/189 [00:00=4.28` ไปฅ้™ใ€`synced_gpus` ใŒๆ˜Ž็คบ็š„ใซๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใ“ใ‚Œใ‚‰ใฎๆกไปถใŒๆคœๅ‡บใ•ใ‚Œใ‚‹ใจ่‡ชๅ‹•็š„ใซ `True` ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆ `synced_gpus` ใฎๅ€คใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +## Deepspeed ็ตฑๅˆใฎใƒ†ใ‚นใƒˆ + +DeepSpeed ็ตฑๅˆใ‚’ๅซใ‚€ PR ใ‚’้€ไฟกใ™ใ‚‹ๅ ดๅˆใฏใ€CircleCI PR CI ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใซใฏ GPU ใŒใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใใฎใŸใ‚ใ€GPU ใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใƒ†ใ‚นใƒˆใฏๅˆฅใฎ CI ใงๆฏŽๆ™ฉใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€PR ใง็ท‘่‰ฒใฎ CI ใƒฌใƒใƒผใƒˆใŒ่กจ็คบใ•ใ‚Œใฆใ‚‚ใ€DeepSpeed ใƒ†ใ‚นใƒˆใŒๅˆๆ ผใ—ใŸใ“ใจใ‚’ๆ„ๅ‘ณใ™ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +DeepSpeed ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๅฐ‘ใชใใจใ‚‚ไปฅไธ‹ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +RUN_SLOW=1 pytest tests/deepspeed/test_deepspeed.py +``` + +ใƒขใƒ‡ใƒชใƒณใ‚ฐใพใŸใฏ pytorch ใ‚ตใƒณใƒ—ใƒซ ใ‚ณใƒผใƒ‰ใฎใ„ใšใ‚Œใ‹ใ‚’ๅค‰ๆ›ดใ—ใŸๅ ดๅˆใฏใ€Model Zoo ใƒ†ใ‚นใƒˆใ‚‚ๅฎŸ่กŒใ—ใพใ™ใ€‚ไปฅไธ‹ใฏใ™ในใฆใฎ DeepSpeed ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +RUN_SLOW=1 pytest tests/deepspeed +``` + + +## Main DeepSpeed Resources + +- [ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใฎ github](https://github.com/deepspeedai/DeepSpeed) +- [ไฝฟ็”จๆ–นๆณ•ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.deepspeed.ai/getting-started/) +- [API ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://deepspeed.readthedocs.io/en/latest/index.html) +- [ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://www.microsoft.com/en-us/research/search/?q=deepspeed) + +่ซ–ๆ–‡: + +- [ZeRO: ๅ…†ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅ‘ใ‘ใŸใƒกใƒขใƒชใฎๆœ€้ฉๅŒ–](https://huggingface.co/papers/1910.02054) +- [ZeRO-Offload: 10 ๅ„„่ฆๆจกใฎใƒขใƒ‡ใƒซ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆฐ‘ไธปๅŒ–](https://huggingface.co/papers/2101.06840) +- [ZeRO-Infinity: ๆฅต้™ใ‚นใ‚ฑใƒผใƒซใฎๆทฑๅฑคๅญฆ็ฟ’ใฎใŸใ‚ใฎ GPU ใƒกใƒขใƒชใฎๅฃใ‚’ๆ‰“ใก็ ดใ‚‹](https://huggingface.co/papers/2104.07857) + +ๆœ€ๅพŒใซใ€HuggingFace [`Trainer`] ใฏ DeepSpeed ใฎใฟใ‚’็ตฑๅˆใ—ใฆใ„ใ‚‹ใ“ใจใ‚’่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ +DeepSpeed ใฎไฝฟ็”จใซ้–ขใ—ใฆๅ•้กŒใ‚„่ณชๅ•ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[DeepSpeed GitHub](https://github.com/deepspeedai/DeepSpeed/issues) ใซๅ•้กŒใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/feature_extractor.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/feature_extractor.md new file mode 100644 index 0000000000000000000000000000000000000000..a2bd8c59a84f35f5a2b6bf131bb5eba4ccb2c677 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/feature_extractor.md @@ -0,0 +1,41 @@ + + +# Feature Extractor + + +ใƒ•ใ‚ฃใƒผใƒใƒฃใƒผใ‚จใ‚ฏใ‚นใƒˆใƒฉใ‚ฏใ‚ฟใฏใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใพใŸใฏใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใฎใŸใ‚ใฎๅ…ฅๅŠ›ใƒ•ใ‚ฃใƒผใƒใƒฃใƒผใฎๆบ–ๅ‚™ใ‚’ๆ‹…ๅฝ“ใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‹ใ‚‰ใฎใƒ•ใ‚ฃใƒผใƒใƒฃใƒผๆŠฝๅ‡บ๏ผˆไพ‹๏ผšใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใฎๅ‰ๅ‡ฆ็†ใ‹ใ‚‰Log-Melใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใƒ•ใ‚ฃใƒผใƒใƒฃใƒผใธใฎๅค‰ๆ›๏ผ‰ใ€็”ปๅƒใ‹ใ‚‰ใฎใƒ•ใ‚ฃใƒผใƒใƒฃใƒผๆŠฝๅ‡บ๏ผˆไพ‹๏ผš็”ปๅƒใƒ•ใ‚กใ‚คใƒซใฎใ‚ฏใƒญใƒƒใƒ”ใƒณใ‚ฐ๏ผ‰ใ€ใพใŸใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ€ๆญฃ่ฆๅŒ–ใ€ใใ—ใฆNumpyใ€PyTorchใ€TensorFlowใƒ†ใƒณใ‚ฝใƒซใธใฎๅค‰ๆ›ใ‚‚ๅซใพใ‚Œใพใ™ใ€‚ + + +## FeatureExtractionMixin + +[[autodoc]] feature_extraction_utils.FeatureExtractionMixin + - from_pretrained + - save_pretrained + +## SequenceFeatureExtractor + +[[autodoc]] SequenceFeatureExtractor + - pad + +## BatchFeature + +[[autodoc]] BatchFeature + +## ImageFeatureExtractionMixin + +[[autodoc]] image_utils.ImageFeatureExtractionMixin diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/image_processor.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/image_processor.md new file mode 100644 index 0000000000000000000000000000000000000000..bfd33b83c2e50b8f41ee0e6584ee512a1be45bbc --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/image_processor.md @@ -0,0 +1,33 @@ + + +# Image Processor + +็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ใƒ“ใ‚ธใƒงใƒณ ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›็‰นๅพดใฎๆบ–ๅ‚™ใจใใฎๅ‡บๅŠ›ใฎๅพŒๅ‡ฆ็†ใ‚’ๆ‹…ๅฝ“ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ€ๆญฃ่ฆๅŒ–ใ€PyTorchใ€TensorFlowใ€Flaxใ€Numpy ใƒ†ใƒณใ‚ฝใƒซใธใฎๅค‰ๆ›ใชใฉใฎๅค‰ๆ›ใŒๅซใพใ‚Œใพใ™ใ€‚ใƒญใ‚ธใƒƒใƒˆใ‚’ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒžใ‚นใ‚ฏใซๅค‰ๆ›ใ™ใ‚‹ใชใฉใ€ใƒขใƒ‡ใƒซๅ›บๆœ‰ใฎๅพŒๅ‡ฆ็†ใ‚‚ๅซใพใ‚Œใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## ImageProcessingMixin + +[[autodoc]] image_processing_utils.ImageProcessingMixin + - from_pretrained + - save_pretrained + +## BatchFeature + +[[autodoc]] BatchFeature + +## BaseImageProcessor + +[[autodoc]] image_processing_utils.BaseImageProcessor diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/keras_callbacks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/keras_callbacks.md new file mode 100644 index 0000000000000000000000000000000000000000..ff28107a4345795ff04e11ffec7f76a93561635a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/keras_callbacks.md @@ -0,0 +1,28 @@ + + +# Keras callbacks + +Keras ใ‚’ไฝฟ็”จใ—ใฆ Transformers ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ไธ€่ˆฌ็š„ใชๅ‡ฆ็†ใ‚’่‡ชๅ‹•ๅŒ–ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชๅ›บๆœ‰ใฎใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ฟใ‚นใ‚ฏ: + +## KerasMetricCallback + +[[autodoc]] KerasMetricCallback + +## PushToHubCallback + +[[autodoc]] PushToHubCallback diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/logging.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/logging.md new file mode 100644 index 0000000000000000000000000000000000000000..4b4f4a2a3e0940e353ffa7f08f9bba842cbbfbc3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/logging.md @@ -0,0 +1,121 @@ + + +# Logging + +๐Ÿค— Transformersใซใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎ่ฉณ็ดฐๅบฆใ‚’็ฐกๅ˜ใซ่จญๅฎšใงใใ‚‹ไธญๅคฎ้›†ไธญๅž‹ใฎใƒญใ‚ฎใƒณใ‚ฐใ‚ทใ‚นใƒ†ใƒ ใŒใ‚ใ‚Šใพใ™ใ€‚ + +็พๅœจใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ่ฉณ็ดฐๅบฆใฏใ€ŒWARNINGใ€ใงใ™ใ€‚ + +่ฉณ็ดฐๅบฆใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใซใฏใ€็›ดๆŽฅ่จญๅฎšใƒกใ‚ฝใƒƒใƒ‰ใฎ1ใคใ‚’ไฝฟ็”จใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ไพ‹ใˆใฐใ€่ฉณ็ดฐๅบฆใ‚’INFOใƒฌใƒ™ใƒซใซๅค‰ๆ›ดใ™ใ‚‹ๆ–นๆณ•ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™ใ€‚ + + +```python +import transformers + +transformers.logging.set_verbosity_info() +``` + + +็’ฐๅขƒๅค‰ๆ•ฐ `TRANSFORMERS_VERBOSITY` ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ†—้•ทๆ€งใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚่จญๅฎšใงใใพใ™ +`debug`ใ€`info`ใ€`warning`ใ€`error`ใ€`critical` ใฎใ„ใšใ‚Œใ‹ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ไพ‹ใˆใฐ๏ผš + +```bash +TRANSFORMERS_VERBOSITY=error ./myprogram.py +``` + + +ใ•ใ‚‰ใซใ€ไธ€้ƒจใฎใ€Œ่ญฆๅ‘Šใ€ใฏ็’ฐๅขƒๅค‰ๆ•ฐใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใง็„กๅŠนใซใงใใพใ™ใ€‚ +`TRANSFORMERS_NO_ADVISORY_WARNINGS` ใ‚’ *1* ใชใฉใฎ true ๅ€คใซ่จญๅฎšใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆฌกใ‚’ไฝฟ็”จใ—ใฆใƒญใ‚ฐใซ่จ˜้Œฒใ•ใ‚Œใ‚‹่ญฆๅ‘ŠใŒ็„กๅŠนใซใชใ‚Šใพใ™ใ€‚ +[`logger.warning_advice`]ใ€‚ไพ‹ใˆใฐ๏ผš + +```bash +TRANSFORMERS_NO_ADVISORY_WARNINGS=1 ./myprogram.py +``` + + +ไปฅไธ‹ใฏใ€็‹ฌ่‡ชใฎใƒขใ‚ธใƒฅใƒผใƒซใพใŸใฏใ‚นใ‚ฏใƒชใƒ—ใƒˆใงใƒฉใ‚คใƒ–ใƒฉใƒชใจๅŒใ˜ใƒญใ‚ฌใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใงใ™ใ€‚ + +```python +from transformers.utils import logging + +logging.set_verbosity_info() +logger = logging.get_logger("transformers") +logger.info("INFO") +logger.warning("WARN") +``` + +ใ“ใฎใƒญใ‚ฎใƒณใ‚ฐ ใƒขใ‚ธใƒฅใƒผใƒซใฎใ™ในใฆใฎใƒกใ‚ฝใƒƒใƒ‰ใฏไปฅไธ‹ใซๆ–‡ๆ›ธๅŒ–ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไธปใชใƒกใ‚ฝใƒƒใƒ‰ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +[`logging.get_verbosity`] ใƒญใ‚ฌใƒผใฎ็พๅœจใฎๅ†—้•ทใƒฌใƒ™ใƒซใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ +[`logging.set_verbosity`] ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ†—้•ทๆ€งใ‚’้ธๆŠžใ—ใŸใƒฌใƒ™ใƒซใซ่จญๅฎšใ—ใพใ™ใ€‚้ †็•ชใซ๏ผˆๅฐ‘ใชใ„ใ‚‚ใฎใ‹ใ‚‰๏ผ‰ +ๅ†—้•ทใ‹ใ‚‰ๆœ€ใ‚‚ๅ†—้•ทใพใง)ใ€ใใ‚Œใ‚‰ใฎใƒฌใƒ™ใƒซ (ๆ‹ฌๅผงๅ†…ใฏๅฏพๅฟœใ™ใ‚‹ int ๅ€ค) ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- `transformers.logging.CRITICAL` ใพใŸใฏ `transformers.logging.FATAL` (int ๅ€คใ€50): ๆœ€ใ‚‚ๅคšใ„ใ‚‚ใฎใฎใฟใ‚’ใƒฌใƒใƒผใƒˆใ—ใพใ™ใ€‚ + ้‡ๅคงใชใ‚จใƒฉใƒผใ€‚ +- `transformers.logging.ERROR` (int ๅ€คใ€40): ใ‚จใƒฉใƒผใฎใฟใ‚’ๅ ฑๅ‘Šใ—ใพใ™ใ€‚ +- `transformers.logging.WARNING` ใพใŸใฏ `transformers.logging.WARN` (int ๅ€คใ€30): ใ‚จใƒฉใƒผใจ + ่ญฆๅ‘Šใ€‚ใ“ใ‚Œใฏใƒฉใ‚คใƒ–ใƒฉใƒชใงไฝฟ็”จใ•ใ‚Œใ‚‹ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒฌใƒ™ใƒซใงใ™ใ€‚ +- `transformers.logging.INFO` (int ๅ€คใ€20): ใ‚จใƒฉใƒผใ€่ญฆๅ‘Šใ€ใŠใ‚ˆใณๅŸบๆœฌๆƒ…ๅ ฑใ‚’ใƒฌใƒใƒผใƒˆใ—ใพใ™ใ€‚ +- `transformers.logging.DEBUG` (int ๅ€คใ€10): ใ™ในใฆใฎๆƒ…ๅ ฑใ‚’ใƒฌใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใƒขใƒ‡ใƒซใฎใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ไธญใซใ€Œtqdmใ€้€ฒ่กŒ็ŠถๆณใƒใƒผใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ [`logging.disable_progress_bar`] ใŠใ‚ˆใณ [`logging.enable_progress_bar`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใ“ใฎๅ‹•ไฝœใ‚’ๆŠ‘ๅˆถใพใŸใฏๆŠ‘ๅˆถ่งฃ้™คใงใใพใ™ใ€‚ + +## `logging` vs `warnings` + +Python ใซใฏใ€ใ‚ˆใ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใ•ใ‚Œใ‚‹ 2 ใคใฎใƒญใ‚ฎใƒณใ‚ฐ ใ‚ทใ‚นใƒ†ใƒ ใŒใ‚ใ‚Šใพใ™ใ€‚ไธŠใง่ชฌๆ˜Žใ—ใŸ `logging` ใจ `warnings` ใงใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€็‰นๅฎšใฎใƒใ‚ฑใƒƒใƒˆๅ†…ใฎ่ญฆๅ‘Šใ‚’ใ•ใ‚‰ใซๅˆ†้กžใงใใพใ™ (ไพ‹: ๆฉŸ่ƒฝใพใŸใฏใƒ‘ใ‚นใฎ`FutureWarning`) +ใ“ใ‚Œใฏใ™ใงใซ้žๆŽจๅฅจใซใชใฃใฆใŠใ‚Šใ€`DeprecationWarning`ใฏไปŠๅพŒใฎ้žๆŽจๅฅจใ‚’็คบใ—ใพใ™ใ€‚ + +ไธกๆ–นใจใ‚‚`transformers`ใƒฉใ‚คใƒ–ใƒฉใƒชใงไฝฟ็”จใ—ใพใ™ใ€‚ `logging`ใฎ`captureWarning`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆดป็”จใ—ใฆ้ฉๅฟœใ•ใ›ใฆใ€ +ใ“ใ‚Œใ‚‰ใฎ่ญฆๅ‘Šใƒกใƒƒใ‚ปใƒผใ‚ธใฏใ€ไธŠ่จ˜ใฎๅ†—้•ท่จญๅฎšใƒ„ใƒผใƒซใซใ‚ˆใฃใฆ็ฎก็†ใ•ใ‚Œใพใ™ใ€‚ + +ใใ‚Œใฏใƒฉใ‚คใƒ–ใƒฉใƒชใฎ้–‹็™บ่€…ใซใจใฃใฆไฝ•ใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ‹?ๆฌกใฎใƒ’ใƒฅใƒผใƒชใ‚นใƒ†ใ‚ฃใƒƒใ‚ฏใ‚’ๅฐŠ้‡ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- `warnings`ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใŠใ‚ˆใณ`transformers`ใซไพๅญ˜ใ™ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใฎ้–‹็™บ่€…ใซๅ„ชๅ…ˆใ•ใ‚Œใ‚‹ในใใงใ™ใ€‚ +- `logging`ใฏใ€ๆ—ฅๅธธใฎใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใงใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใ‚จใƒณใƒ‰ใƒฆใƒผใ‚ถใƒผใซไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฎ`captureWarnings`ใƒกใ‚ฝใƒƒใƒ‰ใฎใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] logging.captureWarnings + +## Base setters + +[[autodoc]] logging.set_verbosity_error + +[[autodoc]] logging.set_verbosity_warning + +[[autodoc]] logging.set_verbosity_info + +[[autodoc]] logging.set_verbosity_debug + +## Other functions + +[[autodoc]] logging.get_verbosity + +[[autodoc]] logging.set_verbosity + +[[autodoc]] logging.get_logger + +[[autodoc]] logging.enable_default_handler + +[[autodoc]] logging.disable_default_handler + +[[autodoc]] logging.enable_explicit_format + +[[autodoc]] logging.reset_format + +[[autodoc]] logging.enable_progress_bar + +[[autodoc]] logging.disable_progress_bar diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/model.md new file mode 100644 index 0000000000000000000000000000000000000000..0923fea1cc059594275c56fc9c6d2f3d1f5533e7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/model.md @@ -0,0 +1,149 @@ + + +# Models + +ใƒ™ใƒผใ‚นใ‚ฏใƒฉใ‚นใงใ‚ใ‚‹ [`PreTrainedModel`]ใ€[`TFPreTrainedModel`]ใ€[`FlaxPreTrainedModel`] ใฏใ€ใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟใจไฟๅญ˜ใซ้–ขใ™ใ‚‹ๅ…ฑ้€šใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่ฃ…ใ—ใฆใŠใ‚Šใ€ใ“ใ‚Œใฏใƒญใƒผใ‚ซใƒซใฎใƒ•ใ‚กใ‚คใƒซใ‚„ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‹ใ‚‰ใ€ใพใŸใฏใƒฉใ‚คใƒ–ใƒฉใƒชใŒๆไพ›ใ™ใ‚‹ไบ‹ๅ‰ๅญฆ็ฟ’ใƒขใƒ‡ใƒซๆง‹ๆˆ๏ผˆHuggingFaceใฎAWS S3ใƒชใƒใ‚ธใƒˆใƒชใ‹ใ‚‰ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰๏ผ‰ใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚ + +[`PreTrainedModel`] ใจ [`TFPreTrainedModel`] ใฏใ€ๆฌกใฎๅ…ฑ้€šใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚‚ๅฎŸ่ฃ…ใ—ใฆใ„ใพใ™๏ผš + +- ่ชžๅฝ™ใซๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใŒ่ฟฝๅŠ ใ•ใ‚ŒใŸๅ ดๅˆใซใ€ๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใฎใƒชใ‚ตใ‚คใ‚บใ‚’่กŒใ† +- ใƒขใƒ‡ใƒซใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒ˜ใƒƒใƒ‰ใ‚’ๅˆˆใ‚Š่พผใ‚€ + +ๅ„ใƒขใƒ‡ใƒซใซๅ…ฑ้€šใ™ใ‚‹ใใฎไป–ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€[`~modeling_utils.ModuleUtilsMixin`]๏ผˆPyTorchใƒขใƒ‡ใƒซ็”จ๏ผ‰ใŠใ‚ˆใณ[`~modeling_tf_utils.TFModuleUtilsMixin`]๏ผˆTensorFlowใƒขใƒ‡ใƒซ็”จ๏ผ‰ใงๅฎš็พฉใ•ใ‚ŒใฆใŠใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎๅ ดๅˆใ€[`~generation.GenerationMixin`]๏ผˆPyTorchใƒขใƒ‡ใƒซ็”จ๏ผ‰ใ€[`~generation.TFGenerationMixin`]๏ผˆTensorFlowใƒขใƒ‡ใƒซ็”จ๏ผ‰ใ€ใŠใ‚ˆใณ[`~generation.FlaxGenerationMixin`]๏ผˆFlax/JAXใƒขใƒ‡ใƒซ็”จ๏ผ‰ใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + + +## PreTrainedModel + +[[autodoc]] PreTrainedModel + - push_to_hub + - all + + + + +### ๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟ + +Transformers 4.20.0ใงใฏใ€[`~PreTrainedModel.from_pretrained`] ใƒกใ‚ฝใƒƒใƒ‰ใŒๅ†่จญ่จˆใ•ใ‚Œใ€[Accelerate](https://huggingface.co/docs/accelerate/big_modeling) ใ‚’ไฝฟ็”จใ—ใฆๅคง่ฆๆจกใƒขใƒ‡ใƒซใ‚’ๆ‰ฑใ†ใ“ใจใŒๅฏ่ƒฝใซใชใ‚Šใพใ—ใŸใ€‚ใ“ใ‚Œใซใฏ Accelerate >= 0.9.0 ใจ PyTorch >= 1.9.0 ใŒๅฟ…่ฆใงใ™ใ€‚ไปฅๅ‰ใฎๆ–นๆณ•ใงใƒ•ใƒซใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใ€ใใฎๅพŒไบ‹ๅ‰ๅญฆ็ฟ’ใฎ้‡ใฟใ‚’่ชญใฟ่พผใ‚€ไปฃใ‚ใ‚Šใซ๏ผˆใ“ใ‚Œใซใฏใƒกใƒขใƒชๅ†…ใฎใƒขใƒ‡ใƒซใ‚ตใ‚คใ‚บใŒ2ๅ€ๅฟ…่ฆใงใ€ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซ็”จใจ้‡ใฟ็”จใฎ2ใคใŒๅฟ…่ฆใงใ—ใŸ๏ผ‰ใ€ใƒขใƒ‡ใƒซใ‚’็ฉบใฎๅค–ๆฎปใจใ—ใฆไฝœๆˆใ—ใ€ไบ‹ๅ‰ๅญฆ็ฟ’ใฎ้‡ใฟใŒ่ชญใฟ่พผใพใ‚Œใ‚‹ใจใใซใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅฎŸไฝ“ๅŒ–ใ™ใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ + +ใ•ใ‚‰ใซใ€ใƒขใƒ‡ใƒซใŒๅฎŒๅ…จใซRAMใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผˆ็พๆ™‚็‚นใงใฏๆŽจ่ซ–ใฎใฟๆœ‰ๅŠน๏ผ‰ใ€็•ฐใชใ‚‹ใƒ‡ใƒใ‚คใ‚นใซใƒขใƒ‡ใƒซใ‚’็›ดๆŽฅ้…็ฝฎใงใใพใ™ใ€‚`device_map="auto"` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€Accelerateใฏๅ„ใƒฌใ‚คใƒคใƒผใ‚’ใฉใฎใƒ‡ใƒใ‚คใ‚นใซ้…็ฝฎใ™ใ‚‹ใ‹ใ‚’ๆฑบๅฎšใ—ใ€ๆœ€้€Ÿใฎใƒ‡ใƒใ‚คใ‚น๏ผˆGPU๏ผ‰ใ‚’ๆœ€ๅคง้™ใซๆดป็”จใ—ใ€ๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใ‚’CPUใ€ใ‚ใ‚‹ใ„ใฏGPU RAMใŒไธ่ถณใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใƒใƒผใƒ‰ใƒ‰ใƒฉใ‚คใƒ–ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใŒ่ค‡ๆ•ฐใฎใƒ‡ใƒใ‚คใ‚นใซๅˆ†ๅ‰ฒใ•ใ‚Œใฆใ„ใฆใ‚‚ใ€้€šๅธธใฉใŠใ‚ŠๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + + +```py +from transformers import AutoModelForSeq2SeqLM + +t0pp = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp", device_map="auto") +``` + +ใƒขใƒ‡ใƒซใŒใƒ‡ใƒใ‚คใ‚น้–“ใงใฉใฎใ‚ˆใ†ใซๅˆ†ๅ‰ฒใ•ใ‚ŒใŸใ‹ใฏใ€ใใฎ `hf_device_map` ๅฑžๆ€งใ‚’่ฆ‹ใ‚‹ใ“ใจใง็ขบ่ชใงใใพใ™: + +```py +t0pp.hf_device_map +``` + +```python out +{'shared': 0, + 'decoder.embed_tokens': 0, + 'encoder': 0, + 'decoder.block.0': 0, + 'decoder.block.1': 1, + 'decoder.block.2': 1, + 'decoder.block.3': 1, + 'decoder.block.4': 1, + 'decoder.block.5': 1, + 'decoder.block.6': 1, + 'decoder.block.7': 1, + 'decoder.block.8': 1, + 'decoder.block.9': 1, + 'decoder.block.10': 1, + 'decoder.block.11': 1, + 'decoder.block.12': 1, + 'decoder.block.13': 1, + 'decoder.block.14': 1, + 'decoder.block.15': 1, + 'decoder.block.16': 1, + 'decoder.block.17': 1, + 'decoder.block.18': 1, + 'decoder.block.19': 1, + 'decoder.block.20': 1, + 'decoder.block.21': 1, + 'decoder.block.22': 'cpu', + 'decoder.block.23': 'cpu', + 'decoder.final_layer_norm': 'cpu', + 'decoder.dropout': 'cpu', + 'lm_head': 'cpu'} +``` + +ๅŒใ˜ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใซๅพ“ใฃใฆใ€็‹ฌ่‡ชใฎใƒ‡ใƒใ‚คใ‚นใƒžใƒƒใƒ—ใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผˆใƒฌใ‚คใƒคใƒผๅใ‹ใ‚‰ใƒ‡ใƒใ‚คใ‚นใธใฎ่พžๆ›ธใงใ™๏ผ‰ใ€‚ใƒขใƒ‡ใƒซใฎใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ‡ใƒใ‚คใ‚นใซใƒžใƒƒใƒ—ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€1ใคใฎใƒฌใ‚คใƒคใƒผใŒๅฎŒๅ…จใซๅŒใ˜ใƒ‡ใƒใ‚คใ‚นใซใ‚ใ‚‹ๅ ดๅˆใ€ใใฎใƒฌใ‚คใƒคใƒผใฎใ‚ตใƒ–ใƒขใ‚ธใƒฅใƒผใƒซใฎใ™ในใฆใŒใฉใ“ใซ่กŒใใ‹ใฎ่ฉณ็ดฐใ‚’็คบใ™ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎใƒ‡ใƒใ‚คใ‚นใƒžใƒƒใƒ—ใฏT0ppใซ้ฉใ—ใฆใ„ใพใ™๏ผˆGPUใƒกใƒขใƒชใŒใ‚ใ‚‹ๅ ดๅˆ๏ผ‰: + +```python +device_map = {"shared": 0, "encoder": 0, "decoder": 1, "lm_head": 1} +``` + +ใƒขใƒ‡ใƒซใฎใƒกใƒขใƒชใธใฎๅฝฑ้Ÿฟใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‹ใ‚‚ใ† 1 ใคใฎๆ–นๆณ•ใฏใ€ไฝŽ็ฒพๅบฆใฎ dtype (`torch.float16` ใชใฉ) ใงใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใ‹ใ€ไปฅไธ‹ใง่ชฌๆ˜Žใ™ใ‚‹็›ดๆŽฅ้‡ๅญๅŒ–ๆ‰‹ๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +### Model Instantiation dtype + +Pytorch ใงใฏใ€ใƒขใƒ‡ใƒซใฏ้€šๅธธ `torch.float32` ๅฝขๅผใงใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใ—ใ‚ˆใ†ใจใ™ใ‚‹ใจๅ•้กŒใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ +้‡ใฟใŒ fp16 ใซใ‚ใ‚‹ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใ€2 ๅ€ใฎใƒกใƒขใƒชใŒๅฟ…่ฆใซใชใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใฎๅˆถ้™ใ‚’ๅ…‹ๆœใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ“ใจใŒใงใใพใ™ใ€‚ +`torch_dtype` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€็›ฎ็š„ใฎ `dtype` ใ‚’ๆ˜Ž็คบ็š„ใซๆธกใ—ใพใ™ใ€‚ + +```python +model = T5ForConditionalGeneration.from_pretrained("t5", torch_dtype=torch.float16) +``` +ใพใŸใฏใ€ใƒขใƒ‡ใƒซใ‚’ๅธธใซๆœ€้ฉใชใƒกใƒขใƒช ใƒ‘ใ‚ฟใƒผใƒณใงใƒญใƒผใƒ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€็‰นๅˆฅใชๅ€ค `"auto"` ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ +ใใ—ใฆใ€`dtype` ใฏใƒขใƒ‡ใƒซใฎ้‡ใฟใ‹ใ‚‰่‡ชๅ‹•็š„ใซๅฐŽๅ‡บใ•ใ‚Œใพใ™ใ€‚ + +```python +model = T5ForConditionalGeneration.from_pretrained("t5", torch_dtype="auto") +``` + +ใ‚นใ‚ฏใƒฉใƒƒใƒใ‹ใ‚‰ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใซใฏใ€ใฉใฎ `dtype` ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ๆŒ‡็คบใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```python +config = T5Config.from_pretrained("t5") +model = AutoModel.from_config(config) +``` + +Pytorch ใฎ่จญ่จˆใซใ‚ˆใ‚Šใ€ใ“ใฎๆฉŸ่ƒฝใฏๆตฎๅ‹•ๅฐๆ•ฐ็‚น dtype ใงใฎใฟไฝฟ็”จใงใใพใ™ใ€‚ + +## ModuleUtilsMixin + +[[autodoc]] modeling_utils.ModuleUtilsMixin + +## TFPreTrainedModel + +[[autodoc]] TFPreTrainedModel + - push_to_hub + - all + +## TFModelUtilsMixin + +[[autodoc]] modeling_tf_utils.TFModelUtilsMixin + +## FlaxPreTrainedModel + +[[autodoc]] FlaxPreTrainedModel + - push_to_hub + - all + +## Pushing to the Hub + +[[autodoc]] utils.PushToHubMixin + +## Sharded checkpoints + +[[autodoc]] modeling_utils.load_sharded_checkpoint diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/onnx.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/onnx.md new file mode 100644 index 0000000000000000000000000000000000000000..f12427760976a0cdde8c197fd581f41bccd3dcf3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/onnx.md @@ -0,0 +1,55 @@ + + +# Exporting ๐Ÿค— Transformers models to ONNX + +๐Ÿค— Transformers ใฏ `transformers.onnx` ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +่จญๅฎšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅˆฉ็”จใ™ใ‚‹ใ“ใจใงใ€ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ONNXใ‚ฐใƒฉใƒ•ใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +่ฉณ็ดฐใฏ[ใ‚ฌใ‚คใƒ‰](../serialization) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## ONNX Configurations + +ไปฅไธ‹ใฎ3ใคใฎๆŠฝ่ฑกใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ +ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใŸใ„ใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ฟใ‚คใƒ—ใซๅฟœใ˜ใฆใ€็ถ™ๆ‰ฟใ™ในใ3ใคใฎๆŠฝ่ฑกใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใพใ™๏ผš + +* ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฏ [`~onnx.config.OnnxConfig`] ใ‚’็ถ™ๆ‰ฟใ—ใพใ™ใ€‚ +* ใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฏ [`~onnx.config.OnnxConfigWithPast`] ใ‚’็ถ™ๆ‰ฟใ—ใพใ™ใ€‚ +* ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒปใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฏ [`~onnx.config.OnnxSeq2SeqConfigWithPast`] ใ‚’็ถ™ๆ‰ฟใ—ใฆใ„ใพใ™ใ€‚ + + +### OnnxConfig + +[[autodoc]] onnx.config.OnnxConfig + +### OnnxConfigWithPast + +[[autodoc]] onnx.config.OnnxConfigWithPast + +### OnnxSeq2SeqConfigWithPast + +[[autodoc]] onnx.config.OnnxSeq2SeqConfigWithPast + +## ONNX Features + +ๅ„ ONNX ๆง‹ๆˆใฏใ€ๆฌกใฎใ“ใจใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ไธ€้€ฃใฎ _ๆฉŸ่ƒฝ_ ใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ +ใ•ใพใ–ใพใชใ‚ฟใ‚คใƒ—ใฎใƒˆใƒใƒญใ‚ธใพใŸใฏใ‚ฟใ‚นใ‚ฏใฎใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +### FeaturesManager + +[[autodoc]] onnx.features.FeaturesManager + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/optimizer_schedules.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/optimizer_schedules.md new file mode 100644 index 0000000000000000000000000000000000000000..cd6dada007cba5c54e2eca1119ec386767cf226e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/optimizer_schedules.md @@ -0,0 +1,73 @@ + + +# Optimization + +`.optimization` ใƒขใ‚ธใƒฅใƒผใƒซใฏไปฅไธ‹ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +- ใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซไฝฟ็”จใงใใ‚‹้‡ใฟๆธ›่กฐใŒไฟฎๆญฃใ•ใ‚ŒใŸใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ€ใŠใ‚ˆใณ +- `_LRSchedule` ใ‹ใ‚‰็ถ™ๆ‰ฟใ™ใ‚‹ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅฝขๅผใฎใ„ใใคใ‹ใฎใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซ: +- ่ค‡ๆ•ฐใฎใƒใƒƒใƒใฎๅ‹พ้…ใ‚’็ดฏ็ฉใ™ใ‚‹ใŸใ‚ใฎๅ‹พ้…็ดฏ็ฉใ‚ฏใƒฉใ‚น + +## AdaFactor (PyTorch) + +[[autodoc]] Adafactor + +## AdamWeightDecay (TensorFlow) + +[[autodoc]] AdamWeightDecay + +[[autodoc]] create_optimizer + +## Schedules + +### Learning Rate Schedules (Pytorch) + +[[autodoc]] SchedulerType + +[[autodoc]] get_scheduler + +[[autodoc]] get_constant_schedule + +[[autodoc]] get_constant_schedule_with_warmup + + + +[[autodoc]] get_cosine_schedule_with_warmup + + + +[[autodoc]] get_cosine_with_hard_restarts_schedule_with_warmup + + + +[[autodoc]] get_linear_schedule_with_warmup + + + +[[autodoc]] get_polynomial_decay_schedule_with_warmup + +[[autodoc]] get_inverse_sqrt_schedule + +### Warmup (TensorFlow) + +[[autodoc]] WarmUp + +## Gradient Strategies + +### GradientAccumulator (TensorFlow) + +[[autodoc]] GradientAccumulator diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/output.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/output.md new file mode 100644 index 0000000000000000000000000000000000000000..beb9dcbb4423556c7bafd2f0b9ad63c89727a897 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/output.md @@ -0,0 +1,321 @@ + + +# Model outputs + +ใ™ในใฆใฎใƒขใƒ‡ใƒซใซใฏใ€[`~utils.ModelOutput`] ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใงใ‚ใ‚‹ๅ‡บๅŠ›ใŒใ‚ใ‚Šใพใ™ใ€‚ใใ‚Œใ‚‰ใฏ +ใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆ่ฟ”ใ•ใ‚Œใ‚‹ใ™ในใฆใฎๆƒ…ๅ ฑใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟๆง‹้€ ใงใ™ใŒใ€ใ‚ฟใƒ—ใƒซใพใŸใฏ +่พžๆ›ธใ€‚ + +ใ“ใ‚ŒใŒใฉใฎใ‚ˆใ†ใซใชใ‚‹ใ‹ใ‚’ไพ‹ใง่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +from transformers import BertTokenizer, BertForSequenceClassification +import torch + +tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +model = BertForSequenceClassification.from_pretrained("google-bert/bert-base-uncased") + +inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") +labels = torch.tensor([1]).unsqueeze(0) # Batch size 1 +outputs = model(**inputs, labels=labels) +``` + +`outputs`ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏ[`~modeling_outputs.SequenceClassifierOutput`]ใงใ‚ใ‚‹ใ€‚ +ใ“ใ‚Œใฏใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใง `loss`ใ€`logits`ใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใง `hidden_states`ใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใง `attentions` ๅฑžๆ€งใ‚’ๆŒใคใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎ `attentions` ๅฑžๆ€งใ‚’ๆŒใคใ“ใจใ‚’ๆ„ๅ‘ณใ™ใ‚‹ใ€‚ใ“ใ“ใงใฏใ€`labels`ใ‚’ๆธกใ—ใŸใฎใง`loss`ใŒใ‚ใ‚‹ใŒใ€`hidden_states`ใจ`attentions`ใฏใชใ„ใ€‚ +`output_hidden_states=True`ใ‚„`output_attentions=True`ใ‚’ๆธกใ—ใฆใ„ใชใ„ใฎใงใ€`hidden_states`ใจ`attentions`ใฏใชใ„ใ€‚ +`output_attentions=True`ใ‚’ๆธกใ•ใชใ‹ใฃใŸใ‹ใ‚‰ใ ใ€‚ + + + +`output_hidden_states=True`ใ‚’ๆธกใ™ใจใ€`outputs.hidden_states[-1]`ใŒ `outputs.last_hidden_states` ใจๆญฃ็ขบใซไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใ€‚ +ใ—ใ‹ใ—ใ€ๅฟ…ใšใ—ใ‚‚ใใ†ใชใ‚‹ใจใฏ้™ใ‚Šใพใ›ใ‚“ใ€‚ใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆใฏใ€ๆœ€ๅพŒใซ้š ใ•ใ‚ŒใŸ็Šถๆ…‹ใŒ่ฟ”ใ•ใ‚ŒใŸใจใใซใ€ๆญฃ่ฆๅŒ–ใ‚„ใใฎๅพŒใฎๅ‡ฆ็†ใ‚’้ฉ็”จใ™ใ‚‹ใ‚‚ใฎใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + + + + +้€šๅธธใจๅŒใ˜ใ‚ˆใ†ใซๅ„ๅฑžๆ€งใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ใใฎๅฑžๆ€งใŒใƒขใƒ‡ใƒซใ‹ใ‚‰่ฟ”ใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใฏใ€ +ใฏ `None`ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ“ใ“ใงใ€ใŸใจใˆใฐ`outputs.loss`ใฏใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆ่จˆ็ฎ—ใ•ใ‚ŒใŸๆๅคฑใงใ‚ใ‚Šใ€`outputs.attentions`ใฏ +`None`ใ€‚ + +`outputs`ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚ฟใƒ—ใƒซใจใ—ใฆ่€ƒใˆใ‚‹ๅ ดๅˆใ€`None`ๅ€คใ‚’ๆŒใŸใชใ„ๅฑžๆ€งใฎใฟใŒ่€ƒๆ…ฎใ•ใ‚Œใพใ™ใ€‚ +ใŸใจใˆใฐใ€ใ“ใ“ใซใฏ 2 ใคใฎ่ฆ็ด ใ€`loss`ใ€ๆฌกใซ`logits`ใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +outputs[:2] +``` + +ใŸใจใˆใฐใ€ใ‚ฟใƒ—ใƒซ `(outputs.loss, Outputs.logits)` ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +`outputs`ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่พžๆ›ธใจใ—ใฆ่€ƒๆ…ฎใ™ใ‚‹ๅ ดๅˆใ€ใ€ŒNoneใ€ใ‚’ๆŒใŸใชใ„ๅฑžๆ€งใฎใฟใŒ่€ƒๆ…ฎใ•ใ‚Œใพใ™ใ€‚ +ไพกๅ€ค่ฆณใ€‚ใŸใจใˆใฐใ€ใ“ใ“ใซใฏ`loss` ใจ `logits`ใจใ„ใ† 2 ใคใฎใ‚ญใƒผใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใ“ใงใฏใ€่ค‡ๆ•ฐใฎใƒขใƒ‡ใƒซ ใ‚ฟใ‚คใƒ—ใงไฝฟ็”จใ•ใ‚Œใ‚‹ๆฑŽ็”จใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใ‚’ๆ–‡ๆ›ธๅŒ–ใ—ใพใ™ใ€‚ๅ…ทไฝ“็š„ใชๅ‡บๅŠ›ใ‚ฟใ‚คใƒ—ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +ๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒซใฎใƒšใƒผใ‚ธใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## ModelOutput + +[[autodoc]] utils.ModelOutput + - to_tuple + +## BaseModelOutput + +[[autodoc]] modeling_outputs.BaseModelOutput + +## BaseModelOutputWithPooling + +[[autodoc]] modeling_outputs.BaseModelOutputWithPooling + +## BaseModelOutputWithCrossAttentions + +[[autodoc]] modeling_outputs.BaseModelOutputWithCrossAttentions + +## BaseModelOutputWithPoolingAndCrossAttentions + +[[autodoc]] modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions + +## BaseModelOutputWithPast + +[[autodoc]] modeling_outputs.BaseModelOutputWithPast + +## BaseModelOutputWithPastAndCrossAttentions + +[[autodoc]] modeling_outputs.BaseModelOutputWithPastAndCrossAttentions + +## Seq2SeqModelOutput + +[[autodoc]] modeling_outputs.Seq2SeqModelOutput + +## CausalLMOutput + +[[autodoc]] modeling_outputs.CausalLMOutput + +## CausalLMOutputWithCrossAttentions + +[[autodoc]] modeling_outputs.CausalLMOutputWithCrossAttentions + +## CausalLMOutputWithPast + +[[autodoc]] modeling_outputs.CausalLMOutputWithPast + +## MaskedLMOutput + +[[autodoc]] modeling_outputs.MaskedLMOutput + +## Seq2SeqLMOutput + +[[autodoc]] modeling_outputs.Seq2SeqLMOutput + +## NextSentencePredictorOutput + +[[autodoc]] modeling_outputs.NextSentencePredictorOutput + +## SequenceClassifierOutput + +[[autodoc]] modeling_outputs.SequenceClassifierOutput + +## Seq2SeqSequenceClassifierOutput + +[[autodoc]] modeling_outputs.Seq2SeqSequenceClassifierOutput + +## MultipleChoiceModelOutput + +[[autodoc]] modeling_outputs.MultipleChoiceModelOutput + +## TokenClassifierOutput + +[[autodoc]] modeling_outputs.TokenClassifierOutput + +## QuestionAnsweringModelOutput + +[[autodoc]] modeling_outputs.QuestionAnsweringModelOutput + +## Seq2SeqQuestionAnsweringModelOutput + +[[autodoc]] modeling_outputs.Seq2SeqQuestionAnsweringModelOutput + +## Seq2SeqSpectrogramOutput + +[[autodoc]] modeling_outputs.Seq2SeqSpectrogramOutput + +## SemanticSegmenterOutput + +[[autodoc]] modeling_outputs.SemanticSegmenterOutput + +## ImageClassifierOutput + +[[autodoc]] modeling_outputs.ImageClassifierOutput + +## ImageClassifierOutputWithNoAttention + +[[autodoc]] modeling_outputs.ImageClassifierOutputWithNoAttention + +## DepthEstimatorOutput + +[[autodoc]] modeling_outputs.DepthEstimatorOutput + +## Wav2Vec2BaseModelOutput + +[[autodoc]] modeling_outputs.Wav2Vec2BaseModelOutput + +## XVectorOutput + +[[autodoc]] modeling_outputs.XVectorOutput + +## Seq2SeqTSModelOutput + +[[autodoc]] modeling_outputs.Seq2SeqTSModelOutput + +## Seq2SeqTSPredictionOutput + +[[autodoc]] modeling_outputs.Seq2SeqTSPredictionOutput + +## SampleTSPredictionOutput + +[[autodoc]] modeling_outputs.SampleTSPredictionOutput + +## TFBaseModelOutput + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutput + +## TFBaseModelOutputWithPooling + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPooling + +## TFBaseModelOutputWithPoolingAndCrossAttentions + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPoolingAndCrossAttentions + +## TFBaseModelOutputWithPast + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPast + +## TFBaseModelOutputWithPastAndCrossAttentions + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPastAndCrossAttentions + +## TFSeq2SeqModelOutput + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqModelOutput + +## TFCausalLMOutput + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutput + +## TFCausalLMOutputWithCrossAttentions + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithCrossAttentions + +## TFCausalLMOutputWithPast + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithPast + +## TFMaskedLMOutput + +[[autodoc]] modeling_tf_outputs.TFMaskedLMOutput + +## TFSeq2SeqLMOutput + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqLMOutput + +## TFNextSentencePredictorOutput + +[[autodoc]] modeling_tf_outputs.TFNextSentencePredictorOutput + +## TFSequenceClassifierOutput + +[[autodoc]] modeling_tf_outputs.TFSequenceClassifierOutput + +## TFSeq2SeqSequenceClassifierOutput + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqSequenceClassifierOutput + +## TFMultipleChoiceModelOutput + +[[autodoc]] modeling_tf_outputs.TFMultipleChoiceModelOutput + +## TFTokenClassifierOutput + +[[autodoc]] modeling_tf_outputs.TFTokenClassifierOutput + +## TFQuestionAnsweringModelOutput + +[[autodoc]] modeling_tf_outputs.TFQuestionAnsweringModelOutput + +## TFSeq2SeqQuestionAnsweringModelOutput + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqQuestionAnsweringModelOutput + +## FlaxBaseModelOutput + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutput + +## FlaxBaseModelOutputWithPast + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPast + +## FlaxBaseModelOutputWithPooling + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPooling + +## FlaxBaseModelOutputWithPastAndCrossAttentions + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPastAndCrossAttentions + +## FlaxSeq2SeqModelOutput + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqModelOutput + +## FlaxCausalLMOutputWithCrossAttentions + +[[autodoc]] modeling_flax_outputs.FlaxCausalLMOutputWithCrossAttentions + +## FlaxMaskedLMOutput + +[[autodoc]] modeling_flax_outputs.FlaxMaskedLMOutput + +## FlaxSeq2SeqLMOutput + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqLMOutput + +## FlaxNextSentencePredictorOutput + +[[autodoc]] modeling_flax_outputs.FlaxNextSentencePredictorOutput + +## FlaxSequenceClassifierOutput + +[[autodoc]] modeling_flax_outputs.FlaxSequenceClassifierOutput + +## FlaxSeq2SeqSequenceClassifierOutput + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqSequenceClassifierOutput + +## FlaxMultipleChoiceModelOutput + +[[autodoc]] modeling_flax_outputs.FlaxMultipleChoiceModelOutput + +## FlaxTokenClassifierOutput + +[[autodoc]] modeling_flax_outputs.FlaxTokenClassifierOutput + +## FlaxQuestionAnsweringModelOutput + +[[autodoc]] modeling_flax_outputs.FlaxQuestionAnsweringModelOutput + +## FlaxSeq2SeqQuestionAnsweringModelOutput + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqQuestionAnsweringModelOutput diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/pipelines.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/pipelines.md new file mode 100644 index 0000000000000000000000000000000000000000..3980becebbde36ed8cc224bcb721dd338ac079b0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/pipelines.md @@ -0,0 +1,498 @@ + + +# Pipelines + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€ๆŽจ่ซ–ใซใƒขใƒ‡ใƒซใ‚’ไฝฟใ†ใŸใ‚ใฎ็ฐกๅ˜ใงๅ„ชใ‚ŒใŸๆ–นๆณ•ใงใ‚ใ‚‹ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€่ค‡้›‘ใชใ‚ณใƒผใƒ‰ใฎใปใจใ‚“ใฉใ‚’ๆŠฝ่ฑกๅŒ–ใ—ใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰่ค‡้›‘ใชใ‚ณใƒผใƒ‰ใฎใปใจใ‚“ใฉใ‚’ๆŠฝ่ฑกๅŒ–ใ—ใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ€ๅๅ‰ไป˜ใๅ›บๆœ‰่กจ็พ่ช่ญ˜ใ€ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€ๆ„Ÿๆƒ…ๅˆ†ๆžใ€็‰นๅพดๆŠฝๅ‡บใ€่ณชๅ•ๅฟœ็ญ”ใชใฉใฎใ‚ฟใ‚นใ‚ฏใซ็‰นๅŒ–ใ—ใŸใ‚ทใƒณใƒ—ใƒซใชAPIใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +Recognitionใ€Masked Language Modelingใ€Sentiment Analysisใ€Feature Extractionใ€Question Answeringใชใฉใฎใ‚ฟใ‚นใ‚ฏใซ็‰นๅŒ–ใ—ใŸใ‚ทใƒณใƒ—ใƒซใชAPIใ‚’ๆไพ›ใ—ใพใ™ใ€‚ไปฅไธ‹ใ‚’ๅ‚็…งใฎใ“ใจใ€‚ +[ใ‚ฟใ‚นใ‚ฏๆฆ‚่ฆ](../task_summary)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎๆŠฝ่ฑกๅŒ–ใซใฏ2ใคใฎใ‚ซใƒ†ใ‚ดใƒชใƒผใŒใ‚ใ‚‹๏ผš + +- [`pipeline`] ใฏใ€ไป–ใฎใ™ในใฆใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚ซใƒ—ใ‚ปใƒซๅŒ–ใ™ใ‚‹ๆœ€ใ‚‚ๅผทๅŠ›ใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ใ€‚ +- ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€[ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช](#audio)ใ€[ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ](#computer-vision)ใ€[่‡ช็„ถ่จ€่ชžๅ‡ฆ็†](#natural-language-processing)ใ€ใŠใ‚ˆใณ [ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ](#multimodal) ใ‚ฟใ‚นใ‚ฏใงไฝฟ็”จใงใใพใ™ใ€‚ + +## The pipeline abstraction + +*ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ* ๆŠฝ่ฑกๅŒ–ใฏใ€ไป–ใฎใ™ในใฆใฎๅˆฉ็”จๅฏ่ƒฝใชใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎใƒฉใƒƒใƒ‘ใƒผใงใ™ใ€‚ไป–ใฎใ‚‚ใฎใจๅŒๆง˜ใซใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ•ใ‚Œใพใ™ +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใ™ใŒใ€ใ•ใ‚‰ใชใ‚‹็”Ÿๆดปใฎ่ณชใ‚’ๆไพ›ใงใใพใ™ใ€‚ + +1 ใคใฎ้ …็›ฎใซๅฏพใ™ใ‚‹ๅ˜็ด”ใชๅ‘ผใณๅ‡บใ—: + +```python +>>> pipe = pipeline("text-classification") +>>> pipe("This restaurant is awesome") +[{'label': 'POSITIVE', 'score': 0.9998743534088135}] +``` + +[ใƒใƒ–](https://huggingface.co) ใฎ็‰นๅฎšใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒขใƒ‡ใƒซใŒใ‚ชใƒณใซใชใฃใฆใ„ใ‚‹ๅ ดๅˆใฏใ‚ฟใ‚นใ‚ฏใ‚’็„ก่ฆ–ใงใใพใ™ใ€‚ +ใƒใƒ–ใฏใ™ใงใซใใ‚Œใ‚’ๅฎš็พฉใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> pipe = pipeline(model="FacebookAI/roberta-large-mnli") +>>> pipe("This restaurant is awesome") +[{'label': 'NEUTRAL', 'score': 0.7313136458396912}] +``` + +ๅคšใใฎ้ …็›ฎใซๅฏพใ—ใฆใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅ‘ผใณๅ‡บใ™ใซใฏใ€*list* ใ‚’ไฝฟ็”จใ—ใฆใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +>>> pipe = pipeline("text-classification") +>>> pipe(["This restaurant is awesome", "This restaurant is awful"]) +[{'label': 'POSITIVE', 'score': 0.9998743534088135}, + {'label': 'NEGATIVE', 'score': 0.9996669292449951}] +``` + +ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅๅพฉใ™ใ‚‹ใซใฏใ€`Dataset`ใ‚’็›ดๆŽฅไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ๅฟ…่ฆใŒใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใ‚‚ใ€่‡ชๅˆ†ใงใƒใƒƒใƒๅ‡ฆ็†ใ‚’่กŒใ†ๅฟ…่ฆใ‚‚ใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ‚ซใ‚นใ‚ฟใƒ ใƒซใƒผใƒ—ใจๅŒใ˜ใใ‚‰ใ„้€Ÿใๅ‹•ไฝœใ™ใ‚‹ใฏใšใงใ™ใ€‚ +GPUใ€‚ใใ‚ŒใŒๅ•้กŒใงใชใ„ๅ ดๅˆใฏใ€ใŸใ‚ใ‚‰ใ‚ใšใซๅ•้กŒใ‚’ไฝœๆˆใ—ใฆใใ ใ•ใ„ใ€‚ + +```python +import datasets +from transformers import pipeline +from transformers.pipelines.pt_utils import KeyDataset +from tqdm.auto import tqdm + +pipe = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h", device=0) +dataset = datasets.load_dataset("superb", name="asr", split="test") + +# KeyDataset (only *pt*) will simply return the item in the dict returned by the dataset item +# as we're not interested in the *target* part of the dataset. For sentence pair use KeyPairDataset +for out in tqdm(pipe(KeyDataset(dataset, "file"))): + print(out) + # {"text": "NUMBER TEN FRESH NELLY IS WAITING ON YOU GOOD NIGHT HUSBAND"} + # {"text": ....} + # .... +``` + +ไฝฟใ„ใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซใ€ใ‚ธใ‚งใƒใƒฌใƒผใ‚ฟใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```python +from transformers import pipeline + +pipe = pipeline("text-classification") + + +def data(): + while True: + # This could come from a dataset, a database, a queue or HTTP request + # in a server + # Caveat: because this is iterative, you cannot use `num_workers > 1` variable + # to use multiple threads to preprocess data. You can still have 1 thread that + # does the preprocessing while the main runs the big inference + yield "This is a test" + + +for out in pipe(data()): + print(out) + # {"text": "NUMBER TEN FRESH NELLY IS WAITING ON YOU GOOD NIGHT HUSBAND"} + # {"text": ....} + # .... +``` + +[[autodoc]] pipeline + + +## Pipeline batching + + +ใ™ในใฆใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใƒใƒƒใƒๅ‡ฆ็†ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ†ใพใใ„ใใพใ™ +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใŒใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ใจใใฏๅธธใซ (ใคใพใ‚Šใ€ใƒชใ‚นใƒˆใ€`dataset`ใ€ใพใŸใฏ `generator`ใ‚’ๆธกใ™ใจใ)ใ€‚ + +```python +from transformers import pipeline +from transformers.pipelines.pt_utils import KeyDataset +import datasets + +dataset = datasets.load_dataset("imdb", name="plain_text", split="unsupervised") +pipe = pipeline("text-classification", device=0) +for out in pipe(KeyDataset(dataset, "text"), batch_size=8, truncation="only_first"): + print(out) + # [{'label': 'POSITIVE', 'score': 0.9998743534088135}] + # Exactly the same output as before, but the content are passed + # as batches to the model +``` + + + + +ใŸใ ใ—ใ€ใ“ใ‚Œใซใ‚ˆใฃใฆใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒ่‡ชๅ‹•็š„ใซๅ‘ไธŠใ™ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚็Šถๆณใซๅฟœใ˜ใฆใ€10 ๅ€ใฎ้ซ˜้€ŸๅŒ–ใพใŸใฏ 5 ๅ€ใฎไฝŽ้€ŸๅŒ–ใฎใ„ใšใ‚Œใ‹ใซใชใ‚Šใพใ™ใ€‚ +ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ€ใƒ‡ใƒผใ‚ฟใ€ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ๅฎŸ้š›ใฎใƒขใƒ‡ใƒซใซใคใ„ใฆใ€‚ + +ไธปใซ้ซ˜้€ŸๅŒ–ใงใ‚ใ‚‹ไพ‹: + + + + +```python +from transformers import pipeline +from torch.utils.data import Dataset +from tqdm.auto import tqdm + +pipe = pipeline("text-classification", device=0) + + +class MyDataset(Dataset): + def __len__(self): + return 5000 + + def __getitem__(self, i): + return "This is a test" + + +dataset = MyDataset() + +for batch_size in [1, 8, 64, 256]: + print("-" * 30) + print(f"Streaming batch_size={batch_size}") + for out in tqdm(pipe(dataset, batch_size=batch_size), total=len(dataset)): + pass +``` + +``` +# On GTX 970 +------------------------------ +Streaming no batching +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:26<00:00, 187.52it/s] +------------------------------ +Streaming batch_size=8 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:04<00:00, 1205.95it/s] +------------------------------ +Streaming batch_size=64 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:02<00:00, 2478.24it/s] +------------------------------ +Streaming batch_size=256 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:01<00:00, 2554.43it/s] +(diminishing returns, saturated the GPU) +``` + +ๆœ€ใ‚‚้€ŸๅบฆใŒไฝŽไธ‹ใ™ใ‚‹ไพ‹: + + +```python +class MyDataset(Dataset): + def __len__(self): + return 5000 + + def __getitem__(self, i): + if i % 64 == 0: + n = 100 + else: + n = 1 + return "This is a test" * n +``` + +ใ“ใ‚Œใฏใ€ไป–ใฎๆ–‡ใซๆฏ”ในใฆ้žๅธธใซ้•ทใ„ๆ–‡ใŒๆ™‚ๆŠ˜ใ‚ใ‚Šใพใ™ใ€‚ใใฎๅ ดๅˆใ€**ๅ…จไฝ“**ใฎใƒใƒƒใƒใฏ 400 ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒณใŒ้•ทใ„ใŸใ‚ใ€ใƒใƒƒใƒๅ…จไฝ“ใŒ [64, 4] ใงใฏใชใ [64, 400] ใซใชใ‚Šใ€้€ŸๅบฆใŒๅคงๅน…ใซไฝŽไธ‹ใ—ใพใ™ใ€‚ใ•ใ‚‰ใซๆ‚ชใ„ใ“ใจใซใ€ +ใƒใƒƒใƒใŒๅคงใใใชใ‚‹ใจใ€ใƒ—ใƒญใ‚ฐใƒฉใƒ ใฏๅ˜็ด”ใซใ‚ฏใƒฉใƒƒใ‚ทใƒฅใ—ใพใ™ใ€‚ + +``` +------------------------------ +Streaming no batching +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:05<00:00, 183.69it/s] +------------------------------ +Streaming batch_size=8 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:03<00:00, 265.74it/s] +------------------------------ +Streaming batch_size=64 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:26<00:00, 37.80it/s] +------------------------------ +Streaming batch_size=256 + 0%| | 0/1000 [00:00 + for out in tqdm(pipe(dataset, batch_size=256), total=len(dataset)): +.... + q = q / math.sqrt(dim_per_head) # (bs, n_heads, q_length, dim_per_head) +RuntimeError: CUDA out of memory. Tried to allocate 376.00 MiB (GPU 0; 3.95 GiB total capacity; 1.72 GiB already allocated; 354.88 MiB free; 2.46 GiB reserved in total by PyTorch) +``` + +ใ“ใฎๅ•้กŒใซๅฏพใ™ใ‚‹้ฉๅˆ‡ใช (ไธ€่ˆฌ็š„ใช) ่งฃๆฑบ็ญ–ใฏใชใใ€ไฝฟ็”จใงใใ‚‹่ท้›ขใฏใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซใ‚ˆใฃใฆ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใฎใƒซใƒผใƒซ +่ฆชๆŒ‡๏ผš + +ใƒฆใƒผใ‚ถใƒผใซใจใฃใฆใฎ็ตŒ้จ“ๅ‰‡ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- **ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚’ไฝฟ็”จใ—ใฆใ€่ฒ ่ทใซๅฏพใ™ใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๆธฌๅฎšใ—ใพใ™ใ€‚ๆธฌใฃใฆใ€ๆธฌใฃใฆใ€ๆธฌใ‚Š็ถšใ‘ใ‚‹ใ€‚ๅฎŸๆ•ฐใจใ„ใ†ใฎใฏใ€ + ้€ฒใ‚€ในใๅ”ฏไธ€ใฎๆ–นๆณ•ใ€‚** +- ใƒฌใ‚คใƒ†ใƒณใ‚ทใซๅˆถ็ด„ใŒใ‚ใ‚‹ๅ ดๅˆ (ๅฎŸ้š›ใฎ่ฃฝๅ“ใŒๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใฆใ„ใ‚‹ๅ ดๅˆ)ใ€ใƒใƒƒใƒๅ‡ฆ็†ใ‚’่กŒใ‚ใชใ„ใงใใ ใ•ใ„ใ€‚ +- CPU ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ใƒใƒƒใƒๅ‡ฆ็†ใ‚’่กŒใ‚ใชใ„ใงใใ ใ•ใ„ใ€‚ +- GPU ใงใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆ (ๅคง้‡ใฎ้™็š„ใƒ‡ใƒผใ‚ฟใงใƒขใƒ‡ใƒซใ‚’ๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆ)ใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + + - sequence_length (ใ€Œ่‡ช็„ถใชใ€ใƒ‡ใƒผใ‚ฟ) ใฎใ‚ตใ‚คใ‚บใซใคใ„ใฆใพใฃใŸใใ‚ใ‹ใ‚‰ใชใ„ๅ ดๅˆใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใƒใƒƒใƒๅ‡ฆ็†ใ‚„ๆธฌๅฎšใ‚’่กŒใ‚ใšใ€ + ๆšซๅฎš็š„ใซ่ฟฝๅŠ ใ—ใฆใฟใพใ™ใ€‚ๅคฑๆ•—ใ—ใŸๅ ดๅˆใซๅ›žๅพฉใ™ใ‚‹ใŸใ‚ใซ OOM ใƒใ‚งใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ—ใพใ™ (ๅคฑๆ•—ใ—ใŸๅ ดๅˆใฏใ€ใ‚ใ‚‹ๆ™‚็‚นใงๅ›žๅพฉใ—ใพใ™)ใ€‚ + sequence_length ใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚) + - sequence_length ใŒ้žๅธธใซ่ฆๅ‰‡็š„ใงใ‚ใ‚‹ๅ ดๅˆใ€ใƒใƒƒใƒๅ‡ฆ็†ใฏ้žๅธธใซ่ˆˆๅ‘ณๆทฑใ„ใ‚‚ใฎใจใชใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใใ€ๆธฌๅฎšใ—ใฆใƒ—ใƒƒใ‚ทใƒฅใ—ใฆใใ ใ•ใ„ใ€‚ + OOM ใŒ็™บ็”Ÿใ™ใ‚‹ใพใง็ถšใ‘ใพใ™ใ€‚ + - GPU ใŒๅคงใใ„ใปใฉใ€ใƒใƒƒใƒๅ‡ฆ็†ใŒใ‚ˆใ‚Š่ˆˆๅ‘ณๆทฑใ„ใ‚‚ใฎใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใใชใ‚Šใพใ™ใ€‚ +- ใƒใƒƒใƒๅ‡ฆ็†ใ‚’ๆœ‰ๅŠนใซใ—ใŸใ‚‰ใ™ใใซใ€OOM ใ‚’้ฉๅˆ‡ใซๅ‡ฆ็†ใงใใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +## Pipeline chunk batching + +`zero-shot-classification` ใจ `question-answering` ใฏใ€ๅ˜ไธ€ใฎๅ…ฅๅŠ›ใง็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใจใ„ใ†ๆ„ๅ‘ณใงใ€ๅฐ‘ใ—็‰นๆฎŠใงใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎ่ค‡ๆ•ฐใฎๅ‰ๆ–นใƒ‘ใ‚นใ€‚้€šๅธธใฎ็Šถๆณใงใฏใ€ใ“ใ‚Œใซใ‚ˆใ‚Š `batch_size` ๅผ•ๆ•ฐใซ้–ขใ™ใ‚‹ๅ•้กŒใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ + +ใ“ใฎๅ•้กŒใ‚’ๅ›ž้ฟใ™ใ‚‹ใŸใ‚ใซใ€ใ“ใ‚Œใ‚‰ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใฉใกใ‚‰ใ‚‚ๅฐ‘ใ—็‰นๆฎŠใซใชใฃใฆใŠใ‚Šใ€ไปฃใ‚ใ‚Šใซ `ChunkPipeline` ใซใชใฃใฆใ„ใพใ™ใ€‚ +้€šๅธธใฎ `Pipeline`ใ€‚่ฆใ™ใ‚‹ใซ๏ผš + +```python +preprocessed = pipe.preprocess(inputs) +model_outputs = pipe.forward(preprocessed) +outputs = pipe.postprocess(model_outputs) +``` + +ไปŠใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™: + +```python +all_model_outputs = [] +for preprocessed in pipe.preprocess(inputs): + model_outputs = pipe.forward(preprocessed) + all_model_outputs.append(model_outputs) +outputs = pipe.postprocess(all_model_outputs) +``` + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏไปฅไธ‹ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏใ‚ณใƒผใƒ‰ใซๅฏพใ—ใฆ้žๅธธใซ้€้Ž็š„ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๅŒใ˜ๆ–นๆณ•ใ€‚ + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใƒใƒƒใƒใ‚’่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใงใใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ็ฐก็•ฅๅŒ–ใ•ใ‚ŒใŸใƒ“ใƒฅใƒผใงใ™ใ€‚ๆฐ—ใซใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใจใ„ใ†ๆ„ๅ‘ณใงใ™ +ๅ…ฅๅŠ›ใŒๅฎŸ้š›ใซใƒˆใƒชใ‚ฌใƒผใ™ใ‚‹ๅ‰ๆ–นใƒ‘ใ‚นใฎๆ•ฐใซใคใ„ใฆใฏใ€`batch_size` ใ‚’ๆœ€้ฉๅŒ–ใงใใพใ™ใ€‚ +ๅ…ฅๅŠ›ใจใฏ็‹ฌ็ซ‹ใ—ใฆใ€‚ๅ‰ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆณจๆ„ไบ‹้ …ใŒๅผ•ใ็ถšใ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + +## Pipeline custom code + +็‰นๅฎšใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ๅ ดๅˆใ€‚ + +็›ฎใฎๅ‰ใฎใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹ๅ•้กŒใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚’่บŠ่บ‡ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎ็›ฎๆจ™ใฏใ€ไฝฟใ„ใ‚„ใ™ใใ€ใปใจใ‚“ใฉใฎใƒฆใƒผใ‚ถใƒผใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€`transformers`ใŒใ‚ใชใŸใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + + +ๅ˜็ด”ใซ่ฉฆใ—ใฆใฟใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใ“ใจใŒใงใใพใ™ใ€‚ + +- ้ธๆŠžใ—ใŸใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ—ใพใ™ + +```python +class MyPipeline(TextClassificationPipeline): + def postprocess(): + # Your code goes here + scores = scores * 100 + # And here + + +my_pipeline = MyPipeline(model=model, tokenizer=tokenizer, ...) +# or if you use *pipeline* function, then: +my_pipeline = pipeline(model="xxxx", pipeline_class=MyPipeline) +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฟ…่ฆใชใ‚ซใ‚นใ‚ฟใƒ  ใ‚ณใƒผใƒ‰ใ‚’ใ™ในใฆๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +## Implementing a pipeline + +[Implementing a new pipeline](../add_new_pipeline) + +## Audio + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ + +### AudioClassificationPipeline + +[[autodoc]] AudioClassificationPipeline + - __call__ + - all + +### AutomaticSpeechRecognitionPipeline + +[[autodoc]] AutomaticSpeechRecognitionPipeline + - __call__ + - all + +### TextToAudioPipeline + +[[autodoc]] TextToAudioPipeline + - __call__ + - all + + +### ZeroShotAudioClassificationPipeline + +[[autodoc]] ZeroShotAudioClassificationPipeline + - __call__ + - all + +## Computer vision + +ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ + +### DepthEstimationPipeline +[[autodoc]] DepthEstimationPipeline + - __call__ + - all + +### ImageClassificationPipeline + +[[autodoc]] ImageClassificationPipeline + - __call__ + - all + +### ImageSegmentationPipeline + +[[autodoc]] ImageSegmentationPipeline + - __call__ + - all + +### ImageToImagePipeline + +[[autodoc]] ImageToImagePipeline + - __call__ + - all + +### ObjectDetectionPipeline + +[[autodoc]] ObjectDetectionPipeline + - __call__ + - all + +### VideoClassificationPipeline + +[[autodoc]] VideoClassificationPipeline + - __call__ + - all + +### ZeroShotImageClassificationPipeline + +[[autodoc]] ZeroShotImageClassificationPipeline + - __call__ + - all + +### ZeroShotObjectDetectionPipeline + +[[autodoc]] ZeroShotObjectDetectionPipeline + - __call__ + - all + +## Natural Language Processing + +่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ + +### FillMaskPipeline + +[[autodoc]] FillMaskPipeline + - __call__ + - all + +### NerPipeline + +[[autodoc]] NerPipeline + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[`TokenClassificationPipeline`] ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### QuestionAnsweringPipeline + +[[autodoc]] QuestionAnsweringPipeline + - __call__ + - all + +### SummarizationPipeline + +[[autodoc]] SummarizationPipeline + - __call__ + - all + +### TableQuestionAnsweringPipeline + +[[autodoc]] TableQuestionAnsweringPipeline + - __call__ + +### TextClassificationPipeline + +[[autodoc]] TextClassificationPipeline + - __call__ + - all + +### TextGenerationPipeline + +[[autodoc]] TextGenerationPipeline + - __call__ + - all + +### Text2TextGenerationPipeline + +[[autodoc]] Text2TextGenerationPipeline + - __call__ + - all + +### TokenClassificationPipeline + +[[autodoc]] TokenClassificationPipeline + - __call__ + - all + +### TranslationPipeline + +[[autodoc]] TranslationPipeline + - __call__ + - all + +### ZeroShotClassificationPipeline + +[[autodoc]] ZeroShotClassificationPipeline + - __call__ + - all + +## Multimodal + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ + +### DocumentQuestionAnsweringPipeline + +[[autodoc]] DocumentQuestionAnsweringPipeline + - __call__ + - all + +### FeatureExtractionPipeline + +[[autodoc]] FeatureExtractionPipeline + - __call__ + - all + +### ImageFeatureExtractionPipeline + +[[autodoc]] ImageFeatureExtractionPipeline + - __call__ + - all + +### ImageToTextPipeline + +[[autodoc]] ImageToTextPipeline + - __call__ + - all + +### ImageTextToTextPipeline + +[[autodoc]] ImageTextToTextPipeline + - __call__ + - all + +### VisualQuestionAnsweringPipeline + +[[autodoc]] VisualQuestionAnsweringPipeline + - __call__ + - all + +## Parent class: `Pipeline` + +[[autodoc]] Pipeline diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/processors.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/processors.md new file mode 100644 index 0000000000000000000000000000000000000000..45333859e8fdf61b7973b1e8c03389a5509c5946 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/processors.md @@ -0,0 +1,160 @@ + + +# Processors + +Transformers ใƒฉใ‚คใƒ–ใƒฉใƒชใงใฏใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏ 2 ใคใฎ็•ฐใชใ‚‹ๆ„ๅ‘ณใ‚’ๆŒใกใพใ™ใ€‚ +- [Wav2Vec2](../model_doc/wav2vec2) ใชใฉใฎใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (้Ÿณๅฃฐใจใƒ†ใ‚ญใ‚นใƒˆ) + ใพใŸใฏ [CLIP](../model_doc/clip) (ใƒ†ใ‚ญใ‚นใƒˆใจใƒ“ใ‚ธใƒงใƒณ) +- ๅคใ„ใƒใƒผใ‚ธใƒงใƒณใฎใƒฉใ‚คใƒ–ใƒฉใƒชใง GLUE ใพใŸใฏ SQUAD ใฎใƒ‡ใƒผใ‚ฟใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใฆใ„ใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏ้žๆŽจๅฅจใซใชใ‚Šใพใ—ใŸใ€‚ + +## Multi-modal processors + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใƒขใƒ‡ใƒซใงใฏใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒ่ค‡ๆ•ฐใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃ (ใƒ†ใ‚ญใ‚นใƒˆใ€ +่ฆ–่ฆšใจ้Ÿณๅฃฐ๏ผ‰ใ€‚ใ“ใ‚Œใฏใ€2 ใคไปฅไธŠใฎๅ‡ฆ็†ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ™ใ‚‹ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใจๅ‘ผใฐใ‚Œใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซใ‚ˆใฃใฆๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ (ใƒ†ใ‚ญใ‚นใƒˆ ใƒขใƒ€ใƒชใƒ†ใ‚ฃ็”จ)ใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผ (่ฆ–่ฆš็”จ)ใ€็‰นๅพดๆŠฝๅ‡บๅ™จ (ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช็”จ) ใชใฉใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ไฟๅญ˜ใŠใ‚ˆใณใƒญใƒผใƒ‰ๆฉŸ่ƒฝใ‚’ๅฎŸ่ฃ…ใ™ใ‚‹ๆฌกใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใ‚’็ถ™ๆ‰ฟใ—ใพใ™ใ€‚ + +[[autodoc]] ProcessorMixin + +## Deprecated processors + +ใ™ในใฆใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ๅŒใ˜ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅพ“ใฃใฆใ„ใพใ™ใ€‚ +[`~data.processors.utils.DataProcessor`]ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏๆฌกใฎใƒชใ‚นใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ +[`~data.processors.utils.InputExample`]ใ€‚ใ“ใ‚Œใ‚‰ +[`~data.processors.utils.InputExample`] ใฏๆฌกใฎใ‚ˆใ†ใซๅค‰ๆ›ใงใใพใ™ใ€‚ +[`~data.processors.utils.Input features`] ใ‚’ใƒขใƒ‡ใƒซใซใƒ•ใ‚ฃใƒผใƒ‰ใ—ใพใ™ใ€‚ + +[[autodoc]] data.processors.utils.DataProcessor + +[[autodoc]] data.processors.utils.InputExample + +[[autodoc]] data.processors.utils.InputFeatures + +## GLUE + +[ไธ€่ˆฌ่จ€่ชž็†่งฃ่ฉ•ไพก (GLUE)](https://gluebenchmark.com/) ใฏใ€ +ๆ—ขๅญ˜ใฎ NLU ใ‚ฟใ‚นใ‚ฏใฎๅคšๆง˜ใชใ‚ปใƒƒใƒˆใซใ‚ใŸใ‚‹ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ€‚็ด™ใจๅŒๆ™‚็™บๅฃฒใ•ใ‚ŒใŸ [GLUE: A +่‡ช็„ถ่จ€่ชž็†่งฃใฎใŸใ‚ใฎใƒžใƒซใƒใ‚ฟใ‚นใ‚ฏใƒ™ใƒณใƒใƒžใƒผใ‚ฏใŠใ‚ˆใณๅˆ†ๆžใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ](https://openreview.net/pdf?id=rJ4km2R5t7) + +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€MRPCใ€MNLIใ€MNLI (ไธไธ€่‡ด)ใ€CoLAใ€SST2ใ€STSBใ€ +QQPใ€QNLIใ€RTEใ€WNLIใ€‚ + +ใใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- [`~data.processors.utils.MrpcProcessor`] +- [`~data.processors.utils.MnliProcessor`] +- [`~data.processors.utils.MnliMismatchedProcessor`] +- [`~data.processors.utils.Sst2Processor`] +- [`~data.processors.utils.StsbProcessor`] +- [`~data.processors.utils.QqpProcessor`] +- [`~data.processors.utils.QnliProcessor`] +- [`~data.processors.utils.RteProcessor`] +- [`~data.processors.utils.WnliProcessor`] + + +ใ•ใ‚‰ใซใ€ๆฌกใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟ ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ๅ€คใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒชใ‚นใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +[`~data.processors.utils.InputExample`]ใ€‚ + +[[autodoc]] data.processors.glue.glue_convert_examples_to_features + +## XNLI + +[ใ‚ฏใƒญใ‚นใƒชใƒณใ‚ฌใƒซ NLI ใ‚ณใƒผใƒ‘ใ‚น (XNLI)](https://www.nyu.edu/projects/bowman/xnli/) ใฏใ€ +่จ€่ชžใ‚’่ถ…ใˆใŸใƒ†ใ‚ญใ‚นใƒˆ่กจ็พใฎๅ“่ณชใ€‚ XNLI ใฏใ€[*MultiNLI*](http://www.nyu.edu/projects/bowman/multinli/) ใซๅŸบใฅใใ‚ฏใƒฉใ‚ฆใƒ‰ใ‚ฝใƒผใ‚นใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใฎใƒšใ‚ขใซใฏใ€15 ๅ€‹ใฎใƒ†ใ‚ญใ‚นใƒˆๅซๆ„ใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใŒใƒฉใƒ™ใƒซไป˜ใ‘ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใ•ใพใ–ใพใช่จ€่ชž (่‹ฑ่ชžใชใฉใฎ้ซ˜ใƒชใ‚ฝใƒผใ‚น่จ€่ชžใจใ‚นใƒฏใƒ’ใƒช่ชžใชใฉใฎไฝŽใƒชใ‚ฝใƒผใ‚น่จ€่ชžใฎไธกๆ–นใ‚’ๅซใ‚€)ใ€‚ + +่ซ–ๆ–‡ [XNLI: Evaluating Cross-lingual Sentence Representations](https://huggingface.co/papers/1809.05053) ใจๅŒๆ™‚ใซใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ—ใŸใ€‚ + +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€XNLI ใƒ‡ใƒผใ‚ฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒ›ใ‚นใƒˆใ—ใพใ™ใ€‚ + +- [`~data.processors.utils.XnliProcessor`] + +ใƒ†ใ‚นใƒˆใ‚ปใƒƒใƒˆใซใฏใ‚ดใƒผใƒซใƒ‰ใƒฉใƒ™ใƒซใŒไป˜ใ„ใฆใ„ใ‚‹ใŸใ‚ใ€่ฉ•ไพกใฏใƒ†ใ‚นใƒˆใ‚ปใƒƒใƒˆใง่กŒใ‚ใ‚Œใพใ™ใฎใงใ”ไบ†ๆ‰ฟใใ ใ•ใ„ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ™ใ‚‹ไพ‹ใฏใ€[run_xnli.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification/run_xnli.py) ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## SQuAD + +[The Stanford Question Answering Dataset (SQuAD)](https://rajpurkar.github.io/SQuAD-explorer//) ใฏใ€ๆฌกใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใ™ใ€‚ +่ณชๅ•ๅฟœ็ญ”ใซ้–ขใ™ใ‚‹ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚ v1.1 ใจ v2.0 ใฎ 2 ใคใฎใƒใƒผใ‚ธใƒงใƒณใŒๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ๆœ€ๅˆใฎใƒใƒผใ‚ธใƒงใƒณ +(v1.1) ใฏใ€่ซ–ๆ–‡ [SQuAD: 100,000+ question for Machine Comprehension of Text](https://huggingface.co/papers/1606.05250) ใจใจใ‚‚ใซใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ—ใŸใ€‚ 2 ็•ช็›ฎใฎใƒใƒผใ‚ธใƒงใƒณ (v2.0) ใฏใ€่ซ–ๆ–‡ [Know What You Don't ใจๅŒๆ™‚ใซใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ—ใŸใ€‚ +็ŸฅใฃใฆใŠใในใ: SQuAD ใฎ็ญ”ใˆใ‚‰ใ‚Œใชใ„่ณชๅ•](https://huggingface.co/papers/1806.03822)ใ€‚ + +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€ๆฌกใฎ 2 ใคใฎใƒใƒผใ‚ธใƒงใƒณใฎใใ‚Œใžใ‚Œใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒ›ใ‚นใƒˆใ—ใพใ™ใ€‚ + +### Processors + +ใใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +- [`~data.processors.utils.SquadV1Processor`] +- [`~data.processors.utils.SquadV2Processor`] + +ใฉใกใ‚‰ใ‚‚ๆŠฝ่ฑกใ‚ฏใƒฉใ‚น [`~data.processors.utils.SquadProcessor`] ใ‚’็ถ™ๆ‰ฟใ—ใฆใ„ใพใ™ใ€‚ + +[[autodoc]] data.processors.squad.SquadProcessor + - all + +ใ•ใ‚‰ใซใ€ๆฌกใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€SQuAD ใฎไพ‹ใ‚’ๆฌกใฎๅฝขๅผใซๅค‰ๆ›ใงใใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใงใใ‚‹ [`~data.processors.utils.SquadFeatures`]ใ€‚ + +[[autodoc]] data.processors.squad.squad_convert_examples_to_features + +ใ“ใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใจๅ‰่ฟฐใฎๆ–นๆณ•ใฏใ€ใƒ‡ใƒผใ‚ฟใ‚’ๅซใ‚€ใƒ•ใ‚กใ‚คใƒซใ ใ‘ใงใชใใ€ +*tensorflow_datasets* ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ€‚ไปฅไธ‹ใซไพ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +### Example usage + +ไปฅไธ‹ใซใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใŸไพ‹ใจใ€ใƒ‡ใƒผใ‚ฟ ใƒ•ใ‚กใ‚คใƒซใ‚’ไฝฟ็”จใ—ใŸๅค‰ๆ›ๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ + +```python +# Loading a V2 processor +processor = SquadV2Processor() +examples = processor.get_dev_examples(squad_v2_data_dir) + +# Loading a V1 processor +processor = SquadV1Processor() +examples = processor.get_dev_examples(squad_v1_data_dir) + +features = squad_convert_examples_to_features( + examples=examples, + tokenizer=tokenizer, + max_seq_length=max_seq_length, + doc_stride=args.doc_stride, + max_query_length=max_query_length, + is_training=not evaluate, +) +``` + +*tensorflow_datasets* ใฎไฝฟ็”จใฏใ€ใƒ‡ใƒผใ‚ฟ ใƒ•ใ‚กใ‚คใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใฎใจๅŒใ˜ใใ‚‰ใ„็ฐกๅ˜ใงใ™ใ€‚ + +```python +# tensorflow_datasets only handle Squad V1. +tfds_examples = tfds.load("squad") +examples = SquadV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate) + +features = squad_convert_examples_to_features( + examples=examples, + tokenizer=tokenizer, + max_seq_length=max_seq_length, + doc_stride=args.doc_stride, + max_query_length=max_query_length, + is_training=not evaluate, +) +``` + +ใ“ใ‚Œใ‚‰ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ™ใ‚‹ๅˆฅใฎไพ‹ใฏใ€[run_squad.py](https://github.com/huggingface/transformers/tree/main/examples/legacy/question-answering/run_squad.py) ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/quantization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/quantization.md new file mode 100644 index 0000000000000000000000000000000000000000..b511b9dbfc6bc82215bcefc473b1c38630b6c971 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/quantization.md @@ -0,0 +1,447 @@ + + +# Quantize ๐Ÿค— Transformers models + +## `AutoGPTQ` Integration + + +๐Ÿค— Transformers ใซใฏใ€่จ€่ชžใƒขใƒ‡ใƒซใง GPTQ ้‡ๅญๅŒ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎ `optimum` API ใŒ็ตฑๅˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅคงๅน…ใซไฝŽไธ‹ใ•ใ›ใ‚‹ใ“ใจใชใใ€ๆŽจ่ซ–้€Ÿๅบฆใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใ“ใจใชใใ€ใƒขใƒ‡ใƒซใ‚’ 8ใ€4ใ€3ใ€ใ•ใ‚‰ใซใฏ 2 ใƒ“ใƒƒใƒˆใงใƒญใƒผใƒ‰ใŠใ‚ˆใณ้‡ๅญๅŒ–ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใปใจใ‚“ใฉใฎ GPU ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ไปฅไธ‹ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +- [GPTQ](https://huggingface.co/papers/2210.17323) ่ซ–ๆ–‡ +- GPTQ ้‡ๅญๅŒ–ใซ้–ขใ™ใ‚‹ `optimum` [ใ‚ฌใ‚คใƒ‰](https://huggingface.co/docs/optimum/llm_quantization/usage_guides/quantization) +- ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใจใ—ใฆไฝฟ็”จใ•ใ‚Œใ‚‹ [`AutoGPTQ`](https://github.com/PanQiWei/AutoGPTQ) ใƒฉใ‚คใƒ–ใƒฉใƒช + +### Requirements + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎ่ฆไปถใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +- ๆœ€ๆ–ฐใฎ `AutoGPTQ` ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ +`pip install auto-gptq` ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ + +- ๆœ€ๆ–ฐใฎ `optimum` ใ‚’ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ +`git+https://github.com/huggingface/optimum.git` ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ + +- ๆœ€ๆ–ฐใฎ `transformers` ใ‚’ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ +ๆœ€ๆ–ฐใฎ `transformers` ใ‚’ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ `pip install git+https://github.com/huggingface/transformers.git` + +- ๆœ€ๆ–ฐใฎ `accelerate` ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ€‚ +`pip install --upgrade accelerate` ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ€‚ + +GPTQ็ตฑๅˆใฏไปŠใฎใจใ“ใ‚ใƒ†ใ‚ญใ‚นใƒˆใƒขใƒ‡ใƒซใฎใฟใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใฎใงใ€่ฆ–่ฆšใ€้Ÿณๅฃฐใ€ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใƒขใƒ‡ใƒซใงใฏไบˆๆœŸใ›ใฌๆŒ™ๅ‹•ใซ้ญ้‡ใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +### Load and quantize a model + +GPTQ ใฏใ€้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซ้‡ใฟใฎใ‚ญใƒฃใƒชใƒ–ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅฟ…่ฆใจใ™ใ‚‹้‡ๅญๅŒ–ๆ–นๆณ•ใงใ™ใ€‚ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใƒขใƒ‡ใƒซใ‚’ๆœ€ๅˆใ‹ใ‚‰้‡ๅญๅŒ–ใ™ใ‚‹ๅ ดๅˆใฏใ€้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใพใงใซๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ (`facebook/opt-350m`ใƒขใƒ‡ใƒซใฎ Google colab ใงใฏ็ด„ 5 ๅˆ†)ใ€‚ + +ใ—ใŸใŒใฃใฆใ€GPTQ ้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ทใƒŠใƒชใ‚ชใฏ 2 ใคใ‚ใ‚Šใพใ™ใ€‚ๆœ€ๅˆใฎไฝฟ็”จไพ‹ใฏใ€ใƒใƒ–ใงๅˆฉ็”จๅฏ่ƒฝใชไป–ใฎใƒฆใƒผใ‚ถใƒผใซใ‚ˆใฃใฆใ™ใงใซ้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚2 ็•ช็›ฎใฎไฝฟ็”จไพ‹ใฏใ€ใƒขใƒ‡ใƒซใ‚’ๆœ€ๅˆใ‹ใ‚‰้‡ๅญๅŒ–ใ—ใ€ไฟๅญ˜ใ™ใ‚‹ใ‹ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใฆใ€ไป–ใฎใƒฆใƒผใ‚ถใƒผใŒไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใใ‚Œใ‚‚ไฝฟใฃใฆใใ ใ•ใ„ใ€‚ + +#### GPTQ Configuration + +ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆ้‡ๅญๅŒ–ใ™ใ‚‹ใซใฏใ€[`GPTQConfig`] ใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใซใฏใ€`bits`ใฎๆ•ฐใ€้‡ๅญๅŒ–ใ‚’่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎ`dataset`ใ€ใŠใ‚ˆใณใƒขใƒ‡ใƒซใฎ`Tokenizer`ใ‚’ๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +model_id = "facebook/opt-125m" +tokenizer = AutoTokenizer.from_pretrained(model_id) +gptq_config = GPTQConfig(bits=4, dataset = "c4", tokenizer=tokenizer) +``` + +็‹ฌ่‡ชใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆ–‡ๅญ—ๅˆ—ใฎใƒชใ‚นใƒˆใจใ—ใฆๆธกใ™ใ“ใจใŒใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€GPTQ ่ซ–ๆ–‡ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```python +dataset = ["auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."] +quantization = GPTQConfig(bits=4, dataset = dataset, tokenizer=tokenizer) +``` + +#### Quantization + +`from_pretrained` ใ‚’ไฝฟ็”จใ—ใ€`quantization_config` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใงใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใงใใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM +model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=gptq_config) +``` + +ใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใ™ใ‚‹ใซใฏ GPU ใŒๅฟ…่ฆใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใƒขใƒ‡ใƒซใ‚’ CPU ใซ้…็ฝฎใ—ใ€้‡ๅญๅŒ–ใ™ใ‚‹ใŸใ‚ใซใƒขใ‚ธใƒฅใƒผใƒซใ‚’ GPU ใซๅ‰ๅพŒใซ็งปๅ‹•ใ•ใ›ใพใ™ใ€‚ + +CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฎไฝฟ็”จไธญใซ GPU ใฎไฝฟ็”จ้‡ใ‚’ๆœ€ๅคงๅŒ–ใ—ใŸใ„ๅ ดๅˆใฏใ€`device_map = "auto"` ใ‚’่จญๅฎšใงใใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM +model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", quantization_config=gptq_config) +``` + +ใƒ‡ใ‚ฃใ‚นใ‚ฏ ใ‚ชใƒ•ใƒญใƒผใƒ‰ใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ•ใ‚‰ใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒๅŽŸๅ› ใงใƒกใƒขใƒชใŒไธ่ถณใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€`from_pretained` ใง `max_memory` ใ‚’ๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ `device_map`ใจ`max_memory`ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใ“ใฎ [ใ‚ฌใ‚คใƒ‰](https://huggingface.co/docs/accelerate/usage_guides/big_modeling#designing-a-device-map) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +GPTQ ้‡ๅญๅŒ–ใฏใ€็พๆ™‚็‚นใงใฏใƒ†ใ‚ญใ‚นใƒˆ ใƒขใƒ‡ใƒซใงใฎใฟๆฉŸ่ƒฝใ—ใพใ™ใ€‚ใ•ใ‚‰ใซใ€้‡ๅญๅŒ–ใƒ—ใƒญใ‚ปใ‚นใฏใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซใ‚ˆใฃใฆใฏ้•ทๆ™‚้–“ใ‹ใ‹ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ (NVIDIA A100 ใ‚’ไฝฟ็”จใ—ใŸๅ ดๅˆใ€175B ใƒขใƒ‡ใƒซ = 4 gpu ๆ™‚้–“)ใ€‚ใƒขใƒ‡ใƒซใฎ GPTQ ้‡ๅญๅŒ–ใƒใƒผใ‚ธใƒงใƒณใŒๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏใ€ใƒใƒ–ใง็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ใใ†ใงใชใ„ๅ ดๅˆใฏใ€github ใง่ฆๆฑ‚ใ‚’้€ไฟกใงใใพใ™ใ€‚ + + +### Push quantized model to ๐Ÿค— Hub + +ไป–ใฎ ๐Ÿค— ใƒขใƒ‡ใƒซใจๅŒๆง˜ใซใ€`push_to_hub` ใ‚’ไฝฟ็”จใ—ใฆ้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใงใใพใ™ใ€‚้‡ๅญๅŒ–ๆง‹ๆˆใฏไฟๅญ˜ใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใซๆฒฟใฃใฆใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใพใ™ใ€‚ + +```python +quantized_model.push_to_hub("opt-125m-gptq") +tokenizer.push_to_hub("opt-125m-gptq") +``` + +้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใ‚ซใƒซ ใƒžใ‚ทใƒณใซไฟๅญ˜ใ—ใŸใ„ๅ ดๅˆใฏใ€`save_pretrained` ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ†ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + +```python +quantized_model.save_pretrained("opt-125m-gptq") +tokenizer.save_pretrained("opt-125m-gptq") +``` + +`device_map` ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใ—ใŸๅ ดๅˆใฏใ€ไฟๅญ˜ใ™ใ‚‹ๅ‰ใซใƒขใƒ‡ใƒซๅ…จไฝ“ใ‚’ GPU ใพใŸใฏ `cpu` ใฎใ„ใšใ‚Œใ‹ใซ็งปๅ‹•ใ—ใฆใใ ใ•ใ„ใ€‚ + +```python +quantized_model.to("cpu") +quantized_model.save_pretrained("opt-125m-gptq") +``` + +### Load a quantized model from the ๐Ÿค— Hub + +`from_pretrained`ใ‚’ไฝฟ็”จใ—ใฆใ€้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใ‹ใ‚‰ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ +ๅฑžๆ€ง `quantization_config` ใŒใƒขใƒ‡ใƒซ่จญๅฎšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซๅญ˜ๅœจใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใ€ใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚ŒใŸ้‡ใฟใŒ้‡ๅญๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq") +``` + +ๅฟ…่ฆไปฅไธŠใฎใƒกใƒขใƒชใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใšใซใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Š้€Ÿใใƒญใƒผใƒ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€`device_map` ๅผ•ๆ•ฐใฏ้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใงใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚ `accelerate`ใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```python +from transformers import AutoModelForCausalLM +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto") +``` + +### Exllama kernels for faster inference + +4 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€ๆŽจ่ซ–้€Ÿๅบฆใ‚’้ซ˜ใ‚ใ‚‹ใŸใ‚ใซ exllama ใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงๆœ‰ๅŠนใซใชใฃใฆใ„ใพใ™ใ€‚ [`GPTQConfig`] ใง `disable_exllama` ใ‚’ๆธกใ™ใ“ใจใงใ€ใใฎๅ‹•ไฝœใ‚’ๅค‰ๆ›ดใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่จญๅฎšใซไฟๅญ˜ใ•ใ‚Œใฆใ„ใ‚‹้‡ๅญๅŒ–่จญๅฎšใŒไธŠๆ›ธใใ•ใ‚Œใพใ™ใ€‚ใ‚ซใƒผใƒใƒซใซ้–ข้€ฃใ™ใ‚‹ๅฑžๆ€งใฎใฟใ‚’ไธŠๆ›ธใใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ•ใ‚‰ใซใ€exllama ใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒขใƒ‡ใƒซๅ…จไฝ“ใ‚’ GPU ไธŠใซ็ฝฎใๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +import torch +gptq_config = GPTQConfig(bits=4, disable_exllama=False) +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto", quantization_config = gptq_config) +``` + +็พๆ™‚็‚นใงใฏ 4 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใฎใฟใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ•ใ‚‰ใซใ€peft ใ‚’ไฝฟ็”จใ—ใฆ้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€exllama ใ‚ซใƒผใƒใƒซใ‚’้žใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +#### Fine-tune a quantized model + +Hugging Face ใ‚จใ‚ณใ‚ทใ‚นใƒ†ใƒ ใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฎๅ…ฌๅผใ‚ตใƒใƒผใƒˆใซใ‚ˆใ‚Šใ€GPTQ ใง้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใงใใพใ™ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[`peft`](https://github.com/huggingface/peft) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +### Example demo + +GPTQ ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใ™ใ‚‹ๆ–นๆณ•ใจใ€peft ใ‚’ไฝฟ็”จใ—ใฆ้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆใฏใ€Google Colab [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/drive/1_TIrmuKOFhuRRiTWN94iLKUFu6ZX4ceb?usp=sharing) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### GPTQConfig + +[[autodoc]] GPTQConfig + +## `bitsandbytes` Integration + +๐Ÿค— Transformers ใฏใ€`bitsandbytes` ใงๆœ€ใ‚‚ใ‚ˆใไฝฟ็”จใ•ใ‚Œใ‚‹ใƒขใ‚ธใƒฅใƒผใƒซใจ็ทŠๅฏ†ใซ็ตฑๅˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใงใƒขใƒ‡ใƒซใ‚’ 8 ใƒ“ใƒƒใƒˆ็ฒพๅบฆใงใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€`bitsandbytes`ใฎ `0.37.0`ใƒชใƒชใƒผใ‚นไปฅ้™ใ€ใปใจใ‚“ใฉใฎ GPU ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +้‡ๅญๅŒ–ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[LLM.int8()](https://huggingface.co/papers/2208.07339) ่ซ–ๆ–‡ใ€ใพใŸใฏ [ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://huggingface.co/blog/hf-bitsandbytes-) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚็ตฑๅˆ๏ผ‰ใ‚ณใƒฉใƒœใƒฌใƒผใ‚ทใƒงใƒณใซใคใ„ใฆใ€‚ + +`0.39.0`ใƒชใƒชใƒผใ‚นไปฅ้™ใ€FP4 ใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ๆดป็”จใ—ใ€4 ใƒ“ใƒƒใƒˆ้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ—ใฆ`device_map`ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ไปปๆ„ใฎใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +็‹ฌ่‡ชใฎ pytorch ใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใ—ใŸใ„ๅ ดๅˆใฏใ€๐Ÿค— Accelerate ใƒฉใ‚คใƒ–ใƒฉใƒชใฎ [ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization) ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ + +`bitsandbytes`็ตฑๅˆใ‚’ไฝฟ็”จใ—ใฆใงใใ‚‹ใ“ใจใฏๆฌกใฎใจใŠใ‚Šใงใ™ + +### General usage + +ใƒขใƒ‡ใƒซใŒ ๐Ÿค— Accelerate ใซใ‚ˆใ‚‹่ชญใฟ่พผใฟใ‚’ใ‚ตใƒใƒผใƒˆใ—ใ€`torch.nn.Linear` ใƒฌใ‚คใƒคใƒผใŒๅซใพใ‚Œใฆใ„ใ‚‹้™ใ‚Šใ€ [`~PreTrainedModel.from_pretrained`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ™ใจใใซ `load_in_8bit` ใพใŸใฏ `load_in_4bit` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’้‡ๅญๅŒ–ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใฉใฎใ‚ˆใ†ใชใƒขใƒ€ใƒชใƒ†ใ‚ฃใงใ‚‚ๅŒๆง˜ใซๆฉŸ่ƒฝใ™ใ‚‹ใฏใšใงใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM + +model_8bit = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", load_in_8bit=True) +model_4bit = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", load_in_4bit=True) +``` + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ไป–ใฎใ™ในใฆใฎใƒขใ‚ธใƒฅใƒผใƒซ (ไพ‹: `torch.nn.LayerNorm`) ใฏ `torch.float16` ใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใŒใ€ใใฎ `dtype` ใ‚’ๅค‰ๆ›ดใ—ใŸใ„ๅ ดๅˆใฏใ€`torch_dtype` ๅผ•ๆ•ฐใ‚’ไธŠๆ›ธใใงใใพใ™ใ€‚ + +```python +>>> import torch +>>> from transformers import AutoModelForCausalLM + +>>> model_8bit = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", load_in_8bit=True, torch_dtype=torch.float32) +>>> model_8bit.model.decoder.layers[-1].final_layer_norm.weight.dtype +torch.float32 +``` + +### FP4 quantization + +#### Requirements + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ ใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅ‰ใซใ€ไปฅไธ‹ใฎ่ฆไปถใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +- ๆœ€ๆ–ฐใฎ`bitsandbytes`ใƒฉใ‚คใƒ–ใƒฉใƒช +`pip install bitsandbytes>=0.39.0` + +- ๆœ€ๆ–ฐใฎ`accelerate`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ +`pip install --upgrade accelerate` + +- ๆœ€ๆ–ฐใฎ `transformers` ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ +`pip install --upgrade transformers` + +#### Tips and best practices + +- **้ซ˜ๅบฆใชไฝฟ็”จๆณ•:** ๅฏ่ƒฝใชใ™ในใฆใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใŸ 4 ใƒ“ใƒƒใƒˆ้‡ๅญๅŒ–ใฎ้ซ˜ๅบฆใชไฝฟ็”จๆณ•ใซใคใ„ใฆใฏใ€[ใ“ใฎ Google Colab ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/drive/1ge2F1QSK8Q7h0hn3YKuBCOAS0bK8E0wf) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +- **`batch_size=1` ใซใ‚ˆใ‚‹้ซ˜้€ŸๆŽจ่ซ– :** bitsandbytes ใฎ `0.40.0` ใƒชใƒชใƒผใ‚นไปฅ้™ใ€`batch_size=1` ใงใฏ้ซ˜้€ŸๆŽจ่ซ–ใฎๆฉๆตใ‚’ๅ—ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ [ใ“ใ‚Œใ‚‰ใฎใƒชใƒชใƒผใ‚น ใƒŽใƒผใƒˆ](https://github.com/TimDettmers/bitsandbytes/releases/tag/0.40.0) ใ‚’็ขบ่ชใ—ใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ๆดป็”จใ™ใ‚‹ใซใฏ`0.40.0`ไปฅ้™ใฎใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚็ฎฑใฎใ€‚ + +- **ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ:** [QLoRA ่ซ–ๆ–‡](https://huggingface.co/papers/2305.14314) ใซใ‚ˆใ‚‹ใจใ€4 ใƒ“ใƒƒใƒˆๅŸบๆœฌใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆ (ไพ‹: LoRA ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ไฝฟ็”จ)ใ€`bnb_4bit_quant_type='nf4'` ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ใ€‚ + +- **ๆŽจ่ซ–:** ๆŽจ่ซ–ใฎๅ ดๅˆใ€`bnb_4bit_quant_type` ใฏใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซๅคงใใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใจใฎไธ€่ฒซๆ€งใ‚’ไฟใคใŸใ‚ใซใ€ๅฟ…ใšๅŒใ˜ `bnb_4bit_compute_dtype` ใŠใ‚ˆใณ `torch_dtype` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + +#### Load a large model in 4bit + +`.from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ™ใจใใซ `load_in_4bit=True` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ (ใŠใŠใ‚ˆใ) 4 ใงๅ‰ฒใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +# pip install transformers accelerate bitsandbytes +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "bigscience/bloom-1b7" + +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", load_in_4bit=True) +``` + + + +ใƒขใƒ‡ใƒซใŒ 4 ใƒ“ใƒƒใƒˆใงใƒญใƒผใƒ‰ใ•ใ‚Œใ‚‹ใจใ€็พๆ™‚็‚นใงใฏ้‡ๅญๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ“ใจใฏใงใใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ 4 ใƒ“ใƒƒใƒˆใฎ้‡ใฟใฏใพใ ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใชใ„ใ“ใจใซใ‚‚ๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€4 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ฟฝๅŠ ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใซใคใ„ใฆใฏๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ชฌๆ˜Žใ—ใพใ™ใ€‚ + + + +### Load a large model in 8bit + +`.from_pretrained` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ™ใจใใซ `load_in_8bit=True` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒกใƒขใƒช่ฆไปถใ‚’ใŠใ‚ˆใๅŠๅˆ†ใซใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```python +# pip install transformers accelerate bitsandbytes +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +ๆฌกใซใ€้€šๅธธ [`PreTrainedModel`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใฎใจๅŒใ˜ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +`get_memory_footprint` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎใƒกใƒขใƒช ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใ‚’็ขบ่ชใงใใพใ™ใ€‚ + +```python +print(model.get_memory_footprint()) +``` + +ใ“ใฎ็ตฑๅˆใซใ‚ˆใ‚Šใ€ๅคงใใชใƒขใƒ‡ใƒซใ‚’ๅฐใ•ใชใƒ‡ใƒใ‚คใ‚นใซใƒญใƒผใƒ‰ใ—ใ€ๅ•้กŒใชใๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + + +ใƒขใƒ‡ใƒซใŒ 8 ใƒ“ใƒƒใƒˆใงใƒญใƒผใƒ‰ใ•ใ‚Œใ‚‹ใจใ€ๆœ€ๆ–ฐใฎ `transformers`ใจ`bitsandbytes`ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ‚’้™คใใ€้‡ๅญๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ“ใจใฏ็พๅœจไธๅฏ่ƒฝใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ 8 ใƒ“ใƒƒใƒˆใฎ้‡ใฟใฏใพใ ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใชใ„ใ“ใจใซใ‚‚ๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€8 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ฟฝๅŠ ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใซใคใ„ใฆใฏๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ชฌๆ˜Žใ—ใพใ™ใ€‚ +ใพใŸใ€`device_map` ใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใŒใ€ๅˆฉ็”จๅฏ่ƒฝใชใƒชใ‚ฝใƒผใ‚นไธŠใงใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ™ใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซใฏ `device_map = 'auto'` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚ + + + +#### Advanced use cases + +ใ“ใ“ใงใฏใ€FP4 ้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ—ใฆๅฎŸ่กŒใงใใ‚‹ใ„ใใคใ‹ใฎ้ซ˜ๅบฆใชไฝฟ็”จไพ‹ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +##### Change the compute dtype + +compute dtype ใฏใ€่จˆ็ฎ—ไธญใซไฝฟ็”จใ•ใ‚Œใ‚‹ dtype ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใŸใจใˆใฐใ€้š ใ—็Šถๆ…‹ใฏ`float32`ใซใ‚ใ‚Šใพใ™ใŒใ€้ซ˜้€ŸๅŒ–ใฎใŸใ‚ใซ่จˆ็ฎ—ใ‚’ bf16 ใซ่จญๅฎšใงใใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€compute dtype ใฏ `float32` ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ + +```python +import torch +from transformers import BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16) +``` + +##### Using NF4 (Normal Float 4) data type + +NF4 ใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๆญฃ่ฆๅˆ†ๅธƒใ‚’ไฝฟ็”จใ—ใฆๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใซ้ฉๅˆใ—ใŸๆ–ฐใ—ใ„ 4 ใƒ“ใƒƒใƒˆ ใƒ‡ใƒผใ‚ฟๅž‹ใงใ™ใ€‚ใใฎๅฎŸ่กŒใฎใŸใ‚ใซ: + +```python +from transformers import BitsAndBytesConfig + +nf4_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", +) + +model_nf4 = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=nf4_config) +``` + +##### Use nested quantization for more memory efficient inference + +ใพใŸใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸ้‡ๅญๅŒ–ๆ‰‹ๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใชใใ€ใ‚ˆใ‚ŠๅคšใใฎใƒกใƒขใƒชใŒ็ฏ€็ด„ใ•ใ‚Œใพใ™ใ€‚็ตŒ้จ“็š„ใช่ฆณๅฏŸใ‹ใ‚‰ใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใ€NVIDIA-T4 16GB ไธŠใงใ‚ทใƒผใ‚ฑใƒณใ‚น้•ท 1024ใ€ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บ 1ใ€ๅ‹พ้…็ดฏ็ฉใ‚นใƒ†ใƒƒใƒ— 4 ใฎ llama-13b ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ + +```python +from transformers import BitsAndBytesConfig + +double_quant_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_use_double_quant=True, +) + +model_double_quant = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=double_quant_config) +``` + + +### Push quantized models on the ๐Ÿค— Hub + +`push_to_hub`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ˜็ด”ใซไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆœ€ๅˆใซ้‡ๅญๅŒ–ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใŒใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใ€ๆฌกใซ้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎ้‡ใฟใŒใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใพใ™ใ€‚ +ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€ๅฟ…ใš `bitsandbytes>0.37.2` ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ (ใ“ใฎ่จ˜ไบ‹ใฎๅŸท็ญ†ๆ™‚็‚นใงใฏใ€`bitsandbytes==0.38.0.post1` ใงใƒ†ใ‚นใƒˆใ—ใพใ—ใŸ)ใ€‚ + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model = AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m", quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m") + +model.push_to_hub("bloom-560m-8bit") +``` + + + +ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใฏใ€ใƒใƒ–ไธŠใง 8 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ“ใจใŒๅผทใๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใฏใƒกใƒขใƒช ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใฎๅ‰Šๆธ›ใจใ€ใŸใจใˆใฐ Google Colab ใงใฎๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟใซใ‚ˆใ‚‹ๆฉๆตใ‚’ๅ—ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + + +### Load a quantized model from the ๐Ÿค— Hub + +`from_pretrained`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒใƒ–ใ‹ใ‚‰้‡ๅญๅŒ–ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ๅฑžๆ€ง `quantization_config` ใŒใƒขใƒ‡ใƒซ่จญๅฎšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซๅญ˜ๅœจใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใ€ใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚ŒใŸ้‡ใฟใŒ้‡ๅญๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("{your_username}/bloom-560m-8bit", device_map="auto") +``` + +ใ“ใฎๅ ดๅˆใ€ๅผ•ๆ•ฐ `load_in_8bit=True` ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€`bitsandbytes` ใจ `accelerate` ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +ใพใŸใ€`device_map` ใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใŒใ€ๅˆฉ็”จๅฏ่ƒฝใชใƒชใ‚ฝใƒผใ‚นไธŠใงใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ™ใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซใฏ `device_map = 'auto'` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚ + +### Advanced use cases + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏใ€8 ใƒ“ใƒƒใƒˆ ใƒขใƒ‡ใƒซใฎใƒญใƒผใƒ‰ใจๅฎŸ่กŒไปฅๅค–ใซไฝ•ใŒใงใใ‚‹ใ‹ใ‚’ๆŽขๆฑ‚ใ—ใŸใ„ไธŠ็ดšใƒฆใƒผใ‚ถใƒผใ‚’ๅฏพ่ฑกใจใ—ใฆใ„ใพใ™ใ€‚ + +#### Offload between `cpu` and `gpu` + +ใ“ใฎ้ซ˜ๅบฆใชไฝฟ็”จไพ‹ใฎ 1 ใคใฏใ€ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใ€`CPU`ใจ`GPU`ใฎ้–“ใง้‡ใฟใ‚’ใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใงใใ‚‹ใ“ใจใงใ™ใ€‚ CPU ไธŠใงใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ•ใ‚Œใ‚‹้‡ใฟใฏ **8 ใƒ“ใƒƒใƒˆใซๅค‰ๆ›ใ•ใ‚Œใชใ„**ใŸใ‚ใ€`float32`ใซไฟๆŒใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใฎๆฉŸ่ƒฝใฏใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’้ฉๅˆใ•ใ›ใ€ใใฎใƒขใƒ‡ใƒซใ‚’ GPU ใจ CPU ใฎ้–“ใงใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ—ใŸใ„ใƒฆใƒผใ‚ถใƒผใ‚’ๅฏพ่ฑกใจใ—ใฆใ„ใพใ™ใ€‚ + +ใพใšใ€`transformers` ใ‹ใ‚‰ [`BitsAndBytesConfig`] ใ‚’ใƒญใƒผใƒ‰ใ—ใ€ๅฑžๆ€ง `llm_int8_enable_fp32_cpu_offload` ใ‚’ `True` ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload=True) +``` + +`bigscience/bloom-1b7`ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€`lm_head`ใ‚’้™คใใƒขใƒ‡ใƒซๅ…จไฝ“ใซโ€‹โ€‹้ฉๅˆใ™ใ‚‹ใฎใซๅๅˆ†ใช GPU RAM ใŒใ‚ใ‚‹ใจใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆฌกใฎใ‚ˆใ†ใซใ‚ซใ‚นใ‚ฟใƒ  device_map ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```python +device_map = { + "transformer.word_embeddings": 0, + "transformer.word_embeddings_layernorm": 0, + "lm_head": "cpu", + "transformer.h": 0, + "transformer.ln_f": 0, +} +``` + +ใใ—ใฆใ€ๆฌกใฎใ‚ˆใ†ใซใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +```python +model_8bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + device_map=device_map, + quantization_config=quantization_config, +) +``` + +ไปฅไธŠใงใ™๏ผใƒขใƒ‡ใƒซใ‚’ๆฅฝใ—ใ‚“ใงใใ ใ•ใ„๏ผ + +#### Play with `llm_int8_threshold` + +`llm_int8_threshold` ๅผ•ๆ•ฐใ‚’ๆ“ไฝœใ—ใฆใ€ๅค–ใ‚Œๅ€คใฎใ—ใใ„ๅ€คใ‚’ๅค‰ๆ›ดใงใใพใ™ใ€‚ ๅค–ใ‚Œๅ€ค ใจใฏใ€็‰นๅฎšใฎใ—ใใ„ๅ€คใ‚ˆใ‚Šๅคงใใ„้š ใ‚ŒใŸ็Šถๆ…‹ใฎๅ€คใงใ™ใ€‚ +ใ“ใ‚Œใฏใ€`LLM.int8()`่ซ–ๆ–‡ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ๅค–ใ‚Œๅ€คๆคœๅ‡บใฎๅค–ใ‚Œๅ€คใ—ใใ„ๅ€คใซๅฏพๅฟœใ—ใพใ™ใ€‚ใ“ใฎใ—ใใ„ๅ€คใ‚’่ถ…ใˆใ‚‹้š ใ—็Šถๆ…‹ใฎๅ€คใฏๅค–ใ‚Œๅ€คใจใฟใชใ•ใ‚Œใ€ใใ‚Œใ‚‰ใฎๅ€คใซๅฏพใ™ใ‚‹ๆ“ไฝœใฏ fp16 ใงๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚้€šๅธธใ€ๅ€คใฏๆญฃ่ฆๅˆ†ๅธƒใ—ใพใ™ใ€‚ใคใพใ‚Šใ€ใปใจใ‚“ใฉใฎๅ€คใฏ [-3.5, 3.5] ใฎ็ฏ„ๅ›ฒๅ†…ใซใ‚ใ‚Šใพใ™ใŒใ€ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใฏๅคงใใ็•ฐใชใ‚‹ๅˆ†ๅธƒใ‚’็คบใ™ไพ‹ๅค–็š„ใช็ณป็ตฑ็š„ๅค–ใ‚Œๅ€คใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅค–ใ‚Œๅ€คใฏใ€ๅคšใใฎๅ ดๅˆ [-60, -6] ใพใŸใฏ [6, 60] ใฎ็ฏ„ๅ›ฒๅ†…ใซใ‚ใ‚Šใพใ™ใ€‚ Int8 ้‡ๅญๅŒ–ใฏใ€ๅคงใใ•ใŒ 5 ็จ‹ๅบฆใพใงใฎๅ€คใงใฏใ†ใพใๆฉŸ่ƒฝใ—ใพใ™ใŒใ€ใใ‚Œใ‚’่ถ…ใˆใ‚‹ใจใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซไฝŽไธ‹ใ—ใพใ™ใ€‚้ฉๅˆ‡ใชใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ—ใใ„ๅ€คใฏ 6 ใงใ™ใŒใ€ใ‚ˆใ‚Šไธๅฎ‰ๅฎšใชใƒขใƒ‡ใƒซ (ๅฐ่ฆๆจกใชใƒขใƒ‡ใƒซใ€ๅพฎ่ชฟๆ•ด) ใงใฏใ€ใ‚ˆใ‚ŠไฝŽใ„ใ—ใใ„ๅ€คใŒๅฟ…่ฆใซใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใฎๅผ•ๆ•ฐใฏใ€ใƒขใƒ‡ใƒซใฎๆŽจ่ซ–้€Ÿๅบฆใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ฉฆใ—ใฆใฟใฆใ€ใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซๆœ€้ฉใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_threshold=10, +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + device_map=device_map, + quantization_config=quantization_config, +) +tokenizer = AutoTokenizer.from_pretrained(model_id) +``` + +#### Skip the conversion of some modules + +ไธ€้ƒจใฎใƒขใƒ‡ใƒซใซใฏใ€ๅฎ‰ๅฎšๆ€งใ‚’็ขบไฟใ™ใ‚‹ใŸใ‚ใซ 8 ใƒ“ใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ใƒขใ‚ธใƒฅใƒผใƒซใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ใ‚ธใƒฅใƒผใ‚ฏใƒœใƒƒใ‚ฏใ‚น ใƒขใƒ‡ใƒซใซใฏใ€ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ„ใใคใ‹ใฎ `lm_head` ใƒขใ‚ธใƒฅใƒผใƒซใŒใ‚ใ‚Šใพใ™ใ€‚ `llm_int8_skip_modules` ใง้Šใ‚“ใงใฟใ‚‹ + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_skip_modules=["lm_head"], +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + device_map=device_map, + quantization_config=quantization_config, +) +tokenizer = AutoTokenizer.from_pretrained(model_id) +``` + +#### Fine-tune a model that has been loaded in 8-bit + +Hugging Face ใ‚จใ‚ณใ‚ทใ‚นใƒ†ใƒ ใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฎๅ…ฌๅผใ‚ตใƒใƒผใƒˆใซใ‚ˆใ‚Šใ€8 ใƒ“ใƒƒใƒˆใงใƒญใƒผใƒ‰ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใงใใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅ˜ไธ€ใฎ Google Colab ใง`flan-t5-large`ใ‚„`facebook/opt-6.7b`ใชใฉใฎๅคง่ฆๆจกใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[`peft`](https://github.com/huggingface/peft) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใซ `device_map` ใ‚’ๆธกใ™ๅฟ…่ฆใŒใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใƒขใƒ‡ใƒซใŒ GPU ใซ่‡ชๅ‹•็š„ใซใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใƒ‡ใƒใ‚คใ‚น ใƒžใƒƒใƒ—ใ‚’็‰นๅฎšใฎใƒ‡ใƒใ‚คใ‚นใซ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ (ไพ‹: `cuda:0`ใ€`0`ใ€`torch.device('cuda:0')`)ใ€‚ `device_map=auto`ใฏๆŽจ่ซ–ใฎใฟใซไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +### BitsAndBytesConfig + +[[autodoc]] BitsAndBytesConfig + +## Quantization with ๐Ÿค— `optimum` + +`optimum`ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹้‡ๅญๅŒ–ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Optimum ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/index) ใ‚’ๅ‚็…งใ—ใ€ใ“ใ‚Œใ‚‰ใŒ่‡ชๅˆ†ใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉ็”จใงใใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/text_generation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/text_generation.md new file mode 100644 index 0000000000000000000000000000000000000000..18477d97e626d1df942604f4c806cc17268744b2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/text_generation.md @@ -0,0 +1,56 @@ + + +# Generation + +ๅ„ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใซใฏใ€ใใ‚Œใžใ‚Œใฎ `GenerationMixin` ใ‚ฏใƒฉใ‚นใซๅฎŸ่ฃ…ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆ็”ŸๆˆใฎใŸใ‚ใฎ Generate ใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- PyTorch [`~generation.GenerationMixin.generate`] ใฏ [`~generation.GenerationMixin`] ใซๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- TensorFlow [`~generation.TFGenerationMixin.generate`] ใฏ [`~generation.TFGenerationMixin`] ใซๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- Flax/JAX [`~generation.FlaxGenerationMixin.generate`] ใฏ [`~generation.FlaxGenerationMixin`] ใซๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +้ธๆŠžใ—ใŸใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใซ้–ขไฟ‚ใชใใ€[`~generation.GenerationConfig`] ใ‚’ไฝฟ็”จใ—ใฆ็”Ÿๆˆใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใƒ‘ใƒฉใƒกใƒผใ‚ฟๅŒ–ใงใใพใ™ใ€‚ +ใ‚ฏใƒฉใ‚นใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ€‚ๅ‹•ไฝœใ‚’ๅˆถๅพกใ™ใ‚‹็”Ÿๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅฎŒๅ…จใชใƒชใ‚นใƒˆใซใคใ„ใฆใฏใ€ใ“ใฎใ‚ฏใƒฉใ‚นใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +็”Ÿๆˆๆ–นๆณ•ใฎใ“ใจใ€‚ + +ใƒขใƒ‡ใƒซใฎ็”Ÿๆˆๆง‹ๆˆใ‚’ๆคœๆŸปใ™ใ‚‹ๆ–นๆณ•ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใจใฏไฝ•ใ‹ใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ขใƒ‰ใƒ›ใƒƒใ‚ฏใซๅค‰ๆ›ดใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใซใฏใ€ +ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ•ใ‚ŒใŸ็”Ÿๆˆๆง‹ๆˆใ‚’ไฝœๆˆใ—ใฆไฟๅญ˜ใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆใฏใ€ใ€Œ +[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใ‚ฌใ‚คใƒ‰](../generation_strategies)ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€้–ข้€ฃๆฉŸ่ƒฝใฎไฝฟ็”จๆ–นๆณ•ใซใคใ„ใฆใ‚‚่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒณใ‚นใƒˆใƒชใƒผใƒŸใƒณใ‚ฐใฎใ‚ˆใ†ใชใ€‚ + +## GenerationConfig + +[[autodoc]] generation.GenerationConfig + - from_pretrained + - from_model_config + - save_pretrained + +## GenerationMixin + +[[autodoc]] generation.GenerationMixin + - generate + - compute_transition_scores + +## TFGenerationMixin + +[[autodoc]] generation.TFGenerationMixin + - generate + - compute_transition_scores + +## FlaxGenerationMixin + +[[autodoc]] generation.FlaxGenerationMixin + - generate diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/tokenizer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/tokenizer.md new file mode 100644 index 0000000000000000000000000000000000000000..1cf5885bc81286d4f09dad8cca4ef1ff2e76347a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/tokenizer.md @@ -0,0 +1,80 @@ + + +# Tokenizer + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใฎๆบ–ๅ‚™ใ‚’ๆ‹…ๅฝ“ใ—ใพใ™ใ€‚ใƒฉใ‚คใƒ–ใƒฉใƒชใซใฏใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใปใจใ‚“ใฉ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎไธ€้ƒจใฏใ€ๅฎŒๅ…จใช Python ๅฎŸ่ฃ…ใจใ€ +Rust ใƒฉใ‚คใƒ–ใƒฉใƒช [๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers)ใ€‚ ใ€Œ้ซ˜้€Ÿใ€ๅฎŸ่ฃ…ใงใฏๆฌกใฎใ“ใจใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ + +1. ็‰นใซใƒใƒƒใƒใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’่กŒใ†ๅ ดๅˆใฎๅคงๅน…ใชใ‚นใƒ”ใƒผใƒ‰ใ‚ขใƒƒใƒ—ใจ +2. ๅ…ƒใฎๆ–‡ๅญ—ๅˆ— (ๆ–‡ๅญ—ใจๅ˜่ชž) ใจใƒˆใƒผใ‚ฏใƒณ็ฉบ้–“ใฎ้–“ใงใƒžใƒƒใƒ”ใƒณใ‚ฐใ™ใ‚‹่ฟฝๅŠ ใฎใƒกใ‚ฝใƒƒใƒ‰ (ไพ‹: + ็‰นๅฎšใฎๆ–‡ๅญ—ใ‚’ๅซใ‚€ใƒˆใƒผใ‚ฏใƒณใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ€ใพใŸใฏ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใซๅฏพๅฟœใ™ใ‚‹ๆ–‡ๅญ—ใฎ็ฏ„ๅ›ฒ๏ผ‰ใ€‚ + +ๅŸบๆœฌใ‚ฏใƒฉใ‚น [`PreTrainedTokenizer`] ใŠใ‚ˆใณ [`PreTrainedTokenizerFast`] +ใƒขใƒ‡ใƒซๅ…ฅๅŠ›ใฎๆ–‡ๅญ—ๅˆ—ๅ…ฅๅŠ›ใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ— (ไปฅไธ‹ใ‚’ๅ‚็…ง)ใ€Python ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–/ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใฎไธ€่ˆฌ็š„ใชใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ +ใƒญใƒผใ‚ซใƒซ ใƒ•ใ‚กใ‚คใƒซใพใŸใฏใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ€ใพใŸใฏใƒฉใ‚คใƒ–ใƒฉใƒชใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‹ใ‚‰ใฎใ€Œ้ซ˜้€Ÿใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ +(HuggingFace ใฎ AWS S3 ใƒชใƒใ‚ธใƒˆใƒชใ‹ใ‚‰ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰)ใ€‚ไบŒไบบใจใ‚‚้ ผใ‚Šใซใ—ใฆใ„ใ‚‹ใฎใฏใ€ +ๅ…ฑ้€šใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅซใ‚€ [`~tokenization_utils_base.PreTrainedTokenizerBase`] +[`~tokenization_utils_base.SpecialTokensMixin`]ใ€‚ + +ใ—ใŸใŒใฃใฆใ€[`PreTrainedTokenizer`] ใจ [`PreTrainedTokenizerFast`] ใฏใƒกใ‚คใƒณใ‚’ๅฎŸ่ฃ…ใ—ใพใ™ใ€‚ +ใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใฎใƒกใ‚ฝใƒƒใƒ‰: + +- ใƒˆใƒผใ‚ฏใƒณๅŒ– (ๆ–‡ๅญ—ๅˆ—ใ‚’ใ‚ตใƒ–ใƒฏใƒผใƒ‰ ใƒˆใƒผใ‚ฏใƒณๆ–‡ๅญ—ๅˆ—ใซๅˆ†ๅ‰ฒ)ใ€ใƒˆใƒผใ‚ฏใƒณๆ–‡ๅญ—ๅˆ—ใ‚’ ID ใซๅค‰ๆ›ใ—ใŸใ‚Šใ€ใใฎ้€†ใฎๅค‰ๆ›ใ‚’่กŒใฃใŸใ‚Šใ—ใพใ™ใ€‚ + ใ‚จใƒณใ‚ณใƒผใƒ‰/ใƒ‡ใ‚ณใƒผใƒ‰ (ใคใพใ‚Šใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใจๆ•ดๆ•ฐใธใฎๅค‰ๆ›)ใ€‚ +- ๅŸบ็คŽใจใชใ‚‹ๆง‹้€  (BPEใ€SentencePiece...) ใ‹ใ‚‰็‹ฌ็ซ‹ใ—ใŸๆ–นๆณ•ใงใ€่ชžๅฝ™ใซๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +- ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณ (ใƒžใ‚นใ‚ฏใ€ๆ–‡ใฎๅง‹ใพใ‚Šใชใฉ) ใฎ็ฎก็†: ใƒˆใƒผใ‚ฏใƒณใฎ่ฟฝๅŠ ใ€ๅฑžๆ€งใธใฎๅ‰ฒใ‚Šๅฝ“ใฆใ€‚ + ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซใ‚ˆใ‚Šใ€็ฐกๅ˜ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ไธญใซๅˆ†ๅ‰ฒใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +[`BatchEncoding`] ใฏใ€ +[`~tokenization_utils_base.PreTrainedTokenizerBase`] ใฎใ‚จใƒณใ‚ณใƒผใƒ‰ ใƒกใ‚ฝใƒƒใƒ‰ (`__call__`ใ€ +`encode_plus` ใŠใ‚ˆใณ `batch_encode_plus`) ใงใ‚ใ‚Šใ€Python ่พžๆ›ธใ‹ใ‚‰ๆดพ็”Ÿใ—ใฆใ„ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒ็ด”็ฒ‹ใช Python ใฎๅ ดๅˆ +tokenizer ใฎๅ ดๅˆใ€ใ“ใฎใ‚ฏใƒฉใ‚นใฏๆจ™ๆบ–ใฎ Python ่พžๆ›ธใจๅŒใ˜ใ‚ˆใ†ใซๅ‹•ไฝœใ—ใ€ใซใ‚ˆใฃใฆ่จˆ็ฎ—ใ•ใ‚ŒใŸใ•ใพใ–ใพใชใƒขใƒ‡ใƒซๅ…ฅๅŠ›ใ‚’ไฟๆŒใ—ใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒกใ‚ฝใƒƒใƒ‰ (`input_ids`ใ€`attention_mask`...)ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒใ€Œ้ซ˜้€Ÿใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ‚ใ‚‹ๅ ดๅˆ (ใคใพใ‚Šใ€ +HuggingFace [ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ ใƒฉใ‚คใƒ–ใƒฉใƒช](https://github.com/huggingface/tokenizers))ใ€ใ“ใฎใ‚ฏใƒฉใ‚นใฏใ•ใ‚‰ใซๆไพ›ใ—ใพใ™ +ๅ…ƒใฎๆ–‡ๅญ—ๅˆ— (ๆ–‡ๅญ—ใจๅ˜่ชž) ใจ +ใƒˆใƒผใ‚ฏใƒณใ‚นใƒšใƒผใ‚น (ไพ‹: ๆŒ‡ๅฎšใ•ใ‚ŒใŸๆ–‡ๅญ—ใพใŸใฏๅฏพๅฟœใ™ใ‚‹ๆ–‡ๅญ—ใฎ็ฏ„ๅ›ฒใ‚’ๆง‹ๆˆใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฎๅ–ๅพ—) +ไธŽใˆใ‚‰ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใซ๏ผ‰ใ€‚ + +## PreTrainedTokenizer + +[[autodoc]] PreTrainedTokenizer + - __call__ + - apply_chat_template + - batch_decode + - decode + - encode + - push_to_hub + - all + +## PreTrainedTokenizerFast + +[`PreTrainedTokenizerFast`] ใฏ [tokenizers](https://huggingface.co/docs/tokenizers) ใƒฉใ‚คใƒ–ใƒฉใƒชใซไพๅญ˜ใ—ใพใ™ใ€‚ ๐Ÿค— ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ๅ–ๅพ—ใ—ใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€ +๐Ÿค— ใƒˆใƒฉใƒณใ‚นใซ้žๅธธใซ็ฐกๅ˜ใซใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚ŒใŒใฉใฎใ‚ˆใ†ใซ่กŒใ‚ใ‚Œใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹ใซใฏใ€[๐Ÿค— tokenizers ใ‹ใ‚‰ใฎ tokenizers ใ‚’ไฝฟ็”จใ™ใ‚‹](../fast_tokenizers) ใƒšใƒผใ‚ธใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] PreTrainedTokenizerFast + - __call__ + - apply_chat_template + - batch_decode + - decode + - encode + - push_to_hub + - all + +## BatchEncoding + +[[autodoc]] BatchEncoding diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/trainer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/trainer.md new file mode 100644 index 0000000000000000000000000000000000000000..57f0a12a7efbbc6336031dbcb62f0165a37e30a1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/main_classes/trainer.md @@ -0,0 +1,727 @@ + + +# Trainer + +[`Trainer`] ใ‚ฏใƒฉใ‚นใฏใ€ใปใจใ‚“ใฉใฎๆจ™ๆบ–็š„ใชใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซๅฏพใ—ใฆใ€PyTorch ใงๆฉŸ่ƒฝใ‚’ๅฎŒๅ…จใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎ API ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€[ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples) ใฎใปใจใ‚“ใฉใงไฝฟ็”จใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +[`Trainer`] ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ๅ‰ใซใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใฎใ™ในใฆใฎใƒใ‚คใƒณใƒˆใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใŸใ‚ใซ [`TrainingArguments`] ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +ใ“ใฎ API ใฏใ€่ค‡ๆ•ฐใฎ GPU/TPU ใงใฎๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€[NVIDIA Apex](https://github.com/NVIDIA/apex) ใŠใ‚ˆใณ PyTorch ใฎใƒใ‚คใƒ†ใ‚ฃใƒ– AMP ใซใ‚ˆใ‚‹ๆททๅˆ็ฒพๅบฆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +[`Trainer`] ใซใฏใ€ไธŠ่จ˜ใฎๆฉŸ่ƒฝใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๅŸบๆœฌ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใ‚ซใ‚นใ‚ฟใƒ ๅ‹•ไฝœใ‚’ๆŒฟๅ…ฅใ™ใ‚‹ใซใฏใ€ใใ‚Œใ‚‰ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ—ใ€ๆฌกใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ—ใพใ™ใ€‚ + +- **get_train_dataloader** -- ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใƒญใƒผใƒ€ใƒผใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +- **get_eval_dataloader** -- ่ฉ•ไพก็”จใƒ‡ใƒผใ‚ฟใƒญใƒผใƒ€ใƒผใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +- **get_test_dataloader** -- ใƒ†ใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใƒญใƒผใƒ€ใƒผใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +- **log** -- ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็›ฃ่ฆ–ใ—ใฆใ„ใ‚‹ใ•ใพใ–ใพใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใ‚’ใƒญใ‚ฐใซ่จ˜้Œฒใ—ใพใ™ใ€‚ +- **create_optimizer_and_scheduler** -- ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใจๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใŒๆธกใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ—ใพใ™ใ€‚ + ๅˆๆœŸๅŒ–ใ€‚ `create_optimizer`ใƒกใ‚ฝใƒƒใƒ‰ใจ`create_scheduler`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใพใŸใฏใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใ‚‚ใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + ๅˆฅใ€…ใซใ€‚ +- **create_optimizer** -- init ใงๆธกใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใซใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ—ใพใ™ใ€‚ +- **create_scheduler** -- init ใงๆธกใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +- **compute_loss** - ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅ…ฅๅŠ›ใฎใƒใƒƒใƒใฎๆๅคฑใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ +- **training_step** -- ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใƒ†ใƒƒใƒ—ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +- **prediction_step** -- ่ฉ•ไพก/ใƒ†ใ‚นใƒˆ ใ‚นใƒ†ใƒƒใƒ—ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +- **evaluate** -- ่ฉ•ไพกใƒซใƒผใƒ—ใ‚’ๅฎŸ่กŒใ—ใ€ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ฟ”ใ—ใพใ™ใ€‚ +- **predict** -- ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใฎไบˆๆธฌ (ใƒฉใƒ™ใƒซใŒไฝฟ็”จๅฏ่ƒฝใชๅ ดๅˆใฏใƒกใƒˆใƒชใ‚ฏใ‚นใ‚‚ๅซใ‚€) ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + + +[`Trainer`] ใ‚ฏใƒฉใ‚นใฏ ๐Ÿค— Transformers ใƒขใƒ‡ใƒซ็”จใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใฆใŠใ‚Šใ€้ฉšใในใๅ‹•ไฝœใ‚’ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ +ไป–ใฎๆฉŸ็จฎใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€‚็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎ็‚นใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +- ใƒขใƒ‡ใƒซใฏๅธธใซ [`~utils.ModelOutput`] ใฎใ‚ฟใƒ—ใƒซใพใŸใฏใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใ‚’่ฟ”ใ—ใพใ™ใ€‚ +- `labels` ๅผ•ๆ•ฐใŒๆŒ‡ๅฎšใ•ใ‚Œใ€ใใฎๆๅคฑใŒๆœ€ๅˆใฎๅ€คใจใ—ใฆ่ฟ”ใ•ใ‚Œใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏๆๅคฑใ‚’่จˆ็ฎ—ใงใใพใ™ใ€‚ + ใ‚ฟใƒ—ใƒซใฎ่ฆ็ด  (ใƒขใƒ‡ใƒซใŒใ‚ฟใƒ—ใƒซใ‚’่ฟ”ใ™ๅ ดๅˆ) +- ใƒขใƒ‡ใƒซใฏ่ค‡ๆ•ฐใฎใƒฉใƒ™ใƒซๅผ•ๆ•ฐใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ใ“ใจใŒใงใใพใ™ ([`TrainingArguments`] ใง `label_names` ใ‚’ไฝฟ็”จใ—ใฆใ€ใใฎๅๅ‰ใ‚’ [`Trainer`] ใซ็คบใ—ใพใ™) ใŒใ€ใใ‚Œใ‚‰ใฎใ„ใšใ‚Œใซใ‚‚ `"label"` ใจใ„ใ†ๅๅ‰ใ‚’ไป˜ใ‘ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + + + +ไปฅไธ‹ใฏใ€ๅŠ ้‡ๆๅคฑใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซ [`Trainer`] ใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใงใ™ (ไธๅ‡่กกใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใซๅฝน็ซ‹ใกใพใ™)ใ€‚ + +```python +from torch import nn +from transformers import Trainer + + +class CustomTrainer(Trainer): + def compute_loss(self, model, inputs, return_outputs=False): + labels = inputs.pop("labels") + # forward pass + outputs = model(**inputs) + logits = outputs.get("logits") + # compute custom loss (suppose one has 3 labels with different weights) + loss_fct = nn.CrossEntropyLoss(weight=torch.tensor([1.0, 2.0, 3.0], device=model.device)) + loss = loss_fct(logits.view(-1, self.model.config.num_labels), labels.view(-1)) + return (loss, outputs) if return_outputs else loss +``` + +PyTorch [`Trainer`] ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใฎๅ‹•ไฝœใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ™ใ‚‹ใ‚‚ใ† 1 ใคใฎๆ–นๆณ•ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒซใƒผใƒ—ใฎ็Šถๆ…‹ใ‚’ๆคœๆŸปใงใใ‚‹ [callbacks](ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ) ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ (้€ฒ่กŒ็Šถๆณใƒฌใƒใƒผใƒˆใ€TensorBoard ใพใŸใฏไป–ใฎ ML ใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ใงใฎใƒญใ‚ฐ่จ˜้Œฒใชใฉ)ใ€‚ๆฑบๅฎš๏ผˆๆ—ฉๆœŸๅœๆญขใชใฉ๏ผ‰ใ€‚ + +## Trainer + +[[autodoc]] Trainer + - all + +## Seq2SeqTrainer + +[[autodoc]] Seq2SeqTrainer + - evaluate + - predict + +## TrainingArguments + +[[autodoc]] TrainingArguments + - all + +## Seq2SeqTrainingArguments + +[[autodoc]] Seq2SeqTrainingArguments + - all + +## Checkpoints + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€[`Trainer`] ใฏใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ€ +[`TrainingArguments`] ใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใ€xxx ใ‚’ๅซใ‚€`checkpoint-xxx`ใจใ„ใ†ๅๅ‰ใฎใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผใซไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚ +ใใ‚Œใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆฎต้šŽใงใ—ใŸใ€‚ + +ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใ—ใฆ [`Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ + +- `resume_from_checkpoint=True` ใฏๆœ€ๆ–ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ—ใพใ™ +- `resume_from_checkpoint=checkpoint_dir` ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชๅ†…ใฎ็‰นๅฎšใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ—ใพใ™ + ๅˆๆ ผใ—ใŸใ€‚ + +ใ•ใ‚‰ใซใ€`push_to_hub=True` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซ ใƒใƒ–ใซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ฐกๅ˜ใซไฟๅญ˜ใงใใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใ™ในใฆ +ไธญ้–“ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใซไฟๅญ˜ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏๅˆฅใฎใ‚ณใƒŸใƒƒใƒˆใซไฟๅญ˜ใ•ใ‚Œใพใ™ใŒใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใฏไฟๅญ˜ใ•ใ‚Œใพใ›ใ‚“ใ€‚้ฉๅฟœใงใใพใ™ +[`TrainingArguments`] ใฎ `hub-strategy` ๅ€คใ‚’ๆฌกใฎใ„ใšใ‚Œใ‹ใซใ—ใพใ™ใ€‚ + +- `"checkpoint"`: ๆœ€ๆ–ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚‚ last-checkpoint ใจใ„ใ†ๅๅ‰ใฎใ‚ตใƒ–ใƒ•ใ‚ฉใƒซใƒ€ใƒผใซใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใพใ™ใ€‚ + `trainer.train(resume_from_checkpoint="output_dir/last-checkpoint")` ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’็ฐกๅ˜ใซๅ†้–‹ใ—ใพใ™ใ€‚ +- `"all_checkpoints"`: ใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๅ‡บๅŠ›ใƒ•ใ‚ฉใƒซใƒ€ใƒผใซ่กจ็คบใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใพใ™ (ใ—ใŸใŒใฃใฆใ€1 ใคใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒๅพ—ใ‚‰ใ‚Œใพใ™) + ๆœ€็ต‚ใƒชใƒใ‚ธใƒˆใƒชๅ†…ใฎใƒ•ใ‚ฉใƒซใƒ€ใƒผใ”ใจใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใƒ•ใ‚ฉใƒซใƒ€ใƒผ) + +## Logging + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€[`Trainer`] ใฏใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚นใซ `logging.INFO` ใ‚’ไฝฟ็”จใ—ใ€ใƒฌใƒ—ใƒชใ‚ซใŒใ‚ใ‚‹ๅ ดๅˆใซใฏ `logging.WARNING` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฏใ€[`TrainingArguments`] ใฎ 5 ใคใฎ `logging` ใƒฌใƒ™ใƒซใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใงใใพใ™ใ€‚ +ๅผ•ๆ•ฐ: + +- `log_level` - ใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚น็”จ +- `log_level_replica` - ใƒฌใƒ—ใƒชใ‚ซ็”จ + +ใ•ใ‚‰ใซใ€[`TrainingArguments`] ใฎ `log_on_each_node` ใŒ `False` ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒกใ‚คใƒณ ใƒŽใƒผใƒ‰ใฎใฟใŒ +ใƒกใ‚คใƒณ ใƒ—ใƒญใ‚ปใ‚นใฎใƒญใ‚ฐ ใƒฌใƒ™ใƒซ่จญๅฎšใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไป–ใฎใ™ในใฆใฎใƒŽใƒผใƒ‰ใฏใƒฌใƒ—ใƒชใ‚ซใฎใƒญใ‚ฐ ใƒฌใƒ™ใƒซ่จญๅฎšใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +[`Trainer`] ใฏใ€`transformers` ใฎใƒญใ‚ฐ ใƒฌใƒ™ใƒซใ‚’ใƒŽใƒผใƒ‰ใ”ใจใซๅ€‹ๅˆฅใซ่จญๅฎšใ™ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +[`Trainer.__init__`]ใ€‚ใ—ใŸใŒใฃใฆใ€ไป–ใฎๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚’ใ‚ˆใ‚Šๆ—ฉใ่จญๅฎšใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ (ๆฌกใฎไพ‹ใ‚’ๅ‚็…ง)ใ€‚ +[`Trainer`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅ‰ใฎ `transformers` ๆฉŸ่ƒฝใ€‚ + +ใ“ใ‚Œใ‚’ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใงไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```python +[...] +logger = logging.getLogger(__name__) + +# Setup logging +logging.basicConfig( + format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", + datefmt="%m/%d/%Y %H:%M:%S", + handlers=[logging.StreamHandler(sys.stdout)], +) + +# set the main code and the modules it uses to the same log-level according to the node +log_level = training_args.get_process_log_level() +logger.setLevel(log_level) +datasets.utils.logging.set_verbosity(log_level) +transformers.utils.logging.set_verbosity(log_level) + +trainer = Trainer(...) +``` + +ใใ—ใฆใ€ใƒกใ‚คใƒณ ใƒŽใƒผใƒ‰ใจไป–ใฎใ™ในใฆใฎใƒŽใƒผใƒ‰ใง้‡่ค‡ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใ‚‚ใฎใ‚’ๅ‡บๅŠ›ใ—ใชใ„ใ‚ˆใ†ใซ่ญฆๅ‘Šใ™ใ‚‹ใ ใ‘ใ‚’่กจ็คบใ—ใŸใ„ๅ ดๅˆใฏใ€ +่ญฆๅ‘Š: ๆฌกใฎใ‚ˆใ†ใซๅฎŸ่กŒใงใใพใ™ใ€‚ + +```bash +my_app.py ... --log_level warning --log_level_replica error +``` + +ใƒžใƒซใƒใƒŽใƒผใƒ‰็’ฐๅขƒใงใ€ๅ„ใƒŽใƒผใƒ‰ใฎใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚นใฎใƒญใ‚ฐใ‚’็นฐใ‚Š่ฟ”ใ—ใŸใใชใ„ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +ไธŠ่จ˜ใ‚’ๆฌกใฎใ‚ˆใ†ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ + +```bash +my_app.py ... --log_level warning --log_level_replica error --log_on_each_node 0 +``` + +ใใฎๅพŒใ€ๆœ€ๅˆใฎใƒŽใƒผใƒ‰ใฎใƒกใ‚คใƒณ ใƒ—ใƒญใ‚ปใ‚นใฎใฟใŒใ€Œ่ญฆๅ‘Šใ€ใƒฌใƒ™ใƒซใงใƒญใ‚ฐใซ่จ˜้Œฒใ•ใ‚Œใ€ใƒกใ‚คใƒณ ใƒŽใƒผใƒ‰ไธŠใฎไป–ใฎใ™ในใฆใฎใƒ—ใƒญใ‚ปใ‚นใฏใƒญใ‚ฐใซ่จ˜้Œฒใ•ใ‚Œใพใ™ใ€‚ +ใƒŽใƒผใƒ‰ใจไป–ใฎใƒŽใƒผใƒ‰ไธŠใฎใ™ในใฆใฎใƒ—ใƒญใ‚ปใ‚นใฏใ€Œใ‚จใƒฉใƒผใ€ใƒฌใƒ™ใƒซใงใƒญใ‚ฐใซ่จ˜้Œฒใ•ใ‚Œใพใ™ใ€‚ + +ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ใงใใ‚‹ใ ใ‘้™ใ‹ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```bash +my_app.py ... --log_level error --log_level_replica error --log_on_each_node 0 +``` + +(ใƒžใƒซใƒใƒŽใƒผใƒ‰็’ฐๅขƒใฎๅ ดๅˆใฏ `--log_on_each_node 0` ใ‚’่ฟฝๅŠ ใ—ใพใ™) + +## Randomness + +[`Trainer`] ใซใ‚ˆใฃใฆ็”Ÿๆˆใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ๅ†้–‹ใ™ใ‚‹ๅ ดๅˆใ€ใ™ในใฆใฎๅŠชๅŠ›ใŒใใฎ็Šถๆ…‹ใ‚’ๅพฉๅ…ƒใ™ใ‚‹ใŸใ‚ใซ่กŒใ‚ใ‚Œใพใ™ใ€‚ +_python_ใ€_numpy_ใ€ใŠใ‚ˆใณ _pytorch_ ใฎ RNG ็Šถๆ…‹ใฏใ€ใใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใŸๆ™‚็‚นใจๅŒใ˜็Šถๆ…‹ใซใชใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ€Œๅœๆญขใ—ใฆๅ†้–‹ใ€ใจใ„ใ†ใ‚นใ‚ฟใ‚คใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒใ€ใƒŽใƒณใ‚นใƒˆใƒƒใƒ—ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฏ่ƒฝใช้™ใ‚Š่ฟ‘ใฅใ‘ใ‚‰ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ + +ใŸใ ใ—ใ€ใ•ใพใ–ใพใชใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ้žๆฑบๅฎš็š„ใช pytorch ่จญๅฎšใซใ‚ˆใ‚Šใ€ใ“ใ‚ŒใฏๅฎŒๅ…จใซๆฉŸ่ƒฝใ—ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ•ใƒซใ‚’ใ”ๅธŒๆœ›ใฎๅ ดๅˆใฏ +ๆฑบๅฎš่ซ–ใซใคใ„ใฆใฏใ€[ใƒฉใƒณใƒ€ใƒ ๆ€งใฎใ‚ฝใƒผใ‚นใฎๅˆถๅพก](https://pytorch.org/docs/stable/notes/randomness) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€ใ“ใ‚Œใ‚‰ใฎ่จญๅฎšใฎไธ€้ƒจใฏ +็‰ฉไบ‹ใ‚’ๆฑบๅฎš่ซ–็š„ใซใ™ใ‚‹ใ‚‚ใฎ (ไพ‹: `torch.backends.cudnn.deterministic`) ใฏ็‰ฉไบ‹ใ‚’้…ใใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏๅฎŸ่กŒใงใใพใ›ใ‚“ใŒใ€ๅฟ…่ฆใซๅฟœใ˜ใฆ่‡ชๅˆ†ใงๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## Specific GPUs Selection + +ใฉใฎ GPU ใ‚’ใฉใฎใ‚ˆใ†ใช้ †ๅบใงไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ใƒ—ใƒญใ‚ฐใƒฉใƒ ใซๆŒ‡็คบใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +[`DistributedDataParallel`](https://pytorch.org/docs/stable/generated/torch.nn.Parallel.DistributedDataParallel.html) ใ‚’ไฝฟ็”จใ—ใฆ GPU ใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใฎใฟใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ไฝฟ็”จใ™ใ‚‹ GPU ใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ ใ€‚ใŸใจใˆใฐใ€GPU ใŒ 4 ใคใ‚ใ‚‹ใŒใ€ๆœ€ๅˆใฎ 2 ใคใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```bash +torchrun --nproc_per_node=2 trainer-program.py ... +``` + +[`accelerate`](https://github.com/huggingface/accelerate) ใพใŸใฏ [`deepspeed`](https://github.com/deepspeedai/DeepSpeed) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใ‚’ไฝฟ็”จใ—ใฆๅŒใ˜ใ“ใจใ‚’้”ๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใฎไธ€ใค๏ผš + +```bash +accelerate launch --num_processes 2 trainer-program.py ... +``` + +```bash +deepspeed --num_gpus 2 trainer-program.py ... +``` + +ใ“ใ‚Œใ‚‰ใฎใƒฉใƒณใƒใƒฃใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใ€Accelerate ใพใŸใฏ [Deepspeed ็ตฑๅˆ](deepspeed) ๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + + +ใ“ใ‚Œใพใงใฏใ€ใƒ—ใƒญใ‚ฐใƒฉใƒ ใซไฝฟ็”จใ™ใ‚‹ GPU ใฎๆ•ฐใ‚’ๆŒ‡็คบใงใใพใ—ใŸใ€‚ๆฌกใซใ€็‰นๅฎšใฎ GPU ใ‚’้ธๆŠžใ—ใ€ใใฎ้ †ๅบใ‚’ๅˆถๅพกใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ๆฌกใฎ็’ฐๅขƒๅค‰ๆ•ฐใฏใ€ไฝฟ็”จใ™ใ‚‹ GPU ใจใใฎ้ †ๅบใ‚’ๅˆถๅพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +**`CUDA_VISIBLE_DEVICES`** + +่ค‡ๆ•ฐใฎ GPU ใŒใ‚ใ‚Šใ€ใใฎใ†ใกใฎ 1 ใคใพใŸใฏใ„ใใคใ‹ใฎ GPU ใ ใ‘ใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€็’ฐๅขƒๅค‰ๆ•ฐ `CUDA_VISIBLE_DEVICES` ใ‚’ไฝฟ็”จใ™ใ‚‹ GPU ใฎใƒชใ‚นใƒˆใซ่จญๅฎšใ—ใพใ™ใ€‚ + +ใŸใจใˆใฐใ€4 ใคใฎ GPU (0ใ€1ใ€2ใ€3) ใŒใ‚ใ‚‹ใจใ—ใพใ™ใ€‚็‰ฉ็† GPU 0 ใจ 2 ใฎใฟใงๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES=0,2 torchrun trainer-program.py ... +``` + +ใ—ใŸใŒใฃใฆใ€pytorch ใฏ 2 ใคใฎ GPU ใฎใฟใ‚’่ช่ญ˜ใ—ใ€็‰ฉ็† GPU 0 ใจ 2 ใฏใใ‚Œใžใ‚Œ `cuda:0` ใจ `cuda:1` ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + +้ †ๅบใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES=2,0 torchrun trainer-program.py ... +``` + +ใ“ใ“ใงใฏใ€็‰ฉ็† GPU 0 ใจ 2 ใŒใใ‚Œใžใ‚Œ`cuda:1`ใจ`cuda:0`ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ไธŠ่จ˜ใฎไพ‹ใฏใ™ในใฆ `DistributedDataParallel` ไฝฟ็”จใƒ‘ใ‚ฟใƒผใƒณใฎใ‚‚ใฎใงใ™ใŒใ€ๅŒใ˜ๆ–นๆณ•ใŒ [`DataParallel`](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html) ใงใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + + +```bash +CUDA_VISIBLE_DEVICES=2,0 python trainer-program.py ... +``` + +GPU ใฎใชใ„็’ฐๅขƒใ‚’ใ‚จใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ“ใฎ็’ฐๅขƒๅค‰ๆ•ฐใ‚’็ฉบใฎๅ€คใซ่จญๅฎšใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES= python trainer-program.py ... +``` + +ไป–ใฎ็’ฐๅขƒๅค‰ๆ•ฐใจๅŒๆง˜ใซใ€ใ“ใ‚Œใ‚‰ใ‚’ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณใซ่ฟฝๅŠ ใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ๆฌกใฎใ‚ˆใ†ใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```bash +export CUDA_VISIBLE_DEVICES=0,2 +torchrun trainer-program.py ... +``` + +ใŸใ ใ—ใ€ใ“ใฎๆ–นๆณ•ใงใฏใ€ไปฅๅ‰ใซ็’ฐๅขƒๅค‰ๆ•ฐใ‚’่จญๅฎšใ—ใŸใ“ใจใ‚’ๅฟ˜ใ‚Œใฆใ€ใชใœ้–“้•ใฃใŸ GPU ใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใฎใ‹็†่งฃใงใใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ๆททไนฑใ‚’ๆ‹›ใๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎใปใจใ‚“ใฉใฎไพ‹ใง็คบใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€ๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ ใƒฉใ‚คใƒณใง็‰นๅฎšใฎๅฎŸ่กŒใซๅฏพใ—ใฆใฎใฟ็’ฐๅขƒๅค‰ๆ•ฐใ‚’่จญๅฎšใ™ใ‚‹ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ + +**`CUDA_DEVICE_ORDER`** + +็‰ฉ็†ใƒ‡ใƒใ‚คใ‚นใฎ้ †ๅบใ‚’ๅˆถๅพกใ™ใ‚‹่ฟฝๅŠ ใฎ็’ฐๅขƒๅค‰ๆ•ฐ `CUDA_DEVICE_ORDER` ใŒใ‚ใ‚Šใพใ™ใ€‚้ธๆŠž่‚ขใฏๆฌกใฎ 2 ใคใงใ™ใ€‚ + +1. PCIe ใƒใ‚น ID ้ † (`nvidia-smi` ใฎ้ †ๅบใจไธ€่‡ด) - ใ“ใ‚ŒใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใ™ใ€‚ + +```bash +export CUDA_DEVICE_ORDER=PCI_BUS_ID +``` + +2. GPU ใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐ่ƒฝๅŠ›้ †ใซไธฆในใ‚‹ + +```bash +export CUDA_DEVICE_ORDER=FASTEST_FIRST +``` + +ใปใจใ‚“ใฉใฎๅ ดๅˆใ€ใ“ใฎ็’ฐๅขƒๅค‰ๆ•ฐใ‚’ๆฐ—ใซใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅคใ„ GPU ใจๆ–ฐใ—ใ„ GPU ใŒ็‰ฉ็†็š„ใซๆŒฟๅ…ฅใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€้…ใ„ๅคใ„ใ‚ซใƒผใƒ‰ใŒ้…ใใชใฃใฆใ„ใ‚‹ใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ‚ˆใ†ใชๅใฃใŸใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ‚’่กŒใฃใฆใ„ใ‚‹ๅ ดๅˆใซใฏใ€้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ๅˆใ‚ใ€‚ใ“ใ‚Œใ‚’่งฃๆฑบใ™ใ‚‹ 1 ใคใฎๆ–นๆณ•ใฏใ€ใ‚ซใƒผใƒ‰ใ‚’ไบคๆ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใŸใ ใ—ใ€ใ‚ซใƒผใƒ‰ใ‚’ไบคๆ›ใงใใชใ„ๅ ดๅˆ (ใƒ‡ใƒใ‚คใ‚นใฎๅ†ทๅดใŒๅฝฑ้Ÿฟใ‚’ๅ—ใ‘ใŸๅ ดๅˆใชใฉ)ใ€`CUDA_DEVICE_ORDER=FASTEST_FIRST`ใ‚’่จญๅฎšใ™ใ‚‹ใจใ€ๅธธใซๆ–ฐใ—ใ„้ซ˜้€Ÿใ‚ซใƒผใƒ‰ใŒๆœ€ๅˆใซ้…็ฝฎใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€`nvidia-smi`ใฏไพ็„ถใจใ—ใฆ PCIe ใฎ้ †ๅบใงใƒฌใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใ€ๅคšๅฐ‘ๆททไนฑใ™ใ‚‹ใงใ—ใ‚‡ใ†ใ€‚ + +้ †ๅบใ‚’ๅ…ฅใ‚Œๆ›ฟใˆใ‚‹ใ‚‚ใ† 1 ใคใฎ่งฃๆฑบ็ญ–ใฏใ€ไปฅไธ‹ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```bash +export CUDA_VISIBLE_DEVICES=1,0 +``` + +ใ“ใฎไพ‹ใงใฏ 2 ใคใฎ GPU ใ ใ‘ใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใŒใ€ใ‚‚ใกใ‚ใ‚“ใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผใซๆญ่ผ‰ใ•ใ‚Œใฆใ„ใ‚‹ๆ•ฐใฎ GPU ใซใ‚‚ๅŒใ˜ใ“ใจใŒๅฝ“ใฆใฏใพใ‚Šใพใ™ใ€‚ + +ใพใŸใ€ใ“ใฎ็’ฐๅขƒๅค‰ๆ•ฐใ‚’่จญๅฎšใ™ใ‚‹ๅ ดๅˆใฏใ€`~/.bashrc` ใƒ•ใ‚กใ‚คใƒซใพใŸใฏใใฎไป–ใฎ่ตทๅ‹•่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซ่จญๅฎšใ—ใฆใ€ๅฟ˜ใ‚Œใ‚‹ใฎใŒๆœ€ๅ–„ใงใ™ใ€‚ + +## Trainer Integrations + +[`Trainer`] ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅЇ็š„ใซๆ”นๅ–„ใ™ใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ‚ˆใ†ใซๆ‹กๅผตใ•ใ‚Œใพใ—ใŸใ€‚ +ๆ™‚้–“ใจใฏใ‚‹ใ‹ใซๅคงใใชใƒขใƒ‡ใƒซใซ้ฉๅˆใ—ใพใ™ใ€‚ + +็พๅœจใ€ใ‚ตใƒผใƒ‰ใƒ‘ใƒผใƒ†ใ‚ฃใฎใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณ [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) ใŠใ‚ˆใณ [PyTorch FSDP](https://pytorch.org/docs/stable/fsdp.html) ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚่ซ–ๆ–‡ [ZeRO: ใƒกใƒขใƒชใฎๆœ€้ฉๅŒ–ๅ…†ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅ‘ใ‘ใฆใ€Samyam Rajbhandariใ€Jeff Rasleyใ€Olatunji Ruwaseใ€Yuxiong He ่‘—](https://huggingface.co/papers/1910.02054)ใ€‚ + +ใ“ใฎๆไพ›ใ•ใ‚Œใ‚‹ใ‚ตใƒใƒผใƒˆใฏใ€ใ“ใฎ่จ˜ไบ‹ใฎๅŸท็ญ†ๆ™‚็‚นใงใฏๆ–ฐใ—ใใฆๅฎŸ้จ“็š„ใชใ‚‚ใฎใงใ™ใ€‚ DeepSpeed ใจ PyTorch FSDP ใฎใ‚ตใƒใƒผใƒˆใฏใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ใงใ‚ใ‚Šใ€ใใ‚Œใซ้–ขใ™ใ‚‹ๅ•้กŒใฏๆญ“่ฟŽใ—ใพใ™ใŒใ€FairScale ็ตฑๅˆใฏ PyTorch ใƒกใ‚คใƒณใซ็ตฑๅˆใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚‚ใ†ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ ([PyTorch FSDP ็ตฑๅˆ](#pytorch-fully-sharded-data-parallel)) + + + +### CUDA Extension Installation Notes + +ใ“ใฎ่จ˜ไบ‹ใฎๅŸท็ญ†ๆ™‚็‚นใงใฏใ€Deepspeed ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€CUDA C++ ใ‚ณใƒผใƒ‰ใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ™ในใฆใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซใฎๅ•้กŒใฏใ€[Deepspeed](https://github.com/deepspeedai/DeepSpeed/issues) ใฎๅฏพๅฟœใ™ใ‚‹ GitHub ใฎๅ•้กŒใ‚’้€šใ˜ใฆๅฏพๅ‡ฆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€ใƒ“ใƒซใƒ‰ไธญใซ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ไธ€่ˆฌ็š„ใชๅ•้กŒใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ +CUDA ๆ‹กๅผตๆฉŸ่ƒฝใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ PyTorch ๆ‹กๅผตๆฉŸ่ƒฝใ€‚ + +ใ—ใŸใŒใฃใฆใ€ๆฌกใฎๆ“ไฝœใ‚’ๅฎŸ่กŒไธญใซ CUDA ้–ข้€ฃใฎใƒ“ใƒซใƒ‰ใฎๅ•้กŒใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏใ€ๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```bash +pip install deepspeed +``` + +ใพใšๆฌกใฎๆณจๆ„ไบ‹้ …ใ‚’ใŠ่ชญใฟใใ ใ•ใ„ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒŽใƒผใƒˆใงใฏใ€`pytorch` ใŒ CUDA `10.2` ใงใƒ“ใƒซใƒ‰ใ•ใ‚ŒใŸๅ ดๅˆใซไฝ•ใ‚’ใ™ในใใ‹ใฎไพ‹ใ‚’็คบใ—ใพใ™ใ€‚ใ‚ใชใŸใฎ็ŠถๆณใŒๆฌกใฎใ‚ˆใ†ใชๅ ดๅˆ +็•ฐใชใ‚‹ๅ ดๅˆใฏใ€ใƒใƒผใ‚ธใƒงใƒณ็•ชๅทใ‚’็›ฎ็š„ใฎใƒใƒผใ‚ธใƒงใƒณใซ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ + +#### Possible problem #1 + +Pytorch ใซใฏ็‹ฌ่‡ชใฎ CUDA ใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใŒไป˜ๅฑžใ—ใฆใ„ใพใ™ใŒใ€ใ“ใ‚Œใ‚‰ 2 ใคใฎใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใƒ“ใƒซใƒ‰ใ™ใ‚‹ใซใฏใ€ๅŒไธ€ใƒใƒผใ‚ธใƒงใƒณใฎ CUDA ใŒๅฟ…่ฆใงใ™ใ€‚ +ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใพใ™ใ€‚ + +ใŸใจใˆใฐใ€Python ็’ฐๅขƒใซ `cudatoolkit==10.2` ใ‚’ๆŒ‡ๅฎšใ—ใฆ `pytorch` ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅ ดๅˆใฏใ€ๆฌกใฎใ‚‚ใฎใ‚‚ๅฟ…่ฆใงใ™ใ€‚ +CUDA `10.2` ใŒใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใพใ—ใŸใ€‚ + +ๆญฃ็ขบใชๅ ดๆ‰€ใฏใ‚ทใ‚นใƒ†ใƒ ใซใ‚ˆใฃใฆ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใŒใ€ๅคšใใฎใ‚ทใ‚นใƒ†ใƒ ใงใฏ`/usr/local/cuda-10.2`ใŒๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชๅ ดๆ‰€ใงใ™ใ€‚ +Unix ใ‚ทใ‚นใƒ†ใƒ ใ€‚ CUDA ใŒๆญฃใ—ใ่จญๅฎšใ•ใ‚Œใ€`PATH`็’ฐๅขƒๅค‰ๆ•ฐใซ่ฟฝๅŠ ใ•ใ‚Œใ‚‹ใจใ€ +ๆฌกใฎใ‚ˆใ†ใซใ—ใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + + +```bash +which nvcc +``` + +CUDA ใŒใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€ๆœ€ๅˆใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„ใ€‚ใŠๆฐ—ใซๅ…ฅใ‚Šใ‚’ไฝฟ็”จใ—ใฆๆ‰‹้ †ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ +ๆคœ็ดขใ‚จใƒณใ‚ธใƒณใ€‚ใŸใจใˆใฐใ€Ubuntu ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€[ubuntu cuda 10.2 install](https://www.google.com/search?q=ubuntu+cuda+10.2+install) ใ‚’ๆคœ็ดขใ™ใ‚‹ใจใ‚ˆใ„ใงใ—ใ‚‡ใ†ใ€‚ + +#### Possible problem #2 + +ใ‚‚ใ† 1 ใคใฎ่€ƒใˆใ‚‰ใ‚Œใ‚‹ไธ€่ˆฌ็š„ใชๅ•้กŒใฏใ€ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซ่ค‡ๆ•ฐใฎ CUDA ใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚ใŸใจใˆใฐใ‚ใชใŸ +ใŒใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Š๏ผš + +```bash +/usr/local/cuda-10.2 +/usr/local/cuda-11.0 +``` + +ใ“ใฎ็Šถๆณใงใฏใ€`PATH` ใŠใ‚ˆใณ `LD_LIBRARY_PATH` ็’ฐๅขƒๅค‰ๆ•ฐใซไปฅไธ‹ใŒๅซใพใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +็›ฎ็š„ใฎ CUDA ใƒใƒผใ‚ธใƒงใƒณใธใฎๆญฃใ—ใ„ใƒ‘ใ‚นใ€‚้€šๅธธใ€ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธ ใ‚คใƒณใ‚นใƒˆใƒผใƒฉใƒผใฏใ€ใ“ใ‚Œใ‚‰ใซใ€ +ๆœ€ๅพŒใฎใƒใƒผใ‚ธใƒงใƒณใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใพใ—ใŸใ€‚้ฉๅˆ‡ใชใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ใŸใ‚ใซใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎใƒ“ใƒซใƒ‰ใŒๅคฑๆ•—ใ™ใ‚‹ใจใ„ใ†ๅ•้กŒใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏใ€ +CUDA ใƒใƒผใ‚ธใƒงใƒณใŒใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€ๅ‰่ฟฐใฎ 2 ใคใ‚’่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ +็’ฐๅขƒๅค‰ๆ•ฐใ€‚ + +ใพใšใ€ใใฎๅ†…ๅฎนใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```bash +echo $PATH +echo $LD_LIBRARY_PATH +``` + +ใใ‚Œใงใ€ไธญใซไฝ•ใŒๅ…ฅใฃใฆใ„ใ‚‹ใ‹ใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +`LD_LIBRARY_PATH` ใŒ็ฉบใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +`PATH` ใฏๅฎŸ่กŒๅฏ่ƒฝใƒ•ใ‚กใ‚คใƒซใŒๅญ˜ๅœจใ™ใ‚‹ๅ ดๆ‰€ใ‚’ใƒชใ‚นใƒˆใ—ใ€`LD_LIBRARY_PATH` ใฏๅ…ฑๆœ‰ใƒฉใ‚คใƒ–ใƒฉใƒชใฎๅ ดๆ‰€ใ‚’็คบใ—ใพใ™ใ€‚ +ๆŽขใ™ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใฎๅ ดๅˆใ‚‚ใ€ๅ‰ใฎใ‚จใƒณใƒˆใƒชใŒๅพŒใฎใ‚จใƒณใƒˆใƒชใ‚ˆใ‚Šๅ„ชๅ…ˆใ•ใ‚Œใพใ™ใ€‚ `:` ใฏ่ค‡ๆ•ฐใ‚’ๅŒบๅˆ‡ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ +ใ‚จใƒณใƒˆใƒชใ€‚ + +ใ“ใ“ใงใ€ใƒ“ใƒซใƒ‰ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใซ็‰นๅฎšใฎ CUDA ใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใฎๅ ดๆ‰€ใ‚’ๆŒ‡็คบใ™ใ‚‹ใซใฏใ€ๆœ€ๅˆใซใƒชใ‚นใƒˆใ•ใ‚Œใ‚‹ๅธŒๆœ›ใฎใƒ‘ใ‚นใ‚’ๆŒฟๅ…ฅใ—ใพใ™ใ€‚ +ใ‚„ใฃใฆใ„ใ‚‹ใ“ใจ๏ผš + +```bash +export PATH=/usr/local/cuda-10.2/bin:$PATH +export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH +``` + +ๆ—ขๅญ˜ใฎๅ€คใ‚’ไธŠๆ›ธใใ™ใ‚‹ใฎใงใฏใชใใ€ๅ…ˆ้ ญใซ่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ‚‚ใกใ‚ใ‚“ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใƒใƒผใ‚ธใƒงใƒณ็•ชๅทใ‚„ใƒ•ใƒซใƒ‘ใ‚นใ‚’่ชฟๆ•ดใ—ใพใ™ใ€‚ๅ‰ฒใ‚Šๅฝ“ใฆใŸใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒๅฎŸ้š›ใซๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ +ๅญ˜ๅœจใ™ใ‚‹ใ€‚ `lib64` ใ‚ตใƒ–ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฏใ€`libcudart.so` ใชใฉใฎใ•ใพใ–ใพใช CUDA `.so` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒๅญ˜ๅœจใ™ใ‚‹ๅ ดๆ‰€ใงใ™ใ€‚ +ใ‚ทใ‚นใƒ†ใƒ ใงใฏๅˆฅใฎๅๅ‰ใŒไป˜ใ‘ใ‚‰ใ‚Œใพใ™ใŒใ€็พๅฎŸใ‚’ๅๆ˜ ใ™ใ‚‹ใ‚ˆใ†ใซ่ชฟๆ•ดใ—ใฆใใ ใ•ใ„ใ€‚ + +#### Possible problem #3 + +ไธ€้ƒจใฎๅคใ„ CUDA ใƒใƒผใ‚ธใƒงใƒณใฏใ€ๆ–ฐใ—ใ„ใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใฎใƒ“ใƒซใƒ‰ใ‚’ๆ‹’ๅฆใ™ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ใ‚ใชใŸใฏ`gcc-9`ใ‚’ๆŒใฃใฆใ„ใพใ™ใŒใ€ใใ‚ŒใŒๅฟ…่ฆใงใ™ +`gcc-7`ใ€‚ + +ใใ‚Œใซใฏใ•ใพใ–ใพใชๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€ๆ–ฐใฎ CUDA ใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใ‚‹ๅ ดๅˆใฏใ€้€šๅธธใ€ๆ–ฐใ—ใ„ใ‚ณใƒณใƒ‘ใ‚คใƒฉใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใฏใšใงใ™ใ€‚ + +ใ‚ใ‚‹ใ„ใฏใ€ๆ—ขใซๆ‰€ๆœ‰ใ—ใฆใ„ใ‚‹ใ‚ณใƒณใƒ‘ใ‚คใƒฉใซๅŠ ใˆใฆใ€ไธ‹ไฝใƒใƒผใ‚ธใƒงใƒณใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใ™ใงใซๅญ˜ๅœจใ—ใพใ™ใŒใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใชใ„ใŸใ‚ใ€ใƒ“ใƒซใƒ‰ใ‚ทใ‚นใƒ†ใƒ ใฏใใ‚Œใ‚’่ช่ญ˜ใงใใพใ›ใ‚“ใ€‚ ใ€Œgcc-7ใ€ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใŒใ€ +ใƒ“ใƒซใƒ‰ใ‚ทใ‚นใƒ†ใƒ ใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ใจใ„ใ†ใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’่กจ็คบใ™ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎๆ–นๆณ•ใง่งฃๆฑบใงใใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +```bash +sudo ln -s /usr/bin/gcc-7 /usr/local/cuda-10.2/bin/gcc +sudo ln -s /usr/bin/g++-7 /usr/local/cuda-10.2/bin/g++ +``` + +ใ“ใ“ใงใฏใ€`/usr/local/cuda-10.2/bin/gcc` ใ‹ใ‚‰ `gcc-7` ใธใฎใ‚ทใƒณใƒœใƒชใƒƒใ‚ฏใƒชใƒณใ‚ฏใ‚’ไฝœๆˆใ—ใฆใ„ใพใ™ใ€‚ +`/usr/local/cuda-10.2/bin/` ใฏ `PATH` ็’ฐๅขƒๅค‰ๆ•ฐๅ†…ใซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ (ๅ‰ใฎๅ•้กŒใฎ่งฃๆฑบ็ญ–ใ‚’ๅ‚็…ง)ใ€‚ +`gcc-7` (ใŠใ‚ˆใณ `g++7`) ใŒ่ฆ‹ใคใ‹ใ‚‹ใฏใšใงใ€ใƒ“ใƒซใƒ‰ใฏๆˆๅŠŸใ—ใพใ™ใ€‚ + +ใ„ใคใ‚‚ใฎใ‚ˆใ†ใซใ€็Šถๆณใซๅˆใ‚ใ›ใฆไพ‹ใฎใƒ‘ใ‚นใ‚’็ทจ้›†ใ—ใฆใใ ใ•ใ„ใ€‚ + +### PyTorch Fully Sharded Data parallel + +ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใงๅทจๅคงใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใซใฏใ€ๅฎŒๅ…จใซใ‚ทใƒฃใƒผใƒ‰ๅŒ–ใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟไธฆๅˆ—ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ +ใ“ใฎใ‚ฟใ‚คใƒ—ใฎใƒ‡ใƒผใ‚ฟไธฆๅˆ—ใƒ‘ใƒฉใƒ€ใ‚คใƒ ใงใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ใ€ๅ‹พ้…ใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใงใ€ใ‚ˆใ‚Šๅคšใใฎใƒ‡ใƒผใ‚ฟใจๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚ฃใƒƒใƒ†ใ‚ฃใƒณใ‚ฐใงใใพใ™ใ€‚ +ใ“ใฎๆฉŸ่ƒฝใจใใฎๅˆฉ็‚นใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅฎŒๅ…จใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ใƒ–ใƒญใ‚ฐ](https://pytorch.org/blog/introducing-pytorch-full-sharded-data-Parallel-api/) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ +ๆœ€ๆ–ฐใฎ PyTorch ใฎ Fully Sharded Data Parallel (FSDP) ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆฉŸ่ƒฝใ‚’็ตฑๅˆใ—ใพใ—ใŸใ€‚ +ๅฟ…่ฆใชใฎใฏใ€่จญๅฎšใ‚’้€šใ˜ใฆๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใ ใ‘ใงใ™ใ€‚ + +**FSDP ใ‚ตใƒใƒผใƒˆใซๅฟ…่ฆใช PyTorch ใƒใƒผใ‚ธใƒงใƒณ**: PyTorch Nightly (ใƒชใƒชใƒผใ‚นๅพŒใซใ“ใ‚Œใ‚’่ชญใ‚“ใ ๅ ดๅˆใฏ 1.12.0) +FSDP ใ‚’ๆœ‰ๅŠนใซใ—ใŸใƒขใƒ‡ใƒซใฎไฟๅญ˜ใฏใ€ๆœ€่ฟ‘ใฎไฟฎๆญฃใงใฎใฟๅˆฉ็”จใงใใ‚‹ใŸใ‚ใงใ™ใ€‚ + +**ไฝฟ็”จๆณ•**๏ผš + +- ้…ๅธƒใ•ใ‚ŒใŸใƒฉใƒณใƒใƒฃใƒผใŒ่ฟฝๅŠ ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ +ใพใ ไฝฟ็”จใ—ใฆใ„ใชใ„ๅ ดๅˆใฏใ€`-m torch.distributed.launch --nproc_per_node=NUMBER_OF_GPUS_YOU_HAVE`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +- **ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆˆฆ็•ฅ**: + - FULL_SHARD : ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ใƒฏใƒผใ‚ซใƒผ/GPU ใซใ‚ใŸใ‚‹ใ‚ทใƒฃใƒผใƒ‰ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ + ๅ‹พ้… + ใƒขใƒ‡ใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ€‚ + ใ“ใฎใŸใ‚ใซใฏใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp full_shard`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + - SHARD_GRAD_OP : ใ‚ทใƒฃใƒผใƒ‰ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใฎ็Šถๆ…‹ + ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ใƒฏใƒผใ‚ซใƒผ/GPU ๅ…จไฝ“ใฎๅ‹พ้…ใ€‚ + ใ“ใฎใŸใ‚ใซใฏใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp shard_grad_op`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + - NO_SHARD : ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใชใ—ใ€‚ใ“ใฎใŸใ‚ใซใฏใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp no_shard`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +- ใƒ‘ใƒฉใƒกใƒผใ‚ฟใจๅ‹พ้…ใ‚’ CPU ใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€ + ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp "full_shard offload"`ใพใŸใฏ`--fsdp "shard_grad_op offload"`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +- `default_auto_wrap_policy` ใ‚’ไฝฟ็”จใ—ใฆ FSDP ใงใƒฌใ‚คใƒคใƒผใ‚’่‡ชๅ‹•็š„ใซๅ†ๅธฐ็š„ใซใƒฉใƒƒใƒ—ใ™ใ‚‹ใซใฏใ€ + ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp "full_shard auto_wrap"`ใพใŸใฏ`--fsdp "shard_grad_op auto_wrap"`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +- CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใจ่‡ชๅ‹•ใƒฉใƒƒใƒ”ใƒณใ‚ฐใฎไธกๆ–นใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ + ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณๅผ•ๆ•ฐใซ`--fsdp "full_shard offload auto_wrap"`ใพใŸใฏ`--fsdp "shard_grad_op offload auto_wrap"`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +- ๆฎ‹ใ‚Šใฎ FSDP ๆง‹ๆˆใฏใ€`--fsdp_config `ใ‚’ไป‹ใ—ใฆๆธกใ•ใ‚Œใพใ™ใ€‚ใใ‚Œใฏใ€ๆฌกใฎใ„ใšใ‚Œใ‹ใฎๅ ดๆ‰€ใงใ™ใ€‚ + FSDP json ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ (ไพ‹: `fsdp_config.json`)ใ€ใพใŸใฏใ™ใงใซใƒญใƒผใƒ‰ใ•ใ‚Œใฆใ„ใ‚‹ json ใƒ•ใ‚กใ‚คใƒซใ‚’ `dict` ใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + - ่‡ชๅ‹•ใƒฉใƒƒใƒ”ใƒณใ‚ฐใŒๆœ‰ๅŠนใชๅ ดๅˆใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ— ใƒใƒชใ‚ทใƒผใพใŸใฏใ‚ตใ‚คใ‚บ ใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ— ใƒใƒชใ‚ทใƒผใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + - ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ—ใƒใƒชใ‚ทใƒผใฎๅ ดๅˆใ€ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใง `fsdp_transformer_layer_cls_to_wrap` ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ๆŒ‡ๅฎšใ—ใชใ„ๅ ดๅˆใ€ไฝฟ็”จๅฏ่ƒฝใชๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใฏ `model._no_split_modules` ใซใชใ‚Šใพใ™ใ€‚ + ใ“ใ‚Œใฏใ€ใƒฉใƒƒใƒ—ใ™ใ‚‹ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผๅฑคใ‚ฏใƒฉใ‚นๅใฎใƒชใ‚นใƒˆ (ๅคงๆ–‡ๅญ—ใจๅฐๆ–‡ๅญ—ใ‚’ๅŒบๅˆฅ) ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ (ไพ‹: [`BertLayer`]ใ€[`GPTJBlock`]ใ€[`T5Block`] ...)ใ€‚ + ้‡ใฟใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใ‚ตใƒ–ใƒขใ‚ธใƒฅใƒผใƒซ (ๅŸ‹ใ‚่พผใฟๅฑคใชใฉ) ใŒ็•ฐใชใ‚‹ FSDP ใƒฉใƒƒใƒ—ใ•ใ‚ŒใŸใƒฆใƒ‹ใƒƒใƒˆใซใชใ‚‰ใชใ„ใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ้‡่ฆใงใ™ใ€‚ + ใ“ใฎใƒใƒชใ‚ทใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒžใƒซใƒใƒ˜ใƒƒใƒ‰ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจใใ‚Œใซ็ถšใใ„ใใคใ‹ใฎ MLP ใƒฌใ‚คใƒคใƒผใ‚’ๅซใ‚€ใƒ–ใƒญใƒƒใ‚ฏใ”ใจใซใƒฉใƒƒใƒ”ใƒณใ‚ฐใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ + ๅ…ฑๆœ‰ๅŸ‹ใ‚่พผใฟใ‚’ๅซใ‚€ๆฎ‹ใ‚Šใฎๅฑคใฏใ€ๅŒใ˜ๆœ€ใ‚‚ๅค–ๅดใฎ FSDP ใƒฆใƒ‹ใƒƒใƒˆใซใƒฉใƒƒใƒ—ใ•ใ‚Œใ‚‹ใฎใŒไพฟๅˆฉใงใ™ใ€‚ + ใ—ใŸใŒใฃใฆใ€ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใซใฏใ“ใ‚Œใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + - ใ‚ตใ‚คใ‚บใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ—ใƒใƒชใ‚ทใƒผใฎๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซ`fsdp_min_num_params`ใ‚’่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„ใ€‚ + ่‡ชๅ‹•ใƒฉใƒƒใƒ”ใƒณใ‚ฐใฎใŸใ‚ใฎ FSDP ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆœ€ๅฐๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + - ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `fsdp_backward_prefetch` ใ‚’ๆŒ‡ๅฎšใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ๆฌกใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎใ‚ปใƒƒใƒˆใ‚’ใ„ใคใƒ—ใƒชใƒ•ใ‚งใƒƒใƒใ™ใ‚‹ใ‹ใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ + `backward_pre` ใจ `backward_pos` ใŒๅˆฉ็”จๅฏ่ƒฝใชใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใ€‚ + ่ฉณ็ดฐใซใคใ„ใฆใฏใ€`torch.distributed.fsdp.full_sharded_data_Parallel.BackwardPrefetch`ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + - ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `fsdp_forward_prefetch` ใ‚’ๆŒ‡ๅฎšใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ๆฌกใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎใ‚ปใƒƒใƒˆใ‚’ใ„ใคใƒ—ใƒชใƒ•ใ‚งใƒƒใƒใ™ใ‚‹ใ‹ใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ + `True`ใฎๅ ดๅˆใ€FSDP ใฏใƒ•ใ‚ฉใƒฏใƒผใƒ‰ ใƒ‘ใ‚นใงใฎๅฎŸ่กŒไธญใซใ€ๆฌกใซๆฅใ‚‹ใ‚ชใƒผใƒซใ‚ฎใƒฃใ‚ถใƒผใ‚’ๆ˜Ž็คบ็š„ใซใƒ—ใƒชใƒ•ใ‚งใƒƒใƒใ—ใพใ™ใ€‚ + - ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใง `limit_all_gathers` ใ‚’ๆŒ‡ๅฎšใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + `True`ใฎๅ ดๅˆใ€FSDP ใฏ CPU ใ‚นใƒฌใƒƒใƒ‰ใ‚’ๆ˜Ž็คบ็š„ใซๅŒๆœŸใ—ใฆใ€ๅฎŸ่กŒไธญใฎใ‚ชใƒผใƒซใ‚ฎใƒฃใ‚ถใŒๅคšใ™ใŽใ‚‹ใฎใ‚’้˜ฒใŽใพใ™ใ€‚ + - `activation_checkpointing`ใ‚’่จญๅฎšใƒ•ใ‚กใ‚คใƒซใงๆŒ‡ๅฎšใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + `True`ใฎๅ ดๅˆใ€FSDP ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€FSDP ใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ใ‚ฏใƒชใ‚ขใ™ใ‚‹ใ“ใจใงใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๅ‰Šๆธ›ใ™ใ‚‹ๆ‰‹ๆณ•ใงใ™ใ€‚ + ็‰นๅฎšใฎใƒฌใ‚คใƒคใƒผใ‚’ๅ‡ฆ็†ใ—ใ€ใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ ใƒ‘ใ‚นไธญใซใใ‚Œใ‚‰ใ‚’ๅ†่จˆ็ฎ—ใ—ใพใ™ใ€‚ไบ‹ๅฎŸไธŠใ€ใ“ใ‚Œใฏไฝ™ๅˆ†ใช่จˆ็ฎ—ๆ™‚้–“ใ‚’็Š ็‰ฒใซใ—ใพใ™ + ใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ + +**ๆณจๆ„ใ™ในใๆณจๆ„็‚นใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™** +- ใ“ใ‚Œใฏ `generate` ใจไบ’ๆ›ๆ€งใŒใชใ„ใŸใ‚ใ€ `--predict_with_generate` ใจใ‚‚ไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ›ใ‚“ + ใ™ในใฆใฎ seq2seq/clm ใ‚นใ‚ฏใƒชใƒ—ใƒˆ (็ฟป่จณ/่ฆ็ด„/clm ใชใฉ)ใ€‚ + ๅ•้กŒ [#21667](https://github.com/huggingface/transformers/issues/21667) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### PyTorch/XLA Fully Sharded Data parallel + +TPU ใƒฆใƒผใ‚ถใƒผใฎ็š†ๆง˜ใซๆœ—ๅ ฑใงใ™ใ€‚ PyTorch/XLA ใฏ FSDP ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ +ๆœ€ๆ–ฐใฎ Fully Sharded Data Parallel (FSDP) ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒใ™ในใฆใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[FSDP ใ‚’ไฝฟ็”จใ—ใŸ Cloud TPU ใงใฎ PyTorch ใƒขใƒ‡ใƒซใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐ](https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/) ใŠใ‚ˆใณ [PyTorch/XLA ๅฎŸ่ฃ… ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ FSDP ใฎ](https://github.com/pytorch/xla/tree/master/torch_xla/distributed/fsdp) +ๅฟ…่ฆใชใฎใฏใ€่จญๅฎšใ‚’้€šใ˜ใฆๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใ ใ‘ใงใ™ใ€‚ + +**FSDP ใ‚ตใƒใƒผใƒˆใซๅฟ…่ฆใช PyTorch/XLA ใƒใƒผใ‚ธใƒงใƒณ**: >=2.0 + +**ไฝฟ็”จๆณ•**๏ผš + +`--fsdp "full shard"` ใ‚’ใ€`--fsdp_config ` ใซๅŠ ใˆใ‚‰ใ‚Œใ‚‹ๆฌกใฎๅค‰ๆ›ดใจใจใ‚‚ใซๆธกใ—ใพใ™ใ€‚ +- PyTorch/XLA FSDP ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`xla`ใ‚’`True`ใซ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- `xla_fsdp_settings` ๅ€คใฏใ€XLA FSDP ใƒฉใƒƒใƒ”ใƒณใ‚ฐ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆ ผ็ดใ™ใ‚‹่พžๆ›ธใงใ™ใ€‚ + ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎๅฎŒๅ…จใชใƒชใ‚นใƒˆใซใคใ„ใฆใฏใ€[ใ“ใกใ‚‰]( + https://github.com/pytorch/xla/blob/master/torch_xla/distributed/fsdp/xla_full_sharded_data_Parallel.py)ใ€‚ +- `xla_fsdp_grad_ckpt`ใ€‚ `True`ใฎๅ ดๅˆใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸ XLA FSDP ใงใƒฉใƒƒใƒ—ใ•ใ‚ŒใŸๅ„ใƒฌใ‚คใƒคใƒผไธŠใงๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + ใ“ใฎ่จญๅฎšใฏใ€xla ใƒ•ใƒฉใ‚ฐใŒ true ใซ่จญๅฎšใ•ใ‚ŒใฆใŠใ‚Šใ€่‡ชๅ‹•ใƒฉใƒƒใƒ”ใƒณใ‚ฐ ใƒใƒชใ‚ทใƒผใŒๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซใฎใฟไฝฟ็”จใงใใพใ™ใ€‚ + `fsdp_min_num_params` ใพใŸใฏ `fsdp_transformer_layer_cls_to_wrap`ใ€‚ +- ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ— ใƒใƒชใ‚ทใƒผใพใŸใฏใ‚ตใ‚คใ‚บ ใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ— ใƒใƒชใ‚ทใƒผใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + - ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ—ใƒใƒชใ‚ทใƒผใฎๅ ดๅˆใ€ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใง `fsdp_transformer_layer_cls_to_wrap` ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ๆŒ‡ๅฎšใ—ใชใ„ๅ ดๅˆใ€ไฝฟ็”จๅฏ่ƒฝใชๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใฏ `model._no_split_modules` ใซใชใ‚Šใพใ™ใ€‚ + ใ“ใ‚Œใฏใ€ใƒฉใƒƒใƒ—ใ™ใ‚‹ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผๅฑคใ‚ฏใƒฉใ‚นๅใฎใƒชใ‚นใƒˆ (ๅคงๆ–‡ๅญ—ใจๅฐๆ–‡ๅญ—ใ‚’ๅŒบๅˆฅ) ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ (ไพ‹: [`BertLayer`]ใ€[`GPTJBlock`]ใ€[`T5Block`] ...)ใ€‚ + ้‡ใฟใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใ‚ตใƒ–ใƒขใ‚ธใƒฅใƒผใƒซ (ๅŸ‹ใ‚่พผใฟๅฑคใชใฉ) ใŒ็•ฐใชใ‚‹ FSDP ใƒฉใƒƒใƒ—ใ•ใ‚ŒใŸใƒฆใƒ‹ใƒƒใƒˆใซใชใ‚‰ใชใ„ใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ้‡่ฆใงใ™ใ€‚ + ใ“ใฎใƒใƒชใ‚ทใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒžใƒซใƒใƒ˜ใƒƒใƒ‰ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจใใ‚Œใซ็ถšใใ„ใใคใ‹ใฎ MLP ใƒฌใ‚คใƒคใƒผใ‚’ๅซใ‚€ใƒ–ใƒญใƒƒใ‚ฏใ”ใจใซใƒฉใƒƒใƒ”ใƒณใ‚ฐใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ + ๅ…ฑๆœ‰ๅŸ‹ใ‚่พผใฟใ‚’ๅซใ‚€ๆฎ‹ใ‚Šใฎๅฑคใฏใ€ๅŒใ˜ๆœ€ใ‚‚ๅค–ๅดใฎ FSDP ใƒฆใƒ‹ใƒƒใƒˆใซใƒฉใƒƒใƒ—ใ•ใ‚Œใ‚‹ใฎใŒไพฟๅˆฉใงใ™ใ€‚ + ใ—ใŸใŒใฃใฆใ€ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใซใฏใ“ใ‚Œใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + - ใ‚ตใ‚คใ‚บใƒ™ใƒผใ‚นใฎ่‡ชๅ‹•ใƒฉใƒƒใƒ—ใƒใƒชใ‚ทใƒผใฎๅ ดๅˆใฏใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใซ`fsdp_min_num_params`ใ‚’่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„ใ€‚ + ่‡ชๅ‹•ใƒฉใƒƒใƒ”ใƒณใ‚ฐใฎใŸใ‚ใฎ FSDP ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆœ€ๅฐๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +### Using Trainer for accelerated PyTorch Training on Mac + +PyTorch v1.12 ใƒชใƒชใƒผใ‚นใซใ‚ˆใ‚Šใ€้–‹็™บ่€…ใจ็ ”็ฉถ่€…ใฏ Apple ใ‚ทใƒชใ‚ณใƒณ GPU ใ‚’ๅˆฉ็”จใ—ใฆใƒขใƒ‡ใƒซ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅคงๅน…ใซ้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ—ใƒญใƒˆใ‚ฟใ‚คใƒ”ใƒณใ‚ฐใ‚„ๅพฎ่ชฟๆ•ดใชใฉใฎๆฉŸๆขฐๅญฆ็ฟ’ใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใ‚’ Mac ไธŠใงใƒญใƒผใ‚ซใƒซใงๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +PyTorch ใฎใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใจใ—ใฆใฎ Apple ใฎ Metal Performance Shaders (MPS) ใฏใ“ใ‚Œใ‚’ๅฏ่ƒฝใซใ—ใ€ๆ–ฐใ—ใ„ `"mps"` ใƒ‡ใƒใ‚คใ‚น็ตŒ็”ฑใงไฝฟ็”จใงใใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใจใƒ—ใƒชใƒŸใƒ†ใ‚ฃใƒ–ใŒ MPS Graph ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใจ MPS ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹่ชฟๆ•ดใ•ใ‚ŒใŸใ‚ซใƒผใƒใƒซใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๅ…ฌๅผใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ [Mac ใงใฎ Accelerated PyTorch Training ใฎ็ดนไป‹](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ใŠใ‚ˆใณ [MPS ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰](https://pytorch.org/docs/stable/notes/mps.html)ใ€‚ + + + +MacOS ใƒžใ‚ทใƒณใซ PyTorch >= 1.13 (ๅŸท็ญ†ๆ™‚็‚นใงใฏใƒŠใ‚คใƒˆใƒชใƒผ ใƒใƒผใ‚ธใƒงใƒณ) ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฎใƒขใƒ‡ใƒซใฎๆญฃ็ขบๆ€งใจใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใซ้–ข้€ฃใ™ใ‚‹ไธป่ฆใชไฟฎๆญฃใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ +่ฉณ็ดฐใซใคใ„ใฆใฏใ€https://github.com/pytorch/pytorch/issues/82707 ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +**Apple Silicon ใƒใƒƒใƒ—ใ‚’ไฝฟ็”จใ—ใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–ใฎๅˆฉ็‚น** + +1. ใƒฆใƒผใ‚ถใƒผใŒใƒญใƒผใ‚ซใƒซใงๅคง่ฆๆจกใชใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚„ใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ +2. ใƒฆใƒ‹ใƒ•ใ‚กใ‚คใƒ‰ ใƒกใƒขใƒช ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซใ‚ˆใ‚Šใ€ใƒ‡ใƒผใ‚ฟๅ–ๅพ—ใฎ้…ๅปถใŒ็Ÿญ็ธฎใ•ใ‚Œใ€GPU ใŒใƒกใƒขใƒช ใ‚นใƒˆใ‚ขๅ…จไฝ“ใซ็›ดๆŽฅใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ—ใพใ™ใ€‚ +3. ใ‚ฏใƒฉใ‚ฆใƒ‰ใƒ™ใƒผใ‚นใฎ้–‹็™บใซ้–ข้€ฃใ™ใ‚‹ใ‚ณใ‚นใƒˆใ‚„่ฟฝๅŠ ใฎใƒญใƒผใ‚ซใƒซ GPU ใฎๅฟ…่ฆๆ€งใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ + +**ๅ‰ๆๆกไปถ**: mps ใ‚ตใƒใƒผใƒˆใ‚’ๅ‚™ใˆใŸใƒˆใƒผใƒใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ +ใ“ใฎ็ด ๆ™ดใ‚‰ใ—ใ„ใƒกใƒ‡ใ‚ฃใ‚ข่จ˜ไบ‹ [GPU ใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ทใƒงใƒณใŒ M1 Mac ใฎ PyTorch ใซ็™ปๅ ด](https://medium.com/towards-data-science/gpu-acceleration-comes-to-pytorch-on-m1-macs-195c399efcc1) ใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ ใ€‚ + +**ไฝฟ็”จๆณ•**๏ผš +`mps` ใƒ‡ใƒใ‚คใ‚นใฏใ€`cuda` ใƒ‡ใƒใ‚คใ‚นใŒไฝฟ็”จใ•ใ‚Œใ‚‹ๆ–นๆณ•ใจๅŒๆง˜ใซๅˆฉ็”จๅฏ่ƒฝใชๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€ใƒฆใƒผใ‚ถใƒผใซใ‚ˆใ‚‹ใ‚ขใ‚ฏใ‚ทใƒงใƒณใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใŸใจใˆใฐใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€Apple Silicon GPU ใ‚’ไฝฟ็”จใ—ใฆๅ…ฌๅผใฎ Glue ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚’ (ใƒซใƒผใƒˆ ใƒ•ใ‚ฉใƒซใƒ€ใƒผใ‹ใ‚‰) ๅฎŸ่กŒใงใใพใ™ใ€‚ + +```bash +export TASK_NAME=mrpc + +python examples/pytorch/text-classification/run_glue.py \ + --model_name_or_path google-bert/bert-base-cased \ + --task_name $TASK_NAME \ + --do_train \ + --do_eval \ + --max_seq_length 128 \ + --per_device_train_batch_size 32 \ + --learning_rate 2e-5 \ + --num_train_epochs 3 \ + --output_dir /tmp/$TASK_NAME/ \ + --overwrite_output_dir +``` + +**ๆณจๆ„ใ™ในใใ„ใใคใ‹ใฎๆณจๆ„ไบ‹้ …** + +1. ไธ€้ƒจใฎ PyTorch ๆ“ไฝœใฏ mps ใซๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใ‚จใƒฉใƒผใŒใ‚นใƒญใƒผใ•ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใ‚’ๅ›ž้ฟใ™ใ‚‹ 1 ใคใฎๆ–นๆณ•ใฏใ€็’ฐๅขƒๅค‰ๆ•ฐ `PYTORCH_ENABLE_MPS_FALLBACK=1` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎๆ“ไฝœใงใฏ CPU ใซใƒ•ใ‚ฉใƒผใƒซใƒใƒƒใ‚ฏใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใใ‚Œใงใ‚‚ UserWarning ใŒใ‚นใƒญใƒผใ•ใ‚Œใพใ™ใ€‚ +2. ๅˆ†ๆ•ฃใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—`gloo`ใŠใ‚ˆใณ`nccl`ใฏใ€`mps`ใƒ‡ใƒใ‚คใ‚นใงใฏๅ‹•ไฝœใ—ใพใ›ใ‚“ใ€‚ +ใ“ใ‚Œใฏใ€็พๅœจใ€Œmpsใ€ใƒ‡ใƒใ‚คใ‚น ใ‚ฟใ‚คใƒ—ใฎๅ˜ไธ€ GPU ใฎใฟใ‚’ไฝฟ็”จใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + +ๆœ€ๅพŒใซใ€่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ ๐Ÿค— `Trainer` ใฏ MPS ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใฎใฟใ‚’็ตฑๅˆใ™ใ‚‹ใŸใ‚ใ€ +MPS ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใฎไฝฟ็”จใซ้–ขใ—ใฆๅ•้กŒใ‚„่ณชๅ•ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ +[PyTorch GitHub](https://github.com/pytorch/pytorch/issues) ใซๅ•้กŒใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ + +## Using Accelerate Launcher with Trainer + +ๅŠ ้€Ÿใ—ใฆใƒˆใƒฌใƒผใƒŠใƒผใซใƒ‘ใƒฏใƒผใ‚’ไธŽใˆใพใ—ใ‚‡ใ†ใ€‚ใƒฆใƒผใ‚ถใƒผใŒๆœŸๅพ…ใ™ใ‚‹ใ“ใจใซ้–ขใ—ใฆใฏใ€ๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +- ใƒˆใƒฌใƒผใƒŠใƒผๅผ•ๆ•ฐใซๅฏพใ—ใฆ FSDPใ€DeepSpeed ใชใฉใฎใƒˆใƒฌใƒผใƒŠใƒผ ใ‚คใƒณใƒ†ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅค‰ๆ›ดใ›ใšใซไฝฟ็”จใ—็ถšใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- ใƒˆใƒฌใƒผใƒŠใƒผใง Accelerate Launcher ใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸ (ๆŽจๅฅจ)ใ€‚ + +ใƒˆใƒฌใƒผใƒŠใƒผใง Accelerate Launcher ใ‚’ไฝฟ็”จใ™ใ‚‹ๆ‰‹้ †: +1. ๐Ÿค— Accelerate ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚Accelerate ใŒใชใ„ใจ `Trainer` ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚ใใ†ใงใชใ„ๅ ดๅˆใฏใ€`pip install accelerate`ใ—ใฆใใ ใ•ใ„ใ€‚ Accelerate ใฎใƒใƒผใ‚ธใƒงใƒณใ‚’ๆ›ดๆ–ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ‚‚ใ‚ใ‚Šใพใ™: `pip install activate --upgrade` +2. `accelerate config`ใ‚’ๅฎŸ่กŒใ—ใ€ใ‚ขใƒณใ‚ฑใƒผใƒˆใซ่จ˜ๅ…ฅใ—ใพใ™ใ€‚ไปฅไธ‹ใฏๅŠ ้€Ÿ่จญๅฎšใฎไพ‹ใงใ™ใ€‚ + ๏ฝ๏ผŽ DDP ใƒžใƒซใƒใƒŽใƒผใƒ‰ ใƒžใƒซใƒ GPU ๆง‹ๆˆ: + ```yaml + compute_environment: LOCAL_MACHINE + distributed_type: MULTI_GPU + downcast_bf16: 'no' + gpu_ids: all + machine_rank: 0 #change rank as per the node + main_process_ip: 192.168.20.1 + main_process_port: 9898 + main_training_function: main + mixed_precision: fp16 + num_machines: 2 + num_processes: 8 + rdzv_backend: static + same_network: true + tpu_env: [] + tpu_use_cluster: false + tpu_use_sudo: false + use_cpu: false + ``` + + b. FSDP config: + ```yaml + compute_environment: LOCAL_MACHINE + distributed_type: FSDP + downcast_bf16: 'no' + fsdp_config: + fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP + fsdp_backward_prefetch_policy: BACKWARD_PRE + fsdp_forward_prefetch: true + fsdp_offload_params: false + fsdp_sharding_strategy: 1 + fsdp_state_dict_type: FULL_STATE_DICT + fsdp_sync_module_states: true + fsdp_transformer_layer_cls_to_wrap: BertLayer + fsdp_use_orig_params: true + machine_rank: 0 + main_training_function: main + mixed_precision: bf16 + num_machines: 1 + num_processes: 2 + rdzv_backend: static + same_network: true + tpu_env: [] + tpu_use_cluster: false + tpu_use_sudo: false + use_cpu: false + ``` + c.ใƒ•ใ‚กใ‚คใƒซใ‚’ๆŒ‡ใ™ DeepSpeed ๆง‹ๆˆ: + ```yaml + compute_environment: LOCAL_MACHINE + deepspeed_config: + deepspeed_config_file: /home/user/configs/ds_zero3_config.json + zero3_init_flag: true + distributed_type: DEEPSPEED + downcast_bf16: 'no' + machine_rank: 0 + main_training_function: main + num_machines: 1 + num_processes: 4 + rdzv_backend: static + same_network: true + tpu_env: [] + tpu_use_cluster: false + tpu_use_sudo: false + use_cpu: false + ``` + + d.ๅŠ ้€Ÿใƒ—ใƒฉใ‚ฐใ‚คใƒณใ‚’ไฝฟ็”จใ—ใŸ DeepSpeed ๆง‹ๆˆ: + + ```yaml + compute_environment: LOCAL_MACHINE + deepspeed_config: + gradient_accumulation_steps: 1 + gradient_clipping: 0.7 + offload_optimizer_device: cpu + offload_param_device: cpu + zero3_init_flag: true + zero_stage: 2 + distributed_type: DEEPSPEED + downcast_bf16: 'no' + machine_rank: 0 + main_training_function: main + mixed_precision: bf16 + num_machines: 1 + num_processes: 4 + rdzv_backend: static + same_network: true + tpu_env: [] + tpu_use_cluster: false + tpu_use_sudo: false + use_cpu: false + ``` + +3. ๅŠ ้€Ÿ่จญๅฎšใพใŸใฏใƒฉใƒณใƒใƒฃใƒผๅผ•ๆ•ฐใซใ‚ˆใฃใฆไธŠ่จ˜ใงๅ‡ฆ็†ใ•ใ‚ŒใŸๅผ•ๆ•ฐไปฅๅค–ใฎๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ใƒˆใƒฌใƒผใƒŠใƒผ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +ไปฅไธ‹ใฏใ€ไธŠ่จ˜ใฎ FSDP ๆง‹ๆˆใง`accelerate launcher`ใ‚’ไฝฟ็”จใ—ใฆ`run_glue.py`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไพ‹ใงใ™ใ€‚ + +```bash +cd transformers + +accelerate launch \ +./examples/pytorch/text-classification/run_glue.py \ +--model_name_or_path google-bert/bert-base-cased \ +--task_name $TASK_NAME \ +--do_train \ +--do_eval \ +--max_seq_length 128 \ +--per_device_train_batch_size 16 \ +--learning_rate 5e-5 \ +--num_train_epochs 3 \ +--output_dir /tmp/$TASK_NAME/ \ +--overwrite_output_dir +``` + +4. `accelerate launch`ใ™ใ‚‹ใŸใ‚ใฎ cmd ๅผ•ๆ•ฐใ‚’็›ดๆŽฅไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ไธŠใฎไพ‹ใฏๆฌกใฎใ‚ˆใ†ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + +```bash +cd transformers + +accelerate launch --num_processes=2 \ +--use_fsdp \ +--mixed_precision=bf16 \ +--fsdp_auto_wrap_policy=TRANSFORMER_BASED_WRAP \ +--fsdp_transformer_layer_cls_to_wrap="BertLayer" \ +--fsdp_sharding_strategy=1 \ +--fsdp_state_dict_type=FULL_STATE_DICT \ +./examples/pytorch/text-classification/run_glue.py +--model_name_or_path google-bert/bert-base-cased \ +--task_name $TASK_NAME \ +--do_train \ +--do_eval \ +--max_seq_length 128 \ +--per_device_train_batch_size 16 \ +--learning_rate 5e-5 \ +--num_train_epochs 3 \ +--output_dir /tmp/$TASK_NAME/ \ +--overwrite_output_dir +``` + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€๐Ÿค— Accelerate CLI ใ‚ฌใ‚คใƒ‰ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„: [๐Ÿค— Accelerate ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎ่ตทๅ‹•](https://huggingface.co/docs/accelerate/basic_tutorials/launch)ใ€‚ + +็งปๅ‹•ใ•ใ‚ŒใŸใ‚ปใ‚ฏใ‚ทใƒงใƒณ: + +[ DeepSpeed +| Installation +| Deployment with multiple GPUs +| Deployment with one GPU +| Deployment in Notebooks +| Configuration +| Passing Configuration +| Shared Configuration +| ZeRO +| ZeRO-2 Config +| ZeRO-3 Config +| NVMe Support +| ZeRO-2 vs ZeRO-3 Performance +| ZeRO-2 Example +| ZeRO-3 Example +| Optimizer +| Scheduler +| fp32 Precision +| Automatic Mixed Precision +| Batch Size +| Gradient Accumulation +| Gradient Clipping +| Getting The Model Weights Out +] diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/albert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/albert.md new file mode 100644 index 0000000000000000000000000000000000000000..7824f24599912e79315cfdc9166bbe691960e2ac --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/albert.md @@ -0,0 +1,193 @@ + + +# ALBERT + +
+ +Models + + +Spaces + +
+ +## ๆฆ‚่ฆ + +ALBERTใƒขใƒ‡ใƒซใฏใ€ใ€Œ[ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942)ใ€ใจใ„ใ†่ซ–ๆ–‡ใงZhenzhong Lanใ€Mingda Chenใ€Sebastian Goodmanใ€Kevin Gimpelใ€Piyush Sharmaใ€Radu Soricutใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚BERTใฎใƒกใƒขใƒชๆถˆ่ฒปใ‚’ๆธ›ใ‚‰ใ—ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟๅ‰Šๆธ›ๆŠ€่ก“ใ‚’2ใค็คบใ—ใฆใ„ใพใ™๏ผš + +- ๅŸ‹ใ‚่พผใฟ่กŒๅˆ—ใ‚’2ใคใฎๅฐใ•ใช่กŒๅˆ—ใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ€‚ +- ใ‚ฐใƒซใƒผใƒ—้–“ใงๅˆ†ๅ‰ฒใ•ใ‚ŒใŸ็นฐใ‚Š่ฟ”ใ—ๅฑคใ‚’ไฝฟ็”จใ™ใ‚‹ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +*่‡ช็„ถ่จ€่ชž่กจ็พใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆ™‚ใซใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใ‚’ๅข—ใ‚„ใ™ใจใ€ไธ‹ๆตใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ™ใ‚‹ใ“ใจใŒใ—ใฐใ—ใฐใ‚ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ‚ใ‚‹ๆ™‚็‚นใงใ•ใ‚‰ใชใ‚‹ใƒขใƒ‡ใƒซใฎๅข—ๅคงใฏใ€GPU/TPUใฎใƒกใƒขใƒชๅˆถ้™ใ€้•ทใ„่จ“็ทดๆ™‚้–“ใ€ไบˆๆœŸใ›ใฌใƒขใƒ‡ใƒซใฎๅŠฃๅŒ–ใจใ„ใฃใŸๅ•้กŒใฎใŸใ‚ใซๅ›ฐ้›ฃใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅ•้กŒใซๅฏพๅ‡ฆใ™ใ‚‹ใŸใ‚ใซใ€ๆˆ‘ใ€…ใฏBERTใฎใƒกใƒขใƒชๆถˆ่ฒปใ‚’ไฝŽๆธ›ใ—ใ€่จ“็ทด้€Ÿๅบฆใ‚’้ซ˜ใ‚ใ‚‹ใŸใ‚ใฎ2ใคใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟๅ‰Šๆธ›ๆŠ€่ก“ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ๅŒ…ๆ‹ฌ็š„ใชๅฎŸ่จผ็š„่จผๆ‹ ใฏใ€ๆˆ‘ใ€…ใฎๆๆกˆๆ–นๆณ•ใŒๅ…ƒใฎBERTใซๆฏ”ในใฆใฏใ‚‹ใ‹ใซใ‚ˆใใ‚นใ‚ฑใƒผใƒซใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’็”Ÿใฟๅ‡บใ™ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใพใŸใ€ๆ–‡้–“ใฎไธ€่ฒซๆ€งใ‚’ใƒขใƒ‡ใƒชใƒณใ‚ฐใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใŸ่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๆๅคฑใ‚’ไฝฟ็”จใ—ใ€่ค‡ๆ•ฐใฎๆ–‡ใŒๅซใพใ‚Œใ‚‹ไธ‹ๆตใ‚ฟใ‚นใ‚ฏใซไธ€่ฒซใ—ใฆๅŠฉใ‘ใจใชใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ใใฎ็ตๆžœใ€ๆˆ‘ใ€…ใฎๆœ€่‰ฏใฎใƒขใƒ‡ใƒซใฏใ€BERT-largeใซๆฏ”ในใฆใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฐ‘ใชใ„ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€GLUEใ€RACEใ€SQuADใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’็ขบ็ซ‹ใ—ใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[lysandre](https://huggingface.co/lysandre)ใซใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎjaxใƒใƒผใ‚ธใƒงใƒณใฏ[kamalkraj](https://huggingface.co/kamalkraj)ใซใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏ[ใ“ใกใ‚‰](https://github.com/google-research/ALBERT)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## ไฝฟ็”จไธŠใฎใƒ’ใƒณใƒˆ + +- ALBERTใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใชใฎใงใ€้€šๅธธใ€ๅ…ฅๅŠ›ใ‚’ๅทฆๅดใงใฏใชใๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚ +- ALBERTใฏ็นฐใ‚Š่ฟ”ใ—ๅฑคใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใƒกใƒขใƒชไฝฟ็”จ้‡ใฏๅฐใ•ใใชใ‚Šใพใ™ใŒใ€ๅŒใ˜ๆ•ฐใฎ๏ผˆ็นฐใ‚Š่ฟ”ใ—๏ผ‰ๅฑคใ‚’ๅๅพฉใ—ใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„ใŸใ‚ใ€้š ใ‚Œๅฑคใฎๆ•ฐใŒๅŒใ˜ใงใ‚ใ‚ŒใฐBERTใฎใ‚ˆใ†ใชใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจๅŒๆง˜ใฎ่จˆ็ฎ—ใ‚ณใ‚นใƒˆใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚ +- ๅŸ‹ใ‚่พผใฟใ‚ตใ‚คใ‚บEใฏ้š ใ‚Œใ‚ตใ‚คใ‚บHใจ็•ฐใชใ‚Šใพใ™ใŒใ€ใ“ใ‚ŒใฏๅŸ‹ใ‚่พผใฟใŒๆ–‡่„ˆใซไพๅญ˜ใ—ใชใ„๏ผˆไธ€ใคใฎๅŸ‹ใ‚่พผใฟใƒ™ใ‚ฏใƒˆใƒซใŒไธ€ใคใฎใƒˆใƒผใ‚ฏใƒณใ‚’่กจใ™๏ผ‰ใฎใซๅฏพใ—ใ€้š ใ‚Œ็Šถๆ…‹ใฏๆ–‡่„ˆใซไพๅญ˜ใ™ใ‚‹๏ผˆ1ใคใฎ้š ใ‚Œ็Šถๆ…‹ใŒใƒˆใƒผใ‚ฏใƒณ็ณปๅˆ—ใ‚’่กจใ™๏ผ‰ใŸใ‚ใ€H >> Eใจใ™ใ‚‹ใ“ใจใŒใ‚ˆใ‚Š่ซ–็†็š„ใงใ™ใ€‚ใพใŸใ€ๅŸ‹ใ‚่พผใฟ่กŒๅˆ—ใฎใ‚ตใ‚คใ‚บใฏV x Eใจๅคงใใ„ใงใ™๏ผˆVใฏ่ชžๅฝ™ใ‚ตใ‚คใ‚บ๏ผ‰ใ€‚E < Hใงใ‚ใ‚Œใฐใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ +- ๅฑคใฏใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใ‚ฐใƒซใƒผใƒ—ใซๅˆ†ๅ‰ฒใ•ใ‚Œใฆใ„ใพใ™๏ผˆใƒกใƒขใƒช็ฏ€็ด„ใฎใŸใ‚๏ผ‰ใ€‚ๆฌกๆ–‡ไบˆๆธฌ๏ผˆNSP: Next Sentence Prediction๏ผ‰ใฏๆ–‡ใฎ้ †ๅบไบˆๆธฌใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™๏ผšๅ…ฅๅŠ›ใงใฏใ€2ใคใฎๆ–‡AใจB๏ผˆใใ‚Œใ‚‰ใฏ้€ฃ็ถšใ—ใฆใ„ใ‚‹๏ผ‰ใŒใ‚ใ‚Šใ€Aใซ็ถšใ„ใฆBใ‚’ไธŽใˆใ‚‹ใ‹ใ€Bใซ็ถšใ„ใฆAใ‚’ไธŽใˆใพใ™ใ€‚ใƒขใƒ‡ใƒซใฏใใ‚Œใ‚‰ใŒๅ…ฅใ‚Œๆ›ฟใ‚ใฃใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## ๅ‚่€ƒ่ณ‡ๆ–™ + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/masked_language_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## AlbertConfig + +[[autodoc]] AlbertConfig + +## AlbertTokenizer + +[[autodoc]] AlbertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## AlbertTokenizerFast + +[[autodoc]] AlbertTokenizerFast + +## Albert specific outputs + +[[autodoc]] models.albert.modeling_albert.AlbertForPreTrainingOutput + +[[autodoc]] models.albert.modeling_tf_albert.TFAlbertForPreTrainingOutput + + + + +## AlbertModel + +[[autodoc]] AlbertModel + - forward + +## AlbertForPreTraining + +[[autodoc]] AlbertForPreTraining + - forward + +## AlbertForMaskedLM + +[[autodoc]] AlbertForMaskedLM + - forward + +## AlbertForSequenceClassification + +[[autodoc]] AlbertForSequenceClassification + - forward + +## AlbertForMultipleChoice + +[[autodoc]] AlbertForMultipleChoice + +## AlbertForTokenClassification + +[[autodoc]] AlbertForTokenClassification + - forward + +## AlbertForQuestionAnswering + +[[autodoc]] AlbertForQuestionAnswering + - forward + + + + + +## TFAlbertModel + +[[autodoc]] TFAlbertModel + - call + +## TFAlbertForPreTraining + +[[autodoc]] TFAlbertForPreTraining + - call + +## TFAlbertForMaskedLM + +[[autodoc]] TFAlbertForMaskedLM + - call + +## TFAlbertForSequenceClassification + +[[autodoc]] TFAlbertForSequenceClassification + - call + +## TFAlbertForMultipleChoice + +[[autodoc]] TFAlbertForMultipleChoice + - call + +## TFAlbertForTokenClassification + +[[autodoc]] TFAlbertForTokenClassification + - call + +## TFAlbertForQuestionAnswering + +[[autodoc]] TFAlbertForQuestionAnswering + - call + + + + +## FlaxAlbertModel + +[[autodoc]] FlaxAlbertModel + - __call__ + +## FlaxAlbertForPreTraining + +[[autodoc]] FlaxAlbertForPreTraining + - __call__ + +## FlaxAlbertForMaskedLM + +[[autodoc]] FlaxAlbertForMaskedLM + - __call__ + +## FlaxAlbertForSequenceClassification + +[[autodoc]] FlaxAlbertForSequenceClassification + - __call__ + +## FlaxAlbertForMultipleChoice + +[[autodoc]] FlaxAlbertForMultipleChoice + - __call__ + +## FlaxAlbertForTokenClassification + +[[autodoc]] FlaxAlbertForTokenClassification + - __call__ + +## FlaxAlbertForQuestionAnswering + +[[autodoc]] FlaxAlbertForQuestionAnswering + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/align.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/align.md new file mode 100644 index 0000000000000000000000000000000000000000..d1ff4d918a64ffd61c41cd9f139f5797962dd217 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/align.md @@ -0,0 +1,104 @@ + + +# ALIGN + +## ๆฆ‚่ฆ + +ALIGNใƒขใƒ‡ใƒซใฏใ€ใ€Œ[Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://huggingface.co/papers/2102.05918)ใ€ใจใ„ใ†่ซ–ๆ–‡ใงChao Jiaใ€Yinfei Yangใ€Ye Xiaใ€Yi-Ting Chenใ€Zarana Parekhใ€Hieu Phamใ€Quoc V. Leใ€Yunhsuan Sungใ€Zhen Liใ€Tom Duerigใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ALIGNใฏใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใช่ฆ–่ฆš่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใ‚Œใฏ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ้กžไผผๅบฆใ‚„ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใซไฝฟ็”จใงใใพใ™ใ€‚ALIGNใฏ[EfficientNet](efficientnet)ใ‚’่ฆ–่ฆšใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ—ใฆใ€[BERT](bert)ใ‚’ใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ—ใฆๆญ่ผ‰ใ—ใŸใƒ‡ใƒฅใ‚ขใƒซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๆง‹้€ ใ‚’็‰นๅพดใจใ—ใ€ๅฏพ็…งๅญฆ็ฟ’ใซใ‚ˆใฃใฆ่ฆ–่ฆšใจใƒ†ใ‚ญใ‚นใƒˆใฎ่กจ็พใ‚’ๆ•ดๅˆใ•ใ›ใ‚‹ใ“ใจใ‚’ๅญฆใณใพใ™ใ€‚ใใ‚Œใพใงใฎ็ ”็ฉถใจใฏ็•ฐใชใ‚Šใ€ALIGNใฏๅทจๅคงใงใƒŽใ‚คใ‚ธใƒผใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆดป็”จใ—ใ€ใ‚ณใƒผใƒ‘ใ‚นใฎใ‚นใ‚ฑใƒผใƒซใ‚’ๅˆฉ็”จใ—ใฆๅ˜็ด”ใชๆ–นๆณ•ใชใŒใ‚‰ๆœ€ๅ…ˆ็ซฏใฎ่กจ็พใ‚’้”ๆˆใงใใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +*ไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ่กจ็พใฏใ€ๅคšใใฎ่‡ช็„ถ่จ€่ชžๅ‡ฆ็†๏ผˆNLP๏ผ‰ใŠใ‚ˆใณ็Ÿฅ่ฆšใ‚ฟใ‚นใ‚ฏใซใจใฃใฆ้‡่ฆใซใชใฃใฆใ„ใพใ™ใ€‚NLPใซใŠใ‘ใ‚‹่กจ็พๅญฆ็ฟ’ใฏใ€ไบบ้–“ใฎใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใฎใชใ„็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใงใฎๅญฆ็ฟ’ใธใจ็งป่กŒใ—ใฆใ„ใพใ™ใŒใ€่ฆ–่ฆšใŠใ‚ˆใณ่ฆ–่ฆš่จ€่ชžใฎ่กจ็พใฏไพ็„ถใจใ—ใฆ็ฒพๅทงใชๅญฆ็ฟ’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅคงใใไพๅญ˜ใ—ใฆใŠใ‚Šใ€ใ“ใ‚Œใฏ้ซ˜ไพกใงใ‚ใฃใŸใ‚Šๅฐ‚้–€็Ÿฅ่ญ˜ใ‚’ๅฟ…่ฆใจใ—ใŸใ‚Šใ—ใพใ™ใ€‚่ฆ–่ฆšใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฎๅ ดๅˆใ€ImageNetใ‚„OpenImagesใฎใ‚ˆใ†ใชๆ˜Ž็คบ็š„ใชใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใ‚’ๆŒใคใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆๅญฆ็ฟ’ใ•ใ‚Œใ‚‹ใ“ใจใŒใปใจใ‚“ใฉใงใ™ใ€‚่ฆ–่ฆš่จ€่ชžใฎๅ ดๅˆใ€Conceptual Captionsใ€MSCOCOใ€CLIPใชใฉใฎไบบๆฐ—ใฎใ‚ใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใ™ในใฆใ€ใใ‚Œใžใ‚Œ็„ก่ฆ–ใงใใชใ„ใƒ‡ใƒผใ‚ฟๅŽ้›†๏ผˆใŠใ‚ˆใณใ‚ฏใƒชใƒผใƒ‹ใƒณใ‚ฐ๏ผ‰ใƒ—ใƒญใ‚ปใ‚นใ‚’ๅซใฟใพใ™ใ€‚ใ“ใฎใ‚ณใ‚นใƒˆใฎใ‹ใ‹ใ‚‹ใ‚ญใƒฅใƒฌใƒผใ‚ทใƒงใƒณใƒ—ใƒญใ‚ปใ‚นใฏใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใ‚คใ‚บใ‚’ๅˆถ้™ใ—ใ€่จ“็ทดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ‚’ๅฆจใ’ใพใ™ใ€‚ๆœฌ่ซ–ๆ–‡ใงใฏใ€Conceptual Captionsใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ้ซ˜ไพกใชใƒ•ใ‚ฃใƒซใ‚ฟใƒชใƒณใ‚ฐใ‚„ๅพŒๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใชใ—ใงๅพ—ใ‚‰ใ‚ŒใŸใ€10ๅ„„ใ‚’่ถ…ใˆใ‚‹็”ปๅƒalt-textใƒšใ‚ขใฎใƒŽใ‚คใ‚บใฎๅคšใ„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆดป็”จใ—ใพใ™ใ€‚ใ‚ทใƒณใƒ—ใƒซใชใƒ‡ใƒฅใ‚ขใƒซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใ€ๅฏพ็…งๆๅคฑใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใƒšใ‚ขใฎ่ฆ–่ฆš็š„ใŠใ‚ˆใณ่จ€่ชž็š„่กจ็พใ‚’ๆ•ดๅˆใ•ใ›ใ‚‹ใ“ใจใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฏใ€ใ‚ณใƒผใƒ‘ใ‚นใฎ่ฆๆจกใŒใใฎใƒŽใ‚คใ‚บใ‚’่ฃœใ„ใ€ใ“ใฎใ‚ˆใ†ใชๅ˜็ด”ใชๅญฆ็ฟ’ใ‚นใ‚ญใƒผใƒ ใงใ‚‚ๆœ€ๅ…ˆ็ซฏใฎ่กจ็พใซใคใชใŒใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฎ่ฆ–่ฆš่กจ็พใฏใ€ImageNetใ‚„VTABใชใฉใฎๅˆ†้กžใ‚ฟใ‚นใ‚ฏใธใฎ่ปข็งปใซใŠใ„ใฆๅผทๅŠ›ใชๆ€ง่ƒฝใ‚’็™บๆฎใ—ใพใ™ใ€‚ๆ•ดๅˆใ—ใŸ่ฆ–่ฆš็š„ใŠใ‚ˆใณ่จ€่ชž็š„่กจ็พใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใ‚’ๅฏ่ƒฝใซใ—ใ€ใพใŸใ€ใ‚ˆใ‚Šๆด—็ทดใ•ใ‚ŒใŸใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ—ใฆใ‚‚ใ€Flickr30KใŠใ‚ˆใณMSCOCO็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆๆคœ็ดขใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใŠใ„ใฆๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’้”ๆˆใ—ใพใ™ใ€‚ใพใŸใ€ใ“ใ‚Œใ‚‰ใฎ่กจ็พใฏใ€่ค‡้›‘ใชใƒ†ใ‚ญใ‚นใƒˆใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆ+็”ปๅƒใฎใ‚ฏใ‚จใƒชใ‚’็”จใ„ใŸใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซๆคœ็ดขใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[Alara Dirik](https://huggingface.co/adirik)ใซใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏๅ…ฌ้–‹ใ•ใ‚ŒใฆใŠใ‚‰ใšใ€ใ“ใฎๅฎŸ่ฃ…ใฏๅ…ƒ่ซ–ๆ–‡ใซๅŸบใฅใ„ใŸKakao BrainใฎๅฎŸ่ฃ…ใ‚’ใƒ™ใƒผใ‚นใซใ—ใฆใ„ใพใ™ใ€‚ + +## ไฝฟ็”จไพ‹ + +ALIGNใฏEfficientNetใ‚’ไฝฟ็”จใ—ใฆ่ฆ–่ฆš็š„็‰นๅพดใ‚’ใ€BERTใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใจ่ฆ–่ฆšใฎไธกๆ–นใฎ็‰นๅพดใฏใ€ๅŒไธ€ใฎๆฌกๅ…ƒใ‚’ๆŒใคๆฝœๅœจ็ฉบ้–“ใซๅฐ„ๅฝฑใ•ใ‚Œใพใ™ใ€‚ๅฐ„ๅฝฑใ•ใ‚ŒใŸ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพด้–“ใฎใƒ‰ใƒƒใƒˆ็ฉใŒ้กžไผผๅบฆใ‚นใ‚ณใ‚ขใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +[`AlignProcessor`]ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚จใƒณใ‚ณใƒผใƒ‰ใจ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใ‚’ไธกๆ–น่กŒใ†ใŸใ‚ใซใ€[`EfficientNetImageProcessor`]ใจ[`BertTokenizer`]ใ‚’ๅ˜ไธ€ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใฏใ€[`AlignProcessor`]ใจ[`AlignModel`]ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒ-ใƒ†ใ‚ญใ‚นใƒˆ้กžไผผๅบฆใ‚นใ‚ณใ‚ขใ‚’ๅ–ๅพ—ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +import requests +import torch +from PIL import Image +from transformers import AlignProcessor, AlignModel + +processor = AlignProcessor.from_pretrained("kakaobrain/align-base") +model = AlignModel.from_pretrained("kakaobrain/align-base") + +url = "http://images.cocodataset.org/val2017/000000039769.jpg" +image = Image.open(requests.get(url, stream=True).raw) +candidate_labels = ["an image of a cat", "an image of a dog"] + +inputs = processor(text=candidate_labels, images=image, return_tensors="pt") + +with torch.no_grad(): + outputs = model(**inputs) + +# this is the image-text similarity score +logits_per_image = outputs.logits_per_image + +# we can take the softmax to get the label probabilities +probs = logits_per_image.softmax(dim=1) +print(probs) +``` + +## ๅ‚่€ƒ่ณ‡ๆ–™ + +ALIGNใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผใฎHugging Faceใจใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ๏ผˆ๐ŸŒŽใง็คบใ•ใ‚Œใฆใ„ใ‚‹๏ผ‰ใฎๅ‚่€ƒ่ณ‡ๆ–™ใฎไธ€่ฆงใงใ™ใ€‚ + +- [ALIGNใจCOYO-700Mใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/blog/vit-align)ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ€‚ +- ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กž[ใƒ‡ใƒข](https://huggingface.co/spaces/adirik/ALIGN-zero-shot-image-classification)ใ€‚ +- `kakaobrain/align-base` ใƒขใƒ‡ใƒซใฎ[ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰](https://huggingface.co/kakaobrain/align-base)ใ€‚ + +ใ“ใ“ใซๅ‚่€ƒ่ณ‡ๆ–™ใ‚’ๆๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฐ—ๅ…ผใญใชใPull Requestใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚็งใŸใกใฏใใ‚Œใ‚’ใƒฌใƒ“ใƒฅใƒผใ„ใŸใ—ใพใ™๏ผๅ‚่€ƒ่ณ‡ๆ–™ใฏใ€ๆ—ขๅญ˜ใฎใ‚‚ใฎใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ“ใจใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## AlignConfig + +[[autodoc]] AlignConfig + - from_text_vision_configs + +## AlignTextConfig + +[[autodoc]] AlignTextConfig + +## AlignVisionConfig + +[[autodoc]] AlignVisionConfig + +## AlignProcessor + +[[autodoc]] AlignProcessor + +## AlignModel + +[[autodoc]] AlignModel + - forward + - get_text_features + - get_image_features + +## AlignTextModel + +[[autodoc]] AlignTextModel + - forward + +## AlignVisionModel + +[[autodoc]] AlignVisionModel + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/altclip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/altclip.md new file mode 100644 index 0000000000000000000000000000000000000000..fe721d29bfe5370f03c424cb94077f97d2571e06 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/altclip.md @@ -0,0 +1,97 @@ + + +# AltCLIP + +## ๆฆ‚่ฆ + + +AltCLIPใƒขใƒ‡ใƒซใฏใ€ใ€Œ[AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities](https://huggingface.co/papers/2211.06679)ใ€ใจใ„ใ†่ซ–ๆ–‡ใงZhongzhi Chenใ€Guang Liuใ€Bo-Wen Zhangใ€Fulong Yeใ€Qinghong Yangใ€Ledell Wuใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚AltCLIP๏ผˆCLIPใฎ่จ€่ชžใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎไปฃๆ›ฟ๏ผ‰ใฏใ€ๆง˜ใ€…ใช็”ปๅƒ-ใƒ†ใ‚ญใ‚นใƒˆใƒšใ‚ขใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆ-ใƒ†ใ‚ญใ‚นใƒˆใƒšใ‚ขใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใงใ™ใ€‚CLIPใฎใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎๅคš่จ€่ชžใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผXLM-Rใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใงใ€ใปใผๅ…จใฆใฎใ‚ฟใ‚นใ‚ฏใงCLIPใซ้žๅธธใซ่ฟ‘ใ„ๆ€ง่ƒฝใ‚’ๅพ—ใ‚‰ใ‚Œใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎCLIPใฎ่ƒฝๅŠ›ใ‚’ๅคš่จ€่ชž็†่งฃใชใฉใซๆ‹กๅผตใ—ใพใ—ใŸใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +*ใ“ใฎ็ ”็ฉถใงใฏใ€ๅผทๅŠ›ใชใƒใ‚คใƒชใƒณใ‚ฌใƒซใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ่กจ็พใƒขใƒ‡ใƒซใ‚’่จ“็ทดใ™ใ‚‹ใŸใ‚ใฎๆฆ‚ๅฟต็š„ใซๅ˜็ด”ใงๅŠนๆžœ็š„ใชๆ–นๆณ•ใ‚’ๆๆกˆใ—ใพใ™ใ€‚OpenAIใซใ‚ˆใฃใฆใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ่กจ็พใƒขใƒ‡ใƒซCLIPใ‹ใ‚‰้–‹ๅง‹ใ—ใ€ใใฎใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎๅคš่จ€่ชžใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€XLM-Rใซไบคๆ›ใ—ใ€ๆ•™ๅธซๅญฆ็ฟ’ใจๅฏพ็…งๅญฆ็ฟ’ใ‹ใ‚‰ใชใ‚‹2ๆฎต้šŽใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใ‚ญใƒผใƒžใ‚’็”จใ„ใฆ่จ€่ชžใจ็”ปๅƒใฎ่กจ็พใ‚’ๆ•ดๅˆใ•ใ›ใพใ—ใŸใ€‚ๅน…ๅบƒใ„ใ‚ฟใ‚นใ‚ฏใฎ่ฉ•ไพกใ‚’้€šใ˜ใฆใ€ๆˆ‘ใ€…ใฎๆ–นๆณ•ใ‚’ๆคœ่จผใ—ใพใ™ใ€‚ImageNet-CNใ€Flicker30k-CNใ€COCO-CNใ‚’ๅซใ‚€ๅคšใใฎใ‚ฟใ‚นใ‚ฏใงๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏใฎๆ€ง่ƒฝใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚ใ•ใ‚‰ใซใ€ใปใผใ™ในใฆใฎใ‚ฟใ‚นใ‚ฏใงCLIPใซ้žๅธธใซ่ฟ‘ใ„ๆ€ง่ƒฝใ‚’ๅพ—ใฆใŠใ‚Šใ€ใ“ใ‚ŒใฏCLIPใฎใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ ใ‘ใงใ€ๅคš่จ€่ชž็†่งฃใชใฉใฎๆ‹กๅผตใ‚’ๅฎŸ็พใงใใ‚‹ใ“ใจใ‚’็คบๅ”†ใ—ใฆใ„ใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[jongjyh](https://huggingface.co/jongjyh)ใซใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ + +## ไฝฟ็”จไธŠใฎใƒ’ใƒณใƒˆใจไฝฟ็”จไพ‹ + +AltCLIPใฎไฝฟ็”จๆ–นๆณ•ใฏCLIPใซ้žๅธธใซไผผใฆใ„ใพใ™ใ€‚CLIPใจใฎ้•ใ„ใฏใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซใ‚ใ‚Šใพใ™ใ€‚็งใŸใกใฏใ‚ซใ‚ธใƒฅใ‚ขใƒซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใงใฏใชใๅŒๆ–นๅ‘ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใ€XLM-Rใฎ[CLS]ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ†ใ‚ญใ‚นใƒˆๅŸ‹ใ‚่พผใฟใ‚’่กจใ™ใ‚‚ใฎใจใ—ใฆๅ–ใ‚‹ใ“ใจใซ็•™ๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +AltCLIPใฏใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใช่ฆ–่ฆš่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใ‚Œใฏ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ้กžไผผๅบฆใ‚„ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใซไฝฟ็”จใงใใพใ™ใ€‚AltCLIPใฏViTใฎใ‚ˆใ†ใชTransformerใ‚’ไฝฟ็”จใ—ใฆ่ฆ–่ฆš็š„็‰นๅพดใ‚’ใ€ๅŒๆ–นๅ‘่จ€่ชžใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใจ่ฆ–่ฆšใฎไธกๆ–นใฎ็‰นๅพดใฏใ€ๅŒไธ€ใฎๆฌกๅ…ƒใ‚’ๆŒใคๆฝœๅœจ็ฉบ้–“ใซๅฐ„ๅฝฑใ•ใ‚Œใพใ™ใ€‚ๅฐ„ๅฝฑใ•ใ‚ŒใŸ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพด้–“ใฎใƒ‰ใƒƒใƒˆ็ฉใŒ้กžไผผๅบฆใ‚นใ‚ณใ‚ขใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +Transformerใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซ็”ปๅƒใ‚’ไธŽใˆใ‚‹ใซใฏใ€ๅ„็”ปๅƒใ‚’ๅ›บๅฎšใ‚ตใ‚คใ‚บใฎ้‡่ค‡ใ—ใชใ„ใƒ‘ใƒƒใƒใฎ็ณปๅˆ—ใซๅˆ†ๅ‰ฒใ—ใ€ใใ‚Œใ‚‰ใ‚’็ทšๅฝขใซๅŸ‹ใ‚่พผใฟใพใ™ใ€‚็”ปๅƒๅ…จไฝ“ใ‚’่กจ็พใ™ใ‚‹ใŸใ‚ใฎ[CLS]ใƒˆใƒผใ‚ฏใƒณใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚่‘—่€…ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚‚่ฟฝๅŠ ใ—ใ€็ตๆžœใจใ—ใฆๅพ—ใ‚‰ใ‚Œใ‚‹ใƒ™ใ‚ฏใƒˆใƒซใฎ็ณปๅˆ—ใ‚’ๆจ™ๆบ–็š„ใชTransformerใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซไพ›็ตฆใ—ใพใ™ใ€‚[`CLIPImageProcessor`]ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎใŸใ‚ใซ็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ด๏ผˆใพใŸใฏๆ‹กๅคง็ธฎๅฐ๏ผ‰ใจๆญฃ่ฆๅŒ–ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ + +[`AltCLIPProcessor`]ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚จใƒณใ‚ณใƒผใƒ‰ใจ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใ‚’ไธกๆ–น่กŒใ†ใŸใ‚ใซใ€[`CLIPImageProcessor`]ใจ[`XLMRobertaTokenizer`]ใ‚’ๅ˜ไธ€ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใฏใ€[`AltCLIPProcessor`]ใจ[`AltCLIPModel`]ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒ-ใƒ†ใ‚ญใ‚นใƒˆ้กžไผผใ‚นใ‚ณใ‚ขใ‚’ๅ–ๅพ—ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> from PIL import Image +>>> import requests + +>>> from transformers import AltCLIPModel, AltCLIPProcessor + +>>> model = AltCLIPModel.from_pretrained("BAAI/AltCLIP") +>>> processor = AltCLIPProcessor.from_pretrained("BAAI/AltCLIP") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True) + +>>> outputs = model(**inputs) +>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score +>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities +``` + + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ`CLIPModel`ใ‚’ใƒ™ใƒผใ‚นใซใ—ใฆใŠใ‚Šใ€ใ‚ชใƒชใ‚ธใƒŠใƒซใฎ[CLIP](clip)ใจๅŒใ˜ใ‚ˆใ†ใซไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## AltCLIPConfig + +[[autodoc]] AltCLIPConfig + - from_text_vision_configs + +## AltCLIPTextConfig + +[[autodoc]] AltCLIPTextConfig + +## AltCLIPVisionConfig + +[[autodoc]] AltCLIPVisionConfig + +## AltCLIPProcessor + +[[autodoc]] AltCLIPProcessor + +## AltCLIPModel + +[[autodoc]] AltCLIPModel + - forward + - get_text_features + - get_image_features + +## AltCLIPTextModel + +[[autodoc]] AltCLIPTextModel + - forward + +## AltCLIPVisionModel + +[[autodoc]] AltCLIPVisionModel + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/audio-spectrogram-transformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/audio-spectrogram-transformer.md new file mode 100644 index 0000000000000000000000000000000000000000..eb0c3cf6e36eca8ea8774f9467ddaf02fe17310c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/audio-spectrogram-transformer.md @@ -0,0 +1,69 @@ + + +# Audio Spectrogram Transformer + +## ๆฆ‚่ฆ + +Audio Spectrogram Transformerใƒขใƒ‡ใƒซใฏใ€[AST: Audio Spectrogram Transformer](https://huggingface.co/papers/2104.01778)ใจใ„ใ†่ซ–ๆ–‡ใงYuan Gongใ€Yu-An Chungใ€James Glassใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใ‚Œใฏใ€้Ÿณๅฃฐใ‚’็”ปๅƒ๏ผˆใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ๏ผ‰ใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใงใ€้Ÿณๅฃฐใซ[Vision Transformer](vit)ใ‚’้ฉ็”จใ—ใพใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฏ้Ÿณๅฃฐๅˆ†้กžใซใŠใ„ใฆๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’ๅพ—ใฆใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +*้ŽๅŽป10ๅนด้–“ใงใ€็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆCNN๏ผ‰ใฏใ€้Ÿณๅฃฐใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‹ใ‚‰ๅฏพๅฟœใ™ใ‚‹ใƒฉใƒ™ใƒซใธใฎ็›ดๆŽฅ็š„ใชใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ“ใจใ‚’็›ฎๆŒ‡ใ™ใ€ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎ้Ÿณๅฃฐๅˆ†้กžใƒขใƒ‡ใƒซใฎไธป่ฆใชๆง‹ๆˆ่ฆ็ด ใจใ—ใฆๅบƒใๆŽก็”จใ•ใ‚Œใฆใใพใ—ใŸใ€‚้•ท่ท้›ขใฎใ‚ฐใƒญใƒผใƒใƒซใชใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚ˆใ‚Š่‰ฏใๆ‰ใˆใ‚‹ใŸใ‚ใ€ๆœ€่ฟ‘ใฎๅ‚พๅ‘ใจใ—ใฆใ€CNNใฎไธŠใซใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๆฉŸๆง‹ใ‚’่ฟฝๅŠ ใ—ใ€CNN-ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒใ‚คใƒ–ใƒชใƒƒใƒ‰ใƒขใƒ‡ใƒซใ‚’ๅฝขๆˆใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€CNNใธใฎไพๅญ˜ใŒๅฟ…่ฆใ‹ใฉใ†ใ‹ใ€ใใ—ใฆ็ด”็ฒ‹ใซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใซๅŸบใฅใใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ ใ‘ใง้Ÿณๅฃฐๅˆ†้กžใซใŠใ„ใฆ่‰ฏใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใ‚‹ใ‹ใฉใ†ใ‹ใฏๆ˜Žใ‚‰ใ‹ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆœฌ่ซ–ๆ–‡ใงใฏใ€ใ“ใ‚Œใ‚‰ใฎๅ•ใ„ใซ็ญ”ใˆใ‚‹ใŸใ‚ใ€้Ÿณๅฃฐๅˆ†้กž็”จใงใฏๆœ€ๅˆใฎ็•ณใฟ่พผใฟใชใ—ใง็ด”็ฒ‹ใซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใงใ‚ใ‚‹Audio Spectrogram Transformer๏ผˆAST๏ผ‰ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ๆˆ‘ใ€…ใฏASTใ‚’ๆง˜ใ€…ใชใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใƒ™ใƒณใƒใƒžใƒผใ‚ฏใง่ฉ•ไพกใ—ใ€AudioSetใง0.485 mAPใ€ESC-50ใง95.6%ใฎๆญฃ่งฃ็އใ€Speech Commands V2ใง98.1%ใฎๆญฃ่งฃ็އใจใ„ใ†ๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚* + + + + Audio Spectrogram Transformerใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ๅ…ƒ่ซ–ๆ–‡ใ‚ˆใ‚ŠๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[nielsr](https://huggingface.co/nielsr)ใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏ[ใ“ใกใ‚‰](https://github.com/YuanGongND/ast)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## ไฝฟ็”จไธŠใฎใƒ’ใƒณใƒˆ + +- ็‹ฌ่‡ชใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงAudio Spectrogram Transformer๏ผˆAST๏ผ‰ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ๅ…ฅๅŠ›ใฎๆญฃ่ฆๅŒ–๏ผˆๅ…ฅๅŠ›ใฎๅนณๅ‡ใ‚’0ใ€ๆจ™ๆบ–ๅๅทฎใ‚’0.5ใซใ™ใ‚‹ใ“ใจ๏ผ‰ๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚[`ASTFeatureExtractor`]ใฏใ“ใ‚Œใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏAudioSetใฎๅนณๅ‡ใจๆจ™ๆบ–ๅๅทฎใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚่‘—่€…ใŒไธ‹ๆตใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ็ตฑ่จˆใ‚’ใฉใฎใ‚ˆใ†ใซ่จˆ็ฎ—ใ—ใฆใ„ใ‚‹ใ‹ใฏใ€[`ast/src/get_norm_stats.py`](https://github.com/YuanGongND/ast/blob/master/src/get_norm_stats.py)ใง็ขบ่ชใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- ASTใฏไฝŽใ„ๅญฆ็ฟ’็އใŒๅฟ…่ฆใงใ‚ใ‚Š ่‘—่€…ใฏ[PSLA่ซ–ๆ–‡](https://huggingface.co/papers/2102.01243)ใงๆๆกˆใ•ใ‚ŒใŸCNNใƒขใƒ‡ใƒซใซๆฏ”ในใฆ10ๅ€ๅฐใ•ใ„ๅญฆ็ฟ’็އใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™๏ผ‰ใ€็ด ๆ—ฉใๅŽๆŸใ™ใ‚‹ใŸใ‚ใ€ใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใŸๅญฆ็ฟ’็އใจๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ๆŽขใ™ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +## ๅ‚่€ƒ่ณ‡ๆ–™ + +Audio Spectrogram Transformerใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผใฎHugging FaceใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ๏ผˆ๐ŸŒŽใง็คบใ•ใ‚Œใฆใ„ใ‚‹๏ผ‰ใฎๅ‚่€ƒ่ณ‡ๆ–™ใฎไธ€่ฆงใงใ™ใ€‚ + + + +- ASTใ‚’็”จใ„ใŸ้Ÿณๅฃฐๅˆ†้กžใฎๆŽจ่ซ–ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏ[ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/AST)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- [`ASTForAudioClassification`]ใฏใ€ใ“ใฎ[ไพ‹็คบใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/audio-classification)ใจ[ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb)ใซใ‚ˆใฃใฆใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- ใ“ใกใ‚‰ใ‚‚ๅ‚็…ง๏ผš[้Ÿณๅฃฐๅˆ†้กžใ‚ฟใ‚นใ‚ฏ](../tasks/audio_classification)ใ€‚ + +ใ“ใ“ใซๅ‚่€ƒ่ณ‡ๆ–™ใ‚’ๆๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฐ—ๅ…ผใญใชใPull Requestใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚็งใŸใกใฏใใ‚Œใ‚’ใƒฌใƒ“ใƒฅใƒผใ„ใŸใ—ใพใ™๏ผๅ‚่€ƒ่ณ‡ๆ–™ใฏใ€ๆ—ขๅญ˜ใฎใ‚‚ใฎใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ“ใจใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## ASTConfig + +[[autodoc]] ASTConfig + +## ASTFeatureExtractor + +[[autodoc]] ASTFeatureExtractor + - __call__ + +## ASTModel + +[[autodoc]] ASTModel + - forward + +## ASTForAudioClassification + +[[autodoc]] ASTForAudioClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/auto.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/auto.md new file mode 100644 index 0000000000000000000000000000000000000000..27030a264f5791f86993317b388a4e6712629c19 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/auto.md @@ -0,0 +1,381 @@ + + +# Auto Classes + +ๅคšใใฎๅ ดๅˆใ€`from_pretrained()`ใƒกใ‚ฝใƒƒใƒ‰ใซไธŽใˆใ‚‰ใ‚ŒใŸไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎๅๅ‰ใ‚„ใƒ‘ใ‚นใ‹ใ‚‰ใ€ไฝฟ็”จใ—ใŸใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆŽจๆธฌใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ“ใฎไป•ไบ‹ใ‚’ใ‚ใชใŸใซไปฃใ‚ใฃใฆ่กŒใ†ใŸใ‚ใซใ“ใ“ใซใ‚ใ‚Šใพใ™ใฎใงใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟ/่จญๅฎš/่ชžๅฝ™ใธใฎๅๅ‰/ใƒ‘ใ‚นใ‚’ไธŽใˆใ‚‹ใจ่‡ชๅ‹•็š„ใซ้–ข้€ฃใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’ๅ–ๅพ—ใงใใพใ™ใ€‚ + +[`AutoConfig`]ใ€[`AutoModel`]ใ€[`AutoTokenizer`]ใฎใ„ใšใ‚Œใ‹ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใจใ€้–ข้€ฃใ™ใ‚‹ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ฏใƒฉใ‚นใŒ็›ดๆŽฅไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ไพ‹ใˆใฐใ€ + +```python +model = AutoModel.from_pretrained("google-bert/bert-base-cased") +``` + +ใ“ใ‚Œใฏ[`BertModel`]ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใงใ‚ใ‚‹ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +ๅ„ใ‚ฟใ‚นใ‚ฏใ”ใจใ€ใใ—ใฆๅ„ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰๏ผˆPyTorchใ€TensorFlowใ€ใพใŸใฏFlax๏ผ‰ใ”ใจใซ`AutoModel`ใฎใ‚ฏใƒฉใ‚นใŒๅญ˜ๅœจใ—ใพใ™ใ€‚ + +## ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฎๆ‹กๅผต + +ใใ‚Œใžใ‚Œใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใซใฏใ€ใ‚ซใ‚นใ‚ฟใƒ ใ‚ฏใƒฉใ‚นใงๆ‹กๅผตใ™ใ‚‹ใŸใ‚ใฎใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€`NewModel`ใจใ„ใ†ใƒขใƒ‡ใƒซใฎใ‚ซใ‚นใ‚ฟใƒ ใ‚ฏใƒฉใ‚นใ‚’ๅฎš็พฉใ—ใŸๅ ดๅˆใ€`NewModelConfig`ใ‚’็ขบไฟใ—ใฆใŠใ‘ใฐใ“ใฎใ‚ˆใ†ใซใ—ใฆ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใซ่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```python +from transformers import AutoConfig, AutoModel + +AutoConfig.register("new-model", NewModelConfig) +AutoModel.register(NewModelConfig, NewModel) +``` + +ใใฎๅพŒใ€้€šๅธธใฉใŠใ‚Šauto classesใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผ + + + +ใ‚ใชใŸใฎ`NewModelConfig`ใŒ[`~transformers.PretrainedConfig`]ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใงใ‚ใ‚‹ๅ ดๅˆใ€ใใฎ`model_type`ๅฑžๆ€งใŒใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐใ‚’็™ป้Œฒใ™ใ‚‹ใจใใซไฝฟ็”จใ™ใ‚‹ใ‚ญใƒผ๏ผˆใ“ใ“ใงใฏ`"new-model"`๏ผ‰ใจๅŒใ˜ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +ๅŒๆง˜ใซใ€ใ‚ใชใŸใฎ`NewModel`ใŒ[`PreTrainedModel`]ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใงใ‚ใ‚‹ๅ ดๅˆใ€ใใฎ`config_class`ๅฑžๆ€งใŒใƒขใƒ‡ใƒซใ‚’็™ป้Œฒใ™ใ‚‹้š›ใซไฝฟ็”จใ™ใ‚‹ใ‚ฏใƒฉใ‚น๏ผˆใ“ใ“ใงใฏ`NewModelConfig`๏ผ‰ใจๅŒใ˜ใซ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## AutoConfig + +[[autodoc]] AutoConfig + +## AutoTokenizer + +[[autodoc]] AutoTokenizer + +## AutoFeatureExtractor + +[[autodoc]] AutoFeatureExtractor + +## AutoImageProcessor + +[[autodoc]] AutoImageProcessor + +## AutoProcessor + +[[autodoc]] AutoProcessor + +## Generic model classes + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€็‰นๅฎšใฎใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใŸใชใ„ใƒ™ใƒผใ‚นใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚นใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใŸใ‚ใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModel + +[[autodoc]] AutoModel + +### TFAutoModel + +[[autodoc]] TFAutoModel + +### FlaxAutoModel + +[[autodoc]] FlaxAutoModel + +## Generic pretraining classes + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใคใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใŸใ‚ใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModelForPreTraining + +[[autodoc]] AutoModelForPreTraining + +### TFAutoModelForPreTraining + +[[autodoc]] TFAutoModelForPreTraining + +### FlaxAutoModelForPreTraining + +[[autodoc]] FlaxAutoModelForPreTraining + +## Natural Language Processing + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€ๆฌกใฎ่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModelForCausalLM + +[[autodoc]] AutoModelForCausalLM + +### TFAutoModelForCausalLM + +[[autodoc]] TFAutoModelForCausalLM + +### FlaxAutoModelForCausalLM + +[[autodoc]] FlaxAutoModelForCausalLM + +### AutoModelForMaskedLM + +[[autodoc]] AutoModelForMaskedLM + +### TFAutoModelForMaskedLM + +[[autodoc]] TFAutoModelForMaskedLM + +### FlaxAutoModelForMaskedLM + +[[autodoc]] FlaxAutoModelForMaskedLM + +### AutoModelForMaskGeneration + +[[autodoc]] AutoModelForMaskGeneration + +### TFAutoModelForMaskGeneration + +[[autodoc]] TFAutoModelForMaskGeneration + +### AutoModelForSeq2SeqLM + +[[autodoc]] AutoModelForSeq2SeqLM + +### TFAutoModelForSeq2SeqLM + +[[autodoc]] TFAutoModelForSeq2SeqLM + +### FlaxAutoModelForSeq2SeqLM + +[[autodoc]] FlaxAutoModelForSeq2SeqLM + +### AutoModelForSequenceClassification + +[[autodoc]] AutoModelForSequenceClassification + +### TFAutoModelForSequenceClassification + +[[autodoc]] TFAutoModelForSequenceClassification + +### FlaxAutoModelForSequenceClassification + +[[autodoc]] FlaxAutoModelForSequenceClassification + +### AutoModelForMultipleChoice + +[[autodoc]] AutoModelForMultipleChoice + +### TFAutoModelForMultipleChoice + +[[autodoc]] TFAutoModelForMultipleChoice + +### FlaxAutoModelForMultipleChoice + +[[autodoc]] FlaxAutoModelForMultipleChoice + +### AutoModelForNextSentencePrediction + +[[autodoc]] AutoModelForNextSentencePrediction + +### TFAutoModelForNextSentencePrediction + +[[autodoc]] TFAutoModelForNextSentencePrediction + +### FlaxAutoModelForNextSentencePrediction + +[[autodoc]] FlaxAutoModelForNextSentencePrediction + +### AutoModelForTokenClassification + +[[autodoc]] AutoModelForTokenClassification + +### TFAutoModelForTokenClassification + +[[autodoc]] TFAutoModelForTokenClassification + +### FlaxAutoModelForTokenClassification + +[[autodoc]] FlaxAutoModelForTokenClassification + +### AutoModelForQuestionAnswering + +[[autodoc]] AutoModelForQuestionAnswering + +### TFAutoModelForQuestionAnswering + +[[autodoc]] TFAutoModelForQuestionAnswering + +### FlaxAutoModelForQuestionAnswering + +[[autodoc]] FlaxAutoModelForQuestionAnswering + +### AutoModelForTextEncoding + +[[autodoc]] AutoModelForTextEncoding + +### TFAutoModelForTextEncoding + +[[autodoc]] TFAutoModelForTextEncoding + +## Computer vision + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€ๆฌกใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModelForDepthEstimation + +[[autodoc]] AutoModelForDepthEstimation + +### AutoModelForImageClassification + +[[autodoc]] AutoModelForImageClassification + +### TFAutoModelForImageClassification + +[[autodoc]] TFAutoModelForImageClassification + +### FlaxAutoModelForImageClassification + +[[autodoc]] FlaxAutoModelForImageClassification + +### AutoModelForVideoClassification + +[[autodoc]] AutoModelForVideoClassification + +### AutoModelForMaskedImageModeling + +[[autodoc]] AutoModelForMaskedImageModeling + +### TFAutoModelForMaskedImageModeling + +[[autodoc]] TFAutoModelForMaskedImageModeling + +### AutoModelForObjectDetection + +[[autodoc]] AutoModelForObjectDetection + +### AutoModelForImageSegmentation + +[[autodoc]] AutoModelForImageSegmentation + +### AutoModelForImageToImage + +[[autodoc]] AutoModelForImageToImage + +### AutoModelForSemanticSegmentation + +[[autodoc]] AutoModelForSemanticSegmentation + +### TFAutoModelForSemanticSegmentation + +[[autodoc]] TFAutoModelForSemanticSegmentation + +### AutoModelForInstanceSegmentation + +[[autodoc]] AutoModelForInstanceSegmentation + +### AutoModelForUniversalSegmentation + +[[autodoc]] AutoModelForUniversalSegmentation + +### AutoModelForZeroShotImageClassification + +[[autodoc]] AutoModelForZeroShotImageClassification + +### TFAutoModelForZeroShotImageClassification + +[[autodoc]] TFAutoModelForZeroShotImageClassification + +### AutoModelForZeroShotObjectDetection + +[[autodoc]] AutoModelForZeroShotObjectDetection + +## Audio + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€ๆฌกใฎ้Ÿณๅฃฐใ‚ฟใ‚นใ‚ฏใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModelForAudioClassification + +[[autodoc]] AutoModelForAudioClassification + +### AutoModelForAudioFrameClassification + +[[autodoc]] TFAutoModelForAudioClassification + +### TFAutoModelForAudioFrameClassification + +[[autodoc]] AutoModelForAudioFrameClassification + +### AutoModelForCTC + +[[autodoc]] AutoModelForCTC + +### AutoModelForSpeechSeq2Seq + +[[autodoc]] AutoModelForSpeechSeq2Seq + +### TFAutoModelForSpeechSeq2Seq + +[[autodoc]] TFAutoModelForSpeechSeq2Seq + +### FlaxAutoModelForSpeechSeq2Seq + +[[autodoc]] FlaxAutoModelForSpeechSeq2Seq + +### AutoModelForAudioXVector + +[[autodoc]] AutoModelForAudioXVector + +### AutoModelForTextToSpectrogram + +[[autodoc]] AutoModelForTextToSpectrogram + +### AutoModelForTextToWaveform + +[[autodoc]] AutoModelForTextToWaveform + +## Multimodal + +ไปฅไธ‹ใฎ่‡ชๅ‹•ใ‚ฏใƒฉใ‚นใฏใ€ๆฌกใฎใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใซๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +### AutoModelForTableQuestionAnswering + +[[autodoc]] AutoModelForTableQuestionAnswering + +### TFAutoModelForTableQuestionAnswering + +[[autodoc]] TFAutoModelForTableQuestionAnswering + +### AutoModelForDocumentQuestionAnswering + +[[autodoc]] AutoModelForDocumentQuestionAnswering + +### TFAutoModelForDocumentQuestionAnswering + +[[autodoc]] TFAutoModelForDocumentQuestionAnswering + +### AutoModelForVisualQuestionAnswering + +[[autodoc]] AutoModelForVisualQuestionAnswering + +### AutoModelForVision2Seq + +[[autodoc]] AutoModelForVision2Seq + +### TFAutoModelForVision2Seq + +[[autodoc]] TFAutoModelForVision2Seq + +### FlaxAutoModelForVision2Seq + +[[autodoc]] FlaxAutoModelForVision2Seq + +### AutoModelForImageTextToText + +[[autodoc]] AutoModelForImageTextToText + +## Time Series + +### AutoModelForTimeSeriesPrediction + +[[autodoc]] AutoModelForTimeSeriesPrediction + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/autoformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/autoformer.md new file mode 100644 index 0000000000000000000000000000000000000000..65c20bfa60f5de15b351f2d079b2e74afcdd335f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/autoformer.md @@ -0,0 +1,50 @@ + + +# Autoformer + +## ๆฆ‚่ฆ + +Autoformerใƒขใƒ‡ใƒซใฏใ€ใ€Œ[Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting](https://huggingface.co/papers/2106.13008)ใ€ใจใ„ใ†่ซ–ๆ–‡ใงHaixu Wuใ€Jiehui Xuใ€Jianmin Wangใ€Mingsheng Longใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€ไบˆๆธฌใƒ—ใƒญใ‚ปใ‚นไธญใซใƒˆใƒฌใƒณใƒ‰ใจๅญฃ็ฏ€ๆ€งๆˆๅˆ†ใ‚’้€ๆฌก็š„ใซๅˆ†่งฃใงใใ‚‹ๆทฑๅฑคๅˆ†่งฃใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใ—ใฆTransformerใ‚’ๅข—ๅผทใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +*ไพ‹ใˆใฐ็•ฐๅธธๆฐ—่ฑกใฎๆ—ฉๆœŸ่ญฆๅ‘Šใ‚„้•ทๆœŸ็š„ใชใ‚จใƒใƒซใ‚ฎใƒผๆถˆ่ฒป่จˆ็”ปใจใ„ใฃใŸๅฎŸๅฟœ็”จใซใŠใ„ใฆใ€ไบˆๆธฌๆ™‚้–“ใ‚’ๅปถ้•ทใ™ใ‚‹ใ“ใจใฏ้‡่ฆใช่ฆๆฑ‚ใงใ™ใ€‚ๆœฌ่ซ–ๆ–‡ใงใฏใ€ๆ™‚็ณปๅˆ—ใฎ้•ทๆœŸไบˆๆธฌๅ•้กŒใ‚’็ ”็ฉถใ—ใฆใ„ใพใ™ใ€‚ไปฅๅ‰ใฎTransformerใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฏใ€้•ท่ท้›ขไพๅญ˜้–ขไฟ‚ใ‚’็™บ่ฆ‹ใ™ใ‚‹ใŸใ‚ใซๆง˜ใ€…ใชใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๆฉŸๆง‹ใ‚’ๆŽก็”จใ—ใฆใ„ใพใ™ใ€‚ใ—ใ‹ใ—ใ€้•ทๆœŸๆœชๆฅใฎ่ค‡้›‘ใชๆ™‚้–“็š„ใƒ‘ใ‚ฟใƒผใƒณใซใ‚ˆใฃใฆใƒขใƒ‡ใƒซใŒไฟก้ ผใงใใ‚‹ไพๅญ˜้–ขไฟ‚ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใ‚’ๅฆจใ’ใ‚‰ใ‚Œใพใ™ใ€‚ใพใŸใ€Transformerใฏใ€้•ทใ„็ณปๅˆ—ใฎๅŠน็އๅŒ–ใฎใŸใ‚ใซใƒใ‚คใƒณใƒˆใƒฏใ‚คใ‚บใชใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฎใ‚นใƒ‘ใƒผใ‚นใƒใƒผใ‚ธใƒงใƒณใ‚’ๆŽก็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ๆƒ…ๅ ฑๅˆฉ็”จใฎใƒœใƒˆใƒซใƒใƒƒใ‚ฏใจใชใ‚Šใพใ™ใ€‚Transformerใ‚’่ถ…ใˆใฆใ€ๆˆ‘ใ€…ใฏ่‡ชๅทฑ็›ธ้–ขๆฉŸๆง‹ใ‚’ๆŒใคๆ–ฐใ—ใ„ๅˆ†่งฃใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใ—ใฆAutoformerใ‚’่จญ่จˆใ—ใพใ—ใŸใ€‚็ณปๅˆ—ๅˆ†่งฃใฎไบ‹ๅ‰ๅ‡ฆ็†ใฎๆ…ฃ่กŒใ‚’็ ดใ‚Šใ€ใใ‚Œใ‚’ๆทฑๅฑคใƒขใƒ‡ใƒซใฎๅŸบๆœฌ็š„ใชๅ†…้ƒจใƒ–ใƒญใƒƒใ‚ฏใจใ—ใฆ้ฉๆ–ฐใ—ใพใ™ใ€‚ใ“ใฎ่จญ่จˆใฏใ€่ค‡้›‘ใชๆ™‚็ณปๅˆ—ใซๅฏพใ™ใ‚‹Autoformerใฎ้€ฒ่กŒ็š„ใชๅˆ†่งฃ่ƒฝๅŠ›ใ‚’ๅผทๅŒ–ใ—ใพใ™ใ€‚ใ•ใ‚‰ใซใ€็ขบ็އ้Ž็จ‹็†่ซ–ใซ่งฆ็™บใ•ใ‚Œใฆใ€็ณปๅˆ—ใฎๅ‘จๆœŸๆ€งใซๅŸบใฅใ„ใŸ่‡ชๅทฑ็›ธ้–ขๆฉŸๆง‹ใ‚’่จญ่จˆใ—ใ€ใ‚ตใƒ–็ณปๅˆ—ใƒฌใƒ™ใƒซใงใฎไพๅญ˜้–ขไฟ‚ใฎ็™บ่ฆ‹ใจ่กจ็พใฎ้›†็ด„ใ‚’่กŒใ„ใพใ™ใ€‚่‡ชๅทฑ็›ธ้–ขใฏๅŠน็އใจ็ฒพๅบฆใฎไธกๆ–นใงใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไธŠๅ›žใ‚Šใพใ™ใ€‚้•ทๆœŸไบˆๆธฌใซใŠใ„ใฆใ€Autoformerใฏใ€ใ‚จใƒใƒซใ‚ฎใƒผใ€ไบค้€šใ€็ตŒๆธˆใ€ๆฐ—่ฑกใ€็–พ็—…ใฎ5ใคใฎๅฎŸ็”จ็š„ใชๅฟœ็”จใ‚’ใ‚ซใƒใƒผใ™ใ‚‹6ใคใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใง38%ใฎ็›ธๅฏพ็š„ใชๆ”นๅ–„ใ‚’ใ‚‚ใŸใ‚‰ใ—ใ€ๆœ€ๅ…ˆ็ซฏใฎ็ฒพๅบฆใ‚’้”ๆˆใ—ใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[elisim](https://huggingface.co/elisim)ใจ[kashif](https://huggingface.co/kashif)ใ‚ˆใ‚Šๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏ[ใ“ใกใ‚‰](https://github.com/thuml/Autoformer)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +## ๅ‚่€ƒ่ณ‡ๆ–™ + +Autoformerใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผใฎHugging FaceใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ๏ผˆ๐ŸŒŽใง็คบใ•ใ‚Œใฆใ„ใ‚‹๏ผ‰ใฎๅ‚่€ƒ่ณ‡ๆ–™ใฎไธ€่ฆงใงใ™ใ€‚ใ“ใ“ใซๅ‚่€ƒ่ณ‡ๆ–™ใ‚’ๆๅ‡บใ—ใŸใ„ๅ ดๅˆใฏใ€ๆฐ—ๅ…ผใญใชใPull Requestใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚็งใŸใกใฏใใ‚Œใ‚’ใƒฌใƒ“ใƒฅใƒผใ„ใŸใ—ใพใ™๏ผๅ‚่€ƒ่ณ‡ๆ–™ใฏใ€ๆ—ขๅญ˜ใฎใ‚‚ใฎใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ“ใจใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +- HuggingFaceใƒ–ใƒญใ‚ฐใงAutoformerใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผš[ใฏใ„ใ€Transformersใฏๆ™‚็ณปๅˆ—ไบˆๆธฌใซๅŠนๆžœ็š„ใงใ™๏ผˆ+ Autoformer๏ผ‰](https://huggingface.co/blog/autoformer) + +## AutoformerConfig + +[[autodoc]] AutoformerConfig + +## AutoformerModel + +[[autodoc]] AutoformerModel + - forward + +## AutoformerForPrediction + +[[autodoc]] AutoformerForPrediction + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bark.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bark.md new file mode 100644 index 0000000000000000000000000000000000000000..508b8938889bae6960bcd2dddd155a88052174cf --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bark.md @@ -0,0 +1,198 @@ + + +# Bark + +## Overview + +Bark ใฏใ€[suno-ai/bark](https://github.com/suno-ai/bark) ใง Suno AI ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚ŒใŸใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎใƒ†ใ‚ญใ‚นใƒˆ่ชญใฟไธŠใ’ใƒขใƒ‡ใƒซใงใ™ใ€‚ + + +Bark ใฏ 4 ใคใฎไธป่ฆใชใƒขใƒ‡ใƒซใงๆง‹ๆˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +- [`BarkSemanticModel`] ('ใƒ†ใ‚ญใ‚นใƒˆ'ใƒขใƒ‡ใƒซใจใ‚‚ๅ‘ผใฐใ‚Œใ‚‹): ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใฎๆ„ๅ‘ณใ‚’ๆ‰ใˆใ‚‹ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใƒ†ใ‚ญใ‚นใƒˆ ใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ๅ› ๆžœ็š„่‡ชๅทฑๅ›žๅธฐๅค‰ๆ›ใƒขใƒ‡ใƒซใ€‚ +- [`BarkCoarseModel`] ('็ฒ—ใ„้Ÿณ้Ÿฟ' ใƒขใƒ‡ใƒซใจใ‚‚ๅ‘ผใฐใ‚Œใ‚‹): [`BarkSemanticModel`] ใƒขใƒ‡ใƒซใฎ็ตๆžœใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ๅ› ๆžœ็š„่‡ชๅทฑๅ›žๅธฐๅค‰ๆ›ๅ™จใ€‚ EnCodec ใซๅฟ…่ฆใชๆœ€ๅˆใฎ 2 ใคใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ +- [`BarkFineModel`] ('ๅพฎ็ดฐ้Ÿณ้Ÿฟ' ใƒขใƒ‡ใƒซ)ใ€ไปŠๅ›žใฏ้žๅ› ๆžœ็š„ใ‚ชใƒผใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ€ไปฅๅ‰ใฎใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏๅŸ‹ใ‚่พผใฟใฎๅˆ่จˆใซๅŸบใฅใ„ใฆๆœ€ๅพŒใฎใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏใ‚’็นฐใ‚Š่ฟ”ใ—ไบˆๆธฌใ—ใพใ™ใ€‚ +- [`EncodecModel`] ใ‹ใ‚‰ใ™ในใฆใฎใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏ ใƒใƒฃใƒใƒซใ‚’ไบˆๆธฌใ—ใŸใฎใงใ€Bark ใฏใใ‚Œใ‚’ไฝฟ็”จใ—ใฆๅ‡บๅŠ›ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช้…ๅˆ—ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใพใ™ใ€‚ + +ๆœ€ๅˆใฎ 3 ใคใฎใƒขใ‚ธใƒฅใƒผใƒซใฏใใ‚Œใžใ‚Œใ€็‰นๅฎšใฎไบ‹ๅ‰ๅฎš็พฉใ•ใ‚ŒใŸ้Ÿณๅฃฐใซๅพ“ใฃใฆๅ‡บๅŠ›ใ‚ตใ‚ฆใƒณใƒ‰ใ‚’่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎๆกไปถไป˜ใใ‚นใƒ”ใƒผใ‚ซใƒผๅŸ‹ใ‚่พผใฟใ‚’ใ‚ตใƒใƒผใƒˆใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +### Optimizing Bark + +Bark ใฏใ€ใ‚ณใƒผใƒ‰ใ‚’ๆ•ฐ่กŒ่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๆœ€้ฉๅŒ–ใงใใ€**ใƒกใƒขใƒช ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใŒๅคงๅน…ใซๅ‰Šๆธ›**ใ•ใ‚Œใ€**ๆŽจ่ซ–ใŒ้ซ˜้€ŸๅŒ–**ใ•ใ‚Œใพใ™ใ€‚ + +#### Using half-precision + +ใƒขใƒ‡ใƒซใ‚’ๅŠ็ฒพๅบฆใงใƒญใƒผใƒ‰ใ™ใ‚‹ใ ใ‘ใงใ€ๆŽจ่ซ–ใ‚’้ซ˜้€ŸๅŒ–ใ—ใ€ใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ 50% ๅ‰Šๆธ›ใงใใพใ™ใ€‚ + +```python +from transformers import BarkModel +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" +model = BarkModel.from_pretrained("suno/bark-small", torch_dtype=torch.float16).to(device) +``` + +#### Using ๐Ÿค— Better Transformer + +Better Transformer ใฏใ€ๅ†…้ƒจใงใ‚ซใƒผใƒใƒซ่žๅˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ ๐Ÿค— ๆœ€้ฉใชๆฉŸ่ƒฝใงใ™ใ€‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ไฝŽไธ‹ใ•ใ›ใ‚‹ใ“ใจใชใใ€้€Ÿๅบฆใ‚’ 20% ๏ฝž 30% ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— Better Transformer ใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใฎใซๅฟ…่ฆใชใ‚ณใƒผใƒ‰ใฏ 1 ่กŒใ ใ‘ใงใ™ใ€‚ + +```python +model = model.to_bettertransformer() +``` + +ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซ ๐Ÿค— Optimum ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ [ใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ–นๆณ•ใฏใ“ใกใ‚‰](https://huggingface.co/docs/optimum/installation) + +#### Using CPU offload + +ๅ‰่ฟฐใ—ใŸใ‚ˆใ†ใซใ€Bark ใฏ 4 ใคใฎใ‚ตใƒ–ใƒขใƒ‡ใƒซใงๆง‹ๆˆใ•ใ‚ŒใฆใŠใ‚Šใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช็”Ÿๆˆไธญใซ้ †็•ชใซๅ‘ผใณๅ‡บใ•ใ‚Œใพใ™ใ€‚่จ€ใ„ๆ›ใˆใ‚Œใฐใ€1 ใคใฎใ‚ตใƒ–ใƒขใƒ‡ใƒซใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹้–“ใ€ไป–ใฎใ‚ตใƒ–ใƒขใƒ‡ใƒซใฏใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใซใชใ‚Šใพใ™ใ€‚ + +CUDA ใƒ‡ใƒใ‚คใ‚นใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒกใƒขใƒช ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใฎ 80% ๅ‰Šๆธ›ใซใ‚ˆใ‚‹ๆฉๆตใ‚’ๅ—ใ‘ใ‚‹็ฐกๅ˜ใช่งฃๆฑบ็ญ–ใฏใ€ใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใฎ GPU ใฎใ‚ตใƒ–ใƒขใƒ‡ใƒซใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใฎๆ“ไฝœใฏ CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใจๅ‘ผใฐใ‚Œใพใ™ใ€‚ 1่กŒใฎใ‚ณใƒผใƒ‰ใงไฝฟ็”จใงใใพใ™ใ€‚ + +```python +model.enable_cpu_offload() +``` + +ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€๐Ÿค— Accelerate ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ [ใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ–นๆณ•ใฏใ“ใกใ‚‰](https://huggingface.co/docs/accelerate/basic_tutorials/install) + +#### Combining optimization techniques + +ๆœ€้ฉๅŒ–ๆ‰‹ๆณ•ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ€CPU ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ€ๅŠ็ฒพๅบฆใ€๐Ÿค— Better Transformer ใ‚’ใ™ในใฆไธ€ๅบฆใซไฝฟ็”จใงใใพใ™ใ€‚ + +```python +from transformers import BarkModel +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" + +# load in fp16 +model = BarkModel.from_pretrained("suno/bark-small", torch_dtype=torch.float16).to(device) + +# convert to bettertransformer +model = BetterTransformer.transform(model, keep_original_model=False) + +# enable CPU offload +model.enable_cpu_offload() +``` + +ๆŽจ่ซ–ๆœ€้ฉๅŒ–ๆ‰‹ๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใกใ‚‰](https://huggingface.co/docs/transformers/perf_infer_gpu_one) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +### Tips + +Suno ใฏใ€ๅคšใใฎ่จ€่ชžใง้Ÿณๅฃฐใƒ—ใƒชใ‚ปใƒƒใƒˆใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ [ใ“ใกใ‚‰](https://suno-ai.notion.site/8b8e8749ed514b0cbf3f699013548683?v=bc67cff786b04b50b3ceb756fd05f68c)ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒ—ใƒชใ‚ปใƒƒใƒˆใฏใ€ใƒใƒ– [ใ“ใกใ‚‰](https://huggingface.co/suno/bark-small/tree/main/speaker_embeddings) ใพใŸใฏ [ใ“ใกใ‚‰](https://huggingface.co/suno/bark/tree/main/speaker_embeddings)ใ€‚ + +```python +>>> from transformers import AutoProcessor, BarkModel + +>>> processor = AutoProcessor.from_pretrained("suno/bark") +>>> model = BarkModel.from_pretrained("suno/bark") + +>>> voice_preset = "v2/en_speaker_6" + +>>> inputs = processor("Hello, my dog is cute", voice_preset=voice_preset) + +>>> audio_array = model.generate(**inputs) +>>> audio_array = audio_array.cpu().numpy().squeeze() +``` + +Bark ใฏใ€้žๅธธใซใƒชใ‚ขใƒซใช **ๅคš่จ€่ชž** ้Ÿณๅฃฐใ ใ‘ใงใชใใ€้Ÿณๆฅฝใ€่ƒŒๆ™ฏใƒŽใ‚คใ‚บใ€ๅ˜็ด”ใชๅŠนๆžœ้Ÿณใชใฉใฎไป–ใฎ้Ÿณๅฃฐใ‚‚็”Ÿๆˆใงใใพใ™ใ€‚ + +```python +>>> # Multilingual speech - simplified Chinese +>>> inputs = processor("ๆƒŠไบบ็š„๏ผๆˆ‘ไผš่ฏดไธญๆ–‡") + +>>> # Multilingual speech - French - let's use a voice_preset as well +>>> inputs = processor("Incroyable! Je peux gรฉnรฉrer du son.", voice_preset="fr_speaker_5") + +>>> # Bark can also generate music. You can help it out by adding music notes around your lyrics. +>>> inputs = processor("โ™ช Hello, my dog is cute โ™ช") + +>>> audio_array = model.generate(**inputs) +>>> audio_array = audio_array.cpu().numpy().squeeze() +``` + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€็ฌ‘ใ†ใ€ใŸใ‚ๆฏใ€ๆณฃใใชใฉใฎ**้ž่จ€่ชžใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณ**ใ‚’็”Ÿๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + +```python +>>> # Adding non-speech cues to the input text +>>> inputs = processor("Hello uh ... [clears throat], my dog is cute [laughter]") + +>>> audio_array = model.generate(**inputs) +>>> audio_array = audio_array.cpu().numpy().squeeze() +``` + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚’ไฟๅญ˜ใ™ใ‚‹ใซใฏใ€ใƒขใƒ‡ใƒซ่จญๅฎšใจ scipy ใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‹ใ‚‰ใ‚ตใƒณใƒ—ใƒซ ใƒฌใƒผใƒˆใ‚’ๅ–ๅพ—ใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + +```python +>>> from scipy.io.wavfile import write as write_wav + +>>> # save audio to disk, but first take the sample rate from the model config +>>> sample_rate = model.generation_config.sample_rate +>>> write_wav("bark_generation.wav", sample_rate, audio_array) +``` + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[Yoach Lacombe (ylacombe)](https://huggingface.co/ylacombe) ใŠใ‚ˆใณ [Sanchit Gandhi (sanchit-gandhi)](https://github.com/sanchit-gandhi) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/suno-ai/bark) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## BarkConfig + +[[autodoc]] BarkConfig + - all + +## BarkProcessor + +[[autodoc]] BarkProcessor + - all + - __call__ + +## BarkModel + +[[autodoc]] BarkModel + - generate + - enable_cpu_offload + +## BarkSemanticModel + +[[autodoc]] BarkSemanticModel + - forward + +## BarkCoarseModel + +[[autodoc]] BarkCoarseModel + - forward + +## BarkFineModel + +[[autodoc]] BarkFineModel + - forward + +## BarkCausalModel + +[[autodoc]] BarkCausalModel + - forward + +## BarkCoarseConfig + +[[autodoc]] BarkCoarseConfig + - all + +## BarkFineConfig + +[[autodoc]] BarkFineConfig + - all + +## BarkSemanticConfig + +[[autodoc]] BarkSemanticConfig + - all diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bart.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bart.md new file mode 100644 index 0000000000000000000000000000000000000000..4584f4815017b2d7f2ad131c4ff0551a468d982f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bart.md @@ -0,0 +1,223 @@ + + +# BART + +
+ +Models + + +Spaces + +
+ +**ๅ…่ฒฌไบ‹้ …:** ไฝ•ใ‹ๅฅ‡ๅฆ™ใชใ‚‚ใฎใ‚’่ฆ‹ใคใ‘ใŸๅ ดๅˆใฏใ€[Github ๅ•้กŒ](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) ใ‚’ๆๅ‡บใ—ใ€ๅ‰ฒใ‚Šๅฝ“ใฆใฆใใ ใ•ใ„ใ€‚ +@patrickvonplaten + +## Overview + +Bart ใƒขใƒ‡ใƒซใฏใ€[BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generationใ€ +็ฟป่จณใจ็†่งฃ](https://huggingface.co/papers/1910.13461) Mike Lewisใ€Yinhan Liuใ€Naman Goyalใ€Marjan ่‘— +ใ‚ฌใ‚บใƒ“ใƒ‹ใƒใ‚ธใƒฃใƒ‰ใ€ใ‚ขใƒ–ใƒ‡ใƒซใƒฉใƒ•ใƒžใƒณใƒปใƒขใƒใƒกใƒ‰ใ€ใ‚ชใƒกใƒซใƒปใƒฌใƒดใ‚ฃใ€ใƒ™ใ‚นใƒปใ‚นใƒˆใƒคใƒŽใƒ•ใ€ใƒซใƒผใ‚ฏใƒปใ‚ผใƒˆใƒซใƒขใ‚คใƒคใƒผใ€2019ๅนด10ๆœˆ29ๆ—ฅใ€‚ + +่ฆ็ด„ใซใ‚ˆใ‚‹ใจใ€ + +- Bart ใฏใ€ๅŒๆ–นๅ‘ใ‚จใƒณใ‚ณใƒผใƒ€ (BERT ใชใฉ) ใ‚’ๅ‚™ใˆใŸๆจ™ๆบ–ใฎ seq2seq/ๆฉŸๆขฐ็ฟป่จณใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + ๅทฆใ‹ใ‚‰ๅณใธใฎใƒ‡ใ‚ณใƒผใƒ€ (GPT ใชใฉ)ใ€‚ +- ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏใซใฏใ€ๅ…ƒใฎๆ–‡ใฎ้ †ๅบใ‚’ใƒฉใƒณใƒ€ใƒ ใซใ‚ทใƒฃใƒƒใƒ•ใƒซใ—ใ€ๆ–ฐใ—ใ„ๅŸ‹ใ‚่พผใฟใ‚นใ‚ญใƒผใƒ ใŒๅซใพใ‚Œใพใ™ใ€‚ + ใ“ใ“ใงใ€ใƒ†ใ‚ญใ‚นใƒˆใฎ็ฏ„ๅ›ฒใฏๅ˜ไธ€ใฎใƒžใ‚นใ‚ฏ ใƒˆใƒผใ‚ฏใƒณใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ +- BART ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ็”จใซๅพฎ่ชฟๆ•ดใ—ใŸๅ ดๅˆใซ็‰นใซๅŠนๆžœ็š„ใงใ™ใŒใ€็†่งฃใ‚ฟใ‚นใ‚ฏใซใ‚‚้ฉใ—ใฆใ„ใพใ™ใ€‚ใใ‚Œ + RoBERTa ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ GLUE ใŠใ‚ˆใณ SQuAD ใฎๅŒ็ญ‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒชใ‚ฝใƒผใ‚นใจๅŒ็ญ‰ใซใ—ใ€ๆ–ฐใŸใชๆˆๆžœใ‚’้”ๆˆใ—ใพใ™ใ€‚ + ใ•ใพใ–ใพใชๆŠฝ่ฑก็š„ใชๅฏพ่ฉฑใ€่ณชๅ•ๅฟœ็ญ”ใ€่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹ๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใ€ๆˆๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + ใƒซใƒผใ‚ธใƒฅใฏๆœ€ๅคง6ๆžšใพใงใ€‚ + +ใƒใƒƒใƒ—๏ผš + +- BART ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ +- ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‚’ๅ‚™ใˆใŸใ‚ทใƒผใ‚ฑใƒณใ‚นใƒ„ใƒผใ‚ทใƒผใ‚ฑใƒณใ‚น ใƒขใƒ‡ใƒซใ€‚ใ‚จใƒณใ‚ณใƒผใƒ€ใซใฏ็ ดๆใ—ใŸใƒใƒผใ‚ธใƒงใƒณใฎใƒˆใƒผใ‚ฏใƒณใŒไพ›็ตฆใ•ใ‚Œใ€ใƒ‡ใ‚ณใƒผใƒ€ใซใฏๅ…ƒใฎใƒˆใƒผใ‚ฏใƒณใŒไพ›็ตฆใ•ใ‚Œใพใ™๏ผˆใŸใ ใ—ใ€้€šๅธธใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใƒ‡ใ‚ณใƒผใƒ€ใจๅŒๆง˜ใซใ€ๅฐ†ๆฅใฎใƒฏใƒผใƒ‰ใ‚’้š ใ™ใŸใ‚ใฎใƒžใ‚นใ‚ฏใŒใ‚ใ‚Šใพใ™๏ผ‰ใ€‚ๆฌกใฎๅค‰ๆ›ใฎๆง‹ๆˆใฏใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏใซ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + + * ใƒฉใƒณใƒ€ใƒ ใชใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ—ใพใ™ (BERT ใจๅŒๆง˜) + * ใƒฉใƒณใƒ€ใƒ ใชใƒˆใƒผใ‚ฏใƒณใ‚’ๅ‰Š้™คใ—ใพใ™ + * k ๅ€‹ใฎใƒˆใƒผใ‚ฏใƒณใฎใ‚นใƒ‘ใƒณใ‚’ 1 ใคใฎใƒžใ‚นใ‚ฏ ใƒˆใƒผใ‚ฏใƒณใงใƒžใ‚นใ‚ฏใ—ใพใ™ (0 ใƒˆใƒผใ‚ฏใƒณใฎใ‚นใƒ‘ใƒณใฏใƒžใ‚นใ‚ฏ ใƒˆใƒผใ‚ฏใƒณใฎๆŒฟๅ…ฅใงใ™) + * ๆ–‡ใ‚’ไธฆในๆ›ฟใˆใพใ™ + * ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ›ž่ปขใ—ใฆ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰้–‹ๅง‹ใ™ใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [sshleifer](https://huggingface.co/sshleifer) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚่‘—่€…ใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/pytorch/fairseq/tree/master/examples/bart) ใซใ‚ใ‚Šใพใ™ใ€‚ + +### Examples + +- ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใ‚ฟใ‚นใ‚ฏ็”จใฎ BART ใŠใ‚ˆใณใใฎไป–ใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎไพ‹ใจใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ๆฌกใฎๅ ดๆ‰€ใซใ‚ใ‚Šใพใ™ใ€‚ + [examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md)ใ€‚ +- Hugging Face `datasets` ใ‚’ไฝฟ็”จใ—ใฆ [`BartForConditionalGeneration`] ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ + ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏใ€ใ“ใฎ [ใƒ•ใ‚ฉใƒผใƒฉใƒ  ใƒ‡ใ‚ฃใ‚นใ‚ซใƒƒใ‚ทใƒงใƒณ](https://discuss.huggingface.co/t/train-bart-for-conditional-generation-e-g-summarization/1904) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- [ๆŠฝๅ‡บใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?search=distilbart) ใฏใ€ใ“ใฎ [่ซ–ๆ–‡](https://huggingface.co/papers/2010.13002) ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## Implementation Notes + +- Bart ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๅˆ†้กžใซ `token_type_ids` ใ‚’ไฝฟ็”จใ—ใพใ›ใ‚“ใ€‚ [`BartTokenizer`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ€ + [`~BartTokenizer.encode`] ใ‚’ไฝฟ็”จใ—ใฆ้ฉๅˆ‡ใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ +- [`BartModel`] ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฏใ€ๆธกใ•ใ‚Œใชใ‹ใฃใŸๅ ดๅˆใ€`decoder_input_ids` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + ใ“ใ‚Œใฏใ€ไป–ใฎใƒขใƒ‡ใƒชใƒณใ‚ฐ API ใจใฏ็•ฐใชใ‚Šใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใฎไธ€่ˆฌ็š„ใชไฝฟ็”จไพ‹ใฏใ€ใƒžใ‚นใ‚ฏใฎๅก—ใ‚Šใคใถใ—ใงใ™ใ€‚ +- ใƒขใƒ‡ใƒซใฎไบˆๆธฌใฏใ€ๆฌกใฎๅ ดๅˆใซๅ…ƒใฎๅฎŸ่ฃ…ใจๅŒไธ€ใซใชใ‚‹ใ‚ˆใ†ใซๆ„ๅ›ณใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + `forced_bos_token_id=0`ใ€‚ใŸใ ใ—ใ€ใ“ใ‚Œใฏใ€ๆธกใ™ๆ–‡ๅญ—ๅˆ—ใŒๆฌกใฎๅ ดๅˆใซใฎใฟๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + [`fairseq.encode`] ใฏใ‚นใƒšใƒผใ‚นใงๅง‹ใพใ‚Šใพใ™ใ€‚ +- [`~generation.GenerationMixin.generate`] ใฏใ€ๆฌกใฎใ‚ˆใ†ใชๆกไปถไป˜ใ็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + ่ฆ็ด„ใซใคใ„ใฆใฏใ€ใใฎ docstring ใฎไพ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- *facebook/bart-large-cnn* ้‡ใฟใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใƒขใƒ‡ใƒซใซใฏ `mask_token_id` ใŒใชใ„ใ‹ใ€ๅฎŸ่กŒใงใใพใ›ใ‚“ใ€‚ + ใƒžใ‚นใ‚ฏใ‚’ๅŸ‹ใ‚ใ‚‹ใ‚ฟใ‚นใ‚ฏใ€‚ + +## Mask Filling + +`facebook/bart-base` ใŠใ‚ˆใณ `facebook/bart-large` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€ใƒžใƒซใƒใƒˆใƒผใ‚ฏใƒณ ใƒžใ‚นใ‚ฏใ‚’ๅŸ‹ใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +from transformers import BartForConditionalGeneration, BartTokenizer + +model = BartForConditionalGeneration.from_pretrained("facebook/bart-large", forced_bos_token_id=0) +tok = BartTokenizer.from_pretrained("facebook/bart-large") +example_english_phrase = "UN Chief Says There Is No in Syria" +batch = tok(example_english_phrase, return_tensors="pt") +generated_ids = model.generate(batch["input_ids"]) +assert tok.batch_decode(generated_ids, skip_special_tokens=True) == [ + "UN Chief Says There Is No Plan to Stop Chemical Weapons in Syria" +] +``` + +## Resources + +BART ใ‚’ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + + + +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [ๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ: ๐Ÿค— Transformers ใจ Amazon SageMaker ใ‚’ไฝฟ็”จใ—ใŸ่ฆ็ด„ใฎใŸใ‚ใฎ BART/T5 ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://huggingface.co/blog/sagemaker-distributed-training-seq2seq)ใ€‚ +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ [blurr ใ‚’ไฝฟ็”จใ—ใฆ fastai ใง่ฆ็ด„ใ™ใ‚‹ใŸใ‚ใซ BART ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb). ๐ŸŒŽ ๐ŸŒŽ +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ [ใƒˆใƒฌใƒผใƒŠใƒผ ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆ 2 ใคใฎ่จ€่ชžใง่ฆ็ด„ใ™ใ‚‹ใŸใ‚ใซ BART ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)ใ€‚ ๐ŸŒŽ +- [`BartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb)ใ€‚ +- [`TFBartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb)ใ€‚ +- [`FlaxBartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- [่ฆ็ด„](https://huggingface.co/course/chapter7/5?fw=pt#summarization) ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/summarization) + + + +- [`BartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)ใ€‚ +- [`TFBartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)ใ€‚ +- [`FlaxBartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ]( https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)ใ€‚ +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](https://huggingface.co/course/chapter7/3?fw=pt) ๐Ÿค— ้ก”ใƒใ‚ฐ ใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_lang_modeling) + + + +- [ใƒ’ใƒณใƒ‡ใ‚ฃใƒผ่ชžใ‹ใ‚‰่‹ฑ่ชžใธใฎ็ฟป่จณใซ Seq2SeqTrainer ใ‚’ไฝฟ็”จใ—ใฆ mBART ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒŽใƒผใƒˆ](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)ใ€‚ ๐ŸŒŽ +- [`BartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/translation) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb)ใ€‚ +- [`TFBartForConditionalGeneration`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/translation) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb)ใ€‚ +- [็ฟป่จณใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/translation) + +ไปฅไธ‹ใ‚‚ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [ๆŠฝๅ‡บใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?search=distilbart) ใฏใ€ใ“ใฎ [่ซ–ๆ–‡](https://huggingface.co/papers/2010.13002) ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## BartConfig + +[[autodoc]] BartConfig + - all + +## BartTokenizer + +[[autodoc]] BartTokenizer + - all + +## BartTokenizerFast + +[[autodoc]] BartTokenizerFast + - all + +## BartModel + +[[autodoc]] BartModel + - forward + +## BartForConditionalGeneration + +[[autodoc]] BartForConditionalGeneration + - forward + +## BartForSequenceClassification + +[[autodoc]] BartForSequenceClassification + - forward + +## BartForQuestionAnswering + +[[autodoc]] BartForQuestionAnswering + - forward + +## BartForCausalLM + +[[autodoc]] BartForCausalLM + - forward + +## TFBartModel + +[[autodoc]] TFBartModel + - call + +## TFBartForConditionalGeneration + +[[autodoc]] TFBartForConditionalGeneration + - call + +## TFBartForSequenceClassification + +[[autodoc]] TFBartForSequenceClassification + - call + +## FlaxBartModel + +[[autodoc]] FlaxBartModel + - __call__ + - encode + - decode + +## FlaxBartForConditionalGeneration + +[[autodoc]] FlaxBartForConditionalGeneration + - __call__ + - encode + - decode + +## FlaxBartForSequenceClassification + +[[autodoc]] FlaxBartForSequenceClassification + - __call__ + - encode + - decode + +## FlaxBartForQuestionAnswering + +[[autodoc]] FlaxBartForQuestionAnswering + - __call__ + - encode + - decode + +## FlaxBartForCausalLM + +[[autodoc]] FlaxBartForCausalLM + - __call__ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/barthez.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/barthez.md new file mode 100644 index 0000000000000000000000000000000000000000..5668772c2636763f8c546f1c11846af4f2737cb2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/barthez.md @@ -0,0 +1,60 @@ + + +# BARThez + +## Overview + +BARThez ใƒขใƒ‡ใƒซใฏใ€Moussa Kamal Eddineใ€Antoine J.-P ใซใ‚ˆใฃใฆ [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใƒ†ใ‚ฃใ‚ฏใ‚ทใ‚จใ€ใƒŸใ‚ซใƒชใ‚นใƒปใƒดใ‚กใ‚ธใƒซใ‚ธใƒฃใƒณใƒ‹ใ‚นใ€10ๆœˆ23ๆ—ฅใ€ +2020ๅนดใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„: + + +*ๅธฐ็ด็š„่ปข็งปๅญฆ็ฟ’ใฏใ€่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใซใ‚ˆใฃใฆๅฏ่ƒฝใซใชใ‚Šใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ๅ…จไฝ“ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +(NLP) ๅˆ†้‡Žใฏใ€BERT ใ‚„ BART ใชใฉใฎใƒขใƒ‡ใƒซใซใ‚ˆใ‚Šใ€็„กๆ•ฐใฎ่‡ช็„ถ่จ€่ชžใซๆ–ฐใŸใชๆœ€ๅ…ˆ็ซฏๆŠ€่ก“ใ‚’็ขบ็ซ‹ใ—ใ€ๅตใ‚’ๅทปใ่ตทใ“ใ—ใฆใ„ใพใ™ใ€‚ +ใ‚ฟใ‚นใ‚ฏใ‚’็†่งฃใ™ใ‚‹ใ“ใจใ€‚ใ„ใใคใ‹ใฎๆณจ็›ฎใ™ในใไพ‹ๅค–ใฏใ‚ใ‚Šใพใ™ใŒใ€ๅˆฉ็”จๅฏ่ƒฝใชใƒขใƒ‡ใƒซใจ็ ”็ฉถใฎใปใจใ‚“ใฉใฏใ€ +่‹ฑ่ชžใ‚’ๅฏพ่ฑกใซๅฎŸๆ–ฝใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎไฝœๅ“ใงใฏใ€ใƒ•ใƒฉใƒณใ‚น่ชž็”จใฎๆœ€ๅˆใฎ BART ใƒขใƒ‡ใƒซใงใ‚ใ‚‹ BARTez ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +๏ผˆๆˆ‘ใ€…ใฎ็Ÿฅใ‚‹้™ใ‚Šใซ๏ผ‰ใ€‚ BARThez ใฏใ€้ŽๅŽปใฎ็ ”็ฉถใ‹ใ‚‰ๅพ—ใŸ้žๅธธใซๅคง่ฆๆจกใชๅ˜ไธ€่จ€่ชžใƒ•ใƒฉใƒณใ‚น่ชžใ‚ณใƒผใƒ‘ใ‚นใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸ +BART ใฎๆ‘‚ๅ‹•ใ‚นใ‚ญใƒผใƒ ใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ—ใพใ—ใŸใ€‚ๆ—ขๅญ˜ใฎ BERT ใƒ™ใƒผใ‚นใฎใƒ•ใƒฉใƒณใ‚น่ชžใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใ€ +CamemBERT ใจ FlauBERTใ€BARThez ใฏใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใ ใ‘ใงใชใใ€ +ใใฎใƒ‡ใ‚ณใƒผใƒ€ใฏไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ FLUE ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‹ใ‚‰ใฎ่ญ˜ๅˆฅใ‚ฟใ‚นใ‚ฏใซๅŠ ใˆใฆใ€BARThez ใ‚’ๆ–ฐใ—ใ„่ฉ•ไพกใซๅŸบใฅใ„ใฆ่ฉ•ไพกใ—ใพใ™ใ€‚ +ใ“ใฎ่ซ–ๆ–‡ใจใจใ‚‚ใซใƒชใƒชใƒผใ‚นใ™ใ‚‹่ฆ็ด„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€OrangeSumใ€‚ใพใŸใ€ใ™ใงใซ่กŒใ‚ใ‚Œใฆใ„ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚‚็ถ™็ถšใ—ใพใ™ใ€‚ +BARTHez ใฎใ‚ณใƒผใƒ‘ใ‚นไธŠใงๅคš่จ€่ชž BART ใ‚’ไบ‹ๅ‰่จ“็ทดใ—ใ€็ตๆžœใจใ—ใฆๅพ—ใ‚‰ใ‚Œใ‚‹ใƒขใƒ‡ใƒซ (mBARTHez ใจๅ‘ผใถ) ใŒๆฌกใฎใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒใƒ‹ใƒฉใฎ BARThez ใ‚’ๅคงๅน…ใซๅผทๅŒ–ใ—ใ€CamemBERT ใ‚„ FlauBERT ใจๅŒ็ญ‰ใ‹ใใ‚Œใ‚’ไธŠๅ›žใ‚Šใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [moussakam](https://huggingface.co/moussakam) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚่‘—่€…ใฎใ‚ณใƒผใƒ‰ใฏ[ใ“ใ“](https://github.com/moussaKam/BARThez)ใซใ‚ใ‚Šใพใ™ใ€‚ + + + +BARThez ใฎๅฎŸ่ฃ…ใฏใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’้™คใ„ใฆ BART ใจๅŒใ˜ใงใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[BART ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](bart) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ๆง‹ๆˆใ‚ฏใƒฉใ‚นใจใใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€‚ BARThez ๅ›บๆœ‰ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซใคใ„ใฆใฏไปฅไธ‹ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + + + +### Resources + +- BARThez ใฏใ€BART ใจๅŒๆง˜ใฎๆ–นๆณ•ใงใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใฎใ‚ฟใ‚นใ‚ฏใ‚’ๅพฎ่ชฟๆ•ดใงใใพใ™ใ€‚ไปฅไธ‹ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + [examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md)ใ€‚ + + +## BarthezTokenizer + +[[autodoc]] BarthezTokenizer + +## BarthezTokenizerFast + +[[autodoc]] BarthezTokenizerFast diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bartpho.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bartpho.md new file mode 100644 index 0000000000000000000000000000000000000000..3596a91fe1acc23f01887390746a3a948eaf4074 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bartpho.md @@ -0,0 +1,86 @@ + + +# BARTpho + +## Overview + +BARTpho ใƒขใƒ‡ใƒซใฏใ€Nguyen Luong Tranใ€Duong Minh Leใ€Dat Quoc Nguyen ใซใ‚ˆใฃใฆ [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnam](https://huggingface.co/papers/2109.09701) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*BARTpho ใซใฏใ€BARTpho_word ใจ BARTpho_syllable ใฎ 2 ใคใฎใƒใƒผใ‚ธใƒงใƒณใŒใ‚ใ‚Šใ€ๅˆใฎๅ…ฌ้–‹ใ•ใ‚ŒใŸๅคง่ฆๆจกใชๅ˜ไธ€่จ€่ชžใงใ™ใ€‚ +ใƒ™ใƒˆใƒŠใƒ ่ชž็”จใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‚ทใƒผใ‚ฑใƒณใ‚นใƒ„ใƒผใ‚ทใƒผใ‚ฑใƒณใ‚น ใƒขใƒ‡ใƒซใ€‚ๅฝ“็คพใฎ BARTpho ใฏใ€Œๅคง่ฆๆจกใชใ€ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใพใ™ +ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใƒŽใ‚คใ‚บ้™คๅŽปใƒขใƒ‡ใƒซ BART ใฎใ‚นใ‚ญใƒผใƒ ใชใฎใงใ€็”Ÿๆˆ NLP ใ‚ฟใ‚นใ‚ฏใซ็‰นใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ๅฎŸ้จ“ +ใƒ™ใƒˆใƒŠใƒ ่ชžใƒ†ใ‚ญใ‚นใƒˆ่ฆ็ด„ใฎไธ‹ๆตใ‚ฟใ‚นใ‚ฏใงใฏใ€่‡ชๅ‹•่ฉ•ไพกใจไบบ้–“ใซใ‚ˆใ‚‹่ฉ•ไพกใฎไธกๆ–นใงใ€BARTpho ใŒ +ๅผทๅŠ›ใชใƒ™ใƒผใ‚นใƒฉใ‚คใƒณ mBART ใ‚’ไธŠๅ›žใ‚Šใ€ๆœ€ๅ…ˆ็ซฏใฎๆ€ง่ƒฝใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ๅฐ†ๆฅใ‚’ๅฎนๆ˜“ใซใ™ใ‚‹ใŸใ‚ใซBARTphoใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ +็”Ÿๆˆ็š„ใชใƒ™ใƒˆใƒŠใƒ ่ชž NLP ใ‚ฟใ‚นใ‚ฏใฎ็ ”็ฉถใจๅฟœ็”จใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [dqnguyen](https://huggingface.co/dqnguyen) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/VinAIResearch/BARTpho) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage example + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bartpho = AutoModel.from_pretrained("vinai/bartpho-syllable") + +>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bartpho-syllable") + +>>> line = "Chรบng tรดi lร  nhแปฏng nghiรชn cแปฉu viรชn." + +>>> input_ids = tokenizer(line, return_tensors="pt") + +>>> with torch.no_grad(): +... features = bartpho(**input_ids) # Models outputs are now tuples + +>>> # With TensorFlow 2.0+: +>>> from transformers import TFAutoModel + +>>> bartpho = TFAutoModel.from_pretrained("vinai/bartpho-syllable") +>>> input_ids = tokenizer(line, return_tensors="tf") +>>> features = bartpho(**input_ids) +``` + +## Usage tips + +- mBARTใซ็ถšใ„ใฆใ€BARTphoใฏBARTใฎใ€Œๅคง่ฆๆจกใชใ€ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใ€ใใฎไธŠใซ่ฟฝๅŠ ใฎๅฑคๆญฃ่ฆๅŒ–ๅฑคใ‚’ๅ‚™ใˆใฆใ„ใพใ™ใ€‚ + ใ‚จใƒณใ‚ณใƒผใƒ€ใจใƒ‡ใ‚ณใƒผใƒ€ใฎไธกๆ–นใ€‚ใ—ใŸใŒใฃใฆใ€[BART ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](bart) ใฎไฝฟ็”จไพ‹ใฏใ€ไฝฟ็”จใซ้ฉๅฟœใ™ใ‚‹ๅ ดๅˆใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + BARTpho ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€BART ใซ็‰นๅŒ–ใ—ใŸใ‚ฏใƒฉใ‚นใ‚’ mBART ใซ็‰นๅŒ–ใ—ใŸๅฏพๅฟœใ™ใ‚‹ใ‚ฏใƒฉใ‚นใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใซใ‚ˆใฃใฆ่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + ไพ‹ใˆใฐ๏ผš + +```python +>>> from transformers import MBartForConditionalGeneration + +>>> bartpho = MBartForConditionalGeneration.from_pretrained("vinai/bartpho-syllable") +>>> TXT = "Chรบng tรดi lร  nghiรชn cแปฉu viรชn." +>>> input_ids = tokenizer([TXT], return_tensors="pt")["input_ids"] +>>> logits = bartpho(input_ids).logits +>>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item() +>>> probs = logits[0, masked_index].softmax(dim=0) +>>> values, predictions = probs.topk(5) +>>> print(tokenizer.decode(predictions).split()) +``` + +- ใ“ใฎๅฎŸ่ฃ…ใฏใƒˆใƒผใ‚ฏใƒณๅŒ–ใฎใฟใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚`monolingual_vocab_file`ใฏใƒ™ใƒˆใƒŠใƒ ่ชžใซ็‰นๅŒ–ใ—ใŸๅž‹ใงๆง‹ๆˆใ•ใ‚Œใฆใ„ใพใ™ + ๅคš่จ€่ชž XLM-RoBERTa ใ‹ใ‚‰ๅˆฉ็”จใงใใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ SentencePiece ใƒขใƒ‡ใƒซ`vocab_file`ใ‹ใ‚‰ๆŠฝๅ‡บใ•ใ‚Œใพใ™ใ€‚ + ไป–ใฎ่จ€่ชž (ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซใ“ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟๅคš่จ€่ชž SentencePiece ใƒขใƒ‡ใƒซ`vocab_file`ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ) + ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใซใ‚ˆใ‚Šใ€็‹ฌ่‡ชใฎ่จ€่ชžใซ็‰นๅŒ–ใ—ใŸ`monolingual_vocab_file`ใ‚’ไฝฟ็”จใ—ใฆ BartphoTokenizer ใ‚’ๅ†ๅˆฉ็”จใงใใพใ™ใ€‚ + +## BartphoTokenizer + +[[autodoc]] BartphoTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/beit.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/beit.md new file mode 100644 index 0000000000000000000000000000000000000000..21ccc28c68e298288cdbf4b1f7860ac0cb75c23f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/beit.md @@ -0,0 +1,149 @@ + + +# BEiT + +## Overview + +BEiT ใƒขใƒ‡ใƒซใฏใ€[BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใƒใƒณใƒœใƒปใƒใ‚ชใ€ใƒชใƒผใƒปใƒ‰ใƒณใ€ใƒ•ใƒซใƒปใ‚ฆใ‚งใ‚คใ€‚ BERT ใซ่งฆ็™บใ•ใ‚ŒใŸ BEiT ใฏใ€่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ไฝœๆˆใ—ใŸๆœ€ๅˆใฎ่ซ–ๆ–‡ใงใ™ใ€‚ +ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (ViT) ใฏใ€ๆ•™ๅธซไป˜ใไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ใ‚ฏใƒฉใ‚นใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซใƒขใƒ‡ใƒซใ‚’ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใ +([ใ‚ชใƒชใ‚ธใƒŠใƒซใฎ ViT ่ซ–ๆ–‡](https://huggingface.co/papers/2010.11929) ใง่กŒใ‚ใ‚ŒใŸใ‚ˆใ†ใซ) ็”ปๅƒใฎ BEiT ใƒขใƒ‡ใƒซใฏใ€ๆฌกใฎใ‚ˆใ†ใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ OpenAI ใฎ [DALL-E ใƒขใƒ‡ใƒซ](https://huggingface.co/papers/2102.12092) ใฎใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏใ‹ใ‚‰ใƒ“ใ‚ธใƒฅใ‚ขใƒซ ใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใพใ™ +ใƒ‘ใƒƒใƒใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Š่ฆ–่ฆš่กจ็พใƒขใƒ‡ใƒซ BEiT (Bidirectional Encoderpresentation) ใ‚’ๅฐŽๅ…ฅใ—ใพใ™ใ€‚ +ใ‚คใƒกใƒผใ‚ธใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚ˆใ‚Šใ€‚่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ๅˆ†้‡Žใง้–‹็™บใ•ใ‚ŒใŸBERTใซๅ€ฃใ„ใ€ใƒžใ‚นใ‚ฏ็”ปๅƒใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +ใƒ“ใ‚ธใƒงใƒณใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚ฟใ‚นใ‚ฏใ€‚ๅ…ทไฝ“็š„ใซใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใฏๅ„็”ปๅƒใซ 2 ใคใฎใƒ“ใƒฅใƒผใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‘ใƒƒใƒ (16x16 ใƒ”ใ‚ฏใ‚ปใƒซใชใฉ)ใ€ใŠใ‚ˆใณใƒ“ใ‚ธใƒฅใ‚ขใƒซ ใƒˆใƒผใ‚ฏใƒณ (ใคใพใ‚Šใ€ๅ€‹ๅˆฅใฎใƒˆใƒผใ‚ฏใƒณ)ใ€‚ใพใšใ€ๅ…ƒใฎ็”ปๅƒใ‚’ใ€Œใƒˆใƒผใ‚ฏใƒณๅŒ–ใ€ใ—ใฆใ€ +ใƒ“ใ‚ธใƒฅใ‚ขใƒซใƒˆใƒผใ‚ฏใƒณใ€‚ๆฌกใซใ€ใ„ใใคใ‹ใฎ็”ปๅƒใƒ‘ใƒƒใƒใ‚’ใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒใƒƒใ‚ฏใƒœใƒผใƒณใฎ Transformer ใซไพ›็ตฆใ—ใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ +็›ฎ็š„ใฏใ€็ ดๆใ—ใŸใ‚คใƒกใƒผใ‚ธ ใƒ‘ใƒƒใƒใซๅŸบใฅใ„ใฆๅ…ƒใฎใƒ“ใ‚ธใƒฅใ‚ขใƒซ ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ›žๅพฉใ™ใ‚‹ใ“ใจใงใ™ใ€‚ BEiTใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใ€ +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซใ‚ฟใ‚นใ‚ฏ ใƒฌใ‚คใƒคใƒผใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใ€ใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ  ใ‚ฟใ‚นใ‚ฏใฎใƒขใƒ‡ใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’็›ดๆŽฅๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +็”ปๅƒๅˆ†้กžใจใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใซ้–ขใ™ใ‚‹ๅฎŸ้จ“็ตๆžœใฏใ€็งใŸใกใฎใƒขใƒ‡ใƒซใŒ็ซถไบ‰ๅŠ›ใฎใ‚ใ‚‹็ตๆžœใ‚’้”ๆˆใ™ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ +ไปฅๅ‰ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใ‚’ไฝฟ็”จใ—ใฆใ€‚ใŸใจใˆใฐใ€ๅŸบๆœฌใ‚ตใ‚คใ‚บใฎ BEiT ใฏใ€ImageNet-1K ใง 83.2% ใฎใƒˆใƒƒใƒ— 1 ็ฒพๅบฆใ‚’้”ๆˆใ—ใพใ™ใ€‚ +ๅŒใ˜่จญๅฎšใงใ‚ผใƒญใ‹ใ‚‰ใฎ DeiT ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ (81.8%) ใ‚’ๅคงๅน…ใซไธŠๅ›žใ‚Šใพใ—ใŸใ€‚ใพใŸใ€ๅคงๅž‹BEiTใฏ +86.3% ใฏ ImageNet-1K ใฎใฟใ‚’ไฝฟ็”จใ—ใฆใŠใ‚Šใ€ImageNet-22K ใงใฎๆ•™ๅธซไป˜ใไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใŸ ViT-L (85.2%) ใ‚’ไธŠๅ›žใฃใฆใ„ใพใ™ใ€‚* + +## Usage tips + +- BEiT ใƒขใƒ‡ใƒซใฏ้€šๅธธใฎใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ™ใŒใ€ๆ•™ๅธซใ‚ใ‚Šใงใฏใชใ่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šใฎๆ–นๆณ•ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ๅฝผใ‚‰ใฏ + ImageNet-1K ใŠใ‚ˆใณ CIFAR-100 ใงๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใ€[ใ‚ชใƒชใ‚ธใƒŠใƒซ ใƒขใƒ‡ใƒซ (ViT)](vit) ใจ [ใƒ‡ใƒผใ‚ฟๅŠน็އใฎ้ซ˜ใ„ใ‚คใƒกใƒผใ‚ธ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (DeiT)](deit) ใฎไธกๆ–นใ‚’ไธŠๅ›žใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ๆŽจ่ซ–ใซ้–ขใ™ใ‚‹ใƒ‡ใƒขใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚‚ใƒใ‚งใƒƒใ‚ฏใงใใพใ™ใ€‚ + ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใฎๅพฎ่ชฟๆ•ดใฏ [ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/VisionTransformer) (็ฝฎใๆ›ใˆใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™) + [`BeitImageProcessor`] ใซใ‚ˆใ‚‹ [`ViTFeatureExtractor`] ใจ + [`ViTForImageClassification`] by [`BeitForImageClassification`])ใ€‚ +- DALL-E ใฎ็”ปๅƒใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใจ BEiT ใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ๆ–นๆณ•ใ‚’็ดนไป‹ใ™ใ‚‹ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚‚ๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ็”ปๅƒใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ [ใ“ใ“](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/BEiT) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- BEiT ใƒขใƒ‡ใƒซใฏๅ„็”ปๅƒใŒๅŒใ˜ใ‚ตใ‚คใ‚บ (่งฃๅƒๅบฆ) ใงใ‚ใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ๆฌกใฎใ‚ˆใ†ใซไฝฟ็”จใงใใพใ™ใ€‚ + [`BeitImageProcessor`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บใ‚’ๅค‰ๆ›ด (ใพใŸใฏๅ†ใ‚นใ‚ฑใƒผใƒซ) ใ—ใ€ๆญฃ่ฆๅŒ–ใ—ใพใ™ใ€‚ +- ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใพใŸใฏๅพฎ่ชฟๆ•ดไธญใซไฝฟ็”จใ•ใ‚Œใ‚‹ใƒ‘ใƒƒใƒ่งฃๅƒๅบฆใจ็”ปๅƒ่งฃๅƒๅบฆใฎไธกๆ–นใŒๅๅ‰ใซๅๆ˜ ใ•ใ‚Œใพใ™ใ€‚ + ๅ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ€‚ใŸใจใˆใฐใ€`microsoft/beit-base-patch16-224`ใฏใ€ใƒ‘ใƒƒใƒไป˜ใใฎๅŸบๆœฌใ‚ตใ‚คใ‚บใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ + ่งฃๅƒๅบฆใฏ 16x16ใ€ๅพฎ่ชฟๆ•ด่งฃๅƒๅบฆใฏ 224x224 ใงใ™ใ€‚ใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ [ใƒใƒ–](https://huggingface.co/models?search=microsoft/beit) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- ๅˆฉ็”จๅฏ่ƒฝใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€(1) [ImageNet-22k](http://www.image-net.org/) ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ ( + 1,400 ไธ‡ใฎ็”ปๅƒใจ 22,000 ใฎใ‚ฏใƒฉใ‚น) ใฎใฟใ€(2) ImageNet-22k ใงใ‚‚ๅพฎ่ชฟๆ•ดใ€ใพใŸใฏ (3) [ImageNet-1k](http://www.image-net.org/challenges/LSVRC)ใงใ‚‚ๅพฎ่ชฟๆ•ด/2012/) (ILSVRC 2012 ใจใ‚‚ๅ‘ผใฐใ‚Œใ€130 ไธ‡ไปถใฎใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณ) + ็”ปๅƒใจ 1,000 ใ‚ฏใƒฉใ‚น)ใ€‚ +- BEiT ใฏใ€T5 ใƒขใƒ‡ใƒซใ‹ใ‚‰ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใŸ็›ธๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ€่‘—่€…ใฏๆฌกใฎใ“ใจใ‚’ๅ…ฑๆœ‰ใ—ใพใ—ใŸใ€‚ + ใ„ใใคใ‹ใฎ่‡ชๅทฑๆณจๆ„ๅฑค้–“ใฎ็›ธๅฏพ็š„ใชไฝ็ฝฎใฎๅใ‚Šใ€‚ๅพฎ่ชฟๆ•ดไธญใ€ๅ„ใƒฌใ‚คใƒคใƒผใฎ็›ธๅฏพไฝ็ฝฎ + ใƒใ‚คใ‚ขใ‚นใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใซๅ–ๅพ—ใ•ใ‚ŒใŸๅ…ฑๆœ‰็›ธๅฏพไฝ็ฝฎใƒใ‚คใ‚ขใ‚นใงๅˆๆœŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚ใ”ๅธŒๆœ›ใฎๅ ดๅˆใฏใ€ + ใƒขใƒ‡ใƒซใ‚’ๆœ€ๅˆใ‹ใ‚‰ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใซใฏใ€`use_relative_position_bias` ใพใŸใฏ + ่ฟฝๅŠ ใ™ใ‚‹ใซใฏใ€[`BeitConfig`] ใฎ `use_relative_position_bias` ๅฑžๆ€งใ‚’ `True` ใซ่จญๅฎšใ—ใพใ™ใ€‚ + ไฝ็ฝฎใฎๅŸ‹ใ‚่พผใฟใ€‚ + + + + BEiT ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎ JAX/FLAX ใƒใƒผใ‚ธใƒงใƒณใฏใ€ +[kamalkraj](https://huggingface.co/kamalkraj) ใซใ‚ˆใ‚‹ๆŠ•็จฟใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/unilm/tree/master/beit) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +BEiT ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`BeitForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +**ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ** +- [ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/semantic_segmentation) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## BEiT specific outputs + +[[autodoc]] models.beit.modeling_beit.BeitModelOutputWithPooling + +[[autodoc]] models.beit.modeling_flax_beit.FlaxBeitModelOutputWithPooling + +## BeitConfig + +[[autodoc]] BeitConfig + +## BeitFeatureExtractor + +[[autodoc]] BeitFeatureExtractor + - __call__ + - post_process_semantic_segmentation + +## BeitImageProcessor + +[[autodoc]] BeitImageProcessor + - preprocess + - post_process_semantic_segmentation + +## BeitImageProcessorFast + +[[autodoc]] BeitImageProcessorFast + - preprocess + - post_process_semantic_segmentation + +## BeitModel + +[[autodoc]] BeitModel + - forward + +## BeitForMaskedImageModeling + +[[autodoc]] BeitForMaskedImageModeling + - forward + +## BeitForImageClassification + +[[autodoc]] BeitForImageClassification + - forward + +## BeitForSemanticSegmentation + +[[autodoc]] BeitForSemanticSegmentation + - forward + +## FlaxBeitModel + +[[autodoc]] FlaxBeitModel + - __call__ + +## FlaxBeitForMaskedImageModeling + +[[autodoc]] FlaxBeitForMaskedImageModeling + - __call__ + +## FlaxBeitForImageClassification + +[[autodoc]] FlaxBeitForImageClassification + - __call__ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-generation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-generation.md new file mode 100644 index 0000000000000000000000000000000000000000..bf3b93296851e16f051887bf3f7fdf546d2f8844 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-generation.md @@ -0,0 +1,107 @@ + + +# BertGeneration + +## Overview + +BertGeneration ใƒขใƒ‡ใƒซใฏใ€ๆฌกใ‚’ไฝฟ็”จใ—ใฆใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใฎใ‚ฟใ‚นใ‚ฏใซๅˆฉ็”จใงใใ‚‹ BERT ใƒขใƒ‡ใƒซใงใ™ใ€‚ +[Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) ใงๆๆกˆใ•ใ‚Œใฆใ„ใ‚‹ [`EncoderDecoderModel`] +ใ‚ฟใ‚นใ‚ฏใ€Sascha Rotheใ€Sishi Nagayanใ€Aliaksei Severyn ่‘—ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๅคง่ฆๆจกใชใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒขใƒ‡ใƒซใฎๆ•™ๅธซใชใ—ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ๆœ€่ฟ‘ใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใซ้ฉๅ‘ฝใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ—ใŸใ€‚ใซใ‚ˆใ‚‹ +NLP ๅฎŸ่ทต่€…ใฏใ€ๅ…ฌ้–‹ใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใ‚ฆใ‚ฉใƒผใƒ ใ‚นใ‚ฟใƒผใƒˆใ—ใฆใ€่ค‡ๆ•ฐใฎ้ …็›ฎใงๆœ€ๅ…ˆ็ซฏใฎๆŠ€่ก“ใ‚’ๆŽจ้€ฒใ—ใฆใใพใ—ใŸใ€‚ +ใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐๆ™‚้–“ใ‚’ๅคงๅน…ใซ็ฏ€็ด„ใ—ใชใŒใ‚‰ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใ‚Œใพใงใฎใจใ“ใ‚ใ€ไธปใซ่‡ช็„ถ่จ€่ชžใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใใพใ—ใŸใ€‚ +ใ‚ฟใ‚นใ‚ฏใ‚’็†่งฃใ™ใ‚‹ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น็”ŸๆˆใฎใŸใ‚ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎๆœ‰ๅŠนๆ€งใ‚’ๅฎŸ่จผใ—ใพใ™ใ€‚็งใŸใกใฏ +ๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ BERT ใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ Transformer ใƒ™ใƒผใ‚นใฎใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใƒขใƒ‡ใƒซใ‚’้–‹็™บใ—ใพใ—ใŸใ€‚ +GPT-2 ใŠใ‚ˆใณ RoBERTa ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใ€ใƒขใƒ‡ใƒซใฎๅˆๆœŸๅŒ–ใฎๆœ‰็”จๆ€งใซใคใ„ใฆๅบƒ็ฏ„ใชๅฎŸ่จผ็ ”็ฉถใ‚’ๅฎŸๆ–ฝใ—ใพใ—ใŸใ€‚ +ใ‚จใƒณใ‚ณใƒผใƒ€ใจใƒ‡ใ‚ณใƒผใƒ€ใ€ใ“ใ‚Œใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ€‚็งใŸใกใฎใƒขใƒ‡ใƒซใฏใ€ๆฉŸๆขฐ็ฟป่จณใซ้–ขใ™ใ‚‹ๆ–ฐใ—ใ„ๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆใฎ่ฆ็ด„ใ€ๆ–‡ใฎๅˆ†ๅ‰ฒใ€ใŠใ‚ˆใณๆ–‡ใฎ่žๅˆใ€‚* + +## Usage examples and tips + +- ใƒขใƒ‡ใƒซใ‚’ [`EncoderDecoderModel`] ใจ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใ—ใฆใ€2 ใคใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆดป็”จใงใใพใ™ใ€‚ + ๅพŒ็ถšใฎๅพฎ่ชฟๆ•ดใฎใŸใ‚ใฎ BERT ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ€‚ + +```python +>>> # leverage checkpoints for Bert2Bert model... +>>> # use BERT's cls token as BOS token and sep token as EOS token +>>> encoder = BertGenerationEncoder.from_pretrained("google-bert/bert-large-uncased", bos_token_id=101, eos_token_id=102) +>>> # add cross attention layers and use BERT's cls token as BOS token and sep token as EOS token +>>> decoder = BertGenerationDecoder.from_pretrained( +... "google-bert/bert-large-uncased", add_cross_attention=True, is_decoder=True, bos_token_id=101, eos_token_id=102 +... ) +>>> bert2bert = EncoderDecoderModel(encoder=encoder, decoder=decoder) + +>>> # create tokenizer... +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-large-uncased") + +>>> input_ids = tokenizer( +... "This is a long article to summarize", add_special_tokens=False, return_tensors="pt" +... ).input_ids +>>> labels = tokenizer("This is a short summary", return_tensors="pt").input_ids + +>>> # train... +>>> loss = bert2bert(input_ids=input_ids, decoder_input_ids=labels, labels=labels).loss +>>> loss.backward() +``` + +- ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ [`EncoderDecoderModel`] ใ‚‚ใƒขใƒ‡ใƒซ ใƒใƒ–ใง็›ดๆŽฅๅˆฉ็”จใงใใพใ™ใ€‚ + +```python +>>> # instantiate sentence fusion model +>>> sentence_fuser = EncoderDecoderModel.from_pretrained("google/roberta2roberta_L-24_discofuse") +>>> tokenizer = AutoTokenizer.from_pretrained("google/roberta2roberta_L-24_discofuse") + +>>> input_ids = tokenizer( +... "This is the first sentence. This is the second sentence.", add_special_tokens=False, return_tensors="pt" +... ).input_ids + +>>> outputs = sentence_fuser.generate(input_ids) + +>>> print(tokenizer.decode(outputs[0])) +``` + +ใƒใƒƒใƒ—๏ผš + +- [`BertGenerationEncoder`] ใจ [`BertGenerationDecoder`] ใฏใ€ + [`EncoderDecoder`] ใจ็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ +- ่ฆ็ด„ใ€ๆ–‡ใฎๅˆ†ๅ‰ฒใ€ๆ–‡ใฎ่žๅˆใ€ใŠใ‚ˆใณ็ฟป่จณใฎๅ ดๅˆใ€ๅ…ฅๅŠ›ใซ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + ใ—ใŸใŒใฃใฆใ€ๅ…ฅๅŠ›ใฎๆœซๅฐพใซ EOS ใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[patrickvonplaten](https://huggingface.co/patrickvonplaten) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏๆฌกใฎใจใŠใ‚Šใงใ™ +[ใ“ใ“](https://tfhub.dev/s?module-type=text-generation&subtype=module,placeholder) ใŒใ‚ใ‚Šใพใ™ใ€‚ + +## BertGenerationConfig + +[[autodoc]] BertGenerationConfig + +## BertGenerationTokenizer + +[[autodoc]] BertGenerationTokenizer + - save_vocabulary + +## BertGenerationEncoder + +[[autodoc]] BertGenerationEncoder + - forward + +## BertGenerationDecoder + +[[autodoc]] BertGenerationDecoder + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-japanese.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-japanese.md new file mode 100644 index 0000000000000000000000000000000000000000..86cce741aac6cb559efefabd936646a39f9b5854 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert-japanese.md @@ -0,0 +1,81 @@ + + +# BertJapanese + +## Overview + +BERT ใƒขใƒ‡ใƒซใฏๆ—ฅๆœฌ่ชžใƒ†ใ‚ญใ‚นใƒˆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ + +2 ใคใฎ็•ฐใชใ‚‹ใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ–นๆณ•ใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใŒใ‚ใ‚Šใพใ™ใ€‚ + +- MeCab ใจ WordPiece ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€[MeCab](https://taku910.github.io/mecab/) ใฎใƒฉใƒƒใƒ‘ใƒผใงใ‚ใ‚‹ [fugashi](https://github.com/polm/fugashi) ใจใ„ใ†่ฟฝๅŠ ใฎไพๅญ˜้–ขไฟ‚ใŒๅฟ…่ฆใงใ™ใ€‚ +- ๆ–‡ๅญ—ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +*MecabTokenizer* ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€`pip installTransformers["ja"]` (ใพใŸใฏใ€ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅ ดๅˆใฏ `pip install -e .["ja"]`) ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ฝใƒผใ‚นใ‹ใ‚‰๏ผ‰ไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ + +[cl-tohakuใƒชใƒใ‚ธใƒˆใƒชใฎ่ฉณ็ดฐ](https://github.com/cl-tohaku/bert-japanese)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +MeCab ใŠใ‚ˆใณ WordPiece ใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ไพ‹: + + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese") +>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese") + +>>> ## Input Japanese Text +>>> line = "ๅพ่ผฉใฏ็Œซใงใ‚ใ‚‹ใ€‚" + +>>> inputs = tokenizer(line, return_tensors="pt") + +>>> print(tokenizer.decode(inputs["input_ids"][0])) +[CLS] ๅพ่ผฉ ใฏ ็Œซ ใง ใ‚ใ‚‹ ใ€‚ [SEP] + +>>> outputs = bertjapanese(**inputs) +``` + +ๆ–‡ๅญ—ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎไฝฟ็”จไพ‹: + +```python +>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese-char") +>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-char") + +>>> ## Input Japanese Text +>>> line = "ๅพ่ผฉใฏ็Œซใงใ‚ใ‚‹ใ€‚" + +>>> inputs = tokenizer(line, return_tensors="pt") + +>>> print(tokenizer.decode(inputs["input_ids"][0])) +[CLS] ๅพ ่ผฉ ใฏ ็Œซ ใง ใ‚ ใ‚‹ ใ€‚ [SEP] + +>>> outputs = bertjapanese(**inputs) +``` + + + +- ใ“ใฎๅฎŸ่ฃ…ใฏใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ–นๆณ•ใ‚’้™คใ„ใฆ BERT ใจๅŒใ˜ใงใ™ใ€‚ใใฎไป–ใฎไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€[BERT ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](bert) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ[cl-tohaku](https://huggingface.co/cl-tohaku)ใ‹ใ‚‰ๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ + +## BertJapaneseTokenizer + +[[autodoc]] BertJapaneseTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert.md new file mode 100644 index 0000000000000000000000000000000000000000..e0367dcd46ebb28e9e2de72baf80b97c0f8395a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bert.md @@ -0,0 +1,312 @@ + + +# BERT + +
+ +Models + + +Spaces + +
+ +## Overview + +BERT ใƒขใƒ‡ใƒซใฏใ€Jacob Devlinใ€Ming-Wei Changใ€Kenton Leeใ€Kristina Toutanova ใซใ‚ˆใฃใฆ [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใใ‚Œใฏ +ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็›ฎๆจ™ใจๆฌกใฎๆ–‡ใฎ็ต„ใฟๅˆใ‚ใ›ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅŒๆ–นๅ‘ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ +Toronto Book Corpus ใจ Wikipedia ใ‹ใ‚‰ใชใ‚‹ๅคง่ฆๆจกใชใ‚ณใƒผใƒ‘ใ‚นใงใฎไบˆๆธฌใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*BERT ใจๅ‘ผใฐใ‚Œใ‚‹ๆ–ฐใ—ใ„่จ€่ชž่กจ็พใƒขใƒ‡ใƒซใ‚’ๅฐŽๅ…ฅใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ Bidirectional Encoder Representations ใฎ็•ฅใงใ™ +ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚ˆใ‚Šใ€‚ๆœ€่ฟ‘ใฎ่จ€่ชž่กจ็พใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใ€BERT ใฏๆทฑใ„ๅŒๆ–นๅ‘ๆ€งใ‚’ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ‚ˆใ†ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใ™ในใฆใฎใƒฌใ‚คใƒคใƒผใฎๅทฆใจๅณใฎไธกๆ–นใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฑๅŒใงๆกไปถไป˜ใ‘ใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ใƒฉใƒ™ใƒซใฎใชใ„ใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰่กจ็พใ—ใพใ™ใ€‚็ตๆžœใจใ—ใฆใ€ +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ BERT ใƒขใƒ‡ใƒซใฏใ€ๅ‡บๅŠ›ๅฑคใ‚’ 1 ใค่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๅพฎ่ชฟๆ•ดใ—ใฆใ€ๆœ€ๅ…ˆ็ซฏใฎใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใงใใพใ™ใ€‚ +ๅฎŸ่ณช็š„ใชใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใ‚‚ใฎใ‚’ๅฟ…่ฆใจใ›ใšใ€่ณชๅ•ๅฟœ็ญ”ใ‚„่จ€่ชžๆŽจ่ซ–ใชใฉใฎๅน…ๅบƒใ„ใ‚ฟใ‚นใ‚ฏใซๅฏพๅฟœ +ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎๅค‰ๆ›ดใ€‚* + +*BERT ใฏๆฆ‚ๅฟต็š„ใซใฏใ‚ทใƒณใƒ—ใƒซใงใ™ใŒใ€็ตŒ้จ“็š„ใซๅผทๅŠ›ใงใ™ใ€‚ 11 ใฎ่‡ช็„ถใช่ฆ็ด ใซ้–ขใ™ใ‚‹ๆ–ฐใ—ใ„ๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ +่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏ๏ผˆGLUE ใ‚นใ‚ณใ‚ขใ‚’ 80.5% ใซๆŠผใ—ไธŠใ’ใ‚‹๏ผˆ7.7% ใƒใ‚คใƒณใƒˆใฎ็ตถๅฏพๆ”นๅ–„๏ผ‰ใ€MultiNLI ใ‚’ๅซใ‚€๏ผ‰ +็ฒพๅบฆใฏ 86.7% (็ตถๅฏพๅ€ค 4.6% ๅ‘ไธŠ)ใ€SQuAD v1.1 ่ณชๅ•ๅฟœ็ญ”ใƒ†ใ‚นใƒˆ F1 ใฏ 93.2 (็ตถๅฏพๅ€ค 1.5 ใƒใ‚คใƒณใƒˆ) +ๆ”นๅ–„) ใŠใ‚ˆใณ SQuAD v2.0 ใƒ†ใ‚นใƒˆ F1 ใ‹ใ‚‰ 83.1 (5.1 ใƒใ‚คใƒณใƒˆใฎ็ตถๅฏพๆ”นๅ–„)ใ€‚* + +## Usage tips + +- BERT ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ +- BERT ใฏใ€ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ (MLM) ใŠใ‚ˆใณๆฌกใฎๆ–‡ไบˆๆธฌ (NSP) ใฎ็›ฎๆจ™ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ใใ‚Œใฏ + ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใฎไบˆๆธฌใ‚„ NLU ใงใฏไธ€่ˆฌใซๅŠน็އ็š„ใงใ™ใŒใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซใฏๆœ€้ฉใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +- ใƒฉใƒณใƒ€ใƒ  ใƒžใ‚นใ‚ญใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆๅ…ฅๅŠ›ใ‚’็ ดๅฃŠใ—ใพใ™ใ€‚ใ‚ˆใ‚Šๆญฃ็ขบใซใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ€ใƒˆใƒผใ‚ฏใƒณใฎๆŒ‡ๅฎšใ•ใ‚ŒใŸๅ‰ฒๅˆ (้€šๅธธใฏ 15%) ใŒๆฌกใซใ‚ˆใฃใฆใƒžใ‚นใ‚ฏใ•ใ‚Œใพใ™ใ€‚ + + * ็ขบ็އ0.8ใฎ็‰นๅˆฅใชใƒžใ‚นใ‚ฏใƒˆใƒผใ‚ฏใƒณ + * ็ขบ็އ 0.1 ใงใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจใฏ็•ฐใชใ‚‹ใƒฉใƒณใƒ€ใƒ ใชใƒˆใƒผใ‚ฏใƒณ + * ็ขบ็އ 0.1 ใฎๅŒใ˜ใƒˆใƒผใ‚ฏใƒณ + +- ใƒขใƒ‡ใƒซใฏๅ…ƒใฎๆ–‡ใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€2 ็•ช็›ฎใฎ็›ฎ็š„ใŒใ‚ใ‚Šใพใ™ใ€‚ๅ…ฅๅŠ›ใฏ 2 ใคใฎๆ–‡ A ใจ B (้–“ใซๅˆ†้›ขใƒˆใƒผใ‚ฏใƒณใ‚ใ‚Š) ใงใ™ใ€‚็ขบ็އ 50% ใงใฏใ€ๆ–‡ใฏใ‚ณใƒผใƒ‘ใ‚นๅ†…ใง้€ฃ็ถšใ—ใฆใ„ใพใ™ใŒใ€ๆฎ‹ใ‚Šใฎ 50% ใงใฏ้–ข้€ฃๆ€งใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒขใƒ‡ใƒซใฏใ€ๆ–‡ใŒ้€ฃ็ถšใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [thomwolf](https://huggingface.co/thomwolf) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/google-research/bert) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +BERT ใ‚’ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + + + +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [ๅˆฅใฎ่จ€่ชžใงใฎ BERT ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กž](https://www.philschmid.de/bert-text-classification-in-a-different-language)ใ€‚ +- [ใƒžใƒซใƒใƒฉใƒ™ใƒซ ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใฎใŸใ‚ใฎ BERT (ใŠใ‚ˆใณใใฎๅ‹ไบบ) ใฎๅพฎ่ชฟๆ•ด](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/BERT/Fine_tuning_BERT_(and_friends)_for_multi_label_text_classification.ipynb) ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ. +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ [PyTorch ใ‚’ไฝฟ็”จใ—ใŸใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžใฎใŸใ‚ใฎ BERT ใฎๅพฎ่ชฟๆ•ด](https://colab.research.google.com/github/abhmishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)ใ€‚ +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ [่ฆ็ด„ใฎใŸใ‚ใซ BERT ใ‚’ไฝฟ็”จใ—ใฆ EncoderDecoder ใƒขใƒ‡ใƒซใ‚’ใ‚ฆใ‚ฉใƒผใƒ ใ‚นใ‚ฟใƒผใƒˆใ™ใ‚‹](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)ใ€‚ +- [`BertForSequenceClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)ใ€‚ +- [`TFBertForSequenceClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)ใ€‚ +- [`FlaxBertForSequenceClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb)ใ€‚ +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) + + + +- [Hugging Face Transformers with Keras: Fine-tune a non-English BERT for Named Entity Recognition](https://www.philschmid.de/huggingface-transformers-keras-tf) ใฎไฝฟ็”จๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ€‚ +- ๅ„ๅ˜่ชžใฎๆœ€ๅˆใฎๅ˜่ชž้ƒจๅˆ†ใฎใฟใ‚’ไฝฟ็”จใ—ใŸ [ๅ›บๆœ‰่กจ็พ่ช่ญ˜ใฎใŸใ‚ใฎ BERT ใฎๅพฎ่ชฟๆ•ด](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/Custom_Named_Entity_Recognition_with_BERT_only_first_wordpiece.ipynb) ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใƒˆใƒผใ‚ฏใƒณๅŒ–ไธญใฎๅ˜่ชžใƒฉใƒ™ใƒซๅ†…ใ€‚ๅ˜่ชžใฎใƒฉใƒ™ใƒซใ‚’ใ™ในใฆใฎๅ˜่ชž้ƒจๅˆ†ใซไผๆ’ญใ™ใ‚‹ใซใฏใ€ไปฃใ‚ใ‚ŠใซใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฎใ“ใฎ [ใƒใƒผใ‚ธใƒงใƒณ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/BERT/Custom_Named_Entity_Recognition_with_BERT.ipynb) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- [`BertForTokenClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb)ใ€‚ +- [`TFBertForTokenClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)ใ€‚ +- [`FlaxBertForTokenClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification) ใซใ‚ˆใฃใฆใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กž](https://huggingface.co/course/chapter7/2?fw=pt) ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) + + + +- [`BertForMaskedLM`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)ใ€‚ +- [`TFBertForMaskedLM`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/lang-modeling#run_mlmpy) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)ใ€‚ +- [`FlaxBertForMaskedLM`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ]( https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)ใ€‚ +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](https://huggingface.co/course/chapter7/3?fw=pt) ๐Ÿค— ้ก”ใƒใ‚ฐ ใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_lang_modeling) + + + + +- [`BertForQuestionAnswering`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)ใ€‚ +- [`TFBertForQuestionAnswering`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)ใ€‚ +- [`FlaxBertForQuestionAnswering`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- [่ณชๅ•ๅ›ž็ญ”](https://huggingface.co/course/chapter7/7?fw=pt) ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) + +**่ค‡ๆ•ฐใฎ้ธๆŠž่‚ข** +- [`BertForMultipleChoice`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb)ใ€‚ +- [`TFBertForMultipleChoice`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)ใ€‚ +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +โšก๏ธ **ๆŽจ่ซ–** +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [Hugging Face Transformers ใจ AWS Inferentia ใ‚’ไฝฟ็”จใ—ใฆ BERT ๆŽจ่ซ–ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹](https://huggingface.co/blog/bert-inferentia-sagemaker)ใ€‚ +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [GPU ไธŠใฎ DeepSpeed-Inference ใ‚’ไฝฟ็”จใ—ใฆ BERT ๆŽจ่ซ–ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹](https://www.philschmid.de/bert-deepspeed-inference)ใ€‚ + +โš™๏ธ **ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ** +- [Hugging Face Transformers ใจ Habana Gaudi ใ‚’ไฝฟ็”จใ—ใŸ BERT ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://www.philschmid.de/pre-training-bert-habana)ใ€‚ + +๐Ÿš€ **ใƒ‡ใƒ—ใƒญใ‚ค** +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นๆœ€้ฉๅŒ–ใงใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ ONNX ใซๅค‰ๆ›ใ™ใ‚‹](https://www.philschmid.de/convert-transformers-to-onnx)ใ€‚ +- ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [AWS ไธŠใฎ Habana Gaudi ใ‚’ไฝฟ็”จใ—ใŸใƒใ‚ฐ้ก”ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎใŸใ‚ใฎๆทฑๅฑคๅญฆ็ฟ’็’ฐๅขƒใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—](https://www.philschmid.de/getting-started-habana-gaudi#conclusion)ใ€‚ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [Hugging Face Transformersใ€Amazon SageMakerใ€ใŠใ‚ˆใณ Terraform ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใŸ่‡ชๅ‹•ใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐ BERT](https://www.philschmid.de/terraform-huggingface-amazon-sagemaker-advanced)ใ€‚ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [HuggingFaceใ€AWS Lambdaใ€Docker ใ‚’ไฝฟ็”จใ—ใŸใ‚ตใƒผใƒใƒผใƒฌใ‚น BERT](https://www.philschmid.de/serverless-bert-with-huggingface-aws-lambda-docker)ใ€‚ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [Amazon SageMaker ใจ Training Compiler ใ‚’ไฝฟ็”จใ—ใŸ Hugging Face Transformers BERT ๅพฎ่ชฟๆ•ด](https://www.philschmid.de/huggingface-amazon-sagemaker-training-compiler)ใ€‚ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ [Transformers ใจ Amazon SageMaker ใ‚’ไฝฟ็”จใ—ใŸ BERT ใฎใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎ็Ÿฅ่ญ˜ใฎ่’ธ็•™](https://www.philschmid.de/knowledge-distillation-bert-transformers) + +## BertConfig + +[[autodoc]] BertConfig + - all + +## BertTokenizer + +[[autodoc]] BertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + + + + +## BertTokenizerFast + +[[autodoc]] BertTokenizerFast + + + + +## TFBertTokenizer + +[[autodoc]] TFBertTokenizer + + + + +## Bert specific outputs + +[[autodoc]] models.bert.modeling_bert.BertForPreTrainingOutput + +[[autodoc]] models.bert.modeling_tf_bert.TFBertForPreTrainingOutput + +[[autodoc]] models.bert.modeling_flax_bert.FlaxBertForPreTrainingOutput + + + + +## BertModel + +[[autodoc]] BertModel + - forward + +## BertForPreTraining + +[[autodoc]] BertForPreTraining + - forward + +## BertLMHeadModel + +[[autodoc]] BertLMHeadModel + - forward + +## BertForMaskedLM + +[[autodoc]] BertForMaskedLM + - forward + +## BertForNextSentencePrediction + +[[autodoc]] BertForNextSentencePrediction + - forward + +## BertForSequenceClassification + +[[autodoc]] BertForSequenceClassification + - forward + +## BertForMultipleChoice + +[[autodoc]] BertForMultipleChoice + - forward + +## BertForTokenClassification + +[[autodoc]] BertForTokenClassification + - forward + +## BertForQuestionAnswering + +[[autodoc]] BertForQuestionAnswering + - forward + + + + +## TFBertModel + +[[autodoc]] TFBertModel + - call + +## TFBertForPreTraining + +[[autodoc]] TFBertForPreTraining + - call + +## TFBertModelLMHeadModel + +[[autodoc]] TFBertLMHeadModel + - call + +## TFBertForMaskedLM + +[[autodoc]] TFBertForMaskedLM + - call + +## TFBertForNextSentencePrediction + +[[autodoc]] TFBertForNextSentencePrediction + - call + +## TFBertForSequenceClassification + +[[autodoc]] TFBertForSequenceClassification + - call + +## TFBertForMultipleChoice + +[[autodoc]] TFBertForMultipleChoice + - call + +## TFBertForTokenClassification + +[[autodoc]] TFBertForTokenClassification + - call + +## TFBertForQuestionAnswering + +[[autodoc]] TFBertForQuestionAnswering + - call + + + + + +## FlaxBertModel + +[[autodoc]] FlaxBertModel + - __call__ + +## FlaxBertForPreTraining + +[[autodoc]] FlaxBertForPreTraining + - __call__ + +## FlaxBertForCausalLM + +[[autodoc]] FlaxBertForCausalLM + - __call__ + +## FlaxBertForMaskedLM + +[[autodoc]] FlaxBertForMaskedLM + - __call__ + +## FlaxBertForNextSentencePrediction + +[[autodoc]] FlaxBertForNextSentencePrediction + - __call__ + +## FlaxBertForSequenceClassification + +[[autodoc]] FlaxBertForSequenceClassification + - __call__ + +## FlaxBertForMultipleChoice + +[[autodoc]] FlaxBertForMultipleChoice + - __call__ + +## FlaxBertForTokenClassification + +[[autodoc]] FlaxBertForTokenClassification + - __call__ + +## FlaxBertForQuestionAnswering + +[[autodoc]] FlaxBertForQuestionAnswering + - __call__ + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bertweet.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bertweet.md new file mode 100644 index 0000000000000000000000000000000000000000..3a5dddbf04cc5884576083fbddac127ff24ec8ae --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bertweet.md @@ -0,0 +1,68 @@ + + +# BERTweet + +## Overview + +BERTweet ใƒขใƒ‡ใƒซใฏใ€Dat Quoc Nguyenใ€Thanh Vu ใซใ‚ˆใฃใฆ [BERTweet: A pre-trained language model for English Tweets](https://www.aclweb.org/anthology/2020.emnlp-demos.2.pdf) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใ‚ขใƒณใƒปใƒˆใ‚ฅใ‚ขใƒณใƒปใ‚ฐใ‚จใƒณใ•ใ‚“ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*็งใŸใกใฏใ€่‹ฑ่ชžใƒ„ใ‚คใƒผใƒˆ็”จใซๅˆใ‚ใฆๅ…ฌ้–‹ใ•ใ‚ŒใŸๅคง่ฆๆจกใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใงใ‚ใ‚‹ BERTweet ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚็งใŸใกใฎBERTweetใฏใ€ +BERT ใƒ™ใƒผใ‚นใจๅŒใ˜ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ (Devlin et al., 2019) ใฏใ€RoBERTa ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ‰‹้ † (Liu et al.) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ +al.ใ€2019๏ผ‰ใ€‚ๅฎŸ้จ“ใงใฏใ€BERTweet ใŒๅผทๅŠ›ใชใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใงใ‚ใ‚‹ RoBERTa ใƒ™ใƒผใ‚นใŠใ‚ˆใณ XLM-R ใƒ™ใƒผใ‚นใ‚’ไธŠๅ›žใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ™ใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ (Conneau et al., +2020)ใ€3 ใคใฎใƒ„ใ‚คใƒผใƒˆ NLP ใ‚ฟใ‚นใ‚ฏใซใŠใ„ใฆใ€ไปฅๅ‰ใฎๆœ€ๅ…ˆ็ซฏใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ—ใŸใ€‚ +ๅ“่ฉžใ‚ฟใ‚ฐไป˜ใ‘ใ€ๅ›บๆœ‰่กจ็พ่ช่ญ˜ใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ€‚* + +## Usage example + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bertweet = AutoModel.from_pretrained("vinai/bertweet-base") + +>>> # For transformers v4.x+: +>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False) + +>>> # For transformers v3.x: +>>> # tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base") + +>>> # INPUT TWEET IS ALREADY NORMALIZED! +>>> line = "SC has first two presumptive cases of coronavirus , DHEC confirms HTTPURL via @USER :cry:" + +>>> input_ids = torch.tensor([tokenizer.encode(line)]) + +>>> with torch.no_grad(): +... features = bertweet(input_ids) # Models outputs are now tuples + +>>> # With TensorFlow 2.0+: +>>> # from transformers import TFAutoModel +>>> # bertweet = TFAutoModel.from_pretrained("vinai/bertweet-base") +``` + + +ใ“ใฎๅฎŸ่ฃ…ใฏใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ–นๆณ•ใ‚’้™คใ„ใฆ BERT ใจๅŒใ˜ใงใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[BERT ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](bert) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นๆƒ…ๅ ฑใ€‚ + + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [dqnguyen](https://huggingface.co/dqnguyen) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/VinAIResearch/BERTweet) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## BertweetTokenizer + +[[autodoc]] BertweetTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/big_bird.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/big_bird.md new file mode 100644 index 0000000000000000000000000000000000000000..d5f4f9d282eac46ca0109f56ea46c70da7d87877 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/big_bird.md @@ -0,0 +1,176 @@ + + +# BigBird + +## Overview + +BigBird ใƒขใƒ‡ใƒซใฏใ€[Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ถใƒ’ใƒผใƒซใ€ใƒžใƒณใ‚ธใƒซใจใ‚ฐใƒซใ‚ฌใƒใ‚ทใƒฅใ€ใ‚ฐใƒซใจใƒ€ใƒ™ใ‚คใ€ใ‚ฏใƒžใƒผใƒซใƒปใ‚ขใƒดใ‚ฃใƒŠใƒดใ‚กใจใ‚จใ‚คใƒณใ‚บใƒชใƒผใ€ใ‚ธใƒงใ‚ทใƒฅใ‚ขใจใ‚ขใƒซใƒ™ใƒซใƒ†ใ‚ฃใ€ใ‚ฏใƒชใ‚นใจใ‚ชใƒณใ‚ฟใƒŽใƒณใ€ +ใ‚ตใƒณใƒ†ใ‚ฃใ‚ขใ‚ดใจใƒ•ใ‚กใƒ ใ€ใƒ•ใ‚ฃใƒชใƒƒใƒ—ใจใƒฉใƒ–ใƒฉใ€ใ‚ขใƒ‹ใƒซใƒผใƒ‰ใจใƒฏใƒณใ€ใ‚ญใƒผใƒ•ใ‚กใƒณใจใƒคใƒณใ€ใƒชใƒผใชใฉใ€‚ BigBird ใฏๆณจ็›ฎๅบฆใŒไฝŽใ„ +BERT ใชใฉใฎ Transformer ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใ‚’ใ•ใ‚‰ใซ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๆ‹กๅผตใ™ใ‚‹ใ€Transformer ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใ€‚ใพใฐใ‚‰ใซๅŠ ใˆใฆ +ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจๅŒๆง˜ใซใ€BigBird ใฏๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒฉใƒณใƒ€ใƒ  ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ ใ‘ใงใชใใ‚ฐใƒญใƒผใƒใƒซ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚‚้ฉ็”จใ—ใพใ™ใ€‚็†่ซ–็š„ใซใฏใ€ +ใพใฐใ‚‰ใงๅ…จไฝ“็š„ใงใƒฉใƒณใƒ€ใƒ ใชๆณจๆ„ใ‚’้ฉ็”จใ™ใ‚‹ใจใ€ๅฎŒๅ…จใชๆณจๆ„ใซ่ฟ‘ใฅใใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใŒใ€ +้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใงใฏ่จˆ็ฎ—ๅŠน็އใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ™ใ€‚ใ‚ˆใ‚Š้•ทใ„ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ‚‹ๆฉŸ่ƒฝใฎ็ตๆžœใจใ—ใฆใ€ +BigBird ใฏใ€่ณชๅ•ๅฟœ็ญ”ใ‚„ +BERT ใพใŸใฏ RoBERTa ใจๆฏ”่ผƒใ—ใŸ่ฆ็ด„ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*BERT ใชใฉใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฏใ€NLP ใงๆœ€ใ‚‚ๆˆๅŠŸใ—ใŸๆทฑๅฑคๅญฆ็ฟ’ใƒขใƒ‡ใƒซใฎ 1 ใคใงใ™ใ€‚ +ๆฎ‹ๅฟตใชใŒใ‚‰ใ€ใใ‚Œใ‚‰ใฎไธญๆ ธ็š„ใชๅˆถ้™ใฎ 1 ใคใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพใ™ใ‚‹ไบŒๆฌกไพๅญ˜ๆ€ง (ไธปใซใƒกใƒขใƒชใซ้–ขใ™ใ‚‹) ใงใ™ใ€‚ +ๅฎŒๅ…จใชๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใซใ‚ˆใ‚‹้•ทใ•ใงใ™ใ€‚ใ“ใ‚Œใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใ€BigBird ใฏใ€ใพใฐใ‚‰ใชๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +ใ“ใฎไบŒๆฌกไพๅญ˜้–ขไฟ‚ใ‚’็ทšๅฝขใซๅ‰Šๆธ›ใ—ใพใ™ใ€‚ BigBird ใŒใ‚ทใƒผใ‚ฑใƒณใ‚น้–ขๆ•ฐใฎๆฑŽ็”จ่ฟ‘ไผผๅ™จใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒใƒฅใƒผใƒชใƒณใ‚ฐใฏๅฎŒๅ…จใงใ‚ใ‚‹ใŸใ‚ใ€ไบŒๆฌกๅฎŒๅ…จๆณจๆ„ใƒขใƒ‡ใƒซใฎใ“ใ‚Œใ‚‰ใฎ็‰นๆ€งใŒไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚้€”ไธญใ€็งใŸใกใฎ +็†่ซ–ๅˆ†ๆžใซใ‚ˆใ‚Šใ€O(1) ๅ€‹ใฎใ‚ฐใƒญใƒผใƒใƒซ ใƒˆใƒผใ‚ฏใƒณ (CLS ใชใฉ) ใ‚’ๆŒใคๅˆฉ็‚นใฎไธ€้ƒจใŒๆ˜Žใ‚‰ใ‹ใซใชใ‚Šใ€ +ใ‚นใƒ‘ใƒผใ‚นๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใฎไธ€้ƒจใจใ—ใฆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ€‚ๆๆกˆใ•ใ‚ŒใŸใ‚นใƒ‘ใƒผใ‚น ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฏใ€ๆฌกใฎ้•ทใ•ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ +ๅŒๆง˜ใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚’ไฝฟ็”จใ—ใฆไปฅๅ‰ใซๅฏ่ƒฝใงใ‚ใฃใŸใ‚‚ใฎใฎ 8 ๅ€ใ€‚ใ‚ˆใ‚Š้•ทใ„ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ‚‹ๆฉŸ่ƒฝใฎ็ตๆžœใจใ—ใฆใ€ +BigBird ใฏใ€่ณชๅ•ๅฟœ็ญ”ใ‚„่ฆ็ด„ใชใฉใฎใ•ใพใ–ใพใช NLP ใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅคงๅน…ใซๅ‘ไธŠใ•ใ›ใพใ™ใ€‚็ง้”ใ‚‚ +ใ‚ฒใƒŽใƒŸใ‚ฏใ‚นใƒ‡ใƒผใ‚ฟใธใฎๆ–ฐใ—ใ„ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ๆๆกˆใ—ใพใ™ใ€‚* + +ใƒใƒƒใƒ—๏ผš + +- BigBird ใฎๆณจๆ„ใŒใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใซใคใ„ใฆใฎ่ฉณ็ดฐใช่ชฌๆ˜Žใซใคใ„ใฆใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://huggingface.co/blog/big-bird) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- BigBird ใซใฏใ€**original_full** ใจ **block_sparse** ใฎ 2 ใคใฎๅฎŸ่ฃ…ใŒไป˜ๅฑžใ—ใฆใ„ใพใ™ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ 1024 ๆœชๆบ€ใฎๅ ดๅˆใ€ๆฌกใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + **block_sparse** ใ‚’ไฝฟ็”จใ—ใฆใ‚‚ใƒกใƒชใƒƒใƒˆใŒใชใ„ใŸใ‚ใ€**original_full** ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +- ใ‚ณใƒผใƒ‰ใฏ็พๅœจใ€3 ใƒ–ใƒญใƒƒใ‚ฏใจ 2 ใ‚ฐใƒญใƒผใƒใƒซ ใƒ–ใƒญใƒƒใ‚ฏใฎใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆ ใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ +- ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใฏใƒ–ใƒญใƒƒใ‚ฏ ใ‚ตใ‚คใ‚บใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ็พๅœจใฎๅฎŸ่ฃ…ใงใฏ **ITC** ใฎใฟใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- ็พๅœจใฎๅฎŸ่ฃ…ใงใฏ **num_random_blocks = 0** ใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ +- BigBird ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ + + ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[vasudevgupta](https://huggingface.co/vasudevgupta) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใŒ่ฆ‹ใคใ‹ใ‚‹ +[ใ“ใกใ‚‰](https://github.com/google-research/bigbird)ใ€‚ + +## ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒชใ‚ฝใƒผใ‚น + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_lang_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## BigBirdConfig + +[[autodoc]] BigBirdConfig + +## BigBirdTokenizer + +[[autodoc]] BigBirdTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## BigBirdTokenizerFast + +[[autodoc]] BigBirdTokenizerFast + +## BigBird specific outputs + +[[autodoc]] models.big_bird.modeling_big_bird.BigBirdForPreTrainingOutput + + + + +## BigBirdModel + +[[autodoc]] BigBirdModel + - forward + +## BigBirdForPreTraining + +[[autodoc]] BigBirdForPreTraining + - forward + +## BigBirdForCausalLM + +[[autodoc]] BigBirdForCausalLM + - forward + +## BigBirdForMaskedLM + +[[autodoc]] BigBirdForMaskedLM + - forward + +## BigBirdForSequenceClassification + +[[autodoc]] BigBirdForSequenceClassification + - forward + +## BigBirdForMultipleChoice + +[[autodoc]] BigBirdForMultipleChoice + - forward + +## BigBirdForTokenClassification + +[[autodoc]] BigBirdForTokenClassification + - forward + +## BigBirdForQuestionAnswering + +[[autodoc]] BigBirdForQuestionAnswering + - forward + + + + +## FlaxBigBirdModel + +[[autodoc]] FlaxBigBirdModel + - __call__ + +## FlaxBigBirdForPreTraining + +[[autodoc]] FlaxBigBirdForPreTraining + - __call__ + +## FlaxBigBirdForCausalLM + +[[autodoc]] FlaxBigBirdForCausalLM + - __call__ + +## FlaxBigBirdForMaskedLM + +[[autodoc]] FlaxBigBirdForMaskedLM + - __call__ + +## FlaxBigBirdForSequenceClassification + +[[autodoc]] FlaxBigBirdForSequenceClassification + - __call__ + +## FlaxBigBirdForMultipleChoice + +[[autodoc]] FlaxBigBirdForMultipleChoice + - __call__ + +## FlaxBigBirdForTokenClassification + +[[autodoc]] FlaxBigBirdForTokenClassification + - __call__ + +## FlaxBigBirdForQuestionAnswering + +[[autodoc]] FlaxBigBirdForQuestionAnswering + - __call__ + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bigbird_pegasus.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bigbird_pegasus.md new file mode 100644 index 0000000000000000000000000000000000000000..ecee19d6a5190a1969ebfa9e704d2159bc0bce50 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bigbird_pegasus.md @@ -0,0 +1,95 @@ + + +# BigBirdPegasus + +## Overview + +BigBird ใƒขใƒ‡ใƒซใฏใ€[Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ถใƒ’ใƒผใƒซใ€ใƒžใƒณใ‚ธใƒซใจใ‚ฐใƒซใ‚ฌใƒใ‚ทใƒฅใ€ใ‚ฐใƒซใจใƒ€ใƒ™ใ‚คใ€ใ‚ฏใƒžใƒผใƒซใƒปใ‚ขใƒดใ‚ฃใƒŠใƒดใ‚กใจใ‚จใ‚คใƒณใ‚บใƒชใƒผใ€ใ‚ธใƒงใ‚ทใƒฅใ‚ขใจใ‚ขใƒซใƒ™ใƒซใƒ†ใ‚ฃใ€ใ‚ฏใƒชใ‚นใจใ‚ชใƒณใ‚ฟใƒŽใƒณใ€ +ใ‚ตใƒณใƒ†ใ‚ฃใ‚ขใ‚ดใจใƒ•ใ‚กใƒ ใ€ใƒ•ใ‚ฃใƒชใƒƒใƒ—ใจใƒฉใƒ–ใƒฉใ€ใ‚ขใƒ‹ใƒซใƒผใƒ‰ใจใƒฏใƒณใ€ใ‚ญใƒผใƒ•ใ‚กใƒณใจใƒคใƒณใ€ใƒชใƒผใชใฉใ€‚ BigBird ใฏๆณจ็›ฎๅบฆใŒไฝŽใ„ +BERT ใชใฉใฎ Transformer ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใ‚’ใ•ใ‚‰ใซ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๆ‹กๅผตใ™ใ‚‹ใ€Transformer ใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใ€‚ใพใฐใ‚‰ใซๅŠ ใˆใฆ +ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจๅŒๆง˜ใซใ€BigBird ใฏๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒฉใƒณใƒ€ใƒ  ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ ใ‘ใงใชใใ‚ฐใƒญใƒผใƒใƒซ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚‚้ฉ็”จใ—ใพใ™ใ€‚็†่ซ–็š„ใซใฏใ€ +ใพใฐใ‚‰ใงๅ…จไฝ“็š„ใงใƒฉใƒณใƒ€ใƒ ใชๆณจๆ„ใ‚’้ฉ็”จใ™ใ‚‹ใจใ€ๅฎŒๅ…จใชๆณจๆ„ใซ่ฟ‘ใฅใใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใŒใ€ +้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใงใฏ่จˆ็ฎ—ๅŠน็އใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ™ใ€‚ใ‚ˆใ‚Š้•ทใ„ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ‚‹ๆฉŸ่ƒฝใฎ็ตๆžœใจใ—ใฆใ€ +BigBird ใฏใ€่ณชๅ•ๅฟœ็ญ”ใ‚„ +BERT ใพใŸใฏ RoBERTa ใจๆฏ”่ผƒใ—ใŸ่ฆ็ด„ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*BERT ใชใฉใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฏใ€NLP ใงๆœ€ใ‚‚ๆˆๅŠŸใ—ใŸๆทฑๅฑคๅญฆ็ฟ’ใƒขใƒ‡ใƒซใฎ 1 ใคใงใ™ใ€‚ +ๆฎ‹ๅฟตใชใŒใ‚‰ใ€ใใ‚Œใ‚‰ใฎไธญๆ ธ็š„ใชๅˆถ้™ใฎ 1 ใคใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพใ™ใ‚‹ไบŒๆฌกไพๅญ˜ๆ€ง (ไธปใซใƒกใƒขใƒชใซ้–ขใ™ใ‚‹) ใงใ™ใ€‚ +ๅฎŒๅ…จใชๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใซใ‚ˆใ‚‹้•ทใ•ใงใ™ใ€‚ใ“ใ‚Œใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใ€BigBird ใฏใ€ใพใฐใ‚‰ใชๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +ใ“ใฎไบŒๆฌกไพๅญ˜้–ขไฟ‚ใ‚’็ทšๅฝขใซๅ‰Šๆธ›ใ—ใพใ™ใ€‚ BigBird ใŒใ‚ทใƒผใ‚ฑใƒณใ‚น้–ขๆ•ฐใฎๆฑŽ็”จ่ฟ‘ไผผๅ™จใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒใƒฅใƒผใƒชใƒณใ‚ฐใฏๅฎŒๅ…จใงใ‚ใ‚‹ใŸใ‚ใ€ไบŒๆฌกๅฎŒๅ…จๆณจๆ„ใƒขใƒ‡ใƒซใฎใ“ใ‚Œใ‚‰ใฎ็‰นๆ€งใŒไฟๅญ˜ใ•ใ‚Œใพใ™ใ€‚้€”ไธญใ€็งใŸใกใฎ +็†่ซ–ๅˆ†ๆžใซใ‚ˆใ‚Šใ€O(1) ๅ€‹ใฎใ‚ฐใƒญใƒผใƒใƒซ ใƒˆใƒผใ‚ฏใƒณ (CLS ใชใฉ) ใ‚’ๆŒใคๅˆฉ็‚นใฎไธ€้ƒจใŒๆ˜Žใ‚‰ใ‹ใซใชใ‚Šใ€ +ใ‚นใƒ‘ใƒผใ‚นๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใฎไธ€้ƒจใจใ—ใฆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ€‚ๆๆกˆใ•ใ‚ŒใŸใ‚นใƒ‘ใƒผใ‚น ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฏใ€ๆฌกใฎ้•ทใ•ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ +ๅŒๆง˜ใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚’ไฝฟ็”จใ—ใฆไปฅๅ‰ใซๅฏ่ƒฝใงใ‚ใฃใŸใ‚‚ใฎใฎ 8 ๅ€ใ€‚ใ‚ˆใ‚Š้•ทใ„ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ‚‹ๆฉŸ่ƒฝใฎ็ตๆžœใจใ—ใฆใ€ +BigBird ใฏใ€่ณชๅ•ๅฟœ็ญ”ใ‚„่ฆ็ด„ใชใฉใฎใ•ใพใ–ใพใช NLP ใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅคงๅน…ใซๅ‘ไธŠใ•ใ›ใพใ™ใ€‚็ง้”ใ‚‚ +ใ‚ฒใƒŽใƒŸใ‚ฏใ‚นใƒ‡ใƒผใ‚ฟใธใฎๆ–ฐใ—ใ„ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ๆๆกˆใ—ใพใ™ใ€‚* + +## Usage tips + +- BigBird ใฎๆณจๆ„ใŒใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใซใคใ„ใฆใฎ่ฉณ็ดฐใช่ชฌๆ˜Žใซใคใ„ใฆใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://huggingface.co/blog/big-bird) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- BigBird ใซใฏใ€**original_full** ใจ **block_sparse** ใฎ 2 ใคใฎๅฎŸ่ฃ…ใŒไป˜ๅฑžใ—ใฆใ„ใพใ™ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ 1024 ๆœชๆบ€ใฎๅ ดๅˆใ€ๆฌกใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + **block_sparse** ใ‚’ไฝฟ็”จใ—ใฆใ‚‚ใƒกใƒชใƒƒใƒˆใŒใชใ„ใŸใ‚ใ€**original_full** ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +- ใ‚ณใƒผใƒ‰ใฏ็พๅœจใ€3 ใƒ–ใƒญใƒƒใ‚ฏใจ 2 ใ‚ฐใƒญใƒผใƒใƒซ ใƒ–ใƒญใƒƒใ‚ฏใฎใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆ ใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ +- ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใฏใƒ–ใƒญใƒƒใ‚ฏ ใ‚ตใ‚คใ‚บใงๅ‰ฒใ‚Šๅˆ‡ใ‚Œใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ็พๅœจใฎๅฎŸ่ฃ…ใงใฏ **ITC** ใฎใฟใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- ็พๅœจใฎๅฎŸ่ฃ…ใงใฏ **num_random_blocks = 0** ใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ +- BigBirdPegasus ใฏ [PegasusTokenizer](https://github.com/huggingface/transformers/blob/main/src/transformers/models/pegasus/tokenization_pegasus.py) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +- BigBird ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ + +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/google-research/bigbird) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒชใ‚ฝใƒผใ‚น + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [็ฟป่จณใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/translation) +- [่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/summarization) + +## BigBirdPegasusConfig + +[[autodoc]] BigBirdPegasusConfig + - all + +## BigBirdPegasusModel + +[[autodoc]] BigBirdPegasusModel + - forward + +## BigBirdPegasusForConditionalGeneration + +[[autodoc]] BigBirdPegasusForConditionalGeneration + - forward + +## BigBirdPegasusForSequenceClassification + +[[autodoc]] BigBirdPegasusForSequenceClassification + - forward + +## BigBirdPegasusForQuestionAnswering + +[[autodoc]] BigBirdPegasusForQuestionAnswering + - forward + +## BigBirdPegasusForCausalLM + +[[autodoc]] BigBirdPegasusForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/biogpt.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/biogpt.md new file mode 100644 index 0000000000000000000000000000000000000000..0634062a6b7214f0bc7631826c05db31b1322bcd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/biogpt.md @@ -0,0 +1,73 @@ + + +# BioGPT + +## Overview + +BioGPT ใƒขใƒ‡ใƒซใฏใ€[BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) by Renqian Luoใ€Liai Sunใ€Yingce Xiaใ€ Tao Qinใ€Sheng Zhangใ€Hoifung Poonใ€Tie-Yan Liuใ€‚ BioGPT ใฏใ€็”Ÿ็‰ฉๅŒปๅญฆใƒ†ใ‚ญใ‚นใƒˆใฎ็”Ÿๆˆใจใƒžใ‚คใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎใ€ใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎ็”Ÿๆˆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ Transformer ่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ BioGPT ใฏใ€Transformer ่จ€่ชžใƒขใƒ‡ใƒซใฎใƒใƒƒใ‚ฏใƒœใƒผใƒณใซๅพ“ใ„ใ€1,500 ไธ‡ใฎ PubMed ๆŠ„้Œฒใงๆœ€ๅˆใ‹ใ‚‰ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ไธ€่ˆฌ็š„ใช่‡ช็„ถ่จ€่ชž้ ˜ๅŸŸใงใฎๅคงใใชๆˆๅŠŸใซ่งฆ็™บใ•ใ‚Œใฆใ€็”Ÿ็‰ฉๅŒปๅญฆ้ ˜ๅŸŸใงใพใ™ใพใ™ๆณจ็›ฎใ‚’้›†ใ‚ใฆใ„ใพใ™ใ€‚ไธ€่ˆฌ่จ€่ชžใƒ‰ใƒกใ‚คใƒณใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฎ 2 ใคใฎไธปใชใƒ–ใƒฉใƒณใƒใ€ใคใพใ‚Š BERT (ใŠใ‚ˆใณใใฎใƒใƒชใ‚ขใƒณใƒˆ) ใจ GPT (ใŠใ‚ˆใณใใฎใƒใƒชใ‚ขใƒณใƒˆ) ใฎใ†ใกใ€1 ใค็›ฎใฏ BioBERT ใ‚„ PubMedBERT ใชใฉใฎ็”Ÿ็‰ฉๅŒปๅญฆใƒ‰ใƒกใ‚คใƒณใงๅบƒใ็ ”็ฉถใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใ•ใพใ–ใพใชไธ‹ๆตใฎ็”Ÿ็‰ฉๅŒปๅญฆ็š„ใ‚ฟใ‚นใ‚ฏใงๅคงใใชๆˆๅŠŸใ‚’ๅŽใ‚ใฆใ„ใพใ™ใŒใ€็”Ÿๆˆ่ƒฝๅŠ›ใฎๆฌ ๅฆ‚ใซใ‚ˆใ‚Šๅฟœ็”จ็ฏ„ๅ›ฒใŒๅˆถ้™ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๅคง่ฆๆจกใช็”Ÿ็‰ฉๅŒปๅญฆๆ–‡็Œฎใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎ็”Ÿๆˆ Transformer ่จ€่ชžใƒขใƒ‡ใƒซใงใ‚ใ‚‹ BioGPT ใ‚’ๆๆกˆใ—ใพใ™ใ€‚็งใŸใกใฏ 6 ใคใฎ็”Ÿ็‰ฉๅŒปๅญฆ็š„่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใ‚ฟใ‚นใ‚ฏใง BioGPT ใ‚’่ฉ•ไพกใ—ใ€ใปใจใ‚“ใฉใฎใ‚ฟใ‚นใ‚ฏใง็งใŸใกใฎใƒขใƒ‡ใƒซใŒไปฅๅ‰ใฎใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’ๅฎŸ่จผใ—ใพใ—ใŸใ€‚็‰นใซใ€BC5CDRใ€KD-DTIใ€DDI ใฎใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰้–ขไฟ‚ๆŠฝๅ‡บใ‚ฟใ‚นใ‚ฏใงใฏใใ‚Œใžใ‚Œ 44.98%ใ€38.42%ใ€40.76% ใฎ F1 ใ‚นใ‚ณใ‚ขใ‚’็ฒๅพ—ใ—ใ€PubMedQA ใงใฏ 78.2% ใฎ็ฒพๅบฆใ‚’็ฒๅพ—ใ—ใ€ๆ–ฐ่จ˜้Œฒใ‚’ๆจน็ซ‹ใ—ใพใ—ใŸใ€‚ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซ้–ขใ™ใ‚‹็งใŸใกใฎใ‚ฑใƒผใ‚นใ‚นใ‚ฟใƒ‡ใ‚ฃใฏใ€็”Ÿ็‰ฉๅŒปๅญฆๆ–‡็ŒฎใซใŠใ‘ใ‚‹ BioGPT ใฎๅˆฉ็‚นใ‚’ใ•ใ‚‰ใซๅฎŸ่จผใ—ใ€็”Ÿ็‰ฉๅŒปๅญฆ็”จ่ชžใฎๆตๆšขใช่ชฌๆ˜Žใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚* + +## Usage tips + +- BioGPT ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅทฆๅดใงใฏใชใๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +- BioGPT ใฏๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ (CLM) ็›ฎ็š„ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใฎใซๅผทๅŠ›ใงใ™ใ€‚ run_generation.py ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใง็ขบ่ชใงใใ‚‹ใ‚ˆใ†ใซใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใ™ใ‚‹ใจใ€BioGPT ใฏๆง‹ๆ–‡็š„ใซไธ€่ฒซใ—ใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ +- ใƒขใƒ‡ใƒซใฏใ€ไปฅๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸใ‚ญใƒผใจๅ€คใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒšใ‚ขใงใ‚ใ‚‹`past_key_values`(PyTorch ใฎๅ ดๅˆ) ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใฎ (past_key_values ใพใŸใฏ past) ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใŒใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใงไบ‹ๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸๅ€คใ‚’ๅ†่จˆ็ฎ—ใงใใชใใชใ‚Šใพใ™ใ€‚ PyTorch ใฎไฝฟ็”จๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€BioGptForCausalLM.forward() ใƒกใ‚ฝใƒƒใƒ‰ใฎ past_key_values ๅผ•ๆ•ฐใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[kamalkraj](https://huggingface.co/kamalkraj) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/BioGPT) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Documentation resources + +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) + +## BioGptConfig + +[[autodoc]] BioGptConfig + + +## BioGptTokenizer + +[[autodoc]] BioGptTokenizer + - save_vocabulary + + +## BioGptModel + +[[autodoc]] BioGptModel + - forward + + +## BioGptForCausalLM + +[[autodoc]] BioGptForCausalLM + - forward + + +## BioGptForTokenClassification + +[[autodoc]] BioGptForTokenClassification + - forward + + +## BioGptForSequenceClassification + +[[autodoc]] BioGptForSequenceClassification + - forward + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bit.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bit.md new file mode 100644 index 0000000000000000000000000000000000000000..a9e13e270aaf9690b042ebfab9d23836350cb142 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bit.md @@ -0,0 +1,70 @@ + + +# Big Transfer (BiT) + +## Overview + +BiT ใƒขใƒ‡ใƒซใฏใ€Alexander Kolesnikovใ€Lucas Beyerใ€Xiaohua Zhaiใ€Joan Puigcerverใ€Jessica Yungใ€Sylvain Gelly ใซใ‚ˆใฃใฆ [Big Transfer (BiT): General Visual Representation Learning](https://huggingface.co/papers/1912.11370) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใƒ‹ใƒผใƒซใƒปใƒ›ใƒผใƒซใ‚บใƒ“ใƒผใ€‚ +BiT ใฏใ€[ResNet](resnet) ใฎใ‚ˆใ†ใชใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ (ๅ…ทไฝ“็š„ใซใฏ ResNetv2) ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚นใ‚ฑใƒผใƒซใ‚ขใƒƒใƒ—ใ™ใ‚‹ใŸใ‚ใฎ็ฐกๅ˜ใชใƒฌใ‚ทใƒ”ใงใ™ใ€‚ใ“ใฎๆ–นๆณ•ใซใ‚ˆใ‚Šใ€่ปข็งปๅญฆ็ฟ’ใŒๅคงๅน…ใซๆ”นๅ–„ใ•ใ‚Œใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่กจ็พใฎ่ปข้€ใซใ‚ˆใ‚Šใ€ใ‚ตใƒณใƒ—ใƒซๅŠน็އใŒๅ‘ไธŠใ—ใ€่ฆ–่ฆš็”จใฎใƒ‡ใ‚ฃใƒผใƒ— ใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ่ชฟๆ•ดใŒ็ฐก็ด ๅŒ–ใ•ใ‚Œใพใ™ใ€‚ๅคง่ฆๆจกใชๆ•™ๅธซใ‚ใ‚Šใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ฟใ‚นใ‚ฏใงใฎใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใฎใƒ‘ใƒฉใƒ€ใ‚คใƒ ใ‚’ๅ†ๆคœ่จŽใ—ใพใ™ใ€‚็งใŸใกใฏไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚นใ‚ฑใƒผใƒซใ‚ขใƒƒใƒ—ใ—ใ€Big Transfer (BiT) ใจๅ‘ผใถใ‚ทใƒณใƒ—ใƒซใชใƒฌใ‚ทใƒ”ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ใ„ใใคใ‹ใฎๆ…Ž้‡ใซ้ธๆŠžใ•ใ‚ŒใŸใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’็ต„ใฟๅˆใ‚ใ›ใ€ใ‚ทใƒณใƒ—ใƒซใชใƒ’ใƒฅใƒผใƒชใ‚นใƒ†ใ‚ฃใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใฆ่ปข้€ใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€20 ใ‚’่ถ…ใˆใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ BiT ใฏใ€ใ‚ฏใƒฉใ‚นใ”ใจใซ 1 ใคใฎใ‚ตใƒณใƒ—ใƒซใ‹ใ‚‰ๅˆ่จˆ 100 ไธ‡ใฎใ‚ตใƒณใƒ—ใƒซใพใงใ€้ฉšใใปใฉๅบƒ็ฏ„ๅ›ฒใฎใƒ‡ใƒผใ‚ฟ้ ˜ๅŸŸใซใ‚ใŸใฃใฆ่‰ฏๅฅฝใซใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ BiT ใฏใ€ILSVRC-2012 ใง 87.5%ใ€CIFAR-10 ใง 99.4%ใ€19 ใ‚ฟใ‚นใ‚ฏใฎ Visual Task Adaptation Benchmark (VTAB) ใง 76.3% ใฎใƒˆใƒƒใƒ— 1 ็ฒพๅบฆใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚ๅฐ่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฏใ€BiT ใฏ ILSVRC-2012 (ใ‚ฏใƒฉใ‚นใ‚ใŸใ‚Š 10 ไพ‹) ใง 76.8%ใ€CIFAR-10 (ใ‚ฏใƒฉใ‚นใ‚ใŸใ‚Š 10 ไพ‹) ใง 97.0% ใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚้ซ˜ใ„่ปขๅ†™ๆ€ง่ƒฝใ‚’ๅฎŸ็พใ™ใ‚‹ไธป่ฆๆˆๅˆ†ใ‚’่ฉณ็ดฐใซๅˆ†ๆžโ€ปใ€‚ + +## Usage tips + +- BiT ใƒขใƒ‡ใƒซใฏใ€ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎ็‚นใง ResNetv2 ใจๅŒ็ญ‰ใงใ™ใŒใ€ๆฌกใฎ็‚นใŒ็•ฐใชใ‚Šใพใ™: 1) ใ™ในใฆใฎใƒใƒƒใƒๆญฃ่ฆๅŒ–ๅฑคใŒ [ใ‚ฐใƒซใƒผใƒ—ๆญฃ่ฆๅŒ–](https://huggingface.co/papers/1803.08494) ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ +2) [้‡ใฟใฎๆจ™ๆบ–ๅŒ–](https://huggingface.co/papers/1903.10520) ใฏ็•ณใฟ่พผใฟๅฑคใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚่‘—่€…ใ‚‰ใฏใ€ไธกๆ–นใฎ็ต„ใฟๅˆใ‚ใ›ใŒๅคงใใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฝน็ซ‹ใกใ€้‡่ฆใชๅŠนๆžœใŒใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ +่ปข็งปๅญฆ็ฟ’ใธใฎๅฝฑ้Ÿฟใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/google-research/big_transfer) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +BiT ใ‚’ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`BitForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## BitConfig + +[[autodoc]] BitConfig + +## BitImageProcessor + +[[autodoc]] BitImageProcessor + - preprocess + +## BitImageProcessorFast + +[[autodoc]] BitImageProcessorFast + - preprocess + +## BitModel + +[[autodoc]] BitModel + - forward + +## BitForImageClassification + +[[autodoc]] BitForImageClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot-small.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot-small.md new file mode 100644 index 0000000000000000000000000000000000000000..97455bddf806d5c29a7e2ad409152448c73c3eef --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot-small.md @@ -0,0 +1,110 @@ + + +# Blenderbot Small + +[`BlenderbotSmallModel`] ใจ +[`BlenderbotSmallForConditionalGeneration`] ใฏใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจ็ต„ใฟๅˆใ‚ใ›ใฆใฎใฟไฝฟ็”จใ•ใ‚Œใพใ™ +[facebook/blenderbot-90M](https://huggingface.co/facebook/blenderbot-90M)ใ€‚ใ‚ˆใ‚Šๅคง่ฆๆจกใช Blenderbot ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ +ไปฃใ‚ใ‚Šใซ [`BlenderbotModel`] ใจใจใ‚‚ใซไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ +[`BlenderbotForConditionalGeneration`] + +## Overview + +Blender ใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆ ใƒขใƒ‡ใƒซใฏใ€[Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) Stephen Rollerใ€Emily Dinanใ€Naman Goyalใ€Da Juใ€Mary Williamsonใ€yinghan Liuใ€ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ธใƒณใƒปใ‚ทใƒฅใƒผใ€ใƒžใ‚คใƒซใƒปใ‚ชใƒƒใƒˆใ€ใ‚ซใƒผใƒˆใƒปใ‚ทใƒฃใ‚นใ‚ฟใƒผใ€ใ‚จใƒชใƒƒใ‚ฏใƒปMใƒปใ‚นใƒŸใ‚นใ€Y-ใƒฉใƒณใƒปใƒ–ใƒผใƒญใƒผใ€ใ‚ธใ‚งใ‚คใ‚ฝใƒณใƒปใ‚ฆใ‚งใ‚นใƒˆใƒณใ€2020ๅนด4ๆœˆ30ๆ—ฅใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณใฎใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆใฎๆง‹็ฏ‰ใฏใ€ๆฉŸๆขฐๅญฆ็ฟ’็ ”็ฉถใซใจใฃใฆ้›ฃใ—ใ„ๅˆ†้‡Žใงใ™ใ€‚ใ“ใ‚Œใพใงใฎ็ ”็ฉถใงใฏๆฌกใฎใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใŒใ€ +ใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒขใƒ‡ใƒซใ‚’ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๆ•ฐใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏพ่ฑกใฎใƒ‡ใƒผใ‚ฟใฎใ‚ตใ‚คใ‚บใงใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ™ใ‚‹ใจใ€็ตๆžœใŒๅ‘ไธŠใ—ใพใ™ใ€‚ +้ซ˜ๆ€ง่ƒฝใฎใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆใซใฏไป–ใฎ่ฆ็ด ใ‚‚้‡่ฆใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚่‰ฏใ„ไผš่ฉฑใซใฏๅคšใใฎใ“ใจใŒๅฟ…่ฆใงใ™ +ไผš่ฉฑใฎๅฐ‚้–€ๅฎถใŒใ‚ทใƒผใƒ ใƒฌใ‚นใซ่žๅˆใ™ใ‚‹ใ‚นใ‚ญใƒซ: ้ญ…ๅŠ›็š„ใช่ฉฑใฎใƒใ‚คใƒณใƒˆใ‚’ๆไพ›ใ—ใ€่ฉฑใ‚’่žใ +ไธ€่ฒซใ—ใŸๆ…‹ๅบฆใ‚’็ถญๆŒใ—ใชใŒใ‚‰ใ€็Ÿฅ่ญ˜ใ€ๅ…ฑๆ„Ÿใ€ๅ€‹ๆ€งใ‚’้ฉๅˆ‡ใซ่กจ็พใ™ใ‚‹ +ใƒšใƒซใ‚ฝใƒŠใ€‚้ฉๅˆ‡ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใจ้ธๆŠžใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใ€ๅคง่ฆๆจกใƒขใƒ‡ใƒซใŒใ“ใ‚Œใ‚‰ใฎใ‚นใ‚ญใƒซใ‚’ๅญฆ็ฟ’ใงใใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ไธ–ไปฃๆˆฆ็•ฅใ€‚ 90Mใ€2.7Bใ€9.4B ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ“ใ‚Œใ‚‰ใฎใƒฌใ‚ทใƒ”ใฎใƒใƒชใ‚ขใƒณใƒˆใ‚’ๆง‹็ฏ‰ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ‚ณใƒผใƒ‰ใฏๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไบบ้–“ใซใ‚ˆใ‚‹่ฉ•ไพกใงใฏใ€ๅฝ“็คพใฎๆœ€่‰ฏใฎใƒขใƒ‡ใƒซใŒๆ—ขๅญ˜ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใƒžใƒซใƒใ‚ฟใƒผใƒณใง็คบใ•ใ‚Œใฆใ„ใพใ™ +้ญ…ๅŠ›ใจไบบ้–“ๆ€งใฎๆธฌๅฎšใจใ„ใ†่ฆณ็‚นใ‹ใ‚‰ใฎๅฏพ่ฉฑใ€‚ๆฌกใซใ€ๅˆ†ๆžใซใ‚ˆใฃใฆใ“ใฎไฝœๆฅญใฎ้™็•Œใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +ๅผŠ็คพๆฉŸ็จฎใฎๆ•…้šœไบ‹ไพ‹* + +ใƒใƒƒใƒ—๏ผš + +- Blenderbot Small ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใชใฎใงใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[patrickvonplaten](https://huggingface.co/patrickvonplaten) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚่‘—่€…ใฎใ‚ณใƒผใƒ‰ใฏๆฌกใฎใจใŠใ‚Šใงใ™ +[ใ“ใ“](https://github.com/facebookresearch/ParlAI) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## Documentation resources + +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [็ฟป่จณใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/translation) +- [่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/summarization) + +## BlenderbotSmallConfig + +[[autodoc]] BlenderbotSmallConfig + +## BlenderbotSmallTokenizer + +[[autodoc]] BlenderbotSmallTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## BlenderbotSmallTokenizerFast + +[[autodoc]] BlenderbotSmallTokenizerFast + +## BlenderbotSmallModel + +[[autodoc]] BlenderbotSmallModel + - forward + +## BlenderbotSmallForConditionalGeneration + +[[autodoc]] BlenderbotSmallForConditionalGeneration + - forward + +## BlenderbotSmallForCausalLM + +[[autodoc]] BlenderbotSmallForCausalLM + - forward + +## TFBlenderbotSmallModel + +[[autodoc]] TFBlenderbotSmallModel + - call + +## TFBlenderbotSmallForConditionalGeneration + +[[autodoc]] TFBlenderbotSmallForConditionalGeneration + - call + +## FlaxBlenderbotSmallModel + +[[autodoc]] FlaxBlenderbotSmallModel + - __call__ + - encode + - decode + +## FlaxBlenderbotForConditionalGeneration + +[[autodoc]] FlaxBlenderbotSmallForConditionalGeneration + - __call__ + - encode + - decode diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot.md new file mode 100644 index 0000000000000000000000000000000000000000..f2a03e69c9875c5db7f76cc0d68535da435c5554 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blenderbot.md @@ -0,0 +1,132 @@ + + +# Blenderbot + +**ๅ…่ฒฌไบ‹้ …:** ไฝ•ใ‹ๅฅ‡ๅฆ™ใชใ‚‚ใฎใ‚’่ฆ‹ใคใ‘ใŸๅ ดๅˆใฏใ€ [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) ใ‚’ๅ ฑๅ‘Šใ—ใฆใใ ใ•ใ„ใ€‚ + +## Overview + +Blender ใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆ ใƒขใƒ‡ใƒซใฏใ€[Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) Stephen Rollerใ€Emily Dinanใ€Naman Goyalใ€Da Juใ€Mary Williamsonใ€yinghan Liuใ€ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ธใƒณใƒปใ‚ทใƒฅใƒผใ€ใƒžใ‚คใƒซใƒปใ‚ชใƒƒใƒˆใ€ใ‚ซใƒผใƒˆใƒปใ‚ทใƒฃใ‚นใ‚ฟใƒผใ€ใ‚จใƒชใƒƒใ‚ฏใƒปMใƒปใ‚นใƒŸใ‚นใ€Y-ใƒฉใƒณใƒปใƒ–ใƒผใƒญใƒผใ€ใ‚ธใ‚งใ‚คใ‚ฝใƒณใƒปใ‚ฆใ‚งใ‚นใƒˆใƒณใ€2020ๅนด4ๆœˆ30ๆ—ฅใ€‚ + +่ซ–ๆ–‡ใฎ่ฆๆ—จใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณใฎใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆใฎๆง‹็ฏ‰ใฏใ€ๆฉŸๆขฐๅญฆ็ฟ’็ ”็ฉถใซใจใฃใฆ้›ฃใ—ใ„ๅˆ†้‡Žใงใ™ใ€‚ใ“ใ‚Œใพใงใฎ็ ”็ฉถใงใฏๆฌกใฎใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใŒใ€ +ใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒขใƒ‡ใƒซใ‚’ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๆ•ฐใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏพ่ฑกใฎใƒ‡ใƒผใ‚ฟใฎใ‚ตใ‚คใ‚บใงใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ™ใ‚‹ใจใ€็ตๆžœใŒๅ‘ไธŠใ—ใพใ™ใ€‚ +้ซ˜ๆ€ง่ƒฝใฎใƒใƒฃใƒƒใƒˆใƒœใƒƒใƒˆใซใฏไป–ใฎ่ฆ็ด ใ‚‚้‡่ฆใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚่‰ฏใ„ไผš่ฉฑใซใฏๅคšใใฎใ“ใจใŒๅฟ…่ฆใงใ™ +ไผš่ฉฑใฎๅฐ‚้–€ๅฎถใŒใ‚ทใƒผใƒ ใƒฌใ‚นใซ่žๅˆใ™ใ‚‹ใ‚นใ‚ญใƒซ: ้ญ…ๅŠ›็š„ใช่ฉฑใฎใƒใ‚คใƒณใƒˆใ‚’ๆไพ›ใ—ใ€่ฉฑใ‚’่žใ +ไธ€่ฒซใ—ใŸๆ…‹ๅบฆใ‚’็ถญๆŒใ—ใชใŒใ‚‰ใ€็Ÿฅ่ญ˜ใ€ๅ…ฑๆ„Ÿใ€ๅ€‹ๆ€งใ‚’้ฉๅˆ‡ใซ่กจ็พใ™ใ‚‹ +ใƒšใƒซใ‚ฝใƒŠใ€‚้ฉๅˆ‡ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใจ้ธๆŠžใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใ€ๅคง่ฆๆจกใƒขใƒ‡ใƒซใŒใ“ใ‚Œใ‚‰ใฎใ‚นใ‚ญใƒซใ‚’ๅญฆ็ฟ’ใงใใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ไธ–ไปฃๆˆฆ็•ฅใ€‚ 90Mใ€2.7Bใ€9.4B ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ“ใ‚Œใ‚‰ใฎใƒฌใ‚ทใƒ”ใฎใƒใƒชใ‚ขใƒณใƒˆใ‚’ๆง‹็ฏ‰ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ‚ณใƒผใƒ‰ใฏๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ไบบ้–“ใซใ‚ˆใ‚‹่ฉ•ไพกใงใฏใ€ๅฝ“็คพใฎๆœ€่‰ฏใฎใƒขใƒ‡ใƒซใŒๆ—ขๅญ˜ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใƒžใƒซใƒใ‚ฟใƒผใƒณใง็คบใ•ใ‚Œใฆใ„ใพใ™ +้ญ…ๅŠ›ใจไบบ้–“ๆ€งใฎๆธฌๅฎšใจใ„ใ†่ฆณ็‚นใ‹ใ‚‰ใฎๅฏพ่ฉฑใ€‚ๆฌกใซใ€ๅˆ†ๆžใซใ‚ˆใฃใฆใ“ใฎไฝœๆฅญใฎ้™็•Œใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +ๅผŠ็คพๆฉŸ็จฎใฎๆ•…้šœไบ‹ไพ‹* + +ใƒใƒƒใƒ—๏ผš + +- Blenderbot ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [sshleifer](https://huggingface.co/sshleifer) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚่‘—่€…ใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/facebookresearch/ParlAI) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Implementation Notes + +- Blenderbot ใฏใ€ๆจ™ๆบ–ใฎ [seq2seq ใƒขใƒ‡ใƒซ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ](https://huggingface.co/papers/1706.03762) ใƒ™ใƒผใ‚นใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +- ๅˆฉ็”จๅฏ่ƒฝใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€[ใƒขใƒ‡ใƒซ ใƒใƒ–](https://huggingface.co/models?search=blenderbot) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +- ใ“ใ‚Œใฏ *ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆ* Blenderbot ใƒขใƒ‡ใƒซ ใ‚ฏใƒฉใ‚นใงใ™ใ€‚ใŸใ ใ—ใ€ๆฌกใฎใ‚ˆใ†ใชๅฐใ•ใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚‚ใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ + `facebook/blenderbot_small_90M` ใฏใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒ็•ฐใชใ‚‹ใŸใ‚ใ€ไธ€็ท’ใซไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + [BlenderbotSmall](ใƒ–ใƒฌใƒณใƒ€ใƒผใƒœใƒƒใƒˆๅฐ)ใ€‚ + +## Usage + +ใƒขใƒ‡ใƒซใฎไฝฟ็”จไพ‹ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```python +>>> from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration + +>>> mname = "facebook/blenderbot-400M-distill" +>>> model = BlenderbotForConditionalGeneration.from_pretrained(mname) +>>> tokenizer = BlenderbotTokenizer.from_pretrained(mname) +>>> UTTERANCE = "My friends are cool but they eat too many carbs." +>>> inputs = tokenizer([UTTERANCE], return_tensors="pt") +>>> reply_ids = model.generate(**inputs) +>>> print(tokenizer.batch_decode(reply_ids)) +[" That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"] +``` + +## Documentation resources + +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [็ฟป่จณใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/translation) +- [่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/summarization) + +## BlenderbotConfig + +[[autodoc]] BlenderbotConfig + +## BlenderbotTokenizer + +[[autodoc]] BlenderbotTokenizer + - build_inputs_with_special_tokens + +## BlenderbotTokenizerFast + +[[autodoc]] BlenderbotTokenizerFast + - build_inputs_with_special_tokens + +## BlenderbotModel + +*forward* ใŠใ‚ˆใณ *generate* ใฎๅผ•ๆ•ฐใซใคใ„ใฆใฏใ€`transformers.BartModel`ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] BlenderbotModel + - forward + +## BlenderbotForConditionalGeneration + +*forward* ใจ *generate* ใฎๅผ•ๆ•ฐใซใคใ„ใฆใฏใ€[`~transformers.BartForConditionalGeneration`] ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +[[autodoc]] BlenderbotForConditionalGeneration + - forward + +## BlenderbotForCausalLM + +[[autodoc]] BlenderbotForCausalLM + - forward + +## TFBlenderbotModel + +[[autodoc]] TFBlenderbotModel + - call + +## TFBlenderbotForConditionalGeneration + +[[autodoc]] TFBlenderbotForConditionalGeneration + - call + +## FlaxBlenderbotModel + +[[autodoc]] FlaxBlenderbotModel + - __call__ + - encode + - decode + +## FlaxBlenderbotForConditionalGeneration + +[[autodoc]] FlaxBlenderbotForConditionalGeneration + - __call__ + - encode + - decode diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip-2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip-2.md new file mode 100644 index 0000000000000000000000000000000000000000..52a092ac9ae650256c6252659ab66aaeedee5264 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip-2.md @@ -0,0 +1,90 @@ + + +# BLIP-2 + +## Overview + +BLIP-2 ใƒขใƒ‡ใƒซใฏใ€[BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models](https://huggingface.co/papers/2301.12597) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Junnan Li, Dongxu Li, Silvio Savarese, Steven Hoi.ใƒปใ‚ตใƒใƒฌใƒผใ‚ผใ€ใ‚นใƒ†ใ‚ฃใƒผใƒ–ใƒณใƒปใƒ›ใ‚คใ€‚ BLIP-2 ใฏใ€่ปฝ้‡ใฎ 12 ๅฑค Transformer ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใงใ€ใƒ•ใƒชใƒผใ‚บใ•ใ‚ŒใŸไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ็”ปๅƒใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซ (LLM) ใ‚’ๆดป็”จใ—ใพใ™ใ€‚ +ใใ‚Œใ‚‰ใฎ้–“ใซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’้…็ฝฎใ—ใ€ใ•ใพใ–ใพใช่ฆ–่ฆš่จ€่ชžใ‚ฟใ‚นใ‚ฏใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ๆœ€ใ‚‚ๆณจ็›ฎใ™ในใ็‚นใฏใ€BLIP-2 ใŒ 800 ๅ„„ใƒ‘ใƒฉใƒกใƒผใ‚ฟ ใƒขใƒ‡ใƒซใงใ‚ใ‚‹ [Flamingo](https://huggingface.co/papers/2204.14198) ใ‚’ 8.7% ๆ”นๅ–„ใ—ใฆใ„ใ‚‹ใ“ใจใงใ™ใ€‚ +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ VQAv2 ใงใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒ 54 ๅˆ†ใฎ 1 ใซๆธ›ๅฐ‘ใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ‚ˆใ‚Šใ€่ฆ–่ฆšใจ่จ€่ชžใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใ‚ณใ‚นใƒˆใฏใพใ™ใพใ™ๆณ•ๅค–ใชใ‚‚ใฎใซใชใฃใฆใใฆใ„ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๅธ‚่ฒฉใฎๅ‡็ตๆธˆใฟไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”ปๅƒใ‚จใƒณใ‚ณใƒผใƒ€ใจๅ‡็ตใ•ใ‚ŒใŸๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซใ‹ใ‚‰่ฆ–่ฆš่จ€่ชžใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใƒ–ใƒผใƒˆใ‚นใƒˆใƒฉใƒƒใƒ—ใ™ใ‚‹ใ€ๆฑŽ็”จ็š„ใงๅŠน็އ็š„ใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆˆฆ็•ฅใงใ‚ใ‚‹ BLIP-2 ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ BLIP-2 ใฏใ€2 ๆฎต้šŽใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่ปฝ้‡ใฎ Querying Transformer ใงใƒขใƒ€ใƒชใƒ†ใ‚ฃใฎใ‚ฎใƒฃใƒƒใƒ—ใ‚’ๆฉ‹ๆธกใ—ใ—ใพใ™ใ€‚ๆœ€ๅˆใฎใ‚นใƒ†ใƒผใ‚ธใงใฏใ€ใƒ•ใƒชใƒผใ‚บใ•ใ‚ŒใŸ็”ปๅƒใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‹ใ‚‰ๅญฆ็ฟ’ใ™ใ‚‹่ฆ–่ฆš่จ€่ชž่กจ็พใ‚’ใƒ–ใƒผใƒˆใ‚นใƒˆใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚็ฌฌ 2 ๆฎต้šŽใงใฏใ€ๅ‡็ตใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใ‹ใ‚‰่ฆ–่ฆšใ‹ใ‚‰่จ€่ชžใธใฎ็”Ÿๆˆๅญฆ็ฟ’ใ‚’ใƒ–ใƒผใƒˆใ‚นใƒˆใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ BLIP-2 ใฏใ€ๆ—ขๅญ˜ใฎๆ–นๆณ•ใ‚ˆใ‚Šใ‚‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒๅคงๅน…ใซๅฐ‘ใชใ„ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€ใ•ใพใ–ใพใช่ฆ–่ฆš่จ€่ชžใ‚ฟใ‚นใ‚ฏใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€็งใŸใกใฎใƒขใƒ‡ใƒซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒ 54 ๅˆ†ใฎ 1 ๅฐ‘ใชใ„ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ VQAv2 ใงใ€Flamingo80B ใ‚’ 8.7% ไธŠๅ›žใฃใฆใ„ใพใ™ใ€‚ใพใŸใ€่‡ช็„ถ่จ€่ชžใฎๅ‘ฝไปคใซๅพ“ใ†ใ“ใจใŒใงใใ‚‹ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎ็”Ÿๆˆใจใ„ใ†ใƒขใƒ‡ใƒซใฎๆ–ฐใ—ใ„ๆฉŸ่ƒฝใ‚‚ๅฎŸ่จผใ—ใพใ™* + + + + BLIP-2 ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/salesforce/LAVIS/tree/5ee63d688ba4cebff63acee04adaef2dee9af207) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- BLIP-2 ใฏใ€็”ปๅƒใจใ‚ชใƒ—ใ‚ทใƒงใƒณใฎใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆŒ‡ๅฎšใ—ใฆๆกไปถไป˜ใใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚ๆŽจ่ซ–ๆ™‚ใซใฏใ€ [`generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +- [`Blip2Processor`] ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซ็”จใฎ็”ปๅƒใ‚’ๆบ–ๅ‚™ใ—ใ€ไบˆๆธฌใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +## Resources + +BLIP-2 ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + +- ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ€ใƒ“ใ‚ธใƒฅใ‚ขใƒซ่ณชๅ•ๅฟœ็ญ” (VQA)ใ€ใŠใ‚ˆใณใƒใƒฃใƒƒใƒˆใฎใ‚ˆใ†ใชไผš่ฉฑใฎใŸใ‚ใฎ BLIP-2 ใฎใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏใ€[ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/BLIP-2) ใซใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## Blip2Config + +[[autodoc]] Blip2Config + - from_vision_qformer_text_configs + +## Blip2VisionConfig + +[[autodoc]] Blip2VisionConfig + +## Blip2QFormerConfig + +[[autodoc]] Blip2QFormerConfig + +## Blip2Processor + +[[autodoc]] Blip2Processor + +## Blip2VisionModel + +[[autodoc]] Blip2VisionModel + - forward + +## Blip2QFormerModel + +[[autodoc]] Blip2QFormerModel + - forward + +## Blip2Model + +[[autodoc]] Blip2Model + - forward + - get_text_features + - get_image_features + - get_qformer_features + +## Blip2ForConditionalGeneration + +[[autodoc]] Blip2ForConditionalGeneration + - forward + - generate \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip.md new file mode 100644 index 0000000000000000000000000000000000000000..e93c740883ab90d778629ac8b886f5cd1b1e4c03 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/blip.md @@ -0,0 +1,139 @@ + + +# BLIP + +## Overview + +BLIP ใƒขใƒ‡ใƒซใฏใ€[BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation](https://huggingface.co/papers/2201.12086) ใง Junnan Liใ€Dongxu Liใ€Caiming Xiongใ€Steven Hoi ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ ใ€‚ + +BLIP ใฏใ€ๆฌกใฎใ‚ˆใ†ใชใ•ใพใ–ใพใชใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใ‚ฟใ‚นใ‚ฏใ‚’ๅฎŸ่กŒใงใใ‚‹ใƒขใƒ‡ใƒซใงใ™ใ€‚ +- ่ฆ–่ฆš็š„ใช่ณชๅ•ๅฟœ็ญ” +- ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎๆคœ็ดข๏ผˆ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎใƒžใƒƒใƒใƒณใ‚ฐ๏ผ‰ +- ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*่ฆ–่ฆš่จ€่ชžไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ (VLP) ใซใ‚ˆใ‚Šใ€ๅคšใใฎ่ฆ–่ฆš่จ€่ชžใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ—ใพใ—ใŸใ€‚ +ใŸใ ใ—ใ€ๆ—ขๅญ˜ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใฎใปใจใ‚“ใฉใฏใ€็†่งฃใƒ™ใƒผใ‚นใฎใ‚ฟใ‚นใ‚ฏใพใŸใฏไธ–ไปฃใƒ™ใƒผใ‚นใฎใ‚ฟใ‚นใ‚ฏใฎใ„ใšใ‚Œใ‹ใงใฎใฟๅ„ชใ‚Œใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซใ€ๆœ€้ฉใงใฏใชใ„็›ฃ่ฆ–ใ‚ฝใƒผใ‚นใงใ‚ใ‚‹ Web ใ‹ใ‚‰ๅŽ้›†ใ•ใ‚ŒใŸใƒŽใ‚คใ‚บใฎๅคšใ„็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎใƒšใ‚ขใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใ‚นใ‚ฑใƒผใƒซใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใงใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใŒๅคงๅน…ใซ้”ๆˆใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€่ฆ–่ฆš่จ€่ชžใฎ็†่งฃใจ็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใฎไธกๆ–นใซๆŸ”่ปŸใซ็งป่กŒใ™ใ‚‹ๆ–ฐใ—ใ„ VLP ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใ‚ใ‚‹ BLIP ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ BLIP ใฏใ€ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ใƒ–ใƒผใƒˆใ‚นใƒˆใƒฉใƒƒใƒ—ใ™ใ‚‹ใ“ใจใงใƒŽใ‚คใ‚บใฎๅคšใ„ Web ใƒ‡ใƒผใ‚ฟใ‚’ๅŠนๆžœ็š„ใซๅˆฉ็”จใ—ใพใ™ใ€‚ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒŠใƒผใŒๅˆๆˆใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใ—ใ€ใƒ•ใ‚ฃใƒซใ‚ฟใƒผใŒใƒŽใ‚คใ‚บใฎๅคšใ„ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’้™คๅŽปใ—ใพใ™ใ€‚็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆๆคœ็ดข (ๅนณๅ‡ๅ†็พ็އ +2.7%@1)ใ€็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณไฝœๆˆ (CIDEr ใง +2.8%)ใ€VQA ( VQA ใ‚นใ‚ณใ‚ขใฏ +1.6%)ใ€‚ BLIP ใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆๆ–นๅผใงใƒ“ใƒ‡ใ‚ช่จ€่ชžใ‚ฟใ‚นใ‚ฏใซ็›ดๆŽฅ่ปข้€ใ—ใŸๅ ดๅˆใซใ‚‚ใ€ๅผทๅŠ›ใชไธ€่ˆฌๅŒ–่ƒฝๅŠ›ใ‚’็™บๆฎใ—ใพใ™ใ€‚ใ‚ณใƒผใƒ‰ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใฆใ„ใพใ™ใ€‚* + +![BLIP.gif](https://cdn-uploads.huggingface.co/production/uploads/1670928184033-62441d1d9fdefb55a0b7d12c.gif) + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [ybelkada](https://huggingface.co/ybelkada) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/salesforce/BLIP) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +- [Jupyter ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb) ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ็”จใซ BLIP ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ• + +## BlipConfig + +[[autodoc]] BlipConfig + - from_text_vision_configs + +## BlipTextConfig + +[[autodoc]] BlipTextConfig + +## BlipVisionConfig + +[[autodoc]] BlipVisionConfig + +## BlipProcessor + +[[autodoc]] BlipProcessor + +## BlipImageProcessor + +[[autodoc]] BlipImageProcessor + - preprocess + +## BlipImageProcessorFast + +[[autodoc]] BlipImageProcessorFast + - preprocess + + + + +## BlipModel + +[[autodoc]] BlipModel + - forward + - get_text_features + - get_image_features + +## BlipTextModel + +[[autodoc]] BlipTextModel + - forward + +## BlipVisionModel + +[[autodoc]] BlipVisionModel + - forward + +## BlipForConditionalGeneration + +[[autodoc]] BlipForConditionalGeneration + - forward + +## BlipForImageTextRetrieval + +[[autodoc]] BlipForImageTextRetrieval + - forward + +## BlipForQuestionAnswering + +[[autodoc]] BlipForQuestionAnswering + - forward + + + + +## TFBlipModel + +[[autodoc]] TFBlipModel + - call + - get_text_features + - get_image_features + +## TFBlipTextModel + +[[autodoc]] TFBlipTextModel + - call + +## TFBlipVisionModel + +[[autodoc]] TFBlipVisionModel + - call + +## TFBlipForConditionalGeneration + +[[autodoc]] TFBlipForConditionalGeneration + - call + +## TFBlipForImageTextRetrieval + +[[autodoc]] TFBlipForImageTextRetrieval + - call + +## TFBlipForQuestionAnswering + +[[autodoc]] TFBlipForQuestionAnswering + - call + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bloom.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bloom.md new file mode 100644 index 0000000000000000000000000000000000000000..f7cb66ab953288b2b840b1986d57a4ad84919bc1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bloom.md @@ -0,0 +1,107 @@ + + +# BLOOM + +## Overview + +BLOOM ใƒขใƒ‡ใƒซใฏใ€[BigScience Workshop](https://bigscience.huggingface.co/) ใ‚’้€šใ˜ใฆใ•ใพใ–ใพใชใƒใƒผใ‚ธใƒงใƒณใงๆๆกˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ BigScience ใฏใ€็ ”็ฉถ่€…ใŒๆ™‚้–“ใจใƒชใ‚ฝใƒผใ‚นใ‚’ใƒ—ใƒผใƒซใ—ใฆๅ…ฑๅŒใงใ‚ˆใ‚Š้ซ˜ใ„ๅŠนๆžœใ‚’้”ๆˆใ™ใ‚‹ไป–ใฎใ‚ชใƒผใƒ—ใƒณ ใ‚ตใ‚คใ‚จใƒณใ‚น ใ‚คใƒ‹ใ‚ทใ‚ขใƒใƒ–ใ‹ใ‚‰ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใฆใ„ใพใ™ใ€‚ +BLOOM ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏๅŸบๆœฌ็š„ใซ GPT3 (ๆฌกใฎใƒˆใƒผใ‚ฏใƒณไบˆๆธฌใฎใŸใ‚ใฎ่‡ชๅทฑๅ›žๅธฐใƒขใƒ‡ใƒซ) ใซไผผใฆใ„ใพใ™ใŒใ€46 ใฎ็•ฐใชใ‚‹่จ€่ชžใจ 13 ใฎใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎใ„ใใคใ‹ใฎๅฐใ•ใ„ใƒใƒผใ‚ธใƒงใƒณใŒๅŒใ˜ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ BLOOM ใฏๆฌกใฎใƒใƒผใ‚ธใƒงใƒณใงๅˆฉ็”จใงใใพใ™ใ€‚ + +- [bloom-560m](https://huggingface.co/bigscience/bloom-560m) +- [bloom-1b1](https://huggingface.co/bigscience/bloom-1b1) +- [bloom-1b7](https://huggingface.co/bigscience/bloom-1b7) +- [bloom-3b](https://huggingface.co/bigscience/bloom-3b) +- [bloom-7b1](https://huggingface.co/bigscience/bloom-7b1) +- [bloom](https://huggingface.co/bigscience/bloom) (176B parameters) + +## Resources + +BLOOM ใ‚’ไฝฟใ„ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + + + +- [`BloomForCausalLM`] ใ“ใ‚Œใซใ‚ˆใฃใฆใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ [causal language modeling example script](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#gpt-2gpt-and-causal-language-modeling) and [notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb). + +ไปฅไธ‹ใ‚‚ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) + + +โšก๏ธ ๆŽจ่ซ– +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ [ๆœ€้ฉๅŒ–ใฎ่ฉฑ: ใƒ–ใƒซใƒผใƒ ๆŽจ่ซ–](https://huggingface.co/blog/bloom-inference-optimization)ใ€‚ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ [DeepSpeed ใจ Accelerate ใ‚’ไฝฟ็”จใ—ใŸไฟกใ˜ใ‚‰ใ‚Œใชใ„ใปใฉ้ซ˜้€Ÿใช BLOOM ๆŽจ่ซ–](https://huggingface.co/blog/bloom-inference-pytorch-scripts)ใ€‚ + +โš™๏ธใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ +- ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ [BLOOM ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่ƒŒๅพŒใซใ‚ใ‚‹ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผ](https://huggingface.co/blog/bloom-megatron-deepspeed)ใ€‚ + +## BloomConfig + +[[autodoc]] BloomConfig + - all + +## BloomTokenizerFast + +[[autodoc]] BloomTokenizerFast + - all + + + + + +## BloomModel + +[[autodoc]] BloomModel + - forward + +## BloomForCausalLM + +[[autodoc]] BloomForCausalLM + - forward + +## BloomForSequenceClassification + +[[autodoc]] BloomForSequenceClassification + - forward + +## BloomForTokenClassification + +[[autodoc]] BloomForTokenClassification + - forward + +## BloomForQuestionAnswering + +[[autodoc]] BloomForQuestionAnswering + - forward + + + + +## FlaxBloomModel + +[[autodoc]] FlaxBloomModel + - __call__ + +## FlaxBloomForCausalLM + +[[autodoc]] FlaxBloomForCausalLM + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bort.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bort.md new file mode 100644 index 0000000000000000000000000000000000000000..185187219e74f6e036e50c20297b37750230b74d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bort.md @@ -0,0 +1,55 @@ + + +# BORT + + + +ใ“ใฎใƒขใƒ‡ใƒซใฏใƒกใƒณใƒ†ใƒŠใƒณใ‚น ใƒขใƒผใƒ‰ใฎใฟใงใ‚ใ‚Šใ€ใ‚ณใƒผใƒ‰ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๆ–ฐใ—ใ„ PR ใฏๅ—ใ‘ไป˜ใ‘ใ‚‰ใ‚Œใพใ›ใ‚“ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฎๅฎŸ่กŒไธญใซๅ•้กŒใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใŸๆœ€ๅพŒใฎใƒใƒผใ‚ธใƒงใƒณ (v4.30.0) ใ‚’ๅ†ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„ใ€‚ +ใ“ใ‚Œใ‚’่กŒใ†ใซใฏใ€ใ‚ณใƒžใƒณใƒ‰ `pip install -U Transformers==4.30.0` ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + + + +## Overview + +BORT ใƒขใƒ‡ใƒซใฏใ€[Optimal Subarchitecture Extraction for BERT](https://huggingface.co/papers/2010.10499) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Adrian de Wynter and Daniel J. Perry.ใ“ใ‚Œใฏใ€BERT ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆœ€้ฉใชใ‚ตใƒ–ใ‚ปใƒƒใƒˆใงใ™ใ€‚ +่‘—่€…ใฏใ€Œใƒœใƒซใƒˆใ€ใจๅ‘ผใ‚“ใงใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*Devlin ใ‚‰ใ‹ใ‚‰ BERT ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆœ€้ฉใชใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ (2018) +ใƒ‹ใƒฅใƒผใƒฉใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃๆคœ็ดขใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใซใŠใ‘ใ‚‹ๆœ€่ฟ‘ใฎ็”ปๆœŸ็š„ใชๆŠ€่ก“ใ‚’้ฉ็”จใ—ใพใ™ใ€‚ใ“ใฎๆœ€้ฉใชใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ๆฌกใฎใ‚ˆใ†ใซๅ‘ผใณใพใ™ใ€‚ +"Bort" ใฏๆ˜Žใ‚‰ใ‹ใซๅฐใ•ใใ€ๆœ‰ๅŠน (ใคใพใ‚Šใ€ๅŸ‹ใ‚่พผใฟๅฑคใ‚’่€ƒๆ…ฎใ—ใชใ„) ใ‚ตใ‚คใ‚บใฏ 5.5% ใงใ™ใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎ BERT ๅคง่ฆๆจกใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€ใŠใ‚ˆใณใƒใƒƒใƒˆ ใ‚ตใ‚คใ‚บใฎ 16%ใ€‚ Bort ใฏ 288 GPU ๆ™‚้–“ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ๆœ€้ซ˜ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎ BERT ใƒ‘ใƒฉใƒกใƒˆใƒชใƒƒใ‚ฏ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ ใƒใƒชใ‚ขใƒณใƒˆใงใ‚ใ‚‹ RoBERTa-large ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฟ…่ฆใชๆ™‚้–“ใฎ 1.2% +(Liu et al., 2019)ใ€ๅŒใ˜ใƒžใ‚ทใƒณใง BERT-large ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใฎใซๅฟ…่ฆใช GPU ๆ™‚้–“ใฎไธ–็•Œ่จ˜้Œฒใฎ็ด„ 33% +ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ€‚ใพใŸใ€CPU ไธŠใง 7.9 ๅ€้ซ˜้€Ÿใงใ‚ใ‚‹ใ ใ‘ใงใชใใ€ไป–ใฎๅœง็ธฎใƒใƒผใ‚ธใƒงใƒณใ‚ˆใ‚Šใ‚‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ„ชใ‚Œใฆใ„ใพใ™ใ€‚ +ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€ใŠใ‚ˆใณไธ€้ƒจใฎ้žๅœง็ธฎใƒใƒชใ‚ขใƒณใƒˆ: 0.3% ๏ฝž 31% ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๅ‘ไธŠใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ +BERT-large ใซ้–ขใ—ใฆใ€่ค‡ๆ•ฐใฎๅ…ฌ้–‹่‡ช็„ถ่จ€่ชž็†่งฃ (NLU) ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใŠใ‘ใ‚‹็ตถๅฏพ็š„ใช่ฉ•ไพกใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [stefan-it](https://huggingface.co/stefan-it) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ[ใ“ใ“](https://github.com/alexa/bort/)ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- BORT ใฎใƒขใƒ‡ใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏ BERT ใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[BERT ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒšใƒผใ‚ธ](bert) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + ใƒขใƒ‡ใƒซใฎ API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใจไฝฟ็”จไพ‹ใ€‚ +- BORT ใฏ BERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎไปฃใ‚ใ‚Šใซ RoBERTa ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใจไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€[RoBERTa ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒšใƒผใ‚ธ](roberta) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- BORT ใซใฏใ€ [Agora](https://adewynter.github.io/notes/bort_algorithms_and_applications.html#fine-tuning-with-algebraic-topology) ใจๅ‘ผใฐใ‚Œใ‚‹็‰นๅฎšใฎๅพฎ่ชฟๆ•ดใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใŒๅฟ…่ฆใงใ™ใ€‚ + ๆฎ‹ๅฟตใชใŒใ‚‰ใพใ ใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นๅŒ–ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚่ชฐใ‹ใŒๅฎŸ่ฃ…ใ—ใ‚ˆใ†ใจใ™ใ‚‹ใจใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใซใจใฃใฆ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ + BORT ใฎๅพฎ่ชฟๆ•ดใ‚’ๆฉŸ่ƒฝใ•ใ›ใ‚‹ใŸใ‚ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ€‚ \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bridgetower.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bridgetower.md new file mode 100644 index 0000000000000000000000000000000000000000..116d87caa5f400c6fc19dee9c1cd43577b1b7096 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bridgetower.md @@ -0,0 +1,176 @@ + + +# BridgeTower + +## Overview + +BridgeTower ใƒขใƒ‡ใƒซใฏใ€Xiao Xuใ€Chenfei Wuใ€Shachar Rosenmanใ€Vasudev Lalใ€Wanxiang Cheใ€Nan Duan [BridgeTower: Building Bridges Between Encoders in Vision-Language Representative Learning](https://huggingface.co/papers/2206.08657) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใƒ‰ใ‚ฅใ‚ขใƒณใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎ็›ฎๆจ™ใฏใ€ +ๅ„ใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใจใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใฎ้–“ใฎใƒ–ใƒชใƒƒใ‚ธใซใ‚ˆใ‚Šใ€ใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใฎๅ„ๅฑคใงใฎๅŒ…ๆ‹ฌ็š„ใ‹ใค่ฉณ็ดฐใชๅฏพ่ฉฑใŒๅฏ่ƒฝใซใชใ‚Šใ€่ฟฝๅŠ ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจ่จˆ็ฎ—ใ‚ณใ‚นใƒˆใŒใปใจใ‚“ใฉ็„ก่ฆ–ใงใใ‚‹็จ‹ๅบฆใงใ€ใ•ใพใ–ใพใชไธ‹ๆตใ‚ฟใ‚นใ‚ฏใงๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ + +ใ“ใฎ่ซ–ๆ–‡ใฏ [AAAI'23](https://aaai.org/Conferences/AAAI-23/) ไผš่ญฐใซๆŽกๆŠžใ•ใ‚Œใพใ—ใŸใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*TWO-TOWER ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๅ‚™ใˆใŸใƒ“ใ‚ธใƒงใƒณ่จ€่ชž (VL) ใƒขใƒ‡ใƒซใฏใ€่ฟ‘ๅนดใฎ่ฆ–่ฆš่จ€่ชž่กจ็พๅญฆ็ฟ’ใฎไธปๆตใจใชใฃใฆใ„ใพใ™ใ€‚ +็พๅœจใฎ VL ใƒขใƒ‡ใƒซใฏใ€่ปฝ้‡ใฎใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใ‚ฃใƒผใƒ— ใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใงไธกๆ–นใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‚’ๅŒๆ™‚ใซๆŠฝๅ‡บใ€ไฝ็ฝฎๅˆใ‚ใ›ใ€่žๅˆใ™ใ‚‹ใ“ใจใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ‹ใ€ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‡ใ‚ฃใƒผใƒ— ใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‹ใ‚‰ๆœ€็ต‚ๅฑคใฎใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ่กจ็พใ‚’ไธŠ้ƒจใฎใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€‚ +ใฉใกใ‚‰ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚‚ใ€่ฆ–่ฆš่จ€่ชž่กจ็พใฎๅญฆ็ฟ’ใ‚’ๅˆถ้™ใ—ใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅˆถ้™ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใฎๆœ€ไธŠไฝๅฑคใจใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใฎๅ„ๅฑคใฎ้–“ใฎๆŽฅ็ถšใ‚’ๆง‹็ฏ‰ใ™ใ‚‹่ค‡ๆ•ฐใฎใƒ–ใƒชใƒƒใ‚ธๅฑคใ‚’ๅฐŽๅ…ฅใ™ใ‚‹ BRIDGETOWER ใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅŠนๆžœ็š„ใชใƒœใƒˆใƒ ใ‚ขใƒƒใƒ—ใฎใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ่ชฟๆ•ดใจใ€ใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๅ†…ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎใ•ใพใ–ใพใชใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใƒฌใƒ™ใƒซใฎ่ฆ–่ฆš่กจ็พใจใƒ†ใ‚ญใ‚นใƒˆ่กจ็พใฎ้–“ใฎ่žๅˆใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ BRIDGETOWER ใฏ 4M ็”ปๅƒใฎใฟใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใฆใŠใ‚Šใ€ใ•ใพใ–ใพใชไธ‹ๆตใฎ่ฆ–่ฆš่จ€่ชžใ‚ฟใ‚นใ‚ฏใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ +็‰นใซใ€VQAv2 ใƒ†ใ‚นใƒˆๆจ™ๆบ–ใ‚ปใƒƒใƒˆใงใฏใ€BRIDGETOWER ใฏ 78.73% ใฎ็ฒพๅบฆใ‚’้”ๆˆใ—ใ€ๅŒใ˜ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใจใปใผ็„ก่ฆ–ใงใใ‚‹่ฟฝๅŠ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใจ่จˆ็ฎ—ใ‚ณใ‚นใƒˆใงไปฅๅ‰ใฎๆœ€ๅ…ˆ็ซฏใƒขใƒ‡ใƒซ METER ใ‚’ 1.09% ไธŠๅ›žใ‚Šใพใ—ใŸใ€‚ +็‰นใซใ€ใƒขใƒ‡ใƒซใ‚’ใ•ใ‚‰ใซใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ™ใ‚‹ใจใ€BRIDGETOWER ใฏ 81.15% ใฎ็ฒพๅบฆใ‚’้”ๆˆใ—ใ€ๆก้•ใ„ใซๅคงใใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ไธŠๅ›žใ‚Šใพใ—ใŸใ€‚* + + + + ใƒ–ใƒชใƒƒใ‚ธใ‚ฟใƒฏใƒผ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[Anahita Bhiwandiwalla](https://huggingface.co/anahita-b)ใ€[Tiep Le](https://huggingface.co/Tile)ใ€[Shaoyen Tseng](https://huggingface.co/shaoyent) ใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/BridgeTower) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips and examples + +BridgeTower ใฏใ€ใƒ“ใ‚ธใƒฅใ‚ขใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€ใƒ†ใ‚ญใ‚นใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€ใŠใ‚ˆใณ่ค‡ๆ•ฐใฎ่ปฝ้‡ใƒ–ใƒชใƒƒใ‚ธ ใƒฌใ‚คใƒคใƒผใ‚’ๅ‚™ใˆใŸใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใงๆง‹ๆˆใ•ใ‚Œใพใ™ใ€‚ +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฎ็›ฎๆจ™ใฏใ€ๅ„ใƒฆใƒ‹ใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้–“ใซใƒ–ใƒชใƒƒใ‚ธใ‚’ๆง‹็ฏ‰ใ—ใ€ใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎๅ„ๅฑคใงๅŒ…ๆ‹ฌ็š„ใ‹ใค่ฉณ็ดฐใชๅฏพ่ฉฑใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใ“ใจใงใ—ใŸใ€‚ +ๅŽŸๅ‰‡ใจใ—ใฆใ€ๆๆกˆใ•ใ‚ŒใŸใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใฏใ€ไปปๆ„ใฎใƒ“ใ‚ธใƒฅใ‚ขใƒซใ€ใƒ†ใ‚ญใ‚นใƒˆใ€ใพใŸใฏใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’้ฉ็”จใงใใพใ™ใ€‚ + +[`BridgeTowerProcessor`] ใฏใ€[`RobertaTokenizer`] ใจ [`BridgeTowerImageProcessor`] ใ‚’ๅ˜ไธ€ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซใƒฉใƒƒใƒ—ใ—ใ€ไธกๆ–นใฎๆฉŸ่ƒฝใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใ€็”ปๅƒใ‚’ใใ‚Œใžใ‚Œ็”จๆ„ใ—ใพใ™ใ€‚ + +ๆฌกใฎไพ‹ใฏใ€[`BridgeTowerProcessor`] ใจ [`BridgeTowerForContrastiveLearning`] ใ‚’ไฝฟ็”จใ—ใฆๅฏพ็…งๅญฆ็ฟ’ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> from transformers import BridgeTowerProcessor, BridgeTowerForContrastiveLearning +>>> import requests +>>> from PIL import Image + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> texts = ["An image of two cats chilling on a couch", "A football player scoring a goal"] + +>>> processor = BridgeTowerProcessor.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-itc") +>>> model = BridgeTowerForContrastiveLearning.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-itc") + +>>> # forward pass +>>> scores = dict() +>>> for text in texts: +... # prepare inputs +... encoding = processor(image, text, return_tensors="pt") +... outputs = model(**encoding) +... scores[text] = outputs +``` + +ๆฌกใฎไพ‹ใฏใ€[`BridgeTowerProcessor`] ใจ [`BridgeTowerForImageAndTextRetrieval`] ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆใฎๅ–ๅพ—ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> from transformers import BridgeTowerProcessor, BridgeTowerForImageAndTextRetrieval +>>> import requests +>>> from PIL import Image + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> texts = ["An image of two cats chilling on a couch", "A football player scoring a goal"] + +>>> processor = BridgeTowerProcessor.from_pretrained("BridgeTower/bridgetower-base-itm-mlm") +>>> model = BridgeTowerForImageAndTextRetrieval.from_pretrained("BridgeTower/bridgetower-base-itm-mlm") + +>>> # forward pass +>>> scores = dict() +>>> for text in texts: +... # prepare inputs +... encoding = processor(image, text, return_tensors="pt") +... outputs = model(**encoding) +... scores[text] = outputs.logits[0, 1].item() +``` + +ๆฌกใฎไพ‹ใฏใ€[`BridgeTowerProcessor`] ใจ [`BridgeTowerForMaskedLM`] ใ‚’ไฝฟ็”จใ—ใฆใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> from transformers import BridgeTowerProcessor, BridgeTowerForMaskedLM +>>> from PIL import Image +>>> import requests + +>>> url = "http://images.cocodataset.org/val2017/000000360943.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw).convert("RGB") +>>> text = "a looking out of the window" + +>>> processor = BridgeTowerProcessor.from_pretrained("BridgeTower/bridgetower-base-itm-mlm") +>>> model = BridgeTowerForMaskedLM.from_pretrained("BridgeTower/bridgetower-base-itm-mlm") + +>>> # prepare inputs +>>> encoding = processor(image, text, return_tensors="pt") + +>>> # forward pass +>>> outputs = model(**encoding) + +>>> results = processor.decode(outputs.logits.argmax(dim=-1).squeeze(0).tolist()) + +>>> print(results) +.a cat looking out of the window. +``` + +ใƒใƒƒใƒ—๏ผš + +- BridgeTower ใฎใ“ใฎๅฎŸ่ฃ…ใงใฏใ€[`RobertaTokenizer`] ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆๅŸ‹ใ‚่พผใฟใ‚’็”Ÿๆˆใ—ใ€OpenAI ใฎ CLIP/ViT ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ฆ–่ฆš็š„ๅŸ‹ใ‚่พผใฟใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ +- ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ [bridgeTower-base](https://huggingface.co/BridgeTower/bridgetower-base) ใŠใ‚ˆใณ [bridgetower ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใจ็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆ ใƒžใƒƒใƒใƒณใ‚ฐ](https://huggingface.co/BridgeTower/bridgetower--base-itm-mlm) ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ ใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ—ใŸใ€‚ +- ็”ปๅƒๆคœ็ดขใŠใ‚ˆใณใใฎไป–ใฎไธ‹ๆตใ‚ฟใ‚นใ‚ฏใซใŠใ‘ใ‚‹ BridgeTower ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซใคใ„ใฆใฏใ€[่กจ 5](https://huggingface.co/papers/2206.08657) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +- ใ“ใฎใƒขใƒ‡ใƒซใฎ PyTorch ใƒใƒผใ‚ธใƒงใƒณใฏใ€torch 1.10 ไปฅ้™ใงใฎใฟไฝฟ็”จใงใใพใ™ใ€‚ + +## BridgeTowerConfig + +[[autodoc]] BridgeTowerConfig + +## BridgeTowerTextConfig + +[[autodoc]] BridgeTowerTextConfig + +## BridgeTowerVisionConfig + +[[autodoc]] BridgeTowerVisionConfig + +## BridgeTowerImageProcessor + +[[autodoc]] BridgeTowerImageProcessor + - preprocess + +## BridgeTowerImageProcessorFast + +[[autodoc]] BridgeTowerImageProcessorFast + - preprocess + +## BridgeTowerProcessor + +[[autodoc]] BridgeTowerProcessor + - __call__ + +## BridgeTowerModel + +[[autodoc]] BridgeTowerModel + - forward + +## BridgeTowerForContrastiveLearning + +[[autodoc]] BridgeTowerForContrastiveLearning + - forward + +## BridgeTowerForMaskedLM + +[[autodoc]] BridgeTowerForMaskedLM + - forward + +## BridgeTowerForImageAndTextRetrieval + +[[autodoc]] BridgeTowerForImageAndTextRetrieval + - forward + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bros.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bros.md new file mode 100644 index 0000000000000000000000000000000000000000..58b59e2524610d6c03ba7fbb042c270ae686e07a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/bros.md @@ -0,0 +1,113 @@ + + +# BROS + +## Overview + +BROS ใƒขใƒ‡ใƒซใฏใ€Teakgyu Honใ€Donghyun Kimใ€Mingi Ji, Wonseok Hwang, Daehyun Nam, Sungrae Park ใซใ‚ˆใฃใฆ [BROS: A Pre-trained Language Model Focusing on Text and Layout for Better Key Information Extraction from Documents](https://huggingface.co/papers/2108.04539) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ + +BROS ใฏ *BERT Relying On Spatality* ใฎ็•ฅใงใ™ใ€‚ใ“ใ‚Œใฏใ€ไธ€้€ฃใฎใƒˆใƒผใ‚ฏใƒณใจใใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใ€ไธ€้€ฃใฎ้š ใ‚Œ็Šถๆ…‹ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จใฎ Transformer ใƒขใƒ‡ใƒซใงใ™ใ€‚ BROS ใฏใ€็ตถๅฏพ็š„ใช็ฉบ้–“ๆƒ…ๅ ฑใ‚’ไฝฟ็”จใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€็›ธๅฏพ็š„ใช็ฉบ้–“ๆƒ…ๅ ฑใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใพใ™ใ€‚ + +BERT ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใƒˆใƒผใ‚ฏใƒณใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็›ฎๆจ™ (TMLM) ใจๆ–ฐใ—ใ„ใ‚จใƒชใ‚ขใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็›ฎๆจ™ (AMLM) ใฎ 2 ใคใฎ็›ฎๆจ™ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +TMLM ใงใฏใ€ใƒˆใƒผใ‚ฏใƒณใฏใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฏ็ฉบ้–“ๆƒ…ๅ ฑใจไป–ใฎใƒžใ‚นใ‚ฏใ•ใ‚Œใฆใ„ใชใ„ใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ +AMLM ใฏ TMLM ใฎ 2D ใƒใƒผใ‚ธใƒงใƒณใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆ ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ—ใ€TMLM ใจๅŒใ˜ๆƒ…ๅ ฑใงไบˆๆธฌใ—ใพใ™ใŒใ€ใƒ†ใ‚ญใ‚นใƒˆ ใƒ–ใƒญใƒƒใ‚ฏ (้ ˜ๅŸŸ) ใ‚’ใƒžใ‚นใ‚ฏใ—ใพใ™ใ€‚ + +`BrosForTokenClassification`ใซใฏใ€BrosModel ใฎไธŠใซๅ˜็ด”ใช็ทšๅฝขๅฑคใŒใ‚ใ‚Šใพใ™ใ€‚ๅ„ใƒˆใƒผใ‚ฏใƒณใฎใƒฉใƒ™ใƒซใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ +`BrosSpadeEEForTokenClassification`ใซใฏใ€BrosModel ใฎไธŠใซ`initial_token_classifier`ใจ`subsequent_token_classifier`ใŒใ‚ใ‚Šใพใ™ใ€‚ `initial_token_classifier` ใฏๅ„ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎๆœ€ๅˆใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ€`subsequent_token_classifier` ใฏใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ `BrosSpadeELForTokenClassification`ใซใฏ BrosModel ใฎไธŠใซ`entity_linker`ใŒใ‚ใ‚Šใพใ™ใ€‚ `entity_linker` ใฏ 2 ใคใฎใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃ้–“ใฎ้–ขไฟ‚ใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +`BrosForTokenClassification`ใจ`BrosSpadeEEForTokenClassification`ใฏๅŸบๆœฌ็š„ใซๅŒใ˜ใ‚ธใƒงใƒ–ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใŸใ ใ—ใ€`BrosForTokenClassification`ใฏๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณใŒๅฎŒๅ…จใซใ‚ทใƒชใ‚ขใƒซๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’ๅ‰ๆใจใ—ใฆใ„ใพใ™ (ใƒˆใƒผใ‚ฏใƒณใฏ 2D ็ฉบ้–“ใซๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ้žๅธธใซๅ›ฐ้›ฃใชไฝœๆฅญใงใ™)ใ€‚ไธ€ๆ–นใ€`BrosSpadeEEForTokenClassification`ใฏ 1 ใคใฎใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰ๆฌกใฎๆŽฅ็ถšใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใ€ใ‚ทใƒชใ‚ขใƒซๅŒ–ใ‚จใƒฉใƒผใฎๅ‡ฆ็†ใ‚’ใ‚ˆใ‚ŠๆŸ”่ปŸใซ่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ + +`BrosSpadeELForTokenClassification` ใฏใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃๅ†…ใฎใƒชใƒณใ‚ฏ ใ‚ฟใ‚นใ‚ฏใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ 2 ใคใฎใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใŒไฝ•ใ‚‰ใ‹ใฎ้–ขไฟ‚ใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ๅ ดๅˆใ€(ใ‚ใ‚‹ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎ) 1 ใคใฎใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰ (ๅˆฅใฎใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎ) ๅˆฅใฎใƒˆใƒผใ‚ฏใƒณใธใฎ้–ขไฟ‚ใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ + +BROS ใฏใ€ๆ˜Ž็คบ็š„ใช่ฆ–่ฆšๆฉŸ่ƒฝใซไพๅญ˜ใ›ใšใซใ€FUNSDใ€SROIEใ€CORDใ€SciTSR ใชใฉใฎ Key Information Extraction (KIE) ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงๅŒ็ญ‰ไปฅไธŠใฎ็ตๆžœใ‚’้”ๆˆใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๆ–‡ๆ›ธ็”ปๅƒใ‹ใ‚‰ใฎ้‡่ฆๆƒ…ๅ ฑๆŠฝๅ‡บ (KIE) ใซใฏใ€2 ๆฌกๅ…ƒ (2D) ็ฉบ้–“ใซใŠใ‘ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎๆ–‡่„ˆ็š„ใŠใ‚ˆใณ็ฉบ้–“็š„ๆ„ๅ‘ณ่ซ–ใ‚’็†่งฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆœ€่ฟ‘ใฎ็ ”็ฉถใฎๅคšใใฏใ€ๆ–‡ๆ›ธ็”ปๅƒใฎ่ฆ–่ฆš็š„็‰นๅพดใจใƒ†ใ‚ญใ‚นใƒˆใŠใ‚ˆใณใใฎใƒฌใ‚คใ‚ขใ‚ฆใƒˆใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใซ้‡็‚นใ‚’็ฝฎใ„ใŸไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใ‚’้–‹็™บใ™ใ‚‹ใ“ใจใงใ€ใ“ใฎ่ชฒ้กŒใ‚’่งฃๆฑบใ—ใ‚ˆใ†ใจใ—ใฆใ„ใพใ™ใ€‚ไธ€ๆ–นใ€ใ“ใฎใƒšใƒผใƒ‘ใƒผใงใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใจใƒฌใ‚คใ‚ขใ‚ฆใƒˆใฎๅŠนๆžœ็š„ใช็ต„ใฟๅˆใ‚ใ›ใจใ„ใ†ๅŸบๆœฌใซ็ซ‹ใก่ฟ”ใฃใฆใ“ใฎๅ•้กŒใซๅ–ใ‚Š็ต„ใฟใพใ™ใ€‚ๅ…ทไฝ“็š„ใซใฏใ€BROS (BERT Relying On Spatality) ใจใ„ใ†ๅๅ‰ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใ‚’ๆๆกˆใ—ใพใ™ใ€‚ใ“ใฎ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€2D ็ฉบ้–“ๅ†…ใฎใƒ†ใ‚ญใ‚นใƒˆใฎ็›ธๅฏพไฝ็ฝฎใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใ€ใ‚จใƒชใ‚ข ใƒžใ‚นใ‚ญใƒณใ‚ฐๆˆฆ็•ฅใ‚’ไฝฟ็”จใ—ใฆใƒฉใƒ™ใƒซใฎใชใ„ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‹ใ‚‰ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ 2D ็ฉบ้–“ๅ†…ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’็†่งฃใ™ใ‚‹ใŸใ‚ใฎใ“ใฎๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ญใƒผใƒ ใซใ‚ˆใ‚Šใ€BROS ใฏใ€่ฆ–่ฆš็š„ใช็‰นๅพดใซไพๅญ˜ใ™ใ‚‹ใ“ใจใชใใ€4 ใคใฎ KIE ใƒ™ใƒณใƒใƒžใƒผใ‚ฏ (FUNSDใ€SROIE*ใ€CORDใ€ใŠใ‚ˆใณ SciTSR) ใงไปฅๅ‰ใฎๆ–นๆณ•ใจๆฏ”่ผƒใ—ใฆๅŒ็ญ‰ไปฅไธŠใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ—ใพใ—ใŸใ€‚ใพใŸใ€ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€KIE ใ‚ฟใ‚นใ‚ฏใซใŠใ‘ใ‚‹ 2 ใคใฎ็พๅฎŸไธ–็•Œใฎ่ชฒ้กŒ ((1) ้–“้•ใฃใŸใƒ†ใ‚ญใ‚นใƒˆ้ †ๅบใซใ‚ˆใ‚‹ใ‚จใƒฉใƒผใฎๆœ€ๅฐๅŒ–ใ€ใŠใ‚ˆใณ (2) ๅฐ‘ๆ•ฐใฎไธ‹ๆตไพ‹ใ‹ใ‚‰ใฎๅŠน็އ็š„ใชๅญฆ็ฟ’) ใ‚’ๆ˜Žใ‚‰ใ‹ใซใ—ใ€ไปฅๅ‰ใฎๆ–นๆณ•ใซๅฏพใ™ใ‚‹ BROS ใฎๅ„ชไฝๆ€งใ‚’ๅฎŸ่จผใ—ใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [jinho8345](https://huggingface.co/jinho8345) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/clovaai/bros) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips and examples + +- [`~transformers.BrosModel.forward`] ใซใฏใ€`input_ids` ใจ `bbox` (ใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒœใƒƒใ‚ฏใ‚น) ใŒๅฟ…่ฆใงใ™ใ€‚ๅ„ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฏใ€(x0ใ€y0ใ€x1ใ€y1) ๅฝขๅผ (ๅทฆไธŠ้š…ใ€ๅณไธ‹้š…) ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎๅ–ๅพ—ใฏๅค–้ƒจ OCR ใ‚ทใ‚นใƒ†ใƒ ใซไพๅญ˜ใ—ใพใ™ใ€‚ ใ€Œxใ€ๅบงๆจ™ใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็”ปๅƒใฎๅน…ใงๆญฃ่ฆๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใ€Œyใ€ๅบงๆจ™ใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็”ปๅƒใฎ้ซ˜ใ•ใงๆญฃ่ฆๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```python +def expand_and_normalize_bbox(bboxes, doc_width, doc_height): + # here, bboxes are numpy array + + # Normalize bbox -> 0 ~ 1 + bboxes[:, [0, 2]] = bboxes[:, [0, 2]] / width + bboxes[:, [1, 3]] = bboxes[:, [1, 3]] / height +``` + +- [`~transformers.BrosForTokenClassification.forward`ใ€`~transformers.BrosSpadeEEForTokenClassification.forward`ใ€`~transformers.BrosSpadeEEForTokenClassification.forward`] ใงใฏใ€ๆๅคฑ่จˆ็ฎ—ใซ `input_ids` ใจ `bbox` ใ ใ‘ใงใชใ `box_first_token_mask` ใ‚‚ๅฟ…่ฆใงใ™ใ€‚ใ“ใ‚Œใฏใ€ๅ„ใƒœใƒƒใ‚ฏใ‚นใฎๅ…ˆ้ ญไปฅๅค–ใฎใƒˆใƒผใ‚ฏใƒณใ‚’้™คๅค–ใ™ใ‚‹ใŸใ‚ใฎใƒžใ‚นใ‚ฏใงใ™ใ€‚ใ“ใฎใƒžใ‚นใ‚ฏใฏใ€ๅ˜่ชžใ‹ใ‚‰ `input_ids` ใ‚’ไฝœๆˆใ™ใ‚‹ใจใใซๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎ้–‹ๅง‹ใƒˆใƒผใ‚ฏใƒณ ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใงๅ–ๅพ—ใงใใพใ™ใ€‚ๆฌกใฎใ‚ณใƒผใƒ‰ใง`box_first_token_mask`ใ‚’ไฝœๆˆใงใใพใ™ใ€‚ + +```python +def make_box_first_token_mask(bboxes, words, tokenizer, max_seq_length=512): + + box_first_token_mask = np.zeros(max_seq_length, dtype=np.bool_) + + # encode(tokenize) each word from words (list[str]) + input_ids_list: list[list[int]] = [tokenizer.encode(e, add_special_tokens=False) for e in words] + + # get the length of each box + tokens_length_list: list[int] = [len(l) for l in input_ids_list] + + box_end_token_indices = np.array(list(itertools.accumulate(tokens_length_list))) + box_start_token_indices = box_end_token_indices - np.array(tokens_length_list) + + # filter out the indices that are out of max_seq_length + box_end_token_indices = box_end_token_indices[box_end_token_indices < max_seq_length - 1] + if len(box_start_token_indices) > len(box_end_token_indices): + box_start_token_indices = box_start_token_indices[: len(box_end_token_indices)] + + # set box_start_token_indices to True + box_first_token_mask[box_start_token_indices] = True + + return box_first_token_mask + +``` + +## Resources + +- ใƒ‡ใƒข ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏ [ใ“ใกใ‚‰](https://github.com/clovaai/bros) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## BrosConfig + +[[autodoc]] BrosConfig + +## BrosProcessor + +[[autodoc]] BrosProcessor + - __call__ + +## BrosModel + +[[autodoc]] BrosModel + - forward + + +## BrosForTokenClassification + +[[autodoc]] BrosForTokenClassification + - forward + +## BrosSpadeEEForTokenClassification + +[[autodoc]] BrosSpadeEEForTokenClassification + - forward + +## BrosSpadeELForTokenClassification + +[[autodoc]] BrosSpadeELForTokenClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/byt5.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/byt5.md new file mode 100644 index 0000000000000000000000000000000000000000..83f7f0b4ac57c79717fd2fc96c58514b7f2895b7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/byt5.md @@ -0,0 +1,154 @@ + + +# ByT5 + +## Overview + +ByT5 ใƒขใƒ‡ใƒซใฏใ€[ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir +Kale, Adam Roberts, Colin Raffel. + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๆœ€ใ‚‚ๅบƒใไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ๅ˜่ชžใพใŸใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ๅ˜ไฝใซๅฏพๅฟœใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใงๅ‹•ไฝœใ—ใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใจใ—ใฆใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒๅฟ…่ฆใงใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏ้€šๅธธใ€ +ใƒขใƒ‡ใƒซใ€‚ไปฃใ‚ใ‚Šใซ็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆ (ใƒใ‚คใƒˆใพใŸใฏๆ–‡ๅญ—) ใ‚’็›ดๆŽฅๆ“ไฝœใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใƒ•ใƒชใƒผ ใƒขใƒ‡ใƒซใซใฏๅคšใใฎๅˆฉ็‚นใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ™ใใซไฝฟ็”จใงใใ‚‹ใ‚ใ‚‰ใ‚†ใ‚‹่จ€่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ€ใƒŽใ‚คใ‚บใซๅฏพใ—ใฆใ‚ˆใ‚Šๅ …็‰ขใงใ‚ใ‚Šใ€ๆŠ€่ก“็š„่ฒ ๅ‚ตใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใพใ™ใ€‚ +่ค‡้›‘ใงใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใ‚„ใ™ใ„ใƒ†ใ‚ญใ‚นใƒˆๅ‰ๅ‡ฆ็†ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ใƒใ‚คใƒˆใพใŸใฏๆ–‡ๅญ—ๅˆ—ใŒใƒˆใƒผใ‚ฏใƒณใ‚ˆใ‚Š้•ทใ„ใŸใ‚ +ใƒˆใƒผใ‚ฏใƒณใƒ•ใƒชใƒผ ใƒขใƒ‡ใƒซใซ้–ขใ™ใ‚‹้ŽๅŽปใฎ็ ”็ฉถใงใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใ‚ณใ‚นใƒˆใ‚’ๅ„Ÿๅดใ™ใ‚‹ใ‚ˆใ†ใซ่จญ่จˆใ•ใ‚ŒใŸๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒๅฐŽๅ…ฅใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ—ใŸใ€‚ +็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’็›ดๆŽฅๆ“ไฝœใ—ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๆจ™ๆบ–็š„ใช Transformer ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒๆฌกใฎใ‚ˆใ†ใชใ‚‚ใฎใงไฝฟ็”จใงใใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒใ‚คใƒˆใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎๆœ€ๅฐ้™ใฎๅค‰ๆ›ดใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟๆ•ฐใฎ่ฆณ็‚นใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใ‚’ๆณจๆ„ๆทฑใ็‰นๅพดไป˜ใ‘ใพใ™ใ€‚ +FLOP ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–้€Ÿๅบฆใ‚’่ชฟในใ€ใƒใ‚คใƒˆใƒฌใƒ™ใƒซใฎใƒขใƒ‡ใƒซใŒใƒˆใƒผใ‚ฏใƒณใƒฌใƒ™ใƒซใจ็ซถๅˆใงใใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ๅฏพๅฟœ่€…ใ€‚ใพใŸใ€ใƒใ‚คใƒˆใƒฌใƒ™ใƒซใฎใƒขใƒ‡ใƒซใฏใƒŽใ‚คใ‚บใซๅฏพใ—ใฆๅคงๅน…ใซๅ …็‰ขใงใ‚ใ‚Šใ€ใ‚ˆใ‚Šๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ™ใ‚‹ใ“ใจใ‚‚็คบใ—ใฆใ„ใพใ™ใ€‚ +ใ‚นใƒšใƒซใจ็™บ้Ÿณใซๆ•ๆ„Ÿใชใ‚ฟใ‚นใ‚ฏใ€‚็งใŸใกใฎ่ฒข็Œฎใฎไธ€็’ฐใจใ—ใฆใ€ๆ–ฐใ—ใ„ใ‚ปใƒƒใƒˆใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ใ€‚ +T5 ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅŸบใฅใ„ใŸไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใƒใ‚คใƒˆใƒฌใƒ™ใƒซใฎ Transformer ใƒขใƒ‡ใƒซใจใ€ใใ“ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ™ในใฆใฎใ‚ณใƒผใƒ‰ใจใƒ‡ใƒผใ‚ฟ +ๅฎŸ้จ“ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[patrickvonplaten](https://huggingface.co/patrickvonplaten) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏๆฌกใฎใจใŠใ‚Šใงใ™ +[ใ“ใ“](https://github.com/google-research/byt5) ใซใ‚ใ‚Šใพใ™ใ€‚ + + + +ByT5 ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏ T5v1.1 ใƒขใƒ‡ใƒซใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใซใคใ„ใฆใฏใ€[T5v1.1 ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒšใƒผใ‚ธ](t5v1.1) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ๅฝผใ‚‰ใฏ +ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ๆ–นๆณ•ใŒ็•ฐใชใ‚‹ใ ใ‘ใงใ™ใ€‚ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ไพ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ByT5 ใฏๆ•™ๅธซใชใ—ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๅ˜ไธ€ใ‚ฟใ‚นใ‚ฏไธญใซใ‚ฟใ‚นใ‚ฏ ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๅˆฉ็‚นใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ๅพฎ่ชฟๆ•ดใ€‚ใƒžใƒซใƒใ‚ฟใ‚นใ‚ฏใฎๅพฎ่ชฟๆ•ดใ‚’่กŒใ†ๅ ดๅˆใฏใ€ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Usage Examples + +ByT5 ใฏ็”Ÿใฎ UTF-8 ใƒใ‚คใƒˆใงๅ‹•ไฝœใ™ใ‚‹ใŸใ‚ใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใชใ—ใงไฝฟ็”จใงใใพใ™ใ€‚ + +```python +>>> from transformers import T5ForConditionalGeneration +>>> import torch + +>>> model = T5ForConditionalGeneration.from_pretrained("google/byt5-small") + +>>> num_special_tokens = 3 +>>> # Model has 3 special tokens which take up the input ids 0,1,2 of ByT5. +>>> # => Need to shift utf-8 character encodings by 3 before passing ids to model. + +>>> input_ids = torch.tensor([list("Life is like a box of chocolates.".encode("utf-8"))]) + num_special_tokens + +>>> labels = torch.tensor([list("La vie est comme une boรฎte de chocolat.".encode("utf-8"))]) + num_special_tokens + +>>> loss = model(input_ids, labels=labels).loss +>>> loss.item() +2.66 +``` + +ใŸใ ใ—ใ€ใƒใƒƒใƒๆŽจ่ซ–ใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅ ดๅˆใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + +```python +>>> from transformers import T5ForConditionalGeneration, AutoTokenizer + +>>> model = T5ForConditionalGeneration.from_pretrained("google/byt5-small") +>>> tokenizer = AutoTokenizer.from_pretrained("google/byt5-small") + +>>> model_inputs = tokenizer( +... ["Life is like a box of chocolates.", "Today is Monday."], padding="longest", return_tensors="pt" +... ) +>>> labels_dict = tokenizer( +... ["La vie est comme une boรฎte de chocolat.", "Aujourd'hui c'est lundi."], padding="longest", return_tensors="pt" +... ) +>>> labels = labels_dict.input_ids + +>>> loss = model(**model_inputs, labels=labels).loss +>>> loss.item() +17.9 +``` + +[T5](t5) ใจๅŒๆง˜ใซใ€ByT5 ใฏใ‚นใƒ‘ใƒณใƒžใ‚นใ‚ฏใƒŽใ‚คใ‚บ้™คๅŽปใ‚ฟใ‚นใ‚ฏใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ใ—ใ‹ใ—ใ€ +ใƒขใƒ‡ใƒซใฏใ‚ญใƒฃใƒฉใ‚ฏใ‚ฟใƒผใซ็›ดๆŽฅไฝœ็”จใ™ใ‚‹ใŸใ‚ใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฟใ‚นใ‚ฏใฏๅฐ‘ใ—่ค‡้›‘ใงใ™ +้•ใ†ใ€‚ใฎใ„ใใคใ‹ใฎๆ–‡ๅญ—ใ‚’็ ดๆใ—ใฆใฟใพใ—ใ‚‡ใ† +`"The dog chases a ball in the park."`ใจใ„ใ†ๆ–‡ใ‚’ๅ…ฅๅŠ›ใ—ใ€ByT5 ใซไบˆๆธฌใ—ใฆใ‚‚ใ‚‰ใ„ใพใ™ใ€‚ +ใ‚ใŸใ—ใŸใกใฎใŸใ‚ใ€‚ + +```python +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM +>>> import torch + +>>> tokenizer = AutoTokenizer.from_pretrained("google/byt5-base") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("google/byt5-base") + +>>> input_ids_prompt = "The dog chases a ball in the park." +>>> input_ids = tokenizer(input_ids_prompt).input_ids + +>>> # Note that we cannot add "{extra_id_...}" to the string directly +>>> # as the Byte tokenizer would incorrectly merge the tokens +>>> # For ByT5, we need to work directly on the character level +>>> # Contrary to T5, ByT5 does not use sentinel tokens for masking, but instead +>>> # uses final utf character ids. +>>> # UTF-8 is represented by 8 bits and ByT5 has 3 special tokens. +>>> # => There are 2**8+2 = 259 input ids and mask tokens count down from index 258. +>>> # => mask to "The dog [258]a ball [257]park." + +>>> input_ids = torch.tensor([input_ids[:8] + [258] + input_ids[14:21] + [257] + input_ids[28:]]) +>>> input_ids +tensor([[ 87, 107, 104, 35, 103, 114, 106, 35, 258, 35, 100, 35, 101, 100, 111, 111, 257, 35, 115, 100, 117, 110, 49, 1]]) + +>>> # ByT5 produces only one char at a time so we need to produce many more output characters here -> set `max_length=100`. +>>> output_ids = model.generate(input_ids, max_length=100)[0].tolist() +>>> output_ids +[0, 258, 108, 118, 35, 119, 107, 104, 35, 114, 113, 104, 35, 122, 107, 114, 35, 103, 114, 104, 118, 257, 35, 108, 113, 35, 119, 107, 104, 35, 103, 108, 118, 102, 114, 256, 108, 113, 35, 119, 107, 104, 35, 115, 100, 117, 110, 49, 35, 87, 107, 104, 35, 103, 114, 106, 35, 108, 118, 35, 119, 107, 104, 35, 114, 113, 104, 35, 122, 107, 114, 35, 103, 114, 104, 118, 35, 100, 35, 101, 100, 111, 111, 35, 108, 113, 255, 35, 108, 113, 35, 119, 107, 104, 35, 115, 100, 117, 110, 49] + +>>> # ^- Note how 258 descends to 257, 256, 255 + +>>> # Now we need to split on the sentinel tokens, let's write a short loop for this +>>> output_ids_list = [] +>>> start_token = 0 +>>> sentinel_token = 258 +>>> while sentinel_token in output_ids: +... split_idx = output_ids.index(sentinel_token) +... output_ids_list.append(output_ids[start_token:split_idx]) +... start_token = split_idx +... sentinel_token -= 1 + +>>> output_ids_list.append(output_ids[start_token:]) +>>> output_string = tokenizer.batch_decode(output_ids_list) +>>> output_string +['', 'is the one who does', ' in the disco', 'in the park. The dog is the one who does a ball in', ' in the park.'] +``` + +## ByT5Tokenizer + +[[autodoc]] ByT5Tokenizer + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[`ByT5Tokenizer`] ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/camembert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/camembert.md new file mode 100644 index 0000000000000000000000000000000000000000..382077613dd1637fe99d7017061f172b3b1704a6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/camembert.md @@ -0,0 +1,135 @@ + + +# CamemBERT + +## Overview + +CamemBERT ใƒขใƒ‡ใƒซใฏใ€[CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Louis Martin, Benjamin Muller, Pedro Javier Ortiz Suรกrez, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la +Clergerie, Djamรฉ Seddah, and Benoรฎt Sagot. 2019ๅนดใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸFacebookใฎRoBERTaใƒขใƒ‡ใƒซใ‚’ใƒ™ใƒผใ‚นใซใ—ใŸใƒขใƒ‡ใƒซใงใ™ใ€‚ +138GBใฎใƒ•ใƒฉใƒณใ‚น่ชžใƒ†ใ‚ญใ‚นใƒˆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใฏ็พๅœจใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†ใงๅบƒใๆ™ฎๅŠใ—ใฆใ„ใพใ™ใ€‚ๆˆๅŠŸใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€ๅˆฉ็”จๅฏ่ƒฝใชใปใจใ‚“ใฉใฎ +ใƒขใƒ‡ใƒซใฏ่‹ฑ่ชžใฎใƒ‡ใƒผใ‚ฟใ€ใพใŸใฏ่ค‡ๆ•ฐ่จ€่ชžใฎใƒ‡ใƒผใ‚ฟใฎ้€ฃ็ตใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ +ใ“ใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใฎๅฎŸ้š›ใฎไฝฟ็”จใฏใ€่‹ฑ่ชžใ‚’้™คใใ™ในใฆใฎ่จ€่ชžใง้žๅธธใซ้™ใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ใƒ•ใƒฉใƒณใ‚นไบบใซใจใฃใฆใ“ใฎๅ•้กŒใซๅฏพๅ‡ฆใ™ใ‚‹ใ“ใจใ‚’็›ฎๆŒ‡ใ—ใฆใ€ +Bi-direction Encoders for Transformers (BERT) ใฎใƒ•ใƒฉใƒณใ‚น่ชž็‰ˆใงใ‚ใ‚‹ CamemBERT ใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ใ€‚ๆธฌๅฎšใ—ใพใ™ +่ค‡ๆ•ฐใฎไธ‹ๆตใ‚ฟใ‚นใ‚ฏใ€ใคใพใ‚Šๅ“่ฉžใ‚ฟใ‚ฐไป˜ใ‘ใซใŠใ‘ใ‚‹ๅคš่จ€่ชžใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ—ใŸ CamemBERT ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น +ไพๅญ˜้–ขไฟ‚่งฃๆžใ€ๅ›บๆœ‰่กจ็พ่ช่ญ˜ใ€่‡ช็„ถ่จ€่ชžๆŽจ่ซ–ใ€‚ CamemBERT ใฏๆœ€ๅ…ˆ็ซฏๆŠ€่ก“ใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ +ๆคœ่จŽใ•ใ‚Œใฆใ„ใ‚‹ใปใจใ‚“ใฉใฎใ‚ฟใ‚นใ‚ฏใซๅฏพๅฟœใ—ใพใ™ใ€‚็งใŸใกใฏใ€็ ”็ฉถใจ +ใƒ•ใƒฉใƒณใ‚น่ชž NLP ใฎไธ‹ๆตใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [camembert](https://huggingface.co/camembert) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://camembert-model.fr/) ใซใ‚ใ‚Šใพใ™ใ€‚ + + + + +ใ“ใฎๅฎŸ่ฃ…ใฏRoBERTaใจๅŒใ˜ใงใ™ใ€‚ไฝฟ็”จไพ‹ใซใคใ„ใฆใฏ[RoBERTaใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](roberta)ใ‚‚ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ๅ…ฅๅŠ›ใจๅ‡บๅŠ›ใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใจใ—ใฆใ€‚ + + + +## Resources + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_language_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## CamembertConfig + +[[autodoc]] CamembertConfig + +## CamembertTokenizer + +[[autodoc]] CamembertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## CamembertTokenizerFast + +[[autodoc]] CamembertTokenizerFast + + + + +## CamembertModel + +[[autodoc]] CamembertModel + +## CamembertForCausalLM + +[[autodoc]] CamembertForCausalLM + +## CamembertForMaskedLM + +[[autodoc]] CamembertForMaskedLM + +## CamembertForSequenceClassification + +[[autodoc]] CamembertForSequenceClassification + +## CamembertForMultipleChoice + +[[autodoc]] CamembertForMultipleChoice + +## CamembertForTokenClassification + +[[autodoc]] CamembertForTokenClassification + +## CamembertForQuestionAnswering + +[[autodoc]] CamembertForQuestionAnswering + + + + +## TFCamembertModel + +[[autodoc]] TFCamembertModel + +## TFCamembertForCasualLM + +[[autodoc]] TFCamembertForCausalLM + +## TFCamembertForMaskedLM + +[[autodoc]] TFCamembertForMaskedLM + +## TFCamembertForSequenceClassification + +[[autodoc]] TFCamembertForSequenceClassification + +## TFCamembertForMultipleChoice + +[[autodoc]] TFCamembertForMultipleChoice + +## TFCamembertForTokenClassification + +[[autodoc]] TFCamembertForTokenClassification + +## TFCamembertForQuestionAnswering + +[[autodoc]] TFCamembertForQuestionAnswering + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/canine.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/canine.md new file mode 100644 index 0000000000000000000000000000000000000000..35bd6dc702ec0891cd70121bb3a8f242ab4e0571 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/canine.md @@ -0,0 +1,144 @@ + + +# CANINE + +## Overview + +CANINE ใƒขใƒ‡ใƒซใฏใ€[CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language +Representation](https://huggingface.co/papers/2103.06874)ใ€Jonathan H. Clarkใ€Dan Garretteใ€Iulia Turcใ€John Wieting ่‘—ใ€‚ใใฎ +ๆ˜Ž็คบ็š„ใชใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚นใƒ†ใƒƒใƒ— (ใƒใ‚คใƒˆ ใƒšใ‚ขใชใฉ) ใ‚’ไฝฟ็”จใ›ใšใซ Transformer ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆœ€ๅˆใฎ่ซ–ๆ–‡ใฎ 1 ใค +ใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ (BPEใ€WordPiece ใพใŸใฏ SentencePiece)ใ€‚ไปฃใ‚ใ‚Šใซใ€ใƒขใƒ‡ใƒซใฏ Unicode ๆ–‡ๅญ—ใƒฌใƒ™ใƒซใง็›ดๆŽฅใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ +ใ‚ญใƒฃใƒฉใ‚ฏใ‚ฟใƒผใƒฌใƒ™ใƒซใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใฏๅฟ…็„ถ็š„ใซใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใŒ้•ทใใชใ‚Šใพใ™ใŒใ€CANINE ใฏใ“ใ‚Œใ‚’ๅŠน็އ็š„ใชๆ–นๆณ•ใง่งฃๆฑบใ—ใพใ™ใ€‚ +ใƒ‡ใ‚ฃใƒผใƒ— Transformer ใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’้ฉ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐๆˆฆ็•ฅใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ NLP ใ‚ทใ‚นใƒ†ใƒ ใฏใ€ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒขใƒ‡ใƒชใƒณใ‚ฐใซๅคง้ƒจๅˆ†ใŒๅ–ใฃใฆไปฃใ‚ใ‚‰ใ‚Œใฆใ„ใพใ™ใŒใ€ไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใปใผใ™ในใฆใฎใƒขใƒ‡ใƒซใฏ +ไพ็„ถใจใ—ใฆๆ˜Ž็คบ็š„ใชใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ‰‹้ †ใŒๅฟ…่ฆใงใ™ใ€‚ๆœ€่ฟ‘ใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚ขใƒ—ใƒญใƒผใƒใฏใƒ‡ใƒผใ‚ฟ็”ฑๆฅใฎใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅŸบใฅใ„ใฆใ„ใพใ™ใŒใ€ +ใƒฌใ‚ญใ‚ทใ‚ณใƒณใฏๆ‰‹ๅ‹•ใงไฝœๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚ˆใ‚Šใ‚‚่„†ๅผฑใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใ“ใ‚Œใ‚‰ใฎๆŠ€่ก“ใฏใ™ในใฆใฎ่จ€่ชžใซ็ญ‰ใ—ใ้ฉใ—ใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +่จ€่ชžใ‚„ๅ›บๅฎš่ชžๅฝ™ใฎไฝฟ็”จใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฎ้ฉๅฟœ่ƒฝๅŠ›ใŒๅˆถ้™ใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€CANINE ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +ๆ˜Ž็คบ็š„ใชใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚„่ชžๅฝ™ใ‚’ไฝฟ็”จใ›ใšใซใ€ๆ–‡ๅญ—ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็›ดๆŽฅๆ“ไฝœใ™ใ‚‹ใƒ‹ใƒฅใƒผใƒฉใƒซ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ€ +ๆ–‡ๅญ—ใซ็›ดๆŽฅไฝœ็”จใ™ใ‚‹ใ‹ใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ‚ตใƒ–ใƒฏใƒผใƒ‰ใ‚’ใ‚ฝใƒ•ใƒˆ่ช˜ๅฐŽใƒใ‚คใ‚ขใ‚นใจใ—ใฆไฝฟ็”จใ™ใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆˆฆ็•ฅใ€‚ +ใ‚ˆใ‚Šใใ‚ใฎ็ดฐใ‹ใ„ๅ…ฅๅŠ›ใ‚’ๅŠนๆžœ็š„ใ‹ใคๅŠน็އ็š„ใซไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใ€CANINE ใฏใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ€ๅ…ฅๅŠ›ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ +ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใƒ‡ใ‚ฃใƒผใƒ—ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚นใ‚ฟใƒƒใ‚ฏใ‚’ๅ‚™ใˆใŸใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใ€‚ CANINE ใฏใ€ๅŒ็ญ‰ใฎ mBERT ใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ๆฌกใฎ็‚นใงๅ„ชใ‚Œใฆใ„ใพใ™ใ€‚ +TyDi QA ใฎ 2.8 F1 ใฏใ€ใƒขใƒ‡ใƒซ ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ 28% ๅฐ‘ใชใ„ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€ๅ›ฐ้›ฃใชๅคš่จ€่ชžใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/google-research/language/tree/master/language/canine) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- CANINE ใฏๅ†…้ƒจใงๅฐ‘ใชใใจใ‚‚ 3 ใคใฎ Transformer ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใพใ™: 2 ใคใฎใ€Œๆต…ใ„ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ (ๅ˜ไธ€ใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎใฟใงๆง‹ๆˆ) + ใƒฌใ‚คใƒคใƒผ) ใจ 1 ใคใฎใ€Œใƒ‡ใ‚ฃใƒผใƒ—ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ (้€šๅธธใฎ BERT ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ)ใ€‚ใพใšใ€ใ€Œๆต…ใ„ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ—ใฆใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + ใƒญใƒผใ‚ซใƒซ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใŸๆ–‡ๅญ—ใฎๅŸ‹ใ‚่พผใฟใ€‚ๆฌกใซใ€ใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใฎๅพŒใ€ใ€Œใƒ‡ใ‚ฃใƒผใƒ—ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใŒ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ใคใ„ใซใ€ + ใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐๅพŒใ€ใ€Œๆต…ใ„ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ—ใฆๆœ€็ต‚็š„ใชๆ–‡ๅญ—ๅŸ‹ใ‚่พผใฟใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ใ‚ขใƒƒใƒ—ใจ + ใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใซใคใ„ใฆใฏ่ซ–ๆ–‡ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- CANINE ใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง 2048 ๆ–‡ๅญ—ใฎๆœ€ๅคงใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [`CanineTokenizer`] ใ‚’ไฝฟ็”จใงใใพใ™ + ใƒขใƒ‡ใƒซ็”จใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ +- ็‰นๅˆฅใช [CLS] ใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚็š„ใช้ž่กจ็คบ็Šถๆ…‹ใฎไธŠใซ็ทšๅฝขใƒฌใ‚คใƒคใƒผใ‚’้…็ฝฎใ™ใ‚‹ใ“ใจใงๅˆ†้กžใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ + (ไบ‹ๅ‰ๅฎš็พฉใ•ใ‚ŒใŸ Unicode ใ‚ณใƒผใƒ‰ ใƒใ‚คใƒณใƒˆใŒใ‚ใ‚Šใพใ™)ใ€‚ใŸใ ใ—ใ€ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใฏใ€ใƒ€ใ‚ฆใƒณใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚ŒใŸใ‚ทใƒผใ‚ฑใƒณใ‚น + ใƒˆใƒผใ‚ฏใƒณใฏใ€ๅ…ƒใฎๆ–‡ๅญ—ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ• (2048) ใจไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซๅ†ๅบฆใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใฎ + ่ฉณ็ดฐใซใคใ„ใฆใฏใ€่ซ–ๆ–‡ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ: + + - [google/canine-c](https://huggingface.co/google/canine-c): ่‡ชๅทฑๅ›žๅธฐๆ–‡ๅญ—ๆๅคฑใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใ€ + 12 ใƒฌใ‚คใƒคใƒผใ€768 ้š ใ—ใ€12 ใƒ˜ใƒƒใƒ‰ใ€121M ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ (ใ‚ตใ‚คใ‚บ ~500 MB)ใ€‚ + - [google/canine-s](https://huggingface.co/google/canine-s): ใ‚ตใƒ–ใƒฏใƒผใƒ‰ๆๅคฑใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใ€12 ๅฑคใ€ + 768 ๅ€‹ใฎ้ž่กจ็คบใ€12 ใƒ˜ใƒƒใƒ‰ใ€121M ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผ (ใ‚ตใ‚คใ‚บ ~500 MB)ใ€‚ + +## Usage example + +CANINE ใฏ็”Ÿใฎๆ–‡ๅญ—ใงๅ‹•ไฝœใ™ใ‚‹ใŸใ‚ใ€**ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใชใ—**ใงไฝฟ็”จใงใใพใ™ใ€‚ + +```python +>>> from transformers import CanineModel +>>> import torch + +>>> model = CanineModel.from_pretrained("google/canine-c") # model pre-trained with autoregressive character loss + +>>> text = "hello world" +>>> # use Python's built-in ord() function to turn each character into its unicode code point id +>>> input_ids = torch.tensor([[ord(char) for char in text]]) + +>>> outputs = model(input_ids) # forward pass +>>> pooled_output = outputs.pooler_output +>>> sequence_output = outputs.last_hidden_state +``` + +ใŸใ ใ—ใ€ใƒใƒƒใƒๆŽจ่ซ–ใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅ ดๅˆใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™๏ผˆใ™ในใฆใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ/ๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใŸใ‚๏ผ‰ +ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅŒใ˜้•ทใ•ใซใ—ใพใ™): + +```python +>>> from transformers import CanineTokenizer, CanineModel + +>>> model = CanineModel.from_pretrained("google/canine-c") +>>> tokenizer = CanineTokenizer.from_pretrained("google/canine-c") + +>>> inputs = ["Life is like a box of chocolates.", "You never know what you gonna get."] +>>> encoding = tokenizer(inputs, padding="longest", truncation=True, return_tensors="pt") + +>>> outputs = model(**encoding) # forward pass +>>> pooled_output = outputs.pooler_output +>>> sequence_output = outputs.last_hidden_state +``` + +## Resources + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## CanineConfig + +[[autodoc]] CanineConfig + +## CanineTokenizer + +[[autodoc]] CanineTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + +## CANINE specific outputs + +[[autodoc]] models.canine.modeling_canine.CanineModelOutputWithPooling + +## CanineModel + +[[autodoc]] CanineModel + - forward + +## CanineForSequenceClassification + +[[autodoc]] CanineForSequenceClassification + - forward + +## CanineForMultipleChoice + +[[autodoc]] CanineForMultipleChoice + - forward + +## CanineForTokenClassification + +[[autodoc]] CanineForTokenClassification + - forward + +## CanineForQuestionAnswering + +[[autodoc]] CanineForQuestionAnswering + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/chinese_clip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/chinese_clip.md new file mode 100644 index 0000000000000000000000000000000000000000..5dac6b3589f3b4240b7b5b322d787aa2d96b93e7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/chinese_clip.md @@ -0,0 +1,117 @@ + + +# Chinese-CLIP + +## Overview + +Chinese-CLIP An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou [Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese](https://huggingface.co/papers/2211.01335) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ๅ‘จใ€ๅผตๅ‘จใ€‚ +Chinese-CLIP ใฏใ€ไธญๅ›ฝ่ชžใฎ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎใƒšใ‚ขใฎๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅฏพใ™ใ‚‹ CLIP (Radford et al., 2021) ใฎๅฎŸ่ฃ…ใงใ™ใ€‚ใ‚ฏใƒญใ‚นใƒขใƒผใƒ€ใƒซๆคœ็ดขใ‚’ๅฎŸ่กŒใงใใ‚‹ใปใ‹ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใ€ใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใชใฉใฎใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใฎใƒ“ใ‚ธใƒงใƒณใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎไธญๅ›ฝ่ชž-CLIPใ‚ณใƒผใƒ‰ใฏ[ใ“ใฎใƒชใƒณใ‚ฏใง](https://github.com/OFA-Sys/Chinese-CLIP)ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*CLIP ใฎๅคงๆˆๅŠŸ (Radford et al., 2021) ใซใ‚ˆใ‚Šใ€่ฆ–่ฆš่จ€่ชžใฎไบ‹ๅ‰่จ“็ทดใฎใŸใ‚ใฎๅฏพ็…งๅญฆ็ฟ’ใฎ็ ”็ฉถใจๅฟœ็”จใŒไฟƒ้€ฒใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎ็ ”็ฉถใงใฏใ€ใปใจใ‚“ใฉใฎใƒ‡ใƒผใ‚ฟใŒๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ๅ–ๅพ—ใ•ใ‚ŒใŸไธญๅ›ฝ่ชžใฎ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎใƒšใ‚ขใฎๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆง‹็ฏ‰ใ—ใ€ๆ–ฐใ—ใ„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไธญๅ›ฝ่ชžใฎ CLIP ใƒขใƒ‡ใƒซใ‚’ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ๅฝ“็คพใงใฏใ€7,700 ไธ‡ใ‹ใ‚‰ 9 ๅ„„ 5,800 ไธ‡ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใซใ‚ใŸใ‚‹ใ€่ค‡ๆ•ฐใฎใ‚ตใ‚คใ‚บใฎ 5 ใคใฎไธญๅ›ฝ CLIP ใƒขใƒ‡ใƒซใ‚’้–‹็™บใ—ใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซใ€ๆœ€ๅˆใซ็”ปๅƒใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ใƒ•ใƒชใƒผใ‚บใ•ใ›ใฆใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ๆฌกใซใ™ในใฆใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆœ€้ฉๅŒ–ใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ 2 ๆฎต้šŽใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใ‚’ๆๆกˆใ—ใพใ™ใ€‚็งใŸใกใฎๅŒ…ๆ‹ฌ็š„ใชๅฎŸ้จ“ใงใฏใ€ไธญๅ›ฝใฎ CLIP ใŒใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆๅญฆ็ฟ’ใจๅพฎ่ชฟๆ•ดใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใง MUGEใ€Flickr30K-CNใ€ใŠใ‚ˆใณ COCO-CN ไธŠใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใงใใ€ใ‚ผใƒญใง็ซถไบ‰ๅŠ›ใฎใ‚ใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใงใใ‚‹ใ“ใจใ‚’ๅฎŸ่จผใ—ใฆใ„ใพใ™ใ€‚ - ELEVATER ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใฎ่ฉ•ไพกใซๅŸบใฅใใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใฎๅˆ†้กž (Li et al., 2022)ใ€‚ใ‚ณใƒผใƒ‰ใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใ€ใƒ‡ใƒขใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ—ใŸใ€‚* + +Chinese-CLIP ใƒขใƒ‡ใƒซใฏใ€[OFA-Sys](https://huggingface.co/OFA-Sys) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ + +## Usage example + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ ใ‚นใƒ‹ใƒšใƒƒใƒˆใฏใ€็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ็‰นๅพดใจ้กžไผผๆ€งใ‚’่จˆ็ฎ—ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +```python +>>> from PIL import Image +>>> import requests +>>> from transformers import ChineseCLIPProcessor, ChineseCLIPModel + +>>> model = ChineseCLIPModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16") +>>> processor = ChineseCLIPProcessor.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16") + +>>> url = "https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> # Squirtle, Bulbasaur, Charmander, Pikachu in English +>>> texts = ["ๆฐๅฐผ้พŸ", "ๅฆ™่›™็งๅญ", "ๅฐ็ซ้พ™", "็šฎๅกไธ˜"] + +>>> # compute image feature +>>> inputs = processor(images=image, return_tensors="pt") +>>> image_features = model.get_image_features(**inputs) +>>> image_features = image_features / image_features.norm(p=2, dim=-1, keepdim=True) # normalize + +>>> # compute text features +>>> inputs = processor(text=texts, padding=True, return_tensors="pt") +>>> text_features = model.get_text_features(**inputs) +>>> text_features = text_features / text_features.norm(p=2, dim=-1, keepdim=True) # normalize + +>>> # compute image-text similarity scores +>>> inputs = processor(text=texts, images=image, return_tensors="pt", padding=True) +>>> outputs = model(**inputs) +>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score +>>> probs = logits_per_image.softmax(dim=1) # probs: [[1.2686e-03, 5.4499e-02, 6.7968e-04, 9.4355e-01]] +``` + +็พๅœจใ€ๆฌกใฎใ‚นใ‚ฑใƒผใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ Chinese-CLIP ใƒขใƒ‡ใƒซใŒ ๐Ÿค— Hub ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +- [OFA-Sys/chinese-clip-vit-base-patch16](https://huggingface.co/OFA-Sys/chinese-clip-vit-base-patch16) +- [OFA-Sys/chinese-clip-vit-large-patch14](https://huggingface.co/OFA-Sys/chinese-clip-vit-large-patch14) +- [OFA-Sys/chinese-clip-vit-large-patch14-336px](https://huggingface.co/OFA-Sys/chinese-clip-vit-large-patch14-336px) +- [OFA-Sys/chinese-clip-vit-huge-patch14](https://huggingface.co/OFA-Sys/chinese-clip-vit-huge-patch14) + +## ChineseCLIPConfig + +[[autodoc]] ChineseCLIPConfig + - from_text_vision_configs + +## ChineseCLIPTextConfig + +[[autodoc]] ChineseCLIPTextConfig + +## ChineseCLIPVisionConfig + +[[autodoc]] ChineseCLIPVisionConfig + +## ChineseCLIPImageProcessor + +[[autodoc]] ChineseCLIPImageProcessor + - preprocess + +## ChineseCLIPImageProcessorFast + +[[autodoc]] ChineseCLIPImageProcessorFast + - preprocess + +## ChineseCLIPFeatureExtractor + +[[autodoc]] ChineseCLIPFeatureExtractor + +## ChineseCLIPProcessor + +[[autodoc]] ChineseCLIPProcessor + +## ChineseCLIPModel + +[[autodoc]] ChineseCLIPModel + - forward + - get_text_features + - get_image_features + +## ChineseCLIPTextModel + +[[autodoc]] ChineseCLIPTextModel + - forward + +## ChineseCLIPVisionModel + +[[autodoc]] ChineseCLIPVisionModel + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clap.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clap.md new file mode 100644 index 0000000000000000000000000000000000000000..1a5f2b5dfef163528e86db3e572c3e6edc2d18e0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clap.md @@ -0,0 +1,80 @@ + + +# CLAP + +## Overview + +CLAP ใƒขใƒ‡ใƒซใฏใ€[Large Scale Contrastive Language-Audio pretraining with +feature fusion and keyword-to-caption augmentation](https://huggingface.co/papers/2211.06687)ใ€Yusong Wuใ€Ke Chenใ€Tianyu Zhangใ€Yuchen Huiใ€Taylor Berg-Kirkpatrickใ€Shlomo Dubnov ่‘—ใ€‚ + +CLAP (Contrastive Language-Audio Pretraining) ใฏใ€ใ•ใพใ–ใพใช (้Ÿณๅฃฐใ€ใƒ†ใ‚ญใ‚นใƒˆ) ใƒšใ‚ขใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใงใ™ใ€‚ใ‚ฟใ‚นใ‚ฏใซๅˆใ‚ใ›ใฆ็›ดๆŽฅๆœ€้ฉๅŒ–ใ™ใ‚‹ใ“ใจใชใใ€้ŸณๅฃฐใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใซๆœ€ใ‚‚้–ข้€ฃๆ€งใฎ้ซ˜ใ„ใƒ†ใ‚ญใ‚นใƒˆ ใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใงใใพใ™ใ€‚ CLAP ใƒขใƒ‡ใƒซใฏใ€SWINTransformer ใ‚’ไฝฟ็”จใ—ใฆ log-Mel ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ๅ…ฅๅŠ›ใ‹ใ‚‰ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใ€RoBERTa ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆ็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ๆฌกใซใ€ใƒ†ใ‚ญใ‚นใƒˆใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใฎไธกๆ–นใฎ็‰นๅพดใŒใ€ๅŒใ˜ๆฌกๅ…ƒใฎๆฝœๅœจ็ฉบ้–“ใซๆŠ•ๅฝฑใ•ใ‚Œใพใ™ใ€‚ๆŠ•ๅฝฑใ•ใ‚ŒใŸใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใจใƒ†ใ‚ญใ‚นใƒˆใฎ็‰นๅพดใฎ้–“ใฎใƒ‰ใƒƒใƒˆ็ฉใŒใ€ๅŒๆง˜ใฎใ‚นใ‚ณใ‚ขใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๅฏพ็…งๅญฆ็ฟ’ใฏใ€ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ่กจ็พๅญฆ็ฟ’ใฎๅˆ†้‡Žใง็›ฎ่ฆšใพใ—ใ„ๆˆๅŠŸใ‚’ๅŽใ‚ใฆใ„ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใจ่‡ช็„ถ่จ€่ชž่จ˜่ฟฐใ‚’็ต„ใฟๅˆใ‚ใ›ใฆ้Ÿณๅฃฐ่กจ็พใ‚’้–‹็™บใ™ใ‚‹ใ€ๅฏพ็…ง็š„ใช่จ€่ชž้Ÿณๅฃฐไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๆๆกˆใ—ใพใ™ใ€‚ใ“ใฎ็›ฎๆจ™ใ‚’้”ๆˆใ™ใ‚‹ใŸใ‚ใซใ€็งใŸใกใฏใพใšใ€ใ•ใพใ–ใพใชใƒ‡ใƒผใ‚ฟ ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใฎ 633,526 ๅ€‹ใฎ้Ÿณๅฃฐใจใƒ†ใ‚ญใ‚นใƒˆใฎใƒšใ‚ขใฎๅคง่ฆๆจกใชใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใงใ‚ใ‚‹ LAION-Audio-630K ใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ใ€‚ๆฌกใซใ€ใ•ใพใ–ใพใชใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใ‚จใƒณใ‚ณใƒผใƒ€ใจใƒ†ใ‚ญใ‚นใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’่€ƒๆ…ฎใ—ใฆใ€ๅฏพ็…ง็š„ใช่จ€่ชžใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ๆฉŸ่ƒฝ่žๅˆใƒกใ‚ซใƒ‹ใ‚บใƒ ใจใ‚ญใƒผใƒฏใƒผใƒ‰ใ‹ใ‚‰ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใธใฎๆ‹กๅผตใ‚’ใƒขใƒ‡ใƒซ่จญ่จˆใซ็ต„ใฟ่พผใ‚“ใงใ€ใƒขใƒ‡ใƒซใŒๅฏๅค‰้•ทใฎ้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใงใใ‚‹ใ‚ˆใ†ใซใ—ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ 3 ็•ช็›ฎใซใ€ๅŒ…ๆ‹ฌ็š„ใชๅฎŸ้จ“ใ‚’ๅฎŸ่กŒใ—ใฆใ€ใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰้Ÿณๅฃฐใธใฎๅ–ๅพ—ใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ้Ÿณๅฃฐๅˆ†้กžใ€ๆ•™ๅธซไป˜ใ้Ÿณๅฃฐๅˆ†้กžใฎ 3 ใคใฎใ‚ฟใ‚นใ‚ฏใซใ‚ใŸใฃใฆใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚็ตๆžœใฏใ€็งใŸใกใฎใƒขใƒ‡ใƒซใŒใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰้Ÿณๅฃฐใธใฎๆคœ็ดขใ‚ฟใ‚นใ‚ฏใซใŠใ„ใฆๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใ‚ฟใ‚นใ‚ฏใงใฏใ€ใƒขใƒ‡ใƒซใฏใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ่จญๅฎšใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใ—ใ€้žใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ่จญๅฎšใงใ‚‚ใƒขใƒ‡ใƒซใฎ็ตๆžœใซๅŒนๆ•ตใ™ใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ LAION-ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช-6* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[Younes Belkada](https://huggingface.co/ybelkada) ใŠใ‚ˆใณ [Arthur Zucker](https://huggingface.co/ArthurZ) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/LAION-AI/Clap) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## ClapConfig + +[[autodoc]] ClapConfig + - from_text_audio_configs + +## ClapTextConfig + +[[autodoc]] ClapTextConfig + +## ClapAudioConfig + +[[autodoc]] ClapAudioConfig + +## ClapFeatureExtractor + +[[autodoc]] ClapFeatureExtractor + +## ClapProcessor + +[[autodoc]] ClapProcessor + +## ClapModel + +[[autodoc]] ClapModel + - forward + - get_text_features + - get_audio_features + +## ClapTextModel + +[[autodoc]] ClapTextModel + - forward + +## ClapTextModelWithProjection + +[[autodoc]] ClapTextModelWithProjection + - forward + +## ClapAudioModel + +[[autodoc]] ClapAudioModel + - forward + +## ClapAudioModelWithProjection + +[[autodoc]] ClapAudioModelWithProjection + - forward + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clip.md new file mode 100644 index 0000000000000000000000000000000000000000..6b21dbd143950868a32f838a4f4bcb1f0ae75a6f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clip.md @@ -0,0 +1,225 @@ + + +# CLIP + +## Overview + +CLIP ใƒขใƒ‡ใƒซใฏใ€Alec Radfordใ€Jong Wook Kimใ€Chris Hallacyใ€Aditya Rameshใ€Gabriel Goh Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ตใƒณใƒ‡ใ‚ฃใƒ‹ใƒปใ‚ขใ‚ฌใƒซใƒฏใƒซใ€ใ‚ฎใƒชใƒƒใ‚ทใƒฅใƒปใ‚ตใ‚นใƒˆใƒชใƒผใ€ใ‚ขใƒžใƒณใƒ€ใƒปใ‚ขใ‚นใ‚ฑใƒซใ€ใƒ‘ใƒกใƒฉใƒปใƒŸใ‚ทใƒฅใ‚ญใƒณใ€ใ‚ธใƒฃใƒƒใ‚ฏใƒปใ‚ฏใƒฉใƒผใ‚ฏใ€ใ‚ฐใƒฌใƒƒใƒใ‚งใƒณใƒปใ‚ฏใƒซใƒผใ‚ฌใƒผใ€ใ‚คใƒชใƒคใƒปใ‚ตใƒ„ใ‚ฑใƒดใ‚กใƒผใ€‚ใ‚ฏใƒชใƒƒใƒ— +(Contrastive Language-Image Pre-Training) ใฏใ€ใ•ใพใ–ใพใช (็”ปๅƒใ€ใƒ†ใ‚ญใ‚นใƒˆ) ใƒšใ‚ขใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใงใ™ใ€‚ใ‹ใ‚‚ใญ +็›ดๆŽฅๆœ€้ฉๅŒ–ใ™ใ‚‹ใ“ใจใชใใ€ไธŽใˆใ‚‰ใ‚ŒใŸ็”ปๅƒใ‹ใ‚‰ๆœ€ใ‚‚้–ข้€ฃๆ€งใฎ้ซ˜ใ„ใƒ†ใ‚ญใ‚นใƒˆ ใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซ่‡ช็„ถ่จ€่ชžใงๆŒ‡็คบใ•ใ‚Œใพใ™ใ€‚ +GPT-2 ใŠใ‚ˆใณ 3 ใฎใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆๆฉŸ่ƒฝใจๅŒๆง˜ใซใ€ใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๆœ€ๅ…ˆ็ซฏใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใ‚ทใ‚นใƒ†ใƒ ใฏใ€ใ‚ใ‚‰ใ‹ใ˜ใ‚ๅฎšใ‚ใ‚‰ใ‚ŒใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ซใƒ†ใ‚ดใƒชใฎๅ›บๅฎšใ‚ปใƒƒใƒˆใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œ +ๅˆถ้™ใ•ใ‚ŒใŸๅฝขๅผใฎ็›ฃ่ฆ–ใงใฏใ€ๆŒ‡ๅฎšใ™ใ‚‹ใŸใ‚ใซ่ฟฝๅŠ ใฎใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใŒๅฟ…่ฆใจใชใ‚‹ใŸใ‚ใ€ไธ€่ˆฌๆ€งใจไฝฟใ„ใ‚„ใ™ใ•ใŒๅˆถ้™ใ•ใ‚Œใพใ™ใ€‚ +ใใฎไป–ใฎ่ฆ–่ฆš็š„ใชใ‚ณใƒณใ‚ปใƒ—ใƒˆใ€‚็”ปๅƒใซ้–ขใ™ใ‚‹็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰็›ดๆŽฅๅญฆ็ฟ’ใ™ใ‚‹ใ“ใจใฏใ€ +ใ‚ˆใ‚Šๅบƒ็ฏ„ใช็›ฃ็ฃๆบใ€‚ใฉใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒ่กจ็คบใ•ใ‚Œใ‚‹ใ‹ใ‚’ไบˆๆธฌใ™ใ‚‹ใจใ„ใ†ๅ˜็ด”ใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏใŒๆœ‰ๅŠนใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +400 ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง SOTA ็”ปๅƒ่กจ็พใ‚’ๆœ€ๅˆใ‹ใ‚‰ๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใฎๅŠน็އ็š„ใ‹ใคใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชๆ–นๆณ•ใฏใฉใฎ็”ปๅƒใงใ™ใ‹ +ใ‚คใƒณใ‚ฟใƒผใƒใƒƒใƒˆใ‹ใ‚‰ๅŽ้›†ใ•ใ‚ŒใŸๆ•ฐ็™พไธ‡ใฎ๏ผˆ็”ปๅƒใ€ใƒ†ใ‚ญใ‚นใƒˆ๏ผ‰ใƒšใ‚ขใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใ€่‡ช็„ถ่จ€่ชžใ‚’ไฝฟ็”จใ—ใฆๅ‚็…งใ—ใพใ™ใ€‚ +่ฆ–่ฆš็š„ใชๆฆ‚ๅฟตใ‚’ๅญฆ็ฟ’ใ—๏ผˆใพใŸใฏๆ–ฐใ—ใ„ๆฆ‚ๅฟตใ‚’่ชฌๆ˜Žใ—๏ผ‰ใ€ไธ‹ๆตใฎใ‚ฟใ‚นใ‚ฏใธใฎใƒขใƒ‡ใƒซใฎใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ่ปข้€ใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚็งใŸใกใฏๅ‹‰ๅผทใ—ใพใ™ +30 ใ‚’่ถ…ใˆใ‚‹ใ•ใพใ–ใพใชๆ—ขๅญ˜ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ‚ฟใ‚นใ‚ฏใ‚’ใพใŸใŒใฃใฆใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’่กŒใ†ใ“ใจใซใ‚ˆใ‚Šใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚ +OCRใ€ใƒ“ใƒ‡ใ‚ชๅ†…ใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณ่ช่ญ˜ใ€ๅœฐ็†็š„ไฝ็ฝฎ็‰นๅฎšใ€ใŠใ‚ˆใณใ•ใพใ–ใพใช็จฎ้กžใฎใใ‚็ดฐใ‹ใ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๅˆ†้กžใชใฉใ€‚ใฎ +ใƒขใƒ‡ใƒซใฏใปใจใ‚“ใฉใฎใ‚ฟใ‚นใ‚ฏใซ็ฐกๅ˜ใซ็งป่กŒใงใใ€ๅคšใใฎๅ ดๅˆใ€ๅฟ…่ฆใŒใชใใฆใ‚‚ๅฎŒๅ…จใซ็›ฃ่ฆ–ใ•ใ‚ŒใŸใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใจ็ซถๅˆใ—ใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ›บๆœ‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ใŸใจใˆใฐใ€ImageNet ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใงใฏใ‚ชใƒชใ‚ธใƒŠใƒซใฎ ResNet-50 ใฎ็ฒพๅบฆใจไธ€่‡ดใ—ใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใ•ใ‚ŒใŸ 128 ไธ‡ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ตใƒณใƒ—ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ‚ณใƒผใƒ‰ใ‚’ใƒชใƒชใƒผใ‚นใ—ใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ +ใƒขใƒ‡ใƒซใฎ้‡ใฟใฏใ“ใฎ https URL ใง็ขบ่ชใงใใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [valhalla](https://huggingface.co/valhalla) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/openai/CLIP) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips and example + +CLIP ใฏใ€ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใชใƒ“ใ‚ธใƒงใƒณใŠใ‚ˆใณ่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ้กžไผผๆ€งใ‚„ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใซไฝฟ็”จใงใใพใ™ใ€‚ +ๅˆ†้กžใ€‚ CLIP ใฏใ€ViT ใฎใ‚ˆใ†ใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ไฝฟ็”จใ—ใฆ่ฆ–่ฆš็š„็‰นๅพดใ‚’ๅ–ๅพ—ใ—ใ€ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ–ๅพ—ใ—ใพใ™ +็‰นๅพดใ€‚ๆฌกใซใ€ใƒ†ใ‚ญใ‚นใƒˆใจ่ฆ–่ฆšใฎไธกๆ–นใฎ็‰นๅพดใŒใ€ๅŒใ˜ๆฌกๅ…ƒใฎๆฝœๅœจ็ฉบ้–“ใซๆŠ•ๅฝฑใ•ใ‚Œใพใ™ใ€‚ใƒ‰ใƒƒใƒˆ +ๆŠ•ๅฝฑใ•ใ‚ŒใŸ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ็‰นๅพด้–“ใฎ็ฉใŒๅŒๆง˜ใฎใ‚นใ‚ณใ‚ขใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +็”ปๅƒใ‚’ Transformer ใ‚จใƒณใ‚ณใƒผใƒ€ใซไพ›็ตฆใ™ใ‚‹ใŸใ‚ใซใ€ๅ„็”ปๅƒใฏๅ›บๅฎšใ‚ตใ‚คใ‚บใฎ้‡่ค‡ใ—ใชใ„ใƒ‘ใƒƒใƒใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆ†ๅ‰ฒใ•ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฏ็ทšๅฝขใซๅŸ‹ใ‚่พผใพใ‚Œใพใ™ใ€‚ [CLS] ใƒˆใƒผใ‚ฏใƒณใฏใ€ใ‚คใƒกใƒผใ‚ธๅ…จไฝ“ใฎ่กจ็พใจใ—ใฆๆฉŸ่ƒฝใ™ใ‚‹ใŸใ‚ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ไฝœๅฎถใŸใก +ใพใŸใ€็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’่ฟฝๅŠ ใ—ใ€็ตๆžœใจใ—ใฆๅพ—ใ‚‰ใ‚Œใ‚‹ใƒ™ใ‚ฏใƒˆใƒซใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆจ™ๆบ–ใฎ Transformer ใ‚จใƒณใ‚ณใƒผใƒ€ใซไพ›็ตฆใ—ใพใ™ใ€‚ +[`CLIPImageProcessor`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ด (ใพใŸใฏๅ†ใ‚นใ‚ฑใƒผใƒซ) ใŠใ‚ˆใณๆญฃ่ฆๅŒ–ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ + +[`CLIPTokenizer`] ใฏใƒ†ใ‚ญใ‚นใƒˆใฎใ‚จใƒณใ‚ณใƒผใƒ‰ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ [`CLIPProcessor`] ใฏใƒฉใƒƒใƒ—ใ—ใพใ™ +[`CLIPImageProcessor`] ใจ [`CLIPTokenizer`] ใ‚’ไธกๆ–นใฎๅ˜ไธ€ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใซ็ตฑๅˆ +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ๆฌกใฎไพ‹ใฏใ€ๆฌกใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎ้กžไผผๆ€งใ‚นใ‚ณใ‚ขใ‚’ๅ–ๅพ—ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ +[`CLIPProcessor`] ใจ [`CLIPModel`]ใ€‚ + +```python +>>> from PIL import Image +>>> import requests + +>>> from transformers import CLIPProcessor, CLIPModel + +>>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") +>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True) + +>>> outputs = model(**inputs) +>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score +>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities +``` + +## Resources + +CLIP ใ‚’ไฝฟใ„ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + +- [ใƒชใƒขใƒผใƒˆ ใ‚ปใƒณใ‚ทใƒณใ‚ฐ (่ก›ๆ˜Ÿ) ็”ปๅƒใจใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใŸ CLIP ใฎๅพฎ่ชฟๆ•ด](https://huggingface.co/blog/fine-tune-clip-rsicd)ใ€[RSICD ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ] ใ‚’ไฝฟ็”จใ—ใฆ CLIP ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟ(https://github.com/201528014227051/RSICD_optimal) ใจใ€ใƒ‡ใƒผใ‚ฟๆ‹กๅผตใซใ‚ˆใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅค‰ๅŒ–ใฎๆฏ”่ผƒใ€‚ +- ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/contrastive-image-text) ใฏใ€ใƒ—ใƒฌ- [COCO ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://cocodataset.org/#home) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ“ใ‚ธใƒงใƒณใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€‚ + + + +- ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใฎใƒ“ใƒผใƒ ๆคœ็ดขใซใ‚ˆใ‚‹ๆŽจ่ซ–ใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ CLIP ใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/drive/1tuoAC5F4sC7qid56Z0ap-stR3rwdk0ZV?usp=sharing)ใ€‚ ๐ŸŒŽ + +**็”ปๅƒๆคœ็ดข** + +- ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ CLIP ใ‚’ไฝฟ็”จใ—ใŸ็”ปๅƒๆคœ็ดขใจ MRR (ๅนณๅ‡็›ธไบ’ใƒฉใƒณใ‚ฏ) ใ‚นใ‚ณใ‚ขใฎ่จˆ็ฎ—ใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/drive/1bLVwVKpAndpEDHqjzxVPr_9nGrSbuOQd?usp=sharing)ใ€‚ ๐ŸŒŽ +- ็”ปๅƒใฎๅ–ๅพ—ใจ้กžไผผๆ€งใ‚นใ‚ณใ‚ขใฎ่กจ็คบใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/deep-diver/image_search_with_natural_language/blob/main/notebooks/Image_Search_CLIP.ipynb)ใ€‚ ๐ŸŒŽ +- ๅคš่จ€่ชž CLIP ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅŒใ˜ใƒ™ใ‚ฏใƒˆใƒซ็ฉบ้–“ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/drive/1xO-wC_m_GNzgjIBQ4a4znvQkvDoZJvH4?usp=sharing)ใ€‚ ๐ŸŒŽ +- ใ‚’ไฝฟ็”จใ—ใฆใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚คใƒกใƒผใ‚ธๆคœ็ดขใง CLIP ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/vivien000/clip-demo/blob/master/clip.ipynb#scrollTo=uzdFhRGqiWkR) [Unsplash](https://unsplash.com) ใŠใ‚ˆใณ [TMDB](https://www.themoviedb.org/) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ ๐ŸŒŽ + +**่ชฌๆ˜Žๅฏ่ƒฝๆ€ง** + +- ๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณใจ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒˆใฎ้กžไผผๆ€งใ‚’่ฆ–่ฆšๅŒ–ใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/hila-chefer/Transformer-MM-Explainability/blob/main/CLIP_explainability.ipynb)ใ€‚ ๐ŸŒŽ + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ +ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## CLIPConfig + +[[autodoc]] CLIPConfig + - from_text_vision_configs + +## CLIPTextConfig + +[[autodoc]] CLIPTextConfig + +## CLIPVisionConfig + +[[autodoc]] CLIPVisionConfig + +## CLIPTokenizer + +[[autodoc]] CLIPTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## CLIPTokenizerFast + +[[autodoc]] CLIPTokenizerFast + +## CLIPImageProcessor + +[[autodoc]] CLIPImageProcessor + - preprocess + +## CLIPImageProcessorFast + +[[autodoc]] CLIPImageProcessorFast + - preprocess + +## CLIPFeatureExtractor + +[[autodoc]] CLIPFeatureExtractor + +## CLIPProcessor + +[[autodoc]] CLIPProcessor + + + + +## CLIPModel + +[[autodoc]] CLIPModel + - forward + - get_text_features + - get_image_features + +## CLIPTextModel + +[[autodoc]] CLIPTextModel + - forward + +## CLIPTextModelWithProjection + +[[autodoc]] CLIPTextModelWithProjection + - forward + +## CLIPVisionModelWithProjection + +[[autodoc]] CLIPVisionModelWithProjection + - forward + +## CLIPVisionModel + +[[autodoc]] CLIPVisionModel + - forward + + + + +## TFCLIPModel + +[[autodoc]] TFCLIPModel + - call + - get_text_features + - get_image_features + +## TFCLIPTextModel + +[[autodoc]] TFCLIPTextModel + - call + +## TFCLIPVisionModel + +[[autodoc]] TFCLIPVisionModel + - call + + + + +## FlaxCLIPModel + +[[autodoc]] FlaxCLIPModel + - __call__ + - get_text_features + - get_image_features + +## FlaxCLIPTextModel + +[[autodoc]] FlaxCLIPTextModel + - __call__ + +## FlaxCLIPTextModelWithProjection + +[[autodoc]] FlaxCLIPTextModelWithProjection + - __call__ + +## FlaxCLIPVisionModel + +[[autodoc]] FlaxCLIPVisionModel + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clipseg.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clipseg.md new file mode 100644 index 0000000000000000000000000000000000000000..8853565fac0c9c1f678c7e2f4a4cf205c6a93ff9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clipseg.md @@ -0,0 +1,104 @@ + + +# CLIPSeg + +## Overview + +CLIPSeg ใƒขใƒ‡ใƒซใฏใ€Timo Lรผddecke, Alexander Ecker ใซใ‚ˆใฃใฆ [Image Segmentation using Text and Image Prompts](https://huggingface.co/papers/2112.10003) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใใ—ใฆใ‚ขใƒฌใ‚ฏใ‚ตใƒณใƒ€ใƒผใƒปใ‚จใƒƒใ‚ซใƒผใ€‚ CLIPSeg ใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใŠใ‚ˆใณใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใŸใ‚ใซใ€ๅ‡็ตใ•ใ‚ŒใŸ [CLIP](clip) ใƒขใƒ‡ใƒซใฎไธŠใซๆœ€ๅฐ้™ใฎใƒ‡ใ‚ณใƒผใƒ€ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*็”ปๅƒใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏ้€šๅธธใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ‚ˆใฃใฆ่งฃๆฑบใ•ใ‚Œใพใ™ใ€‚ +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใƒฉใ‚นใฎๅ›บๅฎšใ‚ปใƒƒใƒˆใฎใƒขใƒ‡ใƒซใ€‚ๅพŒใง่ฟฝๅŠ ใฎใ‚ฏใƒฉใ‚นใ‚„ใ‚ˆใ‚Š่ค‡้›‘ใชใ‚ฏใ‚จใƒชใ‚’็ต„ใฟ่พผใ‚€ใจใ‚ณใ‚นใƒˆใŒใ‹ใ‹ใ‚Šใพใ™ +ใ“ใ‚Œใ‚‰ใฎๅผใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ๅ†ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใ“ใงใ‚ทใ‚นใƒ†ใƒ ใ‚’ๆๆกˆใ—ใพใ™ +ไปปๆ„ใฎๆƒ…ๅ ฑใซๅŸบใฅใ„ใฆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ +ใƒ†ใ‚นใƒˆๆ™‚ใซใƒ—ใƒญใƒณใƒ—โ€‹โ€‹ใƒˆใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฏใƒ†ใ‚ญใ‚นใƒˆใพใŸใฏ +็”ปๅƒใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใซใ‚ˆใ‚Šใ€็ตฑไธ€ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใงใใพใ™ใ€‚ +3 ใคใฎไธ€่ˆฌ็š„ใชใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซใคใ„ใฆ (1 ๅ›žใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ) +ๅ‚็…งๅผใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใจใ„ใ†ๆ˜Ž็ขบใช่ชฒ้กŒใŒไผดใ„ใพใ™ใ€‚ +CLIP ใƒขใƒ‡ใƒซใ‚’ใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆๆง‹็ฏ‰ใ—ใ€ใ“ใ‚Œใ‚’ใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎใƒ‡ใ‚ณใƒผใƒ€ใงๆ‹กๅผตใ—ใฆใ€้ซ˜ๅฏ†ๅบฆใชใƒ‡ใƒผใ‚ฟ้€šไฟกใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ +ไบˆๆธฌใ€‚ใฎๆ‹กๅผตใƒใƒผใ‚ธใƒงใƒณใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใŸๅพŒใ€ +PhraseCut ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€็งใŸใกใฎใ‚ทใ‚นใƒ†ใƒ ใฏใ€ใƒ•ใƒชใƒผใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใพใŸใฏ +ใ‚ฏใ‚จใƒชใ‚’่กจใ™่ฟฝๅŠ ใฎ็”ปๅƒใ€‚ๅพŒ่€…ใฎ็”ปๅƒใƒ™ใƒผใ‚นใฎใƒ—ใƒญใƒณใƒ—ใƒˆใฎใ•ใพใ–ใพใชใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณใ‚’่ฉณ็ดฐใซๅˆ†ๆžใ—ใพใ™ใ€‚ +ใ“ใฎๆ–ฐใ—ใ„ใƒใ‚คใƒ–ใƒชใƒƒใƒ‰ๅ…ฅๅŠ›ใซใ‚ˆใ‚Šใ€ๅ‹•็š„้ฉๅฟœใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ +ๅ‰่ฟฐใฎ 3 ใคใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใฎใฟใงใ™ใŒใ€ +ใƒ†ใ‚ญใ‚นใƒˆใพใŸใฏ็”ปๅƒใ‚’ใ‚ฏใ‚จใƒชใ™ใ‚‹ใƒใ‚คใƒŠใƒช ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซ +ๅฎšๅผๅŒ–ใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใ€‚ๆœ€ๅพŒใซใ€ใ‚ทใ‚นใƒ†ใƒ ใŒใ†ใพใ้ฉๅฟœใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸ +ใ‚ขใƒ•ใ‚ฉใƒผใƒ€ใƒณใ‚นใพใŸใฏใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ๅซใ‚€ไธ€่ˆฌๅŒ–ใ•ใ‚ŒใŸใ‚ฏใ‚จใƒช* + + + + CLIPSeg ใฎๆฆ‚่ฆใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/timojl/clipseg) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- [`CLIPSegForImageSegmentation`] ใฏใ€[`CLIPSegModel`] ใฎไธŠใซใƒ‡ใ‚ณใƒผใƒ€ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ๅพŒ่€…ใฏ [`CLIPModel`] ใจๅŒใ˜ใงใ™ใ€‚ +- [`CLIPSegForImageSegmentation`] ใฏใ€ใƒ†ใ‚นใƒˆๆ™‚ใซไปปๆ„ใฎใƒ—ใƒญใƒณใƒ—ใƒˆใซๅŸบใฅใ„ใฆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฏใƒ†ใ‚ญใ‚นใƒˆใฎใ„ใšใ‚Œใ‹ใงใ™ +(`input_ids` ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใ‚‹) ใพใŸใฏ็”ปๅƒ (`conditional_pixel_values` ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใ‚‹)ใ€‚ใ‚ซใ‚นใ‚ฟใƒ ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ +ๆกไปถไป˜ใๅŸ‹ใ‚่พผใฟ (`conditional_embeddings`ใจใ—ใฆใƒขใƒ‡ใƒซใซๆไพ›ใ•ใ‚Œใพใ™)ใ€‚ + +## Resources + +CLIPSeg ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคใ€ๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + + + +- [CLIPSeg ใ‚’ไฝฟ็”จใ—ใŸใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/CLIPSeg/Zero_shot_image_segmentation_with_CLIPSeg.ipynb) ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ€‚ + +## CLIPSegConfig + +[[autodoc]] CLIPSegConfig + - from_text_vision_configs + +## CLIPSegTextConfig + +[[autodoc]] CLIPSegTextConfig + +## CLIPSegVisionConfig + +[[autodoc]] CLIPSegVisionConfig + +## CLIPSegProcessor + +[[autodoc]] CLIPSegProcessor + +## CLIPSegModel + +[[autodoc]] CLIPSegModel + - forward + - get_text_features + - get_image_features + +## CLIPSegTextModel + +[[autodoc]] CLIPSegTextModel + - forward + +## CLIPSegVisionModel + +[[autodoc]] CLIPSegVisionModel + - forward + +## CLIPSegForImageSegmentation + +[[autodoc]] CLIPSegForImageSegmentation + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clvp.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clvp.md new file mode 100644 index 0000000000000000000000000000000000000000..874e0779c7c3affd2104195561fed3a033dcb956 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/clvp.md @@ -0,0 +1,123 @@ + + +# CLVP + +## Overview + +CLVP (Contrastive Language-Voice Pretrained Transformer) ใƒขใƒ‡ใƒซใฏใ€James Betker ใซใ‚ˆใฃใฆ [Better speech synthesis through scaling](https://huggingface.co/papers/2305.07243) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*่ฟ‘ๅนดใ€็”ปๅƒ็”Ÿๆˆใฎๅˆ†้‡Žใฏ่‡ชๅทฑๅ›žๅธฐๅค‰ๆ›ๅ™จใจ DDPM ใฎๅฟœ็”จใซใ‚ˆใฃใฆ้ฉๅ‘ฝใ‚’่ตทใ“ใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€็”ปๅƒ็”Ÿๆˆใฎใƒ—ใƒญใ‚ปใ‚นใ‚’ๆฎต้šŽ็š„ใช็ขบ็އ็š„ใƒ—ใƒญใ‚ปใ‚นใจใ—ใฆใƒขใƒ‡ใƒซๅŒ–ใ—ใ€ๅคง้‡ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐใจใƒ‡ใƒผใ‚ฟใ‚’ๆดป็”จใ—ใฆ็”ปๅƒใฎๅˆ†ๅธƒใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใฎๆ–นๆณ•่ซ–ใฏใ€็”ปๅƒใซ้™ๅฎšใ•ใ‚Œใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€็”ปๅƒ็”Ÿๆˆใƒ‰ใƒกใ‚คใƒณใฎ้€ฒๆญฉใ‚’้Ÿณๅฃฐๅˆๆˆใซ้ฉ็”จใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ใใฎ็ตๆžœใ€่กจ็พๅŠ›่ฑŠใ‹ใชใƒžใƒซใƒ้Ÿณๅฃฐใƒ†ใ‚ญใ‚นใƒˆ่ชญใฟไธŠใ’ใ‚ทใ‚นใƒ†ใƒ ใงใ‚ใ‚‹ TorToise ใŒ่ช•็”Ÿใ—ใพใ—ใŸใ€‚ + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [Susnato Dhar](https://huggingface.co/susnato) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/neonbjb/tortoise-tts) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +1. CLVP ใฏ Tortoise TTS ใƒขใƒ‡ใƒซใฎไธๅฏๆฌ ใช้ƒจๅˆ†ใงใ™ใ€‚ +2. CLVP ใ‚’ไฝฟ็”จใ—ใฆใ€็”Ÿๆˆใ•ใ‚ŒใŸใ•ใพใ–ใพใช้Ÿณๅฃฐๅ€™่ฃœใ‚’ๆไพ›ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใจๆฏ”่ผƒใ™ใ‚‹ใ“ใจใŒใงใใ€ๆœ€่‰ฏใฎ้Ÿณๅฃฐใƒˆใƒผใ‚ฏใƒณใŒๆ‹กๆ•ฃใƒขใƒ‡ใƒซใซ่ปข้€ใ•ใ‚Œใพใ™ใ€‚ +3. Tortoise ใฎไฝฟ็”จใซใฏใ€[`ClvpModelForConditionalGeneration.generate()`] ใƒกใ‚ฝใƒƒใƒ‰ใฎไฝฟ็”จใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +4. 16 kHz ใ‚’ๆœŸๅพ…ใ™ใ‚‹ไป–ใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒขใƒ‡ใƒซใจใฏๅฏพ็…ง็š„ใซใ€CLVP ใƒขใƒ‡ใƒซใฏใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใŒ 22.05 kHz ใงใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +## Brief Explanation: + +- [`ClvpTokenizer`] ใฏใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€[`ClvpFeatureExtractor`] ใฏ็›ฎ็š„ใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‹ใ‚‰ใƒญใ‚ฐ ใƒกใƒซ ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ +- [`ClvpConditioningEncoder`] ใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚ญใ‚นใƒˆ ใƒˆใƒผใ‚ฏใƒณใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช่กจ็พใ‚’ๅ–ๅพ—ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใซๅŸบใฅใ„ใฆๆกไปถไป˜ใ‘ใ•ใ‚ŒใŸๅŸ‹ใ‚่พผใฟใซๅค‰ๆ›ใ—ใพใ™ใ€‚ +- [`ClvpForCausalLM`] ใฏใ€ใ“ใ‚Œใ‚‰ใฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ—ใฆ่ค‡ๆ•ฐใฎ้Ÿณๅฃฐๅ€™่ฃœใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ +- ๅ„้Ÿณๅฃฐๅ€™่ฃœใฏ้Ÿณๅฃฐใ‚จใƒณใ‚ณใƒผใƒ€ ([`ClvpEncoder`]) ใ‚’้€š้Žใ—ใฆใƒ™ใ‚ฏใƒˆใƒซ่กจ็พใซๅค‰ๆ›ใ•ใ‚Œใ€ใƒ†ใ‚ญใ‚นใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ€ ([`ClvpEncoder`]) ใฏใƒ†ใ‚ญใ‚นใƒˆ ใƒˆใƒผใ‚ฏใƒณใ‚’ๅŒใ˜ๆฝœๅœจ็ฉบ้–“ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ +- ๆœ€ๅพŒใซใ€ๅ„้Ÿณๅฃฐใƒ™ใ‚ฏใƒˆใƒซใ‚’ใƒ†ใ‚ญใ‚นใƒˆ ใƒ™ใ‚ฏใƒˆใƒซใจๆฏ”่ผƒใ—ใฆใ€ใฉใฎ้Ÿณๅฃฐใƒ™ใ‚ฏใƒˆใƒซใŒใƒ†ใ‚ญใ‚นใƒˆ ใƒ™ใ‚ฏใƒˆใƒซใซๆœ€ใ‚‚้กžไผผใ—ใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +- [`ClvpModelForConditionalGeneration.generate()`] ใฏใ€ไธŠ่จ˜ใฎใ™ในใฆใฎใƒญใ‚ธใƒƒใ‚ฏใ‚’ 1 ใคใฎใƒกใ‚ฝใƒƒใƒ‰ใซๅœง็ธฎใ—ใพใ™ใ€‚ + +ไพ‹ ๏ผš + +```python +>>> import datasets +>>> from transformers import ClvpProcessor, ClvpModelForConditionalGeneration + +>>> # Define the Text and Load the Audio (We are taking an audio example from HuggingFace Hub using `datasets` library). +>>> text = "This is an example text." + +>>> ds = datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") +>>> ds = ds.cast_column("audio", datasets.Audio(sampling_rate=22050)) +>>> sample = ds[0]["audio"] + +>>> # Define processor and model. +>>> processor = ClvpProcessor.from_pretrained("susnato/clvp_dev") +>>> model = ClvpModelForConditionalGeneration.from_pretrained("susnato/clvp_dev") + +>>> # Generate processor output and model output. +>>> processor_output = processor(raw_speech=sample["array"], sampling_rate=sample["sampling_rate"], text=text, return_tensors="pt") +>>> generated_output = model.generate(**processor_output) +``` + + +## ClvpConfig + +[[autodoc]] ClvpConfig + - from_sub_model_configs + +## ClvpEncoderConfig + +[[autodoc]] ClvpEncoderConfig + +## ClvpDecoderConfig + +[[autodoc]] ClvpDecoderConfig + +## ClvpTokenizer + +[[autodoc]] ClvpTokenizer + - save_vocabulary + +## ClvpFeatureExtractor + +[[autodoc]] ClvpFeatureExtractor + - __call__ + +## ClvpProcessor + +[[autodoc]] ClvpProcessor + - __call__ + - decode + - batch_decode + +## ClvpModelForConditionalGeneration + +[[autodoc]] ClvpModelForConditionalGeneration + - forward + - generate + - get_text_features + - get_speech_features + +## ClvpForCausalLM + +[[autodoc]] ClvpForCausalLM + +## ClvpModel + +[[autodoc]] ClvpModel + +## ClvpEncoder + +[[autodoc]] ClvpEncoder + +## ClvpDecoder + +[[autodoc]] ClvpDecoder + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/code_llama.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/code_llama.md new file mode 100644 index 0000000000000000000000000000000000000000..e489bfa031dc62471a06efb6748c5fe35568d13f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/code_llama.md @@ -0,0 +1,126 @@ + + +# CodeLlama + +## Overview + +Code Llama ใƒขใƒ‡ใƒซใฏใซใ‚ˆใฃใฆ [Code Llama: Open Foundation Models for Code](https://ai.meta.com/research/publications/code-llama-open-foundation-models-for-code/) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ Baptiste Roziรจre, Jonas Gehring, Fabian Gloeckle, Sten Sootla, Itai Gat, Xiaoqing Ellen Tan, Yossi Adi, Jingyu Liu, Tal Remez, Jรฉrรฉmy Rapin, Artyom Kozhevnikov, Ivan Evtimov, Joanna Bitton, Manish Bhatt, Cristian Canton Ferrer, Aaron Grattafiori, Wenhan Xiong, Alexandre Dรฉfossez, Jade Copet, Faisal Azhar, Hugo Touvron, Louis Martin, Nicolas Usunier, Thomas Scialom, Gabriel Synnaeve. +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*็งใŸใกใฏ Code Llama ใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ Llama 2 ใซๅŸบใฅใใ‚ณใƒผใƒ‰ใฎๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซ ใƒ•ใ‚กใƒŸใƒชใงใ‚ใ‚Šใ€ใ‚ชใƒผใƒ—ใƒณ ใƒขใƒ‡ใƒซใฎไธญใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ€ๅŸ‹ใ‚่พผใฟๆฉŸ่ƒฝใ€ๅคง่ฆๆจกใชๅ…ฅๅŠ›ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ตใƒใƒผใƒˆใ€ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏใฎใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆๅ‘ฝไปค่ฟฝๅพ“ๆฉŸ่ƒฝใ‚’ๆไพ›ใ—ใพใ™ใ€‚ ใ€‚ๅน…ๅบƒใ„ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ใ‚ซใƒใƒผใ™ใ‚‹ใŸใ‚ใฎ่ค‡ๆ•ฐใฎใƒ•ใƒฌใƒผใƒใƒผใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ๅŸบ็›คใƒขใƒ‡ใƒซ (Code Llama)ใ€Python ็‰นๅŒ– (Code Llama - Python)ใ€ใŠใ‚ˆใณใใ‚Œใžใ‚Œ 7Bใ€13Bใ€ใŠใ‚ˆใณ 34B ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅ‚™ใˆใŸๅ‘ฝไปค่ฟฝๅพ“ใƒขใƒ‡ใƒซ (Code Llama - Instruct) ใงใ™ใ€‚ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏ 16,000 ใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ€ๆœ€ๅคง 100,000 ใƒˆใƒผใ‚ฏใƒณใฎๅ…ฅๅŠ›ใงๆ”นๅ–„ใŒ่ฆ‹ใ‚‰ใ‚Œใพใ™ใ€‚ 7B ใŠใ‚ˆใณ 13B ใ‚ณใƒผใƒ‰ ใƒฉใƒžใจใ‚ณใƒผใƒ‰ ใƒฉใƒž - ๅ‘ฝไปคใƒใƒชใ‚ขใƒณใƒˆใฏใ€ๅ‘จๅ›ฒใฎใ‚ณใƒณใƒ†ใƒณใƒ„ใซๅŸบใฅใ„ใŸๅŸ‹ใ‚่พผใฟใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ Code Llama ใฏใ€ใ„ใใคใ‹ใฎใ‚ณใƒผใƒ‰ ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใ‚ชใƒผใƒ—ใƒณ ใƒขใƒ‡ใƒซใฎไธญใงๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซ้”ใ—ใ€HumanEval ใจ MBPP ใงใใ‚Œใžใ‚Œๆœ€ๅคง 53% ใจ 55% ใฎใ‚นใ‚ณใ‚ขใ‚’็ฒๅพ—ใ—ใพใ—ใŸใ€‚็‰นใซใ€Code Llama - Python 7B ใฏ HumanEval ใŠใ‚ˆใณ MBPP ไธŠใง Llama 2 70B ใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ—ใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏ MultiPL-E ไธŠใงๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใ‚‹ไป–ใฎใ™ในใฆใฎใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใพใ™ใ€‚็งใŸใกใฏใ€็ ”็ฉถใจๅ•†ๆฅญๅˆฉ็”จใฎไธกๆ–นใ‚’่จฑๅฏใ™ใ‚‹ๅฏ›ๅฎนใชใƒฉใ‚คใ‚ปใƒณใ‚นใซๅŸบใฅใ„ใฆ Code Llama ใ‚’ใƒชใƒชใƒผใ‚นใ—ใฆใ„ใพใ™ใ€‚* + +ใ™ในใฆใฎ Code Llama ใƒขใƒ‡ใƒซ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ [ใ“ใกใ‚‰](https://huggingface.co/models?search=code_llama) ใง็ขบ่ชใ—ใ€[meta llama org](https://huggingface.co/meta-llama) ใงๆญฃๅผใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [ArthurZucker](https://huggingface.co/ArthurZ) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚่‘—่€…ใฎใ‚ชใƒชใ‚ธใƒŠใƒซใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/facebookresearch/llama) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips and examples + + + +Code Llama ใฎใƒ™ใƒผใ‚นใจใชใ‚‹`Llama2`ใƒ•ใ‚กใƒŸใƒชใƒผ ใƒขใƒ‡ใƒซใฏใ€`bfloat16`ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใŒใ€ๅ…ƒใฎๆŽจ่ซ–ใงใฏ`float16`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ•ใพใ–ใพใช็ฒพๅบฆใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +* `float32`: ใƒขใƒ‡ใƒซใฎๅˆๆœŸๅŒ–ใซ้–ขใ™ใ‚‹ PyTorch ใฎ่ฆ็ด„ใงใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒใฉใฎ `dtype` ใงๆ ผ็ดใ•ใ‚ŒใŸใ‹ใซ้–ขไฟ‚ใชใใ€ใƒขใƒ‡ใƒซใ‚’ `float32` ใซใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ ใ€Œtransformersใ€ใ‚‚ใ€PyTorch ใจใฎไธ€่ฒซๆ€งใ‚’ไฟใคใŸใ‚ใซใ“ใฎ่ฆๅ‰‡ใซๅพ“ใฃใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง้ธๆŠžใ•ใ‚Œใพใ™ใ€‚ `AutoModel` API ใงใ‚นใƒˆใƒฌใƒผใ‚ธใฎ้‡ใฟไป˜ใ‘ใ‚ฟใ‚คใƒ—ใ‚’ไฝฟ็”จใ—ใฆใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎใƒญใƒผใƒ‰ใ‚’ใ‚ญใƒฃใ‚นใƒˆใ™ใ‚‹ๅ ดๅˆใฏใ€`torch_dtype="auto"` ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`ใ€‚ +* `bfloat16`: ใ‚ณใƒผใƒ‰ Llama ใฏใ“ใฎ็ฒพๅบฆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ•ใ‚‰ใชใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚„ๅพฎ่ชฟๆ•ดใซไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +* `float16`: ใ“ใฎ็ฒพๅบฆใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚้€šๅธธใฏ `bfloat16` ใ‚ˆใ‚Š้ซ˜้€Ÿใงใ‚ใ‚Šใ€่ฉ•ไพกใƒกใƒˆใƒชใ‚ฏใ‚นใซใฏ `bfloat16` ใจๆฏ”ในใฆๆ˜Žใ‚‰ใ‹ใชไฝŽไธ‹ใŒ่ฆ‹ใ‚‰ใ‚Œใชใ„ใŸใ‚ใงใ™ใ€‚ bfloat16 ใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ๅพฎ่ชฟๆ•ดๅพŒใ€float16 ใจ bfloat16 ใฎไธกๆ–นใงๆŽจ่ซ–็ตๆžœใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +ไธŠใง่ฟฐในใŸใ‚ˆใ†ใซใ€ใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹ใจใใซ `torch_dtype="auto"` ใ‚’ไฝฟ็”จใ—ใชใ„้™ใ‚Šใ€ใ‚นใƒˆใƒฌใƒผใ‚ธใฎ้‡ใฟใฎ `dtype` ใฏใปใจใ‚“ใฉ็„ก้–ขไฟ‚ใงใ™ใ€‚ใใฎ็†็”ฑใฏใ€ใƒขใƒ‡ใƒซใŒๆœ€ๅˆใซใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œ (ใ‚ชใƒณใƒฉใ‚คใƒณใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ `dtype` ใ‚’ไฝฟ็”จ)ใ€ๆฌกใซ `torch` ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ `dtype` ใซใ‚ญใƒฃใ‚นใƒˆใ•ใ‚Œใ‚‹ใŸใ‚ใงใ™ (`torch.float32` ใซใชใ‚Šใพใ™)ใ€‚ๆŒ‡ๅฎšใ•ใ‚ŒใŸ `torch_dtype` ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ไปฃใ‚ใ‚Šใซใใ‚ŒใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + + +ใƒใƒƒใƒ—๏ผš +- ๅ……ๅกซใ‚ฟใ‚นใ‚ฏใฏใ™ใใซใ‚ตใƒใƒผใƒˆใ•ใ‚Œใพใ™ใ€‚ๅ…ฅๅŠ›ใ‚’ๅŸ‹ใ‚ใŸใ„ๅ ดๆ‰€ใซใฏ `tokenizer.fill_token` ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ใƒขใƒ‡ใƒซๅค‰ๆ›ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€`Llama2` ใƒ•ใ‚กใƒŸใƒชใฎๅ ดๅˆใจๅŒใ˜ใงใ™ใ€‚ + +ไฝฟ็”จไพ‹ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```bash +python src/transformers/models/llama/convert_llama_weights_to_hf.py \ + --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path +``` + +ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€(ๆœ€ๅคงใฎใƒใƒผใ‚ธใƒงใƒณใงใ‚ใฃใฆใ‚‚) float16 ็ฒพๅบฆใงใƒขใƒ‡ใƒซๅ…จไฝ“ใ‚’ใƒ›ใ‚นใƒˆใ™ใ‚‹ใฎใซๅๅˆ†ใช CPU RAM ใŒๅฟ…่ฆใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +ใ„ใใคใ‹ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใ‚ใ‚Šใ€ใใ‚Œใžใ‚Œใซใƒขใƒ‡ใƒซใฎๅ„้‡ใฟใฎไธ€้ƒจใŒๅซใพใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ™ในใฆใ‚’ RAM ใซใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ + +ๅค‰ๆ›ๅพŒใ€ใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏๆฌกใฎๆ–นๆณ•ใงใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```python +>>> from transformers import LlamaForCausalLM, CodeLlamaTokenizer + +>>> tokenizer = CodeLlamaTokenizer.from_pretrained("meta-llama/CodeLlama-7b-hf") +>>> model = LlamaForCausalLM.from_pretrained("meta-llama/CodeLlama-7b-hf") +>>> PROMPT = '''def remove_non_ascii(s: str) -> str: + """ + return result +''' +>>> input_ids = tokenizer(PROMPT, return_tensors="pt")["input_ids"] +>>> generated_ids = model.generate(input_ids, max_new_tokens=128) + +>>> filling = tokenizer.batch_decode(generated_ids[:, input_ids.shape[1]:], skip_special_tokens = True)[0] +>>> print(PROMPT.replace("", filling)) +def remove_non_ascii(s: str) -> str: + """ Remove non-ASCII characters from a string. + + Args: + s: The string to remove non-ASCII characters from. + + Returns: + The string with non-ASCII characters removed. + """ + result = "" + for c in s: + if ord(c) < 128: + result += c + return result +``` + +ๅก—ใ‚Šใคใถใ•ใ‚ŒใŸ้ƒจๅˆ†ใ ใ‘ใŒๅฟ…่ฆใชๅ ดๅˆ: + +```python +>>> from transformers import pipeline +>>> import torch + +>>> generator = pipeline("text-generation",model="meta-llama/CodeLlama-7b-hf",torch_dtype=torch.float16, device_map="auto") +>>> generator('def remove_non_ascii(s: str) -> str:\n """ \n return result', max_new_tokens = 128) +[{'generated_text': 'def remove_non_ascii(s: str) -> str:\n """ \n return resultRemove non-ASCII characters from a string. """\n result = ""\n for c in s:\n if ord(c) < 128:\n result += c'}] +``` + +ๅ†…้ƒจใงใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒ [`` ใซใ‚ˆใฃใฆ่‡ชๅ‹•็š„ใซๅˆ†ๅ‰ฒ](https://huggingface.co/docs/transformers/main/model_doc/code_llama#transformers.CodeLlamaTokenizer.fill_token) ใ—ใฆใ€[ ใซ็ถšใๆ›ธๅผ่จญๅฎšใ•ใ‚ŒใŸๅ…ฅๅŠ›ๆ–‡ๅญ—ๅˆ—ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‘ใ‚ฟใƒผใƒณ](https://github.com/facebookresearch/codellama/blob/cb51c14ec761370ba2e2bc351374a79265d0465e/llama/generation.py#L402)ใ€‚ใ“ใ‚Œใฏใ€ใƒ‘ใ‚ฟใƒผใƒณใ‚’่‡ชๅˆ†ใงๆบ–ๅ‚™ใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚ๅ …็‰ขใงใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณใฎๆŽฅ็€ใชใฉใ€ใƒ‡ใƒใƒƒใ‚ฐใŒ้žๅธธใซ้›ฃใ—ใ„่ฝใจใ—็ฉดใ‚’ๅ›ž้ฟใงใใพใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใพใŸใฏไป–ใฎใƒขใƒ‡ใƒซใซๅฟ…่ฆใช CPU ใŠใ‚ˆใณ GPU ใƒกใƒขใƒชใฎ้‡ใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€ใใฎๅ€คใ‚’ๆฑบๅฎšใ™ใ‚‹ใฎใซๅฝน็ซ‹ใค [ใ“ใฎ่จˆ็ฎ—ใƒ„ใƒผใƒซ](https://huggingface.co/spaces/hf-accelerate/model-memory-usage) ใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ + +LLaMA ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏใ€[sentencepiece](https://github.com/google/sentencepiece) ใซๅŸบใฅใ BPE ใƒขใƒ‡ใƒซใงใ™ใ€‚ใ‚ปใƒณใƒ†ใƒณใ‚นใƒ”ใƒผใ‚นใฎ็™–ใฎ 1 ใคใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ใจใใซใ€ๆœ€ๅˆใฎใƒˆใƒผใ‚ฏใƒณใŒๅ˜่ชžใฎๅ…ˆ้ ญ (ไพ‹: ใ€ŒBananaใ€) ใงใ‚ใ‚‹ๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏๆ–‡ๅญ—ๅˆ—ใฎๅ…ˆ้ ญใซใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚น ใ‚นใƒšใƒผใ‚นใ‚’่ฟฝๅŠ ใ—ใชใ„ใ“ใจใงใ™ใ€‚ + + + +ใ‚ณใƒผใƒ‰ Llama ใฏใ€`Llama2` ใƒขใƒ‡ใƒซใจๅŒใ˜ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใซใคใ„ใฆใฏใ€[Llama2 ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒšใƒผใ‚ธ](llama2) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ไปฅไธ‹ใฎ Code Llama ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใ‚’่ฆ‹ใคใ‘ใฆใใ ใ•ใ„ใ€‚ + + +## CodeLlamaTokenizer + +[[autodoc]] CodeLlamaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## CodeLlamaTokenizerFast + +[[autodoc]] CodeLlamaTokenizerFast + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - update_post_processor + - save_vocabulary diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/codegen.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/codegen.md new file mode 100644 index 0000000000000000000000000000000000000000..28ab376260bd43fe6cda5b08c534dee433314e84 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/codegen.md @@ -0,0 +1,90 @@ + + +# CodeGen + +## Overview + + +CodeGen ใƒขใƒ‡ใƒซใฏใ€[A Conversational Paradigm for Program Synthesis](https://huggingface.co/papers/2203.13474) ใง Erik Nijkampใ€Bo Pangใ€ๆž—ๅฎๆ˜Žใ€Lifu Tuใ€Huan Wangใ€Yingbo Zhouใ€Silvio Savareseใ€Caiming Xiong ใŠใ‚ˆใณใ‚ซใ‚คใƒŸใƒณใƒปใ‚ทใƒงใƒณใ•ใ‚“ใ€‚ + +CodeGen ใฏใ€[The Pile](https://pile.eleuther.ai/)ใ€BigQueryใ€BigPython ใง้ †ๆฌกใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆ็”จใฎ่‡ชๅทฑๅ›žๅธฐ่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆใฏใ€ไธŽใˆใ‚‰ใ‚ŒใŸๅ•้กŒไป•ๆง˜ใฎ่งฃๆฑบ็ญ–ใจใ—ใฆใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’็”Ÿๆˆใ™ใ‚‹ใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ๆˆ‘ใ€…ใฏใ€ๅคง่ฆๆจกใช่จ€่ชžใƒขใƒ‡ใƒซใ‚’ไป‹ใ—ใŸไผš่ฉฑๅž‹ใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๆๆกˆใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅพ“ๆฅใฎใ‚ขใƒ—ใƒญใƒผใƒใง็›ด้ขใ—ใŸๅบƒๅคงใชใƒ—ใƒญใ‚ฐใƒฉใƒ ็ฉบ้–“ใจใƒฆใƒผใ‚ถใƒผใฎๆ„ๅ›ณใฎไป•ๆง˜ใ‚’ๆคœ็ดขใ™ใ‚‹ใจใ„ใ†่ชฒ้กŒใซๅฏพๅ‡ฆใ—ใพใ™ใ€‚็งใŸใกใฎๆ–ฐใ—ใ„ใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€ไป•ๆง˜ใจใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ไฝœๆˆใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใ‚’ใ€ใƒฆใƒผใ‚ถใƒผใจใ‚ทใ‚นใƒ†ใƒ ใฎ้–“ใฎ่ค‡ๆ•ฐๅ›žใฎๅฏพ่ฉฑใจใ—ใฆๆ‰ใˆใพใ™ใ€‚ใ“ใ‚Œใฏใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆใ‚’ใ‚ทใƒผใ‚ฑใƒณใ‚นไบˆๆธฌๅ•้กŒใจใ—ใฆๆ‰ฑใ„ใ€ไป•ๆง˜ใŒ่‡ช็„ถ่จ€่ชžใง่กจ็พใ•ใ‚Œใ€็›ฎ็š„ใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใŒๆกไปถไป˜ใใงใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚็งใŸใกใฏใ€่‡ช็„ถ่จ€่ชžใจใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใฎใƒ‡ใƒผใ‚ฟใซๅŸบใฅใ„ใฆใ€CodeGen ใจๅ‘ผใฐใ‚Œใ‚‹ๅคง่ฆๆจกใช่จ€่ชžใƒขใƒ‡ใƒซใฎใƒ•ใ‚กใƒŸใƒชใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใฎ็›ฃ่ฆ–ใŒๅผฑใใ€ใƒ‡ใƒผใ‚ฟ ใ‚ตใ‚คใ‚บใจใƒขใƒ‡ใƒซ ใ‚ตใ‚คใ‚บใŒๆ‹กๅคงใ™ใ‚‹ใจใ€ๅ˜็ด”ใช่‡ชๅทฑๅ›žๅธฐ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ‹ใ‚‰ไผš่ฉฑ่ƒฝๅŠ›ใŒ็”Ÿใพใ‚Œใพใ™ใ€‚ไผš่ฉฑๅž‹ใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆใซใŠใ‘ใ‚‹ใƒขใƒ‡ใƒซใฎๅ‹•ไฝœใ‚’็ ”็ฉถใ™ใ‚‹ใŸใ‚ใซใ€ใƒžใƒซใƒใ‚ฟใƒผใƒณ ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ ใƒ™ใƒณใƒใƒžใƒผใ‚ฏ (MTPB) ใ‚’้–‹็™บใ—ใพใ™ใ€‚ใ“ใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใฏใ€ๅ„ๅ•้กŒใ‚’่งฃๆฑบใ™ใ‚‹ใซใฏใ€ใƒฆใƒผใ‚ถใƒผใจใƒขใƒ‡ใƒซ้–“ใฎใƒžใƒซใƒใ‚ฟใƒผใƒณไผš่ฉฑใ‚’ไป‹ใ—ใŸใƒžใƒซใƒใ‚นใƒ†ใƒƒใƒ—ๅˆๆˆใŒๅฟ…่ฆใงใ™ใ€‚็งใŸใกใฎ่ชฟๆŸป็ตๆžœใฏใ€ไผš่ฉฑๆฉŸ่ƒฝใฎๅ‡บ็พใจใ€ๆๆกˆใ•ใ‚Œใฆใ„ใ‚‹ไผš่ฉฑใƒ—ใƒญใ‚ฐใƒฉใƒ ๅˆๆˆใƒ‘ใƒฉใƒ€ใ‚คใƒ ใฎๆœ‰ๅŠนๆ€งใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซใ€็งใŸใกใฎใƒขใƒ‡ใƒซ CodeGen (TPU-v4 ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๆœ€ๅคง 16B ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅซใ‚€) ใฏใ€HumanEval ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใง OpenAI ใฎ Codex ใ‚’ไธŠๅ›žใ‚Šใพใ™ใ€‚็งใŸใกใฏใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅซใ‚€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒฉใ‚คใƒ–ใƒฉใƒช JaxFormer ใ‚’ใ‚ชใƒผใƒ—ใƒณ ใ‚ฝใƒผใ‚นใฎใ‚ณใƒณใƒˆใƒชใƒ“ใƒฅใƒผใ‚ทใƒงใƒณใจใ—ใฆๅˆฉ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™: [ใ“ใฎ https URL](https://github.com/salesforce/codegen)*ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [ๆž— ๅฎๆ˜Ž](https://huggingface.co/rooa) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/salesforce/codegen) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Checkpoint Naming + +* CodeGen ใƒขใƒ‡ใƒซ [ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?other=codegen) ใฏใ€ๅฏๅค‰ใ‚ตใ‚คใ‚บใฎใ•ใพใ–ใพใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใงๅˆฉ็”จใงใใพใ™ใ€‚ +* ๅฝขๅผใฏใ€ŒSalesforce/codegen-{size}-{data}ใ€ใงใ™ใ€‚ใ“ใ“ใงใ€ + * `size`: `350M`ใ€`2B`ใ€`6B`ใ€`16B` + * `data`: + * `nl`: ใƒ‘ใ‚คใƒซใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ + * `multi`: `nl` ใงๅˆๆœŸๅŒ–ใ•ใ‚Œใ€่ค‡ๆ•ฐใฎใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใƒ‡ใƒผใ‚ฟใงใ•ใ‚‰ใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + * `mono`: `multi` ใงๅˆๆœŸๅŒ–ใ•ใ‚Œใ€Python ใƒ‡ใƒผใ‚ฟใงใ•ใ‚‰ใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ +* ใŸใจใˆใฐใ€`Salesforce/codegen-350M-mono` ใฏใ€Pileใ€่ค‡ๆ•ฐใฎใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใ€ใŠใ‚ˆใณ Python ใง้ †ๆฌกไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ 3 ๅ„„ 5,000 ไธ‡ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +## Usage example + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> checkpoint = "Salesforce/codegen-350M-mono" +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) + +>>> text = "def hello_world():" + +>>> completion = model.generate(**tokenizer(text, return_tensors="pt")) + +>>> print(tokenizer.decode(completion[0])) +def hello_world(): + print("Hello World") + +hello_world() +``` + +## Resources + +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) + +## CodeGenConfig + +[[autodoc]] CodeGenConfig + - all + +## CodeGenTokenizer + +[[autodoc]] CodeGenTokenizer + - save_vocabulary + +## CodeGenTokenizerFast + +[[autodoc]] CodeGenTokenizerFast + +## CodeGenModel + +[[autodoc]] CodeGenModel + - forward + +## CodeGenForCausalLM + +[[autodoc]] CodeGenForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/conditional_detr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/conditional_detr.md new file mode 100644 index 0000000000000000000000000000000000000000..d0bb090630fa4dadf6b71a27874a5ce67327a7b1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/conditional_detr.md @@ -0,0 +1,80 @@ + + +# Conditional DETR + +## Overview + +ๆกไปถไป˜ใ DETR ใƒขใƒ‡ใƒซใฏใ€[Conditional DETR for Fast Training Convergence](https://huggingface.co/papers/2108.06152) ใง Depu Mengใ€Xiaokang Chenใ€Zejia Fanใ€Gang Zengใ€Houqiang Liใ€Yuhui Yuanใ€Lei Sun, Jingdong Wang ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚็Ž‹ไบฌๆฑใ€‚ๆกไปถไป˜ใ DETR ใฏใ€้ซ˜้€Ÿ DETR ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎๆกไปถไป˜ใใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ๆกไปถไป˜ใ DETR ใฏ DETR ใ‚ˆใ‚Šใ‚‚ 6.7 ๅ€ใ‹ใ‚‰ 10 ๅ€้€ŸใๅŽๆŸใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๆœ€่ฟ‘้–‹็™บใ•ใ‚ŒใŸ DETR ใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใŠใ‚ˆใณใƒ‡ใ‚ณใƒผใƒ€ใƒผ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’็‰ฉไฝ“ๆคœๅ‡บใซ้ฉ็”จใ—ใ€ๆœ‰ๆœ›ใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅŽๆŸใŒ้…ใ„ใจใ„ใ†้‡่ฆใชๅ•้กŒใ‚’ๆ‰ฑใ„ใ€้ซ˜้€Ÿ DETR ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎๆกไปถไป˜ใใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚็งใŸใกใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€DETR ใซใŠใ‘ใ‚‹ใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใŒ 4 ใคใฎๅ››่‚ขใฎไฝ็ฝฎ็‰นๅฎšใจใƒœใƒƒใ‚ฏใ‚นใฎไบˆๆธฌใซใ‚ณใƒณใƒ†ใƒณใƒ„ใฎๅŸ‹ใ‚่พผใฟใซๅคงใใไพๅญ˜ใ—ใฆใ„ใ‚‹ใŸใ‚ใ€้ซ˜ๅ“่ณชใฎใ‚ณใƒณใƒ†ใƒณใƒ„ใฎๅŸ‹ใ‚่พผใฟใฎๅฟ…่ฆๆ€งใŒ้ซ˜ใพใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ้›ฃๆ˜“ๅบฆใŒ้ซ˜ใใชใ‚‹ใจใ„ใ†็‚นใซๅ‹•ๆฉŸใฅใ‘ใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ๆกไปถไป˜ใ DETR ใจๅ‘ผใฐใ‚Œใ‚‹็งใŸใกใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎใƒžใƒซใƒใƒ˜ใƒƒใƒ‰ ใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฎใŸใ‚ใซใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎๅŸ‹ใ‚่พผใฟใ‹ใ‚‰ๆกไปถไป˜ใใฎ็ฉบ้–“ใ‚ฏใ‚จใƒชใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ๅˆฉ็‚นใฏใ€ๆกไปถไป˜ใ็ฉบ้–“ใ‚ฏใ‚จใƒชใ‚’้€šใ˜ใฆใ€ๅ„ใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒ˜ใƒƒใƒ‰ใŒใ€ๅ€‹ๅˆฅใฎ้ ˜ๅŸŸ (ใŸใจใˆใฐใ€1 ใคใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ็ซฏใพใŸใฏใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใƒœใƒƒใ‚ฏใ‚นๅ†…ใฎ้ ˜ๅŸŸ) ใ‚’ๅซใ‚€ใƒใƒณใƒ‰ใซๆณจ็›ฎใงใใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๅˆ†้กžใจใƒœใƒƒใ‚ฏใ‚นๅ›žๅธฐใฎใŸใ‚ใฎๅ€‹ๅˆฅใฎ้ ˜ๅŸŸใ‚’ใƒญใƒผใ‚ซใƒฉใ‚คใ‚บใ™ใ‚‹ใŸใ‚ใฎ็ฉบ้–“็ฏ„ๅ›ฒใŒ็‹ญใพใ‚Šใ€ใ‚ณใƒณใƒ†ใƒณใƒ„ใฎๅŸ‹ใ‚่พผใฟใธใฎไพๅญ˜ใŒ็ทฉๅ’Œใ•ใ‚Œใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎนๆ˜“ใซใชใ‚Šใพใ™ใ€‚ๅฎŸ้จ“็ตๆžœใฏใ€ๆกไปถไป˜ใ DETR ใŒใƒใƒƒใ‚ฏใƒœใƒผใƒณ R50 ใŠใ‚ˆใณ R101 ใง 6.7 ๅ€้€ŸใๅŽๆŸใ—ใ€ใ‚ˆใ‚ŠๅผทๅŠ›ใชใƒใƒƒใ‚ฏใƒœใƒผใƒณ DC5-R50 ใŠใ‚ˆใณ DC5-R101 ใง 10 ๅ€้€ŸใๅŽๆŸใ™ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ‚ณใƒผใƒ‰ใฏ https://github.com/Atten4Vis/ConditionalDETR ใงๅ…ฅๆ‰‹ใงใใพใ™ใ€‚* + + + + ๆกไปถไป˜ใ DETR ใฏใ€ๅ…ƒใฎ DETR ใซๆฏ”ในใฆใฏใ‚‹ใ‹ใซ้€Ÿใ„ๅŽๆŸใ‚’็คบใ—ใพใ™ใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๅผ•็”จใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [DepuMeng](https://huggingface.co/DepuMeng) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/Atten4Vis/ConditionalDETR) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +- [ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/object_detection) + +## ConditionalDetrConfig + +[[autodoc]] ConditionalDetrConfig + +## ConditionalDetrImageProcessor + +[[autodoc]] ConditionalDetrImageProcessor + - preprocess + +## ConditionalDetrImageProcessorFast + +[[autodoc]] ConditionalDetrImageProcessorFast + - preprocess + - post_process_object_detection + - post_process_instance_segmentation + - post_process_semantic_segmentation + - post_process_panoptic_segmentation + +## ConditionalDetrFeatureExtractor + +[[autodoc]] ConditionalDetrFeatureExtractor + - __call__ + - post_process_object_detection + - post_process_instance_segmentation + - post_process_semantic_segmentation + - post_process_panoptic_segmentation + +## ConditionalDetrModel + +[[autodoc]] ConditionalDetrModel + - forward + +## ConditionalDetrForObjectDetection + +[[autodoc]] ConditionalDetrForObjectDetection + - forward + +## ConditionalDetrForSegmentation + +[[autodoc]] ConditionalDetrForSegmentation + - forward + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convbert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convbert.md new file mode 100644 index 0000000000000000000000000000000000000000..c581b715db96923e4b510d4ac79f86c53a50c1a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convbert.md @@ -0,0 +1,145 @@ + + +# ConvBERT + +
+ +Models + + +Spaces + +
+ +## Overview + +ConvBERT ใƒขใƒ‡ใƒซใฏใ€[ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) ใง Zihang Jiangใ€Weihao Yuใ€Daquan Zhouใ€Yunpeng Chenใ€Jiashi Fengใ€Shuicheng Yan ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚„ใ‚“ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*BERT ใ‚„ใใฎใƒใƒชใ‚ขใƒณใƒˆใชใฉใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ๆœ€่ฟ‘ใ€ใ•ใพใ–ใพใช็’ฐๅขƒใง็›ฎ่ฆšใพใ—ใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใ—ใฆใ„ใพใ™ใ€‚ +่‡ช็„ถ่จ€่ชž็†่งฃใ‚ฟใ‚นใ‚ฏใ€‚ใŸใ ใ—ใ€BERT ใฏใ‚ฐใƒญใƒผใƒใƒซใช่‡ชๅทฑๆณจๆ„ใƒ–ใƒญใƒƒใ‚ฏใซๅคงใใไพๅญ˜ใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ๅ•้กŒใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ +ใƒกใƒขใƒชไฝฟ็”จ้‡ใจ่จˆ็ฎ—ใ‚ณใ‚นใƒˆใŒๅคงใใใชใ‚Šใพใ™ใ€‚ใ™ในใฆใฎๆณจๆ„ใŒๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ…จไฝ“ใซๅฏพใ—ใฆใ‚ฏใ‚จใƒชใ‚’ๅฎŸ่กŒใ—ใพใ™ใŒใ€ +ใ‚ฐใƒญใƒผใƒใƒซใช่ฆณ็‚นใ‹ใ‚‰ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒžใƒƒใƒ—ใ‚’็”Ÿๆˆใ™ใ‚‹ใจใ€ไธ€้ƒจใฎใƒ˜ใƒƒใƒ‰ใฏใƒญใƒผใ‚ซใƒซใชไพๅญ˜้–ขไฟ‚ใฎใฟใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€่จˆ็ฎ—ใฎๅ†—้•ทๆ€งใŒๅญ˜ๅœจใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆˆ‘ใ€…ใฏใ€ๆ–ฐใ—ใ„ใ‚นใƒ‘ใƒณใƒ™ใƒผใ‚นใฎๅ‹•็š„็•ณใฟ่พผใฟใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒ˜ใƒƒใƒ‰ใ‚’็ฝฎใๆ›ใˆใฆใ€ใƒญใƒผใ‚ซใƒซใฎไพๅญ˜้–ขไฟ‚ใ‚’็›ดๆŽฅใƒขใƒ‡ใƒซๅŒ–ใ—ใพใ™ใ€‚ๆ–ฐใ—ใ„ใ‚ณใƒณใƒœใƒชใƒฅใƒผใ‚ทใƒงใƒณใƒ˜ใƒƒใƒ‰ใจใ€ +่‡ชๅทฑๆณจๆ„ใฎ้ ญใ‚’ไผ‘ใ‚ใ€ใ‚ฐใƒญใƒผใƒใƒซใจใƒญใƒผใ‚ซใƒซใฎไธกๆ–นใฎ็Šถๆณใงใ‚ˆใ‚ŠๅŠน็އ็š„ใชๆ–ฐใ—ใ„ๆททๅˆๆณจๆ„ใƒ–ใƒญใƒƒใ‚ฏใ‚’ๅฝขๆˆใ—ใพใ™ +ๅญฆใถใ€‚ใ“ใฎๆททๅˆๆณจๆ„่จญ่จˆใ‚’ BERT ใซ่ฃ…ๅ‚™ใ—ใ€ConvBERT ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ๅฎŸ้จ“ใงใ‚ใ‹ใฃใŸใ“ใจใฏใ€ +ConvBERT ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ณใ‚นใƒˆใŒไฝŽใใ€ใ•ใพใ–ใพใชไธ‹ๆตใ‚ฟใ‚นใ‚ฏใซใŠใ„ใฆ BERT ใŠใ‚ˆใณใใฎไบœ็จฎใ‚ˆใ‚Šใ‚‚ๅคงๅน…ใซๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ๆณจ็›ฎใ™ในใใ“ใจใซใ€ConvBERTbase ใƒขใƒ‡ใƒซใฏ 86.4 GLUE ใ‚นใ‚ณใ‚ขใ‚’้”ๆˆใ—ใ€ELECTRAbase ใ‚ˆใ‚Šใ‚‚ 0.7 ้ซ˜ใ„ใฎใซๅฏพใ—ใ€ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใ‚นใƒˆใฏ 1/4 ๆœชๆบ€ใงใ™ใ€‚ใ‚ณใƒผใƒ‰ใจไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[abhishek](https://huggingface.co/abhishek) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎๅฎŸ่ฃ…ใŒ่ฆ‹ใคใ‹ใ‚Šใพใ™ +ใ“ใ“: https://github.com/yitu-opensource/ConvBert + +## Usage tips + +ConvBERT ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ’ใƒณใƒˆใฏ BERT ใฎใƒ’ใƒณใƒˆใจไผผใฆใ„ใพใ™ใ€‚ไฝฟ็”จไธŠใฎใƒ’ใƒณใƒˆใซใคใ„ใฆใฏใ€[BERT ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](bert) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Resources + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_lang_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## ConvBertConfig + +[[autodoc]] ConvBertConfig + +## ConvBertTokenizer + +[[autodoc]] ConvBertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## ConvBertTokenizerFast + +[[autodoc]] ConvBertTokenizerFast + + + + +## ConvBertModel + +[[autodoc]] ConvBertModel + - forward + +## ConvBertForMaskedLM + +[[autodoc]] ConvBertForMaskedLM + - forward + +## ConvBertForSequenceClassification + +[[autodoc]] ConvBertForSequenceClassification + - forward + +## ConvBertForMultipleChoice + +[[autodoc]] ConvBertForMultipleChoice + - forward + +## ConvBertForTokenClassification + +[[autodoc]] ConvBertForTokenClassification + - forward + +## ConvBertForQuestionAnswering + +[[autodoc]] ConvBertForQuestionAnswering + - forward + + + + +## TFConvBertModel + +[[autodoc]] TFConvBertModel + - call + +## TFConvBertForMaskedLM + +[[autodoc]] TFConvBertForMaskedLM + - call + +## TFConvBertForSequenceClassification + +[[autodoc]] TFConvBertForSequenceClassification + - call + +## TFConvBertForMultipleChoice + +[[autodoc]] TFConvBertForMultipleChoice + - call + +## TFConvBertForTokenClassification + +[[autodoc]] TFConvBertForTokenClassification + - call + +## TFConvBertForQuestionAnswering + +[[autodoc]] TFConvBertForQuestionAnswering + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnext.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnext.md new file mode 100644 index 0000000000000000000000000000000000000000..336f27709d409643264b92c63bdca1e842eb5d17 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnext.md @@ -0,0 +1,99 @@ + + +# ConvNeXT + +## Overview + +ConvNeXT ใƒขใƒ‡ใƒซใฏใ€[A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) ใง Zhuang Liuใ€Hanzi Maoใ€Chao-Yuan Wuใ€Christoph Feichtenhoferใ€Trevor Darrellใ€Saining Xie ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ConvNeXT ใฏใ€ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎ่จญ่จˆใ‹ใ‚‰ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใŸ็ด”็ฒ‹ใช็•ณใฟ่พผใฟใƒขใƒ‡ใƒซ (ConvNet) ใงใ‚ใ‚Šใ€ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ™ใ‚‹ใจไธปๅผตใ—ใฆใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*่ฆ–่ฆš่ช่ญ˜ใฎใ€Œ็‹‚้จ’ใฎ 20 ๅนดไปฃใ€ใฏใ€ๆœ€ๅ…ˆ็ซฏใฎ็”ปๅƒๅˆ†้กžใƒขใƒ‡ใƒซใจใ—ใฆ ConvNet ใซใ™ใใซๅ–ใฃใฆไปฃใ‚ใ‚‰ใ‚ŒใŸ Vision Transformers (ViT) ใฎๅฐŽๅ…ฅใ‹ใ‚‰ๅง‹ใพใ‚Šใพใ—ใŸใ€‚ +ไธ€ๆ–นใ€ใƒใƒ‹ใƒฉ ViT ใฏใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚„ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใชใฉใฎไธ€่ˆฌ็š„ใชใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใซ้ฉ็”จใ™ใ‚‹ใจๅ›ฐ้›ฃใซ็›ด้ขใ—ใพใ™ใ€‚้šŽๅฑคๅž‹ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ™ +(Swin Transformers ใชใฉ) ใฏใ€ใ„ใใคใ‹ใฎ ConvNet ใฎไปฅๅ‰ใฎๆฉŸ่ƒฝใ‚’ๅ†ๅฐŽๅ…ฅใ—ใ€Transformers ใ‚’ๆฑŽ็”จใƒ“ใ‚ธใƒงใƒณ ใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆๅฎŸ็”จ็š„ใซๅฏ่ƒฝใซใ—ใ€ๅน…ๅบƒใ„็’ฐๅขƒใง้ก•่‘—ใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใพใ—ใŸใ€‚ +ใ•ใพใ–ใพใช่ฆ–่ฆšใ‚ฟใ‚นใ‚ฏใ€‚ใŸใ ใ—ใ€ใ“ใฎใ‚ˆใ†ใชใƒใ‚คใƒ–ใƒชใƒƒใƒ‰ ใ‚ขใƒ—ใƒญใƒผใƒใฎๆœ‰ๅŠนๆ€งใฏใ€ไพ็„ถใจใ—ใฆใ€ๅ›บๆœ‰ใฎ่ช˜ๅฐŽๆ€งใงใฏใชใใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎๆœฌ่ณช็š„ใชๅ„ชไฝๆ€งใซใ‚ˆใ‚‹ใจใ“ใ‚ใŒๅคงใใ„ใจ่€ƒใˆใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ +็•ณใฟ่พผใฟใฎใƒใ‚คใ‚ขใ‚นใ€‚ใ“ใฎไฝœๆฅญใงใฏใ€่จญ่จˆ็ฉบ้–“ใ‚’ๅ†ๆคœ่จŽใ—ใ€็ด”็ฒ‹ใช ConvNet ใŒ้”ๆˆใงใใ‚‹้™็•Œใ‚’ใƒ†ใ‚นใƒˆใ—ใพใ™ใ€‚ๆจ™ๆบ– ResNet ใ‚’่จญ่จˆใซๅ‘ใ‘ใฆๅพใ€…ใซใ€Œๆœ€ๆ–ฐๅŒ–ใ€ใ—ใพใ™ใ€‚ +ใƒ“ใ‚ธใƒงใƒณ Transformer ใฎๆฆ‚่ฆใ‚’็ขบ่ชใ—ใ€้€”ไธญใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎ้•ใ„ใซๅฏ„ไธŽใ™ใ‚‹ใ„ใใคใ‹ใฎ้‡่ฆใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚’็™บ่ฆ‹ใ—ใพใ™ใ€‚ใ“ใฎ่ชฟๆŸปใฎ็ตๆžœใฏใ€็ด”็ฒ‹ใช ConvNet ใƒขใƒ‡ใƒซใฎใƒ•ใ‚กใƒŸใƒชใƒผใงใ™ใ€‚ +ConvNextใจๅ‘ผใฐใ‚Œใพใ™ใ€‚ ConvNeXts ใฏๅฎŒๅ…จใซๆจ™ๆบ–ใฎ ConvNet ใƒขใ‚ธใƒฅใƒผใƒซใ‹ใ‚‰ๆง‹็ฏ‰ใ•ใ‚ŒใฆใŠใ‚Šใ€็ฒพๅบฆใจๆ‹กๅผตๆ€งใฎ็‚นใง Transformers ใจๆœ‰ๅˆฉใซ็ซถๅˆใ—ใ€87.8% ใฎ ImageNet ใƒˆใƒƒใƒ— 1 ็ฒพๅบฆใ‚’้”ๆˆใ—ใฆใ„ใพใ™ใ€‚ +ๆจ™ๆบ– ConvNet ใฎใ‚ทใƒณใƒ—ใƒซใ•ใจๅŠน็އใ‚’็ถญๆŒใ—ใชใŒใ‚‰ใ€COCO ๆคœๅ‡บใจ ADE20K ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใงใฏ Swin Transformers ใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚* + + + + ConvNeXT ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ TensorFlow ใƒใƒผใ‚ธใƒงใƒณใฎใƒขใƒ‡ใƒซใฏ [ariG23498](https://github.com/ariG23498) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +[gante](https://github.com/gante)ใ€ใŠใ‚ˆใณ [sayakpaul](https://github.com/sayakpaul) (ๅŒ็ญ‰ใฎ่ฒข็Œฎ)ใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/facebookresearch/ConvNeXt) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +ConvNeXT ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`ConvNextForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## ConvNextConfig + +[[autodoc]] ConvNextConfig + +## ConvNextFeatureExtractor + +[[autodoc]] ConvNextFeatureExtractor + +## ConvNextImageProcessor + +[[autodoc]] ConvNextImageProcessor + - preprocess + +## ConvNextImageProcessorFast + +[[autodoc]] ConvNextImageProcessorFast + - preprocess + + + + +## ConvNextModel + +[[autodoc]] ConvNextModel + - forward + +## ConvNextForImageClassification + +[[autodoc]] ConvNextForImageClassification + - forward + + + + +## TFConvNextModel + +[[autodoc]] TFConvNextModel + - call + +## TFConvNextForImageClassification + +[[autodoc]] TFConvNextForImageClassification + - call + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnextv2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnextv2.md new file mode 100644 index 0000000000000000000000000000000000000000..cadd5e8ca0bf00c2a9ecc7d01c50562e53ace4e0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/convnextv2.md @@ -0,0 +1,68 @@ + + +# ConvNeXt V2 + +## Overview + +ConvNeXt V2 ใƒขใƒ‡ใƒซใฏใ€Sanghyun Wooใ€Shobhik Debnathใ€Ronghang Huใ€Xinlei Chenใ€Zhuang Liu, In So Kweon, Saining Xie. ใซใ‚ˆใฃใฆ [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ConvNeXt V2 ใฏใ€Vision Transformers ใฎ่จญ่จˆใ‹ใ‚‰ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใŸ็ด”็ฒ‹ใช็•ณใฟ่พผใฟใƒขใƒ‡ใƒซ (ConvNet) ใงใ‚ใ‚Šใ€[ConvNeXT](convnext) ใฎๅพŒ็ถ™ใงใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎๆ”นๅ–„ใจ่กจ็พๅญฆ็ฟ’ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎๆ”นๅ–„ใซใ‚ˆใ‚Šใ€่ฆ–่ฆš่ช่ญ˜ใฎๅˆ†้‡Žใฏ 2020 ๅนดไปฃๅˆ้ ญใซๆ€ฅ้€Ÿใช่ฟ‘ไปฃๅŒ–ใจใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใ‚’ๅฎŸ็พใ—ใพใ—ใŸใ€‚ใŸใจใˆใฐใ€ConvNeXt ใซไปฃ่กจใ•ใ‚Œใ‚‹ๆœ€ๆ–ฐใฎ ConvNet ใฏใ€ใ•ใพใ–ใพใชใ‚ทใƒŠใƒชใ‚ชใงๅผทๅŠ›ใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ‚‚ใจใ‚‚ใจ ImageNet ใƒฉใƒ™ใƒซใ‚’ไฝฟ็”จใ—ใŸๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’็”จใซ่จญ่จˆใ•ใ‚Œใพใ—ใŸใŒใ€ใƒžใ‚นใ‚ฏ ใ‚ชใƒผใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ (MAE) ใชใฉใฎ่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ๆ‰‹ๆณ•ใ‹ใ‚‰ใ‚‚ๆฝœๅœจ็š„ใซๆฉๆตใ‚’ๅ—ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใ‚Œใ‚‰ 2 ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅ˜็ด”ใซ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใจใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๆจ™ๆบ–ไปฅไธ‹ใซใชใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๅฎŒๅ…จ็•ณใฟ่พผใฟใƒžใ‚นใ‚ฏ ใ‚ชใƒผใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใจใ€ใƒใƒฃใƒใƒซ้–“ใฎๆฉŸ่ƒฝ็ซถๅˆใ‚’ๅผทๅŒ–ใ™ใ‚‹ใŸใ‚ใซ ConvNeXt ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซ่ฟฝๅŠ ใงใใ‚‹ๆ–ฐใ—ใ„ Global Response Normalization (GRN) ๅฑคใ‚’ๆๆกˆใ—ใพใ™ใ€‚ใ“ใฎ่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ๆ‰‹ๆณ•ใจใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎๆ”นๅ–„ใฎๅ…ฑๅŒ่จญ่จˆใซใ‚ˆใ‚Šใ€ConvNeXt V2 ใจๅ‘ผใฐใ‚Œใ‚‹ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซ ใƒ•ใ‚กใƒŸใƒชใŒ่ช•็”Ÿใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ImageNet ๅˆ†้กžใ€COCO ๆคœๅ‡บใ€ADE20K ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใชใฉใฎใ•ใพใ–ใพใช่ช่ญ˜ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใŠใ‘ใ‚‹็ด”็ฒ‹ใช ConvNet ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ™ใ€‚ใพใŸใ€ImageNet ใงใƒˆใƒƒใƒ— 1 ใฎ็ฒพๅบฆ 76.7% ใ‚’่ช‡ใ‚‹ๅŠน็އ็š„ใช 370 ไธ‡ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ Atto ใƒขใƒ‡ใƒซใ‹ใ‚‰ใ€ๆœ€ๅ…ˆ็ซฏใฎ 88.9% ใ‚’้”ๆˆใ™ใ‚‹ 650M Huge ใƒขใƒ‡ใƒซใพใงใ€ใ•ใพใ–ใพใชใ‚ตใ‚คใ‚บใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ ConvNeXt V2 ใƒขใƒ‡ใƒซใ‚‚ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ๅ…ฌ้–‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใฎใฟใ‚’ไฝฟ็”จใ—ใŸ็ฒพๅบฆ*ใ€‚ + + + + ConvNeXt V2 ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [adirik](https://huggingface.co/adirik) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/facebookresearch/ConvNeXt-V2) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +ConvNeXt V2 ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`ConvNextV2ForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## ConvNextV2Config + +[[autodoc]] ConvNextV2Config + +## ConvNextV2Model + +[[autodoc]] ConvNextV2Model + - forward + +## ConvNextV2ForImageClassification + +[[autodoc]] ConvNextV2ForImageClassification + - forward + +## TFConvNextV2Model + +[[autodoc]] TFConvNextV2Model + - call + + +## TFConvNextV2ForImageClassification + +[[autodoc]] TFConvNextV2ForImageClassification + - call diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpm.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpm.md new file mode 100644 index 0000000000000000000000000000000000000000..e10e8af7751b0e1b600493bf80955b41455edfb6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpm.md @@ -0,0 +1,54 @@ + + +# CPM + +## Overview + +CPM ใƒขใƒ‡ใƒซใฏใ€Zhengyan Zhangใ€Xu Hanใ€Hao Zhouใ€Pei Keใ€Yuxian Gu ใซใ‚ˆใฃใฆ [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚่‘‰ๅพณๆ˜Žใ€็งฆ่ฃ•ไฝณใ€ +Yusheng Suใ€Haozhe Jiใ€Jian Guanใ€Fanchao Qiใ€Xiaozi Wangใ€Yanan Zhengใ€Guoyang Zengใ€Huanqi Caoใ€Shengqi Chenใ€ +Daixuan Liใ€Zhenbo Sunใ€Zhiyuan Liuใ€Minlie Huangใ€Wentao Hanใ€Jie Tangใ€Juanzi Liใ€Xiaoyan Zhuใ€Maosong Sunใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซ (PLM) ใฏใ€ใ•ใพใ–ใพใชไธ‹ๆตใฎ NLP ใ‚ฟใ‚นใ‚ฏใซๆœ‰็›Šใงใ‚ใ‚‹ใ“ใจใŒ่จผๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚ๆœ€่ฟ‘ใงใฏGPT-3ใ€ +1,750ๅ„„ๅ€‹ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใจ570GBใฎๅญฆ็ฟ’ใƒ‡ใƒผใ‚ฟใ‚’ๅ‚™ใˆใ€ๆ•ฐๅ›žใฎๆ’ฎๅฝฑ๏ผˆ1ๆžšใงใ‚‚๏ผ‰ใฎๅฎน้‡ใงๅคงใใชๆณจ็›ฎใ‚’้›†ใ‚ใพใ—ใŸ +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ๏ผ‰ๅญฆ็ฟ’ใ€‚ใŸใ ใ—ใ€GPT-3 ใ‚’้ฉ็”จใ—ใฆไธญๅ›ฝ่ชžใฎ NLP ใ‚ฟใ‚นใ‚ฏใซๅฏพๅ‡ฆใ™ใ‚‹ใ“ใจใฏไพ็„ถใจใ—ใฆๅ›ฐ้›ฃใงใ™ใ€‚ +GPT-3 ใฎ่จ€่ชžใฏไธปใซ่‹ฑ่ชžใงใ‚ใ‚Šใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฏๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใ“ใฎๆŠ€่ก“ใƒฌใƒใƒผใƒˆใงใฏใ€ +ๅคง่ฆๆจกใชไธญๅ›ฝ่ชžใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใซๅฏพใ™ใ‚‹็”Ÿๆˆ็š„ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ‚™ใˆใŸไธญๅ›ฝ่ชžไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซ (CPM)ใ€‚ๆœ€้ซ˜ใซ +็งใŸใกใฎ็Ÿฅ่ญ˜ใฎ้™ใ‚Šใงใฏใ€26 ๅ„„ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใจ 100GB ใฎไธญๅ›ฝ่ชžใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใ‚’ๅ‚™ใˆใŸ CPM ใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸไธญๅ›ฝ่ชžใจใ—ใฆใฏๆœ€ๅคงใฎใ‚‚ใฎใงใ™ใ€‚ +่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ไผš่ฉฑใ€ใ‚จใƒƒใ‚ปใ‚คใฎไฝœๆˆใ€ +ใ‚ฏใƒญใƒผใ‚ผใƒ†ใ‚นใƒˆใจ่จ€่ชž็†่งฃใ€‚ๅบƒ็ฏ„ใชๅฎŸ้จ“ใซใ‚ˆใ‚Šใ€CPM ใŒๅคšใใฎ็’ฐๅขƒใงๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใงใใ‚‹ใ“ใจใŒๅฎŸ่จผใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ๅฐ‘ๆ•ฐใ‚ทใƒงใƒƒใƒˆ (ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใงใ‚‚) ๅญฆ็ฟ’ใฎ่จญๅฎšใงใฎ NLP ใ‚ฟใ‚นใ‚ฏใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [canwenxu](https://huggingface.co/canwenxu) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซใฎๅฎŸ่ฃ…ใŒ่ฆ‹ใคใ‹ใ‚Šใพใ™ +ใ“ใ“: https://github.com/TsinghuaAI/CPM-Generate + + + + +CPM ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ–นๆณ•ใ‚’้™คใ„ใฆ GPT-2 ใจๅŒใ˜ใงใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[GPT-2 ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](openai-community/gpt2) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นๆƒ…ๅ ฑใ€‚ + + + +## CpmTokenizer + +[[autodoc]] CpmTokenizer + +## CpmTokenizerFast + +[[autodoc]] CpmTokenizerFast diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpmant.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpmant.md new file mode 100644 index 0000000000000000000000000000000000000000..ca1f65caa16c045c2a22838682dd03d3af7f897f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cpmant.md @@ -0,0 +1,47 @@ + + +# CPMAnt + +## Overview + +CPM-Ant ใฏใ€10B ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ‚™ใˆใŸใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใฎไธญๅ›ฝ่ชžใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ่จ€่ชžใƒขใƒ‡ใƒซ (PLM) ใงใ™ใ€‚ใ“ใ‚Œใฏใ€CPM-Live ใฎใƒฉใ‚คใƒ– ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ—ใƒญใ‚ปใ‚นใฎๆœ€ๅˆใฎใƒžใ‚คใƒซใ‚นใƒˆใƒผใƒณใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใฏ่ฒป็”จๅฏพๅŠนๆžœใŒ้ซ˜ใใ€็’ฐๅขƒใซๅ„ชใ—ใ„ใ‚‚ใฎใงใ™ใ€‚ CPM-Ant ใฏใ€CUGE ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใฎใƒ‡ใƒซใ‚ฟ ใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใงใ‚‚ๆœ‰ๆœ›ใช็ตๆžœใ‚’้”ๆˆใ—ใฆใ„ใพใ™ใ€‚ใƒ•ใƒซ ใƒขใƒ‡ใƒซใซๅŠ ใˆใฆใ€ใ•ใพใ–ใพใชใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขๆง‹ๆˆใฎ่ฆไปถใ‚’ๆบ€ใŸใ™ใ•ใพใ–ใพใชๅœง็ธฎใƒใƒผใ‚ธใƒงใƒณใ‚‚ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ [่ฉณ็ดฐใ‚’่ฆ‹ใ‚‹](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live) + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [OpenBMB](https://huggingface.co/openbmb) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +- [CPM-Live](https://github.com/OpenBMB/CPM-Live/tree/cpm-ant/cpm-live) ใซ้–ขใ™ใ‚‹ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ€‚ + +## CpmAntConfig + +[[autodoc]] CpmAntConfig + - all + +## CpmAntTokenizer + +[[autodoc]] CpmAntTokenizer + - all + +## CpmAntModel + +[[autodoc]] CpmAntModel + - all + +## CpmAntForCausalLM + +[[autodoc]] CpmAntForCausalLM + - all \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/ctrl.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/ctrl.md new file mode 100644 index 0000000000000000000000000000000000000000..508a0d2e432aeec023df4626164dba9e2ac18236 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/ctrl.md @@ -0,0 +1,113 @@ + + +# CTRL + +
+ +Models + + +Spaces + +
+ +## Overview + +CTRL ใƒขใƒ‡ใƒซใฏใ€Nitish Shirish Keskar*ใ€Bryan McCann*ใ€Lav R. Varshneyใ€Caiming Xiong, Richard Socher ใซใ‚ˆใฃใฆ [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใƒชใƒใƒฃใƒผใƒ‰ใƒปใ‚ฝใƒผใƒใƒฃใƒผใ€‚ใ“ใ‚Œใฏใ€้žๅธธใซๅคง่ฆๆจกใชใ‚ณใƒผใƒ‘ใ‚นใฎ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅ› ๆžœ็š„ (ไธ€ๆ–นๅ‘) ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ™ +ๆœ€ๅˆใฎใƒˆใƒผใ‚ฏใƒณใŒๅˆถๅพกใ‚ณใƒผใƒ‰ (ใƒชใƒณใ‚ฏใ€ๆ›ธ็ฑใ€Wikipedia ใชใฉ) ใจใ—ใฆไบˆ็ด„ใ•ใ‚Œใฆใ„ใ‚‹ใ€็ด„ 140 GB ใฎใƒ†ใ‚ญใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๅคง่ฆๆจกใช่จ€่ชžใƒขใƒ‡ใƒซใฏๆœ‰ๆœ›ใชใƒ†ใ‚ญใ‚นใƒˆ็”ŸๆˆๆฉŸ่ƒฝใ‚’็คบใ—ใฆใ„ใพใ™ใŒใ€ใƒฆใƒผใ‚ถใƒผใฏ็‰นๅฎšใฎ่จ€่ชžใƒขใƒ‡ใƒซใ‚’็ฐกๅ˜ใซๅˆถๅพกใงใใพใ›ใ‚“ +็”Ÿๆˆใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใฎๅด้ขใ€‚ 16 ๅ„„ 3,000 ไธ‡ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆกไปถไป˜ใใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ่จ€่ชžใƒขใƒ‡ใƒซใงใ‚ใ‚‹ CTRL ใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ™ใ€‚ +ใ‚นใ‚ฟใ‚คใƒซใ€ใ‚ณใƒณใƒ†ใƒณใƒ„ใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎๅ‹•ไฝœใ‚’ๅˆถๅพกใ™ใ‚‹ๅˆถๅพกใ‚ณใƒผใƒ‰ใ‚’ๆกไปถไป˜ใ‘ใ‚‹ใ‚ˆใ†ใซ่จ“็ทดใ•ใ‚Œใฆใ„ใพใ™ใ€‚ๅˆถๅพกใ‚ณใƒผใƒ‰ใฏ +็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใจ่‡ช็„ถใซๅ…ฑ็”Ÿใ™ใ‚‹ๆง‹้€ ใ‹ใ‚‰ๆดพ็”Ÿใ—ใ€ๆ•™ๅธซใชใ—ๅญฆ็ฟ’ใฎๅˆฉ็‚นใ‚’็ถญๆŒใ—ใชใŒใ‚‰ใ€ +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚’ใ‚ˆใ‚Šๆ˜Ž็คบ็š„ใซๅˆถๅพกใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€CTRL ใงใฉใฎ้ƒจๅˆ†ใŒไบˆๆธฌใ•ใ‚Œใ‚‹ใฎใ‹ใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใซใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใŒไธŽใˆใ‚‰ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒๆœ€ใ‚‚้ซ˜ใใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅคง้‡ใฎใƒ‡ใƒผใ‚ฟใ‚’ๅˆ†ๆžใ™ใ‚‹ใŸใ‚ใฎๆฝœๅœจ็š„ใชๆ–นๆณ•ใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใƒ™ใƒผใ‚นใฎใ‚ฝใƒผใ‚นๅธฐๅฑžใ‚’ไป‹ใ—ใฆใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[keskarnitishr](https://huggingface.co/keskarnitishr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใŒ่ฆ‹ใคใ‹ใ‚‹ +[ใ“ใกใ‚‰](https://github.com/salesforce/Salesforce/ctrl)ใ€‚ + +## Usage tips + +- CTRL ใฏๅˆถๅพกใ‚ณใƒผใƒ‰ใ‚’ๅˆฉ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚็”Ÿๆˆใ‚’็‰นๅฎšใฎๅ˜่ชžใ‚„ๆ–‡ใง้–‹ๅง‹ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + ใพใŸใฏใƒชใƒณใ‚ฏใ—ใฆไธ€่ฒซใ—ใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ [ๅ…ƒใฎๅฎŸ่ฃ…](https://github.com/salesforce/Salesforce/ctrl) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + ่ฉณใ—ใใฏใ€‚ +- CTRL ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ€‚ +- CTRL ใฏๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ (CLM) ใฎ็›ฎ็š„ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๆฌกใฎไบˆๆธฌใซๅผทๅŠ›ใงใ™ใ€‚ + ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎใƒˆใƒผใ‚ฏใƒณใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใ™ใ‚‹ใจใ€CTRL ใฏๆง‹ๆ–‡็š„ใซไธ€่ฒซใ—ใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + *run_generation.py* ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆใง็ขบ่ชใงใใพใ™ใ€‚ +- PyTorch ใƒขใƒ‡ใƒซใฏใ€ไปฅๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸใ‚ญใƒผใจๅ€คใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒšใ‚ขใงใ‚ใ‚‹`past_key_values`ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + TensorFlow ใƒขใƒ‡ใƒซใฏ`past`ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ…ฅใ‚Œใพใ™ใ€‚ `past_key_values`ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใŒๅ†่จˆ็ฎ—ใ•ใ‚Œใชใใชใ‚Šใพใ™ใ€‚ + ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใงไบ‹ๅ‰ใซ่จˆ็ฎ—ใ•ใ‚ŒใŸๅ€คใ€‚ [`forward`](model_doc/ctrl#transformers.CTRLModel.forward) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + ใ“ใฎๅผ•ๆ•ฐใฎไฝฟ็”จๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Resources + +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) + +## CTRLConfig + +[[autodoc]] CTRLConfig + +## CTRLTokenizer + +[[autodoc]] CTRLTokenizer + - save_vocabulary + + + + +## CTRLModel + +[[autodoc]] CTRLModel + - forward + +## CTRLLMHeadModel + +[[autodoc]] CTRLLMHeadModel + - forward + +## CTRLForSequenceClassification + +[[autodoc]] CTRLForSequenceClassification + - forward + + + + +## TFCTRLModel + +[[autodoc]] TFCTRLModel + - call + +## TFCTRLLMHeadModel + +[[autodoc]] TFCTRLLMHeadModel + - call + +## TFCTRLForSequenceClassification + +[[autodoc]] TFCTRLForSequenceClassification + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cvt.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cvt.md new file mode 100644 index 0000000000000000000000000000000000000000..ba092732986de8dfff59cc4e27c26b6b07277f1d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/cvt.md @@ -0,0 +1,88 @@ + + +# Convolutional Vision Transformer (CvT) + +## Overview + +CvT ใƒขใƒ‡ใƒซใฏใ€Haping Wuใ€Bin Xiaoใ€Noel Codellaใ€Mengchen Liuใ€Xiyang Daiใ€Lu Yuanใ€Lei Zhang ใซใ‚ˆใฃใฆ [CvT: Introduction Convolutions to Vision Transformers](https://huggingface.co/papers/2103.15808) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚็•ณใฟ่พผใฟใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (CvT) ใฏใ€ViT ใซ็•ณใฟ่พผใฟใ‚’ๅฐŽๅ…ฅใ—ใฆไธกๆ–นใฎ่จญ่จˆใฎ้•ทๆ‰€ใ‚’ๅผ•ใๅ‡บใ™ใ“ใจใซใ‚ˆใ‚Šใ€[ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (ViT)](vit) ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (ViT) ใ‚’ๆ”นๅ–„ใ™ใ‚‹ใ€็•ณใฟ่พผใฟใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ (CvT) ใจๅ‘ผใฐใ‚Œใ‚‹ๆ–ฐใ—ใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +ViT ใซ็•ณใฟ่พผใฟใ‚’ๅฐŽๅ…ฅใ—ใฆไธกๆ–นใฎ่จญ่จˆใฎ้•ทๆ‰€ใ‚’ๅผ•ใๅ‡บใ™ใ“ใจใงใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ใ“ใ‚Œใฏๆฌกใฎใ‚ˆใ†ใซใ—ใฆๅฎŸ็พใ•ใ‚Œใพใ™ใ€‚ +2 ใคใฎไธป่ฆใชๅค‰ๆ›ด: ๆ–ฐใ—ใ„็•ณใฟ่พผใฟใƒˆใƒผใ‚ฏใƒณใฎๅŸ‹ใ‚่พผใฟใ‚’ๅซใ‚€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎ้šŽๅฑคใจใ€็•ณใฟ่พผใฟใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ +็•ณใฟ่พผใฟๅฐ„ๅฝฑใ‚’ๅˆฉ็”จใ—ใŸใƒ–ใƒญใƒƒใ‚ฏใ€‚ใ“ใ‚Œใ‚‰ใฎๅค‰ๆ›ดใซใ‚ˆใ‚Šใ€็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ (CNN) ใฎๆœ›ใพใ—ใ„็‰นๆ€งใŒๅฐŽๅ…ฅใ•ใ‚Œใพใ™ใ€‚ +ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎๅˆฉ็‚น (ๅ‹•็š„ใชๆณจๆ„ๅŠ›ใ€ +ใ‚ฐใƒญใƒผใƒใƒซใชใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใจใ‚ˆใ‚Š่‰ฏใ„ไธ€่ˆฌๅŒ–)ใ€‚็งใŸใกใฏๅบƒ็ฏ„ใชๅฎŸ้จ“ใ‚’ๅฎŸๆ–ฝใ™ใ‚‹ใ“ใจใง CvT ใ‚’ๆคœ่จผใ—ใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใŒ้”ๆˆใงใใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ +ImageNet-1k ไธŠใฎไป–ใฎใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚„ ResNet ใ‚ˆใ‚Šใ‚‚ใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฐ‘ใชใใ€FLOP ใŒไฝŽใ„ใ€ๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ๅŠ ใˆใฆใ€ +ใ‚ˆใ‚Šๅคงใใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ (ไพ‹: ImageNet-22k) ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ไธ‹ๆตใฎใ‚ฟใ‚นใ‚ฏใซๅˆใ‚ใ›ใฆๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใŒ็ถญๆŒใ•ใ‚Œใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ +ImageNet-22kใ€ๅฝ“็คพใฎ CvT-W24 ใฏใ€ImageNet-1k val set ใง 87.7\% ใจใ„ใ†ใƒˆใƒƒใƒ— 1 ใฎ็ฒพๅบฆใ‚’็ฒๅพ—ใ—ใฆใ„ใพใ™ใ€‚ๆœ€ๅพŒใซใ€็งใŸใกใฎ็ตๆžœใฏใ€ไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใŒใ€ +ๆ—ขๅญ˜ใฎใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎ้‡่ฆใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใงใ‚ใ‚‹ใ“ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏใ€ใƒขใƒ‡ใƒซใงใฏๅฎ‰ๅ…จใซๅ‰Š้™คใงใใ‚‹ใŸใ‚ใ€้ซ˜่งฃๅƒๅบฆใฎใƒ“ใ‚ธใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใฎ่จญ่จˆใŒ็ฐก็ด ๅŒ–ใ•ใ‚Œใพใ™ใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [anugunj](https://huggingface.co/anugunj) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/CvT) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- CvT ใƒขใƒ‡ใƒซใฏ้€šๅธธใฎ Vision Transformer ใงใ™ใŒใ€็•ณใฟ่พผใฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ ImageNet-1K ใŠใ‚ˆใณ CIFAR-100 ใงๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใ€[ใ‚ชใƒชใ‚ธใƒŠใƒซ ใƒขใƒ‡ใƒซ (ViT)](vit) ใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ +- ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใฎๅพฎ่ชฟๆ•ดใ ใ‘ใงใชใๆŽจ่ซ–ใซ้–ขใ™ใ‚‹ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚‚ [ใ“ใ“](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/VisionTransformer) ใง็ขบ่ชใงใใพใ™ ([`ViTFeatureExtractor ใ‚’็ฝฎใๆ›ใˆใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™) `] ใซใ‚ˆใ‚‹ [`AutoImageProcessor`] ใŠใ‚ˆใณ [`ViTForImageClassification`] ใซใ‚ˆใ‚‹ [`CvtForImageClassification`])ใ€‚ +- ๅˆฉ็”จๅฏ่ƒฝใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€(1) [ImageNet-22k](http://www.image-net.org/) (1,400 ไธ‡ใฎ็”ปๅƒใจ 22,000 ใฎใ‚ฏใƒฉใ‚นใฎใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณ) ใงใฎใฟไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใ€(2) ใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ ImageNet-22k ใง่ชฟๆ•ดใ€ใพใŸใฏ (3) [ImageNet-1k](http://www.image-net.org/challenges/LSVRC/2012/) (ILSVRC 2012 ใจใ‚‚ๅ‘ผใฐใ‚Œใ‚‹ใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณ) ใงใ‚‚ๅพฎ่ชฟๆ•ด130ไธ‡ใฎ + ็”ปๅƒใจ 1,000 ใ‚ฏใƒฉใ‚น)ใ€‚ + +## Resources + +CvT ใ‚’ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`CvtForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## CvtConfig + +[[autodoc]] CvtConfig + + + + +## CvtModel + +[[autodoc]] CvtModel + - forward + +## CvtForImageClassification + +[[autodoc]] CvtForImageClassification + - forward + + + + +## TFCvtModel + +[[autodoc]] TFCvtModel + - call + +## TFCvtForImageClassification + +[[autodoc]] TFCvtForImageClassification + - call + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/data2vec.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/data2vec.md new file mode 100644 index 0000000000000000000000000000000000000000..b5267aae35b67f8f3f1dd45761504208ed87e1f5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/data2vec.md @@ -0,0 +1,187 @@ + + +# Data2Vec + +## Overview + +Data2Vec ใƒขใƒ‡ใƒซใฏใ€[data2vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) ใง Alexei Baevskiใ€Wei-Ning Hsuใ€Qiantong Xuใ€ใƒArun Babu, Jiatao Gu and Michael Auli. +Data2Vec ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ€้Ÿณๅฃฐใ€็”ปๅƒใชใฉใฎใ•ใพใ–ใพใชใƒ‡ใƒผใ‚ฟ ใƒขใƒ€ใƒชใƒ†ใ‚ฃใซใ‚ใŸใ‚‹่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใฎใŸใ‚ใฎ็ตฑไธ€ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’ๆๆกˆใ—ใพใ™ใ€‚ +้‡่ฆใชใฎใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎไบˆๆธฌใ‚ฟใƒผใ‚ฒใƒƒใƒˆใฏใ€ใƒขใƒ€ใƒชใƒ†ใ‚ฃๅ›บๆœ‰ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใซไพๅญ˜ใ—ใชใ„ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใงใฏใชใใ€ๅ…ฅๅŠ›ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆๅŒ–ใ•ใ‚ŒใŸๆฝœๅœจ่กจ็พใงใ‚ใ‚‹ใ“ใจใงใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใฎไธ€่ˆฌ็š„ใช่€ƒใˆๆ–นใฏใฉใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใงใ‚‚ๅŒใ˜ใงใ™ใŒใ€ๅฎŸ้š›ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใจ +ๅ˜ไธ€ใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‚’ๅฟต้ ญใซ็ฝฎใ„ใฆ้–‹็™บใ•ใ‚ŒใŸใŸใ‚ใ€็›ฎ็š„ใฏๅคงใใ็•ฐใชใ‚Šใพใ™ใ€‚ไธ€่ˆฌใซ่ฟ‘ใฅใ‘ใ‚‹ใŸใ‚ใซ +่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ใงใฏใ€ใฉใกใ‚‰ใฎ้Ÿณๅฃฐใซๅฏพใ—ใฆใ‚‚ๅŒใ˜ๅญฆ็ฟ’ๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใ‚ใ‚‹ data2vec ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +NLP ใพใŸใฏใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณใ€‚ไธญๅฟƒใจใชใ‚‹ใ‚ขใ‚คใƒ‡ใ‚ขใฏใ€ๅฎŒๅ…จใชๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใฎๆฝœๅœจ็š„ใช่กจ็พใ‚’ใ€ +ๆจ™ๆบ–ใฎ Transformer ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใŸ่‡ชๅทฑ่’ธ็•™ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฎๅ…ฅๅŠ›ใฎใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒ“ใƒฅใƒผใ€‚ +ๅ˜่ชžใ€่ฆ–่ฆš็š„ใƒˆใƒผใ‚ฏใƒณใ€ไบบ้–“ใฎ้Ÿณๅฃฐๅ˜ไฝใชใฉใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃๅ›บๆœ‰ใฎใ‚ฟใƒผใ‚ฒใƒƒใƒˆใ‚’ไบˆๆธฌใ™ใ‚‹ใฎใงใฏใชใใ€ +ๆœฌ่ณช็š„ใซใƒญใƒผใ‚ซใƒซใงใ‚ใ‚‹ใŸใ‚ใ€data2vec ใฏใ€ใ‹ใ‚‰ใฎๆƒ…ๅ ฑใ‚’ๅซใ‚€ๆ–‡่„ˆๅŒ–ใ•ใ‚ŒใŸๆฝœๅœจ่กจ็พใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ +ๅ…ฅๅŠ›ๅ…จไฝ“ใ€‚้Ÿณๅฃฐ่ช่ญ˜ใ€็”ปๅƒๅˆ†้กžใ€ใŠใ‚ˆใณ +่‡ช็„ถ่จ€่ชž็†่งฃใฏใ€ๆ–ฐใ—ใ„ๆœ€ๅ…ˆ็ซฏๆŠ€่ก“ใ‚„ใ€ไธปๆตใฎใ‚ขใƒ—ใƒญใƒผใƒใซๅŒนๆ•ตใ™ใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใจใ‚ณใƒผใƒ‰ใฏใ€www.github.com/pytorch/fairseq/tree/master/examples/data2vec.* ใงๅ…ฅๆ‰‹ใงใใพใ™ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[edugp](https://huggingface.co/edugp) ใŠใ‚ˆใณ [patrickvonplaten](https://huggingface.co/patrickvonplaten) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +[sayakpaul](https://github.com/sayakpaul) ใจ [Rocketknight1](https://github.com/Rocketknight1) ใฏใ€TensorFlow ใฎใƒ“ใ‚ธใƒงใƒณใซ Data2Vec ใ‚’ๆไพ›ใ—ใพใ—ใŸใ€‚ + +ๅ…ƒใฎใ‚ณใƒผใƒ‰ (NLP ใŠใ‚ˆใณ้Ÿณๅฃฐ็”จ) ใฏใ€[ใ“ใกใ‚‰](https://github.com/pytorch/fairseq/tree/main/examples/data2vec) ใซใ‚ใ‚Šใพใ™ใ€‚ +ใƒ“ใ‚ธใƒงใƒณใฎๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/facebookresearch/data2vec_vision/tree/main/beit) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +- Data2VecAudioใ€Data2VecTextใ€ใŠใ‚ˆใณ Data2VecVision ใฏใ™ในใฆใ€ๅŒใ˜่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šๅญฆ็ฟ’ๆ–นๆณ•ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- Data2VecAudio ใฎๅ ดๅˆใ€ๅ‰ๅ‡ฆ็†ใฏ็‰นๅพดๆŠฝๅ‡บใ‚’ๅซใ‚ใฆ [`Wav2Vec2Model`] ใจๅŒใ˜ใงใ™ใ€‚ +- Data2VecText ใฎๅ ดๅˆใ€ๅ‰ๅ‡ฆ็†ใฏใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’ๅซใ‚ใฆ [`RobertaModel`] ใจๅŒใ˜ใงใ™ใ€‚ +- Data2VecVision ใฎๅ ดๅˆใ€ๅ‰ๅ‡ฆ็†ใฏ็‰นๅพดๆŠฝๅ‡บใ‚’ๅซใ‚ใฆ [`BeitModel`] ใจๅŒใ˜ใงใ™ใ€‚ + +## Resources + +Data2Vec ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`Data2VecVisionForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://cola.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง [`TFData2VecVisionForImageClassification`] ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€[ใ“ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/sayakpaul/TF-2.0-Hacks/blob/master/data2vec_vision_image_classification.ipynb) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ๏ผ‰ใ€‚ + +**Data2VecText ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒชใ‚ฝใƒผใ‚น** +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/language_modeling) +- [ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_language_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +**Data2VecAudio ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒชใ‚ฝใƒผใ‚น** +- [้Ÿณๅฃฐๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/audio_classification) +- [่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/asr) + +**Data2VecVision ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒชใ‚ฝใƒผใ‚น** +- [็”ปๅƒๅˆ†้กž](../tasks/image_classification) +- [ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](../tasks/semantic_segmentation) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## Data2VecTextConfig + +[[autodoc]] Data2VecTextConfig + +## Data2VecAudioConfig + +[[autodoc]] Data2VecAudioConfig + +## Data2VecVisionConfig + +[[autodoc]] Data2VecVisionConfig + + + + +## Data2VecAudioModel + +[[autodoc]] Data2VecAudioModel + - forward + +## Data2VecAudioForAudioFrameClassification + +[[autodoc]] Data2VecAudioForAudioFrameClassification + - forward + +## Data2VecAudioForCTC + +[[autodoc]] Data2VecAudioForCTC + - forward + +## Data2VecAudioForSequenceClassification + +[[autodoc]] Data2VecAudioForSequenceClassification + - forward + +## Data2VecAudioForXVector + +[[autodoc]] Data2VecAudioForXVector + - forward + +## Data2VecTextModel + +[[autodoc]] Data2VecTextModel + - forward + +## Data2VecTextForCausalLM + +[[autodoc]] Data2VecTextForCausalLM + - forward + +## Data2VecTextForMaskedLM + +[[autodoc]] Data2VecTextForMaskedLM + - forward + +## Data2VecTextForSequenceClassification + +[[autodoc]] Data2VecTextForSequenceClassification + - forward + +## Data2VecTextForMultipleChoice + +[[autodoc]] Data2VecTextForMultipleChoice + - forward + +## Data2VecTextForTokenClassification + +[[autodoc]] Data2VecTextForTokenClassification + - forward + +## Data2VecTextForQuestionAnswering + +[[autodoc]] Data2VecTextForQuestionAnswering + - forward + +## Data2VecVisionModel + +[[autodoc]] Data2VecVisionModel + - forward + +## Data2VecVisionForImageClassification + +[[autodoc]] Data2VecVisionForImageClassification + - forward + +## Data2VecVisionForSemanticSegmentation + +[[autodoc]] Data2VecVisionForSemanticSegmentation + - forward + + + + +## TFData2VecVisionModel + +[[autodoc]] TFData2VecVisionModel + - call + +## TFData2VecVisionForImageClassification + +[[autodoc]] TFData2VecVisionForImageClassification + - call + +## TFData2VecVisionForSemanticSegmentation + +[[autodoc]] TFData2VecVisionForSemanticSegmentation + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta-v2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta-v2.md new file mode 100644 index 0000000000000000000000000000000000000000..279dae3610abfa2a2403036e8b94e7debe26d5d3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta-v2.md @@ -0,0 +1,167 @@ + + +# DeBERTa-v2 + +## Overview + +DeBERTa ใƒขใƒ‡ใƒซใฏใ€Pengcheng Heใ€Xiaodong Liuใ€Jianfeng Gaoใ€Weizhu Chen ใซใ‚ˆใฃใฆ [DeBERTa: Decoding-enhanced BERT with Disentangled Attendant](https://huggingface.co/papers/2006.03654) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚Google ใฎใƒขใƒ‡ใƒซใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ +2018ๅนดใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸBERTใƒขใƒ‡ใƒซใจ2019ๅนดใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸFacebookใฎRoBERTaใƒขใƒ‡ใƒซใ€‚ + +ใ“ใ‚Œใฏใ€ใ‚‚ใคใ‚ŒใŸๆณจๆ„ใ‚’่งฃใใปใใ—ใ€ไฝฟ็”จใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใฎๅŠๅˆ†ใ‚’ไฝฟ็”จใ—ใฆๅผทๅŒ–ใ•ใ‚ŒใŸใƒžใ‚นใ‚ฏ ใƒ‡ใ‚ณใƒผใƒ€ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ‚™ใˆใŸ RoBERTa ใซๅŸบใฅใ„ใฆๆง‹็ฏ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒญใƒ™ใƒซใ‚ฟใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‹ใƒฅใƒผใƒฉใƒซ่จ€่ชžใƒขใƒ‡ใƒซใฎๆœ€่ฟ‘ใฎ้€ฒๆญฉใซใ‚ˆใ‚Šใ€ๅคšใใฎ่‡ช็„ถ่จ€่ชžใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ—ใŸใ€‚ +่จ€่ชžๅ‡ฆ็† (NLP) ใ‚ฟใ‚นใ‚ฏใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ DeBERTa (Decoding-enhanced BERT with +ใ“ใ‚Œใฏใ€2 ใคใฎๆ–ฐใ—ใ„ๆŠ€่ก“ใ‚’ไฝฟ็”จใ—ใฆ BERT ใƒขใƒ‡ใƒซใจ RoBERTa ใƒขใƒ‡ใƒซใ‚’ๆ”นๅ–„ใ—ใพใ™ใ€‚ 1ใค็›ฎใฏใ€ +ใ‚‚ใคใ‚Œใ‚’่งฃใๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ€‚ๅ„ๅ˜่ชžใฏใ€ใใฎๅ†…ๅฎนใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ 2 ใคใฎใƒ™ใ‚ฏใƒˆใƒซใ‚’ไฝฟ็”จใ—ใฆ่กจ็พใ•ใ‚Œใ€ +ๅ˜่ชž้–“ใฎๆณจๆ„ใฎ้‡ใฟใฏใ€ใใ‚Œใ‚‰ใฎๅ˜่ชžใฎใ‚‚ใคใ‚Œ่งฃ้™ค่กŒๅˆ—ใ‚’ไฝฟ็”จใ—ใฆ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ +ๅ†…ๅฎนใจ็›ธๅฏพ็š„ใชไฝ็ฝฎใ€‚ 2 ็•ช็›ฎใซใ€ๅผทๅŒ–ใ•ใ‚ŒใŸใƒžใ‚นใ‚ฏ ใƒ‡ใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ‡บๅŠ›ใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚น ใƒฌใ‚คใƒคใ‚’ๆฌกใฎใ‚ˆใ†ใซ็ฝฎใๆ›ใˆใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ 2 ใคใฎๆ‰‹ๆณ•ใซใ‚ˆใ‚ŠๅŠน็އใŒๅคงๅน…ใซๅ‘ไธŠใ™ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจไธ‹ๆตใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใ€‚ RoBERTa-Large ใจๆฏ”่ผƒใ™ใ‚‹ใจใ€DeBERTa ใƒขใƒ‡ใƒซใฏๅŠๅˆ†ใฎใƒฌใƒ™ใƒซใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใฏๅน…ๅบƒใ„ NLP ใ‚ฟใ‚นใ‚ฏใงไธ€่ฒซใ—ใฆๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ—ใ€MNLI ใง +0.9% ใฎๆ”นๅ–„ใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚ +(90.2% ๅฏพ 91.1%)ใ€SQuAD v2.0 ใงใฏ +2.3% (88.4% ๅฏพ 90.7%)ใ€RACE ใงใฏ +3.6% (83.2% ๅฏพ 86.8%) ใงใ—ใŸใ€‚ DeBERTa ใ‚ณใƒผใƒ‰ใจ +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏ https://github.com/microsoft/DeBERTa ใงๅ…ฌ้–‹ใ•ใ‚Œใพใ™ใ€‚* + +ๆฌกใฎๆƒ…ๅ ฑใฏใ€[ๅ…ƒใฎๅฎŸ่ฃ…ใง็›ดๆŽฅ่กจ็คบใ•ใ‚Œใพใ™ ใƒชใƒใ‚ธใƒˆใƒช](https://github.com/microsoft/DeBERTa)ใ€‚ DeBERTa v2 ใฏใ€DeBERTa ใƒขใƒ‡ใƒซใฎ 2 ็•ช็›ฎใฎใƒใƒผใ‚ธใƒงใƒณใงใ™ใ€‚ใใ‚Œใซใฏไปฅไธ‹ใŒๅซใพใ‚Œใพใ™ +SuperGLUE ๅ˜ไธ€ใƒขใƒ‡ใƒซใฎๆๅ‡บใซไฝฟ็”จใ•ใ‚ŒใŸ 1.5B ใƒขใƒ‡ใƒซใฏใ€ไบบ้–“ใฎใƒ™ใƒผใ‚นใƒฉใ‚คใƒณ 89.8 ใซๅฏพใ—ใฆ 89.9 ใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚ใ‚ใชใŸใฏใงใใ‚‹ +ใ“ใฎๆŠ•็จฟใซ้–ขใ™ใ‚‹่ฉณ็ดฐใซใคใ„ใฆใฏใ€่‘—่€…ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[ใƒ–ใƒญใ‚ฐ](https://www.microsoft.com/en-us/research/blog/microsoft-deberta-surpasses-human-performance-on-the-superglue-benchmark/) + +v2 ใฎๆ–ฐๆฉŸ่ƒฝ: + +- **่ชžๅฝ™** v2 ใงใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ๆง‹็ฏ‰ใ•ใ‚ŒใŸใ‚ตใ‚คใ‚บ 128K ใฎๆ–ฐใ—ใ„่ชžๅฝ™ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒๅค‰ๆ›ดใ•ใ‚Œใพใ—ใŸใ€‚ + GPT2 ใƒ™ใƒผใ‚นใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎไปฃใ‚ใ‚Šใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏ + [sentencepiece ใƒ™ใƒผใ‚น](https://github.com/google/sentencepiece) ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€‚ +- **nGiE(nGram Induced Input Encoding)** DeBERTa-v2 ใƒขใƒ‡ใƒซใฏใ€ๆœ€ๅˆใฎ็•ณใฟ่พผใฟๅฑคใจใฏๅˆฅใซ่ฟฝๅŠ ใฎ็•ณใฟ่พผใฟๅฑคใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผๅฑคใ‚’ไฝฟ็”จใ—ใฆใ€ๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณใฎใƒญใƒผใ‚ซใƒซไพๅญ˜้–ขไฟ‚ใ‚’ใ‚ˆใ‚Šใ‚ˆใๅญฆ็ฟ’ใ—ใพใ™ใ€‚ +- **ไฝ็ฝฎๅฐ„ๅฝฑ่กŒๅˆ—ใ‚’ๆณจ็›ฎใƒฌใ‚คใƒคใƒผใฎใ‚ณใƒณใƒ†ใƒณใƒ„ๅฐ„ๅฝฑ่กŒๅˆ—ใจๅ…ฑๆœ‰** ไปฅๅ‰ใซๅŸบใฅใ + ๅฎŸ้จ“ใงใฏใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ“ใจใชใใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฟๅญ˜ใงใใพใ™ใ€‚ +- **ใƒใ‚ฑใƒƒใƒˆใ‚’้ฉ็”จใ—ใฆ็›ธๅฏพไฝ็ฝฎใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใพใ™** DeBERTa-v2 ใƒขใƒ‡ใƒซใฏใƒญใ‚ฐ ใƒใ‚ฑใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆ็›ธๅฏพไฝ็ฝฎใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใพใ™ + T5ใซไผผใฆใ„ใพใ™ใ€‚ +- **900M ใƒขใƒ‡ใƒซ & 1.5B ใƒขใƒ‡ใƒซ** 2 ใคใฎ่ฟฝๅŠ ใƒขใƒ‡ใƒซ ใ‚ตใ‚คใ‚บ: 900M ใจ 1.5B ใŒๅˆฉ็”จๅฏ่ƒฝใงใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ™ใ€‚ + ไธ‹ๆตใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [DeBERTa](https://huggingface.co/DeBERTa) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎ TF 2.0 ๅฎŸ่ฃ…ใฏใ€ +[kamalkraj](https://huggingface.co/kamalkraj) ใซใ‚ˆใ‚‹ๆŠ•็จฟใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/microsoft/DeBERTa) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) +- [ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_language_modeling) +- [ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/multiple_choice) + +## DebertaV2Config + +[[autodoc]] DebertaV2Config + +## DebertaV2Tokenizer + +[[autodoc]] DebertaV2Tokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## DebertaV2TokenizerFast + +[[autodoc]] DebertaV2TokenizerFast + - build_inputs_with_special_tokens + - create_token_type_ids_from_sequences + + + + +## DebertaV2Model + +[[autodoc]] DebertaV2Model + - forward + +## DebertaV2PreTrainedModel + +[[autodoc]] DebertaV2PreTrainedModel + - forward + +## DebertaV2ForMaskedLM + +[[autodoc]] DebertaV2ForMaskedLM + - forward + +## DebertaV2ForSequenceClassification + +[[autodoc]] DebertaV2ForSequenceClassification + - forward + +## DebertaV2ForTokenClassification + +[[autodoc]] DebertaV2ForTokenClassification + - forward + +## DebertaV2ForQuestionAnswering + +[[autodoc]] DebertaV2ForQuestionAnswering + - forward + +## DebertaV2ForMultipleChoice + +[[autodoc]] DebertaV2ForMultipleChoice + - forward + + + + +## TFDebertaV2Model + +[[autodoc]] TFDebertaV2Model + - call + +## TFDebertaV2PreTrainedModel + +[[autodoc]] TFDebertaV2PreTrainedModel + - call + +## TFDebertaV2ForMaskedLM + +[[autodoc]] TFDebertaV2ForMaskedLM + - call + +## TFDebertaV2ForSequenceClassification + +[[autodoc]] TFDebertaV2ForSequenceClassification + - call + +## TFDebertaV2ForTokenClassification + +[[autodoc]] TFDebertaV2ForTokenClassification + - call + +## TFDebertaV2ForQuestionAnswering + +[[autodoc]] TFDebertaV2ForQuestionAnswering + - call + +## TFDebertaV2ForMultipleChoice + +[[autodoc]] TFDebertaV2ForMultipleChoice + - call + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta.md new file mode 100644 index 0000000000000000000000000000000000000000..8a9440a91f0099f477911a24aba15f620f46c6f0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deberta.md @@ -0,0 +1,164 @@ + + +# DeBERTa + +## Overview + +DeBERTa ใƒขใƒ‡ใƒซใฏใ€Pengcheng Heใ€Xiaodong Liuใ€Jianfeng Gaoใ€Weizhu Chen ใซใ‚ˆใฃใฆ [DeBERTa: Decoding-enhanced BERT with Disentangled Attendant](https://huggingface.co/papers/2006.03654) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚Google ใฎใƒขใƒ‡ใƒซใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ +2018ๅนดใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸBERTใƒขใƒ‡ใƒซใจ2019ๅนดใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸFacebookใฎRoBERTaใƒขใƒ‡ใƒซใ€‚ + +ใ“ใ‚Œใฏใ€ใ‚‚ใคใ‚ŒใŸๆณจๆ„ใ‚’่งฃใใปใใ—ใ€ไฝฟ็”จใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใฎๅŠๅˆ†ใ‚’ไฝฟ็”จใ—ใฆๅผทๅŒ–ใ•ใ‚ŒใŸใƒžใ‚นใ‚ฏ ใƒ‡ใ‚ณใƒผใƒ€ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ‚™ใˆใŸ RoBERTa ใซๅŸบใฅใ„ใฆๆง‹็ฏ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒญใƒ™ใƒซใ‚ฟใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‹ใƒฅใƒผใƒฉใƒซ่จ€่ชžใƒขใƒ‡ใƒซใฎๆœ€่ฟ‘ใฎ้€ฒๆญฉใซใ‚ˆใ‚Šใ€ๅคšใใฎ่‡ช็„ถ่จ€่ชžใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ—ใŸใ€‚ +่จ€่ชžๅ‡ฆ็† (NLP) ใ‚ฟใ‚นใ‚ฏใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ DeBERTa (Decoding-enhanced BERT with +ใ“ใ‚Œใฏใ€2 ใคใฎๆ–ฐใ—ใ„ๆŠ€่ก“ใ‚’ไฝฟ็”จใ—ใฆ BERT ใƒขใƒ‡ใƒซใจ RoBERTa ใƒขใƒ‡ใƒซใ‚’ๆ”นๅ–„ใ—ใพใ™ใ€‚ 1ใค็›ฎใฏใ€ +ใ‚‚ใคใ‚Œใ‚’่งฃใๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ€‚ๅ„ๅ˜่ชžใฏใ€ใใฎๅ†…ๅฎนใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ 2 ใคใฎใƒ™ใ‚ฏใƒˆใƒซใ‚’ไฝฟ็”จใ—ใฆ่กจ็พใ•ใ‚Œใ€ +ๅ˜่ชž้–“ใฎๆณจๆ„ใฎ้‡ใฟใฏใ€ใใ‚Œใ‚‰ใฎๅ˜่ชžใฎใ‚‚ใคใ‚Œ่งฃ้™ค่กŒๅˆ—ใ‚’ไฝฟ็”จใ—ใฆ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ +ๅ†…ๅฎนใจ็›ธๅฏพ็š„ใชไฝ็ฝฎใ€‚ 2 ็•ช็›ฎใซใ€ๅผทๅŒ–ใ•ใ‚ŒใŸใƒžใ‚นใ‚ฏ ใƒ‡ใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ‡บๅŠ›ใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚น ใƒฌใ‚คใƒคใ‚’ๆฌกใฎใ‚ˆใ†ใซ็ฝฎใๆ›ใˆใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ 2 ใคใฎๆ‰‹ๆณ•ใซใ‚ˆใ‚ŠๅŠน็އใŒๅคงๅน…ใซๅ‘ไธŠใ™ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจไธ‹ๆตใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใ€‚ RoBERTa-Large ใจๆฏ”่ผƒใ™ใ‚‹ใจใ€DeBERTa ใƒขใƒ‡ใƒซใฏๅŠๅˆ†ใฎใƒฌใƒ™ใƒซใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใฏๅน…ๅบƒใ„ NLP ใ‚ฟใ‚นใ‚ฏใงไธ€่ฒซใ—ใฆๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ—ใ€MNLI ใง +0.9% ใฎๆ”นๅ–„ใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚ +(90.2% ๅฏพ 91.1%)ใ€SQuAD v2.0 ใงใฏ +2.3% (88.4% ๅฏพ 90.7%)ใ€RACE ใงใฏ +3.6% (83.2% ๅฏพ 86.8%) ใงใ—ใŸใ€‚ DeBERTa ใ‚ณใƒผใƒ‰ใจ +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏ https://github.com/microsoft/DeBERTa ใงๅ…ฌ้–‹ใ•ใ‚Œใพใ™ใ€‚* + + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [DeBERTa](https://huggingface.co/DeBERTa) ใซใ‚ˆใฃใฆๅฏ„็จฟใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎ TF 2.0 ๅฎŸ่ฃ…ใฏใ€ +[kamalkraj](https://huggingface.co/kamalkraj) ใซใ‚ˆใ‚‹ๅฏ„็จฟใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/microsoft/DeBERTa) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +DeBERTa ใ‚’ไฝฟใ„ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + + + +- DeBERTa ใ‚’ไฝฟ็”จใ—ใฆ [DeepSpeed ใ‚’ไฝฟ็”จใ—ใฆๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅŠ ้€Ÿใ™ใ‚‹](https://huggingface.co/blog/accelerate-deepspeed) ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ€‚ +- DeBERTa ใซใ‚ˆใ‚‹ [ๆฉŸๆขฐๅญฆ็ฟ’ใซใ‚ˆใ‚‹ใ‚นใƒผใƒ‘ใƒผใƒใƒฃใƒผใ‚ธใ•ใ‚ŒใŸ้กงๅฎขใ‚ตใƒผใƒ“ใ‚น](https://huggingface.co/blog/supercharge-customer-service-with-machine-learning) ใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ€‚ +- [`DebertaForSequenceClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)ใ€‚ +- [`TFDebertaForSequenceClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)ใ€‚ +- [ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../../en/tasks/sequence_classification) + + + +- [`DebertaForTokenClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb)ใ€‚ +- [`TFDebertaForTokenClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)ใ€‚ +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กž](https://huggingface.co/course/chapter7/2?fw=pt) ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ [ใƒใ‚คใƒˆใƒšใ‚ขใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฎใƒˆใƒผใ‚ฏใƒณๅŒ–](https://huggingface.co/course/chapter6/5?fw=pt) ใฎ็ซ ใ€‚ +- [ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/token_classification) + + + +- [`DebertaForMaskedLM`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)ใ€‚ +- [`TFDebertaForMaskedLM`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/lang-modeling#run_mlmpy) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)ใ€‚ +- [ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](https://huggingface.co/course/chapter7/3?fw=pt) ๐Ÿค— ้ก”ใฎใƒใ‚ฐ ใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/masked_language_modeling) + + + +- [`DebertaForQuestionAnswering`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)ใ€‚ +- [`TFDebertaForQuestionAnswering`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)ใ€‚ +- [่ณชๅ•ๅ›ž็ญ”](https://huggingface.co/course/chapter7/7?fw=pt) ๐Ÿค— ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎ็ซ ใ€‚ +- [่ณชๅ•ๅ›ž็ญ”ใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/question_answering) + +## DebertaConfig + +[[autodoc]] DebertaConfig + +## DebertaTokenizer + +[[autodoc]] DebertaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## DebertaTokenizerFast + +[[autodoc]] DebertaTokenizerFast + - build_inputs_with_special_tokens + - create_token_type_ids_from_sequences + + + + +## DebertaModel + +[[autodoc]] DebertaModel + - forward + +## DebertaPreTrainedModel + +[[autodoc]] DebertaPreTrainedModel + +## DebertaForMaskedLM + +[[autodoc]] DebertaForMaskedLM + - forward + +## DebertaForSequenceClassification + +[[autodoc]] DebertaForSequenceClassification + - forward + +## DebertaForTokenClassification + +[[autodoc]] DebertaForTokenClassification + - forward + +## DebertaForQuestionAnswering + +[[autodoc]] DebertaForQuestionAnswering + - forward + + + + +## TFDebertaModel + +[[autodoc]] TFDebertaModel + - call + +## TFDebertaPreTrainedModel + +[[autodoc]] TFDebertaPreTrainedModel + - call + +## TFDebertaForMaskedLM + +[[autodoc]] TFDebertaForMaskedLM + - call + +## TFDebertaForSequenceClassification + +[[autodoc]] TFDebertaForSequenceClassification + - call + +## TFDebertaForTokenClassification + +[[autodoc]] TFDebertaForTokenClassification + - call + +## TFDebertaForQuestionAnswering + +[[autodoc]] TFDebertaForQuestionAnswering + - call + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/decision_transformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/decision_transformer.md new file mode 100644 index 0000000000000000000000000000000000000000..e2dee43cd03df249cce60862f610102a6047c8d7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/decision_transformer.md @@ -0,0 +1,50 @@ + + +# Decision Transformer + +## Overview + +Decision Transformer ใƒขใƒ‡ใƒซใฏใ€[Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +_ๅผทๅŒ–ๅญฆ็ฟ’๏ผˆRL๏ผ‰ใ‚’ใ‚ทใƒผใ‚ฑใƒณใ‚นใƒขใƒ‡ใƒชใƒณใ‚ฐๅ•้กŒใจใ—ใฆๆŠฝ่ฑกๅŒ–ใ™ใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€Transformer ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใ‚ทใƒณใƒ—ใƒซใ•ใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใ€ใŠใ‚ˆใณ้–ข้€ฃใ™ใ‚‹้€ฒๆญฉใ‚’ๆดป็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +GPT-x ใ‚„ BERT ใชใฉใฎ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใงใ€‚็‰นใซใ€Decision Transformer ใจใ„ใ†ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +RL ใฎๅ•้กŒใ‚’ๆกไปถไป˜ใใ‚ทใƒผใ‚ฑใƒณใ‚น ใƒขใƒ‡ใƒชใƒณใ‚ฐใจใ—ใฆๆŠ•ใ’ใ‹ใ‘ใพใ™ใ€‚ๅ€ค้–ขๆ•ฐใซ้ฉๅˆใ™ใ‚‹ไปฅๅ‰ใฎ RL ใ‚ขใƒ—ใƒญใƒผใƒใจใฏ็•ฐใชใ‚Šใ€ +ใƒใƒชใ‚ทใƒผๅ‹พ้…ใ‚’่จˆ็ฎ—ใ™ใ‚‹ใจใ€Decision Transformer ใฏๅ› ๆžœ็š„ใซใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ๅˆฉ็”จใ—ใฆๆœ€้ฉใชใ‚ขใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ +ๅค‰ๆˆๅ™จใ€‚ๆœ›ใพใ—ใ„ใƒชใ‚ฟใƒผใƒณ (ๅ ฑ้…ฌ)ใ€้ŽๅŽปใฎ็Šถๆ…‹ใ€ใ‚ขใ‚ฏใ‚ทใƒงใƒณใซๅŸบใฅใ„ใฆ่‡ชๅทฑๅ›žๅธฐใƒขใƒ‡ใƒซใ‚’ๆกไปถไป˜ใ‘ใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ +Decision Transformer ใƒขใƒ‡ใƒซใฏใ€ๆœ›ใพใ—ใ„ใƒชใ‚ฟใƒผใƒณใ‚’้”ๆˆใ™ใ‚‹ๅฐ†ๆฅใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ใใฎใ‚ทใƒณใƒ—ใƒซใ•ใซใ‚‚้–ขใ‚ใ‚‰ใšใ€ +Decision Transformer ใฏใ€ๆœ€ๅ…ˆ็ซฏใฎใƒขใƒ‡ใƒซใƒ•ใƒชใƒผใฎใ‚ชใƒ•ใƒฉใ‚คใƒณ RL ใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจๅŒ็ญ‰ใ€ใพใŸใฏใใ‚Œใ‚’่ถ…ใˆใฆใ„ใพใ™ใ€‚ +Atariใ€OpenAI Gymใ€Key-to-Door ใ‚ฟใ‚นใ‚ฏ_ + +ใ“ใฎใƒใƒผใ‚ธใƒงใƒณใฎใƒขใƒ‡ใƒซใฏใ€็Šถๆ…‹ใŒใƒ™ใ‚ฏใƒˆใƒซใงใ‚ใ‚‹ใ‚ฟใ‚นใ‚ฏ็”จใงใ™ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[edbeeching](https://huggingface.co/edbeeching) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/kzl/decision-transformer) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## DecisionTransformerConfig + +[[autodoc]] DecisionTransformerConfig + +## DecisionTransformerGPT2Model + +[[autodoc]] DecisionTransformerGPT2Model - forward + +## DecisionTransformerModel + +[[autodoc]] DecisionTransformerModel - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deformable_detr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deformable_detr.md new file mode 100644 index 0000000000000000000000000000000000000000..3264f3d960be6dcf2de7637da884e05cbc8b88a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deformable_detr.md @@ -0,0 +1,75 @@ + + +# Deformable DETR + +## Overview + +ๅค‰ๅฝขๅฏ่ƒฝ DETR ใƒขใƒ‡ใƒซใฏใ€Xizhou Zhuใ€Weijie Suใ€Lewei Luใ€Bin Liใ€Xiaogang Wang, Jifeng Dai ใซใ‚ˆใฃใฆ [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://huggingface.co/papers/2010.04159) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸ +ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใฏใ€ๅ‚็…งๅ‘จๅ›ฒใฎๅฐ‘ๆ•ฐใฎไธป่ฆใชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒใ‚คใƒณใƒˆใฎใฟใซๆณจ็›ฎใ™ใ‚‹ๆ–ฐใ—ใ„ๅค‰ๅฝขๅฏ่ƒฝใชใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅˆฉ็”จใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ๅŽๆŸใฎ้…ใ•ใฎๅ•้กŒใจๅ…ƒใฎ [DETR](detr) ใฎๅˆถ้™ใ•ใ‚ŒใŸ็‰นๅพดใฎ็ฉบ้–“่งฃๅƒๅบฆใ‚’่ปฝๆธ›ใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*DETR ใฏใ€ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใชใŒใ‚‰ใ€็‰ฉไฝ“ๆคœๅ‡บใซใŠใ‘ใ‚‹ๅคšใใฎๆ‰‹ไฝœๆฅญใง่จญ่จˆใ•ใ‚ŒใŸใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎๅฟ…่ฆๆ€งใ‚’ๆŽ’้™คใ™ใ‚‹ใŸใ‚ใซๆœ€่ฟ‘ๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใŸใ ใ—ใ€็”ปๅƒ็‰นๅพดใƒžใƒƒใƒ—ใฎๅ‡ฆ็†ใซใŠใ‘ใ‚‹ Transformer ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใฎๅˆถ้™ใซใ‚ˆใ‚Šใ€ๅŽๆŸใŒ้…ใใ€็‰นๅพดใฎ็ฉบ้–“่งฃๅƒๅบฆใŒๅˆถ้™ใ•ใ‚Œใ‚‹ใจใ„ใ†ๅ•้กŒใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅ•้กŒใ‚’่ปฝๆธ›ใ™ใ‚‹ใŸใ‚ใซใ€็งใŸใกใฏ Deformable DETR ใ‚’ๆๆกˆใ—ใพใ—ใŸใ€‚ใ“ใฎ DETR ใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒขใ‚ธใƒฅใƒผใƒซใฏใ€ๅ‚็…งๅ‘จๅ›ฒใฎๅฐ‘ๆ•ฐใฎไธป่ฆใชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒใ‚คใƒณใƒˆใฎใฟใซๆณจ็›ฎใ—ใพใ™ใ€‚ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใฏใ€10 ๅˆ†ใฎ 1 ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚จใƒใƒƒใ‚ฏใงใ€DETR ใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น (็‰นใซๅฐใ•ใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅ ดๅˆ) ใ‚’้”ๆˆใงใใพใ™ใ€‚ COCO ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซ้–ขใ™ใ‚‹ๅบƒ็ฏ„ใชๅฎŸ้จ“ใซใ‚ˆใ‚Šใ€็งใŸใกใฎใ‚ขใƒ—ใƒญใƒผใƒใฎๆœ‰ๅŠนๆ€งใŒๅฎŸ่จผใ•ใ‚Œใพใ—ใŸใ€‚* + + + + ๅค‰ๅฝขๅฏ่ƒฝใช DETR ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/fundamentalvision/Deformable-DETR) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + + + - ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ Deformable DETR ใฏใ€ๅ…ƒใฎ [DETR](detr) ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใจๅŒ็ญ‰ใงใ™ใ€‚ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎ [resources](#resources) ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Resources + +Deformable DETR ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`DeformableDetrForObjectDetection`] ใฎใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฎๆŽจ่ซ–ใจๅพฎ่ชฟๆ•ดใซ้–ขใ™ใ‚‹ใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏใ€[ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Deformable-DETR) ใซใ‚ใ‚Šใพใ™ใ€‚ +- [็‰ฉไฝ“ๆคœๅ‡บใ‚ฟใ‚นใ‚ฏใ‚ฌใ‚คใƒ‰](../tasks/object_detection) ใ‚‚ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## DeformableDetrImageProcessor + +[[autodoc]] DeformableDetrImageProcessor + - preprocess + - post_process_object_detection + +## DeformableDetrFeatureExtractor + +[[autodoc]] DeformableDetrFeatureExtractor + - __call__ + - post_process_object_detection + +## DeformableDetrConfig + +[[autodoc]] DeformableDetrConfig + +## DeformableDetrModel + +[[autodoc]] DeformableDetrModel + - forward + +## DeformableDetrForObjectDetection + +[[autodoc]] DeformableDetrForObjectDetection + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deit.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deit.md new file mode 100644 index 0000000000000000000000000000000000000000..ba769dcf0dbf045a57cec2b8deb778c8c60d6631 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deit.md @@ -0,0 +1,153 @@ + + +# DeiT + +## Overview + +DeiT ใƒขใƒ‡ใƒซใฏใ€Hugo Touvronใ€Matthieu Cordใ€Matthijs Douzeใ€Francisco Massaใ€Alexandre +Sablayrolles, Hervรฉ Jรฉgou.ใซใ‚ˆใฃใฆ [Training data-efficient image Transformers & distillation through attention](https://huggingface.co/papers/2012.12877) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +ใ‚ตใƒ–ใƒฌใ‚คใƒญใƒผใƒซใ€ใ‚จใƒซใƒดใ‚งใƒปใ‚ธใ‚งใ‚ฐใƒผใ€‚ [Dosovitskiy et al., 2020](https://huggingface.co/papers/2010.11929) ใง็ดนไป‹ใ•ใ‚ŒใŸ [Vision Transformer (ViT)](vit) ใฏใ€ๆ—ขๅญ˜ใฎ็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใจๅŒ็ญ‰ใ€ใพใŸใฏใใ‚Œใ‚’ไธŠๅ›žใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใงใใ‚‹ใ“ใจใ‚’็คบใ—ใพใ—ใŸใ€‚ +Transformer ใ‚จใƒณใ‚ณใƒผใƒ€ (BERT ใฎใ‚ˆใ†ใช) ใ‚’ไฝฟ็”จใ—ใŸใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ€‚ใŸใ ใ—ใ€ใใฎ่ซ–ๆ–‡ใง็ดนไป‹ใ•ใ‚ŒใŸ ViT ใƒขใƒ‡ใƒซใซใฏใ€ๆฌกใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฟ…่ฆใงใ—ใŸใ€‚ +ๅค–้ƒจใƒ‡ใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใ€ๆ•ฐ้€ฑ้–“ใซใ‚ใŸใ‚‹้ซ˜ไพกใชใ‚คใƒณใƒ•ใƒฉใ‚นใƒˆใƒฉใ‚ฏใƒใƒฃใ€‚ DeiT (ใƒ‡ใƒผใ‚ฟๅŠน็އใฎ้ซ˜ใ„็”ปๅƒๅค‰ๆ›ๅ™จ) ใฏใ•ใ‚‰ใซๅ„ชใ‚Œใฆใ„ใพใ™ +็”ปๅƒๅˆ†้กž็”จใซๅŠน็އ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใซใ‚ˆใ‚Šใ€ๅฟ…่ฆใชใƒ‡ใƒผใ‚ฟใจใ‚ณใƒณใƒ”ใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐ ใƒชใ‚ฝใƒผใ‚นใŒใฏใ‚‹ใ‹ใซๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ +ใ‚ชใƒชใ‚ธใƒŠใƒซใฎ ViT ใƒขใƒ‡ใƒซใจใฎๆฏ”่ผƒใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ๆœ€่ฟ‘ใ€็ด”็ฒ‹ใซๆณจๆ„ใซๅŸบใฅใใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใŒใ€็”ปๅƒใชใฉใฎ็”ปๅƒ็†่งฃใ‚ฟใ‚นใ‚ฏใซๅฏพๅ‡ฆใงใใ‚‹ใ“ใจใŒ็คบใ•ใ‚Œใพใ—ใŸใ€‚ +ๅˆ†้กžใ€‚ใŸใ ใ—ใ€ใ“ใ‚Œใ‚‰ใฎใƒ“ใ‚ธใƒฅใ‚ขใƒซ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏใ€ +ใ‚คใƒณใƒ•ใƒฉใ‚นใƒˆใƒฉใ‚ฏใƒใƒฃใŒ้ซ˜ไพกใงใ‚ใ‚‹ใŸใ‚ใ€ใใฎๆŽก็”จใŒๅˆถ้™ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎไฝœๆฅญใงใฏใ€ใ‚ณใƒณใƒœใƒชใƒฅใƒผใ‚ทใƒงใƒณใƒ•ใƒชใƒผใฎ็ซถไบ‰ๅŠ›ใฎใ‚ใ‚‹ใ‚ฒใƒผใƒ ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +Imagenet ใฎใฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ 1 ๅฐใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผใง 3 ๆ—ฅไปฅๅ†…ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ„ใพใ™ใ€‚็งใŸใกใฎๅŸบๆบ–ใจใชใ‚‹ใƒ“ใ‚ธใƒงใƒณ +ใƒˆใƒฉใƒณใ‚น (86M ใƒ‘ใƒฉใƒกใƒผใ‚ฟ) ใฏใ€ๅค–้ƒจใชใ—ใง ImageNet ไธŠใง 83.1% (ๅ˜ไธ€ใ‚ฏใƒญใƒƒใƒ—่ฉ•ไพก) ใฎใƒˆใƒƒใƒ— 1 ใฎ็ฒพๅบฆใ‚’้”ๆˆใ—ใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ€‚ใ•ใ‚‰ใซ้‡่ฆใชใฎใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใซ็‰นๆœ‰ใฎๆ•™ๅธซใจ็”Ÿๅพ’ใฎๆˆฆ็•ฅใ‚’ๅฐŽๅ…ฅใ™ใ‚‹ใ“ใจใงใ™ใ€‚่’ธ็•™ใซไพๅญ˜ใ—ใฆใ„ใ‚‹ +ๅญฆ็”ŸใŒๆณจๆ„ใ‚’ๆ‰•ใฃใฆๆ•™ๅธซใ‹ใ‚‰ๅญฆใถใ“ใจใ‚’ไฟ่จผใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใ€‚็งใŸใกใฏใ“ใฎใƒˆใƒผใ‚ฏใƒณใƒ™ใƒผใ‚นใซ่ˆˆๅ‘ณใ‚’็คบใ—ใพใ™ +็‰นใซ convnet ใ‚’ๆ•™ๅธซใจใ—ใฆไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€convnet ใจ็ซถๅˆใ™ใ‚‹็ตๆžœใ‚’ๅ ฑๅ‘Šใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +Imagenet (ๆœ€ๅคง 85.2% ใฎ็ฒพๅบฆใŒๅพ—ใ‚‰ใ‚Œใพใ™) ใจไป–ใฎใ‚ฟใ‚นใ‚ฏใซ่ปข้€ใ™ใ‚‹ใจใใฎไธกๆ–นใงใ€‚็งใŸใกใฏใ‚ณใƒผใƒ‰ใ‚’ๅ…ฑๆœ‰ใ—ใ€ +ใƒขใƒ‡ใƒซใ€‚* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฎ TensorFlow ใƒใƒผใ‚ธใƒงใƒณใฏใ€[amyeroberts](https://huggingface.co/amyeroberts) ใซใ‚ˆใฃใฆ่ฟฝๅŠ ใ•ใ‚Œใพใ—ใŸใ€‚ + +## Usage tips + +- ViT ใจๆฏ”่ผƒใ—ใฆใ€DeiT ใƒขใƒ‡ใƒซใฏใ„ใ‚ใ‚†ใ‚‹่’ธ็•™ใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆๆ•™ๅธซใ‹ใ‚‰ๅŠนๆžœ็š„ใซๅญฆ็ฟ’ใ—ใพใ™ (ใ“ใ‚Œใฏใ€ + DeiT ่ซ–ๆ–‡ใฏใ€ResNet ใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใงใ™)ใ€‚่’ธ็•™ใƒˆใƒผใ‚ฏใƒณใฏใ€ใƒใƒƒใ‚ฏใƒ—ใƒญใƒ‘ใ‚ฒใƒผใ‚ทใƒงใƒณใ‚’้€šใ˜ใฆใ€ใจๅฏพ่ฉฑใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅญฆ็ฟ’ใ•ใ‚Œใพใ™ใ€‚ + ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๅฑคใ‚’ไป‹ใ—ใŸใ‚ฏใƒฉใ‚น ([CLS]) ใจใƒ‘ใƒƒใƒ ใƒˆใƒผใ‚ฏใƒณใ€‚ +- ๆŠฝๅ‡บใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏ 2 ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚(1) ไธŠ้ƒจใซไบˆๆธฌใƒ˜ใƒƒใƒ‰ใ‚’้…็ฝฎใ™ใ‚‹ใ ใ‘ใฎๅคๅ…ธ็š„ใชๆ–นๆณ•ใ€‚ + ใ‚ฏใƒฉใ‚น ใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚็š„ใช้ž่กจ็คบ็Šถๆ…‹ใ‚’ๆŠฝๅ‡บใ—ใ€่’ธ็•™ใ‚ทใ‚ฐใƒŠใƒซใ‚’ไฝฟ็”จใ—ใชใ„ใ€ใพใŸใฏ (2) ไธกๆ–นใฎ + ไบˆๆธฌใƒ˜ใƒƒใƒ‰ใฏใ‚ฏใƒฉใ‚น ใƒˆใƒผใ‚ฏใƒณใฎไธŠใจ่’ธ็•™ใƒˆใƒผใ‚ฏใƒณใฎไธŠใซใ‚ใ‚Šใพใ™ใ€‚ใใฎๅ ดๅˆใ€[CLS] ไบˆๆธฌใฏ + head ใฏใ€head ใฎไบˆๆธฌใจใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ ใƒˆใ‚ฅใƒซใƒผใ‚น ใƒฉใƒ™ใƒซ้–“ใฎ้€šๅธธใฎใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + ่’ธ็•™ไบˆๆธฌใƒ˜ใƒƒใƒ‰ใฏใ€็กฌ่’ธ็•™ (ไบˆๆธฌใจไบˆๆธฌใฎ้–“ใฎใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผ) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ + ่’ธ็•™ใƒ˜ใƒƒใƒ‰ใจๆ•™ๅธซใŒไบˆๆธฌใ—ใŸใƒฉใƒ™ใƒซ๏ผ‰ใ€‚ๆŽจ่ซ–ๆ™‚ใซใ€ๅนณๅ‡ไบˆๆธฌใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + ๆœ€็ต‚็š„ใชไบˆๆธฌใจใ—ใฆไธก้ ญใฎ้–“ใงใ€‚ (2) ใฏใ€Œ่’ธ็•™ใซใ‚ˆใ‚‹ๅพฎ่ชฟๆ•ดใ€ใจใ‚‚ๅ‘ผใฐใ‚Œใพใ™ใ€‚ + ไธ‹ๆตใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ™ใงใซๅพฎ่ชฟๆ•ดใ•ใ‚Œใฆใ„ใ‚‹ๆ•™ๅธซใ€‚ใƒขใƒ‡ใƒซ็š„ใซใฏ (1) ใซ็›ธๅฝ“ใ—ใพใ™ใ€‚ + [`DeiTForImageClassification`] ใจ (2) ใซๅฏพๅฟœใ—ใพใ™ใ€‚ + [`DeiTForImageClassificationWithTeacher`]ใ€‚ +- ่‘—่€…ใ‚‰ใฏ (2) ใซใคใ„ใฆใ‚‚ใ‚ฝใƒ•ใƒˆ่’ธ็•™ใ‚’่ฉฆใฟใŸใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ (ใ“ใฎๅ ดๅˆใ€่’ธ็•™ไบˆๆธฌใƒ˜ใƒƒใƒ‰ใฏ + ๆ•™ๅธซใฎใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚นๅ‡บๅŠ›ใซไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซ KL ใƒ€ใ‚คใƒใƒผใ‚ธใ‚งใƒณใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ—ใŸ๏ผ‰ใŒใ€ใƒใƒผใƒ‰่’ธ็•™ใŒๆœ€่‰ฏใฎ็ตๆžœใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ—ใŸใ€‚ +- ใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใ™ในใฆใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ImageNet-1k ใฎใฟใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๅพฎ่ชฟๆ•ดใ•ใ‚Œใพใ—ใŸใ€‚ๅค–้ƒจใƒ‡ใƒผใ‚ฟใฏไฝฟ็”จใ•ใ‚Œใพใ›ใ‚“ใงใ—ใŸใ€‚ใ“ใ‚Œใฏ + JFT-300M ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ/Imagenet-21k ใชใฉใฎๅค–้ƒจใƒ‡ใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใŸๅ…ƒใฎ ViT ใƒขใƒ‡ใƒซใจใฏๅฏพ็…ง็š„ใงใ™ใ€‚ + ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€‚ +- DeiT ใฎไฝœ่€…ใฏใ€ใ‚ˆใ‚ŠๅŠน็އ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ ViT ใƒขใƒ‡ใƒซใ‚‚ใƒชใƒชใƒผใ‚นใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใฏใ€็›ดๆŽฅใƒ—ใƒฉใ‚ฐใ‚คใƒณใงใใพใ™ใ€‚ + [`ViTModel`] ใพใŸใฏ [`ViTForImageClassification`]ใ€‚ใƒ‡ใƒผใ‚ฟใชใฉใฎใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏ + ใฏใ‚‹ใ‹ใซๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ทใƒŸใƒฅใƒฌใƒผใƒˆใ™ใ‚‹ใŸใ‚ใซใ€ๆ‹กๅผตใ€ๆœ€้ฉๅŒ–ใ€ๆญฃๅ‰‡ๅŒ–ใŒไฝฟ็”จใ•ใ‚Œใพใ—ใŸใ€‚ + (ใŸใ ใ—ใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใฏ ImageNet-1k ใฎใฟใ‚’ไฝฟ็”จใ—ใพใ™)ใ€‚ 4 ใคใฎใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณ (3 ใคใฎ็•ฐใชใ‚‹ใ‚ตใ‚คใ‚บ) ใŒๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + *facebook/deit-tiny-patch16-224*ใ€*facebook/deit-small-patch16-224*ใ€*facebook/deit-base-patch16-224* ใŠใ‚ˆใณ + *facebook/deit-base-patch16-384*ใ€‚ไปฅไธ‹ใ‚’่กŒใ†ใซใฏ [`DeiTImageProcessor`] ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + ใƒขใƒ‡ใƒซ็”จใฎ็”ปๅƒใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ + +## Resources + +DeiT ใ‚’ๅง‹ใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- [`DeiTForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +ใใ‚ŒใซๅŠ ใˆใฆ: + +- [`DeiTForMaskedImageModeling`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining) ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## DeiTConfig + +[[autodoc]] DeiTConfig + +## DeiTFeatureExtractor + +[[autodoc]] DeiTFeatureExtractor + - __call__ + +## DeiTImageProcessor + +[[autodoc]] DeiTImageProcessor + - preprocess + +## DeiTImageProcessorFast + +[[autodoc]] DeiTImageProcessorFast + - preprocess + + + + +## DeiTModel + +[[autodoc]] DeiTModel + - forward + +## DeiTForMaskedImageModeling + +[[autodoc]] DeiTForMaskedImageModeling + - forward + +## DeiTForImageClassification + +[[autodoc]] DeiTForImageClassification + - forward + +## DeiTForImageClassificationWithTeacher + +[[autodoc]] DeiTForImageClassificationWithTeacher + - forward + + + + +## TFDeiTModel + +[[autodoc]] TFDeiTModel + - call + +## TFDeiTForMaskedImageModeling + +[[autodoc]] TFDeiTForMaskedImageModeling + - call + +## TFDeiTForImageClassification + +[[autodoc]] TFDeiTForImageClassification + - call + +## TFDeiTForImageClassificationWithTeacher + +[[autodoc]] TFDeiTForImageClassificationWithTeacher + - call + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deplot.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deplot.md new file mode 100644 index 0000000000000000000000000000000000000000..5e125fa9b52e6a63d9273d2bf0ff1d0c2d603611 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deplot.md @@ -0,0 +1,65 @@ + + +# DePlot + +## Overview + +DePlot ใฏใ€Fangyu Liuใ€Julian Martin Aisenschlosใ€Francesco Piccinnoใ€Syrine Kricheneใ€Chenxi Pang, Kenton Lee, Mandar Joshi, Wenhu Chen, Nigel Collier, Yasemin Altun. ใฎ่ซ–ๆ–‡ [DePlot: One-shot visual language reasoning by plot-to-table translation](https://huggingface.co/papers/2212.10505) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ใƒ‘ใƒณใƒป + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใซใฏๆฌกใฎใ‚ˆใ†ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +*ใƒใƒฃใƒผใƒˆใ‚„ใƒ—ใƒญใƒƒใƒˆใชใฉใฎ่ฆ–่ฆš่จ€่ชžใฏไบบ้–“ใฎไธ–็•Œใซ้ๅœจใ—ใฆใ„ใพใ™ใ€‚ใƒ—ใƒญใƒƒใƒˆใ‚„ใƒใƒฃใƒผใƒˆใ‚’็†่งฃใ™ใ‚‹ใซใฏใ€ๅผทๅŠ›ใชๆŽจ่ซ–ใ‚นใ‚ญใƒซใŒๅฟ…่ฆใงใ™ใ€‚ๅพ“ๆฅใฎๆœ€ๅ…ˆ็ซฏ (SOTA) ใƒขใƒ‡ใƒซใซใฏๅฐ‘ใชใใจใ‚‚ๆ•ฐไธ‡ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ตใƒณใƒ—ใƒซใŒๅฟ…่ฆใงใ‚ใ‚Šใ€ใใฎๆŽจ่ซ–่ƒฝๅŠ›ใฏใ€็‰นใซไบบ้–“ใŒไฝœๆˆใ—ใŸ่ค‡้›‘ใชใ‚ฏใ‚จใƒชใงใฏไพ็„ถใจใ—ใฆๅคงๅน…ใซๅˆถ้™ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎ่ซ–ๆ–‡ใงใฏใ€่ฆ–่ฆš่จ€่ชžๆŽจ่ซ–ใซๅฏพใ™ใ‚‹ๆœ€ๅˆใฎใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ ใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใ‚’็ดนไป‹ใ—ใพใ™ใ€‚็งใŸใกใฏใ€่ฆ–่ฆš่จ€่ชžๆŽจ่ซ–ใฎ่ชฒ้กŒใ‚’ 2 ใคใฎใ‚นใƒ†ใƒƒใƒ—ใซๅˆ†่งฃใ—ใพใ™ใ€‚(1) ใƒ—ใƒญใƒƒใƒˆใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎ็ฟป่จณใจใ€(2) ็ฟป่จณใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใซๅฏพใ™ใ‚‹ๆŽจ่ซ–ใงใ™ใ€‚ใ“ใฎๆ–นๆณ•ใฎ้ตใจใชใ‚‹ใฎใฏใ€ใƒ—ใƒญใƒƒใƒˆใพใŸใฏใƒใƒฃใƒผใƒˆใฎ็”ปๅƒใ‚’็ทšๅฝขๅŒ–ใ•ใ‚ŒใŸใƒ†ใƒผใƒ–ใƒซใซๅค‰ๆ›ใ™ใ‚‹ใ€DePlot ใจใ„ใ†ๅๅ‰ใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃๅค‰ๆ›ใƒขใ‚ธใƒฅใƒผใƒซใงใ™ใ€‚ใใฎๅพŒใ€DePlot ใฎๅ‡บๅŠ›ใ‚’็›ดๆŽฅไฝฟ็”จใ—ใฆใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซ (LLM) ใ‚’ใƒ—ใƒญใƒณใƒ—ใƒˆใ—ใ€LLM ใฎๅฐ‘ๆ•ฐใ‚ทใƒงใƒƒใƒˆๆŽจ่ซ–ๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใงใใพใ™ใ€‚ DePlot ใ‚’ๅ–ๅพ—ใ™ใ‚‹ใซใฏใ€็ตฑไธ€ใ•ใ‚ŒใŸใ‚ฟใ‚นใ‚ฏๅฝขๅผใจใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’็ขบ็ซ‹ใ™ใ‚‹ใ“ใจใงใƒ—ใƒญใƒƒใƒˆใ‹ใ‚‰ใƒ†ใƒผใƒ–ใƒซใธใฎใ‚ฟใ‚นใ‚ฏใ‚’ๆจ™ๆบ–ๅŒ–ใ—ใ€ใ“ใฎใ‚ฟใ‚นใ‚ฏใง DePlot ใ‚’ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ DePlot ใฏใ€ใƒ—ใƒฉใ‚ฐใ‚ขใƒณใƒ‰ใƒ—ใƒฌใ‚คๆ–นๅผใง LLM ใจใจใ‚‚ใซๆ—ข่ฃฝใงไฝฟ็”จใงใใพใ™ใ€‚ 28,000 ใ‚’่ถ…ใˆใ‚‹ใƒ‡ใƒผใ‚ฟ ใƒใ‚คใƒณใƒˆใงๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ SOTA ใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ—ใฆใ€ใƒฏใƒณใ‚ทใƒงใƒƒใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใฎใฟใ‚’ไฝฟ็”จใ™ใ‚‹ DePlot+LLM ใฏใ€ใƒใƒฃใƒผใƒˆ QA ใ‚ฟใ‚นใ‚ฏใ‹ใ‚‰ใฎไบบใŒไฝœๆˆใ—ใŸใ‚ฏใ‚จใƒชใซ้–ขใ—ใฆใ€ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ SOTA ใ‚ˆใ‚Š 24.0% ใฎๆ”นๅ–„ใ‚’้”ๆˆใ—ใพใ—ใŸใ€‚* + +DePlot ใฏใ€`Pix2Struct` ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใงใ™ใ€‚ `Pix2Struct` ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Pix2Struct ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/transformers/main/en/model_doc/pix2struct) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +DePlot ใฏใ€`Pix2Struct` ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎ Visual Question Answering ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใงใ™ใ€‚ๅ…ฅๅŠ›ใ•ใ‚ŒใŸ่ณชๅ•ใ‚’็”ปๅƒไธŠใซใƒฌใƒณใƒ€ใƒชใƒณใ‚ฐใ—ใ€็ญ”ใˆใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ + +## Usage example + +็พๅœจใ€DePlot ใงไฝฟ็”จใงใใ‚‹ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ 1 ใคใงใ™ใ€‚ + +- `google/deplot`: ChartQA ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ DePlot + +```python +from transformers import AutoProcessor, Pix2StructForConditionalGeneration +import requests +from PIL import Image + +model = Pix2StructForConditionalGeneration.from_pretrained("google/deplot") +processor = AutoProcessor.from_pretrained("google/deplot") +url = "https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/5090.png" +image = Image.open(requests.get(url, stream=True).raw) + +inputs = processor(images=image, text="Generate underlying data table of the figure below:", return_tensors="pt") +predictions = model.generate(**inputs, max_new_tokens=512) +print(processor.decode(predictions[0], skip_special_tokens=True)) +``` + +## Fine-tuning + +DePlot ใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€pix2struct [ๅพฎ่ชฟๆ•ดใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_pix2struct.ipynb) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ `Pix2Struct` ใƒขใƒ‡ใƒซใฎๅ ดๅˆใ€Adafactor ใจใ‚ณใ‚ตใ‚คใƒณๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใ€ๅŽๆŸใŒ้ซ˜้€ŸๅŒ–ใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ—ใŸใ€‚ +```python +from transformers.optimization import Adafactor, get_cosine_schedule_with_warmup + +optimizer = Adafactor(self.parameters(), scale_parameter=False, relative_step=False, lr=0.01, weight_decay=1e-05) +scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=1000, num_training_steps=40000) +``` + + + +DePlot ใฏใ€`Pix2Struct`ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใงใ™ใ€‚ API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใซใคใ„ใฆใฏใ€[`Pix2Struct` ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](pix2struct) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deta.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deta.md new file mode 100644 index 0000000000000000000000000000000000000000..7c8a5687ba5344ff028f393b16add08c9c70d9a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/deta.md @@ -0,0 +1,64 @@ + + +# DETA + +## Overview + +DETA ใƒขใƒ‡ใƒซใฏใ€[NMS Strikes Back](https://huggingface.co/papers/2212.06137) ใง Jeffrey Ouyang-Zhangใ€Jang Hyun Choใ€Xingyi Zhouใ€Philipp Krรคhenbรผhl ใซใ‚ˆใฃใฆๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +DETA (Detection Transformers with Assignment ใฎ็•ฅ) ใฏใ€1 ๅฏพ 1 ใฎ 2 ้ƒจใƒใƒณใ‚ฌใƒชใ‚ขใƒณ ใƒžใƒƒใƒใƒณใ‚ฐๆๅคฑใ‚’็ฝฎใๆ›ใˆใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€[Deformable DETR](deformable_detr) ใ‚’ๆ”นๅ–„ใ—ใพใ™ใ€‚ +้žๆœ€ๅคงๆŠ‘ๅˆถ (NMS) ใ‚’ๅ‚™ใˆใŸๅพ“ๆฅใฎๆคœๅ‡บๅ™จใงไฝฟ็”จใ•ใ‚Œใ‚‹ 1 ๅฏพๅคšใฎใƒฉใƒ™ใƒซๅ‰ฒใ‚Šๅฝ“ใฆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆœ€ๅคง 2.5 mAP ใฎๅคงๅน…ใชๅข—ๅŠ ใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*Detection Transformer (DETR) ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ 1 ๅฏพ 1 ใฎ 2 ้ƒจใƒžใƒƒใƒใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใ‚ฏใ‚จใƒชใ‚’ไธ€ๆ„ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ็›ดๆŽฅๅค‰ๆ›ใ—ใ€ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ๆœ€่ฟ‘ใ€ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€็ด›ใ‚Œใ‚‚ใชใ„ๅ„ช้›…ใ•ใง COCO ใฎๅพ“ๆฅใฎๆคœๅ‡บๅ™จใ‚’ไธŠๅ›žใ‚Šใพใ—ใŸใ€‚ใŸใ ใ—ใ€ใƒขใƒ‡ใƒซ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚„ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใชใฉใ€ใ•ใพใ–ใพใช่จญ่จˆใซใŠใ„ใฆๅพ“ๆฅใฎๆคœๅ‡บๅ™จใจใฏ็•ฐใชใ‚‹ใŸใ‚ใ€1 ๅฏพ 1 ใƒžใƒƒใƒใƒณใ‚ฐใฎๆœ‰ๅŠนๆ€งใฏๅฎŒๅ…จใซใฏ็†่งฃใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใ“ใฎ็ ”็ฉถใงใฏใ€DETR ใงใฎ 1 ๅฏพ 1 ใฎใƒใƒณใ‚ฌใƒชใƒผ่ชžใƒžใƒƒใƒใƒณใ‚ฐใจใ€้žๆœ€ๅคง็›ฃ่ฆ– (NMS) ใ‚’ๅ‚™ใˆใŸๅพ“ๆฅใฎๆคœๅ‡บๅ™จใงใฎ 1 ๅฏพๅคšใฎใƒฉใƒ™ใƒซๅ‰ฒใ‚Šๅฝ“ใฆใจใฎ้–“ใฎๅŽณๅฏ†ใชๆฏ”่ผƒใ‚’่กŒใ„ใพใ™ใ€‚้ฉšใในใใ“ใจใซใ€NMS ใ‚’ไฝฟ็”จใ—ใŸ 1 ๅฏพๅคšใฎๅ‰ฒใ‚Šๅฝ“ใฆใฏใ€ๅŒใ˜่จญๅฎšใฎไธ‹ใงๆจ™ๆบ–็š„ใช 1 ๅฏพ 1 ใฎใƒžใƒƒใƒใƒณใ‚ฐใ‚ˆใ‚Šใ‚‚ไธ€่ฒซใ—ใฆๅ„ชใ‚ŒใฆใŠใ‚Šใ€ๆœ€ๅคง 2.5 mAP ใจใ„ใ†ๅคงๅน…ใชๅ‘ไธŠใŒ่ฆ‹ใ‚‰ใ‚Œใพใ™ใ€‚ๅพ“ๆฅใฎ IoU ใƒ™ใƒผใ‚นใฎใƒฉใƒ™ใƒซๅ‰ฒใ‚Šๅฝ“ใฆใ‚’ไฝฟ็”จใ—ใฆ Deformable-DETR ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฝ“็คพใฎๆคœๅ‡บๅ™จใฏใ€ResNet50 ใƒใƒƒใ‚ฏใƒœใƒผใƒณใ‚’ไฝฟ็”จใ—ใฆ 12 ใ‚จใƒใƒƒใ‚ฏ (1x ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซ) ไปฅๅ†…ใซ 50.2 COCO mAP ใ‚’้”ๆˆใ—ใ€ใ“ใฎ่จญๅฎšใงๆ—ขๅญ˜ใฎใ™ในใฆใฎๅพ“ๆฅใฎๆคœๅ‡บๅ™จใพใŸใฏใƒˆใƒฉใƒณใ‚นใƒ™ใƒผใ‚นใฎๆคœๅ‡บๅ™จใ‚’ไธŠๅ›žใ‚Šใพใ—ใŸใ€‚่ค‡ๆ•ฐใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซ้–ขใ—ใฆใ€็งใŸใกใฏไธ€่ฒซใ—ใฆใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎ้ซ˜ใ„ๆคœๅ‡บใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใซใฏไบŒ้ƒจใƒžใƒƒใƒใƒณใ‚ฐใŒไธ่ฆใงใ‚ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซใ€ๆคœๅ‡บใƒˆใƒฉใƒณใ‚นใฎๆˆๅŠŸใฏใ€่กจ็พๅŠ›่ฑŠใ‹ใชใƒˆใƒฉใƒณใ‚น ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซใ‚ˆใ‚‹ใ‚‚ใฎใงใ‚ใ‚‹ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚* + + + + DETA ใฎๆฆ‚่ฆใ€‚ ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/jozhang97/DETA) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Resources + +DETA ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + +- DETA ใฎใƒ‡ใƒข ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏ [ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETA) ใซใ‚ใ‚Šใพใ™ใ€‚ +- ๅ‚็…ง: [ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/object_detection) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## DetaConfig + +[[autodoc]] DetaConfig + +## DetaImageProcessor + +[[autodoc]] DetaImageProcessor + - preprocess + - post_process_object_detection + +## DetaModel + +[[autodoc]] DetaModel + - forward + +## DetaForObjectDetection + +[[autodoc]] DetaForObjectDetection + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/detr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/detr.md new file mode 100644 index 0000000000000000000000000000000000000000..bef1a8fce770f725b3022cc43e37f38b25255349 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/detr.md @@ -0,0 +1,226 @@ + + +# DETR + +## Overview + +DETR ใƒขใƒ‡ใƒซใฏใ€[Transformers ใ‚’ไฝฟ็”จใ—ใŸใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บ](https://huggingface.co/papers/2005.12872) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov and Sergey Zagoruyko ใƒซใ‚คใ‚ณใ€‚ DETR +็•ณใฟ่พผใฟใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ€ใใฎๅพŒใซใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ/ใƒ‡ใ‚ณใƒผใƒ€ใƒผ Transformer ใงๆง‹ๆˆใ•ใ‚Œใพใ™ใ€‚ +็‰ฉไฝ“ใฎๆคœๅ‡บใ€‚ Faster-R-CNN ใ‚„ Mask-R-CNN ใชใฉใฎใƒขใƒ‡ใƒซใฎ่ค‡้›‘ใ•ใฎๅคšใใŒๅคงๅน…ใซ็ฐก็ด ๅŒ–ใ•ใ‚Œใพใ™ใ€‚ +้ ˜ๅŸŸๆๆกˆใ€้žๆœ€ๅคงๆŠ‘ๅˆถๆ‰‹้ †ใ€ใ‚ขใƒณใ‚ซใƒผ็”Ÿๆˆใชใฉใงใ™ใ€‚ใ•ใ‚‰ใซใ€DETR ใฏๆฌกใฎใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใƒ‡ใ‚ณใƒผใƒ€ๅ‡บๅŠ›ใฎไธŠใซใƒžใ‚นใ‚ฏ ใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงใ€ใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซ่‡ช็„ถใซๆ‹กๅผตใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*็‰ฉไฝ“ๆคœๅ‡บใ‚’็›ดๆŽฅ้›†ๅˆไบˆๆธฌๅ•้กŒใจใ—ใฆ่ฆ‹ใ‚‹ๆ–ฐใ—ใ„ๆ–นๆณ•ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚็งใŸใกใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ +ๆคœๅ‡บใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใ‚ˆใ‚Šใ€้žๆœ€ๅคงๆŠ‘ๅˆถใชใฉใฎๅคšใใฎๆ‰‹ไฝœๆฅญใง่จญ่จˆใ•ใ‚ŒใŸใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎๅฟ…่ฆๆ€งใŒๅŠนๆžœ็š„ใซๆŽ’้™คใ•ใ‚Œใพใ™ใ€‚ +ใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹ไบ‹ๅ‰ใฎ็Ÿฅ่ญ˜ใ‚’ๆ˜Ž็คบ็š„ใซใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใƒ—ใƒญใ‚ทใƒผใ‚ธใƒฃใพใŸใฏใ‚ขใƒณใ‚ซใƒผใฎ็”Ÿๆˆใ€‚ใฎไธปใชๆˆๅˆ†ใฏใ€ +DEtection TRansformer ใพใŸใฏ DETR ใจๅ‘ผใฐใ‚Œใ‚‹ๆ–ฐใ—ใ„ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฏใ€ใ‚ปใƒƒใƒˆใƒ™ใƒผใ‚นใฎใ‚ฐใƒญใƒผใƒใƒซๆๅคฑใงใ‚ใ‚Šใ€ +ไบŒ้ƒจใƒžใƒƒใƒใƒณใ‚ฐใ€ใŠใ‚ˆใณใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ/ใƒ‡ใ‚ณใƒผใƒ€ใƒผ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€‚ๅญฆ็ฟ’ใ•ใ‚ŒใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒชใฎๅ›บๅฎšใ•ใ‚ŒใŸๅฐใ•ใชใ‚ปใƒƒใƒˆใŒไธŽใˆใ‚‰ใ‚Œใ‚‹ใจใ€ +DETR ใฏใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใจใ‚ฐใƒญใƒผใƒใƒซ ใ‚คใƒกใƒผใ‚ธ ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใฎ้–ขไฟ‚ใซใคใ„ใฆๆŽจ่ซ–ใ—ใ€ๆœ€็ต‚ใ‚ปใƒƒใƒˆใ‚’็›ดๆŽฅๅ‡บๅŠ›ใ—ใพใ™ใ€‚ +ไธฆ่กŒใ—ใฆไบˆๆƒณใ‚‚ใ€‚ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใฏๆฆ‚ๅฟต็š„ใซใ‚ทใƒณใƒ—ใƒซใงใ‚ใ‚Šใ€ๅคšใใฎใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใ€็‰นๆฎŠใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ๅฟ…่ฆใจใ—ใพใ›ใ‚“ใ€‚ +ไป–ใฎๆœ€ๆ–ฐใฎๆคœๅ‡บๅ™จใ€‚ DETR ใฏใ€็ขบ็ซ‹ใ•ใ‚ŒใŸใ€ใŠใ‚ˆใณๅŒ็ญ‰ใฎ็ฒพๅบฆใจๅฎŸ่กŒๆ™‚ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ่จผใ—ใพใ™ใ€‚ +ๅ›ฐ้›ฃใช COCO ็‰ฉไฝ“ๆคœๅ‡บใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅŸบใฅใใ€้ซ˜ๅบฆใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸ Faster RCNN ใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใ€‚ใ•ใ‚‰ใซใ€DETR ใฏ็ฐกๅ˜ใซๅฎŸ่กŒใงใใพใ™ใ€‚ +็ตฑไธ€ใ•ใ‚ŒใŸๆ–นๆณ•ใงใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซไธ€่ˆฌๅŒ–ใ•ใ‚Œใพใ—ใŸใ€‚็ซถๅˆไป–็คพใ‚’ๅคงๅน…ใซไธŠๅ›žใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็คบใ—ใฆใ„ใพใ™ +ใƒ™ใƒผใ‚นใƒฉใ‚คใƒณ* + +ใ“ใฎใƒขใƒ‡ใƒซใฏใ€[nielsr](https://huggingface.co/nielsr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใกใ‚‰](https://github.com/facebookresearch/detr) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## How DETR works + +[`~transformers.DetrForObjectDetection`] ใŒใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใ‚’่ชฌๆ˜Žใ™ใ‚‹ TLDR ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +ใพใšใ€ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ็•ณใฟ่พผใฟใƒใƒƒใ‚ฏใƒœใƒผใƒณใ‚’้€šใ˜ใฆ็”ปๅƒใŒ้€ไฟกใ•ใ‚Œใพใ™ (่ซ–ๆ–‡ใงใฏใ€่‘—่€…ใ‚‰ใฏๆฌกใฎใ‚ˆใ†ใซไฝฟ็”จใ—ใฆใ„ใพใ™)ใ€‚ +ResNet-50/ResNet-101)ใ€‚ใƒใƒƒใƒ ใƒ‡ใ‚ฃใƒกใƒณใ‚ทใƒงใƒณใ‚‚่ฟฝๅŠ ใ™ใ‚‹ใจไปฎๅฎšใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒใƒƒใ‚ฏใƒœใƒผใƒณใธใฎๅ…ฅๅŠ›ใŒ +็”ปๅƒใซ 3 ใคใฎใ‚ซใƒฉใƒผ ใƒใƒฃใƒใƒซ (RGB) ใŒใ‚ใ‚‹ใจไปฎๅฎšใ—ใŸๅ ดๅˆใฎใ€ๅฝข็Šถ `(batch_size, 3, height, width)` ใฎใƒ†ใƒณใ‚ฝใƒซใ€‚ CNNใฎใƒใƒƒใ‚ฏใƒœใƒผใƒณ +้€šๅธธใฏ `(batch_size, 2048, height/32, width/32)` ใฎๅฝข็Šถใฎใ€ๆ–ฐใ—ใ„ไฝŽ่งฃๅƒๅบฆใฎ็‰นๅพดใƒžใƒƒใƒ—ใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ +ๆฌกใซใ€DETR ใฎ Transformer ใฎ้š ใ‚Œๆฌกๅ…ƒ (ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ `256`) ใซไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซๆŠ•ๅฝฑใ•ใ‚Œใพใ™ใ€‚ +`nn.Conv2D` ใƒฌใ‚คใƒคใƒผใ€‚ใ“ใ‚Œใงใ€ๅฝข็Šถ `(batch_size, 256, height/32, width/32)` ใฎใƒ†ใƒณใ‚ฝใƒซใŒๅฎŒๆˆใ—ใพใ—ใŸใ€‚ +็‰นๅพดใƒžใƒƒใƒ—ใฏๅนณๅฆๅŒ–ใŠใ‚ˆใณ่ปข็ฝฎใ•ใ‚Œใ€ๅฝข็Šถ `(batch_size, seq_len, d_model)` ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๅ–ๅพ—ใ—ใพใ™ = +`(batch_size, width/32*height/32, 256)`ใ€‚ใ—ใŸใŒใฃใฆใ€NLP ใƒขใƒ‡ใƒซใจใฎ้•ใ„ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใŒๅฎŸ้š›ใซใฏ +้€šๅธธใ‚ˆใ‚Šใ‚‚้•ทใใชใ‚Šใพใ™ใŒใ€ใ€Œd_modelใ€ใฏๅฐใ•ใใชใ‚Šใพใ™ (NLP ใงใฏ้€šๅธธ 768 ไปฅไธŠใงใ™)ใ€‚ + +ๆฌกใซใ€ใ“ใ‚ŒใŒใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ไป‹ใ—ใฆ้€ไฟกใ•ใ‚Œใ€ๅŒใ˜ๅฝข็Šถใฎ `encoder_hidden_โ€‹โ€‹states` ใŒๅ‡บๅŠ›ใ•ใ‚Œใพใ™ (ๆฌกใฎใ‚ˆใ†ใซ่€ƒใˆใ‚‹ใ“ใจใŒใงใใพใ™)ใ€‚ +ใ“ใ‚Œใ‚‰ใฏ็”ปๅƒใฎ็‰นๅพดใจใ—ใฆ๏ผ‰ใ€‚ๆฌกใซใ€ใ„ใ‚ใ‚†ใ‚‹ **ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒช**ใŒใƒ‡ใ‚ณใƒผใƒ€ใ‚’้€šใ˜ใฆ้€ไฟกใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏๅฝข็Šถใฎใƒ†ใƒณใ‚ฝใƒซใงใ™ +`(batch_size, num_queries, d_model)`ใ€‚้€šๅธธใ€`num_queries` ใฏ 100 ใซ่จญๅฎšใ•ใ‚Œใ€ใ‚ผใƒญใงๅˆๆœŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎๅ…ฅๅŠ›ๅŸ‹ใ‚่พผใฟใฏๅญฆ็ฟ’ใ•ใ‚ŒใŸไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใงใ‚ใ‚Šใ€ไฝœๆˆ่€…ใฏใ“ใ‚Œใ‚’ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒชใจๅ‘ผใณใ€ๅŒๆง˜ใซ +ใ‚จใƒณใ‚ณใƒผใƒ€ใงใฏใ€ใใ‚Œใ‚‰ใฏๅ„ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๅฑคใฎๅ…ฅๅŠ›ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒชใฏ็‰นๅฎšใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœ็ดขใ—ใพใ™ใ€‚ +็”ปๅƒใงใฏใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใฏใ€่ค‡ๆ•ฐใฎใ‚ปใƒซใƒ• ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒฌใ‚คใƒคใจใ‚จใƒณใ‚ณใƒผใƒ€ ใƒ‡ใ‚ณใƒผใƒ€ ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒฌใ‚คใƒคใ‚’้€šใ˜ใฆใ“ใ‚Œใ‚‰ใฎๅŸ‹ใ‚่พผใฟใ‚’ๆ›ดๆ–ฐใ—ใพใ™ใ€‚ +ๅŒใ˜ๅฝข็Šถใฎ `decoder_hidden_โ€‹โ€‹states` ใ‚’ๅ‡บๅŠ›ใ—ใพใ™: `(batch_size, num_queries, d_model)`ใ€‚ๆฌกใซ้ ญใŒ๏ผ’ใค +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฎใŸใ‚ใซไธŠ้ƒจใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒชใ‚’ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ 1 ใคใซๅˆ†้กžใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขใƒฌใ‚คใƒคใƒผใ€ใพใŸใฏใ€Œใ„ใ„ใˆใ€ +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ€ใ€ใŠใ‚ˆใณๅ„ใ‚ฏใ‚จใƒชใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ไบˆๆธฌใ™ใ‚‹ MLPใ€‚ + +ใƒขใƒ‡ใƒซใฏ **2 ้ƒจใƒžใƒƒใƒใƒณใ‚ฐๆๅคฑ**ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ใคใพใ‚Šใ€ๅฎŸ้š›ใซ่กŒใ†ใ“ใจใฏใ€ไบˆๆธฌใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚นใ‚’ๆฏ”่ผƒใ™ใ‚‹ใ“ใจใงใ™ + +ใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ ใƒˆใ‚ฅใƒซใƒผใ‚น ใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณใซๅฏพใ™ใ‚‹ N = 100 ๅ€‹ใฎๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒชใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚น (ๅŒใ˜้•ทใ• N ใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ) +(ใ—ใŸใŒใฃใฆใ€็”ปๅƒใซใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒ 4 ใคใ—ใ‹ๅซใพใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€96 ๅ€‹ใฎๆณจ้‡ˆใซใฏใ‚ฏใƒฉใ‚นใจใ—ใฆใ€Œใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใชใ—ใ€ใ€ใŠใ‚ˆใณใ‚ฏใƒฉใ‚นใจใ—ใฆใ€Œๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใชใ—ใ€ใŒๅซใพใ‚Œใ‚‹ใ ใ‘ใซใชใ‚Šใพใ™ใ€‚ +ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚น)ใ€‚ [Hungarian matching algorithm](https://en.wikipedia.org/wiki/Hungarian_algorithm) ใฏใ€ๆคœ็ดขใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +N ๅ€‹ใฎใ‚ฏใ‚จใƒชใฎใใ‚Œใžใ‚Œใ‹ใ‚‰ N ๅ€‹ใฎๆณจ้‡ˆใฎใใ‚Œใžใ‚Œใธใฎๆœ€้ฉใช 1 ๅฏพ 1 ใฎใƒžใƒƒใƒ”ใƒณใ‚ฐใ€‚ๆฌกใซใ€ๆจ™ๆบ–ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผ ( +ใ‚ฏใƒฉใ‚น)ใ€ใŠใ‚ˆใณ L1 ใจ [generalized IoU loss](https://giou.stanford.edu/) ใฎ็ทšๅฝข็ตๅˆ ( +ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚น) ใฏใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +DETR ใฏใ€ใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ (ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใจใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’็ตฑๅˆใ™ใ‚‹) ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ‚ˆใ†ใซ่‡ช็„ถใซๆ‹กๅผตใงใใพใ™ใ€‚ +ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ๏ผ‰ใ€‚ [`~transformers.DetrForSegmentation`] ใฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒžใ‚นใ‚ฏ ใƒ˜ใƒƒใƒ‰ใ‚’ไธŠใซ่ฟฝๅŠ ใ—ใพใ™ +[`~transformers.DetrForObjectDetection`]ใ€‚ใƒžใ‚นใ‚ฏ ใƒ˜ใƒƒใƒ‰ใฏใ€ๅ…ฑๅŒใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใ€2 ๆฎต้šŽใฎใƒ—ใƒญใ‚ปใ‚นใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใ“ใ“ใงใ€ๆœ€ๅˆใซ [`~transformers.DetrForObjectDetection`] ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ€ไธกๆ–นใฎๅ‘จๅ›ฒใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ๆคœๅ‡บใ—ใพใ™ใ€‚ +ใ€Œใ‚‚ใฎใ€๏ผˆใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น๏ผ‰ใจใ€Œใ‚‚ใฎใ€๏ผˆๆœจใ€้“่ทฏใ€็ฉบใชใฉใฎ่ƒŒๆ™ฏใฎใ‚‚ใฎ๏ผ‰ใ‚’ใ™ในใฆๅ‡็ตใ—ใ€ใ™ในใฆใฎ้‡ใฟใ‚’ใƒ•ใƒชใƒผใ‚บใ—ใฆใฎใฟใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ +25 ใ‚จใƒใƒƒใ‚ฏใฎใƒžใ‚นใ‚ฏใƒ˜ใƒƒใƒ‰ใ€‚ๅฎŸ้จ“็š„ใซใฏใ€ใ“ใ‚Œใ‚‰ 2 ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใฏๅŒๆง˜ใฎ็ตๆžœใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ™ใ€‚ใƒœใƒƒใ‚ฏใ‚นใฎไบˆๆธฌใฏ +ใƒใƒณใ‚ฌใƒชใƒผ่ชžใฎใƒžใƒƒใƒใƒณใ‚ฐใฏใƒœใƒƒใ‚ฏใ‚น้–“ใฎ่ท้›ขใ‚’ไฝฟ็”จใ—ใฆ่จˆ็ฎ—ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใŸใ‚ใซใฏใ“ใ‚ŒใŒๅฟ…่ฆใงใ™ใ€‚ + +## Usage tips + +- DETR ใฏใ€ใ„ใ‚ใ‚†ใ‚‹ **ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ ใ‚ฏใ‚จใƒช** ใ‚’ไฝฟ็”จใ—ใฆใ€็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใ—ใพใ™ใ€‚ใ‚ฏใ‚จใƒชใฎๆ•ฐใซใ‚ˆใฃใฆๆœ€ๅคงๅ€คใŒๆฑบใพใ‚Šใพใ™ + ๅ˜ไธ€ใฎ็”ปๅƒๅ†…ใงๆคœๅ‡บใงใใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๆ•ฐใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ 100 ใซ่จญๅฎšใ•ใ‚Œใพใ™ (ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๅ‚็…ง) + [`~transformers.DetrConfig`] ใฎ `num_queries`)ใ€‚ใ‚ใ‚‹็จ‹ๅบฆใฎไฝ™่ฃ•ใŒใ‚ใ‚‹ใฎใฏ่‰ฏใ„ใ“ใจใงใ™ (COCO ใงใฏใ€ + ่‘—่€…ใฏ 100 ใ‚’ไฝฟ็”จใ—ใพใ—ใŸใŒใ€COCO ใ‚คใƒกใƒผใ‚ธๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๆœ€ๅคงๆ•ฐใฏ็ด„ 70 ใงใ™)ใ€‚ +- DETR ใฎใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใ€ใ‚ฏใ‚จใƒชใฎๅŸ‹ใ‚่พผใฟใ‚’ไธฆ่กŒใ—ใฆๆ›ดๆ–ฐใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ GPT-2 ใฎใ‚ˆใ†ใช่จ€่ชžใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใพใ™ใ€‚ + ไธฆๅˆ—ใงใฏใชใ่‡ชๅทฑๅ›žๅธฐใƒ‡ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๅ› ๆžœ็š„ๆณจๆ„ใƒžใ‚นใ‚ฏใฏไฝฟ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ +- DETR ใฏใ€ๆŠ•ๅฝฑๅ‰ใซๅ„ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๅฑคใจใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณๅฑคใฎ้š ใ‚Œ็Šถๆ…‹ใซไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + ใ‚ฏใ‚จใƒชใจใ‚ญใƒผใซใ€‚็”ปๅƒใฎไฝ็ฝฎๅŸ‹ใ‚่พผใฟใซใคใ„ใฆใฏใ€ๅ›บๅฎšๆญฃๅผฆๆณขใพใŸใฏๅญฆ็ฟ’ๆธˆใฟใฎใฉใกใ‚‰ใ‹ใ‚’้ธๆŠžใงใใพใ™ใ€‚ + ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใƒ‘ใƒฉใƒกใƒผใ‚ฟ `position_embedding_type` ใฏ + [`~transformers.DetrConfig`] ใฏ `"sine"` ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ +- DETR ใฎไฝœๆˆ่€…ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ€็‰นใซใƒ‡ใ‚ณใƒผใƒ€ใง่ฃœๅŠฉๆๅคฑใ‚’ไฝฟ็”จใ™ใ‚‹ใจๅฝน็ซ‹ใคใ“ใจใซๆฐ—ใฅใใพใ—ใŸใ€‚ + ใƒขใƒ‡ใƒซใฏๅ„ใ‚ฏใƒฉใ‚นใฎๆญฃใ—ใ„ๆ•ฐใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟ `auxiliary_loss` ใ‚’่จญๅฎšใ™ใ‚‹ใจใ€ + [`~transformers.DetrConfig`] ใ‚’`True`ใซ่จญๅฎšใ—ใ€ใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ ใƒ‹ใƒฅใƒผใƒฉใƒซ ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใจใƒใƒณใ‚ฌใƒชใƒผๆๅคฑใ‚’ไบˆๆธฌใ—ใพใ™ + ใฏๅ„ใƒ‡ใ‚ณใƒผใƒ€ๅฑคใฎๅพŒใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ (FFN ใŒใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹)ใ€‚ +- ่ค‡ๆ•ฐใฎใƒŽใƒผใƒ‰ใซใ‚ใŸใ‚‹ๅˆ†ๆ•ฃ็’ฐๅขƒใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใฏใ€ + _modeling_detr.py_ ใฎ _DetrLoss_ ใ‚ฏใƒฉใ‚นใฎ _num_boxes_ ๅค‰ๆ•ฐใ€‚่ค‡ๆ•ฐใฎใƒŽใƒผใƒ‰ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ใ“ใ‚Œใฏๆฌกใฎใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ + ๅ…ƒใฎๅฎŸ่ฃ…ใง่ฆ‹ใ‚‰ใ‚Œใ‚‹ใ‚ˆใ†ใซใ€ใ™ในใฆใฎใƒŽใƒผใƒ‰ใซใ‚ใŸใ‚‹ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใƒœใƒƒใ‚ฏใ‚นใฎๅนณๅ‡ๆ•ฐใซ่จญๅฎšใ•ใ‚Œใพใ™ [ใ“ใกใ‚‰](https://github.com/facebookresearch/detr/blob/a54b77800eb8e64e3ad0d8237789fcbf2f8350c5/models/detr.py#L227-L232) ใ€‚ +- [`~transformers.DetrForObjectDetection`] ใŠใ‚ˆใณ [`~transformers.DetrForSegmentation`] ใฏๆฌกใฎใ‚ˆใ†ใซๅˆๆœŸๅŒ–ใงใใพใ™ใ€‚ + [timm ใƒฉใ‚คใƒ–ใƒฉใƒช](https://github.com/rwightman/pytorch-image-models) ใงๅˆฉ็”จๅฏ่ƒฝใช็•ณใฟ่พผใฟใƒใƒƒใ‚ฏใƒœใƒผใƒณใ€‚ + ใŸใจใˆใฐใ€MobileNet ใƒใƒƒใ‚ฏใƒœใƒผใƒณใ‚’ไฝฟ็”จใ—ใŸๅˆๆœŸๅŒ–ใฏใ€ๆฌกใฎ `backbone` ๅฑžๆ€งใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใงๅฎŸ่กŒใงใใพใ™ใ€‚ + [`~transformers.DetrConfig`] ใ‚’ `"tf_mobilenetv3_small_075"` ใซ่จญๅฎšใ—ใ€ใใ‚Œใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ + ๆง‹ๆˆใ€‚ +- DETR ใฏใ€ๆœ€็Ÿญ่พบใŒไธ€ๅฎšใฎใƒ”ใ‚ฏใ‚ปใƒซๆ•ฐไปฅไธŠใซใชใ‚Šใ€ๆœ€้•ท่พบใŒไธ€ๅฎš้‡ไปฅไธŠใซใชใ‚‹ใ‚ˆใ†ใซๅ…ฅๅŠ›็”ปๅƒใฎใ‚ตใ‚คใ‚บใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ + ๆœ€ๅคง 1333 ใƒ”ใ‚ฏใ‚ปใƒซใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚ใซใ€ๆœ€็Ÿญ่พบใŒใƒฉใƒณใƒ€ใƒ ใซ ใซ่จญๅฎšใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ‚นใ‚ฑใƒผใƒซๆ‹กๅผตใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + ๆœ€ๅฐ 480ใ€ๆœ€ๅคง 800 ใƒ”ใ‚ฏใ‚ปใƒซใ€‚ๆŽจ่ซ–ๆ™‚ใซใฏใ€ๆœ€็Ÿญ่พบใŒ 800 ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ + +ไฝฟ็”จใงใใพใ™ + [`~transformers.DetrImageProcessor`] ็”จใฎ็”ปๅƒ (ใŠใ‚ˆใณใ‚ชใƒ—ใ‚ทใƒงใƒณใฎ COCO ๅฝขๅผใฎๆณจ้‡ˆ) ใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ + ใƒขใƒ‡ใƒซใ€‚ใ“ใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใซใ‚ˆใ‚Šใ€ใƒใƒƒใƒๅ†…ใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บใŒ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ DETR ใฏใ€็”ปๅƒใ‚’ๆœ€ๅคงใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใงใ“ใฎๅ•้กŒใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ + ใฉใฎใƒ”ใ‚ฏใ‚ปใƒซใŒๅฎŸๆ•ฐใงใฉใฎใƒ”ใ‚ฏใ‚ปใƒซใŒใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใงใ‚ใ‚‹ใ‹ใ‚’็คบใ™ใƒ”ใ‚ฏใ‚ปใƒซ ใƒžใ‚นใ‚ฏใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆใ€ใƒใƒƒใƒๅ†…ใฎๆœ€ๅคงใ‚ตใ‚คใ‚บใ‚’ๆฑบๅฎšใ—ใพใ™ใ€‚ + ใ‚ใ‚‹ใ„ใฏใ€็”ปๅƒใ‚’ใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซใ‚ซใ‚นใ‚ฟใƒ ใฎ `collatโ€‹โ€‹e_fn` ใ‚’ๅฎš็พฉใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + [`~transformers.DetrImageProcessor.pad_and_create_pixel_mask`]ใ€‚ +- ็”ปๅƒใฎใ‚ตใ‚คใ‚บใซใ‚ˆใฃใฆไฝฟ็”จใ•ใ‚Œใ‚‹ใƒกใƒขใƒชใฎ้‡ใŒๆฑบใพใ‚Šใ€ใ—ใŸใŒใฃใฆใ€Œbatch_sizeใ€ใ‚‚ๆฑบใพใ‚Šใพใ™ใ€‚ + GPU ใ‚ใŸใ‚Š 2 ใฎใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใฎ Github ใ‚นใƒฌใƒƒใƒ‰](https://github.com/facebookresearch/detr/issues/150) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +DETR ใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใซใฏ 3 ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ (ๅฅฝใฟใซๅฟœใ˜ใฆ)ใ€‚ + +ใ‚ชใƒ—ใ‚ทใƒงใƒณ 1: ใƒขใƒ‡ใƒซๅ…จไฝ“ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ้‡ใฟใ‚’ไฝฟ็”จใ—ใฆ DETR ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ + +```py +>>> from transformers import DetrForObjectDetection + +>>> model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") +``` + +ใ‚ชใƒ—ใ‚ทใƒงใƒณ 2: Transformer ใซใคใ„ใฆใฏใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’ไฝฟ็”จใ—ใฆ DETR ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใŒใ€ใƒใƒƒใ‚ฏใƒœใƒผใƒณใซใคใ„ใฆใฏไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ้‡ใฟใ‚’ไฝฟ็”จใ—ใพใ™ + +```py +>>> from transformers import DetrConfig, DetrForObjectDetection + +>>> config = DetrConfig() +>>> model = DetrForObjectDetection(config) +``` + +ใ‚ชใƒ—ใ‚ทใƒงใƒณ 3: ใƒใƒƒใ‚ฏใƒœใƒผใƒณ + ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ้‡ใฟใ‚’ไฝฟ็”จใ—ใฆ DETR ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> config = DetrConfig(use_pretrained_backbone=False) +>>> model = DetrForObjectDetection(config) +``` + +| Task | Object detection | Instance segmentation | Panoptic segmentation | +|------|------------------|-----------------------|-----------------------| +| **Description** |็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅ‘จๅ›ฒใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใจใ‚ฏใƒฉใ‚น ใƒฉใƒ™ใƒซใ‚’ไบˆๆธฌใ™ใ‚‹ | ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (ใคใพใ‚Šใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น) ใฎๅ‘จๅ›ฒใฎใƒžใ‚นใ‚ฏใ‚’ไบˆๆธฌใ™ใ‚‹ | ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น) ใจใ€Œใ‚‚ใฎใ€ (ๆœจใ‚„้“่ทฏใชใฉใฎ่ƒŒๆ™ฏ) ใฎไธกๆ–นใฎๅ‘จๅ›ฒใฎใƒžใ‚นใ‚ฏใ‚’ไบˆๆธฌใ—ใพใ™ | +| **Model** | [`~transformers.DetrForObjectDetection`] | [`~transformers.DetrForSegmentation`] | [`~transformers.DetrForSegmentation`] | +| **Example dataset** | COCO detection | COCO detection, COCO panoptic | COCO panoptic | | +| **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `list[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `list[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} and masks_path (path to directory containing PNG files of the masks) | +| **Postprocessing** (i.e. converting the output of the model to Pascal VOC format) | [`~transformers.DetrImageProcessor.post_process`] | [`~transformers.DetrImageProcessor.post_process_segmentation`] | [`~transformers.DetrImageProcessor.post_process_segmentation`], [`~transformers.DetrImageProcessor.post_process_panoptic`] | +| **evaluators** | `CocoEvaluator` with `iou_types="bbox"` | `CocoEvaluator` with `iou_types="bbox"` or `"segm"` | `CocoEvaluator` with `iou_tupes="bbox"` or `"segm"`, `PanopticEvaluator` | + +ใคใพใ‚Šใ€COCO ๆคœๅ‡บใพใŸใฏ COCO ใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏๅฝขๅผใงใƒ‡ใƒผใ‚ฟใ‚’ๆบ–ๅ‚™ใ—ใฆใ‹ใ‚‰ใ€ๆฌกใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[`~transformers.DetrImageProcessor`] `pixel_values`ใ€`pixel_mask`ใ€ใŠใ‚ˆใณใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ€Œใƒฉใƒ™ใƒซใ€ใ€‚ใ“ใ‚Œใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ (ใพใŸใฏๅพฎ่ชฟๆ•ด) ใงใใพใ™ใ€‚่ฉ•ไพกใ™ใ‚‹ใซใฏใ€ใพใšใ€ +[`~transformers.DetrImageProcessor`] ใฎๅพŒๅ‡ฆ็†ใƒกใ‚ฝใƒƒใƒ‰ใฎ 1 ใคใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใ€‚ใ“ใ‚Œใ‚‰ใฏใงใใพใ™ +`CocoEvaluator` ใพใŸใฏ `PanopticEvaluator` ใฎใ„ใšใ‚Œใ‹ใซๆไพ›ใ•ใ‚Œใ€ๆฌกใฎใ‚ˆใ†ใชใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใงใใพใ™ใ€‚ +ๅนณๅ‡ๅนณๅ‡็ฒพๅบฆ (mAP) ใจใƒ‘ใƒŽใƒฉใƒžๅ“่ณช (PQ)ใ€‚ๅพŒ่€…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏ [ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒช](https://github.com/facebookresearch/detr) ใซๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ™ใ€‚่ฉ•ไพกใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ‚ตใƒณใƒ—ใƒซ ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Resources + +DETR ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + +- ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ [`DetrForObjectDetection`] ใจ [`DetrForSegmentation`] ใฎๅพฎ่ชฟๆ•ดใ‚’่ชฌๆ˜Žใ™ใ‚‹ใ™ในใฆใฎใ‚ตใƒณใƒ—ใƒซ ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใฏใ€[ใ“ใกใ‚‰](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ ใ€‚ +- ๅ‚็…ง: [ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/object_detection) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## DetrConfig + +[[autodoc]] DetrConfig + +## DetrImageProcessor + +[[autodoc]] DetrImageProcessor + - preprocess + - post_process_object_detection + - post_process_semantic_segmentation + - post_process_instance_segmentation + - post_process_panoptic_segmentation + +## DetrImageProcessorFast + +[[autodoc]] DetrImageProcessorFast + - preprocess + - post_process_object_detection + - post_process_semantic_segmentation + - post_process_instance_segmentation + - post_process_panoptic_segmentation + +## DetrFeatureExtractor + +[[autodoc]] DetrFeatureExtractor + - __call__ + - post_process_object_detection + - post_process_semantic_segmentation + - post_process_instance_segmentation + - post_process_panoptic_segmentation + +## DETR specific outputs + +[[autodoc]] models.detr.modeling_detr.DetrModelOutput + +[[autodoc]] models.detr.modeling_detr.DetrObjectDetectionOutput + +[[autodoc]] models.detr.modeling_detr.DetrSegmentationOutput + +## DetrModel + +[[autodoc]] DetrModel + - forward + +## DetrForObjectDetection + +[[autodoc]] DetrForObjectDetection + - forward + +## DetrForSegmentation + +[[autodoc]] DetrForSegmentation + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dialogpt.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dialogpt.md new file mode 100644 index 0000000000000000000000000000000000000000..80e54237854bbf2d3c7f1f8d6f8ef1b2c19f2557 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dialogpt.md @@ -0,0 +1,57 @@ + + +# DialoGPT + +## Overview + +DialoGPT ใฏใ€[DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) ใง Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, +Jianfeng Gao, Jingjing Liu, Bill Dolan.ใ“ใ‚Œใฏใ€ใ‹ใ‚‰ๆŠฝๅ‡บใ•ใ‚ŒใŸ 147M ไธ‡ใฎไผš่ฉฑใฎใ‚ˆใ†ใชใ‚„ใ‚Šใจใ‚Šใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ GPT2 ใƒขใƒ‡ใƒซใงใ™ใ€‚ +ใƒฌใƒ‡ใ‚ฃใƒƒใƒˆใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*็งใŸใกใฏใ€ๅคง่ฆๆจกใง่ชฟๆ•ดๅฏ่ƒฝใชใƒ‹ใƒฅใƒผใƒฉใƒซไผš่ฉฑๅฟœ็ญ”็”Ÿๆˆใƒขใƒ‡ใƒซ DialoGPT (ๅฏพ่ฉฑ็”Ÿๆˆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟ) ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ +ๅค‰ๆˆๅ™จ๏ผ‰ใ€‚ Reddit ใฎใ‚ณใƒกใƒณใƒˆ ใƒใ‚งใƒผใƒณใ‹ใ‚‰ๆŠฝๅ‡บใ•ใ‚ŒใŸ 1 ๅ„„ 4,700 ไธ‡ไปถใฎไผš่ฉฑใฎใ‚ˆใ†ใชใ‚„ใ‚Šๅ–ใ‚Šใ‚’ๅฏพ่ฑกใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ +2005 ๅนดใ‹ใ‚‰ 2017 ๅนดใซใ‹ใ‘ใฆใ€DialoGPT ใฏไบบ้–“ใซ่ฟ‘ใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’้”ๆˆใ™ใ‚‹ใŸใ‚ใซ Hugging Face PyTorch ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ๆ‹กๅผตใ—ใพใ—ใŸใ€‚ +ใ‚ทใƒณใ‚ฐใƒซใ‚ฟใƒผใƒณใƒ€ใ‚คใ‚ขใƒญใ‚ฐ่จญๅฎšใซใŠใ‘ใ‚‹่‡ชๅ‹•่ฉ•ไพกใจไบบ้–“ใซใ‚ˆใ‚‹่ฉ•ไพกใฎไธกๆ–นใ€‚ไผš่ฉฑใ‚ทใ‚นใƒ†ใƒ ใŒ +DialoGPT ใ‚’ๆดป็”จใ™ใ‚‹ใจใ€ๅผทๅŠ›ใชใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใ‚ˆใ‚Šใ‚‚้–ข้€ฃๆ€งใŒ้ซ˜ใใ€ๅ†…ๅฎนใŒๅ……ๅฎŸใ—ใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใซไธ€่ฒซๆ€งใฎใ‚ใ‚‹ๅฟœ็ญ”ใŒ็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ +ใ‚ทใ‚นใƒ†ใƒ ใ€‚็ฅž็ตŒๅๅฟœใฎ็ ”็ฉถใ‚’ไฟƒ้€ฒใ™ใ‚‹ใŸใ‚ใซใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใŒๅ…ฌ้–‹ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ใ‚ˆใ‚Šใ‚คใƒณใƒ†ใƒชใ‚ธใ‚งใƒณใƒˆใชใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณๅฏพ่ฉฑใ‚ทใ‚นใƒ†ใƒ ใฎ็”Ÿๆˆใจ้–‹็™บใ€‚* + +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/microsoft/DialoGPT) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + + +- DialoGPT ใฏ็ตถๅฏพไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚’ๅ‚™ใˆใŸใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฏๅ…ฅๅŠ›ใ‚’ๅณๅดใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + ๅทฆใ‚ˆใ‚Šใ‚‚ใ€‚ +- DialoGPT ใฏใ€ไผš่ฉฑใƒ‡ใƒผใ‚ฟใฎๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ (CLM) ็›ฎๆจ™ใซๅŸบใฅใ„ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๅผทๅŠ›ใงใ™ + ใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณๅฏพ่ฉฑใ‚ทใ‚นใƒ†ใƒ ใซใŠใ‘ใ‚‹ๅฟœ็ญ”็”Ÿๆˆๆ™‚ใ€‚ +- DialoGPT ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€[DialoGPT's model card](https://huggingface.co/microsoft/DialoGPT-medium) ใซ็คบใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€ใƒฆใƒผใ‚ถใƒผใฏใ‚ใšใ‹ 10 ่กŒใฎใ‚ณใƒผใƒ‰ใงใƒใƒฃใƒƒใƒˆ ใƒœใƒƒใƒˆใ‚’ไฝœๆˆใงใใพใ™ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผš + +DialoGPT ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใพใŸใฏๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ๅ…ฌๅผ่ซ–ๆ–‡ใ‚’ๅผ•็”จใ™ใ‚‹ใจ๏ผš *็งใŸใกใฏ +OpenAI GPT-2ใซๅพ“ใฃใฆใ€ใƒžใƒซใƒใ‚ฟใƒผใƒณๅฏพ่ฉฑใ‚ปใƒƒใ‚ทใƒงใƒณใ‚’้•ทใ„ใƒ†ใ‚ญใ‚นใƒˆใจใ—ใฆใƒขใƒ‡ใƒซๅŒ–ใ—ใ€็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใ‚’่จ€่ชžใจใ—ใฆใƒ•ใƒฌใƒผใƒ ๅŒ–ใ—ใพใ™ +ใƒขใƒ‡ใƒชใƒณใ‚ฐใ€‚ใพใšใ€ใƒ€ใ‚คใ‚ขใƒญใ‚ฐ ใ‚ปใƒƒใ‚ทใƒงใƒณๅ†…ใฎใ™ในใฆใฎใƒ€ใ‚คใ‚ขใƒญใ‚ฐ ใ‚ฟใƒผใƒณใ‚’้•ทใ„ใƒ†ใ‚ญใ‚นใƒˆ x_1,..., x_N ใซ้€ฃ็ตใ—ใพใ™ (N ใฏ +* ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๅ…ƒใฎ่ซ–ๆ–‡ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +DialoGPT ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏ GPT2 ใƒขใƒ‡ใƒซใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใจไพ‹ใซใคใ„ใฆใฏใ€[GPT2 ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ ใƒšใƒผใ‚ธ](openai-community/gpt2) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dinat.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dinat.md new file mode 100644 index 0000000000000000000000000000000000000000..ee373cdd75ad4fd7a8629dccf237ef4d551cf469 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_doc/dinat.md @@ -0,0 +1,93 @@ + + +# Dilated Neighborhood Attention Transformer + +## Overview + +DiNAT ใฏ [Dilated Neighborhood Attender Transformer](https://huggingface.co/papers/2209.15001) ใงๆๆกˆใ•ใ‚Œใพใ—ใŸใ€‚ +Ali Hassani and Humphrey Shi. + +[NAT](nat) ใ‚’ๆ‹กๅผตใ™ใ‚‹ใŸใ‚ใซใ€ๆ‹กๅผต่ฟ‘้šฃใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ ใƒ‘ใ‚ฟใƒผใƒณใ‚’่ฟฝๅŠ ใ—ใฆใ‚ฐใƒญใƒผใƒใƒซ ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ—ใพใ™ใ€‚ +ใใ—ใฆใใ‚Œใจๆฏ”่ผƒใ—ใฆๅคงๅน…ใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใŒ่ฆ‹ใ‚‰ใ‚Œใพใ™ใ€‚ + +่ซ–ๆ–‡ใฎ่ฆ็ด„ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +*ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏๆ€ฅ้€Ÿใซใ€ใ•ใพใ–ใพใชใƒขใƒ€ใƒชใƒ†ใ‚ฃใซใ‚ใŸใฃใฆๆœ€ใ‚‚้ ป็นใซ้ฉ็”จใ•ใ‚Œใ‚‹ๆทฑๅฑคๅญฆ็ฟ’ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎ 1 ใคใซใชใ‚Šใคใคใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‰ใƒกใ‚คใƒณใจใ‚ฟใ‚นใ‚ฏใ€‚ใƒ“ใ‚ธใƒงใƒณใงใฏใ€ๅ˜็ด”ใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใธใฎ็ถ™็ถš็š„ใชๅ–ใ‚Š็ต„ใฟใซๅŠ ใˆใฆใ€้šŽๅฑคๅž‹ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใŒ +ใพใŸใ€ใใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจๆ—ขๅญ˜ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใธใฎ็ฐกๅ˜ใช็ตฑๅˆใฎใŠใ‹ใ’ใงใ€ๅคงใใชๆณจ็›ฎใ‚’้›†ใ‚ใพใ—ใŸใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏ้€šๅธธใ€ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐ ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฎ่ฟ‘้šฃใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ (NA) ใชใฉใฎๅฑ€ๆ‰€็š„ใชๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ๆŽก็”จใ—ใฆใ„ใพใ™ใ€‚ +ใพใŸใฏ Swin Transformer ใฎใ‚ทใƒ•ใƒˆ ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆ ใ‚ปใƒซใƒ• ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ€‚่‡ชๅทฑๆณจๆ„ใฎไบŒๆฌก่ค‡้›‘ใ•ใ‚’่ปฝๆธ›ใ™ใ‚‹ใฎใซๅŠนๆžœ็š„ใงใ™ใŒใ€ +ๅฑ€ๆ‰€็š„ใชๆณจๆ„ใฏใ€่‡ชๅทฑๆณจๆ„ใฎๆœ€ใ‚‚ๆœ›ใพใ—ใ„ 2 ใคใฎ็‰นๆ€งใ‚’ๅผฑใ‚ใพใ™ใ€‚ใใ‚Œใฏใ€้•ท่ท้›ขใฎ็›ธไบ’ไพๅญ˜ๆ€งใƒขใƒ‡ใƒชใƒณใ‚ฐใงใ™ใ€‚ +ใใ—ใฆๅ…จไฝ“็š„ใชๅ—ๅฎน้‡Žใ€‚ใ“ใฎใƒšใƒผใƒ‘ใƒผใงใฏใ€่‡ช็„ถใงๆŸ”่ปŸใงใ€ +NA ใธใฎๅŠน็އ็š„ใชๆ‹กๅผตใซใ‚ˆใ‚Šใ€ใ‚ˆใ‚Šใ‚ฐใƒญใƒผใƒใƒซใชใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ•ๆ‰ใ—ใ€ๅ—ๅฎน้‡Žใ‚’ใ‚ผใƒญใ‹ใ‚‰ๆŒ‡ๆ•ฐ้–ขๆ•ฐ็š„ใซๆ‹กๅผตใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +่ฟฝๅŠ ่ฒป็”จใ€‚ NA ใฎใƒญใƒผใ‚ซใƒซใชๆณจ็›ฎใจ DiNA ใฎใพใฐใ‚‰ใชใ‚ฐใƒญใƒผใƒใƒซใชๆณจ็›ฎใฏ็›ธไบ’ใซ่ฃœๅฎŒใ—ๅˆใ†ใŸใ‚ใ€็งใŸใกใฏ +ไธกๆ–นใซๅŸบใฅใ„ใฆๆง‹็ฏ‰ใ•ใ‚ŒใŸๆ–ฐใ—ใ„้šŽๅฑคๅž‹ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ‚ใ‚‹ Dilated Neighborhood Attendant Transformer (DiNAT) ใ‚’ๅฐŽๅ…ฅใ—ใพใ™ใ€‚ +DiNAT ใฎใƒใƒชใ‚ขใƒณใƒˆใฏใ€NATใ€Swinใ€ConvNeXt ใชใฉใฎๅผทๅŠ›ใชใƒ™ใƒผใ‚นใƒฉใ‚คใƒณใซๆฏ”ในใฆๅคงๅน…ใซๆ”นๅ–„ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +็งใŸใกใฎๅคง่ฆๆจกใƒขใƒ‡ใƒซใฏใ€COCO ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใซใŠใ„ใฆ Swin ใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚้ซ˜้€Ÿใงใ€ใƒœใƒƒใ‚ฏใ‚น AP ใŒ 1.5% ๅ„ชใ‚Œใฆใ„ใพใ™ใ€‚ +COCO ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใงใฏ 1.3% ใฎใƒžใ‚นใ‚ฏ APใ€ADE20K ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใงใฏ 1.1% ใฎ mIoUใ€‚ +ๆ–ฐใ—ใ„ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใจ็ต„ใฟๅˆใ‚ใ›ใŸๅฝ“็คพใฎๅคง่ฆๆจกใƒใƒชใ‚ขใƒณใƒˆใฏใ€COCO (58.2 PQ) ไธŠใฎๆ–ฐใ—ใ„ๆœ€ๅ…ˆ็ซฏใฎใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒขใƒ‡ใƒซใงใ™ใ€‚ +ใŠใ‚ˆใณ ADE20K (48.5 PQ)ใ€ใŠใ‚ˆใณ Cityscapes (44.5 AP) ใŠใ‚ˆใณ ADE20K (35.4 AP) ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒขใƒ‡ใƒซ (่ฟฝๅŠ ใƒ‡ใƒผใ‚ฟใชใ—)ใ€‚ +ใพใŸใ€ADE20K (58.2 mIoU) ไธŠใฎๆœ€ๅ…ˆ็ซฏใฎ็‰นๆฎŠใชใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒขใƒ‡ใƒซใจใ‚‚ไธ€่‡ดใ—ใพใ™ใ€‚ +้ƒฝๅธ‚ๆ™ฏ่ฆณ (84.5 mIoU) ใงใฏ 2 ไฝใซใƒฉใƒณใ‚ฏใ•ใ‚Œใฆใ„ใพใ™ (่ฟฝๅŠ ใƒ‡ใƒผใ‚ฟใชใ—)ใ€‚ * + + + + + ็•ฐใชใ‚‹ๆ‹กๅผตๅ€คใ‚’ไฝฟ็”จใ—ใŸ่ฟ‘้šฃใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ€‚ +ๅ…ƒใฎ่ซ–ๆ–‡ใ‹ใ‚‰ๆŠœ็ฒ‹ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ [Ali Hassani](https://huggingface.co/alihassanijr) ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใพใ—ใŸใ€‚ +ๅ…ƒใฎใ‚ณใƒผใƒ‰ใฏ [ใ“ใ“](https://github.com/SHI-Labs/Neighborhood-Attendance-Transformer) ใซใ‚ใ‚Šใพใ™ใ€‚ + +## Usage tips + +DiNAT ใฏ *ใƒใƒƒใ‚ฏใƒœใƒผใƒณ* ใจใ—ใฆไฝฟ็”จใงใใพใ™ใ€‚ ใ€Œoutput_hidden_โ€‹โ€‹states = Trueใ€ใฎๅ ดๅˆใ€ +`hidden_โ€‹โ€‹states` ใจ `reshaped_hidden_โ€‹โ€‹states` ใฎไธกๆ–นใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ `reshape_hidden_โ€‹โ€‹states` ใฏใ€`(batch_size, height, width, num_channels)` ใงใฏใชใใ€`(batch, num_channels, height, width)` ใฎๅฝข็Šถใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ + +ใƒŽใƒผใƒˆ๏ผš +- DiNAT ใฏใ€[NATTEN](https://github.com/SHI-Labs/NATTEN/) ใซใ‚ˆใ‚‹่ฟ‘้šฃใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจๆ‹กๅผต่ฟ‘้šฃใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฎๅฎŸ่ฃ…ใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚ +[shi-labs.com/natten](https://shi-labs.com/natten) ใ‚’ๅ‚็…งใ—ใฆใ€Linux ็”จใฎใƒ“ใƒซใƒ‰ๆธˆใฟใƒ›ใ‚คใƒผใƒซใ‚’ไฝฟ็”จใ—ใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ‹ใ€`pip install natten` ใ‚’ๅฎŸ่กŒใ—ใฆใ‚ทใ‚นใƒ†ใƒ ไธŠใซๆง‹็ฏ‰ใงใใพใ™ใ€‚ +ๅพŒ่€…ใฏใ‚ณใƒณใƒ‘ใ‚คใƒซใซๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ NATTEN ใฏใพใ  Windows ใƒ‡ใƒใ‚คใ‚นใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ใ€‚ +- ็พๆ™‚็‚นใงใฏใƒ‘ใƒƒใƒ ใ‚ตใ‚คใ‚บ 4 ใฎใฟใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## Resources + +DiNAT ใฎไฝฟ็”จใ‚’้–‹ๅง‹ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ…ฌๅผ Hugging Face ใŠใ‚ˆใณใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃ (๐ŸŒŽ ใง็คบใ•ใ‚Œใฆใ„ใ‚‹) ใƒชใ‚ฝใƒผใ‚นใฎใƒชใ‚นใƒˆใ€‚ + + + + +- [`DinatForImageClassification`] ใฏใ€ใ“ใฎ [ใ‚ตใƒณใƒ—ใƒซ ใ‚นใ‚ฏใƒชใƒ—ใƒˆ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification) ใŠใ‚ˆใณ [ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)ใ€‚ +- ๅ‚็…ง: [็”ปๅƒๅˆ†้กžใ‚ฟใ‚นใ‚ฏ ใ‚ฌใ‚คใƒ‰](../tasks/image_classification) + +ใ“ใ“ใซๅซใ‚ใ‚‹ใƒชใ‚ฝใƒผใ‚นใฎ้€ไฟกใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ใŠๆฐ—่ปฝใซใƒ—ใƒซ ใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใ„ใฆใใ ใ•ใ„ใ€‚ๅฏฉๆŸปใ•ใ›ใฆใ„ใŸใ ใใพใ™ใ€‚ใƒชใ‚ฝใƒผใ‚นใฏใ€ๆ—ขๅญ˜ใฎใƒชใ‚ฝใƒผใ‚นใ‚’่ค‡่ฃฝใ™ใ‚‹ใฎใงใฏใชใใ€ไฝ•ใ‹ๆ–ฐใ—ใ„ใ‚‚ใฎใ‚’็คบใ™ใ“ใจใŒ็†ๆƒณ็š„ใงใ™ใ€‚ + +## DinatConfig + +[[autodoc]] DinatConfig + +## DinatModel + +[[autodoc]] DinatModel + - forward + +## DinatForImageClassification + +[[autodoc]] DinatForImageClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_memory_anatomy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_memory_anatomy.md new file mode 100644 index 0000000000000000000000000000000000000000..c8b7c90a946835b41a7a3593031f038d0b0271d3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_memory_anatomy.md @@ -0,0 +1,255 @@ + + +# Model training anatomy + +ใƒขใƒ‡ใƒซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซ้ฉ็”จใงใใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’็†่งฃใ™ใ‚‹ใซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซGPUใŒใฉใฎใ‚ˆใ†ใซๅˆฉ็”จใ•ใ‚Œใ‚‹ใ‹ใ€ใŠใ‚ˆใณๅฎŸ่กŒใ•ใ‚Œใ‚‹ๆ“ไฝœใซๅฟœใ˜ใฆ่จˆ็ฎ—ๅผทๅบฆใŒใฉใฎใ‚ˆใ†ใซๅค‰ๅŒ–ใ™ใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹ใ“ใจใŒๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใพใšใฏใ€GPUใฎๅˆฉ็”จไพ‹ใจใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฎŸ่กŒใซ้–ขใ™ใ‚‹็คบๅ”†ใซๅฏŒใ‚€ไพ‹ใ‚’ๆŽขๆฑ‚ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚ใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณใฎใŸใ‚ใซใ€ใ„ใใคใ‹ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™: + +```bash +pip install transformers datasets accelerate nvidia-ml-py3 +``` + +`nvidia-ml-py3` ใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€Pythonๅ†…ใ‹ใ‚‰ใƒขใƒ‡ใƒซใฎใƒกใƒขใƒชไฝฟ็”จ็Šถๆณใ‚’ใƒขใƒ‹ใ‚ฟใƒผใ™ใ‚‹ใ“ใจใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ใŠใใ‚‰ใใ€ใ‚ฟใƒผใƒŸใƒŠใƒซใงใฎ `nvidia-smi` ใ‚ณใƒžใƒณใƒ‰ใซใคใ„ใฆใฏใŠ่žใใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€Pythonใ‹ใ‚‰ๅŒใ˜ๆƒ…ๅ ฑใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ + +ใใ‚Œใ‹ใ‚‰ใ€ใ„ใใคใ‹ใฎใƒ€ใƒŸใƒผใƒ‡ใƒผใ‚ฟใ‚’ไฝœๆˆใ—ใพใ™ใ€‚100ใ‹ใ‚‰30000ใฎ้–“ใฎใƒฉใƒณใƒ€ใƒ ใชใƒˆใƒผใ‚ฏใƒณIDใจใ€ๅˆ†้กžๅ™จใฎใŸใ‚ใฎใƒใ‚คใƒŠใƒชใƒฉใƒ™ใƒซใงใ™ใ€‚ๅˆ่จˆใงใ€512ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚Šใ€ใใ‚Œใžใ‚Œใฎ้•ทใ•ใฏ512ใงใ€PyTorchใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใฎ [`~datasets.Dataset`] ใซๆ ผ็ดใ•ใ‚Œใพใ™ใ€‚ + + +```py +>>> import numpy as np +>>> from datasets import Dataset + + +>>> seq_len, dataset_size = 512, 512 +>>> dummy_data = { +... "input_ids": np.random.randint(100, 30000, (dataset_size, seq_len)), +... "labels": np.random.randint(0, 1, (dataset_size)), +... } +>>> ds = Dataset.from_dict(dummy_data) +>>> ds.set_format("pt") +``` + + +[`Trainer`]ใ‚’ไฝฟ็”จใ—ใฆGPUๅˆฉ็”จ็އใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฎŸ่กŒใฎ่ฆ็ด„็ตฑ่จˆๆƒ…ๅ ฑใ‚’่กจ็คบใ™ใ‚‹ใŸใ‚ใซใ€2ใคใฎใƒ˜ใƒซใƒ‘ใƒผ้–ขๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + + +```py +>>> from pynvml import * + + +>>> def print_gpu_utilization(): +... nvmlInit() +... handle = nvmlDeviceGetHandleByIndex(0) +... info = nvmlDeviceGetMemoryInfo(handle) +... print(f"GPU memory occupied: {info.used//1024**2} MB.") + + +>>> def print_summary(result): +... print(f"Time: {result.metrics['train_runtime']:.2f}") +... print(f"Samples/second: {result.metrics['train_samples_per_second']:.2f}") +... print_gpu_utilization() +``` + +ไปฅไธ‹ใฏใ€็„กๆ–™ใฎGPUใƒกใƒขใƒชใ‹ใ‚‰้–‹ๅง‹ใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ—ใ‚‡ใ†๏ผš + + +```py +>>> print_gpu_utilization() +GPU memory occupied: 0 MB. +``` + +GPUใƒกใƒขใƒชใŒใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ๅ‰ใฎใ‚ˆใ†ใซๅ ๆœ‰ใ•ใ‚Œใฆใ„ใชใ„ใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใ€‚ใ“ใ‚ŒใŒใŠไฝฟใ„ใฎใƒžใ‚ทใƒณใงใฎ็Šถๆณใงใชใ„ๅ ดๅˆใฏใ€GPUใƒกใƒขใƒชใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ™ในใฆใฎใƒ—ใƒญใ‚ปใ‚นใ‚’ๅœๆญขใ—ใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€ใ™ในใฆใฎ็ฉบใGPUใƒกใƒขใƒชใ‚’ใƒฆใƒผใ‚ถใƒผใŒไฝฟ็”จใงใใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒขใƒ‡ใƒซใŒGPUใซ่ชญใฟ่พผใพใ‚Œใ‚‹ใจใ€ใ‚ซใƒผใƒใƒซใ‚‚่ชญใฟ่พผใพใ‚Œใ€1ใ€œ2GBใฎใƒกใƒขใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใใ‚ŒใŒใฉใ‚Œใใ‚‰ใ„ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€GPUใซๅฐใ•ใชใƒ†ใƒณใ‚ฝใƒซใ‚’่ชญใฟ่พผใ‚€ใจใ€ใ‚ซใƒผใƒใƒซใ‚‚่ชญใฟ่พผใพใ‚Œใพใ™ใ€‚ + + +```py +>>> import torch + + +>>> torch.ones((1, 1)).to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 1343 MB. +``` + +ใ‚ซใƒผใƒใƒซใ ใ‘ใง1.3GBใฎGPUใƒกใƒขใƒชใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ๆฌกใซใ€ใƒขใƒ‡ใƒซใŒใฉใ‚Œใ ใ‘ใฎใ‚นใƒšใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +## Load Model + +ใพใšใ€`google-bert/bert-large-uncased` ใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’็›ดๆŽฅGPUใซ่ชญใฟ่พผใ‚€ใ“ใจใงใ€้‡ใฟใ ใ‘ใŒใฉใ‚Œใ ใ‘ใฎใ‚นใƒšใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใงใใพใ™ใ€‚ + + +```py +>>> from transformers import AutoModelForSequenceClassification + + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-large-uncased").to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 2631 MB. +``` + +ใƒขใƒ‡ใƒซใฎ้‡ใฟใ ใ‘ใงใ€GPUใƒกใƒขใƒชใ‚’1.3 GBไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ๆญฃ็ขบใชๆ•ฐๅ€คใฏใ€ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ…ทไฝ“็š„ใชGPUใซไพๅญ˜ใ—ใพใ™ใ€‚ๆ–ฐใ—ใ„GPUใงใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸๆ–นๆณ•ใง่ชญใฟ่พผใพใ‚Œใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใฎไฝฟ็”จใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใŒใ‚ˆใ‚Šๅคšใใฎใ‚นใƒšใƒผใ‚นใ‚’ๅ ๆœ‰ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ•ใฆใ€`nvidia-smi` CLIใจๅŒใ˜็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใ‚‹ใ‹ใ‚’็ฐกๅ˜ใซ็ขบ่ชใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + +```bash +nvidia-smi +``` + +```bash +Tue Jan 11 08:58:05 2022 ++-----------------------------------------------------------------------------+ +| NVIDIA-SMI 460.91.03 Driver Version: 460.91.03 CUDA Version: 11.2 | +|-------------------------------+----------------------+----------------------+ +| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|===============================+======================+======================| +| 0 Tesla V100-SXM2... On | 00000000:00:04.0 Off | 0 | +| N/A 37C P0 39W / 300W | 2631MiB / 16160MiB | 0% Default | +| | | N/A | ++-------------------------------+----------------------+----------------------+ + ++-----------------------------------------------------------------------------+ +| Processes: | +| GPU GI CI PID Type Process name GPU Memory | +| ID ID Usage | +|=============================================================================| +| 0 N/A N/A 3721 C ...nvs/codeparrot/bin/python 2629MiB | ++-----------------------------------------------------------------------------+ +``` + +ๅ‰ๅ›žใจๅŒใ˜ๆ•ฐๅ€คใ‚’ๅ–ๅพ—ใ—ใ€16GBใฎใƒกใƒขใƒชใ‚’ๆญ่ผ‰ใ—ใŸV100 GPUใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ•ใฆใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใ€GPUใƒกใƒขใƒชใฎๆถˆ่ฒปใŒใฉใฎใ‚ˆใ†ใซๅค‰ๅŒ–ใ™ใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ใพใšใ€ใ„ใใคใ‹ใฎๆจ™ๆบ–็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’่จญๅฎšใ—ใพใ™: + + +```py +default_args = { + "output_dir": "tmp", + "eval_strategy": "steps", + "num_train_epochs": 1, + "log_level": "error", + "report_to": "none", +} +``` + + + +่ค‡ๆ•ฐใฎๅฎŸ้จ“ใ‚’ๅฎŸ่กŒใ™ใ‚‹ไบˆๅฎšใŒใ‚ใ‚‹ๅ ดๅˆใ€ๅฎŸ้จ“้–“ใงใƒกใƒขใƒชใ‚’้ฉๅˆ‡ใซใ‚ฏใƒชใ‚ขใ™ใ‚‹ใŸใ‚ใซใ€ๅฎŸ้จ“ใฎ้–“ใซ Python ใ‚ซใƒผใƒใƒซใ‚’ๅ†่ตทๅ‹•ใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## Memory utilization at vanilla training + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใฆใ€GPU ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๆœ€้ฉๅŒ–ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’ไฝฟ็”จใ›ใšใซใƒใƒƒใƒใ‚ตใ‚คใ‚บ 4 ใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ—ใ‚‡ใ†๏ผš + + +```py +>>> from transformers import TrainingArguments, Trainer, logging + +>>> logging.set_verbosity_error() + + +>>> training_args = TrainingArguments(per_device_train_batch_size=4, **default_args) +>>> trainer = Trainer(model=model, args=training_args, train_dataset=ds) +>>> result = trainer.train() +>>> print_summary(result) +``` + +``` +Time: 57.82 +Samples/second: 8.86 +GPU memory occupied: 14949 MB. +``` + +ๆ—ขใซใ€ๆฏ”่ผƒ็š„ๅฐใ•ใ„ใƒใƒƒใƒใ‚ตใ‚คใ‚บใงใ‚‚ใ€GPUใฎใปใจใ‚“ใฉใฎใƒกใƒขใƒชใŒใ™ใงใซไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใ€ใ—ใฐใ—ใฐใƒขใƒ‡ใƒซใฎๅŽๆŸใŒ้€ŸใใชใฃใŸใ‚Šใ€ๆœ€็ต‚็š„ใชๆ€ง่ƒฝใŒๅ‘ไธŠใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€็†ๆƒณ็š„ใซใฏใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ใƒขใƒ‡ใƒซใฎ่ฆไปถใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ—ใŸใ„ใฎใงใ™ใŒใ€GPUใฎๅˆถ้™ใซๅˆใ‚ใ›ใฆ่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚่ˆˆๅ‘ณๆทฑใ„ใ“ใจใซใ€ใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซๅคšใใฎใƒกใƒขใƒชใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใชใœใใ†ใชใ‚‹ใฎใ‹ใ‚’ๅฐ‘ใ—็†่งฃใ™ใ‚‹ใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใฎๆ“ไฝœใจใƒกใƒขใƒชใฎๅฟ…่ฆๆ€งใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +## Anatomy of Model's Operations + +Transformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซใฏใ€่จˆ็ฎ—ๅผทๅบฆใซใ‚ˆใฃใฆไปฅไธ‹ใฎ3ใคใฎไธป่ฆใชๆ“ไฝœใ‚ฐใƒซใƒผใƒ—ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + +1. **ใƒ†ใƒณใ‚ฝใƒซใฎๅŽ็ธฎ** + + ็ทšๅฝขๅฑคใจMulti-Head Attentionใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏใ€ใ™ในใฆใƒใƒƒใƒๅ‡ฆ็†ใ•ใ‚ŒใŸ **่กŒๅˆ—-่กŒๅˆ—ใฎไน—็ฎ—** ใ‚’่กŒใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๆ“ไฝœใฏใ€Transformerใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใŠใ„ใฆๆœ€ใ‚‚่จˆ็ฎ—้›†็ด„็š„ใช้ƒจๅˆ†ใงใ™ใ€‚ + +2. **็ตฑ่จˆ็š„ๆญฃ่ฆๅŒ–** + + Softmaxใจๅฑคๆญฃ่ฆๅŒ–ใฏใ€ใƒ†ใƒณใ‚ฝใƒซใฎๅŽ็ธฎใ‚ˆใ‚Šใ‚‚่จˆ็ฎ—่ฒ ่ทใŒๅฐ‘ใชใใ€1ใคใพใŸใฏ่ค‡ๆ•ฐใฎ **็ธฎ็ด„ๆ“ไฝœ** ใ‚’ๅซใฟใ€ใใฎ็ตๆžœใŒใƒžใƒƒใƒ—ใ‚’ไป‹ใ—ใฆ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + +3. **่ฆ็ด ใ”ใจใฎๆผ”็ฎ—ๅญ** + + ใ“ใ‚Œใ‚‰ใฏๆฎ‹ใ‚Šใฎๆผ”็ฎ—ๅญใงใ™๏ผš**ใƒใ‚คใ‚ขใ‚นใ€ใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆใ€ๆดปๆ€งๅŒ–ใ€ใŠใ‚ˆใณๆฎ‹ๅทฎๆŽฅ็ถš** ใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏๆœ€ใ‚‚่จˆ็ฎ—้›†็ด„็š„ใชๆ“ไฝœใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎใƒœใƒˆใƒซใƒใƒƒใ‚ฏใ‚’ๅˆ†ๆžใ™ใ‚‹้š›ใซใ€ใ“ใฎ็Ÿฅ่ญ˜ใฏๅฝน็ซ‹ใคใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎ่ฆ็ด„ใฏใ€[Data Movement Is All You Need: Optimizing Transformers 2020ใซ้–ขใ™ใ‚‹ใ‚ฑใƒผใ‚นใ‚นใ‚ฟใƒ‡ใ‚ฃ](https://huggingface.co/papers/2007.00072)ใ‹ใ‚‰ๆดพ็”Ÿใ—ใฆใ„ใพใ™ใ€‚ + +## Anatomy of Model's Memory + +ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒGPUใซ้…็ฝฎใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซๅคšใใฎใƒกใƒขใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’่ฆ‹ใฆใใพใ—ใŸใ€‚ใ“ใ‚Œใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซGPUใƒกใƒขใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ๅคšใใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใงใ™ใ€‚GPUใƒกใƒขใƒชไธŠใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +1. ใƒขใƒ‡ใƒซใฎ้‡ใฟ +2. ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ +3. ๅ‹พ้… +4. ๅ‹พ้…่จˆ็ฎ—ใฎใŸใ‚ใซไฟๅญ˜ใ•ใ‚ŒใŸๅ‰ๅ‘ใๆดปๆ€งๅŒ– +5. ไธ€ๆ™‚ใƒใƒƒใƒ•ใ‚ก +6. ๆฉŸ่ƒฝๅ›บๆœ‰ใฎใƒกใƒขใƒช + +้€šๅธธใ€AdamWใ‚’ไฝฟ็”จใ—ใฆๆททๅˆ็ฒพๅบฆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€ใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใ”ใจใซ18ใƒใ‚คใƒˆใจใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™ใ€‚ๆŽจ่ซ–ใงใฏใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใจๅ‹พ้…ใฏไธ่ฆใงใ™ใฎใงใ€ใ“ใ‚Œใ‚‰ใ‚’ๅทฎใ—ๅผ•ใใ“ใจใŒใงใใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆททๅˆ็ฒพๅบฆใฎๆŽจ่ซ–ใซใŠใ„ใฆใฏใ€ใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใ”ใจใซ6ใƒใ‚คใƒˆใจใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™ใ€‚ + +่ฉณ็ดฐใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +**ใƒขใƒ‡ใƒซใฎ้‡ใฟ:** + +- fp32ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 4ใƒใ‚คใƒˆ +- ใƒŸใƒƒใ‚ฏใ‚นใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 6ใƒใ‚คใƒˆ๏ผˆใƒกใƒขใƒชๅ†…ใซfp32ใจfp16ใฎใƒขใƒ‡ใƒซใ‚’็ถญๆŒ๏ผ‰ + +**ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹:** + +- ้€šๅธธใฎAdamWใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 8ใƒใ‚คใƒˆ๏ผˆ2ใคใฎ็Šถๆ…‹ใ‚’็ถญๆŒ๏ผ‰ +- 8-bit AdamWใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 2ใƒใ‚คใƒˆ๏ผˆ[bitsandbytes](https://github.com/TimDettmers/bitsandbytes)ใฎใ‚ˆใ†ใชใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถ๏ผ‰ +- ใƒขใƒผใƒกใƒณใ‚ฟใƒ ใ‚’ๆŒใคSGDใฎใ‚ˆใ†ใชใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 4ใƒใ‚คใƒˆ๏ผˆ1ใคใฎ็Šถๆ…‹ใ‚’็ถญๆŒ๏ผ‰ + +**ๅ‹พ้…** + +- fp32ใพใŸใฏใƒŸใƒƒใ‚ฏใ‚นใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆ•ฐ * 4ใƒใ‚คใƒˆ๏ผˆๅ‹พ้…ใฏๅธธใซfp32ใงไฟๆŒ๏ผ‰ + +**ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ** + +- ใ‚ตใ‚คใ‚บใฏๅคšใใฎ่ฆๅ› ใซไพๅญ˜ใ—ใ€ไธป่ฆใช่ฆๅ› ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใ€้š ใ‚Œๅฑคใฎใ‚ตใ‚คใ‚บใ€ใŠใ‚ˆใณใƒใƒƒใƒใ‚ตใ‚คใ‚บใงใ™ใ€‚ + +ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใจใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใฎ้–ขๆ•ฐใซใ‚ˆใฃใฆๆธกใ•ใ‚Œใ€่ฟ”ใ•ใ‚Œใ‚‹ๅ…ฅๅŠ›ใจๅ‡บๅŠ›ใ€ใŠใ‚ˆใณๅ‹พ้…่จˆ็ฎ—ใฎใŸใ‚ใซไฟๅญ˜ใ•ใ‚Œใ‚‹ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใŒใ‚ใ‚Šใพใ™ใ€‚ + +**ไธ€ๆ™‚็š„ใชใƒกใƒขใƒช** + +ใ•ใ‚‰ใซใ€่จˆ็ฎ—ใŒๅฎŒไบ†ใ—ใŸๅพŒใซ่งฃๆ”พใ•ใ‚Œใ‚‹ใ•ใพใ–ใพใชไธ€ๆ™‚ๅค‰ๆ•ฐใŒใ‚ใ‚Šใพใ™ใŒใ€ใ“ใ‚Œใ‚‰ใฏไธ€ๆ™‚็š„ใซ่ฟฝๅŠ ใฎใƒกใƒขใƒชใ‚’ๅฟ…่ฆใจใ—ใ€OOMใซ้”ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๆ™‚ใซใฏใ“ใฎใ‚ˆใ†ใชไธ€ๆ™‚ๅค‰ๆ•ฐใซๆˆฆ็•ฅ็š„ใซ่€ƒใˆใ€ๅฟ…่ฆใชใใชใฃใŸใ‚‰ๆ˜Ž็คบ็š„ใซ่งฃๆ”พใ™ใ‚‹ใ“ใจใŒ้žๅธธใซ้‡่ฆใงใ™ใ€‚ + +**ๆฉŸ่ƒฝๅ›บๆœ‰ใฎใƒกใƒขใƒช** + +ๆฌกใซใ€ใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ขใซใฏ็‰นๅˆฅใชใƒกใƒขใƒช่ฆไปถใŒใ‚ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ใƒ“ใƒผใƒ ใ‚ตใƒผใƒใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ๅ ดๅˆใ€ใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ขใฏ่ค‡ๆ•ฐใฎๅ…ฅๅŠ›ใจๅ‡บๅŠ›ใฎใ‚ณใƒ”ใƒผใ‚’็ถญๆŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +**`forward`ใจ`backward`ใฎๅฎŸ่กŒ้€Ÿๅบฆ** + +็•ณใฟ่พผใฟๅฑคใจ็ทšๅฝขๅฑคใงใฏใ€ใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใซใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใจๆฏ”ในใฆ2ๅ€ใฎFLOPSใŒใ‚ใ‚Šใ€ไธ€่ˆฌ็š„ใซใฏ็ด„2ๅ€้…ใใชใ‚Šใพใ™๏ผˆใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใฎใ‚ตใ‚คใ‚บใŒไธไพฟใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใŸใ‚ใ€ใใ‚ŒไปฅไธŠใซใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ‰ใ€‚ ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใฏ้€šๅธธใ€ใƒใƒณใƒ‰ๅน…ๅˆถ้™ใ•ใ‚ŒใฆใŠใ‚Šใ€ใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใงใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใŒใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใ‚ˆใ‚Šใ‚‚ๅคšใใฎใƒ‡ใƒผใ‚ฟใ‚’่ชญใ‚€ใ“ใจใŒไธ€่ˆฌ็š„ใงใ™๏ผˆใŸใจใˆใฐใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใฏ1ๅ›ž่ชญใฟๅ–ใ‚Šใ€1ๅ›žๆ›ธใ่พผใฟใ€ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใฏใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใฎgradOutputใŠใ‚ˆใณๅ‡บๅŠ›ใ‚’2ๅ›ž่ชญใฟๅ–ใ‚Šใ€1ๅ›žๆ›ธใ่พผใฟใพใ™๏ผ‰ใ€‚ + +ใ”่ฆงใฎ้€šใ‚Šใ€GPUใƒกใƒขใƒชใ‚’็ฏ€็ด„ใ—ใŸใ‚Šๆ“ไฝœใ‚’้ซ˜้€ŸๅŒ–ใงใใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ใ„ใใคใ‹ใฎๅ ดๆ‰€ใŒใ‚ใ‚Šใพใ™ใ€‚ GPUใฎๅˆฉ็”จใจ่จˆ็ฎ—้€Ÿๅบฆใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹่ฆๅ› ใ‚’็†่งฃใ—ใŸใฎใงใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใฎๆŠ€่ก“ใซใคใ„ใฆใฏใ€[ๅ˜ไธ€GPUใงใฎๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎๆ–นๆณ•ใจใƒ„ใƒผใƒซ](perf_train_gpu_one)ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒšใƒผใ‚ธใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +่ฉณ็ดฐใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_sharing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_sharing.md new file mode 100644 index 0000000000000000000000000000000000000000..83df9d8f687e7e52fb3fb56ba576ac6e7e321882 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_sharing.md @@ -0,0 +1,262 @@ + + +# Share a Model + +ๆœ€ๅพŒใฎ2ใคใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€PyTorchใ€Kerasใ€ใŠใ‚ˆใณ๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใพใ—ใŸใ€‚ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใฏใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใงใ™๏ผHugging Faceใงใฏใ€็Ÿฅ่ญ˜ใจใƒชใ‚ฝใƒผใ‚นใ‚’ๅ…ฌ้–‹็š„ใซๅ…ฑๆœ‰ใ—ใ€ไบบๅทฅ็Ÿฅ่ƒฝใ‚’่ชฐใซใงใ‚‚ๆไพ›ใ™ใ‚‹ใ“ใจใ‚’ไฟกใ˜ใฆใ„ใพใ™ใ€‚ไป–ใฎไบบใ€…ใŒๆ™‚้–“ใจใƒชใ‚ฝใƒผใ‚นใ‚’็ฏ€็ด„ใงใใ‚‹ใ‚ˆใ†ใซใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ๆคœ่จŽใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€่จ“็ทดๆธˆใฟใพใŸใฏใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’[Model Hub](https://huggingface.co/models)ใซๅ…ฑๆœ‰ใ™ใ‚‹2ใคใฎๆ–นๆณ•ใ‚’ๅญฆใณใพใ™๏ผš + +- ใƒ—ใƒญใ‚ฐใƒฉใƒ ใงใƒ•ใ‚กใ‚คใƒซใ‚’Hubใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ€‚ +- ใ‚ฆใ‚งใƒ–ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚กใ‚คใƒซใ‚’Hubใซใƒ‰ใƒฉใƒƒใ‚ฐใ‚ขใƒณใƒ‰ใƒ‰ใƒญใƒƒใƒ—ใ™ใ‚‹ใ€‚ + + + + + +ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใซใฏใ€[huggingface.co](https://huggingface.co/join)ใงใ‚ขใ‚ซใ‚ฆใƒณใƒˆใŒๅฟ…่ฆใงใ™ใ€‚ๆ—ขๅญ˜ใฎ็ต„็น”ใซๅ‚ๅŠ ใ—ใŸใ‚Šใ€ๆ–ฐใ—ใ„็ต„็น”ใ‚’ไฝœๆˆใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + +## Repository Features + +Model HubไธŠใฎๅ„ใƒชใƒใ‚ธใƒˆใƒชใฏใ€้€šๅธธใฎGitHubใƒชใƒใ‚ธใƒˆใƒชใฎใ‚ˆใ†ใซๅ‹•ไฝœใ—ใพใ™ใ€‚ใƒชใƒใ‚ธใƒˆใƒชใฏใƒใƒผใ‚ธใƒงใƒ‹ใƒณใ‚ฐใ€ใ‚ณใƒŸใƒƒใƒˆๅฑฅๆญดใ€้•ใ„ใฎ่ฆ–่ฆšๅŒ–ใฎๆฉŸ่ƒฝใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +Model Hubใฎ็ต„ใฟ่พผใฟใƒใƒผใ‚ธใƒงใƒ‹ใƒณใ‚ฐใฏgitใŠใ‚ˆใณ[git-lfs](https://git-lfs.github.com/)ใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚่จ€ใ„ๆ›ใˆใ‚Œใฐใ€ใƒขใƒ‡ใƒซใ‚’1ใคใฎใƒชใƒใ‚ธใƒˆใƒชใจใ—ใฆๆ‰ฑใ†ใ“ใจใŒใงใใ€ใ‚ˆใ‚Šๅคงใใชใ‚ขใ‚ฏใ‚ปใ‚นๅˆถๅพกใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ใƒใƒผใ‚ธใƒงใƒณ็ฎก็†ใซใฏ*ใƒชใƒ“ใ‚ธใƒงใƒณ*ใŒใ‚ใ‚Šใ€ใ‚ณใƒŸใƒƒใƒˆใƒใƒƒใ‚ทใƒฅใ€ใ‚ฟใ‚ฐใ€ใพใŸใฏใƒ–ใƒฉใƒณใƒๅใง็‰นๅฎšใฎใƒขใƒ‡ใƒซใƒใƒผใ‚ธใƒงใƒณใ‚’ใƒ”ใƒณ็•™ใ‚ใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ + +ใใฎ็ตๆžœใ€`revision`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆ็‰นๅฎšใฎใƒขใƒ‡ใƒซใƒใƒผใ‚ธใƒงใƒณใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> model = AutoModel.from_pretrained( +... "julien-c/EsperBERTo-small", revision="4c77982" # ใ‚ฟใ‚ฐๅใ€ใพใŸใฏใƒ–ใƒฉใƒณใƒๅใ€ใพใŸใฏใ‚ณใƒŸใƒƒใƒˆใƒใƒƒใ‚ทใƒฅ +... ) +``` + +ใƒ•ใ‚กใ‚คใƒซใฏใƒชใƒใ‚ธใƒˆใƒชๅ†…ใง็ฐกๅ˜ใซ็ทจ้›†ใงใใ€ใ‚ณใƒŸใƒƒใƒˆๅฑฅๆญดใจๅทฎๅˆ†ใ‚’่กจ็คบใงใใพใ™๏ผš + +![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png) + +## Set Up + +ใƒขใƒ‡ใƒซใ‚’Hubใซๅ…ฑๆœ‰ใ™ใ‚‹ๅ‰ใซใ€Hugging Faceใฎ่ช่จผๆƒ…ๅ ฑใŒๅฟ…่ฆใงใ™ใ€‚ใ‚ฟใƒผใƒŸใƒŠใƒซใธใฎใ‚ขใ‚ฏใ‚ปใ‚นๆจฉใŒใ‚ใ‚‹ๅ ดๅˆใ€๐Ÿค— TransformersใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ไปฎๆƒณ็’ฐๅขƒใงไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ขใ‚ฏใ‚ปใ‚นใƒˆใƒผใ‚ฏใƒณใŒHugging Faceใฎใ‚ญใƒฃใƒƒใ‚ทใƒฅใƒ•ใ‚ฉใƒซใƒ€ใซไฟๅญ˜ใ•ใ‚Œใพใ™๏ผˆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ `~/.cache/` ใซไฟๅญ˜ใ•ใ‚Œใพใ™๏ผ‰๏ผš + +```bash +hf auth login +``` + +Jupyterใ‚„Colaboratoryใฎใ‚ˆใ†ใชใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€[`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library)ใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€Hubใจใƒ—ใƒญใ‚ฐใƒฉใƒ ็š„ใซๅฏพ่ฉฑใงใใพใ™ใ€‚ + +```bash +pip install huggingface_hub +``` + +ๆฌกใซใ€`notebook_login`ใ‚’ไฝฟ็”จใ—ใฆHubใซใ‚ตใ‚คใƒณใ‚คใƒณใ—ใ€[ใ“ใกใ‚‰ใฎใƒชใƒณใ‚ฏ](https://huggingface.co/settings/token)ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใƒญใ‚ฐใ‚คใƒณใซไฝฟ็”จใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใ‚’็”Ÿๆˆใ—ใพใ™๏ผš + +```python +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Convert a Model for all frameworks + +็•ฐใชใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงไฝœๆฅญใ—ใฆใ„ใ‚‹ไป–ใฎใƒฆใƒผใ‚ถใƒผใŒใ‚ใชใŸใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซใ€ +PyTorchใŠใ‚ˆใณTensorFlowใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใงใƒขใƒ‡ใƒซใ‚’ๅค‰ๆ›ใ—ใฆใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +ใ“ใฎใ‚นใƒ†ใƒƒใƒ—ใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ใจใ€ใƒฆใƒผใ‚ถใƒผใฏ็•ฐใชใ‚‹ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใŒใ€ +ใƒขใƒ‡ใƒซใ‚’ใ‚ชใƒณใ‚ถใƒ•ใƒฉใ‚คใงๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€้…ใใชใ‚Šใพใ™ใ€‚ + +ๅˆฅใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ็”จใซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅค‰ๆ›ใ™ใ‚‹ใ“ใจใฏ็ฐกๅ˜ใงใ™ใ€‚ +PyTorchใจTensorFlowใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผˆใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใซใคใ„ใฆใฏ[ใ“ใกใ‚‰](installation)ใ‚’ๅ‚็…ง๏ผ‰ใ—ใ€ +ใใฎๅพŒใ€ไป–ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏๅ‘ใ‘ใซ็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏ็”จใฎใƒขใƒ‡ใƒซใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + + + +TensorFlowใ‹ใ‚‰PyTorchใซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€`from_tf=True`ใ‚’ๆŒ‡ๅฎšใ—ใพใ™๏ผš + +```python +>>> pt_model = DistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_tf=True) +>>> pt_model.save_pretrained("path/to/awesome-name-you-picked") +``` + + + +ๆŒ‡ๅฎšใ—ใฆใ€PyTorchใ‹ใ‚‰TensorFlowใซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅค‰ๆ›ใ™ใ‚‹ใซใฏ `from_pt=True` ใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + +```python +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_pt=True) +``` + +ๆ–ฐใ—ใ„TensorFlowใƒขใƒ‡ใƒซใจใใฎๆ–ฐใ—ใ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใงใใพใ™๏ผš + +```python +>>> tf_model.save_pretrained("path/to/awesome-name-you-picked") +``` + + + +Flaxใงใƒขใƒ‡ใƒซใŒๅˆฉ็”จๅฏ่ƒฝใชๅ ดๅˆใ€PyTorchใ‹ใ‚‰Flaxใธใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎๅค‰ๆ›ใ‚‚่กŒใ†ใ“ใจใŒใงใใพใ™๏ผš + +```py +>>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( +... "path/to/awesome-name-you-picked", from_pt=True +... ) +``` + + + +## Push a model during traning + + + + + +ใƒขใƒ‡ใƒซใ‚’Hubใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใ“ใจใฏใ€่ฟฝๅŠ ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใพใŸใฏใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใง็ฐกๅ˜ใงใ™ใ€‚ +[ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](training)ใ‹ใ‚‰ๆ€ใ„ๅ‡บใ—ใฆใใ ใ•ใ„ใ€[`TrainingArguments`]ใ‚ฏใƒฉใ‚นใฏใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใจ่ฟฝๅŠ ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅ ดๆ‰€ใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ชใƒ—ใ‚ทใƒงใƒณใฎ1ใคใซใ€ใƒขใƒ‡ใƒซใ‚’็›ดๆŽฅHubใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆฉŸ่ƒฝใŒใ‚ใ‚Šใพใ™ใ€‚[`TrainingArguments`]ใง`push_to_hub=True`ใ‚’่จญๅฎšใ—ใพใ™๏ผš + +```py +>>> training_args = TrainingArguments(output_dir="my-awesome-model", push_to_hub=True) +``` + +Pass your training arguments as usual to [`Trainer`]: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +[`Trainer`]ใซ้€šๅธธ้€šใ‚Šใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ๆธกใ—ใพใ™๏ผš + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€[`Trainer`]ใง[`~transformers.Trainer.push_to_hub`]ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใ‚’Hubใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ใ€‚๐Ÿค— Transformersใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็ตๆžœใ€ใŠใ‚ˆใณใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎใƒใƒผใ‚ธใƒงใƒณใ‚’่‡ชๅ‹•็š„ใซใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใซ่ฟฝๅŠ ใ—ใพใ™๏ผ + +```py +>>> trainer.push_to_hub() +``` + + + + +[`PushToHubCallback`]ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’Hubใซๅ…ฑๆœ‰ใ—ใพใ™ใ€‚[`PushToHubCallback`]้–ขๆ•ฐใซใฏใ€ๆฌกใฎใ‚‚ใฎใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผš + +- ใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ€‚ +- ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€‚ +- `hub_model_id`ใ€ใคใพใ‚ŠHubใฎใƒฆใƒผใ‚ถใƒผๅใจใƒขใƒ‡ใƒซๅใ€‚ + +```python +>>> from transformers import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="./your_model_save_path", tokenizer=tokenizer, hub_model_id="your-username/my-awesome-model" +... ) +``` + +๐Ÿค— Transformersใฏ[`fit`](https://keras.io/api/models/model_training_apis/)ใซใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใ‚’Hubใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™๏ผš + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback) +``` + + + +## `push_to_hub` ้–ขๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ + +ใพใŸใ€ใƒขใƒ‡ใƒซใ‚’็›ดๆŽฅHubใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซใ€`push_to_hub` ใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +`push_to_hub` ใงใƒขใƒ‡ใƒซๅใ‚’ๆŒ‡ๅฎšใ—ใพใ™๏ผš + +```py +>>> pt_model.push_to_hub("my-awesome-model") +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒฆใƒผใ‚ถใƒผๅใฎไธ‹ใซใƒขใƒ‡ใƒซๅ `my-awesome-model` ใ‚’ๆŒใคใƒชใƒใ‚ธใƒˆใƒชใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ +ใƒฆใƒผใ‚ถใƒผใฏใ€`from_pretrained` ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใงใใพใ™๏ผš + +```py +>>> from transformers import AutoModel + +>>> model = AutoModel.from_pretrained("your_username/my-awesome-model") +``` + +็ต„็น”ใซๆ‰€ๅฑžใ—ใ€ใƒขใƒ‡ใƒซใ‚’็ต„็น”ๅใฎใ‚‚ใจใซใƒ—ใƒƒใ‚ทใƒฅใ—ใŸใ„ๅ ดๅˆใ€`repo_id` ใซใใ‚Œใ‚’่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„๏ผš + +```python +>>> pt_model.push_to_hub("my-awesome-org/my-awesome-model") +``` + +`push_to_hub`้–ขๆ•ฐใฏใ€ใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชใซไป–ใฎใƒ•ใ‚กใ‚คใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชใซ่ฟฝๅŠ ใ—ใพใ™๏ผš + +```py +>>> tokenizer.push_to_hub("my-awesome-model") +``` + +ใ‚ใ‚‹ใ„ใฏใ€ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸPyTorchใƒขใƒ‡ใƒซใฎTensorFlowใƒใƒผใ‚ธใƒงใƒณใ‚’่ฟฝๅŠ ใ—ใŸใ„ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผš + +```python +>>> tf_model.push_to_hub("my-awesome-model") +``` + +Hugging Faceใƒ—ใƒญใƒ•ใ‚ฃใƒผใƒซใซ็งปๅ‹•ใ™ใ‚‹ใจใ€ๆ–ฐใ—ใไฝœๆˆใ—ใŸใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชใŒ่กจ็คบใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚**Files**ใ‚ฟใƒ–ใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ™ใ‚‹ใจใ€ใƒชใƒใ‚ธใƒˆใƒชใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใ™ในใฆใฎใƒ•ใ‚กใ‚คใƒซใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +ใƒชใƒใ‚ธใƒˆใƒชใซใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใŠใ‚ˆใณใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€Hubใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ[ใ“ใกใ‚‰](https://huggingface.co/docs/hub/how-to-upstream)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Upload with the web interface + +ใ‚ณใƒผใƒ‰ใ‚’ๆ›ธใ‹ใšใซใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใ„ใƒฆใƒผใ‚ถใƒผใฏใ€Hubใฎใ‚ฆใ‚งใƒ–ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚[huggingface.co/new](https://huggingface.co/new)ใ‚’่จชใ‚Œใฆๆ–ฐใ—ใ„ใƒชใƒใ‚ธใƒˆใƒชใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png) + +ใ“ใ“ใ‹ใ‚‰ใ€ใƒขใƒ‡ใƒซใซ้–ขใ™ใ‚‹ใ„ใใคใ‹ใฎๆƒ…ๅ ฑใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผš + +- ใƒชใƒใ‚ธใƒˆใƒชใฎ**ๆ‰€ๆœ‰่€…**ใ‚’้ธๆŠžใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ‚ใชใŸ่‡ช่บซใพใŸใฏๆ‰€ๅฑžใ—ใฆใ„ใ‚‹็ต„็น”ใฎใ„ใšใ‚Œใ‹ใงใ™ใ€‚ +- ใƒขใƒ‡ใƒซใฎๅๅ‰ใ‚’้ธๆŠžใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใƒชใƒใ‚ธใƒˆใƒชใฎๅๅ‰ใซใ‚‚ใชใ‚Šใพใ™ใ€‚ +- ใƒขใƒ‡ใƒซใŒๅ…ฌ้–‹ใ‹้žๅ…ฌ้–‹ใ‹ใ‚’้ธๆŠžใ—ใพใ™ใ€‚ +- ใƒขใƒ‡ใƒซใฎใƒฉใ‚คใ‚ปใƒณใ‚นไฝฟ็”จๆ–นๆณ•ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +ใใฎๅพŒใ€**Files**ใ‚ฟใƒ–ใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใ€**Add file**ใƒœใ‚ฟใƒณใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใƒชใƒใ‚ธใƒˆใƒชใซๆ–ฐใ—ใ„ใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ๆฌกใซใ€ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ‰ใƒฉใƒƒใ‚ฐใ‚ขใƒณใƒ‰ใƒ‰ใƒญใƒƒใƒ—ใ—ใฆใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใ€ใ‚ณใƒŸใƒƒใƒˆใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png) + +## Add a model card + +ใƒฆใƒผใ‚ถใƒผใŒใƒขใƒ‡ใƒซใฎๆฉŸ่ƒฝใ€ๅˆถ้™ใ€ๆฝœๅœจ็š„ใชๅใ‚Šใ€ๅ€ซ็†็š„ใช่€ƒๆ…ฎไบ‹้ …ใ‚’็†่งฃใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชใซใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ—ใฆใใ ใ•ใ„ใ€‚ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใฏ`README.md`ใƒ•ใ‚กใ‚คใƒซใงๅฎš็พฉใ•ใ‚Œใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๆ–นๆณ•๏ผš + +* ๆ‰‹ๅ‹•ใง`README.md`ใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใŠใ‚ˆใณใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใ€‚ +* ใƒขใƒ‡ใƒซใƒชใƒใ‚ธใƒˆใƒชๅ†…ใฎ**Edit model card**ใƒœใ‚ฟใƒณใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ™ใ‚‹ใ€‚ + +ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใซๅซใ‚ใ‚‹ในใๆƒ…ๅ ฑใฎไพ‹ใซใคใ„ใฆใฏใ€DistilBert [ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰](https://huggingface.co/distilbert/distilbert-base-uncased)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚`README.md`ใƒ•ใ‚กใ‚คใƒซใงๅˆถๅพกใงใใ‚‹ไป–ใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ€ไพ‹ใˆใฐใƒขใƒ‡ใƒซใฎ็‚ญ็ด ใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใ‚„ใ‚ฆใ‚ฃใ‚ธใ‚งใƒƒใƒˆใฎไพ‹ใชใฉใซใคใ„ใฆใฎ่ฉณ็ดฐใฏใ€[ใ“ใกใ‚‰ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://huggingface.co/docs/hub/models-cards)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..b49bde5c20c495f02e95e34c49e501559194ffb2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/model_summary.md @@ -0,0 +1,110 @@ + + +# The Transformer model family + +2017ๅนดใซๅฐŽๅ…ฅใ•ใ‚Œใฆไปฅๆฅใ€[ๅ…ƒใฎTransformer](https://huggingface.co/papers/1706.03762)ใƒขใƒ‡ใƒซใฏใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†๏ผˆNLP๏ผ‰ใฎใ‚ฟใ‚นใ‚ฏใ‚’่ถ…ใˆใ‚‹ๅคšใใฎๆ–ฐใ—ใ„ใ‚จใ‚ญใ‚ตใ‚คใƒ†ใ‚ฃใƒณใ‚ฐใชใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใƒ‘ใ‚คใ‚ขใ—ใพใ—ใŸใ€‚[ใ‚ฟใƒณใƒ‘ใ‚ฏ่ณชใฎๆŠ˜ใ‚ŠใŸใŸใพใ‚ŒใŸๆง‹้€ ใ‚’ไบˆๆธฌ](https://huggingface.co/blog/deep-learning-with-proteins)ใ™ใ‚‹ใƒขใƒ‡ใƒซใ€[ใƒใƒผใ‚ฟใƒผใ‚’่ตฐใ‚‰ใ›ใ‚‹ใŸใ‚ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://huggingface.co/blog/train-decision-transformers)ใ™ใ‚‹ใƒขใƒ‡ใƒซใ€ใใ—ใฆ[ๆ™‚็ณปๅˆ—ไบˆๆธฌ](https://huggingface.co/blog/time-series-transformers)ใฎใŸใ‚ใฎใƒขใƒ‡ใƒซใชใฉใŒใ‚ใ‚Šใพใ™ใ€‚Transformerใฎใ•ใพใ–ใพใชใƒใƒชใ‚ขใƒณใƒˆใŒๅˆฉ็”จๅฏ่ƒฝใงใ™ใŒใ€ๅคงๅฑ€ใ‚’่ฆ‹่ฝใจใ™ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ™ในใฆใฎใƒขใƒ‡ใƒซใซๅ…ฑ้€šใ™ใ‚‹ใฎใฏใ€ๅ…ƒใฎTransformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅŸบใฅใ„ใฆใ„ใ‚‹ใ“ใจใงใ™ใ€‚ไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ‚จใƒณใ‚ณใƒผใƒ€ใพใŸใฏใƒ‡ใ‚ณใƒผใƒ€ใฎใฟใ‚’ไฝฟ็”จใ—ใ€ไป–ใฎใƒขใƒ‡ใƒซใฏไธกๆ–นใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€Transformerใƒ•ใ‚กใƒŸใƒชใƒผๅ†…ใฎใƒขใƒ‡ใƒซใฎ้ซ˜ใƒฌใƒ™ใƒซใฎ้•ใ„ใ‚’ใ‚ซใƒ†ใ‚ดใƒฉใ‚คใ‚บใ—ใ€่ชฟๆŸปใ™ใ‚‹ใŸใ‚ใฎๆœ‰็”จใชๅˆ†้กžๆณ•ใ‚’ๆไพ›ใ—ใ€ไปฅๅ‰ใซๅ‡บไผšใฃใŸใ“ใจใฎใชใ„Transformerใ‚’็†่งฃใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ๅ…ƒใฎTransformerใƒขใƒ‡ใƒซใซๆ…ฃใ‚Œใฆใ„ใชใ„ใ‹ใ€ใƒชใƒ•ใƒฌใƒƒใ‚ทใƒฅใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€Hugging Faceใ‚ณใƒผใ‚นใฎ[Transformerใฎๅ‹•ไฝœๅŽŸ็†](https://huggingface.co/course/chapter1/4?fw=pt)็ซ ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ + +
+ +
+ +## Computer vision + + + +### Convolutional network + +้•ทใ„้–“ใ€็•ณใฟ่พผใฟใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆCNN๏ผ‰ใฏใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใ‚ฟใ‚นใ‚ฏใซใŠใ„ใฆๆ”ฏ้…็š„ใชใƒ‘ใƒฉใƒ€ใ‚คใƒ ใงใ—ใŸใŒใ€[ใƒ“ใ‚ธใƒงใƒณTransformer](https://huggingface.co/papers/2010.11929)ใฏใใฎใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใจๅŠน็އๆ€งใ‚’็คบใ—ใพใ—ใŸใ€‚ใใ‚Œใงใ‚‚ใ€ไธ€้ƒจใฎCNNใฎๆœ€้ซ˜ใฎ็‰นๆ€งใ€็‰นใซ็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซใจใฃใฆใฏ้žๅธธใซๅผทๅŠ›ใช็ฟป่จณไธๅค‰ๆ€งใชใฉใ€ไธ€้ƒจใฎTransformerใฏใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซ็•ณใฟ่พผใฟใ‚’็ต„ใฟ่พผใ‚“ใงใ„ใพใ™ใ€‚[ConvNeXt](model_doc/convnext)ใฏใ€็•ณใฟ่พผใฟใ‚’็พไปฃๅŒ–ใ™ใ‚‹ใŸใ‚ใซTransformerใ‹ใ‚‰่จญ่จˆใฎ้ธๆŠž่‚ขใ‚’ๅ–ใ‚Šๅ…ฅใ‚Œใ€ไพ‹ใˆใฐใ€ConvNeXtใฏ็”ปๅƒใ‚’ใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใ™ใ‚‹ใŸใ‚ใซ้‡ใชใ‚Šๅˆใ‚ใชใ„ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใจใ€ใ‚ฐใƒญใƒผใƒใƒซๅ—ๅฎน้‡Žใ‚’ๅข—ๅŠ ใ•ใ›ใ‚‹ใŸใ‚ใฎๅคงใใชใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ConvNeXtใฏใ€ใƒกใƒขใƒชๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซใ„ใใคใ‹ใฎใƒฌใ‚คใƒคใƒผใƒ‡ใ‚ถใ‚คใƒณใฎ้ธๆŠž่‚ขใ‚‚ๆไพ›ใ—ใ€Transformerใจ็ซถๅˆ็š„ใซใชใ‚Šใพใ™๏ผ + + +### Encoder[[cv-encoder]] + +[ใƒ“ใ‚ธใƒงใƒณ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ๏ผˆViT๏ผ‰](model_doc/vit) ใฏใ€็•ณใฟ่พผใฟใ‚’ไฝฟ็”จใ—ใชใ„ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใฎๆ‰‰ใ‚’้–‹ใ‘ใพใ—ใŸใ€‚ViT ใฏๆจ™ๆบ–ใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€็”ปๅƒใ‚’ๆ‰ฑใ†ๆ–นๆณ•ใŒไธป่ฆใชใƒ–ใƒฌใƒผใ‚ฏใ‚นใƒซใƒผใงใ—ใŸใ€‚็”ปๅƒใ‚’ๅ›บๅฎšใ‚ตใ‚คใ‚บใฎใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒˆใƒผใ‚ฏใƒณใฎใ‚ˆใ†ใซไฝฟ็”จใ—ใฆๅŸ‹ใ‚่พผใฟใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ViT ใฏใ€ๅฝ“ๆ™‚ใฎCNNใจ็ซถไบ‰ๅŠ›ใฎใ‚ใ‚‹็ตๆžœใ‚’็คบใ™ใŸใ‚ใซใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎๅŠน็އ็š„ใชใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ๆดป็”จใ—ใพใ—ใŸใŒใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅฟ…่ฆใชใƒชใ‚ฝใƒผใ‚นใŒๅฐ‘ใชใใฆๆธˆใฟใพใ—ใŸใ€‚ViT ใซ็ถšใ„ใฆใ€ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚„ๆคœๅ‡บใชใฉใฎๅฏ†ใชใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใ‚’ๅ‡ฆ็†ใงใใ‚‹ไป–ใฎใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใ‚‚็™ปๅ ดใ—ใพใ—ใŸใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฎ1ใคใŒ[Swin](model_doc/swin) ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ™ใ€‚Swin ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏใ€ใ‚ˆใ‚Šๅฐใ•ใชใ‚ตใ‚คใ‚บใฎใƒ‘ใƒƒใƒใ‹ใ‚‰้šŽๅฑค็š„ใช็‰นๅพดใƒžใƒƒใƒ—๏ผˆCNNใฎใ‚ˆใ†ใง ViT ใจใฏ็•ฐใชใ‚Šใพใ™๏ผ‰ใ‚’ๆง‹็ฏ‰ใ—ใ€ๆทฑๅฑคใฎใƒ‘ใƒƒใƒใจ้šฃๆŽฅใ™ใ‚‹ใƒ‘ใƒƒใƒใจใƒžใƒผใ‚ธใ—ใพใ™ใ€‚ๆณจๆ„ใฏใƒญใƒผใ‚ซใƒซใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๅ†…ใงใฎใฟ่จˆ็ฎ—ใ•ใ‚Œใ€ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฏๆณจๆ„ใฎใƒฌใ‚คใƒคใƒผ้–“ใงใ‚ทใƒ•ใƒˆใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใŒใ‚ˆใ‚Š่‰ฏใๅญฆ็ฟ’ใ™ใ‚‹ใฎใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๆŽฅ็ถšใ‚’ไฝœๆˆใ—ใพใ™ใ€‚Swin ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏ้šŽๅฑค็š„ใช็‰นๅพดใƒžใƒƒใƒ—ใ‚’็”Ÿๆˆใงใใ‚‹ใŸใ‚ใ€ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚„ๆคœๅ‡บใชใฉใฎๅฏ†ใชไบˆๆธฌใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใฆใ„ใพใ™ใ€‚[SegFormer](model_doc/segformer) ใ‚‚้šŽๅฑค็š„ใช็‰นๅพดใƒžใƒƒใƒ—ใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใซใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ใ™ในใฆใฎ็‰นๅพดใƒžใƒƒใƒ—ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซใ‚ทใƒณใƒ—ใƒซใชใƒžใƒซใƒใƒฌใ‚คใƒคใƒผใƒ‘ใƒผใ‚ปใƒ—ใƒˆใƒญใƒณ๏ผˆMLP๏ผ‰ใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +BeIT ใŠใ‚ˆใณ ViTMAE ใชใฉใฎไป–ใฎใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใฏใ€BERTใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็›ฎๆจ™ใ‹ใ‚‰ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใพใ—ใŸใ€‚[BeIT](model_doc/beit) ใฏ *masked image modeling (MIM)* ใซใ‚ˆใฃใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚็”ปๅƒใƒ‘ใƒƒใƒใฏใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ•ใ‚Œใ€็”ปๅƒใ‚‚่ฆ–่ฆšใƒˆใƒผใ‚ฏใƒณใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใพใ™ใ€‚BeIT ใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒ‘ใƒƒใƒใซๅฏพๅฟœใ™ใ‚‹่ฆ–่ฆšใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚[ViTMAE](model_doc/vitmae) ใ‚‚ไผผใŸใ‚ˆใ†ใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็›ฎๆจ™ใ‚’ๆŒใฃใฆใŠใ‚Šใ€่ฆ–่ฆšใƒˆใƒผใ‚ฏใƒณใฎไปฃใ‚ใ‚Šใซใƒ”ใ‚ฏใ‚ปใƒซใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚็•ฐไพ‹ใชใฎใฏ็”ปๅƒใƒ‘ใƒƒใƒใฎ75%ใŒใƒžใ‚นใ‚ฏใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใงใ™๏ผใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒ‘ใƒƒใƒใ‹ใ‚‰ใƒ”ใ‚ฏใ‚ปใƒซใ‚’ๅ†ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅพŒใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏๆจใฆใ‚‰ใ‚Œใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฏใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใฎใ‚ฟใ‚นใ‚ฏใงไฝฟ็”จใงใใ‚‹็Šถๆ…‹ใงใ™ใ€‚ + +### Decoder[[cv-decoder]] + +ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎใฟใฎใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใฏ็ใ—ใ„ใงใ™ใ€‚ใชใœใชใ‚‰ใ€ใปใจใ‚“ใฉใฎใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใฏ็”ปๅƒ่กจ็พใ‚’ๅญฆใถใŸใ‚ใซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ใ—ใ‹ใ—ใ€็”ปๅƒ็”Ÿๆˆใชใฉใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใงใฏใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏ่‡ช็„ถใช้ฉๅฟœใงใ™ใ€‚ใ“ใ‚Œใฏใ€GPT-2ใชใฉใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใ‹ใ‚‰่ฆ‹ใฆใใŸใ‚ˆใ†ใซใ€[ImageGPT](model_doc/imagegpt) ใงใ‚‚ๅŒๆง˜ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€็”ปๅƒๅ†…ใฎๆฌกใฎใƒ”ใ‚ฏใ‚ปใƒซใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚็”ปๅƒ็”ŸๆˆใซๅŠ ใˆใฆใ€ImageGPT ใฏ็”ปๅƒๅˆ†้กžใฎใŸใ‚ใซใ‚‚ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใงใใพใ™ใ€‚ + +### Encoder-decoder[[cv-encoder-decoder]] + +ใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใฏไธ€่ˆฌ็š„ใซใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ๏ผˆใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ‚‚ๅ‘ผใฐใ‚Œใพใ™๏ผ‰ใ‚’ไฝฟ็”จใ—ใฆ้‡่ฆใช็”ปๅƒ็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚’ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใซๆธกใ™ใŸใ‚ใซไฝฟ็”จใ—ใพใ™ใ€‚[DETR](model_doc/detr) ใฏไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใƒใƒƒใ‚ฏใƒœใƒผใƒณใ‚’ๆŒใฃใฆใ„ใพใ™ใŒใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฎใŸใ‚ใซๅฎŒๅ…จใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚‚ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฏ็”ปๅƒ่กจ็พใ‚’ๅญฆใณใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒช๏ผˆๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใฏ็”ปๅƒๅ†…ใฎ้ ˜ๅŸŸใพใŸใฏใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใŸๅญฆ็ฟ’ใ•ใ‚ŒใŸๅŸ‹ใ‚่พผใฟใงใ™๏ผ‰ใจ็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚DETR ใฏๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใซๅฏพใ™ใ‚‹ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎๅบงๆจ™ใจใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ + +## Natural lanaguage processing + + + +### Encoder[[nlp-encoder]] + +[BERT](model_doc/bert) ใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จใฎTransformerใงใ€ๅ…ฅๅŠ›ใฎไธ€้ƒจใฎใƒˆใƒผใ‚ฏใƒณใ‚’ใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ—ใฆไป–ใฎใƒˆใƒผใ‚ฏใƒณใ‚’่ฆ‹ใชใ„ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ—ใŸๆ–‡่„ˆใซๅŸบใฅใ„ใฆใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใŒไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็›ฎๆจ™ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€BERTใฏๅ…ฅๅŠ›ใฎใ‚ˆใ‚Šๆทฑใ„ใ‹ใค่ฑŠใ‹ใช่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใฎใซๅทฆๅณใฎๆ–‡่„ˆใ‚’ๅฎŒๅ…จใซๆดป็”จใงใใพใ™ใ€‚ใ—ใ‹ใ—ใ€BERTใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆˆฆ็•ฅใซใฏใพใ ๆ”นๅ–„ใฎไฝ™ๅœฐใŒใ‚ใ‚Šใพใ—ใŸใ€‚[RoBERTa](model_doc/roberta) ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้•ทๆ™‚้–“่กŒใ„ใ€ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ไบ‹ๅ‰ๅ‡ฆ็†ไธญใซไธ€ๅบฆใ ใ‘ใงใชใๅ„ใ‚จใƒใƒƒใ‚ฏใงใƒˆใƒผใ‚ฏใƒณใ‚’ใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ—ใ€ๆฌกๆ–‡ไบˆๆธฌใฎ็›ฎๆจ™ใ‚’ๅ‰Š้™คใ™ใ‚‹ๆ–ฐใ—ใ„ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒฌใ‚ทใƒ”ใ‚’ๅฐŽๅ…ฅใ™ใ‚‹ใ“ใจใงใ“ใ‚Œใ‚’ๆ”นๅ–„ใ—ใพใ—ใŸใ€‚ + +ๆ€ง่ƒฝใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ไธป่ฆใชๆˆฆ็•ฅใฏใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใ‚’ๅข—ใ‚„ใ™ใ“ใจใงใ™ใŒใ€ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏ่จˆ็ฎ—ใ‚ณใ‚นใƒˆใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚่จˆ็ฎ—ใ‚ณใ‚นใƒˆใ‚’ๅ‰Šๆธ›ใ™ใ‚‹ๆ–นๆณ•ใฎ1ใคใฏใ€[DistilBERT](model_doc/distilbert) ใฎใ‚ˆใ†ใชๅฐใ•ใชใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚DistilBERTใฏ[็Ÿฅ่ญ˜่’ธ็•™](https://huggingface.co/papers/1503.02531) - ๅœง็ธฎๆŠ€่ก“ - ใ‚’ไฝฟ็”จใ—ใฆใ€BERTใฎใปใผใ™ในใฆใฎ่จ€่ชž็†่งฃๆฉŸ่ƒฝใ‚’ไฟๆŒใ—ใชใŒใ‚‰ใ€ใ‚ˆใ‚Šๅฐใ•ใชใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +ใ—ใ‹ใ—ใ€ใปใจใ‚“ใฉใฎTransformerใƒขใƒ‡ใƒซใฏๅผ•ใ็ถšใใ‚ˆใ‚Šๅคšใใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใŒ็™ปๅ ดใ—ใฆใ„ใพใ™ใ€‚[ALBERT](model_doc/albert) ใฏใ€2ใคใฎๆ–นๆณ•ใงใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใ‚’ๆธ›ใ‚‰ใ™ใ“ใจใซใ‚ˆใฃใฆใƒกใƒขใƒชๆถˆ่ฒป้‡ใ‚’ๅ‰Šๆธ›ใ—ใพใ™ใ€‚ๅคงใใช่ชžๅฝ™ๅŸ‹ใ‚่พผใฟใ‚’2ใคใฎๅฐใ•ใช่กŒๅˆ—ใซๅˆ†ๅ‰ฒใ—ใ€ใƒฌใ‚คใƒคใƒผใŒใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚[DeBERTa](model_doc/deberta) ใฏใ€ๅ˜่ชžใจใใฎไฝ็ฝฎใ‚’2ใคใฎใƒ™ใ‚ฏใƒˆใƒซใงๅˆฅใ€…ใซใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹่งฃใ‹ใ‚ŒใŸๆณจๆ„ๆฉŸๆง‹ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ๆณจๆ„ใฏใ“ใ‚Œใ‚‰ใฎๅˆฅใ€…ใฎใƒ™ใ‚ฏใƒˆใƒซใ‹ใ‚‰่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ๅ˜่ชžใจไฝ็ฝฎใฎๅŸ‹ใ‚่พผใฟใŒๅซใพใ‚Œใ‚‹ๅ˜ไธ€ใฎใƒ™ใ‚ฏใƒˆใƒซใงใฏใชใใ€[Longformer](model_doc/longformer) ใฏใ€็‰นใซ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซๆณจๆ„ใ‚’ใ‚ˆใ‚ŠๅŠน็އ็š„ใซใ™ใ‚‹ใ“ใจใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ—ใŸใ€‚ๅ›บๅฎšใ•ใ‚ŒใŸใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใ‚ตใ‚คใ‚บใฎๅ‘จใ‚Šใฎๅ„ใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰่จˆ็ฎ—ใ•ใ‚Œใ‚‹ใƒญใƒผใ‚ซใƒซใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆไป˜ใๆณจๆ„๏ผˆ็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใƒˆใƒผใ‚ฏใƒณ๏ผˆๅˆ†้กžใฎใŸใ‚ใฎ `[CLS]` ใชใฉ๏ผ‰ใฎใฟใฎใŸใ‚ใฎใ‚ฐใƒญใƒผใƒใƒซใชๆณจๆ„ใ‚’ๅซใ‚€๏ผ‰ใฎ็ต„ใฟๅˆใ‚ใ›ใ‚’ไฝฟ็”จใ—ใฆใ€ๅฎŒๅ…จใชๆณจๆ„่กŒๅˆ—ใงใฏใชใ็–Žใชๆณจๆ„่กŒๅˆ—ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + + +### Decoder[[nlp-decoder]] + +[GPT-2](model_doc/gpt2)ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จใฎTransformerใงใ™ใ€‚ใƒขใƒ‡ใƒซใฏๅ…ˆใ‚’่ฆ‹ใ‚‹ใ“ใจใŒใงใใชใ„ใ‚ˆใ†ใซใƒˆใƒผใ‚ฏใƒณใ‚’ๅณใซใƒžใ‚นใ‚ฏใ—ใ€"ใฎใžใ่ฆ‹"ใ‚’้˜ฒใŽใพใ™ใ€‚ๅคง้‡ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใŸใ“ใจใซใ‚ˆใ‚Šใ€GPT-2ใฏใƒ†ใ‚ญใ‚นใƒˆ็”ŸๆˆใŒ้žๅธธใซๅพ—ๆ„ใงใ€ใƒ†ใ‚ญใ‚นใƒˆใŒๆญฃ็ขบใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใซใ—ใฆใ‚‚ใ€ๆ™‚ๆŠ˜ๆญฃ็ขบใงใฏใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€GPT-2ใซใฏBERTใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‹ใ‚‰ใฎๅŒๆ–นๅ‘ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒไธ่ถณใ—ใฆใŠใ‚Šใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซใฏ้ฉใ—ใฆใ„ใพใ›ใ‚“ใงใ—ใŸใ€‚[XLNET](model_doc/xlnet)ใฏใ€ๅŒๆ–นๅ‘ใซๅญฆ็ฟ’ใงใใ‚‹้ †ๅˆ—่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็›ฎๆจ™๏ผˆPLM๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€BERTใจGPT-2ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็›ฎๆจ™ใฎใƒ™ใ‚นใƒˆใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ„ใพใ™ใ€‚ + +GPT-2ใฎๅพŒใ€่จ€่ชžใƒขใƒ‡ใƒซใฏใ•ใ‚‰ใซๅคงใใๆˆ้•ทใ—ใ€ไปŠใงใฏ*ๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซ๏ผˆLLM๏ผ‰*ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚Œใฐใ€LLMใฏใปใผใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆๅญฆ็ฟ’ใ‚’็คบใ™ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚[GPT-J](model_doc/gptj)ใฏใ€6Bใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคLLMใงใ€400Bใฎใƒˆใƒผใ‚ฏใƒณใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚GPT-Jใซใฏ[OPT](model_doc/opt)ใŒ็ถšใใ€ใใฎใ†ใกๆœ€ๅคงใฎใƒขใƒ‡ใƒซใฏ175Bใงใ€180Bใฎใƒˆใƒผใ‚ฏใƒณใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ๅŒใ˜ๆ™‚ๆœŸใซ[BLOOM](model_doc/bloom)ใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใ€ใ“ใฎใƒ•ใ‚กใƒŸใƒชใƒผใฎๆœ€ๅคงใฎใƒขใƒ‡ใƒซใฏ176Bใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใกใ€46ใฎ่จ€่ชžใจ13ใฎใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใง366Bใฎใƒˆใƒผใ‚ฏใƒณใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +### Encoder-decoder[[nlp-encoder-decoder]] + +[BART](model_doc/bart)ใฏใ€ๅ…ƒใฎTransformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฟๆŒใ—ใฆใ„ใพใ™ใŒใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็›ฎๆจ™ใ‚’*ใƒ†ใ‚ญใ‚นใƒˆ่ฃœๅฎŒ*ใฎ็ ดๆใซๅค‰ๆ›ดใ—ใฆใ„ใพใ™ใ€‚ไธ€้ƒจใฎใƒ†ใ‚ญใ‚นใƒˆใ‚นใƒ‘ใƒณใฏๅ˜ไธ€ใฎ`mask`ใƒˆใƒผใ‚ฏใƒณใง็ฝฎๆ›ใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏ็ ดๆใ—ใฆใ„ใชใ„ใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—๏ผˆๆœชๆฅใฎใƒˆใƒผใ‚ฏใƒณใฏใƒžใ‚นใ‚ฏใ•ใ‚Œใพใ™๏ผ‰ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ไฝฟ็”จใ—ใฆไบˆๆธฌใ‚’่ฃœๅŠฉใ—ใพใ™ใ€‚[Pegasus](model_doc/pegasus)ใฏBARTใซไผผใฆใ„ใพใ™ใŒใ€Pegasusใฏใƒ†ใ‚ญใ‚นใƒˆใ‚นใƒ‘ใƒณใฎไปฃใ‚ใ‚Šใซๆ–‡ๅ…จไฝ“ใ‚’ใƒžใ‚นใ‚ฏใ—ใพใ™ใ€‚ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใซๅŠ ใˆใฆใ€Pegasusใฏใ‚ฎใƒฃใƒƒใƒ—ๆ–‡็”Ÿๆˆ๏ผˆGSG๏ผ‰ใซใ‚ˆใฃใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚GSGใฎ็›ฎๆจ™ใฏใ€ๆ–‡ๆ›ธใซ้‡่ฆใชๆ–‡ใ‚’ใƒžใ‚นใ‚ฏใ—ใ€ใใ‚Œใ‚‰ใ‚’`mask`ใƒˆใƒผใ‚ฏใƒณใง็ฝฎๆ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏๆฎ‹ใ‚Šใฎๆ–‡ใ‹ใ‚‰ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ—ใชใ‘ใ‚Œใฐใชใ‚Šใพใ›ใ‚“ใ€‚[T5](model_doc/t5)ใฏใ€ใ™ในใฆใฎNLPใ‚ฟใ‚นใ‚ฏใ‚’็‰นๅฎšใฎใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆๅฏพใƒ†ใ‚ญใ‚นใƒˆใฎๅ•้กŒใซๅค‰ๆ›ใ™ใ‚‹ใ‚ˆใ‚Šใƒฆใƒ‹ใƒผใ‚ฏใชใƒขใƒ‡ใƒซใงใ™ใ€‚ใŸใจใˆใฐใ€ใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚น`Summarize:`ใฏ่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใ‚’็คบใ—ใพใ™ใ€‚T5ใฏๆ•™ๅธซใ‚ใ‚Šใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผˆGLUEใจSuperGLUE๏ผ‰ใจ่‡ชๅทฑๆ•™ๅธซใ‚ใ‚Šใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผˆใƒˆใƒผใ‚ฏใƒณใฎ15๏ผ…ใ‚’ใƒฉใƒณใƒ€ใƒ ใซใ‚ตใƒณใƒ—ใƒซใ—ใƒ‰ใƒญใƒƒใƒ—ใ‚ขใ‚ฆใƒˆ๏ผ‰ใซใ‚ˆใฃใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + + +## Audio + + + +### Encoder[[audio-encoder]] + +[Wav2Vec2](model_doc/wav2vec2) ใฏใ€็”Ÿใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใ‹ใ‚‰็›ดๆŽฅ้Ÿณๅฃฐ่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใฎTransformerใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅฏพ็…ง็š„ใชใ‚ฟใ‚นใ‚ฏใงไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚Œใ€ไธ€้€ฃใฎๅฝใฎ่กจ็พใ‹ใ‚‰็œŸใฎ้Ÿณๅฃฐ่กจ็พใ‚’็‰นๅฎšใ—ใพใ™ใ€‚ [HuBERT](model_doc/hubert) ใฏWav2Vec2ใซไผผใฆใ„ใพใ™ใŒใ€็•ฐใชใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใƒฉใƒ™ใƒซใฏใ€้กžไผผใ—ใŸใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ปใ‚ฐใƒกใƒณใƒˆใŒใ‚ฏใƒฉใ‚นใ‚ฟใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‰ใ‚Œใ€ใ“ใ‚ŒใŒ้š ใ‚Œใƒฆใƒ‹ใƒƒใƒˆใซใชใ‚‹ใ‚ฏใƒฉใ‚นใ‚ฟใƒชใƒณใ‚ฐใ‚นใƒ†ใƒƒใƒ—ใซใ‚ˆใฃใฆไฝœๆˆใ•ใ‚Œใพใ™ใ€‚้š ใ‚Œใƒฆใƒ‹ใƒƒใƒˆใฏๅŸ‹ใ‚่พผใฟใซใƒžใƒƒใƒ—ใ•ใ‚Œใ€ไบˆๆธฌใ‚’่กŒใ„ใพใ™ใ€‚ + +### Encoder-decoder[[audio-encoder-decoder]] + +[Speech2Text](model_doc/speech_to_text) ใฏใ€่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผˆASR๏ผ‰ใŠใ‚ˆใณ้Ÿณๅฃฐ็ฟป่จณใฎใŸใ‚ใซ่จญ่จˆใ•ใ‚ŒใŸ้Ÿณๅฃฐใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฏใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใ‹ใ‚‰ๆŠฝๅ‡บใ•ใ‚ŒใŸใƒญใ‚ฐใƒกใƒซใƒ•ใ‚ฃใƒซใ‚ฟใƒผใƒใƒณใ‚ฏใƒ•ใ‚ฃใƒผใƒใƒฃใƒผใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ่‡ชๅทฑๅ›žๅธฐ็š„ใซใƒˆใƒฉใƒณใ‚นใ‚ฏใƒชใƒ—ใƒˆใพใŸใฏ็ฟป่จณใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ [Whisper](model_doc/whisper) ใ‚‚ASRใƒขใƒ‡ใƒซใงใ™ใŒใ€ไป–ใฎๅคšใใฎ้Ÿณๅฃฐใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใ€โœจ ใƒฉใƒ™ใƒซไป˜ใ โœจ ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒˆใƒฉใƒณใ‚นใ‚ฏใƒชใƒ—ใ‚ทใƒงใƒณใƒ‡ใƒผใ‚ฟใ‚’ๅคง้‡ใซไบ‹ๅ‰ใซๅญฆ็ฟ’ใ—ใฆใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅคง้ƒจๅˆ†ใซใฏ้ž่‹ฑ่ชžใฎ่จ€่ชžใ‚‚ๅซใพใ‚ŒใฆใŠใ‚Šใ€WhisperใฏไฝŽใƒชใ‚ฝใƒผใ‚น่จ€่ชžใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ๆง‹้€ ็š„ใซใฏใ€WhisperใฏSpeech2Textใซไผผใฆใ„ใพใ™ใ€‚ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชไฟกๅทใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซใ‚ˆใฃใฆใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒญใ‚ฐใƒกใƒซใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใจๅ‰ใฎใƒˆใƒผใ‚ฏใƒณใ‹ใ‚‰ใƒˆใƒฉใƒณใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’่‡ชๅทฑๅ›žๅธฐ็š„ใซ็”Ÿๆˆใ—ใพใ™ใ€‚ + +## Multimodal + + + +### Encoder[[mm-encoder]] + +[VisualBERT](model_doc/visual_bert) ใฏใ€BERTใฎๅพŒใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒ“ใ‚ธใƒงใƒณ่จ€่ชžใ‚ฟใ‚นใ‚ฏๅ‘ใ‘ใฎใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใ‚ŒใฏBERTใจไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ็‰ฉไฝ“ๆคœๅ‡บใ‚ทใ‚นใƒ†ใƒ ใ‚’็ต„ใฟๅˆใ‚ใ›ใ€็”ปๅƒ็‰นๅพดใ‚’ใƒ“ใ‚ธใƒฅใ‚ขใƒซๅŸ‹ใ‚่พผใฟใซๆŠฝๅ‡บใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆๅŸ‹ใ‚่พผใฟใจไธ€็ท’ใซBERTใซๆธกใ—ใพใ™ใ€‚VisualBERTใฏ้žใƒžใ‚นใ‚ฏใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅŸบใซใ—ใŸใƒžใ‚นใ‚ฏใƒ†ใ‚ญใ‚นใƒˆใ‚’ไบˆๆธฌใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใŒ็”ปๅƒใจๆ•ดๅˆใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚‚ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ViTใŒใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸ้š›ใ€[ViLT](model_doc/vilt) ใฏ็”ปๅƒๅŸ‹ใ‚่พผใฟใ‚’ๅ–ๅพ—ใ™ใ‚‹ใŸใ‚ใซใ“ใฎๆ–นๆณ•ใ‚’ๆŽก็”จใ—ใพใ—ใŸใ€‚็”ปๅƒๅŸ‹ใ‚่พผใฟใฏใƒ†ใ‚ญใ‚นใƒˆๅŸ‹ใ‚่พผใฟใจๅ…ฑใซๅ…ฑๅŒใงๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ใใ‚Œใ‹ใ‚‰ใ€ViLTใฏ็”ปๅƒใƒ†ใ‚ญใ‚นใƒˆใƒžใƒƒใƒใƒณใ‚ฐใ€ใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€ใŠใ‚ˆใณๅ…จๅ˜่ชžใƒžใ‚นใ‚ญใƒณใ‚ฐใซใ‚ˆใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +[CLIP](model_doc/clip) ใฏ็•ฐใชใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅ–ใ‚Šใ€(`็”ปๅƒ`ใ€`ใƒ†ใ‚ญใ‚นใƒˆ`) ใฎใƒšใ‚ขไบˆๆธฌใ‚’่กŒใ„ใพใ™ใ€‚็”ปๅƒใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ๏ผˆViT๏ผ‰ใจใƒ†ใ‚ญใ‚นใƒˆใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ๏ผˆTransformer๏ผ‰ใฏใ€(`็”ปๅƒ`ใ€`ใƒ†ใ‚ญใ‚นใƒˆ`) ใƒšใ‚ขใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆไธŠใงๅ…ฑๅŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ€(`็”ปๅƒ`ใ€`ใƒ†ใ‚ญใ‚นใƒˆ`) ใƒšใ‚ขใฎ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎๅŸ‹ใ‚่พผใฟใฎ้กžไผผๆ€งใ‚’ๆœ€ๅคงๅŒ–ใ—ใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใ€CLIPใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใ‚’ไบˆๆธฌใ—ใŸใ‚Šใ€ใใฎ้€†ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚[OWL-ViT](model_doc/owlvit) ใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็‰ฉไฝ“ๆคœๅ‡บใฎใƒใƒƒใ‚ฏใƒœใƒผใƒณใจใ—ใฆCLIPใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใ€็‰ฉไฝ“ๆคœๅ‡บใƒ˜ใƒƒใƒ‰ใŒ่ฟฝๅŠ ใ•ใ‚Œใ€(`ใ‚ฏใƒฉใ‚น`ใ€`ใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚น`) ใƒšใ‚ขใซๅฏพใ™ใ‚‹ใ‚ปใƒƒใƒˆไบˆๆธฌใŒ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +### Encoder-decoder[[mm-encoder-decoder]] + +ๅ…‰ๅญฆๆ–‡ๅญ—่ช่ญ˜๏ผˆOCR๏ผ‰ใฏใ€้€šๅธธใ€็”ปๅƒใ‚’็†่งฃใ—ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซ่ค‡ๆ•ฐใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒ้–ขไธŽใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆ่ช่ญ˜ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ [TrOCR](model_doc/trocr) ใฏใ€ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎTransformerใ‚’ไฝฟ็”จใ—ใฆใ“ใฎใƒ—ใƒญใ‚ปใ‚นใ‚’็ฐก็•ฅๅŒ–ใ—ใพใ™ใ€‚ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฏ็”ปๅƒใ‚’ๅ›บๅฎšใ‚ตใ‚คใ‚บใฎใƒ‘ใƒƒใƒใจใ—ใฆๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎViTใ‚นใ‚ฟใ‚คใƒซใฎใƒขใƒ‡ใƒซใงใ‚ใ‚Šใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’่‡ชๅทฑๅ›žๅธฐ็š„ใซ็”Ÿๆˆใ—ใพใ™ใ€‚[Donut](model_doc/donut) ใฏOCRใƒ™ใƒผใ‚นใฎใ‚ขใƒ—ใƒญใƒผใƒใซไพๅญ˜ใ—ใชใ„ใ‚ˆใ‚Šไธ€่ˆฌ็š„ใชใƒ“ใ‚ธใƒฅใ‚ขใƒซใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็†่งฃใƒขใƒ‡ใƒซใงใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใ—ใฆSwin Transformerใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใจใ—ใฆๅคš่จ€่ชžBARTใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ Donutใฏ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎๆณจ้‡ˆใซๅŸบใฅใ„ใฆๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’่ชญใ‚€ใŸใ‚ใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไธŽใˆใ‚‰ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฏๅ„ใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใ‚ฟใ‚นใ‚ฏใ”ใจใซ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆ่กจ็พใ•ใ‚Œใพใ™ใ€‚ไพ‹ใˆใฐใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎ่งฃๆžใซใฏ`่งฃๆž`ใƒˆใƒผใ‚ฏใƒณใŒใ‚ใ‚Šใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใจ็ต„ใฟๅˆใ‚ใ•ใ‚Œใฆใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๆง‹้€ ๅŒ–ใ•ใ‚ŒใŸๅ‡บๅŠ›ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆ๏ผˆJSON๏ผ‰ใซ่งฃๆžใ—ใพใ™ใ€‚ + +## Reinforcement learning + + + +### Decoder[[rl-decoder]] + +ๆ„ๆ€ๆฑบๅฎšใจ่ปŒ่ทกใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฏใ€็Šถๆ…‹ใ€ใ‚ขใ‚ฏใ‚ทใƒงใƒณใ€ๅ ฑ้…ฌใ‚’ใ‚ทใƒผใ‚ฑใƒณใ‚นใƒขใƒ‡ใƒชใƒณใ‚ฐใฎๅ•้กŒใจใ—ใฆๆ‰ใˆใพใ™ใ€‚ [Decision Transformer](model_doc/decision_transformer) ใฏใ€ใƒชใ‚ฟใƒผใƒณใƒปใƒˆใ‚ฅใƒปใ‚ดใƒผใ€้ŽๅŽปใฎ็Šถๆ…‹ใ€ใŠใ‚ˆใณใ‚ขใ‚ฏใ‚ทใƒงใƒณใซๅŸบใฅใ„ใฆๅฐ†ๆฅใฎๅธŒๆœ›ใƒชใ‚ฟใƒผใƒณใซใคใชใŒใ‚‹ใ‚ขใ‚ฏใ‚ทใƒงใƒณใฎ็ณปๅˆ—ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ๆœ€ๅพŒใฎ *K* ใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€3ใคใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใใ‚Œใžใ‚ŒใŒใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใซๅค‰ๆ›ใ•ใ‚Œใ€ๅฐ†ๆฅใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซGPTใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚[Trajectory Transformer](model_doc/trajectory_transformer) ใ‚‚็Šถๆ…‹ใ€ใ‚ขใ‚ฏใ‚ทใƒงใƒณใ€ๅ ฑ้…ฌใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€GPTใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงๅ‡ฆ็†ใ—ใพใ™ใ€‚ๅ ฑ้…ฌ่ชฟๆ•ดใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใŸDecision Transformerใจใฏ็•ฐใชใ‚Šใ€Trajectory Transformerใฏใƒ“ใƒผใƒ ใ‚ตใƒผใƒใ‚’ไฝฟ็”จใ—ใฆๅฐ†ๆฅใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/multilingual.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/multilingual.md new file mode 100644 index 0000000000000000000000000000000000000000..39524195f88810e018e29398ad78f65f518522f2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/multilingual.md @@ -0,0 +1,178 @@ + + +# ๆŽจ่ซ–ใฎใŸใ‚ใฎๅคš่จ€่ชžใƒขใƒ‡ใƒซ + +[[open-in-colab]] + +๐Ÿค— Transformers ใซใฏใ„ใใคใ‹ใฎๅคš่จ€่ชžใƒขใƒ‡ใƒซใŒใ‚ใ‚Šใ€ใใ‚Œใ‚‰ใฎๆŽจ่ซ–ใฎไฝฟ็”จๆ–นๆณ•ใฏๅ˜ไธ€่จ€่ชžใƒขใƒ‡ใƒซใจใฏ็•ฐใชใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ๅคš่จ€่ชžใƒขใƒ‡ใƒซใฎไฝฟ็”จๆ–นๆณ•ใŒใ™ในใฆ็•ฐใชใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ [google-bert/bert-base-multilingual-uncased](https://huggingface.co/google-bert/bert-base-multilingual-uncased) ใชใฉใฎไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ€ๅ˜ไธ€่จ€่ชžใƒขใƒ‡ใƒซใจๅŒๆง˜ใซไฝฟ็”จใงใใพใ™ใ€‚ ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆŽจ่ซ–ใฎใŸใ‚ใซไฝฟ็”จๆ–นๆณ•ใŒ็•ฐใชใ‚‹ๅคš่จ€่ชžใƒขใƒ‡ใƒซใ‚’ใฉใฎใ‚ˆใ†ใซไฝฟใ†ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +## XLM + +XLM ใซใฏ10ใฎ็•ฐใชใ‚‹ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใ‚ใ‚Šใ€ใใฎใ†ใกใฎ1ใคใ ใ‘ใŒๅ˜ไธ€่จ€่ชžใงใ™ใ€‚ ๆฎ‹ใ‚Šใฎ9ใคใฎใƒขใƒ‡ใƒซใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€่จ€่ชžๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ™ใ‚‹ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจไฝฟ็”จใ—ใชใ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ2ใคใฎใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +### ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใŒใ‚ใ‚‹ XLM + +ๆฌกใฎ XLM ใƒขใƒ‡ใƒซใฏใ€่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ—ใฆใ€ๆŽจ่ซ–ใงไฝฟ็”จใ•ใ‚Œใ‚‹่จ€่ชžใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +- `FacebookAI/xlm-mlm-ende-1024` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒ‰ใ‚คใƒ„่ชž) +- `FacebookAI/xlm-mlm-enfr-1024` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒ•ใƒฉใƒณใ‚น่ชž) +- `FacebookAI/xlm-mlm-enro-1024` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒซใƒผใƒžใƒ‹ใ‚ข่ชž) +- `FacebookAI/xlm-mlm-xnli15-1024` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€XNLI ่จ€่ชž) +- `FacebookAI/xlm-mlm-tlm-xnli15-1024` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + ็ฟป่จณ + XNLI ่จ€่ชž) +- `FacebookAI/xlm-clm-enfr-1024` (ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒ•ใƒฉใƒณใ‚น่ชž) +- `FacebookAI/xlm-clm-ende-1024` (ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒ‰ใ‚คใƒ„่ชž) + +่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใฏใ€ใƒขใƒ‡ใƒซใซๆธกใ•ใ‚Œใ‚‹ `input_ids` ใจๅŒใ˜ๅฝข็Šถใฎใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่กจใ•ใ‚Œใพใ™ใ€‚ ใ“ใ‚Œใ‚‰ใฎใƒ†ใƒณใ‚ฝใƒซใฎๅ€คใฏใ€ไฝฟ็”จใ•ใ‚Œใ‚‹่จ€่ชžใซไพๅญ˜ใ—ใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ `lang2id` ใŠใ‚ˆใณ `id2lang` ๅฑžๆ€งใซใ‚ˆใฃใฆ่ญ˜ๅˆฅใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใฎไพ‹ใงใฏใ€`FacebookAI/xlm-clm-enfr-1024` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ (ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€่‹ฑ่ชž-ใƒ•ใƒฉใƒณใ‚น่ชž)ใ€‚ + +```py +>>> import torch +>>> from transformers import XLMTokenizer, XLMWithLMHeadModel + +>>> tokenizer = XLMTokenizer.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +>>> model = XLMWithLMHeadModel.from_pretrained("FacebookAI/xlm-clm-enfr-1024") +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ `lang2id` ๅฑžๆ€งใฏใ€ใ“ใฎใƒขใƒ‡ใƒซใฎ่จ€่ชžใจใใฎ ID ใ‚’่กจ็คบใ—ใพใ™ใ€‚ + +```py +>>> print(tokenizer.lang2id) +{'en': 0, 'fr': 1} +``` + +ๆฌกใซใ€ๅ…ฅๅŠ›ไพ‹ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) # batch size of 1 +``` + +่จ€่ชž ID ใ‚’ `en` ใซ่จญๅฎšใ—ใ€ใใ‚Œใ‚’ไฝฟ็”จใ—ใฆ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใฏใ€่‹ฑ่ชžใฎ่จ€่ชž ID ใงใ‚ใ‚‹ใŸใ‚ใ€`0` ใงๅŸ‹ใ‚ใ‚‰ใ‚ŒใŸใƒ†ใƒณใ‚ฝใƒซใงใ™ใ€‚ ใ“ใฎใƒ†ใƒณใ‚ฝใƒซใฏ `input_ids` ใจๅŒใ˜ใ‚ตใ‚คใ‚บใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> language_id = tokenizer.lang2id["en"] # 0 +>>> langs = torch.tensor([language_id] * input_ids.shape[1]) # torch.tensor([0, 0, 0, ..., 0]) + +>>> # We reshape it to be of size (batch_size, sequence_length) +>>> langs = langs.view(1, -1) # is now of shape [1, sequence_length] (we have a batch size of 1) +``` + +ใ“ใ‚Œใงใ€`input_ids` ใจ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +```py +>>> outputs = model(input_ids, langs=langs) +``` + +[run_generation.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-generation/run_generation.py) ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€`xlm-clm` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€่จ€่ชžใŒๅŸ‹ใ‚่พผใพใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ + +### ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใŒใชใ„XLM + +ๆฌกใฎ XLM ใƒขใƒ‡ใƒซใฏใ€ๆŽจ่ซ–ไธญใซ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ๅฟ…่ฆใจใ—ใพใ›ใ‚“ใ€‚ + +- `FacebookAI/xlm-mlm-17-1280` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€17ใฎ่จ€่ชž) +- `FacebookAI/xlm-mlm-100-1280` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€100ใฎ่จ€่ชž) + +ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€ไปฅๅ‰ใฎ XLM ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจใฏ็•ฐใชใ‚Šใ€ไธ€่ˆฌ็š„ใชๆ–‡ใฎ่กจ็พใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +## BERT + +ไปฅไธ‹ใฎ BERT ใƒขใƒ‡ใƒซใฏใ€ๅคš่จ€่ชžใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใพใ™ใ€‚ + +- `google-bert/bert-base-multilingual-uncased` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + ๆฌกใฎๆ–‡ใฎไบˆๆธฌใ€102ใฎ่จ€่ชž) +- `google-bert/bert-base-multilingual-cased` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + ๆฌกใฎๆ–‡ใฎไบˆๆธฌใ€104ใฎ่จ€่ชž) + +ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€ๆŽจ่ซ–ไธญใซ่จ€่ชžใฎๅŸ‹ใ‚่พผใฟใ‚’ๅฟ…่ฆใจใ—ใพใ›ใ‚“ใ€‚ ๆ–‡่„ˆใ‹ใ‚‰่จ€่ชžใ‚’่ญ˜ๅˆฅใ—ใ€ใใ‚Œใซๅฟœใ˜ใฆๆŽจๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## XLM-RoBERTa + +ๆฌกใฎ XLM-RoBERTa ใƒขใƒ‡ใƒซใฏใ€ๅคš่จ€่ชžใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใพใ™ใ€‚ + +- `FacebookAI/xlm-roberta-base` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€100ใฎ่จ€่ชž) +- `FacebookAI/xlm-roberta-large` (ใƒžใ‚นใ‚ฏๅŒ–ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ€100ใฎ่จ€่ชž) + +XLM-RoBERTa ใฏใ€100ใฎ่จ€่ชžใงๆ–ฐใ—ใไฝœๆˆใŠใ‚ˆใณใ‚ฏใƒชใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ2.5 TB ใฎ CommonCrawl ใƒ‡ใƒผใ‚ฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ—ใŸใ€‚ ใ“ใ‚Œใฏใ€ๅˆ†้กžใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒฉใƒ™ใƒซไป˜ใ‘ใ€่ณชๅ•ๅฟœ็ญ”ใชใฉใฎใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใ‚ฟใ‚นใ‚ฏใงใ€mBERT ใ‚„ XLM ใชใฉใฎไปฅๅ‰ใซใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸๅคš่จ€่ชžใƒขใƒ‡ใƒซใ‚’ๅคงๅน…ใซๆ”นๅ–„ใ—ใพใ™ใ€‚ + +## M2M100 + +ๆฌกใฎ M2M100 ใƒขใƒ‡ใƒซใฏใ€ๅคš่จ€่ชž็ฟป่จณใซไฝฟ็”จใงใใพใ™ใ€‚ + +- `facebook/m2m100_418M` (็ฟป่จณ) +- `facebook/m2m100_1.2B` (็ฟป่จณ) + +ใ“ใฎไพ‹ใงใฏใ€`facebook/m2m100_418M` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ไธญๅ›ฝ่ชžใ‹ใ‚‰่‹ฑ่ชžใซ็ฟป่จณใ—ใพใ™ใ€‚ ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ‚ฝใƒผใ‚น่จ€่ชžใ‚’่จญๅฎšใงใใพใ™ใ€‚ + +```py +>>> from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> chinese_text = "ไธ่ฆๆ’ๆ‰‹ๅทซๅธซ็š„ไบ‹ๅ‹™, ๅ› ็‚บไป–ๅ€‘ๆ˜ฏๅพฎๅฆ™็š„, ๅพˆๅฟซๅฐฑๆœƒ็™ผๆ€’." + +>>> tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="zh") +>>> model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M") +``` + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> encoded_zh = tokenizer(chinese_text, return_tensors="pt") +``` + +M2M100 ใฏใ€ๆœ€ๅˆใซ็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจใ—ใฆใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชž ID ใ‚’ๅผทๅˆถ็š„ใซใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชžใซ็ฟป่จณใ—ใพใ™ใ€‚ ่‹ฑ่ชžใซ็ฟป่จณใ™ใ‚‹ใซใฏใ€`generate` ใƒกใ‚ฝใƒƒใƒ‰ใง `forced_bos_token_id` ใ‚’ `en` ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +'Do not interfere with the matters of the witches, because they are delicate and will soon be angry.' +``` + +## MBart + +ๅคš่จ€่ชž็ฟป่จณใซใฏใ€ๆฌกใฎ MBart ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +- `facebook/mbart-large-50-one-to-many-mmt` (One-to-many multilingual machine translation, 50 languages) +- `facebook/mbart-large-50-many-to-many-mmt` (Many-to-many multilingual machine translation, 50 languages) +- `facebook/mbart-large-50-many-to-one-mmt` (Many-to-one multilingual machine translation, 50 languages) +- `facebook/mbart-large-50` (Multilingual translation, 50 languages) +- `facebook/mbart-large-cc25` + +ใ“ใฎไพ‹ใงใฏใ€`facebook/mbart-large-50-many-to-many-mmt` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ใƒ•ใ‚ฃใƒณใƒฉใƒณใƒ‰่ชžใ‚’่‹ฑ่ชžใซ็ฟป่จณใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ‚ฝใƒผใ‚น่จ€่ชžใ‚’่จญๅฎšใงใใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + +>>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." +>>> fi_text = "ร„lรค sekaannu velhojen asioihin, sillรค ne ovat hienovaraisia ja nopeasti vihaisia." + +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="fi_FI") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/mbart-large-50-many-to-many-mmt") +``` + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> encoded_en = tokenizer(en_text, return_tensors="pt") +``` + +MBart ใฏใ€ๆœ€ๅˆใซ็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจใ—ใฆใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชž ID ใ‚’ๅผทๅˆถ็š„ใซใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชžใซ็ฟป่จณใ—ใพใ™ใ€‚ ่‹ฑ่ชžใซ็ฟป่จณใ™ใ‚‹ใซใฏใ€`generate` ใƒกใ‚ฝใƒƒใƒ‰ใง `forced_bos_token_id` ใ‚’ `en` ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id("en_XX")) +>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) +"Don't interfere with the wizard's affairs, because they are subtle, will soon get angry." +``` + +`facebook/mbart-large-50-many-to-one-mmt` ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ๆœ€ๅˆใซ็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจใ—ใฆใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชž ID ใ‚’ๅผทๅˆถใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใ€ไฝฟ็”จๆ–นๆณ•ใฏๅŒใ˜ใงใ™ใ€‚ \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pad_truncation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pad_truncation.md new file mode 100644 index 0000000000000000000000000000000000000000..4da23fd82e0f49d3cba205d49bbbc76d8ea28e32 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pad_truncation.md @@ -0,0 +1,63 @@ + + +# Padding and truncation + +ใƒใƒƒใƒๅ…ฅๅŠ›ใฏใ—ใฐใ—ใฐ็•ฐใชใ‚‹้•ทใ•ใงใ‚ใ‚Šใ€ๅ›บๅฎšใ‚ตใ‚คใ‚บใฎใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใงใใชใ„ใŸใ‚ใ€ๅค‰ๅ‹•ใ™ใ‚‹้•ทใ•ใฎใƒใƒƒใƒใ‹ใ‚‰้•ทๆ–นๅฝขใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใฎๆˆฆ็•ฅใจใ—ใฆใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏใ€็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใ‚ทใƒผใ‚ฑใƒณใ‚นใพใŸใฏใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ทใจๅŒใ˜้•ทใ•ใซใชใ‚‹ใ‚ˆใ†ใซใ€็‰นๅˆฅใช**ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณ**ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ๅˆ‡ใ‚Š่ฉฐใ‚ใฏใ€้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใ“ใจใง้€†ๆ–นๅ‘ใซๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +ใปใจใ‚“ใฉใฎๅ ดๅˆใ€ใƒใƒƒใƒใ‚’ๆœ€้•ทใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ—ใ€ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ทใซๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใ“ใจใงใ€ใ†ใพใๅ‹•ไฝœใ—ใพใ™ใ€‚ใŸใ ใ—ใ€APIใฏใใ‚ŒไปฅไธŠใฎๆˆฆ็•ฅใ‚‚ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ๅฟ…่ฆใช3ใคใฎๅผ•ๆ•ฐใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš`padding`ใ€`truncation`ใ€ใŠใ‚ˆใณ `max_length`ใ€‚ + +`padding`ๅผ•ๆ•ฐใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ใƒ–ใƒผใƒซๅ€คใพใŸใฏๆ–‡ๅญ—ๅˆ—ใงใ‚ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + + - `True`ใพใŸใฏ`'longest'`๏ผšใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผˆใ‚ทใƒผใ‚ฑใƒณใ‚นใŒ1ใคใ—ใ‹ๆไพ›ใ•ใ‚Œใชใ„ๅ ดๅˆใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“๏ผ‰ใ€‚ + - `max_length'`๏ผš`max_length`ๅผ•ๆ•ฐใงๆŒ‡ๅฎšใ•ใ‚ŒใŸ้•ทใ•ใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใพใŸใฏ`max_length`ใŒๆไพ›ใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ท๏ผˆ`max_length=None`๏ผ‰ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒ1ใคใ—ใ‹ๆไพ›ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + - `False`ใพใŸใฏ`'do_not_pad'`๏ผšใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚ŒใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ‹•ไฝœใงใ™ใ€‚ + +`truncation`ๅผ•ๆ•ฐใฏๅˆ‡ใ‚Š่ฉฐใ‚ใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ใƒ–ใƒผใƒซๅ€คใพใŸใฏๆ–‡ๅญ—ๅˆ—ใงใ‚ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + + - `True`ใพใŸใฏ`'longest_first'`๏ผšๆœ€ๅคง้•ทใ‚’`max_length`ๅผ•ๆ•ฐใงๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ท๏ผˆ`max_length=None`๏ผ‰ใพใงๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ใ“ใ‚Œใฏใƒˆใƒผใ‚ฏใƒณใ”ใจใซๅˆ‡ใ‚Š่ฉฐใ‚ใ€้ฉๅˆ‡ใช้•ทใ•ใซ้”ใ™ใ‚‹ใพใงใƒšใ‚ขๅ†…ใฎๆœ€้•ทใ‚ทใƒผใ‚ฑใƒณใ‚นใ‹ใ‚‰ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + - `'only_second'`๏ผšๆœ€ๅคง้•ทใ‚’`max_length`ๅผ•ๆ•ฐใงๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ท๏ผˆ`max_length=None`๏ผ‰ใพใงๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ใ“ใ‚Œใฏใƒšใ‚ขใฎ2็•ช็›ฎใฎๆ–‡ใ ใ‘ใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™๏ผˆใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒšใ‚ขใพใŸใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒใƒƒใƒใฎใƒšใ‚ขใŒๆไพ›ใ•ใ‚ŒใŸๅ ดๅˆ๏ผ‰ใ€‚ + - `'only_first'`๏ผšๆœ€ๅคง้•ทใ‚’`max_length`ๅผ•ๆ•ฐใงๆŒ‡ๅฎšใ™ใ‚‹ใ‹ใ€ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคง้•ท๏ผˆ`max_length=None`๏ผ‰ใพใงๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ใ“ใ‚Œใฏใƒšใ‚ขใฎๆœ€ๅˆใฎๆ–‡ใ ใ‘ใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™๏ผˆใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒšใ‚ขใพใŸใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒใƒƒใƒใฎใƒšใ‚ขใŒๆไพ›ใ•ใ‚ŒใŸๅ ดๅˆ๏ผ‰ใ€‚ + - `False`ใพใŸใฏ`'do_not_truncate'`๏ผšๅˆ‡ใ‚Š่ฉฐใ‚ใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚ŒใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ‹•ไฝœใงใ™ใ€‚ + +`max_length`ๅผ•ๆ•ฐใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ใฎ้•ทใ•ใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ๆ•ดๆ•ฐใพใŸใฏ`None`ใงใ‚ใ‚Šใ€ใ“ใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคงๅ…ฅๅŠ›้•ทใซใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ใƒขใƒ‡ใƒซใซ็‰นๅฎšใฎๆœ€ๅคงๅ…ฅๅŠ›้•ทใŒใชใ„ๅ ดๅˆใ€`max_length`ใธใฎๅˆ‡ใ‚Š่ฉฐใ‚ใพใŸใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏ็„กๅŠนใซใชใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฎ่กจใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ใ‚’่จญๅฎšใ™ใ‚‹ๆŽจๅฅจๆ–นๆณ•ใ‚’่ฆ็ด„ใ—ใฆใ„ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใฎใ„ใšใ‚Œใ‹ใงๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎใƒšใ‚ขใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`truncation=True`ใ‚’`['only_first', 'only_second', 'longest_first']`ใง้ธๆŠžใ—ใŸ`STRATEGY`ใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใคใพใ‚Šใ€`truncation='only_second'`ใพใŸใฏ`truncation='longest_first'`ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒšใ‚ขๅ†…ใฎไธกๆ–นใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‰่ฟฐใฎใ‚ˆใ†ใซๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ๆ–นๆณ•ใ‚’ๅˆถๅพกใงใใพใ™ใ€‚ + + + +| Truncation | Padding | Instruction | +|--------------------------------------|-----------------------------------|---------------------------------------------------------------------------------------------| +| no truncation | no padding | `tokenizer(batch_sentences)` | +| | padding to max sequence in batch | `tokenizer(batch_sentences, padding=True)` or | +| | | `tokenizer(batch_sentences, padding='longest')` | +| | padding to max model input length | `tokenizer(batch_sentences, padding='max_length')` | +| | padding to specific length | `tokenizer(batch_sentences, padding='max_length', max_length=42)` | +| | padding to a multiple of a value | `tokenizer(batch_sentences, padding=True, pad_to_multiple_of=8)` | +| truncation to max model input length | no padding | `tokenizer(batch_sentences, truncation=True)` or | +| | | `tokenizer(batch_sentences, truncation=STRATEGY)` | +| | padding to max sequence in batch | `tokenizer(batch_sentences, padding=True, truncation=True)` or | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY)` | +| | padding to max model input length | `tokenizer(batch_sentences, padding='max_length', truncation=True)` or | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY)` | +| | padding to specific length | Not possible | +| truncation to specific length | no padding | `tokenizer(batch_sentences, truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, truncation=STRATEGY, max_length=42)` | +| | padding to max sequence in batch | `tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42)` | +| | padding to max model input length | Not possible | +| | padding to specific length | `tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` or | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42)` | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/peft.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/peft.md new file mode 100644 index 0000000000000000000000000000000000000000..77dd7be86c72b895ce74bd660fd05538323a3672 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/peft.md @@ -0,0 +1,214 @@ + + + +# Load adapters with ๐Ÿค— PEFT + +[[open-in-colab]] + +[Parameter-Efficient Fine Tuning (PEFT)](https://huggingface.co/blog/peft) ใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐไธญใซๅ‡็ตใ—ใ€ใใฎไธŠใซใ‚ใšใ‹ใช่จ“็ทดๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟ๏ผˆใ‚ขใƒ€ใƒ—ใ‚ฟใƒผ๏ผ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใงใ™ใ€‚ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฏใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎๆƒ…ๅ ฑใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใซ่จ“็ทดใ•ใ‚Œใพใ™ใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ใƒกใƒขใƒชไฝฟ็”จ้‡ใŒๅฐ‘ใชใใ€ๅฎŒๅ…จใซใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใจๆฏ”่ผƒใ—ใฆ่จˆ็ฎ—ใƒชใ‚ฝใƒผใ‚นใ‚’ไฝŽใๆŠ‘ใˆใคใคใ€ๅŒ็ญ‰ใฎ็ตๆžœใ‚’็”Ÿๆˆใ™ใ‚‹ใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +PEFTใง่จ“็ทดใ•ใ‚ŒใŸใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฏ้€šๅธธใ€ๅฎŒๅ…จใชใƒขใƒ‡ใƒซใฎใ‚ตใ‚คใ‚บใ‚ˆใ‚Šใ‚‚1ๆกๅฐใ•ใใ€ๅ…ฑๆœ‰ใ€ไฟๅญ˜ใ€่ชญใฟ่พผใ‚€ใฎใŒไพฟๅˆฉใงใ™ใ€‚ + +
+ +
Hubใซๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹OPTForCausalLMใƒขใƒ‡ใƒซใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผ้‡ใฟใฏใ€ใƒขใƒ‡ใƒซใฎๅ…จไฝ“ใ‚ตใ‚คใ‚บใฎ็ด„6MBใงใ€ใƒขใƒ‡ใƒซ้‡ใฟใฎๅ…จใ‚ตใ‚คใ‚บใฏ็ด„700MBใงใ™ใ€‚
+
+ +๐Ÿค— PEFTใƒฉใ‚คใƒ–ใƒฉใƒชใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://huggingface.co/docs/peft/index)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## Setup + +๐Ÿค— PEFTใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆๅง‹ใ‚ใพใ—ใ‚‡ใ†๏ผš + + +```bash +pip install peft +``` + +ๆ–ฐๆฉŸ่ƒฝใ‚’่ฉฆใ—ใฆใฟใŸใ„ๅ ดๅˆใ€ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ“ใจใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผš + +```bash +pip install git+https://github.com/huggingface/peft.git +``` + +## Supported PEFT models + +๐Ÿค— Transformersใฏใ€ใ„ใใคใ‹ใฎPEFT๏ผˆParameter Efficient Fine-Tuning๏ผ‰ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใƒใ‚คใƒ†ใ‚ฃใƒ–ใซใ‚ตใƒใƒผใƒˆใ—ใฆใŠใ‚Šใ€ใƒญใƒผใ‚ซใƒซใพใŸใฏHubใซๆ ผ็ดใ•ใ‚ŒใŸใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚ฆใ‚งใ‚คใƒˆใ‚’็ฐกๅ˜ใซ่ชญใฟ่พผใ‚“ใงๅฎŸ่กŒใพใŸใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใพใ™ใ€‚ไปฅไธ‹ใฎใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™๏ผš + +- [Low Rank Adapters](https://huggingface.co/docs/peft/conceptual_guides/lora) +- [IA3](https://huggingface.co/docs/peft/conceptual_guides/ia3) +- [AdaLoRA](https://huggingface.co/papers/2303.10512) + +ไป–ใฎPEFTใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใ€ใƒ—ใƒญใƒณใƒ—ใƒˆๅญฆ็ฟ’ใ‚„ใƒ—ใƒญใƒณใƒ—ใƒˆ่ชฟๆ•ดใชใฉใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใ€ใพใŸใฏ๐Ÿค— PEFTใƒฉใ‚คใƒ–ใƒฉใƒชๅ…จ่ˆฌใซใคใ„ใฆใฏใ€[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://huggingface.co/docs/peft/index)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +## Load a PEFT adapter + +๐Ÿค— Transformersใ‹ใ‚‰PEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚“ใงไฝฟ็”จใ™ใ‚‹ใซใฏใ€Hubใƒชใƒใ‚ธใƒˆใƒชใพใŸใฏใƒญใƒผใ‚ซใƒซใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซ `adapter_config.json` ใƒ•ใ‚กใ‚คใƒซใจใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚ฆใ‚งใ‚คใƒˆใŒๅซใพใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ๆฌกใซใ€`AutoModelFor` ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆPEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ใ“ใจใŒใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็”จใฎPEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ใซใฏ๏ผš + +1. PEFTใƒขใƒ‡ใƒซใฎIDใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ +2. ใใ‚Œใ‚’[`AutoModelForCausalLM`] ใ‚ฏใƒฉใ‚นใซๆธกใ—ใพใ™ใ€‚ + + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +peft_model_id = "ybelkada/opt-350m-lora" +model = AutoModelForCausalLM.from_pretrained(peft_model_id) +``` + + + +PEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’`AutoModelFor`ใ‚ฏใƒฉใ‚นใพใŸใฏๅŸบๆœฌใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚น๏ผˆ`OPTForCausalLM`ใพใŸใฏ`LlamaForCausalLM`ใชใฉ๏ผ‰ใง่ชญใฟ่พผใ‚€ใ“ใจใŒใงใใพใ™ใ€‚ + + + +ใพใŸใ€`load_adapter`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใงใ€PEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’่ชญใฟ่พผใ‚€ใ“ใจใ‚‚ใงใใพใ™๏ผš + + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "facebook/opt-350m" +peft_model_id = "ybelkada/opt-350m-lora" + +model = AutoModelForCausalLM.from_pretrained(model_id) +model.load_adapter(peft_model_id) +``` + +## Load in 8bit or 4bit + + +`bitsandbytes` ็ตฑๅˆใฏใ€8ใƒ“ใƒƒใƒˆใŠใ‚ˆใณ4ใƒ“ใƒƒใƒˆใฎ็ฒพๅบฆใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใŠใ‚Šใ€ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€้š›ใซใƒกใƒขใƒชใ‚’็ฏ€็ด„ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™๏ผˆ่ฉณ็ดฐใซใคใ„ใฆใฏ `bitsandbytes` ็ตฑๅˆใฎ[ใ‚ฌใ‚คใƒ‰](./quantization#bitsandbytes-integration)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚[`~PreTrainedModel.from_pretrained`] ใซ `load_in_8bit` ใพใŸใฏ `load_in_4bit` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ฟฝๅŠ ใ—ใ€`device_map="auto"` ใ‚’่จญๅฎšใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅŠนๆžœ็š„ใซใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซๅˆ†ๆ•ฃ้…็ฝฎใงใใพใ™๏ผš + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +peft_model_id = "ybelkada/opt-350m-lora" +model = AutoModelForCausalLM.from_pretrained(peft_model_id, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +## Add a new adapter + +ๆ—ขๅญ˜ใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ๆŒใคใƒขใƒ‡ใƒซใซๆ–ฐใ—ใ„ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใซ [`~peft.PeftModel.add_adapter`] ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใŸใ ใ—ใ€ๆ–ฐใ—ใ„ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฏ็พๅœจใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใจๅŒใ˜ใ‚ฟใ‚คใƒ—ใงใ‚ใ‚‹้™ใ‚Šใ€ใ“ใ‚Œใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ใƒขใƒ‡ใƒซใซๆ—ขๅญ˜ใฎ LoRA ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใŒใ‚ขใ‚ฟใƒƒใƒใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆ๏ผš + + +```py +from transformers import AutoModelForCausalLM, OPTForCausalLM, AutoTokenizer +from peft import PeftConfig + +model_id = "facebook/opt-350m" +model = AutoModelForCausalLM.from_pretrained(model_id) + +lora_config = LoraConfig( + target_modules=["q_proj", "k_proj"], + init_lora_weights=False +) + +model.add_adapter(lora_config, adapter_name="adapter_1") +``` + +ๆ–ฐใ—ใ„ใ‚ขใƒ€ใƒ—ใ‚ฟใ‚’่ฟฝๅŠ ใ™ใ‚‹ใซใฏ: + + +```py +# attach new adapter with same config +model.add_adapter(lora_config, adapter_name="adapter_2") +``` + +[`~peft.PeftModel.set_adapter`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใฉใฎใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’่จญๅฎšใงใใพใ™๏ผš + + +```py +# use adapter_1 +model.set_adapter("adapter_1") +output = model.generate(**inputs) +print(tokenizer.decode(output_disabled[0], skip_special_tokens=True)) + +# use adapter_2 +model.set_adapter("adapter_2") +output_enabled = model.generate(**inputs) +print(tokenizer.decode(output_enabled[0], skip_special_tokens=True)) +``` + +## Enable and disable adapters + +ใƒขใƒ‡ใƒซใซใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’่ฟฝๅŠ ใ—ใŸใ‚‰ใ€ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๆœ‰ๅŠนใพใŸใฏ็„กๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใพใ™๏ผš + +```py +from transformers import AutoModelForCausalLM, OPTForCausalLM, AutoTokenizer +from peft import PeftConfig + +model_id = "facebook/opt-350m" +adapter_model_id = "ybelkada/opt-350m-lora" +tokenizer = AutoTokenizer.from_pretrained(model_id) +text = "Hello" +inputs = tokenizer(text, return_tensors="pt") + +model = AutoModelForCausalLM.from_pretrained(model_id) +peft_config = PeftConfig.from_pretrained(adapter_model_id) + +# to initiate with random weights +peft_config.init_lora_weights = False + +model.add_adapter(peft_config) +model.enable_adapters() +output = model.generate(**inputs) +``` + +ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใƒขใ‚ธใƒฅใƒผใƒซใ‚’็„กๅŠนใซใ™ใ‚‹ใซใฏ๏ผš + +```py +model.disable_adapters() +output = model.generate(**inputs) +``` + +## Train a PEFT adapter + +PEFTใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฏ[`Trainer`]ใ‚ฏใƒฉใ‚นใงใ‚ตใƒใƒผใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€็‰นๅฎšใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซๅฏพใ—ใฆใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™ใ€‚ใŸใจใˆใฐใ€LoRAใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆ: + + + +[`Trainer`]ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ด](training)ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +1. ใ‚ฟใ‚นใ‚ฏใ‚ฟใ‚คใƒ—ใจใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใซๅฏพใ™ใ‚‹ใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใฎๆง‹ๆˆใ‚’ๅฎš็พฉใ—ใพใ™๏ผˆใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ[`~peft.LoraConfig`]ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚ + + +```py +from peft import LoraConfig + +peft_config = LoraConfig( + lora_alpha=16, + lora_dropout=0.1, + r=64, + bias="none", + task_type="CAUSAL_LM", +) +``` + +2. ใƒขใƒ‡ใƒซใซใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ€‚ + + +```py +model.add_adapter(peft_config) +``` + +3. ใ“ใ‚Œใงใ€ใƒขใƒ‡ใƒซใ‚’ [`Trainer`] ใซๆธกใ™ใ“ใจใŒใงใใพใ™๏ผ + +```py +trainer = Trainer(model=model, ...) +trainer.train() +``` + +ไฟๅญ˜ใ™ใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใ‚ขใƒ€ใƒ—ใ‚ฟใจใใ‚Œใ‚’่ชญใฟ่พผใ‚€ใŸใ‚ใฎๆ‰‹้ †๏ผš diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_hardware.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_hardware.md new file mode 100644 index 0000000000000000000000000000000000000000..0d104ed3ddb0b366cd979b66d0f507c9a54e43af --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_hardware.md @@ -0,0 +1,160 @@ + + +# Custom hardware for training + +ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใฏใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซๅคงใใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚GPUใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€Tim Dettmerใฎๅ„ชใ‚ŒใŸ[ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://timdettmers.com/2020/09/07/which-gpu-for-deep-learning/)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +GPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฎๅฎŸ็”จ็š„ใชใ‚ขใƒ‰ใƒใ‚คใ‚นใ‚’ใ„ใใคใ‹่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +## GPU +ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ๅŸบๆœฌ็š„ใซใฏไปฅไธ‹ใฎ3ใคใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใŒใ‚ใ‚Šใพใ™๏ผš + +- ใ‚ˆใ‚ŠๅคงใใชGPU +- ใ‚ˆใ‚ŠๅคšใใฎGPU +- ใ‚ˆใ‚ŠๅคšใใฎCPUใŠใ‚ˆใณNVMe๏ผˆ[DeepSpeed-Infinity](main_classes/deepspeed#nvme-support)ใซใ‚ˆใ‚‹ใ‚ชใƒ•ใƒญใƒผใƒ‰๏ผ‰ + +ใพใšใ€ๅ˜ไธ€ใฎGPUใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚ + +### Power and Cooling + +้ซ˜ไพกใชใƒใ‚คใ‚จใƒณใƒ‰GPUใ‚’่ณผๅ…ฅใ—ใŸๅ ดๅˆใ€ๆญฃใ—ใ„้›ปๅŠ›ไพ›็ตฆใจๅๅˆ†ใชๅ†ทๅดใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ + +**้›ปๅŠ›**๏ผš + +ไธ€้ƒจใฎ้ซ˜็ดšใ‚ณใƒณใ‚ทใƒฅใƒผใƒžGPUใ‚ซใƒผใƒ‰ใซใฏใ€2ใคใพใŸใฏ3ใคใฎPCI-E 8ใƒ”ใƒณ้›ปๆบใ‚ฝใ‚ฑใƒƒใƒˆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚ซใƒผใƒ‰ใซใ‚ใ‚‹ใ‚ฝใ‚ฑใƒƒใƒˆใฎๆ•ฐใ ใ‘ใ€็‹ฌ็ซ‹ใ—ใŸ12V PCI-E 8ใƒ”ใƒณใ‚ฑใƒผใƒ–ใƒซใŒๆŽฅ็ถšใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ๅŒใ˜ใ‚ฑใƒผใƒ–ใƒซใฎไธ€็ซฏใซใ‚ใ‚‹2ใคใฎๅˆ†ๅฒ๏ผˆใพใŸใฏใƒ”ใƒƒใ‚ฐใƒ†ใƒผใƒซใ‚ฑใƒผใƒ–ใƒซใจใ—ใฆใ‚‚็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™๏ผ‰ใ‚’ไฝฟ็”จใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ใคใพใ‚Šใ€GPUใซ2ใคใฎใ‚ฝใ‚ฑใƒƒใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใ€PSUใ‹ใ‚‰ใ‚ซใƒผใƒ‰ใซๅ‘ใ‘ใฆ2ใคใฎPCI-E 8ใƒ”ใƒณใ‚ฑใƒผใƒ–ใƒซใ‚’ไฝฟ็”จใ—ใ€1ใคใฎใ‚ฑใƒผใƒ–ใƒซใฎ็ซฏใซ2ใคใฎPCI-E 8ใƒ”ใƒณใ‚ณใƒใ‚ฏใ‚ฟใŒใ‚ใ‚‹ใ‚‚ใฎใฏไฝฟ็”จใ—ใชใ„ใงใใ ใ•ใ„๏ผใใ†ใ—ใชใ„ใจใ€ใ‚ซใƒผใƒ‰ใ‹ใ‚‰ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅๅˆ†ใซๅผ•ใๅ‡บใ™ใ“ใจใŒใงใใพใ›ใ‚“ใ€‚ + +ๅ„PCI-E 8ใƒ”ใƒณ้›ปๆบใ‚ฑใƒผใƒ–ใƒซใฏใ€PSUๅดใฎ12VใƒฌใƒผใƒซใซๆŽฅ็ถšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ๆœ€ๅคงใง150Wใฎ้›ปๅŠ›ใ‚’ไพ›็ตฆใงใใพใ™ใ€‚ + +ไธ€้ƒจใฎใ‚ซใƒผใƒ‰ใฏPCI-E 12ใƒ”ใƒณใ‚ณใƒใ‚ฏใ‚ฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใ€ใ“ใ‚Œใ‚‰ใฏๆœ€ๅคงใง500-600Wใฎ้›ปๅŠ›ใ‚’ไพ›็ตฆใงใใพใ™ใ€‚ + +ไฝŽไพกๆ ผๅธฏใฎใ‚ซใƒผใƒ‰ใฏ6ใƒ”ใƒณใ‚ณใƒใ‚ฏใ‚ฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใ€ๆœ€ๅคงใง75Wใฎ้›ปๅŠ›ใ‚’ไพ›็ตฆใ—ใพใ™ใ€‚ + +ใ•ใ‚‰ใซใ€ใ‚ซใƒผใƒ‰ใŒๅฟ…่ฆใจใ™ใ‚‹ๅฎ‰ๅฎšใ—ใŸ้›ปๅœงใ‚’ๆไพ›ใ™ใ‚‹้ซ˜ๅ“่ณชใช้›ปๆบใƒฆใƒ‹ใƒƒใƒˆ๏ผˆPSU๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ‚‚ใกใ‚ใ‚“ใ€PSUใซใฏใ‚ซใƒผใƒ‰ใ‚’้ง†ๅ‹•ใ™ใ‚‹ใŸใ‚ใซๅๅˆ†ใชๆœชไฝฟ็”จใฎ้›ปๅŠ›ใŒๅฟ…่ฆใงใ™ใ€‚ + +**ๅ†ทๅด**๏ผš + +GPUใŒ้Ž็†ฑใ™ใ‚‹ใจใ€ใ‚นใƒญใƒƒใƒˆใƒชใƒณใ‚ฐใŒ้–‹ๅง‹ใ•ใ‚Œใ€ใƒ•ใƒซใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๆไพ›ใ—ใชใใชใ‚Šใ€้Ž็†ฑใ—ใ™ใŽใ‚‹ใจใ‚ทใƒฃใƒƒใƒˆใƒ€ใ‚ฆใƒณใ™ใ‚‹ใ“ใจใ•ใˆใ‚ใ‚Šใพใ™ใ€‚ + +GPUใŒ้‡่ฆใช่ฒ ่ทใฎไธ‹ใงใฉใฎใ‚ˆใ†ใชๆธฉๅบฆใ‚’็›ฎๆŒ‡ใ™ในใใ‹ใ‚’ๆญฃ็ขบใซ็คบใ™ใ“ใจใฏ้›ฃใ—ใ„ใงใ™ใŒใ€ใŠใใ‚‰ใ+80โ„ƒๆœชๆบ€ใงใ‚ใ‚Œใฐ่‰ฏใ„ใงใ—ใ‚‡ใ†ใŒใ€ใใ‚Œใ‚ˆใ‚ŠไฝŽใ„ๆ–นใŒ่‰ฏใ„ใงใ™ - ใŠใใ‚‰ใ70-75โ„ƒใŒๅ„ชใ‚ŒใŸ็ฏ„ๅ›ฒใงใ—ใ‚‡ใ†ใ€‚ใ‚นใƒญใƒƒใƒˆใƒชใƒณใ‚ฐใฎ้–‹ๅง‹ๆธฉๅบฆใฏใŠใใ‚‰ใ84-90โ„ƒใฎใ‚ใŸใ‚Šใ‹ใ‚‰ใงใ—ใ‚‡ใ†ใ€‚ใ‚นใƒญใƒƒใƒˆใƒชใƒณใ‚ฐใซใ‚ˆใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎไฝŽไธ‹ไปฅๅค–ใซใ‚‚ใ€้•ทๆ™‚้–“ใซใ‚ใŸใ‚‹้žๅธธใซ้ซ˜ใ„ๆธฉๅบฆใฏGPUใฎๅฏฟๅ‘ฝใ‚’็Ÿญ็ธฎใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆฌกใซใ€่ค‡ๆ•ฐใฎGPUใ‚’ๆŒใค้š›ใซๆœ€ใ‚‚้‡่ฆใชๅด้ขใฎไธ€ใคใงใ‚ใ‚‹ๆŽฅ็ถšใซใคใ„ใฆ่ฉณใ—ใ่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +### Multi-GPU Connectivity + +่ค‡ๆ•ฐใฎGPUใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใ‚ซใƒผใƒ‰ใฎ็›ธไบ’ๆŽฅ็ถšๆ–นๆณ•ใฏใƒˆใƒผใ‚ฟใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ใซๅคงใใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚GPUใŒๅŒใ˜็‰ฉ็†ใƒŽใƒผใƒ‰ใซใ‚ใ‚‹ๅ ดๅˆใ€ๆฌกใฎใ‚ˆใ†ใซๅฎŸ่กŒใงใใพใ™๏ผš + + +```bash +nvidia-smi topo -m +``` + +ใ‚‚ใกใ‚ใ‚“ใ€GPUใŒใฉใฎใ‚ˆใ†ใซ็›ธไบ’ๆŽฅ็ถšใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ใƒ‡ใƒฅใ‚ขใƒซGPUใ‚’ๆญ่ผ‰ใ—ใ€NVLinkใงๆŽฅ็ถšใ•ใ‚Œใฆใ„ใ‚‹ใƒžใ‚ทใƒณใงใฏใ€ใŠใใ‚‰ใไปฅไธ‹ใฎใ‚ˆใ†ใชๆƒ…ๅ ฑใŒ่กจ็คบใ•ใ‚Œใ‚‹ใงใ—ใ‚‡ใ†๏ผš + +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X NV2 0-23 N/A +GPU1 NV2 X 0-23 N/A +``` + +ๅˆฅใฎNVLinkใชใ—ใฎใƒžใ‚ทใƒณใงใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใช็ŠถๆณใŒ็™บ็”Ÿใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผš + +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X PHB 0-11 N/A +GPU1 PHB X 0-11 N/A +``` + +ใ“ใกใ‚‰ใŒไผ่ชฌใงใ™๏ผš + +``` + X = Self + SYS = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI) + NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node + PHB = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU) + PXB = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge) + PIX = Connection traversing at most a single PCIe bridge + NV# = Connection traversing a bonded set of # NVLinks +``` + +ๆœ€ๅˆใฎใƒฌใƒใƒผใƒˆใงใ‚ใ‚‹ `NV2` ใงใฏใ€GPUใฏ2ใคใฎNVLinkใงๆŽฅ็ถšใ•ใ‚ŒใฆใŠใ‚Šใ€2็•ช็›ฎใฎใƒฌใƒใƒผใƒˆใงใ‚ใ‚‹ `PHB` ใงใฏใ€ๅ…ธๅž‹็š„ใชๆถˆ่ฒป่€…ๅ‘ใ‘ใฎPCIe+Bridgeใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ‚ใชใŸใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใฉใฎ็จฎ้กžใฎๆŽฅ็ถšๆ€งใŒใ‚ใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใ‚‰ใฎๆŽฅ็ถšๆ–นๆณ•ใฎใ„ใใคใ‹ใฏใ‚ซใƒผใƒ‰้–“ใฎ้€šไฟกใ‚’้€Ÿใใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผˆไพ‹๏ผšNVLink๏ผ‰ใ€ไป–ใฎใ‚‚ใฎใฏ้…ใใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผˆไพ‹๏ผšPHB๏ผ‰ใ€‚ + +ไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใฎ็จฎ้กžใซๅฟœใ˜ใฆใ€ๆŽฅ็ถš้€Ÿๅบฆใฏๅคงใใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ“ใจใ‚‚ใ€ๅฐใ•ใชๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใ€‚GPUใŒใ‚ใพใ‚Š้ ป็นใซๅŒๆœŸใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ๅ ดๅˆใ€DDPใฎใ‚ˆใ†ใซใ€้…ใ„ๆŽฅ็ถšใฎๅฝฑ้Ÿฟใฏใใ‚Œใปใฉ้‡่ฆใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ—ใ‹ใ—ใ€GPUใŒ้ ป็นใซใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’้€ไฟกใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€ZeRO-DPใฎใ‚ˆใ†ใซใ€้ซ˜้€ŸใฎๆŽฅ็ถšใŒใ‚ˆใ‚Š้ซ˜้€Ÿใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซ้žๅธธใซ้‡่ฆใซใชใ‚Šใพใ™ใ€‚ + +#### NVlink + +[NVLink](https://en.wikipedia.org/wiki/NVLink) ใฏใ€Nvidiaใซใ‚ˆใฃใฆ้–‹็™บใ•ใ‚ŒใŸๆœ‰็ทšใฎใ‚ทใƒชใ‚ขใƒซใƒžใƒซใƒใƒฌใƒผใƒณใฎ่ฟ‘่ท้›ข้€šไฟกใƒชใƒณใ‚ฏใงใ™ใ€‚ + +ๅ„ๆ–ฐไธ–ไปฃใงใฏใ€ใ‚ˆใ‚Š้ซ˜้€ŸใชๅธฏๅŸŸๅน…ใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ใŸใจใˆใฐใ€[Nvidia Ampere GA102 GPU Architecture](https://www.nvidia.com/content/dam/en-zz/Solutions/geforce/ampere/pdf/NVIDIA-ampere-GA102-GPU-Architecture-Whitepaper-V1.pdf) ใ‹ใ‚‰ใฎๅผ•็”จใงใ™ใ€‚ + +> Third-Generation NVLinkยฎ +> GA102 GPUs utilize NVIDIAโ€™s third-generation NVLink interface, which includes four x4 links, +> with each link providing 14.0625 GB/sec bandwidth in each direction between two GPUs. Four +> links provide 56.25 GB/sec bandwidth in each direction, and 112.5 GB/sec total bandwidth +> between two GPUs. Two RTX 3090 GPUs can be connected together for SLI using NVLink. +> (Note that 3-Way and 4-Way SLI configurations are not supported.) + +ใ—ใŸใŒใฃใฆใ€`nvidia-smi topo -m` ใฎๅ‡บๅŠ›ใฎ `NVX` ใƒฌใƒใƒผใƒˆใงๅ–ๅพ—ใ™ใ‚‹ `X` ใŒ้ซ˜ใ„ใปใฉ่‰ฏใ„ใงใ™ใ€‚ไธ–ไปฃใฏใ‚ใชใŸใฎGPUใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซไพๅญ˜ใ—ใพใ™ใ€‚ + +ๅฐใ•ใชใ‚ตใƒณใƒ—ใƒซใฎwikitextใ‚’ไฝฟ็”จใ—ใŸgpt2่จ€่ชžใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅฎŸ่กŒใ‚’ๆฏ”่ผƒใ—ใพใ—ใ‚‡ใ†ใ€‚ + +็ตๆžœใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +๏ผˆใ“ใ“ใซ็ตๆžœใ‚’ๆŒฟๅ…ฅ๏ผ‰ + +ไธŠ่จ˜ใฎใƒ†ใ‚ญใ‚นใƒˆใฎๆ—ฅๆœฌ่ชž่จณใ‚’ๆไพ›ใ—ใพใ—ใŸใ€‚Markdownใ‚ณใƒผใƒ‰ใจใ—ใฆใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ—ใพใ—ใŸใ€‚ใฉใ‚“ใชไป–ใฎ่ณชๅ•ใŒใ‚ใ‚Œใฐใ€ใŠๆฐ—่ปฝใซใŠ็Ÿฅใ‚‰ใ›ใใ ใ•ใ„๏ผ + +| NVlink | Time | +| ----- | ---: | +| Y | 101s | +| N | 131s | + + +NVLinkใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ็ด„23๏ผ…้€ŸใๅฎŒไบ†ใ™ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚2็•ช็›ฎใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงใฏใ€`NCCL_P2P_DISABLE=1`ใ‚’ไฝฟ็”จใ—ใฆใ€GPUใŒNVLinkใ‚’ไฝฟ็”จใ—ใชใ„ใ‚ˆใ†ใซๆŒ‡็คบใ—ใฆใ„ใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ๅฎŒๅ…จใชใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚ณใƒผใƒ‰ใจๅ‡บๅŠ›ใงใ™๏ผš + + +```bash +# DDP w/ NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train \ +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 101.9003, 'train_samples_per_second': 1.963, 'epoch': 0.69} + +# DDP w/o NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 NCCL_P2P_DISABLE=1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 131.4367, 'train_samples_per_second': 1.522, 'epoch': 0.69} +``` + +Hardware: 2x TITAN RTX 24GB each + NVlink with 2 NVLinks (`NV2` in `nvidia-smi topo -m`) +Software: `pytorch-1.8-to-be` + `cuda-11.0` / `transformers==4.3.0.dev0` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..d23ae65f309f41c044e5df8c93ba4945947d5a28 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_cpu.md @@ -0,0 +1,74 @@ + + + +# Efficient Inference on CPU + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ€CPUไธŠใงๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎๅŠน็އ็š„ใชๆŽจ่ซ–ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ + +## `BetterTransformer` for faster inference + +ๆœ€่ฟ‘ใ€ใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€ใŠใ‚ˆใณ้Ÿณๅฃฐใƒขใƒ‡ใƒซใฎCPUไธŠใงใฎ้ซ˜้€ŸใชๆŽจ่ซ–ใฎใŸใ‚ใซ`BetterTransformer`ใ‚’็ตฑๅˆใ—ใพใ—ใŸใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใ“ใฎ็ตฑๅˆใซ้–ขใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’[ใ“ใกใ‚‰](https://huggingface.co/docs/optimum/bettertransformer/overview)ใง็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +## PyTorch JITใƒขใƒผใƒ‰๏ผˆTorchScript๏ผ‰ +TorchScriptใฏใ€PyTorchใ‚ณใƒผใƒ‰ใ‹ใ‚‰ใ‚ทใƒชใ‚ขใƒฉใ‚คใ‚บๅฏ่ƒฝใงๆœ€้ฉๅŒ–ๅฏ่ƒฝใชใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ไปปๆ„ใฎTorchScriptใƒ—ใƒญใ‚ฐใƒฉใƒ ใฏใ€Pythonไพๅญ˜ๆ€งใฎใชใ„ใƒ—ใƒญใ‚ปใ‚นใงไฟๅญ˜ใŠใ‚ˆใณใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚คใƒผใ‚ฌใƒผใƒขใƒผใƒ‰ใจๆฏ”่ผƒใ—ใฆใ€PyTorchใฎjitใƒขใƒผใƒ‰ใฏ้€šๅธธใ€ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใชใฉใฎๆœ€้ฉๅŒ–ๆ‰‹ๆณ•ใซใ‚ˆใ‚Šใƒขใƒ‡ใƒซๆŽจ่ซ–ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ—ใพใ™ใ€‚ + +TorchScriptใฎ็ฐกๅ˜ใช็ดนไป‹ใซใคใ„ใฆใฏใ€[PyTorch TorchScriptใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html#tracing-modules)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### JITใƒขใƒผใƒ‰ใงใฎIPEXใ‚ฐใƒฉใƒ•ๆœ€้ฉๅŒ– +Intelยฎ Extension for PyTorchใฏใ€Transformersใ‚ทใƒชใƒผใ‚บใƒขใƒ‡ใƒซใฎjitใƒขใƒผใƒ‰ใซใ•ใ‚‰ใชใ‚‹ๆœ€้ฉๅŒ–ใ‚’ๆไพ›ใ—ใพใ™ใ€‚Intelยฎ Extension for PyTorchใ‚’jitใƒขใƒผใƒ‰ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚Transformersใƒขใƒ‡ใƒซใ‹ใ‚‰ใ‚ˆใไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผใƒ‘ใ‚ฟใƒผใƒณใฎใ„ใใคใ‹ใฏใ€ๆ—ขใซIntelยฎ Extension for PyTorchใงjitใƒขใƒผใƒ‰ใฎใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใซๅฏพๅฟœใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใƒ‘ใ‚ฟใƒผใƒณ๏ผˆMulti-head-attentionใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใ€Concat Linearใ€Linear+Addใ€Linear+Geluใ€Add+LayerNormใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใชใฉ๏ผ‰ใฏๆœ‰ๅŠนใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒ่‰ฏใ„ใงใ™ใ€‚ใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใฎๅˆฉ็‚นใฏใ€ใƒฆใƒผใ‚ถใƒผใซ้€้Ž็š„ใซๆไพ›ใ•ใ‚Œใพใ™ใ€‚ๅˆ†ๆžใซใ‚ˆใ‚Œใฐใ€ๆœ€ใ‚‚ไบบๆฐ—ใฎใ‚ใ‚‹่ณชๅ•ๅฟœ็ญ”ใ€ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ€ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใฎNLPใ‚ฟใ‚นใ‚ฏใฎ็ด„70๏ผ…ใŒใ€ใ“ใ‚Œใ‚‰ใฎใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใƒ‘ใ‚ฟใƒผใƒณใ‚’ไฝฟ็”จใ—ใฆFloat32็ฒพๅบฆใจBFloat16ๆททๅˆ็ฒพๅบฆใฎไธกๆ–นใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅˆฉ็‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +[IPEXใ‚ฐใƒฉใƒ•ๆœ€้ฉๅŒ–ใฎ่ฉณ็ดฐๆƒ…ๅ ฑ](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/graph_optimization.html)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +#### IPEX installation: + +IPEXใฎใƒชใƒชใƒผใ‚นใฏPyTorchใซๅพ“ใฃใฆใ„ใพใ™ใ€‚[IPEXใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ–นๆณ•](https://intel.github.io/intel-extension-for-pytorch/)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### Usage of JIT-mode +Trainerใง่ฉ•ไพกใพใŸใฏไบˆๆธฌใฎใŸใ‚ใซJITใƒขใƒผใƒ‰ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ใƒฆใƒผใ‚ถใƒผใฏTrainerใ‚ณใƒžใƒณใƒ‰ๅผ•ๆ•ฐใซ`jit_mode_eval`ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +PyTorch >= 1.14.0ใฎๅ ดๅˆใ€jitใƒขใƒผใƒ‰ใฏjit.traceใงdictๅ…ฅๅŠ›ใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ไบˆๆธฌใจ่ฉ•ไพกใซไปปๆ„ใฎใƒขใƒ‡ใƒซใซๅˆฉ็›Šใ‚’ใ‚‚ใŸใ‚‰ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +PyTorch < 1.14.0ใฎๅ ดๅˆใ€jitใƒขใƒผใƒ‰ใฏforwardใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ้ †ๅบใŒjit.traceใฎใ‚ฟใƒ—ใƒซๅ…ฅๅŠ›ใฎ้ †ๅบใจไธ€่‡ดใ™ใ‚‹ใƒขใƒ‡ใƒซใซๅˆฉ็›Šใ‚’ใ‚‚ใŸใ‚‰ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผˆ่ณชๅ•ๅฟœ็ญ”ใƒขใƒ‡ใƒซใชใฉ๏ผ‰ใ€‚jit.traceใŒใ‚ฟใƒ—ใƒซๅ…ฅๅŠ›ใฎ้ †ๅบใจไธ€่‡ดใ—ใชใ„ๅ ดๅˆใ€ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใƒขใƒ‡ใƒซใชใฉใ€jit.traceใฏๅคฑๆ•—ใ—ใ€ใ“ใ‚Œใ‚’ใƒ•ใ‚ฉใƒผใƒซใƒใƒƒใ‚ฏใ•ใ›ใ‚‹ใŸใ‚ใซไพ‹ๅค–ใงใ‚ญใƒฃใƒƒใƒใ—ใฆใ„ใพใ™ใ€‚ใƒญใ‚ฐใฏใƒฆใƒผใ‚ถใƒผใซ้€š็Ÿฅใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + + +[Transformers่ณชๅ•ๅฟœ็ญ”ใฎไฝฟ็”จไพ‹](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)ใ‚’ๅ‚่€ƒใซใ—ใฆใใ ใ•ใ„ใ€‚ + +- Inference using jit mode on CPU: +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--jit_mode_eval 
+ +- Inference with IPEX using jit mode on CPU: +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--use_ipex \
+--jit_mode_eval
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..6a71c1094494d5aca3480c021c8af16086724ee4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_many.md @@ -0,0 +1,127 @@ + + +# Efficient Inference on a Multiple GPUs + +ใ“ใฎๆ–‡ๆ›ธใซใฏใ€่ค‡ๆ•ฐใฎGPUใงๅŠน็އ็š„ใซๆŽจ่ซ–ใ‚’่กŒใ†ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + + +ๆณจๆ„: ่ค‡ๆ•ฐใฎGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฏใ€[ๅ˜ไธ€ใฎGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](./perf_infer_gpu_one)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใปใจใ‚“ใฉใฎๆˆฆ็•ฅใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใŸใ ใ—ใ€ใ‚ˆใ‚Š่‰ฏใ„ไฝฟ็”จๆณ•ใฎใŸใ‚ใซไฝฟ็”จใงใใ‚‹็ฐกๅ˜ใชใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใซใคใ„ใฆใ‚‚่ช่ญ˜ใ—ใฆใŠใๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +## Flash Attention 2 + +Flash Attention 2ใฎ็ตฑๅˆใฏใ€่ค‡ๆ•ฐใฎGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใ‚‚ๆฉŸ่ƒฝใ—ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅ˜ไธ€ใฎGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](./perf_infer_gpu_one#Flash-Attention-2)ใฎ้ฉๅˆ‡ใชใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## BetterTransformer + +[BetterTransformer](https://huggingface.co/docs/optimum/bettertransformer/overview)ใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ้ซ˜้€ŸๅฎŸ่กŒใƒ‘ใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅค‰ๆ›ใ—ใ€ใใฎไธ‹ใงFlash Attentionใชใฉใฎๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใ‚ซใƒผใƒใƒซใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ + +BetterTransformerใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€้Ÿณๅฃฐใƒขใƒ‡ใƒซใฎๅ˜ไธ€GPUใŠใ‚ˆใณ่ค‡ๆ•ฐGPUใงใฎ้ซ˜้€ŸๆŽจ่ซ–ใ‚‚ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ + + +Flash Attentionใฏใ€fp16ใพใŸใฏbf16 dtypeใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใซใฎใฟไฝฟ็”จใงใใพใ™ใ€‚BetterTransformerใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใ‚’้ฉๅˆ‡ใชdtypeใซใ‚ญใƒฃใ‚นใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ + + + +### Decoder models + +ใƒ†ใ‚ญใ‚นใƒˆใƒขใƒ‡ใƒซใ€็‰นใซใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซ๏ผˆGPTใ€T5ใ€Llamaใชใฉ๏ผ‰ใฎๅ ดๅˆใ€BetterTransformer APIใฏใ™ในใฆใฎๆณจๆ„ๆ“ไฝœใ‚’[`torch.nn.functional.scaled_dot_product_attention`ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผ](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention)๏ผˆSDPA๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏPyTorch 2.0ไปฅ้™ใงใฎใฟไฝฟ็”จๅฏ่ƒฝใงใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’BetterTransformerใซๅค‰ๆ›ใ™ใ‚‹ใซใฏ๏ผš + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m") +# convert the model to BetterTransformer +model.to_bettertransformer() + +# Use it for training or inference +``` + +SDPAใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚„ๅ•้กŒใฎใ‚ตใ‚คใ‚บใชใฉใฎ็‰นๅฎšใฎ่จญๅฎšใง[Flash Attention](https://huggingface.co/papers/2205.14135)ใ‚ซใƒผใƒใƒซใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚Flash Attentionใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€็‰นๅฎšใฎ่จญๅฎš๏ผˆใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ€ๅ•้กŒใฎใ‚ตใ‚คใ‚บ๏ผ‰ใงๅˆฉ็”จๅฏ่ƒฝใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[`torch.nn.kernel.sdpa_kernel`](https://pytorch.org/docs/stable/generated/torch.nn.attention.sdpa_kernel.html)ใ‚’ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒžใƒใƒผใ‚ธใƒฃใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + + +```diff +import torch ++ from torch.nn.attention import SDPBackend, sdpa_kernel +from transformers import AutoModelForCausalLM, AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m").to("cuda") +# convert the model to BetterTransformer +model.to_bettertransformer() + +input_text = "Hello my dog is cute and" +inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + ++ with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + outputs = model.generate(**inputs) + +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` + +ใ‚‚ใ—ใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใงๆฌกใฎใ‚ˆใ†ใชใ‚จใƒฉใƒผใƒกใƒƒใ‚ปใƒผใ‚ธใŒ่กจ็คบใ•ใ‚ŒใŸๅ ดๅˆ๏ผš + + +```bash +RuntimeError: No available kernel. Aborting execution. +``` + +ๅฝ“ๆ—ฅใ€Flash Attentionใฎใ‚ซใƒใƒฌใƒƒใ‚ธใŒๅบƒ็ฏ„ๅ›ฒใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹PyTorch Nightlyใƒใƒผใ‚ธใƒงใƒณใ‚’่ฉฆใ™ใ‚ˆใ†ใซใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```bash +pip3 install -U --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118 +``` + +[ใ“ใฎใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://pytorch.org/blog/out-of-the-box-acceleration/)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใ€BetterTransformer + SDPA APIใงๅฏ่ƒฝใชใ“ใจใซใคใ„ใฆ่ฉณใ—ใๅญฆใณใพใ—ใ‚‡ใ†ใ€‚ + +### Encoder Models + +ๆŽจ่ซ–ไธญใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใงใฏใ€BetterTransformerใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒฌใ‚คใƒคใƒผใฎforwardๅ‘ผใณๅ‡บใ—ใ‚’ใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒฌใ‚คใƒคใƒผใฎ[`torch.nn.TransformerEncoderLayer`](https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html)ใฎ็›ธๅฝ“ใ™ใ‚‹ใ‚‚ใฎใซใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒฌใ‚คใƒคใƒผใฎ้ซ˜้€ŸๅฎŸ่ฃ…ใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + +`torch.nn.TransformerEncoderLayer`ใฎ้ซ˜้€ŸๅฎŸ่ฃ…ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใชใ„ใŸใ‚ใ€ไปฃใ‚ใ‚Šใซ`torch.nn.functional.scaled_dot_product_attention`ใซใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸใƒ†ใƒณใ‚ฝใƒซใ‚’ๆดป็”จใ—ใชใ„Flash AttentionใพใŸใฏMemory-Efficient Attentionใฎ่žๅˆใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +BetterTransformerใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใ“ใฎ[ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)ใ‚’ใ”่ฆงใ„ใŸใ ใ‘ใพใ™ใ€‚ใพใŸใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซ็”จใฎBetterTransformerใซใคใ„ใฆใฏใ€ใ“ใฎ[ใƒ–ใƒญใ‚ฐ](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/)ใง่ฉณใ—ใๅญฆใถใ“ใจใŒใงใใพใ™ใ€‚ + + +## Advanced usage: mixing FP4 (or Int8) and BetterTransformer + +ใƒขใƒ‡ใƒซใฎๆœ€่‰ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใ€ไธŠ่จ˜ใง่ชฌๆ˜Žใ—ใŸ็•ฐใชใ‚‹ๆ–นๆณ•ใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€FP4ใƒŸใƒƒใ‚ฏใ‚นใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณๆŽจ่ซ–+Flash Attentionใ‚’ไฝฟ็”จใ—ใŸBetterTransformerใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```py +import torch +from torch.nn.attention import SDPBackend, sdpa_kernel +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_compute_dtype=torch.float16 +) + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", quantization_config=quantization_config) + +input_text = "Hello my dog is cute and" +inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + +with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + outputs = model.generate(**inputs) + +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..374d725a992374970044f695e79c4d2798e154c3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_gpu_one.md @@ -0,0 +1,443 @@ + + +# Efficient Inference on a Single GPU + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใซๅŠ ใˆใฆใ€[1ใคใฎGPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ฌใ‚คใƒ‰](perf_train_gpu_one)ใจ[CPUใงใฎๆŽจ่ซ–ใ‚ฌใ‚คใƒ‰](perf_infer_cpu)ใซ้–ข้€ฃใ™ใ‚‹ๆƒ…ๅ ฑใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Flash Attention 2 + + + +ใ“ใฎๆฉŸ่ƒฝใฏๅฎŸ้จ“็š„ใงใ‚ใ‚Šใ€ๅฐ†ๆฅใฎใƒใƒผใ‚ธใƒงใƒณใงๅคงๅน…ใซๅค‰ๆ›ดใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€Flash Attention 2 APIใฏ่ฟ‘ใ„ๅฐ†ๆฅ`BetterTransformer` APIใซ็งป่กŒใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ + + + +Flash Attention 2ใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–้€Ÿๅบฆใ‚’ๅคงๅน…ใซ้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚Flash Attention 2ใฏใ€Tri Daoๆฐใซใ‚ˆใฃใฆ[ๅ…ฌๅผใฎFlash Attentionใƒชใƒใ‚ธใƒˆใƒช](https://github.com/Dao-AILab/flash-attention)ใงๅฐŽๅ…ฅใ•ใ‚Œใพใ—ใŸใ€‚Flash Attentionใซ้–ขใ™ใ‚‹็ง‘ๅญฆ่ซ–ๆ–‡ใฏ[ใ“ใกใ‚‰](https://huggingface.co/papers/2205.14135)ใง่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +Flash Attention 2ใ‚’ๆญฃใ—ใใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ไธŠ่จ˜ใฎใƒชใƒใ‚ธใƒˆใƒชใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใ‚‹ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ‚ฌใ‚คใƒ‰ใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ + +ไปฅไธ‹ใฎใƒขใƒ‡ใƒซใซๅฏพใ—ใฆFlash Attention 2ใ‚’ใƒใ‚คใƒ†ใ‚ฃใƒ–ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™๏ผš + +- Llama +- Falcon + +ใ•ใ‚‰ใซๅคšใใฎใƒขใƒ‡ใƒซใซFlash Attention 2ใฎใ‚ตใƒใƒผใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’GitHubใงๆๆกˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใ€ๅค‰ๆ›ดใ‚’็ตฑๅˆใ™ใ‚‹ใŸใ‚ใซใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅซใ‚€ใ€ๆŽจ่ซ–ใจใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใงใใพใ™๏ผˆ็พๅœจใฎ`BetterTransformer` APIใงใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„๏ผ‰ใ€‚ + + + +Flash Attention 2ใฏใ€ใƒขใƒ‡ใƒซใฎdtypeใŒ`fp16`ใพใŸใฏ`bf16`ใฎๅ ดๅˆใซใฎใฟไฝฟ็”จใงใใ€NVIDIA-GPUใƒ‡ใƒใ‚คใ‚นใงใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใ‚’้ฉๅˆ‡ใชdtypeใซใ‚ญใƒฃใ‚นใƒˆใ—ใ€ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒ‡ใƒใ‚คใ‚นใซใƒญใƒผใƒ‰ใ—ใฆใใ ใ•ใ„ใ€‚ + + + +### Quick usage + +ใƒขใƒ‡ใƒซใงFlash Attention 2ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`from_pretrained`ใฎๅผ•ๆ•ฐใซ`attn_implementation="flash_attention_2"`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM + +model_id = "tiiuae/falcon-7b" +tokenizer = AutoTokenizer.from_pretrained(model_id) + +model = AutoModelForCausalLM.from_pretrained( + model_id, + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2", +) +``` + +ใ“ใกใ‚‰ใฏใ€็”ŸๆˆใพใŸใฏๅพฎ่ชฟๆ•ดใฎใŸใ‚ใซไฝฟ็”จใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใงใ™ใ€‚ + +### Expected speedups + +็‰นใซ้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฏพใ—ใฆใ€ๅพฎ่ชฟๆ•ดใจๆŽจ่ซ–ใฎ้š›ใซใฏใ€ใ‹ใชใ‚Šใฎ้ซ˜้€ŸๅŒ–ใŒๆœŸๅพ…ใงใใพใ™ใ€‚ใŸใ ใ—ใ€Flash Attentionใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ—ใชใ„ใŸใ‚ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒๅซใพใ‚Œใ‚‹ๅ ดๅˆใ€ใƒใƒƒใƒๆŽจ่ซ–ใซใŠใ„ใฆใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’ๆ‰‹ๅ‹•ใงใƒ‘ใƒƒใƒ‰/ใ‚ขใƒณใƒ‘ใƒƒใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ๅซใ‚€ใƒใƒƒใƒ็”Ÿๆˆใฎๅคงๅน…ใช้…ๅปถใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚’ๅ…‹ๆœใ™ใ‚‹ใŸใ‚ใซใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ›ใšใซFlash Attentionใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผˆใŸใจใˆใฐใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ‘ใƒƒใ‚ฏใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆœ€ๅคงใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใซ้”ใ™ใ‚‹ใพใง้€ฃ็ตใ™ใ‚‹ใ“ใจใชใฉ๏ผ‰ใ€‚ใ“ใ“ใซ[ไพ‹](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_clm.py#L516)ใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใฎใชใ„ๅ ดๅˆใซใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ4096ใฎ[tiiuae/falcon-7b](https://hf.co/tiiuae/falcon-7b)ใซๅฏพใ™ใ‚‹ๅ˜็ด”ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎไบˆๆƒณใ•ใ‚Œใ‚‹้ซ˜้€ŸๅŒ–ใงใ™ใ€‚ใ•ใพใ–ใพใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš + +
+ +
+ +ไปฅไธ‹ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใฎใชใ„ๅ ดๅˆใซใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใŒ4096ใฎ[`meta-llama/Llama-7b-hf`](https://hf.co/meta-llama/Llama-7b-hf)ใซๅฏพใ™ใ‚‹ๅ˜็ด”ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใฎไบˆๆƒณใ•ใ‚Œใ‚‹้ซ˜้€ŸๅŒ–ใงใ™ใ€‚ใ•ใพใ–ใพใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš + +
+ +
+ +ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ๅซใ‚€ใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผˆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใพใŸใฏ็”Ÿๆˆใ™ใ‚‹๏ผ‰ใฎๅ ดๅˆใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚นใ‚ณใ‚ขใ‚’ๆญฃใ—ใ่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใ‚ขใƒณใƒ‘ใƒƒใƒ‰/ใƒ‘ใƒƒใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฏ”่ผƒ็š„ๅฐใ•ใ„ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎๅ ดๅˆใ€็ด”็ฒ‹ใชใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใงใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒ30%ๆœชๆบ€ใ—ใ‹ๅŸ‹ใ‚ใ‚‰ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใ“ใ‚Œใฏใ‚ใšใ‹ใช้ซ˜้€ŸๅŒ–ใ‚’ใ‚‚ใŸใ‚‰ใ—ใพใ™ใ€‚ + +
+ +
+ +ใ—ใ‹ใ—ใ€ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎๅ ดๅˆใ€็ด”็ฒ‹ใชๆŽจ่ซ–๏ผˆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚‚ๅซใ‚€๏ผ‰ใซใฏ่ˆˆๅ‘ณๆทฑใ„้ซ˜้€ŸๅŒ–ใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +Flash Attentionใฏใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ่จˆ็ฎ—ใ‚’ใ‚ˆใ‚ŠใƒกใƒขใƒชๅŠน็އใฎ่‰ฏใ„ใ‚‚ใฎใซใ—ใ€ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใงใฎCUDA OOMใฎๅ•้กŒใ‚’ๅ›ž้ฟใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ๅคงใใชใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใซๅฏพใ—ใฆๆœ€ๅคง20ใฎใƒกใƒขใƒชๅ‰Šๆธ›ใ‚’ใ‚‚ใŸใ‚‰ใ™ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ๅ…ฌๅผใฎFlash Attentionใƒชใƒใ‚ธใƒˆใƒช](https://github.com/Dao-AILab/flash-attention)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +
+ +
+ + +### Advanced usage + +ใ“ใฎๆฉŸ่ƒฝใ‚’ใƒขใƒ‡ใƒซใฎๆœ€้ฉๅŒ–ใซๅคšใใฎๆ—ขๅญ˜ใฎๆฉŸ่ƒฝใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใซใ„ใใคใ‹ใฎไพ‹ใ‚’็คบใ—ใพใ™๏ผš + +### Combining Flash Attention 2 and 8-bit models + +ใ“ใฎๆฉŸ่ƒฝใ‚’8ใƒ“ใƒƒใƒˆใฎ้‡ๅญๅŒ–ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM + +model_id = "tiiuae/falcon-7b" +tokenizer = AutoTokenizer.from_pretrained(model_id) + +model = AutoModelForCausalLM.from_pretrained( + model_id, + load_in_8bit=True, + attn_implementation="flash_attention_2", +) +``` + +### Combining Flash Attention 2 and 4-bit models + +ใ“ใฎๆฉŸ่ƒฝใ‚’ 4 ใƒ“ใƒƒใƒˆใฎ้‡ๅญๅŒ–ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM + +model_id = "tiiuae/falcon-7b" +tokenizer = AutoTokenizer.from_pretrained(model_id) + +model = AutoModelForCausalLM.from_pretrained( + model_id, + load_in_4bit=True, + attn_implementation="flash_attention_2", +) +``` + +### Combining Flash Attention 2 and PEFT + +ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ—ใฆใ€Flash Attention 2ใ‚’ใƒ™ใƒผใ‚นใซใ‚ขใƒ€ใƒ—ใ‚ฟใƒผใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใซPEFTใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM +from peft import LoraConfig + +model_id = "tiiuae/falcon-7b" +tokenizer = AutoTokenizer.from_pretrained(model_id) + +model = AutoModelForCausalLM.from_pretrained( + model_id, + load_in_4bit=True, + attn_implementation="flash_attention_2", +) + +lora_config = LoraConfig( + r=8, + task_type="CAUSAL_LM" +) + +model.add_adapter(lora_config) + +... # train your model +``` + +## BetterTransformer + +[BetterTransformer](https://huggingface.co/docs/optimum/bettertransformer/overview)ใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ้ซ˜้€Ÿใƒ‘ใ‚นๅฎŸ่กŒใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€Flash Attentionใชใฉใฎๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใ‚ซใƒผใƒใƒซใŒๅ†…้ƒจใงๅ‘ผใณๅ‡บใ•ใ‚Œใพใ™ใ€‚ + +BetterTransformerใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€ใŠใ‚ˆใณใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒขใƒ‡ใƒซใฎๅ˜ไธ€ใŠใ‚ˆใณใƒžใƒซใƒGPUใงใฎ้ซ˜้€ŸใชๆŽจ่ซ–ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ + + + +Flash Attentionใฏใ€fp16ใพใŸใฏbf16ใฎdtypeใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใซใฎใฟไฝฟ็”จใงใใพใ™ใ€‚BetterTransformerใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใ‚’้ฉๅˆ‡ใชdtypeใซใ‚ญใƒฃใ‚นใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ + + + +### Encoder models + +PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ[`nn.MultiHeadAttention`](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/)ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ้ซ˜้€Ÿใƒ‘ใ‚นใ€BetterTransformerใจๅ‘ผใฐใ‚Œใ‚‹ใ‚‚ใฎใฏใ€[๐Ÿค— Optimumใƒฉใ‚คใƒ–ใƒฉใƒช](https://huggingface.co/docs/optimum/bettertransformer/overview)ใฎ็ตฑๅˆใ‚’้€šใ˜ใฆTransformersใจไธ€็ท’ใซไฝฟ็”จใงใใพใ™ใ€‚ + +PyTorchใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ้ซ˜้€Ÿใƒ‘ใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ‚ซใƒผใƒใƒซใƒ•ใƒฅใƒผใ‚ธใƒงใƒณใจ[ใƒใ‚นใƒˆใ•ใ‚ŒใŸใƒ†ใƒณใ‚ฝใƒซ](https://pytorch.org/docs/stable/nested.html)ใฎไฝฟ็”จใซใ‚ˆใ‚Šใ€ๆŽจ่ซ–ใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚่ฉณ็ดฐใชใƒ™ใƒณใƒใƒžใƒผใ‚ฏๆƒ…ๅ ฑใฏ[ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)ใซใ‚ใ‚Šใพใ™ใ€‚ + +[`optimum`](https://github.com/huggingface/optimum)ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅพŒใ€ๆŽจ่ซ–ไธญใซBetter Transformerใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€้–ข้€ฃใ™ใ‚‹ๅ†…้ƒจใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใง็ฝฎใๆ›ใˆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™[`~PreTrainedModel.to_bettertransformer`]: + + +```python +model = model.to_bettertransformer() +``` + +ใƒกใ‚ฝใƒƒใƒ‰ [`~PreTrainedModel.reverse_bettertransformer`] ใฏใ€ใƒขใƒ‡ใƒซใ‚’ไฟๅญ˜ใ™ใ‚‹ๅ‰ใซไฝฟ็”จใ™ในใใงใ€ๆจ™ๆบ–ใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™๏ผš + +```python +model = model.reverse_bettertransformer() +model.save_pretrained("saved_model") +``` + +BetterTransformer APIใ‚’ไฝฟใฃใŸใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฎๅฏ่ƒฝๆ€งใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚‹ใซใฏใ€[ใ“ใฎใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +### Decoder models + +ใƒ†ใ‚ญใ‚นใƒˆใƒขใƒ‡ใƒซใ€็‰นใซใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ™ใƒผใ‚นใฎใƒขใƒ‡ใƒซ๏ผˆGPTใ€T5ใ€Llamaใชใฉ๏ผ‰ใซใจใฃใฆใ€BetterTransformer APIใฏใ™ในใฆใฎๆณจๆ„ๆ“ไฝœใ‚’[`torch.nn.functional.scaled_dot_product_attention`ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผ](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention)๏ผˆSDPA๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใฎใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผใฏPyTorch 2.0ไปฅ้™ใงใฎใฟๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’BetterTransformerใซๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m") +# convert the model to BetterTransformer +model.to_bettertransformer() + +# Use it for training or inference +``` + +SDPAใฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚„ๅ•้กŒใฎใ‚ตใ‚คใ‚บใซๅฟœใ˜ใฆ[Flash Attention](https://huggingface.co/papers/2205.14135)ใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚Flash Attentionใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ‹ใ€็‰นๅฎšใฎ่จญๅฎš๏ผˆใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ€ๅ•้กŒใ‚ตใ‚คใ‚บ๏ผ‰ใงไฝฟ็”จๅฏ่ƒฝใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[`torch.nn.attention.sdpa_kernel`](https://pytorch.org/docs/stable/generated/torch.nn.attention.sdpa_kernel.html)ใ‚’ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒžใƒใƒผใ‚ธใƒฃใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + + +```diff +import torch ++ from torch.nn.attention import SDPBackend, sdpa_kernel +from transformers import AutoModelForCausalLM, AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", torch_dtype=torch.float16).to("cuda") +# convert the model to BetterTransformer +model.to_bettertransformer() + +input_text = "Hello my dog is cute and" +inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + ++ with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + outputs = model.generate(**inputs) + +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` + +ใ‚‚ใ—ใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใซใƒใ‚ฐใŒ่กจ็คบใ•ใ‚ŒใŸๅ ดๅˆ + +```bash +RuntimeError: No available kernel. Aborting execution. +``` + +Flash Attention ใฎๅบƒ็ฏ„ใชใ‚ซใƒใƒฌใƒƒใ‚ธใ‚’ๆŒใคใ‹ใ‚‚ใ—ใ‚Œใชใ„ PyTorch ใฎใƒŠใ‚คใƒˆใƒชใƒผใƒใƒผใ‚ธใƒงใƒณใ‚’่ฉฆใ—ใฆใฟใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```bash +pip3 install -U --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu118 +``` + +Or make sure your model is correctly casted in float16 or bfloat16 + +ใƒขใƒ‡ใƒซใŒๆญฃใ—ใfloat16ใพใŸใฏbfloat16ใซใ‚ญใƒฃใ‚นใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +Have a look at [this detailed blogpost](https://pytorch.org/blog/out-of-the-box-acceleration/) to read more about what is possible to do with `BetterTransformer` + SDPA API. + +`BetterTransformer` + SDPA APIใ‚’ไฝฟ็”จใ—ใฆไฝ•ใŒๅฏ่ƒฝใ‹ใซใคใ„ใฆ่ฉณใ—ใ่ชญใ‚€ใซใฏใ€[ใ“ใฎ่ฉณ็ดฐใชใƒ–ใƒญใ‚ฐใƒใ‚นใƒˆ](https://pytorch.org/blog/out-of-the-box-acceleration/)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## `bitsandbytes` integration for FP4 mixed-precision inference + +FP4ๆททๅˆ็ฒพๅบฆๆŽจ่ซ–ใฎใŸใ‚ใฎ`bitsandbytes`็ตฑๅˆ + +You can install `bitsandbytes` and benefit from easy model compression on GPUs. Using FP4 quantization you can expect to reduce up to 8x the model size compared to its native full precision version. Check out below how to get started. + +`bitsandbytes`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใ€GPUใง็ฐกๅ˜ใชใƒขใƒ‡ใƒซใฎๅœง็ธฎใ‚’ๅˆฉ็”จใงใใพใ™ใ€‚FP4้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎใƒ•ใƒซใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณใƒใƒผใ‚ธใƒงใƒณใจๆฏ”่ผƒใ—ใฆใƒขใƒ‡ใƒซใ‚ตใ‚คใ‚บใ‚’ๆœ€ๅคง8ๅ€ๅ‰Šๆธ›ใงใใ‚‹ใ“ใจใŒๆœŸๅพ…ใงใใพใ™ใ€‚ไปฅไธ‹ใ‚’็ขบ่ชใ—ใฆใ€ใฉใฎใ‚ˆใ†ใซๅง‹ใ‚ใ‚‹ใ‹ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +Note that this feature can also be used in a multi GPU setup. + +ใ“ใฎๆฉŸ่ƒฝใฏใ€ใƒžใƒซใƒGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + + + +### Requirements [[requirements-for-fp4-mixedprecision-inference]] + +- Latest `bitsandbytes` library +`pip install bitsandbytes>=0.39.0` + +- Install latest `accelerate` from source +`pip install git+https://github.com/huggingface/accelerate.git` + +- Install latest `transformers` from source +`pip install git+https://github.com/huggingface/transformers.git` + + +### Running FP4 models - single GPU setup - Quickstart + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใงใ€็ฐกๅ˜ใซๅ˜ไธ€ใฎGPUใงFP4ใƒขใƒ‡ใƒซใ‚’ๅฎŸ่กŒใงใใพใ™: + + +```py +from transformers import AutoModelForCausalLM + +model_name = "bigscience/bloom-2b5" +model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) +``` + +ๆณจๆ„: `device_map`ใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใŒใ€ๆŽจ่ซ–ๆ™‚ใซ `device_map = 'auto'` ใ‚’่จญๅฎšใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅˆฉ็”จๅฏ่ƒฝใชใƒชใ‚ฝใƒผใ‚นใซๅŠน็އ็š„ใซใƒขใƒ‡ใƒซใŒใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใ•ใ‚Œใพใ™ใ€‚ + +### Running FP4 models - multi GPU setup + +ๆททๅˆ4ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’่ค‡ๆ•ฐใฎGPUใซใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฏใ€ๅ˜ไธ€GPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใงใ™๏ผˆๅ˜ไธ€GPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใงใ™๏ผ‰๏ผš + +```py +model_name = "bigscience/bloom-2b5" +model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) +``` + +ใ—ใ‹ใ—ใ€`accelerate`ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ„GPUใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹GPU RAMใ‚’ๅˆถๅพกใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใ€`max_memory`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + + +```py +max_memory_mapping = {0: "600MB", 1: "1GB"} +model_name = "bigscience/bloom-3b" +model_4bit = AutoModelForCausalLM.from_pretrained( + model_name, device_map="auto", load_in_4bit=True, max_memory=max_memory_mapping +) +``` + +ใ“ใฎไพ‹ใงใฏใ€ๆœ€ๅˆใฎGPUใฏ600MBใฎใƒกใƒขใƒชใ‚’ไฝฟ็”จใ—ใ€2็•ช็›ฎใฎGPUใฏ1GBใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +### Advanced usage + +ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฎใ•ใ‚‰ใชใ‚‹้ซ˜ๅบฆใชไฝฟ็”จๆณ•ใซใคใ„ใฆใฏใ€[้‡ๅญๅŒ–](main_classes/quantization)ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒšใƒผใ‚ธใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## `bitsandbytes` integration for Int8 mixed-precision matrix decomposition + + + +ใ“ใฎๆฉŸ่ƒฝใฏใ€ใƒžใƒซใƒGPU็’ฐๅขƒใงใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ + + + +่ซ–ๆ–‡[`LLM.int8()๏ผšใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชTransformerๅ‘ใ‘ใฎ8ใƒ“ใƒƒใƒˆ่กŒๅˆ—ไน—็ฎ—`](https://huggingface.co/papers/2208.07339)ใซใ‚ˆใ‚Œใฐใ€Hugging Face็ตฑๅˆใŒHubๅ†…ใฎใ™ในใฆใฎใƒขใƒ‡ใƒซใงใ‚ใšใ‹ๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ๅŠ็ฒพๅบฆ๏ผˆ`float16`ใŠใ‚ˆใณ`bfloat16`๏ผ‰ใฎ้‡ใฟใฎๅ ดๅˆใซ`nn.Linear`ใ‚ตใ‚คใ‚บใ‚’2ๅ€ใ€ๅ˜็ฒพๅบฆ๏ผˆ`float32`๏ผ‰ใฎ้‡ใฟใฎๅ ดๅˆใฏ4ๅ€ใซ็ธฎๅฐใ—ใ€ๅค–ใ‚Œๅ€คใซๅฏพใ—ใฆใปใจใ‚“ใฉๅฝฑ้Ÿฟใ‚’ไธŽใˆใพใ›ใ‚“ใ€‚ + +![HFxbitsandbytes.png](https://cdn-uploads.huggingface.co/production/uploads/1659861207959-62441d1d9fdefb55a0b7d12c.png) + +Int8ๆททๅˆ็ฒพๅบฆ่กŒๅˆ—ๅˆ†่งฃใฏใ€่กŒๅˆ—ไน—็ฎ—ใ‚’2ใคใฎใ‚นใƒˆใƒชใƒผใƒ ใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅ‹•ไฝœใ—ใพใ™๏ผš(1) ใ‚ทใ‚นใƒ†ใƒžใƒ†ใ‚ฃใƒƒใ‚ฏใช็‰นๅพดๅค–ใ‚Œๅ€คใ‚นใƒˆใƒชใƒผใƒ ใŒfp16ใง่กŒๅˆ—ไน—็ฎ—๏ผˆ0.01%๏ผ‰ใ€(2) int8่กŒๅˆ—ไน—็ฎ—ใฎ้€šๅธธใฎใ‚นใƒˆใƒชใƒผใƒ ๏ผˆ99.9%๏ผ‰ใ€‚ใ“ใฎๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€้žๅธธใซๅคงใใชใƒขใƒ‡ใƒซใซๅฏพใ—ใฆไบˆๆธฌใฎๅŠฃๅŒ–ใชใ—ใซint8ๆŽจ่ซ–ใŒๅฏ่ƒฝใงใ™ใ€‚ +ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[่ซ–ๆ–‡](https://huggingface.co/papers/2208.07339)ใพใŸใฏ[ใ“ใฎ็ตฑๅˆใซ้–ขใ™ใ‚‹ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://huggingface.co/blog/hf-bitsandbytes-integration)ใ‚’ใ”็ขบ่ชใใ ใ•ใ„ใ€‚ + +![MixedInt8.gif](https://cdn-uploads.huggingface.co/production/uploads/1660567469965-62441d1d9fdefb55a0b7d12c.gif) + +ใชใŠใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏGPUใŒๅฟ…่ฆใงใ‚ใ‚Šใ€ใ‚ซใƒผใƒใƒซใฏGPUๅฐ‚็”จใซใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใƒขใƒ‡ใƒซใฎ1/4๏ผˆใพใŸใฏใƒใƒผใƒ•็ฒพๅบฆใฎ้‡ใฟใฎๅ ดๅˆใฏ1/2๏ผ‰ใ‚’ไฟๅญ˜ใ™ใ‚‹ใฎใซๅๅˆ†ใชGPUใƒกใƒขใƒชใŒใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ใ“ใฎใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ™ใ‚‹้š›ใฎใƒ˜ใƒซใƒ—ใซ้–ขใ™ใ‚‹่ฉณ็ดฐใฏใ€ไปฅไธ‹ใฎใƒŽใƒผใƒˆใ‚’ใ”่ฆงใ„ใŸใ ใใ‹ใ€[Google Colabใฎใƒ‡ใƒข](#colab-demos)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +### Requirements [[requirements-for-int8-mixedprecision-matrix-decomposition]] + +- `bitsandbytes<0.37.0`ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€NVIDIA GPUใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€8ใƒ“ใƒƒใƒˆใƒ†ใƒณใ‚ฝใƒซใ‚ณใ‚ขใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผˆTuringใ€Ampereใ€ใพใŸใฏใใ‚Œไปฅ้™ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใƒผใ€ไพ‹๏ผšT4ใ€RTX20s RTX30sใ€A40-A100ใชใฉ๏ผ‰ใ€‚`bitsandbytes>=0.37.0`ใฎๅ ดๅˆใ€ใ™ในใฆใฎGPUใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใ‚‹ใฏใšใงใ™ใ€‚ +- ๆญฃใ—ใ„ใƒใƒผใ‚ธใƒงใƒณใฎ`bitsandbytes`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš +`pip install bitsandbytes>=0.31.5` +- `accelerate`ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™๏ผš +`pip install accelerate>=0.12.0` + + +### Running mixed-Int8 models - single GPU setup + +ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅพŒใ€ใƒŸใƒƒใ‚ฏใ‚น 8 ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚€ๆ–นๆณ•ใฏๆฌกใฎ้€šใ‚Šใงใ™๏ผš + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +ไปฅไธ‹ใฏใ‚ทใƒณใƒ—ใƒซใชไพ‹ใงใ™๏ผš + +* `pipeline()` ้–ขๆ•ฐใฎไปฃใ‚ใ‚Šใซใ€ใƒขใƒ‡ใƒซใฎ `generate()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚`pipeline()` ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ™ใ‚‹ใ“ใจใฏๅฏ่ƒฝใงใ™ใŒใ€ๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใฆใŠใ‚‰ใšใ€`generate()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚้…ใใชใ‚Šใพใ™ใ€‚ใพใŸใ€ไธ€้ƒจใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐๆˆฆ็•ฅ๏ผˆไพ‹๏ผšใƒŒใ‚ฏใƒฌใ‚ฆใ‚นใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ๏ผ‰ใฏใ€`pipeline()` ้–ขๆ•ฐใงใฏๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใงใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ +* ใ™ในใฆใฎๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใจๅŒใ˜ใƒ‡ใƒใ‚คใ‚นใซ้…็ฝฎใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +tokenizer = AutoTokenizer.from_pretrained(model_name) +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) + +prompt = "Hello, my llama is cute" +inputs = tokenizer(prompt, return_tensors="pt").to("cuda") +generated_ids = model.generate(**inputs) +outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +``` + +### Running mixed-int8 models - multi GPU setup + +่ค‡ๆ•ฐใฎGPUใซๆททๅˆ8ใƒ“ใƒƒใƒˆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใฏใ€ๆฌกใฎ้€šใ‚Šใงใ™๏ผˆใ‚ทใƒณใ‚ฐใƒซGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใจๅŒใ˜ใ‚ณใƒžใƒณใƒ‰ใงใ™๏ผ‰๏ผš + +```py +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +`accelerate`ใ‚’ไฝฟ็”จใ—ใฆๅ„GPUใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹GPU RAMใ‚’ๅˆถๅพกใ™ใ‚‹้š›ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซ`max_memory`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + + +```py +max_memory_mapping = {0: "1GB", 1: "2GB"} +model_name = "bigscience/bloom-3b" +model_8bit = AutoModelForCausalLM.from_pretrained( + model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping +) +``` + +In this example, the first GPU will use 1GB of memory and the second 2GB. + +### Colab demos + +ใ“ใฎๆ–นๆณ•ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไปฅๅ‰ใฎGoogle ColabใงใฏๆŽจ่ซ–ใงใใชใ‹ใฃใŸใƒขใƒ‡ใƒซใซๅฏพใ—ใฆๆŽจ่ซ–ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™ใ€‚ไปฅไธ‹ใฏใ€Google Colabใง8ใƒ“ใƒƒใƒˆ้‡ๅญๅŒ–ใ‚’ไฝฟ็”จใ—ใฆT5-11b๏ผˆfp32ใง42GB๏ผ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใƒ‡ใƒขใฎใƒชใƒณใ‚ฏใงใ™๏ผš + +[![Open In Colab: T5-11b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing) + +ใพใŸใ€BLOOM-3Bใฎใƒ‡ใƒขใ‚‚ใ”่ฆงใ„ใŸใ ใ‘ใพใ™๏ผš + +[![Open In Colab: BLOOM-3b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) + +## Advanced usage: mixing FP4 (or Int8) and BetterTransformer + +็•ฐใชใ‚‹ๆ–นๆณ•ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ€ใƒขใƒ‡ใƒซใฎๆœ€้ฉใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€BetterTransformerใ‚’ไฝฟ็”จใ—ใฆFP4ใƒŸใƒƒใ‚ฏใ‚นใƒ—ใƒฌใ‚ทใ‚ธใƒงใƒณๆŽจ่ซ–ใจใƒ•ใƒฉใƒƒใ‚ทใƒฅใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```py +import torch +from torch.nn.attention import SDPBackend, sdpa_kernel +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_compute_dtype=torch.float16 +) + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m") +model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m", quantization_config=quantization_config) + +input_text = "Hello my dog is cute and" +inputs = tokenizer(input_text, return_tensors="pt").to("cuda") + +with sdpa_kernel(SDPBackend.FLASH_ATTENTION): + outputs = model.generate(**inputs) + +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_special.md new file mode 100644 index 0000000000000000000000000000000000000000..a5102590a0504cf20b9bbdc31312c6ff200ec9e7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_infer_special.md @@ -0,0 +1,18 @@ + + +# Inference on Specialized Hardware + +ใ“ใกใ‚‰ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใ€ๅฐ‚็”จใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใฎๆŽจ่ซ–ๆ–นๆณ•ใซใคใ„ใฆใฎๆƒ…ๅ ฑใŒใพใ‚‚ใชใๆไพ›ใ•ใ‚Œใพใ™ใ€‚ใใฎ้–“ใซใ€CPUใงใฎๆŽจ่ซ–ใซ้–ขใ™ใ‚‹ใ‚ฌใ‚คใƒ‰ใ‚’ใ”่ฆงใ„ใŸใ ใ‘ใพใ™ใ€‚[the guide for inference on CPUs](perf_infer_cpu). \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_torch_compile.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_torch_compile.md new file mode 100644 index 0000000000000000000000000000000000000000..e2f0acebd6c4f60d319ce30711f415f8e6f3dd6a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_torch_compile.md @@ -0,0 +1,359 @@ + + +# Optimize inference using torch.compile() + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ€[`torch.compile()`](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html) ใ‚’ไฝฟ็”จใ—ใŸๆŽจ่ซ–้€Ÿๅบฆใฎๅ‘ไธŠใซ้–ขใ™ใ‚‹ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใ€[๐Ÿค— Transformers ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซ](https://huggingface.co/models?pipeline_tag=image-classification&library=transformers&sort=trending)ๅ‘ใ‘ใฎใ‚‚ใฎใงใ™ใ€‚ + +## Benefits of torch.compile + +`torch.compile()`ใฎๅˆฉ็‚น +ใƒขใƒ‡ใƒซใจGPUใซใ‚ˆใฃใฆใฏใ€torch.compile()ใฏๆŽจ่ซ–ๆ™‚ใซๆœ€ๅคง30%ใฎ้ซ˜้€ŸๅŒ–ใ‚’ๅฎŸ็พใ—ใพใ™ใ€‚ `torch.compile()`ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒใƒผใ‚ธใƒงใƒณ2.0ไปฅไธŠใฎtorchใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ + +ใƒขใƒ‡ใƒซใฎใ‚ณใƒณใƒ‘ใ‚คใƒซใซใฏๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ใŸใ‚ใ€ๆฏŽๅ›žๆŽจ่ซ–ใ™ใ‚‹ใฎใงใฏใชใใ€ใƒขใƒ‡ใƒซใ‚’1ๅบฆใ ใ‘ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ๅ ดๅˆใซๅฝน็ซ‹ใกใพใ™ใ€‚ +ไปปๆ„ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซ`torch.compile()`ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™๏ผš + +```diff +from transformers import AutoModelForImageClassification + +model = AutoModelForImageClassification.from_pretrained(MODEL_ID, device_map="auto") ++ model = torch.compile(model) +``` + +`compile()` ใฏใ€ใ‚ณใƒณใƒ‘ใ‚คใƒซใซ้–ขใ™ใ‚‹็•ฐใชใ‚‹ใƒขใƒผใƒ‰ใ‚’ๅ‚™ใˆใฆใŠใ‚Šใ€ๅŸบๆœฌ็š„ใซใฏใ‚ณใƒณใƒ‘ใ‚คใƒซๆ™‚้–“ใจๆŽจ่ซ–ใฎใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒ็•ฐใชใ‚Šใพใ™ใ€‚`max-autotune` ใฏ `reduce-overhead` ใ‚ˆใ‚Šใ‚‚ๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใŒใ€ๆŽจ่ซ–้€ŸๅบฆใŒ้€Ÿใใชใ‚Šใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒขใƒผใƒ‰ใฏใ‚ณใƒณใƒ‘ใ‚คใƒซใซใŠใ„ใฆใฏๆœ€้€Ÿใงใ™ใŒใ€ๆŽจ่ซ–ๆ™‚้–“ใซใŠใ„ใฆใฏ `reduce-overhead` ใซๆฏ”ในใฆๅŠน็އใŒ่‰ฏใใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒขใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ—ใŸใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใกใ‚‰](https://pytorch.org/get-started/pytorch-2.0/#user-experience) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +`torch` ใƒใƒผใ‚ธใƒงใƒณ 2.0.1 ใง็•ฐใชใ‚‹ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใ€ใ‚ฟใ‚นใ‚ฏใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใฎ็จฎ้กžใ€ใŠใ‚ˆใณใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ—ใฆ `torch.compile` ใ‚’ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ—ใพใ—ใŸใ€‚ + +## Benchmarking code + +ไปฅไธ‹ใซใ€ๅ„ใ‚ฟใ‚นใ‚ฏใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚ณใƒผใƒ‰ใ‚’็คบใ—ใพใ™ใ€‚ๆŽจ่ซ–ๅ‰ใซGPUใ‚’ใ‚ฆใ‚ฉใƒผใƒ ใ‚ขใƒƒใƒ—ใ—ใ€ๆฏŽๅ›žๅŒใ˜็”ปๅƒใ‚’ไฝฟ็”จใ—ใฆ300ๅ›žใฎๆŽจ่ซ–ใฎๅนณๅ‡ๆ™‚้–“ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +### Image Classification with ViT + +```python +from PIL import Image +import requests +import numpy as np +from transformers import AutoImageProcessor, AutoModelForImageClassification + +url = 'http://images.cocodataset.org/val2017/000000039769.jpg' +image = Image.open(requests.get(url, stream=True).raw) + +processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224", device_map="auto") +model = torch.compile(model) + +processed_input = processor(image, return_tensors='pt').to(model.device) + +with torch.no_grad(): + _ = model(**processed_input) +``` + +#### Object Detection with DETR + +```python +from transformers import AutoImageProcessor, AutoModelForObjectDetection + +processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50") +model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50", device_map="auto") +model = torch.compile(model) + +texts = ["a photo of a cat", "a photo of a dog"] +inputs = processor(text=texts, images=image, return_tensors="pt").to(model.device) + +with torch.no_grad(): + _ = model(**inputs) +``` + +#### Image Segmentation with Segformer + +```python +from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation + +processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512") +model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512", device_map="auto") +model = torch.compile(model) +seg_inputs = processor(images=image, return_tensors="pt").to(model.device) + +with torch.no_grad(): + _ = model(**seg_inputs) +``` + +ไปฅไธ‹ใฏใ€็งใŸใกใŒใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’่กŒใฃใŸใƒขใƒ‡ใƒซใฎใƒชใ‚นใƒˆใงใ™ใ€‚ + + +**Image Classification** +- [google/vit-base-patch16-224](https://huggingface.co/google/vit-base-patch16-224) +- [microsoft/beit-base-patch16-224-pt22k-ft22k](https://huggingface.co/microsoft/beit-base-patch16-224-pt22k-ft22k) +- [facebook/convnext-large-224](https://huggingface.co/facebook/convnext-large-224) +- [microsoft/resnet-50](https://huggingface.co/) + +**Image Segmentation** +- [nvidia/segformer-b0-finetuned-ade-512-512](https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512) +- [facebook/mask2former-swin-tiny-coco-panoptic](https://huggingface.co/facebook/mask2former-swin-tiny-coco-panoptic) +- [facebook/maskformer-swin-base-ade](https://huggingface.co/facebook/maskformer-swin-base-ade) +- [google/deeplabv3_mobilenet_v2_1.0_513](https://huggingface.co/google/deeplabv3_mobilenet_v2_1.0_513) + +**Object Detection** +- [google/owlvit-base-patch32](https://huggingface.co/google/owlvit-base-patch32) +- [facebook/detr-resnet-101](https://huggingface.co/facebook/detr-resnet-101) +- [microsoft/conditional-detr-resnet-50](https://huggingface.co/microsoft/conditional-detr-resnet-50) + + +ไปฅไธ‹ใฏใ€`torch.compile()`ใ‚’ไฝฟ็”จใ—ใŸๅ ดๅˆใจไฝฟ็”จใ—ใชใ„ๅ ดๅˆใฎๆŽจ่ซ–ๆ™‚้–“ใฎๅฏ่ฆ–ๅŒ–ใจใ€็•ฐใชใ‚‹ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใจใƒใƒƒใƒใ‚ตใ‚คใ‚บใฎๅ„ใƒขใƒ‡ใƒซใซๅฏพใ™ใ‚‹ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๅ‘ไธŠใฎๅ‰ฒๅˆใงใ™ใ€‚ + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +![Duration Comparison on V100 with Batch Size of 1](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/torch_compile/v100_1_duration.png) + +![Percentage Improvement on T4 with Batch Size of 4](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/torch_compile/T4_4_percentage.png) + +ไธ‹่จ˜ใฏใ€ๅ„ใƒขใƒ‡ใƒซใซใคใ„ใฆ`compile()`ใ‚’ไฝฟ็”จใ—ใŸๅ ดๅˆใจไฝฟ็”จใ—ใชใ‹ใฃใŸๅ ดๅˆใฎๆŽจ่ซ–ๆ™‚้–“๏ผˆใƒŸใƒช็ง’ๅ˜ไฝ๏ผ‰ใงใ™ใ€‚ใชใŠใ€OwlViTใฏๅคงใใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใงใฎไฝฟ็”จๆ™‚ใซใƒกใƒขใƒชไธ่ถณ๏ผˆOOM๏ผ‰ใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +### A100 (batch size: 1) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 9.325 | 7.584 | +| Image Segmentation/Segformer | 11.759 | 10.500 | +| Object Detection/OwlViT | 24.978 | 18.420 | +| Image Classification/BeiT | 11.282 | 8.448 | +| Object Detection/DETR | 34.619 | 19.040 | +| Image Classification/ConvNeXT | 10.410 | 10.208 | +| Image Classification/ResNet | 6.531 | 4.124 | +| Image Segmentation/Mask2former | 60.188 | 49.117 | +| Image Segmentation/Maskformer | 75.764 | 59.487 | +| Image Segmentation/MobileNet | 8.583 | 3.974 | +| Object Detection/Resnet-101 | 36.276 | 18.197 | +| Object Detection/Conditional-DETR | 31.219 | 17.993 | + + +### A100 (batch size: 4) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 14.832 | 14.499 | +| Image Segmentation/Segformer | 18.838 | 16.476 | +| Image Classification/BeiT | 13.205 | 13.048 | +| Object Detection/DETR | 48.657 | 32.418| +| Image Classification/ConvNeXT | 22.940 | 21.631 | +| Image Classification/ResNet | 6.657 | 4.268 | +| Image Segmentation/Mask2former | 74.277 | 61.781 | +| Image Segmentation/Maskformer | 180.700 | 159.116 | +| Image Segmentation/MobileNet | 14.174 | 8.515 | +| Object Detection/Resnet-101 | 68.101 | 44.998 | +| Object Detection/Conditional-DETR | 56.470 | 35.552 | + +### A100 (batch size: 16) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 40.944 | 40.010 | +| Image Segmentation/Segformer | 37.005 | 31.144 | +| Image Classification/BeiT | 41.854 | 41.048 | +| Object Detection/DETR | 164.382 | 161.902 | +| Image Classification/ConvNeXT | 82.258 | 75.561 | +| Image Classification/ResNet | 7.018 | 5.024 | +| Image Segmentation/Mask2former | 178.945 | 154.814 | +| Image Segmentation/Maskformer | 638.570 | 579.826 | +| Image Segmentation/MobileNet | 51.693 | 30.310 | +| Object Detection/Resnet-101 | 232.887 | 155.021 | +| Object Detection/Conditional-DETR | 180.491 | 124.032 | + +### V100 (batch size: 1) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 10.495 | 6.00 | +| Image Segmentation/Segformer | 13.321 | 5.862 | +| Object Detection/OwlViT | 25.769 | 22.395 | +| Image Classification/BeiT | 11.347 | 7.234 | +| Object Detection/DETR | 33.951 | 19.388 | +| Image Classification/ConvNeXT | 11.623 | 10.412 | +| Image Classification/ResNet | 6.484 | 3.820 | +| Image Segmentation/Mask2former | 64.640 | 49.873 | +| Image Segmentation/Maskformer | 95.532 | 72.207 | +| Image Segmentation/MobileNet | 9.217 | 4.753 | +| Object Detection/Resnet-101 | 52.818 | 28.367 | +| Object Detection/Conditional-DETR | 39.512 | 20.816 | + +### V100 (batch size: 4) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 15.181 | 14.501 | +| Image Segmentation/Segformer | 16.787 | 16.188 | +| Image Classification/BeiT | 15.171 | 14.753 | +| Object Detection/DETR | 88.529 | 64.195 | +| Image Classification/ConvNeXT | 29.574 | 27.085 | +| Image Classification/ResNet | 6.109 | 4.731 | +| Image Segmentation/Mask2former | 90.402 | 76.926 | +| Image Segmentation/Maskformer | 234.261 | 205.456 | +| Image Segmentation/MobileNet | 24.623 | 14.816 | +| Object Detection/Resnet-101 | 134.672 | 101.304 | +| Object Detection/Conditional-DETR | 97.464 | 69.739 | + +### V100 (batch size: 16) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 52.209 | 51.633 | +| Image Segmentation/Segformer | 61.013 | 55.499 | +| Image Classification/BeiT | 53.938 | 53.581 | +| Object Detection/DETR | OOM | OOM | +| Image Classification/ConvNeXT | 109.682 | 100.771 | +| Image Classification/ResNet | 14.857 | 12.089 | +| Image Segmentation/Mask2former | 249.605 | 222.801 | +| Image Segmentation/Maskformer | 831.142 | 743.645 | +| Image Segmentation/MobileNet | 93.129 | 55.365 | +| Object Detection/Resnet-101 | 482.425 | 361.843 | +| Object Detection/Conditional-DETR | 344.661 | 255.298 | + +### T4 (batch size: 1) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 16.520 | 15.786 | +| Image Segmentation/Segformer | 16.116 | 14.205 | +| Object Detection/OwlViT | 53.634 | 51.105 | +| Image Classification/BeiT | 16.464 | 15.710 | +| Object Detection/DETR | 73.100 | 53.99 | +| Image Classification/ConvNeXT | 32.932 | 30.845 | +| Image Classification/ResNet | 6.031 | 4.321 | +| Image Segmentation/Mask2former | 79.192 | 66.815 | +| Image Segmentation/Maskformer | 200.026 | 188.268 | +| Image Segmentation/MobileNet | 18.908 | 11.997 | +| Object Detection/Resnet-101 | 106.622 | 82.566 | +| Object Detection/Conditional-DETR | 77.594 | 56.984 | + +### T4 (batch size: 4) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 43.653 | 43.626 | +| Image Segmentation/Segformer | 45.327 | 42.445 | +| Image Classification/BeiT | 52.007 | 51.354 | +| Object Detection/DETR | 277.850 | 268.003 | +| Image Classification/ConvNeXT | 119.259 | 105.580 | +| Image Classification/ResNet | 13.039 | 11.388 | +| Image Segmentation/Mask2former | 201.540 | 184.670 | +| Image Segmentation/Maskformer | 764.052 | 711.280 | +| Image Segmentation/MobileNet | 74.289 | 48.677 | +| Object Detection/Resnet-101 | 421.859 | 357.614 | +| Object Detection/Conditional-DETR | 289.002 | 226.945 | + +### T4 (batch size: 16) + +| **Task/Model** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:| +| Image Classification/ViT | 163.914 | 160.907 | +| Image Segmentation/Segformer | 192.412 | 163.620 | +| Image Classification/BeiT | 188.978 | 187.976 | +| Object Detection/DETR | OOM | OOM | +| Image Classification/ConvNeXT | 422.886 | 388.078 | +| Image Classification/ResNet | 44.114 | 37.604 | +| Image Segmentation/Mask2former | 756.337 | 695.291 | +| Image Segmentation/Maskformer | 2842.940 | 2656.88 | +| Image Segmentation/MobileNet | 299.003 | 201.942 | +| Object Detection/Resnet-101 | 1619.505 | 1262.758 | +| Object Detection/Conditional-DETR | 1137.513 | 897.390| + +## PyTorch Nightly +ใพใŸใ€PyTorchใฎใƒŠใ‚คใƒˆใƒชใƒผใƒใƒผใ‚ธใƒงใƒณ๏ผˆ2.1.0dev๏ผ‰ใงใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’่กŒใ„ใ€ใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚Œใฆใ„ใชใ„ใƒขใƒ‡ใƒซใจใ‚ณใƒณใƒ‘ใ‚คใƒซๆธˆใฟใƒขใƒ‡ใƒซใฎไธกๆ–นใงใƒฌใ‚คใƒ†ใƒณใ‚ทใƒผใฎๅ‘ไธŠใ‚’่ฆณๅฏŸใ—ใพใ—ใŸใ€‚ใƒ›ใ‚คใƒผใƒซใฏ[ใ“ใกใ‚‰](https://download.pytorch.org/whl/nightly/cu118)ใ‹ใ‚‰ๅ…ฅๆ‰‹ใงใใพใ™ใ€‚ + + +### A100 + +| **Task/Model** | **Batch Size** | **torch 2.0 - no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:|:---:| +| Image Classification/BeiT | Unbatched | 12.462 | 6.954 | +| Image Classification/BeiT | 4 | 14.109 | 12.851 | +| Image Classification/BeiT | 16 | 42.179 | 42.147 | +| Object Detection/DETR | Unbatched | 30.484 | 15.221 | +| Object Detection/DETR | 4 | 46.816 | 30.942 | +| Object Detection/DETR | 16 | 163.749 | 163.706 | + +### T4 + +| **Task/Model** | **Batch Size** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:|:---:| +| Image Classification/BeiT | Unbatched | 14.408 | 14.052 | +| Image Classification/BeiT | 4 | 47.381 | 46.604 | +| Image Classification/BeiT | 16 | 42.179 | 42.147 | +| Object Detection/DETR | Unbatched | 68.382 | 53.481 | +| Object Detection/DETR | 4 | 269.615 | 204.785 | +| Object Detection/DETR | 16 | OOM | OOM | + +### V100 + +| **Task/Model** | **Batch Size** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:|:---:| +| Image Classification/BeiT | Unbatched | 13.477 | 7.926 | +| Image Classification/BeiT | 4 | 15.103 | 14.378 | +| Image Classification/BeiT | 16 | 52.517 | 51.691 | +| Object Detection/DETR | Unbatched | 28.706 | 19.077 | +| Object Detection/DETR | 4 | 88.402 | 62.949| +| Object Detection/DETR | 16 | OOM | OOM | + + +## Reduce Overhead +Nightlyใƒ“ใƒซใƒ‰ใงA100ใŠใ‚ˆใณT4ๅ‘ใ‘ใฎ `reduce-overhead` ใ‚ณใƒณใƒ‘ใ‚คใƒซใƒขใƒผใƒ‰ใ‚’ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ—ใพใ—ใŸใ€‚ + +### A100 + +| **Task/Model** | **Batch Size** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:|:---:| +| Image Classification/ConvNeXT | Unbatched | 11.758 | 7.335 | +| Image Classification/ConvNeXT | 4 | 23.171 | 21.490 | +| Image Classification/ResNet | Unbatched | 7.435 | 3.801 | +| Image Classification/ResNet | 4 | 7.261 | 2.187 | +| Object Detection/Conditional-DETR | Unbatched | 32.823 | 11.627 | +| Object Detection/Conditional-DETR | 4 | 50.622 | 33.831 | +| Image Segmentation/MobileNet | Unbatched | 9.869 | 4.244 | +| Image Segmentation/MobileNet | 4 | 14.385 | 7.946 | + + +### T4 + +| **Task/Model** | **Batch Size** | **torch 2.0 -
no compile** | **torch 2.0 -
compile** | +|:---:|:---:|:---:|:---:| +| Image Classification/ConvNeXT | Unbatched | 32.137 | 31.84 | +| Image Classification/ConvNeXT | 4 | 120.944 | 110.209 | +| Image Classification/ResNet | Unbatched | 9.761 | 7.698 | +| Image Classification/ResNet | 4 | 15.215 | 13.871 | +| Object Detection/Conditional-DETR | Unbatched | 72.150 | 57.660 | +| Object Detection/Conditional-DETR | 4 | 301.494 | 247.543 | +| Image Segmentation/MobileNet | Unbatched | 22.266 | 19.339 | +| Image Segmentation/MobileNet | 4 | 78.311 | 50.983 | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..bf623d131363b56682b778f9703ebe9cddde196f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu.md @@ -0,0 +1,67 @@ + + +# Efficient Training on CPU + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ€CPUไธŠใงๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ + +## Mixed precision with IPEX + +IPEXใฏAVX-512ไปฅไธŠใฎCPUใซๆœ€้ฉๅŒ–ใ•ใ‚ŒใฆใŠใ‚Šใ€AVX2ใฎใฟใฎCPUใงใ‚‚ๆฉŸ่ƒฝ็š„ใซๅ‹•ไฝœใ—ใพใ™ใ€‚ใใฎใŸใ‚ใ€AVX-512ไปฅไธŠใฎIntel CPUไธ–ไปฃใงใฏใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ‘ไธŠใŒๆœŸๅพ…ใ•ใ‚Œใพใ™ใŒใ€AVX2ใฎใฟใฎCPU๏ผˆไพ‹๏ผšAMD CPUใพใŸใฏๅคใ„Intel CPU๏ผ‰ใงใฏIPEXใฎไธ‹ใงใ‚ˆใ‚Š่‰ฏใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅพ—ใ‚‰ใ‚Œใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ไฟ่จผใ•ใ‚Œใพใ›ใ‚“ใ€‚IPEXใฏใ€Float32ใจBFloat16ใฎไธกๆ–นใงCPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๆœ€้ฉๅŒ–ใ—ใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€BFloat16ใฎไฝฟ็”จใซ้‡็‚นใ‚’็ฝฎใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ไฝŽ็ฒพๅบฆใƒ‡ใƒผใ‚ฟๅž‹ใงใ‚ใ‚‹BFloat16ใฏใ€AVX512ๅ‘ฝไปคใ‚ปใƒƒใƒˆใ‚’ๅ‚™ใˆใŸ็ฌฌ3ไธ–ไปฃXeonยฎ Scalable Processors๏ผˆๅˆฅๅCooper Lake๏ผ‰ใงใƒใ‚คใƒ†ใ‚ฃใƒ–ใ‚ตใƒใƒผใƒˆใ•ใ‚ŒใฆใŠใ‚Šใ€ใ•ใ‚‰ใซ้ซ˜ๆ€ง่ƒฝใชIntelยฎ Advanced Matrix Extensions๏ผˆIntelยฎ AMX๏ผ‰ๅ‘ฝไปคใ‚ปใƒƒใƒˆใ‚’ๅ‚™ใˆใŸๆฌกไธ–ไปฃใฎIntelยฎ Xeonยฎ Scalable Processorsใงใ‚‚ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใพใ™ใ€‚CPUใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰็”จใฎ่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใŒPyTorch-1.10ไปฅ้™ใงๆœ‰ๅŠนใซใชใฃใฆใ„ใพใ™ใ€‚ๅŒๆ™‚ใซใ€Intelยฎ Extension for PyTorchใงใฎCPU็”จBFloat16ใฎ่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใ‚ตใƒใƒผใƒˆใจใ€ใ‚ชใƒšใƒฌใƒผใ‚ฟใƒผใฎBFloat16ๆœ€้ฉๅŒ–ใฎใ‚ตใƒใƒผใƒˆใŒๅคงๅน…ใซๅ‘ไธŠใ—ใ€ไธ€้ƒจใŒPyTorchใฎใƒกใ‚คใƒณใƒ–ใƒฉใƒณใƒใซใ‚ขใƒƒใƒ—ใ‚นใƒˆใƒชใƒผใƒ ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใƒฆใƒผใ‚ถใƒผใฏIPEX Auto Mixed Precisionใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€ใ‚ˆใ‚Šๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใจใƒฆใƒผใ‚ถใƒผใ‚จใ‚ฏใ‚นใƒšใƒชใ‚จใƒณใ‚นใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +่ฉณ็ดฐใชๆƒ…ๅ ฑใซใคใ„ใฆใฏใ€[Auto Mixed Precision](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/amp.html)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### IPEX installation: + +IPEXใฎใƒชใƒชใƒผใ‚นใฏPyTorchใซๅพ“ใฃใฆใŠใ‚Šใ€pipใ‚’ไฝฟ็”จใ—ใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™๏ผš + +| PyTorch Version | IPEX version | +| :---------------: | :----------: | +| 1.13 | 1.13.0+cpu | +| 1.12 | 1.12.300+cpu | +| 1.11 | 1.11.200+cpu | +| 1.10 | 1.10.100+cpu | + +```bash +pip install intel_extension_for_pytorch== -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +[IPEXใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ–นๆณ•](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/installation.html)ใซใคใ„ใฆใ€ใ•ใ‚‰ใชใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### Trainerใงใฎไฝฟ็”จๆ–นๆณ• +TrainerใงIPEXใฎ่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ใƒฆใƒผใ‚ถใƒผใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใƒžใƒณใƒ‰ๅผ•ๆ•ฐใซ `use_ipex`ใ€`bf16`ใ€ใŠใ‚ˆใณ `no_cuda` ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +[Transformersใฎ่ณชๅ•ๅฟœ็ญ”](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)ใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใ‚’ไพ‹ใซ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +- CPUไธŠใงBF16่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใ—ใฆIPEXใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ†ๅ ดๅˆ๏ผš +
 python run_qa.py \
+--model_name_or_path google-bert/bert-base-uncased \
+--dataset_name squad \
+--do_train \
+--do_eval \
+--per_device_train_batch_size 12 \
+--learning_rate 3e-5 \
+--num_train_epochs 2 \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/debug_squad/ \
+--use_ipex \
+--bf16 --no_cuda
+ +### Practice example + +Blog: [Accelerating PyTorch Transformers with Intel Sapphire Rapids](https://huggingface.co/blog/intel-sapphire-rapids) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..26da32f577251ff437ab0978ad4083e01ba17781 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_cpu_many.md @@ -0,0 +1,151 @@ + + + +# Efficient Training on Multiple CPUs + +1ใคใฎCPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ้…ใ™ใŽใ‚‹ๅ ดๅˆใ€่ค‡ๆ•ฐใฎCPUใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ€PyTorchใƒ™ใƒผใ‚นใฎDDPใ‚’ไฝฟ็”จใ—ใŸๅˆ†ๆ•ฃCPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ + +## Intelยฎ oneCCL Bindings for PyTorch + +[Intelยฎ oneCCL](https://github.com/oneapi-src/oneCCL)๏ผˆ้›†ๅˆ้€šไฟกใƒฉใ‚คใƒ–ใƒฉใƒช๏ผ‰ใฏใ€allreduceใ€allgatherใ€alltoallใชใฉใฎๅŽ้›†้€šไฟกใ‚’ๅฎŸ่ฃ…ใ—ใŸๅŠน็އ็š„ใชๅˆ†ๆ•ฃใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒฉใ‚คใƒ–ใƒฉใƒชใงใ™ใ€‚oneCCLใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[oneCCLใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)ใจ[oneCCLไป•ๆง˜](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ใƒขใ‚ธใƒฅใƒผใƒซ`oneccl_bindings_for_pytorch`๏ผˆใƒใƒผใ‚ธใƒงใƒณ1.12ไปฅๅ‰ใฏ`torch_ccl`๏ผ‰ใฏใ€PyTorch C10D ProcessGroup APIใ‚’ๅฎŸ่ฃ…ใ—ใ€ๅค–้ƒจใฎProcessGroupใจใ—ใฆๅ‹•็š„ใซใƒญใƒผใƒ‰ใงใใ€็พๅœจใฏLinuxใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ใงใฎใฟๅ‹•ไฝœใ—ใพใ™ใ€‚ + +[torch-ccl](https://github.com/intel/torch-ccl)ใฎ่ฉณ็ดฐๆƒ…ๅ ฑใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### Intelยฎ oneCCL Bindings for PyTorch installation: + +Wheelใƒ•ใ‚กใ‚คใƒซใฏใ€ไปฅไธ‹ใฎPythonใƒใƒผใ‚ธใƒงใƒณ็”จใซๅˆฉ็”จๅฏ่ƒฝใงใ™: + +| Extension Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | Python 3.10 | +| :---------------: | :--------: | :--------: | :--------: | :--------: | :---------: | +| 1.13.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.100 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.11.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.10.0 | โˆš | โˆš | โˆš | โˆš | | + +```bash +pip install oneccl_bind_pt=={pytorch_version} -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +where `{pytorch_version}` should be your PyTorch version, for instance 1.13.0. +Check more approaches for [oneccl_bind_pt installation](https://github.com/intel/torch-ccl). +Versions of oneCCL and PyTorch must match. + + + +oneccl_bindings_for_pytorch 1.12.0 prebuilt wheel does not work with PyTorch 1.12.1 (it is for PyTorch 1.12.0) +PyTorch 1.12.1 should work with oneccl_bindings_for_pytorch 1.12.100 + + + +`{pytorch_version}` ใฏใ€ใ‚ใชใŸใฎPyTorchใฎใƒใƒผใ‚ธใƒงใƒณ๏ผˆไพ‹๏ผš1.13.0๏ผ‰ใซ็ฝฎใๆ›ใˆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚้‡่ฆใชใฎใฏใ€oneCCLใจPyTorchใฎใƒใƒผใ‚ธใƒงใƒณใŒไธ€่‡ดใ—ใฆใ„ใ‚‹ใ“ใจใงใ™ใ€‚[oneccl_bind_ptใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ](https://github.com/intel/torch-ccl)ใซ้–ขใ™ใ‚‹ใ•ใ‚‰ใชใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใ‚’็ขบ่ชใงใใพใ™ใ€‚ + + + +`oneccl_bindings_for_pytorch`ใฎ1.12.0ใƒ—ใƒชใƒ“ใƒซใƒˆใƒ›ใ‚คใƒผใƒซใฏPyTorch 1.12.1ใจไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ›ใ‚“๏ผˆใ“ใ‚ŒใฏPyTorch 1.12.0็”จใงใ™๏ผ‰ใ€‚PyTorch 1.12.1ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€`oneccl_bindings_for_pytorch`ใƒใƒผใ‚ธใƒงใƒณ1.12.100ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +## Intelยฎ MPI library + + +ใ“ใฎๅŸบๆบ–ใƒ™ใƒผใ‚นใฎMPIๅฎŸ่ฃ…ใ‚’ไฝฟ็”จใ—ใฆใ€Intelยฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃไธŠใงๆŸ”่ปŸใงๅŠน็އ็š„ใ€ใ‚นใ‚ฑใƒผใƒฉใƒ–ใƒซใชใ‚ฏใƒฉใ‚นใ‚ฟใƒกใƒƒใ‚ปใƒผใ‚ธใƒณใ‚ฐใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏใ€Intelยฎ oneAPI HPC Toolkitใฎไธ€้ƒจใงใ™ใ€‚ + +oneccl_bindings_for_pytorchใฏMPIใƒ„ใƒผใƒซใ‚ปใƒƒใƒˆใจไธ€็ท’ใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใพใ™ใ€‚ไฝฟ็”จใ™ใ‚‹ๅ‰ใซ็’ฐๅขƒใ‚’ใ‚ฝใƒผใ‚นๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +for Intelยฎ oneCCL >= 1.12.0 +```bash +oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") +source $oneccl_bindings_for_pytorch_path/env/setvars.sh +``` + +for Intelยฎ oneCCL whose version < 1.12.0 +```bash +torch_ccl_path=$(python -c "import torch; import torch_ccl; import os; print(os.path.abspath(os.path.dirname(torch_ccl.__file__)))") +source $torch_ccl_path/env/setvars.sh +``` + +#### IPEX installation: + +IPEXใฏใ€Float32ใŠใ‚ˆใณBFloat16ใฎไธกๆ–นใงCPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใ‚’ๆไพ›ใ—ใพใ™ใ€‚่ฉณ็ดฐใฏ[ใ“ใกใ‚‰ใฎใ‚ทใƒณใ‚ฐใƒซCPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](./perf_train_cpu)ใ‚’ใ”ๅ‚็…งใใ ใ•ใ„ใ€‚ + +ไปฅไธ‹ใฎใ€ŒใƒˆใƒฌใƒผใƒŠใƒผใงใฎไฝฟ็”จใ€ใฏใ€Intelยฎ MPIใƒฉใ‚คใƒ–ใƒฉใƒชใงmpirunใ‚’ไฝฟ็”จใ™ใ‚‹ไพ‹ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +## Usage in Trainer +ใƒˆใƒฌใƒผใƒŠใƒผใงใฎใƒžใƒซใƒCPUๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใŸใ‚ใซใ€ใƒฆใƒผใ‚ถใƒผใฏใ‚ณใƒžใƒณใƒ‰ๅผ•ๆ•ฐใซ **`--ddp_backend ccl`** ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚[่ณชๅ•ๅฟœ็ญ”ใฎไพ‹](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering) + +ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใฏใ€1ใคใฎXeonใƒŽใƒผใƒ‰ใง2ใคใฎใƒ—ใƒญใ‚ปใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚1ใคใฎใƒ—ใƒญใ‚ปใ‚นใŒ1ใคใฎใ‚ฝใ‚ฑใƒƒใƒˆใงๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚OMP_NUM_THREADS/CCL_WORKER_COUNTๅค‰ๆ•ฐใฏใ€ๆœ€้ฉใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใซ่ชฟๆ•ดใงใใพใ™ใ€‚ + + +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=127.0.0.1 + mpirun -n 2 -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex +``` + +ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใฏใ€2ใคใฎXeonใƒ—ใƒญใ‚ปใƒƒใ‚ต๏ผˆnode0ใจnode1ใ€node0ใ‚’ใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚นใจใ—ใฆไฝฟ็”จ๏ผ‰ใงๅˆ่จˆ4ใคใฎใƒ—ใƒญใ‚ปใ‚นใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ppn๏ผˆใƒŽใƒผใƒ‰ใ”ใจใฎใƒ—ใƒญใ‚ปใ‚นๆ•ฐ๏ผ‰ใฏ2ใซ่จญๅฎšใ•ใ‚Œใ€1ใคใฎใ‚ฝใ‚ฑใƒƒใƒˆใ”ใจใซ1ใคใฎใƒ—ใƒญใ‚ปใ‚นใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ๆœ€้ฉใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใ€OMP_NUM_THREADS/CCL_WORKER_COUNTๅค‰ๆ•ฐใ‚’่ชฟๆ•ดใงใใพใ™ใ€‚ + +node0ใงใฏใ€ๅ„ใƒŽใƒผใƒ‰ใฎIPใ‚ขใƒ‰ใƒฌใ‚นใ‚’ๅซใ‚€ๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใ€ใใฎๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚นใ‚’ๅผ•ๆ•ฐใจใ—ใฆๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```shell script + cat hostfile + xxx.xxx.xxx.xxx #node0 ip + xxx.xxx.xxx.xxx #node1 ip +``` + +ใƒŽใƒผใƒ‰0ใงๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€ใƒŽใƒผใƒ‰0ใจใƒŽใƒผใƒ‰1ใง**4DDP**ใŒBF16่‡ชๅ‹•ๆททๅˆ็ฒพๅบฆใงๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ + + +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip + mpirun -f hostfile -n 4 -ppn 2 \ + -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex \ + --bf16 +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..6721ba69a92521147026c1f11357b44afabc8567 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_many.md @@ -0,0 +1,529 @@ + + +# Efficient Training on Multiple GPUs + +ๅ˜ไธ€ใฎGPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ้…ใ™ใŽใ‚‹ๅ ดๅˆใ‚„ใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใŒๅ˜ไธ€ใฎGPUใฎใƒกใƒขใƒชใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆใ€่ค‡ๆ•ฐใฎGPUใ‚’ไฝฟ็”จใ—ใŸใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใŒๅฟ…่ฆใจใชใ‚Šใพใ™ใ€‚ๅ˜ไธ€ใฎGPUใ‹ใ‚‰่ค‡ๆ•ฐใฎGPUใธใฎๅˆ‡ใ‚Šๆ›ฟใˆใซใฏใ€ใƒฏใƒผใ‚ฏใƒญใƒผใƒ‰ใ‚’ๅˆ†ๆ•ฃใ™ใ‚‹ใŸใ‚ใฎใ‚ใ‚‹็จฎใฎไธฆๅˆ—ๅ‡ฆ็†ใŒๅฟ…่ฆใงใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ€ใƒ†ใƒณใ‚ฝใƒซใ€ใพใŸใฏใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎไธฆๅˆ—ๅ‡ฆ็†ใชใฉใ€ใ•ใพใ–ใพใชไธฆๅˆ—ๅ‡ฆ็†ๆŠ€่ก“ใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ใ™ในใฆใซ้ฉใ—ใŸไธ€ใคใฎ่งฃๆฑบ็ญ–ใฏๅญ˜ๅœจใ›ใšใ€ๆœ€้ฉใช่จญๅฎšใฏไฝฟ็”จใ™ใ‚‹ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซไพๅญ˜ใ—ใพใ™ใ€‚ใ“ใฎ่จ˜ไบ‹ใฏใ€ใŠใใ‚‰ใไป–ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใซใ‚‚้ฉ็”จใ•ใ‚Œใ‚‹ไธป่ฆใชๆฆ‚ๅฟตใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใคใคใ€PyTorchใƒ™ใƒผใ‚นใฎๅฎŸ่ฃ…ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ + + + +**ๆณจๆ„**: [ๅ˜ไธ€GPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_one) ใง็ดนไป‹ใ•ใ‚ŒใŸๅคšใใฎๆˆฆ็•ฅ๏ผˆๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚„ๅ‹พ้…่“„็ฉใชใฉ๏ผ‰ใฏไธ€่ˆฌ็š„ใงใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไธ€่ˆฌ็š„ใซ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒžใƒซใƒGPUใ‚„CPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใชใฉใฎๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใซๅ…ฅใ‚‹ๅ‰ใซใ€ใใ‚Œใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ใพใšใ€ใ•ใพใ–ใพใช1Dไธฆๅˆ—ๅ‡ฆ็†ๆŠ€่ก“ใจใใฎๅˆฉ็‚นใŠใ‚ˆใณๆฌ ็‚นใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใ€ใใ‚Œใ‚‰ใ‚’2DใŠใ‚ˆใณ3Dไธฆๅˆ—ๅ‡ฆ็†ใซ็ต„ใฟๅˆใ‚ใ›ใฆใ•ใ‚‰ใซ้ซ˜้€Ÿใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฎŸ็พใ—ใ€ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆคœ่จŽใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชไป–ใฎๅผทๅŠ›ใชไปฃๆ›ฟๆ‰‹ๆณ•ใ‚‚็ดนไป‹ใ•ใ‚Œใพใ™ใ€‚ + +## Concepts + +ไปฅไธ‹ใฏใ€ใ“ใฎๆ–‡ๆ›ธใงๅพŒใง่ฉณใ—ใ่ชฌๆ˜Žใ•ใ‚Œใ‚‹ไธป่ฆใชๆฆ‚ๅฟตใฎ็ฐกๅ˜ใช่ชฌๆ˜Žใงใ™ใ€‚ + +1. **DataParallel (DP)** - ๅŒใ˜ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใŒ่ค‡ๆ•ฐๅ›ž่ค‡่ฃฝใ•ใ‚Œใ€ๅ„ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใซใƒ‡ใƒผใ‚ฟใฎใ‚นใƒฉใ‚คใ‚นใŒไพ›็ตฆใ•ใ‚Œใพใ™ใ€‚ๅ‡ฆ็†ใฏไธฆ่กŒใ—ใฆ่กŒใ‚ใ‚Œใ€ๅ„ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใƒ†ใƒƒใƒ—ใฎๆœ€ๅพŒใซๅŒๆœŸใ•ใ‚Œใพใ™ใ€‚ +2. **TensorParallel (TP)** - ๅ„ใƒ†ใƒณใ‚ฝใƒซใฏ่ค‡ๆ•ฐใฎใƒใƒฃใƒณใ‚ฏใซๅˆ†ๅ‰ฒใ•ใ‚Œใ€ๅ˜ไธ€ใฎGPUใซใƒ†ใƒณใ‚ฝใƒซๅ…จไฝ“ใŒๅญ˜ๅœจใ™ใ‚‹ใฎใงใฏใชใใ€ใƒ†ใƒณใ‚ฝใƒซใฎๅ„ใ‚ทใƒฃใƒผใƒ‰ใŒๆŒ‡ๅฎšใ•ใ‚ŒใŸGPUใซๅญ˜ๅœจใ—ใพใ™ใ€‚ๅ‡ฆ็†ไธญใซใ€ๅ„ใ‚ทใƒฃใƒผใƒ‰ใฏๅˆฅใ€…ใซไธฆ่กŒใ—ใฆๅ‡ฆ็†ใ•ใ‚Œใ€็•ฐใชใ‚‹GPUใงๅŒๆœŸใ•ใ‚Œใ€ใ‚นใƒ†ใƒƒใƒ—ใฎๆœ€ๅพŒใซ็ตๆžœใŒๅŒๆœŸใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏๆฐดๅนณไธฆๅˆ—ๅ‡ฆ็†ใจๅ‘ผใฐใ‚Œใ‚‹ใ‚‚ใฎใงใ€ๅˆ†ๅ‰ฒใฏๆฐดๅนณใƒฌใƒ™ใƒซใง่กŒใ‚ใ‚Œใพใ™ใ€‚ +3. **PipelineParallel (PP)** - ใƒขใƒ‡ใƒซใฏๅž‚็›ด๏ผˆใƒฌใ‚คใƒคใƒผใƒฌใƒ™ใƒซ๏ผ‰ใซ่ค‡ๆ•ฐใฎGPUใซๅˆ†ๅ‰ฒใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฎๅ˜ไธ€ใพใŸใฏ่ค‡ๆ•ฐใฎใƒฌใ‚คใƒคใƒผใŒๅ˜ไธ€ใฎGPUใซ้…็ฝฎใ•ใ‚Œใพใ™ใ€‚ๅ„GPUใฏใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎ็•ฐใชใ‚‹ใ‚นใƒ†ใƒผใ‚ธใ‚’ไธฆ่กŒใ—ใฆๅ‡ฆ็†ใ—ใ€ใƒใƒƒใƒใฎๅฐใ•ใชใƒใƒฃใƒณใ‚ฏใงไฝœๆฅญใ—ใพใ™ใ€‚ +4. **Zero Redundancy Optimizer (ZeRO)** - TPใจใ„ใใ‚‰ใ‹ไผผใŸใ‚ˆใ†ใชใƒ†ใƒณใ‚ฝใƒซใฎใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ—ใพใ™ใŒใ€ๅ‰ๅ‘ใใพใŸใฏๅพŒๅ‘ใใฎ่จˆ็ฎ—ใฎใŸใ‚ใซใƒ†ใƒณใ‚ฝใƒซๅ…จไฝ“ใŒๅ†ๆง‹็ฏ‰ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใพใŸใ€GPUใƒกใƒขใƒชใŒๅˆถ้™ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซ่ฃœๅ„Ÿใ™ใ‚‹ใŸใ‚ใฎใ•ใพใ–ใพใชใ‚ชใƒ•ใƒญใƒผใƒ‰ๆŠ€่ก“ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ +5. **Sharded DDP** - Sharded DDPใฏใ€ใ•ใพใ–ใพใชZeROๅฎŸ่ฃ…ใงไฝฟ็”จใ•ใ‚Œใ‚‹ๅŸบๆœฌ็š„ใชZeROใ‚ณใƒณใ‚ปใƒ—ใƒˆใฎๅˆฅๅใงใ™ใ€‚ + +ๅ„ใ‚ณใƒณใ‚ปใƒ—ใƒˆใฎ่ฉณ็ดฐใซๆทฑๅ…ฅใ‚Šใ™ใ‚‹ๅ‰ใซใ€ๅคง่ฆๆจกใชใ‚คใƒณใƒ•ใƒฉใ‚นใƒˆใƒฉใ‚ฏใƒใƒฃใงๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใฎๅคงใพใ‹ใชๆฑบๅฎšใƒ—ใƒญใ‚ปใ‚นใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +## Scalability Strategy + +**โ‡จ ใ‚ทใƒณใ‚ฐใƒซใƒŽใƒผใƒ‰ / ใƒžใƒซใƒGPU** +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€ใฎGPUใซๅŽใพใ‚‹ๅ ดๅˆ๏ผš + + 1. DDP - ๅˆ†ๆ•ฃใƒ‡ใƒผใ‚ฟไธฆๅˆ— + 2. ZeRO - ็Šถๆณใจไฝฟ็”จใ•ใ‚Œใ‚‹ๆง‹ๆˆใซๅฟœใ˜ใฆ้€Ÿใ„ใ‹ใฉใ†ใ‹ใŒ็•ฐใชใ‚Šใพใ™ + +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€ใฎGPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. PP + 2. ZeRO + 3. TP + + ้žๅธธใซ้ซ˜้€ŸใชใƒŽใƒผใƒ‰ๅ†…ๆŽฅ็ถš๏ผˆNVLINKใพใŸใฏNVSwitchใชใฉ๏ผ‰ใŒใ‚ใ‚Œใฐใ€ใ“ใ‚Œใ‚‰ใฎ3ใคใฏใปใผๅŒใ˜้€Ÿๅบฆใซใชใ‚‹ใฏใšใงใ€ใ“ใ‚Œใ‚‰ใŒใชใ„ๅ ดๅˆใ€PPใฏTPใพใŸใฏZeROใ‚ˆใ‚Šใ‚‚้€Ÿใใชใ‚Šใพใ™ใ€‚TPใฎ็จ‹ๅบฆใ‚‚ๅทฎใ‚’็”Ÿใ˜ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚็‰นๅฎšใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใฎๅ‹่€…ใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซๅฎŸ้จ“ใ™ใ‚‹ใ“ใจใŒๆœ€ๅ–„ใงใ™ใ€‚ + + TPใฏใปใจใ‚“ใฉใฎๅ ดๅˆใ€ๅ˜ไธ€ใƒŽใƒผใƒ‰ๅ†…ใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใคใพใ‚Šใ€TPใ‚ตใ‚คใ‚บ <= ใƒŽใƒผใƒ‰ใ”ใจใฎGPUๆ•ฐใงใ™ใ€‚ + +* ๆœ€ๅคงใฎใƒฌใ‚คใƒคใƒผใŒๅ˜ไธ€ใฎGPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. ZeROใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆ - TPใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚PPๅ˜็‹ฌใงใฏๅŽใพใ‚‰ใชใ„ใงใ—ใ‚‡ใ†ใ€‚ + 2. ZeROใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ - "ใ‚ทใƒณใ‚ฐใƒซGPU"ใฎใ‚จใƒณใƒˆใƒชใจๅŒใ˜ใ‚‚ใฎใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ + +**โ‡จ ใƒžใƒซใƒใƒŽใƒผใƒ‰ / ใƒžใƒซใƒGPU** + +* ใƒŽใƒผใƒ‰้–“ใฎ้ซ˜้€ŸๆŽฅ็ถšใŒใ‚ใ‚‹ๅ ดๅˆ๏ผš + + 1. ZeRO - ใƒขใƒ‡ใƒซใธใฎใปใจใ‚“ใฉใฎๅค‰ๆ›ดใŒไธ่ฆใงใ™ + 2. PP+TP+DP - ้€šไฟกใŒๅฐ‘ใชใใ€ใƒขใƒ‡ใƒซใธใฎๅคง่ฆๆจกใชๅค‰ๆ›ดใŒๅฟ…่ฆใงใ™ + +* ใƒŽใƒผใƒ‰้–“ใฎๆŽฅ็ถšใŒ้…ใใ€GPUใƒกใƒขใƒชใŒใพใ ไธ่ถณใ—ใฆใ„ใ‚‹ๅ ดๅˆ๏ผš + + 1. DP+PP+TP+ZeRO-1 + +## Data Parallelism + +2ใคใฎGPUใ‚’ๆŒใคใปใจใ‚“ใฉใฎใƒฆใƒผใ‚ถใƒผใฏใ€`DataParallel`๏ผˆDP๏ผ‰ใจ`DistributedDataParallel`๏ผˆDDP๏ผ‰ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€Ÿๅบฆใฎๅ‘ไธŠใ‚’ใ™ใงใซไบซๅ—ใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใปใผ่‡ชๆ˜Žใซไฝฟ็”จใงใใ‚‹PyTorchใฎ็ต„ใฟ่พผใฟๆฉŸ่ƒฝใงใ™ใ€‚ไธ€่ˆฌ็š„ใซใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใงๅ‹•ไฝœใ™ใ‚‹DDPใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚DPใฏไธ€้ƒจใฎใƒขใƒ‡ใƒซใงๅคฑๆ•—ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚[PyTorchใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://pytorch.org/docs/master/generated/torch.nn.DataParallel.html)่‡ชไฝ“ใ‚‚DDPใฎไฝฟ็”จใ‚’ๆŽจๅฅจใ—ใฆใ„ใพใ™ใ€‚ + +### DP vs DDP + +`DistributedDataParallel`๏ผˆDDP๏ผ‰ใฏ้€šๅธธใ€`DataParallel`๏ผˆDP๏ผ‰ใ‚ˆใ‚Šใ‚‚้ซ˜้€Ÿใงใ™ใŒใ€ๅธธใซใใ†ใจใฏ้™ใ‚Šใพใ›ใ‚“๏ผš +* DPใฏPythonใ‚นใƒฌใƒƒใƒ‰ใƒ™ใƒผใ‚นใงใ™ใŒใ€DDPใฏใƒžใƒซใƒใƒ—ใƒญใ‚ปใ‚นใƒ™ใƒผใ‚นใงใ™ใ€‚ใใฎใŸใ‚ใ€GIL๏ผˆGlobal Interpreter Lock๏ผ‰ใชใฉใฎPythonใ‚นใƒฌใƒƒใƒ‰ใฎๅˆถ็ด„ใŒใชใ„ใŸใ‚ใงใ™ใ€‚ +* ไธ€ๆ–นใ€GPUใ‚ซใƒผใƒ‰้–“ใฎ้…ใ„็›ธไบ’ๆŽฅ็ถšๆ€งใฏใ€DDPใฎๅ ดๅˆใซๅฎŸ้š›ใซใฏ้…ใ„็ตๆžœใ‚’ใ‚‚ใŸใ‚‰ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€2ใคใฎใƒขใƒผใƒ‰้–“ใฎGPU้–“้€šไฟกใฎไธปใช้•ใ„ใงใ™๏ผš + +[DDP](https://pytorch.org/docs/master/notes/ddp.html): + +- ้–‹ๅง‹ๆ™‚ใ€ใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚นใฏใƒขใƒ‡ใƒซใ‚’GPU 0ใ‹ใ‚‰ไป–ใฎGPUใซ่ค‡่ฃฝใ—ใพใ™ใ€‚ +- ใใ‚Œใ‹ใ‚‰ๅ„ใƒใƒƒใƒใ”ใจใซ: + 1. ๅ„GPUใฏๅ„่‡ชใฎใƒŸใƒ‹ใƒใƒƒใƒใฎใƒ‡ใƒผใ‚ฟใ‚’็›ดๆŽฅๆถˆ่ฒปใ—ใพใ™ใ€‚ + 2. `backward`ไธญใ€ใƒญใƒผใ‚ซใƒซๅ‹พ้…ใŒๆบ–ๅ‚™ใงใใ‚‹ใจใ€ใใ‚Œใ‚‰ใฏใ™ในใฆใฎใƒ—ใƒญใ‚ปใ‚นใงๅนณๅ‡ๅŒ–ใ•ใ‚Œใพใ™ใ€‚ + +[DP](https://pytorch.org/docs/master/generated/torch.nn.DataParallel.html): + +ๅ„ใƒใƒƒใƒใ”ใจใซ: + 1. GPU 0ใฏใƒ‡ใƒผใ‚ฟใƒใƒƒใƒใ‚’่ชญใฟๅ–ใ‚Šใ€ใใ‚Œใ‹ใ‚‰ๅ„GPUใซใƒŸใƒ‹ใƒใƒƒใƒใ‚’้€ไฟกใ—ใพใ™ใ€‚ + 2. GPU 0ใ‹ใ‚‰ๅ„GPUใซๆœ€ๆ–ฐใฎใƒขใƒ‡ใƒซใ‚’่ค‡่ฃฝใ—ใพใ™ใ€‚ + 3. `forward`ใ‚’ๅฎŸ่กŒใ—ใ€ๅ„GPUใ‹ใ‚‰GPU 0ใซๅ‡บๅŠ›ใ‚’้€ไฟกใ—ใ€ๆๅคฑใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ + 4. GPU 0ใ‹ใ‚‰ใ™ในใฆใฎGPUใซๆๅคฑใ‚’ๅˆ†ๆ•ฃใ—ใ€`backward`ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + 5. ๅ„GPUใ‹ใ‚‰GPU 0ใซๅ‹พ้…ใ‚’้€ไฟกใ—ใ€ใใ‚Œใ‚‰ใ‚’ๅนณๅ‡ๅŒ–ใ—ใพใ™ใ€‚ + +DDPใฏใƒใƒƒใƒใ”ใจใซ่กŒใ†้€šไฟกใฏๅ‹พ้…ใฎ้€ไฟกใฎใฟใงใ‚ใ‚Šใ€ไธ€ๆ–นใ€DPใฏใƒใƒƒใƒใ”ใจใซ5ใคใฎ็•ฐใชใ‚‹ใƒ‡ใƒผใ‚ฟไบคๆ›ใ‚’่กŒใ„ใพใ™ใ€‚ + +DPใฏใƒ—ใƒญใ‚ปใ‚นๅ†…ใงใƒ‡ใƒผใ‚ฟใ‚’Pythonใ‚นใƒฌใƒƒใƒ‰ใ‚’ไป‹ใ—ใฆใ‚ณใƒ”ใƒผใ—ใพใ™ใŒใ€DDPใฏ[torch.distributed](https://pytorch.org/docs/master/distributed.html)ใ‚’ไป‹ใ—ใฆใƒ‡ใƒผใ‚ฟใ‚’ใ‚ณใƒ”ใƒผใ—ใพใ™ใ€‚ + +DPใงใฏGPU 0ใฏไป–ใฎGPUใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซๅคšใใฎไฝœๆฅญใ‚’่กŒใ†ใŸใ‚ใ€GPUใฎๆœชไฝฟ็”จ็އใŒ้ซ˜ใใชใ‚Šใพใ™ใ€‚ + +DDPใฏ่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณ้–“ใงไฝฟ็”จใงใใพใ™ใŒใ€DPใฎๅ ดๅˆใฏใใ†ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +DPใจDDPใฎไป–ใซใ‚‚้•ใ„ใŒใ‚ใ‚Šใพใ™ใŒใ€ใ“ใฎ่ญฐ่ซ–ใซใฏ้–ขไฟ‚ใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ“ใ‚Œใ‚‰2ใคใฎใƒขใƒผใƒ‰ใ‚’ๆทฑใ็†่งฃใ—ใŸใ„ๅ ดๅˆใ€ใ“ใฎ[่จ˜ไบ‹](https://www.telesens.co/2019/04/04/distributed-data-parallel-training-using-pytorch-on-aws/)ใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚็ด ๆ™ดใ‚‰ใ—ใ„ใƒ€ใ‚คใ‚ขใ‚ฐใƒฉใƒ ใ‚’ๅซใฟใ€ใ•ใพใ–ใพใชใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใฎ่ค‡ๆ•ฐใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใจใƒ—ใƒญใƒ•ใ‚กใ‚คใƒฉใฎๅ‡บๅŠ›ใ‚’็คบใ—ใ€็ŸฅใฃใฆใŠใๅฟ…่ฆใŒใ‚ใ‚‹ใ™ในใฆใฎๅพฎๅฆ™ใชใƒ‹ใƒฅใ‚ขใƒณใ‚นใ‚’่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ + +ๅฎŸ้š›ใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +| Type | NVlink | Time | +| :----- | ----- | ---: | +| 2:DP | Y | 110s | +| 2:DDP | Y | 101s | +| 2:DDP | N | 131s | + + +่งฃๆž๏ผš + +ใ“ใ“ใงใ€DPใฏNVlinkใ‚’ไฝฟ็”จใ—ใŸDDPใซๆฏ”ในใฆ็ด„10๏ผ…้…ใใ€NVlinkใ‚’ไฝฟ็”จใ—ใชใ„DDPใซๆฏ”ในใฆ็ด„15๏ผ…้ซ˜้€Ÿใงใ‚ใ‚‹ใ“ใจใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ๅฎŸ้š›ใฎ้•ใ„ใฏใ€ๅ„GPUใŒไป–ใฎGPUใจๅŒๆœŸใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใƒ‡ใƒผใ‚ฟใฎ้‡ใซไพๅญ˜ใ—ใพใ™ใ€‚ๅŒๆœŸใ™ใ‚‹ใƒ‡ใƒผใ‚ฟใŒๅคšใ„ใปใฉใ€้…ใ„ใƒชใƒณใ‚ฏใŒๅˆ่จˆใฎๅฎŸ่กŒๆ™‚้–“ใ‚’้…ใใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใใชใ‚Šใพใ™ใ€‚ + +ไปฅไธ‹ใฏๅฎŒๅ…จใชใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚ณใƒผใƒ‰ใจๅ‡บๅŠ›ใงใ™๏ผš + +`NCCL_P2P_DISABLE=1`ใ‚’ไฝฟ็”จใ—ใฆใ€ๅฏพๅฟœใ™ใ‚‹ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใงNVLinkๆฉŸ่ƒฝใ‚’็„กๅŠนใซใ—ใพใ—ใŸใ€‚ + + +```bash + +# DP +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 \ +python examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 110.5948, 'train_samples_per_second': 1.808, 'epoch': 0.69} + +# DDP w/ NVlink +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 \ +torchrun --nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 101.9003, 'train_samples_per_second': 1.963, 'epoch': 0.69} + +# DDP w/o NVlink +rm -r /tmp/test-clm; NCCL_P2P_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1 \ +torchrun --nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 131.4367, 'train_samples_per_second': 1.522, 'epoch': 0.69} +``` + +ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ข: 2x TITAN RTXใ€ๅ„24GB + 2ใคใฎNVLink๏ผˆ`nvidia-smi topo -m`ใง `NV2`๏ผ‰ + +ใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ข: `pytorch-1.8-to-be` + `cuda-11.0` / `transformers==4.3.0.dev0` + +## ZeRO Data Parallelism + +ZeROใƒ‘ใƒฏใƒผใƒ‰ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ๅ‡ฆ็†๏ผˆZeRO-DP๏ผ‰ใฏใ€ๆฌกใฎ[ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://www.microsoft.com/en-us/research/blog/zero-deepspeed-new-system-optimizations-enable-training-models-with-over-100-billion-parameters/)ใฎใƒ€ใ‚คใ‚ขใ‚ฐใƒฉใƒ ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +![DeepSpeed-Image-1](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-zero.png) + +ใ“ใ‚Œใฏ็†่งฃใŒ้›ฃใ—ใ„ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ๅฎŸ้š›ใซใฏใ“ใฎๆฆ‚ๅฟตใฏ้žๅธธใซใ‚ทใƒณใƒ—ใƒซใงใ™ใ€‚ใ“ใ‚Œใฏ้€šๅธธใฎ`DataParallel`๏ผˆDP๏ผ‰ใงใ™ใŒใ€ๅฎŒๅ…จใชใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€ๅ‹พ้…ใ€ใŠใ‚ˆใณใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใ‚’่ค‡่ฃฝใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ๅ„GPUใฏใใ‚Œใžใ‚Œใฎใ‚นใƒฉใ‚คใ‚นใฎใฟใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ใใ—ใฆใ€ๅฎŸ่กŒๆ™‚ใซใ€็‰นๅฎšใฎใƒฌใ‚คใƒคใƒผใซๅฟ…่ฆใชๅฎŒๅ…จใชใƒฌใ‚คใƒคใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฟ…่ฆใชๅ ดๅˆใ€ใ™ในใฆใฎGPUใŒๅŒๆœŸใ—ใฆใ€ใŠไบ’ใ„ใซไธ่ถณใ—ใฆใ„ใ‚‹้ƒจๅˆ†ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใใ‚ŒใŒใ™ในใฆใงใ™ใ€‚ + +3ใคใฎใƒฌใ‚คใƒคใƒผใ‹ใ‚‰ใชใ‚‹ๅ˜็ด”ใชใƒขใƒ‡ใƒซใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚ๅ„ใƒฌใ‚คใƒคใƒผใซใฏ3ใคใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใพใ™๏ผš + + +``` +La | Lb | Lc +---|----|--- +a0 | b0 | c0 +a1 | b1 | c1 +a2 | b2 | c2 +``` + +ใƒฌใ‚คใƒคใƒผLaใซใฏใ€้‡ใฟa0ใ€a1ใ€ใŠใ‚ˆใณa2ใŒใ‚ใ‚Šใพใ™ใ€‚ + +3ใคใฎGPUใŒใ‚ใ‚‹ๅ ดๅˆใ€Sharded DDP๏ผˆ= Zero-DP๏ผ‰ใฏใƒขใƒ‡ใƒซใ‚’3ใคใฎGPUใซๆฌกใฎใ‚ˆใ†ใซๅˆ†ๅ‰ฒใ—ใพใ™๏ผš + + +``` +GPU0: +La | Lb | Lc +---|----|--- +a0 | b0 | c0 + +GPU1: +La | Lb | Lc +---|----|--- +a1 | b1 | c1 + +GPU2: +La | Lb | Lc +---|----|--- +a2 | b2 | c2 +``` + +ใ“ใ‚Œใฏใ€ๅ…ธๅž‹็š„ใชใƒ‡ใ‚ฃใƒผใƒ—ใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆDNN๏ผ‰ใฎใƒ€ใ‚คใ‚ขใ‚ฐใƒฉใƒ ใ‚’ๆƒณๅƒใ™ใ‚‹ใจใ€ใƒ†ใƒณใ‚ฝใƒซไธฆๅˆ—ๅ‡ฆ็†ใจๅŒๆง˜ใฎๆฐดๅนณใ‚นใƒฉใ‚คใ‚นใงใ‚ใ‚‹ใ‚ˆใ†ใชใ‚‚ใฎใงใ™ใ€‚ๅž‚็›ดใ‚นใƒฉใ‚คใ‚นใฏใ€็•ฐใชใ‚‹GPUใซๅฎŒๅ…จใชๅฑคใ‚ฐใƒซใƒผใƒ—ใ‚’้…็ฝฎใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ใ—ใ‹ใ—ใ€ใ“ใ‚Œใฏๅ˜ใชใ‚‹ๅ‡บ็™บ็‚นใซ้ŽใŽใพใ›ใ‚“ใ€‚ + +ใ“ใ‚Œใ‹ใ‚‰ใ€ๅ„GPUใฏ้€šๅธธใฎใƒ‡ใƒผใ‚ฟไธฆๅˆ—ๅ‡ฆ็†๏ผˆDP๏ผ‰ใจๅŒๆง˜ใซใ€้€šๅธธใฎใƒŸใƒ‹ใƒใƒƒใƒใ‚’ๅ—ใ‘ๅ–ใ‚Šใพใ™๏ผš + +``` +x0 => GPU0 +x1 => GPU1 +x2 => GPU2 +``` + +ๆœ€ๅˆใซใ€ๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใฏใƒฌใ‚คใƒคใƒผLaใซ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + +GPU0ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ—ใ‚‡ใ†๏ผšx0ใฏใ€ใใฎๅ‰ๅ‘ใใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใซa0ใ€a1ใ€a2ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฟ…่ฆใงใ™ใŒใ€GPU0ใซใฏa0ใ—ใ‹ใ‚ใ‚Šใพใ›ใ‚“ใ€‚GPU1ใ‹ใ‚‰a1ใ‚’ใ€GPU2ใ‹ใ‚‰a2ใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ใƒขใƒ‡ใƒซใฎๅ„้ƒจๅˆ†ใ‚’ใพใจใ‚ใพใ™ใ€‚ + +ๅŒๆง˜ใซใ€GPU1ใฏใƒŸใƒ‹ใƒใƒƒใƒx1ใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€a1ใ—ใ‹ๆŒใฃใฆใ„ใพใ›ใ‚“ใŒใ€a0ใจa2ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅฟ…่ฆใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏGPU0ใจGPU2ใ‹ใ‚‰ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +GPU2ใ‚‚x2ใ‚’ๅ—ใ‘ๅ–ใ‚Šใพใ™ใ€‚a0ใจa1ใฏGPU0ใจGPU1ใ‹ใ‚‰ๅ—ใ‘ๅ–ใ‚Šใ€a2ใจใจใ‚‚ใซๅฎŒๅ…จใชใƒ†ใƒณใ‚ฝใƒซใ‚’ๅ†ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ + +3ใคใฎGPUใฏๅฎŒๅ…จใชใƒ†ใƒณใ‚ฝใƒซใ‚’ๅ†ๆง‹็ฏ‰ใ—ใ€ๅ‰ๅ‘ใ่จˆ็ฎ—ใŒ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +่จˆ็ฎ—ใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ไธ่ฆใซใชใฃใŸใƒ‡ใƒผใ‚ฟใฏๅ‰Š้™คใ•ใ‚Œใพใ™ใ€‚่จˆ็ฎ—ไธญใ ใ‘ไฝฟ็”จใ•ใ‚Œใ€ๅ†ๆง‹็ฏ‰ใฏไบ‹ๅ‰ใซใƒ•ใ‚งใƒƒใƒใ‚’ไฝฟ็”จใ—ใฆๅŠน็އ็š„ใซ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +ใใ—ใฆใ€ใ“ใฎใƒ—ใƒญใ‚ปใ‚นๅ…จไฝ“ใŒใƒฌใ‚คใƒคใƒผLbใ€ๆฌกใซๅ‰ๅ‘ใใงLcใ€ใใ—ใฆ้€†ๆ–นๅ‘ใงLc -> Lb -> Laใซๅฏพใ—ใฆ็นฐใ‚Š่ฟ”ใ•ใ‚Œใพใ™ใ€‚ + +็งใซใจใฃใฆใ€ใ“ใ‚ŒใฏๅŠน็އ็š„ใชใ‚ฐใƒซใƒผใƒ—ใงใฎ้‡ใฟใฎๅˆ†ๆ•ฃๆˆฆ็•ฅใฎใ‚ˆใ†ใซ่žใ“ใˆใพใ™๏ผš + +1. ไบบAใฏใƒ†ใƒณใƒˆใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ +2. ไบบBใฏใ‚นใƒˆใƒผใƒ–ใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ +3. ไบบCใฏๆ–งใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ + +ไปŠใ€ๅฝผใ‚‰ใฏๆฏŽๆ™ฉๆŒใฃใฆใ„ใ‚‹ใ‚‚ใฎใ‚’ๅ…ฑๆœ‰ใ—ใ€ไป–ใฎไบบใ‹ใ‚‰ๆŒใฃใฆใ„ใชใ„ใ‚‚ใฎใ‚’ใ‚‚ใ‚‰ใ„ใ€ๆœใซใฏๅ‰ฒใ‚Šๅฝ“ใฆใ‚‰ใ‚ŒใŸใ‚ฟใ‚คใƒ—ใฎใ‚ฎใ‚ขใ‚’่ฉฐใ‚ใฆๆ—…ใ‚’็ถšใ‘ใพใ™ใ€‚ใ“ใ‚ŒใŒSharded DDP / Zero DPใงใ™ใ€‚ + +ใ“ใฎๆˆฆ็•ฅใ‚’ใ€ๅ„ไบบใŒ็‹ฌ่‡ชใฎใƒ†ใƒณใƒˆใ€ใ‚นใƒˆใƒผใƒ–ใ€ๆ–งใ‚’ๆŒใฃใฆ้‹ใฐใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„ใ‚ทใƒณใƒ—ใƒซใชๆˆฆ็•ฅใจๆฏ”่ผƒใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ใ“ใ‚ŒใŒPyTorchใฎDataParallel๏ผˆDPใŠใ‚ˆใณDDP๏ผ‰ใงใ™ใ€‚ + +ใ“ใฎใƒˆใƒ”ใƒƒใ‚ฏใฎๆ–‡็Œฎใ‚’่ชญใ‚€้š›ใซใ€ไปฅไธ‹ใฎ้กž็พฉ่ชžใซๅ‡บไผšใ†ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผšShardedใ€Partitionedใ€‚ + +ZeROใŒใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ๅˆ†ๅ‰ฒใ™ใ‚‹ๆ–นๆณ•ใซๆณจๆ„ใ‚’ๆ‰•ใ†ใจใ€ใ“ใ‚Œใฏใƒ†ใƒณใ‚ฝใƒซใƒ‘ใƒฉใƒฌใƒชใ‚บใƒ ใจ้žๅธธใซไผผใฆใ„ใ‚‹ใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใ€‚ใ“ใ‚ŒใฏๅพŒใง่ญฐ่ซ–ใ•ใ‚Œใ‚‹ๅž‚็›ดใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒฌใƒชใ‚บใƒ ใจใฏ็•ฐใชใ‚Šใ€ๅ„ใƒฌใ‚คใƒคใƒผใฎ้‡ใฟใ‚’ใƒ‘ใƒผใƒ†ใ‚ฃใ‚ทใƒงใƒณ/ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ—ใพใ™ใ€‚ + +Implementations: + +- [DeepSpeed](https://www.deepspeed.ai/tutorials/zero/) ZeRO-DP stages 1+2+3 +- [`transformers` integration](main_classes/trainer#trainer-integrations) + + +## Naive Model Parallelism (Vertical) and Pipeline Parallelism + +ใƒŠใ‚คใƒผใƒ–ใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒฌใƒชใ‚บใƒ ๏ผˆMP๏ผ‰ใฏใ€ใƒขใƒ‡ใƒซใฎๅฑคใ‚’่ค‡ๆ•ฐใฎGPUใซๅˆ†ๆ•ฃใ•ใ›ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ใ“ใฎใƒกใ‚ซใƒ‹ใ‚บใƒ ใฏๆฏ”่ผƒ็š„ๅ˜็ด”ใงใ€ๅธŒๆœ›ใ™ใ‚‹ๅฑคใ‚’`.to()`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ็‰นๅฎšใฎใƒ‡ใƒใ‚คใ‚นใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ใ ใ‘ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‡ใƒผใ‚ฟใŒใ“ใ‚Œใ‚‰ใฎๅฑคใ‚’้€š้Žใ™ใ‚‹ใŸใณใซใ€ใƒ‡ใƒผใ‚ฟใ‚‚ๅฑคใจๅŒใ˜ใƒ‡ใƒใ‚คใ‚นใซๅˆ‡ใ‚Šๆ›ฟใˆใ‚‰ใ‚Œใ€ๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใฏๅค‰ๆ›ดใ•ใ‚Œใพใ›ใ‚“ใ€‚ + +็งใŸใกใฏใ“ใ‚Œใ‚’ใ€Œๅž‚็›ดMPใ€ใจๅ‘ผใณใพใ™ใ€‚ใชใœใชใ‚‰ใ€ใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใŒใฉใฎใ‚ˆใ†ใซๆใ‹ใ‚Œใ‚‹ใ‹ใ‚’ๆ€ใ„ๅ‡บใ™ใจใ€ๅฑคใ‚’ๅž‚็›ดใซใ‚นใƒฉใ‚คใ‚นใ™ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ใŸใจใˆใฐใ€ไปฅไธ‹ใฎๅ›ณใฏ8ๅฑคใฎใƒขใƒ‡ใƒซใ‚’็คบใ—ใฆใ„ใพใ™๏ผš + + +``` +=================== =================== +| 0 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | +=================== =================== + gpu0 gpu1 +``` + +ๆˆ‘ใ€…ใฏใ€ใƒขใƒ‡ใƒซใ‚’ๅž‚็›ดใซ2ใคใซๅˆ†ๅ‰ฒใ—ใ€ใƒฌใ‚คใƒคใƒผ0ใ‹ใ‚‰3ใ‚’GPU0ใซ้…็ฝฎใ—ใ€ใƒฌใ‚คใƒคใƒผ4ใ‹ใ‚‰7ใ‚’GPU1ใซ้…็ฝฎใ—ใพใ—ใŸใ€‚ + +ใƒ‡ใƒผใ‚ฟใŒใƒฌใ‚คใƒคใƒผ0ใ‹ใ‚‰1ใ€1ใ‹ใ‚‰2ใ€2ใ‹ใ‚‰3ใซ็งปๅ‹•ใ™ใ‚‹้–“ใฏ้€šๅธธใฎใƒขใƒ‡ใƒซใจๅŒใ˜ใงใ™ใ€‚ใ—ใ‹ใ—ใ€ใƒ‡ใƒผใ‚ฟใŒใƒฌใ‚คใƒคใƒผ3ใ‹ใ‚‰ใƒฌใ‚คใƒคใƒผ4ใซ็งปๅ‹•ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€GPU0ใ‹ใ‚‰GPU1ใธใฎ็งปๅ‹•ใŒ็™บ็”Ÿใ—ใ€้€šไฟกใฎใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ๅ‚ๅŠ ใ—ใฆใ„ใ‚‹GPUใŒๅŒใ˜ใ‚ณใƒณใƒ”ใƒฅใƒผใƒˆใƒŽใƒผใƒ‰๏ผˆไพ‹๏ผšๅŒใ˜็‰ฉ็†ใƒžใ‚ทใƒณ๏ผ‰ใซใ‚ใ‚‹ๅ ดๅˆใ€ใ“ใฎใ‚ณใƒ”ใƒผใฏ้žๅธธใซ้ซ˜้€Ÿใงใ™ใŒใ€็•ฐใชใ‚‹ใ‚ณใƒณใƒ”ใƒฅใƒผใƒˆใƒŽใƒผใƒ‰๏ผˆไพ‹๏ผš่ค‡ๆ•ฐใฎใƒžใ‚ทใƒณ๏ผ‰ใซใ‚ใ‚‹ๅ ดๅˆใ€้€šไฟกใฎใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใฏๅคงๅน…ใซๅข—ๅŠ ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใใฎๅพŒใ€ใƒฌใ‚คใƒคใƒผ4ใ‹ใ‚‰5ใ€6ใ‹ใ‚‰7ใพใงใฏ้€šๅธธใฎใƒขใƒ‡ใƒซใจๅŒๆง˜ใซๅ‹•ไฝœใ—ใ€7็•ช็›ฎใฎใƒฌใ‚คใƒคใƒผใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒ‡ใƒผใ‚ฟใ‚’ใ—ใฐใ—ใฐใƒฌใ‚คใƒคใƒผ0ใซๆˆปใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผˆใพใŸใฏใƒฉใƒ™ใƒซใ‚’ๆœ€ๅพŒใฎใƒฌใ‚คใƒคใƒผใซ้€ไฟกใ—ใพใ™๏ผ‰ใ€‚ใ“ใ‚Œใงๆๅคฑใ‚’่จˆ็ฎ—ใ—ใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใŒไฝœๆฅญใ‚’้–‹ๅง‹ใงใใพใ™ใ€‚ + +ๅ•้กŒ็‚น๏ผš +- ไธปใชๆฌ ็‚นใ€ใŠใ‚ˆใณใชใœใ“ใ‚Œใ‚’ใ€Œๅ˜็ด”ใชใ€MPใจๅ‘ผใถใฎใ‹ใฏใ€1ใคใ‚’้™คใ„ใฆใ™ในใฆใฎGPUใŒใฉใ‚“ใช็žฌ้–“ใงใ‚‚ใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใงใ‚ใ‚‹ใ“ใจใงใ™ใ€‚ใ—ใŸใŒใฃใฆใ€4ใคใฎGPUใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ๅ˜็ด”ใชMPใฏใ€1ใคใฎGPUใฎใƒกใƒขใƒชๅฎน้‡ใ‚’4ๅ€ใซใ™ใ‚‹ใฎใจใปใผๅŒใ˜ใงใ‚ใ‚Šใ€ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใฎๆฎ‹ใ‚Šใ‚’็„ก่ฆ–ใ—ใพใ™ใ€‚ใ•ใ‚‰ใซใ€ใƒ‡ใƒผใ‚ฟใฎใ‚ณใƒ”ใƒผใฎใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒใ‚ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใฆใฏใ„ใ‘ใพใ›ใ‚“ใ€‚ใ—ใŸใŒใฃใฆใ€4ๆžšใฎ6GBใฎใ‚ซใƒผใƒ‰ใฏใ€ใƒ‡ใƒผใ‚ฟใฎใ‚ณใƒ”ใƒผใฎใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒใชใ„1ๆžšใฎ24GBใฎใ‚ซใƒผใƒ‰ใจๅŒใ˜ใ‚ตใ‚คใ‚บใ‚’ๅŽๅฎนใงใใ‚‹ใงใ—ใ‚‡ใ†ใŒใ€ๅพŒ่€…ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ˆใ‚Š่ฟ…้€ŸใซๅฎŒไบ†ใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใŸใจใˆใฐ40GBใฎใ‚ซใƒผใƒ‰ใŒใ‚ใ‚Šใ€45GBใฎใƒขใƒ‡ใƒซใ‚’ๅŽใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€ๅ‹พ้…ใจใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใฎใŸใ‚ใซใปใจใ‚“ใฉๅŽใ‚ใ‚‹ใ“ใจใŒใงใใพใ›ใ‚“ใ€‚ +- ๅ…ฑๆœ‰ใฎๅŸ‹ใ‚่พผใฟใฏใ€GPU้–“ใงใ‚ณใƒ”ใƒผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๅ‡ฆ็†๏ผˆPP๏ผ‰ใฏใ€ใปใผๅ˜็ด”ใชMPใจๅŒใ˜ใงใ™ใŒใ€GPUใŒใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใซใชใ‚‹ๅ•้กŒใ‚’่งฃๆฑบใ—ใ€ๅ…ฅๅŠ›ใƒใƒƒใƒใ‚’ใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไบบๅทฅ็š„ใซไฝœๆˆใ™ใ‚‹ใ“ใจใซใ‚ˆใ‚Šใ€็•ฐใชใ‚‹GPUใŒ่จˆ็ฎ—ใƒ—ใƒญใ‚ปใ‚นใซๅŒๆ™‚ใซๅ‚ๅŠ ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€[GPipe่ซ–ๆ–‡](https://ai.googleblog.com/2019/03/introducing-gpipe-open-source-library.html)ใ‹ใ‚‰ใฎๅ›ณใงใ€ไธŠ้ƒจใซใฏๅ˜็ด”ใชMPใ€ไธ‹้ƒจใซใฏPPใŒ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš + +![mp-pp](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-gpipe-bubble.png) + +ใ“ใฎๅ›ณใ‹ใ‚‰ใ€PPใŒGPUใŒใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใฎ้ ˜ๅŸŸใงใ‚ใ‚‹ใ€Œใƒใƒ–ใƒซใ€ใ‚’ๅฐ‘ใชใๆŒใคใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ‚ขใ‚คใƒ‰ใƒซ็Šถๆ…‹ใฎ้ƒจๅˆ†ใฏใ€Œใƒใƒ–ใƒซใ€ใจๅ‘ผใฐใ‚Œใพใ™ใ€‚ + +ๅ›ณใฎไธกๆ–นใฎ้ƒจๅˆ†ใฏใ€4ใคใฎGPUใŒใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๅ‚ๅŠ ใ—ใฆใ„ใ‚‹4ใฎๆฌกๅ…ƒใฎไธฆๅˆ—ๆ€งใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใคใพใ‚Šใ€4ใคใฎใƒ‘ใ‚คใƒ—ใ‚นใƒ†ใƒผใ‚ธF0ใ€F1ใ€F2ใ€F3ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใŒใ‚ใ‚Šใ€้€†้ †ใฎใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใƒ‘ใ‚นB3ใ€B2ใ€B1ใ€B0ใŒใ‚ใ‚Šใพใ™ใ€‚ + +PPใฏ่ชฟๆ•ดใ™ใ‚‹ๆ–ฐใ—ใ„ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฐŽๅ…ฅใ—ใพใ™ใ€‚ใใ‚Œใฏ `chunks` ใงใ€ๅŒใ˜ใƒ‘ใ‚คใƒ—ใ‚นใƒ†ใƒผใ‚ธใ‚’้€šใ˜ใฆ้€ฃ็ถšใ—ใฆ้€ไฟกใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใฎใƒใƒฃใƒณใ‚ฏใฎๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€ไธ‹ใฎๅ›ณใงใฏ `chunks=4` ใŒ่กจ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚GPU0ใฏใƒใƒฃใƒณใ‚ฏ0ใ€1ใ€2ใ€3๏ผˆF0,0ใ€F0,1ใ€F0,2ใ€F0,3๏ผ‰ใงๅŒใ˜ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ—ใ€ไป–ใฎGPUใŒไฝœๆฅญใ‚’้–‹ๅง‹ใ—ๅง‹ใ‚ใ‚‹ใฎใ‚’ๅพ…ใฃใฆใ‹ใ‚‰ใ€GPU0ใฏใƒใƒฃใƒณใ‚ฏ3ใ€2ใ€1ใ€0๏ผˆB0,3ใ€B0,2ใ€B0,1ใ€B0,0๏ผ‰ใง้€†้ †ใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +ๆณจๆ„ใ™ในใใฏใ€ๆฆ‚ๅฟต็š„ใซใฏใ“ใ‚ŒใŒๅ‹พ้…่“„็ฉใ‚นใƒ†ใƒƒใƒ—๏ผˆGAS๏ผ‰ใจๅŒใ˜ใ‚ณใƒณใ‚ปใƒ—ใƒˆใงใ‚ใ‚‹ใ“ใจใงใ™ใ€‚PyTorchใฏ `chunks` ใ‚’ไฝฟ็”จใ—ใ€DeepSpeedใฏๅŒใ˜ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’GASใจๅ‘ผใณใพใ™ใ€‚ + +`chunks` ใฎๅฐŽๅ…ฅใซใ‚ˆใ‚Šใ€PPใฏใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒ๏ผˆMBS๏ผ‰ใฎๆฆ‚ๅฟตใ‚’ๅฐŽๅ…ฅใ—ใพใ™ใ€‚DPใฏใ‚ฐใƒญใƒผใƒใƒซใƒ‡ใƒผใ‚ฟใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ใƒŸใƒ‹ใƒใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€DPใฎๆฌกๆ•ฐใŒ4ใงใ€ใ‚ฐใƒญใƒผใƒใƒซใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒ1024ใฎๅ ดๅˆใ€4ใคใฎใƒŸใƒ‹ใƒใƒƒใƒ๏ผˆใใ‚Œใžใ‚Œ256๏ผ‰ใซๅˆ†ๅ‰ฒใ•ใ‚Œใพใ™๏ผˆ1024/4๏ผ‰ใ€‚ใใ—ใฆใ€`chunks`๏ผˆใพใŸใฏGAS๏ผ‰ใฎๆ•ฐใŒ32ใงใ‚ใ‚‹ๅ ดๅˆใ€ใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใ‚ตใ‚คใ‚บใฏ8ใซใชใ‚Šใพใ™๏ผˆ256/32๏ผ‰ใ€‚ๅ„ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚นใƒ†ใƒผใ‚ธใฏ1ใคใฎใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใงไฝœๆฅญใ—ใพใ™ใ€‚ + +DP + PPใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฎใ‚ฐใƒญใƒผใƒใƒซใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’่จˆ็ฎ—ใ™ใ‚‹ใซใฏใ€`mbs*chunks*dp_degree`๏ผˆ`8*32*4=1024`๏ผ‰ใ‚’่กŒใ„ใพใ™ใ€‚ + +ๅ›ณใซๆˆปใ‚Šใพใ—ใ‚‡ใ†ใ€‚ + +`chunks=1` ใงใ‚ใ‚Œใฐใ€้žๅŠน็އใชๅ˜็ด”ใชMPใซใชใ‚Šใพใ™ใ€‚้žๅธธใซๅคงใใช `chunks` ๅ€คใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€้žๅธธใซๅฐใ•ใชใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใ‚ตใ‚คใ‚บใซใชใ‚Šใ€ๅŠน็އใŒใ‚ใพใ‚Š้ซ˜ใใชใ„ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ—ใŸใŒใฃใฆใ€GPUใฎๅŠน็އ็š„ใชๅˆฉ็”จใ‚’ๆœ€ๅคงๅŒ–ใ™ใ‚‹ๅ€คใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซๅฎŸ้จ“ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒใƒ–ใƒซใฎใ‚ตใ‚คใ‚บใ‚’ๆœ€ๅฐ้™ใซใ™ใ‚‹ใ“ใจใซๅฏพๅฟœใ™ใ‚‹ใ€ใ™ในใฆใฎๅ‚ๅŠ GPUใซใ‚ใŸใ‚‹้ซ˜ใ„ไธฆ่กŒGPUๅˆฉ็”จใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +2ใคใฎใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใ‚ฐใƒซใƒผใƒ—ใŒใ‚ใ‚Šใพใ™ใ€‚ๅพ“ๆฅใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณAPIใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใจใ€ใƒฆใƒผใ‚ถใƒผใฎใƒขใƒ‡ใƒซใ‚’ๅคงๅน…ใซๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‚ˆใ‚Š็พไปฃ็š„ใชใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใงใ™ใ€‚ + +ๅพ“ๆฅใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณAPIใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณ๏ผš +- PyTorch +- DeepSpeed +- Megatron-LM + +็พไปฃ็š„ใชใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณ๏ผš +- Varuna +- Sagemaker + +ๅพ“ๆฅใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณAPIใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใฎๅ•้กŒ็‚น๏ผš +- ใƒขใƒ‡ใƒซใ‚’ใ‹ใชใ‚Šๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€Pipelineใฏใƒขใ‚ธใƒฅใƒผใƒซใฎ้€šๅธธใฎใƒ•ใƒญใƒผใ‚’`nn.Sequential`ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅ†ๆ›ธใ่พผใ‚€ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใฎ่จญ่จˆใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใŒๅฟ…่ฆใงใ™ใ€‚ +- ็พๅœจใ€Pipeline APIใฏ้žๅธธใซๅˆถ้™็š„ใงใ™ใ€‚ๆœ€ๅˆใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚นใƒ†ใƒผใ‚ธใซๆธกใ•ใ‚Œใ‚‹Pythonๅค‰ๆ•ฐใฎใ‚ปใƒƒใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใ€ๅ›ž้ฟ็ญ–ใ‚’่ฆ‹ใคใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚็พๅœจใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใงใฏใ€ๅ”ฏไธ€ใฎใƒ†ใƒณใ‚ฝใƒซใพใŸใฏใƒ†ใƒณใ‚ฝใƒซใฎใ‚ฟใƒ—ใƒซใ‚’ๅ…ฅๅŠ›ใจๅ‡บๅŠ›ใจใ—ใฆ่ฆๆฑ‚ใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ†ใƒณใ‚ฝใƒซใฏใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ๆœ€ๅˆใฎๆฌกๅ…ƒใจใ—ใฆๆŒใฃใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใƒŸใƒ‹ใƒใƒƒใƒใ‚’ใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ๅฏ่ƒฝใชๆ”นๅ–„็‚นใซใคใ„ใฆใฏใ€ใ“ใกใ‚‰ใฎ่ญฐ่ซ–ใŒ่กŒใ‚ใ‚Œใฆใ„ใพใ™๏ผšhttps://github.com/pytorch/pytorch/pull/50693 +- ใƒ‘ใ‚คใƒ—ใ‚นใƒ†ใƒผใ‚ธใฎใƒฌใƒ™ใƒซใงใฎๆกไปถไป˜ใๅˆถๅพกใƒ•ใƒญใƒผใฏไธๅฏ่ƒฝใงใ™ใ€‚ไพ‹ใˆใฐใ€T5ใฎใ‚ˆใ†ใชใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฏใ€ๆกไปถไป˜ใใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚นใƒ†ใƒผใ‚ธใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ็‰นๅˆฅใชๅ›ž้ฟ็ญ–ใŒๅฟ…่ฆใงใ™ใ€‚ +- ๅ„ใƒฌใ‚คใƒคใƒผใ‚’้…็ฝฎใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€1ใคใฎใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใŒไป–ใฎใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใซใชใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +VarunaใจSageMakerใจใฎๅฎŸ้จ“ใฏใพใ ่กŒใฃใฆใ„ใพใ›ใ‚“ใŒใ€ๅฝผใ‚‰ใฎ่ซ–ๆ–‡ใซใ‚ˆใ‚Œใฐใ€ไธŠ่จ˜ใง่ฟฐในใŸๅ•้กŒใฎใƒชใ‚นใƒˆใ‚’ๅ…‹ๆœใ—ใ€ใƒฆใƒผใ‚ถใƒผใฎใƒขใƒ‡ใƒซใซใฏใฏใ‚‹ใ‹ใซๅฐใ•ใชๅค‰ๆ›ดใ—ใ‹ๅฟ…่ฆใจใ—ใชใ„ใจๅ ฑๅ‘Šใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ๅฎŸ่ฃ…๏ผš + +- [Pytorch](https://pytorch.org/docs/stable/pipeline.html) (initial support in pytorch-1.8, and progressively getting improved in 1.9 and more so in 1.10). Some [examples](https://github.com/pytorch/pytorch/blob/master/benchmarks/distributed/pipeline/pipe.py) +- [DeepSpeed](https://www.deepspeed.ai/tutorials/pipeline/) +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) has an internal implementation - no API. +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) - this is a proprietary solution that can only be used on AWS. +- [OSLO](https://github.com/tunib-ai/oslo) - ใ“ใฎๅฎŸ่ฃ…ใฏใ€Hugging Face TransformersใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ + +๐Ÿค— Transformersใฎใ‚นใƒ†ใƒผใ‚ฟใ‚น: ใ“ใฎๅŸท็ญ†ๆ™‚็‚นใงใฏใ€ใ„ใšใ‚Œใฎใƒขใƒ‡ใƒซใ‚‚ๅฎŒๅ…จใชPP๏ผˆใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๅ‡ฆ็†๏ผ‰ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ใ€‚GPT2ใƒขใƒ‡ใƒซใจT5ใƒขใƒ‡ใƒซใฏๅ˜็ด”ใชMP๏ผˆใƒขใƒ‡ใƒซไธฆๅˆ—ๅ‡ฆ็†๏ผ‰ใ‚ตใƒใƒผใƒˆใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ไธปใช้šœๅฎณใฏใ€ใƒขใƒ‡ใƒซใ‚’`nn.Sequential`ใซๅค‰ๆ›ใงใใšใ€ใ™ในใฆใฎๅ…ฅๅŠ›ใŒใƒ†ใƒณใ‚ฝใƒซใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚็พๅœจใฎใƒขใƒ‡ใƒซใซใฏใ€ๅค‰ๆ›ใ‚’้žๅธธใซ่ค‡้›‘ใซใ™ใ‚‹ๅคšใใฎๆฉŸ่ƒฝใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ใ“ใ‚Œใ‚‰ใ‚’ๅ‰Š้™คใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไป–ใฎใ‚ขใƒ—ใƒญใƒผใƒ๏ผš + +DeepSpeedใ€Varunaใ€ใŠใ‚ˆใณSageMakerใฏใ€[ไบคไบ’ใซใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅฎŸ่กŒ](https://docs.aws.amazon.com/sagemaker/latest/dg/model-parallel-core-features.html)ใ™ใ‚‹ใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ“ใ“ใงใฏใ€ใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅ„ชๅ…ˆใ•ใ›ใฆใƒใƒ–ใƒซ๏ผˆใ‚ขใ‚คใƒ‰ใƒซๆ™‚้–“๏ผ‰ใ‚’ใ•ใ‚‰ใซๆœ€ๅฐ้™ใซๆŠ‘ใˆใพใ™ใ€‚ + +Varunaใฏใ€ๆœ€้ฉใชใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’็™บ่ฆ‹ใ™ใ‚‹ใŸใ‚ใซใ‚ทใƒŸใƒฅใƒฌใƒผใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใฆใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’ใ•ใ‚‰ใซๆ”นๅ–„ใ—ใ‚ˆใ†ใจใ—ใพใ™ใ€‚ + +OSLOใฏใ€`nn.Sequential`ใฎๅค‰ๆ›ใชใ—ใงTransformersใซๅŸบใฅใใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๅ‡ฆ็†ใ‚’ๅฎŸ่ฃ…ใ—ใฆใ„ใพใ™ใ€‚ + +## Tensor Parallelism + +ใƒ†ใƒณใ‚ฝใƒซไธฆๅˆ—ๅ‡ฆ็†ใงใฏใ€ๅ„GPUใŒใƒ†ใƒณใ‚ฝใƒซใฎใ‚นใƒฉใ‚คใ‚นใฎใฟใ‚’ๅ‡ฆ็†ใ—ใ€ๅ…จไฝ“ใŒๅฟ…่ฆใชๆ“ไฝœใฎใŸใ‚ใซใฎใฟๅฎŒๅ…จใชใƒ†ใƒณใ‚ฝใƒซใ‚’้›†็ด„ใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€[Megatron-LM](https://github.com/NVIDIA/Megatron-LM)่ซ–ๆ–‡ใ‹ใ‚‰ใฎใ‚ณใƒณใ‚ปใƒ—ใƒˆใจๅ›ณใ‚’ไฝฟ็”จใ—ใพใ™๏ผš[GPUใ‚ฏใƒฉใ‚นใ‚ฟใงใฎๅŠน็އ็š„ใชๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://huggingface.co/papers/2104.04473)ใ€‚ + +ใฉใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใฎไธป่ฆใชๆง‹็ฏ‰่ฆ็ด ใฏใ€ๅฎŒๅ…จใซๆŽฅ็ถšใ•ใ‚ŒใŸ`nn.Linear`ใซ็ถšใ้ž็ทšๅฝขใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณ`GeLU`ใงใ™ใ€‚ + +Megatronใฎ่ซ–ๆ–‡ใฎ่กจ่จ˜ๆณ•ใซๅพ“ใฃใฆใ€่กŒๅˆ—ใฎไน—็ฎ—้ƒจๅˆ†ใ‚’`Y = GeLU(XA)`ใจๆ›ธใใ“ใจใŒใงใใพใ™ใ€‚ใ“ใ“ใงใ€`X`ใจ`Y`ใฏๅ…ฅๅŠ›ใƒ™ใ‚ฏใƒˆใƒซใจๅ‡บๅŠ›ใƒ™ใ‚ฏใƒˆใƒซใงใ€`A`ใฏ้‡ใฟ่กŒๅˆ—ใงใ™ใ€‚ + +่กŒๅˆ—ใฎ่จˆ็ฎ—ใ‚’่กŒๅˆ—ๅฝขๅผใง่ฆ‹ใ‚‹ใจใ€่กŒๅˆ—ไน—็ฎ—ใ‚’่ค‡ๆ•ฐใฎGPUใงๅˆ†ๅ‰ฒใงใใ‚‹ๆ–นๆณ•ใŒ็ฐกๅ˜ใซ็†่งฃใงใใพใ™๏ผš +![Parallel GEMM](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_gemm.png) + +้‡ใฟ่กŒๅˆ—`A`ใ‚’`N`ๅ€‹ใฎGPUใซๅฏพใ—ใฆๅˆ—ใ”ใจใซๅˆ†ๅ‰ฒใ—ใ€ไธฆๅˆ—ใง่กŒๅˆ—ไน—็ฎ—`XA_1`ใ‹ใ‚‰`XA_n`ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจใ€`N`ๅ€‹ใฎๅ‡บๅŠ›ใƒ™ใ‚ฏใƒˆใƒซ`Y_1ใ€Y_2ใ€...ใ€Y_n`ใŒๅพ—ใ‚‰ใ‚Œใ€ใใ‚Œใ‚‰ใ‚’็‹ฌ็ซ‹ใ—ใฆ`GeLU`ใซไพ›็ตฆใงใใพใ™๏ผš +![็‹ฌ็ซ‹ใ—ใŸGeLU](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-independent-gelu.png) + +ใ“ใฎๅŽŸ็†ใ‚’ไฝฟ็”จใ—ใฆใ€ๆœ€ๅพŒใพใงๅŒๆœŸใŒๅฟ…่ฆใชใ„ใพใพใ€ไปปๆ„ใฎๆทฑใ•ใฎMLPใ‚’ๆ›ดๆ–ฐใงใใพใ™ใ€‚Megatron-LMใฎ่‘—่€…ใฏใใฎใŸใ‚ใฎๆœ‰็”จใชใ‚คใƒฉใ‚นใƒˆใ‚’ๆไพ›ใ—ใฆใ„ใพใ™๏ผš +![ไธฆๅˆ—ใ‚ทใƒฃใƒผใƒ‰ๅ‡ฆ็†](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_shard_processing.png) + +ใƒžใƒซใƒใƒ˜ใƒƒใƒ‰ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒฌใ‚คใƒคใƒผใ‚’ไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใ“ใจใฏใ•ใ‚‰ใซ็ฐกๅ˜ใงใ™ใ€‚ใใ‚Œใ‚‰ใฏๆ—ขใซ่ค‡ๆ•ฐใฎ็‹ฌ็ซ‹ใ—ใŸใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€ๆœฌ่ณช็š„ใซไธฆๅˆ—ใงใ™๏ผ +![ไธฆๅˆ—ใ‚ปใƒซใƒ•ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_self_attention.png) + +็‰นๅˆฅใช่€ƒๆ…ฎไบ‹้ …๏ผšTPใซใฏ้žๅธธใซ้ซ˜้€Ÿใชใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใŒๅฟ…่ฆใงใ‚ใ‚Šใ€ใ—ใŸใŒใฃใฆ1ใคใฎใƒŽใƒผใƒ‰ใ‚’่ถ…ใˆใฆTPใ‚’ๅฎŸ่กŒใ—ใชใ„ใ“ใจใŒใŠๅ‹งใ‚ใ•ใ‚Œใพใ›ใ‚“ใ€‚ๅฎŸ้š›ใซใฏใ€1ใคใฎใƒŽใƒผใƒ‰ใซ4ใคใฎGPUใŒใ‚ใ‚‹ๅ ดๅˆใ€ๆœ€ๅคงใฎTPๅบฆๆ•ฐใฏ4ใงใ™ใ€‚TPๅบฆๆ•ฐ8ใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€ๅฐ‘ใชใใจใ‚‚8ใคใฎGPUใ‚’ๆŒใคใƒŽใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏใ€ๅ…ƒใฎใ‚ˆใ‚Š่ฉณ็ดฐใช[TPใฎๆฆ‚่ฆ](https://github.com/huggingface/transformers/issues/10321#issuecomment-783543530)ใซๅŸบใฅใ„ใฆใ„ใพใ™ใ€‚ +by [@anton-l](https://github.com/anton-l)ใ€‚ + +SageMakerใฏใ€ใ‚ˆใ‚ŠๅŠน็އ็š„ใชๅ‡ฆ็†ใฎใŸใ‚ใซTPใจDPใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + +ไปฃๆ›ฟๅ๏ผš +- [DeepSpeed](https://github.com/deepspeedai/DeepSpeed)ใฏใ“ใ‚Œใ‚’ใ€Œใƒ†ใƒณใ‚ฝใƒซใ‚นใƒฉใ‚คใ‚ทใƒณใ‚ฐใ€ใจๅ‘ผใณใพใ™ใ€‚่ฉณ็ดฐใฏ[DeepSpeedใฎ็‰นๅพด](https://www.deepspeed.ai/training/#model-parallelism)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +ๅฎŸ่ฃ…ไพ‹: +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM)ใซใฏใ€ใƒขใƒ‡ใƒซๅ›บๆœ‰ใฎๅ†…้ƒจๅฎŸ่ฃ…ใŒใ‚ใ‚Šใพใ™ใ€‚ +- [parallelformers](https://github.com/tunib-ai/parallelformers)๏ผˆ็พๆ™‚็‚นใงใฏๆŽจ่ซ–ใฎใฟ๏ผ‰ใ€‚ +- [SageMaker](https://huggingface.co/papers/2111.05972) - ใ“ใ‚ŒใฏAWSใงใฎใฟไฝฟ็”จใงใใ‚‹ใƒ—ใƒญใƒ—ใƒฉใ‚คใ‚จใ‚ฟใƒชใชใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใงใ™ใ€‚ +- [OSLO](https://github.com/tunib-ai/oslo)ใซใฏใ€TransformersใซๅŸบใฅใ„ใŸใƒ†ใƒณใ‚ฝใƒซไธฆๅˆ—ๅฎŸ่ฃ…ใŒใ‚ใ‚Šใพใ™ใ€‚ + +๐Ÿค— Transformersใฎ็Šถๆณ: +- ใ‚ณใ‚ข: ใพใ ใ‚ณใ‚ขใซใฏๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ +- ใŸใ ใ—ใ€ๆŽจ่ซ–ใŒๅฟ…่ฆใชๅ ดๅˆใ€[parallelformers](https://github.com/tunib-ai/parallelformers)ใฏใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใซๅฏพใ—ใฆใ‚ตใƒใƒผใƒˆใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใ‚ŒใŒใ‚ณใ‚ขใซๅฎŸ่ฃ…ใ•ใ‚Œใ‚‹ใพใงใ€ใ“ใ‚Œใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใใ—ใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒขใƒผใƒ‰ใ‚‚ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใ„ใพใ™ใ€‚ +- Deepspeed-Inferenceใงใฏใ€BERTใ€GPT-2ใ€ใŠใ‚ˆใณGPT-Neoใƒขใƒ‡ใƒซใ‚’CUDAใ‚ซใƒผใƒใƒซใƒ™ใƒผใ‚นใฎ้ซ˜้€ŸๆŽจ่ซ–ใƒขใƒผใƒ‰ใงใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚่ฉณ็ดฐใฏ[ใ“ใกใ‚‰](https://www.deepspeed.ai/tutorials/inference-tutorial/)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## DP+PP + +DeepSpeedใฎ[ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://www.deepspeed.ai/tutorials/pipeline/)ใ‹ใ‚‰ใฎๆฌกใฎๅ›ณใฏใ€DPใ‚’PPใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ + +![dp-pp-2d](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-zero-dp-pp.png) + +ใ“ใ“ใง้‡่ฆใชใฎใฏใ€DPใƒฉใƒณใ‚ฏ0ใŒGPU2ใ‚’่ฆ‹ใˆใชใใ—ใ€DPใƒฉใƒณใ‚ฏ1ใŒGPU3ใ‚’่ฆ‹ใˆใชใใ™ใ‚‹ใ“ใจใงใ™ใ€‚DPใซใจใฃใฆใ€ๅญ˜ๅœจใ™ใ‚‹ใฎใฏGPU 0 ใจ 1 ใฎใฟใงใ€ใใ‚Œใ‚‰ใฎ2ใคใฎGPUใฎใ‚ˆใ†ใซใƒ‡ใƒผใ‚ฟใ‚’ไพ›็ตฆใ—ใพใ™ใ€‚GPU0ใฏPPใ‚’ไฝฟ็”จใ—ใฆGPU2ใซไธ€้ƒจใฎ่ฒ ่ทใ‚’ใ€Œ็ง˜ๅฏ†่ฃใซใ€ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใ€GPU1ใ‚‚ๅŒๆง˜ใซGPU3ใ‚’ๆ”ฏๆดใซๅผ•ใๅ…ฅใ‚Œใพใ™ใ€‚ + +ๅ„ๆฌกๅ…ƒใซใฏๅฐ‘ใชใใจใ‚‚2ใคใฎGPUใŒๅฟ…่ฆใงใ™ใฎใงใ€ใ“ใ“ใงใฏๅฐ‘ใชใใจใ‚‚4ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ + +ๅฎŸ่ฃ…ไพ‹: +- [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) +- [OSLO](https://github.com/tunib-ai/oslo) + +๐Ÿค— Transformersใฎ็Šถๆณ: ใพใ ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ + +## DP+PP+TP + +ใ•ใ‚‰ใซๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ†ใŸใ‚ใซใ€3Dใƒ‘ใƒฉใƒฌใƒชใ‚บใƒ ใ‚’ไฝฟ็”จใ—ใ€PPใ‚’TPใจDPใจ็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ใ“ใ‚Œใฏๆฌกใฎๅ›ณใง็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +![dp-pp-tp-3d](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-deepspeed-3d.png) + +ใ“ใฎๅ›ณใฏ[3Dใƒ‘ใƒฉใƒฌใƒชใ‚บใƒ ๏ผšๅ…†ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒขใƒ‡ใƒซใธใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐ](https://www.microsoft.com/en-us/research/blog/deepspeed-extreme-scale-model-training-for-everyone/)ใจใ„ใ†ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ‹ใ‚‰ๅ–ๅพ—ใ•ใ‚ŒใŸใ‚‚ใฎใงใ€ใŠใ™ใ™ใ‚ใฎ่ชญใฟ็‰ฉใงใ™ใ€‚ + +ๅ„ๆฌกๅ…ƒใซใฏๅฐ‘ใชใใจใ‚‚2ใคใฎGPUใŒๅฟ…่ฆใงใ™ใฎใงใ€ใ“ใ“ใงใฏๅฐ‘ใชใใจใ‚‚8ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ + +ๅฎŸ่ฃ…ไพ‹: +- [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) - DeepSpeedใซใฏใ€ใ•ใ‚‰ใซๅŠน็އ็š„ใชDPใงใ‚ใ‚‹ZeRO-DPใจๅ‘ผใฐใ‚Œใ‚‹ใ‚‚ใฎใ‚‚ๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) +- [OSLO](https://github.com/tunib-ai/oslo) + +๐Ÿค— Transformersใฎ็Šถๆณ: ใพใ ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚PPใจTPใŒใชใ„ใŸใ‚ใ€‚ + +## ZeRO DP+PP+TP + +DeepSpeedใฎไธป่ฆใชๆฉŸ่ƒฝใฎ1ใคใฏZeROใงใ€ใ“ใ‚ŒใฏDPใฎๆ‹กๅผตๆฉŸ่ƒฝใงใ™ใ€‚ใ“ใ‚Œใซใคใ„ใฆใฏใ™ใงใซใ€ŒZeROใƒ‡ใƒผใ‚ฟไธฆๅˆ—ๅŒ–ใ€ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใพใ™ใ€‚้€šๅธธใ€ใ“ใ‚Œใฏๅ˜็‹ฌใงๅ‹•ไฝœใ™ใ‚‹ๆฉŸ่ƒฝใงใ€PPใ‚„TPใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ—ใ‹ใ—ใ€PPใจTPใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +ZeRO-DPใŒPPใจ็ต„ใฟๅˆใ‚ใ•ใ‚Œใ‚‹ๅ ดๅˆใ€้€šๅธธใฏZeROใ‚นใƒ†ใƒผใ‚ธ1๏ผˆใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ๏ผ‰ใฎใฟใŒๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ + +ZeROใ‚นใƒ†ใƒผใ‚ธ2๏ผˆๅ‹พ้…ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ๏ผ‰ใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๅŒ–ใจ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จใ™ใ‚‹็†่ซ–็š„ใชๅฏ่ƒฝๆ€งใฏใ‚ใ‚Šใพใ™ใŒใ€ๆ€ง่ƒฝใซๆ‚ชๅฝฑ้Ÿฟใ‚’ๅŠใผใ—ใพใ™ใ€‚ๅ„ใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใ”ใจใซๅ‹พ้…ใ‚’ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซใ€ๅ‹พ้…ใ‚’้›†็ด„ใ™ใ‚‹ใŸใ‚ใฎ่ฟฝๅŠ ใฎใƒชใƒ€ใ‚ฏใ‚ทใƒงใƒณใ‚นใ‚ญใƒฃใƒƒใ‚ฟใƒผ้›†่จˆใŒๅฟ…่ฆใงใ€้€šไฟกใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๅŒ–ใฎๆ€ง่ณชไธŠใ€ๅฐใ•ใชใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใŒไฝฟ็”จใ•ใ‚Œใ€่จˆ็ฎ—ใฎ้›†ไธญๅบฆ๏ผˆใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒใ‚ตใ‚คใ‚บ๏ผ‰ใ‚’ใƒใƒฉใƒณใ‚นใซใ‹ใ‘ใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใƒใƒ–ใƒซ๏ผˆใƒžใ‚คใ‚ฏใƒญใƒใƒƒใƒๆ•ฐ๏ผ‰ใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใ‚‹ใ“ใจใซ็„ฆ็‚นใŒๅฝ“ใฆใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใ‚‰ใฎ้€šไฟกใ‚ณใ‚นใƒˆใฏๅฝฑ้Ÿฟใ‚’ๅŠใผใ™ใงใ—ใ‚‡ใ†ใ€‚ + +ใ•ใ‚‰ใซใ€PPใซใฏ้€šๅธธใ‚ˆใ‚Šใ‚‚ๅฐ‘ใชใ„ๅฑคใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ใƒกใƒขใƒชใฎ็ฏ€็ด„ใฏใใ‚Œใปใฉๅคงใใใ‚ใ‚Šใพใ›ใ‚“ใ€‚PPใฏๆ—ขใซๅ‹พ้…ใ‚ตใ‚คใ‚บใ‚’ใ€Œ1/PPใ€ใซๅ‰Šๆธ›ใ™ใ‚‹ใŸใ‚ใ€ๅ‹พ้…ใ‚ทใƒฃใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใฎ็ฏ€็ด„ใฏ็ด”็ฒ‹ใชDPใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซ้‡่ฆใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ZeROใ‚นใƒ†ใƒผใ‚ธ3ใ‚‚ๅŒๆง˜ใฎ็†็”ฑใง้ฉใ—ใฆใ„ใพใ›ใ‚“ - ใ‚ˆใ‚ŠๅคšใใฎใƒŽใƒผใƒ‰้–“้€šไฟกใŒๅฟ…่ฆใงใ™ใ€‚ + +ใใ—ใฆใ€ZeROใ‚’ๆŒใฃใฆใ„ใ‚‹ใฎใงใ€ใ‚‚ใ†ไธ€ใคใฎๅˆฉ็‚นใฏZeRO-Offloadใงใ™ใ€‚ใ“ใ‚Œใฏใ‚นใƒ†ใƒผใ‚ธ1ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ‚นใƒ†ใƒผใƒˆใ‚’CPUใซใ‚ชใƒ•ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +ๅฎŸ่ฃ…ไพ‹: +- [Megatron-DeepSpeed](https://github.com/microsoft/Megatron-DeepSpeed)ใจ[BigScienceใ‹ใ‚‰ใฎMegatron-Deepspeed](https://github.com/bigscience-workshop/Megatron-DeepSpeed)ใฏใ€ๅ‰่€…ใฎใƒชใƒใ‚ธใƒˆใƒชใฎใƒ•ใ‚ฉใƒผใ‚ฏใงใ™ใ€‚ +- [OSLO](https://github.com/tunib-ai/oslo) + +้‡่ฆใช่ซ–ๆ–‡: + +- [DeepSpeedใจMegatronใ‚’ไฝฟ็”จใ—ใŸMegatron-Turing NLG 530Bใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](https://huggingface.co/papers/2201.11990) + +๐Ÿค— Transformersใฎ็Šถๆณ: ใพใ ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚PPใจTPใŒใชใ„ใŸใ‚ใ€‚ + + +## FlexFlow + +[FlexFlow](https://github.com/flexflow/FlexFlow)ใฏใ€ใ‚ใšใ‹ใซ็•ฐใชใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใงไธฆๅˆ—ๅŒ–ใฎๅ•้กŒใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ + +่ซ–ๆ–‡: [Zhihao Jiaใ€Matei Zahariaใ€Alex Aikenใซใ‚ˆใ‚‹ "Deep Neural Networksใฎใƒ‡ใƒผใ‚ฟใจใƒขใƒ‡ใƒซใฎไธฆๅˆ—ๅŒ–ใ‚’่ถ…ใˆใฆ"](https://huggingface.co/papers/1807.05358) + +FlexFlowใฏใ€ใ‚ตใƒณใƒ—ใƒซ-ใ‚ชใƒšใƒฌใƒผใ‚ฟ-ๅฑžๆ€ง-ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ4Dไธฆๅˆ—ๅŒ–ใ‚’่กŒใ„ใพใ™ใ€‚ + +1. ใ‚ตใƒณใƒ—ใƒซ = ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ๅŒ–๏ผˆใ‚ตใƒณใƒ—ใƒซๅ˜ไฝใฎไธฆๅˆ—ๅŒ–๏ผ‰ +2. ใ‚ชใƒšใƒฌใƒผใ‚ฟ = ๅ˜ไธ€ใฎๆ“ไฝœใ‚’ใ„ใใคใ‹ใฎใ‚ตใƒ–ๆ“ไฝœใซไธฆๅˆ—ๅŒ– +3. ๅฑžๆ€ง = ใƒ‡ใƒผใ‚ฟไธฆๅˆ—ๅŒ–๏ผˆ้•ทใ•ๆ–นๅ‘ใฎไธฆๅˆ—ๅŒ–๏ผ‰ +4. ใƒ‘ใƒฉใƒกใƒผใ‚ฟ = ใƒขใƒ‡ใƒซไธฆๅˆ—ๅŒ–๏ผˆๆฌกๅ…ƒใซ้–ขไฟ‚ใชใใ€ๆฐดๅนณใพใŸใฏๅž‚็›ด๏ผ‰ + +ไพ‹: +* ใ‚ตใƒณใƒ—ใƒซ + +ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ท512ใฎ10ใƒใƒƒใƒใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚ใ“ใ‚Œใ‚‰ใ‚’ใ‚ตใƒณใƒ—ใƒซๆฌกๅ…ƒใง2ใคใฎใƒ‡ใƒใ‚คใ‚นใซไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใจใ€10 x 512ใŒ5 x 2 x 512ใซใชใ‚Šใพใ™ใ€‚ + +* ใ‚ชใƒšใƒฌใƒผใ‚ฟ + +ๅฑคๆญฃ่ฆๅŒ–ใ‚’่กŒใ†ๅ ดๅˆใ€ใพใšstdใ‚’่จˆ็ฎ—ใ—ใ€ๆฌกใซmeanใ‚’่จˆ็ฎ—ใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’ๆญฃ่ฆๅŒ–ใงใใพใ™ใ€‚ใ‚ชใƒšใƒฌใƒผใ‚ฟใฎไธฆๅˆ—ๅŒ–ใซใ‚ˆใ‚Šใ€stdใจmeanใ‚’ไธฆๅˆ—ใซ่จˆ็ฎ—ใงใใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ‚ชใƒšใƒฌใƒผใ‚ฟๆฌกๅ…ƒใง2ใคใฎใƒ‡ใƒใ‚คใ‚น๏ผˆcuda:0ใ€cuda:1๏ผ‰ใซไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใจใ€ๆœ€ๅˆใซๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใ‚’ไธกๆ–นใฎใƒ‡ใƒใ‚คใ‚นใซใ‚ณใƒ”ใƒผใ—ใ€cuda:0ใงstdใ‚’่จˆ็ฎ—ใ—ใ€cuda:1ใงmeanใ‚’ๅŒๆ™‚ใซ่จˆ็ฎ—ใ—ใพใ™ใ€‚ + +* ๅฑžๆ€ง + +10ใƒใƒƒใƒใฎ512้•ทใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใ‚’ๅฑžๆ€งๆฌกๅ…ƒใง2ใคใฎใƒ‡ใƒใ‚คใ‚นใซไธฆๅˆ—ๅŒ–ใ™ใ‚‹ใจใ€10 x 512ใŒ10 x 2 x 256ใซใชใ‚Šใพใ™ใ€‚ + +* ใƒ‘ใƒฉใƒกใƒผใ‚ฟ + +ใ“ใ‚Œใฏใƒ†ใƒณใ‚ฝใƒซใƒขใƒ‡ใƒซใฎไธฆๅˆ—ๅŒ–ใพใŸใฏๅ˜็ด”ใชๅฑคใ”ใจใฎใƒขใƒ‡ใƒซใฎไธฆๅˆ—ๅŒ–ใจไผผใฆใ„ใพใ™ใ€‚ + +ใ“ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎ้‡่ฆๆ€งใฏใ€๏ผˆ1๏ผ‰GPU/TPU/CPUๅฏพ๏ผˆ2๏ผ‰RAM/DRAMๅฏพ๏ผˆ3๏ผ‰้ซ˜้€Ÿๅ†…้ƒจๆŽฅ็ถš/ไฝŽ้€Ÿๅค–้ƒจๆŽฅ็ถšใชใฉใฎใƒชใ‚ฝใƒผใ‚นใ‚’ๅ–ใ‚Šใ€ใ“ใ‚Œใ‚‰ใ™ในใฆใ‚’ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใซใ‚ˆใฃใฆ่‡ชๅ‹•็š„ใซๆœ€้ฉๅŒ–ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใฎไธฆๅˆ—ๅŒ–ใ‚’ใฉใ“ใงไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ็š„ใซๆฑบๅฎšใ—ใพใ™ใ€‚ + +้žๅธธใซ้‡่ฆใชๅด้ขใฎ1ใคใฏใ€FlexFlowใฏ้™็š„ใงๅ›บๅฎšใฎใƒฏใƒผใ‚ฏใƒญใƒผใƒ‰ใ‚’ๆŒใคใƒขใƒ‡ใƒซใฎใŸใ‚ใซ่จญ่จˆใ•ใ‚ŒใฆใŠใ‚Šใ€ๅ‹•็š„ใชๅ‹•ไฝœใ‚’ๆŒใคใƒขใƒ‡ใƒซใฏใ‚คใƒ†ใƒฌใƒผใ‚ทใƒงใƒณใ”ใจใซ็•ฐใชใ‚‹ไธฆๅˆ—ๅŒ–ๆˆฆ็•ฅใ‚’ๅฅฝใ‚€ๅ ดๅˆใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚ + +ใ—ใŸใŒใฃใฆใ€ใ“ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎ็ด„ๆŸใฏ้žๅธธใซ้ญ…ๅŠ›็š„ใงใ™ใ€‚้ธๆŠžใ—ใŸใ‚ฏใƒฉใ‚นใ‚ฟใง30ๅˆ†้–“ใฎใ‚ทใƒŸใƒฅใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅฎŸ่กŒใ—ใ€ใ“ใฎ็‰นๅฎšใฎ็’ฐๅขƒใ‚’ๆœ€้ฉใซๅˆฉ็”จใ™ใ‚‹ใŸใ‚ใฎๆœ€่‰ฏใฎๆˆฆ็•ฅใ‚’ๆไพ›ใ—ใพใ™ใ€‚้ƒจๅˆ†ใ‚’่ฟฝๅŠ /ๅ‰Š้™ค/็ฝฎๆ›ใ™ใ‚‹ใจใ€ใใ‚Œใซๅฏพใ—ใฆๅฎŸ่กŒใ—ใฆๅ†ๆœ€้ฉๅŒ–ใƒ—ใƒฉใƒณใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใใฎๅพŒใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใพใ™ใ€‚็•ฐใชใ‚‹ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใซใฏ็‹ฌ่‡ชใฎๆœ€้ฉๅŒ–ใŒใ‚ใ‚Šใพใ™ใ€‚ + +๐Ÿค— Transformersใฎ็พๅœจใฎ็Šถๆณ: ใพใ ็ตฑๅˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใ™ใงใซ[transformers.utils.fx](https://github.com/huggingface/transformers/blob/master/src/transformers/utils/fx.py)ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใŒFXใƒˆใƒฌใƒผใ‚นๅฏ่ƒฝใงใ‚ใ‚‹ใŸใ‚ใ€FlexFlowใ‚’ๅ‹•ไฝœใ•ใ›ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชๆ‰‹้ †ใ‚’่ชฐใ‹ใŒ่ฆ‹ใคใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Which Strategy To Use When + +ใ“ใ“ใงใฏใ€ใฉใฎไธฆๅˆ—ๅŒ–ๆˆฆ็•ฅใ‚’ใ„ใคไฝฟ็”จใ™ใ‚‹ใ‹ใฎ้žๅธธใซใŠใŠใพใ‹ใชใ‚ขใ‚ฆใƒˆใƒฉใ‚คใƒณใ‚’็คบใ—ใพใ™ใ€‚ๅ„ใƒชใ‚นใƒˆใฎๆœ€ๅˆใŒ้€šๅธธใ‚ˆใ‚Šใ‚‚้€Ÿใ„ใ“ใจใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ + +**โ‡จ ๅ˜ไธ€GPU** + +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€GPUใซๅŽใพใ‚‹ๅ ดๅˆ๏ผš + + 1. ้€šๅธธใฎไฝฟ็”จ + +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€GPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. ZeRO + CPUใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ใ—ใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใงNVMeใ‚’ใ‚ชใƒ•ใƒญใƒผใƒ‰ + 2. ไธŠ่จ˜ใซๅŠ ใˆใฆใ€ๆœ€ๅคงใฎใƒฌใ‚คใƒคใƒผใŒๅ˜ไธ€GPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆใ€[Memory Centric Tiling](https://deepspeed.readthedocs.io/en/latest/zero3.html#memory-centric-tiling)๏ผˆ่ฉณ็ดฐใฏไปฅไธ‹ๅ‚็…ง๏ผ‰ใ‚’ๆœ‰ๅŠนๅŒ– + +* ๆœ€ๅคงใฎใƒฌใ‚คใƒคใƒผใŒๅ˜ไธ€GPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. ZeROใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆ - TPใ‚’ๆœ‰ๅŠนๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใชใœใชใ‚‰ใ€PPใ ใ‘ใงใฏๅŽใ‚ใ‚‹ใ“ใจใŒใงใใชใ„ใ‹ใ‚‰ใงใ™ใ€‚ + 2. ZeROใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ไธŠ่จ˜ใฎใ€Œๅ˜ไธ€GPUใ€ใฎใ‚จใƒณใƒˆใƒชใจๅŒใ˜ใ‚‚ใฎใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ + +**โ‡จ ๅ˜ไธ€ใƒŽใƒผใƒ‰/ใƒžใƒซใƒGPU** + +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€GPUใซๅŽใพใ‚‹ๅ ดๅˆ๏ผš + + 1. DDP - ๅˆ†ๆ•ฃใƒ‡ใƒผใ‚ฟไธฆๅˆ— + 2. ZeRO - ็Šถๆณใจไฝฟ็”จใ•ใ‚Œใ‚‹ๆง‹ๆˆใซไพๅญ˜ใ—ใฆ้€Ÿใ„ใ‹ใฉใ†ใ‹ใŒ็•ฐใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ + +* ใƒขใƒ‡ใƒซใŒๅ˜ไธ€GPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. PP + 2. ZeRO + 3. TP + + ้žๅธธใซ้ซ˜้€ŸใชใƒŽใƒผใƒ‰ๅ†…ๆŽฅ็ถšใŒNVLINKใพใŸใฏNVSwitchใงใ‚ใ‚‹ๅ ดๅˆใ€ใ“ใ‚Œใ‚‰ใฎใ™ในใฆใฏใปใจใ‚“ใฉๅŒ็ญ‰ใฎๆ€ง่ƒฝใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใŒใชใ„ๅ ดๅˆใ€PPใฏTPใพใŸใฏZeROใ‚ˆใ‚Šใ‚‚้€Ÿใใชใ‚Šใพใ™ใ€‚TPใฎๅบฆๅˆใ„ใ‚‚้•ใ„ใ‚’็”Ÿใ˜ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚็‰นๅฎšใฎใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงๅ‹่€…ใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซๅฎŸ้จ“ใ™ใ‚‹ใฎใŒๆœ€ๅ–„ใงใ™ใ€‚ + + TPใฏใปใจใ‚“ใฉๅธธใซๅ˜ไธ€ใƒŽใƒผใƒ‰ๅ†…ใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใคใพใ‚Šใ€TPใ‚ตใ‚คใ‚บ <= ใƒŽใƒผใƒ‰ใ‚ใŸใ‚ŠใฎGPUใงใ™ใ€‚ + +* ๆœ€ๅคงใฎใƒฌใ‚คใƒคใƒผใŒๅ˜ไธ€GPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆ๏ผš + + 1. ZeROใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆ - TPใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใชใœใชใ‚‰ใ€PPใ ใ‘ใงใฏๅŽใ‚ใ‚‹ใ“ใจใŒใงใใชใ„ใ‹ใ‚‰ใงใ™ใ€‚ + 2. ZeROใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€ไธŠ่จ˜ใฎใ€Œๅ˜ไธ€GPUใ€ใฎใ‚จใƒณใƒˆใƒชใจๅŒใ˜ใ‚‚ใฎใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ + +**โ‡จ ใƒžใƒซใƒใƒŽใƒผใƒ‰/ใƒžใƒซใƒGPU** + +* ้ซ˜้€ŸใชใƒŽใƒผใƒ‰้–“ๆŽฅ็ถšใŒใ‚ใ‚‹ๅ ดๅˆ๏ผš + + 1. ZeRO - ใƒขใƒ‡ใƒซใธใฎใปใจใ‚“ใฉใฎๅค‰ๆ›ดใŒไธ่ฆใงใ™ + 2. PP+TP+DP - ้€šไฟกใŒๅฐ‘ใชใใ€ใƒขใƒ‡ใƒซใซๅคง่ฆๆจกใชๅค‰ๆ›ดใŒๅฟ…่ฆใงใ™ + +* ้…ใ„ใƒŽใƒผใƒ‰้–“ๆŽฅ็ถšใŒใ‚ใ‚Šใ€GPUใƒกใƒขใƒชใŒๅฐ‘ใชใ„ๅ ดๅˆ๏ผš + + 1. DP+PP+TP+ZeRO-1 + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..1a82ebb60d0a3fdbf809467f3e0e61dd4933bd35 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_gpu_one.md @@ -0,0 +1,437 @@ + + +# Methods and tools for efficient training on a single GPU + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ใƒกใƒขใƒชใฎๅˆฉ็”จๅŠน็އใ‚’ๆœ€้ฉๅŒ–ใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใ“ใจใงใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใ‚‹ๅฎŸ็”จ็š„ใชใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซGPUใŒใฉใฎใ‚ˆใ†ใซๅˆฉ็”จใ•ใ‚Œใ‚‹ใ‹ใ‚’็†่งฃใ—ใŸใ„ๅ ดๅˆใฏใ€ๆœ€ๅˆใซใ€Œ[ใƒขใƒ‡ใƒซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่งฃๅ‰–ๅญฆ](model_memory_anatomy)ใ€ใฎใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚ฌใ‚คใƒ‰ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏๅฎŸ็”จ็š„ใชใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใพใ™ใ€‚ + + + +่ค‡ๆ•ฐใฎGPUใ‚’ๆญ่ผ‰ใ—ใŸใƒžใ‚ทใƒณใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ๅ ดๅˆใ€ใ“ใ‚Œใ‚‰ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏไพ็„ถใจใ—ใฆๆœ‰ๅŠนใงใ™ใ€‚ใ•ใ‚‰ใซใ€[ใƒžใƒซใƒGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_many)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹่ฟฝๅŠ ใฎๆ–นๆณ•ใ‚’ๆดป็”จใงใใพใ™ใ€‚ + + + +ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใ€ๅŒๆ™‚ใซ่€ƒๆ…ฎใ™ในใ2ใคใฎๅด้ขใŒใ‚ใ‚Šใพใ™๏ผš + +* ใƒ‡ใƒผใ‚ฟใฎใ‚นใƒซใƒผใƒ—ใƒƒใƒˆ/ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚้–“ +* ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น + +ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆ๏ผˆใ‚ตใƒณใƒ—ใƒซ/็ง’๏ผ‰ใ‚’ๆœ€ๅคงๅŒ–ใ™ใ‚‹ใ“ใจใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใ‚นใƒˆใ‚’ไฝŽๆธ›ใ•ใ›ใพใ™ใ€‚ใ“ใ‚Œใฏไธ€่ˆฌ็š„ใซใ€GPUใ‚’ใงใใ‚‹ใ ใ‘ๅŠนๆžœ็š„ใซๆดป็”จใ—ใ€GPUใƒกใƒขใƒชใ‚’้™็•ŒใพใงๅŸ‹ใ‚ใ‚‹ใ“ใจใซใ‚ˆใฃใฆ้”ๆˆใ•ใ‚Œใพใ™ใ€‚ๅธŒๆœ›ใ™ใ‚‹ใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒGPUใƒกใƒขใƒชใฎๅˆถ้™ใ‚’่ถ…ใˆใ‚‹ๅ ดๅˆใ€ๅ‹พ้…่“„็ฉใชใฉใฎใƒกใƒขใƒชๆœ€้ฉๅŒ–ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใŒๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ—ใ‹ใ—ใ€ๅฅฝใฟใฎใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒใƒกใƒขใƒชใซๅŽใพใ‚‹ๅ ดๅˆใ€ใƒกใƒขใƒชใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’้ฉ็”จใ™ใ‚‹็†็”ฑใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๅคงใใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใงใใ‚‹ใ‹ใ‚‰ใจใ„ใฃใฆใ€ใใ‚Œใ‚’ๅฟ…ใšใ—ใ‚‚ไฝฟ็”จใ™ในใใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎ่ชฟๆ•ดใฎไธ€็’ฐใจใ—ใฆใ€ใฉใฎใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒๆœ€่‰ฏใฎ็ตๆžœใ‚’็”Ÿใฟๅ‡บใ™ใ‹ใ‚’ๆฑบๅฎšใ—ใ€ใƒชใ‚ฝใƒผใ‚นใ‚’้ฉๅˆ‡ใซๆœ€้ฉๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใ‚ซใƒใƒผใ•ใ‚Œใฆใ„ใ‚‹ๆ–นๆณ•ใจใƒ„ใƒผใƒซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใซไธŽใˆใ‚‹ๅฝฑ้ŸฟใซๅŸบใฅใ„ใฆๅˆ†้กžใงใใพใ™๏ผš + + +| Method/tool | Improves training speed | Optimizes memory utilization | +|:-----------------------------------------------------------|:------------------------|:-----------------------------| +| [Batch size choice](#batch-size-choice) | Yes | Yes | +| [Gradient accumulation](#gradient-accumulation) | No | Yes | +| [Gradient checkpointing](#gradient-checkpointing) | No | Yes | +| [Mixed precision training](#mixed-precision-training) | Yes | (No) | +| [Optimizer choice](#optimizer-choice) | Yes | Yes | +| [Data preloading](#data-preloading) | Yes | No | +| [DeepSpeed Zero](#deepspeed-zero) | No | Yes | +| [torch.compile](#using-torchcompile) | Yes | No | + + + +**ๆณจๆ„**: ๅฐใ•ใชใƒขใƒ‡ใƒซใจๅคงใใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใƒกใƒขใƒชใฎ็ฏ€็ด„ใŒ่กŒใ‚ใ‚Œใพใ™ใŒใ€ๅคงใใชใƒขใƒ‡ใƒซใจๅฐใ•ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใƒกใƒขใƒชใฎไฝฟ็”จ้‡ใŒๅข—ๅŠ ใ—ใพใ™ใ€‚ + + + +ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใฏใ€[`Trainer`]ใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ„ใ‚‹ๅ ดๅˆใ‚„ใ€็ด”็ฒ‹ใชPyTorchใƒซใƒผใƒ—ใ‚’่จ˜่ฟฐใ—ใฆใ„ใ‚‹ๅ ดๅˆใฎไธกๆ–นใงๅˆฉ็”จใงใใพใ™ใ€‚่ฉณ็ดฐใชๆœ€้ฉๅŒ–ใฎ่จญๅฎšใซใคใ„ใฆใฏใ€๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ—ใฆ[ใ“ใ‚Œใ‚‰ใฎๆœ€้ฉๅŒ–ใ‚’่จญๅฎšใงใใพใ™](#using--accelerate)ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎๆ–นๆณ•ใŒๅๅˆ†ใชๅˆฉ็›Šใ‚’ใ‚‚ใŸใ‚‰ใ•ใชใ„ๅ ดๅˆใ€ไปฅไธ‹ใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆคœ่จŽใงใใพใ™๏ผš +* [ๅŠน็އ็š„ใชใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ขใƒ—ใƒชใƒ“ใƒซใƒ‰ใ‚’ๅ‚™ใˆใŸใ‚ซใ‚นใ‚ฟใƒ Dockerใ‚ณใƒณใƒ†ใƒŠใฎไฝœๆˆ](#efficient-software-prebuilds) +* [Mixture of Experts๏ผˆMoE๏ผ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’ๆคœ่จŽ](#mixture-of-experts) +* [ใƒขใƒ‡ใƒซใ‚’BetterTransformerใซๅค‰ๆ›ใ—ใฆใ€PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ๆดป็”จ](#using-pytorch-native-attention) + +ๆœ€ๅพŒใซใ€ใ“ใ‚Œใ‚‰ใฎๆ–นๆณ•ใŒใพใ ๅๅˆ†ใงใชใ„ๅ ดๅˆใ€A100ใชใฉใฎใ‚ตใƒผใƒใƒผใ‚ฐใƒฌใƒผใƒ‰GPUใซๅˆ‡ใ‚Šๆ›ฟใˆใฆใ‚‚ใ€ใ•ใ‚‰ใชใ‚‹ๆ”นๅ–„ใŒๅฟ…่ฆใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ใƒžใƒซใƒGPUใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใ‚‚ๆœ‰ๅŠนใงใ‚ใ‚Šใ€[ใƒžใƒซใƒGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_many)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹่ฟฝๅŠ ใฎไธฆๅˆ—ๅŒ–ๆŠ€่ก“ใ‚’ๆดป็”จใงใใพใ™ใ€‚ + +## Batch size choice + +ๆœ€้ฉใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซใ€้ฉๅˆ‡ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’็‰นๅฎšใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚2^Nใฎใ‚ตใ‚คใ‚บใฎใƒใƒƒใƒใ‚ตใ‚คใ‚บใจๅ…ฅๅŠ›/ๅ‡บๅŠ›ใƒ‹ใƒฅใƒผใƒญใƒณๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใฆใ„ใพใ™ใ€‚้€šๅธธใ€ใ“ใ‚Œใฏ8ใฎๅ€ๆ•ฐใงใ™ใŒใ€ไฝฟ็”จใ™ใ‚‹ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใจใƒขใƒ‡ใƒซใฎใƒ‡ใƒผใ‚ฟๅž‹ใซไพๅญ˜ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๅ‚่€ƒใพใงใซใ€NVIDIAใฎ[ๅ…ฅๅŠ›/ๅ‡บๅŠ›ใƒ‹ใƒฅใƒผใƒญใƒณๆ•ฐใฎๆŽจๅฅจไบ‹้ …](https://docs.nvidia.com/deeplearning/performance/dl-performance-fully-connected/index.html#input-features)ใจ[ใƒใƒƒใƒใ‚ตใ‚คใ‚บ](https://docs.nvidia.com/deeplearning/performance/dl-performance-fully-connected/index.html#batch-size)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผˆใ“ใ‚Œใ‚‰ใฏGEMM๏ผˆไธ€่ˆฌ็š„ใช่กŒๅˆ—ไน—็ฎ—๏ผ‰ใซ้–ขไธŽใ—ใพใ™๏ผ‰ใ€‚ + +[Tensor Core่ฆไปถ](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#requirements-tc)ใงใฏใ€ใƒ‡ใƒผใ‚ฟๅž‹ใจใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซๅŸบใฅใ„ใฆไน—ๆ•ฐใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใŸใจใˆใฐใ€fp16ใƒ‡ใƒผใ‚ฟๅž‹ใฎๅ ดๅˆใ€64ใฎๅ€ๆ•ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใพใ™๏ผˆA100 GPUใฎๅ ดๅˆใ‚’้™คใ๏ผ‰ใ€‚ + +ๅฐใ•ใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅ ดๅˆใ€[ๆฌกๅ…ƒ้‡ๅญๅŒ–ๅŠนๆžœ](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#dim-quantization)ใ‚‚่€ƒๆ…ฎใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใฏใ‚ฟใ‚คใƒชใƒณใ‚ฐใŒ่กŒใ‚ใ‚Œใ€้ฉๅˆ‡ใชไน—ๆ•ฐใŒๅคงๅน…ใช้ซ˜้€ŸๅŒ–ใ‚’ใ‚‚ใŸใ‚‰ใ™ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Gradient Accumulation + +**ๅ‹พ้…่“„็ฉ**ใƒกใ‚ฝใƒƒใƒ‰ใฏใ€GPUใฎใƒกใƒขใƒชๅฎน้‡ใฎๅˆถ็ด„ใซใ‚ˆใฃใฆ่ชฒใ›ใ‚‰ใ‚Œใ‚‹ๅˆถ้™ใ‚’่ถ…ใˆใŸๅŠนๆžœ็š„ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซใ€ๅ‹พ้…ใ‚’ๅฐใ•ใชๅข—ๅˆ†ใง่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€ใƒขใƒ‡ใƒซใ‚’้ †ๆ–นๅ‘ใŠใ‚ˆใณ้€†ๆ–นๅ‘ใซๅฐใ•ใชใƒใƒƒใƒใงๅๅพฉ็š„ใซ่จˆ็ฎ—ใ—ใ€ใใฎ้Ž็จ‹ใงๅ‹พ้…ใ‚’่“„็ฉใ—ใพใ™ใ€‚ๅๅˆ†ใชๆ•ฐใฎๅ‹พ้…ใŒ่“„็ฉใ•ใ‚ŒใŸใ‚‰ใ€ใƒขใƒ‡ใƒซใฎๆœ€้ฉๅŒ–ใ‚นใƒ†ใƒƒใƒ—ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ๅ‹พ้…่“„็ฉใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€GPUใฎใƒกใƒขใƒชๅฎน้‡ใซใ‚ˆใ‚‹ๅˆถ็ด„ใ‚’่ถ…ใˆใฆ**ๅŠนๆžœ็š„ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บ**ใ‚’ๅข—ใ‚„ใ™ใ“ใจใŒใงใใพใ™ใŒใ€ๅ‹พ้…่“„็ฉใซใ‚ˆใฃใฆๅฐŽๅ…ฅใ•ใ‚Œใ‚‹่ฟฝๅŠ ใฎ้ †ๆ–นๅ‘ใŠใ‚ˆใณ้€†ๆ–นๅ‘ใฎ่จˆ็ฎ—ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใ‚’้…ใใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใŒๅฟ…่ฆใงใ™ใ€‚ + +`TrainingArguments`ใซ`gradient_accumulation_steps`ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใ€ๅ‹พ้…่“„็ฉใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```py +training_args = TrainingArguments(per_device_train_batch_size=1, gradient_accumulation_steps=4, **default_args) +``` + +ไธŠ่จ˜ใฎไพ‹ใงใฏใ€ๅŠนๆžœ็š„ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใฏ4ใซใชใ‚Šใพใ™ใ€‚ + +ใพใŸใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๅฎŒๅ…จใซๅˆถๅพกใ™ใ‚‹ใŸใ‚ใซ๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚๐Ÿค— Accelerateใฎไพ‹ใฏใ€[ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๅพŒๅŠใซใ‚ใ‚‹](#using--accelerate)ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ใงใใ‚‹ใ ใ‘GPUใฎไฝฟ็”จ็އใ‚’ๆœ€ๅคง้™ใซใ™ใ‚‹ใ“ใจใŒๆŽจๅฅจใ•ใ‚Œใฆใ„ใพใ™ใŒใ€้ซ˜ใ„ๅ‹พ้…่“„็ฉใ‚นใƒ†ใƒƒใƒ—ๆ•ฐใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ้…ๅปถใ‚’ใ‚ˆใ‚Š้ก•่‘—ใซใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚`per_device_train_batch_size=4`ใฎๅ ดๅˆใ€ๅ‹พ้…่“„็ฉใ‚’ไฝฟ็”จใ—ใชใ„ใจGPUใฎๅˆถ้™ใซ้”ใ—ใพใ™ใ€‚ใƒใƒƒใƒใ‚ตใ‚คใ‚บ64ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใŸใ„ๅ ดๅˆใ€`per_device_train_batch_size`ใ‚’1ใซ่จญๅฎšใ—ใ€`gradient_accumulation_steps`ใ‚’64ใซ่จญๅฎšใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ไปฃใ‚ใ‚Šใซใ€`per_device_train_batch_size=4`ใ‚’ไฟๆŒใ—ใ€`gradient_accumulation_steps=16`ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅŒใ˜ๅŠนๆžœ็š„ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใŒๅพ—ใ‚‰ใ‚Œใ€ๅˆฉ็”จๅฏ่ƒฝใชGPUใƒชใ‚ฝใƒผใ‚นใŒๅŠนๆžœ็š„ใซๆดป็”จใ•ใ‚Œใพใ™ใ€‚ + +่ฉณ็ดฐใชๆƒ…ๅ ฑใซใคใ„ใฆใฏใ€[RTX-3090็”จใฎใƒใƒƒใƒใ‚ตใ‚คใ‚บใจๅ‹พ้…่“„็ฉใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏ](https://github.com/huggingface/transformers/issues/14608#issuecomment-1004392537)ใŠใ‚ˆใณ[A100็”จใฎใƒใƒƒใƒใ‚ตใ‚คใ‚บใจๅ‹พ้…่“„็ฉใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏ](https://github.com/huggingface/transformers/issues/15026#issuecomment-1005033957)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Gradient Checkpointing + +ไธ€้ƒจใฎๅคงใใชใƒขใƒ‡ใƒซใฏใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’1ใซ่จญๅฎšใ—ใ€ๅ‹พ้…่“„็ฉใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ใƒกใƒขใƒชใฎๅ•้กŒใซ็›ด้ขใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒกใƒขใƒชใ‚นใƒˆใƒฌใƒผใ‚ธใŒๅฟ…่ฆใชไป–ใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใ‚‚ๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ๅ‰ๅ‘ใใƒ‘ใ‚นใ‹ใ‚‰ใฎใ™ในใฆใฎใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ไฟๅญ˜ใ—ใฆใ€้€†ๅ‘ใใƒ‘ใ‚นใงๅ‹พ้…ใ‚’่จˆ็ฎ—ใ™ใ‚‹ใจใ€ใ‹ใชใ‚Šใฎใƒกใƒขใƒชใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒ็™บ็”Ÿใ—ใพใ™ใ€‚้€†ๅ‘ใใƒ‘ใ‚นใงๅฟ…่ฆใชใจใใซใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’็ ดๆฃ„ใ—ใฆๅ†่จˆ็ฎ—ใ™ใ‚‹ไปฃๆ›ฟใ‚ขใƒ—ใƒญใƒผใƒใฏใ€่จˆ็ฎ—ใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒๅคงๅน…ใซๅข—ๅŠ ใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ—ใƒญใ‚ปใ‚นใŒ้…ใใชใ‚Šใพใ™ใ€‚ + +**ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ**ใฏใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใฎๆŠ˜่กทๆกˆใ‚’ๆไพ›ใ—ใ€่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ๅ…จไฝ“ใงๆˆฆ็•ฅ็š„ใซ้ธๆŠžใ•ใ‚ŒใŸใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใฎใฟใ‚’ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใ€ๅ‹พ้…ใ‚’ๅ†่จˆ็ฎ—ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใฎไธ€้ƒจใ ใ‘ใ‚’็ฏ€็ด„ใ—ใพใ™ใ€‚ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใฎ็ด ๆ™ดใ‚‰ใ—ใ„่จ˜ไบ‹](https://medium.com/tensorflow/fitting-larger-networks-into-memory-583e3c758ff9)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +[`Trainer`]ใงๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€[`TrainingArguments`]ใซๅฏพๅฟœใ™ใ‚‹ใƒ•ใƒฉใ‚ฐใ‚’ๆธกใ—ใพใ™๏ผš + + +```py +training_args = TrainingArguments( + per_device_train_batch_size=1, gradient_accumulation_steps=4, gradient_checkpointing=True, **default_args +) +``` + +ไปฃๆ›ฟๆ‰‹ๆฎตใจใ—ใฆใ€๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ - ๐Ÿค— Accelerateใฎไพ‹ใฏ[ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎใ•ใ‚‰ใซๅพŒใ‚ใซใ‚ใ‚Šใพใ™](#using--accelerate)ใ€‚ + + + +ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใƒกใƒขใƒชๅŠน็އใŒๅ‘ไธŠใ™ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใŒใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€Ÿๅบฆใฏ็ด„20%้…ใใชใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## Mixed precision training + +**ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ**ใฏใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่จˆ็ฎ—ๅŠน็އใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ๆŠ€่ก“ใงใ€็‰นๅฎšใฎๅค‰ๆ•ฐใซๅฏพใ—ใฆไฝŽ็ฒพๅบฆใฎๆ•ฐๅ€คใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ‚’ๅˆฉ็”จใ—ใพใ™ใ€‚ๅพ“ๆฅใ€ใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใฏๅค‰ๆ•ฐใ‚’่กจ็พใ—ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ32ใƒ“ใƒƒใƒˆๆตฎๅ‹•ๅฐๆ•ฐ็‚น็ฒพๅบฆ๏ผˆfp32ใพใŸใฏfloat32๏ผ‰ใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ™ในใฆใฎๅค‰ๆ•ฐใŒๆญฃ็ขบใช็ตๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใ“ใฎ้ซ˜็ฒพๅบฆใฎใƒฌใƒ™ใƒซใ‚’ๅฟ…่ฆใจใ—ใชใ„ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ไธ€้ƒจใฎๅค‰ๆ•ฐใฎ็ฒพๅบฆใ‚’16ใƒ“ใƒƒใƒˆๆตฎๅ‹•ๅฐๆ•ฐ็‚น๏ผˆfp16ใพใŸใฏfloat16๏ผ‰ใชใฉใฎใ‚ˆใ‚ŠไฝŽใ„ๆ•ฐๅ€คใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใซๅค‰ๆ›ดใ™ใ‚‹ใ“ใจใงใ€่จˆ็ฎ—ใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€ไธ€้ƒจใฎ่จˆ็ฎ—ใฏๅŠ็ฒพๅบฆใง่กŒใ‚ใ‚Œใ€ไธ€้ƒจใฏใพใ ๅฎŒๅ…จใช็ฒพๅบฆใง่กŒใ‚ใ‚Œใ‚‹ใŸใ‚ใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๅ‘ผใฐใ‚Œใฆใ„ใพใ™ใ€‚ + +ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใซๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€fp16๏ผˆfloat16๏ผ‰ใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ไฝฟ็”จใ—ใฆๅฎŸ็พใ•ใ‚Œใพใ™ใŒใ€ไธ€้ƒจใฎGPUใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ๏ผˆใ‚ขใƒณใƒšใ‚ขใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใชใฉ๏ผ‰ใงใฏbf16ใŠใ‚ˆใณtf32๏ผˆCUDAๅ†…้ƒจใƒ‡ใƒผใ‚ฟๅž‹๏ผ‰ใƒ‡ใƒผใ‚ฟๅž‹ใ‚‚ๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ‡ใƒผใ‚ฟๅž‹ใฎ้•ใ„ใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€[NVIDIAใฎใƒ–ใƒญใ‚ฐ](https://developer.nvidia.com/blog/accelerating-ai-training-with-tf32-tensor-cores/)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### fp16 + +ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎไธปใชๅˆฉ็‚นใฏใ€ๅŠ็ฒพๅบฆ๏ผˆfp16๏ผ‰ใงใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ +ๅ‹พ้…ใ‚‚ๅŠ็ฒพๅบฆใง่จˆ็ฎ—ใ•ใ‚Œใพใ™ใŒใ€ๆœ€้ฉๅŒ–ใ‚นใƒ†ใƒƒใƒ—ใงใฏๅ†ใณๅฎŒๅ…จ็ฒพๅบฆใซๅค‰ๆ›ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใ“ใ“ใงใฏใƒกใƒขใƒชใฏไฟๅญ˜ใ•ใ‚Œใพใ›ใ‚“ใ€‚ +ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏ่จˆ็ฎ—้€Ÿๅบฆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ไธ€ๆ–นใ€็‰นใซๅฐใ•ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใฎๅ ดๅˆใ€ใ‚ˆใ‚ŠๅคšใใฎGPUใƒกใƒขใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใŒGPUไธŠใซ16ใƒ“ใƒƒใƒˆใŠใ‚ˆใณ32ใƒ“ใƒƒใƒˆ็ฒพๅบฆใฎไธกๆ–นใงๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใงใ™๏ผˆGPUไธŠใฎๅ…ƒใฎใƒขใƒ‡ใƒซใฎ1.5ๅ€๏ผ‰ใ€‚ + +ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€`fp16`ใƒ•ใƒฉใ‚ฐใ‚’`True`ใซ่จญๅฎšใ—ใพใ™๏ผš + +```py +training_args = TrainingArguments(per_device_train_batch_size=4, fp16=True, **default_args) +``` + +๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€๐Ÿค— Accelerateใฎไพ‹ใฏ[ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎใ•ใ‚‰ใซๅพŒใ‚ใซใ‚ใ‚Šใพใ™](#using--accelerate)ใ€‚ + +### BF16 + +AmpereใพใŸใฏใใ‚Œไปฅ้™ใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ๅ ดๅˆใ€ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจ่ฉ•ไพกใซbf16ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚bf16ใฏfp16ใ‚ˆใ‚Šใ‚‚็ฒพๅบฆใŒๅŠฃใ‚Šใพใ™ใŒใ€ใฏใ‚‹ใ‹ใซๅคงใใชๅ‹•็š„็ฏ„ๅ›ฒใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚fp16ใงใฏใ€ๆŒใคใ“ใจใŒใงใใ‚‹ๆœ€ๅคงใฎๆ•ฐใฏ `65535` ใงใ‚ใ‚Šใ€ใใ‚Œใ‚’่ถ…ใˆใ‚‹ๆ•ฐๅ€คใฏใ‚ชใƒผใƒใƒผใƒ•ใƒญใƒผใ‚’ๅผ•ใ่ตทใ“ใ—ใพใ™ใ€‚ไธ€ๆ–นใ€bf16ใฎๆ•ฐๅ€คใฏ `3.39e+38` ใฎใ‚ˆใ†ใซๅคงใใใ€ใ“ใ‚Œใฏfp32ใจใปใผๅŒใ˜ใงใ™ - ใฉใกใ‚‰ใ‚‚ๆ•ฐๅ€ค็ฏ„ๅ›ฒใซ8ใƒ“ใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +BF16ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€๐Ÿค— Trainerใงไปฅไธ‹ใฎใ‚ˆใ†ใซ่จญๅฎšใ—ใพใ™๏ผš + + +```python +training_args = TrainingArguments(bf16=True, **default_args) +``` + + +### TF32 + +ใ‚ขใƒณใƒšใ‚ขใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใฏใ€tf32ใจใ„ใ†็‰นๅˆฅใชใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€fp32ใจๅŒใ˜ๆ•ฐๅ€ค็ฏ„ๅ›ฒ๏ผˆ8ใƒ“ใƒƒใƒˆ๏ผ‰ใ‚’ๆŒใฃใฆใ„ใพใ™ใŒใ€23ใƒ“ใƒƒใƒˆใฎ็ฒพๅบฆใงใฏใชใใ€10ใƒ“ใƒƒใƒˆใฎ็ฒพๅบฆ๏ผˆfp16ใจๅŒใ˜๏ผ‰ใ‚’ๆŒใกใ€ๅˆ่จˆใง19ใƒ“ใƒƒใƒˆใ—ใ‹ไฝฟ็”จใ—ใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏ้€šๅธธใฎfp32ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆŽจ่ซ–ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใ€tf32ใ‚ตใƒใƒผใƒˆใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใงใ€ๆœ€ๅคง3ๅ€ใฎใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใฎๅ‘ไธŠใŒๅพ—ใ‚‰ใ‚Œใ‚‹็‚นใงใ€Œ้ญ”ๆณ•ใฎใ‚ˆใ†ใ€ใงใ™ใ€‚่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ใฎใฏใ€ๆฌกใฎใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงใ™๏ผš + +```python +import torch +torch.backends.cuda.matmul.allow_tf32 = True +torch.backends.cudnn.allow_tf32 = True +``` + + +ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹GPUใŒใ‚ขใƒณใƒšใ‚ขใ‚ทใƒชใƒผใ‚บใงใ‚ใ‚‹ใจไปฎๅฎšใ—ใ€CUDAใฏๅฏ่ƒฝใช้™ใ‚Štf32ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ˆใ†ใซ่‡ชๅ‹•็š„ใซๅˆ‡ใ‚Šๆ›ฟใˆใพใ™ใ€‚ + +[NVIDIAใฎ็ ”็ฉถใซใ‚ˆใ‚Œใฐ](https://developer.nvidia.com/blog/accelerating-ai-training-with-tf32-tensor-cores/)ใ€ใปใจใ‚“ใฉใฎๆฉŸๆขฐๅญฆ็ฟ’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒฏใƒผใ‚ฏใƒญใƒผใƒ‰ใฏtf32ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจfp32ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงๅŒใ˜้›ฃ่งฃๅบฆใจๅŽๆŸใ‚’็คบใ—ใพใ™ใ€‚ใ™ใงใซfp16ใพใŸใฏbf16ๆททๅˆ็ฒพๅบฆใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใฎๅ‘ไธŠใซๅฝน็ซ‹ใคใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +๐Ÿค— Trainerใงใ“ใฎใƒขใƒผใƒ‰ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + + +```python +TrainingArguments(tf32=True, **default_args) +``` + + + +tf32ใฏ`tensor.to(dtype=torch.tf32)`ใ‚’ไป‹ใ—ใฆ็›ดๆŽฅใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏๅ†…้ƒจใฎCUDAใƒ‡ใƒผใ‚ฟๅž‹ใงใ™ใ€‚tf32ใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€`torch>=1.7`ใŒๅฟ…่ฆใงใ™ใ€‚ + + + +tf32ใจไป–ใฎ็ฒพๅบฆใซ้–ขใ™ใ‚‹่ฉณ็ดฐใชๆƒ…ๅ ฑใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผš +[RTX-3090](https://github.com/huggingface/transformers/issues/14608#issuecomment-1004390803)ใŠใ‚ˆใณ +[A100](https://github.com/huggingface/transformers/issues/15026#issuecomment-1004543189)ใ€‚ + +## Flash Attention 2 + +transformersใงFlash Attention 2็ตฑๅˆใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚Flash Attention 2ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅซใ‚€ใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟๆ–นๆณ•ใซใคใ„ใฆใฏใ€[single GPU section](./perf_infer_gpu_one#Flash-Attention-2)ใฎ้ฉๅˆ‡ใชใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’็ขบ่ชใ—ใฆ่ฉณ็ดฐใ‚’ๅญฆใณใพใ—ใ‚‡ใ†ใ€‚ + +## ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ้ธๆŠž + +Transformerใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซๆœ€ใ‚‚ไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฏAdamใพใŸใฏAdamW๏ผˆ้‡ใฟๆธ›่กฐใ‚’ไผดใ†Adam๏ผ‰ใงใ™ใ€‚Adamใฏๅ‰ๅ›žใฎๅ‹พ้…ใฎ็งปๅ‹•ๅนณๅ‡ใ‚’ไฟๅญ˜ใ™ใ‚‹ใ“ใจใงๅŽๆŸใ‚’้”ๆˆใ—ใพใ™ใŒใ€ใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใฎใ‚ชใƒผใƒ€ใƒผใฎ่ฟฝๅŠ ใƒกใƒขใƒชใƒ•ใƒƒใƒˆใƒ—ใƒชใƒณใƒˆใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚’่งฃๆถˆใ™ใ‚‹ใŸใ‚ใซใ€ไปฃๆ›ฟใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใŸใจใˆใฐใ€[NVIDIA/apex](https://github.com/NVIDIA/apex)ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€`adamw_apex_fused`ใฏใ™ในใฆใฎใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹AdamWใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎไธญใงๆœ€ใ‚‚้ซ˜้€Ÿใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไฝ“้จ“ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +[`Trainer`]ใฏใ€็›ดๆŽฅไฝฟ็”จใงใใ‚‹ใ•ใพใ–ใพใชใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’็ตฑๅˆใ—ใฆใŠใ‚Šใ€`adamw_hf`ใ€`adamw_torch`ใ€`adamw_torch_fused`ใ€`adamw_apex_fused`ใ€`adamw_anyprecision`ใ€`adafactor`ใ€ใพใŸใฏ`adamw_bnb_8bit`ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใ‚ตใƒผใƒ‰ใƒ‘ใƒผใƒ†ใ‚ฃใฎๅฎŸ่ฃ…ใ‚’ไป‹ใ—ใฆใ•ใ‚‰ใซๅคšใใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’่ฟฝๅŠ ใงใใพใ™ใ€‚ + +AdamWใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎไปฃๆ›ฟๆ‰‹ๆฎตใซใคใ„ใฆ่ฉณใ—ใ่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš +1. [`Trainer`]ใงไฝฟ็”จๅฏ่ƒฝใช`adafactor` +2. Trainerใงไฝฟ็”จๅฏ่ƒฝใช`adamw_bnb_8bit`ใฏใ€ใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณ็”จใซไปฅไธ‹ใงใ‚ตใƒผใƒ‰ใƒ‘ใƒผใƒ†ใ‚ฃใฎ็ตฑๅˆใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ๆฏ”่ผƒใฎใŸใ‚ใ€3Bใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒขใƒ‡ใƒซ๏ผˆไพ‹๏ผšใ€Œgoogle-t5/t5-3bใ€๏ผ‰ใฎๅ ดๅˆ๏ผš +* ๆจ™ๆบ–ใฎAdamWใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฏใ€ๅ„ใƒ‘ใƒฉใƒกใƒผใ‚ฟใซ8ใƒใ‚คใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใ€24GBใฎGPUใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™๏ผˆ8 * 3 => 24GB๏ผ‰ใ€‚ +* Adafactorใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฏ12GBไปฅไธŠๅฟ…่ฆใงใ™ใ€‚ๅ„ใƒ‘ใƒฉใƒกใƒผใ‚ฟใซใ‚ใšใ‹4ใƒใ‚คใƒˆไปฅไธŠใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใ€4 * 3ใจๅฐ‘ใ—ไฝ™ๅˆ†ใซใชใ‚Šใพใ™ใ€‚ +* 8ใƒ“ใƒƒใƒˆใฎBNB้‡ๅญๅŒ–ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฏใ€ใ™ในใฆใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใŒ้‡ๅญๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ใ‚ใšใ‹6GBใ—ใ‹ไฝฟ็”จใ—ใพใ›ใ‚“ใ€‚ + +### Adafactor + +Adafactorใฏใ€้‡ใฟ่กŒๅˆ—ใฎๅ„่ฆ็ด ใฎใŸใ‚ใซๅ‰ๅ›žใฎๅนณๅ‡ใ‚’ไฟๅญ˜ใ—ใพใ›ใ‚“ใ€‚ไปฃใ‚ใ‚Šใซใ€๏ผˆ่กŒใ”ใจใจๅˆ—ใ”ใจใฎๅนณๅ‡ใฎๅˆ่จˆใชใฉ๏ผ‰้›† + + +```py +training_args = TrainingArguments(per_device_train_batch_size=4, optim="adafactor", **default_args) +``` + + +ไป–ใฎใ‚ขใƒ—ใƒญใƒผใƒ๏ผˆๅ‹พ้…่“„็ฉใ€ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ€ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผ‰ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใงใ€ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’็ถญๆŒใ—ใชใŒใ‚‰ๆœ€ๅคง3ๅ€ใฎๅ‘ไธŠใŒ่ฆ‹ใ‚‰ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผใŸใ ใ—ใ€ๅ‰่ฟฐใฎใ‚ˆใ†ใซใ€AdafactorใฎๅŽๆŸๆ€งใฏAdamใ‚ˆใ‚Šใ‚‚ๆ‚ชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +### 8ใƒ“ใƒƒใƒˆ Adam + +Adafactorใฎใ‚ˆใ†ใซใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใฎ็Šถๆ…‹ใ‚’้›†็ด„ใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€8ใƒ“ใƒƒใƒˆใฎAdamใฏๅฎŒๅ…จใช็Šถๆ…‹ใ‚’ไฟๆŒใ—ใ€ใใ‚Œใ‚’้‡ๅญๅŒ–ใ—ใพใ™ใ€‚้‡ๅญๅŒ–ใจใฏใ€็Šถๆ…‹ใ‚’ไฝŽใ„็ฒพๅบฆใงไฟๅญ˜ใ—ใ€ๆœ€้ฉๅŒ–ใฎใŸใ‚ใ ใ‘ใซ้ž้‡ๅญๅŒ–ใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใ“ใ‚Œใฏๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ่ƒŒๅพŒใซใ‚ใ‚‹ใ‚ขใ‚คใƒ‡ใ‚ขใจไผผใฆใ„ใพใ™ใ€‚ + +`adamw_bnb_8bit`ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๅ˜ใซ[`TrainingArguments`]ใง`optim="adamw_bnb_8bit"`ใ‚’่จญๅฎšใ™ใ‚‹ใ ใ‘ใงใ™๏ผš + + +```py +training_args = TrainingArguments(per_device_train_batch_size=4, optim="adamw_bnb_8bit", **default_args) +``` + +ใŸใ ใ—ใ€ใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณ็›ฎ็š„ใง8ใƒ“ใƒƒใƒˆใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ใ‚ตใƒผใƒ‰ใƒ‘ใƒผใƒ†ใ‚ฃใฎๅฎŸ่ฃ…ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใ‚’็ตฑๅˆใ™ใ‚‹ๆ–นๆณ•ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ใพใšใ€8ใƒ“ใƒƒใƒˆAdamใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ๅฎŸ่ฃ…ใ—ใŸ`bitsandbytes`ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใŸใ‚ใซใ€GitHub [ใƒชใƒใ‚ธใƒˆใƒช](https://github.com/TimDettmers/bitsandbytes)ๅ†…ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซใ‚ฌใ‚คใƒ‰ใซๅพ“ใฃใฆใใ ใ•ใ„ใ€‚ + +ๆฌกใซใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใฏ2ใคใฎใ‚นใƒ†ใƒƒใƒ—ใŒๅซใพใ‚Œใพใ™๏ผš +* ใพใšใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’2ใคใฎใ‚ฐใƒซใƒผใƒ—ใซๅˆ†ใ‘ใพใ™ - ้‡ใฟๆธ›่กฐใ‚’้ฉ็”จใ™ใ‚‹ในใใ‚ฐใƒซใƒผใƒ—ใจใ€้ฉ็”จใ™ในใใงใชใ„ใ‚ฐใƒซใƒผใƒ—ใงใ™ใ€‚้€šๅธธใ€ใƒใ‚คใ‚ขใ‚นใจใƒฌใ‚คใƒคใƒผๆญฃ่ฆๅŒ–ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏ้‡ใฟๆธ›่กฐใ•ใ‚Œใพใ›ใ‚“ใ€‚ +* ๆฌกใซใ€ไปฅๅ‰ใซไฝฟ็”จใ—ใŸAdamWใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใจๅŒใ˜ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใ€ใ„ใใคใ‹ใฎๅผ•ๆ•ฐใฎ่ชฟๆ•ดใ‚’่กŒใ„ใพใ™ใ€‚ + + +```py +import bitsandbytes as bnb +from torch import nn +from transformers.trainer_pt_utils import get_parameter_names + +training_args = TrainingArguments(per_device_train_batch_size=4, **default_args) + +decay_parameters = get_parameter_names(model, [nn.LayerNorm], ["bias", "layernorm", "rmsnorm"]) +optimizer_grouped_parameters = [ + { + "params": [p for n, p in model.named_parameters() if n in decay_parameters], + "weight_decay": training_args.weight_decay, + }, + { + "params": [p for n, p in model.named_parameters() if n not in decay_parameters], + "weight_decay": 0.0, + }, +] + +optimizer_kwargs = { + "betas": (training_args.adam_beta1, training_args.adam_beta2), + "eps": training_args.adam_epsilon, +} +optimizer_kwargs["lr"] = training_args.learning_rate +adam_bnb_optim = bnb.optim.Adam8bit( + optimizer_grouped_parameters, + betas=(training_args.adam_beta1, training_args.adam_beta2), + eps=training_args.adam_epsilon, + lr=training_args.learning_rate, +) +``` + +ๆœ€ๅพŒใซใ€ใ‚ซใ‚นใ‚ฟใƒ ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’`Trainer`ใซๅผ•ๆ•ฐใจใ—ใฆๆธกใ—ใพใ™๏ผš + + +```py +trainer = Trainer(model=model, args=training_args, train_dataset=ds, optimizers=(adam_bnb_optim, None)) +``` + +ไป–ใฎใ‚ขใƒ—ใƒญใƒผใƒ๏ผˆๅ‹พ้…่“„็ฉใ€ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ€ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ๏ผ‰ใจ็ต„ใฟๅˆใ‚ใ›ใ‚‹ใ“ใจใงใ€Adafactorใฎไฝฟ็”จใจๅŒ็ญ‰ไปฅไธŠใฎ3ๅ€ใฎใƒกใƒขใƒชๆ”นๅ–„ใŠใ‚ˆใณใ‚ใšใ‹ใซ้ซ˜ใ„ใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๆœŸๅพ…ใงใใพใ™ใ€‚ + +### multi_tensor + +pytorch-nightlyใฏใ€ๅคšใใฎๅฐใ•ใช็‰นๅพดใƒ†ใƒณใ‚ฝใƒซใŒใ‚ใ‚‹็Šถๆณใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ๅคงๅน…ใซ้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใฏใšใฎ`torch.optim._multi_tensor`ใ‚’ๅฐŽๅ…ฅใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใฏๆœ€็ต‚็š„ใซใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใซใชใ‚‹ใฏใšใงใ™ใŒใ€ใใ‚Œใ‚’ๆ—ฉใ่ฉฆใ—ใฆใฟใŸใ„ๅ ดๅˆใฏใ€ใ“ใฎGitHub [issue](https://github.com/huggingface/transformers/issues/9965)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## ใƒ‡ใƒผใ‚ฟใฎไบ‹ๅ‰่ชญใฟ่พผใฟ + +ๅ„ชใ‚ŒใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€Ÿๅบฆใซๅˆฐ้”ใ™ใ‚‹ใŸใ‚ใฎ้‡่ฆใช่ฆไปถใฎ1ใคใฏใ€GPUใŒๅ‡ฆ็†ใงใใ‚‹ๆœ€ๅคง้€Ÿๅบฆใงใƒ‡ใƒผใ‚ฟใ‚’ไพ›็ตฆใงใใ‚‹่ƒฝๅŠ›ใงใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ™ในใฆใŒใƒกใ‚คใƒณใƒ—ใƒญใ‚ปใ‚นใง่กŒใ‚ใ‚Œใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒ‡ใ‚ฃใ‚นใ‚ฏใ‹ใ‚‰ๅๅˆ†้€Ÿใ่ชญใฟๅ–ใ‚‹ใ“ใจใŒใงใใชใ„ๅ ดๅˆใ€GPUใฎใ‚ขใƒณใƒ€ใƒผใƒฆใƒผใƒ†ใ‚ฃใƒชใ‚ผใƒผใ‚ทใƒงใƒณใ‚’ๅผ•ใ่ตทใ“ใ™ใƒœใƒˆใƒซใƒใƒƒใ‚ฏใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใƒœใƒˆใƒซใƒใƒƒใ‚ฏใ‚’ๆธ›ใ‚‰ใ™ใŸใ‚ใซใ€ไปฅไธ‹ใฎๅผ•ๆ•ฐใ‚’่จญๅฎšใ—ใพใ™๏ผš + +- `DataLoader(pin_memory=True, ...)` - ใƒ‡ใƒผใ‚ฟใ‚’CPUใฎใƒ”ใƒณใƒกใƒขใƒชใซไบ‹ๅ‰่ชญใฟ่พผใฟใ—ใ€้€šๅธธใ€CPUใ‹ใ‚‰GPUใƒกใƒขใƒชใธใฎ่ปข้€ใŒใฏใ‚‹ใ‹ใซ้ซ˜้€ŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚ +- `DataLoader(num_workers=4, ...)` - ใƒ‡ใƒผใ‚ฟใ‚’ใ‚ˆใ‚Š้€Ÿใไบ‹ๅ‰่ชญใฟ่พผใฟใ™ใ‚‹ใŸใ‚ใซ่ค‡ๆ•ฐใฎใƒฏใƒผใ‚ซใƒผใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซGPUใฎๅˆฉ็”จ็Šถๆณใฎ็ตฑ่จˆๆƒ…ๅ ฑใ‚’็ขบ่ชใ—ใ€100๏ผ…ใ‹ใ‚‰้ ใ„ๅ ดๅˆใ€ใƒฏใƒผใ‚ซใƒผใฎๆ•ฐใ‚’ๅข—ใ‚„ใ™ๅฎŸ้จ“ใ‚’่กŒใฃใฆใใ ใ•ใ„ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€ๅ•้กŒใฏไป–ใฎๅ ดๆ‰€ใซใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใฎใงใ€ๅคšใใฎใƒฏใƒผใ‚ซใƒผใŒๅฟ…ใšใ—ใ‚‚ๆ€ง่ƒฝๅ‘ไธŠใซใคใชใŒใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +[`Trainer`]ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ๅฏพๅฟœใ™ใ‚‹[`TrainingArguments`]ใฏ`dataloader_pin_memory`๏ผˆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ`True`๏ผ‰ใŠใ‚ˆใณ`dataloader_num_workers`๏ผˆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฏ`0`๏ผ‰ใงใ™ใ€‚ + +## DeepSpeed ZeRO + +DeepSpeedใฏใ€๐Ÿค— Transformersใจ๐Ÿค— Accelerateใจ็ตฑๅˆใ•ใ‚ŒใŸใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใฎใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐๆœ€้ฉๅŒ–ใƒฉใ‚คใƒ–ใƒฉใƒชใงใ™ใ€‚ +ๅคง่ฆๆจกใชใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅŠน็އใจใ‚นใ‚ฑใƒผใƒฉใƒ“ใƒชใƒ†ใ‚ฃใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซ่จญ่จˆใ•ใ‚ŒใŸใ•ใพใ–ใพใชๆฉŸ่ƒฝใจๆœ€้ฉๅŒ–ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใŒๅ˜ไธ€ใฎGPUใซๅŽใพใ‚Šใ€ๅฐใ•ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ๅŽใ‚ใ‚‹ใ‚นใƒšใƒผใ‚นใŒใ‚ใ‚‹ๅ ดๅˆใ€DeepSpeedใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใใ‚Œใฏใ‚€ใ—ใ‚้…ใใชใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ใƒขใƒ‡ใƒซใŒๅ˜ไธ€ใฎGPUใซๅŽใพใ‚‰ใชใ„ๅ ดๅˆใ€ใพใŸใฏๅฐใ•ใชใƒใƒƒใƒใ‚’ๅŽใ‚ใ‚‹ใ“ใจใŒใงใใชใ„ๅ ดๅˆใ€DeepSpeed ZeRO + CPU OffloadใพใŸใฏNVMe Offloadใ‚’ๅˆฉ็”จใงใใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€[ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ๅˆฅ้€”ใ‚คใƒณใ‚นใƒˆใƒผใƒซ](main_classes/deepspeed#installation)ใ—ใ€่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใ€DeepSpeedใ‚’่ตทๅ‹•ใ™ใ‚‹ใŸใ‚ใฎใ‚ฌใ‚คใƒ‰ใ‚’ใƒ•ใ‚ฉใƒญใƒผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +* [`Trainer`]ใจใฎDeepSpeed็ตฑๅˆใฎ่ฉณ็ดฐใ‚ฌใ‚คใƒ‰ใซใคใ„ใฆใฏใ€[่ฉฒๅฝ“ใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](main_classes/deepspeed)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚็‰นใซใ€[ๅ˜ไธ€GPU็”จใฎใƒ‡ใƒ—ใƒญใ‚คใƒกใƒณใƒˆ](main_classes/deepspeed#deployment-with-one-gpu)ใซ้–ขใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใ™ใ€‚DeepSpeedใ‚’ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใงไฝฟ็”จใ™ใ‚‹ใซใฏใ„ใใคใ‹ใฎ่ชฟๆ•ดใŒๅฟ…่ฆใงใ™ใฎใงใ€[่ฉฒๅฝ“ใ™ใ‚‹ใ‚ฌใ‚คใƒ‰](main_classes/deepspeed#deployment-in-notebooks)ใ‚‚ใ”่ฆงใใ ใ•ใ„ใ€‚ +* ๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏใ€[๐Ÿค— Accelerate DeepSpeedใ‚ฌใ‚คใƒ‰](https://huggingface.co/docs/accelerate/en/usage_guides/deepspeed)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## torch.compileใฎไฝฟ็”จ + +PyTorch 2.0ใฏๆ–ฐใ—ใ„ใ‚ณใƒณใƒ‘ใ‚คใƒซ้–ขๆ•ฐใ‚’ๅฐŽๅ…ฅใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใฏๆ—ขๅญ˜ใฎPyTorchใ‚ณใƒผใƒ‰ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€1่กŒใฎใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใ‚ณใƒผใƒ‰ใ‚’ๆœ€้ฉๅŒ–ใงใใพใ™๏ผš`model = torch.compile(model)`ใ€‚ + +[`Trainer`]ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€[`TrainingArguments`]ๅ†…ใฎ`torch_compile`ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆธกใ™ใ ใ‘ใงใ™๏ผš + + +```python +training_args = TrainingArguments(torch_compile=True, **default_args) +``` + +`torch.compile`ใฏใ€ๆ—ขๅญ˜ใฎPyTorchใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‹ใ‚‰ใ‚ฐใƒฉใƒ•ใ‚’่‡ชๅ‹•็š„ใซไฝœๆˆใ™ใ‚‹ใŸใ‚ใซPythonใฎใƒ•ใƒฌใƒผใƒ ่ฉ•ไพกAPIใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ‚ฐใƒฉใƒ•ใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ—ใŸๅพŒใ€็•ฐใชใ‚‹ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใ‚’ๅฑ•้–‹ใ—ใฆๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใ‚จใƒณใ‚ธใƒณใซๅค‰ๆ›ใงใใพใ™ใ€‚ +่ฉณ็ดฐใŠใ‚ˆใณใƒ™ใƒณใƒใƒžใƒผใ‚ฏใซใคใ„ใฆใฏใ€[PyTorchใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://pytorch.org/get-started/pytorch-2.0/)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +`torch.compile`ใซใฏใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎไพๅญ˜้–ขไฟ‚ใ‚’ๆŒใคๆˆ้•ทไธญใฎใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใฎใƒชใ‚นใƒˆใŒใ‚ใ‚Šใ€`torchdynamo.list_backends()`ใ‚’ๅ‘ผใณๅ‡บใ—ใฆ็ขบ่ชใงใใพใ™ใ€‚ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใ‚‹ไธ€้ƒจใฎใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +**ใƒ‡ใƒใƒƒใ‚ฐ็”จใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰**๏ผš +* `dynamo.optimize("eager")` - ๆŠฝๅ‡บใ•ใ‚ŒใŸGraphModuleใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใซPyTorchใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏTorchDynamoใฎๅ•้กŒใ‚’ใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹้š›ใซ้žๅธธใซๅฝน็ซ‹ใกใพใ™ใ€‚ +* `dynamo.optimize("aot_eager")` - ใ‚ณใƒณใƒ‘ใ‚คใƒฉใƒผใ‚’ไฝฟ็”จใ—ใชใ„AotAutogradใ‚’ไฝฟ็”จใ—ใฆAotAutogradใฎๆŠฝๅ‡บใ•ใ‚ŒใŸใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใŠใ‚ˆใณใƒใƒƒใ‚ฏใƒฏใƒผใƒ‰ใ‚ฐใƒฉใƒ•ใซๅฏพใ—ใฆๅ˜ใซPyTorch eagerใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใƒ‡ใƒใƒƒใ‚ฐใซๅฝน็ซ‹ใกใ€้ซ˜้€ŸๅŒ–ใฏๆœŸๅพ…ใงใใพใ›ใ‚“ใ€‚ + +**ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆŽจ่ซ–ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰**๏ผš +* `dynamo.optimize("inductor")` - TorchInductorใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใ‚’ไฝฟ็”จใ—ใ€AotAutogradใŠใ‚ˆใณcudagraphsใ‚’ๆดป็”จใ—ใฆใ‚ณใƒผใƒ‰็”Ÿๆˆใ•ใ‚ŒใŸTritonใ‚ซใƒผใƒใƒซใ‚’ไฝฟ็”จใ—ใพใ™ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://dev-discuss.pytorch.org/t/torchinductor-a-pytorch-native-compiler-with-define-by-run-ir-and-symbolic-shapes/747) +* `dynamo.optimize("nvfuser")` - nvFuser with TorchScriptใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://dev-discuss.pytorch.org/t/tracing-with-primitives-update-1-nvfuser-and-its-primitives/593) +* `dynamo.optimize("aot_nvfuser")` - nvFuser with AotAutogradใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://dev-discuss.pytorch.org/t/tracing-with-primitives-update-1-nvfuser-and-its-primitives/593) +* `dynamo.optimize("aot_cudagraphs")` - AotAutogradใ‚’ไฝฟ็”จใ—ใฆcudagraphsใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://github.com/pytorch/torchdynamo/pull/757) + +**ๆŽจ่ซ–ๅฐ‚็”จใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰**๏ผš +* `dynamo.optimize("ofi")` - Torchscriptใฎ`optimize_for_inference`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://pytorch.org/docs/stable/generated/torch.jit.optimize_for_inference.html) +* `dynamo.optimize("fx2trt")` - Nvidia TensorRTใ‚’ไฝฟ็”จใ—ใŸๆŽจ่ซ–ใฎๆœ€้ฉๅŒ–ใซNvidia TensorRTใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://pytorch.org/TensorRT/tutorials/getting_started_with_fx_path.html) +* `dynamo.optimize("onnxrt")` - CPU/GPUใงใฎๆŽจ่ซ–ใซONNX Runtimeใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://onnxruntime.ai/) +* `dynamo.optimize("ipex")` - CPUใงใฎๆŽจ่ซ–ใซIPEXใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ [่ฉณ็ดฐใฏใ“ใกใ‚‰](https://github.com/intel/intel-extension-for-pytorch) + +๐Ÿค— Transformersใ‚’ไฝฟ็”จใ—ใŸ`torch.compile`ใฎไฝฟ็”จไพ‹ใซใคใ„ใฆใฏใ€ใ“ใฎ[ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://www.philschmid.de/getting-started-pytorch-2-0-transformers)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## Using ๐Ÿค— Accelerate + +[๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate/index)ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไธŠ่จ˜ใฎๆ–นๆณ•ใ‚’ไฝฟ็”จใ—ใชใŒใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๅฎŒๅ…จใซๅˆถๅพกใงใใ€ๅŸบๆœฌ็š„ใซใฏ็ด”็ฒ‹ใชPyTorchใงใƒซใƒผใƒ—ใ‚’ๆ›ธใใ“ใจใŒใงใใพใ™ใ€‚ + +ๆฌกใซใ€[`TrainingArguments`]ๅ†…ใงๆ–นๆณ•ใ‚’็ต„ใฟๅˆใ‚ใ›ใŸๅ ดๅˆใ‚’ๆƒณ + +```py +training_args = TrainingArguments( + per_device_train_batch_size=1, + gradient_accumulation_steps=4, + gradient_checkpointing=True, + fp16=True, + **default_args, +) +``` + +๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ—ใŸๅฎŒๅ…จใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใฎไพ‹ใฏใ€ใปใ‚“ใฎๆ•ฐ่กŒใฎใ‚ณใƒผใƒ‰ใงใ™๏ผš + +```py +from accelerate import Accelerator +from torch.utils.data.dataloader import DataLoader + +dataloader = DataLoader(ds, batch_size=training_args.per_device_train_batch_size) + +if training_args.gradient_checkpointing: + model.gradient_checkpointing_enable() + +accelerator = Accelerator(fp16=training_args.fp16) +model, optimizer, dataloader = accelerator.prepare(model, adam_bnb_optim, dataloader) + +model.train() +for step, batch in enumerate(dataloader, start=1): + loss = model(**batch).loss + loss = loss / training_args.gradient_accumulation_steps + accelerator.backward(loss) + if step % training_args.gradient_accumulation_steps == 0: + optimizer.step() + optimizer.zero_grad() +``` + +ใพใšใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’[`DataLoader`](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader)ใงใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ +ๆฌกใซใ€ใƒขใƒ‡ใƒซใฎ[`~PreTrainedModel.gradient_checkpointing_enable`]ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใงๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆœ‰ๅŠนใซใงใใพใ™ใ€‚ +[`Accelerator`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator)ใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹้š›ใซใ€ๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’[`prepare`](https://huggingface.co/docs/accelerate/package_reference/accelerator#accelerate.Accelerator.prepare)ใฎๅ‘ผใณๅ‡บใ—ใงๆŒ‡ๅฎšใ—ใ€่ค‡ๆ•ฐใฎGPUใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`prepare`ใฎ้–“ใซใƒ‡ใƒผใ‚ฟใƒญใƒผใƒ€ใƒผใ‚‚ใƒฏใƒผใ‚ซใƒผ้–“ใงๅˆ†ๆ•ฃใ•ใ‚Œใพใ™ใ€‚ๅŒใ˜[8ใƒ“ใƒƒใƒˆใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถ](#8-bit-adam)ใ‚’ๅ‰ใฎไพ‹ใ‹ใ‚‰ไฝฟ็”จใ—ใพใ™ใ€‚ + +ๆœ€ๅพŒใซใ€ไธป่ฆใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’่ฟฝๅŠ ใงใใพใ™ใ€‚`backward`ใฎๅ‘ผใณๅ‡บใ—ใฏ๐Ÿค— Accelerateใซใ‚ˆใฃใฆๅ‡ฆ็†ใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใพใŸใ€ๅ‹พ้…ใฎ่“„็ฉใŒใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใ‚‚็ขบ่ชใงใใพใ™ใ€‚ๆๅคฑใ‚’ๆญฃ่ฆๅŒ–ใ—ใฆใ„ใ‚‹ใŸใ‚ใ€่“„็ฉใฎๆœ€ๅพŒใซๅนณๅ‡ใ‚’ๅพ—ใฆใ€ๅๅˆ†ใชใ‚นใƒ†ใƒƒใƒ—ใŒใ‚ใ‚‹ใจๆœ€้ฉๅŒ–ใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎๆœ€้ฉๅŒ–ๆŠ€่ก“ใ‚’๐Ÿค— Accelerateใ‚’ไฝฟ็”จใ—ใฆๅฎŸ่ฃ…ใ™ใ‚‹ใฎใฏใ€ใ‚ใšใ‹ใชใ‚ณใƒผใƒ‰่กŒใง่กŒใ†ใ“ใจใŒใงใใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใฎๆŸ”่ปŸๆ€งใŒๅ‘ไธŠใ—ใพใ™ใ€‚ใ™ในใฆใฎๆฉŸ่ƒฝใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Accelerateใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/accelerate/index)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Efficient Software Prebuilds + +PyTorchใฎ[pipใจcondaใƒ“ใƒซใƒ‰](https://pytorch.org/get-started/locally/#start-locally)ใฏใ€PyTorchใ‚’ๅฎŸ่กŒใ™ใ‚‹ใฎใซๅๅˆ†ใชcudaใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใงไบ‹ๅ‰ใซใƒ“ใƒซใƒ‰ใ•ใ‚Œใฆใ„ใพใ™ใŒใ€cudaๆ‹กๅผตใ‚’ใƒ“ใƒซใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใซใฏไธๅๅˆ†ใงใ™ใ€‚ + +ๆ™‚ๆŠ˜ใ€่ฟฝๅŠ ใฎๅŠชๅŠ›ใŒๅฟ…่ฆใชๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ไบ‹ๅ‰ใซใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚Œใฆใ„ใชใ„`apex`ใชใฉใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใงใ™ใ€‚ใพใŸใ€ใ‚ทใ‚นใƒ†ใƒ ๅ…จไฝ“ใง้ฉๅˆ‡ใชcudaใƒ„ใƒผใƒซใ‚ญใƒƒใƒˆใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒ้›ฃใ—ใ„ๅ ดๅˆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใ‚ทใƒŠใƒชใ‚ชใซๅฏพๅ‡ฆใ™ใ‚‹ใŸใ‚ใซใ€PyTorchใจNVIDIAใฏcudaๆ‹กๅผตใŒใ™ใงใซไบ‹ๅ‰ใซใƒ“ใƒซใƒ‰ใ•ใ‚Œใฆใ„ใ‚‹NGC dockerใ‚ณใƒณใƒ†ใƒŠใฎๆ–ฐใ—ใ„ใƒใƒผใ‚ธใƒงใƒณใ‚’ใƒชใƒชใƒผใ‚นใ—ใพใ—ใŸใ€‚ใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ ใ‘ใงใ€ใใฎใพใพๅฎŸ่กŒใงใใพใ™ใ€‚ + +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€PyTorchใฎใ‚ฝใƒผใ‚นใ‚’่ชฟๆ•ดใ—ใŸใ‚Šใ€ๆ–ฐใ—ใ„ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ•ใ‚ŒใŸใƒ“ใƒซใƒ‰ใ‚’ไฝœๆˆใ—ใŸใ‚Šใ—ใŸใ„ๅ ดๅˆใซใ‚‚ๅฝน็ซ‹ใกใพใ™ใ€‚ +ๆฌฒใ—ใ„dockerใ‚คใƒกใƒผใ‚ธใƒใƒผใ‚ธใƒงใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใซใฏใ€ใพใš[PyTorchใฎใƒชใƒชใƒผใ‚นใƒŽใƒผใƒˆ](https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/)ใ‹ใ‚‰ๅง‹ใ‚ใ€ๆœ€ๆ–ฐใฎๆœˆๆฌกใƒชใƒชใƒผใ‚นใฎใ„ใšใ‚Œใ‹ใ‚’้ธๆŠžใ—ใพใ™ใ€‚ๅธŒๆœ›ใฎใƒชใƒชใƒผใ‚นใฎใƒชใƒชใƒผใ‚นใƒŽใƒผใƒˆใซ็งปๅ‹•ใ—ใ€็’ฐๅขƒใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒๅฟ…่ฆใชใ‚‚ใฎใจไธ€่‡ดใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™๏ผˆNVIDIA Driverใฎ่ฆไปถใ‚‚ๅซใ‚€๏ผ๏ผ‰ใ€ใใฎๆ–‡ๆ›ธใฎไธ€็•ชไธŠใซ่กŒใใ€ๅฏพๅฟœใ™ใ‚‹NGCใƒšใƒผใ‚ธใซ็งปๅ‹•ใ—ใพใ™ใ€‚ใชใœใ‹ใ‚ใ‹ใ‚‰ใชใ„ๅ ดๅˆใฏใ€[ใ™ในใฆใฎPyTorch NGCใ‚คใƒกใƒผใ‚ธใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch)ใงใ™ใ€‚ + +ๆฌกใซใ€dockerใ‚คใƒกใƒผใ‚ธใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆๅฑ•้–‹ใ™ใ‚‹ๆ‰‹้ †ใซๅพ“ใ„ใพใ™ใ€‚ + +## Mixture of Experts + +ๆœ€่ฟ‘ใฎ่ซ–ๆ–‡ใซใ‚ˆใ‚Œใฐใ€Transformerใƒขใƒ‡ใƒซใซๅฐ‚้–€ๅฎถใฎๆททๅˆ๏ผˆMoE๏ผ‰ใ‚’็ตฑๅˆใ™ใ‚‹ใ“ใจใงใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€ŸๅบฆใŒ4ใ€œ5ๅ€ๅ‘ไธŠใ—ใ€ๆŽจ่ซ–ใ‚‚้ซ˜้€ŸๅŒ–ใ•ใ‚Œใ‚‹ใ“ใจใŒๅ ฑๅ‘Šใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ‚ˆใ‚Šๅคšใใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ˆใ‚Š่‰ฏใ„ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซใคใชใŒใ‚‹ใ“ใจใŒใ‚ใ‹ใฃใฆใ„ใ‚‹ใŸใ‚ใ€ใ“ใฎๆŠ€่ก“ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใ‚นใƒˆใ‚’ๅข—ใ‚„ใ™ใ“ใจใชใใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใ‚’ๆก้•ใ„ใซๅข—ใ‚„ใ™ใ“ใจใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€ไป–ใฎFFNๅฑคใฎไปฃใ‚ใ‚ŠใซMoEๅฑคใŒ้…็ฝฎใ•ใ‚Œใ€ๅ„ๅฐ‚้–€ๅฎถใ‚’ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใซๅฟœใ˜ใฆใƒใƒฉใƒณใ‚นใ‚ˆใใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ‚ฒใƒผใƒˆ้–ขๆ•ฐใงๆง‹ๆˆใ•ใ‚Œใพใ™ใ€‚ + +![MoE Transformer 2x block](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/perf-moe-transformer.png) + +๏ผˆๅ‡บๅ…ธ: [GLAM](https://ai.googleblog.com/2021/12/more-efficient-in-context-learning-with.html)๏ผ‰ + +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฎไธปใชๆฌ ็‚นใฏใ€GPUใƒกใƒขใƒชใ‚’ใปใผๆก้•ใ„ใซๅคšใๅฟ…่ฆใจใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒกใƒขใƒช่ฆไปถใŒใฏใ‚‹ใ‹ใซๅคงใใ„ใ“ใจใŒใใฎใพใพๅๆ˜ ใ•ใ‚Œใพใ™ใ€‚ใ‚ˆใ‚Š้ซ˜ใ„ใƒกใƒขใƒช่ฆไปถใ‚’ๅ…‹ๆœใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆใฏใ€ใ•ใพใ–ใพใช่’ธ็•™ใŠใ‚ˆใณใ‚ขใƒ—ใƒญใƒผใƒใŒๆๆกˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใŸใ ใ—ใ€็›ดๆŽฅใฎใƒˆใƒฌใƒผใƒ‰ใ‚ชใƒ•ใŒใ‚ใ‚Šใพใ™ใ€‚ๆ•ฐไบบใฎๅฐ‚้–€ๅฎถใ‚’ไฝฟ็”จใ—ใฆใƒ™ใƒผใ‚นใƒขใƒ‡ใƒซใ‚’2ใ€œ3ๅ€ๅฐใ•ใใ™ใ‚‹ใ“ใจใงใ€5ๅ€ๅฐใ•ใชใƒขใƒ‡ใƒซใซใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€Ÿๅบฆใ‚’้ฉๅบฆใซๅ‘ไธŠใ•ใ›ใ€ใƒกใƒขใƒช่ฆไปถใ‚’้ฉๅบฆใซๅข—ใ‚„ใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +้–ข้€ฃใ™ใ‚‹ใปใจใ‚“ใฉใฎ่ซ–ๆ–‡ใŠใ‚ˆใณๅฎŸ่ฃ…ใฏTensorflow/TPUใ‚’ไธญๅฟƒใซๆง‹็ฏ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +- [GShard: Conditional Computation and Automatic Shardingใ‚’ๆดป็”จใ—ใŸๅทจๅคงใƒขใƒ‡ใƒซใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐ](https://huggingface.co/papers/2006.16668) +- [Switch Transformers: ใ‚ทใƒณใƒ—ใƒซใงๅŠน็އ็š„ใชใ‚นใƒ‘ใƒผใ‚นๆ€งใ‚’ๅ‚™ใˆใŸใƒˆใƒชใƒชใ‚ชใƒณใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒขใƒ‡ใƒซใธใฎใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐ](https://huggingface.co/papers/2101.03961) +- [GLaM: Generalist Language Model (GLaM)](https://ai.googleblog.com/2021/12/more-efficient-in-context-learning-with.html) + +PytorchใซใฏDeepSpeedใŒๆง‹็ฏ‰ใ—ใŸใ‚‚ใฎใ‚‚ใ‚ใ‚Šใพใ™: [DeepSpeed-MoE: Advancing Mixture-of-Experts Inference and Training to Power Next-Generation AI Scale](https://huggingface.co/papers/2201.05596)ใ€[Mixture of Experts](https://www.deepspeed.ai/tutorials/mixture-of-experts/) - ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹: [1](https://www.microsoft.com/en-us/research/blog/deepspeed-powers-8x-larger-moe-model-training-with-high-performance/)ใ€[2](https://www.microsoft.com/en-us/research/publication/scalable-and-efficient-moe-training-for-multitask-multilingual-models/)ใ€ๅคง่ฆๆจกใชTransformerใƒ™ใƒผใ‚นใฎ่‡ช็„ถ่จ€่ชž็”Ÿๆˆใƒขใƒ‡ใƒซใฎๅ…ทไฝ“็š„ใชๅฑ•้–‹ใซใคใ„ใฆใฏใ€[ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://www.deepspeed.ai/2021/12/09/deepspeed-moe-nlg.html)ใ€[Megatron-Deepspeedใƒ–ใƒฉใƒณใƒ](https://github.com/microsoft/Megatron-DeepSpeed/tree/moe-training)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + +## PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจFlash Attentionใฎไฝฟ็”จ + +PyTorch 2.0ใงใฏใ€ใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ[`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html)๏ผˆSDPA๏ผ‰ใŒใƒชใƒชใƒผใ‚นใ•ใ‚Œใ€[ใƒกใƒขใƒชๅŠน็އใฎ้ซ˜ใ„ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ](https://huggingface.co/papers/2112.05682)ใ‚„[ใƒ•ใƒฉใƒƒใ‚ทใƒฅใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ](https://huggingface.co/papers/2205.14135)ใชใฉใฎ่žๅˆใ•ใ‚ŒใŸGPUใ‚ซใƒผใƒใƒซใฎไฝฟ็”จใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ + +[`optimum`](https://github.com/huggingface/optimum)ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸๅพŒใ€้–ข้€ฃใ™ใ‚‹ๅ†…้ƒจใƒขใ‚ธใƒฅใƒผใƒซใ‚’็ฝฎใๆ›ใˆใฆใ€PyTorchใฎใƒใ‚คใƒ†ใ‚ฃใƒ–ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซ่จญๅฎšใ—ใพใ™๏ผš + + +```python +model = model.to_bettertransformer() +``` + +ๅค‰ๆ›ๅพŒใ€้€šๅธธ้€šใ‚Šใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใใ ใ•ใ„ใ€‚ + + + +PyTorchใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎ`scaled_dot_product_attention`ๆผ”็ฎ—ๅญใฏใ€`attention_mask`ใŒๆไพ›ใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใซใฎใฟFlash Attentionใซใƒ‡ใ‚ฃใ‚นใƒ‘ใƒƒใƒใงใใพใ™ใ€‚ + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒขใƒผใƒ‰ใงBetterTransformer็ตฑๅˆใฏใƒžใ‚นใ‚ฏใ‚ตใƒใƒผใƒˆใ‚’ๅ‰Š้™คใ—ใ€ใƒใƒƒใƒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒžใ‚นใ‚ฏใŒๅฟ…่ฆใชใ„ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ—ใ‹ไฝฟ็”จใงใใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ไพ‹ใˆใฐใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚„ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎใ‚ˆใ†ใชใ€ใƒใƒƒใƒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒžใ‚นใ‚ฏใŒไธ่ฆใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๅ ดๅˆใซ่ฉฒๅฝ“ใ—ใพใ™ใ€‚BetterTransformerใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒžใ‚นใ‚ฏใŒๅฟ…่ฆใชใ‚ฟใ‚นใ‚ฏใซๅฏพใ™ใ‚‹ใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซใฏ้ฉใ—ใฆใ„ใพใ›ใ‚“ใ€‚ + + + +SDPAใ‚’ไฝฟ็”จใ—ใŸใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ทใƒงใƒณใจใƒกใƒขใƒชใฎ็ฏ€็ด„ใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€ใ“ใฎ[ใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://pytorch.org/blog/out-of-the-box-acceleration/)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_special.md new file mode 100644 index 0000000000000000000000000000000000000000..080ff66f4cf5628dd134c0ec5a0e10015604ad58 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_special.md @@ -0,0 +1,24 @@ + + +# Training on Specialized Hardware + + + +ๆณจๆ„: [ๅ˜ไธ€GPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_one)ใง็ดนไป‹ใ•ใ‚ŒใŸใปใจใ‚“ใฉใฎๆˆฆ็•ฅ๏ผˆๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚„ๅ‹พ้…่“„็ฉใชใฉ๏ผ‰ใŠใ‚ˆใณ[ใƒžใƒซใƒGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_many)ใฏไธ€่ˆฌ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒขใƒ‡ใƒซใซ้ฉ็”จใ•ใ‚Œใ‚‹ๆฑŽ็”จ็š„ใชใ‚‚ใฎใงใ™ใฎใงใ€ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใซๅ…ฅใ‚‹ๅ‰ใซใใ‚Œใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใ€ๅฐ‚็”จใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใ‚’่ฟ‘ๆ—ฅไธญใซ่ฟฝๅŠ ไบˆๅฎšใงใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu.md new file mode 100644 index 0000000000000000000000000000000000000000..aadd588ae84d35afb79f2ca89d9c0983c0634c37 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu.md @@ -0,0 +1,24 @@ + + +# Training on TPUs + + + + ๆณจๆ„: [ใ‚ทใƒณใ‚ฐใƒซGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_one)ใง็ดนไป‹ใ•ใ‚Œใฆใ„ใ‚‹ใปใจใ‚“ใฉใฎๆˆฆ็•ฅ๏ผˆๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚„ๅ‹พ้…่“„็ฉใชใฉ๏ผ‰ใŠใ‚ˆใณ[ใƒžใƒซใƒGPUใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_many)ใฏไธ€่ˆฌ็š„ใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซ้ฉ็”จใงใใพใ™ใฎใงใ€ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใซๅ…ฅใ‚‹ๅ‰ใซใใ‚Œใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใ€TPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใ‚’ใพใ‚‚ใชใ่ฟฝๅŠ ใ„ใŸใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu_tf.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu_tf.md new file mode 100644 index 0000000000000000000000000000000000000000..3ffe88267cddeb9cb254be39d215c5f8a3ea64e2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perf_train_tpu_tf.md @@ -0,0 +1,168 @@ + + +# Training on TPU with TensorFlow + + + +่ฉณ็ดฐใช่ชฌๆ˜ŽใŒไธ่ฆใงใ€ๅ˜ใซTPUใฎใ‚ณใƒผใƒ‰ใ‚ตใƒณใƒ—ใƒซใ‚’ๅ…ฅๆ‰‹ใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใŸใ„ๅ ดๅˆใฏใ€[็งใŸใกใฎTPUใฎไพ‹ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb) + + + +### What is a TPU? + +TPUใฏ**Tensor Processing Unit๏ผˆใƒ†ใƒณใ‚ฝใƒซๅ‡ฆ็†ใƒฆใƒ‹ใƒƒใƒˆ๏ผ‰**ใฎ็•ฅใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏGoogleใŒ่จญ่จˆใ—ใŸใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใ€ใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏๅ†…ใฎใƒ†ใƒณใ‚ฝใƒซ่จˆ็ฎ—ใ‚’ๅคงๅน…ใซ้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚ŒใฏGPUใฎใ‚ˆใ†ใชใ‚‚ใฎใงใ™ใ€‚ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–ใฎไธกๆ–นใซไฝฟ็”จใงใใพใ™ใ€‚ไธ€่ˆฌ็š„ใซใฏGoogleใฎใ‚ฏใƒฉใ‚ฆใƒ‰ใ‚ตใƒผใƒ“ใ‚นใ‚’ไป‹ใ—ใฆใ‚ขใ‚ฏใ‚ปใ‚นใ•ใ‚Œใพใ™ใŒใ€Google ColabใจKaggle Kernelsใ‚’้€šใ˜ใฆใ‚‚็„กๆ–™ใงๅฐ่ฆๆจกใฎTPUใซ็›ดๆŽฅใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ + +[๐Ÿค— Transformersใฎใ™ในใฆใฎTensorFlowใƒขใƒ‡ใƒซใฏKerasใƒขใƒ‡ใƒซใงใ™](https://huggingface.co/blog/tensorflow-philosophy)ใฎใงใ€ใ“ใฎๆ–‡ๆ›ธใฎใปใจใ‚“ใฉใฎๆ–นๆณ•ใฏไธ€่ˆฌ็š„ใซKerasใƒขใƒ‡ใƒซ็”จใฎTPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซ้ฉ็”จใงใใพใ™๏ผใŸใ ใ—ใ€TransformersใจDatasetsใฎHuggingFaceใ‚จใ‚ณใ‚ทใ‚นใƒ†ใƒ ๏ผˆhug-o-system๏ผŸ๏ผ‰ใซๅ›บๆœ‰ใฎใƒใ‚คใƒณใƒˆใ‚‚ใ„ใใคใ‹ใ‚ใ‚Šใ€ใใ‚Œใซใคใ„ใฆใฏ้ฉ็”จใ™ใ‚‹ใจใใซใใ‚Œใ‚’็คบใ—ใพใ™ใ€‚ + +### What kinds of TPU are available? + +ๆ–ฐใ—ใ„ใƒฆใƒผใ‚ถใƒผใฏใ€ใ•ใพใ–ใพใชTPUใจใใฎใ‚ขใ‚ฏใ‚ปใ‚นๆ–นๆณ•ใซ้–ขใ™ใ‚‹ๅน…ๅบƒใ„ๆƒ…ๅ ฑใซใ‚ˆใๆททไนฑใ—ใพใ™ใ€‚็†่งฃใ™ใ‚‹ใŸใ‚ใฎๆœ€ๅˆใฎ้‡่ฆใช้•ใ„ใฏใ€**TPUใƒŽใƒผใƒ‰**ใจ**TPU VM**ใฎ้•ใ„ใงใ™ใ€‚ + +**TPUใƒŽใƒผใƒ‰**ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ไบ‹ๅฎŸไธŠใƒชใƒขใƒผใƒˆใฎTPUใซ้–“ๆŽฅ็š„ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใพใ™ใ€‚ๅˆฅๅ€‹ใฎVMใŒๅฟ…่ฆใงใ€ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใจใƒ‡ใƒผใ‚ฟใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅˆๆœŸๅŒ–ใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒชใƒขใƒผใƒˆใƒŽใƒผใƒ‰ใซ่ปข้€ใ—ใพใ™ใ€‚Google ColabใงTPUใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€**TPUใƒŽใƒผใƒ‰**ใ‚นใ‚ฟใ‚คใƒซใงใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใ„ใพใ™ใ€‚ + +TPUใƒŽใƒผใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใใ‚Œใซๆ…ฃใ‚Œใฆใ„ใชใ„ไบบใ€…ใซใฏใ‹ใชใ‚ŠไบˆๆœŸใ—ใชใ„ๅ‹•ไฝœใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ็‰นใซใ€TPUใฏPythonใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใ„ใ‚‹ใƒžใ‚ทใƒณใจ็‰ฉ็†็š„ใซ็•ฐใชใ‚‹ใ‚ทใ‚นใƒ†ใƒ ใซ้…็ฝฎใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใƒ‡ใƒผใ‚ฟใฏใƒญใƒผใ‚ซใƒซใƒžใ‚ทใƒณใซใƒญใƒผใ‚ซใƒซใงๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹ใƒ‡ใƒผใ‚ฟใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใŒๅฎŒๅ…จใซๅคฑๆ•—ใ—ใพใ™ใ€‚ไปฃใ‚ใ‚Šใซใ€ใƒ‡ใƒผใ‚ฟใฏGoogle Cloud Storageใซๆ ผ็ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ“ใงใƒ‡ใƒผใ‚ฟใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใƒชใƒขใƒผใƒˆใฎTPUใƒŽใƒผใƒ‰ใงๅฎŸ่กŒใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ใƒ‡ใƒผใ‚ฟใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ + + + +ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใ‚’`np.ndarray`ใพใŸใฏ`tf.Tensor`ใจใ—ใฆใƒกใƒขใƒชใซๅŽใ‚ใ‚‹ใ“ใจใŒใงใใ‚‹ๅ ดๅˆใ€ColabใพใŸใฏTPUใƒŽใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใงใ‚‚ใ€ใƒ‡ใƒผใ‚ฟใ‚’Google Cloud Storageใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ›ใšใซ`fit()`ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใพใ™ใ€‚ + + + + + +**๐Ÿค— Hugging Faceๅ›บๆœ‰ใฎใƒ’ใƒณใƒˆ๐Ÿค—:** TFใ‚ณใƒผใƒ‰ใฎไพ‹ใงใ‚ˆใ่ฆ‹ใ‚‹ใงใ‚ใ‚ใ†`Dataset.to_tf_dataset()`ใจใใฎ้ซ˜ใƒฌใƒ™ใƒซใฎใƒฉใƒƒใƒ‘ใƒผใงใ‚ใ‚‹`model.prepare_tf_dataset()`ใฏใ€TPUใƒŽใƒผใƒ‰ใงๅคฑๆ•—ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€`tf.data.Dataset`ใ‚’ไฝœๆˆใ—ใฆใ„ใ‚‹ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€ใใ‚ŒใŒใ€Œ็ด”็ฒ‹ใชใ€`tf.data`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใฏใชใใ€`tf.numpy_function`ใพใŸใฏ`Dataset.from_generator()`ใ‚’ไฝฟ็”จใ—ใฆๅŸบ็›คใจใชใ‚‹HuggingFace `Dataset`ใ‹ใ‚‰ใƒ‡ใƒผใ‚ฟใ‚’ใ‚นใƒˆใƒชใƒผใƒ ใง่ชญใฟ่พผใ‚€ใ“ใจใ‹ใ‚‰ใงใ™ใ€‚ใ“ใฎHuggingFace `Dataset`ใฏใƒญใƒผใ‚ซใƒซใƒ‡ใ‚ฃใ‚นใ‚ฏไธŠใฎใƒ‡ใƒผใ‚ฟใ‚’ใƒใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใ—ใฆใŠใ‚Šใ€ใƒชใƒขใƒผใƒˆTPUใƒŽใƒผใƒ‰ใŒ่ชญใฟๅ–ใ‚‹ใ“ใจใŒใงใใชใ„ใŸใ‚ใงใ™ใ€‚ + + + +TPUใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹็ฌฌไบŒใฎๆ–นๆณ•ใฏใ€**TPU VM**ใ‚’ไป‹ใ—ใฆใงใ™ใ€‚TPU VMใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€TPUใŒๆŽฅ็ถšใ•ใ‚Œใฆใ„ใ‚‹ใƒžใ‚ทใƒณใซ็›ดๆŽฅๆŽฅ็ถšใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏGPU VMใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ†ใฎใจๅŒๆง˜ใงใ™ใ€‚TPU VMใฏไธ€่ˆฌ็š„ใซใƒ‡ใƒผใ‚ฟใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซ้–ขใ—ใฆใฏ็‰นใซไฝœๆฅญใŒใ—ใ‚„ใ™ใใ€ไธŠ่จ˜ใฎใ™ในใฆใฎ่ญฆๅ‘ŠใฏTPU VMใซใฏ้ฉ็”จใ•ใ‚Œใพใ›ใ‚“๏ผ + +ใ“ใ‚Œใฏไธป่ฆณ็š„ใชๆ–‡ๆ›ธใงใ™ใฎใงใ€ใ“ใกใ‚‰ใฎๆ„่ฆ‹ใงใ™๏ผš**ๅฏ่ƒฝใช้™ใ‚ŠTPUใƒŽใƒผใƒ‰ใฎไฝฟ็”จใ‚’้ฟใ‘ใฆใใ ใ•ใ„ใ€‚** TPU VMใ‚ˆใ‚Šใ‚‚ๆททไนฑใ—ใ‚„ใ™ใใ€ใƒ‡ใƒใƒƒใ‚ฐใŒ้›ฃใ—ใ„ใงใ™ใ€‚ๅฐ†ๆฅ็š„ใซใฏใ‚ตใƒใƒผใƒˆใ•ใ‚Œใชใใชใ‚‹ๅฏ่ƒฝๆ€งใ‚‚ใ‚ใ‚Šใพใ™ - Googleใฎๆœ€ๆ–ฐใฎTPUใงใ‚ใ‚‹TPUv4ใฏใ€TPU VMใจใ—ใฆใฎใฟใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใŸใ‚ใ€TPUใƒŽใƒผใƒ‰ใฏๅฐ†ๆฅ็š„ใซใฏใ€Œใƒฌใ‚ฌใ‚ทใƒผใ€ใฎใ‚ขใ‚ฏใ‚ปใ‚นๆ–นๆณ•ใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใงใ™ใ€‚ใŸใ ใ—ใ€็„กๆ–™ใงTPUใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใฎใฏColabใจKaggle Kernelsใฎๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใใฎๅ ดๅˆใ€ใฉใ†ใ—ใฆใ‚‚ไฝฟ็”จใ—ใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„ๅ ดๅˆใฎๅ–ใ‚Šๆ‰ฑใ„ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใ‚ˆใ†ใจใ—ใพใ™๏ผ่ฉณ็ดฐใฏ[TPUใฎไพ‹ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb)ใง่ฉณ็ดฐใช่ชฌๆ˜Žใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +### What sizes of TPU are available? + +ๅ˜ไธ€ใฎTPU๏ผˆv2-8/v3-8/v4-8๏ผ‰ใฏ8ใคใฎใƒฌใƒ—ใƒชใ‚ซใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚TPUใฏๆ•ฐ็™พใ‹ใ‚‰ๆ•ฐๅƒใฎใƒฌใƒ—ใƒชใ‚ซใ‚’ๅŒๆ™‚ใซๅฎŸ่กŒใงใใ‚‹**ใƒใƒƒใƒ‰**ใซๅญ˜ๅœจใ—ใพใ™ใ€‚ๅ˜ไธ€ใฎTPUใ‚ˆใ‚Šใ‚‚ๅคšใใฎTPUใ‚’ไฝฟ็”จใ™ใ‚‹ใŒใ€ใƒใƒƒใƒ‰ๅ…จไฝ“ใงใฏใชใ„ๅ ดๅˆ๏ผˆใŸใจใˆใฐv3-32๏ผ‰ใ€TPUใƒ•ใƒชใƒผใƒˆใฏ**ใƒใƒƒใƒ‰ใ‚นใƒฉใ‚คใ‚น**ใจใ—ใฆๅ‚็…งใ•ใ‚Œใพใ™ใ€‚ + +Colabใ‚’ไป‹ใ—ใฆ็„กๆ–™ใฎTPUใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ๅ ดๅˆใ€้€šๅธธใฏๅ˜ไธ€ใฎv2-8 TPUใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ + + +### I keep hearing about this XLA thing. Whatโ€™s XLA, and how does it relate to TPUs? + +XLAใฏใ€TensorFlowใจJAXใฎไธกๆ–นใงไฝฟ็”จใ•ใ‚Œใ‚‹ๆœ€้ฉๅŒ–ใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ™ใ€‚JAXใงใฏๅ”ฏไธ€ใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ‚ใ‚Šใ€TensorFlowใงใฏใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใŒ๏ผˆใ—ใ‹ใ—TPUใงใฏๅฟ…้ ˆใงใ™๏ผ๏ผ‰ใ€Kerasใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใซ`model.compile()`ใซๅผ•ๆ•ฐ`jit_compile=True`ใ‚’ๆธกใ™ใ“ใจใงๆœ€ใ‚‚็ฐกๅ˜ใซๆœ‰ๅŠนใซใงใใพใ™ใ€‚ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ›ใšใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒ่‰ฏๅฅฝใงใ‚ใ‚Œใฐใ€ใใ‚ŒใฏTPUใซ็งป่กŒใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใฃใŸ่‰ฏใ„ๅ…†ๅ€™ใงใ™๏ผ + +TPUไธŠใงใฎใƒ‡ใƒใƒƒใ‚ฐใฏไธ€่ˆฌ็š„ใซCPU/GPUใ‚ˆใ‚Šใ‚‚ๅฐ‘ใ—้›ฃใ—ใ„ใŸใ‚ใ€TPUใง่ฉฆใ™ๅ‰ใซใพใšCPU/GPUใงXLAใ‚’ไฝฟ็”จใ—ใฆใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ‚‚ใกใ‚ใ‚“ใ€้•ทๆ™‚้–“ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒขใƒ‡ใƒซใจใƒ‡ใƒผใ‚ฟใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใŒๆœŸๅพ…้€šใ‚Šใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎๆ•ฐใ‚นใƒ†ใƒƒใƒ—ใ ใ‘ใงใ™ใ€‚ + + + +XLAใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใฏ้€šๅธธ้ซ˜้€Ÿใงใ™ใ€‚ใ—ใŸใŒใฃใฆใ€TPUใงๅฎŸ่กŒใ™ใ‚‹ไบˆๅฎšใŒใชใ„ๅ ดๅˆใงใ‚‚ใ€`jit_compile=True`ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใŸใ ใ—ใ€ไปฅไธ‹ใฎXLAไบ’ๆ›ๆ€งใซ้–ขใ™ใ‚‹ๆณจๆ„ไบ‹้ …ใซๆณจๆ„ใ—ใฆใใ ใ•ใ„๏ผ + + + + + +**่‹ฆใ„็ตŒ้จ“ใ‹ใ‚‰็”Ÿใพใ‚ŒใŸใƒ’ใƒณใƒˆ:** `jit_compile=True`ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใ€CPU/GPUใ‚ณใƒผใƒ‰ใŒXLAไบ’ๆ›ใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€้€Ÿๅบฆใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹่‰ฏใ„ๆ–นๆณ•ใงใ™ใŒใ€ๅฎŸ้š›ใซTPUใงใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ™ใ‚‹้š›ใซใฏๅคšใใฎๅ•้กŒใ‚’ๅผ•ใ่ตทใ“ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ XLAใ‚ณใƒณใƒ‘ใ‚คใƒซใฏTPUไธŠใงๆš—้ป™็š„ใซ่กŒใ‚ใ‚Œใ‚‹ใŸใ‚ใ€ๅฎŸ้š›ใซใ‚ณใƒผใƒ‰ใ‚’TPUใงๅฎŸ่กŒใ™ใ‚‹ๅ‰ใซใใฎ่กŒใ‚’ๅ‰Š้™คใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„๏ผ + + + +### How do I make my model XLA compatible? + +ๅคšใใฎๅ ดๅˆใ€ใ‚ณใƒผใƒ‰ใฏใ™ใงใซXLAไบ’ๆ›ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผใŸใ ใ—ใ€XLAใงใฏๅ‹•ไฝœใ™ใ‚‹้€šๅธธใฎTensorFlowใงใ‚‚ๅ‹•ไฝœใ—ใชใ„ใ„ใใคใ‹ใฎ่ฆ็ด ใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใซใ€3ใคใฎไธป่ฆใชใƒซใƒผใƒซใซใพใจใ‚ใฆใ„ใพใ™๏ผš + + + +**๐Ÿค— HuggingFaceๅ›บๆœ‰ใฎใƒ’ใƒณใƒˆ๐Ÿค—:** TensorFlowใƒขใƒ‡ใƒซใจๆๅคฑ้–ขๆ•ฐใ‚’XLAไบ’ๆ›ใซๆ›ธใ็›ดใ™ใŸใ‚ใซๅคšใใฎๅŠชๅŠ›ใ‚’ๆ‰•ใฃใฆใ„ใพใ™ใ€‚้€šๅธธใ€ใƒขใƒ‡ใƒซใจๆๅคฑ้–ขๆ•ฐใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใƒซใƒผใƒซ๏ผƒ1ใจ๏ผƒ2ใซๅพ“ใฃใฆใ„ใ‚‹ใŸใ‚ใ€`transformers`ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ“ใ‚Œใ‚‰ใ‚’ใ‚นใ‚ญใƒƒใƒ—ใงใใพใ™ใ€‚ใŸใ ใ—ใ€็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใจๆๅคฑ้–ขๆ•ฐใ‚’่จ˜่ฟฐใ™ใ‚‹ๅ ดๅˆใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒซใƒผใƒซใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„๏ผ + + + +#### XLA Rule #1: Your code cannot have โ€œdata-dependent conditionalsโ€ + +ใ“ใ‚Œใฏใ€ไปปๆ„ใฎ`if`ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใŒ`tf.Tensor`ๅ†…ใฎๅ€คใซไพๅญ˜ใ—ใฆใ„ใชใ„ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎใ‚ณใƒผใƒ‰ใƒ–ใƒญใƒƒใ‚ฏใฏXLAใงใ‚ณใƒณใƒ‘ใ‚คใƒซใงใใพใ›ใ‚“๏ผ + +```python +if tf.reduce_sum(tensor) > 10: + tensor = tensor / 2.0 +``` + +ใ“ใ‚Œใฏๆœ€ๅˆใฏ้žๅธธใซๅˆถ้™็š„ใซๆ€ใˆใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ใปใจใ‚“ใฉใฎใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใ‚ณใƒผใƒ‰ใฏใ“ใ‚Œใ‚’่กŒใ†ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚้€šๅธธใ€ใ“ใฎๅˆถ็ด„ใ‚’ๅ›ž้ฟใ™ใ‚‹ใŸใ‚ใซ`tf.cond`ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹๏ผˆใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใ“ใกใ‚‰ใ‚’ๅ‚็…ง๏ผ‰ใ€ๆกไปถใ‚’ๅ‰Š้™คใ—ใฆไปฃใ‚ใ‚ŠใซๆŒ‡็คบๅค‰ๆ•ฐใ‚’ไฝฟ็”จใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆฌกใฎใ‚ˆใ†ใซ๏ผš + +```python +sum_over_10 = tf.cast(tf.reduce_sum(tensor) > 10, tf.float32) +tensor = tensor / (1.0 + sum_over_10) +``` + +ใ“ใฎใ‚ณใƒผใƒ‰ใฏใ€ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใจใพใฃใŸใๅŒใ˜ๅŠนๆžœใ‚’ๆŒใฃใฆใ„ใพใ™ใŒใ€ๆกไปถใ‚’ๅ›ž้ฟใ™ใ‚‹ใ“ใจใงใ€XLAใงๅ•้กŒใชใใ‚ณใƒณใƒ‘ใ‚คใƒซใงใใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™๏ผ + +#### XLA Rule #2: Your code cannot have โ€œdata-dependent shapesโ€ + +ใ“ใ‚Œใฏใ€ใ‚ณใƒผใƒ‰ๅ†…ใฎใ™ในใฆใฎ `tf.Tensor` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅฝข็ŠถใŒใ€ใใฎๅ€คใซไพๅญ˜ใ—ใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€`tf.unique` ้–ขๆ•ฐใฏXLAใงใ‚ณใƒณใƒ‘ใ‚คใƒซใงใใชใ„ใฎใงใ€ใ“ใฎใƒซใƒผใƒซใซ้•ๅใ—ใพใ™ใ€‚ใชใœใชใ‚‰ใ€ใ“ใ‚Œใฏๅ…ฅๅŠ› `Tensor` ใฎไธ€ๆ„ใฎๅ€คใฎๅ„ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ๅซใ‚€ `tensor` ใ‚’่ฟ”ใ™ใŸใ‚ใงใ™ใ€‚ใ“ใฎๅ‡บๅŠ›ใฎๅฝข็Šถใฏใ€ๅ…ฅๅŠ› `Tensor` ใฎ้‡่ค‡ๅ…ทๅˆใซใ‚ˆใฃใฆ็•ฐใชใ‚‹ใŸใ‚ใ€XLAใฏใใ‚Œใ‚’ๅ‡ฆ็†ใ—ใชใ„ใ“ใจใซใชใ‚Šใพใ™๏ผ + +ไธ€่ˆฌ็š„ใซใ€ใปใจใ‚“ใฉใฎใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚ณใƒผใƒ‰ใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใƒซใƒผใƒซ๏ผƒ2ใซๅพ“ใ„ใพใ™ใ€‚ใŸใ ใ—ใ€ใ„ใใคใ‹ใฎไธ€่ˆฌ็š„ใชใ‚ฑใƒผใ‚นใงใฏๅ•้กŒใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚้žๅธธใซไธ€่ˆฌ็š„ใชใ‚ฑใƒผใ‚นใฎ1ใคใฏใ€**ใƒฉใƒ™ใƒซใƒžใ‚นใ‚ญใƒณใ‚ฐ**ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใงใ™ใ€‚ใƒฉใƒ™ใƒซใ‚’็„ก่ฆ–ใ—ใฆๆๅคฑใ‚’่จˆ็ฎ—ใ™ใ‚‹ๅ ดๆ‰€ใ‚’็คบใ™ใŸใ‚ใซใ€ใƒฉใƒ™ใƒซใ‚’่ฒ ใฎๅ€คใซ่จญๅฎšใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚NumPyใพใŸใฏPyTorchใฎใƒฉใƒ™ใƒซใƒžใ‚นใ‚ญใƒณใ‚ฐใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๆๅคฑ้–ขๆ•ฐใ‚’่ฆ‹ใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใช[ใƒ–ใƒผใƒซใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น](https://numpy.org/doc/stable/user/basics.indexing.html#boolean-array-indexing)ใ‚’ไฝฟ็”จใ—ใŸใ‚ณใƒผใƒ‰ใŒใ‚ˆใ่ฆ‹ใ‚‰ใ‚Œใพใ™๏ผš + + +```python +label_mask = labels >= 0 +masked_outputs = outputs[label_mask] +masked_labels = labels[label_mask] +loss = compute_loss(masked_outputs, masked_labels) +mean_loss = torch.mean(loss) +``` + +ใ“ใฎใ‚ณใƒผใƒ‰ใฏNumPyใ‚„PyTorchใงใฏๅฎŒๅ…จใซๆฉŸ่ƒฝใ—ใพใ™ใŒใ€XLAใงใฏๅ‹•ไฝœใ—ใพใ›ใ‚“๏ผใชใœใชใ‚‰ใ€`masked_outputs`ใจ`masked_labels`ใฎๅฝข็Šถใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸไฝ็ฝฎใฎๆ•ฐใซไพๅญ˜ใ™ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏ**ใƒ‡ใƒผใ‚ฟไพๅญ˜ใฎๅฝข็Šถ**ใซใชใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ใƒซใƒผใƒซ๏ผƒ1ใจๅŒๆง˜ใซใ€ใ“ใฎใ‚ณใƒผใƒ‰ใ‚’ๆ›ธใ็›ดใ—ใฆใ€ใƒ‡ใƒผใ‚ฟไพๅญ˜ใฎๅฝข็Šถใชใ—ใงใพใฃใŸใๅŒใ˜ๅ‡บๅŠ›ใ‚’็”Ÿๆˆใงใใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```python +label_mask = tf.cast(labels >= 0, tf.float32) +loss = compute_loss(outputs, labels) +loss = loss * label_mask # Set negative label positions to 0 +mean_loss = tf.reduce_sum(loss) / tf.reduce_sum(label_mask) +``` + + +ใ“ใ“ใงใฏใ€ใƒ‡ใƒผใ‚ฟไพๅญ˜ใฎๅฝข็Šถใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใซใ€ๅ„ไฝ็ฝฎใงๆๅคฑใ‚’่จˆ็ฎ—ใ—ใฆใ‹ใ‚‰ใ€ๅนณๅ‡ใ‚’่จˆ็ฎ—ใ™ใ‚‹้š›ใซๅˆ†ๅญใจๅˆ†ๆฏใฎไธกๆ–นใงใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸไฝ็ฝฎใ‚’ใ‚ผใƒญๅŒ–ใ™ใ‚‹ๆ–นๆณ•ใ‚’็ดนไป‹ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆœ€ๅˆใฎใ‚ขใƒ—ใƒญใƒผใƒใจใพใฃใŸใๅŒใ˜็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ™ใŒใ€XLAไบ’ๆ›ๆ€งใ‚’็ถญๆŒใ—ใพใ™ใ€‚ๆณจๆ„็‚นใจใ—ใฆใ€ใƒซใƒผใƒซ๏ผƒ1ใจๅŒใ˜ใƒˆใƒชใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใพใ™ - `tf.bool`ใ‚’`tf.float32`ใซๅค‰ๆ›ใ—ใฆๆŒ‡ๆจ™ๅค‰ๆ•ฐใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ้žๅธธใซไพฟๅˆฉใชใƒˆใƒชใƒƒใ‚ฏใงใ™ใฎใงใ€่‡ชๅˆ†ใฎใ‚ณใƒผใƒ‰ใ‚’XLAใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใซใฏ่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„๏ผ + +#### XLA Rule #3: XLA will need to recompile your model for every different input shape it sees + +ใ“ใ‚Œใฏ้‡่ฆใชใƒซใƒผใƒซใงใ™ใ€‚ใ“ใ‚Œใฏใคใพใ‚Šใ€ๅ…ฅๅŠ›ๅฝข็ŠถใŒ้žๅธธใซๅค‰ๅ‹•็š„ใชๅ ดๅˆใ€XLA ใฏใƒขใƒ‡ใƒซใ‚’ไฝ•ๅบฆใ‚‚ๅ†ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ๅคงใใชใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ•้กŒใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใฏ NLP ใƒขใƒ‡ใƒซใงไธ€่ˆฌ็š„ใซ็™บ็”Ÿใ—ใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บๅพŒใฎๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใฎ้•ทใ•ใŒ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ไป–ใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใงใฏใ€้™็š„ใชๅฝข็ŠถใŒไธ€่ˆฌ็š„ใงใ‚ใ‚Šใ€ใ“ใฎใƒซใƒผใƒซใฏใปใจใ‚“ใฉๅ•้กŒใซใชใ‚Šใพใ›ใ‚“ใ€‚ + +ใƒซใƒผใƒซ๏ผƒ3ใ‚’ๅ›ž้ฟใ™ใ‚‹ๆ–นๆณ•ใฏไฝ•ใงใ—ใ‚‡ใ†ใ‹๏ผŸ้ตใฏใ€Œใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ€ใงใ™ - ใ™ในใฆใฎๅ…ฅๅŠ›ใ‚’ๅŒใ˜้•ทใ•ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ—ใ€ๆฌกใซใ€Œattention_maskใ€ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€ๅฏๅค‰ๅฝข็ŠถใจๅŒใ˜็ตๆžœใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใŒใ€XLA ใฎๅ•้กŒใฏ็™บ็”Ÿใ—ใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€้Žๅบฆใฎใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚‚ๆทฑๅˆปใช้…ๅปถใ‚’ๅผ•ใ่ตทใ“ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ - ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใงๆœ€ๅคงใฎ้•ทใ•ใซใ™ในใฆใฎใ‚ตใƒณใƒ—ใƒซใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใจใ€ๅคšใใฎ่จˆ็ฎ—ใจใƒกใƒขใƒชใ‚’็„ก้ง„ใซใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผ + +ใ“ใฎๅ•้กŒใซใฏๅฎŒ็’งใช่งฃๆฑบ็ญ–ใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใ„ใใคใ‹ใฎใƒˆใƒชใƒƒใ‚ฏใ‚’่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚้žๅธธใซไพฟๅˆฉใชใƒˆใƒชใƒƒใ‚ฏใฎ1ใคใฏใ€**ใƒใƒƒใƒใฎใ‚ตใƒณใƒ—ใƒซใ‚’32ใพใŸใฏ64ใƒˆใƒผใ‚ฏใƒณใฎๅ€ๆ•ฐใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹**ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒˆใƒผใ‚ฏใƒณๆ•ฐใŒใ‚ใšใ‹ใซๅข—ๅŠ ใ™ใ‚‹ใ ใ‘ใงใ€ใ™ในใฆใฎๅ…ฅๅŠ›ๅฝข็ŠถใŒ32ใพใŸใฏ64ใฎๅ€ๆ•ฐใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ไธ€ๆ„ใฎๅ…ฅๅŠ›ๅฝข็Šถใฎๆ•ฐใŒๅคงๅน…ใซๆธ›ๅฐ‘ใ—ใพใ™ใ€‚ไธ€ๆ„ใฎๅ…ฅๅŠ›ๅฝข็ŠถใŒๅฐ‘ใชใ„ใจใ€XLA ใฎๅ†ใ‚ณใƒณใƒ‘ใ‚คใƒซใŒๅฐ‘ใชใใชใ‚Šใพใ™๏ผ + + + +**๐Ÿค— HuggingFace ใซ้–ขใ™ใ‚‹ๅ…ทไฝ“็š„ใชใƒ’ใƒณใƒˆ๐Ÿค—:** ๅผŠ็คพใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใจใƒ‡ใƒผใ‚ฟใ‚ณใƒฌใ‚ฏใ‚ฟใƒผใซใฏใ€ใ“ใ“ใงๅฝน็ซ‹ใคใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ๅ‘ผใณๅ‡บใ™้š›ใซ `padding="max_length"` ใพใŸใฏ `padding="longest"` ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ‚ˆใ†ใซ่จญๅฎšใงใใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใจใƒ‡ใƒผใ‚ฟใ‚ณใƒฌใ‚ฏใ‚ฟใƒผใซใฏใ€ไธ€ๆ„ใฎๅ…ฅๅŠ›ๅฝข็Šถใฎๆ•ฐใ‚’ๆธ›ใ‚‰ใ™ใฎใซๅฝน็ซ‹ใค `pad_to_multiple_of` ๅผ•ๆ•ฐใ‚‚ใ‚ใ‚Šใพใ™๏ผ + + + +### How do I actually train my model on TPU? + +ไธ€ๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ XLA ไบ’ๆ›ๆ€งใŒใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€๏ผˆTPU Node/Colab ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใฏ๏ผ‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒ้ฉๅˆ‡ใซๆบ–ๅ‚™ใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€TPU ไธŠใงๅฎŸ่กŒใ™ใ‚‹ใ“ใจใฏ้ฉšใใปใฉ็ฐกๅ˜ใงใ™๏ผใ‚ณใƒผใƒ‰ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใฎใฏใ€ใ„ใใคใ‹ใฎ่กŒใ‚’่ฟฝๅŠ ใ—ใฆ TPU ใ‚’ๅˆๆœŸๅŒ–ใ—ใ€ใƒขใƒ‡ใƒซใจใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒ `TPUStrategy` ใ‚นใ‚ณใƒผใƒ—ๅ†…ใงไฝœๆˆใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใ ใ‘ใงใ™ใ€‚ใ“ใ‚Œใ‚’ๅฎŸ้š›ใซ่ฆ‹ใ‚‹ใซใฏใ€[TPU ใฎใ‚ตใƒณใƒ—ใƒซใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb)ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผ + +### Summary + +ใ“ใ“ใงใฏๅคšใใฎๆƒ…ๅ ฑใŒๆไพ›ใ•ใ‚Œใพใ—ใŸใฎใงใ€TPU ใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹้š›ใซไปฅไธ‹ใฎใƒใ‚งใƒƒใ‚ฏใƒชใ‚นใƒˆใ‚’ไฝฟ็”จใงใใพใ™๏ผš + +- ใ‚ณใƒผใƒ‰ใŒ XLA ใฎไธ‰ใคใฎใƒซใƒผใƒซใซๅพ“ใฃใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +- CPU/GPU ใง `jit_compile=True` ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใ€XLA ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +- ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒกใƒขใƒชใซ่ชญใฟ่พผใ‚€ใ‹ใ€TPU ไบ’ๆ›ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ่ชญใฟ่พผใฟใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ—ใพใ™๏ผˆ[ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ๅ‚็…ง](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb)๏ผ‰ใ€‚ +- ใ‚ณใƒผใƒ‰ใ‚’ Colab๏ผˆใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ฟใ‚’ใ€ŒTPUใ€ใซ่จญๅฎš๏ผ‰ใพใŸใฏ Google Cloud ใฎ TPU VM ใซ็งป่กŒใ—ใพใ™ใ€‚ +- TPU ๅˆๆœŸๅŒ–ใ‚ณใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผˆ[ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ๅ‚็…ง](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb)๏ผ‰ใ€‚ +- `TPUStrategy` ใ‚’ไฝœๆˆใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ชญใฟ่พผใฟใจใƒขใƒ‡ใƒซใฎไฝœๆˆใŒ `strategy.scope()` ๅ†…ใง่กŒใ‚ใ‚Œใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™๏ผˆ[ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ๅ‚็…ง](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb)๏ผ‰ใ€‚ +- TPU ใซ็งป่กŒใ™ใ‚‹้š›ใซ `jit_compile=True` ใ‚’ๅค–ใ™ใฎใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„๏ผ +- ๐Ÿ™๐Ÿ™๐Ÿ™๐Ÿฅบ๐Ÿฅบ๐Ÿฅบ +- `model.fit()` ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ +- ใŠใ‚ใงใจใ†ใ”ใ–ใ„ใพใ™๏ผ + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/performance.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/performance.md new file mode 100644 index 0000000000000000000000000000000000000000..bcd3987c5535363dd6a8a92c77b1504578038437 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/performance.md @@ -0,0 +1,68 @@ + + +# Performance and Scalability + +ๅคง่ฆๆจกใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆœฌ็•ช็’ฐๅขƒใธใฎๅฑ•้–‹ใฏใ•ใพใ–ใพใช่ชฒ้กŒใ‚’ๆ่ตทใ—ใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใฏใ€ใƒขใƒ‡ใƒซใŒๅˆฉ็”จๅฏ่ƒฝใชGPUใƒกใƒขใƒชใ‚ˆใ‚Šใ‚‚ๅคšใใ‚’ๅฟ…่ฆใจใ—ใŸใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้€ŸๅบฆใŒ้…ใ‹ใฃใŸใ‚Šใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‡ใƒ—ใƒญใ‚คใƒ•ใ‚งใƒผใ‚บใงใฏใ€ใƒขใƒ‡ใƒซใŒๆœฌ็•ช็’ฐๅขƒใงๅฟ…่ฆใชใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใฎใซ่‹ฆๅŠดใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏใ€ใ“ใ‚Œใ‚‰ใฎ่ชฒ้กŒใ‚’ๅ…‹ๆœใ—ใ€ใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซๆœ€้ฉใช่จญๅฎšใ‚’่ฆ‹ใคใ‘ใ‚‹ใฎใซๅฝน็ซ‹ใคใ“ใจใ‚’็›ฎ็š„ใจใ—ใฆใ„ใพใ™ใ€‚ +ใ‚ฌใ‚คใƒ‰ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆŽจ่ซ–ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใซๅˆ†ใ‹ใ‚ŒใฆใŠใ‚Šใ€ใใ‚Œใžใ‚Œ็•ฐใชใ‚‹่ชฒ้กŒใจ่งฃๆฑบ็ญ–ใŒๅญ˜ๅœจใ—ใพใ™ใ€‚ +ๅ„ใ‚ปใ‚ฏใ‚ทใƒงใƒณๅ†…ใซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใ‚ทใƒณใ‚ฐใƒซGPUๅฏพใƒžใƒซใƒGPUใ€ๆŽจ่ซ–็”จใฎCPUๅฏพGPUใชใฉใ€็•ฐใชใ‚‹ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขๆง‹ๆˆ็”จใฎๅˆฅใ€…ใฎใ‚ฌใ‚คใƒ‰ใŒ็”จๆ„ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‡บ็™บ็‚นใจใ—ใฆใ€ใ‚ทใƒŠใƒชใ‚ชใซๅˆใฃใŸๆ–นๆณ•ใซ้€ฒใ‚€ใŸใ‚ใฎๆƒ…ๅ ฑๆบใจใ—ใฆใ”ๅˆฉ็”จใใ ใ•ใ„ใ€‚ + +## Training + +ๅคง่ฆๆจกใชใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใซใฏใ€GPUใ‚„TPUใชใฉใฎใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ฟใŒๅฟ…่ฆใงใ™ใ€‚ +ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชใ‚ฑใƒผใ‚นใฏใ€ใ‚ทใƒณใ‚ฐใƒซGPUใŒใ‚ใ‚‹ๅ ดๅˆใงใ™ใ€‚ใ‚ทใƒณใ‚ฐใƒซGPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅŠน็އใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ใŸใ‚ใฎไธ€่ˆฌ็š„ใชใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅญฆใถใซใฏใ€ไปฅไธ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +* [ใ‚ทใƒณใ‚ฐใƒซGPUใงใฎๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎๆ–นๆณ•ใจใƒ„ใƒผใƒซ](perf_train_gpu_one): GPUใƒกใƒขใƒชใฎๅŠนๆžœ็š„ใชๅˆฉ็”จใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ้ซ˜้€ŸๅŒ–ใชใฉใ‚’ๆ”ฏๆดใ™ใ‚‹ๅ…ฑ้€šใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅญฆใถใŸใ‚ใซใ“ใ“ใ‹ใ‚‰ๅง‹ใ‚ใฆใใ ใ•ใ„ใ€‚ +* [ใƒžใƒซใƒGPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_gpu_many): ใƒžใƒซใƒGPU็’ฐๅขƒใซ้ฉ็”จใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใ€ใƒ†ใƒณใ‚ฝใƒซใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณไธฆๅˆ—ๆ€งใชใฉใ€ใ•ใ‚‰ใชใ‚‹ๆœ€้ฉๅŒ–ๆ–นๆณ•ใซใคใ„ใฆ่ฉณ็ดฐใซๅญฆใณใพใ™ใ€‚ +* [CPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ปใ‚ฏใ‚ทใƒงใƒณ](perf_train_cpu): CPUไธŠใงใฎๆททๅˆ็ฒพๅบฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใคใ„ใฆๅญฆใณใพใ™ใ€‚ +* [่ค‡ๆ•ฐCPUใงใฎๅŠน็އ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](perf_train_cpu_many): ๅˆ†ๆ•ฃCPUใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใคใ„ใฆๅญฆใณใพใ™ใ€‚ +* [TensorFlowใงTPUใ‚’ไฝฟ็”จใ—ใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](perf_train_tpu_tf): TPUใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€TPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจXLAใฎไฝฟ็”จใซใคใ„ใฆใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +* [ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใฎใ‚ซใ‚นใ‚ฟใƒ ใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ข](perf_hardware): ็‹ฌ่‡ชใฎใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐ็’ฐๅขƒใ‚’ๆง‹็ฏ‰ใ™ใ‚‹้š›ใฎใƒ’ใƒณใƒˆใ‚„ใƒˆใƒชใƒƒใ‚ฏใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ +* [Trainer APIใ‚’ไฝฟ็”จใ—ใŸใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๆคœ็ดข](hpo_train) + +## Inference + +ๆœฌ็•ช็’ฐๅขƒใงๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซๆŽจ่ซ–ใ™ใ‚‹ใ“ใจใฏใ€ใใ‚Œใ‚‰ใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใจๅŒใ˜ใใ‚‰ใ„้›ฃใ—ใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ +ไปฅไธ‹ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€CPUใŠใ‚ˆใณใ‚ทใƒณใ‚ฐใƒซ/ใƒžใƒซใƒGPU็’ฐๅขƒใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ‰‹้ †ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +* [ใ‚ทใƒณใ‚ฐใƒซCPUใงใฎๆŽจ่ซ–](perf_infer_cpu) +* [ใ‚ทใƒณใ‚ฐใƒซGPUใงใฎๆŽจ่ซ–](perf_infer_gpu_one) +* [ใƒžใƒซใƒGPUๆŽจ่ซ–](perf_infer_gpu_many) +* [TensorFlowใƒขใƒ‡ใƒซใฎXLA็ตฑๅˆ](tf_xla) + +## Training and inference + +ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ‹ใ€ใใ‚Œใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ‹ใซ้–ขไฟ‚ใชใ้ฉ็”จใ•ใ‚Œใ‚‹ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ€ใƒ’ใƒณใƒˆใ€ใƒˆใƒชใƒƒใ‚ฏใŒใ“ใ“ใซใ‚ใ‚Šใพใ™ใ€‚ + +* [ๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–](big_models) +* [ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎๅ•้กŒใฎใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐ](debugging) + +## Contribute + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใพใ ๅฎŒๅ…จใงใฏใชใใ€ใ•ใ‚‰ใซ่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹้ …็›ฎใŒใŸใใ•ใ‚“ใ‚ใ‚Šใพใ™ใ€‚ +่ฟฝๅŠ ใ‚„่จ‚ๆญฃใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€้ ๆ…ฎใ›ใšใซPRใ‚’ใ‚ชใƒผใƒ—ใƒณใ™ใ‚‹ใ‹ใ€่ฉณ็ดฐใ‚’่ญฐ่ซ–ใ™ใ‚‹ใŸใ‚ใซIssueใ‚’้–‹ๅง‹ใ—ใฆใใ ใ•ใ„ใ€‚ + +AใŒBใ‚ˆใ‚Šใ‚‚ๅ„ชใ‚Œใฆใ„ใ‚‹ใจใ„ใ†่ฒข็Œฎใ‚’่กŒใ†้š›ใซใฏใ€ๅ†็พๅฏ่ƒฝใชใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚„ใใฎๆƒ…ๅ ฑใฎๅ‡บๅ…ธใธใฎใƒชใƒณใ‚ฏใ‚’ๅซใ‚ใฆใฟใฆใใ ใ•ใ„๏ผˆใ‚ใชใŸ่‡ช่บซใฎๆƒ…ๅ ฑใงใ‚ใ‚‹ๅ ดๅˆใ‚’้™คใ๏ผ‰ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perplexity.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perplexity.md new file mode 100644 index 0000000000000000000000000000000000000000..368a301ec3ab4a36c6a21086e4ba748aad242a5e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/perplexity.md @@ -0,0 +1,116 @@ + + +# Perplexity of fixed-length models + +[[open-in-colab]] + +ใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃ๏ผˆPPL๏ผ‰ใฏ่จ€่ชžใƒขใƒ‡ใƒซใฎ่ฉ•ไพกใซๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชๆŒ‡ๆจ™ใฎ1ใคใงใ™ใ€‚ๆทฑๅ…ฅใ‚Šใ™ใ‚‹ๅ‰ใซใ€ใ“ใฎๆŒ‡ๆจ™ใฏ็‰นใซๅคๅ…ธ็š„ใช่จ€่ชžใƒขใƒ‡ใƒซ๏ผˆๆ™‚ใซใฏใ‚ชใƒผใƒˆใƒฌใ‚ฐใƒฌใƒƒใ‚ทใƒ–ใพใŸใฏๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒซใจใ‚‚ๅ‘ผใฐใ‚Œใ‚‹๏ผ‰ใซ้ฉ็”จใ•ใ‚Œใ€BERTใชใฉใฎใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใซใฏ้ฉใ—ใฆใ„ใชใ„ใ“ใจใซๆณจๆ„ใ™ในใใงใ™๏ผˆใƒขใƒ‡ใƒซใฎๆฆ‚่ฆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„[ใƒขใƒ‡ใƒซใฎๆฆ‚่ฆ](model_summary)๏ผ‰ใ€‚ + +ใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๆŒ‡ๆ•ฐๅนณๅ‡่ฒ ใฎๅฏพๆ•ฐๅฐคๅบฆใจใ—ใฆๅฎš็พฉใ•ใ‚Œใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚ŒใŸใ‚ทใƒผใ‚ฑใƒณใ‚น \\(X = (x_0, x_1, \dots, x_t)\\) ใŒใ‚ใ‚‹ๅ ดๅˆใ€\\(X\\) ใฎใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃใฏๆฌกใฎใ‚ˆใ†ใซ่กจใ•ใ‚Œใพใ™ใ€‚ + +$$\text{PPL}(X) = \exp \left\{ {-\frac{1}{t}\sum_i^t \log p_\theta (x_i|x_{ + +ใ—ใ‹ใ—ใ€้€šๅธธใ€่ฟ‘ไผผใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใŒๅ‡ฆ็†ใงใใ‚‹ใƒˆใƒผใ‚ฏใƒณๆ•ฐใซๅˆถ็ด„ใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ๆœ€ๅคงใฎ[GPT-2](model_doc/gpt2)ใฎใƒใƒผใ‚ธใƒงใƒณใฏ1024ใƒˆใƒผใ‚ฏใƒณใฎๅ›บๅฎš้•ทใ‚’ๆŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€1024ใ‚ˆใ‚Šใ‚‚ๅคงใใ„ \\(t\\) ใซๅฏพใ—ใฆ \\(p_\theta(x_t|x_{ + +ใ“ใ‚Œใฏๅ„ใ‚ปใ‚ฐใƒกใƒณใƒˆใฎใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃใŒ1ๅ›žใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใง่จˆ็ฎ—ใงใใ‚‹ใŸใ‚ใ€่จˆ็ฎ—ใŒ่ฟ…้€Ÿใงใ™ใŒใ€้€šๅธธใ€ใƒขใƒ‡ใƒซใฏใปใจใ‚“ใฉใฎไบˆๆธฌใ‚นใƒ†ใƒƒใƒ—ใงใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒๅฐ‘ใชใ„ใŸใ‚ใ€ๅฎŒๅ…จใซๅ› ๅญๅˆ†่งฃใ•ใ‚ŒใŸใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃใฎๆ‚ชใ„่ฟ‘ไผผใจใชใ‚Šใ€้€šๅธธใ€ใ‚ˆใ‚Š้ซ˜ใ„๏ผˆๆ‚ชใ„๏ผ‰PPLใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +ไปฃใ‚ใ‚Šใซใ€ๅ›บๅฎš้•ทใƒขใƒ‡ใƒซใฎPPLใฏใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๆˆฆ็•ฅใ‚’็”จใ„ใฆ่ฉ•ไพกใ™ใ‚‹ในใใงใ™ใ€‚ใ“ใ‚Œใซใฏใ€ใƒขใƒ‡ใƒซใŒๅ„ไบˆๆธฌใ‚นใƒ†ใƒƒใƒ—ใงใ‚ˆใ‚Šๅคšใใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆŒใคใ‚ˆใ†ใซใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใ‚’็นฐใ‚Š่ฟ”ใ—ใ‚นใƒฉใ‚คใƒ‰ใ•ใ›ใ‚‹ใจใ„ใ†ๆ–นๆณ•ใŒๅซใพใ‚Œใพใ™ใ€‚ + +Sliding window PPL taking advantage of all available context + +ใ“ใ‚Œใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ็ขบ็އใฎใ‚ˆใ‚Šๆญฃ็ขบใชๅˆ†่งฃใซ่ฟ‘ใ„ใ‚‚ใฎใงใ‚ใ‚Šใ€้€šๅธธใฏใ‚ˆใ‚Šๆœ‰ๅˆฉใชใ‚นใ‚ณใ‚ขใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ๆฌ ็‚นใฏใ€ใ‚ณใƒผใƒ‘ใ‚นๅ†…ใฎๅ„ใƒˆใƒผใ‚ฏใƒณใซๅฏพใ—ใฆๅˆฅๅ€‹ใฎๅ‰ๆ–นใƒ‘ใ‚นใŒๅฟ…่ฆใงใ™ใ€‚ๅฎŸ็”จ็š„ใชๅฆฅๅ”ๆกˆใฏใ€1ใƒˆใƒผใ‚ฏใƒณใšใคใ‚นใƒฉใ‚คใƒ‰ใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ใ‚ˆใ‚Šๅคงใใชใ‚นใƒˆใƒฉใ‚คใƒ‰ใงใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’็งปๅ‹•ใ™ใ‚‹ใ‚นใƒˆใƒฉใ‚คใƒ‰ๅž‹ใฎใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่จˆ็ฎ—ใŒใฏใ‚‹ใ‹ใซ้ซ˜้€Ÿใซ้€ฒ่กŒใงใใ‚‹ไธ€ๆ–นใงใ€ใƒขใƒ‡ใƒซใซใฏๅ„ใ‚นใƒ†ใƒƒใƒ—ใงไบˆๆธฌใ‚’่กŒใ†ใŸใ‚ใฎๅคงใใชใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ + +## Example: Calculating perplexity with GPT-2 in ๐Ÿค— Transformers + +GPT-2ใ‚’ไฝฟ็”จใ—ใฆใ“ใฎใƒ—ใƒญใ‚ปใ‚นใ‚’ใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +from transformers import GPT2LMHeadModel, GPT2TokenizerFast + +device = "cuda" +model_id = "openai-community/gpt2-large" +model = GPT2LMHeadModel.from_pretrained(model_id).to(device) +tokenizer = GPT2TokenizerFast.from_pretrained(model_id) +``` + +WikiText-2ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใ€็•ฐใชใ‚‹ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๆˆฆ็•ฅใ‚’ไฝฟ็”จใ—ใฆใƒ‘ใƒผใƒ—ใƒฌใ‚ญใ‚ทใƒ†ใ‚ฃใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏๅฐ่ฆๆจกใงใ€ใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅฏพใ—ใฆๅ˜ไธ€ใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ ใ‘ใชใฎใงใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ใƒกใƒขใƒชใซ่ชญใฟ่พผใ‚“ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใ ใ‘ใงๅๅˆ†ใงใ™ใ€‚ + + +```python +from datasets import load_dataset + +test = load_dataset("wikitext", "wikitext-2-raw-v1", split="test") +encodings = tokenizer("\n\n".join(test["text"]), return_tensors="pt") +``` + +๐Ÿค— Transformersใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๅ˜็ด”ใซ`input_ids`ใ‚’ใƒขใƒ‡ใƒซใฎ`labels`ใจใ—ใฆๆธกใ™ใ“ใจใงใ€ๅ„ใƒˆใƒผใ‚ฏใƒณใฎๅนณๅ‡่ฒ ใฎๅฏพๆ•ฐๅฐคๅบฆใŒๆๅคฑใจใ—ใฆ่ฟ”ใ•ใ‚Œใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฎใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€ๅ„ใ‚คใƒ†ใƒฌใƒผใ‚ทใƒงใƒณใงใƒขใƒ‡ใƒซใซๆธกใ™ใƒˆใƒผใ‚ฏใƒณใซใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—ใŒใ‚ใ‚Šใพใ™ใ€‚็งใŸใกใฏใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใจใ—ใฆๆ‰ฑใฃใฆใ„ใ‚‹ใƒˆใƒผใ‚ฏใƒณใฎๅฏพๆ•ฐๅฐคๅบฆใ‚’ๆๅคฑใซๅซใ‚ใŸใใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใใฎใŸใ‚ใ€ใ“ใ‚Œใ‚‰ใฎๅฏพ่ฑกใ‚’ `-100` ใซ่จญๅฎšใ—ใฆ็„ก่ฆ–ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ไปฅไธ‹ใฏใ€ใ‚นใƒˆใƒฉใ‚คใƒ‰ใ‚’ `512` ใจใ—ใŸๅ ดๅˆใฎไพ‹ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฏไปปๆ„ใฎใƒˆใƒผใ‚ฏใƒณใฎๆกไปถไป˜ใ‘ใฎๅฐคๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹้š›ใซใ€ๅฐ‘ใชใใจใ‚‚ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใจใ—ใฆ 512 ใƒˆใƒผใ‚ฏใƒณใ‚’ๆŒใคใ“ใจใซใชใ‚Šใพใ™๏ผˆ512 ๅ€‹ใฎๅ‰ใฎใƒˆใƒผใ‚ฏใƒณใŒๅˆฉ็”จๅฏ่ƒฝใงใ‚ใ‚‹ๅ ดๅˆ๏ผ‰ใ€‚ + + +```python +import torch +from tqdm import tqdm + +max_length = model.config.n_positions +stride = 512 +seq_len = encodings.input_ids.size(1) + +nlls = [] +prev_end_loc = 0 +for begin_loc in tqdm(range(0, seq_len, stride)): + end_loc = min(begin_loc + max_length, seq_len) + trg_len = end_loc - prev_end_loc # may be different from stride on last loop + input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device) + target_ids = input_ids.clone() + target_ids[:, :-trg_len] = -100 + + with torch.no_grad(): + outputs = model(input_ids, labels=target_ids) + + # loss is calculated using CrossEntropyLoss which averages over valid labels + # N.B. the model only calculates loss over trg_len - 1 labels, because it internally shifts the labels + # to the left by 1. + neg_log_likelihood = outputs.loss + + nlls.append(neg_log_likelihood) + + prev_end_loc = end_loc + if end_loc == seq_len: + break + +ppl = torch.exp(torch.stack(nlls).mean()) +``` + +ใ‚นใƒˆใƒฉใ‚คใƒ‰้•ทใŒๆœ€ๅคงๅ…ฅๅŠ›้•ทใจๅŒใ˜ๅ ดๅˆใ€ไธŠ่ฟฐใฎๆœ€้ฉใงใชใ„ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๆˆฆ็•ฅใจๅŒ็ญ‰ใงใ™ใ€‚ใ‚นใƒˆใƒฉใ‚คใƒ‰ใŒๅฐใ•ใ„ใปใฉใ€ใƒขใƒ‡ใƒซใฏๅ„ไบˆๆธฌใ‚’่กŒใ†้š›ใซใ‚ˆใ‚Šๅคšใใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆŒใคใŸใ‚ใ€้€šๅธธใ€ๅ ฑๅ‘Šใ•ใ‚Œใ‚‹ๅ›ฐ้›ฃๅบฆ๏ผˆperplexity๏ผ‰ใŒๅ‘ไธŠใ—ใพใ™ใ€‚ + +ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚’ `stride = 1024` ใงๅฎŸ่กŒใ™ใ‚‹ใจใ€ใ‚ชใƒผใƒใƒผใƒฉใƒƒใƒ—ใŒใชใ„็Šถๆ…‹ใงใ€็ตๆžœใฎๅ›ฐ้›ฃๅบฆ๏ผˆperplexity๏ผ‰ใฏ `19.44` ใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏ GPT-2 ใฎ่ซ–ๆ–‡ใซๅ ฑๅ‘Šใ•ใ‚ŒใŸ `19.93` ใจใปใผๅŒ็ญ‰ใงใ™ใ€‚ไธ€ๆ–นใ€`stride = 512` ใ‚’ไฝฟ็”จใ—ใ€ใ“ใฎใ‚ˆใ†ใซใ‚นใƒˆใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๆˆฆ็•ฅใ‚’ๆŽก็”จใ™ใ‚‹ใจใ€ๅ›ฐ้›ฃๅบฆ๏ผˆperplexity๏ผ‰ใŒ `16.45` ใซๅ‘ไธŠใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ‚ˆใ‚Šๅฅฝๆ„็š„ใชใ‚นใ‚ณใ‚ขใ ใ‘ใงใชใใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๅฐคๅบฆใฎ็œŸใฎ่‡ชๅทฑๅ›žๅธฐๅˆ†่งฃใซใ‚ˆใ‚Š่ฟ‘ใ„ๆ–นๆณ•ใง่จˆ็ฎ—ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/philosophy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/philosophy.md new file mode 100644 index 0000000000000000000000000000000000000000..3edef0bd2add3b20d37332d0afadb904214ab1b0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/philosophy.md @@ -0,0 +1,67 @@ + + +# Philosophy + +๐Ÿค— Transformersใฏใ€ๆฌกใฎใ‚ˆใ†ใช็›ฎ็š„ใงๆง‹็ฏ‰ใ•ใ‚ŒใŸๆ„่ฆ‹ใ‚’ๆŒใคใƒฉใ‚คใƒ–ใƒฉใƒชใงใ™๏ผš + +- ๅคง่ฆๆจกใชTransformersใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ€็ ”็ฉถใ€ใพใŸใฏๆ‹กๅผตใ—ใŸใ„ๆฉŸๆขฐๅญฆ็ฟ’็ ”็ฉถ่€…ใŠใ‚ˆใณๆ•™่‚ฒ่€…ใ€‚ +- ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใ‚Šใ€ๆœฌ็•ช็’ฐๅขƒใงๆไพ›ใ—ใŸใ‚Šใ€ใพใŸใฏใใฎไธกๆ–นใ‚’่กŒใ„ใŸใ„ๅฎŸๅ‹™ๅฎถใ€‚ +- ไธŽใˆใ‚‰ใ‚ŒใŸๆฉŸๆขฐๅญฆ็ฟ’ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆไฝฟ็”จใ—ใŸใ„ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใ€‚ + +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€2ใคใฎๅผทๅŠ›ใช็›ฎๆจ™ใ‚’ๆŒใฃใฆ่จญ่จˆใ•ใ‚Œใพใ—ใŸ๏ผš + +1. ใงใใ‚‹ใ ใ‘็ฐกๅ˜ใ‹ใค้ซ˜้€Ÿใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจ๏ผš + + - ใƒฆใƒผใ‚ถใƒผๅ‘ใ‘ใฎๆŠฝ่ฑกๅŒ–ใ‚’้™ใ‚Šใชใๅฐ‘ใชใใ—ใ€ๅฎŸ้š›ใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใ€ๆŠฝ่ฑกๅŒ–ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + ๅ„ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใช3ใคใฎๆจ™ๆบ–ใ‚ฏใƒฉใ‚นใ ใ‘ใŒๅญ˜ๅœจใ—ใพใ™๏ผš[ๆง‹ๆˆ](main_classes/configuration)ใ€ + [ใƒขใƒ‡ใƒซ](main_classes/model)ใ€ใŠใ‚ˆใณๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚น๏ผˆNLP็”จใฎ[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](main_classes/tokenizer)ใ€ใƒ“ใ‚ธใƒงใƒณ็”จใฎ[ใ‚คใƒกใƒผใ‚ธใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/image_processor)ใ€ + ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช็”จใฎ[็‰นๅพดๆŠฝๅ‡บๅ™จ](main_classes/feature_extractor)ใ€ใŠใ‚ˆใณใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›็”จใฎ[ใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/processors)๏ผ‰ใ€‚ + - ใ“ใ‚Œใ‚‰ใฎใ‚ฏใƒฉใ‚นใฏใ€ๅ…ฑ้€šใฎ`from_pretrained()`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‹ใ‚‰็ฐกๅ˜ใ‹ใค็ตฑไธ€ใ•ใ‚ŒใŸๆ–นๆณ•ใงๅˆๆœŸๅŒ–ใงใใพใ™ใ€‚ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰้–ข้€ฃใ™ใ‚‹ใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใจ้–ข้€ฃใƒ‡ใƒผใ‚ฟ๏ผˆๆง‹ๆˆใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎ่ชžๅฝ™ใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟ๏ผ‰ใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰๏ผˆๅฟ…่ฆใชๅ ดๅˆใฏใ‚ญใƒฃใƒƒใ‚ทใƒฅ๏ผ‰ใ—ใฆ่ชญใฟ่พผใฟใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใฎไธŠใซใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฏ2ใคใฎAPIใ‚’ๆไพ›ใ—ใฆใ„ใพใ™๏ผš[ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ]ใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใงใƒขใƒ‡ใƒซใ‚’ใ™ใฐใ‚„ใๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ‚ใ‚Šใ€[`Trainer`]ใฏPyTorchใƒขใƒ‡ใƒซใ‚’่ฟ…้€Ÿใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใพใŸใฏๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™๏ผˆใ™ในใฆใฎTensorFlowใƒขใƒ‡ใƒซใฏ`Keras.fit`ใจไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ™๏ผ‰ใ€‚ + - ใใฎ็ตๆžœใ€ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎใƒขใ‚ธใƒฅใƒฉใƒผใƒ„ใƒผใƒซใƒœใƒƒใ‚ฏใ‚นใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ๆ‹กๅผตใพใŸใฏๆง‹็ฏ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€้€šๅธธใฎPythonใ€PyTorchใ€TensorFlowใ€Kerasใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใ‹ใ‚‰็ถ™ๆ‰ฟใ—ใฆใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟใจไฟๅญ˜ใชใฉใฎๆฉŸ่ƒฝใ‚’ๅ†ๅˆฉ็”จใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐๅ“ฒๅญฆใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€[Repeat Yourself](https://huggingface.co/blog/transformers-design-philosophy)ใƒ–ใƒญใ‚ฐๆŠ•็จฟใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +2. ใ‚ชใƒชใ‚ธใƒŠใƒซใฎใƒขใƒ‡ใƒซใซใงใใ‚‹ใ ใ‘่ฟ‘ใ„ๆ€ง่ƒฝใ‚’ๆŒใคๆœ€ๆ–ฐใฎใƒขใƒ‡ใƒซใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจ๏ผš + + - ๅ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซๅฏพใ—ใฆใ€ๅ…ฌๅผใช่‘—่€…ใ‹ใ‚‰ๆไพ›ใ•ใ‚ŒใŸ็ตๆžœใ‚’ๅ†็พใ™ใ‚‹ๅฐ‘ใชใใจใ‚‚1ใคใฎไพ‹ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + - ใ‚ณใƒผใƒ‰ใฏ้€šๅธธใ€ๅฏ่ƒฝใช้™ใ‚Šๅ…ƒใฎใ‚ณใƒผใƒ‰ใƒ™ใƒผใ‚นใซ่ฟ‘ใ„ใ‚‚ใฎใงใ‚ใ‚Šใ€ใ“ใ‚ŒใฏPyTorchใ‚ณใƒผใƒ‰ใŒTensorFlowใ‚ณใƒผใƒ‰ใซๅค‰ๆ›ใ•ใ‚Œใ‚‹ใ“ใจใ‹ใ‚‰็”Ÿใ˜ใ€้€†ใ‚‚ใพใŸ็„ถใ‚Šใงใ™ใ€‚ + +ใใฎไป–ใฎใ„ใใคใ‹ใฎ็›ฎๆจ™๏ผš + +- ใƒขใƒ‡ใƒซใฎๅ†…้ƒจใ‚’ใงใใ‚‹ใ ใ‘ไธ€่ฒซใ—ใฆๅ…ฌ้–‹ใ™ใ‚‹ใ“ใจ๏ผš + + - ใƒ•ใƒซใช้š ใ‚Œ็Šถๆ…‹ใจๆณจๆ„ใฎ้‡ใฟใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ๅ˜ไธ€ใฎAPIใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + - ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใจๅŸบๆœฌใƒขใƒ‡ใƒซใฎAPIใฏๆจ™ๆบ–ๅŒ–ใ•ใ‚Œใ€็ฐกๅ˜ใซใƒขใƒ‡ใƒซ้–“ใ‚’ๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +- ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใจ่ชฟๆŸปใฎใŸใ‚ใฎๆœ‰ๆœ›ใชใƒ„ใƒผใƒซใ‚’ไธป่ฆณ็š„ใซ้ธๅฎšใ™ใ‚‹ใ“ใจ๏ผš + + - ่ชžๅฝ™ใจๅŸ‹ใ‚่พผใฟใซๆ–ฐใ—ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’่ฟฝๅŠ ใ™ใ‚‹ใŸใ‚ใฎ็ฐกๅ˜ใงไธ€่ฒซใ—ใŸๆ–นๆณ•ใ€‚ + - Transformerใƒ˜ใƒƒใƒ‰ใ‚’ใƒžใ‚นใ‚ฏใŠใ‚ˆใณใƒ—ใƒซใƒผใƒณใ™ใ‚‹ใŸใ‚ใฎ็ฐกๅ˜ใชๆ–นๆณ•ใ€‚ + +- PyTorchใ€TensorFlow 2.0ใ€ใŠใ‚ˆใณFlaxใฎ้–“ใ‚’็ฐกๅ˜ใซๅˆ‡ใ‚Šๆ›ฟใˆใฆใ€1ใคใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ๅˆฅใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงๆŽจ่ซ–ใ‚’่กŒใ†ใ“ใจใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใ“ใจใ€‚ + +## Main concepts + +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€ๅ„ใƒขใƒ‡ใƒซใซใคใ„ใฆๆฌกใฎ3ใคใฎใ‚ฟใ‚คใƒ—ใฎใ‚ฏใƒฉใ‚นใ‚’ไธญๅฟƒใซๆง‹็ฏ‰ใ•ใ‚Œใฆใ„ใพใ™๏ผš + +- **ใƒขใƒ‡ใƒซใ‚ฏใƒฉใ‚น**ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใงๆไพ›ใ•ใ‚Œใ‚‹ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎ้‡ใฟใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹PyTorchใƒขใƒ‡ใƒซ๏ผˆ[torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)๏ผ‰ใ€Kerasใƒขใƒ‡ใƒซ๏ผˆ[tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model)๏ผ‰ใพใŸใฏJAX/Flaxใƒขใƒ‡ใƒซ๏ผˆ[flax.linen.Module](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html)๏ผ‰ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ +- **ๆง‹ๆˆใ‚ฏใƒฉใ‚น**ใฏใ€ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆ ผ็ดใ—ใพใ™๏ผˆๅฑคใฎๆ•ฐใ‚„้š ใ‚Œๅฑคใฎใ‚ตใ‚คใ‚บใชใฉ๏ผ‰ใ€‚ใ“ใ‚Œใ‚‰ใ‚’่‡ชๅˆ†ใงใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚็‰นใซใ€ๅค‰ๆ›ดใ‚’ๅŠ ใˆใšใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใจ่‡ชๅ‹•็š„ใซๆง‹ๆˆใŒใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผˆใ“ใ‚Œใฏใƒขใƒ‡ใƒซใฎไธ€้ƒจใงใ™๏ผ‰ใ€‚ +- **ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚น**ใฏใ€็”Ÿใƒ‡ใƒผใ‚ฟใ‚’ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](main_classes/tokenizer)ใฏๅ„ใƒขใƒ‡ใƒซใฎ่ชžๅฝ™ใ‚’ไฟๅญ˜ใ—ใ€ๆ–‡ๅญ—ๅˆ—ใ‚’ใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฎใƒชใ‚นใƒˆใซใ‚จใƒณใ‚ณใƒผใƒ‰ใŠใ‚ˆใณใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆไพ›ใ—ใพใ™ใ€‚[ใ‚คใƒกใƒผใ‚ธใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/image_processor)ใฏใƒ“ใ‚ธใƒงใƒณๅ…ฅๅŠ›ใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€[็‰นๅพดๆŠฝๅ‡บๅ™จ](main_classes/feature_extractor)ใฏใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅ…ฅๅŠ›ใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€[ใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/processors)ใฏใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใ™ในใฆใฎใ‚ฏใƒฉใ‚นใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‹ใ‚‰ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒญใƒผใ‚ซใƒซใซไฟๅญ˜ใ—ใ€Hubใงๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใŒใงใใ‚‹3ใคใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™๏ผš + +- `from_pretrained()`ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒช่‡ชไฝ“ใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚Œใ‚‹๏ผˆ[ใƒขใƒ‡ใƒซใƒใƒ–](https://huggingface.co/models)ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใŒใ‚ใ‚Šใพใ™๏ผ‰ใ‹ใ€ใƒฆใƒผใ‚ถใƒผใซใ‚ˆใฃใฆใƒญใƒผใ‚ซใƒซใซไฟๅญ˜ใ•ใ‚ŒใŸ๏ผˆใพใŸใฏใ‚ตใƒผใƒใƒผใซไฟๅญ˜ใ•ใ‚ŒใŸ๏ผ‰ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒใƒผใ‚ธใƒงใƒณใ‹ใ‚‰ใƒขใƒ‡ใƒซใ€ๆง‹ๆˆใ€ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใŸใ‚ใฎใƒกใ‚ฝใƒƒใƒ‰ใงใ™ใ€‚ +- `save_pretrained()`ใฏใ€ใƒขใƒ‡ใƒซใ€ๆง‹ๆˆใ€ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’ใƒญใƒผใ‚ซใƒซใซไฟๅญ˜ใ—ใ€`from_pretrained()`ใ‚’ไฝฟ็”จใ—ใฆๅ†่ชญใฟ่พผใฟใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +- `push_to_hub()`ใฏใ€ใƒขใƒ‡ใƒซใ€ๆง‹ๆˆใ€ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’Hubใซๅ…ฑๆœ‰ใ—ใ€่ชฐใงใ‚‚็ฐกๅ˜ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..5dbda5ce4d4a359b9ae0a1f5be9714feb18b6ea5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_tutorial.md @@ -0,0 +1,295 @@ + + +# Pipelines for inference + +[`pipeline`]ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€[Hub](https://huggingface.co/models)ใ‹ใ‚‰ใฎไปปๆ„ใฎใƒขใƒ‡ใƒซใ‚’่จ€่ชžใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ€้Ÿณๅฃฐใ€ใŠใ‚ˆใณใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใฎๆŽจ่ซ–ใซ็ฐกๅ˜ใซไฝฟ็”จใงใใพใ™ใ€‚ +็‰นๅฎšใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใซ้–ขใ™ใ‚‹็ตŒ้จ“ใŒใชใ„ๅ ดๅˆใ‚„ใ€ใƒขใƒ‡ใƒซใฎ่ƒŒๅพŒใซใ‚ใ‚‹ใ‚ณใƒผใƒ‰ใซ็ฒพ้€šใ—ใฆใ„ใชใ„ๅ ดๅˆใงใ‚‚ใ€[`pipeline`]ใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใงใใพใ™๏ผ +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ๆฌกใฎใ“ใจใ‚’ๅญฆใณใพใ™๏ผš + +- ๆŽจ่ซ–ใฎใŸใ‚ใฎ[`pipeline`]ใฎไฝฟ็”จๆ–นๆณ•ใ€‚ +- ็‰นๅฎšใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚„ใƒขใƒ‡ใƒซใฎไฝฟ็”จๆ–นๆณ•ใ€‚ +- ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ€ใƒ“ใ‚ธใƒงใƒณใ€ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใฎ[`pipeline`]ใฎไฝฟ็”จๆ–นๆณ•ใ€‚ + + + +ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฟใ‚นใ‚ฏใจๅˆฉ็”จๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๅฎŒๅ…จใชไธ€่ฆงใซใคใ„ใฆใฏใ€[`pipeline`]ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +## Pipeline usage + +ๅ„ใ‚ฟใ‚นใ‚ฏใซใฏ้–ข้€ฃใ™ใ‚‹[`pipeline`]ใŒใ‚ใ‚Šใพใ™ใŒใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎ[`pipeline`]ใ‚’ไฝฟ็”จใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ใ™ในใฆใฎใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅซใ‚€ไธ€่ˆฌ็š„ใช[`pipeline`]ใฎๆŠฝ่ฑกๅŒ–ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใ‚ˆใ‚Š็ฐกๅ˜ใงใ™ใ€‚[`pipeline`]ใฏ่‡ชๅ‹•็š„ใซใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒขใƒ‡ใƒซใจใ€ใ‚ฟใ‚นใ‚ฏใฎๆŽจ่ซ–ใŒๅฏ่ƒฝใชๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +1. [`pipeline`]ใ‚’ไฝœๆˆใ—ใ€ๆŽจ่ซ–ใ‚ฟใ‚นใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆๅง‹ใ‚ใพใ™๏ผš + +```py +>>> from transformers import pipeline + +>>> generator = pipeline(task="automatic-speech-recognition") +``` + +2. [`pipeline`]ใซๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆธกใ—ใพใ™๏ผš + +```python +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'} +``` + +ใƒใ‚งใƒƒใ‚ฏใ‚ขใ‚ฆใƒˆใงใใชใ‹ใฃใŸใ‹๏ผŸ [Hubใฎๆœ€ใ‚‚ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚ŒใŸ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใƒขใƒ‡ใƒซ](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=downloads) ใฎใ„ใใคใ‹ใ‚’่ฆ‹ใฆใ€ใ‚ˆใ‚Š่‰ฏใ„่ปขๅ†™ใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ +[openai/whisper-large](https://huggingface.co/openai/whisper-large) ใ‚’่ฉฆใ—ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> generator = pipeline(model="openai/whisper-large") +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +ใ“ใฎ็ตๆžœใฏใ‚ˆใ‚Šๆญฃ็ขบใซ่ฆ‹ใˆใพใ™ใญ๏ผ +็•ฐใชใ‚‹่จ€่ชžใ€ๅฐ‚้–€ๅˆ†้‡Žใซ็‰นๅŒ–ใ—ใŸใƒขใƒ‡ใƒซใ€ใใฎไป–ใฎใƒขใƒ‡ใƒซใซใคใ„ใฆใฏใ€Hubใ‚’ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹ใ“ใจใ‚’ๅผทใใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +Hubใงใฏใ€ใƒ–ใƒฉใ‚ฆใ‚ถใ‹ใ‚‰็›ดๆŽฅใƒขใƒ‡ใƒซใฎ็ตๆžœใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใ€ไป–ใฎใƒขใƒ‡ใƒซใ‚ˆใ‚Šใ‚‚้ฉใ—ใฆใ„ใ‚‹ใ‹ใ€็‰นๆฎŠใชใ‚ฑใƒผใ‚นใ‚’ใ‚ˆใ‚Šใ‚ˆใๅ‡ฆ็†ใงใใ‚‹ใ‹ใ‚’็ขบ่ชใงใใพใ™ใ€‚ +ใใ—ใฆใ€ใ‚ใชใŸใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉใ—ใŸใƒขใƒ‡ใƒซใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ๅ ดๅˆใ€ใ„ใคใงใ‚‚[ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ](training)ใ‚’้–‹ๅง‹ใงใใพใ™๏ผ + +่ค‡ๆ•ฐใฎๅ…ฅๅŠ›ใŒใ‚ใ‚‹ๅ ดๅˆใ€ๅ…ฅๅŠ›ใ‚’ใƒชใ‚นใƒˆใจใ—ใฆๆธกใ™ใ“ใจใŒใงใใพใ™๏ผš + +```py +generator( + [ + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", + ] +) +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’็นฐใ‚Š่ฟ”ใ—ๅ‡ฆ็†ใ—ใŸใ‚Šใ€ใ‚ฆใ‚งใƒ–ใ‚ตใƒผใƒใƒผใงๆŽจ่ซ–ใซไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ๅฐ‚็”จใฎ้ƒจๅˆ†ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ + +[ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ™ใ‚‹](#using-pipeline-in-a-dataset) + +[ใ‚ฆใ‚งใƒ–ใ‚ตใƒผใƒใƒผใงใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ™ใ‚‹](./pipeline_webserver) + + + +## ใƒ‘ใƒฉใƒกใƒผใ‚ฟ + +[`pipeline`]ใฏๅคšใใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใŠใ‚Šใ€ไธ€้ƒจใฏใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใงใ‚ใ‚Šใ€ไธ€้ƒจใฏใ™ในใฆใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๅ…ฑ้€šใงใ™ใ€‚ +ไธ€่ˆฌ็š„ใซใฏใ€ใฉใ“ใงใ‚‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒ‡ๅฎšใงใใพใ™๏ผš + +```py +generator = pipeline(model="openai/whisper-large", my_parameter=1) +out = generator(...) # ใ“ใ‚Œใฏ `my_parameter=1` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +out = generator(..., my_parameter=2) # ใ“ใ‚ŒใฏไธŠๆ›ธใใ—ใฆ `my_parameter=2` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +out = generator(...) # ใ“ใ‚Œใฏๅ†ใณ `my_parameter=1` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +``` + +3ใคใฎ้‡่ฆใชใ‚‚ใฎใ‚’็ขบ่ชใ—ใพใ—ใ‚‡ใ†๏ผš + +### Device + +`device=n` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใƒขใƒ‡ใƒซใ‚’ๆŒ‡ๅฎšใ—ใŸใƒ‡ใƒใ‚คใ‚นใซ่‡ชๅ‹•็š„ใซ้…็ฝฎใ—ใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€PyTorchใพใŸใฏTensorflowใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใซ้–ขไฟ‚ใชใๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +```py +generator = pipeline(model="openai/whisper-large", device=0) +``` + +ใ‚‚ใ—ใƒขใƒ‡ใƒซใŒๅ˜ไธ€ใฎGPUใซใฏๅคงใใ™ใŽใ‚‹ๅ ดๅˆใ€`device_map="auto"`ใ‚’่จญๅฎšใ—ใฆใ€๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate) ใซใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ใฉใฎใ‚ˆใ†ใซใƒญใƒผใƒ‰ใ—ใ€ไฟๅญ˜ใ™ใ‚‹ใ‹ใ‚’่‡ชๅ‹•็š„ใซๆฑบๅฎšใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +#!pip install accelerate +generator = pipeline(model="openai/whisper-large", device_map="auto") +``` + +ๆณจๆ„: `device_map="auto"` ใŒๆธกใ•ใ‚ŒใŸๅ ดๅˆใ€`pipeline` ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹้š›ใซ `device=device` ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใใ†ใ—ใชใ„ใจใ€ไบˆๆœŸใ—ใชใ„ๅ‹•ไฝœใซ้ญ้‡ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผ + +### Batch size + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ่ฉณ็ดฐใซใคใ„ใฆ[ใ“ใกใ‚‰](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching)ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹็†็”ฑใ‹ใ‚‰ใ€ๆŽจ่ซ–ใ‚’ใƒใƒƒใƒๅ‡ฆ็†ใ—ใพใ›ใ‚“ใ€‚ใใฎ็†็”ฑใฏใ€ใƒใƒƒใƒๅ‡ฆ็†ใŒๅฟ…ใšใ—ใ‚‚้€Ÿใใชใ„ใŸใ‚ใงใ‚ใ‚Šใ€ๅฎŸ้š›ใซใฏใ„ใใคใ‹ใฎใ‚ฑใƒผใ‚นใงใ‹ใชใ‚Š้…ใใชใ‚‹ใ“ใจใŒใ‚ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ + +ใŸใ ใ—ใ€ใ‚ใชใŸใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใงๆฉŸ่ƒฝใ™ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซไฝฟ็”จใงใใพใ™๏ผš + +```py +generator = pipeline(model="openai/whisper-large", device=0, batch_size=2) +audio_filenames = [f"audio_{i}.flac" for i in range(10)] +texts = generator(audio_filenames) +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏๆไพ›ใ•ใ‚ŒใŸ10ๅ€‹ใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใงใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅฎŸ่กŒใ—ใพใ™ใŒใ€ +ใƒขใƒ‡ใƒซใซใฏใƒใƒƒใƒๅ‡ฆ็†ใŒใ‚ˆใ‚ŠๅŠนๆžœ็š„ใงใ‚ใ‚‹GPUไธŠใซใ‚ใ‚Šใ€ใƒใƒƒใƒๅ‡ฆ็†ใ‚’่กŒใ†ใŸใ‚ใฎ่ฟฝๅŠ ใฎใ‚ณใƒผใƒ‰ใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ๅ‡บๅŠ›ใฏๅธธใซใƒใƒƒใƒๅ‡ฆ็†ใชใ—ใงๅ—ใ‘ๅ–ใฃใŸใ‚‚ใฎใจไธ€่‡ดใ™ใ‚‹ใฏใšใงใ™ใ€‚ใ“ใ‚Œใฏๅ˜ใซใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‹ใ‚‰ใ‚ˆใ‚Š้ซ˜้€Ÿใชๅ‡ฆ็†ใ‚’ๅพ—ใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใจใ—ใฆๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€ใƒใƒƒใƒๅ‡ฆ็†ใฎใ„ใใคใ‹ใฎ่ค‡้›‘ใ•ใ‚’่ปฝๆธ›ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใชใœใชใ‚‰ใ€ไธ€้ƒจใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใฏใ€ +ใƒขใƒ‡ใƒซใงๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ1ใคใฎใ‚ขใ‚คใƒ†ใƒ ๏ผˆ้•ทใ„ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใฎใ‚ˆใ†ใชใ‚‚ใฎ๏ผ‰ใ‚’่ค‡ๆ•ฐใฎ้ƒจๅˆ†ใซๅˆ†ๅ‰ฒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใŒใ‚ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ“ใ‚Œใ‚’ใ‚ใชใŸใฎใŸใ‚ใซๅฎŸ่กŒใ—ใพใ™ใ€‚[*ใƒใƒฃใƒณใ‚ฏใƒใƒƒใƒๅ‡ฆ็†*](./main_classes/pipelines#pipeline-chunk-batching)ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใ‚‹ใ‚‚ใฎใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +### Task specific parameters + +ใ™ในใฆใฎใ‚ฟใ‚นใ‚ฏใฏใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆไพ›ใ—ใ€่ฟฝๅŠ ใฎๆŸ”่ปŸๆ€งใจใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๆไพ›ใ—ใฆใ€ไฝœๆฅญใ‚’ใ‚นใƒ ใƒผใ‚บใซ้€ฒใ‚ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ +ใŸใจใˆใฐใ€[`transformers.AutomaticSpeechRecognitionPipeline.__call__`]ใƒกใ‚ฝใƒƒใƒ‰ใซใฏใ€ใƒ“ใƒ‡ใ‚ชใฎๅญ—ๅน•ไฝœๆˆใซๆœ‰็”จใช`return_timestamps`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> # Not using whisper, as it cannot provide timestamps. +>>> generator = pipeline(model="facebook/wav2vec2-large-960h-lv60-self", return_timestamps="word") +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP AND LIVE OUT THE TRUE MEANING OF ITS CREED', 'chunks': [{'text': 'I', 'timestamp': (1.22, 1.24)}, {'text': 'HAVE', 'timestamp': (1.42, 1.58)}, {'text': 'A', 'timestamp': (1.66, 1.68)}, {'text': 'DREAM', 'timestamp': (1.76, 2.14)}, {'text': 'BUT', 'timestamp': (3.68, 3.8)}, {'text': 'ONE', 'timestamp': (3.94, 4.06)}, {'text': 'DAY', 'timestamp': (4.16, 4.3)}, {'text': 'THIS', 'timestamp': (6.36, 6.54)}, {'text': 'NATION', 'timestamp': (6.68, 7.1)}, {'text': 'WILL', 'timestamp': (7.32, 7.56)}, {'text': 'RISE', 'timestamp': (7.8, 8.26)}, {'text': 'UP', 'timestamp': (8.38, 8.48)}, {'text': 'AND', 'timestamp': (10.08, 10.18)}, {'text': 'LIVE', 'timestamp': (10.26, 10.48)}, {'text': 'OUT', 'timestamp': (10.58, 10.7)}, {'text': 'THE', 'timestamp': (10.82, 10.9)}, {'text': 'TRUE', 'timestamp': (10.98, 11.18)}, {'text': 'MEANING', 'timestamp': (11.26, 11.58)}, {'text': 'OF', 'timestamp': (11.66, 11.7)}, {'text': 'ITS', 'timestamp': (11.76, 11.88)}, {'text': 'CREED', 'timestamp': (12.0, 12.38)}]} +``` + +ใƒขใƒ‡ใƒซใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆŽจๆธฌใ—ใ€ๆ–‡ใฎไธญใงๅ„ๅ˜่ชžใŒใ„ใค็™บ้Ÿณใ•ใ‚ŒใŸใ‹ใ‚’ๅ‡บๅŠ›ใ—ใพใ—ใŸใ€‚ + +ๅ„ใ‚ฟใ‚นใ‚ฏใ”ใจใซๅˆฉ็”จๅฏ่ƒฝใชๅคšใใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใพใ™ใฎใงใ€ไฝ•ใ‚’่ชฟๆ•ดใงใใ‚‹ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซๅ„ใ‚ฟใ‚นใ‚ฏใฎAPIใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผ +ใŸใจใˆใฐใ€[`~transformers.AutomaticSpeechRecognitionPipeline`]ใซใฏใ€ใƒขใƒ‡ใƒซๅ˜ไฝ“ใงใฏๅ‡ฆ็†ใงใใชใ„้žๅธธใซ้•ทใ„ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซ๏ผˆใŸใจใˆใฐใ€ๆ˜ ็”ปๅ…จไฝ“ใ‚„1ๆ™‚้–“ใฎใƒ“ใƒ‡ใ‚ชใฎๅญ—ๅน•ไป˜ใ‘ใชใฉ๏ผ‰ใงๅฝน็ซ‹ใค`chunk_length_s`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ๅฝน็ซ‹ใคใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ๅ ดๅˆใฏใ€[ใƒชใ‚ฏใ‚จใ‚นใƒˆ](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml)ใ—ใฆใใ ใ•ใ„๏ผ + +## Using pipeline in a dataset + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆไธŠใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใ‚’่กŒใ†ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใ‚คใƒ†ใƒฌใƒผใ‚ฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™๏ผš + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + + +pipe = pipeline(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipe(data()): + generated_characters += len(out[0]["generated_text"]) +``` + +ใ‚คใƒ†ใƒฌใƒผใ‚ฟใƒผ `data()` ใฏๅ„็ตๆžœใ‚’็”Ÿๆˆใ—ใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ่‡ชๅ‹•็š„ใซๅ…ฅๅŠ›ใŒๅๅพฉๅฏ่ƒฝใงใ‚ใ‚‹ใ“ใจใ‚’่ช่ญ˜ใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’ๅ–ๅพ—ใ—็ถšใ‘ใชใŒใ‚‰GPUไธŠใงๅ‡ฆ็†ใ‚’่กŒใ„ใพใ™๏ผˆใ“ใ‚Œใฏ[DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader)ใ‚’ๅ†…้ƒจใงไฝฟ็”จใ—ใฆใ„ใพใ™๏ผ‰ใ€‚ +ใ“ใ‚Œใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซใƒกใƒขใƒชใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ๅฟ…่ฆใŒใชใใ€GPUใซใงใใ‚‹ใ ใ‘้€Ÿใใƒ‡ใƒผใ‚ฟใ‚’ไพ›็ตฆใงใใ‚‹ใŸใ‚้‡่ฆใงใ™ใ€‚ + +ใƒใƒƒใƒๅ‡ฆ็†ใฏๅ‡ฆ็†ใ‚’้ซ˜้€ŸๅŒ–ใงใใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ใ“ใ“ใง`batch_size`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ชฟๆ•ดใ—ใฆ่ฉฆใ™ใ“ใจใŒๅฝน็ซ‹ใคใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅๅพฉๅ‡ฆ็†ใ™ใ‚‹ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€๐Ÿค— [Datasets](https://github.com/huggingface/datasets/)ใ‹ใ‚‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใ‚€ใ“ใจใงใ™๏ผš + +```py +# KeyDataset is a util that will just output the item we're interested in. +from transformers.pipelines.pt_utils import KeyDataset +from datasets import load_dataset + +pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) +dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") + +for out in pipe(KeyDataset(dataset, "audio")): + print(out) +``` + +## Using pipelines for a webserver + + +ๆŽจ่ซ–ใ‚จใƒณใ‚ธใƒณใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใฏ่ค‡้›‘ใชใƒˆใƒ”ใƒƒใ‚ฏใงใ€็‹ฌ่‡ชใฎใƒšใƒผใ‚ธใŒๅฟ…่ฆใงใ™ใ€‚ + + +[ใƒชใƒณใ‚ฏ](./pipeline_webserver) + +## Vision pipeline + +ใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏ็”จใฎ[`pipeline`]ใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใฏใปใผๅŒใ˜ใงใ™ใ€‚ + +ใ‚ฟใ‚นใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใ€็”ปๅƒใ‚’ใ‚ฏใƒฉใ‚ทใƒ•ใ‚กใ‚คใ‚ขใซๆธกใ—ใพใ™ใ€‚็”ปๅƒใฏใƒชใƒณใ‚ฏใ€ใƒญใƒผใ‚ซใƒซใƒ‘ใ‚นใ€ใพใŸใฏBase64ใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็”ปๅƒใงใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€ไปฅไธ‹ใฎ็”ปๅƒใฏใฉใฎ็จฎ้กžใฎ็Œซใงใ™ใ‹๏ผŸ + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(model="google/vit-base-patch16-224") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` + +## Text pipeline + +[`pipeline`]ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใฏใ€NLPใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใปใผๅŒใ˜ใงใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> # This model is a `zero-shot-classification` model. +>>> # It will classify text, except you are free to choose any label you might imagine +>>> classifier = pipeline(model="facebook/bart-large-mnli") +>>> classifier( +... "I have a problem with my iphone that needs to be resolved asap!!", +... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], +... ) +{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]} +``` + +## Multimodal pipeline + +[`pipeline`]ใฏใ€1ใคไปฅไธŠใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ใŸใจใˆใฐใ€่ฆ–่ฆš็š„ใช่ณชๅ•ๅฟœ็ญ”๏ผˆVQA๏ผ‰ใ‚ฟใ‚นใ‚ฏใฏใƒ†ใ‚ญใ‚นใƒˆใจ็”ปๅƒใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ„ใพใ™ใ€‚ +ๅฅฝใใช็”ปๅƒใƒชใƒณใ‚ฏใจ็”ปๅƒใซ้–ขใ™ใ‚‹่ณชๅ•ใ‚’่‡ช็”ฑใซไฝฟใฃใฆใใ ใ•ใ„ใ€‚็”ปๅƒใฏURLใพใŸใฏ็”ปๅƒใฎใƒญใƒผใ‚ซใƒซใƒ‘ใ‚นใงๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +ไพ‹ใˆใฐใ€ใ“ใฎ[่ซ‹ๆฑ‚ๆ›ธ็”ปๅƒ](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png)ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ๏ผš + +```py +>>> from transformers import pipeline + +>>> vqa = pipeline(model="impira/layoutlm-document-qa") +>>> output = vqa( +... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", +... question="What is the invoice number?", +... ) +>>> output[0]["score"] = round(output[0]["score"], 3) +>>> output +[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}] +``` + + + +ไธŠ่จ˜ใฎไพ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€๐Ÿค— TransformersใซๅŠ ใˆใฆ [`pytesseract`](https://pypi.org/project/pytesseract/) ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```bash +sudo apt install -y tesseract-ocr +pip install pytesseract +``` + + + +## Using `pipeline` on large models with ๐Ÿค— `accelerate`: + +ใพใšใ€`accelerate` ใ‚’`pip install accelerate` ใงใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +ๆฌกใซใ€`device_map="auto"` ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ“ใฎไพ‹ใงใฏ `facebook/opt-1.3b` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```python +# pip install accelerate +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto") +output = pipe("ใ“ใ‚Œใฏ็ด ๆ™ดใ‚‰ใ—ใ„ไพ‹ใงใ™๏ผ", do_sample=True, top_p=0.95) +``` + +ใ‚‚ใ— `bitsandbytes` ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใ€`load_in_8bit=True` ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚Œใฐใ€8ใƒ“ใƒƒใƒˆใง่ชญใฟ่พผใพใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆธกใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +# pip install accelerate bitsandbytes +import torch +from transformers import pipeline + +pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}) +output = pipe("This is a cool example!", do_sample=True, top_p=0.95) +``` + +ๆณจๆ„: BLOOMใชใฉใฎๅคง่ฆๆจกใƒขใƒ‡ใƒซใฎใƒญใƒผใƒ‰ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹Hugging Faceใƒขใƒ‡ใƒซใฎใ„ใšใ‚Œใ‹ใงใ€ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ฝฎใๆ›ใˆใ‚‹ใ“ใจใŒใงใใพใ™๏ผ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_webserver.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_webserver.md new file mode 100644 index 0000000000000000000000000000000000000000..3b35a01490d40912a5256cb42fb3f5ba2f789ab5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pipeline_webserver.md @@ -0,0 +1,132 @@ + + +# Webใ‚ตใƒผใƒใƒผ็”จใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎไฝฟ็”จ + + +ๆŽจ่ซ–ใ‚จใƒณใ‚ธใƒณใฎไฝœๆˆใฏ่ค‡้›‘ใชใƒˆใƒ”ใƒƒใ‚ฏใงใ‚ใ‚Šใ€"ๆœ€้ฉใช"ใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใฏใŠใใ‚‰ใๅ•้กŒใฎ้ ˜ๅŸŸใซไพๅญ˜ใ™ใ‚‹ใงใ—ใ‚‡ใ†ใ€‚CPUใพใŸใฏGPUใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ‹๏ผŸๆœ€ไฝŽใฎใƒฌใ‚คใƒ†ใƒณใ‚ทใ€ๆœ€้ซ˜ใฎใ‚นใƒซใƒผใƒ—ใƒƒใƒˆใ€ๅคšใใฎใƒขใƒ‡ใƒซใฎใ‚ตใƒใƒผใƒˆใ€ใพใŸใฏ็‰นๅฎšใฎใƒขใƒ‡ใƒซใฎ้ซ˜ๅบฆใชๆœ€้ฉๅŒ–ใ‚’ๆœ›ใ‚“ใงใ„ใพใ™ใ‹๏ผŸ +ใ“ใฎใƒˆใƒ”ใƒƒใ‚ฏใซๅ–ใ‚Š็ต„ใ‚€ใŸใ‚ใฎๅคšใใฎๆ–นๆณ•ใŒใ‚ใ‚Šใ€็งใŸใกใŒ็ดนไป‹ใ™ใ‚‹ใฎใฏใ€ใŠใใ‚‰ใๆœ€้ฉใชใ‚ฝใƒชใƒฅใƒผใ‚ทใƒงใƒณใงใฏใชใ„ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใŒใ€ๅง‹ใ‚ใ‚‹ใŸใ‚ใฎ่‰ฏใ„ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใ™ใ€‚ + + +้‡่ฆใชใ“ใจใฏใ€Webใ‚ตใƒผใƒใƒผใฏใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’ๅพ…ๆฉŸใ—ใ€ๅ—ไฟกใ—ใŸใ‚ˆใ†ใซๆ‰ฑใ†ใ‚ทใ‚นใƒ†ใƒ ใงใ‚ใ‚‹ใŸใ‚ใ€[ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](pipeline_tutorial#using-pipelines-on-a-dataset)ใฎใ‚ˆใ†ใซใ€ใ‚คใƒ†ใƒฌใƒผใ‚ฟใ‚’ไฝฟ็”จใงใใ‚‹ใ“ใจใงใ™ใ€‚ + +้€šๅธธใ€Webใ‚ตใƒผใƒใƒผใฏไธฆๅˆ—ๅ‡ฆ็†๏ผˆใƒžใƒซใƒใ‚นใƒฌใƒƒใƒ‰ใ€้žๅŒๆœŸใชใฉ๏ผ‰ใ•ใ‚Œใฆใ€ใ•ใพใ–ใพใชใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’ๅŒๆ™‚ใซๅ‡ฆ็†ใ—ใพใ™ใ€‚ไธ€ๆ–นใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ๏ผˆใŠใ‚ˆใณไธปใซใใฎๅŸบ็คŽใจใชใ‚‹ใƒขใƒ‡ใƒซ๏ผ‰ใฏไธฆๅˆ—ๅ‡ฆ็†ใซใฏใ‚ใพใ‚Š้ฉใ—ใฆใ„ใพใ›ใ‚“ใ€‚ใใ‚Œใ‚‰ใฏๅคšใใฎRAMใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใ€ๅฎŸ่กŒไธญใซๅˆฉ็”จๅฏ่ƒฝใชใƒชใ‚ฝใƒผใ‚นใ‚’ใ™ในใฆๆไพ›ใ™ใ‚‹ใ‹ใ€่จˆ็ฎ—้›†็ด„ๅž‹ใฎใ‚ธใƒงใƒ–ใงใ‚ใ‚‹ๅ ดๅˆใซๆœ€้ฉใงใ™ใ€‚ + +Webใ‚ตใƒผใƒใƒผใฏๅ—ไฟกใจ้€ไฟกใฎ่ปฝใ„่ฒ ่ทใ‚’ๅ‡ฆ็†ใ—ใ€ๅฎŸ้š›ใฎไฝœๆฅญใ‚’1ใคใฎใ‚นใƒฌใƒƒใƒ‰ใงๅ‡ฆ็†ใ™ใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใ“ใฎไพ‹ใงใฏ`starlette`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅฎŸ้š›ใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฏใ‚ใพใ‚Š้‡่ฆใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅˆฅใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๅŒใ˜ๅŠนๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใ‚ณใƒผใƒ‰ใ‚’่ชฟๆ•ดใพใŸใฏๅค‰ๆ›ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ + +`server.py`ใ‚’ไฝœๆˆใ—ใฆใใ ใ•ใ„๏ผš + + +```py +from starlette.applications import Starlette +from starlette.responses import JSONResponse +from starlette.routing import Route +from transformers import pipeline +import asyncio + + +async def homepage(request): + payload = await request.body() + string = payload.decode("utf-8") + response_q = asyncio.Queue() + await request.app.model_queue.put((string, response_q)) + output = await response_q.get() + return JSONResponse(output) + + +async def server_loop(q): + pipe = pipeline(model="google-bert/bert-base-uncased") + while True: + (string, response_q) = await q.get() + out = pipe(string) + await response_q.put(out) + + +app = Starlette( + routes=[ + Route("/", homepage, methods=["POST"]), + ], +) + + +@app.on_event("startup") +async def startup_event(): + q = asyncio.Queue() + app.model_queue = q + asyncio.create_task(server_loop(q)) +``` + +ใ“ใ“ใ‹ใ‚‰ๅง‹ใ‚ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš +```bash +uvicorn server:app +``` + +ใใ—ใฆใ€ๆฌกใฎใ‚ˆใ†ใซใ‚ฏใ‚จใƒชใงใใพใ™๏ผš +```bash +curl -X POST -d "test [MASK]" http://localhost:8000/ +#[{"score":0.7742936015129089,"token":1012,"token_str":".","sequence":"test."},...] +``` + + + +ใใ—ใฆใ€ใ“ใ‚Œใงใ‚ฆใ‚งใƒ–ใ‚ตใƒผใƒใƒผใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•ใฎ่‰ฏใ„ใ‚ขใ‚คใƒ‡ใ‚ขใ‚’ๆŒใฃใฆใ„ใพใ™๏ผ + +ๆœฌๅฝ“ใซ้‡่ฆใชใฎใฏใ€ใƒขใƒ‡ใƒซใ‚’**ไธ€ๅบฆใ ใ‘**ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚ฆใ‚งใƒ–ใ‚ตใƒผใƒใƒผไธŠใซใƒขใƒ‡ใƒซใฎใ‚ณใƒ”ใƒผใŒใชใ„ใŸใ‚ใ€ไธๅฟ…่ฆใชRAMใŒไฝฟ็”จใ•ใ‚Œใชใใชใ‚Šใพใ™ใ€‚ +ใใฎๅพŒใ€ใ‚ญใƒฅใƒผใ‚คใƒณใ‚ฐใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ‹•็š„ใƒใƒƒใƒๅ‡ฆ็†ใ‚’่กŒใ†ใชใฉใ€ใ„ใใคใ‹ใฎใ‚ขใ‚คใƒ†ใƒ ใ‚’่“„็ฉใ—ใฆใ‹ใ‚‰ๆŽจ่ซ–ใ‚’่กŒใ†ใชใฉใ€้ซ˜ๅบฆใชๅ‡ฆ็†ใ‚’่กŒใ†ใ“ใจใŒใงใใพใ™๏ผš + + + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใ‚ตใƒณใƒ—ใƒซใฏใ€ๅฏ่ชญๆ€งใฎใŸใ‚ใซๆ“ฌไผผใ‚ณใƒผใƒ‰ใฎใ‚ˆใ†ใซๆ›ธใ‹ใ‚Œใฆใ„ใพใ™ใ€‚ใ‚ทใ‚นใƒ†ใƒ ใƒชใ‚ฝใƒผใ‚นใซๅˆ็†็š„ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ›ใšใซๅฎŸ่กŒใ—ใชใ„ใงใใ ใ•ใ„๏ผ + + + + +```py +(string, rq) = await q.get() +strings = [] +queues = [] +while True: + try: + (string, rq) = await asyncio.wait_for(q.get(), timeout=0.001) # 1ms + except asyncio.exceptions.TimeoutError: + break + strings.append(string) + queues.append(rq) +strings +outs = pipe(strings, batch_size=len(strings)) +for rq, out in zip(queues, outs): + await rq.put(out) +``` + +ใพใš็ฌฌไธ€ใซใ€้€šๅธธใฏใ‚ใพใ‚Š่‰ฏใ„ใ‚ขใ‚คใƒ‡ใ‚ขใงใฏใชใ„ใƒใƒƒใƒใ‚ตใ‚คใ‚บใฎๅˆถ้™ใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆฌกใซใ€ใ‚ฟใ‚คใƒ ใ‚ขใ‚ฆใƒˆใฏใ‚ญใƒฅใƒผใฎๅ–ๅพ—ใ”ใจใซใƒชใ‚ปใƒƒใƒˆใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅ‰ใซ1msไปฅไธŠๅพ…ใคๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผˆๆœ€ๅˆใฎใƒชใ‚ฏใ‚จใ‚นใƒˆใฎ้…ๅปถใซ1msๅˆ†้…ใ‚ŒใŒ็”Ÿใ˜ใพใ™๏ผ‰ใ€‚ + +1msใฎ็ท ใ‚ๅˆ‡ใ‚Šใ‚’1ๅ›žใ ใ‘ๆŒใคใฎใŒ่‰ฏใ„ใงใ—ใ‚‡ใ†ใ€‚ + +ใ“ใ‚Œใฏใ€ใ‚ญใƒฅใƒผใซไฝ•ใ‚‚ใชใ„ๅ ดๅˆใงใ‚‚ๅธธใซ1msๅพ…ๆฉŸใ—ใพใ™ใŒใ€ใ‚ญใƒฅใƒผใซไฝ•ใ‚‚ใชใ„ๅ ดๅˆใซๆŽจ่ซ–ใ‚’้–‹ๅง‹ใ—ใŸใ„ๅ ดๅˆใฏ้ฉใ—ใฆใ„ใชใ„ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€ใƒใƒƒใƒๅ‡ฆ็†ใŒๆœฌๅฝ“ใซ้‡่ฆใชๅ ดๅˆใซใฏๆ„ๅ‘ณใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ๅ†ๅบฆใ€1ใคใฎๆœ€้ฉใช่งฃๆฑบ็ญ–ใฏๅญ˜ๅœจใ—ใพใ›ใ‚“ใ€‚ + +## Few things you might want to consider + +### Error checking + +ๆœฌ็•ช็’ฐๅขƒใงใฏๅคšใใฎๅ•้กŒใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผšใƒกใƒขใƒชไธ่ถณใ€ใ‚นใƒšใƒผใ‚นไธ่ถณใ€ใƒขใƒ‡ใƒซใฎ่ชญใฟ่พผใฟใŒๅคฑๆ•—ใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€ใ‚ฏใ‚จใƒชใŒ่ชคใฃใฆใ„ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€ใ‚ฏใ‚จใƒชใŒๆญฃใ—ใ„ๅ ดๅˆใงใ‚‚ใƒขใƒ‡ใƒซใฎๆง‹ๆˆใ‚จใƒฉใƒผใฎใŸใ‚ใซๅฎŸ่กŒใซๅคฑๆ•—ใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใชใฉใ€‚ + +ไธ€่ˆฌ็š„ใซใฏใ€ใ‚ตใƒผใƒใƒผใŒใ‚จใƒฉใƒผใ‚’ใƒฆใƒผใ‚ถใƒผใซๅ‡บๅŠ›ใ™ใ‚‹ใจ่‰ฏใ„ใŸใ‚ใ€ใ“ใ‚Œใ‚‰ใฎใ‚จใƒฉใƒผใ‚’่กจ็คบใ™ใ‚‹ใŸใ‚ใฎๅคšใใฎ`try..except`ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใฏ่‰ฏใ„ใ‚ขใ‚คใƒ‡ใ‚ขใงใ™ใ€‚ใŸใ ใ—ใ€ใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใซๅฟœใ˜ใฆใ“ใ‚Œใ‚‰ใฎใ‚จใƒฉใƒผใ‚’ใ™ในใฆ่กจ็คบใ™ใ‚‹ใ“ใจใฏใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใƒชใ‚นใ‚ฏใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +### Circuit breaking + +Webใ‚ตใƒผใƒใƒผใฏ้€šๅธธใ€้Ž่ฒ ่ทๆ™‚ใซๆญฃใ—ใ„ใ‚จใƒฉใƒผใ‚’่ฟ”ใ™ๆ–นใŒ่‰ฏใ„ใงใ™ใ€‚ใ‚ฏใ‚จใƒชใ‚’็„กๆœŸ้™ใซๅพ…ใคไปฃใ‚ใ‚Šใซ้ฉๅˆ‡ใชใ‚จใƒฉใƒผใ‚’่ฟ”ใ—ใพใ™ใ€‚้•ทๆ™‚้–“ๅพ…ใคไปฃใ‚ใ‚Šใซ503ใ‚จใƒฉใƒผใ‚’่ฟ”ใ™ใ‹ใ€้•ทๆ™‚้–“ๅพ…ใฃใฆใ‹ใ‚‰504ใ‚จใƒฉใƒผใ‚’่ฟ”ใ™ใ‹ใงใ™ใ€‚ + +ๆๆกˆใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใงใฏๅ˜ไธ€ใฎใ‚ญใƒฅใƒผใŒใ‚ใ‚‹ใŸใ‚ใ€ใ‚ญใƒฅใƒผใ‚ตใ‚คใ‚บใ‚’่ฆ‹ใ‚‹ใ“ใจใฏใ€Webใ‚ตใƒผใƒใƒผใŒ่ฒ ่ทใซ่€ใˆใ‚‹ๅ‰ใซใ‚จใƒฉใƒผใ‚’่ฟ”ใ™ใŸใ‚ใฎๅŸบๆœฌ็š„ใชๆ–นๆณ•ใงใ™ใ€‚ + +### Blocking the main thread + +็พๅœจใ€PyTorchใฏ้žๅŒๆœŸใ‚’่ช่ญ˜ใ—ใฆใ„ใชใ„ใŸใ‚ใ€่จˆ็ฎ—ใฏใƒกใ‚คใƒณใ‚นใƒฌใƒƒใƒ‰ใ‚’ใƒ–ใƒญใƒƒใ‚ฏใ—ใพใ™ใ€‚ใคใพใ‚Šใ€PyTorchใŒ็‹ฌ่‡ชใฎใ‚นใƒฌใƒƒใƒ‰/ใƒ—ใƒญใ‚ปใ‚นใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใจ่‰ฏใ„ใงใ—ใ‚‡ใ†ใ€‚ๆๆกˆใ•ใ‚ŒใŸใ‚ณใƒผใƒ‰ใฏใ€ใ‚นใƒฌใƒƒใƒ‰ใจ้žๅŒๆœŸใจใ‚ญใƒฅใƒผใŒใ†ใพใ้€ฃๆบใ—ใชใ„ใŸใ‚ใ€ใ“ใ‚Œใฏ่กŒใ‚ใ‚Œใฆใ„ใพใ›ใ‚“ใŒใ€ๆœ€็ต‚็š„ใซใฏๅŒใ˜ใ“ใจใ‚’่กŒใ„ใพใ™ใ€‚ + +ใ“ใ‚Œใฏใ€ๅ˜ไธ€ใฎใ‚ขใ‚คใƒ†ใƒ ใฎๆŽจ่ซ–ใŒ้•ทใ„ๅ ดๅˆ๏ผˆ>1็ง’๏ผ‰ใซ้‡่ฆใงใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๆŽจ่ซ–ไธญใซใ™ในใฆใฎใ‚ฏใ‚จใƒชใŒ1็ง’ๅพ…ใŸใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + +### Dynamic batching + +ไธ€่ˆฌ็š„ใซใ€ใƒใƒƒใƒๅ‡ฆ็†ใฏ1ๅ›žใฎใ‚ขใ‚คใƒ†ใƒ ใ‚’1ๅ›žๆธกใ™ใ‚ˆใ‚Šใ‚‚ๆ”นๅ–„ใ•ใ‚Œใ‚‹ใ“ใจใฏๅฟ…ใšใ—ใ‚‚ใ‚ใ‚Šใพใ›ใ‚“๏ผˆ่ฉณ็ดฐใฏ[ใƒใƒƒใƒๅ‡ฆ็†ใฎ่ฉณ็ดฐ](./main_classes/pipelines#pipeline-batching)ใ‚’ๅ‚็…ง๏ผ‰ใ€‚ใ—ใ‹ใ—ใ€ๆญฃใ—ใ„่จญๅฎšใงไฝฟ็”จใ™ใ‚‹ใจ้žๅธธใซๅŠนๆžœ็š„ใงใ™ใ€‚APIใงใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงๅ‹•็š„ใƒใƒƒใƒๅ‡ฆ็†ใฏ่กŒใ‚ใ‚Œใพใ›ใ‚“๏ผˆ้…ๅปถใฎๆฉŸไผšใŒๅคšใ™ใŽใพใ™๏ผ‰ใ€‚ใ—ใ‹ใ—ใ€้žๅธธใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใ‚ใ‚‹BLOOMๆŽจ่ซ–ใฎๅ ดๅˆใ€ๅ‹•็š„ใƒใƒƒใƒๅ‡ฆ็†ใฏ**้‡่ฆ**ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ™ในใฆใฎใƒฆใƒผใ‚ถใƒผใซใจใฃใฆใพใจใ‚‚ใชใ‚จใ‚ฏใ‚นใƒšใƒชใ‚จใƒณใ‚นใ‚’ๆไพ›ใงใใพใ™ใ€‚ + +ไปฅไธŠใŒใ€ๆไพ›ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใฎMarkdownๅฝขๅผใฎ็ฟป่จณใงใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pr_checks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pr_checks.md new file mode 100644 index 0000000000000000000000000000000000000000..dc8450b52502e670c12cf4baeb0f0777395df15f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/pr_checks.md @@ -0,0 +1,208 @@ + + + +# Checks on a Pull Request + +๐Ÿค— Transformersใƒชใƒใ‚ธใƒˆใƒชใงใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’้–‹ใใจใ€่ฟฝๅŠ ใ—ใฆใ„ใ‚‹ใƒ‘ใƒƒใƒใŒๆ—ขๅญ˜ใฎใ‚‚ใฎใ‚’ๅฃŠใ—ใฆใ„ใชใ„ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ใ‹ใชใ‚Šใฎๆ•ฐใฎใƒใ‚งใƒƒใ‚ฏใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใซใฏใ€ๆฌกใฎ4ใคใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™๏ผš +- ้€šๅธธใฎใƒ†ใ‚นใƒˆ +- ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใƒ“ใƒซใƒ‰ +- ใ‚ณใƒผใƒ‰ใจใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใ‚นใ‚ฟใ‚คใƒซ +- ใƒชใƒใ‚ธใƒˆใƒชๅ…จไฝ“ใฎไธ€่ฒซๆ€ง + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€ใ“ใ‚Œใ‚‰ใฎใ•ใพใ–ใพใชใƒใ‚งใƒƒใ‚ฏใจใใฎ่ƒŒๅพŒใซใ‚ใ‚‹็†็”ฑใ€ใใ—ใฆใใ‚Œใ‚‰ใฎใ„ใšใ‚Œใ‹ใŒใ‚ใชใŸใฎใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใงๅคฑๆ•—ใ—ใŸๅ ดๅˆใฎใƒญใƒผใ‚ซใƒซใงใฎใƒ‡ใƒใƒƒใ‚ฐๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ใชใŠใ€็†ๆƒณ็š„ใซใฏใ€้–‹็™บ่€…็”จใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซใŒๅฟ…่ฆใงใ™๏ผš + + +```bash +pip install transformers[dev] +``` + +ใพใŸใฏ็ทจ้›†ๅฏ่ƒฝใชใ‚คใƒณใ‚นใƒˆใƒผใƒซใฎๅ ดๅˆ๏ผš + + +```bash +pip install -e .[dev] +``` + +ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚บใฎใƒชใƒใ‚ธใƒˆใƒชๅ†…ใงไฝœๆฅญใ—ใฆใ„ใพใ™ใ€‚ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚บใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใฎไพๅญ˜้–ขไฟ‚ใฎๆ•ฐใŒๅข—ใˆใŸใŸใ‚ใ€ใ™ในใฆใ‚’ๅ–ๅพ—ใงใใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚้–‹็™บ็”จใ‚คใƒณใ‚นใƒˆใƒผใƒซใŒๅคฑๆ•—ใ—ใŸๅ ดๅˆใ€ไฝœๆฅญใ—ใฆใ„ใ‚‹ใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ๏ผˆPyTorchใ€TensorFlowใ€ใŠใ‚ˆใณ/ใพใŸใฏFlax๏ผ‰ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„ใ€‚ + + +```bash +pip install transformers[quality] +``` + +ใพใŸใฏ็ทจ้›†ๅฏ่ƒฝใชใ‚คใƒณใ‚นใƒˆใƒผใƒซใฎๅ ดๅˆ๏ผš + +```bash +pip install -e .[quality] +``` + +## Tests + +`ci/circleci: run_tests_` ใงๅง‹ใพใ‚‹ใ™ในใฆใฎใ‚ธใƒงใƒ–ใฏใ€Transformersใฎใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎไธ€้ƒจใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚ธใƒงใƒ–ใฏใ€็‰นๅฎšใฎ็’ฐๅขƒใงใƒฉใ‚คใƒ–ใƒฉใƒชใฎไธ€้ƒจใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใŸใจใˆใฐใ€`ci/circleci: run_tests_pipelines_tf` ใฏใ€TensorFlowใฎใฟใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚ŒใŸ็’ฐๅขƒใงใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎไธ€้ƒจใฎใฟใŒๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฏใ€ๅค‰ๆ›ดๅ‰ใจๅค‰ๆ›ดๅพŒใฎPRใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฎ้•ใ„ใ‚’ๆฑบๅฎšใ—ใ€ใใฎ้•ใ„ใซๅฝฑ้Ÿฟใ‚’ๅ—ใ‘ใ‚‹ใƒ†ใ‚นใƒˆใ‚’้ธๆŠžใ™ใ‚‹ใŸใ‚ใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใ“ใฎใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใฏใ€ใƒญใƒผใ‚ซใƒซใงไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆๅฎŸ่กŒใงใใพใ™๏ผš + + +```bash +python utils/tests_fetcher.py +``` + +1. ใƒชใƒใ‚ธใƒˆใƒชใฎใƒซใƒผใƒˆใ‹ใ‚‰ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใ‚Œใฏๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใ‚’ๅฎŸ่กŒใ—ใพใ™๏ผš + + 1. ๅทฎๅˆ†ๅ†…ใฎๅ„ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใ€ๅค‰ๆ›ดใŒใ‚ณใƒผใƒ‰ๅ†…ใซใ‚ใ‚‹ใ‹ใ€ใ‚ณใƒกใƒณใƒˆใ‚„ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณๆ–‡ๅญ—ๅˆ—ใฎใฟใซใ‚ใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ๅฎŸ้š›ใฎใ‚ณใƒผใƒ‰ๅค‰ๆ›ดใŒใ‚ใ‚‹ใƒ•ใ‚กใ‚คใƒซใฎใฟใ‚’ไฟๆŒใ—ใพใ™ใ€‚ + + 2. ๅ†…้ƒจใฎใƒžใƒƒใƒ—ใ‚’ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ใ“ใฎใƒžใƒƒใƒ—ใฏใ€ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใ‚ฝใƒผใ‚นใ‚ณใƒผใƒ‰ใฎๅ„ใƒ•ใ‚กใ‚คใƒซใŒๅ†ๅธฐ็š„ใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใ™ในใฆใฎใƒ•ใ‚กใ‚คใƒซใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใƒขใ‚ธใƒฅใƒผใƒซAใŒใƒขใ‚ธใƒฅใƒผใƒซBใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ใจใฏใ€ใƒขใ‚ธใƒฅใƒผใƒซBใŒใƒขใ‚ธใƒฅใƒผใƒซAใ‚’ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ๅ ดๅˆใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ๅ†ๅธฐ็š„ใชๅฝฑ้Ÿฟใ‚’ๅพ—ใ‚‹ใซใฏใ€ใƒขใ‚ธใƒฅใƒผใƒซAใ‹ใ‚‰ใƒขใ‚ธใƒฅใƒผใƒซBใธใฎใƒขใ‚ธใƒฅใƒผใƒซใฎใƒใ‚งใƒผใƒณใŒๅฟ…่ฆใงใ€ๅ„ใƒขใ‚ธใƒฅใƒผใƒซใฏๅ‰ใฎใƒขใ‚ธใƒฅใƒผใƒซใ‚’ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + 3. ใ“ใฎใƒžใƒƒใƒ—ใ‚’ใ‚นใƒ†ใƒƒใƒ—1ใงๅŽ้›†ใ—ใŸใƒ•ใ‚กใ‚คใƒซใซ้ฉ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€PRใซๅฝฑ้Ÿฟใ‚’ๅ—ใ‘ใ‚‹ใƒขใƒ‡ใƒซใƒ•ใ‚กใ‚คใƒซใฎใƒชใ‚นใƒˆใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + + 4. ใ“ใ‚Œใ‚‰ใฎใƒ•ใ‚กใ‚คใƒซใ‚’ใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใซใƒžใƒƒใƒ—ใ—ใ€ๅฎŸ่กŒใ™ใ‚‹ใƒ†ใ‚นใƒˆใฎใƒชใ‚นใƒˆใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +2. ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ใƒญใƒผใ‚ซใƒซใงๅฎŸ่กŒใ™ใ‚‹ๅ ดๅˆใ€ใ‚นใƒ†ใƒƒใƒ—1ใ€3ใ€ใŠใ‚ˆใณ4ใฎ็ตๆžœใŒ่กจ็คบใ•ใ‚Œใ€ๅฎŸ่กŒใ™ใ‚‹ใƒ†ใ‚นใƒˆใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใพใŸใ€`test_list.txt` ใจใ„ใ†ๅๅ‰ใฎใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ“ใฎใƒ•ใ‚กใ‚คใƒซใซใฏๅฎŸ่กŒใ™ใ‚‹ใƒ†ใ‚นใƒˆใฎใƒชใ‚นใƒˆใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใงใƒญใƒผใ‚ซใƒซใงๅฎŸ่กŒใงใใพใ™๏ผš + +```bash +python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) +``` + +## Documentation build + +`build_pr_documentation` ใ‚ธใƒงใƒ–ใฏใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใƒ“ใƒซใƒ‰ใ‚’่กŒใ„ใ€ใ‚ใชใŸใฎPRใŒใƒžใƒผใ‚ธใ•ใ‚ŒใŸๅพŒใซใ™ในใฆใŒๆญฃๅธธใซ่กจ็คบใ•ใ‚Œใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ใƒœใƒƒใƒˆใŒใƒ—ใƒฌใƒ“ใƒฅใƒผใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใธใฎใƒชใƒณใ‚ฏใ‚’PRใซ่ฟฝๅŠ ใ—ใพใ™ใ€‚PRใซๅฏพใ™ใ‚‹ๅค‰ๆ›ดใฏใ€ใƒ—ใƒฌใƒ“ใƒฅใƒผใซ่‡ชๅ‹•็š„ใซๅๆ˜ ใ•ใ‚Œใพใ™ใ€‚ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎใƒ“ใƒซใƒ‰ใซๅคฑๆ•—ใ—ใŸๅ ดๅˆใ€ๅคฑๆ•—ใ—ใŸใ‚ธใƒงใƒ–ใฎ้šฃใซใ‚ใ‚‹ใ€Œ่ฉณ็ดฐใ€ใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใ€ไฝ•ใŒๅ•้กŒใซใชใฃใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใงใใพใ™ใ€‚ๅคšใใฎๅ ดๅˆใ€ๅ•้กŒใฏ`toctree`ๅ†…ใฎใƒ•ใ‚กใ‚คใƒซใŒไธ่ถณใ—ใฆใ„ใ‚‹ใชใฉใ€ๅ˜็ด”ใชใ‚‚ใฎใงใ™ใ€‚ + +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ใƒญใƒผใ‚ซใƒซใงใƒ“ใƒซใƒ‰ใพใŸใฏใƒ—ใƒฌใƒ“ใƒฅใƒผใ—ใŸใ„ๅ ดๅˆใฏใ€[docsใƒ•ใ‚ฉใƒซใƒ€ๅ†…ใฎ`README.md`](https://github.com/huggingface/transformers/tree/main/docs)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +## Code and documentation style + +ใ™ในใฆใฎใ‚ฝใƒผใ‚นใƒ•ใ‚กใ‚คใƒซใ€ไพ‹ใ€ใƒ†ใ‚นใƒˆใซใฏใ€`black`ใจ`ruff`ใ‚’ไฝฟ็”จใ—ใฆใ‚ณใƒผใƒ‰ใฎใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใŒ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ใพใŸใ€ใƒ‰ใƒƒใ‚ฏใ‚นใƒˆใƒชใƒณใ‚ฐใจ`rst`ใƒ•ใ‚กใ‚คใƒซใฎใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ€Transformersใฎ`__init__.py`ใƒ•ใ‚กใ‚คใƒซใงๅฎŸ่กŒใ•ใ‚Œใ‚‹้…ๅปถใ‚คใƒณใƒใƒผใƒˆใฎ้ †ๅบใซใคใ„ใฆใ‚‚ใ‚ซใ‚นใ‚ฟใƒ ใƒ„ใƒผใƒซใŒๅญ˜ๅœจใ—ใพใ™๏ผˆ`utils/style_doc.py`ใจ`utils/custom_init_isort.py`๏ผ‰ใ€‚ใ“ใ‚Œใ‚‰ใ™ในใฆใฏใ€ไปฅไธ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใง่ตทๅ‹•ใงใใพใ™ใ€‚ + + +```bash +make style +``` + +CIใฏใ€`ci/circleci: check_code_quality` ใƒใ‚งใƒƒใ‚ฏๅ†…ใงใ“ใ‚Œใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใŒ้ฉ็”จใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ใพใŸใ€`ruff` ใ‚’ๅฎŸ่กŒใ—ใ€ๆœชๅฎš็พฉใฎๅค‰ๆ•ฐใ‚„ไฝฟ็”จใ•ใ‚Œใฆใ„ใชใ„ๅค‰ๆ•ฐใŒใ‚ใ‚‹ๅ ดๅˆใซใ‚จใƒฉใƒผใ‚’ๅ ฑๅ‘Šใ—ใพใ™ใ€‚ใ“ใฎใƒใ‚งใƒƒใ‚ฏใ‚’ใƒญใƒผใ‚ซใƒซใงๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + +```bash +make quality +``` + +ๆ™‚้–“ใŒใ‹ใ‹ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€็พๅœจใฎใƒ–ใƒฉใƒณใƒใงๅค‰ๆ›ดใ—ใŸใƒ•ใ‚กใ‚คใƒซใฎใฟใงๅŒใ˜ใ“ใจใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + + +```bash +make fixup +``` + +ใ“ใฎๆœ€ๅพŒใฎใ‚ณใƒžใƒณใƒ‰ใฏใ€ใƒชใƒใ‚ธใƒˆใƒชใฎๆ•ดๅˆๆ€งใฎใŸใ‚ใฎใ™ในใฆใฎ่ฟฝๅŠ ใฎใƒใ‚งใƒƒใ‚ฏใ‚‚ๅฎŸ่กŒใ—ใพใ™ใ€‚ใใ‚Œใ‚‰ใ‚’่ฉณใ—ใ่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +## Repository consistency + +ใ“ใ‚Œใซใฏใ€ใ‚ใชใŸใฎPRใŒใƒชใƒใ‚ธใƒˆใƒชใ‚’้ฉๅˆ‡ใช็Šถๆ…‹ใซไฟใฃใŸใพใพใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ci/`circleci: check_repository_consistency` ใƒใ‚งใƒƒใ‚ฏใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ใƒญใƒผใ‚ซใƒซใงใ“ใฎใƒใ‚งใƒƒใ‚ฏใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +make repo-consistency +``` + +ใ“ใ‚Œใ‚’็ขบ่ชใ—ใพใ™๏ผš + +- `utils/check_repo.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€init ใซ่ฟฝๅŠ ใ•ใ‚ŒใŸใ™ในใฆใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒๆ–‡ๆ›ธๅŒ–ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- `utils/check_inits.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ใ™ในใฆใฎ `__init__.py` ใƒ•ใ‚กใ‚คใƒซใŒใใฎ2ใคใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงๅŒใ˜ๅ†…ๅฎนใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ +- `utils/check_copies.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ไป–ใฎใƒขใ‚ธใƒฅใƒผใƒซใ‹ใ‚‰ใฎใ‚ณใƒ”ใƒผใจใ—ใฆ่ญ˜ๅˆฅใ•ใ‚ŒใŸใ™ในใฆใฎใ‚ณใƒผใƒ‰ใŒๅ…ƒใฎใ‚ณใƒผใƒ‰ใจไธ€่‡ดใ—ใฆใ„ใพใ™ใ€‚ +- `utils/check_config_docstrings.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ใ™ในใฆใฎ่จญๅฎšใ‚ฏใƒฉใ‚นใซใฏๅฐ‘ใชใใจใ‚‚1ใคใฎๆœ‰ๅŠนใชใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆๆ–‡ๅญ—ๅˆ—ใซ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +- `utils/check_config_attributes.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ใ™ในใฆใฎ่จญๅฎšใ‚ฏใƒฉใ‚นใซใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใงไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ๅฑžๆ€งใฎใฟใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +- `utils/check_copies.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€README ใจใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฎ็ฟป่จณใŒใ€ใƒกใ‚คใƒณใฎREADME ใจๅŒใ˜ใƒขใƒ‡ใƒซใƒชใ‚นใƒˆใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ +- `utils/check_table.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎ่‡ชๅ‹•็”Ÿๆˆใƒ†ใƒผใƒ–ใƒซใŒๆœ€ๆ–ฐใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +- `utils/check_dummies.py` ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ€ใ™ในใฆใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒๅˆฉ็”จๅฏ่ƒฝใงใ‚ใ‚Šใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎไพๅญ˜้–ขไฟ‚ใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใชใใฆใ‚‚ๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ“ใฎใƒใ‚งใƒƒใ‚ฏใŒๅคฑๆ•—ใ™ใ‚‹ๅ ดๅˆใ€ๆœ€ๅˆใฎ2ใคใฎ้ …็›ฎใฏๆ‰‹ๅ‹•ใงไฟฎๆญฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ๆœ€ๅพŒใฎ4ใคใฏใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆ่‡ชๅ‹•็š„ใซไฟฎๆญฃใงใใพใ™ใ€‚ + + +```bash +make fix-copies +``` + +่ฟฝๅŠ ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹Pull Request๏ผˆPR๏ผ‰ใซ้–ข้€ฃใ—ใฆใ„ใพใ™ใ€‚ไธปใซๆฌกใฎ็‚นใ‚’็ขบ่ชใ—ใพใ™๏ผš + +- ใ™ในใฆใฎ่ฟฝๅŠ ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€Auto-mapping๏ผˆ`utils/check_repo.py`ใงๅฎŸ่กŒ๏ผ‰ใซๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + +- ใ™ในใฆใฎใƒขใƒ‡ใƒซใŒ้ฉๅˆ‡ใซใƒ†ใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™๏ผˆ`utils/check_repo.py`ใงๅฎŸ่กŒ๏ผ‰ใ€‚ + + + + +### Check copies + +Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€ใƒขใƒ‡ใƒซใ‚ณใƒผใƒ‰ใซ้–ขใ—ใฆ้žๅธธใซๆ„่ฆ‹ใŒใ‚ใ‚‹ใŸใ‚ใ€ๅ„ใƒขใƒ‡ใƒซใฏไป–ใฎใƒขใƒ‡ใƒซใซไพๅญ˜ใ›ใšใซๅฎŒๅ…จใซ1ใคใฎใƒ•ใ‚กใ‚คใƒซใซๅฎŸ่ฃ…ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€็‰นๅฎšใฎใƒขใƒ‡ใƒซใฎใ‚ณใƒผใƒ‰ใฎใ‚ณใƒ”ใƒผใŒๅ…ƒใฎใ‚ณใƒผใƒ‰ใจไธ€่ฒซใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ไป•็ต„ใฟใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒใ‚ฐไฟฎๆญฃใŒใ‚ใ‚‹ๅ ดๅˆใ€ไป–ใฎๅฝฑ้Ÿฟใ‚’ๅ—ใ‘ใ‚‹ใƒขใƒ‡ใƒซใ‚’ใ™ในใฆ็ขบ่ชใ—ใ€ๅค‰ๆ›ดใ‚’ไผ้”ใ™ใ‚‹ใ‹ใ‚ณใƒ”ใƒผใ‚’็ ดๆฃ„ใ™ใ‚‹ใ‹ใ‚’้ธๆŠžใงใใพใ™ใ€‚ + + + +ใƒ•ใ‚กใ‚คใƒซใŒๅˆฅใฎใƒ•ใ‚กใ‚คใƒซใฎๅฎŒๅ…จใชใ‚ณใƒ”ใƒผใงใ‚ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚’`utils/check_copies.py`ใฎ`FULL_COPIES`ๅฎšๆ•ฐใซ็™ป้Œฒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ใ“ใฎไป•็ต„ใฟใฏใ€`# Copied from xxx`ใจใ„ใ†ๅฝขๅผใฎใ‚ณใƒกใƒณใƒˆใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚`xxx`ใฏใ€ใ‚ณใƒ”ใƒผใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฏใƒฉใ‚นใพใŸใฏ้–ขๆ•ฐใฎๅฎŒๅ…จใชใƒ‘ใ‚นใ‚’ๅซใ‚€ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€`RobertaSelfOutput`ใฏ`BertSelfOutput`ใ‚ฏใƒฉใ‚นใฎ็›ดๆŽฅใฎใ‚ณใƒ”ใƒผใงใ™ใฎใงใ€[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L289)ใซใ‚ณใƒกใƒณใƒˆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +# Copied from transformers.models.bert.modeling_bert.BertSelfOutput +``` + +ๆณจๆ„็‚นใจใ—ใฆใ€ใ“ใ‚Œใ‚’ใ‚ฏใƒฉใ‚นๅ…จไฝ“ใซ้ฉ็”จใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ใ‚ณใƒ”ใƒผๅ…ƒใฎ้–ข้€ฃใƒกใ‚ฝใƒƒใƒ‰ใซ้ฉ็”จใงใใพใ™ใ€‚ใŸใจใˆใฐใ€[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L598)ใงใฏใ€`RobertaPreTrainedModel._init_weights` ใŒ `BertPreTrainedModel` ใ‹ใ‚‰ใ‚ณใƒ”ใƒผใ•ใ‚ŒใฆใŠใ‚Šใ€ไปฅไธ‹ใฎใ‚ณใƒกใƒณใƒˆใŒใ‚ใ‚Šใพใ™๏ผš + + +```py +# Copied from transformers.models.bert.modeling_bert.BertAttention with Bert->Roberta +``` + +ๆณจ๏ผš็Ÿขๅฐใฎๅ‘จใ‚Šใซใฏใ‚นใƒšใƒผใ‚นใŒๅซใพใ‚Œใฆใ„ใฆใฏใ„ใ‘ใพใ›ใ‚“๏ผˆใ‚‚ใกใ‚ใ‚“ใ€ใใฎใ‚นใƒšใƒผใ‚นใŒ็ฝฎๆ›ใƒ‘ใ‚ฟใƒผใƒณใฎไธ€้ƒจใงใ‚ใ‚‹ๅ ดๅˆใ‚’้™คใใพใ™๏ผ‰ใ€‚ + +ใ‚ซใƒณใƒžใงๅŒบๅˆ‡ใ‚‰ใ‚ŒใŸ่ค‡ๆ•ฐใฎใƒ‘ใ‚ฟใƒผใƒณใ‚’่ฟฝๅŠ ใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€ใ“ใ“ใงใฏ `CamemberForMaskedLM` ใฏ `RobertaForMaskedLM` ใฎ็›ดๆŽฅใฎใ‚ณใƒ”ใƒผใงใ€2ใคใฎ็ฝฎๆ›ใŒใ‚ใ‚Šใพใ™๏ผš `Roberta` ใ‹ใ‚‰ `Camembert` ใธใ€ใใ—ใฆ `ROBERTA` ใ‹ใ‚‰ `CAMEMBERT` ใธใจ็ฝฎๆ›ใ•ใ‚Œใพใ™ใ€‚[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/camembert/modeling_camembert.py#L929)ใงใ€ใ“ใฎไฝœๆฅญใฏใ‚ณใƒกใƒณใƒˆไป˜ใใง่กŒใ‚ใ‚Œใฆใ„ใพใ™ใ€‚ + + +```py +# Copied from transformers.models.roberta.modeling_roberta.RobertaForMaskedLM with Roberta->Camembert, ROBERTA->CAMEMBERT +``` + + +ใ‚‚ใ—้ †ๅบใŒ้‡่ฆใชๅ ดๅˆ๏ผˆไปฅๅ‰ใฎ็ฝฎๆ›ใจ็ซถๅˆใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚๏ผ‰ใ€็ฝฎๆ›ใฏๅทฆใ‹ใ‚‰ๅณใซๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + + + +ใ‚‚ใ—็ฝฎๆ›ใŒใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ‚’ๅค‰ๆ›ดใ™ใ‚‹ๅ ดๅˆ๏ผˆใŸใจใˆใฐใ€็Ÿญใ„ๅๅ‰ใ‚’้žๅธธใซ้•ทใ„ๅๅ‰ใซ็ฝฎใๆ›ใˆใ‚‹ๅ ดๅˆใชใฉ๏ผ‰ใ€่‡ชๅ‹•ใƒ•ใ‚ฉใƒผใƒžใƒƒใ‚ฟใ‚’้ฉ็”จใ—ใŸๅพŒใซใ‚ณใƒ”ใƒผใŒ็ขบ่ชใ•ใ‚Œใพใ™ใ€‚ + + + +ใƒ‘ใ‚ฟใƒผใƒณใŒๅŒใ˜็ฝฎๆ›ใฎ็•ฐใชใ‚‹ใ‚ฑใƒผใ‚น๏ผˆๅคงๆ–‡ๅญ—ใจๅฐๆ–‡ๅญ—ใฎใƒใƒชใ‚ขใƒณใƒˆใŒใ‚ใ‚‹๏ผ‰ใฎๅ ดๅˆใ€ใ‚ชใƒ—ใ‚ทใƒงใƒณใจใ—ใฆ `all-casing` ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใฎๅˆฅใฎๆ–นๆณ•ใ‚‚ใ‚ใ‚Šใพใ™ใ€‚[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/mobilebert/modeling_mobilebert.py#L1237)ใฏใ€`MobileBertForSequenceClassification` ๅ†…ใฎไพ‹ใงใ€ใ‚ณใƒกใƒณใƒˆใŒใคใ„ใฆใ„ใพใ™ใ€‚ + + +```py +# Copied from transformers.models.bert.modeling_bert.BertForSequenceClassification with Bert->MobileBert all-casing +``` + +ใ“ใฎๅ ดๅˆใ€ใ‚ณใƒผใƒ‰ใฏใ€ŒBertForSequenceClassificationใ€ใ‹ใ‚‰ใ‚ณใƒ”ใƒผใ•ใ‚Œใ€ๆฌกใฎใ‚ˆใ†ใซ็ฝฎๆ›ใ•ใ‚Œใพใ™๏ผš +- `Bert` ใ‚’ `MobileBert` ใซ็ฝฎใๆ›ใˆใ‚‹๏ผˆไพ‹๏ผš`init`ใง `MobileBertModel` ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆ๏ผ‰ +- `bert` ใ‚’ `mobilebert` ใซ็ฝฎใๆ›ใˆใ‚‹๏ผˆไพ‹๏ผš`self.mobilebert` ใ‚’ๅฎš็พฉใ™ใ‚‹ๅ ดๅˆ๏ผ‰ +- `BERT` ใ‚’ `MOBILEBERT` ใซ็ฝฎใๆ›ใˆใ‚‹๏ผˆๅฎšๆ•ฐ `MOBILEBERT_INPUTS_DOCSTRING` ๅ†…ใง๏ผ‰ + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/preprocessing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/preprocessing.md new file mode 100644 index 0000000000000000000000000000000000000000..ea0b98df028031e15f985cf53df53ca49b45891b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/preprocessing.md @@ -0,0 +1,533 @@ + + +# Preprocess + +[[open-in-colab]] + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซใ€ใใ‚Œใ‚’ใƒขใƒ‡ใƒซใฎๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใซๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใŒใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€ใพใŸใฏใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใงใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใซใ‹ใ‹ใ‚ใ‚‰ใšใ€ใใ‚Œใ‚‰ใฏใƒ†ใƒณใ‚ฝใƒซใฎใƒใƒƒใƒใซๅค‰ๆ›ใ—ใฆ็ต„ใฟ็ซ‹ใฆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +๐Ÿค— Transformersใฏใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ๆฌกใฎใ“ใจใ‚’ๅญฆใณใพใ™๏ผš + +* ใƒ†ใ‚ญใ‚นใƒˆใฎๅ ดๅˆใ€[Tokenizer](./main_classes/tokenizer)ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅค‰ๆ›ใ—ใ€ใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐๅ€ค่กจ็พใ‚’ไฝœๆˆใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซ็ต„ใฟ็ซ‹ใฆใ‚‹ๆ–นๆณ•ใ€‚ +* ้Ÿณๅฃฐใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใฎๅ ดๅˆใ€[Feature extractor](./main_classes/feature_extractor)ใ‚’ไฝฟ็”จใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใ‹ใ‚‰้€ฃ็ถš็š„ใช็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ๆ–นๆณ•ใ€‚ +* ็”ปๅƒๅ…ฅๅŠ›ใฎๅ ดๅˆใ€[ImageProcessor](./main_classes/image)ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ๆ–นๆณ•ใ€‚ +* ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใฎๅ ดๅˆใ€[Processor](./main_classes/processors)ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจ็‰นๅพดๆŠฝๅ‡บๅ™จใพใŸใฏ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ๆ–นๆณ•ใ€‚ + + + +`AutoProcessor`ใฏๅธธใซๅ‹•ไฝœใ—ใ€ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใซ้ฉๅˆ‡ใชใ‚ฏใƒฉใ‚นใ‚’่‡ชๅ‹•็š„ใซ้ธๆŠžใ—ใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพดๆŠฝๅ‡บๅ™จใ€ใพใŸใฏใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใซใ‹ใ‹ใ‚ใ‚‰ใšใ€ๅ‹•ไฝœใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€๐Ÿค— Datasetsใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ€ใ„ใใคใ‹ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ฉฆใ™ใ“ใจใŒใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pip install datasets +``` + +## Natural Language Processing + + + +ใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใฎๅ‰ๅ‡ฆ็†ใซไฝฟ็”จใ™ใ‚‹ไธป่ฆใชใƒ„ใƒผใƒซใฏใ€[ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ](main_classes/tokenizer)ใงใ™ใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ไธ€้€ฃใฎใƒซใƒผใƒซใซๅพ“ใฃใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’*ใƒˆใƒผใ‚ฏใƒณ*ใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณใฏๆ•ฐๅ€คใซๅค‰ๆ›ใ•ใ‚Œใ€ใใฎๅพŒใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใจใชใ‚Šใพใ™ใ€‚ใƒขใƒ‡ใƒซใŒๅฟ…่ฆใจใ™ใ‚‹่ฟฝๅŠ ใฎๅ…ฅๅŠ›ใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใ‚ˆใฃใฆ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ + + + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ไบˆๅฎšใฎๅ ดๅˆใ€้–ข้€ฃใ™ใ‚‹ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใŒไบ‹ๅ‰ๅญฆ็ฟ’ใ‚ณใƒผใƒ‘ใ‚นใจๅŒใ˜ๆ–นๆณ•ใงๅˆ†ๅ‰ฒใ•ใ‚Œใ€ไบ‹ๅ‰ๅญฆ็ฟ’ไธญใซ้€šๅธธ*ใƒœใ‚ญใƒฃใƒ–*ใจใ—ใฆๅ‚็…งใ•ใ‚Œใ‚‹ๅฏพๅฟœใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + + + +[`AutoTokenizer.from_pretrained`]ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€้–‹ๅง‹ใ—ใพใ—ใ‚‡ใ†ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ*ใƒœใ‚ญใƒฃใƒ–*ใŒใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚Œใพใ™๏ผš + +```python +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") +``` + +ๆฌกใซใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซๆธกใ—ใพใ™๏ผš + +```py +>>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.") +>>> print(encoded_input) +{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€้‡่ฆใช3ใคใฎ้ …็›ฎใ‚’ๆŒใค่พžๆ›ธใ‚’่ฟ”ใ—ใพใ™๏ผš + +* [input_ids](glossary#input-ids) ใฏๆ–‡ไธญใฎๅ„ใƒˆใƒผใ‚ฏใƒณใซๅฏพๅฟœใ™ใ‚‹ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใงใ™ใ€‚ +* [attention_mask](glossary#attention-mask) ใฏใƒˆใƒผใ‚ฏใƒณใŒใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ๅ—ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ +* [token_type_ids](glossary#token-type-ids) ใฏ่ค‡ๆ•ฐใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚‹ๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒณใŒใฉใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅฑžใ—ใฆใ„ใ‚‹ใ‹ใ‚’่ญ˜ๅˆฅใ—ใพใ™ใ€‚ + +`input_ids` ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆๅ…ฅๅŠ›ใ‚’่ฟ”ใ—ใพใ™๏ผš + +```python +>>> tokenizer.decode(encoded_input["input_ids"]) +'[CLS] ้ญ”ๆณ•ไฝฟใ„ใฎไบ‹ใซๅนฒๆธ‰ใ™ใ‚‹ใชใ€ๅฝผใ‚‰ใฏๅพฎๅฆ™ใงๆ€’ใ‚Šใฃใฝใ„ใ€‚ [SEP]' +``` + +ๅฆ‚ไฝ•ใซใŠๅˆ†ใ‹ใ‚Šใ„ใŸใ ใ‘ใ‚‹ใ‹ใจๆ€ใ„ใพใ™ใŒใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ“ใฎๆ–‡็ซ ใซ2ใคใฎ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ€`CLS`๏ผˆใ‚ฏใƒฉใ‚ทใƒ•ใ‚กใ‚คใ‚ข๏ผ‰ใจ`SEP`๏ผˆใ‚ปใƒ‘ใƒฌใƒผใ‚ฟ๏ผ‰ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ +ใ™ในใฆใฎใƒขใƒ‡ใƒซใŒ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ‚’ๅฟ…่ฆใจใ™ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅฟ…่ฆใชๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏ่‡ชๅ‹•็š„ใซใใ‚Œใ‚‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +่ค‡ๆ•ฐใฎๆ–‡็ซ ใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅ ดๅˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซใƒชใ‚นใƒˆใจใ—ใฆๆธกใ—ใฆใใ ใ•ใ„๏ผš + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_inputs = tokenizer(batch_sentences) +>>> print(encoded_inputs) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1]]} +``` + +### Pad + +ๆ–‡็ซ ใฏๅธธใซๅŒใ˜้•ทใ•ใงใฏใชใ„ใ“ใจใŒใ‚ใ‚Šใ€ใ“ใ‚Œใฏใƒ†ใƒณใ‚ฝใƒซ๏ผˆใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›๏ผ‰ใŒๅ‡ไธ€ใชๅฝข็Šถใ‚’ๆŒใคๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ๅ•้กŒใจใชใ‚Šใพใ™ใ€‚ +ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใฏใ€็Ÿญใ„ๆ–‡ใซ็‰นๅˆฅใชใ€Œใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ€ใ‚’่ฟฝๅŠ ใ—ใฆใ€ใƒ†ใƒณใ‚ฝใƒซใ‚’้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆใ‚ใ›ใ‚‹ใŸใ‚ใฎๆˆฆ็•ฅใงใ™ใ€‚ + +ใƒใƒƒใƒๅ†…ใฎ็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆœ€้•ทใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆใ‚ใ›ใ‚‹ใŸใ‚ใซใ€`padding`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`True`ใซ่จญๅฎšใ—ใพใ™๏ผš + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + +1็•ช็›ฎใจ3็•ช็›ฎใฎๆ–‡ใฏใ€็Ÿญใ„ใŸใ‚ใซ`0`ใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +### Truncation + +้€†ใฎใ‚นใƒšใ‚ฏใƒˆใƒซใงใฏใ€ๆ™‚ๆŠ˜ใ€ใƒขใƒ‡ใƒซใŒๅ‡ฆ็†ใ™ใ‚‹ใฎใซ้•ทใ™ใŽใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใฎๅ ดๅˆใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’็Ÿญ็ธฎใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใŒๅ—ใ‘ๅ…ฅใ‚Œใ‚‹ๆœ€ๅคงใฎ้•ทใ•ใซใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใซใฏใ€`truncation`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`True`ใซ่จญๅฎšใ—ใพใ™๏ผš + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True) +>>> print(encoded_input) +{'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], + 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], + 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]} +``` + + + +็•ฐใชใ‚‹ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚ใฎๅผ•ๆ•ฐใซใคใ„ใฆ่ฉณใ—ใใฏใ€[ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Š่ฉฐใ‚](./pad_truncation)ใฎใ‚ณใƒณใ‚ปใƒ—ใƒˆใ‚ฌใ‚คใƒ‰ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +### Build tensors + +ๆœ€ๅพŒใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒใƒขใƒ‡ใƒซใซไพ›็ตฆใ•ใ‚Œใ‚‹ๅฎŸ้š›ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ™ใ‚ˆใ†ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +`return_tensors`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’`pt`๏ผˆPyTorch็”จ๏ผ‰ใพใŸใฏ`tf`๏ผˆTensorFlow็”จ๏ผ‰ใซ่จญๅฎšใ—ใพใ™๏ผš + + + + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt") +>>> print(encoded_input) +{'input_ids': tensor([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], + [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], + [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), + 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])} +``` + + +```py +>>> batch_sentences = [ +... "But what about second breakfast?", +... "Don't think he knows about second breakfast, Pip.", +... "What about elevensies?", +... ] +>>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="tf") +>>> print(encoded_input) +{'input_ids': , + 'token_type_ids': , + 'attention_mask': } +``` + + + +## Audio + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใซ[็‰นๅพดๆŠฝๅ‡บๅ™จ](main_classes/feature_extractor)ใŒๅฟ…่ฆใงใ™ใ€‚ +็‰นๅพดๆŠฝๅ‡บๅ™จใฏ็”Ÿใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‹ใ‚‰็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +[PolyAI/minds14](https://huggingface.co/datasets/PolyAI/minds14)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใฆ๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค— [Datasetsใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰ใ€ +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใฉใฎใ‚ˆใ†ใซไฝฟ็”จใงใใ‚‹ใ‹ใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +ใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆ`audio`ๅˆ—ใฎๆœ€ๅˆใฎ่ฆ็ด ใ‚’็ขบ่ชใ—ใพใ™ใ€‚`audio`ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ™ใจใ€่‡ชๅ‹•็š„ใซใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใŒ่ชญใฟ่พผใพใ‚Œใ€ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™๏ผš + +```py +>>> dataset[0]["audio"] +{'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414, + 0. , 0. ], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', + 'sampling_rate': 8000} +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€3ใคใฎใ‚ขใ‚คใƒ†ใƒ ใŒ่ฟ”ใ•ใ‚Œใพใ™๏ผš + +* `array` ใฏ่ชญใฟ่พผใพใ‚ŒใŸ้Ÿณๅฃฐไฟกๅทใงใ€1Dใฎ้…ๅˆ—ใจใ—ใฆ่ชญใฟ่พผใพใ‚Œใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ +* `path` ใฏ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใฎๅ ดๆ‰€ใ‚’ๆŒ‡ใ—ใพใ™ใ€‚ +* `sampling_rate` ใฏ้Ÿณๅฃฐไฟกๅทๅ†…ใฎใƒ‡ใƒผใ‚ฟใƒใ‚คใƒณใƒˆใŒ1็ง’้–“ใซใ„ใใคๆธฌๅฎšใ•ใ‚Œใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€[Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base)ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ‚’็ขบ่ชใ™ใ‚‹ใจใ€Wav2Vec2ใŒ16kHzใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚ŒใŸ้Ÿณๅฃฐใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใงไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ๅญฆ็ฟ’ใซไฝฟ็”จใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใจใ€ใ‚ใชใŸใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒไธ€่‡ดใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒ็•ฐใชใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. ๐Ÿค— Datasetsใฎ [`~datasets.Dataset.cast_column`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใ‚’16kHzใซใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™๏ผš + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +2. ๅ†ใณ `audio` ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใ‚’ใƒชใ‚ตใƒณใƒ—ใƒซใ—ใพใ™๏ผš + +```py +>>> dataset[0]["audio"] +{'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ..., + 3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', + 'sampling_rate': 16000} +``` + +ๆฌกใซใ€ๅ…ฅๅŠ›ใ‚’ๆญฃ่ฆๅŒ–ใ—ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใซใฏ `0` ใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๅŒใ˜่€ƒใˆๆ–นใŒใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใซใ‚‚้ฉ็”จใ•ใ‚Œใพใ™ใ€‚็‰นๅพดๆŠฝๅ‡บๅ™จใฏ `array` ใซ `0` ใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผˆใ“ใ‚Œใฏ็„ก้Ÿณใจใ—ใฆ่งฃ้‡ˆใ•ใ‚Œใพใ™๏ผ‰ใ€‚ + +[`AutoFeatureExtractor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆ็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```python +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช `array` ใ‚’็‰นๅพดๆŠฝๅ‡บๅ™จใซๆธกใ—ใพใ™ใ€‚็‰นๅพดๆŠฝๅ‡บๅ™จใง็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹็„ก้Ÿณใ‚จใƒฉใƒผใ‚’ใ‚ˆใ‚Š่‰ฏใใƒ‡ใƒใƒƒใ‚ฐใ™ใ‚‹ใŸใ‚ใซใ€็‰นๅพดๆŠฝๅ‡บๅ™จใซ `sampling_rate` ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```python +>>> audio_input = [dataset[0]["audio"]["array"]] +>>> feature_extractor(audio_input, sampling_rate=16000) +{'input_values': [array([ 3.8106556e-04, 2.7506407e-03, 2.8015103e-03, ..., + 5.6335266e-04, 4.6588284e-06, -1.7142107e-04], dtype=float32)]} +``` + +ๅŒๆง˜ใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅŒๆง˜ใซใ€ใƒใƒƒใƒๅ†…ใฎๅฏๅค‰ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใพใŸใฏๅˆ‡ใ‚Š่ฉฐใ‚ใ‚’้ฉ็”จใงใใพใ™ใ€‚ๆฌกใซใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ตใƒณใƒ—ใƒซใฎใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> dataset[0]["audio"]["array"].shape +(173398,) + +>>> dataset[1]["audio"]["array"].shape +(106496,) +``` + +ใ“ใฎ้–ขๆ•ฐใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ตใƒณใƒ—ใƒซใฎ้•ทใ•ใ‚’ๅŒใ˜ใซใ™ใ‚‹ใŸใ‚ใฎใ‚‚ใฎใงใ™ใ€‚ๆœ€ๅคงใ‚ตใƒณใƒ—ใƒซ้•ทใ‚’ๆŒ‡ๅฎšใ—ใ€็‰นๅพดๆŠฝๅ‡บๅ™จใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใใ‚Œใซๅˆใ‚ใ›ใฆใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใพใŸใฏๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, +... sampling_rate=16000, +... padding=True, +... max_length=100000, +... truncation=True, +... ) +... return inputs +``` + +`preprocess_function`ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๆœ€ๅˆใฎๆ•ฐไพ‹ใซ้ฉ็”จใ—ใพใ™๏ผš + +```python +>>> processed_dataset = preprocess_function(dataset[:5]) +``` + +ใ‚ตใƒณใƒ—ใƒซใฎ้•ทใ•ใฏ็พๅœจๅŒใ˜ใงใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸๆœ€ๅคง้•ทใจไธ€่‡ดใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใงๅ‡ฆ็†ใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใŒใงใใพใ™๏ผ + +```py +>>> processed_dataset["input_values"][0].shape +(100000,) + +>>> processed_dataset["input_values"][1].shape +(100000,) +``` + +## Computer Vision + +ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใงใฏใ€ใƒขใƒ‡ใƒซ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใฎ[็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/image_processor)ใŒๅฟ…่ฆใงใ™ใ€‚ +็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใซใฏใ€็”ปๅƒใ‚’ใƒขใƒ‡ใƒซใŒๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎใ„ใใคใ‹ใฎใ‚นใƒ†ใƒƒใƒ—ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚นใƒ†ใƒƒใƒ—ใซใฏใ€ใƒชใ‚ตใ‚คใ‚บใ€ๆญฃ่ฆๅŒ–ใ€ใ‚ซใƒฉใƒผใƒใƒฃใƒใƒซใฎ่ฃœๆญฃใ€ใŠใ‚ˆใณ็”ปๅƒใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅค‰ๆ›ใ™ใ‚‹ใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ + + + +็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใฏใ€้€šๅธธใ€็”ปๅƒใฎๅข—ๅผทใฎๅฝขๅผใซๅพ“ใ„ใพใ™ใ€‚็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใจ็”ปๅƒใฎๅข—ๅผทใฎไธกๆ–นใฏ็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ๅค‰ๆ›ใ—ใพใ™ใŒใ€็•ฐใชใ‚‹็›ฎ็š„ใŒใ‚ใ‚Šใพใ™๏ผš + +* ็”ปๅƒใฎๅข—ๅผทใฏใ€้Žๅญฆ็ฟ’ใ‚’้˜ฒใŽใ€ใƒขใƒ‡ใƒซใฎๅ …็‰ขๆ€งใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใฎใซๅฝน็ซ‹ใคๆ–นๆณ•ใง็”ปๅƒใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚’ๅข—ๅผทใ™ใ‚‹ๆ–นๆณ•ใฏ็„ก้™ใงใ€ๆ˜Žใ‚‹ใ•ใ‚„่‰ฒใฎ่ชฟๆ•ดใ€ใ‚ฏใƒญใƒƒใƒ—ใ€ๅ›ž่ปขใ€ใƒชใ‚ตใ‚คใ‚บใ€ใ‚บใƒผใƒ ใชใฉใ€ๆง˜ใ€…ใชๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ๅข—ๅผทๆ“ไฝœใซใ‚ˆใฃใฆ็”ปๅƒใฎๆ„ๅ‘ณใŒๅค‰ใ‚ใ‚‰ใชใ„ใ‚ˆใ†ใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +* ็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใฏใ€็”ปๅƒใŒใƒขใƒ‡ใƒซใฎๆœŸๅพ…ใ™ใ‚‹ๅ…ฅๅŠ›ๅฝขๅผใจไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’ไฟ่จผใ—ใพใ™ใ€‚ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€็”ปๅƒใฏใƒขใƒ‡ใƒซใŒๆœ€ๅˆใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใจใใจใพใฃใŸใๅŒใ˜ๆ–นๆณ•ใงๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +็”ปๅƒใฎๅข—ๅผทใซใฏไปปๆ„ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใงใใพใ™ใ€‚็”ปๅƒใฎๅ‰ๅ‡ฆ็†ใซใฏใ€ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸ`ImageProcessor`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ™ใŸใ‚ใซใ€[food101](https://huggingface.co/datasets/food101)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค—[Datasetsใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰๏ผš + + + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒใ‹ใชใ‚Šๅคงใใ„ใŸใ‚ใ€๐Ÿค— Datasetsใฎ`split`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใฎๅฐใ•ใชใ‚ตใƒณใƒ—ใƒซใฎใฟใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผ + + + +```python +>>> from datasets import load_dataset + +>>> dataset = load_dataset("food101", split="train[:100]") +``` + +ๆฌกใซใ€๐Ÿค— Datasetsใฎ [`Image`](https://huggingface.co/docs/datasets/package_reference/main_classes?highlight=image#datasets.Image) ๆฉŸ่ƒฝใง็”ปๅƒใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> dataset[0]["image"] +``` + +
+ +
+ +AutoImageProcessorใ‚’[`AutoImageProcessor.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +``` + +1. ใพใšใ€็”ปๅƒใฎๆ‹กๅผตใ‚’่ฟฝๅŠ ใ—ใพใ—ใ‚‡ใ†ใ€‚ๅฅฝใใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใงใใพใ™ใŒใ€ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏtorchvisionใฎ[`transforms`](https://pytorch.org/vision/stable/transforms.html)ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅˆฅใฎใƒ‡ใƒผใ‚ฟๆ‹กๅผตใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€[Albumentations](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb)ใพใŸใฏ[Kornia notebooks](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb)ใง่ฉณ็ดฐใ‚’ๅญฆใถใ“ใจใŒใงใใพใ™ใ€‚ + + ใ“ใ“ใงใฏใ€[`Compose`](https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html)ใ‚’ไฝฟ็”จใ—ใฆใ„ใใคใ‹ใฎๅค‰ๆ›ใ‚’้€ฃ้Ž–ใ•ใ›ใพใ™ - [`RandomResizedCrop`](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html)ใจ[`ColorJitter`](https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html)ใ€‚ + ใ‚ตใ‚คใ‚บใฎๅค‰ๆ›ดใซ้–ขใ—ใฆใฏใ€`image_processor`ใ‹ใ‚‰็”ปๅƒใ‚ตใ‚คใ‚บใฎ่ฆไปถใ‚’ๅ–ๅพ—ใงใใพใ™ใ€‚ + ไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏใ€ๆญฃ็ขบใช้ซ˜ใ•ใจๅน…ใŒๅฟ…่ฆใงใ™ใŒใ€ไป–ใฎใƒขใƒ‡ใƒซใงใฏ`shortest_edge`ใฎใฟใŒๅฎš็พฉใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +```py +>>> from torchvision.transforms import RandomResizedCrop, ColorJitter, Compose + +>>> size = ( +... image_processor.size["shortest_edge"] +... if "shortest_edge" in image_processor.size +... else (image_processor.size["height"], image_processor.size["width"]) +... ) + +>>> _transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=0.5, hue=0.5)]) +``` + +2. ใƒขใƒ‡ใƒซใฏ[`pixel_values`](model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values)ใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใพใ™ใ€‚ +`ImageProcessor`ใฏ็”ปๅƒใฎๆญฃ่ฆๅŒ–ใจ้ฉๅˆ‡ใชใƒ†ใƒณใ‚ฝใƒซใฎ็”Ÿๆˆใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ +ไธ€้€ฃใฎ็”ปๅƒใซๅฏพใ™ใ‚‹็”ปๅƒๆ‹กๅผตใจ็”ปๅƒๅ‰ๅ‡ฆ็†ใ‚’็ต„ใฟๅˆใ‚ใ›ใ€`pixel_values`ใ‚’็”Ÿๆˆใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```python +>>> def transforms(examples): +... images = [_transforms(img.convert("RGB")) for img in examples["image"]] +... examples["pixel_values"] = image_processor(images, do_resize=False, return_tensors="pt")["pixel_values"] +... return examples +``` + + + +ไธŠ่จ˜ใฎไพ‹ใงใฏใ€็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’ๆ—ขใซ็”ปๅƒๅข—ๅผทๅค‰ๆ›ใง่กŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€`do_resize=False`ใ‚’่จญๅฎšใ—ใพใ—ใŸใ€‚ +้ฉๅˆ‡ใช `image_processor` ใ‹ใ‚‰ใฎ `size` ๅฑžๆ€งใ‚’ๆดป็”จใ—ใฆใ„ใพใ™ใ€‚็”ปๅƒๅข—ๅผทไธญใซ็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’่กŒใ‚ใชใ„ๅ ดๅˆใฏใ€ใ“ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’็œ็•ฅใ—ใฆใใ ใ•ใ„ใ€‚ +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€`ImageProcessor` ใŒใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +็”ปๅƒใ‚’ๅข—ๅผทๅค‰ๆ›ใฎไธ€้ƒจใจใ—ใฆๆญฃ่ฆๅŒ–ใ—ใŸใ„ๅ ดๅˆใฏใ€`image_processor.image_mean` ใจ `image_processor.image_std` ใฎๅ€คใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + +3. ๆฌกใซใ€๐Ÿค— Datasetsใฎ[`set_transform`](https://huggingface.co/docs/datasets/process#format-transform)ใ‚’ไฝฟ็”จใ—ใฆใ€ๅค‰ๆ›ใ‚’ใƒชใ‚ขใƒซใ‚ฟใ‚คใƒ ใง้ฉ็”จใ—ใพใ™๏ผš + +```python +>>> dataset.set_transform(transforms) +``` + +4. ็”ปๅƒใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใจใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใŒ `pixel_values` ใ‚’่ฟฝๅŠ ใ—ใŸใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใงๅ‡ฆ็†ๆธˆใฟใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใŒใงใใพใ™๏ผ + +```python +>>> dataset[0].keys() +``` + +ไปฅไธ‹ใฏใ€ๅค‰ๆ›ใŒ้ฉ็”จใ•ใ‚ŒใŸๅพŒใฎ็”ปๅƒใฎๅค–่ฆณใงใ™ใ€‚ ็”ปๅƒใฏใƒฉใƒณใƒ€ใƒ ใซๅˆ‡ใ‚ŠๆŠœใ‹ใ‚Œใ€ใใฎ่‰ฒใฎ็‰นๆ€งใ‚‚็•ฐใชใ‚Šใพใ™ใ€‚ + +```py +>>> import numpy as np +>>> import matplotlib.pyplot as plt + +>>> img = dataset[0]["pixel_values"] +>>> plt.imshow(img.permute(1, 2, 0)) +``` + +
+ +
+ + + +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ€ๆ„ๅ‘ณใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ใŠใ‚ˆใณใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใชใฉใฎใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€`ImageProcessor`ใฏ +ใƒใ‚นใƒˆๅ‡ฆ็†ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ใƒขใƒ‡ใƒซใฎ็”Ÿใฎๅ‡บๅŠ›ใ‚’ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚„ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒžใƒƒใƒ—ใชใฉใฎๆ„ๅ‘ณใฎใ‚ใ‚‹ไบˆๆธฌใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + + + +### Pad + +ไธ€้ƒจใฎๅ ดๅˆใ€ใŸใจใˆใฐใ€[DETR](./model_doc/detr)ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚ใซใ‚นใ‚ฑใƒผใƒซใฎๅค‰ๆ›ดใ‚’้ฉ็”จใ—ใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒใƒƒใƒๅ†…ใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บใŒ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚[`DetrImageProcessor`]ใ‹ใ‚‰[`DetrImageProcessor.pad`]ใ‚’ไฝฟ็”จใ—ใ€ +ใ‚ซใ‚นใ‚ฟใƒ ใฎ`collate_fn`ใ‚’ๅฎš็พฉใ—ใฆ็”ปๅƒใ‚’ไธ€็ท’ใซใƒใƒƒใƒๅ‡ฆ็†ใงใใพใ™ใ€‚ + +```py +>>> def collate_fn(batch): +... pixel_values = [item["pixel_values"] for item in batch] +... encoding = image_processor.pad(pixel_values, return_tensors="pt") +... labels = [item["labels"] for item in batch] +... batch = {} +... batch["pixel_values"] = encoding["pixel_values"] +... batch["pixel_mask"] = encoding["pixel_mask"] +... batch["labels"] = labels +... return batch +``` + +## Multi Modal + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซ็”จใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใฎ[ใƒ—ใƒญใ‚ปใƒƒใ‚ต](main_classes/processors)ใŒๅฟ…่ฆใงใ™ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚„็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใชใฉใฎ2ใคใฎๅ‡ฆ็†ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’็ตๅˆใ—ใพใ™ใ€‚ + +่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผˆASR๏ผ‰ใฎใŸใ‚ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎไฝฟ็”จๆ–นๆณ•ใ‚’็คบใ™ใŸใ‚ใซใ€[LJ Speech](https://huggingface.co/datasets/lj_speech)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒญใƒผใƒ‰ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค— [Datasets ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://huggingface.co/docs/datasets/load_hub)ใ‚’ๅ‚็…ง๏ผ‰๏ผš + +```python +>>> from datasets import load_dataset + +>>> lj_speech = load_dataset("lj_speech", split="train") +``` + +ASR๏ผˆ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผ‰ใฎๅ ดๅˆใ€ไธปใซ `audio` ใจ `text` ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใ‚‹ใŸใ‚ใ€ไป–ใฎๅˆ—ใ‚’ๅ‰Š้™คใงใใพใ™๏ผš + +```python +>>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"]) +``` + +ๆฌกใซใ€`audio`ใจ`text`ใฎๅˆ—ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> lj_speech[0]["audio"] +{'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., + 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', + 'sampling_rate': 22050} + +>>> lj_speech[0]["text"] +'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition' +``` + +ๅธธใซใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใ‚’ใ€ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ๅญฆ็ฟ’ใซไฝฟ็”จใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใจไธ€่‡ดใ•ใ›ใ‚‹ใ‚ˆใ†ใซ[ใƒชใ‚ตใƒณใƒ—ใƒซ](preprocessing#audio)ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผ + +```py +>>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000)) +``` + +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ [`AutoProcessor.from_pretrained`] ใ‚’ไฝฟ็”จใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h") +``` + +1. `array`ๅ†…ใซๅซใพใ‚Œใ‚‹ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚’`input_values`ใซๅ‡ฆ็†ใ—ใ€`text`ใ‚’`labels`ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```py +>>> def prepare_dataset(example): +... audio = example["audio"] + +... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) + +... return example +``` + +2. ใ‚ตใƒณใƒ—ใƒซใซ`prepare_dataset`้–ขๆ•ฐใ‚’้ฉ็”จใ—ใพใ™๏ผš + +```py +>>> prepare_dataset(lj_speech[0]) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..0eb00cf220b54ab6c9f407564d5f1053461cdf51 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/quicktour.md @@ -0,0 +1,588 @@ + + +# Quick tour + +[[open-in-colab]] + +๐Ÿค— Transformersใ‚’ไฝฟใ„ๅง‹ใ‚ใพใ—ใ‚‡ใ†๏ผ ้–‹็™บ่€…ใงใ‚ใ‚ใ†ใจใ€ๆ—ฅๅธธ็š„ใชใƒฆใƒผใ‚ถใƒผใงใ‚ใ‚ใ†ใจใ€ใ“ใฎใ‚ฏใ‚คใƒƒใ‚ฏใƒ„ใ‚ขใƒผใฏ +ๅˆใ‚ใฆๅง‹ใ‚ใ‚‹ใฎใ‚’ๆ”ฏๆดใ—ใ€[`pipeline`]ใ‚’ไฝฟใฃใŸๆŽจ่ซ–ๆ–นๆณ•ใ€[AutoClass](./model_doc/auto)ใงไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใจใƒ—ใƒชใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ€ +ใใ—ใฆPyTorchใพใŸใฏTensorFlowใง็ด ๆ—ฉใใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ ๅˆๅฟƒ่€…ใฎๅ ดๅˆใ€ใ“ใ“ใง็ดนไป‹ใ•ใ‚ŒใŸใ‚ณใƒณใ‚ปใƒ—ใƒˆใฎ่ฉณ็ดฐใช่ชฌๆ˜Žใ‚’ๆไพ›ใ™ใ‚‹ +ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใพใŸใฏ[ใ‚ณใƒผใ‚น](https://huggingface.co/course/chapter1/1)ใ‚’ๆฌกใซๅ‚็…งใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผš + +```bash +!pip install transformers datasets evaluate accelerate +``` + +ใ‚ใชใŸใฏใพใŸใ€ๅฅฝใใชๆฉŸๆขฐๅญฆ็ฟ’ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™: + + + + +```bash +pip install torch +``` + + + +```bash +pip install tensorflow +``` + + + +## Pipeline + + + +[`pipeline`] ใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซๆœ€ใ‚‚็ฐกๅ˜ใง้ซ˜้€Ÿใชๆ–นๆณ•ใงใ™ใ€‚ +[`pipeline`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ€ใ•ใพใ–ใพใชใƒขใƒ€ใƒชใƒ†ใ‚ฃใซใ‚ใŸใ‚‹ๅคšใใฎใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆๅณๅบงใซไฝฟ็”จใงใใพใ™ใ€‚ +ใ„ใใคใ‹ใฎใ‚ฟใ‚นใ‚ฏใฏไปฅไธ‹ใฎ่กจใซ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš + + + +ไฝฟ็”จๅฏ่ƒฝใชใ‚ฟใ‚นใ‚ฏใฎๅฎŒๅ…จใชไธ€่ฆงใซใคใ„ใฆใฏใ€[pipeline API ใƒชใƒ•ใ‚กใƒฌใƒณใ‚น](./main_classes/pipelines)ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +| **ใ‚ฟใ‚นใ‚ฏ** | **่ชฌๆ˜Ž** | **ใƒขใƒ€ใƒชใƒ†ใ‚ฃ** | **ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ่ญ˜ๅˆฅๅญ** | +|------------------------------|--------------------------------------------------------------------------------------------------------------|-----------------|-----------------------------------------------| +| ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กž | ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ | NLP | pipeline(task="sentiment-analysis") | +| ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ | ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆŒ‡ๅฎšใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ | NLP | pipeline(task="text-generation") | +| ่ฆ็ด„ | ใƒ†ใ‚ญใ‚นใƒˆใพใŸใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎ่ฆ็ด„ใ‚’็”Ÿๆˆใ™ใ‚‹ | NLP | pipeline(task="summarization") | +| ็”ปๅƒๅˆ†้กž | ็”ปๅƒใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ | ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณ | pipeline(task="image-classification") | +| ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ | ็”ปๅƒใฎๅ„ๅ€‹ๅˆฅใฎใƒ”ใ‚ฏใ‚ปใƒซใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹๏ผˆใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ€ใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใ€ใŠใ‚ˆใณใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ใ‚ตใƒใƒผใƒˆ๏ผ‰ | ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณ | pipeline(task="image-segmentation") | +| ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บ | ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใจใ‚ฏใƒฉใ‚นใ‚’ไบˆๆธฌใ™ใ‚‹ | ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณ | pipeline(task="object-detection") | +| ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กž | ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ | ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช | pipeline(task="audio-classification") | +| ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ | ้Ÿณๅฃฐใ‚’ใƒ†ใ‚ญใ‚นใƒˆใซๅค‰ๆ›ใ™ใ‚‹ | ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช | pipeline(task="automatic-speech-recognition") | +| ใƒ“ใ‚ธใƒฅใ‚ขใƒซใ‚ฏใ‚จใ‚นใƒใƒงใƒณๅฟœ็ญ” | ็”ปๅƒใจ่ณชๅ•ใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใซใ€็”ปๅƒใซ้–ขใ™ใ‚‹่ณชๅ•ใซๅ›ž็ญ”ใ™ใ‚‹ | ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ | pipeline(task="vqa") | +| ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚ฏใ‚จใ‚นใƒใƒงใƒณๅฟœ็ญ” | ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใจ่ณชๅ•ใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใซใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซ้–ขใ™ใ‚‹่ณชๅ•ใซๅ›ž็ญ”ใ™ใ‚‹ | ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ | pipeline(task="document-question-answering") | +| ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒ‹ใƒณใ‚ฐ | ไธŽใˆใ‚‰ใ‚ŒใŸ็”ปๅƒใซใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใ™ใ‚‹ | ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ | pipeline(task="image-to-text") | + +ใพใšใ€[`pipeline`] ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใ€ไฝฟ็”จใ—ใŸใ„ใ‚ฟใ‚นใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ใ‚ปใƒณใƒใƒกใƒณใƒˆๅˆ†ๆžใฎใŸใ‚ใซ [`pipeline`] ใ‚’ไฝฟ็”จใ™ใ‚‹ไพ‹ใ‚’็คบใ—ใพใ™๏ผš + +```python +>>> from transformers import pipeline + +>>> classifier = pipeline("sentiment-analysis") +``` + +[`pipeline`]ใฏใ€ๆ„Ÿๆƒ…ๅˆ†ๆžใฎใŸใ‚ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ[ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซ](https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english)ใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆใ‚ญใƒฃใƒƒใ‚ทใƒฅใ—ใ€ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใงใ€`classifier`ใ‚’ๅฏพ่ฑกใฎใƒ†ใ‚ญใ‚นใƒˆใซไฝฟ็”จใงใใพใ™๏ผš + +```python +>>> classifier("็งใŸใกใฏ๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใŠ่ฆ‹ใ›ใงใใฆใจใฆใ‚‚ๅฌ‰ใ—ใ„ใงใ™ใ€‚") +[{'label': 'POSITIVE', 'score': 0.9998}] +``` + +่ค‡ๆ•ฐใฎๅ…ฅๅŠ›ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[`pipeline`]ใซๅ…ฅๅŠ›ใ‚’ใƒชใ‚นใƒˆใจใ—ใฆๆธกใ—ใฆใ€่พžๆ›ธใฎใƒชใ‚นใƒˆใ‚’่ฟ”ใ—ใพใ™๏ผš + +```py +>>> results = classifier(["๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ”็ดนไป‹ใงใใฆ้žๅธธใซๅฌ‰ใ—ใ„ใงใ™ใ€‚", "ๅซŒใ„ใซใชใ‚‰ใชใ„ใงใปใ—ใ„ใงใ™ใ€‚"]) +>>> for result in results: +... print(f"label: {result['label']}, ใ‚นใ‚ณใ‚ข: {round(result['score'], 4)}") +label: POSITIVE, ใ‚นใ‚ณใ‚ข: 0.9998 +label: NEGATIVE, ใ‚นใ‚ณใ‚ข: 0.5309 +``` + +[`pipeline`]ใฏใ€ไปปๆ„ใฎใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’็นฐใ‚Š่ฟ”ใ—ๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใฎไพ‹ใงใฏใ€่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ‚’ใ‚ฟใ‚นใ‚ฏใจใ—ใฆ้ธใณใพใ—ใ‚‡ใ†๏ผš + +```python +>>> import torch +>>> from transformers import pipeline + +>>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h") +``` + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆ่ฉณ็ดฐใซใคใ„ใฆใฏ๐Ÿค— Datasets [ใ‚ฏใ‚คใƒƒใ‚ฏใ‚นใ‚ฟใƒผใƒˆ](https://huggingface.co/docs/datasets/quickstart#audio)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚ +ใŸใจใˆใฐใ€[MInDS-14](https://huggingface.co/datasets/PolyAI/minds14)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```python +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒ[`facebook/wav2vec2-base-960h`](https://huggingface.co/facebook/wav2vec2-base-960h)ใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใจไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผš + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate)) +``` + +"audio"ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ™ใจใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใ‚กใ‚คใƒซใฏ่‡ชๅ‹•็š„ใซใƒญใƒผใƒ‰ใ•ใ‚Œใ€ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ๆœ€ๅˆใฎ4ใคใฎใ‚ตใƒณใƒ—ใƒซใ‹ใ‚‰็”Ÿใฎๆณขๅฝข้…ๅˆ—ใ‚’ๆŠฝๅ‡บใ—ใ€ใใ‚Œใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซใƒชใ‚นใƒˆใจใ—ใฆๆธกใ—ใพใ™ใ€‚ + +```py +>>> result = speech_recognizer(dataset[:4]["audio"]) +>>> print([d["text"] for d in result]) +['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FONDERING HOW I'D SET UP A JOIN TO HELL T WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE APSO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AN I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I FURN A JOINA COUT'] +``` + +ๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ€ๅ…ฅๅŠ›ใŒๅคงใใ„ๅ ดๅˆ๏ผˆ้Ÿณๅฃฐใ‚„็”ปๅƒใชใฉ๏ผ‰ใ€ใ™ในใฆใฎๅ…ฅๅŠ›ใ‚’ใƒกใƒขใƒชใซ่ชญใฟ่พผใ‚€ไปฃใ‚ใ‚Šใซใ€ใƒชใ‚นใƒˆใงใฏใชใใ‚ธใ‚งใƒใƒฌใƒผใ‚ฟใ‚’ๆธกใ™ใ“ใจใŒใŠๅ‹งใ‚ใงใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏ[ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณAPIใƒชใƒ•ใ‚กใƒฌใƒณใ‚น](./main_classes/pipelines)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### Use another model and tokenizer in the pipeline + +[`pipeline`]ใฏ[Hub](https://huggingface.co/models)ใ‹ใ‚‰ใฎไปปๆ„ใฎใƒขใƒ‡ใƒซใ‚’ๅŽๅฎนใงใใ€ไป–ใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ[`pipeline`]ใ‚’้ฉๅฟœใ•ใ›ใ‚‹ใ“ใจใŒๅฎนๆ˜“ใงใ™ใ€‚ใŸใจใˆใฐใ€ใƒ•ใƒฉใƒณใ‚น่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใงใใ‚‹ใƒขใƒ‡ใƒซใŒๅฟ…่ฆใชๅ ดๅˆใ€Hubใฎใ‚ฟใ‚ฐใ‚’ไฝฟ็”จใ—ใฆ้ฉๅˆ‡ใชใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚ฃใƒซใ‚ฟใƒชใƒณใ‚ฐใงใใพใ™ใ€‚ใƒˆใƒƒใƒ—ใฎใƒ•ใ‚ฃใƒซใ‚ฟใƒชใƒณใ‚ฐใ•ใ‚ŒใŸ็ตๆžœใฏใ€ใƒ•ใƒฉใƒณใ‚น่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใซไฝฟ็”จใงใใ‚‹ๆ„Ÿๆƒ…ๅˆ†ๆž็”จใซ่ชฟๆ•ดใ•ใ‚ŒใŸๅคš่จ€่ชžใฎ[BERTใƒขใƒ‡ใƒซ](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment)ใ‚’่ฟ”ใ—ใพใ™๏ผš + +```py +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +``` + + + +[`AutoModelForSequenceClassification`]ใจ[`AutoTokenizer`]ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใจใใ‚Œใซ้–ข้€ฃใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผˆๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง`AutoClass`ใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™๏ผ‰๏ผš + +```python +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + +ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใฏใ€[`TFAutoModelForSequenceClassification`]ใŠใ‚ˆใณ[`AutoTokenizer`]ใ‚’ไฝฟ็”จใ—ใฆใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใจใใฎ้–ข้€ฃใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™๏ผˆ`TFAutoClass`ใซใคใ„ใฆใฏๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™๏ผ‰๏ผš + +```python +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + +ๆŒ‡ๅฎšใ—ใŸใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’[`pipeline`]ใซ่จญๅฎšใ—ใ€ไปŠๅบฆใฏใƒ•ใƒฉใƒณใ‚น่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใซ`classifier`ใ‚’้ฉ็”จใงใใพใ™๏ผš + +```py +>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) +>>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") +[{'label': '5 stars', 'score': 0.7273}] +``` + +ใ‚‚ใ—ใ€ใ‚ใชใŸใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉใ—ใŸใƒขใƒ‡ใƒซใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ๅ ดๅˆใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใ‚ใชใŸใฎใƒ‡ใƒผใ‚ฟใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใฎๆ–นๆณ•ใซใคใ„ใฆใฏใ€[ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](./training)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ +ๆœ€ๅพŒใซใ€ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใŸไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ—ใ€ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใƒใƒ–ใงๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ๆคœ่จŽใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆฉŸๆขฐๅญฆ็ฟ’ใ‚’ๆฐ‘ไธปๅŒ–ใ™ใ‚‹ๆ‰‹ๅŠฉใ‘ใŒใงใใพใ™๏ผ ๐Ÿค— + +## AutoClass + + + +[`AutoModelForSequenceClassification`] ใŠใ‚ˆใณ [`AutoTokenizer`] ใ‚ฏใƒฉใ‚นใฏใ€ไธŠ่จ˜ใงไฝฟ็”จใ—ใŸ [`pipeline`] ใ‚’้ง†ๅ‹•ใ™ใ‚‹ใŸใ‚ใซๅ”ๅŠ›ใ—ใฆๅ‹•ไฝœใ—ใพใ™ใ€‚ +[AutoClass](./model_doc/auto) ใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ใใฎๅๅ‰ใพใŸใฏใƒ‘ใ‚นใ‹ใ‚‰่‡ชๅ‹•็š„ใซๅ–ๅพ—ใ™ใ‚‹ใ‚ทใƒงใƒผใƒˆใ‚ซใƒƒใƒˆใงใ™ใ€‚ +้ฉๅˆ‡ใช `AutoClass` ใ‚’้ธๆŠžใ—ใ€ใใ‚Œใซ้–ข้€ฃใ™ใ‚‹ๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’้ธๆŠžใ™ใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™ใ€‚ + +ๅ‰ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‹ใ‚‰ใฎไพ‹ใซๆˆปใ‚Šใ€`AutoClass` ใ‚’ไฝฟ็”จใ—ใฆ [`pipeline`] ใฎ็ตๆžœใ‚’ๅ†็พใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +### AutoTokenizer + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใงใใ‚‹ๆ•ฐๅ€คใฎ้…ๅˆ—ใซๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฝนๅ‰ฒใ‚’ๆžœใŸใ—ใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใƒ—ใƒญใ‚ปใ‚นใซใฏใ€ๅ˜่ชžใ‚’ใฉใฎใ‚ˆใ†ใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ‹ใ‚„ใ€ๅ˜่ชžใ‚’ใฉใฎใƒฌใƒ™ใƒซใงๅˆ†ๅ‰ฒใ™ใ‚‹ใ‹ใจใ„ใฃใŸๅคšใใฎใƒซใƒผใƒซใŒใ‚ใ‚Šใพใ™ +๏ผˆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใซใคใ„ใฆใฎ่ฉณ็ดฐใฏ [ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚ตใƒžใƒชใƒผ](./tokenizer_summary) ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผ‰ใ€‚ +ๆœ€ใ‚‚้‡่ฆใชใ“ใจใฏใ€ใƒขใƒ‡ใƒซใŒไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใซใชใฃใŸใจใใจๅŒใ˜ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใƒซใƒผใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซใ€ๅŒใ˜ใƒขใƒ‡ใƒซๅใงใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚ + +[`AutoTokenizer`] ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```python +>>> from transformers import AutoTokenizer + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + +Pass your text to the tokenizer: + +```python +>>> encoding = tokenizer("็งใŸใกใฏ๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใŠ่ฆ‹ใ›ใงใใฆใจใฆใ‚‚ๅฌ‰ใ—ใ„ใงใ™ใ€‚") +>>> print(encoding) +{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ๆฌกใฎๆƒ…ๅ ฑใ‚’ๅซใ‚€่พžๆ›ธใ‚’่ฟ”ใ—ใพใ™๏ผš + +- [input_ids](./glossary#input-ids): ใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐๅ€ค่กจ็พใ€‚ +- [attention_mask](.glossary#attention-mask): ใฉใฎใƒˆใƒผใ‚ฏใƒณใซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ๅ‘ใ‘ใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใพใŸใ€ๅ…ฅๅŠ›ใฎใƒชใ‚นใƒˆใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ไธ€ๆง˜ใช้•ทใ•ใฎใƒใƒƒใƒใ‚’่ฟ”ใ™ใŸใ‚ใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใŠใ‚ˆใณๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + + + +```py +>>> pt_batch = tokenizer( +... ["๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใŠ่ฆ‹ใ›ใงใใฆ้žๅธธใซๅฌ‰ใ—ใ„ใงใ™ใ€‚", "ๅซŒใ„ใงใฏใชใ„ใ“ใจใ‚’้ก˜ใฃใฆใ„ใพใ™ใ€‚"], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="pt", +... ) +``` + + + +```py +>>> tf_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="tf", +... ) +``` + + + + + +[ๅ‰ๅ‡ฆ็†](./preprocessing)ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใ„ใŸใ ใใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใฎ่ฉณ็ดฐใ‚„ใ€[`AutoImageProcessor`]ใ€[`AutoFeatureExtractor`]ใ€[`AutoProcessor`]ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ€ใŠใ‚ˆใณใƒžใƒซใƒใƒขใƒผใƒ€ใƒซๅ…ฅๅŠ›ใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใƒšใƒผใ‚ธใ‚‚ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +### AutoModel + + + +๐Ÿค— Transformersใฏไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’็ฐกๅ˜ใซ็ตฑไธ€็š„ใซใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€[`AutoTokenizer`]ใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใฎใจๅŒใ˜ใ‚ˆใ†ใซ[`AutoModel`]ใ‚’ใƒญใƒผใƒ‰ใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใŸ[`AutoModel`]ใ‚’้ธๆŠžใ™ใ‚‹ไปฅๅค–ใฎ้•ใ„ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆ๏ผˆใพใŸใฏใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผ‰ๅˆ†้กžใฎๅ ดๅˆใ€[`AutoModelForSequenceClassification`]ใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +[`AutoModel`]ใ‚ฏใƒฉใ‚นใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ‚ฟใ‚นใ‚ฏใฎๆฆ‚่ฆ](./task_summary)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ไปŠใ€ๅ‰ๅ‡ฆ็†ๆธˆใฟใฎใƒใƒƒใƒใ‚’็›ดๆŽฅใƒขใƒ‡ใƒซใซๆธกใ—ใพใ™ใ€‚่พžๆ›ธใ‚’ๅฑ•้–‹ใ™ใ‚‹ใ ใ‘ใงใ€`**`ใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```python +>>> pt_outputs = pt_model(**pt_batch) +``` + +ใƒขใƒ‡ใƒซใฏใ€`logits`ๅฑžๆ€งใซๆœ€็ต‚็š„ใชใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ `logits`ใซsoftmax้–ขๆ•ฐใ‚’้ฉ็”จใ—ใฆ็ขบ็އใ‚’ๅ–ๅพ—ใ—ใพใ™๏ผš + +```py +>>> from torch import nn + +>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) +>>> print(pt_predictions) +tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], + [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=) +``` + + + +๐Ÿค— Transformersใฏไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใฎใ‚ทใƒณใƒ—ใƒซใง็ตฑไธ€ใ•ใ‚ŒใŸๆ–นๆณ•ใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +ใ“ใ‚Œใฏใ€[`TFAutoModel`]ใ‚’[`AutoTokenizer`]ใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใฎใจๅŒใ˜ใ‚ˆใ†ใซใƒญใƒผใƒ‰ใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ๅ”ฏไธ€ใฎ้•ใ„ใฏใ€ใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใŸ[`TFAutoModel`]ใ‚’้ธๆŠžใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆ๏ผˆใพใŸใฏใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผ‰ๅˆ†้กžใฎๅ ดๅˆใ€[`TFAutoModelForSequenceClassification`]ใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +่ฉณ็ดฐใซใคใ„ใฆใฏใ€[`AutoModel`]ใ‚ฏใƒฉใ‚นใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑใฏใ€[ใ‚ฟใ‚นใ‚ฏใฎๆฆ‚่ฆ](./task_summary)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ๆฌกใซใ€ๅ‰ๅ‡ฆ็†ๆธˆใฟใฎใƒใƒƒใƒใ‚’็›ดๆŽฅใƒขใƒ‡ใƒซใซๆธกใ—ใพใ™ใ€‚ใƒ†ใƒณใ‚ฝใƒซใ‚’ใใฎใพใพๆธกใ™ใ“ใจใŒใงใใพใ™๏ผš + +```python +>>> tf_outputs = tf_model(tf_batch) +``` + +ใƒขใƒ‡ใƒซใฏ`logits`ๅฑžๆ€งใซๆœ€็ต‚็š„ใชใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚`logits`ใซใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚น้–ขๆ•ฐใ‚’้ฉ็”จใ—ใฆ็ขบ็އใ‚’ๅ–ๅพ—ใ—ใพใ™๏ผš + +```python +>>> import tensorflow as tf + +>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) +>>> tf_predictions # doctest: +IGNORE_RESULT +``` + + + + + + +๐Ÿค— Transformersใฎใ™ในใฆใฎใƒขใƒ‡ใƒซ๏ผˆPyTorchใพใŸใฏTensorFlow๏ผ‰ใฏใ€ๆœ€็ต‚็š„ใชๆดปๆ€งๅŒ–้–ขๆ•ฐ๏ผˆsoftmaxใชใฉ๏ผ‰*ๅ‰*ใฎใƒ†ใƒณใ‚ฝใƒซใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ +ๆœ€็ต‚็š„ใชๆดปๆ€งๅŒ–้–ขๆ•ฐใฏใ€ใ—ใฐใ—ใฐๆๅคฑใจ็ตๅˆใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใงใ™ใ€‚ใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใฏ็‰นๅˆฅใชใƒ‡ใƒผใ‚ฟใ‚ฏใƒฉใ‚นใงใ‚ใ‚Šใ€ใใฎๅฑžๆ€งใฏIDEใง่‡ชๅ‹•่ฃœๅฎŒใ•ใ‚Œใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใฏใ€ใ‚ฟใƒ—ใƒซใพใŸใฏ่พžๆ›ธใฎใ‚ˆใ†ใซๅ‹•ไฝœใ—ใพใ™๏ผˆๆ•ดๆ•ฐใ€ใ‚นใƒฉใ‚คใ‚นใ€ใพใŸใฏๆ–‡ๅญ—ๅˆ—ใงใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ‚’ไป˜ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™๏ผ‰ใ€‚ +ใ“ใฎๅ ดๅˆใ€Noneใงใ‚ใ‚‹ๅฑžๆ€งใฏ็„ก่ฆ–ใ•ใ‚Œใพใ™ใ€‚ + + + +### Save a Model + + + +ใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใŸใ‚‰ใ€[`PreTrainedModel.save_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใจๅ…ฑใซไฟๅญ˜ใงใใพใ™๏ผš + +```py +>>> pt_save_directory = "./pt_save_pretrained" +>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT +>>> pt_model.save_pretrained(pt_save_directory) +``` + +ๅ†ใณใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๆบ–ๅ‚™ใŒใงใใŸใ‚‰ใ€[`PreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆๅ†ๅบฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") +``` + + + +ใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใŸใ‚‰ใ€ใใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ไฟๅญ˜ใงใใพใ™ใ€‚[`TFPreTrainedModel.save_pretrained`]ใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + +```py +>>> tf_save_directory = "./tf_save_pretrained" +>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT +>>> tf_model.save_pretrained(tf_save_directory) +``` + +ใƒขใƒ‡ใƒซใ‚’ๅ†ๅบฆไฝฟ็”จใ™ใ‚‹ๆบ–ๅ‚™ใŒใงใใŸใ‚‰ใ€[`TFPreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆๅ†ๅบฆใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + +```py +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") +``` + + + + +๐Ÿค— Transformersใฎ็‰นใซ็ด ๆ™ดใ‚‰ใ—ใ„ๆฉŸ่ƒฝใฎไธ€ใคใฏใ€ใƒขใƒ‡ใƒซใ‚’ไฟๅญ˜ใ—ใ€ใใ‚Œใ‚’PyTorchใƒขใƒ‡ใƒซใพใŸใฏTensorFlowใƒขใƒ‡ใƒซใจใ—ใฆๅ†ใƒญใƒผใƒ‰ใงใใ‚‹ใ“ใจใงใ™ใ€‚ `from_pt`ใพใŸใฏ`from_tf`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ้–“ใงๅค‰ๆ›ใงใใพใ™๏ผš + + + + +```py +>>> from transformers import AutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) +``` + + + + +```py +>>> from transformers import TFAutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) +``` + + + +## Custom model builds + +ใƒขใƒ‡ใƒซใ‚’ๆง‹็ฏ‰ๆ–นๆณ•ใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใซใฏใ€ใƒขใƒ‡ใƒซใฎ่จญๅฎšใ‚ฏใƒฉใ‚นใ‚’ๅค‰ๆ›ดใงใใพใ™ใ€‚่จญๅฎšใฏใƒขใƒ‡ใƒซใฎๅฑžๆ€งใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€้š ใ‚Œๅฑคใฎๆ•ฐใ‚„ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒ˜ใƒƒใƒ‰ใฎๆ•ฐใชใฉใŒใ“ใ‚Œใซๅซใพใ‚Œใพใ™ใ€‚ใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšใ‚ฏใƒฉใ‚นใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹้š›ใซใฏใ€ใ‚ผใƒญใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎๅฑžๆ€งใฏใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚Œใ€ๆœ‰ๆ„็พฉใช็ตๆžœใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€ๅˆใซ[`AutoConfig`]ใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใ€ๅค‰ๆ›ดใ—ใŸใ„ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚[`AutoConfig.from_pretrained`]ๅ†…ใงใ€ๅค‰ๆ›ดใ—ใŸใ„ๅฑžๆ€ง๏ผˆไพ‹๏ผšใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒ˜ใƒƒใƒ‰ใฎๆ•ฐ๏ผ‰ใ‚’ๆŒ‡ๅฎšใงใใพใ™๏ผš + +```python +>>> from transformers import AutoConfig + +>>> my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12) +``` + + + +[`AutoModel.from_config`]ใ‚’ไฝฟ็”จใ—ใฆใ‚ซใ‚นใ‚ฟใƒ ่จญๅฎšใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```python +>>> from transformers import AutoModel + +>>> my_model = AutoModel.from_config(my_config) +``` + + + +ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใซใฏใ€[`TFAutoModel.from_config`]ใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + +```py +>>> from transformers import TFAutoModel + +>>> my_model = TFAutoModel.from_config(my_config) +``` + + + + +[ใ‚ซใ‚นใ‚ฟใƒ ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’ไฝœๆˆ](./create_a_model)ใ‚ฌใ‚คใƒ‰ใ‚’ๅ‚็…งใ—ใฆใ€ใ‚ซใ‚นใ‚ฟใƒ ๆง‹ๆˆใฎ่ฉณ็ดฐๆƒ…ๅ ฑใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +## Trainer - PyTorchๅ‘ใ‘ใฎๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ— + +ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏๆจ™ๆบ–ใฎ[`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)ใงใ‚ใ‚‹ใŸใ‚ใ€้€šๅธธใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใงไฝฟ็”จใงใใพใ™ใ€‚ +็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ไฝœๆˆใงใใพใ™ใŒใ€๐Ÿค— TransformersใฏPyTorchๅ‘ใ‘ใซ[`Trainer`]ใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใฆใŠใ‚Šใ€ๅŸบๆœฌ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใซๅŠ ใˆใฆใ€ +ๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€ๆททๅˆ็ฒพๅบฆใชใฉใฎๆฉŸ่ƒฝใฎ่ฟฝๅŠ ใ‚’่กŒใฃใฆใ„ใพใ™ใ€‚ + +ใ‚ฟใ‚นใ‚ฏใซๅฟœใ˜ใฆใ€้€šๅธธใฏ[`Trainer`]ใซไปฅไธ‹ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆธกใ—ใพใ™๏ผš + +1. [`PreTrainedModel`]ใพใŸใฏ[`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)ใ‹ใ‚‰ๅง‹ใ‚ใพใ™๏ผš + + ```py + >>> from transformers import AutoModelForSequenceClassification + + >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. [`TrainingArguments`]ใซใฏใ€ๅค‰ๆ›ดใงใใ‚‹ใƒขใƒ‡ใƒซใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ๅญฆ็ฟ’็އใ€ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚จใƒใƒƒใ‚ฏๆ•ฐใชใฉใŒๅค‰ๆ›ดใงใใพใ™ใ€‚ๆŒ‡ๅฎšใ—ใชใ„ๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใŒไฝฟ็”จใ•ใ‚Œใพใ™๏ผš + + ```py + >>> from transformers import TrainingArguments + + >>> training_args = TrainingArguments( + ... output_dir="path/to/save/folder/", + ... learning_rate=2e-5, + ... per_device_train_batch_size=8, + ... per_device_eval_batch_size=8, + ... num_train_epochs=2, + ... ) + ``` + +3. ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ€ใพใŸใฏใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใ‚ˆใ†ใชๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +4. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹: + + ```py + >>> from datasets import load_dataset + + >>> dataset = load_dataset("rotten_tomatoes") # doctest: +IGNORE_RESULT + ``` + +5. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใŸใ‚ใฎ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš + + ```python + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) + ``` + + ใใฎๅพŒใ€[`~datasets.Dataset.map`]ใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ฉ็”จใ—ใพใ™๏ผš + + ```python + >>> dataset = dataset.map(tokenize_dataset, batched=True) + ``` + +6. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ใฎไพ‹ใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใฎ [`DataCollatorWithPadding`]๏ผš + + ```py + >>> from transformers import DataCollatorWithPadding + + >>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer) + ``` + +ๆฌกใซใ€ใ“ใ‚Œใ‚‰ใฎใ‚ฏใƒฉใ‚นใ‚’[`Trainer`]ใซใพใจใ‚ใพใ™๏ผš + +```python +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=dataset["train"], +... eval_dataset=dataset["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) # doctest: +SKIP +``` + +่จ“็ทดใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒใงใใŸใ‚‰ใ€[`~Trainer.train`]ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใพใ™๏ผš + +```py +>>> trainer.train() # doctest: +SKIP +``` + + + +็ฟป่จณใ‚„่ฆ็ด„ใชใฉใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใซใฏใ€ไปฃใ‚ใ‚Šใซ[`Seq2SeqTrainer`]ใจ[`Seq2SeqTrainingArguments`]ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + + + +[`Trainer`]ๅ†…ใฎใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ™ใ‚‹ใ“ใจใงใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใฎๅ‹•ไฝœใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆๅคฑ้–ขๆ•ฐใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ€ใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใชใฉใฎๆฉŸ่ƒฝใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใงใใพใ™ใ€‚ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใงใใ‚‹ใƒกใ‚ฝใƒƒใƒ‰ใฎไธ€่ฆงใซใคใ„ใฆใฏใ€[`Trainer`]ใƒชใƒ•ใ‚กใƒฌใƒณใ‚นใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ™ใ‚‹ๅˆฅใฎๆ–นๆณ•ใฏใ€[Callbacks](./main_classes/callback)ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใฆไป–ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใจ็ตฑๅˆใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’็›ฃ่ฆ–ใ—ใฆ้€ฒๆ—็Šถๆณใ‚’ๅ ฑๅ‘Šใ—ใŸใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆ—ฉๆœŸใซๅœๆญขใ—ใŸใ‚Šใงใใพใ™ใ€‚ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—่‡ชไฝ“ใซใฏไฝ•ใ‚‚ๅค‰ๆ›ดใ‚’ๅŠ ใˆใพใ›ใ‚“ใ€‚ๆๅคฑ้–ขๆ•ฐใชใฉใฎใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใ‚’่กŒใ†ๅ ดๅˆใฏใ€[`Trainer`]ใ‚’ใ‚ตใƒ–ใ‚ฏใƒฉใ‚นๅŒ–ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Train with TensorFlow + +ใ™ในใฆใฎใƒขใƒ‡ใƒซใฏๆจ™ๆบ–ใฎ[`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)ใงใ‚ใ‚‹ใŸใ‚ใ€[Keras](https://keras.io/) APIใ‚’ไฝฟ็”จใ—ใฆTensorFlowใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใพใ™ใ€‚ +๐Ÿค— Transformersใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’`tf.data.Dataset`ใจใ—ใฆ็ฐกๅ˜ใซใƒญใƒผใƒ‰ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹[`~TFPreTrainedModel.prepare_tf_dataset`]ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆไพ›ใ—ใฆใŠใ‚Šใ€Kerasใฎ[`compile`](https://keras.io/api/models/model_training_apis/#compile-method)ใŠใ‚ˆใณ[`fit`](https://keras.io/api/models/model_training_apis/#fit-method)ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ™ใใซ้–‹ๅง‹ใงใใพใ™ใ€‚ + +1. [`TFPreTrainedModel`]ใพใŸใฏ[`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)ใ‹ใ‚‰ๅง‹ใ‚ใพใ™๏ผš + + ```py + >>> from transformers import TFAutoModelForSequenceClassification + + >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€็‰นๅพด้‡ๆŠฝๅ‡บๅ™จใ€ใพใŸใฏใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใ‚ˆใ†ใชๅ‰ๅ‡ฆ็†ใ‚ฏใƒฉใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™๏ผš + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +3. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ™ใ‚‹ใŸใ‚ใฎ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™๏ผš + + ```python + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) # doctest: +SKIP + ``` + +4. [`~datasets.Dataset.map`]ใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’้ฉ็”จใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’[`~TFPreTrainedModel.prepare_tf_dataset`]ใซๆธกใ—ใพใ™ใ€‚ใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ๅค‰ๆ›ดใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใ‚ทใƒฃใƒƒใƒ•ใƒซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + ```python + >>> dataset = dataset.map(tokenize_dataset) # doctest: +SKIP + >>> tf_dataset = model.prepare_tf_dataset( + ... dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer + ... ) # doctest: +SKIP + ``` + +5. ๆบ–ๅ‚™ใŒใงใใŸใ‚‰ใ€`compile`ใจ`fit`ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใงใใพใ™ใ€‚ Transformersใƒขใƒ‡ใƒซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏใซ้–ข้€ฃใ™ใ‚‹ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€ๆŒ‡ๅฎšใ—ใชใ„้™ใ‚Šใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + + ```python + >>> from tensorflow.keras.optimizers import Adam + + >>> model.compile(optimizer=Adam(3e-5)) # ๆๅคฑ้–ขๆ•ฐใฎๅผ•ๆ•ฐใฏไธ่ฆใงใ™๏ผ + >>> model.fit(tf + ``` + +## What's next? + +๐Ÿค— Transformersใฎใ‚ฏใ‚คใƒƒใ‚ฏใƒ„ใ‚ขใƒผใ‚’ๅฎŒไบ†ใ—ใŸใ‚‰ใ€ใ‚ฌใ‚คใƒ‰ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใ€ใ‚ซใ‚นใ‚ฟใƒ ใƒขใƒ‡ใƒซใฎไฝœๆˆใ€ใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใชใฉใ€ใ‚ˆใ‚Šๅ…ทไฝ“็š„ใชใ“ใจใ‚’ๅญฆใถใ“ใจใŒใงใใพใ™ใ€‚๐Ÿค— Transformersใฎใ‚ณใ‚ขใ‚ณใƒณใ‚ปใƒ—ใƒˆใซใคใ„ใฆใ‚‚ใฃใจ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€ใ‚ณใƒณใ‚ปใƒ—ใƒใƒฅใ‚ขใƒซใ‚ฌใ‚คใƒ‰ใ‚’่ชญใ‚“ใงใฟใฆใใ ใ•ใ„๏ผ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/run_scripts.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/run_scripts.md new file mode 100644 index 0000000000000000000000000000000000000000..ca224d75a453b88603643d77558771a4d5d9558a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/run_scripts.md @@ -0,0 +1,370 @@ + + +# Train with a script + +๐Ÿค— Transformersใฎ[notebooks](./notebooks/README)ใจไธ€็ท’ใซใ€[PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch)ใ€[TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow)ใ€ใพใŸใฏ[JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax)ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ™ใ‚ตใƒณใƒ—ใƒซใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ + +ใพใŸใ€็งใŸใกใฎ[็ ”็ฉถใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆ](https://github.com/huggingface/transformers-research-projects/)ใ‚„[ใƒฌใ‚ฌใ‚ทใƒผใฎไพ‹](https://github.com/huggingface/transformers/tree/main/examples/legacy)ใงไฝฟ็”จใ—ใŸใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚‚่ฆ‹ใคใ‹ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏ็พๅœจใƒกใƒณใƒ†ใƒŠใƒณใ‚นใ•ใ‚ŒใฆใŠใ‚‰ใšใ€ใŠใใ‚‰ใๆœ€ๆ–ฐใƒใƒผใ‚ธใƒงใƒณใฎใƒฉใ‚คใƒ–ใƒฉใƒชใจไบ’ๆ›ๆ€งใŒใชใ„็‰นๅฎšใฎ๐Ÿค— Transformersใฎใƒใƒผใ‚ธใƒงใƒณใŒๅฟ…่ฆใงใ™ใ€‚ + +ใ‚ตใƒณใƒ—ใƒซใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ™ในใฆใฎๅ•้กŒใงใใฎใพใพๅ‹•ไฝœใ™ใ‚‹ใ“ใจใฏๆœŸๅพ…ใ•ใ‚ŒใฆใŠใ‚‰ใšใ€่งฃๆฑบใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ๅ•้กŒใซใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’้ฉๅฟœใ•ใ›ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใฎ็‚นใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใซใ€ใปใจใ‚“ใฉใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใƒ‡ใƒผใ‚ฟใŒใฉใฎใ‚ˆใ†ใซๅ‰ๅ‡ฆ็†ใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’ๅฎŒๅ…จใซๅ…ฌ้–‹ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆ็ทจ้›†ใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™ใ€‚ + +ใ‚ตใƒณใƒ—ใƒซใ‚นใ‚ฏใƒชใƒ—ใƒˆใงๅฎŸ่ฃ…ใ—ใŸใ„ๆฉŸ่ƒฝใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[ใƒ•ใ‚ฉใƒผใƒฉใƒ ](https://discuss.huggingface.co/)ใ‹[ใ‚คใ‚ทใƒฅใƒผใƒˆใƒฉใƒƒใ‚ซใƒผ](https://github.com/huggingface/transformers/issues)ใง่ญฐ่ซ–ใ—ใฆใ‹ใ‚‰ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใ‚’ๆๅ‡บใ—ใฆใใ ใ•ใ„ใ€‚ใƒใ‚ฐไฟฎๆญฃใฏๆญ“่ฟŽใ—ใพใ™ใŒใ€่ชญใฟใ‚„ใ™ใ•ใฎใ‚ณใ‚นใƒˆใงๆฉŸ่ƒฝใ‚’่ฟฝๅŠ ใ™ใ‚‹ใƒ—ใƒซใƒชใ‚ฏใ‚จใ‚นใƒˆใฏใปใจใ‚“ใฉใƒžใƒผใ‚ธใ•ใ‚Œใชใ„ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใงใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€[PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization)ใจ[TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization)ใงๅฎŸ่กŒใ™ใ‚‹ใ‚ตใƒžใƒชใ‚ผใƒผใ‚ทใƒงใƒณใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎๅฎŸ่กŒๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ใ™ในใฆใฎไพ‹ใฏใ€ๆ˜Ž็คบ็š„ใซๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„้™ใ‚Šใ€ไธกๆ–นใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใจใ‚‚ใซๅ‹•ไฝœใ™ใ‚‹ใ“ใจใŒๆœŸๅพ…ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +## Setup + +ๆœ€ๆ–ฐใƒใƒผใ‚ธใƒงใƒณใฎใ‚ตใƒณใƒ—ใƒซใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๆญฃๅธธใซๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆ–ฐใ—ใ„ไปฎๆƒณ็’ฐๅขƒใซ๐Ÿค— Transformersใ‚’ใ‚ฝใƒผใ‚นใ‹ใ‚‰ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™: + + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` + +ไปฅๅ‰ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎใƒใƒผใ‚ธใƒงใƒณใซใคใ„ใฆใฏใ€ไปฅไธ‹ใฎใƒˆใ‚ฐใƒซใ‚’ใ‚ฏใƒชใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผš + +
+ ไปฅๅ‰ใฎ๐Ÿค— Transformersใฎใƒใƒผใ‚ธใƒงใƒณใซ้–ขใ™ใ‚‹ไพ‹ + +
+ +ๆฌกใซใ€็พๅœจใฎ๐Ÿค— Transformersใฎใ‚ฏใƒญใƒผใƒณใ‚’็‰นๅฎšใฎใƒใƒผใ‚ธใƒงใƒณใซๅˆ‡ใ‚Šๆ›ฟใˆใฆใใ ใ•ใ„ใ€‚ใŸใจใˆใฐใ€v3.5.1ใชใฉใงใ™ใ€‚ + + +```bash +git checkout tags/v3.5.1 +``` + + +้ฉๅˆ‡ใชใƒฉใ‚คใƒ–ใƒฉใƒชใƒใƒผใ‚ธใƒงใƒณใ‚’่จญๅฎšใ—ใŸใ‚‰ใ€ไปปๆ„ใฎไพ‹ใฎใƒ•ใ‚ฉใƒซใƒ€ใซ็งปๅ‹•ใ—ใ€ไพ‹ๅ›บๆœ‰ใฎ่ฆไปถใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™๏ผš + + + +```bash +pip install -r requirements.txt +``` + +## Run a script + + + +ใ“ใฎไพ‹ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใ€ๅ‰ๅ‡ฆ็†ใ‚’่กŒใ„ใพใ™ใ€‚ๆฌกใซใ€[Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) ใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃไธŠใงใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใงใฏใ€[CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆไธŠใง [T5-small](https://huggingface.co/google-t5/t5-small) ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใŒ็คบใ•ใ‚Œใฆใ„ใพใ™ใ€‚T5 ใƒขใƒ‡ใƒซใฏใ€ใใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใซ่ตทๅ› ใ—ใฆ่ฟฝๅŠ ใฎ `source_prefix` ๅผ•ๆ•ฐใŒๅฟ…่ฆใงใ™ใ€‚ใ“ใฎใƒ—ใƒญใƒณใƒ—ใƒˆใซใ‚ˆใ‚Šใ€T5 ใฏใ“ใ‚ŒใŒ่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚‹ใ“ใจใ‚’็Ÿฅใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + + +ใ“ใฎไพ‹ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆๅ‰ๅ‡ฆ็†ใ—ใพใ™ใ€‚ใใฎๅพŒใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏ่ฆ็ด„ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃไธŠใง Keras ใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใงใฏใ€[T5-small](https://huggingface.co/google-t5/t5-small) ใ‚’ [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚T5 ใƒขใƒ‡ใƒซใฏใ€ใใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใซ่ตทๅ› ใ—ใฆ่ฟฝๅŠ ใฎ `source_prefix` ๅผ•ๆ•ฐใŒๅฟ…่ฆใงใ™ใ€‚ใ“ใฎใƒ—ใƒญใƒณใƒ—ใƒˆใฏใ€T5 ใซใ“ใ‚ŒใŒ่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚‹ใ“ใจใ‚’็Ÿฅใ‚‰ใ›ใพใ™ใ€‚ + + +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Distributed training and mixed precision + +[Trainer](https://huggingface.co/docs/transformers/main_classes/trainer)ใฏใ€ๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจๆททๅˆ็ฒพๅบฆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ใคใพใ‚Šใ€ใ“ใฎๆฉŸ่ƒฝใ‚’ใ‚นใ‚ฏใƒชใƒ—ใƒˆใงไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๆฉŸ่ƒฝใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +- `fp16`ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ—ใฆๆททๅˆ็ฒพๅบฆใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚ +- `nproc_per_node`ๅผ•ๆ•ฐใงไฝฟ็”จใ™ใ‚‹GPUใฎๆ•ฐใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใฏๆไพ›ใ•ใ‚ŒใŸBashใ‚ณใƒผใƒ‰ใงใ™ใ€‚ใ“ใฎใ‚ณใƒผใƒ‰ใฎๆ—ฅๆœฌ่ชž่จณใ‚’Markdownๅฝขๅผใง่จ˜่ผ‰ใ—ใพใ™ใ€‚ + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +TensorFlowใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซ[`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy)ใ‚’ไฝฟ็”จใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใ‚ฏใƒชใƒ—ใƒˆใซ่ฟฝๅŠ ใฎๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚TensorFlowใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง่ค‡ๆ•ฐใฎGPUใŒๅˆฉ็”จๅฏ่ƒฝใชๅ ดๅˆใซใใ‚Œใ‚‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +## Run a script on a TPU + + + +Tensor Processing Units (TPUs)ใฏใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅŠ ้€Ÿใ•ใ›ใ‚‹ใŸใ‚ใซ็‰นๅˆฅใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚PyTorchใฏใ€[XLA](https://www.tensorflow.org/xla)ใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐใ‚ณใƒณใƒ‘ใ‚คใƒฉใ‚’ไฝฟ็”จใ—ใฆTPUsใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใŠใ‚Šใ€่ฉณ็ดฐใซใคใ„ใฆใฏ[ใ“ใกใ‚‰](https://github.com/pytorch/xla/blob/master/README.md)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚TPUใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€`xla_spawn.py`ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’่ตทๅ‹•ใ—ใ€`num_cores`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆไฝฟ็”จใ™ใ‚‹TPUใ‚ณใ‚ขใฎๆ•ฐใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +```bash +python xla_spawn.py --num_cores 8 \ + summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +ใ‚‚ใกใ‚ใ‚“ใ€Tensor Processing Units๏ผˆTPUs๏ผ‰ใฏๆ€ง่ƒฝใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซ็‰นๅˆฅใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚TensorFlowใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€TPUsใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซ[`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy)ใ‚’ๅˆฉ็”จใ—ใพใ™ใ€‚TPUใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€TPUใƒชใ‚ฝใƒผใ‚นใฎๅๅ‰ใ‚’`tpu`ๅผ•ๆ•ฐใซๆธกใ—ใพใ™ใ€‚ + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## Run a script with ๐Ÿค— Accelerate + +๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate)ใฏใ€PyTorchๅฐ‚็”จใฎใƒฉใ‚คใƒ–ใƒฉใƒชใงใ€CPUใฎใฟใ€่ค‡ๆ•ฐใฎGPUใ€TPUใชใฉใ€ใ•ใพใ–ใพใชใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎ็ตฑไธ€ใ•ใ‚ŒใŸๆ–นๆณ•ใ‚’ๆไพ›ใ—ใพใ™ใ€‚PyTorchใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๅฎŒๅ…จใซๅฏ่ฆ–ๅŒ–ใ—ใชใŒใ‚‰ๅฎŸ่กŒใงใใพใ™ใ€‚ใพใ ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ„ใชใ„ๅ ดๅˆใฏใ€๐Ÿค— Accelerateใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„๏ผš + +> ๆณจๆ„๏ผšAccelerateใฏๆ€ฅ้€Ÿใซ้–‹็™บใŒ้€ฒ่กŒใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏaccelerateใฎgitใƒใƒผใ‚ธใƒงใƒณใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +ไปฃใ‚ใ‚Šใซใ€`run_summarization_no_trainer.py` ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ ๐Ÿค— Accelerate ใŒใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซใฏใ€ใƒ•ใ‚ฉใƒซใƒ€ๅ†…ใซ `task_no_trainer.py` ใƒ•ใ‚กใ‚คใƒซใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใพใšใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆ่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’ไฝœๆˆใ—ใ€ไฟๅญ˜ใ—ใพใ™๏ผš + +```bash +accelerate config +``` + +ใƒ†ใ‚นใƒˆใ‚’่กŒใ„ใ€่จญๅฎšใŒๆญฃใ—ใๆง‹ๆˆใ•ใ‚Œใฆใ„ใ‚‹ใ‹็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผš + + +```bash +accelerate test +``` + +Now you are ready to launch the training: + + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization +``` + +## Use a custom dataset + +่ฆ็ด„ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€CSVใพใŸใฏJSON Lineใƒ•ใ‚กใ‚คใƒซใงใ‚ใ‚Œใฐใ€ใ‚ซใ‚นใ‚ฟใƒ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚็‹ฌ่‡ชใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใ„ใใคใ‹ใฎ่ฟฝๅŠ ใฎๅผ•ๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `train_file`ใŠใ‚ˆใณ`validation_file`ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใจใƒใƒชใƒ‡ใƒผใ‚ทใƒงใƒณใฎใƒ•ใ‚กใ‚คใƒซใธใฎใƒ‘ใ‚นใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ +- `text_column`ใฏ่ฆ็ด„ใ™ใ‚‹ใŸใ‚ใฎๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใงใ™ใ€‚ +- `summary_column`ใฏๅ‡บๅŠ›ใ™ใ‚‹ๅฏพ่ฑกใƒ†ใ‚ญใ‚นใƒˆใงใ™ใ€‚ + +ใ‚ซใ‚นใ‚ฟใƒ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใŸ่ฆ็ด„ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate +``` + +## Test a script + +ใ™ในใฆใŒไบˆๆƒณ้€šใ‚Šใซๅ‹•ไฝœใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ๅ‰ใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎไธ€้ƒจใฎไพ‹ใงใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใฏ่‰ฏใ„ใ‚ขใ‚คใƒ‡ใ‚ขใงใ™ใ€‚ไปฅไธ‹ใฎๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆœ€ๅคงใ‚ตใƒณใƒ—ใƒซๆ•ฐใซๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™๏ผš + +- `max_train_samples` +- `max_eval_samples` +- `max_predict_samples` + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +ไธ€้ƒจใฎไพ‹ใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€`max_predict_samples`ๅผ•ๆ•ฐใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅผ•ๆ•ฐใŒใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใŒใ‚ใ‹ใ‚‰ใชใ„ๅ ดๅˆใฏใ€`-h`ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ—ใฆ็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +examples/pytorch/summarization/run_summarization.py -h +``` + +## Resume training from checkpoint + +ไปฅๅ‰ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ใŸใ‚ใฎๅฝน็ซ‹ใคใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒไธญๆ–ญใ•ใ‚ŒใŸๅ ดๅˆใงใ‚‚ใ€ๆœ€ๅˆใ‹ใ‚‰ใ‚„ใ‚Š็›ดใ™ใ“ใจใชใใ€ไธญๆ–ญใ—ใŸใจใ“ใ‚ใ‹ใ‚‰ๅ†้–‹ใงใใพใ™ใ€‚ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ใŸใ‚ใฎ2ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€ๅˆใฎๆ–นๆณ•ใฏใ€`output_dir previous_output_dir` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€`output_dir` ใซไฟๅญ˜ใ•ใ‚ŒใŸๆœ€ๆ–ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ใ“ใฎๅ ดๅˆใ€`overwrite_output_dir` ใ‚’ๅ‰Š้™คใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --output_dir previous_output_dir \ + --predict_with_generate +``` + +2็•ช็›ฎใฎๆ–นๆณ•ใงใฏใ€`resume_from_checkpoint path_to_specific_checkpoint` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€็‰นๅฎšใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใƒ•ใ‚ฉใƒซใƒ€ใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ†้–‹ใ—ใพใ™ใ€‚ + + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate +``` + +## Share your model + +ใ™ในใฆใฎใ‚นใ‚ฏใƒชใƒ—ใƒˆใฏใ€ๆœ€็ต‚็š„ใชใƒขใƒ‡ใƒซใ‚’ [Model Hub](https://huggingface.co/models) ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซ Hugging Face ใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +hf auth login +``` + +ๆฌกใซใ€ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซ `push_to_hub` ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใฎๅผ•ๆ•ฐใฏใ€Hugging Face ใฎใƒฆใƒผใ‚ถใƒผๅใจ `output_dir` ใงๆŒ‡ๅฎšใ—ใŸใƒ•ใ‚ฉใƒซใƒ€ๅใงใƒชใƒใ‚ธใƒˆใƒชใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +็‰นๅฎšใฎๅๅ‰ใ‚’ใƒชใƒใ‚ธใƒˆใƒชใซไป˜ใ‘ใ‚‹ใซใฏใ€`push_to_hub_model_id` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆ่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใฎใƒชใƒใ‚ธใƒˆใƒชใฏ่‡ชๅ‹•็š„ใซใ‚ใชใŸใฎๅๅ‰็ฉบ้–“ใฎไธ‹ใซใƒชใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ + +ไปฅไธ‹ใฎไพ‹ใฏใ€็‰นๅฎšใฎใƒชใƒใ‚ธใƒˆใƒชๅใงใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™: + + + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/serialization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/serialization.md new file mode 100644 index 0000000000000000000000000000000000000000..3e9d81180de0464984fa36560c8ba78b7817ec8d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/serialization.md @@ -0,0 +1,191 @@ + + +# Export to ONNX + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ๆœฌ็•ช็’ฐๅขƒใซๅฑ•้–‹ใ™ใ‚‹้š›ใซใฏใ€ใƒขใƒ‡ใƒซใ‚’็‰นๆฎŠใชใƒฉใƒณใ‚ฟใ‚คใƒ ใŠใ‚ˆใณใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใง่ชญใฟ่พผใฟใ€ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ทใƒชใ‚ขใƒฉใ‚คใ‚บใ•ใ‚ŒใŸๅฝขๅผใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใŒๅฟ…่ฆใงใ‚ใ‚‹ใ‹ใ€ใใฎๆฉๆตใ‚’ๅ—ใ‘ใ‚‹ใ“ใจใŒใงใใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +๐Ÿค— Optimumใฏใ€Transformersใฎๆ‹กๅผตๆฉŸ่ƒฝใงใ‚ใ‚Šใ€PyTorchใพใŸใฏTensorFlowใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ONNXใ‚„TFLiteใชใฉใฎใ‚ทใƒชใ‚ขใƒฉใ‚คใ‚บใ•ใ‚ŒใŸๅฝขๅผใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใ€Œexportersใ€ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ใพใŸใ€๐Ÿค— Optimumใฏใ€ๆœ€ๅคงใฎๅŠน็އใงใ‚ฟใƒผใ‚ฒใƒƒใƒˆใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใƒ„ใƒผใƒซใ‚‚ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’๐Ÿค— Optimumใ‚’ไฝฟ็”จใ—ใฆONNXใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใŠใ‚Šใ€ใƒขใƒ‡ใƒซใ‚’TFLiteใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆใฏ[Export to TFLiteใƒšใƒผใ‚ธ](tflite)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Export to ONNX + +[ONNX๏ผˆOpen Neural Network eXchange๏ผ‰](http://onnx.ai)ใฏใ€PyTorchใŠใ‚ˆใณTensorFlowใ‚’ๅซใ‚€ใ•ใพใ–ใพใชใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงๆทฑๅฑคๅญฆ็ฟ’ใƒขใƒ‡ใƒซใ‚’่กจ็พใ™ใ‚‹ใŸใ‚ใฎๅ…ฑ้€šใฎไธ€้€ฃใฎๆผ”็ฎ—ๅญใจใƒ•ใ‚กใ‚คใƒซๅฝขๅผใ‚’ๅฎš็พฉใ™ใ‚‹ใ‚ชใƒผใƒ—ใƒณใ‚นใ‚ฟใƒณใƒ€ใƒผใƒ‰ใงใ™ใ€‚ใƒขใƒ‡ใƒซใŒONNXๅฝขๅผใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ•ใ‚Œใ‚‹ใจใ€ใ“ใ‚Œใ‚‰ใฎๆผ”็ฎ—ๅญใฏใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚’ไป‹ใ™ใ‚‹ใƒ‡ใƒผใ‚ฟใฎๆตใ‚Œใ‚’่กจใ™่จˆ็ฎ—ใ‚ฐใƒฉใƒ•๏ผˆไธ€่ˆฌ็š„ใซใฏใ€Œไธญ้–“่กจ็พใ€ใจๅ‘ผใฐใ‚Œใ‚‹๏ผ‰ใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ๆจ™ๆบ–ๅŒ–ใ•ใ‚ŒใŸๆผ”็ฎ—ๅญใจใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ๅ‚™ใˆใŸใ‚ฐใƒฉใƒ•ใ‚’ๅ…ฌ้–‹ใ™ใ‚‹ใ“ใจใงใ€ONNXใฏใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏ้–“ใฎๅˆ‡ใ‚Šๆ›ฟใˆใ‚’ๅฎนๆ˜“ใซใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€PyTorchใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏONNXๅฝขๅผใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใ€ใใ‚Œใ‚’TensorFlowใงใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผˆ้€†ใ‚‚ๅŒๆง˜ใงใ™๏ผ‰ใ€‚ + +ONNXๅฝขๅผใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซไฝฟ็”จใงใใพใ™๏ผš +- [ใ‚ฐใƒฉใƒ•ๆœ€้ฉๅŒ–](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization)ใ‚„[้‡ๅญๅŒ–](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization)ใชใฉใฎใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใฎใŸใ‚ใซๆœ€้ฉๅŒ–ใ€‚ +- [`ORTModelForXXX`ใ‚ฏใƒฉใ‚น](https://huggingface.co/docs/optimum/onnxruntime/package_reference/modeling_ort)ใ‚’ไป‹ใ—ใฆONNX RuntimeใงๅฎŸ่กŒใ—ใ€๐Ÿค— TransformersใงใŠใชใ˜ใฟใฎ`AutoModel` APIใซๅพ“ใ„ใพใ™ใ€‚ +- [ๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸๆŽจ่ซ–ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines)ใ‚’ไป‹ใ—ใฆๅฎŸ่กŒใ—ใ€๐Ÿค— Transformersใฎ[`pipeline`]้–ขๆ•ฐใจๅŒใ˜APIใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ + +๐Ÿค— Optimumใฏใ€่จญๅฎšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆดป็”จใ—ใฆONNXใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใŠใ‚Šใ€ใ“ใ‚Œใ‚‰ใฎ่จญๅฎšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏๅคšใใฎใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃ็”จใซไบ‹ๅ‰ใซไฝœๆˆใ•ใ‚ŒใฆใŠใ‚Šใ€ไป–ใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซใ‚‚็ฐกๅ˜ใซๆ‹กๅผตใงใใ‚‹ใ‚ˆใ†ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ไบ‹ๅ‰ใซไฝœๆˆใ•ใ‚ŒใŸ่จญๅฎšใฎใƒชใ‚นใƒˆใซใคใ„ใฆใฏใ€[๐Ÿค— Optimumใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/exporters/onnx/overview)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ONNXใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใฏ2ใคใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใงใฏไธกๆ–นใฎๆ–นๆณ•ใ‚’็คบใ—ใพใ™๏ผš + +- export with ๐Ÿค— Optimum via CLI. +- export with ๐Ÿค— Optimum with `optimum.onnxruntime`. + +### Exporting a ๐Ÿค— Transformers model to ONNX with CLI + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ONNXใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€ใพใš่ฟฝๅŠ ใฎไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pip install optimum[exporters] +``` + +ใ™ในใฆใฎๅˆฉ็”จๅฏ่ƒฝใชๅผ•ๆ•ฐใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[๐Ÿค— Optimumใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใพใŸใฏใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใงใƒ˜ใƒซใƒ—ใ‚’่กจ็คบใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผš + + +```bash +optimum-cli export onnx --help +``` + +๐Ÿค— Hubใ‹ใ‚‰ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€ไพ‹ใˆใฐ `distilbert/distilbert-base-uncased-distilled-squad` ใ‚’ไฝฟใ„ใŸใ„ๅ ดๅˆใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +```bash +optimum-cli export onnx --model distilbert/distilbert-base-uncased-distilled-squad distilbert_base_uncased_squad_onnx/ +``` + +้€ฒ่กŒ็Šถๆณใ‚’็คบใ—ใ€็ตๆžœใฎ `model.onnx` ใŒไฟๅญ˜ใ•ใ‚Œใ‚‹ๅ ดๆ‰€ใ‚’่กจ็คบใ™ใ‚‹ใƒญใ‚ฐใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซ่กจ็คบใ•ใ‚Œใ‚‹ใฏใšใงใ™๏ผš + + +```bash +Validating ONNX model distilbert_base_uncased_squad_onnx/model.onnx... + -[โœ“] ONNX model output names match reference model (start_logits, end_logits) + - Validating ONNX Model output "start_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) + - Validating ONNX Model output "end_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) +The ONNX export succeeded and the exported model was saved at: distilbert_base_uncased_squad_onnx +``` + +ไธŠ่จ˜ใฎไพ‹ใฏ๐Ÿค— Hubใ‹ใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใƒญใƒผใ‚ซใƒซใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๅ ดๅˆใ€ใพใšใƒขใƒ‡ใƒซใฎ้‡ใฟใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ•ใ‚กใ‚คใƒซใ‚’ๅŒใ˜ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช๏ผˆ`local_path`๏ผ‰ใซไฟๅญ˜ใ—ใฆใใ ใ•ใ„ใ€‚CLIใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€๐Ÿค— Hubใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆๅใฎไปฃใ‚ใ‚Šใซ`model`ๅผ•ๆ•ฐใซ`local_path`ใ‚’ๆธกใ—ใ€`--task`ๅผ•ๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใฆใใ ใ•ใ„ใ€‚[๐Ÿค— Optimumใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/exporters/task_manager)ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‚ฟใ‚นใ‚ฏใฎใƒชใ‚นใƒˆใ‚’็ขบ่ชใงใใพใ™ใ€‚`task`ๅผ•ๆ•ฐใŒๆŒ‡ๅฎšใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใŸใชใ„ใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง้ธๆŠžใ•ใ‚Œใพใ™ใ€‚ + + +```bash +optimum-cli export onnx --model local_path --task question-answering distilbert_base_uncased_squad_onnx/ +``` + +ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ•ใ‚ŒใŸ `model.onnx` ใƒ•ใ‚กใ‚คใƒซใฏใ€ONNXๆจ™ๆบ–ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹[ๅคšใใฎใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ฟ](https://onnx.ai/supported-tools.html#deployModel)ใฎ1ใคใงๅฎŸ่กŒใงใใพใ™ใ€‚ใŸใจใˆใฐใ€[ONNX Runtime](https://onnxruntime.ai/)ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใฟใ€ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + + +```python +>>> from transformers import AutoTokenizer +>>> from optimum.onnxruntime import ORTModelForQuestionAnswering + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> model = ORTModelForQuestionAnswering.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> inputs = tokenizer("What am I using?", "Using DistilBERT with ONNX Runtime!", return_tensors="pt") +>>> outputs = model(**inputs) +``` + +๐Ÿค— Hubใ‹ใ‚‰TensorFlowใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใฏใ€ๅŒๆง˜ใงใ™ใ€‚ไพ‹ใˆใฐใ€[Keras organization](https://huggingface.co/keras-io)ใ‹ใ‚‰็ด”็ฒ‹ใชTensorFlowใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + + +```bash +optimum-cli export onnx --model keras-io/transformers-qa distilbert_base_cased_squad_onnx/ +``` + +### Exporting a ๐Ÿค— Transformers model to ONNX with `optimum.onnxruntime` + +CLIใฎไปฃใ‚ใ‚Šใซใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ONNXใซใƒ—ใƒญใ‚ฐใƒฉใƒ ็š„ใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซ่กŒใ„ใพใ™๏ผš + +```python +>>> from optimum.onnxruntime import ORTModelForSequenceClassification +>>> from transformers import AutoTokenizer + +>>> model_checkpoint = "distilbert_base_uncased_squad" +>>> save_directory = "onnx/" + +>>> # Load a model from transformers and export it to ONNX +>>> ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=True) +>>> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) + +>>> # Save the onnx model and tokenizer +>>> ort_model.save_pretrained(save_directory) +>>> tokenizer.save_pretrained(save_directory) +``` + +### Exporting a model for an unsupported architecture + +็พๅœจใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใงใใชใ„ใƒขใƒ‡ใƒซใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใซ่ฒข็Œฎใ—ใŸใ„ๅ ดๅˆใ€ใพใš[`optimum.exporters.onnx`](https://huggingface.co/docs/optimum/exporters/onnx/overview)ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใ€ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏ[๐Ÿค— Optimumใซ่ฒข็Œฎ](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/contribute)ใ—ใฆใใ ใ•ใ„ใ€‚ + +### Exporting a model with `transformers.onnx` + + + +`transformers.onnx`ใฏใ‚‚ใฏใ‚„ใƒกใƒณใƒ†ใƒŠใƒณใ‚นใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใƒขใƒ‡ใƒซใ‚’ไธŠ่จ˜ใง่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซ๐Ÿค— Optimumใงใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฏๅฐ†ๆฅใฎใƒใƒผใ‚ธใƒงใƒณใงๅ‰Š้™คใ•ใ‚Œใพใ™ใ€‚ + + + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ONNXใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€่ฟฝๅŠ ใฎไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„๏ผš + + +```bash +pip install transformers[onnx] +``` + +`transformers.onnx`ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใ‚’Pythonใƒขใ‚ธใƒฅใƒผใƒซใจใ—ใฆไฝฟ็”จใ—ใฆใ€ไบ‹ๅ‰ใซ็”จๆ„ใ•ใ‚ŒใŸ่จญๅฎšใ‚’ไฝฟ็”จใ—ใฆใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased onnx/ +``` + +ใ“ใฎๆ–นๆณ•ใฏใ€`--model`ๅผ•ๆ•ฐใงๅฎš็พฉใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎONNXใ‚ฐใƒฉใƒ•ใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใพใ™ใ€‚๐Ÿค— Hubใฎใ„ใšใ‚Œใ‹ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใพใŸใฏใƒญใƒผใ‚ซใƒซใซไฟๅญ˜ใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๆธกใ™ใ“ใจใŒใงใใพใ™ใ€‚ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ•ใ‚ŒใŸ`model.onnx`ใƒ•ใ‚กใ‚คใƒซใฏใ€ONNXๆจ™ๆบ–ใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ๅคšใใฎใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ฟใงๅฎŸ่กŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€ONNX Runtimeใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใ‚“ใงๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + + +```python +>>> from transformers import AutoTokenizer +>>> from onnxruntime import InferenceSession + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> session = InferenceSession("onnx/model.onnx") +>>> # ONNX Runtime expects NumPy arrays as input +>>> inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="np") +>>> outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs)) +``` + +ๅฟ…่ฆใชๅ‡บๅŠ›ๅ๏ผˆไพ‹: `["last_hidden_state"]`๏ผ‰ใฏใ€ๅ„ใƒขใƒ‡ใƒซใฎONNXๆง‹ๆˆใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใงๅ–ๅพ—ใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€DistilBERTใฎๅ ดๅˆใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +```python +>>> from transformers.models.distilbert import DistilBertConfig, DistilBertOnnxConfig + +>>> config = DistilBertConfig() +>>> onnx_config = DistilBertOnnxConfig(config) +>>> print(list(onnx_config.outputs.keys())) +["last_hidden_state"] +``` + +ใƒใƒ–ใ‹ใ‚‰็ด”็ฒ‹ใชTensorFlowใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒ—ใƒญใ‚ฐใƒฉใƒ ็š„ใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซๅŒๆง˜ใงใ™๏ผš + +```bash +python -m transformers.onnx --model=keras-io/transformers-qa onnx/ +``` + +ใƒญใƒผใ‚ซใƒซใซไฟๅญ˜ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ•ใ‚กใ‚คใƒซใ‚’ๅŒใ˜ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซไฟๅญ˜ใ—ใฆใใ ใ•ใ„๏ผˆไพ‹๏ผš `local-pt-checkpoint`๏ผ‰ใ€‚ใใฎๅพŒใ€`transformers.onnx`ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎ `--model`ๅผ•ๆ•ฐใ‚’ๅธŒๆœ›ใ™ใ‚‹ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซๅ‘ใ‘ใฆ่จญๅฎšใ—ใฆใ€ONNXใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใพใ™๏ผš + + +```bash +python -m transformers.onnx --model=local-pt-checkpoint onnx/ +``` + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/task_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/task_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..93f4783b1520103fd50243eeb4347605b8fba9d8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/task_summary.md @@ -0,0 +1,355 @@ + + +# What ๐Ÿค— Transformers can do + +๐Ÿค— Transformersใฏใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†๏ผˆNLP๏ผ‰ใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ€้Ÿณๅฃฐๅ‡ฆ็†ใชใฉใฎๆœ€ๆ–ฐใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใƒฉใ‚คใƒ–ใƒฉใƒชใงใ™ใ€‚ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใซใฏใ€Transformerใƒขใƒ‡ใƒซใ ใ‘ใงใชใใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏๅ‘ใ‘ใฎใƒขใƒ€ใƒณใช็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใชใฉใ€Transformerไปฅๅค–ใฎใƒขใƒ‡ใƒซใ‚‚ๅซใพใ‚Œใฆใ„ใพใ™ใ€‚็พไปฃใฎใ‚นใƒžใƒผใƒˆใƒ•ใ‚ฉใƒณใ€ใ‚ขใƒ—ใƒชใ€ใƒ†ใƒฌใƒ“ใชใฉใ€ๆœ€ใ‚‚ไบบๆฐ—ใฎใ‚ใ‚‹ๆถˆ่ฒป่€…่ฃฝๅ“ใฎๅคšใใซใฏใ€ๆทฑๅฑคๅญฆ็ฟ’ๆŠ€่ก“ใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ‚นใƒžใƒผใƒˆใƒ•ใ‚ฉใƒณใงๆ’ฎๅฝฑใ—ใŸๅ†™็œŸใ‹ใ‚‰่ƒŒๆ™ฏใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅ‰Š้™คใ—ใŸใ„ใงใ™ใ‹๏ผŸใ“ใ‚Œใฏใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใƒปใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ๏ผˆใพใ ไฝ•ใ‚’ๆ„ๅ‘ณใ™ใ‚‹ใ‹ใ‚ใ‹ใ‚‰ใชใใฆใ‚‚ๅฟƒ้…ใ—ใชใ„ใงใใ ใ•ใ„ใ€ไปฅไธ‹ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ชฌๆ˜Žใ—ใพใ™๏ผ๏ผ‰ใฎใ‚ฟใ‚นใ‚ฏใฎไธ€ไพ‹ใงใ™ใ€‚ + +ใ“ใฎใƒšใƒผใ‚ธใงใฏใ€๐Ÿค— Transformersใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€ใŸใฃใŸ3่กŒใฎใ‚ณใƒผใƒ‰ใง่งฃๆฑบใงใใ‚‹ใ•ใพใ–ใพใช้ŸณๅฃฐใŠใ‚ˆใณ้Ÿณๅฃฐใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ€NLPใ‚ฟใ‚นใ‚ฏใฎๆฆ‚่ฆใ‚’ๆไพ›ใ—ใพใ™๏ผ + +## Audio + +้Ÿณๅฃฐใจ้Ÿณๅฃฐๅ‡ฆ็†ใฎใ‚ฟใ‚นใ‚ฏใฏใ€ไป–ใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใจใฏๅฐ‘ใ—็•ฐใชใ‚Šใพใ™ใ€‚ใชใœใชใ‚‰ใ€ๅ…ฅๅŠ›ใจใ—ใฆใฎ็”Ÿใฎ้Ÿณๅฃฐๆณขๅฝขใฏ้€ฃ็ถš็š„ใชไฟกๅทใงใ‚ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใจใฏ็•ฐใชใ‚Šใ€็”Ÿใฎ้Ÿณๅฃฐๆณขๅฝขใฏๆ–‡็ซ ใ‚’ๅ˜่ชžใซๅˆ†ๅ‰ฒใงใใ‚‹ใ‚ˆใ†ใซใใ‚Œใ„ใซๅˆ†ๅ‰ฒใงใใพใ›ใ‚“ใ€‚ใ“ใ‚Œใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใ€้€šๅธธใ€็”Ÿใฎ้ŸณๅฃฐไฟกๅทใฏๅฎšๆœŸ็š„ใช้–“้š”ใงใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚้–“้š”ๅ†…ใงใ‚ˆใ‚Šๅคšใใฎใ‚ตใƒณใƒ—ใƒซใ‚’ๅ–ใ‚‹ใจใ€ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใŒ้ซ˜ใใชใ‚Šใ€้Ÿณๅฃฐใฏใ‚ˆใ‚Šๅ…ƒใฎ้Ÿณๅฃฐใ‚ฝใƒผใ‚นใซ่ฟ‘ใฅใใพใ™ใ€‚ + +ไปฅๅ‰ใฎใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€้Ÿณๅฃฐใ‚’ๅ‰ๅ‡ฆ็†ใ—ใฆใใ‚Œใ‹ใ‚‰ๆœ‰็”จใช็‰นๅพดใ‚’ๆŠฝๅ‡บใ—ใพใ—ใŸใ€‚ใ—ใ‹ใ—ใ€็พๅœจใงใฏใ€็”Ÿใฎ้Ÿณๅฃฐๆณขๅฝขใ‚’็‰นๅพดใ‚จใƒณใ‚ณใƒผใƒ€ใซ็›ดๆŽฅใƒ•ใ‚ฃใƒผใƒ‰ใ—ใ€้Ÿณๅฃฐ่กจ็พใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใ‚‹ใ“ใจใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใŒ็ฐก็ด ๅŒ–ใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฏๆœ€ใ‚‚้‡่ฆใช็‰นๅพดใ‚’ๅญฆ็ฟ’ใงใใพใ™ใ€‚ + +### Audio classification + +้Ÿณๅฃฐๅˆ†้กžใฏใ€ไบ‹ๅ‰ใซๅฎš็พฉใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ใ“ใ‚Œใฏๅคšใใฎๅ…ทไฝ“็š„ใชใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ๅซใ‚€ๅบƒ็ฏ„ใชใ‚ซใƒ†ใ‚ดใƒชใงใ‚ใ‚Šใ€ใ„ใใคใ‹ใฎไพ‹ใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +- ้Ÿณ้Ÿฟใ‚ทใƒผใƒณๅˆ†้กž๏ผš้Ÿณๅฃฐใซใ‚ทใƒผใƒณใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹๏ผˆใ€Œใ‚ชใƒ•ใ‚ฃใ‚นใ€ใ€ใ€Œใƒ“ใƒผใƒใ€ใ€ใ€Œใ‚นใ‚ฟใ‚ธใ‚ขใƒ ใ€๏ผ‰ +- ้Ÿณ้Ÿฟใ‚คใƒ™ใƒณใƒˆๆคœๅ‡บ๏ผš้Ÿณๅฃฐใซ้Ÿณๅฃฐใ‚คใƒ™ใƒณใƒˆใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹๏ผˆใ€Œ่ปŠใฎใ‚ฏใƒฉใ‚ฏใ‚ทใƒงใƒณใ€ใ€ใ€Œใ‚ฏใ‚ธใƒฉใฎๅ‘ผใณๅฃฐใ€ใ€ใ€Œใ‚ฌใƒฉใ‚นใฎ็ ด่ฃ‚ใ€๏ผ‰ +- ใ‚ฟใ‚ฎใƒณใ‚ฐ๏ผš่ค‡ๆ•ฐใฎ้Ÿณใ‚’ๅซใ‚€้Ÿณๅฃฐใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹๏ผˆ้ณฅใฎ้ณดใๅฃฐใ€ไผš่ญฐใงใฎใ‚นใƒ”ใƒผใ‚ซใƒผ่ญ˜ๅˆฅ๏ผ‰ +- ้Ÿณๆฅฝๅˆ†้กž๏ผš้Ÿณๆฅฝใซใ‚ธใƒฃใƒณใƒซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹๏ผˆใ€Œใƒกใ‚ฟใƒซใ€ใ€ใ€Œใƒ’ใƒƒใƒ—ใƒ›ใƒƒใƒ—ใ€ใ€ใ€Œใ‚ซใƒณใƒˆใƒชใƒผใ€๏ผ‰ + + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="audio-classification", model="superb/hubert-base-superb-er") +>>> preds = classifier("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4532, 'label': 'hap'}, + {'score': 0.3622, 'label': 'sad'}, + {'score': 0.0943, 'label': 'neu'}, + {'score': 0.0903, 'label': 'ang'}] +``` + +### Automatic speech recognition + +่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผˆASR๏ผ‰ใฏ้Ÿณๅฃฐใ‚’ใƒ†ใ‚ญใ‚นใƒˆใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ไบบ้–“ใฎใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณใฎ่‡ช็„ถใชๅฝขๅผใงใ‚ใ‚‹้Ÿณๅฃฐใฎไธ€้ƒจใจใ—ใฆใ€้ƒจๅˆ†็š„ใซใใฎใ‚ˆใ†ใชไธ€่ˆฌ็š„ใชใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚ฟใ‚นใ‚ฏใฎไธ€ใคใงใ™ใ€‚ไปŠๆ—ฅใ€ASRใ‚ทใ‚นใƒ†ใƒ ใฏใ‚นใƒ”ใƒผใ‚ซใƒผใ€ใ‚นใƒžใƒผใƒˆใƒ•ใ‚ฉใƒณใ€่‡ชๅ‹•่ปŠใชใฉใฎใ€Œใ‚นใƒžใƒผใƒˆใ€ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใƒ—ใƒญใƒ€ใ‚ฏใƒˆใซ็ต„ใฟ่พผใพใ‚Œใฆใ„ใพใ™ใ€‚็งใŸใกใฏไปฎๆƒณใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใซ้Ÿณๆฅฝใ‚’ๅ†็”Ÿใ—ใฆใ‚‚ใ‚‰ใฃใŸใ‚Šใ€ใƒชใƒžใ‚คใƒณใƒ€ใƒผใ‚’่จญๅฎšใ—ใฆใ‚‚ใ‚‰ใฃใŸใ‚Šใ€ๅคฉๆฐ—ใ‚’ๆ•™ใˆใฆใ‚‚ใ‚‰ใฃใŸใ‚Šใงใใพใ™ใ€‚ + +ใ—ใ‹ใ—ใ€Transformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใŒๅŠฉใ‘ใŸไธป่ฆใช่ชฒ้กŒใฎไธ€ใคใฏใ€ไฝŽใƒชใ‚ฝใƒผใ‚น่จ€่ชžใซใŠใ‘ใ‚‹ใ‚‚ใฎใงใ™ใ€‚ๅคง้‡ใฎ้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ไฝŽใƒชใ‚ฝใƒผใ‚น่จ€่ชžใงใƒฉใƒ™ใƒซไป˜ใ้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใ‚’ใ‚ใšใ‹1ๆ™‚้–“ใ ใ‘ใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใงใ‚‚ใ€ไปฅๅ‰ใฎASRใ‚ทใ‚นใƒ†ใƒ ใจๆฏ”่ผƒใ—ใฆ้ซ˜ๅ“่ณชใช็ตๆžœใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไปฅๅ‰ใฎASRใ‚ทใ‚นใƒ†ใƒ ใฏ100ๅ€ไปฅไธŠใฎใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ—ใŸใŒใ€Transformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใ“ใฎๅ•้กŒใซ่ฒข็Œฎใ—ใพใ—ใŸใ€‚ + + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-small") +>>> transcriber("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + + +## Computer vision + +ๆœ€ๅˆใงๅˆใ‚ใฆๆˆๅŠŸใ—ใŸใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใ‚ฟใ‚นใ‚ฏใฎไธ€ใคใฏใ€[็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆCNN๏ผ‰](glossary#convolution)ใ‚’ไฝฟ็”จใ—ใฆ้ƒตไพฟ็•ชๅทใฎ็”ปๅƒใ‚’่ช่ญ˜ใ™ใ‚‹ใ“ใจใงใ—ใŸใ€‚็”ปๅƒใฏใƒ”ใ‚ฏใ‚ปใƒซใ‹ใ‚‰ๆง‹ๆˆใ•ใ‚Œใ€ๅ„ใƒ”ใ‚ฏใ‚ปใƒซใซใฏๆ•ฐๅ€คใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€็”ปๅƒใ‚’ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใฎ่กŒๅˆ—ใจใ—ใฆ็ฐกๅ˜ใซ่กจ็พใงใใพใ™ใ€‚็‰นๅฎšใฎใƒ”ใ‚ฏใ‚ปใƒซๅ€คใฎ็ต„ใฟๅˆใ‚ใ›ใฏใ€็”ปๅƒใฎ่‰ฒใ‚’่จ˜่ฟฐใ—ใพใ™ใ€‚ + +ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ไธ€่ˆฌ็š„ใชๆ–นๆณ•ใฏๆฌกใฎ2ใคใงใ™๏ผš + +1. ็•ณใฟ่พผใฟใ‚’ไฝฟ็”จใ—ใฆใ€ไฝŽใƒฌใƒ™ใƒซใฎ็‰นๅพดใ‹ใ‚‰้ซ˜ใƒฌใƒ™ใƒซใฎๆŠฝ่ฑก็š„ใชๆƒ…ๅ ฑใพใงใ€็”ปๅƒใฎ้šŽๅฑค็š„ใช็‰นๅพดใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ€‚ +2. ็”ปๅƒใ‚’ใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใ—ใ€ๅ„็”ปๅƒใƒ‘ใƒƒใƒใŒ็”ปๅƒๅ…จไฝ“ใจใฉใฎใ‚ˆใ†ใซ้–ข้€ฃใ—ใฆใ„ใ‚‹ใ‹ใ‚’ๅพใ€…ใซๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใซTransformerใ‚’ไฝฟ็”จใ™ใ‚‹ใ€‚CNNใŒๅฅฝใ‚€ใƒœใƒˆใƒ ใ‚ขใƒƒใƒ—ใ‚ขใƒ—ใƒญใƒผใƒใจใฏ็•ฐใชใ‚Šใ€ใ“ใ‚Œใฏใผใ‚“ใ‚„ใ‚Šใจใ—ใŸ็”ปๅƒใ‹ใ‚‰ๅง‹ใ‚ใฆๅพใ€…ใซ็„ฆ็‚นใ‚’ๅˆใ‚ใ›ใ‚‹ใ‚ˆใ†ใชใ‚‚ใฎใงใ™ใ€‚ + +### Image classification + +็”ปๅƒๅˆ†้กžใฏใ€ไบ‹ๅ‰ใซๅฎš็พฉใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰็”ปๅƒๅ…จไฝ“ใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใพใ™ใ€‚ๅคšใใฎๅˆ†้กžใ‚ฟใ‚นใ‚ฏใจๅŒๆง˜ใซใ€็”ปๅƒๅˆ†้กžใซใฏๅคšใใฎๅฎŸ็”จ็š„ใช็”จ้€”ใŒใ‚ใ‚Šใ€ใใฎไธ€้ƒจใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +* ๅŒป็™‚๏ผš็–พๆ‚ฃใฎๆคœๅ‡บใ‚„ๆ‚ฃ่€…ใฎๅฅๅบทใฎ็›ฃ่ฆ–ใซไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซๅŒป็™‚็”ปๅƒใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹ +* ็’ฐๅขƒ๏ผšๆฃฎๆž—ไผๆŽกใฎ็›ฃ่ฆ–ใ€้‡Ž็”Ÿๅœฐใฎ็ฎก็†ๆƒ…ๅ ฑใ€ใพใŸใฏๅฑฑ็ซไบ‹ใฎๆคœๅ‡บใซไฝฟ็”จใ™ใ‚‹ใŸใ‚ใซ่ก›ๆ˜Ÿ็”ปๅƒใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹ +* ่พฒๆฅญ๏ผšไฝœ็‰ฉใฎๅฅๅบทใ‚’็›ฃ่ฆ–ใ™ใ‚‹ใŸใ‚ใฎไฝœ็‰ฉใฎ็”ปๅƒใ‚„ใ€ๅœŸๅœฐๅˆฉ็”จใฎ็›ฃ่ฆ–ใฎใŸใ‚ใฎ่ก›ๆ˜Ÿ็”ปๅƒใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹ +* ็”Ÿๆ…‹ๅญฆ๏ผš้‡Ž็”Ÿๅ‹•็‰ฉใฎๅ€‹ไฝ“ๆ•ฐใ‚’็›ฃ่ฆ–ใ—ใŸใ‚Šใ€็ตถๆป…ๅฑๆƒง็จฎใ‚’่ฟฝ่ทกใ™ใ‚‹ใŸใ‚ใซๅ‹•ๆค็‰ฉใฎ็จฎใฎ็”ปๅƒใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ‚‹ + + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="image-classification") +>>> preds = classifier( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.4335, 'label': 'lynx, catamount'} +{'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'} +{'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'} +{'score': 0.0239, 'label': 'Egyptian cat'} +{'score': 0.0229, 'label': 'tiger cat'} +``` + +### Object detection + +็”ปๅƒๅˆ†้กžใจใฏ็•ฐใชใ‚Šใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฏ็”ปๅƒๅ†…ใฎ่ค‡ๆ•ฐใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ญ˜ๅˆฅใ—ใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎไฝ็ฝฎใ‚’็”ปๅƒๅ†…ใงๅฎš็พฉใ™ใ‚‹ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใซใ‚ˆใฃใฆ็‰นๅฎšใ—ใพใ™ใ€‚ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฎไพ‹ใซใฏๆฌกใฎใ‚ˆใ†ใช็”จ้€”ใŒใ‚ใ‚Šใพใ™๏ผš + +* ่‡ชๅ‹•้‹่ปข่ปŠ๏ผšไป–ใฎ่ปŠไธกใ€ๆญฉ่กŒ่€…ใ€ไฟกๅทๆฉŸใชใฉใฎๆ—ฅๅธธใฎไบค้€šใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บ +* ใƒชใƒขใƒผใƒˆใ‚ปใƒณใ‚ทใƒณใ‚ฐ๏ผš็ฝๅฎณใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใ€้ƒฝๅธ‚่จˆ็”ปใ€ๅคฉๅ€™ไบˆๆธฌ +* ๆฌ ้™ฅๆคœๅ‡บ๏ผšๅปบ็‰ฉใฎใ‚ฏใƒฉใƒƒใ‚ฏใ‚„ๆง‹้€ ไธŠใฎๆๅ‚ทใ€่ฃฝ้€ ไธŠใฎๆฌ ้™ฅใ‚’ๆคœๅ‡บ + + +```py +>>> from transformers import pipeline + +>>> detector = pipeline(task="object-detection") +>>> preds = detector( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"], "box": pred["box"]} for pred in preds] +>>> preds +[{'score': 0.9865, + 'label': 'cat', + 'box': {'xmin': 178, 'ymin': 154, 'xmax': 882, 'ymax': 598}}] +``` + +### Image segmentation + +็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏใ€็”ปๅƒๅ†…ใฎใ™ในใฆใฎใƒ”ใ‚ฏใ‚ปใƒซใ‚’ใ‚ฏใƒฉใ‚นใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ใƒ”ใ‚ฏใ‚ปใƒซใƒฌใƒ™ใƒซใฎใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ใ“ใ‚Œใฏใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใจใฏ็•ฐใชใ‚Šใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใƒฉใƒ™ใƒซไป˜ใ‘ใ—ใ€ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใซๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏใ‚ˆใ‚Š่ฉณ็ดฐใซใชใ‚Šใพใ™ใ€‚ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏใƒ”ใ‚ฏใ‚ปใƒซใƒฌใƒ™ใƒซใงใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใงใใพใ™ใ€‚็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใซใฏใ„ใใคใ‹ใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™๏ผš + +* ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ๏ผšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใ‚ฏใƒฉใ‚นใ‚’ใƒฉใƒ™ใƒซไป˜ใ‘ใ™ใ‚‹ใ ใ‘ใงใชใใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅ€‹ๅˆฅใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น๏ผˆ"็Šฌ-1"ใ€"็Šฌ-2"๏ผ‰ใ‚‚ใƒฉใƒ™ใƒซไป˜ใ‘ใ—ใพใ™ใ€‚ +* ใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ๏ผšใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใจใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎ็ต„ใฟๅˆใ‚ใ›ใ€‚ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ฏใƒฉใ‚นใ”ใจใซๅ„ใƒ”ใ‚ฏใ‚ปใƒซใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅ€‹ๅˆฅใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚‚ใƒฉใƒ™ใƒซไป˜ใ‘ใ—ใพใ™ใ€‚ + +ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚ฟใ‚นใ‚ฏใฏใ€่‡ชๅ‹•้‹่ปข่ปŠใซใจใฃใฆใ€ๅ‘จๅ›ฒใฎไธ–็•Œใฎใƒ”ใ‚ฏใ‚ปใƒซใƒฌใƒ™ใƒซใฎใƒžใƒƒใƒ—ใ‚’ไฝœๆˆใ—ใ€ๆญฉ่กŒ่€…ใ‚„ไป–ใฎ่ปŠไธกใ‚’ๅฎ‰ๅ…จใซๅ›ž้ฟใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ใพใŸใ€ๅŒป็™‚็”ปๅƒใงใฏใ€ใ‚ฟใ‚นใ‚ฏใฎ็ดฐใ‹ใ„็ฒ’ๅบฆใŒ็•ฐๅธธใช็ดฐ่ƒžใ‚„่‡“ๅ™จใฎ็‰นๅพดใ‚’่ญ˜ๅˆฅใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฏใ€eใ‚ณใƒžใƒผใ‚นใง่กฃ้กžใ‚’ไปฎๆƒณ็š„ใซ่ฉฆ็€ใ—ใŸใ‚Šใ€ใ‚ซใƒกใƒฉใ‚’้€šใ˜ใฆๅฎŸไธ–็•Œใซใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’้‡ใญใฆๆ‹กๅผต็พๅฎŸใฎไฝ“้จ“ใ‚’ไฝœๆˆใ—ใŸใ‚Šใ™ใ‚‹ใŸใ‚ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ + + + +```py +>>> from transformers import pipeline + +>>> segmenter = pipeline(task="image-segmentation") +>>> preds = segmenter( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> print(*preds, sep="\n") +{'score': 0.9879, 'label': 'LABEL_184'} +{'score': 0.9973, 'label': 'snow'} +{'score': 0.9972, 'label': 'cat'} +``` + +### Depth estimation + +ๆทฑๅบฆๆŽจๅฎšใฏใ€็”ปๅƒๅ†…ใฎๅ„ใƒ”ใ‚ฏใ‚ปใƒซใŒใ‚ซใƒกใƒฉใ‹ใ‚‰ใฎ่ท้›ขใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ“ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใฏใ€็‰นใซใ‚ทใƒผใƒณใฎ็†่งฃใจๅ†ๆง‹็ฏ‰ใซ้‡่ฆใงใ™ใ€‚ใŸใจใˆใฐใ€่‡ชๅ‹•้‹่ปข่ปŠใงใฏใ€ๆญฉ่กŒ่€…ใ€ไบค้€šๆจ™่ญ˜ใ€ไป–ใฎ่ปŠใชใฉใฎ็‰ฉไฝ“ใŒใฉใ‚Œใ ใ‘้ ใ„ใ‹ใ‚’็†่งฃใ—ใ€้šœๅฎณ็‰ฉใ‚„่ก็ชใ‚’ๅ›ž้ฟใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใงใ™ใ€‚ๆทฑๅบฆๆƒ…ๅ ฑใฏใพใŸใ€2D็”ปๅƒใ‹ใ‚‰3D่กจ็พใ‚’ๆง‹็ฏ‰ใ—ใ€็”Ÿ็‰ฉๅญฆ็š„ๆง‹้€ ใ‚„ๅปบ็‰ฉใฎ้ซ˜ๅ“่ณชใช3D่กจ็พใ‚’ไฝœๆˆใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ๆทฑๅบฆๆŽจๅฎšใซใฏๆฌกใฎ2ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใŒใ‚ใ‚Šใพใ™๏ผš + +* ใ‚นใƒ†ใƒฌใ‚ช๏ผšๆทฑๅบฆใฏใ€ใ‚ใšใ‹ใซ็•ฐใชใ‚‹่ง’ๅบฆใ‹ใ‚‰ใฎๅŒใ˜็”ปๅƒใฎ2ใคใฎ็”ปๅƒใ‚’ๆฏ”่ผƒใ—ใฆๆŽจๅฎšใ•ใ‚Œใพใ™ใ€‚ +* ใƒขใƒŽใ‚ญใƒฅใƒฉใƒผ๏ผšๆทฑๅบฆใฏๅ˜ไธ€ใฎ็”ปๅƒใ‹ใ‚‰ๆŽจๅฎšใ•ใ‚Œใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> depth_estimator = pipeline(task="depth-estimation") +>>> preds = depth_estimator( +... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +``` + + +## Natural language processing + +NLPใ‚ฟใ‚นใ‚ฏใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใŒ็งใŸใกใซใจใฃใฆ่‡ช็„ถใชใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณๆ‰‹ๆฎตใงใ‚ใ‚‹ใŸใ‚ใ€ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชใ‚ฟใ‚นใ‚ฏใฎไธ€ใคใงใ™ใ€‚ใƒขใƒ‡ใƒซใŒ่ช่ญ˜ใ™ใ‚‹ใŸใ‚ใฎๅฝขๅผใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใŒๅฟ…่ฆใงใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ˜่ชžใ‚„ใ‚ตใƒ–ใƒฏใƒผใƒ‰๏ผˆใƒˆใƒผใ‚ฏใƒณ๏ผ‰ใซๅˆ†ๅ‰ฒใ—ใ€ใใ‚Œใ‚‰ใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆ•ฐๅญ—ใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใใฎ็ตๆžœใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆ•ฐๅญ—ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใจใ—ใฆ่กจ็พใ—ใ€ไธ€ๅบฆๆ•ฐๅญ—ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒใ‚ใ‚Œใฐใ€ใ•ใพใ–ใพใชNLPใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใƒขใƒ‡ใƒซใซๅ…ฅๅŠ›ใงใใพใ™๏ผ + +### Text classification + +ใฉใ‚“ใชใƒขใƒ€ใƒชใƒ†ใ‚ฃใฎๅˆ†้กžใ‚ฟใ‚นใ‚ฏใจๅŒๆง˜ใซใ€ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใฏไบ‹ๅ‰ใซๅฎš็พฉใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚น๏ผˆๆ–‡ใƒฌใƒ™ใƒซใ€ๆฎต่ฝใ€ใพใŸใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ‰ใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใซใฏๅคšใใฎๅฎŸ็”จ็š„ใช็”จ้€”ใŒใ‚ใ‚Šใ€ใใฎไธ€้ƒจใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +* ๆ„Ÿๆƒ…ๅˆ†ๆž๏ผš`positive`ใ‚„`negative`ใฎใ‚ˆใ†ใชๆฅตๆ€งใซๅพ“ใฃใฆใƒ†ใ‚ญใ‚นใƒˆใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใ€ๆ”ฟๆฒปใ€้‡‘่žใ€ใƒžใƒผใ‚ฑใƒ†ใ‚ฃใƒณใ‚ฐใชใฉใฎๅˆ†้‡Žใงใฎๆ„ๆ€ๆฑบๅฎšใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ +* ใ‚ณใƒณใƒ†ใƒณใƒ„ๅˆ†้กž๏ผšใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒ”ใƒƒใ‚ฏใซๅพ“ใฃใฆใƒฉใƒ™ใƒซไป˜ใ‘ใ—ใ€ใƒ‹ใƒฅใƒผใ‚นใ‚„ใ‚ฝใƒผใ‚ทใƒฃใƒซใƒกใƒ‡ใ‚ฃใ‚ขใฎใƒ•ใ‚ฃใƒผใƒ‰ๅ†…ใฎๆƒ…ๅ ฑใ‚’ๆ•ด็†ใ—ใ€ใƒ•ใ‚ฃใƒซใ‚ฟใƒชใƒณใ‚ฐใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™๏ผˆ`ๅคฉๆฐ—`ใ€`ใ‚นใƒใƒผใƒ„`ใ€`้‡‘่ž`ใชใฉ๏ผ‰ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="sentiment-analysis") +>>> preds = classifier("Hugging Face is the best thing since sliced bread!") +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.9991, 'label': 'POSITIVE'}] +``` + +### Token classification + +ใฉใ‚“ใชNLPใ‚ฟใ‚นใ‚ฏใงใ‚‚ใ€ใƒ†ใ‚ญใ‚นใƒˆใฏใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ€‹ใ€…ใฎๅ˜่ชžใ‚„ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใ—ใฆๅ‰ๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ[ใƒˆใƒผใ‚ฏใƒณ](/glossary#token)ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใฏใ€ไบ‹ๅ‰ใซๅฎš็พฉใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰ๅ„ใƒˆใƒผใ‚ฏใƒณใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใฎไธ€่ˆฌ็š„ใชใ‚ฟใ‚คใƒ—ใฏๆฌกใฎ2ใคใงใ™๏ผš + +* ๅ›บๆœ‰่กจ็พ่ช่ญ˜๏ผˆNER๏ผ‰๏ผš็ต„็น”ใ€ไบบ็‰ฉใ€ๅ ดๆ‰€ใ€ๆ—ฅไป˜ใชใฉใฎใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎใ‚ซใƒ†ใ‚ดใƒชใซๅพ“ใฃใฆใƒˆใƒผใ‚ฏใƒณใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใพใ™ใ€‚NERใฏ็‰นใซใƒใ‚คใ‚ชใƒกใƒ‡ใ‚ฃใ‚ซใƒซ็’ฐๅขƒใงไบบๆฐ—ใงใ‚ใ‚Šใ€้บไผๅญใ€ใ‚ฟใƒณใƒ‘ใ‚ฏ่ณชใ€่–ฌ็‰ฉๅใชใฉใ‚’ใƒฉใƒ™ใƒซไป˜ใ‘ใงใใพใ™ใ€‚ +* ๅ“่ฉžใ‚ฟใ‚ฐไป˜ใ‘๏ผˆPOS๏ผ‰๏ผšๅ่ฉžใ€ๅ‹•่ฉžใ€ๅฝขๅฎน่ฉžใชใฉใฎๅ“่ฉžใซๅพ“ใฃใฆใƒˆใƒผใ‚ฏใƒณใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใพใ™ใ€‚POSใฏใ€็ฟป่จณใ‚ทใ‚นใƒ†ใƒ ใŒๅŒใ˜ๅ˜่ชžใŒๆ–‡ๆณ•็š„ใซใฉใฎใ‚ˆใ†ใซ็•ฐใชใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™๏ผˆๅ่ฉžใจใ—ใฆใฎใ€Œ้Š€่กŒใ€ใจๅ‹•่ฉžใจใ—ใฆใฎใ€Œ้Š€่กŒใ€ใชใฉ๏ผ‰ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline(task="ner") +>>> preds = classifier("Hugging Face is a French company based in New York City.") +>>> preds = [ +... { +... "entity": pred["entity"], +... "score": round(pred["score"], 4), +... "index": pred["index"], +... "word": pred["word"], +... "start": pred["start"], +... "end": pred["end"], +... } +... for pred in preds +... ] +>>> print(*preds, sep="\n") +{'entity': 'I-ORG', 'score': 0.9968, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2} +{'entity': 'I-ORG', 'score': 0.9293, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7} +{'entity': 'I-ORG', 'score': 0.9763, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12} +{'entity': 'I-MISC', 'score': 0.9983, 'index': 6, 'word': 'French', 'start': 18, 'end': 24} +{'entity': 'I-LOC', 'score': 0.999, 'index': 10, 'word': 'New', 'start': 42, 'end': 45} +{'entity': 'I-LOC', 'score': 0.9987, 'index': 11, 'word': 'York', 'start': 46, 'end': 50} +{'entity': 'I-LOC', 'score': 0.9992, 'index': 12, 'word': 'City', 'start': 51, 'end': 55} +``` + +### Question answering + +่ณชๅ•ๅฟœ็ญ”ใฏใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ๏ผˆใ‚ชใƒผใƒ—ใƒณใƒ‰ใƒกใ‚คใƒณ๏ผ‰ใ‚’ๅซใ‚€ๅ ดๅˆใจๅซใพใชใ„ๅ ดๅˆ๏ผˆใ‚ฏใƒญใƒผใ‚บใƒ‰ใƒ‰ใƒกใ‚คใƒณ๏ผ‰ใŒใ‚ใ‚‹ๅ ดๅˆใ‚‚ใ‚ใ‚‹ใ€ๅˆฅใฎใƒˆใƒผใ‚ฏใƒณใƒฌใƒ™ใƒซใฎใ‚ฟใ‚นใ‚ฏใงใ€่ณชๅ•ใซๅฏพใ™ใ‚‹ๅ›ž็ญ”ใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใฏใ€ไปฎๆƒณใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใซใƒฌใ‚นใƒˆใƒฉใƒณใŒๅ–ถๆฅญใ—ใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใฎใ‚ˆใ†ใช่ณชๅ•ใ‚’ใ™ใ‚‹ใจใใซ็™บ็”Ÿใ—ใพใ™ใ€‚ใพใŸใ€้กงๅฎขใ‚„ๆŠ€่ก“ใ‚ตใƒใƒผใƒˆใ‚’ๆไพ›ใ—ใ€ๆคœ็ดขใ‚จใƒณใ‚ธใƒณใŒใ‚ใชใŸใŒๆฑ‚ใ‚ใฆใ„ใ‚‹้–ข้€ฃๆƒ…ๅ ฑใ‚’ๅ–ๅพ—ใ™ใ‚‹ใฎใซใ‚‚ๅฝน็ซ‹ใกใพใ™ใ€‚ + +่ณชๅ•ๅฟœ็ญ”ใฎไธ€่ˆฌ็š„ใชใ‚ฟใ‚คใƒ—ใฏๆฌกใฎ2ใคใงใ™๏ผš + +* ๆŠฝๅ‡บๅž‹๏ผš่ณชๅ•ใจไธ€้ƒจใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใ€ใƒขใƒ‡ใƒซใŒใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ๆŠฝๅ‡บใ™ใ‚‹ๅฟ…่ฆใฎใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚นใƒ‘ใƒณใŒๅ›ž็ญ”ใจใชใ‚Šใพใ™ใ€‚ +* ๆŠฝ่ฑก็š„๏ผš่ณชๅ•ใจไธ€้ƒจใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใ€ๅ›ž็ญ”ใฏใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€[`QuestionAnsweringPipeline`]ใงใฏใชใ[`Text2TextGenerationPipeline`]ใงๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> question_answerer = pipeline(task="question-answering") +>>> preds = question_answerer( +... question="What is the name of the repository?", +... context="The name of the repository is huggingface/transformers", +... ) +>>> print( +... f"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}" +... ) +score: 0.9327, start: 30, end: 54, answer: huggingface/transformers +``` + +### Summarization + +่ฆ็ด„ใฏใ€้•ทใ„ใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰็Ÿญใ„ใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝœๆˆใ—ใ€ๅ…ƒใฎๆ–‡ๆ›ธใฎๆ„ๅ‘ณใฎๅคง้ƒจๅˆ†ใ‚’ไฟใกใชใŒใ‚‰่ฉฆใฟใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚่ฆ็ด„ใฏใ‚ทใƒผใ‚ฑใƒณใ‚นใ‹ใ‚‰ใ‚ทใƒผใ‚ฑใƒณใ‚นใธใฎใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚Šใ€ๅ…ฅๅŠ›ใ‚ˆใ‚Šใ‚‚็Ÿญใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚่ฆ็ด„ใ‚’่กŒใ†ใ“ใจใงใ€่ชญ่€…ใŒไธป่ฆใชใƒใ‚คใƒณใƒˆใ‚’่ฟ…้€Ÿใซ็†่งฃใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใฎใซๅฝน็ซ‹ใค้•ทๆ–‡ๆ›ธใŒใŸใใ•ใ‚“ใ‚ใ‚Šใพใ™ใ€‚ๆณ•ๆกˆใ€ๆณ•็š„ใŠใ‚ˆใณ่ฒกๅ‹™ๆ–‡ๆ›ธใ€็‰น่จฑใ€็ง‘ๅญฆ่ซ–ๆ–‡ใชใฉใ€่ชญ่€…ใฎๆ™‚้–“ใ‚’็ฏ€็ด„ใ—่ชญๆ›ธใฎๆ”ฏๆดใจใชใ‚‹ๆ–‡ๆ›ธใฎไพ‹ใŒใ‚ใ‚Šใพใ™ใ€‚ + +่ณชๅ•ๅฟœ็ญ”ใจๅŒๆง˜ใซใ€่ฆ็ด„ใซใฏ2ใคใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™๏ผš + +* ๆŠฝๅ‡บ็š„่ฆ็ด„๏ผšๅ…ƒใฎใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ๆœ€ใ‚‚้‡่ฆใชๆ–‡ใ‚’่ญ˜ๅˆฅใ—ใฆๆŠฝๅ‡บใ—ใพใ™ใ€‚ +* ๆŠฝ่ฑก็š„่ฆ็ด„๏ผšๅ…ƒใฎใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใฎ่ฆ็ด„๏ผˆๅ…ฅๅŠ›ๆ–‡ๆ›ธใซๅซใพใ‚Œใฆใ„ใชใ„ๆ–ฐใ—ใ„ๅ˜่ชžใ‚’ๅซใ‚€ใ“ใจใŒใ‚ใ‚Šใพใ™๏ผ‰ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚[`SummarizationPipeline`]ใฏๆŠฝ่ฑก็š„ใชใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> summarizer = pipeline(task="summarization") +>>> summarizer( +... "In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. In the former task our best model outperforms even all previously reported ensembles." +... ) +[{'summary_text': ' The Transformer is the first sequence transduction model based entirely on attention . It replaces the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention . For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers .'}] +``` + +### Translation + +็ฟป่จณใฏใ€ใ‚ใ‚‹่จ€่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆฅใฎ่จ€่ชžใซๅค‰ๆ›ใ™ใ‚‹ไฝœๆฅญใงใ™ใ€‚ใ“ใ‚Œใฏ็•ฐใชใ‚‹ใƒใƒƒใ‚ฏใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ใ‚’ๆŒใคไบบใ€…ใŒใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’ใจใ‚‹ใฎใซๅฝน็ซ‹ใกใ€ๅบƒ็ฏ„ใช่ฆณๅฎขใซใ‚ณใƒณใƒ†ใƒณใƒ„ใ‚’็ฟป่จณใ—ใฆไผใˆใ‚‹ใฎใซๅฝน็ซ‹ใกใ€ๆ–ฐใ—ใ„่จ€่ชžใ‚’ๅญฆใถใฎใ‚’ๆ”ฏๆดใ™ใ‚‹ๅญฆ็ฟ’ใƒ„ใƒผใƒซใซใ‚‚ใชใ‚Šใพใ™ใ€‚่ฆ็ด„ใจๅ…ฑใซใ€็ฟป่จณใฏใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใฎใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใฏๅ…ฅๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใฎๅ‡บๅŠ›ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +ๅˆๆœŸใฎ็ฟป่จณใƒขใƒ‡ใƒซใฏไธปใซๅ˜ไธ€่จ€่ชžใงใ—ใŸใŒใ€ๆœ€่ฟ‘ใงใฏๅคš่จ€่ชžใƒขใƒ‡ใƒซใซๅฏพใ™ใ‚‹้–ขๅฟƒใŒ้ซ˜ใพใ‚Šใ€ๅคšใใฎ่จ€่ชžๅฏพใง็ฟป่จณใงใใ‚‹ใ‚ˆใ†ใชๅคš่จ€่ชžใƒขใƒ‡ใƒซใซๆณจ็›ฎใŒ้›†ใพใฃใฆใ„ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> text = "translate English to French: Hugging Face is a community-based open-source platform for machine learning." +>>> translator = pipeline(task="translation", model="google-t5/t5-small") +>>> translator(text) +[{'translation_text': "Hugging Face est une tribune communautaire de l'apprentissage des machines."}] +``` + +### ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ + +่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ไบ‹ๅ‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ๅคšใใฎไป–ใฎใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใŸใ‚ใ€้žๅธธใซไบบๆฐ—ใฎใ‚ใ‚‹NLPใ‚ฟใ‚นใ‚ฏใจใชใฃใฆใ„ใพใ™ใ€‚ๆœ€่ฟ‘ใงใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใพใŸใฏใƒ•ใƒฅใƒผใ‚ทใƒงใƒƒใƒˆๅญฆ็ฟ’ใ‚’ๅฎŸ่จผใ™ใ‚‹ๅคง่ฆๆจกใช่จ€่ชžใƒขใƒ‡ใƒซ๏ผˆLLM๏ผ‰ใซๅคงใใช้–ขๅฟƒใŒๅฏ„ใ›ใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใŒๆ˜Ž็คบ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™๏ผ่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ๆตๆšขใง่ชฌๅพ—ๅŠ›ใฎใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใŒใ€ใƒ†ใ‚ญใ‚นใƒˆใŒๅธธใซๆญฃ็ขบใงใ‚ใ‚‹ใ‚ใ‘ใงใฏใชใ„ใŸใ‚ใ€ๆณจๆ„ใŒๅฟ…่ฆใงใ™ใ€‚ + +่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใซใฏ2ใคใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™๏ผš + +* ๅ› ๆžœ็š„๏ผšใƒขใƒ‡ใƒซใฎ็›ฎๆจ™ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใงใ‚ใ‚Šใ€ๅฐ†ๆฅใฎใƒˆใƒผใ‚ฏใƒณใฏใƒžใ‚นใ‚ฏใ•ใ‚Œใพใ™ใ€‚ + + ```py + >>> from transformers import pipeline + + >>> prompt = "Hugging Face is a community-based open-source platform for machine learning." + >>> generator = pipeline(task="text-generation") + >>> generator(prompt) # doctest: +SKIP + ``` + +* ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ๏ผšใƒขใƒ‡ใƒซใฎ็›ฎ็š„ใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎใƒˆใƒผใ‚ฏใƒณๅ…จไฝ“ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใชใŒใ‚‰ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + + ```py + >>> text = "Hugging Face is a community-based open-source for machine learning." + >>> fill_mask = pipeline(task="fill-mask") + >>> preds = fill_mask(text, top_k=1) + >>> preds = [ + ... { + ... "score": round(pred["score"], 4), + ... "token": pred["token"], + ... "token_str": pred["token_str"], + ... "sequence": pred["sequence"], + ... } + ... for pred in preds + ... ] + >>> preds + [{'score': 0.2236, + 'token': 1761, + 'token_str': ' platform', + 'sequence': 'Hugging Face is a community-based open-source platform for machine learning.'}] + ``` + +## Multimodal + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใฏใ€็‰นๅฎšใฎๅ•้กŒใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซ่ค‡ๆ•ฐใฎใƒ‡ใƒผใ‚ฟใƒขใƒ€ใƒชใƒ†ใ‚ฃ๏ผˆใƒ†ใ‚ญใ‚นใƒˆใ€็”ปๅƒใ€้Ÿณๅฃฐใ€ใƒ“ใƒ‡ใ‚ช๏ผ‰ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซใƒขใƒ‡ใƒซใ‚’ๅฟ…่ฆใจใ—ใพใ™ใ€‚็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒ‹ใƒณใ‚ฐใฏใ€ใƒขใƒ‡ใƒซใŒๅ…ฅๅŠ›ใจใ—ใฆ็”ปๅƒใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€็”ปๅƒใ‚’่ชฌๆ˜Žใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใพใŸใฏ็”ปๅƒใฎใ„ใใคใ‹ใฎ็‰นๆ€งใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใ‚ฟใ‚นใ‚ฏใฎไพ‹ใงใ™ใ€‚ + +ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซใƒขใƒ‡ใƒซใฏ็•ฐใชใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ฟใ‚คใƒ—ใพใŸใฏใƒขใƒ€ใƒชใƒ†ใ‚ฃใงไฝœๆฅญใ—ใพใ™ใŒใ€ๅ†…้ƒจ็š„ใซใฏๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใŒใƒขใƒ‡ใƒซใซใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใ‚ฟใ‚คใƒ—ใ‚’ๅŸ‹ใ‚่พผใฟ๏ผˆใƒ‡ใƒผใ‚ฟใซ้–ขใ™ใ‚‹ๆ„ๅ‘ณใฎใ‚ใ‚‹ๆƒ…ๅ ฑใ‚’ไฟๆŒใ™ใ‚‹ใƒ™ใ‚ฏใƒˆใƒซใพใŸใฏๆ•ฐๅญ—ใฎใƒชใ‚นใƒˆ๏ผ‰ใซๅค‰ๆ›ใ™ใ‚‹ใฎใ‚’ๆ”ฏๆดใ—ใพใ™ใ€‚็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒ‹ใƒณใ‚ฐใฎใ‚ˆใ†ใชใ‚ฟใ‚นใ‚ฏใงใฏใ€ใƒขใƒ‡ใƒซใฏ็”ปๅƒใฎๅŸ‹ใ‚่พผใฟใจใƒ†ใ‚ญใ‚นใƒˆใฎๅŸ‹ใ‚่พผใฟใฎ้–“ใฎ้–ขไฟ‚ใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ + +### Document question answering + +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ่ณชๅ•ๅฟœ็ญ”ใฏใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‹ใ‚‰ใฎ่‡ช็„ถ่จ€่ชžใฎ่ณชๅ•ใซ็ญ”ใˆใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒณใƒฌใƒ™ใƒซใฎ่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใจใฏ็•ฐใชใ‚Šใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ่ณชๅ•ๅฟœ็ญ”ใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎ็”ปๅƒใจใใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซ้–ขใ™ใ‚‹่ณชๅ•ใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€็ญ”ใˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ่ณชๅ•ๅฟœ็ญ”ใฏๆง‹้€ ๅŒ–ใ•ใ‚ŒใŸใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’่งฃๆžใ—ใ€ใใ‚Œใ‹ใ‚‰้‡่ฆใชๆƒ…ๅ ฑใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚ไปฅไธ‹ใฎไพ‹ใงใฏใ€ใƒฌใ‚ทใƒผใƒˆใ‹ใ‚‰ๅˆ่จˆ้‡‘้กใจใŠ้‡ฃใ‚Šใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline +>>> from PIL import Image +>>> import requests + +>>> url = "https://huggingface.co/datasets/hf-internal-testing/example-documents/resolve/main/jpeg_images/2.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> doc_question_answerer = pipeline("document-question-answering", model="magorshunov/layoutlm-invoices") +>>> preds = doc_question_answerer( +... question="What is the total amount?", +... image=image, +... ) +>>> preds +[{'score': 0.8531, 'answer': '17,000', 'start': 4, 'end': 4}] +``` + +ใ“ใฎใƒšใƒผใ‚ธใŒๅ„ใƒขใƒ€ใƒชใƒ†ใ‚ฃใฎใ‚ฟใ‚นใ‚ฏใฎ็จฎ้กžใจใใ‚Œใžใ‚Œใฎ้‡่ฆๆ€งใซใคใ„ใฆใฎ่ฟฝๅŠ ใฎ่ƒŒๆ™ฏๆƒ…ๅ ฑใ‚’ๆไพ›ใงใใŸใ“ใจใ‚’้ก˜ใฃใฆใ„ใพใ™ใ€‚ๆฌกใฎ [ใ‚ปใ‚ฏใ‚ทใƒงใƒณ](tasks_explained) ใงใฏใ€๐Ÿค— ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใŒใ“ใ‚Œใ‚‰ใฎใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซ **ใฉใฎใ‚ˆใ†ใซ** ๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’ๅญฆใณใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/asr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/asr.md new file mode 100644 index 0000000000000000000000000000000000000000..5e460a102f440899ef3bbb29e744b5087b2d7946 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/asr.md @@ -0,0 +1,375 @@ + + +# Automatic speech recognition + +[[open-in-colab]] + + + +่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ (ASR) ใฏ้Ÿณๅฃฐไฟกๅทใ‚’ใƒ†ใ‚ญใ‚นใƒˆใซๅค‰ๆ›ใ—ใ€ไธ€้€ฃใฎ้Ÿณๅฃฐๅ…ฅๅŠ›ใ‚’ใƒ†ใ‚ญใ‚นใƒˆๅ‡บๅŠ›ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ—ใพใ™ใ€‚ Siri ใ‚„ Alexa ใชใฉใฎไปฎๆƒณใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใฏ ASR ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒฆใƒผใ‚ถใƒผใ‚’ๆ—ฅๅธธ็š„ใซๆ”ฏๆดใ—ใฆใŠใ‚Šใ€ใƒฉใ‚คใƒ–ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚„ไผš่ญฐไธญใฎใƒกใƒขๅ–ใ‚Šใชใฉใ€ไป–ใซใ‚‚ไพฟๅˆฉใชใƒฆใƒผใ‚ถใƒผๅ‘ใ‘ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใŒๆ•ฐๅคšใใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€้Ÿณๅฃฐใ‚’ใƒ†ใ‚ญใ‚นใƒˆใซๆ›ธใ่ตทใ“ใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/automatic-speech-recognition) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate jiwer +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load MInDS-14 dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train[:100]") +``` + +[`~Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train` ๅˆ†ๅ‰ฒใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 16 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 4 + }) +}) +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ`lang_id`ใ‚„`english_transcription`ใชใฉใฎๅคšใใฎๆœ‰็”จใชๆƒ…ๅ ฑใŒๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€Œ`audio`ใ€ใจใ€Œ`transciption`ใ€ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ [`~datasets.Dataset.remove_columns`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆไป–ใฎๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> minds = minds.remove_columns(["english_transcription", "intent_class", "lang_id"]) +``` + +ใ‚‚ใ†ไธ€ๅบฆไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> minds["train"][0] +{'audio': {'array': array([-0.00024414, 0. , 0. , ..., 0.00024414, + 0.00024414, 0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 8000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +ๆฌกใฎ 2 ใคใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `audio`: ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซๅ‘ผใณๅ‡บใ™ๅฟ…่ฆใŒใ‚ใ‚‹้Ÿณๅฃฐไฟกๅทใฎ 1 ๆฌกๅ…ƒใฎ `array`ใ€‚ +- `transcription`: ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใƒ†ใ‚ญใ‚นใƒˆใ€‚ + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€Wav2Vec2 ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชไฟกๅทใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base") +``` + +MInDS-14 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใฏ 8000kHz ใงใ™ (ใ“ใฎๆƒ…ๅ ฑใฏ [ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใ‚ซใƒผใƒ‰](https://huggingface.co/datasets/PolyAI/minds14) ใง็ขบ่ชใงใใพใ™)ใ€‚ใคใพใ‚Šใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ†ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ Wav2Vec2 ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€16000kHz ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([-2.38064706e-04, -1.58618059e-04, -5.43987835e-06, ..., + 2.78103951e-04, 2.38446111e-04, 1.18740834e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 16000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +ไธŠใฎ `transcription` ใงใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซใ€ใƒ†ใ‚ญใ‚นใƒˆใซใฏๅคงๆ–‡ๅญ—ใจๅฐๆ–‡ๅญ—ใŒๆททๅœจใ—ใฆใ„ใพใ™ใ€‚ Wav2Vec2 ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฏๅคงๆ–‡ๅญ—ใฎใฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใƒ†ใ‚ญใ‚นใƒˆใŒใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ่ชžๅฝ™ใจไธ€่‡ดใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> def uppercase(example): +... return {"transcription": example["transcription"].upper()} + + +>>> minds = minds.map(uppercase) +``` + +ๆฌกใซใ€ๆฌกใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +1. `audio`ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ +2. ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ `input_values` ใ‚’ๆŠฝๅ‡บใ—ใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆ `transcription` ๅˆ—ใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> def prepare_dataset(batch): +... audio = batch["audio"] +... batch = processor(audio["array"], sampling_rate=audio["sampling_rate"], text=batch["transcription"]) +... batch["input_length"] = len(batch["input_values"][0]) +... return batch +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `num_proc` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใƒ—ใƒญใ‚ปใ‚นใฎๆ•ฐใ‚’ๅข—ใ‚„ใ™ใ“ใจใงใ€`map` ใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ [`~datasets.Dataset.remove_columns`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ไธ่ฆใชๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> encoded_minds = minds.map(prepare_dataset, remove_columns=minds.column_names["train"], num_proc=4) +``` + +๐Ÿค— Transformers ใซใฏ ASR ็”จใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใŒใชใ„ใŸใ‚ใ€[`DataCollatโ€‹โ€‹orWithPadding`] ใ‚’่ชฟๆ•ดใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใพใŸใ€ใƒ†ใ‚ญใ‚นใƒˆใจใƒฉใƒ™ใƒซใŒ (ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใงใฏใชใ) ใƒใƒƒใƒๅ†…ใฎๆœ€ใ‚‚้•ทใ„่ฆ็ด ใฎ้•ทใ•ใซๅˆใ‚ใ›ใฆๅ‹•็š„ใซๅŸ‹ใ‚่พผใพใ‚Œใ€ๅ‡ไธ€ใช้•ทใ•ใซใชใ‚Šใพใ™ใ€‚ `padding=True` ใ‚’่จญๅฎšใ™ใ‚‹ใจใ€`tokenizer` ้–ขๆ•ฐใงใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅŸ‹ใ‚่พผใ‚€ใ“ใจใŒใงใใพใ™ใŒใ€ๅ‹•็š„ใชๅŸ‹ใ‚่พผใฟใฎๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ + +ไป–ใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใฏ็•ฐใชใ‚Šใ€ใ“ใฎ็‰นๅฎšใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใฏใ€`input_values`ใจ `labels`ใ€ใซ็•ฐใชใ‚‹ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐๆ–นๆณ•ใ‚’้ฉ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> import torch + +>>> from dataclasses import dataclass, field +>>> from typing import Any, Dict, List, Optional, Union + + +>>> @dataclass +... class DataCollatorCTCWithPadding: +... processor: AutoProcessor +... padding: Union[bool, str] = "longest" + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... # split inputs and labels since they have to be of different lengths and need +... # different padding methods +... input_features = [{"input_values": feature["input_values"][0]} for feature in features] +... label_features = [{"input_ids": feature["labels"]} for feature in features] + +... batch = self.processor.pad(input_features, padding=self.padding, return_tensors="pt") + +... labels_batch = self.processor.pad(labels=label_features, padding=self.padding, return_tensors="pt") + +... # replace padding with -100 to ignore loss correctly +... labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) + +... batch["labels"] = labels + +... return batch +``` + +ๆฌกใซใ€`DataCollatโ€‹โ€‹orForCTCWithPadding` ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> data_collator = DataCollatorCTCWithPadding(processor=processor, padding="longest") +``` + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[ๅ˜่ชžใ‚จใƒฉใƒผ็އ](https://huggingface.co/spaces/evaluate-metric/wer) (WER) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใ€ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่จˆ็ฎ—ใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„)ใ€‚ + +```py +>>> import evaluate + +>>> wer = evaluate.load("wer") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ WER ใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + + +>>> def compute_metrics(pred): +... pred_logits = pred.predictions +... pred_ids = np.argmax(pred_logits, axis=-1) + +... pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id + +... pred_str = processor.batch_decode(pred_ids) +... label_str = processor.batch_decode(pred.label_ids, group_tokens=False) + +... wer = wer.compute(predictions=pred_str, references=label_str) + +... return {"wer": wer} +``` + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForCTC`] ใง Wav2Vec2 ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ `ctc_loss_reduction` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใง้ฉ็”จใ™ใ‚‹ๅ‰Šๆธ›ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ๅคšใใฎๅ ดๅˆใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅˆ่จˆใงใฏใชใๅนณๅ‡ใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นใŒ้ฉๅˆ‡ใงใ™ใ€‚ + +```py +>>> from transformers import AutoModelForCTC, TrainingArguments, Trainer + +>>> model = AutoModelForCTC.from_pretrained( +... "facebook/wav2vec2-base", +... ctc_loss_reduction="mean", +... pad_token_id=processor.tokenizer.pad_token_id, +... ) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`ใƒˆใƒฌใƒผใƒŠใƒผ`] ใฏ WER ใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_asr_mind_model", +... per_device_train_batch_size=8, +... gradient_accumulation_steps=2, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=2000, +... gradient_checkpointing=True, +... fp16=True, +... group_by_length=True, +... eval_strategy="steps", +... per_device_eval_batch_size=8, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... load_best_model_at_end=True, +... metric_for_best_model="wer", +... greater_is_better=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=processor, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + + + +่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎใ‚ˆใ‚Š่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€่‹ฑ่ชž ASR ใŠใ‚ˆใณ่‹ฑ่ชžใฎใ“ใฎใƒ–ใƒญใ‚ฐ [ๆŠ•็จฟ](https://huggingface.co/blog/fine-tune-wav2vec2-english) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ๅคš่จ€่ชž ASR ใซใคใ„ใฆใฏใ€ใ“ใฎ [ๆŠ•็จฟ](https://huggingface.co/blog/fine-tune-xlsr-wav2vec2) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใŸใ„้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใ‚’ใƒขใƒ‡ใƒซใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใจไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") +>>> transcriber(audio_file) +{'text': 'I WOUD LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'} +``` + + + +่ปขๅ†™ใฏใพใ‚ใพใ‚ใงใ™ใŒใ€ใ‚‚ใฃใจ่‰ฏใใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ•ใ‚‰ใซ่‰ฏใ„็ตๆžœใ‚’ๅพ—ใ‚‹ใซใฏใ€ใ‚ˆใ‚Šๅคšใใฎไพ‹ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + + + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใ€Œใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ€ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + + +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใจๆ–‡ๅญ—่ตทใ“ใ—ใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€`input`ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +Pass your inputs to the model and return the logits: + +```py +>>> from transformers import AutoModelForCTC + +>>> model = AutoModelForCTC.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงไบˆๆธฌใ•ใ‚ŒใŸ `input_ids` ใ‚’ๅ–ๅพ—ใ—ใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆไบˆๆธฌใ•ใ‚ŒใŸ `input_ids` ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + + +```py +>>> import torch + +>>> predicted_ids = torch.argmax(logits, dim=-1) +>>> transcription = processor.batch_decode(predicted_ids) +>>> transcription +['I WOUL LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'] +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/audio_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/audio_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..3b33d1b6043d782137c4ba0ec45b5acf2fd343ca --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/audio_classification.md @@ -0,0 +1,323 @@ + + +# Audio classification + +[[open-in-colab]] + + + + +้Ÿณๅฃฐๅˆ†้กžใงใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใจๅŒๆง˜ใซใ€ๅ…ฅๅŠ›ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ๅ‡บๅŠ›ใ•ใ‚ŒใŸใ‚ฏใƒฉใ‚น ใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ๅ”ฏไธ€ใฎ้•ใ„ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใฎไปฃใ‚ใ‚Šใซ็”Ÿใฎใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚้Ÿณๅฃฐๅˆ†้กžใฎๅฎŸ้š›็š„ใชๅฟœ็”จไพ‹ใซใฏใ€่ฉฑ่€…ใฎๆ„ๅ›ณใ€่จ€่ชžๅˆ†้กžใ€ใ•ใ‚‰ใซใฏ้Ÿณใซใ‚ˆใ‚‹ๅ‹•็‰ฉใฎ็จฎ้กžใฎ่ญ˜ๅˆฅใชใฉใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆ่ฉฑ่€…ใฎๆ„ๅ›ณใ‚’ๅˆ†้กžใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/audio-classification) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +```bash +pip install transformers datasets evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load MInDS-14 dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ MInDS-14 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train` ใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใƒˆใƒฌใ‚คใƒณใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 450 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 113 + }) +}) +``` + + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ`lang_id`ใ‚„`english_transcription`ใชใฉใฎๅคšใใฎๆœ‰็”จใชๆƒ…ๅ ฑใŒๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏ`audio`ใจ`intent_class`ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ [`~datasets.Dataset.remove_columns`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆไป–ใฎๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> minds = minds.remove_columns(["path", "transcription", "english_transcription", "lang_id"]) +``` + +ใ“ใ“ใงไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> minds["train"][0] +{'audio': {'array': array([ 0. , 0. , 0. , ..., -0.00048828, + -0.00024414, -0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 8000}, + 'intent_class': 2} +``` + +ๆฌกใฎ 2 ใคใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `audio`: ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใซๅ‘ผใณๅ‡บใ™ๅฟ…่ฆใŒใ‚ใ‚‹้Ÿณๅฃฐไฟกๅทใฎ 1 ๆฌกๅ…ƒใฎ `array`ใ€‚ +- `intent_class`: ใ‚นใƒ”ใƒผใ‚ซใƒผใฎใ‚คใƒณใƒ†ใƒณใƒˆใฎใ‚ฏใƒฉใ‚น ID ใ‚’่กจใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใŒใƒฉใƒ™ใƒซ ID ใ‹ใ‚‰ใƒฉใƒ™ใƒซๅใ‚’ๅ–ๅพ—ใ—ใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซใ€ใƒฉใƒ™ใƒซๅใ‚’ๆ•ดๆ•ฐใซใ€ใพใŸใฏใใฎ้€†ใซใƒžใƒƒใƒ—ใ™ใ‚‹่พžๆ›ธใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> labels = minds["train"].features["intent_class"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +ใ“ใ‚Œใงใ€ใƒฉใƒ™ใƒซ ID ใ‚’ใƒฉใƒ™ใƒซๅใซๅค‰ๆ›ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```py +>>> id2label[str(2)] +'app_error' +``` + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€Wav2Vec2 ็‰นๅพดๆŠฝๅ‡บใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชไฟกๅทใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +MInDS-14 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใฏ 8khz ใงใ™ (ใ“ใฎๆƒ…ๅ ฑใฏ [ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใ‚ซใƒผใƒ‰](https://huggingface.co/datasets/PolyAI/minds14) ใง็ขบ่ชใงใใพใ™)ใ€‚ใคใพใ‚Šใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ†ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ Wav2Vec2 ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€16kHz ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([ 2.2098757e-05, 4.6582241e-05, -2.2803260e-05, ..., + -2.8419291e-04, -2.3305941e-04, -1.1425107e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 16000}, + 'intent_class': 2} +``` + +ๆฌกใซใ€ๆฌกใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +1. `audio`ๅˆ—ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒญใƒผใƒ‰ใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ +2. ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใŒใ€ใƒขใƒ‡ใƒซใŒไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใจไธ€่‡ดใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ใ“ใฎๆƒ…ๅ ฑใฏใ€Wav2Vec2 [ใƒขใƒ‡ใƒซ ใ‚ซใƒผใƒ‰](https://huggingface.co/facebook/wav2vec2-base) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +3. ๅ…ฅๅŠ›ใฎๆœ€ๅคง้•ทใ‚’่จญๅฎšใ—ใฆใ€้•ทใ„ๅ…ฅๅŠ›ใ‚’ๅˆ‡ใ‚Šๆจใฆใšใซใƒใƒƒใƒๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, sampling_rate=feature_extractor.sampling_rate, max_length=16000, truncation=True +... ) +... return inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ไธ่ฆใชๅˆ—ใ‚’ๅ‰Š้™คใ—ใ€`intent_class` ใฎๅๅ‰ใ‚’ `label` ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใƒขใƒ‡ใƒซใŒๆœŸๅพ…ใ™ใ‚‹ๅๅ‰ใงใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +```py +>>> encoded_minds = minds.map(preprocess_function, remove_columns="audio", batched=True) +>>> encoded_minds = encoded_minds.rename_column("intent_class", "label") +``` +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ใƒกใƒˆใƒชใ‚ฏใ‚นใฎ่ชญใฟ่พผใฟใจ่จˆ็ฎ—ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions = np.argmax(eval_pred.predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForAudioClassification`] ใ‚’ไฝฟ็”จใ—ใฆใ€ไบˆๆœŸใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใจใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆ Wav2Vec2 ใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForAudioClassification, TrainingArguments, Trainer + +>>> num_labels = len(id2label) +>>> model = AutoModelForAudioClassification.from_pretrained( +... "facebook/wav2vec2-base", num_labels=num_labels, label2id=label2id, id2label=id2label +... ) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`ใƒˆใƒฌใƒผใƒŠใƒผ`] ใฏ็ฒพๅบฆใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_mind_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=3e-5, +... per_device_train_batch_size=32, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=32, +... num_train_epochs=10, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=feature_extractor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + + +้Ÿณๅฃฐๅˆ†้กž็”จใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb). + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใŸใ„้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใ‚’ใƒขใƒ‡ใƒซใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใจไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซใƒชใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ๅฟ˜ใ‚Œใชใ„ใงใใ ใ•ใ„ใ€‚ + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ้Ÿณๅฃฐๅˆ†้กž็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซ้Ÿณๅฃฐใƒ•ใ‚กใ‚คใƒซใ‚’ๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("audio-classification", model="stevhliu/my_awesome_minds_model") +>>> classifier(audio_file) +[ + {'score': 0.09766869246959686, 'label': 'cash_deposit'}, + {'score': 0.07998877018690109, 'label': 'app_error'}, + {'score': 0.0781070664525032, 'label': 'joint_account'}, + {'score': 0.07667109370231628, 'label': 'pay_bill'}, + {'score': 0.0755252093076706, 'label': 'balance'} +] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline` ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + + +็‰นๅพดๆŠฝๅ‡บๅ™จใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€`input`ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("stevhliu/my_awesome_minds_model") +>>> inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ใƒญใ‚ธใƒƒใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForAudioClassification + +>>> model = AutoModelForAudioClassification.from_pretrained("stevhliu/my_awesome_minds_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎ `id2label` ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใใ‚Œใ‚’ใƒฉใƒ™ใƒซใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> import torch + +>>> predicted_class_ids = torch.argmax(logits).item() +>>> predicted_label = model.config.id2label[predicted_class_ids] +>>> predicted_label +'cash_deposit' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/document_question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/document_question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..f07cc6dff28eae20f29a950e6389591a390b9d79 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/document_question_answering.md @@ -0,0 +1,495 @@ + + +# Document Question Answering + +[[open-in-colab]] + +ๆ–‡ๆ›ธใซใ‚ˆใ‚‹่ณชๅ•ๅฟœ็ญ”ใฏใ€ๆ–‡ๆ›ธใซใ‚ˆใ‚‹่ฆ–่ฆš็š„ใช่ณชๅ•ๅฟœ็ญ”ใจใ‚‚ๅ‘ผใฐใ‚Œใ€ไปฅไธ‹ใ‚’ๆไพ›ใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็”ปๅƒใซ้–ขใ™ใ‚‹่ณชๅ•ใธใฎๅ›ž็ญ”ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใธใฎๅ…ฅๅŠ›ใฏ้€šๅธธใ€็”ปๅƒใจ็”ปๅƒใฎ็ต„ใฟๅˆใ‚ใ›ใงใ™ใ€‚ +่ณชๅ•ใŒใ‚ใ‚Šใ€ๅ‡บๅŠ›ใฏ่‡ช็„ถ่จ€่ชžใง่กจ็พใ•ใ‚ŒใŸๅ›ž็ญ”ใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€ไปฅไธ‹ใ‚’ๅซใ‚€่ค‡ๆ•ฐใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‚’ๅˆฉ็”จใ—ใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆใ€ๅ˜่ชžใฎไฝ็ฝฎ (ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚น)ใ€ใŠใ‚ˆใณ็”ปๅƒ่‡ชไฝ“ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +- [DocVQA ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/datasets/nielsr/docvqa_1200_examples_donut) ใฎ [LayoutLMv2](../model_doc/layoutlmv2) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +- ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/image-to-text) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +LayoutLMv2 ใฏใ€ๆœ€ๅพŒใฎ้ž่กจ็คบใฎใƒ˜ใƒƒใƒ€ใƒผใฎไธŠใซ่ณชๅ•ๅฟœ็ญ”ใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎ่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒณใฎ็Šถๆ…‹ใ‚’่ชฟในใฆใ€ใƒˆใƒผใ‚ฏใƒณใฎ้–‹ๅง‹ใƒˆใƒผใ‚ฏใƒณใจ็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ +็ญ”ใˆใ€‚่จ€ใ„ๆ›ใˆใ‚Œใฐใ€ๅ•้กŒใฏๆŠฝๅ‡บ็š„่ณชๅ•ๅฟœ็ญ”ใจใ—ใฆๆ‰ฑใ‚ใ‚Œใพใ™ใ€‚ใคใพใ‚Šใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒๆ…ฎใ—ใฆใ€ใฉใฎ้ƒจๅˆ†ใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใ‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ +ใฎๆƒ…ๅ ฑใŒ่ณชๅ•ใซ็ญ”ใˆใพใ™ใ€‚ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใฏ OCR ใ‚จใƒณใ‚ธใƒณใฎๅ‡บๅŠ›ใ‹ใ‚‰ๅ–ๅพ—ใ•ใ‚Œใพใ™ใ€‚ใ“ใ“ใงใฏ Google ใฎ Tesseract ใงใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ LayoutLMv2 ใฏ detectron2ใ€torchvisionใ€tesseract ใซไพๅญ˜ใ—ใพใ™ใ€‚ + +```bash +pip install -q transformers datasets +``` + +```bash +pip install 'git+https://github.com/facebookresearch/detectron2.git' +pip install torchvision +``` + +```bash +sudo apt install tesseract-ocr +pip install -q pytesseract +``` + +ใ™ในใฆใฎไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใŸใ‚‰ใ€ใƒฉใƒณใ‚ฟใ‚คใƒ ใ‚’ๅ†่ตทๅ‹•ใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€๐Ÿค— ใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +ใ„ใใคใ‹ใฎใ‚ฐใƒญใƒผใƒใƒซๅค‰ๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> model_checkpoint = "microsoft/layoutlmv2-base-uncased" +>>> batch_size = 4 +``` + +## Load the data + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€๐Ÿค— Hub ใซใ‚ใ‚‹ๅ‰ๅ‡ฆ็†ใ•ใ‚ŒใŸ DocVQA ใฎๅฐใ•ใชใ‚ตใƒณใƒ—ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใƒ•ใƒซใซไฝฟใ„ใŸใ„ๅ ดๅˆใฏใ€ +DocVQA ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใ€[DocVQA ใƒ›ใƒผใƒ ใƒšใƒผใ‚ธ](https://rrc.cvc.uab.es/?ch=17) ใง็™ป้Œฒใ—ใฆใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใใ†ใ™ใ‚Œใฐใ€ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใ‚’้€ฒใ‚ใฆใ€[๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•](https://huggingface.co/docs/datasets/loading#local-and-remote-files) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("nielsr/docvqa_1200_examples") +>>> dataset +DatasetDict({ + train: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 1000 + }) + test: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 200 + }) +}) +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใ™ใงใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ•ใ‚Œใฆใ„ใพใ™ใ€‚็†่งฃใ™ใ‚‹ใŸใ‚ใซใƒฉใƒณใƒ€ใƒ ใชไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ† +ๆฉŸ่ƒฝใ‚’ๅ‚™ใˆใŸ่‡ชๅˆ†่‡ช่บซใ€‚ + +```py +>>> dataset["train"].features +``` + +ๅ€‹ใ€…ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒ่กจใ™ๅ†…ๅฎนใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +* `id`: ใ‚ตใƒณใƒ—ใƒซใฎID +* `image`: ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็”ปๅƒใ‚’ๅซใ‚€ PIL.Image.Image ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ +* `query`: ่ณชๅ•ๆ–‡ๅญ—ๅˆ— - ใ„ใใคใ‹ใฎ่จ€่ชžใงใฎ่‡ช็„ถ่จ€่ชžใซใ‚ˆใ‚‹่ณชๅ• +* `answers`: ใƒ’ใƒฅใƒผใƒžใƒณ ใ‚ขใƒŽใƒ†ใƒผใ‚ฟใƒผใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚ŒใŸๆญฃ่งฃใฎใƒชใ‚นใƒˆ +* `words` ใจ `bounding_boxes`: OCR ใฎ็ตๆžœใ€‚ใ“ใ“ใงใฏไฝฟ็”จใ—ใพใ›ใ‚“ใ€‚ +* `answer`: ๅˆฅใฎใƒขใƒ‡ใƒซใจไธ€่‡ดใ™ใ‚‹็ญ”ใˆใ€‚ใ“ใ“ใงใฏไฝฟ็”จใ—ใพใ›ใ‚“ใ€‚ + +่‹ฑ่ชžใฎ่ณชๅ•ใ ใ‘ใ‚’ๆฎ‹ใ—ใ€ๅˆฅใฎใƒขใƒ‡ใƒซใซใ‚ˆใ‚‹ไบˆๆธฌใŒๅซใพใ‚Œใฆใ„ใ‚‹ใจๆ€ใ‚ใ‚Œใ‚‹`answer`ๆฉŸ่ƒฝใ‚’ๅ‰Š้™คใ—ใพใ—ใ‚‡ใ†ใ€‚ +ใพใŸใ€ใ‚ขใƒŽใƒ†ใƒผใ‚ฟใƒผใซใ‚ˆใฃใฆๆไพ›ใ•ใ‚ŒใŸใ‚ปใƒƒใƒˆใ‹ใ‚‰ๆœ€ๅˆใฎๅ›ž็ญ”ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ‚ใ‚‹ใ„ใฏใ€ใƒฉใƒณใƒ€ใƒ ใซใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +>>> updated_dataset = dataset.map(lambda example: {"question": example["query"]["en"]}, remove_columns=["query"]) +>>> updated_dataset = updated_dataset.map( +... lambda example: {"answer": example["answers"][0]}, remove_columns=["answer", "answers"] +... ) +``` + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงไฝฟ็”จใ™ใ‚‹ LayoutLMv2 ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€`max_position_embeddings = 512` ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ ( +ใ“ใฎๆƒ…ๅ ฑใฏใ€[ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ `config.json` ใƒ•ใ‚กใ‚คใƒซ](https://huggingface.co/microsoft/layoutlmv2-base-uncased/blob/main/config.json#L18)) ใง่ฆ‹ใคใ‘ใฆใใ ใ•ใ„ใ€‚ +ไพ‹ใ‚’็œ็•ฅใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใŒใ€็ญ”ใˆใŒๅคงใใชๆ–‡ๆ›ธใฎๆœ€ๅพŒใซใ‚ใ‚Šใ€็ตๅฑ€็œ็•ฅใ•ใ‚Œใฆใ—ใพใ†ใจใ„ใ†็Šถๆณใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใซใ€ +ใ“ใ“ใงใฏใ€ๅŸ‹ใ‚่พผใฟใŒ 512 ใ‚’่ถ…ใˆใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ„ใใคใ‹ใฎไพ‹ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎใปใจใ‚“ใฉใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใŒ้•ทใ„ๅ ดๅˆใฏใ€ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐ ใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆๆˆฆ็•ฅใ‚’ๅฎŸ่ฃ…ใงใใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใ“ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ ใ€‚ + +```py +>>> updated_dataset = updated_dataset.filter(lambda x: len(x["words"]) + len(x["question"].split()) < 512) +``` + +ใ“ใฎๆ™‚็‚นใงใ€ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ OCR ๆฉŸ่ƒฝใ‚‚ๅ‰Š้™คใ—ใพใ—ใ‚‡ใ†ใ€‚ใ“ใ‚Œใ‚‰ใฏใ€็•ฐใชใ‚‹ใƒ‡ใƒผใ‚ฟใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎ OCR ใฎ็ตๆžœใงใ™ใ€‚ +ใƒขใƒ‡ใƒซใ€‚ใ“ใ‚Œใ‚‰ใฏๅ…ฅๅŠ›่ฆไปถใจไธ€่‡ดใ—ใชใ„ใŸใ‚ใ€ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ•ใ‚‰ใซๅ‡ฆ็†ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใฎใ€‚ไปฃใ‚ใ‚Šใซใ€OCR ใจ OCR ใฎไธกๆ–นใฎๅ…ƒใฎใƒ‡ใƒผใ‚ฟใซๅฏพใ—ใฆ [`LayoutLMv2Processor`] ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ€‚ใ“ใฎใ‚ˆใ†ใซใ—ใฆใ€ใƒขใƒ‡ใƒซใฎไบˆๆƒณใ•ใ‚Œใ‚‹ๅ…ฅๅŠ›ใจไธ€่‡ดใ™ใ‚‹ๅ…ฅๅŠ›ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚็”ปๅƒใ‚’ๆ‰‹ๅ‹•ใงๅŠ ๅทฅใ—ใŸใ„ๅ ดๅˆใฏใ€ +ใƒขใƒ‡ใƒซใŒใฉใฎใ‚ˆใ†ใชๅ…ฅๅŠ›ๅฝขๅผใ‚’ๆƒณๅฎšใ—ใฆใ„ใ‚‹ใ‹ใ‚’็Ÿฅใ‚‹ใซใฏใ€[`LayoutLMv2` ใƒขใƒ‡ใƒซใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](../model_doc/layoutlmv2) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> updated_dataset = updated_dataset.remove_columns("words") +>>> updated_dataset = updated_dataset.remove_columns("bounding_boxes") +``` + +ๆœ€ๅพŒใซใ€็”ปๅƒใ‚ตใƒณใƒ—ใƒซใ‚’็ขบ่ชใ—ใชใ„ใจใƒ‡ใƒผใ‚ฟๆŽข็ดขใฏๅฎŒไบ†ใ—ใพใ›ใ‚“ใ€‚ + + +```py +>>> updated_dataset["train"][11]["image"] +``` + +
+ DocVQA Image Example +
+ +## Preprocess the data + +ๆ–‡ๆ›ธใฎ่ณชๅ•ใซ็ญ”ใˆใ‚‹ใ‚ฟใ‚นใ‚ฏใฏใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚‹ใŸใ‚ใ€ๅ„ใƒขใƒ€ใƒชใƒ†ใ‚ฃใ‹ใ‚‰ใฎๅ…ฅๅŠ›ใŒ็ขบๅฎŸใซ่กŒใ‚ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎๆœŸๅพ…ใซๅพ“ใฃใฆๅ‰ๅ‡ฆ็†ใ•ใ‚Œใพใ™ใ€‚ใพใšใ€[`LayoutLMv2Processor`] ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ๅ‡ฆ็†ใงใใ‚‹็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใจใƒ†ใ‚ญใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใงใใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ๅ†…้ƒจใง็ต„ใฟๅˆใ‚ใ›ใฆใ„ใพใ™ใ€‚ + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(model_checkpoint) +``` + +### Preprocessing document images + +ใพใšใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‹ใ‚‰ใฎ `image_processor` ใ‚’ๅˆฉ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ็”ปๅƒใ‚’ๆบ–ๅ‚™ใ—ใพใ—ใ‚‡ใ†ใ€‚ +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏ็”ปๅƒใฎใ‚ตใ‚คใ‚บใ‚’ 224x224 ใซๅค‰ๆ›ดใ—ใ€ใ‚ซใƒฉใƒผ ใƒใƒฃใƒใƒซใฎ้ †ๅบใŒๆญฃใ—ใ„ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ +tesseract ใ‚’ไฝฟ็”จใ—ใฆ OCR ใ‚’้ฉ็”จใ—ใ€ๅ˜่ชžใจๆญฃ่ฆๅŒ–ใ•ใ‚ŒใŸๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฏใ™ในใฆใ€ใพใ•ใซๅฟ…่ฆใชใ‚‚ใฎใงใ™ใ€‚ +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ็”ปๅƒๅ‡ฆ็†ใ‚’็”ปๅƒใฎใƒใƒƒใƒใซ้ฉ็”จใ—ใ€OCR ใฎ็ตๆžœใ‚’่ฟ”ใ™้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> image_processor = processor.image_processor + + +>>> def get_ocr_words_and_boxes(examples): +... images = [image.convert("RGB") for image in examples["image"]] +... encoded_inputs = image_processor(images) + +... examples["image"] = encoded_inputs.pixel_values +... examples["words"] = encoded_inputs.words +... examples["boxes"] = encoded_inputs.boxes + +... return examples +``` + +ใ“ใฎๅ‰ๅ‡ฆ็†ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ซ˜้€Ÿใซ้ฉ็”จใ™ใ‚‹ใซใฏใ€[`~datasets.Dataset.map`] ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> dataset_with_ocr = updated_dataset.map(get_ocr_words_and_boxes, batched=True, batch_size=2) +``` + +### Preprocessing text data + +็”ปๅƒใซ OCR ใ‚’้ฉ็”จใ—ใŸใ‚‰ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒ†ใ‚ญใ‚นใƒˆ้ƒจๅˆ†ใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใฆใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใซใฏใ€ๅ‰ใฎใ‚นใƒ†ใƒƒใƒ—ใงๅ–ๅพ—ใ—ใŸๅ˜่ชžใจใƒœใƒƒใ‚ฏใ‚นใ‚’ใƒˆใƒผใ‚ฏใƒณใƒฌใƒ™ใƒซใฎ `input_ids`ใ€`attention_mask`ใ€ +`token_type_ids`ใจ`bbox`ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‹ใ‚‰ใฎ`Tokenizer`ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ + +```py +>>> tokenizer = processor.tokenizer +``` + +ๅ‰่ฟฐใฎๅ‰ๅ‡ฆ็†ใซๅŠ ใˆใฆใ€ใƒขใƒ‡ใƒซใฎใƒฉใƒ™ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ `xxxForQuestionAnswering` ใƒขใƒ‡ใƒซใฎๅ ดๅˆ +๐Ÿค— Transformers ใงใฏใ€ใƒฉใƒ™ใƒซใฏ `start_positions` ใจ `end_positions` ใงๆง‹ๆˆใ•ใ‚Œใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒใใฎไฝ็ฝฎใซใ‚ใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ +้–‹ๅง‹็‚นใจใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒๅ›ž็ญ”ใฎๆœ€ๅพŒใซใ‚ใ‚‹ใ‹ใ€‚ + +ใใ‚Œใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚ใ‚ˆใ‚Šๅคงใใชใƒชใ‚นใƒˆ (ๅ˜่ชžใƒชใ‚นใƒˆ) ๅ†…ใฎใ‚ตใƒ–ใƒชใ‚นใƒˆ (ๅ˜่ชžใซๅˆ†ๅ‰ฒใ•ใ‚ŒใŸๅ›ž็ญ”) ใ‚’ๆคœ็ดขใงใใ‚‹ใƒ˜ใƒซใƒ‘ใƒผ้–ขๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +ใ“ใฎ้–ขๆ•ฐใฏใ€`words_list` ใจ `answer_list` ใจใ„ใ† 2 ใคใฎใƒชใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใพใ™ใ€‚ๆฌกใซใ€`words_list`ใ‚’ๅๅพฉๅ‡ฆ็†ใ—ใฆใƒใ‚งใƒƒใ‚ฏใ—ใพใ™ใ€‚ +`words_list` (words_list[i]) ๅ†…ใฎ็พๅœจใฎๅ˜่ชžใŒใ€answer_list (answer_list[0]) ใฎๆœ€ๅˆใฎๅ˜่ชžใจ็ญ‰ใ—ใ„ใ‹ใฉใ†ใ‹ใ€ใŠใ‚ˆใณ +็พๅœจใฎๅ˜่ชžใ‹ใ‚‰ๅง‹ใพใ‚Šใ€`answer_list` ใจๅŒใ˜้•ทใ•ใฎ `words_list` ใฎใ‚ตใƒ–ใƒชใ‚นใƒˆใฏใ€`to answer_list` ใจ็ญ‰ใ—ใใชใ‚Šใพใ™ใ€‚ +ใ“ใฎๆกไปถใŒ true ใฎๅ ดๅˆใ€ไธ€่‡ดใŒ่ฆ‹ใคใ‹ใฃใŸใ“ใจใ‚’ๆ„ๅ‘ณใ—ใ€้–ขๆ•ฐใฏไธ€่‡ดใจใใฎ้–‹ๅง‹ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น (idx) ใ‚’่จ˜้Œฒใ—ใพใ™ใ€‚ +ใจใใฎ็ต‚ไบ†ใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚น (idx + len(answer_list) - 1)ใ€‚่ค‡ๆ•ฐใฎไธ€่‡ดใŒ่ฆ‹ใคใ‹ใฃใŸๅ ดๅˆใ€้–ขๆ•ฐใฏๆœ€ๅˆใฎใ‚‚ใฎใฎใฟใ‚’่ฟ”ใ—ใพใ™ใ€‚ +ไธ€่‡ดใ™ใ‚‹ใ‚‚ใฎใŒ่ฆ‹ใคใ‹ใ‚‰ใชใ„ๅ ดๅˆใ€้–ขๆ•ฐใฏ (`None`ใ€0ใ€ใŠใ‚ˆใณ 0) ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> def subfinder(words_list, answer_list): +... matches = [] +... start_indices = [] +... end_indices = [] +... for idx, i in enumerate(range(len(words_list))): +... if words_list[i] == answer_list[0] and words_list[i : i + len(answer_list)] == answer_list: +... matches.append(answer_list) +... start_indices.append(idx) +... end_indices.append(idx + len(answer_list) - 1) +... if matches: +... return matches[0], start_indices[0], end_indices[0] +... else: +... return None, 0, 0 +``` + +ใ“ใฎ้–ขๆ•ฐใŒ็ญ”ใˆใฎไฝ็ฝฎใ‚’่ฆ‹ใคใ‘ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใŸใ‚ใซใ€ไพ‹ใงไฝฟ็”จใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> example = dataset_with_ocr["train"][1] +>>> words = [word.lower() for word in example["words"]] +>>> match, word_idx_start, word_idx_end = subfinder(words, example["answer"].lower().split()) +>>> print("Question: ", example["question"]) +>>> print("Words:", words) +>>> print("Answer: ", example["answer"]) +>>> print("start_index", word_idx_start) +>>> print("end_index", word_idx_end) +Question: Who is in cc in this letter? +Words: ['wie', 'baw', 'brown', '&', 'williamson', 'tobacco', 'corporation', 'research', '&', 'development', 'internal', 'correspondence', 'to:', 'r.', 'h.', 'honeycutt', 'ce:', 't.f.', 'riehl', 'from:', '.', 'c.j.', 'cook', 'date:', 'may', '8,', '1995', 'subject:', 'review', 'of', 'existing', 'brainstorming', 'ideas/483', 'the', 'major', 'function', 'of', 'the', 'product', 'innovation', 'graup', 'is', 'to', 'develop', 'marketable', 'nove!', 'products', 'that', 'would', 'be', 'profitable', 'to', 'manufacture', 'and', 'sell.', 'novel', 'is', 'defined', 'as:', 'of', 'a', 'new', 'kind,', 'or', 'different', 'from', 'anything', 'seen', 'or', 'known', 'before.', 'innovation', 'is', 'defined', 'as:', 'something', 'new', 'or', 'different', 'introduced;', 'act', 'of', 'innovating;', 'introduction', 'of', 'new', 'things', 'or', 'methods.', 'the', 'products', 'may', 'incorporate', 'the', 'latest', 'technologies,', 'materials', 'and', 'know-how', 'available', 'to', 'give', 'then', 'a', 'unique', 'taste', 'or', 'look.', 'the', 'first', 'task', 'of', 'the', 'product', 'innovation', 'group', 'was', 'to', 'assemble,', 'review', 'and', 'categorize', 'a', 'list', 'of', 'existing', 'brainstorming', 'ideas.', 'ideas', 'were', 'grouped', 'into', 'two', 'major', 'categories', 'labeled', 'appearance', 'and', 'taste/aroma.', 'these', 'categories', 'are', 'used', 'for', 'novel', 'products', 'that', 'may', 'differ', 'from', 'a', 'visual', 'and/or', 'taste/aroma', 'point', 'of', 'view', 'compared', 'to', 'canventional', 'cigarettes.', 'other', 'categories', 'include', 'a', 'combination', 'of', 'the', 'above,', 'filters,', 'packaging', 'and', 'brand', 'extensions.', 'appearance', 'this', 'category', 'is', 'used', 'for', 'novel', 'cigarette', 'constructions', 'that', 'yield', 'visually', 'different', 'products', 'with', 'minimal', 'changes', 'in', 'smoke', 'chemistry', 'two', 'cigarettes', 'in', 'cne.', 'emulti-plug', 'te', 'build', 'yaur', 'awn', 'cigarette.', 'eswitchable', 'menthol', 'or', 'non', 'menthol', 'cigarette.', '*cigarettes', 'with', 'interspaced', 'perforations', 'to', 'enable', 'smoker', 'to', 'separate', 'unburned', 'section', 'for', 'future', 'smoking.', 'ยซshort', 'cigarette,', 'tobacco', 'section', '30', 'mm.', 'ยซextremely', 'fast', 'buming', 'cigarette.', 'ยซnovel', 'cigarette', 'constructions', 'that', 'permit', 'a', 'significant', 'reduction', 'iretobacco', 'weight', 'while', 'maintaining', 'smoking', 'mechanics', 'and', 'visual', 'characteristics.', 'higher', 'basis', 'weight', 'paper:', 'potential', 'reduction', 'in', 'tobacco', 'weight.', 'ยซmore', 'rigid', 'tobacco', 'column;', 'stiffing', 'agent', 'for', 'tobacco;', 'e.g.', 'starch', '*colored', 'tow', 'and', 'cigarette', 'papers;', 'seasonal', 'promotions,', 'e.g.', 'pastel', 'colored', 'cigarettes', 'for', 'easter', 'or', 'in', 'an', 'ebony', 'and', 'ivory', 'brand', 'containing', 'a', 'mixture', 'of', 'all', 'black', '(black', 'paper', 'and', 'tow)', 'and', 'ail', 'white', 'cigarettes.', '499150498'] +Answer: T.F. Riehl +start_index 17 +end_index 18 +``` + +ใŸใ ใ—ใ€ใ‚ตใƒณใƒ—ใƒซใŒใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚Œใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +```py +>>> encoding = tokenizer(example["question"], example["words"], example["boxes"]) +>>> tokenizer.decode(encoding["input_ids"]) +[CLS] who is in cc in this letter? [SEP] wie baw brown & williamson tobacco corporation research & development ... +``` + +ใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸๅ…ฅๅŠ›ๅ†…ใง็ญ”ใˆใฎไฝ็ฝฎใ‚’่ฆ‹ใคใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +* `token_type_ids` ใฏใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒ่ณชๅ•ใฎไธ€้ƒจใงใ‚ใ‚Šใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒๆ–‡ๆ›ธใฎๅ˜่ชžใฎไธ€้ƒจใงใ‚ใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ +* `tokenizer.cls_token_id` ใฏใ€ๅ…ฅๅŠ›ใฎๅ…ˆ้ ญใง็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ +* `word_ids` ใฏใ€ๅ…ƒใฎ `words` ใง่ฆ‹ใคใ‹ใฃใŸๅ›ž็ญ”ใ‚’ใ€ๅฎŒๅ…จใซใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸๅ…ฅๅŠ›ๅ†…ใฎๅŒใ˜ๅ›ž็ญ”ใจ็…งๅˆใ—ใฆๅˆคๆ–ญใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ +ใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸๅ…ฅๅŠ›ๅ†…ใฎๅฟœ็ญ”ใฎ้–‹ๅง‹/็ต‚ไบ†ไฝ็ฝฎใ€‚ + +ใ“ใ‚Œใ‚’ๅฟต้ ญใซ็ฝฎใ„ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚ + + +```py +>>> def encode_dataset(examples, max_length=512): +... questions = examples["question"] +... words = examples["words"] +... boxes = examples["boxes"] +... answers = examples["answer"] + +... # encode the batch of examples and initialize the start_positions and end_positions +... encoding = tokenizer(questions, words, boxes, max_length=max_length, padding="max_length", truncation=True) +... start_positions = [] +... end_positions = [] + +... # loop through the examples in the batch +... for i in range(len(questions)): +... cls_index = encoding["input_ids"][i].index(tokenizer.cls_token_id) + +... # find the position of the answer in example's words +... words_example = [word.lower() for word in words[i]] +... answer = answers[i] +... match, word_idx_start, word_idx_end = subfinder(words_example, answer.lower().split()) + +... if match: +... # if match is found, use `token_type_ids` to find where words start in the encoding +... token_type_ids = encoding["token_type_ids"][i] +... token_start_index = 0 +... while token_type_ids[token_start_index] != 1: +... token_start_index += 1 + +... token_end_index = len(encoding["input_ids"][i]) - 1 +... while token_type_ids[token_end_index] != 1: +... token_end_index -= 1 + +... word_ids = encoding.word_ids(i)[token_start_index : token_end_index + 1] +... start_position = cls_index +... end_position = cls_index + +... # loop over word_ids and increase `token_start_index` until it matches the answer position in words +... # once it matches, save the `token_start_index` as the `start_position` of the answer in the encoding +... for id in word_ids: +... if id == word_idx_start: +... start_position = token_start_index +... else: +... token_start_index += 1 + +... # similarly loop over `word_ids` starting from the end to find the `end_position` of the answer +... for id in word_ids[::-1]: +... if id == word_idx_end: +... end_position = token_end_index +... else: +... token_end_index -= 1 + +... start_positions.append(start_position) +... end_positions.append(end_position) + +... else: +... start_positions.append(cls_index) +... end_positions.append(cls_index) + +... encoding["image"] = examples["image"] +... encoding["start_positions"] = start_positions +... encoding["end_positions"] = end_positions + +... return encoding +``` + +ใ“ใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใŒๅฎŒๆˆใ—ใŸใฎใงใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> encoded_train_dataset = dataset_with_ocr["train"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["train"].column_names +... ) +>>> encoded_test_dataset = dataset_with_ocr["test"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["test"].column_names +... ) +``` + +ใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ็‰นๅพดใŒใฉใฎใ‚ˆใ†ใชใ‚‚ใฎใ‹ใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> encoded_train_dataset.features +{'image': Sequence(feature=Sequence(feature=Sequence(feature=Value(dtype='uint8', id=None), length=-1, id=None), length=-1, id=None), length=-1, id=None), + 'input_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), + 'token_type_ids': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'attention_mask': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'bbox': Sequence(feature=Sequence(feature=Value(dtype='int64', id=None), length=-1, id=None), length=-1, id=None), + 'start_positions': Value(dtype='int64', id=None), + 'end_positions': Value(dtype='int64', id=None)} +``` + +## Evaluation + +ๆ–‡ๆ›ธใฎ่ณชๅ•ๅ›ž็ญ”ใฎ่ฉ•ไพกใซใฏใ€ๅคง้‡ใฎๅพŒๅ‡ฆ็†ใŒๅฟ…่ฆใงใ™ใ€‚้Žๅ‰ฐๆ‘‚ๅ–ใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใซ +็พๆ™‚็‚นใงใฏใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏ่ฉ•ไพกใ‚นใƒ†ใƒƒใƒ—ใ‚’็œ็•ฅใ—ใฆใ„ใพใ™ใ€‚ [`Trainer`] ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ่ฉ•ไพกๆๅคฑใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใ€ +ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซใคใ„ใฆใพใฃใŸใใ‚ใ‹ใ‚‰ใชใ„ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๆŠฝๅ‡บ็š„่ณชๅ•ๅฟœ็ญ”ใฏ้€šๅธธใ€F1/ๅฎŒๅ…จไธ€่‡ดใ‚’ไฝฟ็”จใ—ใฆ่ฉ•ไพกใ•ใ‚Œใพใ™ใ€‚ +่‡ชๅˆ†ใงๅฎŸ่ฃ…ใ—ใŸใ„ๅ ดๅˆใฏใ€[่ณชๅ•ๅฟœ็ญ”ใฎ็ซ ](https://huggingface.co/course/chapter7/7?fw=pt#postprocessing) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +ใ‚คใƒณใ‚นใƒ”ใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅพ—ใ‚‹ใŸใ‚ใซใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใฎใ€‚ + +## Train + +ใŠใ‚ใงใจใ†๏ผใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆœ€ใ‚‚้›ฃใ—ใ„้ƒจๅˆ†ใ‚’็„กไบ‹ใซใƒŠใƒ“ใ‚ฒใƒผใƒˆใงใใŸใฎใงใ€็‹ฌ่‡ชใฎใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใฏๆฌกใฎๆ‰‹้ †ใŒๅซใพใ‚Œใพใ™ใ€‚ +* ๅ‰ๅ‡ฆ็†ใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€[`AutoModelForDocumentQuestionAnswering`] ใงใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ +* [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ +* ใ‚ตใƒณใƒ—ใƒซใ‚’ใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹้–ขๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ใ“ใ“ใงใฏ [`DefaultDataCollatโ€‹โ€‹or`] ใŒ้ฉๅˆ‡ใซๆฉŸ่ƒฝใ—ใพใ™ใ€‚ +* ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +* [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained(model_checkpoint) +``` + +[`TrainingArguments`] ใงใ€`output_dir` ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆง‹ๆˆใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€`push_to_hub`ใ‚’`True`ใซ่จญๅฎšใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ +ใ“ใฎๅ ดๅˆใ€`output_dir`ใฏใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใƒ—ใƒƒใ‚ทใƒฅใ•ใ‚Œใ‚‹ใƒชใƒใ‚ธใƒˆใƒชใฎๅๅ‰ใซใ‚‚ใชใ‚Šใพใ™ใ€‚ + +```py +>>> from transformers import TrainingArguments + +>>> # REPLACE THIS WITH YOUR REPO ID +>>> repo_id = "MariaK/layoutlmv2-base-uncased_finetuned_docvqa" + +>>> training_args = TrainingArguments( +... output_dir=repo_id, +... per_device_train_batch_size=4, +... num_train_epochs=20, +... save_steps=200, +... logging_steps=50, +... eval_strategy="steps", +... learning_rate=5e-5, +... save_total_limit=2, +... remove_unused_columns=False, +... push_to_hub=True, +... ) +``` + +ใ‚ตใƒณใƒ—ใƒซใ‚’ใพใจใ‚ใฆใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎๅ˜็ด”ใชใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + +ๆœ€ๅพŒใซใ€ใ™ในใฆใ‚’ใพใจใ‚ใฆใ€[`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=encoded_train_dataset, +... eval_dataset=encoded_test_dataset, +... processing_class=processor, +... ) + +>>> trainer.train() +``` + +ๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— Hub ใซ่ฟฝๅŠ ใ™ใ‚‹ใซใฏใ€ใƒขใƒ‡ใƒซ ใ‚ซใƒผใƒ‰ใ‚’ไฝœๆˆใ—ใ€`push_to_hub` ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> trainer.create_model_card() +>>> trainer.push_to_hub() +``` + +## Inference + +LayoutLMv2 ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€๐Ÿค— ใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใพใ™ใ€‚ใ‚‚ใฃใจใ‚‚ๅ˜็ด”ใช +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`Pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +ไพ‹ใ‚’ๆŒ™ใ’ใฆใฟใพใ—ใ‚‡ใ†: +```py +>>> example = dataset["test"][2] +>>> question = example["query"]["en"] +>>> image = example["image"] +>>> print(question) +>>> print(example["answers"]) +'Who is โ€˜presidingโ€™ TRRF GENERAL SESSION (PART 1)?' +['TRRF Vice President', 'lee a. waller'] +``` + +ๆฌกใซใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ณชๅ•ใธใฎๅ›ž็ญ”ใ‚’ๆ–‡ๆ›ธๅŒ–ใ—ใ€็”ปๅƒใจ่ณชๅ•ใฎ็ต„ใฟๅˆใ‚ใ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> qa_pipeline = pipeline("document-question-answering", model="MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> qa_pipeline(image, question) +[{'score': 0.9949808120727539, + 'answer': 'Lee A. Waller', + 'start': 55, + 'end': 57}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +1. ็”ปๅƒใจ่ณชๅ•ใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ—ใพใ™ใ€‚ +2. ใƒขใƒ‡ใƒซใ‚’้€šใ˜ใฆ็ตๆžœใพใŸใฏๅ‰ๅ‡ฆ็†ใ‚’่ปข้€ใ—ใพใ™ใ€‚ +3. ใƒขใƒ‡ใƒซใฏ`start_logits`ใจ`end_logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏใ€ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒๅฟœ็ญ”ใฎๅ…ˆ้ ญใซใ‚ใ‚‹ใฎใ‹ใ‚’็คบใ—ใ€ +ใฉใฎใƒˆใƒผใ‚ฏใƒณใŒๅ›ž็ญ”ใฎๆœ€ๅพŒใซใ‚ใ‚Šใพใ™ใ‹ใ€‚ใฉใกใ‚‰ใ‚‚ๅฝข็Šถ (batch_sizeใ€sequence_length) ใ‚’ๆŒใกใพใ™ใ€‚ +4. `start_logits` ใจ `end_logits` ใฎไธกๆ–นใฎๆœ€ๅพŒใฎๆฌกๅ…ƒใง argmax ใ‚’ๅ–ๅพ—ใ—ใ€ไบˆๆธฌใ•ใ‚Œใ‚‹ `start_idx` ใจ `end_idx` ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ +5. ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใฆๅ›ž็ญ”ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> import torch +>>> from transformers import AutoProcessor +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> processor = AutoProcessor.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") + +>>> with torch.no_grad(): +... encoding = processor(image.convert("RGB"), question, return_tensors="pt") +... outputs = model(**encoding) +... start_logits = outputs.start_logits +... end_logits = outputs.end_logits +... predicted_start_idx = start_logits.argmax(-1).item() +... predicted_end_idx = end_logits.argmax(-1).item() + +>>> processor.tokenizer.decode(encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + 1]) +'lee a. waller' +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/idefics.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/idefics.md new file mode 100644 index 0000000000000000000000000000000000000000..3ee9f25e74555d53aec2ca60db1fb23ad91db3dc --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/idefics.md @@ -0,0 +1,430 @@ + + + +# Image tasks with IDEFICS + +[[open-in-colab]] + +ๅ€‹ๅˆฅใฎใ‚ฟใ‚นใ‚ฏใฏ็‰นๆฎŠใชใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใงๅฏพๅ‡ฆใงใใพใ™ใŒใ€ๅˆฅใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ +ๆœ€่ฟ‘็™ปๅ ดใ—ใฆไบบๆฐ—ใ‚’ๅšใ—ใฆใ„ใ‚‹ใฎใฏใ€ๅพฎ่ชฟๆ•ดใ‚’่กŒใ‚ใšใซใ•ใพใ–ใพใชใ‚ฟใ‚นใ‚ฏใซๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +ใŸใจใˆใฐใ€ๅคง่ฆๆจกใช่จ€่ชžใƒขใƒ‡ใƒซใฏใ€่ฆ็ด„ใ€็ฟป่จณใ€ๅˆ†้กžใชใฉใฎ NLP ใ‚ฟใ‚นใ‚ฏใ‚’ๅ‡ฆ็†ใงใใพใ™ใ€‚ +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใชใฉใฎๅ˜ไธ€ใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃใซ้™ๅฎšใ•ใ‚Œใชใใชใ‚Šใพใ—ใŸใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎใ‚ˆใ†ใชๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ +IDEFICS ใจๅ‘ผใฐใ‚Œใ‚‹ๅคง่ฆๆจกใชใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ€็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ + +[IDEFICS](../model_doc/idefics) ใฏใ€[Flamingo](https://huggingface.co/papers/2204.14198) ใซๅŸบใฅใใ‚ชใƒผใƒ—ใƒณใ‚ขใ‚ฏใ‚ปใ‚นใฎใƒ“ใ‚ธใƒงใƒณใŠใ‚ˆใณ่จ€่ชžใƒขใƒ‡ใƒซใงใ™ใ€‚ +DeepMind ใซใ‚ˆใฃใฆๆœ€ๅˆใซ้–‹็™บใ•ใ‚ŒใŸๆœ€ๅ…ˆ็ซฏใฎ่ฆ–่ฆš่จ€่ชžใƒขใƒ‡ใƒซใ€‚ใƒขใƒ‡ใƒซใฏไปปๆ„ใฎ็”ปๅƒใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ™ +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใ—ใ€ๅ‡บๅŠ›ใจใ—ใฆไธ€่ฒซใ—ใŸใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚็”ปๅƒใซ้–ขใ™ใ‚‹่ณชๅ•ใซ็ญ”ใˆใŸใ‚Šใ€่ฆ–่ฆš็š„ใชใ‚ณใƒณใƒ†ใƒณใƒ„ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใŸใ‚Šใ€ +่ค‡ๆ•ฐใฎใ‚คใƒกใƒผใ‚ธใซๅŸบใฅใ„ใŸใ‚นใƒˆใƒผใƒชใƒผใ‚’ไฝœๆˆใ™ใ‚‹ใชใฉใ€‚ IDEFICS ใซใฏ 2 ใคใฎใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณใŒใ‚ใ‚Šใพใ™ - [800 ๅ„„ใƒ‘ใƒฉใƒกใƒผใ‚ฟ](https://huggingface.co/HuggingFaceM4/idefics-80b) +ใŠใ‚ˆใณ [90 ๅ„„ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟ](https://huggingface.co/HuggingFaceM4/idefics-9b)ใ€ใฉใกใ‚‰ใ‚‚ ๐Ÿค— Hub ใงๅ…ฅๆ‰‹ใงใใพใ™ใ€‚ๅ„ใƒใƒชใ‚จใƒผใ‚ทใƒงใƒณใซใคใ„ใฆใ€็ดฐใ‹ใ่ชฟๆ•ดใ•ใ‚ŒใŸๆŒ‡็คบใ‚‚่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ไผš่ฉฑใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉๅฟœใ—ใŸใƒขใƒ‡ใƒซใฎใƒใƒผใ‚ธใƒงใƒณใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใฏ้žๅธธใซๅคš็”จ้€”ใงใ€ๅน…ๅบƒใ„็”ปๅƒใ‚ฟใ‚นใ‚ฏใ‚„ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใพใ™ใ€‚ใ—ใ‹ใ—ใ€ +ๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใจใ„ใ†ใ“ใจใฏใ€ๅคง้‡ใฎ่จˆ็ฎ—ใƒชใ‚ฝใƒผใ‚นใจใ‚คใƒณใƒ•ใƒฉใ‚นใƒˆใƒฉใ‚ฏใƒใƒฃใŒๅฟ…่ฆใงใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใใ‚Œใฏใ‚ใชใŸๆฌก็ฌฌใงใ™ +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ๅ€‹ๅˆฅใฎใ‚ฟใ‚นใ‚ฏใ”ใจใซ็‰นๅŒ–ใ—ใŸใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚ใ€ใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ +- [IDEFICS ใ‚’ใƒญใƒผใƒ‰](#loading-the-model) ใŠใ‚ˆใณ [ใƒขใƒ‡ใƒซใฎ้‡ๅญๅŒ–ใƒใƒผใ‚ธใƒงใƒณใ‚’ใƒญใƒผใƒ‰](#quantized-model) +- IDEFICS ใ‚’ๆฌกใฎ็›ฎ็š„ใงไฝฟ็”จใ—ใพใ™ใ€‚ + - [็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ](#image-captioning) + - [ใƒ—ใƒญใƒณใƒ—ใƒˆ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ](#prompted-image-captioning) + - [Few-shot ใƒ—ใƒญใƒณใƒ—ใƒˆ](#few-shot-prompting) + - [ใƒ“ใ‚ธใƒฅใ‚ขใƒซ่ณชๅ•ๅ›ž็ญ”](#visual-question-answering) + - [็”ปๅƒๅˆ†้กž](#image-classification) + - [็”ปๅƒใ‚ฌใ‚คใƒ‰ไป˜ใใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ](#image-guided-text-generation) +- [ใƒใƒƒใƒใƒขใƒผใƒ‰ใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹](#running-inference-in-batch-mode) +- [ไผš่ฉฑ็”จใซ IDEFICS ๅ‘ฝไปคใ‚’ๅฎŸ่กŒ](#idefics-instruct-for-conversational-use) + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q bitsandbytes sentencepiece accelerate transformers +``` + + +้‡ๅญๅŒ–ใ•ใ‚Œใฆใ„ใชใ„ใƒใƒผใ‚ธใƒงใƒณใฎใƒขใƒ‡ใƒซ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆๆฌกใฎไพ‹ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๅฐ‘ใชใใจใ‚‚ 20GB ใฎ GPU ใƒกใƒขใƒชใŒๅฟ…่ฆใงใ™ใ€‚ + + +## Loading the model + +ใพใšใฏใƒขใƒ‡ใƒซใฎ 90 ๅ„„ๅ€‹ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> checkpoint = "HuggingFaceM4/idefics-9b" +``` + +ไป–ใฎ Transformers ใƒขใƒ‡ใƒซใจๅŒๆง˜ใซใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใจใƒขใƒ‡ใƒซ่‡ชไฝ“ใ‚’ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +IDEFICS ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€[`LlamaTokenizer`] ใจ IDEFICS ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ๅ˜ไธ€ใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใซใƒฉใƒƒใƒ—ใ—ใฆๅ‡ฆ็†ใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใฎใƒ†ใ‚ญใ‚นใƒˆใจ็”ปๅƒใฎๅ…ฅๅŠ›ใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ + + +```py +>>> import torch + +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +``` + +`device_map`ใ‚’`auto`ใซ่จญๅฎšใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’ๆœ€ใ‚‚ๆœ€้ฉๅŒ–ใ•ใ‚ŒใŸ็Šถๆ…‹ใงใƒญใƒผใƒ‰ใŠใ‚ˆใณไฟๅญ˜ใ™ใ‚‹ๆ–นๆณ•ใŒ่‡ชๅ‹•็š„ใซๆฑบๅฎšใ•ใ‚Œใพใ™ใ€‚ +ๆ—ขๅญ˜ใฎใƒ‡ใƒใ‚คใ‚นใ‚’่€ƒๆ…ฎใ—ใŸๆ–นๆณ•ใ€‚ + +### Quantized model + +ใƒใ‚คใƒกใƒขใƒช GPU ใฎๅฏ็”จๆ€งใŒๅ•้กŒใจใชใ‚‹ๅ ดๅˆใฏใ€ใƒขใƒ‡ใƒซใฎ้‡ๅญๅŒ–ใ•ใ‚ŒใŸใƒใƒผใ‚ธใƒงใƒณใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใƒขใƒ‡ใƒซใจ +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ 4 ใƒ“ใƒƒใƒˆ็ฒพๅบฆใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`BitsAndBytesConfig`ใ‚’`from_pretrained`ใƒกใ‚ฝใƒƒใƒ‰ใซๆธกใ™ใจใ€ใƒขใƒ‡ใƒซใŒๅœง็ธฎใ•ใ‚Œใพใ™ใ€‚ +ใƒญใƒผใƒ‰ไธญใซใใฎๅ ดใงใ€‚ + + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor, BitsAndBytesConfig + +>>> quantization_config = BitsAndBytesConfig( +... load_in_4bit=True, +... bnb_4bit_compute_dtype=torch.float16, +... ) + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained( +... checkpoint, +... quantization_config=quantization_config, +... device_map="auto" +... ) +``` + +ๆๆกˆใ•ใ‚ŒใŸๆ–นๆณ•ใฎใ„ใšใ‚Œใ‹ใงใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใŸใฎใงใ€IDEFICS ใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ฟใ‚นใ‚ฏใฎๆŽข็ดขใซ้€ฒใฟใพใ—ใ‚‡ใ†ใ€‚ + +## Image captioning + +็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณไป˜ใ‘ใฏใ€็‰นๅฎšใฎ็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ไธ€่ˆฌ็š„ใช็”จ้€”ใฏ่ฆ–่ฆš้šœๅฎณ่€…ใ‚’ๆ”ฏๆดใ™ใ‚‹ใ“ใจใงใ™ +ไบบใ€…ใฏใ•ใพใ–ใพใช็Šถๆณใ‚’ใƒŠใƒ“ใ‚ฒใƒผใƒˆใ—ใพใ™ใ€‚ใŸใจใˆใฐใ€ใ‚ชใƒณใƒฉใ‚คใƒณใง็”ปๅƒใ‚ณใƒณใƒ†ใƒณใƒ„ใ‚’ๆŽข็ดขใ—ใพใ™ใ€‚ + +ใ‚ฟใ‚นใ‚ฏใ‚’่ชฌๆ˜Žใ™ใ‚‹ใซใฏใ€ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ไป˜ใ‘ใ‚‹็”ปๅƒใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ไพ‹: + +
+ Image of a puppy in a flower bed +
+ +ๅ†™็œŸๆไพ›๏ผš[Hendo Wang](https://unsplash.com/@hendoo) + +IDEFICS ใฏใƒ†ใ‚ญใ‚นใƒˆใจ็”ปๅƒใฎใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€็”ปๅƒใซใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ไป˜ใ‘ใ‚‹ใซใฏใ€ใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ใƒฆใƒผใ‚ถใƒผใซๆไพ›ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใƒขใƒ‡ใƒซใ€ๅ‰ๅ‡ฆ็†ใ•ใ‚ŒใŸๅ…ฅๅŠ›็”ปๅƒใฎใฟใ€‚ใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใŒใชใ„ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏใƒ†ใ‚ญใ‚นใƒˆใฎ็”Ÿๆˆใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚ +BOS (Beginning-of-sequence) ใƒˆใƒผใ‚ฏใƒณใซใ‚ˆใ‚Šใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใธใฎ็”ปๅƒๅ…ฅๅŠ›ใจใ—ใฆใ€็”ปๅƒใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ (`PIL.Image`) ใพใŸใฏ็”ปๅƒใ‚’ๅ–ๅพ—ใงใใ‚‹ URL ใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1583160247711-2191776b4b91?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3542&q=80", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +A puppy in a flower bed +``` + + + +ๅข—ๅŠ ๆ™‚ใซ็™บ็”Ÿใ™ใ‚‹ใ‚จใƒฉใƒผใ‚’้ฟใ‘ใ‚‹ใŸใ‚ใซใ€`generate`ใฎๅ‘ผใณๅ‡บใ—ใซ`bad_words_ids`ใ‚’ๅซใ‚ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ +`max_new_tokens`: ใƒขใƒ‡ใƒซใฏใ€ๆ–ฐใ—ใ„ `` ใพใŸใฏ `` ใƒˆใƒผใ‚ฏใƒณใ‚’็”Ÿๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆ็”ปๅƒใŒ็”Ÿๆˆใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎใ‚ˆใ†ใซใ‚ชใƒณใ‚ถใƒ•ใƒฉใ‚คใง่จญๅฎšใ™ใ‚‹ใ“ใจใ‚‚ใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) ใ‚ฌใ‚คใƒ‰ใง่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซ `GenerationConfig` ใซไฟๅญ˜ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + +## Prompted image captioning + +ใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใง็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ๆ‹กๅผตใงใใ€ใƒขใƒ‡ใƒซใฏ็”ปๅƒใ‚’ๆŒ‡ๅฎšใ—ใฆ็ถš่กŒใ—ใพใ™ใ€‚ๆŒใฃใฆใ„ใใพใ—ใ‚‡ใ† +ๅˆฅใฎๅ›ณใง่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +
+ Image of the Eiffel Tower at night +
+ +ๅ†™็œŸๆไพ›๏ผš[Denys Nevozhai](https://unsplash.com/@dnevozhai)ใ€‚ + +ใƒ†ใ‚ญใ‚นใƒˆใŠใ‚ˆใณ็”ปๅƒใฎใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๅ˜ไธ€ใฎใƒชใ‚นใƒˆใจใ—ใฆใƒขใƒ‡ใƒซใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใซๆธกใ—ใ€้ฉๅˆ‡ใชๅ…ฅๅŠ›ใ‚’ไฝœๆˆใงใใพใ™ใ€‚ + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +This is an image of the Eiffel Tower in Paris, France. +``` + +## Few-shot prompting + +IDEFICS ใฏใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใงๅ„ชใ‚ŒใŸ็ตๆžœใ‚’็คบใ—ใพใ™ใŒใ€ใ‚ฟใ‚นใ‚ฏใซใ‚ˆใฃใฆใฏ็‰นๅฎšใฎๅฝขๅผใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒๅฟ…่ฆใซใชใ‚‹ๅ ดๅˆใ‚„ใ€ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒไป˜ๅฑžใ™ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ฟใ‚นใ‚ฏใฎ่ค‡้›‘ใ•ใ‚’ๅข—ๅคงใ•ใ›ใ‚‹ใใฎไป–ใฎๅˆถ้™ใพใŸใฏ่ฆไปถใ€‚ๅฐ‘ๆ•ฐใฎใ‚ทใƒงใƒƒใƒˆใฎใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆๅ†…ใฎๅญฆ็ฟ’ใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใซไพ‹ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใงใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸไพ‹ใฎๅฝขๅผใ‚’ๆจกๅ€ฃใ—ใŸ็ตๆžœใ‚’็”Ÿๆˆใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใ‚’ๆ“ไฝœใงใใพใ™ใ€‚ + +ๅ‰ใฎใ‚จใƒƒใƒ•ใ‚งใƒซๅก”ใฎ็”ปๅƒใ‚’ใƒขใƒ‡ใƒซใฎไพ‹ใจใ—ใฆไฝฟ็”จใ—ใ€ใƒขใƒ‡ใƒซใซใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณใ™ใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใŒไฝ•ใงใ‚ใ‚‹ใ‹ใ‚’็Ÿฅใ‚‹ใ“ใจใซๅŠ ใˆใฆใ€ใใ‚Œใซ้–ขใ™ใ‚‹่ˆˆๅ‘ณๆทฑใ„ๆƒ…ๅ ฑใ‚‚ๅ–ๅพ—ใ—ใŸใ„ใจ่€ƒใˆใฆใ„ใพใ™ใ€‚ +ๆฌกใซใ€่‡ช็”ฑใฎๅฅณ็ฅžใฎ็”ปๅƒใซๅฏพใ—ใฆๅŒใ˜ๅฟœ็ญ”ๅฝขๅผใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‹ใฉใ†ใ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +
+ Image of the Statue of Liberty +
+ +ๅ†™็œŸๆไพ›๏ผš[Juan Mayobre](https://unsplash.com/@jmayobres)ใ€‚ + +```py +>>> prompt = ["User:", +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "Describe this image.\nAssistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building.\n", +... "User:", +... "https://images.unsplash.com/photo-1524099163253-32b7f0256868?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3387&q=80", +... "Describe this image.\nAssistant:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=30, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +User: Describe this image. +Assistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building. +User: Describe this image. +Assistant: An image of the Statue of Liberty. Fun fact: the Statue of Liberty is 151 feet tall. +``` + +ใƒขใƒ‡ใƒซใฏ 1 ใคใฎไพ‹ (ใคใพใ‚Šใ€1 ใ‚ทใƒงใƒƒใƒˆ) ใ ใ‘ใ‹ใ‚‰ใ‚ฟใ‚นใ‚ฏใฎๅฎŸ่กŒๆ–นๆณ•ใ‚’ๅญฆ็ฟ’ใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจ็›ฎใ—ใฆใใ ใ•ใ„ใ€‚ใ‚ˆใ‚Š่ค‡้›‘ใชใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใฏใ€ +ใ‚ˆใ‚Šๅคšใใฎไพ‹ (3 ใ‚ทใƒงใƒƒใƒˆใ€5 ใ‚ทใƒงใƒƒใƒˆใชใฉ) ใ‚’่‡ช็”ฑใซ่ฉฆใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +## Visual question answering + +Visual Question Answering (VQA) ใฏใ€็”ปๅƒใซๅŸบใฅใ„ใฆ่‡ช็”ฑๅฝขๅผใฎ่ณชๅ•ใซ็ญ”ใˆใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚็”ปๅƒใซไผผใฆใ„ใ‚‹ +ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใฏใ€ใ‚ขใ‚ฏใ‚ปใ‚ทใƒ“ใƒชใƒ†ใ‚ฃ ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ ใ‘ใงใชใใ€ๆ•™่‚ฒ (่ฆ–่ฆš่ณ‡ๆ–™ใซใคใ„ใฆใฎๆŽจ่ซ–) ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ +ใ‚ตใƒผใƒ“ใ‚น๏ผˆ็”ปๅƒใ‚’ๅŸบใซใ—ใŸๅ•†ๅ“ใซ้–ขใ™ใ‚‹่ณชๅ•๏ผ‰ใ€็”ปๅƒๆคœ็ดขใชใฉใ€‚ + +ใ“ใฎใ‚ฟใ‚นใ‚ฏ็”จใซๆ–ฐใ—ใ„็”ปๅƒใ‚’ๅ–ๅพ—ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +
+ Image of a couple having a picnic +
+ +ๅ†™็œŸๆไพ› [Jarritos Mexican Soda](https://unsplash.com/@jarritos). + +้ฉๅˆ‡ใชๆŒ‡็คบใ‚’ใƒ—ใƒญใƒณใƒ—ใƒˆใ™ใ‚‹ใ“ใจใงใ€ใƒขใƒ‡ใƒซใ‚’็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‹ใ‚‰่ฆ–่ฆš็š„ใช่ณชๅ•ใธใฎๅฟœ็ญ”ใซๅฐŽใใ“ใจใŒใงใใพใ™ใ€‚ + +```py +>>> prompt = [ +... "Instruction: Provide an answer to the question. Use the image to answer.\n", +... "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Question: Where are these people and what's the weather like? Answer:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=20, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Provide an answer to the question. Use the image to answer. + Question: Where are these people and what's the weather like? Answer: They're in a park in New York City, and it's a beautiful day. +``` + +## Image classification + +IDEFICS ใฏใ€ๆฌกใฎใƒ‡ใƒผใ‚ฟใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟใซใคใ„ใฆๆ˜Ž็คบ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใชใใฆใ‚‚ใ€็”ปๅƒใ‚’ใ•ใพใ–ใพใชใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใงใใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎ็‰นๅฎšใฎใ‚ซใƒ†ใ‚ดใƒชใ‹ใ‚‰ใฎใƒฉใƒ™ใƒซไป˜ใใฎไพ‹ใ€‚ใ‚ซใƒ†ใ‚ดใƒชใฎใƒชใ‚นใƒˆใ‚’ๆŒ‡ๅฎšใ—ใ€ใใฎ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใ‚’ไฝฟ็”จใ—ใฆ็†่งฃใ™ใ‚‹ +ๆฉŸ่ƒฝใ‚’ๅˆฉ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ็”ปๅƒใŒใฉใฎใ‚ซใƒ†ใ‚ดใƒชใซๅฑžใ™ใ‚‹โ€‹โ€‹ๅฏ่ƒฝๆ€งใŒ้ซ˜ใ„ใ‹ใ‚’ๆŽจๆธฌใงใใพใ™ใ€‚ + +ใŸใจใˆใฐใ€ๆฌกใฎใ‚ˆใ†ใช้‡Ž่œใ‚นใ‚ฟใƒณใƒ‰ใฎ็”ปๅƒใŒใ‚ใ‚‹ใจใ—ใพใ™ใ€‚ + +
+ Image of a vegetable stand +
+ +ๅ†™็œŸๆไพ›๏ผš[Peter Wendt](https://unsplash.com/@peterwendt)ใ€‚ + +็”ปๅƒใ‚’ๆฌกใฎใ„ใšใ‚Œใ‹ใฎใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใงใใพใ™ใ€‚ + +```py +>>> categories = ['animals','vegetables', 'city landscape', 'cars', 'office'] +>>> prompt = [f"Instruction: Classify the following image into a single category from the following list: {categories}.\n", +... "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Category: " +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=6, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Classify the following image into a single category from the following list: ['animals', 'vegetables', 'city landscape', 'cars', 'office']. +Category: Vegetables +``` + +ไธŠใฎไพ‹ใงใฏใ€็”ปๅƒใ‚’ 1 ใคใฎใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใ—ใฆใ„ใพใ™ใŒใ€ใƒฉใƒณใ‚ฏๅˆ†้กžใ‚’่กŒใ†ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +## Image-guided text generation + +ใ‚ˆใ‚Šใ‚ฏใƒชใ‚จใ‚คใƒ†ใ‚ฃใƒ–ใชใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฎๅ ดๅˆใฏใ€็”ปๅƒใ‚ฌใ‚คใƒ‰ไป˜ใใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚’ไฝฟ็”จใ—ใฆใ€็”ปๅƒใซๅŸบใฅใ„ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใงใใพใ™ใ€‚ใ“ใ‚Œใฏๅฏ่ƒฝใงใ™ +่ฃฝๅ“ใ€ๅบƒๅ‘Šใ€ใ‚ทใƒผใƒณใฎ่ชฌๆ˜Žใชใฉใ‚’ไฝœๆˆใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +IDEFICS ใซใ€่ตคใ„ใƒ‰ใ‚ขใฎๅ˜็ด”ใช็”ปๅƒใซๅŸบใฅใ„ใฆใ‚นใƒˆใƒผใƒชใƒผใ‚’ๆ›ธใใ‚ˆใ†ใซไฟƒใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +
+ Image of a red door with a pumpkin on the steps +
+ +ๅ†™็œŸๆไพ›๏ผš[Craig Tidball](https://unsplash.com/@devonshiremedia)ใ€‚ + +```py +>>> prompt = ["Instruction: Use the image to write a story. \n", +... "https://images.unsplash.com/photo-1517086822157-2b0358e7684a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2203&q=80", +... "Story: \n"] + +>>> inputs = processor(prompt, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, num_beams=2, max_new_tokens=200, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Use the image to write a story. + Story: +Once upon a time, there was a little girl who lived in a house with a red door. She loved her red door. It was the prettiest door in the whole world. + +One day, the little girl was playing in her yard when she noticed a man standing on her doorstep. He was wearing a long black coat and a top hat. + +The little girl ran inside and told her mother about the man. + +Her mother said, โ€œDonโ€™t worry, honey. Heโ€™s just a friendly ghost.โ€ + +The little girl wasnโ€™t sure if she believed her mother, but she went outside anyway. + +When she got to the door, the man was gone. + +The next day, the little girl was playing in her yard again when she noticed the man standing on her doorstep. + +He was wearing a long black coat and a top hat. + +The little girl ran +``` + +IDEFICS ใฏ็Ž„้–ขๅ…ˆใซใ‚ใ‚‹ใ‚ซใƒœใƒใƒฃใซๆฐ—ใฅใใ€ๅนฝ้œŠใซ้–ขใ™ใ‚‹ไธๆฐ—ๅ‘ณใชใƒใƒญใ‚ฆใ‚ฃใƒผใƒณใฎ่ฉฑใ‚’ใ—ใŸใ‚ˆใ†ใงใ™ใ€‚ + + + +ใ“ใฎใ‚ˆใ†ใช้•ทใ„ๅ‡บๅŠ›ใฎๅ ดๅˆใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจๅคงใใชใƒกใƒชใƒƒใƒˆใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏๅฝนใซ็ซ‹ใกใพใ™ +็”Ÿๆˆใ•ใ‚Œใ‚‹ๅ‡บๅŠ›ใฎๅ“่ณชใŒๅคงๅน…ใซๅ‘ไธŠใ—ใพใ™ใ€‚ [ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +่ฉณใ—ใ็Ÿฅใ‚‹ใ“ใจใŒใงใใ€‚ + + + +## Running inference in batch mode + +ใ“ใ‚Œใพใงใฎใ™ในใฆใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€IDEFICS ใ‚’ 1 ใคใฎไพ‹ใจใ—ใฆ่ชฌๆ˜Žใ—ใพใ—ใŸใ€‚้žๅธธใซไผผใŸๆ–นๆณ•ใงใ€ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใฎใƒชใ‚นใƒˆใ‚’ๆธกใ™ใ“ใจใซใ‚ˆใ‚Šใ€ใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> prompts = [ +... [ "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... ] + +>>> inputs = processor(prompts, return_tensors="pt").to("cuda") +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i,t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +0: +This is an image of the Eiffel Tower in Paris, France. + +1: +This is an image of a couple on a picnic blanket. + +2: +This is an image of a vegetable stand. +``` + +## IDEFICS instruct for conversational use + +ไผš่ฉฑๅž‹ใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใฎๅ ดๅˆใฏใ€๐Ÿค— ใƒใƒ–ใงใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸๆŒ‡็คบใ•ใ‚ŒใŸใƒใƒผใ‚ธใƒงใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +`HuggingFaceM4/idefics-80b-instruct` ใŠใ‚ˆใณ `HuggingFaceM4/idefics-9b-instruct`ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๆ•™ๅธซใ‚ใ‚Šใƒขใƒ‡ใƒซใจๅ‘ฝไปคใƒขใƒ‡ใƒซใ‚’็ต„ใฟๅˆใ‚ใ›ใŸใใ‚Œใžใ‚ŒใฎๅŸบๆœฌใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸ็ตๆžœใงใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใงใ€ใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใชใŒใ‚‰ใ€ไผš่ฉฑ่จญๅฎšใงใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Šไฝฟใ„ใ‚„ใ™ใใ—ใพใ™ใ€‚ + +ไผš่ฉฑใงใฎไฝฟ็”จใจใƒ—ใƒญใƒณใƒ—ใƒˆใฏใ€ๅŸบๆœฌใƒขใƒ‡ใƒซใฎไฝฟ็”จใจ้žๅธธใซไผผใฆใ„ใพใ™ใ€‚ + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> device = "cuda" if torch.cuda.is_available() else "cpu" + +>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct" +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device) +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> prompts = [ +... [ +... "User: What is in this image?", +... "https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG", +... "", + +... "\nAssistant: This picture depicts Idefix, the dog of Obelix in Asterix and Obelix. Idefix is running on the ground.", + +... "\nUser:", +... "https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052", +... "And who is that?", + +... "\nAssistant:", +... ], +... ] + +>>> # --batched mode +>>> inputs = processor(prompts, add_end_of_utterance_token=False, return_tensors="pt").to(device) +>>> # --single sample mode +>>> # inputs = processor(prompts[0], return_tensors="pt").to(device) + +>>> # Generation args +>>> exit_condition = processor.tokenizer("", add_special_tokens=False).input_ids +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, eos_token_id=exit_condition, bad_words_ids=bad_words_ids, max_length=100) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i, t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_captioning.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_captioning.md new file mode 100644 index 0000000000000000000000000000000000000000..7649947b2c6450110c60ad6f6d7f4a5ad2a9d49d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_captioning.md @@ -0,0 +1,276 @@ + + +# Image captioning + +[[open-in-colab]] + +็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณไป˜ใ‘ใฏใ€็‰นๅฎšใฎ็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ไธ€่ˆฌ็š„ใช็พๅฎŸไธ–็•Œใฎใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ +่ฆ–่ฆš้šœๅฎณ่€…ใŒใ•ใพใ–ใพใช็Šถๆณใ‚’ไน—ใ‚Š่ถŠใˆใ‚‰ใ‚Œใ‚‹ใ‚ˆใ†ๆ”ฏๆดใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€็”ปๅƒใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ +็”ปๅƒใ‚’่ชฌๆ˜Žใ™ใ‚‹ใ“ใจใงไบบใ€…ใฎใ‚ณใƒณใƒ†ใƒณใƒ„ใธใฎใ‚ขใ‚ฏใ‚ปใ‚ทใƒ“ใƒชใƒ†ใ‚ฃใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +* ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +* ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate -q +pip install jiwer -q +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +## Load the Pokรฉmon BLIP captions dataset + +๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€{image-caption} ใƒšใ‚ขใงๆง‹ๆˆใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚็‹ฌ่‡ชใฎ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ใซใฏ +PyTorch ใงใฏใ€[ใ“ใฎใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/GIT/Fine_tune_GIT_on_an_image_captioning_dataset.ipynb) ใ‚’ๅ‚็…งใงใใพใ™ใ€‚ + +```py +ds = load_dataset("lambdalabs/pokemon-blip-captions") +ds +``` + +```bash +DatasetDict({ + train: Dataset({ + features: ['image', 'text'], + num_rows: 833 + }) +}) +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ `image`ใจ`text`ใฎ 2 ใคใฎๆฉŸ่ƒฝใŒใ‚ใ‚Šใพใ™ใ€‚ + + + +ๅคšใใฎ็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใ€็”ปๅƒใ”ใจใซ่ค‡ๆ•ฐใฎใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎใ‚ˆใ†ใชๅ ดๅˆใ€ไธ€่ˆฌ็š„ใชๆˆฆ็•ฅใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซๅˆฉ็”จๅฏ่ƒฝใชใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใฎไธญใ‹ใ‚‰ใƒฉใƒณใƒ€ใƒ ใซใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + + + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใ‚คใƒณ ใ‚นใƒ—ใƒชใƒƒใƒˆใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```python +ds = ds["train"].train_test_split(test_size=0.1) +train_ds = ds["train"] +test_ds = ds["test"] +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใ‹ใ‚‰ใฎใ„ใใคใ‹ใฎใ‚ตใƒณใƒ—ใƒซใ‚’่ฆ–่ฆšๅŒ–ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +from textwrap import wrap +import matplotlib.pyplot as plt +import numpy as np + + +def plot_images(images, captions): + plt.figure(figsize=(20, 20)) + for i in range(len(images)): + ax = plt.subplot(1, len(images), i + 1) + caption = captions[i] + caption = "\n".join(wrap(caption, 12)) + plt.title(caption) + plt.imshow(images[i]) + plt.axis("off") + + +sample_images_to_visualize = [np.array(train_ds[i]["image"]) for i in range(5)] +sample_captions = [train_ds[i]["text"] for i in range(5)] +plot_images(sample_images_to_visualize, sample_captions) +``` + +
+ Sample training images +
+ +## Preprocess the dataset + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ 2 ใคใฎใƒขใƒ€ใƒชใƒ†ใ‚ฃ (็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆ) ใŒใ‚ใ‚‹ใŸใ‚ใ€ๅ‰ๅ‡ฆ็†ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ็”ปๅƒใจใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ๅ‰ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚’่กŒใ†ใซใฏใ€ๅพฎ่ชฟๆ•ดใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸใƒ—ใƒญใ‚ปใƒƒใ‚ต ใ‚ฏใƒฉใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```python +from transformers import AutoProcessor + +checkpoint = "microsoft/git-base" +processor = AutoProcessor.from_pretrained(checkpoint) +``` + + +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏๅ†…้ƒจใง็”ปๅƒใ‚’ๅ‰ๅ‡ฆ็†ใ— (ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚„ใƒ”ใ‚ฏใ‚ปใƒซ ใ‚นใ‚ฑใƒผใƒชใƒณใ‚ฐใ‚’ๅซใ‚€)ใ€ใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +```python +def transforms(example_batch): + images = [x for x in example_batch["image"]] + captions = [x for x in example_batch["text"]] + inputs = processor(images=images, text=captions, padding="max_length") + inputs.update({"labels": inputs["input_ids"]}) + return inputs + + +train_ds.set_transform(transforms) +test_ds.set_transform(transforms) +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๆบ–ๅ‚™ใŒใงใใŸใ‚‰ใ€ๅพฎ่ชฟๆ•ด็”จใซใƒขใƒ‡ใƒซใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใงใใพใ™ใ€‚ + +## Load a base model + +["microsoft/git-base"](https://huggingface.co/microsoft/git-base) ใ‚’ [`AutoModelForCausalLM`](https://huggingface.co/docs/transformers/model_doc/auto#transformers.AutoModelForCausalLM) ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ€‚ + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained(checkpoint) +``` + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained(checkpoint) +``` +## Evaluate + +็”ปๅƒใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณ ใƒขใƒ‡ใƒซใฏ้€šๅธธใ€[Rouge Score](https://huggingface.co/spaces/evaluate-metric/rouge) ใพใŸใฏ [Word Error Rate](https://huggingface.co/spaces/evaluate-metric/) ใง่ฉ•ไพกใ•ใ‚Œใพใ™ใ€‚ใใ†ใ ใฃใŸ๏ผ‰ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€Word Error Rate (WER) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚’่กŒใ†ใซใฏ ๐Ÿค— Evaluate ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ WER ใฎๆฝœๅœจ็š„ใชๅˆถ้™ใ‚„ใใฎไป–ใฎๅ•้กŒ็‚นใซใคใ„ใฆใฏใ€[ใ“ใฎใ‚ฌใ‚คใƒ‰](https://huggingface.co/spaces/evaluate-metric/wer) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +```python +from evaluate import load +import torch + +wer = load("wer") + + +def compute_metrics(eval_pred): + logits, labels = eval_pred + predicted = logits.argmax(-1) + decoded_labels = processor.batch_decode(labels, skip_special_tokens=True) + decoded_predictions = processor.batch_decode(predicted, skip_special_tokens=True) + wer_score = wer.compute(predictions=decoded_predictions, references=decoded_labels) + return {"wer_score": wer_score} +``` + +## Train! + +ใ“ใ‚Œใงใ€ใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใ“ใ‚Œใซใฏ ๐Ÿค— [`Trainer`] ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +ใพใšใ€[`TrainingArguments`] ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```python +from transformers import TrainingArguments, Trainer + +model_name = checkpoint.split("/")[1] + +training_args = TrainingArguments( + output_dir=f"{model_name}-pokemon", + learning_rate=5e-5, + num_train_epochs=50, + fp16=True, + per_device_train_batch_size=32, + per_device_eval_batch_size=32, + gradient_accumulation_steps=2, + save_total_limit=3, + eval_strategy="steps", + eval_steps=50, + save_strategy="steps", + save_steps=50, + logging_steps=50, + remove_unused_columns=False, + push_to_hub=True, + label_names=["labels"], + load_best_model_at_end=True, +) +``` + +Trainer ๆฌกใซใ€ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใจใƒขใƒ‡ใƒซใจไธ€็ท’ใซ ๐Ÿค— ใซๆธกใ—ใพใ™ใ€‚ + +```python +trainer = Trainer( + model=model, + args=training_args, + train_dataset=train_ds, + eval_dataset=test_ds, + compute_metrics=compute_metrics, +) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ใซใฏใ€[`Trainer`] ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ™ใ ใ‘ใงใ™ใ€‚ + +```python +trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒ้€ฒใ‚€ใซใคใ‚Œใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆๅคฑใŒใ‚นใƒ ใƒผใ‚บใซๆธ›ๅฐ‘ใ™ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```python +trainer.push_to_hub() +``` + +## Inference + +`test_ds` ใ‹ใ‚‰ใ‚ตใƒณใƒ—ใƒซ็”ปๅƒใ‚’ๅ–ๅพ—ใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒ†ใ‚นใƒˆใ—ใพใ™ใ€‚ + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png" +image = Image.open(requests.get(url, stream=True).raw) +image +``` + +
+ Test image +
+ +ใƒขใƒ‡ใƒซ็”จใฎ็”ปๅƒใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ + +```python +device = "cuda" if torch.cuda.is_available() else "cpu" + +inputs = processor(images=image, return_tensors="pt").to(device) +pixel_values = inputs.pixel_values +``` + +[`generate`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆไบˆๆธฌใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```python +generated_ids = model.generate(pixel_values=pixel_values, max_length=50) +generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] +print(generated_caption) +``` +```bash +a drawing of a pink and blue pokemon +``` + +ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใซใ‚ˆใ‚Šใ€้žๅธธใซๅ„ชใ‚ŒใŸใ‚ญใƒฃใƒ—ใ‚ทใƒงใƒณใŒ็”Ÿๆˆใ•ใ‚ŒใŸใ‚ˆใ†ใงใ™ใ€‚ + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..013dfc286dce63bd71f5cc934bd5427d4d371efa --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_classification.md @@ -0,0 +1,553 @@ + + + +# Image classification + +[[open-in-colab]] + + + +็”ปๅƒๅˆ†้กžใงใฏใ€็”ปๅƒใซใƒฉใƒ™ใƒซใพใŸใฏใ‚ฏใƒฉใ‚นใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚„้Ÿณๅฃฐใฎๅˆ†้กžใจใฏ็•ฐใชใ‚Šใ€ๅ…ฅๅŠ›ใฏ +็”ปๅƒใ‚’ๆง‹ๆˆใ™ใ‚‹ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใ€‚ๆๅ‚ทใฎๆคœๅ‡บใชใฉใ€็”ปๅƒๅˆ†้กžใซใฏๅคšใใฎ็”จ้€”ใŒใ‚ใ‚Šใพใ™ +่‡ช็„ถ็ฝๅฎณใฎๅพŒใ€ไฝœ็‰ฉใฎๅฅๅบท็Šถๆ…‹ใ‚’็›ฃ่ฆ–ใ—ใŸใ‚Šใ€็—…ๆฐ—ใฎๅ…†ๅ€™ใŒใชใ„ใ‹ๅŒป็™‚็”ปๅƒใ‚’ใ‚นใ‚ฏใƒชใƒผใƒ‹ใƒณใ‚ฐใ—ใŸใ‚Šใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [Food-101](https://huggingface.co/datasets/food101) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ [ViT](model_doc/vit) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€็”ปๅƒๅ†…ใฎ้ฃŸๅ“ใ‚’ๅˆ†้กžใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/image-classification) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate +``` + +Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load Food-101 dataset + +Datasetsใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ Food-101 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆฌกใฎๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ +ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> food = load_dataset("food101", split="train[:5000]") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train` ๅˆ†ๅ‰ฒใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + + +```py +>>> food = food.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> food["train"][0] +{'image': , + 'label': 79} +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎๅ„ไพ‹ใซใฏ 2 ใคใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `image`: ้ฃŸๅ“ใฎ PIL ็”ปๅƒ +- `label`: ้ฃŸๅ“ใฎใƒฉใƒ™ใƒซใ‚ฏใƒฉใ‚น + +ใƒขใƒ‡ใƒซใŒใƒฉใƒ™ใƒซ ID ใ‹ใ‚‰ใƒฉใƒ™ใƒซๅใ‚’ๅ–ๅพ—ใ—ใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซใ€ใƒฉใƒ™ใƒซๅใ‚’ใƒžใƒƒใƒ—ใ™ใ‚‹่พžๆ›ธใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ๆ•ดๆ•ฐใธใฎๅค‰ๆ›ใ€ใพใŸใฏใใฎ้€†: + +```py +>>> labels = food["train"].features["label"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +ใ“ใ‚Œใงใ€ใƒฉใƒ™ใƒซ ID ใ‚’ใƒฉใƒ™ใƒซๅใซๅค‰ๆ›ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```py +>>> id2label[str(79)] +'prime_rib' +``` + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€ViT ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ใƒ†ใƒณใ‚ฝใƒซใซๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "google/vit-base-patch16-224-in21k" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +``` + + + + + +ใ„ใใคใ‹ใฎ็”ปๅƒๅค‰ๆ›ใ‚’็”ปๅƒใซ้ฉ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ้Žๅญฆ็ฟ’ใซๅฏพใ™ใ‚‹ๅ …็‰ขๆ€งใ‚’้ซ˜ใ‚ใพใ™ใ€‚ใ“ใ“ใงใฏ torchvision ใฎ [`transforms`](https://pytorch.org/vision/stable/transforms.html) ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ไปปๆ„ใฎ็”ปๅƒใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +็”ปๅƒใฎใƒฉใƒณใƒ€ใƒ ใช้ƒจๅˆ†ใ‚’ใƒˆใƒชใƒŸใƒณใ‚ฐใ—ใ€ใ‚ตใ‚คใ‚บใ‚’ๅค‰ๆ›ดใ—ใ€็”ปๅƒใฎๅนณๅ‡ใจๆจ™ๆบ–ๅๅทฎใงๆญฃ่ฆๅŒ–ใ—ใพใ™ใ€‚ + + +```py +>>> from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor + +>>> normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std) +>>> size = ( +... image_processor.size["shortest_edge"] +... if "shortest_edge" in image_processor.size +... else (image_processor.size["height"], image_processor.size["width"]) +... ) +>>> _transforms = Compose([RandomResizedCrop(size), ToTensor(), normalize]) +``` + +ๆฌกใซใ€ๅค‰ๆ›ใ‚’้ฉ็”จใ—ใ€็”ปๅƒใฎ `pixel_values` (ใƒขใƒ‡ใƒซใธใฎๅ…ฅๅŠ›) ใ‚’่ฟ”ใ™ๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(img.convert("RGB")) for img in examples["image"]] +... del examples["image"] +... return examples +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.with_transform`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅค‰ๆ›ใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ฆ็ด ใ‚’่ชญใฟ่พผใ‚€ใจใใซใ‚ชใƒณใ‚ถใƒ•ใƒฉใ‚คใง้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ + +```py +>>> food = food.with_transform(transforms) +``` + +ๆฌกใซใ€[`DefaultDataCollatโ€‹โ€‹or`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ๐Ÿค— Transformers ใฎไป–ใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใฏ็•ฐใชใ‚Šใ€`DefaultDataCollatโ€‹โ€‹or` ใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใชใฉใฎ่ฟฝๅŠ ใฎๅ‰ๅ‡ฆ็†ใ‚’้ฉ็”จใ—ใพใ›ใ‚“ใ€‚ + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + + + + + + +้Žๅ‰ฐ้ฉๅˆใ‚’ๅ›ž้ฟใ—ใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Šๅ …็‰ขใซใ™ใ‚‹ใŸใ‚ใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ้ƒจๅˆ†ใซใƒ‡ใƒผใ‚ฟๆ‹กๅผตใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€Keras ๅ‰ๅ‡ฆ็†ใƒฌใ‚คใƒคใƒผใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใฎๅค‰ๆ› (ใƒ‡ใƒผใ‚ฟๆ‹กๅผตใ‚’ๅซใ‚€) ใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ +ๆคœ่จผใƒ‡ใƒผใ‚ฟใฎๅค‰ๆ› (ไธญๅคฎใฎใƒˆใƒชใƒŸใƒณใ‚ฐใ€ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ€ๆญฃ่ฆๅŒ–ใฎใฟ)ใ€‚ `tf.image` ใพใŸใฏ +ไป–ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใงใ‚‚ๆง‹ใ„ใพใ›ใ‚“ใ€‚ + + +```py +>>> from tensorflow import keras +>>> from tensorflow.keras import layers + +>>> size = (image_processor.size["height"], image_processor.size["width"]) + +>>> train_data_augmentation = keras.Sequential( +... [ +... layers.RandomCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... layers.RandomFlip("horizontal"), +... layers.RandomRotation(factor=0.02), +... layers.RandomZoom(height_factor=0.2, width_factor=0.2), +... ], +... name="train_data_augmentation", +... ) + +>>> val_data_augmentation = keras.Sequential( +... [ +... layers.CenterCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... ], +... name="val_data_augmentation", +... ) +``` + +ๆฌกใซใ€ไธ€ๅบฆใซ 1 ใคใฎ็”ปๅƒใงใฏใชใใ€็”ปๅƒใฎใƒใƒƒใƒใซ้ฉๅˆ‡ใชๅค‰ๆ›ใ‚’้ฉ็”จใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np +>>> import tensorflow as tf +>>> from PIL import Image + + +>>> def convert_to_tf_tensor(image: Image): +... np_image = np.array(image) +... tf_image = tf.convert_to_tensor(np_image) +... # `expand_dims()` is used to add a batch dimension since +... # the TF augmentation layers operates on batched inputs. +... return tf.expand_dims(tf_image, 0) + + +>>> def preprocess_train(example_batch): +... """Apply train_transforms across a batch.""" +... images = [ +... train_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch + + +... def preprocess_val(example_batch): +... """Apply val_transforms across a batch.""" +... images = [ +... val_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch +``` + +๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ [`~datasets.Dataset.set_transform`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใใฎๅ ดใงๅค‰ๆ›ใ‚’้ฉ็”จใ—ใพใ™ใ€‚ + +```py +food["train"].set_transform(preprocess_train) +food["test"].set_transform(preprocess_val) +``` + +ๆœ€ๅพŒใฎๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใจใ—ใฆใ€`DefaultDataCollatโ€‹โ€‹or`ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ๐Ÿค— Transformers ใฎไป–ใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๆฉŸ่ƒฝใจใฏ็•ฐใชใ‚Šใ€ +`DefaultDataCollatโ€‹โ€‹or` ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใชใฉใฎ่ฟฝๅŠ ใฎๅ‰ๅ‡ฆ็†ใ‚’้ฉ็”จใ—ใพใ›ใ‚“ใ€‚ + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ใ™ใใซใƒญใƒผใƒ‰ใงใใพใ™ +๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใŸ่ฉ•ไพกๆ–นๆณ•ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€ใƒญใƒผใƒ‰ใ—ใพใ™ +[accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ๆŒ‡ๆจ™ (่ฉณ็ดฐใซใคใ„ใฆใฏใ€๐Ÿค— ่ฉ•ไพก [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่จˆ็ฎ—ใ™ใ‚‹ๆ–นๆณ•): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +ใ“ใ‚Œใง `compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่จญๅฎšใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForImageClassification`] ใ‚’ไฝฟ็”จใ—ใฆ ViT ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒฉใƒ™ใƒซใฎๆ•ฐใจไบˆๆƒณใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใ€ใŠใ‚ˆใณใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForImageClassification, TrainingArguments, Trainer + +>>> model = AutoModelForImageClassification.from_pretrained( +... checkpoint, +... num_labels=len(labels), +... id2label=id2label, +... label2id=label2id, +... ) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ใ‚นใƒ†ใƒƒใƒ—ใฏ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ `image` ๅˆ—ใŒๅ‰Š้™คใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆœชไฝฟ็”จใฎๅˆ—ใ‚’ๅ‰Š้™คใ—ใชใ„ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ `image` ๅˆ—ใŒใชใ„ใจใ€`pixel_values` ใ‚’ไฝœๆˆใงใใพใ›ใ‚“ใ€‚ใ“ใฎๅ‹•ไฝœใ‚’้˜ฒใใซใฏใ€`remove_unused_columns=False`ใ‚’่จญๅฎšใ—ใฆใใ ใ•ใ„ใ€‚ไป–ใซๅฟ…่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใ ใ‘ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ็ฒพๅบฆใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_food_model", +... remove_unused_columns=False, +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=food["train"], +... eval_dataset=food["test"], +... processing_class=image_processor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + + + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€ใพใš [ๅŸบๆœฌใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](./training#train-a-tensorflow-model-with-keras) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆ‰‹้ †ใซๅพ“ใ„ใพใ™ใ€‚ +1. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +2. ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ +3. ๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ +4. ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใพใ™ใ€‚ +5. ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ—ใ€`fit()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +6. ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— Hub ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ—ใพใ™ใ€‚ + +ใพใšใ€ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 5 +>>> num_train_steps = len(food["train"]) * num_epochs +>>> learning_rate = 3e-5 +>>> weight_decay_rate = 0.01 + +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=learning_rate, +... num_train_steps=num_train_steps, +... weight_decay_rate=weight_decay_rate, +... num_warmup_steps=0, +... ) +``` + +ๆฌกใซใ€ใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใจใจใ‚‚ใซ [`TFAutoModelForImageClassification`] ใ‚’ไฝฟ็”จใ—ใฆ ViT ใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ) +``` + +Convert your datasets to the `tf.data.Dataset` format using the [`~datasets.Dataset.to_tf_dataset`] and your `data_collator`: + +```py +>>> # converting our train dataset to tf.data.Dataset +>>> tf_train_dataset = food["train"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) + +>>> # converting our test dataset to tf.data.Dataset +>>> tf_eval_dataset = food["test"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) +``` + +`compile()` ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from tensorflow.keras.losses import SparseCategoricalCrossentropy + +>>> loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) +>>> model.compile(optimizer=optimizer, loss=loss) +``` + +ไบˆๆธฌใ‹ใ‚‰็ฒพๅบฆใ‚’่จˆ็ฎ—ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใซใฏใ€[Keras callbacks](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +`compute_metrics` ้–ขๆ•ฐใ‚’ [KerasMetricCallback](../main_classes/keras_callbacks#transformers.KerasMetricCallback) ใซๆธกใ—ใพใ™ใ€‚ +[PushToHubCallback](../main_classes/keras_callbacks#transformers.PushToHubCallback) ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset) +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="food_classifier", +... tokenizer=image_processor, +... save_strategy="no", +... ) +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks) +Epoch 1/5 +250/250 [==============================] - 313s 1s/step - loss: 2.5623 - val_loss: 1.4161 - accuracy: 0.9290 +Epoch 2/5 +250/250 [==============================] - 265s 1s/step - loss: 0.9181 - val_loss: 0.6808 - accuracy: 0.9690 +Epoch 3/5 +250/250 [==============================] - 252s 1s/step - loss: 0.3910 - val_loss: 0.4303 - accuracy: 0.9820 +Epoch 4/5 +250/250 [==============================] - 251s 1s/step - loss: 0.2028 - val_loss: 0.3191 - accuracy: 0.9900 +Epoch 5/5 +250/250 [==============================] - 238s 949ms/step - loss: 0.1232 - val_loss: 0.3259 - accuracy: 0.9890 +``` + +ใŠใ‚ใงใจใ†๏ผใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€๐Ÿค— Hub ใงๅ…ฑๆœ‰ใ—ใพใ—ใŸใ€‚ใ“ใ‚ŒใงๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + + + + + + +็”ปๅƒๅˆ†้กž็”จใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ [PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb) + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใŸใ„็”ปๅƒใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> ds = load_dataset("food101", split="validation[:10]") +>>> image = ds["image"][0] +``` + +
+ image of beignets +
+ +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒๅˆ†้กž็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซ็”ปๅƒใ‚’ๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("image-classification", model="my_awesome_food_model") +>>> classifier(image) +[{'score': 0.31856709718704224, 'label': 'beignets'}, + {'score': 0.015232225880026817, 'label': 'bruschetta'}, + {'score': 0.01519392803311348, 'label': 'chicken_wings'}, + {'score': 0.013022331520915031, 'label': 'pork_chop'}, + {'score': 0.012728818692266941, 'label': 'prime_rib'}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + + +็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€`input`ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor +>>> import torch + +>>> image_processor = AutoImageProcessor.from_pretrained("my_awesome_food_model") +>>> inputs = image_processor(image, return_tensors="pt") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ใƒญใ‚ธใƒƒใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForImageClassification + +>>> model = AutoModelForImageClassification.from_pretrained("my_awesome_food_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงไบˆๆธฌใ•ใ‚ŒใŸใƒฉใƒ™ใƒซใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎ `id2label` ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใƒฉใƒ™ใƒซใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> predicted_label = logits.argmax(-1).item() +>>> model.config.id2label[predicted_label] +'beignets' +``` + + + + + + +็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€`input`ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("MariaK/food_classifier") +>>> inputs = image_processor(image, return_tensors="tf") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ใƒญใ‚ธใƒƒใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained("MariaK/food_classifier") +>>> logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงไบˆๆธฌใ•ใ‚ŒใŸใƒฉใƒ™ใƒซใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎ `id2label` ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใƒฉใƒ™ใƒซใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + + +```py +>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0]) +>>> model.config.id2label[predicted_class_id] +'beignets' +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_to_image.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_to_image.md new file mode 100644 index 0000000000000000000000000000000000000000..cd429a4e1e27c3281d823683768eb2bac101e197 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/image_to_image.md @@ -0,0 +1,135 @@ + + +# Image-to-Image Task Guide + +[[open-in-colab]] + +Image-to-Image ใ‚ฟใ‚นใ‚ฏใฏใ€ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใŒ็”ปๅƒใ‚’ๅ—ไฟกใ—ใ€ๅˆฅใฎ็”ปๅƒใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ใ“ใ‚Œใซใฏใ€็”ปๅƒๅผทๅŒ– (่ถ…่งฃๅƒๅบฆใ€ไฝŽๅ…‰้‡ๅผทๅŒ–ใ€ใƒ‡ใ‚ฃใƒฌใ‚คใƒณใชใฉ)ใ€็”ปๅƒไฟฎๅพฉใชใฉใ‚’ๅซใ‚€ใ•ใพใ–ใพใชใ‚ตใƒ–ใ‚ฟใ‚นใ‚ฏใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ +- ่ถ…่งฃๅƒๅบฆใ‚ฟใ‚นใ‚ฏใซ็”ปๅƒ้–“ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +- ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ›ใšใซใ€ๅŒใ˜ใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใ‚คใƒกใƒผใ‚ธ้–“ใƒขใƒ‡ใƒซใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใŒใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸๆ™‚็‚นใงใฏใ€`image-to-image`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ่ถ…่งฃๅƒๅบฆใ‚ฟใ‚นใ‚ฏใฎใฟใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚ + +```bash +pip install transformers +``` + +[Swin2SR ใƒขใƒ‡ใƒซ](https://huggingface.co/caidas/swin2SR-lightweight-x2-64) ใ‚’ไฝฟ็”จใ—ใฆใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅˆๆœŸๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ๆฌกใซใ€ใ‚คใƒกใƒผใ‚ธใ‚’ไฝฟ็”จใ—ใฆใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๅ‘ผใณๅ‡บใ™ใ“ใจใงใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ๆŽจ่ซ–ใงใใพใ™ใ€‚็พๆ™‚็‚นใงใฏใ€[Swin2SR ใƒขใƒ‡ใƒซ](https://huggingface.co/models?sort=trending&search=swin2sr) ใฎใฟใŒใ“ใฎใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +```python +from transformers import pipeline + +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') +pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device) +``` + +ใงใฏใ€็”ปๅƒใ‚’่ชญใฟ่พผใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat.jpg" +image = Image.open(requests.get(url, stream=True).raw) + +print(image.size) +``` +```bash +# (532, 432) +``` +
+ Photo of a cat +
+ + +ใ“ใ‚Œใงใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚็Œซใฎ็”ปๅƒใฎๆ‹กๅคงใƒใƒผใ‚ธใƒงใƒณใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```python +upscaled = pipe(image) +print(upscaled.size) +``` +```bash +# (1072, 880) +``` + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ›ใšใซ่‡ชๅˆ†ใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใฎ `Swin2SRForImageSuperResolution` ใ‚ฏใƒฉใ‚นใจ `Swin2SRImageProcessor` ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ใ“ใ‚ŒใซใฏๅŒใ˜ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใจใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```python +from transformers import Swin2SRForImageSuperResolution, Swin2SRImageProcessor + +model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-lightweight-x2-64").to(device) +processor = Swin2SRImageProcessor("caidas/swin2SR-lightweight-x2-64") +``` + +`pipeline`ใ€ใฏใ€่‡ชๅˆ†ใง่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ๅ‰ๅ‡ฆ็†ใจๅพŒๅ‡ฆ็†ใฎใ‚นใƒ†ใƒƒใƒ—ใ‚’ๆŠฝ่ฑกๅŒ–ใ™ใ‚‹ใฎใงใ€็”ปๅƒใ‚’ๅ‰ๅ‡ฆ็†ใ—ใพใ—ใ‚‡ใ†ใ€‚็”ปๅƒใ‚’ใƒ—ใƒญใ‚ปใƒƒใ‚ตใซๆธกใ—ใฆใ‹ใ‚‰ใ€ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใ‚’ GPU ใซ็งปๅ‹•ใ—ใพใ™ใ€‚ + +```python +pixel_values = processor(image, return_tensors="pt").pixel_values +print(pixel_values.shape) + +pixel_values = pixel_values.to(device) +``` + +ใ“ใ‚Œใงใ€ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ใ“ใจใง็”ปๅƒใ‚’ๆŽจๆธฌใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```python +import torch + +with torch.no_grad(): + outputs = model(pixel_values) +``` + +ๅ‡บๅŠ›ใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใช `ImageSuperResolutionOutput` ใ‚ฟใ‚คใƒ—ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใ™ ๐Ÿ‘‡ + +``` +(loss=None, reconstruction=tensor([[[[0.8270, 0.8269, 0.8275, ..., 0.7463, 0.7446, 0.7453], + [0.8287, 0.8278, 0.8283, ..., 0.7451, 0.7448, 0.7457], + [0.8280, 0.8273, 0.8269, ..., 0.7447, 0.7446, 0.7452], + ..., + [0.5923, 0.5933, 0.5924, ..., 0.0697, 0.0695, 0.0706], + [0.5926, 0.5932, 0.5926, ..., 0.0673, 0.0687, 0.0705], + [0.5927, 0.5914, 0.5922, ..., 0.0664, 0.0694, 0.0718]]]], + device='cuda:0'), hidden_states=None, attentions=None) +``` + +`reconstruction`ใ‚’ๅ–ๅพ—ใ—ใ€ใใ‚Œใ‚’่ฆ–่ฆšๅŒ–ใ™ใ‚‹ใŸใ‚ใซๅพŒๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใฉใฎใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ‹่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +outputs.reconstruction.data.shape +# torch.Size([1, 3, 880, 1072]) +``` + +ๅ‡บๅŠ›ใ‚’ๅœง็ธฎใ—ใฆ่ปธ 0 ใ‚’ๅ‰Š้™คใ—ใ€ๅ€คใ‚’ใ‚ฏใƒชใƒƒใƒ—ใ—ใฆใ‹ใ‚‰ใ€ใใ‚Œใ‚’ numpy float ใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฌกใซใ€่ปธใ‚’ [1072, 880] ใฎๅฝข็Šถใซใชใ‚‹ใ‚ˆใ†ใซ้…็ฝฎใ—ใ€ๆœ€ๅพŒใซๅ‡บๅŠ›ใ‚’็ฏ„ๅ›ฒ [0, 255] ใซๆˆปใ—ใพใ™ใ€‚ + +```python +import numpy as np + +# squeeze, take to CPU and clip the values +output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy() +# rearrange the axes +output = np.moveaxis(output, source=0, destination=-1) +# bring values back to pixel values range +output = (output * 255.0).round().astype(np.uint8) +Image.fromarray(output) +``` +
+ Upscaled photo of a cat +
\ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/knowledge_distillation_for_image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/knowledge_distillation_for_image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..b8d4a569b1c24416ee22bd3adabb88d9e2212679 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/knowledge_distillation_for_image_classification.md @@ -0,0 +1,188 @@ + +# Knowledge Distillation for Computer Vision + +[[open-in-colab]] + +็Ÿฅ่ญ˜ใฎ่’ธ็•™ใฏใ€ใ‚ˆใ‚Šๅคง่ฆๆจกใง่ค‡้›‘ใชใƒขใƒ‡ใƒซ (ๆ•™ๅธซ) ใ‹ใ‚‰ใ‚ˆใ‚Šๅฐ่ฆๆจกใงๅ˜็ด”ใชใƒขใƒ‡ใƒซ (็”Ÿๅพ’) ใซ็Ÿฅ่ญ˜ใ‚’ไผ้”ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๆ‰‹ๆณ•ใงใ™ใ€‚ใ‚ใ‚‹ใƒขใƒ‡ใƒซใ‹ใ‚‰ๅˆฅใฎใƒขใƒ‡ใƒซใซ็Ÿฅ่ญ˜ใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใซใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏ (ใ“ใฎๅ ดๅˆใฏ็”ปๅƒๅˆ†้กž) ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟๆ•™ๅธซใƒขใƒ‡ใƒซใ‚’ๅ–ๅพ—ใ—ใ€็”ปๅƒๅˆ†้กžใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚‹็”Ÿๅพ’ใƒขใƒ‡ใƒซใ‚’ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ๆฌกใซใ€ๅญฆ็”Ÿใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใ€ใใฎๅ‡บๅŠ›ใจๆ•™ๅธซใฎๅ‡บๅŠ›ใฎๅทฎใ‚’ๆœ€ๅฐ้™ใซๆŠ‘ใˆใ€ๅ‹•ไฝœใ‚’ๆจกๅ€ฃใ—ใพใ™ใ€‚ใ“ใ‚Œใฏ [Distilling the Knowledge in a Neural Network by Hinton et al](https://huggingface.co/papers/1503.02531) ใงๆœ€ๅˆใซๅฐŽๅ…ฅใ•ใ‚Œใพใ—ใŸใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ใ‚ฟใ‚นใ‚ฏๅ›บๆœ‰ใฎ็Ÿฅ่ญ˜ใฎ่’ธ็•™ใ‚’่กŒใ„ใพใ™ใ€‚ใ“ใ‚Œใซใฏ [Beans ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/datasets/beans) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€[ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ ViT ใƒขใƒ‡ใƒซ](https://huggingface.co/merve/vit-mobilenet-beans-224) (ๆ•™ๅธซใƒขใƒ‡ใƒซ) ใ‚’ๆŠฝๅ‡บใ—ใฆ [MobileNet](https://huggingface.co/google/mobilenet_v2_1.4_224) (ๅญฆ็”Ÿใƒขใƒ‡ใƒซ) ๐Ÿค— Transformers ใฎ [Trainer API](https://huggingface.co/docs/transformers/en/main_classes/trainer#trainer) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +่’ธ็•™ใจใƒ—ใƒญใ‚ปใ‚นใฎ่ฉ•ไพกใซๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```bash +pip install transformers datasets accelerate tensorboard evaluate --upgrade +``` + +ใ“ใฎไพ‹ใงใฏใ€ๆ•™ๅธซใƒขใƒ‡ใƒซใจใ—ใฆ`merve/beans-vit-224`ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใ€Bean ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅŸบใฅใ„ใฆๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ`google/vit-base-patch16-224-in21k`ใซๅŸบใฅใ็”ปๅƒๅˆ†้กžใƒขใƒ‡ใƒซใงใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ MobileNetV2 ใซๆŠฝๅ‡บใ—ใพใ™ใ€‚ + +ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```python +from datasets import load_dataset + +dataset = load_dataset("beans") +``` + +ใ“ใฎๅ ดๅˆใ€ๅŒใ˜่งฃๅƒๅบฆใงๅŒใ˜ๅ‡บๅŠ›ใŒ่ฟ”ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใฉใกใ‚‰ใฎใƒขใƒ‡ใƒซใฎ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ `dataset`ใฎ`map()`ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ™ในใฆใฎๅˆ†ๅ‰ฒใซๅ‰ๅ‡ฆ็†ใ‚’้ฉ็”จใ—ใพใ™ใ€‚ + +```python +from transformers import AutoImageProcessor +teacher_processor = AutoImageProcessor.from_pretrained("merve/beans-vit-224") + +def process(examples): + processed_inputs = teacher_processor(examples["image"]) + return processed_inputs + +processed_datasets = dataset.map(process, batched=True) +``` + +ๅŸบๆœฌ็š„ใซใ€ๆˆ‘ใ€…ใฏ็”Ÿๅพ’ใƒขใƒ‡ใƒซ๏ผˆใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸMobileNet๏ผ‰ใŒๆ•™ๅธซใƒขใƒ‡ใƒซ๏ผˆๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒ“ใ‚ธใƒงใƒณๅค‰ๆ›ๅ™จ๏ผ‰ใ‚’ๆจกๅ€ฃใ™ใ‚‹ใ“ใจใ‚’ๆœ›ใ‚€ใ€‚ใ“ใ‚Œใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซใ€ใพใšๆ•™ๅธซใจ็”Ÿๅพ’ใ‹ใ‚‰ใƒญใ‚ธใƒƒใƒˆๅ‡บๅŠ›ใ‚’ๅพ—ใ‚‹ใ€‚ๆฌกใซใ€ใใ‚Œใžใ‚Œใฎใ‚ฝใƒ•ใƒˆใ‚ฟใƒผใ‚ฒใƒƒใƒˆใฎ้‡่ฆๅบฆใ‚’ๅˆถๅพกใ™ใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟ`temperature`ใงๅˆ†ๅ‰ฒใ™ใ‚‹ใ€‚`lambda`ใจๅ‘ผใฐใ‚Œใ‚‹ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏ่’ธ็•™ใƒญใ‚นใฎ้‡่ฆๅบฆใ‚’้‡ใ‚‹ใ€‚ใ“ใฎไพ‹ใงใฏใ€`temperature=5`ใ€`lambda=0.5`ใจใ™ใ‚‹ใ€‚็”Ÿๅพ’ใจๆ•™ๅธซใฎ้–“ใฎ็™บๆ•ฃใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซใ€Kullback-Leibler็™บๆ•ฃๆๅคฑใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚2ใคใฎใƒ‡ใƒผใ‚ฟPใจQใŒไธŽใˆใ‚‰ใ‚ŒใŸใจใใ€KLใƒ€ใ‚คใƒใƒผใ‚ธใ‚งใƒณใ‚นใฏQใ‚’ไฝฟใฃใฆPใ‚’่กจ็พใ™ใ‚‹ใŸใ‚ใซใฉใ‚Œใ ใ‘ใฎไฝ™ๅˆ†ใชๆƒ…ๅ ฑใŒๅฟ…่ฆใ‹ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใ‚‚ใ—2ใคใŒๅŒใ˜ใงใ‚ใ‚Œใฐใ€Qใ‹ใ‚‰Pใ‚’่ชฌๆ˜Žใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใชไป–ใฎๆƒ…ๅ ฑใฏใชใ„ใฎใงใ€ใใ‚Œใ‚‰ใฎKLใƒ€ใ‚คใƒใƒผใ‚ธใ‚งใƒณใ‚นใฏใ‚ผใƒญใซใชใ‚Šใพใ™ใ€‚ + + +```python +from transformers import TrainingArguments, Trainer +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class ImageDistilTrainer(Trainer): + def __init__(self, *args, teacher_model=None, **kwargs): + super().__init__(*args, **kwargs) + self.teacher = teacher_model + self.student = student_model + self.loss_function = nn.KLDivLoss(reduction="batchmean") + device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') + self.teacher.to(device) + self.teacher.eval() + self.temperature = temperature + self.lambda_param = lambda_param + + def compute_loss(self, student, inputs, return_outputs=False): + student_output = self.student(**inputs) + + with torch.no_grad(): + teacher_output = self.teacher(**inputs) + + # Compute soft targets for teacher and student + soft_teacher = F.softmax(teacher_output.logits / self.temperature, dim=-1) + soft_student = F.log_softmax(student_output.logits / self.temperature, dim=-1) + + # Compute the loss + distillation_loss = self.loss_function(soft_student, soft_teacher) * (self.temperature ** 2) + + # Compute the true label loss + student_target_loss = student_output.loss + + # Calculate final loss + loss = (1. - self.lambda_param) * student_target_loss + self.lambda_param * distillation_loss + return (loss, student_output) if return_outputs else loss +``` + +ๆฌกใซใ€Hugging Face Hub ใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€`trainer`ใ‚’้€šใ˜ใฆใƒขใƒ‡ใƒซใ‚’ Hugging Face Hub ใซใƒ—ใƒƒใ‚ทใƒฅใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +ๆ•™ๅธซใƒขใƒ‡ใƒซใจ็”Ÿๅพ’ใƒขใƒ‡ใƒซใงใ‚ใ‚‹`TrainingArguments`ใ‚’่จญๅฎšใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```python +from transformers import AutoModelForImageClassification, MobileNetV2Config, MobileNetV2ForImageClassification + +training_args = TrainingArguments( + output_dir="my-awesome-model", + num_train_epochs=30, + fp16=True, + logging_dir=f"{repo_name}/logs", + logging_strategy="epoch", + eval_strategy="epoch", + save_strategy="epoch", + load_best_model_at_end=True, + metric_for_best_model="accuracy", + report_to="tensorboard", + push_to_hub=True, + hub_strategy="every_save", + hub_model_id=repo_name, + ) + +num_labels = len(processed_datasets["train"].features["labels"].names) + +# initialize models +teacher_model = AutoModelForImageClassification.from_pretrained( + "merve/beans-vit-224", + num_labels=num_labels, + ignore_mismatched_sizes=True +) + +# training MobileNetV2 from scratch +student_config = MobileNetV2Config() +student_config.num_labels = num_labels +student_model = MobileNetV2ForImageClassification(student_config) +``` + +`compute_metrics` ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใงใใพใ™ใ€‚ใ“ใฎ้–ขๆ•ฐใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ—ใƒญใ‚ปใ‚นไธญใซใƒขใƒ‡ใƒซใฎ`accuracy`ใจ`f1`ใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +```python +import evaluate +import numpy as np + +accuracy = evaluate.load("accuracy") + +def compute_metrics(eval_pred): + predictions, labels = eval_pred + acc = accuracy.compute(references=labels, predictions=np.argmax(predictions, axis=1)) + return {"accuracy": acc["accuracy"]} +``` + +ๅฎš็พฉใ—ใŸใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆ`Trainer`ใ‚’ๅˆๆœŸๅŒ–ใ—ใพใ—ใ‚‡ใ†ใ€‚ใƒ‡ใƒผใ‚ฟ็…งๅˆ่ฃ…็ฝฎใ‚‚ๅˆๆœŸๅŒ–ใ—ใพใ™ใ€‚ + + +```python +from transformers import DefaultDataCollator + +data_collator = DefaultDataCollator() +trainer = ImageDistilTrainer( + student_model=student_model, + teacher_model=teacher_model, + training_args=training_args, + train_dataset=processed_datasets["train"], + eval_dataset=processed_datasets["validation"], + data_collator=data_collator, + processing_class=teacher_extractor, + compute_metrics=compute_metrics, + temperature=5, + lambda_param=0.5 +) +``` + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```python +trainer.train() +``` + +ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใงใใพใ™ใ€‚ + + +```python +trainer.evaluate(processed_datasets["test"]) +``` + +ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใงใฏใ€ใƒขใƒ‡ใƒซใฎ็ฒพๅบฆใฏ 72% ใซ้”ใ—ใพใ™ใ€‚่’ธ็•™ๅŠน็އใฎๅฅๅ…จๆ€งใƒใ‚งใƒƒใ‚ฏใ‚’่กŒใ†ใŸใ‚ใซใ€ๅŒใ˜ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆ Bean ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง MobileNet ใ‚’ๆœ€ๅˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใง 63% ใฎ็ฒพๅบฆใ‚’่ฆณๅฏŸใ—ใพใ—ใŸใ€‚่ชญ่€…ใฎ็š†ๆง˜ใซใฏใ€ใ•ใพใ–ใพใชไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟๆ•™ๅธซใƒขใƒ‡ใƒซใ€ๅญฆ็”Ÿใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€่’ธ็•™ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ฉฆใ—ใฆใ„ใŸใ ใใ€ใใฎ็ตๆžœใ‚’ๅ ฑๅ‘Šใ—ใฆใ„ใŸใ ใใ‚ˆใ†ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ๆŠฝๅ‡บใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒญใ‚ฐใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ [ใ“ใฎใƒชใƒใ‚ธใƒˆใƒช](https://huggingface.co/merve/vit-mobilenet-beans-224) ใซใ‚ใ‚Šใ€ๆœ€ๅˆใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ MobileNetV2 ใฏใ“ใฎ [ใƒชใƒใ‚ธใƒˆใƒช](https://huggingface.co/merve/resnet-mobilenet-beans-5)ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..1cadc0af0ac0a559dd3b332c742dffa503b4fe64 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/language_modeling.md @@ -0,0 +1,437 @@ + + +# Causal language modeling + + +[[open-in-colab]] + + +่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใซใฏใ€ๅ› ๆžœ็š„ใƒขใƒ‡ใƒชใƒณใ‚ฐใจใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎ 2 ใคใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๅ› ๆžœ้–ขไฟ‚ใฎใ‚ใ‚‹่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒซใฏใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซใ‚ˆใไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€ๆฌกใฎใ‚ˆใ†ใชใ‚ฏใƒชใ‚จใ‚คใƒ†ใ‚ฃใƒ–ใชใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซไฝฟ็”จใงใใพใ™ใ€‚ +็‹ฌ่‡ชใฎใƒ†ใ‚ญใ‚นใƒˆ ใ‚ขใƒ‰ใƒ™ใƒณใƒใƒฃใƒผใ‚’้ธๆŠžใ™ใ‚‹ใ‹ใ€Copilot ใ‚„ CodeParrot ใชใฉใฎใ‚คใƒณใƒ†ใƒชใ‚ธใ‚งใƒณใƒˆใชใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ ใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใ‚’้ธๆŠžใ—ใพใ™ใ€‚ + + + + +ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฏใ€ไธ€้€ฃใฎใƒˆใƒผใ‚ฏใƒณๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฏใ€ๆฌกใฎใƒˆใƒผใ‚ฏใƒณใซใฎใฟๅฏพๅฟœใงใใพใ™ใ€‚ +ๅทฆใ€‚ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใŒๅฐ†ๆฅใฎใƒˆใƒผใ‚ฏใƒณใ‚’่ช่ญ˜ใงใใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ GPT-2 ใฏๅ› ๆžœ็š„่จ€่ชžใƒขใƒ‡ใƒซใฎไธ€ไพ‹ใงใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [ELI5](https:/) ใฎ [r/askscience](https://www.reddit.com/r/askscience/) ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใง [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ /huggingface.co/datasets/eli5) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/text-generation) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚u + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load ELI5 dataset + + +ใพใšใ€ELI5 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ r/askscience ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ ๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5", split="train_asks[:5000]") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train_asks` ใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> eli5["train"][0] +{'answers': {'a_id': ['c3d1aib', 'c3d4lya'], + 'score': [6, 3], + 'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]}, + 'answers_urls': {'url': []}, + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']}, + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls': {'url': []}} +``` + +ใ“ใ‚Œใฏๅคšใใฎใ“ใจใฎใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ๅฎŸ้š›ใซ้–ขๅฟƒใŒใ‚ใ‚‹ใฎใฏ`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ ใ‘ใงใ™ใ€‚่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎๅ„ชใ‚Œใฆใ„ใ‚‹็‚น +ใ‚ฟใ‚นใ‚ฏใงใฏใ€ๆฌกใฎๅ˜่ชžใŒใƒฉใƒ™ใƒซ * ใงใ‚ใ‚‹ใŸใ‚ใ€ใƒฉใƒ™ใƒซ (ๆ•™ๅธซใชใ—ใ‚ฟใ‚นใ‚ฏใจใ‚‚ๅ‘ผใฐใ‚Œใพใ™) ใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + + +## Preprocess + + + + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใฏใ€`text`ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ DistilGPT2 ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") +``` + +ไธŠใฎไพ‹ใ‹ใ‚‰ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซใ€`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๅฎŸ้š›ใซใฏ`answers`ๅ†…ใซใƒใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใคใพใ‚Šใ€ๆฌกใฎใ“ใจใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ +[` flatten`](https://huggingface.co/docs/datasets/process.html#flatten) ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸๆง‹้€ ใ‹ใ‚‰ `text` ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'answers.a_id': ['c3d1aib', 'c3d4lya'], + 'answers.score': [6, 3], + 'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"], + 'answers_urls.url': [], + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'], + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls.url': []} +``` + +`answers`ๆŽฅ้ ญ่พžใง็คบใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ€ๅ„ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๅ€‹ๅˆฅใฎๅˆ—ใซใชใ‚Šใ€`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏใƒชใ‚นใƒˆใซใชใ‚Šใพใ—ใŸใ€‚ใใฎไปฃใ‚ใ‚Š +ๅ„ๆ–‡ใ‚’ๅ€‹ๅˆฅใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๅ ดๅˆใฏใ€ใƒชใ‚นใƒˆใ‚’ๆ–‡ๅญ—ๅˆ—ใซๅค‰ๆ›ใ—ใฆใ€ใใ‚Œใ‚‰ใ‚’ใพใจใ‚ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ๅ„ไพ‹ใฎๆ–‡ๅญ—ๅˆ—ใฎใƒชใ‚นใƒˆใ‚’็ตๅˆใ—ใ€็ตๆžœใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๆœ€ๅˆใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใงใ™ใ€‚ + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +ใ“ใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใซใฏใ€`batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ—ใ€`num_proc` ใงใƒ—ใƒญใ‚ปใ‚นใฎๆ•ฐใ‚’ๅข—ใ‚„ใ—ใพใ™ใ€‚ไธ่ฆใชๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใƒˆใƒผใ‚ฏใƒณ ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใใฎไธ€้ƒจใฏใƒขใƒ‡ใƒซใฎๆœ€ๅคงๅ…ฅๅŠ›้•ทใ‚ˆใ‚Šใ‚‚้•ทใใชใ‚Šใพใ™ใ€‚ + +2 ็•ช็›ฎใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ +- ใ™ในใฆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’้€ฃ็ตใ—ใพใ™ +- ้€ฃ็ตใ•ใ‚ŒใŸใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’`block_size`ใงๅฎš็พฉใ•ใ‚ŒใŸ็Ÿญใ„ใƒใƒฃใƒณใ‚ฏใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๆœ€ๅคงๅ…ฅๅŠ›้•ทใ‚ˆใ‚Š็Ÿญใใ€GPU RAM ใซๅๅˆ†ใช้•ทใ•ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... result["labels"] = result["input_ids"].copy() +... return result +``` + +Apply the `group_texts` function over the entire dataset: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +ๆฌกใซใ€[`DataCollatโ€‹โ€‹orForLanguageModeling`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ*ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆ–‡ใ‚’ๆœ€้•ทใฎ้•ทใ•ใซใ—ใพใ™ใ€‚ + + + + +ใ‚ทใƒผใ‚ฑใƒณใ‚น็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒˆใƒผใ‚ฏใƒณใจใ—ใฆไฝฟ็”จใ—ใ€`mlm=False` ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅ…ฅๅŠ›ใ‚’ 1 ่ฆ็ด ๅˆ†ๅณใซใ‚ทใƒ•ใƒˆใ—ใŸใƒฉใƒ™ใƒซใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) +``` + + + +ใ‚ทใƒผใ‚ฑใƒณใ‚น็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒˆใƒผใ‚ฏใƒณใจใ—ใฆไฝฟ็”จใ—ใ€`mlm=False` ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅ…ฅๅŠ›ใ‚’ 1 ่ฆ็ด ๅˆ†ๅณใซใ‚ทใƒ•ใƒˆใ—ใŸใƒฉใƒ™ใƒซใจใ—ใฆไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False, return_tensors="tf") +``` + + + + + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ๅŸบๆœฌใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](../training#train-with-pytorch-trainer) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForCausalLM`] ใ‚’ไฝฟ็”จใ—ใฆ DistilGPT2 ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import AutoModelForCausalLM, TrainingArguments, Trainer + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_clm-model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.evaluate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ—ใ€ใใฎ่ค‡้›‘ใ•ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 49.61 +``` + +ๆฌกใซใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ๅŸบๆœฌใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](../training#train-a-tensorflow-model-with-keras) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +ๆฌกใซใ€[`TFAutoModelForCausalLM`] ใ‚’ไฝฟ็”จใ—ใฆ DistilGPT2 ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ [`~transformers.PushToHubCallback`] ใงใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใงๅฎŸ่กŒใงใใพใ™ใ€‚ + + + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_clm-model", +... tokenizer=tokenizer, +... ) +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + + + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + +ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎใ‚ˆใ‚Š่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)ใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’่€ƒใˆๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> prompt = "Somatic hypermutation allows the immune system to" +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆ็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆธกใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> generator = pipeline("text-generation", model="my_awesome_eli5_clm-model") +>>> generator(prompt) +[{'generated_text': "Somatic hypermutation allows the immune system to be able to effectively reverse the damage caused by an infection.\n\n\nThe damage caused by an infection is caused by the immune system's ability to perform its own self-correcting tasks."}] +``` + + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€ใ€Œinput_idsใ€ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="pt").input_ids +``` + +[`~generation.GenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ +ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) ใƒšใƒผใ‚ธใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained("my_awesome_eli5_clm-model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +["Somatic hypermutation allows the immune system to react to drugs with the ability to adapt to a different environmental situation. In other words, a system of 'hypermutation' can help the immune system to adapt to a different environmental situation or in some cases even a single life. In contrast, researchers at the University of Massachusetts-Boston have found that 'hypermutation' is much stronger in mice than in humans but can be found in humans, and that it's not completely unknown to the immune system. A study on how the immune system"] +``` + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€`input_ids`ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="tf").input_ids +``` + +[`~transformers.generation_tf_utils.TFGenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) ใƒšใƒผใ‚ธใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("my_awesome_eli5_clm-model") +>>> outputs = model.generate(input_ids=inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Somatic hypermutation allows the immune system to detect the presence of other viruses as they become more prevalent. Therefore, researchers have identified a high proportion of human viruses. The proportion of virus-associated viruses in our study increases with age. Therefore, we propose a simple algorithm to detect the presence of these new viruses in our samples as a sign of improved immunity. A first study based on this algorithm, which will be published in Science on Friday, aims to show that this finding could translate into the development of a better vaccine that is more effective for'] +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/masked_language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/masked_language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..29d7b73ae5d026f210de5c9d82401224a4a94bda --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/masked_language_modeling.md @@ -0,0 +1,449 @@ + + +# Masked language modeling + +[[open-in-colab]] + + + +ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฏใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใ€ใƒขใƒ‡ใƒซใฏใƒˆใƒผใ‚ฏใƒณใ‚’ๅŒๆ–นๅ‘ใซๅ‡ฆ็†ใงใใพใ™ใ€‚ใ“ใ‚Œ +ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใŒๅทฆๅณใฎใƒˆใƒผใ‚ฏใƒณใซๅฎŒๅ…จใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฏใ€ๆฌกใฎใ‚ˆใ†ใชใ‚ฟใ‚นใ‚ฏใซๆœ€้ฉใงใ™ใ€‚ +ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ…จไฝ“ใฎๆ–‡่„ˆใ‚’ใ‚ˆใ็†่งฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ BERT ใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒซใฎไธ€ไพ‹ใงใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [ELI5](https://huggingface.co/distilbert/distilroberta-base) ใฎ [r/askscience](https://www.reddit.com/r/askscience/) ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใง [DistilRoBERTa](https://huggingface.co/distilbert/distilroberta-base) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ ://huggingface.co/datasets/eli5) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/fill-mask) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load ELI5 dataset + +ใพใšใ€ELI5 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ r/askscience ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ ๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ“ใ‚Œใง +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒไธŽใˆใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5", split="train_asks[:5000]") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train_asks` ใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> eli5["train"][0] +{'answers': {'a_id': ['c3d1aib', 'c3d4lya'], + 'score': [6, 3], + 'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]}, + 'answers_urls': {'url': []}, + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']}, + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls': {'url': []}} +``` + +ใ“ใ‚Œใฏๅคšใใฎใ“ใจใฎใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€ๅฎŸ้š›ใซ้–ขๅฟƒใŒใ‚ใ‚‹ใฎใฏ`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ ใ‘ใงใ™ใ€‚่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ ใ‚ฟใ‚นใ‚ฏใฎๅ„ชใ‚ŒใŸ็‚นใฏใ€ๆฌกใฎๅ˜่ชžใŒใƒฉใƒ™ใƒซ * ใงใ‚ใ‚‹ใŸใ‚ใ€ใƒฉใƒ™ใƒซ (ๆ•™ๅธซใชใ—ใ‚ฟใ‚นใ‚ฏใจใ‚‚ๅ‘ผใฐใ‚Œใพใ™) ใŒๅฟ…่ฆใชใ„ใ“ใจใงใ™ใ€‚ + +## Preprocess + + + +ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎๅ ดๅˆใ€ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใฏใ€`text`ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซ DistilRoBERTa ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilroberta-base") +``` + +ไธŠใฎไพ‹ใ‹ใ‚‰ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซใ€`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๅฎŸ้š›ใซใฏ`answers`ๅ†…ใซใƒใ‚นใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ +[` flatten`](https://huggingface.co/docs/datasets/process.html#flatten) ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸๆง‹้€ ใ‹ใ‚‰ `text` ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'answers.a_id': ['c3d1aib', 'c3d4lya'], + 'answers.score': [6, 3], + 'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"], + 'answers_urls.url': [], + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'], + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls.url': []} +``` + +`answers`ๆŽฅ้ ญ่พžใง็คบใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ€ๅ„ใ‚ตใƒ–ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๅ€‹ๅˆฅใฎๅˆ—ใซใชใ‚Šใ€`text`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏใƒชใ‚นใƒˆใซใชใ‚Šใพใ—ใŸใ€‚ใใฎไปฃใ‚ใ‚Š +ๅ„ๆ–‡ใ‚’ๅ€‹ๅˆฅใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๅ ดๅˆใฏใ€ใƒชใ‚นใƒˆใ‚’ๆ–‡ๅญ—ๅˆ—ใซๅค‰ๆ›ใ—ใฆใ€ใใ‚Œใ‚‰ใ‚’ใพใจใ‚ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ๅ„ไพ‹ใฎๆ–‡ๅญ—ๅˆ—ใฎใƒชใ‚นใƒˆใ‚’็ตๅˆใ—ใ€็ตๆžœใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๆœ€ๅˆใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใงใ™ใ€‚ + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +ใ“ใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใซใฏใ€`batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ—ใ€`num_proc` ใงใƒ—ใƒญใ‚ปใ‚นใฎๆ•ฐใ‚’ๅข—ใ‚„ใ—ใพใ™ใ€‚ไธ่ฆใชๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใƒˆใƒผใ‚ฏใƒณ ใ‚ทใƒผใ‚ฑใƒณใ‚นใŒๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใใฎไธ€้ƒจใฏใƒขใƒ‡ใƒซใฎๆœ€ๅคงๅ…ฅๅŠ›้•ทใ‚ˆใ‚Šใ‚‚้•ทใใชใ‚Šใพใ™ใ€‚ + +2 ็•ช็›ฎใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ +- ใ™ในใฆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’้€ฃ็ตใ—ใพใ™ +- ้€ฃ็ตใ•ใ‚ŒใŸใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’`block_size`ใงๅฎš็พฉใ•ใ‚ŒใŸ็Ÿญใ„ใƒใƒฃใƒณใ‚ฏใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๆœ€ๅคงๅ…ฅๅŠ›้•ทใ‚ˆใ‚Š็Ÿญใใ€GPU RAM ใซๅๅˆ†ใช้•ทใ•ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... return result +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ`group_texts`้–ขๆ•ฐใ‚’้ฉ็”จใ—ใพใ™ใ€‚ + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +ๆฌกใซใ€[`DataCollatโ€‹โ€‹orForLanguageModeling`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใฎ้•ทใ•ใพใงๆ–‡ใ‚’ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ* ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ + + + + +ใ‚ทใƒผใ‚ฑใƒณใ‚น็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒˆใƒผใ‚ฏใƒณใจใ—ใฆไฝฟ็”จใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’ๅๅพฉใ™ใ‚‹ใŸใณใซใƒฉใƒณใƒ€ใƒ ใซใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ™ใ‚‹ใŸใ‚ใซ `mlm_probability` ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15) +``` + + + +ใ‚ทใƒผใ‚ฑใƒณใ‚น็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ ใƒˆใƒผใ‚ฏใƒณใจใ—ใฆไฝฟ็”จใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’ๅๅพฉใ™ใ‚‹ใŸใณใซใƒฉใƒณใƒ€ใƒ ใซใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ™ใ‚‹ใŸใ‚ใซ `mlm_probability` ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15, return_tensors="tf") +``` + + + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForMaskedLM`] ใ‚’ไฝฟ็”จใ—ใฆ DistilRoBERTa ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_mlm_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.evaluate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ—ใ€ใใฎ่ค‡้›‘ใ•ใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 8.76 +``` + +ๆฌกใซใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +ๆฌกใซใ€[`TFAutoModelForMaskedLM`] ใ‚’ไฝฟ็”จใ—ใฆ DistilRoBERTa ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +This can be done by specifying where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_mlm_model", +... tokenizer=tokenizer, +... ) +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + + + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + +ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎใ‚ˆใ‚Š่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)ใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ใƒขใƒ‡ใƒซใซ็ฉบ็™ฝใ‚’ๅŸ‹ใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒใˆๅ‡บใ—ใ€็‰นๅˆฅใช `` ใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆ็ฉบ็™ฝใ‚’็คบใ—ใพใ™ใ€‚ + +```py +>>> text = "The Milky Way is a galaxy." +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚ฃใƒซใƒžใ‚นใ‚ฏใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`top_k`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใฆใ€่ฟ”ใ™ไบˆๆธฌใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> mask_filler = pipeline("fill-mask", "stevhliu/my_awesome_eli5_mlm_model") +>>> mask_filler(text, top_k=3) +[{'score': 0.5150994658470154, + 'token': 21300, + 'token_str': ' spiral', + 'sequence': 'The Milky Way is a spiral galaxy.'}, + {'score': 0.07087188959121704, + 'token': 2232, + 'token_str': ' massive', + 'sequence': 'The Milky Way is a massive galaxy.'}, + {'score': 0.06434620916843414, + 'token': 650, + 'token_str': ' small', + 'sequence': 'The Milky Way is a small galaxy.'}] +``` + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€`input_ids`ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ `` ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚‚ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="pt") +>>> mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใฎ`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +ๆฌกใซใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ 3 ใคใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใง่ฟ”ใ—ใ€ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ + +```py +>>> top_3_tokens = torch.topk(mask_token_logits, 3, dim=1).indices[0].tolist() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€`input_ids`ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ `` ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚‚ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="tf") +>>> mask_token_index = tf.where(inputs["input_ids"] == tokenizer.mask_token_id)[0, 1] +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใฎ`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +ๆฌกใซใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ 3 ใคใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใง่ฟ”ใ—ใ€ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ + + +```py +>>> top_3_tokens = tf.math.top_k(mask_token_logits, 3).indices.numpy() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/monocular_depth_estimation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/monocular_depth_estimation.md new file mode 100644 index 0000000000000000000000000000000000000000..e7a3a994a60ebc3ee752cfd0461bb7436c7b8406 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/monocular_depth_estimation.md @@ -0,0 +1,149 @@ + + +# Monocular depth estimation + +ๅ˜็œผๅฅฅ่กŒใๆŽจๅฎšใฏใ€ใ‚ทใƒผใƒณใฎๅฅฅ่กŒใๆƒ…ๅ ฑใ‚’็”ปๅƒใ‹ใ‚‰ไบˆๆธฌใ™ใ‚‹ใ“ใจใ‚’ๅซใ‚€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ๅ˜ไธ€ใฎ็”ปๅƒใ€‚่จ€ใ„ๆ›ใˆใ‚Œใฐใ€ใ‚ทใƒผใƒณๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ่ท้›ขใ‚’่ท้›ขใ‹ใ‚‰ๆŽจๅฎšใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใงใ™ใ€‚ +ๅ˜ไธ€ใ‚ซใƒกใƒฉใฎ่ฆ–็‚นใ€‚ + +ๅ˜็œผๅฅฅ่กŒใๆŽจๅฎšใซใฏใ€3D ๅ†ๆง‹็ฏ‰ใ€ๆ‹กๅผต็พๅฎŸใ€่‡ชๅ‹•้‹่ปขใ€ +ใใ—ใฆใƒญใƒœใƒƒใƒˆๅทฅๅญฆใ€‚ใƒขใƒ‡ใƒซใŒใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ้–“ใฎ่ค‡้›‘ใช้–ขไฟ‚ใ‚’็†่งฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใฏๅ›ฐ้›ฃใชไฝœๆฅญใงใ™ใ€‚ +ใ‚ทใƒผใƒณใจใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ๆทฑๅบฆๆƒ…ๅ ฑ๏ผˆ็…งๆ˜Žๆกไปถใชใฉใฎ่ฆๅ› ใฎๅฝฑ้Ÿฟใ‚’ๅ—ใ‘ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™๏ผ‰ +ใ‚ชใ‚ฏใƒซใƒผใ‚ธใƒงใƒณใจใƒ†ใ‚ฏใ‚นใƒใƒฃใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/depth-estimation) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆใณใพใ™ใ€‚ + +* ๆทฑๅบฆๆŽจๅฎšใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝœๆˆใ™ใ‚‹ +* ๆ‰‹ๅ‹•ใงๆทฑๅบฆๆŽจๅฎšๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใพใ™ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q transformers +``` + +## Depth estimation pipeline + +ๆทฑๅบฆๆŽจๅฎšใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใงๆŽจ่ซ–ใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ๅฏพๅฟœใ™ใ‚‹ [`pipeline`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?pipeline_tag=Depth-estimation&sort=downloads) ใ‹ใ‚‰ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> checkpoint = "vinvino02/glpn-nyu" +>>> depth_estimator = pipeline("depth-estimation", model=checkpoint) +``` + +ๆฌกใซใ€ๅˆ†ๆžใ™ใ‚‹็”ปๅƒใ‚’้ธๆŠžใ—ใพใ™ใ€‚ + +```py +>>> from PIL import Image +>>> import requests + +>>> url = "https://unsplash.com/photos/HwBAsSbPBDU/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzR8fGNhciUyMGluJTIwdGhlJTIwc3RyZWV0fGVufDB8MHx8fDE2Nzg5MDEwODg&force=true&w=640" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> image +``` + +
+ Photo of a busy street +
+ +็”ปๅƒใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> predictions = depth_estimator(image) +``` + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏ 2 ใคใฎใ‚จใƒณใƒˆใƒชใ‚’ๅซใ‚€่พžๆ›ธใ‚’่ฟ”ใ—ใพใ™ใ€‚ๆœ€ๅˆใฎใ‚‚ใฎใฏ`predicted_ Depth`ใจๅ‘ผใฐใ‚Œใ€ๆฌกใฎๅ€คใ‚’ๆŒใคใƒ†ใƒณใ‚ฝใƒซใงใ™ใ€‚ +ๆทฑใ•ใฏๅ„ใƒ”ใ‚ฏใ‚ปใƒซใฎใƒกใƒผใƒˆใƒซๅ˜ไฝใง่กจใ•ใ‚Œใพใ™ใ€‚ +2 ็•ช็›ฎใฎ`depth`ใฏใ€ๆทฑๅบฆๆŽจๅฎš็ตๆžœใ‚’่ฆ–่ฆšๅŒ–ใ™ใ‚‹ PIL ็”ปๅƒใงใ™ใ€‚ + +่ฆ–่ฆšๅŒ–ใ•ใ‚ŒใŸ็ตๆžœใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> predictions["depth"] +``` + +
+ Depth estimation visualization +
+ +## Depth estimation inference by hand + +ๆทฑๅบฆๆŽจๅฎšใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎไฝฟ็”จๆ–นๆณ•ใ‚’็†่งฃใ—ใŸใฎใงใ€ๅŒใ˜็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +ใพใšใ€[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?pipeline_tag=Depth-estimation&sort=downloads) ใ‹ใ‚‰ใƒขใƒ‡ใƒซใจ้–ข้€ฃใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€ๅ‰ใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation + +>>> checkpoint = "vinvino02/glpn-nyu" + +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +>>> model = AutoModelForDepthEstimation.from_pretrained(checkpoint) +``` + +ๅฟ…่ฆใช็”ปๅƒๅค‰ๆ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹`image_processor`ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒๅ…ฅๅŠ›ใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ +ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใ‚„ๆญฃ่ฆๅŒ–ใชใฉ: + +```py +>>> pixel_values = image_processor(image, return_tensors="pt").pixel_values +``` + +ๆบ–ๅ‚™ใ•ใ‚ŒใŸๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> import torch + +>>> with torch.no_grad(): +... outputs = model(pixel_values) +... predicted_depth = outputs.predicted_depth +``` + +็ตๆžœใ‚’่ฆ–่ฆšๅŒ–ใ—ใพใ™ใ€‚ + + +```py +>>> import numpy as np + +>>> # interpolate to original size +>>> prediction = torch.nn.functional.interpolate( +... predicted_depth.unsqueeze(1), +... size=image.size[::-1], +... mode="bicubic", +... align_corners=False, +... ).squeeze() +>>> output = prediction.numpy() + +>>> formatted = (output * 255 / np.max(output)).astype("uint8") +>>> depth = Image.fromarray(formatted) +>>> depth +``` + +
+ Depth estimation visualization +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/multiple_choice.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/multiple_choice.md new file mode 100644 index 0000000000000000000000000000000000000000..ab4f2329f3b995974577afc1684ebe0249aa9a70 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/multiple_choice.md @@ -0,0 +1,374 @@ + + +# Multiple choice + +[[open-in-colab]] + +ๅคš่‚ข้ธๆŠžใ‚ฟใ‚นใ‚ฏใฏ่ณชๅ•ๅฟœ็ญ”ใซไผผใฆใ„ใพใ™ใŒใ€ใ„ใใคใ‹ใฎๅ€™่ฃœใฎๅ›ž็ญ”ใŒใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใจใจใ‚‚ใซๆไพ›ใ•ใ‚Œใ€ๆญฃใ—ใ„ๅ›ž็ญ”ใ‚’้ธๆŠžใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚‹็‚นใŒ็•ฐใชใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [SWAG](https://huggingface.co/datasets/swag) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ€Œ้€šๅธธใ€ๆง‹ๆˆใง [BERT](https://huggingface.co/google-bert/bert-base-uncased) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€ๆœ€้ฉใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’้ธๆŠžใ—ใพใ™่ค‡ๆ•ฐใฎ้ธๆŠž่‚ขใจไฝ•ใ‚‰ใ‹ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒๆ…ฎใ—ใฆๅ›ž็ญ”ใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load SWAG dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ SWAG ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ€Œ้€šๅธธใ€ๆง‹ๆˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> swag = load_dataset("swag", "regular") +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> swag["train"][0] +{'ending0': 'passes by walking down the street playing their instruments.', + 'ending1': 'has heard approaching them.', + 'ending2': "arrives and they're outside dancing and asleep.", + 'ending3': 'turns the lead singer watches the performance.', + 'fold-ind': '3416', + 'gold-source': 'gold', + 'label': 0, + 'sent1': 'Members of the procession walk down the street holding small horn brass instruments.', + 'sent2': 'A drum line', + 'startphrase': 'Members of the procession walk down the street holding small horn brass instruments. A drum line', + 'video-id': 'anetv_jkn6uvmqwh4'} +``` + +ใ“ใ“ใซใฏใŸใใ•ใ‚“ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚‹ใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใŒใ€ๅฎŸ้š›ใฏ้žๅธธใซ็ฐกๅ˜ใงใ™ใ€‚ + +- `sent1` ใจ `sent2`: ใ“ใ‚Œใ‚‰ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฏๆ–‡ใฎๅง‹ใพใ‚Šใ‚’็คบใ—ใ€ใ“ใฎ 2 ใคใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใจ `startphrase` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ +- `ending`: ๆ–‡ใฎ็ต‚ใ‚ใ‚Šๆ–นใจใ—ใฆ่€ƒใˆใ‚‰ใ‚Œใ‚‹็ต‚ใ‚ใ‚Šๆ–นใ‚’็คบๅ”†ใ—ใพใ™ใŒใ€ๆญฃใ—ใ„ใฎใฏ 1 ใคใ ใ‘ใงใ™ใ€‚ +- `label`: ๆญฃใ—ใ„ๆ–‡ใฎ็ต‚ใ‚ใ‚Šใ‚’่ญ˜ๅˆฅใ—ใพใ™ใ€‚ + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€BERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ๆ–‡ใฎๅง‹ใพใ‚Šใจ 4 ใคใฎๅฏ่ƒฝใช็ต‚ใ‚ใ‚Šใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") +``` + +ไฝœๆˆใ™ใ‚‹ๅ‰ๅ‡ฆ็†้–ขๆ•ฐใฏๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. `sent1` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎใ‚ณใƒ”ใƒผใ‚’ 4 ใคไฝœๆˆใ—ใ€ใใ‚Œใžใ‚Œใ‚’ `sent2` ใจ็ต„ใฟๅˆใ‚ใ›ใฆๆ–‡ใฎๅง‹ใพใ‚Šใ‚’ๅ†็พใ—ใพใ™ใ€‚ +2. `sent2` ใ‚’ 4 ใคใฎๅฏ่ƒฝใชๆ–‡ๆœซๅฐพใฎใใ‚Œใžใ‚Œใจ็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ +3. ใ“ใ‚Œใ‚‰ 2 ใคใฎใƒชใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใƒ•ใƒฉใƒƒใƒˆๅŒ–ใ—ใ€ใใฎๅพŒใ€ๅ„ไพ‹ใซๅฏพๅฟœใ™ใ‚‹ `input_ids`ใ€`attention_mask`ใ€ใŠใ‚ˆใณ `labels` ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซ้žใƒ•ใƒฉใƒƒใƒˆๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> ending_names = ["ending0", "ending1", "ending2", "ending3"] + + +>>> def preprocess_function(examples): +... first_sentences = [[context] * 4 for context in examples["sent1"]] +... question_headers = examples["sent2"] +... second_sentences = [ +... [f"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(question_headers) +... ] + +... first_sentences = sum(first_sentences, []) +... second_sentences = sum(second_sentences, []) + +... tokenized_examples = tokenizer(first_sentences, second_sentences, truncation=True) +... return {k: [v[i : i + 4] for i in range(0, len(v), 4)] for k, v in tokenized_examples.items()} +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ + + +```py +tokenized_swag = swag.map(preprocess_function, batched=True) +``` + +[`DataCollatorForMultipleChoice`] ใฏใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซๅ…ฅๅŠ›ใ‚’ๅนณๅฆๅŒ–ใ—ใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’้ฉ็”จใ—ใฆใ€็ตๆžœใ‚’้žๅนณๅฆๅŒ–ใ—ใพใ™ใ€‚ +```py +>>> from transformers import DataCollatorForMultipleChoice +>>> collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +``` + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ) ใƒกใƒˆใƒชใ‚ฏใ‚นใฎ่ชญใฟ่พผใฟใจ่จˆ็ฎ—ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚ + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForMultipleChoice`] ใ‚’ไฝฟ็”จใ—ใฆ BERT ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForMultipleChoice, TrainingArguments, Trainer + +>>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ็ฒพๅบฆใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_swag_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... load_best_model_at_end=True, +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_swag["train"], +... eval_dataset=tokenized_swag["validation"], +... processing_class=tokenizer, +... data_collator=collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใพใ™ใ‚ˆใ†ใซใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_train_epochs = 2 +>>> total_train_steps = (len(tokenized_swag["train"]) // batch_size) * num_train_epochs +>>> optimizer, schedule = create_optimizer(init_lr=5e-5, num_warmup_steps=0, num_train_steps=total_train_steps) +``` + +ๆฌกใซใ€[`TFAutoModelForMultipleChoice`] ใ‚’ไฝฟ็”จใ—ใฆ BERT ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForMultipleChoice + +>>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-uncased") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> data_collator = DataCollatorForMultipleChoice(tokenizer=tokenizer) +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_swag["train"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_swag["validation"], +... shuffle=False, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๆœ€ๅพŒใฎ 2 ใคใฎใ“ใจใฏใ€ไบˆๆธฌใ‹ใ‚‰็ฒพๅบฆใ‚’่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใจใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใ‚‚ [Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +`compute_metrics` ้–ขๆ•ฐใ‚’ [`~transformers.KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +[`~transformers.PushToHubCallback`] ใงใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_model", +... tokenizer=tokenizer, +... ) +``` + +ๆฌกใซใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ใพใจใ‚ใฆใƒใƒณใƒ‰ใƒซใ—ใพใ™ใ€‚ + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=2, callbacks=callbacks) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + + +่ค‡ๆ•ฐ้ธๆŠž็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)ใ€‚ + + + + +# Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ใ„ใใคใ‹ใฎใƒ†ใ‚ญใ‚นใƒˆใจ 2 ใคใฎๅ›ž็ญ”ๅ€™่ฃœใ‚’่€ƒใˆใฆใใ ใ•ใ„ใ€‚ + +```py +>>> prompt = "France has a bread law, Le Dรฉcret Pain, with strict rules on what is allowed in a traditional baguette." +>>> candidate1 = "The law does not apply to croissants and brioche." +>>> candidate2 = "The law applies to baguettes." +``` + + + + +ๅ„ใƒ—ใƒญใƒณใƒ—ใƒˆใจๅ›ž็ญ”ๅ€™่ฃœใฎใƒšใ‚ขใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€PyTorch ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ„ใใคใ‹ใฎ`lables`ใ‚‚ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_swag_model") +>>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="pt", padding=True) +>>> labels = torch.tensor(0).unsqueeze(0) +``` + +ๅ…ฅๅŠ›ใจใƒฉใƒ™ใƒซใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForMultipleChoice + +>>> model = AutoModelForMultipleChoice.from_pretrained("my_awesome_swag_model") +>>> outputs = model(**{k: v.unsqueeze(0) for k, v in inputs.items()}, labels=labels) +>>> logits = outputs.logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> predicted_class = logits.argmax().item() +>>> predicted_class +'0' +``` + + + +ๅ„ใƒ—ใƒญใƒณใƒ—ใƒˆใจๅ›ž็ญ”ๅ€™่ฃœใฎใƒšใ‚ขใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€TensorFlow ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_swag_model") +>>> inputs = tokenizer([[prompt, candidate1], [prompt, candidate2]], return_tensors="tf", padding=True) +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForMultipleChoice + +>>> model = TFAutoModelForMultipleChoice.from_pretrained("my_awesome_swag_model") +>>> inputs = {k: tf.expand_dims(v, 0) for k, v in inputs.items()} +>>> outputs = model(inputs) +>>> logits = outputs.logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> predicted_class = int(tf.math.argmax(logits, axis=-1)[0]) +>>> predicted_class +'0' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/object_detection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/object_detection.md new file mode 100644 index 0000000000000000000000000000000000000000..1b6c03ee1032bdfd6fe08f4992642229c05b6add --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/object_detection.md @@ -0,0 +1,598 @@ + + +# Object detection + +[[open-in-colab]] + +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฏใ€็”ปๅƒๅ†…ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น (ไบบ้–“ใ€ๅปบ็‰ฉใ€่ปŠใชใฉ) ใ‚’ๆคœๅ‡บใ™ใ‚‹ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚็‰ฉไฝ“ๆคœๅ‡บใƒขใƒ‡ใƒซใฏ็”ปๅƒใ‚’ๅ…ฅๅŠ›ใŠใ‚ˆใณๅ‡บๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใพใ™ +ๆคœๅ‡บใ•ใ‚ŒใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใจ้–ข้€ฃใ™ใ‚‹ใƒฉใƒ™ใƒซใฎๅบงๆจ™ใ€‚็”ปๅƒใซใฏ่ค‡ๆ•ฐใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๅซใ‚ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใใ‚Œใžใ‚Œใซ็‹ฌ่‡ชใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใจใƒฉใƒ™ใƒซใŒใ‚ใ‚Š (ไพ‹: ่ปŠใจๅปบ็‰ฉใ‚’ๆŒใคใ“ใจใŒใงใใพใ™)ใ€ๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏ +็”ปๅƒใฎใ•ใพใ–ใพใช้ƒจๅˆ†ใซๅญ˜ๅœจใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ (ใŸใจใˆใฐใ€็”ปๅƒใซใฏ่ค‡ๆ•ฐใฎ่ปŠใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™)ใ€‚ +ใ“ใฎใ‚ฟใ‚นใ‚ฏใฏใ€ๆญฉ่กŒ่€…ใ€้“่ทฏๆจ™่ญ˜ใ€ไฟกๅทๆฉŸใชใฉใ‚’ๆคœๅ‡บใ™ใ‚‹ใŸใ‚ใซ่‡ชๅ‹•้‹่ปขใงไธ€่ˆฌ็š„ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ไป–ใฎใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซใฏใ€็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใ‚ซใ‚ฆใƒณใƒˆใ€็”ปๅƒๆคœ็ดขใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ + + 1. Finetune [DETR](https://huggingface.co/docs/transformers/model_doc/detr)ใ€็•ณใฟ่พผใฟใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’็ต„ใฟๅˆใ‚ใ›ใŸใƒขใƒ‡ใƒซ + [CPPE-5](https://huggingface.co/datasets/cppe-5) ไธŠใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ/ใƒ‡ใ‚ณใƒผใƒ€ใƒผ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ๅ‚™ใˆใŸใƒใƒƒใ‚ฏใƒœใƒผใƒณ + ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ + 2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/object-detection) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +```bash +pip install -q datasets transformers evaluate timm albumentations +``` + +๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆ Hugging Face Hub ใ‹ใ‚‰ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใ€๐Ÿค— ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚’ๅข—ๅผทใ™ใ‚‹ใŸใ‚ใฎ`albumentations`ใ€‚ `timm` ใฏ็พๅœจใ€DETR ใƒขใƒ‡ใƒซใฎ็•ณใฟ่พผใฟใƒใƒƒใ‚ฏใƒœใƒผใƒณใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใงใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€ใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load the CPPE-5 dataset + +[CPPE-5 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/datasets/cppe-5) ใซใฏใ€ๆฌกใฎ็”ปๅƒใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +ๆ–ฐๅž‹ใ‚ณใƒญใƒŠใ‚ฆใ‚คใƒซใ‚นๆ„ŸๆŸ“็—‡ใฎใƒ‘ใƒณใƒ‡ใƒŸใƒƒใ‚ฏใซใŠใ‘ใ‚‹ๅŒป็™‚็”จๅ€‹ไบบไฟ่ญทๅ…ท (PPE) ใ‚’่ญ˜ๅˆฅใ™ใ‚‹ๆณจ้‡ˆใ€‚ + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> cppe5 = load_dataset("cppe-5") +>>> cppe5 +DatasetDict({ + train: Dataset({ + features: ['image_id', 'image', 'width', 'height', 'objects'], + num_rows: 1000 + }) + test: Dataset({ + features: ['image_id', 'image', 'width', 'height', 'objects'], + num_rows: 29 + }) +}) +``` + +ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใ€1000 ๆžšใฎ็”ปๅƒใ‚’ๅซใ‚€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใจ 29 ๆžšใฎ็”ปๅƒใ‚’ๅซใ‚€ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใŒใ™ใงใซไป˜ๅฑžใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +ใƒ‡ใƒผใ‚ฟใซๆ…ฃใ‚Œใ‚‹ใŸใ‚ใซใ€ไพ‹ใŒใฉใฎใ‚ˆใ†ใชใ‚‚ใฎใ‹ใ‚’่ชฟในใฆใใ ใ•ใ„ใ€‚ + +```py +>>> cppe5["train"][0] +{'image_id': 15, + 'image': , + 'width': 943, + 'height': 663, + 'objects': {'id': [114, 115, 116, 117], + 'area': [3796, 1596, 152768, 81002], + 'bbox': [[302.0, 109.0, 73.0, 52.0], + [810.0, 100.0, 57.0, 28.0], + [160.0, 31.0, 248.0, 616.0], + [741.0, 68.0, 202.0, 401.0]], + 'category': [4, 4, 0, 0]}} +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎไพ‹ใซใฏๆฌกใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ +- `image_id`: ใ‚ตใƒณใƒ—ใƒซใฎ็”ปๅƒID +- `image`: ็”ปๅƒใ‚’ๅซใ‚€ `PIL.Image.Image` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ +- `width`: ็”ปๅƒใฎๅน… +- `height`: ็”ปๅƒใฎ้ซ˜ใ• +- `objects`: ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎใƒกใ‚ฟใƒ‡ใƒผใ‚ฟใ‚’ๅซใ‚€่พžๆ›ธ: + - `id`: ใ‚ขใƒŽใƒ†ใƒผใ‚ทใƒงใƒณID + - `area`: ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฎ้ ˜ๅŸŸ + - `bbox`: ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚น ([COCO ๅฝขๅผ](https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/#coco) ) + - `category`: ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใ‚ซใƒ†ใ‚ดใƒชใƒผใ€‚ๅฏ่ƒฝใชๅ€คใซใฏใ€`Coverall (0)`ใ€`Face_Shield (1)`ใ€`Gloves (2)`ใ€`Goggles (3)`ใ€ใŠใ‚ˆใณ `Mask (4)` ใŒๅซใพใ‚Œใพใ™ใ€‚ + +`bbox`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒ COCO ๅฝขๅผใซๅพ“ใฃใฆใ„ใ‚‹ใ“ใจใซๆฐ—ใฅใใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏ DETR ใƒขใƒ‡ใƒซใŒไบˆๆœŸใ™ใ‚‹ๅฝขๅผใงใ™ใ€‚ +ใŸใ ใ—ใ€ใ€Œใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ€ๅ†…ใฎใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎใ‚ฐใƒซใƒผใƒ—ๅŒ–ใฏใ€DETR ใŒๅฟ…่ฆใจใ™ใ‚‹ๆณจ้‡ˆๅฝขๅผใจใฏ็•ฐใชใ‚Šใพใ™ใ€‚ใ‚ใชใŸใฏใ™ใ‚‹ใงใ‚ใ‚ใ† +ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ใ„ใใคใ‹ใฎๅ‰ๅ‡ฆ็†ๅค‰ๆ›ใ‚’้ฉ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใƒ‡ใƒผใ‚ฟใ‚’ใ•ใ‚‰ใซๆทฑใ็†่งฃใ™ใ‚‹ใซใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎไพ‹ใ‚’่ฆ–่ฆšๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np +>>> import os +>>> from PIL import Image, ImageDraw + +>>> image = cppe5["train"][0]["image"] +>>> annotations = cppe5["train"][0]["objects"] +>>> draw = ImageDraw.Draw(image) + +>>> categories = cppe5["train"].features["objects"].feature["category"].names + +>>> id2label = {index: x for index, x in enumerate(categories, start=0)} +>>> label2id = {v: k for k, v in id2label.items()} + +>>> for i in range(len(annotations["id"])): +... box = annotations["bbox"][i] +... class_idx = annotations["category"][i] +... x, y, w, h = tuple(box) +... draw.rectangle((x, y, x + w, y + h), outline="red", width=1) +... draw.text((x, y), id2label[class_idx], fill="white") + +>>> image +``` + +
+ CPPE-5 Image Example +
+ +้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸใƒฉใƒ™ใƒซใ‚’ไฝฟ็”จใ—ใฆๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’่ฆ–่ฆšๅŒ–ใ™ใ‚‹ใซใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒกใ‚ฟใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ใƒฉใƒ™ใƒซใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ +`category`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ€‚ +ใพใŸใ€ใƒฉใƒ™ใƒซ ID ใ‚’ใƒฉใƒ™ใƒซ ใ‚ฏใƒฉใ‚นใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ™ใ‚‹่พžๆ›ธ (`id2label`) ใ‚„ใใฎ้€† (`label2id`) ใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฏใ€ๅพŒใงใƒขใƒ‡ใƒซใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซไฝฟ็”จใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒžใƒƒใƒ—ใ‚’ๅซใ‚ใ‚‹ใจใ€ๅ…ฑๆœ‰ใ—ใŸๅ ดๅˆใซไป–ใฎไบบใŒใƒขใƒ‡ใƒซใ‚’ๅ†ๅˆฉ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใƒใƒ–ใซๅ–ใ‚Šไป˜ใ‘ใพใ™ใ€‚ + +ใƒ‡ใƒผใ‚ฟใซๆ…ฃใ‚Œใ‚‹ใŸใ‚ใฎๆœ€ๅพŒใฎใ‚นใƒ†ใƒƒใƒ—ใจใ—ใฆใ€ๆฝœๅœจ็š„ใชๅ•้กŒใŒใชใ„ใ‹ใƒ‡ใƒผใ‚ฟใ‚’่ชฟๆŸปใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซ้–ขใ™ใ‚‹ไธ€่ˆฌ็š„ใชๅ•้กŒใฎ 1 ใคใฏใ€ +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฏใ€็”ปๅƒใฎ็ซฏใ‚’่ถŠใˆใฆใ€Œไผธใณใ‚‹ใ€ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใงใ™ใ€‚ใ“ใฎใ‚ˆใ†ใชใ€Œๆšด่ตฐใ€ๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใฏใ€ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ใŸใ‚ใ€ใ“ใฎๆฎต้šŽใงๅฏพๅ‡ฆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใ€ใ“ใฎๅ•้กŒใซ้–ขใ™ใ‚‹ไพ‹ใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏๅ†…ๅฎนใ‚’ใ‚ใ‹ใ‚Šใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซใ€ใ“ใ‚Œใ‚‰ใฎ็”ปๅƒใ‚’ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> remove_idx = [590, 821, 822, 875, 876, 878, 879] +>>> keep = [i for i in range(len(cppe5["train"])) if i not in remove_idx] +>>> cppe5["train"] = cppe5["train"].select(keep) +``` + +## Preprocess the data + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใซไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ขใƒ—ใƒญใƒผใƒใจๆญฃ็ขบใซไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซใ€ไฝฟ็”จใ™ใ‚‹ไบˆๅฎšใฎใƒ‡ใƒผใ‚ฟใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[`AutoImageProcessor`] ใฏใ€็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ๅ‡ฆ็†ใ—ใฆ `pixel_values`ใ€`pixel_mask`ใ€ใŠใ‚ˆใณ +DETR ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ€Œใƒฉใƒ™ใƒซใ€ใ€‚็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใซใฏใ€ๅฟƒ้…ใ™ใ‚‹ๅฟ…่ฆใฎใชใ„ใ„ใใคใ‹ใฎๅฑžๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `image_mean = [0.485, 0.456, 0.406 ]` +- `image_std = [0.229, 0.224, 0.225]` + +ใ“ใ‚Œใ‚‰ใฏใ€ใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ็”ปๅƒใ‚’ๆญฃ่ฆๅŒ–ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅนณๅ‡ใจๆจ™ๆบ–ๅๅทฎใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎไพกๅ€ค่ฆณใฏ้žๅธธใซ้‡่ฆใงใ™ +ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ็”ปๅƒใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใพใŸใฏๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใใซ่ค‡่ฃฝใ—ใพใ™ใ€‚ + +ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใƒขใƒ‡ใƒซใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใ‚คใƒกใƒผใ‚ธ ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "facebook/detr-resnet-50" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +``` + +็”ปๅƒใ‚’`image_processor`ใซๆธกใ™ๅ‰ใซใ€2 ใคใฎๅ‰ๅ‡ฆ็†ๅค‰ๆ›ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซ้ฉ็”จใ—ใพใ™ใ€‚ +- ็”ปๅƒใฎๆ‹กๅผต +- DETR ใฎๆœŸๅพ…ใซๅฟœใˆใ‚‹ใŸใ‚ใฎๆณจ้‡ˆใฎๅ†ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆ + +ใพใšใ€ใƒขใƒ‡ใƒซใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใซใ‚ชใƒผใƒใƒผใƒ•ใ‚ฃใƒƒใƒˆใ—ใชใ„ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซใ€ไปปๆ„ใฎใƒ‡ใƒผใ‚ฟๆ‹กๅผตใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒๆ‹กๅผตใ‚’้ฉ็”จใงใใพใ™ใ€‚ใ“ใ“ใงใฏ[Albumentations](https://albumentations.ai/docs/)ใ‚’ไฝฟ็”จใ—ใพใ™... +ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฏใ€ๅค‰ๆ›ใŒ็”ปๅƒใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ€ใใ‚Œใซๅฟœใ˜ใฆๅขƒ็•Œใƒœใƒƒใ‚ฏใ‚นใ‚’ๆ›ดๆ–ฐใ™ใ‚‹ใ“ใจใ‚’ไฟ่จผใ—ใพใ™ใ€‚ +๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใซใฏใ€่ฉณ็ดฐใช [็‰ฉไฝ“ๆคœๅ‡บ็”จใซ็”ปๅƒใ‚’ๆ‹กๅผตใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใ‚ฌใ‚คใƒ‰](https://huggingface.co/docs/datasets/object_detection) ใŒ่จ˜่ผ‰ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ไพ‹ใจใ—ใฆใพใฃใŸใๅŒใ˜ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ“ใ“ใงใ‚‚ๅŒใ˜ใ‚ขใƒ—ใƒญใƒผใƒใ‚’้ฉ็”จใ—ใ€ๅ„็”ปๅƒใฎใ‚ตใ‚คใ‚บใ‚’ (480, 480) ใซๅค‰ๆ›ดใ—ใพใ™ใ€‚ +ๆฐดๅนณใซๅ่ปขใ—ใฆๆ˜Žใ‚‹ใใ—ใพใ™ใ€‚ + +```py +>>> import albumentations +>>> import numpy as np +>>> import torch + +>>> transform = albumentations.Compose( +... [ +... albumentations.Resize(480, 480), +... albumentations.HorizontalFlip(p=1.0), +... albumentations.RandomBrightnessContrast(p=1.0), +... ], +... bbox_params=albumentations.BboxParams(format="coco", label_fields=["category"]), +... ) +``` + +`image_processor` ใฏใ€ๆณจ้‡ˆใŒๆฌกใฎๅฝขๅผใงใ‚ใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใพใ™: `{'image_id': int, 'annotations': list[Dict]}`, + ใ“ใ“ใงใ€ๅ„่พžๆ›ธใฏ COCO ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๆณจ้‡ˆใงใ™ใ€‚ 1 ใคใฎไพ‹ใจใ—ใฆใ€ๆณจ้‡ˆใ‚’ๅ†ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ™ใ‚‹้–ขๆ•ฐใ‚’่ฟฝๅŠ ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + ```py +>>> def formatted_anns(image_id, category, area, bbox): +... annotations = [] +... for i in range(0, len(category)): +... new_ann = { +... "image_id": image_id, +... "category_id": category[i], +... "isCrowd": 0, +... "area": area[i], +... "bbox": list(bbox[i]), +... } +... annotations.append(new_ann) + +... return annotations +``` + +ใ“ใ‚Œใงใ€็”ปๅƒใจๆณจ้‡ˆใฎๅค‰ๆ›ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใงไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```py +>>> # transforming a batch +>>> def transform_aug_ann(examples): +... image_ids = examples["image_id"] +... images, bboxes, area, categories = [], [], [], [] +... for image, objects in zip(examples["image"], examples["objects"]): +... image = np.array(image.convert("RGB"))[:, :, ::-1] +... out = transform(image=image, bboxes=objects["bbox"], category=objects["category"]) + +... area.append(objects["area"]) +... images.append(out["image"]) +... bboxes.append(out["bboxes"]) +... categories.append(out["category"]) + +... targets = [ +... {"image_id": id_, "annotations": formatted_anns(id_, cat_, ar_, box_)} +... for id_, cat_, ar_, box_ in zip(image_ids, categories, area, bboxes) +... ] + +... return image_processor(images=images, annotations=targets, return_tensors="pt") +``` + +๐Ÿค— Datasets [`~datasets.Dataset.with_transform`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ“ใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ฉ็”จใ—ใพใ™ใ€‚ใ“ใฎๆ–นๆณ•ใŒ้ฉ็”จใ•ใ‚Œใ‚‹ใฎใฏใ€ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ฆ็ด ใ‚’่ชญใฟ่พผใ‚€ใจใใซใ€ใใฎๅ ดใงๅค‰ๆ›ใ—ใพใ™ใ€‚ + +ใ“ใฎๆ™‚็‚นใงใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎไพ‹ใŒๅค‰ๆ›ๅพŒใซใฉใฎใ‚ˆใ†ใซใชใ‚‹ใ‹ใ‚’็ขบ่ชใงใใพใ™ใ€‚ใƒ†ใƒณใ‚ฝใƒซใŒ่กจ็คบใ•ใ‚Œใ‚‹ใฏใšใงใ™ +`pixel_values`ใ€ใƒ†ใƒณใ‚ฝใƒซใจ `pixel_mask`ใ€ใŠใ‚ˆใณ `labels` ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> cppe5["train"] = cppe5["train"].with_transform(transform_aug_ann) +>>> cppe5["train"][15] +{'pixel_values': tensor([[[ 0.9132, 0.9132, 0.9132, ..., -1.9809, -1.9809, -1.9809], + [ 0.9132, 0.9132, 0.9132, ..., -1.9809, -1.9809, -1.9809], + [ 0.9132, 0.9132, 0.9132, ..., -1.9638, -1.9638, -1.9638], + ..., + [-1.5699, -1.5699, -1.5699, ..., -1.9980, -1.9980, -1.9980], + [-1.5528, -1.5528, -1.5528, ..., -1.9980, -1.9809, -1.9809], + [-1.5528, -1.5528, -1.5528, ..., -1.9980, -1.9809, -1.9809]], + + [[ 1.3081, 1.3081, 1.3081, ..., -1.8431, -1.8431, -1.8431], + [ 1.3081, 1.3081, 1.3081, ..., -1.8431, -1.8431, -1.8431], + [ 1.3081, 1.3081, 1.3081, ..., -1.8256, -1.8256, -1.8256], + ..., + [-1.3179, -1.3179, -1.3179, ..., -1.8606, -1.8606, -1.8606], + [-1.3004, -1.3004, -1.3004, ..., -1.8606, -1.8431, -1.8431], + [-1.3004, -1.3004, -1.3004, ..., -1.8606, -1.8431, -1.8431]], + + [[ 1.4200, 1.4200, 1.4200, ..., -1.6476, -1.6476, -1.6476], + [ 1.4200, 1.4200, 1.4200, ..., -1.6476, -1.6476, -1.6476], + [ 1.4200, 1.4200, 1.4200, ..., -1.6302, -1.6302, -1.6302], + ..., + [-1.0201, -1.0201, -1.0201, ..., -1.5604, -1.5604, -1.5604], + [-1.0027, -1.0027, -1.0027, ..., -1.5604, -1.5430, -1.5430], + [-1.0027, -1.0027, -1.0027, ..., -1.5604, -1.5430, -1.5430]]]), + 'pixel_mask': tensor([[1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 1, 1, 1], + ..., + [1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 1, 1, 1]]), + 'labels': {'size': tensor([800, 800]), 'image_id': tensor([756]), 'class_labels': tensor([4]), 'boxes': tensor([[0.7340, 0.6986, 0.3414, 0.5944]]), 'area': tensor([519544.4375]), 'iscrowd': tensor([0]), 'orig_size': tensor([480, 480])}} +``` + +ๅ€‹ใ€…ใฎ็”ปๅƒใ‚’ๆญฃๅธธใซๆ‹กๅผตใ—ใ€ใใ‚Œใ‚‰ใฎๆณจ้‡ˆใ‚’ๆบ–ๅ‚™ใ—ใพใ—ใŸใ€‚ใŸใ ใ—ใ€ๅ‰ๅ‡ฆ็†ใฏใใ†ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใพใ ๅฎŒๆˆใ—ใฆใ„ใพใ™ใ€‚ๆœ€ๅพŒใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€็”ปๅƒใ‚’ใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎใ‚ซใ‚นใ‚ฟใƒ  `collatโ€‹โ€‹e_fn` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +็”ปๅƒ (็พๅœจใฏ `pixel_values`) ใ‚’ใƒใƒƒใƒๅ†…ใฎๆœ€ๅคงใฎ็”ปๅƒใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ—ใ€ๅฏพๅฟœใ™ใ‚‹ `pixel_mask` ใ‚’ไฝœๆˆใ—ใพใ™ +ใฉใฎใƒ”ใ‚ฏใ‚ปใƒซใŒๅฎŸๆ•ฐ (1) ใงใ€ใฉใฎใƒ”ใ‚ฏใ‚ปใƒซใŒใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ (0) ใงใ‚ใ‚‹ใ‹ใ‚’็คบใ—ใพใ™ใ€‚ + + +```py +>>> def collate_fn(batch): +... pixel_values = [item["pixel_values"] for item in batch] +... encoding = image_processor.pad(pixel_values, return_tensors="pt") +... labels = [item["labels"] for item in batch] +... batch = {} +... batch["pixel_values"] = encoding["pixel_values"] +... batch["pixel_mask"] = encoding["pixel_mask"] +... batch["labels"] = labels +... return batch +``` + +## Training the DETR model + +ๅ‰ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง้‡ๅŠดๅƒใฎใปใจใ‚“ใฉใ‚’ๅฎŒไบ†ใ—ใŸใฎใงใ€ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ +ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎ็”ปๅƒใฏใ€ใ‚ตใ‚คใ‚บใ‚’ๅค‰ๆ›ดใ—ใŸๅพŒใงใ‚‚ไพ็„ถใจใ—ใฆ้žๅธธใซๅคงใใ„ใงใ™ใ€‚ใ“ใ‚Œใฏใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใจใ€ +ๅฐ‘ใชใใจใ‚‚ 1 ใคใฎ GPU ใŒๅฟ…่ฆใงใ™ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใฏๆฌกใฎๆ‰‹้ †ใŒๅซใพใ‚Œใพใ™ใ€‚ +1. ๅ‰ๅ‡ฆ็†ใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€[`AutoModelForObjectDetection`] ใงใƒขใƒ‡ใƒซใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ +2. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ +3. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +4. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +ๅ‰ๅ‡ฆ็†ใซไฝฟ็”จใ—ใŸใฎใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใจใใฏใ€ๅฟ…ใš`label2id`ใ‚’ๆธกใ—ใฆใใ ใ•ใ„ใ€‚ +ใŠใ‚ˆใณ `id2label` ใƒžใƒƒใƒ—ใฏใ€ไปฅๅ‰ใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒกใ‚ฟใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ไฝœๆˆใ—ใŸใ‚‚ใฎใงใ™ใ€‚ใ•ใ‚‰ใซใ€`ignore_mismatched_sizes=True`ใ‚’ๆŒ‡ๅฎšใ—ใฆใ€ๆ—ขๅญ˜ใฎๅˆ†้กž้ ญ้ƒจใ‚’ๆ–ฐใ—ใ„ๅˆ†้กž้ ญ้ƒจใซ็ฝฎใๆ›ใˆใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForObjectDetection + +>>> model = AutoModelForObjectDetection.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ignore_mismatched_sizes=True, +... ) +``` + +[`TrainingArguments`] ใงใ€`output_dir` ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใ€ๅฟ…่ฆใซๅฟœใ˜ใฆใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆง‹ๆˆใ—ใพใ™ใ€‚ +็”ปๅƒๅˆ—ใŒๅ‰Š้™คใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆœชไฝฟ็”จใฎๅˆ—ใ‚’ๅ‰Š้™คใ—ใชใ„ใ“ใจใŒ้‡่ฆใงใ™ใ€‚็”ปๅƒๅˆ—ใŒใชใ„ใจใ€ +`pixel_values` ใ‚’ไฝœๆˆใงใใพใ›ใ‚“ใ€‚ใ“ใฎใŸใ‚ใ€`remove_unused_columns`ใ‚’`False`ใซ่จญๅฎšใ—ใพใ™ใ€‚ +ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ—ใŸใ„ๅ ดๅˆใฏใ€`push_to_hub` ใ‚’ `True` ใซ่จญๅฎšใ—ใพใ™ (Hugging ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™) +้ก”ใซๅ‘ใ‹ใฃใฆใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™๏ผ‰ใ€‚ + +```py +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments( +... output_dir="detr-resnet-50_finetuned_cppe5", +... per_device_train_batch_size=8, +... num_train_epochs=10, +... fp16=True, +... save_steps=200, +... logging_steps=50, +... learning_rate=1e-5, +... weight_decay=1e-4, +... save_total_limit=2, +... remove_unused_columns=False, +... push_to_hub=True, +... ) +``` + +ๆœ€ๅพŒใซใ€ใ™ในใฆใ‚’ใพใจใ‚ใฆใ€[`~transformers.Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=collate_fn, +... train_dataset=cppe5["train"], +... processing_class=image_processor, +... ) + +>>> trainer.train() +``` + +`training_args`ใง`push_to_hub`ใ‚’`True`ใซ่จญๅฎšใ—ใŸๅ ดๅˆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏ +ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใƒใƒ–ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€[`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ€ๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚‚ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + +## Evaluate + +็‰ฉไฝ“ๆคœๅ‡บใƒขใƒ‡ใƒซใฏ้€šๅธธใ€ไธ€้€ฃใฎ COCO ใ‚นใ‚ฟใ‚คใƒซใฎๆŒ‡ๆจ™ใ‚’ไฝฟ็”จใ—ใฆ่ฉ•ไพกใ•ใ‚Œใพใ™ใ€‚ +ๆ—ขๅญ˜ใฎใƒกใƒˆใƒชใ‚ฏใ‚นๅฎŸ่ฃ…ใฎใ„ใšใ‚Œใ‹ใ‚’ไฝฟ็”จใงใใพใ™ใŒใ€ใ“ใ“ใงใฏ`torchvision`ใฎใƒกใƒˆใƒชใ‚ฏใ‚นๅฎŸ่ฃ…ใ‚’ไฝฟ็”จใ—ใฆๆœ€็ต‚็š„ใชใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚ +ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใŸใƒขใƒ‡ใƒซใ€‚ + +`torchvision`ใ‚จใƒใƒชใƒฅใ‚จใƒผใ‚ฟใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ ใƒˆใ‚ฅใƒซใƒผใ‚น COCO ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ COCO ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๆง‹็ฏ‰ใ™ใ‚‹ใŸใ‚ใฎ API +ใƒ‡ใƒผใ‚ฟใ‚’็‰นๅฎšใฎๅฝขๅผใงไฟๅญ˜ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ๆœ€ๅˆใซ็”ปๅƒใจๆณจ้‡ˆใ‚’ใƒ‡ใ‚ฃใ‚นใ‚ฏใซไฟๅญ˜ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใจๅŒใ˜ใ‚ˆใ†ใซ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซใƒ‡ใƒผใ‚ฟใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใจใใ€`cppe5["test"]` ใ‹ใ‚‰ใฎๆณจ้‡ˆใ‚’ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€็”ปๅƒ +ใใฎใพใพใงใ„ใ‚‹ในใใงใ™ใ€‚ + +่ฉ•ไพกใ‚นใƒ†ใƒƒใƒ—ใซใฏๅฐ‘ใ—ไฝœๆฅญใŒๅฟ…่ฆใงใ™ใŒใ€ๅคงใใ 3 ใคใฎใ‚นใƒ†ใƒƒใƒ—ใซๅˆ†ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใพใšใ€`cppe5["test"]` ใ‚ปใƒƒใƒˆใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ๆณจ้‡ˆใ‚’ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’ใƒ‡ใ‚ฃใ‚นใ‚ฏใซไฟๅญ˜ใ—ใพใ™ใ€‚ + + +```py +>>> import json + + +>>> # format annotations the same as for training, no need for data augmentation +>>> def val_formatted_anns(image_id, objects): +... annotations = [] +... for i in range(0, len(objects["id"])): +... new_ann = { +... "id": objects["id"][i], +... "category_id": objects["category"][i], +... "iscrowd": 0, +... "image_id": image_id, +... "area": objects["area"][i], +... "bbox": objects["bbox"][i], +... } +... annotations.append(new_ann) + +... return annotations + + +>>> # Save images and annotations into the files torchvision.datasets.CocoDetection expects +>>> def save_cppe5_annotation_file_images(cppe5): +... output_json = {} +... path_output_cppe5 = f"{os.getcwd()}/cppe5/" + +... if not os.path.exists(path_output_cppe5): +... os.makedirs(path_output_cppe5) + +... path_anno = os.path.join(path_output_cppe5, "cppe5_ann.json") +... categories_json = [{"supercategory": "none", "id": id, "name": id2label[id]} for id in id2label] +... output_json["images"] = [] +... output_json["annotations"] = [] +... for example in cppe5: +... ann = val_formatted_anns(example["image_id"], example["objects"]) +... output_json["images"].append( +... { +... "id": example["image_id"], +... "width": example["image"].width, +... "height": example["image"].height, +... "file_name": f"{example['image_id']}.png", +... } +... ) +... output_json["annotations"].extend(ann) +... output_json["categories"] = categories_json + +... with open(path_anno, "w") as file: +... json.dump(output_json, file, ensure_ascii=False, indent=4) + +... for im, img_id in zip(cppe5["image"], cppe5["image_id"]): +... path_img = os.path.join(path_output_cppe5, f"{img_id}.png") +... im.save(path_img) + +... return path_output_cppe5, path_anno +``` + +ๆฌกใซใ€`cocoevaluator`ใงๅˆฉ็”จใงใใ‚‹`CocoDetection`ใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’็”จๆ„ใ—ใพใ™ใ€‚ + + +```py +>>> import torchvision + + +>>> class CocoDetection(torchvision.datasets.CocoDetection): +... def __init__(self, img_folder, image_processor, ann_file): +... super().__init__(img_folder, ann_file) +... self.image_processor = image_processor + +... def __getitem__(self, idx): +... # read in PIL image and target in COCO format +... img, target = super(CocoDetection, self).__getitem__(idx) + +... # preprocess image and target: converting target to DETR format, +... # resizing + normalization of both image and target) +... image_id = self.ids[idx] +... target = {"image_id": image_id, "annotations": target} +... encoding = self.image_processor(images=img, annotations=target, return_tensors="pt") +... pixel_values = encoding["pixel_values"].squeeze() # remove batch dimension +... target = encoding["labels"][0] # remove batch dimension + +... return {"pixel_values": pixel_values, "labels": target} + + +>>> im_processor = AutoImageProcessor.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5") + +>>> path_output_cppe5, path_anno = save_cppe5_annotation_file_images(cppe5["test"]) +>>> test_ds_coco_format = CocoDetection(path_output_cppe5, im_processor, path_anno) +``` + +ๆœ€ๅพŒใซใ€ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่ฉ•ไพกใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```py +>>> import evaluate +>>> from tqdm import tqdm + +>>> model = AutoModelForObjectDetection.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5") +>>> module = evaluate.load("ybelkada/cocoevaluate", coco=test_ds_coco_format.coco) +>>> val_dataloader = torch.utils.data.DataLoader( +... test_ds_coco_format, batch_size=8, shuffle=False, num_workers=4, collate_fn=collate_fn +... ) + +>>> with torch.no_grad(): +... for idx, batch in enumerate(tqdm(val_dataloader)): +... pixel_values = batch["pixel_values"] +... pixel_mask = batch["pixel_mask"] + +... labels = [ +... {k: v for k, v in t.items()} for t in batch["labels"] +... ] # these are in DETR format, resized + normalized + +... # forward pass +... outputs = model(pixel_values=pixel_values, pixel_mask=pixel_mask) + +... orig_target_sizes = torch.stack([target["orig_size"] for target in labels], dim=0) +... results = im_processor.post_process(outputs, orig_target_sizes) # convert outputs of model to Pascal VOC format (xmin, ymin, xmax, ymax) + +... module.add(prediction=results, reference=labels) +... del batch + +>>> results = module.compute() +>>> print(results) +Accumulating evaluation results... +DONE (t=0.08s). +IoU metric: bbox + Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.352 + Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.681 + Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.292 + Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.168 + Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.208 + Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.429 + Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.274 + Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.484 + Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.501 + Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.191 + Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.323 + Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.590 +``` + +ใ“ใ‚Œใ‚‰ใฎ็ตๆžœใฏใ€[`~transformers.TrainingArguments`] ใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’่ชฟๆ•ดใ™ใ‚‹ใ“ใจใงใ•ใ‚‰ใซๆ”นๅ–„ใงใใพใ™ใ€‚่ฉฆใ—ใฆใ”ใ‚‰ใ‚“๏ผ + +## Inference + +DETR ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆ่ฉ•ไพกใ—ใ€Hugging Face Hub ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใพใ™ใ€‚ +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ +ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใ—ใ€ใใ‚Œใซ็”ปๅƒใ‚’ๆธกใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline +>>> import requests + +>>> url = "https://i.imgur.com/2lnWoly.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> obj_detector = pipeline("object-detection", model="devonho/detr-resnet-50_finetuned_cppe5") +>>> obj_detector(image) +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +>>> image_processor = AutoImageProcessor.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5") +>>> model = AutoModelForObjectDetection.from_pretrained("devonho/detr-resnet-50_finetuned_cppe5") + +>>> with torch.no_grad(): +... inputs = image_processor(images=image, return_tensors="pt") +... outputs = model(**inputs) +... target_sizes = torch.tensor([image.size[::-1]]) +... results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0] + +>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): +... box = [round(i, 2) for i in box.tolist()] +... print( +... f"Detected {model.config.id2label[label.item()]} with confidence " +... f"{round(score.item(), 3)} at location {box}" +... ) +Detected Coverall with confidence 0.566 at location [1215.32, 147.38, 4401.81, 3227.08] +Detected Mask with confidence 0.584 at location [2449.06, 823.19, 3256.43, 1413.9] +``` + +็ตๆžœใ‚’ใƒ—ใƒญใƒƒใƒˆใ—ใฆใฟใพใ—ใ‚‡ใ†: + +```py +>>> draw = ImageDraw.Draw(image) + +>>> for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): +... box = [round(i, 2) for i in box.tolist()] +... x, y, x2, y2 = tuple(box) +... draw.rectangle((x, y, x2, y2), outline="red", width=1) +... draw.text((x, y), model.config.id2label[label.item()], fill="white") + +>>> image +``` + +
+ Object detection result on a new image +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/prompting.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/prompting.md new file mode 100644 index 0000000000000000000000000000000000000000..bd66e751ee61d64058898156d873496355cbd8b8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/prompting.md @@ -0,0 +1,438 @@ + + + +# LLM prompting guide + +[[open-in-colab]] + +Falconใ€LLaMA ใชใฉใฎๅคง่ฆๆจก่จ€่ชžใƒขใƒ‡ใƒซใฏใ€ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผ ใƒขใƒ‡ใƒซใงใ‚ใ‚Šใ€ๆœ€ๅˆใฏไบˆๆธฌใ™ใ‚‹ใ‚ˆใ†ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +ๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใŒไธŽใˆใ‚‰ใ‚ŒใŸๅ ดๅˆใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใ€‚้€šๅธธใ€ๆ•ฐๅๅ„„ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒใ‚ใ‚Šใ€ไฝ•ๅ…†ใ‚‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +้•ทๆœŸ้–“ใฎใƒˆใƒผใ‚ฏใƒณใ€‚ใใฎ็ตๆžœใ€ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏ้žๅธธใซๅผทๅŠ›ใงๅคš็”จ้€”ใซใชใ‚Šใ€ๆฌกใฎใ‚ˆใ†ใชใ“ใจใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ +่‡ช็„ถ่จ€่ชžใƒ—ใƒญใƒณใƒ—ใƒˆใงใƒขใƒ‡ใƒซใซๆŒ‡็คบใ™ใ‚‹ใ“ใจใงใ€ใ™ใใซ่ค‡ๆ•ฐใฎ NLP ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใงใใพใ™ใ€‚ + +ๆœ€้ฉใชๅ‡บๅŠ›ใ‚’ไฟ่จผใ™ใ‚‹ใŸใ‚ใซใ“ใฎใ‚ˆใ†ใชใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’่จญ่จˆใ™ใ‚‹ใ“ใจใฏใ€ๅคšใใฎๅ ดๅˆใ€Œใƒ—ใƒญใƒณใƒ—ใƒˆ ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใ€ใจๅ‘ผใฐใ‚Œใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใจใฏใ€ +ใ‹ใชใ‚Šใฎ้‡ใฎๅฎŸ้จ“ใ‚’ๅฟ…่ฆใจใ™ใ‚‹ๅๅพฉใƒ—ใƒญใ‚ปใ‚นใ€‚่‡ช็„ถ่จ€่ชžใฏใฏใ‚‹ใ‹ใซๆŸ”่ปŸใง่กจ็พๅŠ›่ฑŠใ‹ใงใ™ +ใŸใ ใ—ใ€ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใ‚ˆใ‚Šใ‚‚ใ‚ใ„ใพใ„ใ•ใŒ็”Ÿใ˜ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ๅŒๆ™‚ใซใ€่‡ช็„ถ่จ€่ชžใซใ‚ˆใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆ +ๅค‰ๅŒ–ใซใฏใ‹ใชใ‚Šๆ•ๆ„Ÿใงใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใซใ‚ใšใ‹ใชๅค‰ๆ›ดใ‚’ๅŠ ใˆใŸใ ใ‘ใงใ‚‚ใ€ๅ‡บๅŠ›ใŒๅคงๅน…ใซ็•ฐใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ™ในใฆใฎใ‚ฑใƒผใ‚นใซ้ฉๅˆใ™ใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใฎๆญฃ็ขบใชใƒฌใ‚ทใƒ”ใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€็ ”็ฉถ่€…ใฏใ„ใใคใ‹ใฎๆœ€่‰ฏใฎใƒฌใ‚ทใƒ”ใ‚’่€ƒๆกˆใ—ใพใ—ใŸใ€‚ +ๆœ€้ฉใช็ตๆžœใ‚’ใ‚ˆใ‚Šไธ€่ฒซใ—ใฆ้”ๆˆใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคๅฎŸ่ทตใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ใ‚ˆใ‚Šๅ„ชใ‚ŒใŸ LLM ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใ—ใ€ใ•ใพใ–ใพใช NLP ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใฎใซๅฝน็ซ‹ใคใƒ—ใƒญใƒณใƒ—ใƒˆ ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฎใƒ™ใ‚นใƒˆ ใƒ—ใƒฉใ‚ฏใƒ†ใ‚ฃใ‚นใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +ๆฌกใฎใ“ใจใ‚’ๅญฆใณใพใ™: + +- [ใƒ—ใƒญใƒณใƒ—ใƒˆใฎๅŸบๆœฌ](#basics-of-prompting) +- [LLM ใƒ—ใƒญใƒณใƒ—ใƒˆใฎใƒ™ใ‚นใƒˆ ใƒ—ใƒฉใ‚ฏใƒ†ใ‚ฃใ‚น](#best-practices-of-llm-prompting) +- [้ซ˜ๅบฆใชใƒ—ใƒญใƒณใƒ—ใƒˆ ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏ: ๆ•ฐๅ›žใฎใƒ—ใƒญใƒณใƒ—ใƒˆใจๆ€่€ƒใฎ้€ฃ้Ž–](#advanced-prompting-techniques) +- [ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’่กจ็คบใ™ใ‚‹ไปฃใ‚ใ‚Šใซๅพฎ่ชฟๆ•ดใ™ใ‚‹ๅ ดๅˆ](#prompting-vs-fine-tuning) + + + +่ฟ…้€Ÿใชใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฏใ€LLM ๅ‡บๅŠ›ๆœ€้ฉๅŒ–ใƒ—ใƒญใ‚ปใ‚นใฎไธ€้ƒจใซใ™ใŽใพใ›ใ‚“ใ€‚ใ‚‚ใ† 1 ใคใฎ้‡่ฆใช่ฆ็ด ใฏใ€ +ๆœ€้ฉใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใ€‚ LLM ใŒ็”Ÿๆˆๆ™‚ใซๅพŒ็ถšใฎๅ„ใƒˆใƒผใ‚ฏใƒณใ‚’้ธๆŠžใ™ใ‚‹ๆ–นๆณ•ใ‚’ใ‚ซใ‚นใ‚ฟใƒžใ‚คใ‚บใงใใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅฏ่ƒฝใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ไธ€ๅˆ‡ๅค‰ๆ›ดใ›ใšใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใงใ€ +็”Ÿๆˆใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใซ็นฐใ‚Š่ฟ”ใ—ใŒๅซใพใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚ˆใ‚Šไธ€่ฒซๆ€งใŒใ‚ใ‚Šไบบ้–“ใ‚‰ใ—ใ„้Ÿฟใใซใชใ‚Šใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฏใ“ใฎใ‚ฌใ‚คใƒ‰ใฎ็ฏ„ๅ›ฒๅค–ใงใ™ใŒใ€ใ“ใ‚Œใ‚‰ใฎใƒˆใƒ”ใƒƒใ‚ฏใซใคใ„ใฆ่ฉณใ—ใใฏใ€ๆฌกใฎใƒˆใƒ”ใƒƒใ‚ฏใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +ๆฌกใฎใ‚ฌใ‚คใƒ‰: + +* [LLM ใซใ‚ˆใ‚‹็”Ÿๆˆ](../llm_tutorial) +* [ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) + + + +## Basics of prompting + +### Types of models + +ๆœ€ๆ–ฐใฎ LLM ใฎๅคง้ƒจๅˆ†ใฏใ€ใƒ‡ใ‚ณใƒผใƒ€ๅฐ‚็”จใฎใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใงใ™ใ€‚ไพ‹ใจใ—ใฆใฏใ€[LLaMA](../model_doc/llama)ใ€ +[Llama2](../model_doc/llama2)ใ€[Falcon](../model_doc/falcon)ใ€[GPT2](../model_doc/gpt2)ใ€‚ใŸใ ใ—ใ€้ญ้‡ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ +ใ‚จใƒณใ‚ณใƒผใƒ€ ใƒ‡ใ‚ณใƒผใƒ€ ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒž LLM ใ‚‚ๅŒๆง˜ใงใ™ใ€‚ใŸใจใˆใฐใ€[Flan-T5](../model_doc/flan-t5) ใ‚„ [BART](../model_doc/bart) ใงใ™ใ€‚ + +ใ‚จใƒณใ‚ณใƒผใƒ€ ใƒ‡ใ‚ณใƒผใƒ€ ใ‚นใ‚ฟใ‚คใƒซใฎใƒขใƒ‡ใƒซใฏ้€šๅธธใ€ๅ‡บๅŠ›ใŒๅ…ฅๅŠ›ใซ**ๅคงใใ**ไพๅญ˜ใ™ใ‚‹็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใงไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ +ใŸใจใˆใฐใ€็ฟป่จณใจ่ฆ็ด„ใงใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ๅฐ‚็”จใƒขใƒ‡ใƒซใฏใ€ไป–ใฎใ™ในใฆใฎใ‚ฟใ‚คใƒ—ใฎ็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใฆ LLM ใงใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ๅ ดๅˆใ€ไฝฟ็”จใ—ใฆใ„ใ‚‹ LLM ใฎใ‚ฟใ‚คใƒ—ใ‚’็Ÿฅใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ +็•ฐใชใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +`text-generation`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใ‚ณใƒผใƒ€ใฎใฟใฎใƒขใƒ‡ใƒซใงๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```python +>>> from transformers import pipeline +>>> import torch + +>>> torch.manual_seed(0) # doctest: +IGNORE_RESULT + +>>> generator = pipeline('text-generation', model = 'openai-community/gpt2') +>>> prompt = "Hello, I'm a language model" + +>>> generator(prompt, max_length = 30) +[{'generated_text': "Hello, I'm a language model expert, so I'm a big believer in the concept that I know very well and then I try to look into"}] +``` + +ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ/ใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€`text2text-generation` ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```python +>>> text2text_generator = pipeline("text2text-generation", model = 'google/flan-t5-base') +>>> prompt = "Translate from English to French: I'm very happy to see you" + +>>> text2text_generator(prompt) +[{'generated_text': 'Je suis trรจs heureuse de vous rencontrer.'}] +``` + +### Base vs instruct/chat models + +๐Ÿค— Hub ใงๅˆฉ็”จใงใใ‚‹ๆœ€่ฟ‘ใฎ LLM ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎใปใจใ‚“ใฉใซใฏใ€base ใจ instruct (ใพใŸใฏ chat) ใฎ 2 ใคใฎใƒใƒผใ‚ธใƒงใƒณใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ +[`tiiuae/falcon-7b`](https://huggingface.co/tiiuae/falcon-7b) ใŠใ‚ˆใณ [`tiiuae/falcon-7b-instruct`](https://huggingface.co/tiiuae/falcon-7b) -ๆŒ‡็คบใ™ใ‚‹)ใ€‚ + +ๅŸบๆœฌใƒขใƒ‡ใƒซใฏใ€ๆœ€ๅˆใฎใƒ—ใƒญใƒณใƒ—ใƒˆใŒไธŽใˆใ‚‰ใ‚ŒใŸใจใใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅฎŒๆˆใ•ใ›ใ‚‹ใฎใซใฏๅ„ชใ‚Œใฆใ„ใพใ™ใŒใ€NLP ใ‚ฟใ‚นใ‚ฏใซใฏ็†ๆƒณ็š„ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ๆŒ‡็คบใซๅพ“ใ†ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€ใพใŸใฏไผš่ฉฑใงไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใซไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ“ใงใ€ๆŒ‡็คบ (ใƒใƒฃใƒƒใƒˆ) ใƒใƒผใ‚ธใƒงใƒณใŒ็™ปๅ ดใ—ใพใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฏใ€ๅ‘ฝไปคใจไผš่ฉฑใƒ‡ใƒผใ‚ฟใซๅŸบใฅใ„ใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ™ใƒผใ‚น ใƒใƒผใ‚ธใƒงใƒณใ‚’ใ•ใ‚‰ใซๅพฎ่ชฟๆ•ดใ—ใŸ็ตๆžœใงใ™ใ€‚ +ใ“ใฎ่ฟฝๅŠ ใฎๅพฎ่ชฟๆ•ดใซใ‚ˆใ‚Šใ€ๅคšใใฎ NLP ใ‚ฟใ‚นใ‚ฏใซใจใฃใฆใ‚ˆใ‚Š้ฉๅˆ‡ใช้ธๆŠž่‚ขใซใชใ‚Šใพใ™ใ€‚ + +[`tiiuae/falcon-7b-instruct`](https://huggingface.co/tiiuae/falcon-7b-instruct) ใงไฝฟ็”จใงใใ‚‹ใ„ใใคใ‹ใฎ็ฐกๅ˜ใชใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’็คบใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +ใ„ใใคใ‹ใฎไธ€่ˆฌ็š„ใช NLP ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ + +### NLP tasks + +ใพใšใ€็’ฐๅขƒใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```bash +pip install -q transformers accelerate +``` + +ๆฌกใซใ€้ฉๅˆ‡ใชใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ (`text_generation`) ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```python +>>> from transformers import pipeline, AutoTokenizer +>>> import torch + +>>> torch.manual_seed(0) # doctest: +IGNORE_RESULT +>>> model = "tiiuae/falcon-7b-instruct" + +>>> tokenizer = AutoTokenizer.from_pretrained(model) +>>> pipe = pipeline( +... "text-generation", +... model=model, +... tokenizer=tokenizer, +... torch_dtype=torch.bfloat16, +... device_map="auto", +... ) +``` + + + +Falcon ใƒขใƒ‡ใƒซใฏ `bfloat16` ใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใŸใ‚ใ€ๅŒใ˜ใ‚‚ใฎใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€ๆœ€่ฟ‘ใฎ +CUDA ใฎใƒใƒผใ‚ธใƒงใƒณใซๆบ–ๆ‹ ใ—ใฆใŠใ‚Šใ€ๆœ€ๆ–ฐใฎใ‚ซใƒผใƒ‰ใงๆœ€้ฉใซๅ‹•ไฝœใ—ใพใ™ใ€‚ + + + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ็ตŒ็”ฑใงใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใŸใฎใงใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆ NLP ใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +#### Text classification + +ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใฎๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชๅฝขๅผใฎ 1 ใคใฏใ‚ปใƒณใƒใƒกใƒณใƒˆๅˆ†ๆžใงใ‚ใ‚Šใ€ใ€Œใƒใ‚ธใƒ†ใ‚ฃใƒ–ใ€ใ€ใ€Œใƒใ‚ฌใƒ†ใ‚ฃใƒ–ใ€ใ€ใ€Œใƒใ‚ฌใƒ†ใ‚ฃใƒ–ใ€ใชใฉใฎใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ +ใพใŸใฏใ€ไธ€้€ฃใฎใƒ†ใ‚ญใ‚นใƒˆใซๅฏพใ—ใฆใ€Œไธญ็ซ‹ใ€ใงใ™ใ€‚ไธŽใˆใ‚‰ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆ (ๆ˜ ็”ปใƒฌใƒ“ใƒฅใƒผ) ใ‚’ๅˆ†้กžใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใ™ใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +ใพใšๆŒ‡็คบใ‚’ไธŽใˆใ€ๆฌกใซๅˆ†้กžใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ใใฎใพใพใซใ—ใฆใŠใใฎใงใฏใชใใ€ +ๅฟœ็ญ”ใฎๅ…ˆ้ ญใซใ‚‚่ฟฝๅŠ ใ—ใพใ™ - `"Sentiment: "`: + +```python +>>> torch.manual_seed(0) # doctest: +IGNORE_RESULT +>>> prompt = """Classify the text into neutral, negative or positive. +... Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. +... Sentiment: +... """ + +>>> sequences = pipe( +... prompt, +... max_new_tokens=10, +... ) + +>>> for seq in sequences: +... print(f"Result: {seq['generated_text']}") +Result: Classify the text into neutral, negative or positive. +Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen. +Sentiment: +Positive +``` + +ใใฎ็ตๆžœใ€ๅ‡บๅŠ›ใซใฏใ€ๆ‰‹้ †ใงๆไพ›ใ—ใŸใƒชใ‚นใƒˆใฎๅˆ†้กžใƒฉใƒ™ใƒซใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ใใ‚Œใฏๆญฃใ—ใ„ใƒฉใƒ™ใƒซใงใ™ใ€‚ + + + +ใƒ—ใƒญใƒณใƒ—ใƒˆใซๅŠ ใˆใฆใ€`max_new_tokens`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆธกใ—ใฆใ„ใ‚‹ใ“ใจใซๆฐ—ใฅใใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐใ‚’ๅˆถๅพกใ—ใพใ™ใ€‚ +ใƒขใƒ‡ใƒซใŒ็”Ÿๆˆใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅญฆ็ฟ’ใงใใ‚‹ๅคšใใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ 1 ใคใงใ™ใ€‚ +[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](../generation_strategies) ใ‚ฌใ‚คใƒ‰ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +#### Named Entity Recognition + +ๅ›บๆœ‰่กจ็พ่ช่ญ˜ (NER) ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆๅ†…ใฎไบบ็‰ฉใ€ๅ ดๆ‰€ใ€็ต„็น”ใชใฉใฎๅ›บๆœ‰่กจ็พใ‚’ๆคœ็ดขใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใฎๆŒ‡็คบใ‚’ๅค‰ๆ›ดใ—ใฆใ€LLM ใซใ“ใฎใ‚ฟใ‚นใ‚ฏใ‚’ๅฎŸ่กŒใ•ใ›ใพใ—ใ‚‡ใ†ใ€‚ใ“ใ“ใงใฏ`return_full_text = False`ใ‚‚่จญๅฎšใ—ใพใ—ใ‚‡ใ† +ๅ‡บๅŠ›ใซใƒ—ใƒญใƒณใƒ—โ€‹โ€‹ใƒˆใŒๅซโ€‹โ€‹ใพใ‚Œใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```python +>>> torch.manual_seed(1) # doctest: +IGNORE_RESULT +>>> prompt = """Return a list of named entities in the text. +... Text: The Golden State Warriors are an American professional basketball team based in San Francisco. +... Named entities: +... """ + +>>> sequences = pipe( +... prompt, +... max_new_tokens=15, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"{seq['generated_text']}") +- Golden State Warriors +- San Francisco +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€ใƒขใƒ‡ใƒซใฏๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ 2 ใคใฎๅๅ‰ไป˜ใใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใ‚’ๆญฃใ—ใ่ญ˜ๅˆฅใ—ใพใ—ใŸใ€‚ + +#### Translation + +LLM ใŒๅฎŸ่กŒใงใใ‚‹ใ‚‚ใ† 1 ใคใฎใ‚ฟใ‚นใ‚ฏใฏ็ฟป่จณใงใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใซใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ/ใƒ‡ใ‚ณใƒผใƒ€ใƒผ ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’้ธๆŠžใงใใพใ™ใŒใ€ใ“ใ“ใงใฏ +ไพ‹ใ‚’็ฐกๅ˜ใซใ™ใ‚‹ใŸใ‚ใซใ€ใใกใ‚“ใจใ—ใŸไป•ไบ‹ใ‚’ใ™ใ‚‹ Falcon-7b-instruct ใ‚’ไฝฟใ„็ถšใ‘ใพใ™ใ€‚ใ‚‚ใ†ไธ€ๅบฆใ€ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™ +ใƒ†ใ‚ญใ‚นใƒˆใฎไธ€้ƒจใ‚’่‹ฑ่ชžใ‹ใ‚‰ใ‚คใ‚ฟใƒชใ‚ข่ชžใซ็ฟป่จณใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใ™ใ‚‹ๅŸบๆœฌ็š„ใชใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใงใใพใ™ใ€‚ + +```python +>>> torch.manual_seed(2) # doctest: +IGNORE_RESULT +>>> prompt = """Translate the English text to Italian. +... Text: Sometimes, I've believed as many as six impossible things before breakfast. +... Translation: +... """ + +>>> sequences = pipe( +... prompt, +... max_new_tokens=20, +... do_sample=True, +... top_k=10, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"{seq['generated_text']}") +A volte, ho creduto a sei impossibili cose prima di colazione. +``` + +ใ“ใ“ใงใฏใ€ๅ‡บๅŠ›็”Ÿๆˆๆ™‚ใซใƒขใƒ‡ใƒซใŒใ‚‚ใ†ๅฐ‘ใ—ๆŸ”่ปŸใซใชใ‚‹ใ‚ˆใ†ใซ `do_sample=True` ใจ `top_k=10` ใ‚’่ฟฝๅŠ ใ—ใพใ—ใŸใ€‚ + +#### Text summarization + +็ฟป่จณใจๅŒๆง˜ใซใ€ใƒ†ใ‚ญใ‚นใƒˆใฎ่ฆ็ด„ใ‚‚ใ€ๅ‡บๅŠ›ใŒๅ…ฅๅŠ›ใซ**ๅคงใใ**ไพๅญ˜ใ™ใ‚‹็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ใ‚จใƒณใ‚ณใƒผใƒ€/ใƒ‡ใ‚ณใƒผใƒ€ ใƒขใƒ‡ใƒซใฎๆ–นใŒ่‰ฏใ„้ธๆŠžใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใ ใ—ใ€ใƒ‡ใ‚ณใƒผใƒ€ ใ‚นใ‚ฟใ‚คใƒซใฎใƒขใƒ‡ใƒซใ‚‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใพใ™ใ€‚ +ไปฅๅ‰ใฏใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใฎๅ…ˆ้ ญใซๆŒ‡็คบใ‚’้…็ฝฎใ—ใฆใ„ใพใ—ใŸใ€‚ใŸใ ใ—ใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใฎๆœ€ๅพŒใงใ€ +ๆŒ‡็คบใ‚’ไธŽใˆใ‚‹ใฎใซ้ฉใ—ใŸๅ ดๆ‰€ใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚้€šๅธธใ€ๅ‘ฝไปคใฏใฉใกใ‚‰ใ‹ใฎ็ซฏใซ้…็ฝฎใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +```python +>>> torch.manual_seed(3) # doctest: +IGNORE_RESULT +>>> prompt = """Permaculture is a design process mimicking the diversity, functionality and resilience of natural ecosystems. The principles and practices are drawn from traditional ecological knowledge of indigenous cultures combined with modern scientific understanding and technological innovations. Permaculture design provides a framework helping individuals and communities develop innovative, creative and effective strategies for meeting basic needs while preparing for and mitigating the projected impacts of climate change. +... Write a summary of the above text. +... Summary: +... """ + +>>> sequences = pipe( +... prompt, +... max_new_tokens=30, +... do_sample=True, +... top_k=10, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"{seq['generated_text']}") +Permaculture is an ecological design mimicking natural ecosystems to meet basic needs and prepare for climate change. It is based on traditional knowledge and scientific understanding. +``` + +#### Question answering + +่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆฌกใฎ่ซ–็†ใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซๆง‹้€ ๅŒ–ใงใใพใ™: ๆŒ‡็คบใ€ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ€่ณชๅ•ใ€ +ๅ…ˆ้ ญใฎๅ˜่ชžใพใŸใฏใƒ•ใƒฌใƒผใ‚บ (`"Answer:"`) ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒขใƒ‡ใƒซใ‚’ๆ“ไฝœใ—ใฆ็ญ”ใˆใฎ็”Ÿๆˆใ‚’้–‹ๅง‹ใ—ใพใ™ใ€‚ + + +```python +>>> torch.manual_seed(4) # doctest: +IGNORE_RESULT +>>> prompt = """Answer the question using the context below. +... Context: Gazpacho is a cold soup and drink made of raw, blended vegetables. Most gazpacho includes stale bread, tomato, cucumbers, onion, bell peppers, garlic, olive oil, wine vinegar, water, and salt. Northern recipes often include cumin and/or pimentรณn (smoked sweet paprika). Traditionally, gazpacho was made by pounding the vegetables in a mortar with a pestle; this more laborious method is still sometimes used as it helps keep the gazpacho cool and avoids the foam and silky consistency of smoothie versions made in blenders or food processors. +... Question: What modern tool is used to make gazpacho? +... Answer: +... """ + +>>> sequences = pipe( +... prompt, +... max_new_tokens=10, +... do_sample=True, +... top_k=10, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"Result: {seq['generated_text']}") +Result: Modern tools are used, such as immersion blenders +``` + +#### Reasoning + +LLM ใซใจใฃใฆๆŽจ่ซ–ใฏๆœ€ใ‚‚ๅ›ฐ้›ฃใชใ‚ฟใ‚นใ‚ฏใฎ 1 ใคใงใ‚ใ‚Šใ€่‰ฏใ„็ตๆžœใ‚’้”ๆˆใ™ใ‚‹ใซใฏใ€ๅคšใใฎๅ ดๅˆใ€ๆฌกใฎใ‚ˆใ†ใช้ซ˜ๅบฆใชใƒ—ใƒญใƒณใƒ—ใƒˆ ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’้ฉ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[Chain-of-thought](#chain-of-thought)ใ€‚ + +ๅŸบๆœฌ็š„ใชใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝฟ็”จใ—ใฆใ€ๅ˜็ด”ใช็ฎ—่ก“ใ‚ฟใ‚นใ‚ฏใซ้–ขใ™ใ‚‹ใƒขใƒ‡ใƒซๆŽจ่ซ–ใ‚’ไฝœๆˆใงใใ‚‹ใ‹ใฉใ†ใ‹่ฉฆใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +>>> torch.manual_seed(5) # doctest: +IGNORE_RESULT +>>> prompt = """There are 5 groups of students in the class. Each group has 4 students. How many students are there in the class?""" + +>>> sequences = pipe( +... prompt, +... max_new_tokens=30, +... do_sample=True, +... top_k=10, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"Result: {seq['generated_text']}") +Result: +There are a total of 5 groups, so there are 5 x 4=20 students in the class. +``` + +ๆญฃใ—ใ„๏ผใ‚‚ใ†ๅฐ‘ใ—่ค‡้›‘ใ•ใ‚’ๅข—ใ‚„ใ—ใฆใ€ๅŸบๆœฌ็š„ใชใƒ—ใƒญใƒณใƒ—ใƒˆใงๅ•้กŒใ‚’่งฃๆฑบใงใใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +>>> torch.manual_seed(6) # doctest: +IGNORE_RESULT +>>> prompt = """I baked 15 muffins. I ate 2 muffins and gave 5 muffins to a neighbor. My partner then bought 6 more muffins and ate 2. How many muffins do we now have?""" + +>>> sequences = pipe( +... prompt, +... max_new_tokens=10, +... do_sample=True, +... top_k=10, +... return_full_text = False, +... ) + +>>> for seq in sequences: +... print(f"Result: {seq['generated_text']}") +Result: +The total number of muffins now is 21 +``` + +ใ“ใ‚Œใฏ้–“้•ใฃใŸ็ญ”ใˆใงใ™ใ€‚12 ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใŒๅŸบๆœฌ็š„ใ™ใŽใ‚‹ใ‹ใ€้ธๆŠžๅ†…ๅฎนใŒๅŽŸๅ› ใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +็ตๅฑ€ใฎใจใ“ใ‚ใ€Falcon ใฎๆœ€ๅฐใƒใƒผใ‚ธใƒงใƒณใ‚’้ธๆŠžใ—ใพใ—ใŸใ€‚ใ‚ใ‚‰ใ‚†ใ‚‹ใ‚ตใ‚คใ‚บใฎใƒขใƒ‡ใƒซใงใฏๆŽจ่ซ–ใŒๅ›ฐ้›ฃใงใ™ใŒใ€ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใงใฏ +ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Best practices of LLM prompting + +ใ‚ฌใ‚คใƒ‰ใฎใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใฎ็ตๆžœใ‚’ๆ”นๅ–„ใ™ใ‚‹ๅ‚พๅ‘ใซใ‚ใ‚‹ใƒ™ใ‚นใƒˆ ใƒ—ใƒฉใ‚ฏใƒ†ใ‚ฃใ‚นใฎใƒชใ‚นใƒˆใ‚’ใพใจใ‚ใพใ—ใŸใ€‚ + +* ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’้ธๆŠžใ™ใ‚‹ๅ ดๅˆใฏใ€ๆœ€ๆ–ฐใ‹ใคๆœ€ใ‚‚ๆฉŸ่ƒฝ็š„ใชใƒขใƒ‡ใƒซใฎๆ–นใŒใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +* ใ‚ทใƒณใƒ—ใƒซใง็Ÿญใ„ใƒ—ใƒญใƒณใƒ—ใƒˆใ‹ใ‚‰ๅง‹ใ‚ใฆใ€ใใ“ใ‹ใ‚‰็นฐใ‚Š่ฟ”ใ—ใพใ™ใ€‚ +* ๆŒ‡็คบใฏใƒ—ใƒญใƒณใƒ—ใƒˆใฎๆœ€ๅˆใพใŸใฏๆœ€ๅพŒใซๅ…ฅๅŠ›ใ—ใฆใใ ใ•ใ„ใ€‚ๅคง่ฆๆจกใชใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ‰ฑใ†ๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฏใ•ใพใ–ใพใชๆœ€้ฉๅŒ–ใ‚’้ฉ็”จใ—ใฆใ€ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฎ่ค‡้›‘ใ•ใŒไบŒๆฌก็š„ใซๆ‹กๅคงใ™ใ‚‹ใฎใ‚’้˜ฒใŽใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฏใƒ—ใƒญใƒณใƒ—ใƒˆใฎ้€”ไธญใ‚ˆใ‚Šใ‚‚ๆœ€ๅˆใพใŸใฏๆœ€ๅพŒใซๆณจๆ„ใ‚’ๆ‰•ใ†ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +* ๆŒ‡็คบใจใ€ใใ‚ŒใŒ้ฉ็”จใ•ใ‚Œใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ˜Ž็ขบใซๅŒบๅˆฅใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใ‚Œใซใคใ„ใฆใฏใ€ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใง่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ +* ใ‚ฟใ‚นใ‚ฏใจๆœ›ใพใ—ใ„็ตๆžœ (ใใฎๅฝขๅผใ€้•ทใ•ใ€ใ‚นใ‚ฟใ‚คใƒซใ€่จ€่ชžใชใฉ) ใซใคใ„ใฆๅ…ทไฝ“็š„ใ‹ใค่ชฌๆ˜Ž็š„ใซใ—ใพใ™ใ€‚ +* ๆ›–ๆ˜งใช่ชฌๆ˜Žใ‚„ๆŒ‡็คบใฏ้ฟใ‘ใฆใใ ใ•ใ„ใ€‚ +*ใ€Œไฝ•ใ‚’ใ—ใฆใฏใ„ใ‘ใชใ„ใ‹ใ€ใจใ„ใ†ๆŒ‡็คบใงใฏใชใใ€ใ€Œไฝ•ใ‚’ใ™ในใใ‹ใ€ใจใ„ใ†ๆŒ‡็คบใ‚’ๅ„ชๅ…ˆใ—ใพใ™ใ€‚ +* ๆœ€ๅˆใฎๅ˜่ชžใ‚’ๆ›ธใ„ใฆ (ใพใŸใฏใƒขใƒ‡ใƒซใฎๆœ€ๅˆใฎๆ–‡ใ‚’ๅง‹ใ‚ใฆ)ใ€ๅ‡บๅŠ›ใ‚’ๆญฃใ—ใ„ๆ–นๅ‘ใซใ€ŒๅฐŽใใ€ใพใ™ใ€‚ +* [Few-shot prompting](#few-shot-prompting) ใ‚„ [Chain-of-thought](#chain-of-thought) ใชใฉใฎ้ซ˜ๅบฆใชใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +* ใ•ใพใ–ใพใชใƒขใƒ‡ใƒซใงใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ใƒ†ใ‚นใƒˆใ—ใฆใ€ใใฎๅ …็‰ขๆ€งใ‚’่ฉ•ไพกใ—ใพใ™ใ€‚ +* ใƒ—ใƒญใƒณใƒ—ใƒˆใฎใƒใƒผใ‚ธใƒงใƒณใ‚’็ขบ่ชใ—ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฟฝ่ทกใ—ใพใ™ใ€‚ + +## Advanced prompting techniques + +### Few-shot prompting + +ไธŠ่จ˜ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๅŸบๆœฌ็š„ใชใƒ—ใƒญใƒณใƒ—ใƒˆใฏใ€ใ€Œใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใฎไพ‹ใงใ™ใ€‚ใคใพใ‚Šใ€ใƒขใƒ‡ใƒซใซใฏใ™ใงใซไธŽใˆใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ +ๆŒ‡็คบใจใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใฏใ‚ใ‚Šใพใ™ใŒใ€่งฃๆฑบ็ญ–ใ‚’ๅซใ‚€ไพ‹ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚้€šๅธธใ€ๅ‘ฝไปคใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅŸบใฅใ„ใฆๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ LLM +ใ“ใฎใ‚ˆใ†ใชใ€Œใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใ€ใ‚ฟใ‚นใ‚ฏใงใ‚‚ๅ„ชใ‚ŒใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใ‚ฟใ‚นใ‚ฏใŒใ‚ˆใ‚Š่ค‡้›‘ใงใ‚ใฃใŸใ‚Šๅพฎๅฆ™ใช็‚นใŒใ‚ใฃใŸใ‚Šใ™ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๅ‡บๅŠ›ใซใฏใ€ๅ‘ฝไปคใ ใ‘ใงใฏใƒขใƒ‡ใƒซใŒ็†่งฃใงใใชใ„ใ„ใใคใ‹ใฎ่ฆไปถใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๆฌกใฎใ“ใจใŒใงใใพใ™ใ€‚ +ๅฐ‘ๆ•ฐใ‚ทใƒงใƒƒใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใจๅ‘ผใฐใ‚Œใ‚‹ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’่ฉฆใ—ใฆใใ ใ•ใ„ใ€‚ + +ๅฐ‘ๆ•ฐใ‚ทใƒงใƒƒใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใงใฏใ€ใƒขใƒ‡ใƒซใซใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใฎใ‚ˆใ‚Šๅคšใใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆไพ›ใ™ใ‚‹ใƒ—ใƒญใƒณใƒ—ใƒˆๅ†…ใฎไพ‹ใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ +ไพ‹ใงใฏใ€ไพ‹ใฎใƒ‘ใ‚ฟใƒผใƒณใซๅพ“ใฃใฆๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใ‚’ๆกไปถไป˜ใ‘ใ—ใพใ™ใ€‚ + +ไปฅไธ‹ใซไพ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +```python +>>> torch.manual_seed(0) # doctest: +IGNORE_RESULT +>>> prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961. +... Date: 04/12/1961 +... Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. +... Date:""" + +>>> sequences = pipe( +... prompt, +... max_new_tokens=8, +... do_sample=True, +... top_k=10, +... ) + +>>> for seq in sequences: +... print(f"Result: {seq['generated_text']}") +Result: Text: The first human went into space and orbited the Earth on April 12, 1961. +Date: 04/12/1961 +Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. +Date: 09/28/1960 +``` + +ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ ใ‚นใƒ‹ใƒšใƒƒใƒˆใงใฏใ€ใƒขใƒ‡ใƒซใธใฎ็›ฎ็š„ใฎๅ‡บๅŠ›ใ‚’็คบใ™ใŸใ‚ใซ 1 ใคใฎไพ‹ใ‚’ไฝฟ็”จใ—ใพใ—ใŸใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใฏใ€ +ใ€Œใƒฏใƒณใ‚ทใƒงใƒƒใƒˆใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใ€‚ใŸใ ใ—ใ€ใ‚ฟใ‚นใ‚ฏใฎ่ค‡้›‘ใ•ใซๅฟœใ˜ใฆใ€่ค‡ๆ•ฐใฎไพ‹ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆ•ฐๅ›žใฎใƒ—ใƒญใƒณใƒ—ใƒˆๆ‰‹ๆณ•ใฎๅˆถ้™: +- LLM ใฏไพ‹ใฎใƒ‘ใ‚ฟใƒผใƒณใ‚’็†่งฃใงใใพใ™ใŒใ€ใ“ใ‚Œใ‚‰ใฎๆ‰‹ๆณ•ใฏ่ค‡้›‘ใชๆŽจ่ซ–ใ‚ฟใ‚นใ‚ฏใงใฏใ†ใพใๆฉŸ่ƒฝใ—ใพใ›ใ‚“ใ€‚ +- ๅฐ‘ๆ•ฐใ‚ทใƒงใƒƒใƒˆใฎใƒ—ใƒญใƒณใƒ—ใƒˆใงใฏใ€้•ทใ„ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๅคง้‡ใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๅซใ‚€ใƒ—ใƒญใƒณใƒ—ใƒˆใงใฏใ€่จˆ็ฎ—้‡ใจๅพ…ใกๆ™‚้–“ใŒๅข—ๅŠ ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใฎ้•ทใ•ใซใ‚‚ๅˆถ้™ใŒใ‚ใ‚Šใพใ™ใ€‚ +- ๅคšใใฎไพ‹ใ‚’ไธŽใˆใ‚‹ใจใ€ใƒขใƒ‡ใƒซใŒๅญฆ็ฟ’ใ™ใ‚‹ใคใ‚‚ใ‚Šใฎใชใ‹ใฃใŸใƒ‘ใ‚ฟใƒผใƒณใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ 3็•ช็›ฎใฎๆ˜ ็”ปใƒฌใƒ“ใƒฅใƒผใฏใ„ใคใ‚‚ๅฆๅฎš็š„ใ ใจใ„ใ†ใ“ใจใ€‚ + +### Chain-of-thought + +ๆ€่€ƒ้€ฃ้Ž– (CoT) ใƒ—ใƒญใƒณใƒ—ใƒˆใฏใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆไธญ้–“ๆŽจ่ซ–ใ‚นใƒ†ใƒƒใƒ—ใ‚’็”Ÿๆˆใ—ใ€ๆ”นๅ–„ใ™ใ‚‹ๆ‰‹ๆณ•ใงใ™ใ€‚ +่ค‡้›‘ใชๆŽจ่ซ–ใ‚ฟใ‚นใ‚ฏใฎ็ตๆžœใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ๆ“ไฝœใ—ใฆๆŽจ่ซ–ใ‚นใƒ†ใƒƒใƒ—ใ‚’็”Ÿๆˆใ™ใ‚‹ใซใฏใ€2 ใคใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ +- ่ณชๅ•ใซๅฏพใ™ใ‚‹่ฉณ็ดฐใชๅ›ž็ญ”ใ‚’ๅซใ‚€ไพ‹ใ‚’็คบใ—ใ€ๅ•้กŒใซๅฏพๅ‡ฆใ™ใ‚‹ๆ–นๆณ•ใ‚’ใƒขใƒ‡ใƒซใซ็คบใ™ใ“ใจใงใ€ๆ•ฐๅ›žใฎใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’่กจ็คบใ—ใพใ™ใ€‚ +- ใ€Œใ‚นใƒ†ใƒƒใƒ—ใ”ใจใซ่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€ใพใŸใฏใ€Œๆทฑๅ‘ผๅธใ—ใฆใ€ๅ•้กŒใ‚’ใ‚นใƒ†ใƒƒใƒ—ใ”ใจใซ่งฃๆฑบใ—ใฆใใ ใ•ใ„ใ€ใชใฉใฎใƒ•ใƒฌใƒผใ‚บใ‚’่ฟฝๅŠ ใ—ใฆใƒขใƒ‡ใƒซใซๆŽจ่ซ–ใ‚’ๆŒ‡็คบใ—ใพใ™ใ€‚ + +[ๆŽจ่ซ–ใ‚ปใ‚ฏใ‚ทใƒงใƒณ](#reasoning) ใฎใƒžใƒ•ใ‚ฃใƒณใฎไพ‹ใซ CoT ใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’้ฉ็”จใ—ใ€ใ‚ˆใ‚Šๅคงใใชใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ +[HuggingChat](https://huggingface.co/chat/)ใง้Šในใ‚‹(`tiiuae/falcon-180B-chat`)ใชใฉใ€ +ๆŽจ่ซ–็ตๆžœใฏๅคงๅน…ใซๆ”นๅ–„ใ•ใ‚Œใพใ™ใ€‚ + +```text +Let's go through this step-by-step: +1. You start with 15 muffins. +2. You eat 2 muffins, leaving you with 13 muffins. +3. You give 5 muffins to your neighbor, leaving you with 8 muffins. +4. Your partner buys 6 more muffins, bringing the total number of muffins to 14. +5. Your partner eats 2 muffins, leaving you with 12 muffins. +Therefore, you now have 12 muffins. +``` + +## Prompting vs fine-tuning + +ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ใ“ใจใงๅ„ชใ‚ŒใŸ็ตๆžœใ‚’้”ๆˆใงใใพใ™ใŒใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ‹ใฉใ†ใ‹ใซใคใ„ใฆใฏใพใ ๆ€ๆกˆใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ +ใ‚ใชใŸใฎๅ ดๅˆใซใฏใ‚‚ใฃใจใ†ใพใใ„ใใงใ—ใ‚‡ใ†ใ€‚ใ‚ˆใ‚Šๅฐ่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใ“ใจใŒๅฅฝใพใ—ใ„ใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ‚ใ‚‹ๅ ดๅˆใฎใ„ใใคใ‹ใฎใ‚ทใƒŠใƒชใ‚ชใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +- ใƒ‰ใƒกใ‚คใƒณใŒ LLM ใŒไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ‚‚ใฎใจๅคงใใ็•ฐใชใฃใฆใŠใ‚Šใ€ๅบƒ็ฏ„ใชใƒ—ใƒญใƒณใƒ—ใƒˆๆœ€้ฉๅŒ–ใงใฏๅๅˆ†ใช็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ›ใ‚“ใงใ—ใŸใ€‚ +- ใƒขใƒ‡ใƒซใŒไฝŽใƒชใ‚ฝใƒผใ‚น่จ€่ชžใง้ฉๅˆ‡ใซๅ‹•ไฝœใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ๅŽณๆ ผใช่ฆๅˆถใฎไธ‹ใซใ‚ใ‚‹ๆฉŸๅฏ†ใƒ‡ใƒผใ‚ฟใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +- ใ‚ณใ‚นใƒˆใ€ใƒ—ใƒฉใ‚คใƒใ‚ทใƒผใ€ใ‚คใƒณใƒ•ใƒฉใ‚นใƒˆใƒฉใ‚ฏใƒใƒฃใ€ใพใŸใฏใใฎไป–ใฎๅˆถ้™ใซใ‚ˆใ‚Šใ€ๅฐ่ฆๆจกใชใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ไธŠ่จ˜ใฎใ™ในใฆใฎไพ‹ใงใ€ๅๅˆ†ใชๅคงใใ•ใฎใƒ•ใ‚กใ‚คใƒซใ‚’ใ™ใงใซๆŒใฃใฆใ„ใ‚‹ใ‹ใ€็ฐกๅ˜ใซๅ…ฅๆ‰‹ใงใใ‚‹ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅˆ็†็š„ใชใ‚ณใ‚นใƒˆใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใงใใพใ™ใ€‚ๅๅˆ†ใชๆ™‚้–“ใจใƒชใ‚ฝใƒผใ‚นใ‚‚ๅฟ…่ฆใซใชใ‚Šใพใ™ +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +ไธŠ่จ˜ใฎไพ‹ใŒๅฝ“ใฆใฏใพใ‚‰ใชใ„ๅ ดๅˆใฏใ€ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๆœ€้ฉๅŒ–ใ™ใ‚‹ๆ–นใŒๆœ‰็›Šใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..9217c211e6f973ddfe2d9ca6c3e3ad36bf6cf743 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/question_answering.md @@ -0,0 +1,438 @@ + + +# Question answering + +[[open-in-colab]] + + + +่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใฏใ€่ณชๅ•ใซๅฏพใ—ใฆๅ›ž็ญ”ใ‚’่ฟ”ใ—ใพใ™ใ€‚ Alexaใ€Siriใ€Google ใชใฉใฎไปฎๆƒณใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใซๅคฉๆฐ—ใ‚’ๅฐ‹ใญใŸใ“ใจใŒใ‚ใ‚‹ใชใ‚‰ใ€่ณชๅ•ๅฟœ็ญ”ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใŸใ“ใจใŒใ‚ใ‚‹ใฏใšใงใ™ใ€‚่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใซใฏไธ€่ˆฌ็š„ใซ 2 ใคใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- ๆŠฝๅ‡บ: ไธŽใˆใ‚‰ใ‚ŒใŸใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ๅ›ž็ญ”ใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ +- ๆŠฝ่ฑก็š„: ่ณชๅ•ใซๆญฃใ—ใ็ญ”ใˆใ‚‹ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰ๅ›ž็ญ”ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. ๆŠฝๅ‡บ็š„่ณชๅ•ๅฟœ็ญ”็”จใซ [SQuAD](https://huggingface.co/datasets/squad) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆไธŠใฎ [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/question-answering) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load SQuAD dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ SQuAD ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + + +```py +>>> from datasets import load_dataset + +>>> squad = load_dataset("squad", split="train[:5000]") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train` ๅˆ†ๅ‰ฒใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```py +>>> squad = squad.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> squad["train"][0] +{'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']}, + 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', + 'id': '5733be284776f41900661182', + 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', + 'title': 'University_of_Notre_Dame' +} +``` + +ใ“ใ“ใซใฏใ„ใใคใ‹ใฎ้‡่ฆใชใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒใ‚ใ‚Šใพใ™ใ€‚ + +- `answers`: ๅ›ž็ญ”ใƒˆใƒผใ‚ฏใƒณใจๅ›ž็ญ”ใƒ†ใ‚ญใ‚นใƒˆใฎ้–‹ๅง‹ไฝ็ฝฎใ€‚ +- `context`: ใƒขใƒ‡ใƒซใŒ็ญ”ใˆใ‚’ๆŠฝๅ‡บใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใช่ƒŒๆ™ฏๆƒ…ๅ ฑใ€‚ +- `question`: ใƒขใƒ‡ใƒซใŒ็ญ”ใˆใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹่ณชๅ•ใ€‚ + +## Preprocess + + + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€DistilBERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆ`question`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใจ`context`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใซ็‰นๆœ‰ใฎใ€ๆณจๆ„ใ™ในใๅ‰ๅ‡ฆ็†ๆ‰‹้ †ใŒใ„ใใคใ‹ใ‚ใ‚Šใพใ™ใ€‚ + +1. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎไธ€้ƒจใฎไพ‹ใซใฏใ€ใƒขใƒ‡ใƒซใฎๆœ€ๅคงๅ…ฅๅŠ›้•ทใ‚’่ถ…ใˆใ‚‹้žๅธธใซ้•ทใ„ใ€Œใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ€ใŒๅซใพใ‚Œใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚ˆใ‚Š้•ทใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€`truncation="only_second"` ใ‚’่จญๅฎšใ—ใฆ `context` ใฎใฟใ‚’ๅˆ‡ใ‚Šๆจใฆใพใ™ใ€‚ +2. ๆฌกใซใ€่จญๅฎšใซใ‚ˆใฃใฆใ€ๅ›ž็ญ”ใฎ้–‹ๅง‹ไฝ็ฝฎใจ็ต‚ไบ†ไฝ็ฝฎใ‚’ๅ…ƒใฎ `context`ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ—ใพใ™ใ€‚ + ใ€Œ`return_offset_mapping=True`ใ€ใ€‚ +3. ใƒžใƒƒใƒ”ใƒณใ‚ฐใŒๆ‰‹ๅ…ƒใซใ‚ใ‚‹ใฎใงใ€็ญ”ใˆใฎ้–‹ๅง‹ใƒˆใƒผใ‚ฏใƒณใจ็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ [`~tokenizers.Encoding.sequence_ids`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ + ใ‚ชใƒ•ใ‚ปใƒƒใƒˆใฎใฉใฎ้ƒจๅˆ†ใŒ`question`ใซๅฏพๅฟœใ—ใ€ใฉใฎ้ƒจๅˆ†ใŒ`context`ใซๅฏพๅฟœใ™ใ‚‹ใ‹ใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + +ไปฅไธ‹ใซใ€`answer`ใฎ้–‹ๅง‹ใƒˆใƒผใ‚ฏใƒณใจ็ต‚ไบ†ใƒˆใƒผใ‚ฏใƒณใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใฆ`context`ใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ + +```py +>>> def preprocess_function(examples): +... questions = [q.strip() for q in examples["question"]] +... inputs = tokenizer( +... questions, +... examples["context"], +... max_length=384, +... truncation="only_second", +... return_offsets_mapping=True, +... padding="max_length", +... ) + +... offset_mapping = inputs.pop("offset_mapping") +... answers = examples["answers"] +... start_positions = [] +... end_positions = [] + +... for i, offset in enumerate(offset_mapping): +... answer = answers[i] +... start_char = answer["answer_start"][0] +... end_char = answer["answer_start"][0] + len(answer["text"][0]) +... sequence_ids = inputs.sequence_ids(i) + +... # Find the start and end of the context +... idx = 0 +... while sequence_ids[idx] != 1: +... idx += 1 +... context_start = idx +... while sequence_ids[idx] == 1: +... idx += 1 +... context_end = idx - 1 + +... # If the answer is not fully inside the context, label it (0, 0) +... if offset[context_start][0] > end_char or offset[context_end][1] < start_char: +... start_positions.append(0) +... end_positions.append(0) +... else: +... # Otherwise it's the start and end token positions +... idx = context_start +... while idx <= context_end and offset[idx][0] <= start_char: +... idx += 1 +... start_positions.append(idx - 1) + +... idx = context_end +... while idx >= context_start and offset[idx][1] >= end_char: +... idx -= 1 +... end_positions.append(idx + 1) + +... inputs["start_positions"] = start_positions +... inputs["end_positions"] = end_positions +... return inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ไธ่ฆใชๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> tokenized_squad = squad.map(preprocess_function, batched=True, remove_columns=squad["train"].column_names) +``` + +ๆฌกใซใ€[`DefaultDataCollatโ€‹โ€‹or`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ๐Ÿค— Transformers ใฎไป–ใฎใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใฏ็•ฐใชใ‚Šใ€[`DefaultDataCollatโ€‹โ€‹or`] ใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใชใฉใฎ่ฟฝๅŠ ใฎๅ‰ๅ‡ฆ็†ใ‚’้ฉ็”จใ—ใพใ›ใ‚“ใ€‚ + + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForQuestionAnswering`] ใ‚’ไฝฟ็”จใ—ใฆ DitilBERT ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer + +>>> model = AutoModelForQuestionAnswering.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_qa_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_squad["train"], +... eval_dataset=tokenized_squad["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 2 +>>> total_train_steps = (len(tokenized_squad["train"]) // batch_size) * num_epochs +>>> optimizer, schedule = create_optimizer( +... init_lr=2e-5, +... num_warmup_steps=0, +... num_train_steps=total_train_steps, +... ) +``` + +ๆฌกใซใ€[`TFAutoModelForQuestionAnswering`] ใ‚’ไฝฟ็”จใ—ใฆ DistilBERT ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForQuestionAnswering + +>>> model = TFAutoModelForQuestionAnswering("distilbert/distilbert-base-uncased") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_squad["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_squad["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซๆœ€ๅพŒใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใฏใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ [`~transformers.PushToHubCallback`] ใงใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใงๅฎŸ่กŒใงใใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_qa_model", +... tokenizer=tokenizer, +... ) +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=[callback]) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + +่ณชๅ•ๅฟœ็ญ”็”จใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)ใ€‚ + + + +## Evaluate + +่ณชๅ•ๅฟœ็ญ”ใฎ่ฉ•ไพกใซใฏใ€ๅคง้‡ใฎๅพŒๅ‡ฆ็†ใŒๅฟ…่ฆใงใ™ใ€‚ๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใ™ใŽใชใ„ใ‚ˆใ†ใซใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏ่ฉ•ไพกใ‚นใƒ†ใƒƒใƒ—ใ‚’็œ็•ฅใ—ใฆใ„ใพใ™ใ€‚ [`Trainer`] ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ่ฉ•ไพกๆๅคฑใ‚’่จˆ็ฎ—ใ™ใ‚‹ใŸใ‚ใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใซใคใ„ใฆๅฎŒๅ…จใซๅˆ†ใ‹ใ‚‰ใชใ„ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ‚‚ใฃใจๆ™‚้–“ใŒใ‚ใ‚Šใ€่ณชๅ•ๅฟœ็ญ”็”จใฎใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ™ใ‚‹ๆ–นๆณ•ใซ่ˆˆๅ‘ณใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€[่ณชๅ•ๅฟœ็ญ”](https://huggingface.co/course/chapter7/7?fw=pt#postprocessing) ใฎ็ซ ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ ๐Ÿค—ใƒใ‚ฐใƒ•ใ‚งใ‚คใ‚นใ‚ณใƒผใ‚นใ‹ใ‚‰๏ผ + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +่ณชๅ•ใจใ€ใƒขใƒ‡ใƒซใซไบˆๆธฌใ•ใ›ใŸใ„ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒใˆๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> question = "How many programming languages does BLOOM support?" +>>> context = "BLOOM has 176 billion parameters and can generate text in 46 languages natural languages and 13 programming languages." +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ณชๅ•ๅฟœ็ญ”็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> question_answerer = pipeline("question-answering", model="my_awesome_qa_model") +>>> question_answerer(question=question, context=context) +{'score': 0.2058267742395401, + 'start': 10, + 'end': 95, + 'answer': '176 billion parameters and can generate text in 46 languages natural languages and 13'} +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใฆ PyTorch ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") +>>> inputs = tokenizer(question, context, return_tensors="pt") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +```py +>>> import torch +>>> from transformers import AutoModelForQuestionAnswering + +>>> model = AutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") +>>> with torch.no_grad(): +... outputs = model(**inputs) +``` + +ใƒขใƒ‡ใƒซๅ‡บๅŠ›ใ‹ใ‚‰้–‹ๅง‹ไฝ็ฝฎใจ็ต‚ไบ†ไฝ็ฝฎใฎๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> answer_start_index = outputs.start_logits.argmax() +>>> answer_end_index = outputs.end_logits.argmax() +``` + +ไบˆๆธฌใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆ็ญ”ใˆใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] +>>> tokenizer.decode(predict_answer_tokens) +'176 billion parameters and can generate text in 46 languages natural languages and 13' +``` + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€TensorFlow ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model") +>>> inputs = tokenizer(question, text, return_tensors="tf") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import TFAutoModelForQuestionAnswering + +>>> model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model") +>>> outputs = model(**inputs) +``` + +ใƒขใƒ‡ใƒซๅ‡บๅŠ›ใ‹ใ‚‰้–‹ๅง‹ไฝ็ฝฎใจ็ต‚ไบ†ไฝ็ฝฎใฎๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0]) +>>> answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0]) +``` + +ไบˆๆธฌใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆ็ญ”ใˆใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1] +>>> tokenizer.decode(predict_answer_tokens) +'176 billion parameters and can generate text in 46 languages natural languages and 13' +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/semantic_segmentation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/semantic_segmentation.md new file mode 100644 index 0000000000000000000000000000000000000000..cfbfd7b81c0193acc0db6c261d4425f48897d4e1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/semantic_segmentation.md @@ -0,0 +1,600 @@ + + +# Semantic segmentation + +[[open-in-colab]] + + + +ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใงใฏใ€็”ปๅƒใฎๅ€‹ใ€…ใฎใƒ”ใ‚ฏใ‚ปใƒซใซใƒฉใƒ™ใƒซใพใŸใฏใ‚ฏใƒฉใ‚นใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใซใฏใ„ใใคใ‹ใฎใ‚ฟใ‚คใƒ—ใŒใ‚ใ‚Šใพใ™ใŒใ€ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎๅ ดๅˆใ€ๅŒใ˜ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎไธ€ๆ„ใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚น้–“ใฎๅŒบๅˆฅใฏ่กŒใ‚ใ‚Œใพใ›ใ‚“ใ€‚ไธกๆ–นใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซๅŒใ˜ใƒฉใƒ™ใƒซใŒไป˜ใ‘ใ‚‰ใ‚Œใพใ™ (ใŸใจใˆใฐใ€`car-1`ใจ`car-2`ใฎไปฃใ‚ใ‚Šใซ`car`)ใ€‚ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏ ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎไธ€่ˆฌ็š„ใช็พๅฎŸไธ–็•Œใฎใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซใฏใ€ๆญฉ่กŒ่€…ใ‚„้‡่ฆใชไบค้€šๆƒ…ๅ ฑใ‚’่ญ˜ๅˆฅใ™ใ‚‹ใŸใ‚ใฎ่‡ชๅ‹•้‹่ปข่ปŠใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ€ๅŒป็™‚็”ปๅƒๅ†…ใฎ็ดฐ่ƒžใจ็•ฐๅธธใฎ่ญ˜ๅˆฅใ€่ก›ๆ˜Ÿ็”ปๅƒใ‹ใ‚‰ใฎ็’ฐๅขƒๅค‰ๅŒ–ใฎ็›ฃ่ฆ–ใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [SceneParse150](https://huggingface.co/datasets/scene_parse_150) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ [SegFormer](https://huggingface.co/docs/transformers/main/en/model_doc/segformer#segformer) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/image-segmentation) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q datasets transformers evaluate +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load SceneParse150 dataset + +ใพใšใ€SceneParse150 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ ๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰่ชญใฟ่พผใฟใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> ds = load_dataset("scene_parse_150", split="train[:50]") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ `train` ๅˆ†ๅ‰ฒใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + + +```py +>>> ds = ds.train_test_split(test_size=0.2) +>>> train_ds = ds["train"] +>>> test_ds = ds["test"] +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> train_ds[0] +{'image': , + 'annotation': , + 'scene_category': 368} +``` + +- `image`: ใ‚ทใƒผใƒณใฎ PIL ใ‚คใƒกใƒผใ‚ธใ€‚ +- `annotation`: ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒžใƒƒใƒ—ใฎ PIL ใ‚คใƒกใƒผใ‚ธใ€‚ใƒขใƒ‡ใƒซใฎใ‚ฟใƒผใ‚ฒใƒƒใƒˆใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ +- `scene_category`: "kitchen"ใ‚„"office"ใชใฉใฎ็”ปๅƒใ‚ทใƒผใƒณใ‚’่ชฌๆ˜Žใ™ใ‚‹ใ‚ซใƒ†ใ‚ดใƒช IDใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€`image`ใจ`annotation`ใฎใฟใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ใฉใกใ‚‰ใ‚‚ PIL ใ‚คใƒกใƒผใ‚ธใงใ™ใ€‚ + +ใพใŸใ€ใƒฉใƒ™ใƒซ ID ใ‚’ใƒฉใƒ™ใƒซ ใ‚ฏใƒฉใ‚นใซใƒžใƒƒใƒ—ใ™ใ‚‹่พžๆ›ธใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅพŒใงใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ™ใ‚‹ใจใใซๅฝน็ซ‹ใกใพใ™ใ€‚ใƒใƒ–ใ‹ใ‚‰ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใ€`id2label` ใŠใ‚ˆใณ `label2id` ใƒ‡ใ‚ฃใ‚ฏใ‚ทใƒงใƒŠใƒชใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import json +>>> from pathlib import Path +>>> from huggingface_hub import hf_hub_download + +>>> repo_id = "huggingface/label-files" +>>> filename = "ade20k-id2label.json" +>>> id2label = json.loads(Path(hf_hub_download(repo_id, filename, repo_type="dataset")).read_text()) +>>> id2label = {int(k): v for k, v in id2label.items()} +>>> label2id = {v: k for k, v in id2label.items()} +>>> num_labels = len(id2label) +``` + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€SegFormer ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใจๆณจ้‡ˆใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ใ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ˆใ†ใชไธ€้ƒจใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใ€ใƒใƒƒใ‚ฏใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ ใ‚ฏใƒฉใ‚นใจใ—ใฆใ‚ผใƒญใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ๅฎŸ้š›ใซใฏ่ƒŒๆ™ฏใ‚ฏใƒฉใ‚นใฏ 150 ๅ€‹ใฎใ‚ฏใƒฉใ‚นใซๅซใพใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€`do_reduce_labels=True`ใ‚’่จญๅฎšใ—ใฆใ™ในใฆใฎใƒฉใƒ™ใƒซใ‹ใ‚‰ 1 ใคใ‚’ๅผ•ใๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚ผใƒญใ‚คใƒณใƒ‡ใƒƒใ‚ฏใ‚นใฏ `255` ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใ‚‹ใŸใ‚ใ€SegFormer ใฎๆๅคฑ้–ขๆ•ฐใซใ‚ˆใฃใฆ็„ก่ฆ–ใ•ใ‚Œใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "nvidia/mit-b0" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint, do_reduce_labels=True) +``` + + + + +ใƒขใƒ‡ใƒซใ‚’้Žๅญฆ็ฟ’ใซๅฏพใ—ใฆใ‚ˆใ‚Šๅ …็‰ขใซใ™ใ‚‹ใŸใ‚ใซใ€็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใ„ใใคใ‹ใฎใƒ‡ใƒผใ‚ฟๆ‹กๅผตใ‚’้ฉ็”จใ™ใ‚‹ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€[torchvision](https://pytorch.org/vision/stable/index.html) ใฎ [`ColorJitter`](https://pytorch.org/vision/stable/generated/torchvision.transforms.ColorJitter.html) ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ ) ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใฎ่‰ฒใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ใƒฉใƒณใƒ€ใƒ ใซๅค‰ๆ›ดใ—ใพใ™ใŒใ€ไปปๆ„ใฎ็”ปๅƒใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +>>> from torchvision.transforms import ColorJitter + +>>> jitter = ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25, hue=0.1) +``` + +ๆฌกใซใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใจๆณจ้‡ˆใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ใŸใ‚ใฎ 2 ใคใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎ้–ขๆ•ฐใฏใ€็”ปๅƒใ‚’`pixel_values`ใซๅค‰ๆ›ใ—ใ€ๆณจ้‡ˆใ‚’`labels`ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใฎๅ ดๅˆใ€็”ปๅƒใ‚’็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใซๆไพ›ใ™ใ‚‹ๅ‰ใซ `jitter` ใŒ้ฉ็”จใ•ใ‚Œใพใ™ใ€‚ใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใฎๅ ดๅˆใ€ใƒ†ใ‚นใƒˆไธญใซใƒ‡ใƒผใ‚ฟๆ‹กๅผตใŒ้ฉ็”จใ•ใ‚Œใชใ„ใŸใ‚ใ€็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏ`images`ใ‚’ๅˆ‡ใ‚Šๅ–ใฃใฆๆญฃ่ฆๅŒ–ใ—ใ€`ใƒฉใƒ™ใƒซ`ใฎใฟใ‚’ๅˆ‡ใ‚Šๅ–ใ‚Šใพใ™ใ€‚ + +```py +>>> def train_transforms(example_batch): +... images = [jitter(x) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs + + +>>> def val_transforms(example_batch): +... images = [x for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ`jitter`ใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.set_transform`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅค‰ๆ›ใฏใ‚ชใƒณใ‚ถใƒ•ใƒฉใ‚คใง้ฉ็”จใ•ใ‚Œใ‚‹ใŸใ‚ใ€้ซ˜้€Ÿใงๆถˆ่ฒปใ™ใ‚‹ใƒ‡ใ‚ฃใ‚นใ‚ฏๅฎน้‡ใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ + +```py +>>> train_ds.set_transform(train_transforms) +>>> test_ds.set_transform(val_transforms) +``` + + + + + + + +ใƒขใƒ‡ใƒซใ‚’้Žๅญฆ็ฟ’ใซๅฏพใ—ใฆใ‚ˆใ‚Šๅ …็‰ขใซใ™ใ‚‹ใŸใ‚ใซใ€็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใ„ใใคใ‹ใฎใƒ‡ใƒผใ‚ฟๆ‹กๅผตใ‚’้ฉ็”จใ™ใ‚‹ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€[`tf.image`](https://www.tensorflow.org/api_docs/python/tf/image) ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใฎ่‰ฒใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ใƒฉใƒณใƒ€ใƒ ใซๅค‰ๆ›ดใ—ใพใ™ใŒใ€ไปปๆ„ใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚็”ปๅƒ +ๅฅฝใใชๅ›ณๆ›ธ้คจใ€‚ +2 ใคใฎๅˆฅใ€…ใฎๅค‰ๆ›้–ขๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ +- ็”ปๅƒๆ‹กๅผตใ‚’ๅซใ‚€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟๅค‰ๆ› +- ๐Ÿค— Transformers ใฎใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒผ ใƒ“ใ‚ธใƒงใƒณ ใƒขใƒ‡ใƒซใฏใƒใƒฃใƒใƒซๅ„ชๅ…ˆใฎใƒฌใ‚คใ‚ขใ‚ฆใƒˆใ‚’ๆƒณๅฎšใ—ใฆใ„ใ‚‹ใŸใ‚ใ€็”ปๅƒใ‚’่ปข็ฝฎใ™ใ‚‹ใ ใ‘ใฎๆคœ่จผใƒ‡ใƒผใ‚ฟๅค‰ๆ› + +```py +>>> import tensorflow as tf + + +>>> def aug_transforms(image): +... image = tf.keras.utils.img_to_array(image) +... image = tf.image.random_brightness(image, 0.25) +... image = tf.image.random_contrast(image, 0.5, 2.0) +... image = tf.image.random_saturation(image, 0.75, 1.25) +... image = tf.image.random_hue(image, 0.1) +... image = tf.transpose(image, (2, 0, 1)) +... return image + + +>>> def transforms(image): +... image = tf.keras.utils.img_to_array(image) +... image = tf.transpose(image, (2, 0, 1)) +... return image +``` + +ๆฌกใซใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใจๆณจ้‡ˆใฎใƒใƒƒใƒใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ 2 ใคใฎๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๆฉŸ่ƒฝใŒ้ฉ็”จใ•ใ‚Œใพใ™ +็”ปๅƒๅค‰ๆ›ใ‚’่กŒใ„ใ€ไปฅๅ‰ใซใƒญใƒผใƒ‰ใ•ใ‚ŒใŸ `image_processor` ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‚’ `pixel_values` ใซๅค‰ๆ›ใ—ใ€ +`labels`ใธใฎๆณจ้‡ˆใ€‚ `ImageProcessor` ใฏใ€็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใจๆญฃ่ฆๅŒ–ใ‚‚ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> def train_transforms(example_batch): +... images = [aug_transforms(x.convert("RGB")) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs + + +>>> def val_transforms(example_batch): +... images = [transforms(x.convert("RGB")) for x in example_batch["image"]] +... labels = [x for x in example_batch["annotation"]] +... inputs = image_processor(images, labels) +... return inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†ๅค‰ๆ›ใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.set_transform`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ๅค‰ๆ›ใฏใ‚ชใƒณใ‚ถใƒ•ใƒฉใ‚คใง้ฉ็”จใ•ใ‚Œใ‚‹ใŸใ‚ใ€้ซ˜้€Ÿใงๆถˆ่ฒปใ™ใ‚‹ใƒ‡ใ‚ฃใ‚นใ‚ฏๅฎน้‡ใŒๅฐ‘ใชใใชใ‚Šใพใ™ใ€‚ + +```py +>>> train_ds.set_transform(train_transforms) +>>> test_ds.set_transform(val_transforms) +``` + + + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[Mean Intersection over Union](https://huggingface.co/spaces/evaluate-metric/accuracy) (IoU) ใƒกใƒˆใƒชใƒƒใ‚ฏใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใ€ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่จˆ็ฎ—ใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„)ใ€‚ + +```py +>>> import evaluate + +>>> metric = evaluate.load("mean_iou") +``` + +ๆฌกใซใ€ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ [`~evaluate.EvaluationModule.compute`] ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ไบˆๆธฌใ‚’ๆฌกใฎใ‚ˆใ†ใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ +ๆœ€ๅˆใซใƒญใ‚ธใƒƒใƒˆใ‚’ไฝœๆˆใ—ใ€ๆฌกใซ [`~evaluate.EvaluationModule.compute`] ใ‚’ๅ‘ผใณๅ‡บใ™ๅ‰ใซใƒฉใƒ™ใƒซใฎใ‚ตใ‚คใ‚บใซไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซๅ†ๅฝขๆˆใ—ใพใ™ใ€‚ + + + + +```py +>>> import numpy as np +>>> import torch +>>> from torch import nn + +>>> def compute_metrics(eval_pred): +... with torch.no_grad(): +... logits, labels = eval_pred +... logits_tensor = torch.from_numpy(logits) +... logits_tensor = nn.functional.interpolate( +... logits_tensor, +... size=labels.shape[-2:], +... mode="bilinear", +... align_corners=False, +... ).argmax(dim=1) + +... pred_labels = logits_tensor.detach().cpu().numpy() +... metrics = metric.compute( +... predictions=pred_labels, +... references=labels, +... num_labels=num_labels, +... ignore_index=255, +... reduce_labels=False, +... ) +... for key, value in metrics.items(): +... if type(value) is np.ndarray: +... metrics[key] = value.tolist() +... return metrics +``` + + + + + + + + +```py +>>> def compute_metrics(eval_pred): +... logits, labels = eval_pred +... logits = tf.transpose(logits, perm=[0, 2, 3, 1]) +... logits_resized = tf.image.resize( +... logits, +... size=tf.shape(labels)[1:], +... method="bilinear", +... ) + +... pred_labels = tf.argmax(logits_resized, axis=-1) +... metrics = metric.compute( +... predictions=pred_labels, +... references=labels, +... num_labels=num_labels, +... ignore_index=-1, +... reduce_labels=image_processor.do_reduce_labels, +... ) + +... per_category_accuracy = metrics.pop("per_category_accuracy").tolist() +... per_category_iou = metrics.pop("per_category_iou").tolist() + +... metrics.update({f"accuracy_{id2label[i]}": v for i, v in enumerate(per_category_accuracy)}) +... metrics.update({f"iou_{id2label[i]}": v for i, v in enumerate(per_category_iou)}) +... return {"val_" + k: v for k, v in metrics.items()} +``` + + + + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#finetune-with-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForSemanticSegmentation`] ใ‚’ไฝฟ็”จใ—ใฆ SegFormer ใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใƒฉใƒ™ใƒซ ID ใจใƒฉใƒ™ใƒซ ใ‚ฏใƒฉใ‚น้–“ใฎใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForSemanticSegmentation, TrainingArguments, Trainer + +>>> model = AutoModelForSemanticSegmentation.from_pretrained(checkpoint, id2label=id2label, label2id=label2id) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ `image` ๅˆ—ใŒๅ‰Š้™คใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๆœชไฝฟ็”จใฎๅˆ—ใ‚’ๅ‰Š้™คใ—ใชใ„ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ `image` ๅˆ—ใŒใชใ„ใจใ€`pixel_values` ใ‚’ไฝœๆˆใงใใพใ›ใ‚“ใ€‚ใ“ใฎๅ‹•ไฝœใ‚’้˜ฒใใซใฏใ€`remove_unused_columns=False`ใ‚’่จญๅฎšใ—ใฆใใ ใ•ใ„ใ€‚ไป–ใซๅฟ…่ฆใชใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใ ใ‘ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ IoU ใƒกใƒˆใƒชใƒƒใ‚ฏใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="segformer-b0-scene-parse-150", +... learning_rate=6e-5, +... num_train_epochs=50, +... per_device_train_batch_size=2, +... per_device_eval_batch_size=2, +... save_total_limit=3, +... eval_strategy="steps", +... save_strategy="steps", +... save_steps=20, +... eval_steps=20, +... logging_steps=1, +... eval_accumulation_steps=5, +... remove_unused_columns=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=train_ds, +... eval_dataset=test_ds, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€ใพใš [ๅŸบๆœฌใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](./training#train-a-tensorflow-model-with-keras) ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆ‰‹้ †ใซๅพ“ใ„ใพใ™ใ€‚ +1. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใจๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ +2. ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ +3. ๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ +4. ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใพใ™ใ€‚ +5. ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’่ฟฝๅŠ ใ—ใฆใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— Hub ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ +6. `fit()` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +ใพใšใ€ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 2 +>>> num_epochs = 50 +>>> num_train_steps = len(train_ds) * num_epochs +>>> learning_rate = 6e-5 +>>> weight_decay_rate = 0.01 + +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=learning_rate, +... num_train_steps=num_train_steps, +... weight_decay_rate=weight_decay_rate, +... num_warmup_steps=0, +... ) +``` + +ๆฌกใซใ€ใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใจใจใ‚‚ใซ [`TFAutoModelForSemanticSegmentation`] ใ‚’ไฝฟ็”จใ—ใฆ SegFormer ใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใใ‚Œใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใพใ™ใ€‚ +ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import TFAutoModelForSemanticSegmentation + +>>> model = TFAutoModelForSemanticSegmentation.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ) +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +[`~datasets.Dataset.to_tf_dataset`] ใจ [`DefaultDataCollatโ€‹โ€‹or`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") + +>>> tf_train_dataset = train_ds.to_tf_dataset( +... columns=["pixel_values", "label"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) + +>>> tf_eval_dataset = test_ds.to_tf_dataset( +... columns=["pixel_values", "label"], +... shuffle=True, +... batch_size=batch_size, +... collate_fn=data_collator, +... ) +``` + +ไบˆๆธฌใ‹ใ‚‰็ฒพๅบฆใ‚’่จˆ็ฎ—ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ใซใฏใ€[Keras callbacks](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +`compute_metrics` ้–ขๆ•ฐใ‚’ [`KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ +ใใ—ใฆ [`PushToHubCallback`] ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback + +>>> metric_callback = KerasMetricCallback( +... metric_fn=compute_metrics, eval_dataset=tf_eval_dataset, batch_size=batch_size, label_cols=["labels"] +... ) + +>>> push_to_hub_callback = PushToHubCallback(output_dir="scene_segmentation", tokenizer=image_processor) + +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใŸใ‚ใฎใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ: + + +```py +>>> model.fit( +... tf_train_dataset, +... validation_data=tf_eval_dataset, +... callbacks=callbacks, +... epochs=num_epochs, +... ) +``` + +ใŠใ‚ใงใจใ†๏ผใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€๐Ÿค— Hub ใงๅ…ฑๆœ‰ใ—ใพใ—ใŸใ€‚ใ“ใ‚ŒใงๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใฎใŸใ‚ใซ็”ปๅƒใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> image = ds[0]["image"] +>>> image +``` + +
+ Image of bedroom +
+ + + + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ็”จใฎ `pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซ็”ปๅƒใ‚’ๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> segmenter = pipeline("image-segmentation", model="my_awesome_seg_model") +>>> segmenter(image) +[{'score': None, + 'label': 'wall', + 'mask': }, + {'score': None, + 'label': 'sky', + 'mask': }, + {'score': None, + 'label': 'floor', + 'mask': }, + {'score': None, + 'label': 'ceiling', + 'mask': }, + {'score': None, + 'label': 'bed ', + 'mask': }, + {'score': None, + 'label': 'windowpane', + 'mask': }, + {'score': None, + 'label': 'cabinet', + 'mask': }, + {'score': None, + 'label': 'chair', + 'mask': }, + {'score': None, + 'label': 'armchair', + 'mask': }] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚็”ปๅƒใ‚’็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใงๅ‡ฆ็†ใ—ใ€`pixel_values` ใ‚’ GPU ใซ้…็ฝฎใ—ใพใ™ใ€‚ + +```py +>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # use GPU if available, otherwise use a CPU +>>> encoding = image_processor(image, return_tensors="pt") +>>> pixel_values = encoding.pixel_values.to(device) +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +```py +>>> outputs = model(pixel_values=pixel_values) +>>> logits = outputs.logits.cpu() +``` + +ๆฌกใซใ€ใƒญใ‚ธใƒƒใƒˆใ‚’ๅ…ƒใฎ็”ปๅƒใ‚ตใ‚คใ‚บใซๅ†ใ‚นใ‚ฑใƒผใƒซใ—ใพใ™ใ€‚ + +```py +>>> upsampled_logits = nn.functional.interpolate( +... logits, +... size=image.size[::-1], +... mode="bilinear", +... align_corners=False, +... ) + +>>> pred_seg = upsampled_logits.argmax(dim=1)[0] +``` + + + + + + + +็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€ๅ…ฅๅŠ›ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("MariaK/scene_segmentation") +>>> inputs = image_processor(image, return_tensors="tf") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForSemanticSegmentation + +>>> model = TFAutoModelForSemanticSegmentation.from_pretrained("MariaK/scene_segmentation") +>>> logits = model(**inputs).logits +``` + +ๆฌกใซใ€ใƒญใ‚ธใƒƒใƒˆใ‚’ๅ…ƒใฎ็”ปๅƒใ‚ตใ‚คใ‚บใซๅ†ใ‚นใ‚ฑใƒผใƒซใ—ใ€ใ‚ฏใƒฉใ‚นๆฌกๅ…ƒใซ argmax ใ‚’้ฉ็”จใ—ใพใ™ใ€‚ + +```py +>>> logits = tf.transpose(logits, [0, 2, 3, 1]) + +>>> upsampled_logits = tf.image.resize( +... logits, +... # We reverse the shape of `image` because `image.size` returns width and height. +... image.size[::-1], +... ) + +>>> pred_seg = tf.math.argmax(upsampled_logits, axis=-1)[0] +``` + + + + +็ตๆžœใ‚’่ฆ–่ฆšๅŒ–ใ™ใ‚‹ใซใฏใ€[ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใ‚ซใƒฉใƒผ ใƒ‘ใƒฌใƒƒใƒˆ](https://github.com/tensorflow/models/blob/3f1ca33afe3c1631b733ea7e40c294273b9e406d/research/deeplab/utils/get_dataset_colormap.py#L51) ใ‚’ใ€ใใ‚Œใžใ‚Œใ‚’ใƒžใƒƒใƒ—ใ™ใ‚‹ `ade_palette()` ใจใ—ใฆใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ‚ฏใƒฉใ‚นใ‚’ RGB ๅ€คใซๅค‰ๆ›ใ—ใพใ™ใ€‚ๆฌกใซใ€็”ปๅƒใจไบˆๆธฌใ•ใ‚ŒใŸใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ ใƒžใƒƒใƒ—ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใƒ—ใƒญใƒƒใƒˆใงใใพใ™ใ€‚ + +```py +>>> import matplotlib.pyplot as plt +>>> import numpy as np + +>>> color_seg = np.zeros((pred_seg.shape[0], pred_seg.shape[1], 3), dtype=np.uint8) +>>> palette = np.array(ade_palette()) +>>> for label, color in enumerate(palette): +... color_seg[pred_seg == label, :] = color +>>> color_seg = color_seg[..., ::-1] # convert to BGR + +>>> img = np.array(image) * 0.5 + color_seg * 0.5 # plot the image with the segmentation map +>>> img = img.astype(np.uint8) + +>>> plt.figure(figsize=(15, 10)) +>>> plt.imshow(img) +>>> plt.show() +``` + +
+ Image of bedroom overlaid with segmentation map +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/summarization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/summarization.md new file mode 100644 index 0000000000000000000000000000000000000000..6784696e6c95a3bdc3abe661236ad540b3304bfc --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/summarization.md @@ -0,0 +1,404 @@ + + +# Summarization + +[[open-in-colab]] + + + +่ฆ็ด„ใซใ‚ˆใ‚Šใ€ใ™ในใฆใฎ้‡่ฆใชๆƒ…ๅ ฑใ‚’ใพใจใ‚ใŸ็Ÿญใ„ใƒใƒผใ‚ธใƒงใƒณใฎๆ–‡ๆ›ธใพใŸใฏ่จ˜ไบ‹ใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏใ€็ฟป่จณใจไธฆใ‚“ใงใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ใฎใ‚ฟใ‚นใ‚ฏใจใ—ใฆๅฎšๅผๅŒ–ใงใใ‚‹ใ‚ฟใ‚นใ‚ฏใฎใ‚‚ใ† 1 ใคใฎไพ‹ใงใ™ใ€‚่ฆ็ด„ใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +- ๆŠฝๅ‡บ: ๆ–‡ๆ›ธใ‹ใ‚‰ๆœ€ใ‚‚้–ข้€ฃๆ€งใฎ้ซ˜ใ„ๆƒ…ๅ ฑใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ +- ๆŠฝ่ฑก็š„: ๆœ€ใ‚‚้–ข้€ฃๆ€งใฎ้ซ˜ใ„ๆƒ…ๅ ฑใ‚’ๆ‰ใˆใŸๆ–ฐใ—ใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. ๆŠฝ่ฑก็š„ใช่ฆ็ด„ใฎใŸใ‚ใซใ€[BillSum](https://huggingface.co/datasets/billsum) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ‚ซใƒชใƒ•ใ‚ฉใƒซใƒ‹ใ‚ขๅทž่ซ‹ๆฑ‚ๆ›ธใ‚ตใƒ–ใ‚ปใƒƒใƒˆใง [T5](https://huggingface.co/google-t5/t5-small) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/summarization) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate rouge_score +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load BillSum dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ BillSum ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใ„ใ‚ซใƒชใƒ•ใ‚ฉใƒซใƒ‹ใ‚ขๅทž่ซ‹ๆฑ‚ๆ›ธใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> billsum = load_dataset("billsum", split="ca_test") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + +```py +>>> billsum = billsum.train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> billsum["train"][0] +{'summary': 'Existing law authorizes state agencies to enter into contracts for the acquisition of goods or services upon approval by the Department of General Services. Existing law sets forth various requirements and prohibitions for those contracts, including, but not limited to, a prohibition on entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between spouses and domestic partners or same-sex and different-sex couples in the provision of benefits. Existing law provides that a contract entered into in violation of those requirements and prohibitions is void and authorizes the state or any person acting on behalf of the state to bring a civil action seeking a determination that a contract is in violation and therefore void. Under existing law, a willful violation of those requirements and prohibitions is a misdemeanor.\nThis bill would also prohibit a state agency from entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between employees on the basis of gender identity in the provision of benefits, as specified. By expanding the scope of a crime, this bill would impose a state-mandated local program.\nThe California Constitution requires the state to reimburse local agencies and school districts for certain costs mandated by the state. Statutory provisions establish procedures for making that reimbursement.\nThis bill would provide that no reimbursement is required by this act for a specified reason.', + 'text': 'The people of the State of California do enact as follows:\n\n\nSECTION 1.\nSection 10295.35 is added to the Public Contract Code, to read:\n10295.35.\n(a) (1) Notwithstanding any other law, a state agency shall not enter into any contract for the acquisition of goods or services in the amount of one hundred thousand dollars ($100,000) or more with a contractor that, in the provision of benefits, discriminates between employees on the basis of an employeeโ€™s or dependentโ€™s actual or perceived gender identity, including, but not limited to, the employeeโ€™s or dependentโ€™s identification as transgender.\n(2) For purposes of this section, โ€œcontractโ€ includes contracts with a cumulative amount of one hundred thousand dollars ($100,000) or more per contractor in each fiscal year.\n(3) For purposes of this section, an employee health plan is discriminatory if the plan is not consistent with Section 1365.5 of the Health and Safety Code and Section 10140 of the Insurance Code.\n(4) The requirements of this section shall apply only to those portions of a contractorโ€™s operations that occur under any of the following conditions:\n(A) Within the state.\n(B) On real property outside the state if the property is owned by the state or if the state has a right to occupy the property, and if the contractorโ€™s presence at that location is connected to a contract with the state.\n(C) Elsewhere in the United States where work related to a state contract is being performed.\n(b) Contractors shall treat as confidential, to the maximum extent allowed by law or by the requirement of the contractorโ€™s insurance provider, any request by an employee or applicant for employment benefits or any documentation of eligibility for benefits submitted by an employee or applicant for employment.\n(c) After taking all reasonable measures to find a contractor that complies with this section, as determined by the state agency, the requirements of this section may be waived under any of the following circumstances:\n(1) There is only one prospective contractor willing to enter into a specific contract with the state agency.\n(2) The contract is necessary to respond to an emergency, as determined by the state agency, that endangers the public health, welfare, or safety, or the contract is necessary for the provision of essential services, and no entity that complies with the requirements of this section capable of responding to the emergency is immediately available.\n(3) The requirements of this section violate, or are inconsistent with, the terms or conditions of a grant, subvention, or agreement, if the agency has made a good faith attempt to change the terms or conditions of any grant, subvention, or agreement to authorize application of this section.\n(4) The contractor is providing wholesale or bulk water, power, or natural gas, the conveyance or transmission of the same, or ancillary services, as required for ensuring reliable services in accordance with good utility practice, if the purchase of the same cannot practically be accomplished through the standard competitive bidding procedures and the contractor is not providing direct retail services to end users.\n(d) (1) A contractor shall not be deemed to discriminate in the provision of benefits if the contractor, in providing the benefits, pays the actual costs incurred in obtaining the benefit.\n(2) If a contractor is unable to provide a certain benefit, despite taking reasonable measures to do so, the contractor shall not be deemed to discriminate in the provision of benefits.\n(e) (1) Every contract subject to this chapter shall contain a statement by which the contractor certifies that the contractor is in compliance with this section.\n(2) The department or other contracting agency shall enforce this section pursuant to its existing enforcement powers.\n(3) (A) If a contractor falsely certifies that it is in compliance with this section, the contract with that contractor shall be subject to Article 9 (commencing with Section 10420), unless, within a time period specified by the department or other contracting agency, the contractor provides to the department or agency proof that it has complied, or is in the process of complying, with this section.\n(B) The application of the remedies or penalties contained in Article 9 (commencing with Section 10420) to a contract subject to this chapter shall not preclude the application of any existing remedies otherwise available to the department or other contracting agency under its existing enforcement powers.\n(f) Nothing in this section is intended to regulate the contracting practices of any local jurisdiction.\n(g) This section shall be construed so as not to conflict with applicable federal laws, rules, or regulations. In the event that a court or agency of competent jurisdiction holds that federal law, rule, or regulation invalidates any clause, sentence, paragraph, or section of this code or the application thereof to any person or circumstances, it is the intent of the state that the court or agency sever that clause, sentence, paragraph, or section so that the remainder of this section shall remain in effect.\nSEC. 2.\nSection 10295.35 of the Public Contract Code shall not be construed to create any new enforcement authority or responsibility in the Department of General Services or any other contracting agency.\nSEC. 3.\nNo reimbursement is required by this act pursuant to Section 6 of Article XIII\u2009B of the California Constitution because the only costs that may be incurred by a local agency or school district will be incurred because this act creates a new crime or infraction, eliminates a crime or infraction, or changes the penalty for a crime or infraction, within the meaning of Section 17556 of the Government Code, or changes the definition of a crime within the meaning of Section 6 of Article XIII\u2009B of the California Constitution.', + 'title': 'An act to add Section 10295.35 to the Public Contract Code, relating to public contracts.'} +``` + +ไฝฟ็”จใ™ใ‚‹ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใŒ 2 ใคใ‚ใ‚Šใพใ™ใ€‚ + +- `text`: ใƒขใƒ‡ใƒซใธใฎๅ…ฅๅŠ›ใจใชใ‚‹่ซ‹ๆฑ‚ๆ›ธใฎใƒ†ใ‚ญใ‚นใƒˆใ€‚ +- `summary`: ใƒขใƒ‡ใƒซใฎใ‚ฟใƒผใ‚ฒใƒƒใƒˆใจใชใ‚‹ `text` ใฎ่ฆ็ด„็‰ˆใ€‚ + +## Preprocess + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€T5 ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€Œtextใ€ใจ`summary`ใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> checkpoint = "google-t5/t5-small" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +``` + +ไฝœๆˆใ™ใ‚‹ๅ‰ๅ‡ฆ็†้–ขๆ•ฐใฏๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. T5 ใŒใ“ใ‚ŒใŒ่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚‹ใ“ใจใ‚’่ช่ญ˜ใงใใ‚‹ใ‚ˆใ†ใซใ€ๅ…ฅๅŠ›ใฎๅ‰ใซใƒ—ใƒญใƒณใƒ—โ€‹โ€‹ใƒˆใ‚’ไป˜ใ‘ใพใ™ใ€‚่ค‡ๆ•ฐใฎ NLP ใ‚ฟใ‚นใ‚ฏใŒๅฏ่ƒฝใชไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใฎใƒ—ใƒญใƒณใƒ—ใƒˆใŒๅฟ…่ฆใงใ™ใ€‚ +2. ใƒฉใƒ™ใƒซใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใจใใซใ‚ญใƒผใƒฏใƒผใƒ‰ `text_target` ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +3. `max_length`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใง่จญๅฎšใ•ใ‚ŒใŸๆœ€ๅคง้•ทใ‚’่ถ…ใˆใชใ„ใ‚ˆใ†ใซใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ + +```py +>>> prefix = "summarize: " + + +>>> def preprocess_function(examples): +... inputs = [prefix + doc for doc in examples["text"]] +... model_inputs = tokenizer(inputs, max_length=1024, truncation=True) + +... labels = tokenizer(text_target=examples["summary"], max_length=128, truncation=True) + +... model_inputs["labels"] = labels["input_ids"] +... return model_inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ + +```py +>>> tokenized_billsum = billsum.map(preprocess_function, batched=True) +``` + +ๆฌกใซใ€[`DataCollatโ€‹โ€‹orForSeq2Seq`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใฎ้•ทใ•ใพใงๆ–‡ใ‚’ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ* ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ + + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint) +``` + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, return_tensors="tf") +``` + + + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[ROUGE](https://huggingface.co/spaces/evaluate-metric/rouge) ใƒกใƒˆใƒชใƒƒใ‚ฏใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่จˆ็ฎ—ใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚ + +```py +>>> import evaluate + +>>> rouge = evaluate.load("rouge") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ ROUGE ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) +... labels = np.where(labels != -100, labels, tokenizer.pad_token_id) +... decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) + +... result = rouge.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True) + +... prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in predictions] +... result["gen_len"] = np.mean(prediction_lens) + +... return {k: round(v, 4) for k, v in result.items()} +``` + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForSeq2SeqLM`] ใ‚’ไฝฟ็”จใ—ใฆ T5 ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ๆ‰‹้ †ใฏๆฌกใฎ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`Seq2SeqTrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ ROUGE ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Seq2SeqTrainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="my_awesome_billsum_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... weight_decay=0.01, +... save_total_limit=3, +... num_train_epochs=4, +... predict_with_generate=True, +... fp16=True, +... push_to_hub=True, +... ) + +>>> trainer = Seq2SeqTrainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_billsum["train"], +... eval_dataset=tokenized_billsum["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +ๆฌกใซใ€[`TFAutoModelForSeq2SeqLM`] ใ‚’ไฝฟ็”จใ—ใฆ T5 ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_billsum["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... tokenized_billsum["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๆœ€ๅพŒใฎ 2 ใคใฎใ“ใจใฏใ€ไบˆๆธฌใ‹ใ‚‰ ROUGE ใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใ‚‚ [Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +`compute_metrics` ้–ขๆ•ฐใ‚’ [`~transformers.KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +Specify where to push your model and tokenizer in the [`~transformers.PushToHubCallback`]: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_billsum_model", +... tokenizer=tokenizer, +... ) +``` + +ๆฌกใซใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ใพใจใ‚ใฆใƒใƒณใƒ‰ใƒซใ—ใพใ™ใ€‚ + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + +่ฆ็ด„็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎใ‚ˆใ‚Š่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb)ใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +่ฆ็ด„ใ—ใŸใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒใˆๅ‡บใ—ใพใ™ใ€‚ T5 ใฎๅ ดๅˆใ€ไฝœๆฅญไธญใฎใ‚ฟใ‚นใ‚ฏใซๅฟœใ˜ใฆๅ…ฅๅŠ›ใซๆŽฅ้ ญ่พžใ‚’ไป˜ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚่ฆ็ด„ใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใซ็คบใ™ใ‚ˆใ†ใซๅ…ฅๅŠ›ใซใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใ‚’ไป˜ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> text = "summarize: The Inflation Reduction Act lowers prescription drug costs, health care costs, and energy costs. It's the most aggressive action on tackling the climate crisis in American history, which will lift up American workers and create good-paying, union jobs across the country. It'll lower the deficit and ask the ultra-wealthy and corporations to pay their fair share. And no one making under $400,000 per year will pay a penny more in taxes." +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„็”จใฎ `pipeline` ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> summarizer = pipeline("summarization", model="stevhliu/my_awesome_billsum_model") +>>> summarizer(text) +[{"summary_text": "The Inflation Reduction Act lowers prescription drug costs, health care costs, and energy costs. It's the most aggressive action on tackling the climate crisis in American history, which will lift up American workers and create good-paying, union jobs across the country."}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใ€ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + +Tokenize the text and return the `input_ids` as PyTorch tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model") +>>> inputs = tokenizer(text, return_tensors="pt").input_ids +``` + +[`~generation.GenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Text Generation](../main_classes/text_generation) API ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import AutoModelForSeq2SeqLM + +>>> model = AutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'the inflation reduction act lowers prescription drug costs, health care costs, and energy costs. it's the most aggressive action on tackling the climate crisis in american history. it will ask the ultra-wealthy and corporations to pay their fair share.' +``` + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€`input_ids`ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model") +>>> inputs = tokenizer(text, return_tensors="tf").input_ids +``` + +[`~transformers.generation_tf_utils.TFGenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ่ฆ็ด„ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Text Generation](../main_classes/text_generation) API ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'the inflation reduction act lowers prescription drug costs, health care costs, and energy costs. it's the most aggressive action on tackling the climate crisis in american history. it will ask the ultra-wealthy and corporations to pay their fair share.' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/text-to-speech.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/text-to-speech.md new file mode 100644 index 0000000000000000000000000000000000000000..1c22dfd71a7b6a2c139f55dbd442a1c86762484f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/text-to-speech.md @@ -0,0 +1,638 @@ + + +# Text to speech + +[[open-in-colab]] + +ใƒ†ใ‚ญใ‚นใƒˆ่ชญใฟไธŠใ’ (TTS) ใฏใ€ใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰่‡ช็„ถใช้Ÿณๅฃฐใ‚’ไฝœๆˆใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚้Ÿณๅฃฐใฏ่ค‡ๆ•ฐใฎๅฝขๅผใง็”Ÿๆˆใงใใพใ™ใ€‚ +่จ€่ชžใจ่ค‡ๆ•ฐใฎ่ฉฑ่€…ๅ‘ใ‘ใ€‚็พๅœจใ€ใ„ใใคใ‹ใฎใƒ†ใ‚ญใ‚นใƒˆ่ชญใฟไธŠใ’ใƒขใƒ‡ใƒซใŒ ๐Ÿค— Transformers ใงๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ +[Bark](../model_doc/bark)ใ€[MMS](../model_doc/mms)ใ€[VITS](../model_doc/vits)ใ€ใŠใ‚ˆใณ [SpeechT5](../model_doc/speecht5)ใ€‚ + +`text-to-audio`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ (ใพใŸใฏใใฎๅˆฅๅ - `text-to-speech`) ใ‚’ไฝฟ็”จใ—ใฆใ€้Ÿณๅฃฐใ‚’็ฐกๅ˜ใซ็”Ÿๆˆใงใใพใ™ใ€‚ Bark ใชใฉใฎไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏใ€ +็ฌ‘ใ„ใ€ใŸใ‚ๆฏใ€ๆณฃใใชใฉใฎ้ž่จ€่ชžใ‚ณใƒŸใƒฅใƒ‹ใ‚ฑใƒผใ‚ทใƒงใƒณใ‚’็”Ÿๆˆใ—ใŸใ‚Šใ€้Ÿณๆฅฝใ‚’่ฟฝๅŠ ใ—ใŸใ‚Šใ™ใ‚‹ใ‚ˆใ†ใซๆกไปถไป˜ใ‘ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +Bark ใง`text-to-speech`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใฎไพ‹ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> pipe = pipeline("text-to-speech", model="suno/bark-small") +>>> text = "[clears throat] This is a test ... and I just took a long pause." +>>> output = pipe(text) +``` + +ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใง็ตๆžœใฎ้Ÿณๅฃฐใ‚’่žใใŸใ‚ใซไฝฟ็”จใงใใ‚‹ใ‚ณใƒผใƒ‰ ใ‚นใƒ‹ใƒšใƒƒใƒˆใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```python +>>> from IPython.display import Audio +>>> Audio(output["audio"], rate=output["sampling_rate"]) +``` +Bark ใŠใ‚ˆใณใใฎไป–ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ TTS ใƒขใƒ‡ใƒซใŒใงใใ‚‹ใ“ใจใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๆฌกใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[้Ÿณๅฃฐใ‚ณใƒผใ‚น](https://huggingface.co/learn/audio-course/chapter6/pre-trained_models)ใ€‚ + +TTS ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๅ ดๅˆใ€็พๅœจๅพฎ่ชฟๆ•ดใงใใ‚‹ใฎใฏ SpeechT5 ใฎใฟใงใ™ใ€‚ SpeechT5 ใฏใ€ๆฌกใฎ็ต„ใฟๅˆใ‚ใ›ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +้Ÿณๅฃฐใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎใƒ‡ใƒผใ‚ฟใจใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰้Ÿณๅฃฐใธใฎใƒ‡ใƒผใ‚ฟใ€‚ไธกๆ–นใฎใƒ†ใ‚ญใ‚นใƒˆใซๅ…ฑๆœ‰ใ•ใ‚Œใ‚‹้š ใ•ใ‚ŒใŸ่กจ็พใฎ็ตฑไธ€ใ•ใ‚ŒใŸ็ฉบ้–“ใ‚’ๅญฆ็ฟ’ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +ใใ—ใฆใ‚นใƒ”ใƒผใƒใ€‚ใ“ใ‚Œใฏใ€ๅŒใ˜ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใ•ใพใ–ใพใชใ‚ฟใ‚นใ‚ฏใซๅˆใ‚ใ›ใฆๅพฎ่ชฟๆ•ดใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ใ•ใ‚‰ใซใ€SpeechT5 +X ใƒ™ใ‚ฏใƒˆใƒซ ใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚’้€šใ˜ใฆ่ค‡ๆ•ฐใฎใ‚นใƒ”ใƒผใ‚ซใƒผใ‚’ใ‚ตใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๆฎ‹ใ‚Šใฎ้ƒจๅˆ†ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [VoxPopuli](https://huggingface.co/datasets/facebook/voxpopuli) ใฎใ‚ชใƒฉใƒณใƒ€่ชž (`nl`) ่จ€่ชžใ‚ตใƒ–ใ‚ปใƒƒใƒˆไธŠใฎ่‹ฑ่ชž้Ÿณๅฃฐใงๅ…ƒใ€…ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ [SpeechT5](../model_doc/speecht5) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ +2. ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹็›ดๆŽฅไฝฟ็”จใ™ใ‚‹ใ‹ใฎ 2 ใคใฎๆ–นๆณ•ใฎใ„ใšใ‚Œใ‹ใงใ€ๆด—็ทดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +```bash +pip install datasets soundfile speechbrain accelerate +``` + +SpeechT5 ใฎใ™ในใฆใฎๆฉŸ่ƒฝใŒใพใ ๆญฃๅผใƒชใƒชใƒผใ‚นใซใƒžใƒผใ‚ธใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ใ‚ฝใƒผใ‚นใ‹ใ‚‰ ๐Ÿค—Transformers ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ + +```bash +pip install git+https://github.com/huggingface/transformers.git +``` + + + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใซๅพ“ใ†ใซใฏใ€GPU ใŒๅฟ…่ฆใงใ™ใ€‚ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใงไฝœๆฅญใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎ่กŒใ‚’ๅฎŸ่กŒใ—ใฆ GPU ใŒๅˆฉ็”จๅฏ่ƒฝใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + +```bash +!nvidia-smi +``` + + + +Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load the dataset + +[VoxPopuli](https://huggingface.co/datasets/facebook/voxpopuli) ใฏใ€ไปฅไธ‹ใงๆง‹ๆˆใ•ใ‚Œใ‚‹ๅคง่ฆๆจกใชๅคš่จ€่ชž้Ÿณๅฃฐใ‚ณใƒผใƒ‘ใ‚นใงใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใฏ 2009 ๅนดใ‹ใ‚‰ 2020 ๅนดใฎๆฌงๅทž่ญฐไผšใฎใ‚คใƒ™ใƒณใƒˆ่จ˜้Œฒใ‚’ใ‚ฝใƒผใ‚นใจใ—ใฆใ„ใพใ™ใ€‚ 15 ไปถๅˆ†ใฎใƒฉใƒ™ใƒซไป˜ใ้Ÿณๅฃฐๆ–‡ๅญ—่ตทใ“ใ—ใƒ‡ใƒผใ‚ฟใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +ใƒจใƒผใƒญใƒƒใƒ‘ใฎ่จ€่ชžใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ‚ชใƒฉใƒณใƒ€่ชžใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใŒใ€่‡ช็”ฑใซๅˆฅใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’้ธๆŠžใ—ใฆใใ ใ•ใ„ใ€‚ + +VoxPopuli ใพใŸใฏใใฎไป–ใฎ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ (ASR) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏๆœ€้ฉใงใฏใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ +TTS ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใ€‚้Žๅ‰ฐใชใƒใƒƒใ‚ฏใ‚ฐใƒฉใ‚ฆใƒณใƒ‰ใƒŽใ‚คใ‚บใชใฉใ€ASR ใซใจใฃใฆๆœ‰็›Šใจใชใ‚‹ๆฉŸ่ƒฝใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ +้€šๅธธใ€TTS ใงใฏๆœ›ใพใ—ใใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€ๆœ€้ซ˜ๅ“่ณชใ€ๅคš่จ€่ชžใ€ใƒžใƒซใƒใ‚นใƒ”ใƒผใ‚ซใƒผใฎ TTS ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ฆ‹ใคใ‘ใ‚‹ใฎใฏ้žๅธธใซๅ›ฐ้›ฃใชๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๆŒ‘ๆˆฆ็š„ใ€‚ + +ใƒ‡ใƒผใ‚ฟใ‚’ใƒญใƒผใƒ‰ใ—ใพใ—ใ‚‡ใ†: + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("facebook/voxpopuli", "nl", split="train") +>>> len(dataset) +20968 +``` + +ๅพฎ่ชฟๆ•ดใซใฏ 20968 ๅ€‹ใฎไพ‹ใงๅๅˆ†ใงใ™ใ€‚ SpeechT5 ใฏใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ‡ใƒผใ‚ฟใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐ ใƒฌใƒผใƒˆใŒ 16 kHz ใงใ‚ใ‚‹ใ“ใจใ‚’ๆƒณๅฎšใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎไพ‹ใŒใ“ใฎ่ฆไปถใ‚’ๆบ€ใŸใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +``` + +## Preprocess the data + +ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ๅฎš็พฉใ—ใ€้ฉๅˆ‡ใชใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ—ใ‚‡ใ†ใ€‚ + + +```py +>>> from transformers import SpeechT5Processor + +>>> checkpoint = "microsoft/speecht5_tts" +>>> processor = SpeechT5Processor.from_pretrained(checkpoint) +``` + +### Text cleanup for SpeechT5 tokenization + + +ใพใšใฏใƒ†ใ‚ญใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚’ใ‚ฏใƒชใƒผใƒณใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ้ƒจๅˆ†ใŒๅฟ…่ฆใงใ™ใ€‚ + +```py +>>> tokenizer = processor.tokenizer +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎไพ‹ใซใฏใ€`raw_text`ๆฉŸ่ƒฝใจ `normalized_text`ๆฉŸ่ƒฝใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใจใ—ใฆใฉใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ๆฑบใ‚ใ‚‹ใจใใฏใ€ +SpeechT5 ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซใฏๆ•ฐๅ€คใฎใƒˆใƒผใ‚ฏใƒณใŒใชใ„ใ“ใจใ‚’่€ƒๆ…ฎใ—ใฆใใ ใ•ใ„ใ€‚ `normalized_text`ใซใฏๆ•ฐๅญ—ใŒๆ›ธใ‹ใ‚Œใฆใ„ใพใ™ +ใƒ†ใ‚ญใ‚นใƒˆใจใ—ใฆๅ‡บๅŠ›ใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ“ใ‚Œใฏใ‚ˆใ‚Š้ฉๅˆ‡ใงใ‚ใ‚Šใ€ๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใจใ—ใฆ `normalized_text` ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +SpeechT5 ใฏ่‹ฑ่ชžใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ใ‚ชใƒฉใƒณใƒ€่ชžใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎ็‰นๅฎšใฎๆ–‡ๅญ—ใ‚’่ช่ญ˜ใ—ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚‚ใ— +ๆฎ‹ใฃใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€ใ“ใ‚Œใ‚‰ใฎๆ–‡ๅญ—ใฏ ``ใƒˆใƒผใ‚ฏใƒณใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€ใ‚ชใƒฉใƒณใƒ€่ชžใงใฏใ€`ร `ใชใฉใฎ็‰นๅฎšใฎๆ–‡ๅญ—ใฏ +้Ÿณ็ฏ€ใ‚’ๅผท่ชฟใ™ใ‚‹ใ“ใจใซๆ…ฃใ‚Œใฆใ„ใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใฎๆ„ๅ‘ณใ‚’ไฟๆŒใ™ใ‚‹ใŸใ‚ใซใ€ใ“ใฎๆ–‡ๅญ—ใ‚’้€šๅธธใฎ`a`ใซ็ฝฎใๆ›ใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ใƒˆใƒผใ‚ฏใƒณใ‚’่ญ˜ๅˆฅใ™ใ‚‹ใซใฏใ€`SpeechT5Tokenizer`ใ‚’ไฝฟ็”จใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎใ™ในใฆใฎไธ€ๆ„ใฎๆ–‡ๅญ—ใ‚’ๆŠฝๅ‡บใ—ใพใ™ใ€‚ +ๆ–‡ๅญ—ใ‚’ใƒˆใƒผใ‚ฏใƒณใจใ—ใฆๆ‰ฑใ„ใพใ™ใ€‚ใ“ใ‚Œใ‚’่กŒใ†ใซใฏใ€ไปฅไธ‹ใ‚’้€ฃ็ตใ™ใ‚‹ `extract_all_chars` ใƒžใƒƒใƒ”ใƒณใ‚ฐ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ™ในใฆใฎไพ‹ใ‹ใ‚‰ใฎ่ปขๅ†™ใ‚’ 1 ใคใฎๆ–‡ๅญ—ๅˆ—ใซใพใจใ‚ใ€ใใ‚Œใ‚’ๆ–‡ๅญ—ใ‚ปใƒƒใƒˆใซๅค‰ๆ›ใ—ใพใ™ใ€‚ +ใ™ในใฆใฎๆ–‡ๅญ—่ตทใ“ใ—ใŒไธ€ๅบฆใซๅˆฉ็”จใงใใ‚‹ใ‚ˆใ†ใซใ€`dataset.map()`ใง`bโ€‹โ€‹atched=True`ใจ`batch_size=-1`ใ‚’ๅฟ…ใš่จญๅฎšใ—ใฆใใ ใ•ใ„ใ€‚ +ใƒžใƒƒใƒ”ใƒณใ‚ฐๆฉŸ่ƒฝใ€‚ + +```py +>>> def extract_all_chars(batch): +... all_text = " ".join(batch["normalized_text"]) +... vocab = list(set(all_text)) +... return {"vocab": [vocab], "all_text": [all_text]} + + +>>> vocabs = dataset.map( +... extract_all_chars, +... batched=True, +... batch_size=-1, +... keep_in_memory=True, +... remove_columns=dataset.column_names, +... ) + +>>> dataset_vocab = set(vocabs["vocab"][0]) +>>> tokenizer_vocab = {k for k, _ in tokenizer.get_vocab().items()} +``` + +ใ“ใ‚Œใงใ€2 ใคใฎๆ–‡ๅญ—ใ‚ปใƒƒใƒˆใŒใงใใพใ—ใŸใ€‚1 ใคใฏใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ชžๅฝ™ใ‚’ๆŒใกใ€ใ‚‚ใ† 1 ใคใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใฎ่ชžๅฝ™ใ‚’ๆŒใกใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ๆ–‡ๅญ—ใ‚’็‰นๅฎšใ™ใ‚‹ใซใฏใ€ใ“ใ‚Œใ‚‰ 2 ใคใฎใ‚ปใƒƒใƒˆใฎๅทฎๅˆ†ใ‚’ๅ–ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚็ตๆžœใจใ—ใฆ +set ใซใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏใ‚ใ‚‹ใŒใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใซใฏๅซใพใ‚Œใฆใ„ใชใ„ๆ–‡ๅญ—ใŒๅซใพใ‚Œใพใ™ใ€‚ + +```py +>>> dataset_vocab - tokenizer_vocab +{' ', 'ร ', 'รง', 'รจ', 'รซ', 'รญ', 'รฏ', 'รถ', 'รผ'} +``` + +ๅ‰ใฎๆ‰‹้ †ใง็‰นๅฎšใ•ใ‚ŒใŸใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ๆ–‡ๅญ—ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€ใ“ใ‚Œใ‚‰ใฎๆ–‡ๅญ—ใ‚’ +ๆœ‰ๅŠนใชใƒˆใƒผใ‚ฏใƒณใ€‚ใ‚นใƒšใƒผใ‚นใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ™ใงใซ `โ–` ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๅ€‹ๅˆฅใซๅ‡ฆ็†ใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> replacements = [ +... ("ร ", "a"), +... ("รง", "c"), +... ("รจ", "e"), +... ("รซ", "e"), +... ("รญ", "i"), +... ("รฏ", "i"), +... ("รถ", "o"), +... ("รผ", "u"), +... ] + + +>>> def cleanup_text(inputs): +... for src, dst in replacements: +... inputs["normalized_text"] = inputs["normalized_text"].replace(src, dst) +... return inputs + + +>>> dataset = dataset.map(cleanup_text) +``` + +ใƒ†ใ‚ญใ‚นใƒˆๅ†…ใฎ็‰นๆฎŠๆ–‡ๅญ—ใ‚’ๆ‰ฑใฃใŸใฎใงใ€ไปŠๅบฆใฏ้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใซ็„ฆ็‚นใ‚’็งปใ—ใพใ™ใ€‚ + +### Speakers + +VoxPopuli ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ่ค‡ๆ•ฐใฎ่ฉฑ่€…ใฎ้ŸณๅฃฐใŒๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏไฝ•ไบบใฎ่ฉฑ่€…ใŒๅซใพใ‚Œใฆใ„ใ‚‹ใฎใงใ—ใ‚‡ใ†ใ‹?ใซ +ใ“ใ‚Œใ‚’ๆฑบๅฎšใ™ใ‚‹ใจใ€ไธ€ๆ„ใฎ่ฉฑ่€…ใฎๆ•ฐใจใ€ๅ„่ฉฑ่€…ใŒใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅฏ„ไธŽใ™ใ‚‹ไพ‹ใฎๆ•ฐใ‚’ๆ•ฐใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏๅˆ่จˆ 20,968 ๅ€‹ใฎไพ‹ใŒๅซใพใ‚ŒใฆใŠใ‚Šใ€ใ“ใฎๆƒ…ๅ ฑใซใ‚ˆใ‚Šใ€ๅˆ†ๅธƒใ‚’ใ‚ˆใ‚Šๆทฑใ็†่งฃใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +่ฌ›ๆผ”่€…ใจใƒ‡ใƒผใ‚ฟๅ†…ใฎไพ‹ใ€‚ + +```py +>>> from collections import defaultdict + +>>> speaker_counts = defaultdict(int) + +>>> for speaker_id in dataset["speaker_id"]: +... speaker_counts[speaker_id] += 1 +``` + +ใƒ’ใ‚นใƒˆใ‚ฐใƒฉใƒ ใ‚’ใƒ—ใƒญใƒƒใƒˆใ™ใ‚‹ใจใ€ๅ„่ฉฑ่€…ใซใฉใ‚Œใ ใ‘ใฎใƒ‡ใƒผใ‚ฟใŒใ‚ใ‚‹ใ‹ใ‚’ๆŠŠๆกใงใใพใ™ใ€‚ + +```py +>>> import matplotlib.pyplot as plt + +>>> plt.figure() +>>> plt.hist(speaker_counts.values(), bins=20) +>>> plt.ylabel("Speakers") +>>> plt.xlabel("Examples") +>>> plt.show() +``` + +
+ Speakers histogram +
+ +ใƒ’ใ‚นใƒˆใ‚ฐใƒฉใƒ ใ‹ใ‚‰ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎ่ฉฑ่€…ใฎ็ด„ 3 ๅˆ†ใฎ 1 ใฎไพ‹ใŒ 100 ๆœชๆบ€ใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ +็ด„ 10 ไบบใฎ่ฌ›ๆผ”่€…ใŒ 500 ไปฅไธŠใฎไพ‹ใ‚’ๆŒใฃใฆใ„ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅŠน็އใ‚’ๅ‘ไธŠใ•ใ›ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒใƒฉใƒณใ‚นใ‚’ใจใ‚‹ใŸใ‚ใซใ€ๆฌกใฎใ“ใจใ‚’ๅˆถ้™ใงใใพใ™ใ€‚ +100 ๏ฝž 400 ๅ€‹ใฎไพ‹ใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟใ‚’่ฌ›ๆผ”่€…ใซๆไพ›ใ—ใพใ™ใ€‚ + +```py +>>> def select_speaker(speaker_id): +... return 100 <= speaker_counts[speaker_id] <= 400 + + +>>> dataset = dataset.filter(select_speaker, input_columns=["speaker_id"]) +``` + +ๆฎ‹ใ‚Šใฎใ‚นใƒ”ใƒผใ‚ซใƒผใฎๆ•ฐใ‚’็ขบ่ชใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> len(set(dataset["speaker_id"])) +42 +``` + +ๆฎ‹ใ‚Šใฎไพ‹ใŒใ„ใใคใ‚ใ‚‹ใ‹่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +```py +>>> len(dataset) +9973 +``` + +็ด„ 40 ไบบใฎใƒฆใƒ‹ใƒผใ‚ฏใช่ฌ›ๆผ”่€…ใ‹ใ‚‰ใฎ 10,000 ๅผฑใฎไพ‹ใŒๆฎ‹ใ‚Šใพใ™ใŒใ€ใ“ใ‚Œใงๅๅˆ†ใงใ™ใ€‚ + +ไพ‹ใŒๅฐ‘ใชใ„ใ‚นใƒ”ใƒผใ‚ซใƒผใฎไธญใซใฏใ€ไพ‹ใŒ้•ทใ„ๅ ดๅˆใ€ๅฎŸ้š›ใซใฏใ‚ˆใ‚Šๅคšใใฎ้ŸณๅฃฐใŒๅˆฉ็”จใงใใ‚‹ๅ ดๅˆใŒใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ—ใ‹ใ—ใ€ +ๅ„่ฉฑ่€…ใฎ้Ÿณๅฃฐใฎๅˆ่จˆ้‡ใ‚’ๆฑบๅฎšใ™ใ‚‹ใซใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ใ‚นใ‚ญใƒฃใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๅ„ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ช ใƒ•ใ‚กใ‚คใƒซใฎใƒญใƒผใƒ‰ใจใƒ‡ใ‚ณใƒผใƒ‰ใ‚’ไผดใ†ๆ™‚้–“ใฎใ‹ใ‹ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใ€‚ใใฎใŸใ‚ใ€ใ“ใ“ใงใฏใ“ใฎใ‚นใƒ†ใƒƒใƒ—ใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ใ“ใจใซใ—ใพใ—ใŸใ€‚ + +### Speaker embeddings + +TTS ใƒขใƒ‡ใƒซใŒ่ค‡ๆ•ฐใฎใ‚นใƒ”ใƒผใ‚ซใƒผใ‚’ๅŒบๅˆฅใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€ใ‚ตใƒณใƒ—ใƒซใ”ใจใซใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚’ไฝœๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใฏใ€็‰นๅฎšใฎใ‚นใƒ”ใƒผใ‚ซใƒผใฎ้Ÿณๅฃฐ็‰นๆ€งใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ™ใ‚‹ใƒขใƒ‡ใƒซใธใฎ่ฟฝๅŠ ๅ…ฅๅŠ›ใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎใ‚นใƒ”ใƒผใ‚ซใƒผๅŸ‹ใ‚่พผใฟใ‚’็”Ÿๆˆใ™ใ‚‹ใซใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ [spkrec-xvect-voxceleb](https://huggingface.co/speechbrain/spkrec-xvect-voxceleb) ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +SpeechBrain ใฎใƒขใƒ‡ใƒซใ€‚ + +ๅ…ฅๅŠ›ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๆณขๅฝขใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€512 ่ฆ็ด ใฎใƒ™ใ‚ฏใƒˆใƒซใ‚’ๅ‡บๅŠ›ใ™ใ‚‹้–ขๆ•ฐ `create_speaker_embedding()` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ๅฏพๅฟœใ™ใ‚‹ใ‚นใƒ”ใƒผใ‚ซใƒผๅŸ‹ใ‚่พผใฟใŒๅซใพใ‚Œใพใ™ใ€‚ + + +```py +>>> import os +>>> import torch +>>> from speechbrain.pretrained import EncoderClassifier + +>>> spk_model_name = "speechbrain/spkrec-xvect-voxceleb" + +>>> device = "cuda" if torch.cuda.is_available() else "cpu" +>>> speaker_model = EncoderClassifier.from_hparams( +... source=spk_model_name, +... run_opts={"device": device}, +... savedir=os.path.join("/tmp", spk_model_name), +... ) + + +>>> def create_speaker_embedding(waveform): +... with torch.no_grad(): +... speaker_embeddings = speaker_model.encode_batch(torch.tensor(waveform)) +... speaker_embeddings = torch.nn.functional.normalize(speaker_embeddings, dim=2) +... speaker_embeddings = speaker_embeddings.squeeze().cpu().numpy() +... return speaker_embeddings +``` + +`speechbrain/spkrec-xvect-voxceleb`ใƒขใƒ‡ใƒซใฏใ€VoxCeleb ใ‹ใ‚‰ใฎ่‹ฑ่ชž้Ÿณๅฃฐใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใ“ใจใซๆณจๆ„ใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใ™ใŒใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไพ‹ใฏใ‚ชใƒฉใƒณใƒ€่ชžใงใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฏไปŠๅพŒใ‚‚็”Ÿๆˆใ•ใ‚Œใ‚‹ใจไฟกใ˜ใฆใ„ใพใ™ใŒใ€ +ใ‚ชใƒฉใƒณใƒ€่ชžใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซ้ฉๅˆ‡ใช่ฉฑ่€…ๅŸ‹ใ‚่พผใฟใ‚’่กŒใฃใฆใ‚‚ใ€ใ“ใฎไปฎๅฎšใฏใ™ในใฆใฎๅ ดๅˆใซๅฝ“ใฆใฏใพใ‚‰ใชใ„ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆœ€้ฉใช็ตๆžœใ‚’ๅพ—ใ‚‹ใซใฏใ€ๆœ€ๅˆใซใ‚ฟใƒผใ‚ฒใƒƒใƒˆ้Ÿณๅฃฐใง X ใƒ™ใ‚ฏใƒˆใƒซ ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒ็ขบๅฎŸใซ +ใ‚ชใƒฉใƒณใƒ€่ชžใซๅญ˜ๅœจใ™ใ‚‹็‹ฌ็‰นใฎ้Ÿณๅฃฐ็‰นๅพดใ‚’ใ‚ˆใ‚Šใ‚ˆใๆ‰ใˆใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +### Processing the dataset + +ๆœ€ๅพŒใซใ€ใƒขใƒ‡ใƒซใŒๆœŸๅพ…ใ™ใ‚‹ๅฝขๅผใซใƒ‡ใƒผใ‚ฟใ‚’ๅ‡ฆ็†ใ—ใพใ—ใ‚‡ใ†ใ€‚ใ‚’ๅ–ใ‚Š่พผใ‚€ `prepare_dataset` ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ“ใ‚Œใฏ 1 ใคใฎไพ‹ใงใ‚ใ‚Šใ€`SpeechT5Processor` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝฟ็”จใ—ใฆๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ‚’ใƒญใ‚ฐใƒกใƒซ ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใซใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใพใŸใ€่ฟฝๅŠ ใฎๅ…ฅๅŠ›ใจใ—ใฆใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚‚่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> def prepare_dataset(example): +... audio = example["audio"] + +... example = processor( +... text=example["normalized_text"], +... audio_target=audio["array"], +... sampling_rate=audio["sampling_rate"], +... return_attention_mask=False, +... ) + +... # strip off the batch dimension +... example["labels"] = example["labels"][0] + +... # use SpeechBrain to obtain x-vector +... example["speaker_embeddings"] = create_speaker_embedding(audio["array"]) + +... return example +``` + +ๅ˜ไธ€ใฎไพ‹ใ‚’่ฆ‹ใฆใ€ๅ‡ฆ็†ใŒๆญฃใ—ใ„ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + +```py +>>> processed_example = prepare_dataset(dataset[0]) +>>> list(processed_example.keys()) +['input_ids', 'labels', 'stop_labels', 'speaker_embeddings'] +``` +ใ‚นใƒ”ใƒผใ‚ซใƒผใฎใ‚จใƒณใƒ™ใƒ‡ใ‚ฃใƒณใ‚ฐใฏ 512 ่ฆ็ด ใฎใƒ™ใ‚ฏใƒˆใƒซใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> processed_example["speaker_embeddings"].shape +(512,) +``` + +ใƒฉใƒ™ใƒซใฏใ€80 ใƒกใƒซ ใƒ“ใƒณใ‚’ๅซใ‚€ใƒญใ‚ฐใƒกใƒซ ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> import matplotlib.pyplot as plt + +>>> plt.figure() +>>> plt.imshow(processed_example["labels"].T) +>>> plt.show() +``` + +
+ Log-mel spectrogram with 80 mel bins +
+ +่ฃœ่ถณ: ใ“ใฎใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใŒใ‚ใ‹ใ‚Šใซใใ„ใจๆ„Ÿใ˜ใ‚‹ๅ ดๅˆใฏใ€ไฝŽๅ‘จๆณขใ‚’้…็ฝฎใ™ใ‚‹่ฆๅ‰‡ใซๆ…ฃใ‚Œใฆใ„ใ‚‹ใ“ใจใŒๅŽŸๅ› ใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ—ใƒญใƒƒใƒˆใฎไธ‹้ƒจใซ้ซ˜ๅ‘จๆณขใ€ไธŠ้ƒจใซ้ซ˜ๅ‘จๆณขใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€matplotlib ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‚’็”ปๅƒใจใ—ใฆใƒ—ใƒญใƒƒใƒˆใ™ใ‚‹ๅ ดๅˆใ€ +Y ่ปธใŒๅ่ปขใ•ใ‚Œใ€ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใŒไธŠไธ‹้€†ใซ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +ๆฌกใซใ€ๅ‡ฆ็†้–ขๆ•ฐใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซ้ฉ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใซใฏ 5 ๏ฝž 10 ๅˆ†ใ‹ใ‹ใ‚Šใพใ™ใ€‚ + +```py +>>> dataset = dataset.map(prepare_dataset, remove_columns=dataset.column_names) +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎไธ€้ƒจใฎไพ‹ใŒใ€ใƒขใƒ‡ใƒซใŒๅ‡ฆ็†ใงใใ‚‹ๆœ€ๅคงๅ…ฅๅŠ›้•ท (600 ใƒˆใƒผใ‚ฏใƒณ) ใ‚’่ถ…ใˆใฆใ„ใ‚‹ใ“ใจใ‚’็คบใ™่ญฆๅ‘ŠใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ +ใใ‚Œใ‚‰ใฎไพ‹ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ๅ‰Š้™คใ—ใพใ™ใ€‚ใ“ใ“ใงใฏใ•ใ‚‰ใซ้€ฒใ‚“ใงใ€ใ‚ˆใ‚Šๅคงใใชใƒใƒƒใƒ ใ‚ตใ‚คใ‚บใ‚’ๅฏ่ƒฝใซใ™ใ‚‹ใŸใ‚ใซใ€200 ใƒˆใƒผใ‚ฏใƒณใ‚’่ถ…ใˆใ‚‹ใ‚‚ใฎใฏใ™ในใฆๅ‰Š้™คใ—ใพใ™ใ€‚ + +```py +>>> def is_not_too_long(input_ids): +... input_length = len(input_ids) +... return input_length < 200 + + +>>> dataset = dataset.filter(is_not_too_long, input_columns=["input_ids"]) +>>> len(dataset) +8259 +``` + +ๆฌกใซใ€ๅŸบๆœฌ็š„ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ/ใƒ†ใ‚นใƒˆๅˆ†ๅ‰ฒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> dataset = dataset.train_test_split(test_size=0.1) +``` + +### Data collator + +่ค‡ๆ•ฐใฎไพ‹ใ‚’ 1 ใคใฎใƒใƒƒใƒใซ็ตๅˆใ™ใ‚‹ใซใฏใ€ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ‚’ๅฎš็พฉใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎใ‚ณใƒฌใƒผใ‚ฟใƒผใฏใ€็Ÿญใ„ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใงๅŸ‹ใ‚่พผใฟใพใ™ใ€‚ +ใƒˆใƒผใ‚ฏใƒณใ‚’ไฝฟ็”จใ—ใฆใ€ใ™ในใฆใฎไพ‹ใŒๅŒใ˜้•ทใ•ใซใชใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ  ใƒฉใƒ™ใƒซใฎๅ ดๅˆใ€ๅŸ‹ใ‚่พผใพใ‚ŒใŸ้ƒจๅˆ†ใฏ็‰นๅˆฅใชๅ€ค `-100` ใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใฎ็‰นๅˆฅใชไพกๅ€คใฏ +ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ๆๅคฑใ‚’่จˆ็ฎ—ใ™ใ‚‹ใจใใซใ€ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใฎใใฎ้ƒจๅˆ†ใ‚’็„ก่ฆ–ใ™ใ‚‹ใ‚ˆใ†ใซใƒขใƒ‡ใƒซใซๆŒ‡็คบใ—ใพใ™ใ€‚ + +```py +>>> from dataclasses import dataclass +>>> from typing import Any, Dict, List, Union + + +>>> @dataclass +... class TTSDataCollatorWithPadding: +... processor: Any + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... input_ids = [{"input_ids": feature["input_ids"]} for feature in features] +... label_features = [{"input_values": feature["labels"]} for feature in features] +... speaker_features = [feature["speaker_embeddings"] for feature in features] + +... # collate the inputs and targets into a batch +... batch = processor.pad(input_ids=input_ids, labels=label_features, return_tensors="pt") + +... # replace padding with -100 to ignore loss correctly +... batch["labels"] = batch["labels"].masked_fill(batch.decoder_attention_mask.unsqueeze(-1).ne(1), -100) + +... # not used during fine-tuning +... del batch["decoder_attention_mask"] + +... # round down target lengths to multiple of reduction factor +... if model.config.reduction_factor > 1: +... target_lengths = torch.tensor([len(feature["input_values"]) for feature in label_features]) +... target_lengths = target_lengths.new( +... [length - length % model.config.reduction_factor for length in target_lengths] +... ) +... max_length = max(target_lengths) +... batch["labels"] = batch["labels"][:, :max_length] + +... # also add in the speaker embeddings +... batch["speaker_embeddings"] = torch.tensor(speaker_features) + +... return batch +``` + +SpeechT5 ใงใฏใ€ใƒขใƒ‡ใƒซใฎใƒ‡ใ‚ณใƒผใƒ€้ƒจๅˆ†ใธใฎๅ…ฅๅŠ›ใŒ 2 ๅˆ†ใฎ 1 ใซๅ‰Šๆธ›ใ•ใ‚Œใพใ™ใ€‚ใคใพใ‚Šใ€ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใŒ็ ดๆฃ„ใ•ใ‚Œใพใ™ใ€‚ +ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‹ใ‚‰ใฎไป–ใฎใ‚ฟใ‚คใƒ ใ‚นใƒ†ใƒƒใƒ—ใ€‚ๆฌกใซใ€ใƒ‡ใ‚ณใƒผใƒ€ใฏ 2 ๅ€ใฎ้•ทใ•ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ‚ชใƒชใ‚ธใƒŠใƒซไปฅๆฅ +ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ทใƒผใ‚ฑใƒณใ‚นใฎ้•ทใ•ใŒๅฅ‡ๆ•ฐใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๆฉŸ่ƒฝใฏใƒใƒƒใƒใฎๆœ€ๅคง้•ทใ‚’ๅˆ‡ใ‚Šๆจใฆใฆใ€ +2ใฎๅ€ๆ•ฐใ€‚ + +```py +>>> data_collator = TTSDataCollatorWithPadding(processor=processor) +``` + +## Train the model + +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฎใƒญใƒผใƒ‰ใซไฝฟ็”จใ—ใŸใฎใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import SpeechT5ForTextToSpeech + +>>> model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint) +``` + +`use_cache=True`ใ‚ชใƒ—ใ‚ทใƒงใƒณใฏใ€ๅ‹พ้…ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจไบ’ๆ›ๆ€งใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใŸใ‚ใซ็„กๅŠนใซใ—ใพใ™ใ€‚ + +```py +>>> model.config.use_cache = False +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ใ“ใ“ใงใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ—ใƒญใ‚ปใ‚นไธญใซ่ฉ•ไพกใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ—ใฆใ„ใพใ›ใ‚“ใ€‚ไปฃใ‚ใ‚Šใซใ€ +ๆๅคฑใ ใ‘ใ‚’่ฆ‹ใฆใใ ใ•ใ„ใ€‚ + +```python +>>> from transformers import Seq2SeqTrainingArguments + +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="speecht5_finetuned_voxpopuli_nl", # change to a repo name of your choice +... per_device_train_batch_size=4, +... gradient_accumulation_steps=8, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=4000, +... gradient_checkpointing=True, +... fp16=True, +... eval_strategy="steps", +... per_device_eval_batch_size=2, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... report_to=["tensorboard"], +... load_best_model_at_end=True, +... greater_is_better=False, +... label_names=["labels"], +... push_to_hub=True, +... ) +``` + +`Trainer`ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import Seq2SeqTrainer + +>>> trainer = Seq2SeqTrainer( +... args=training_args, +... model=model, +... train_dataset=dataset["train"], +... eval_dataset=dataset["test"], +... data_collator=data_collator, +... processing_class=processor, +... ) +``` +ใ“ใ‚Œใงใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใฏๆ•ฐๆ™‚้–“ใ‹ใ‹ใ‚Šใพใ™ใ€‚ GPU ใซๅฟœใ˜ใฆใ€ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ใจใใซใ€CUDA ใฎใ€Œใƒกใƒขใƒชไธ่ถณใ€ใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๆธ›ใ‚‰ใ™ใ“ใจใŒใงใใพใ™ +`per_device_train_batch_size`ใ‚’ 2 ๅ€ใซๅข—ๅˆ†ใ—ใ€`gradient_accumulation_steps`ใ‚’ 2 ๅ€ใซๅข—ใ‚„ใ—ใฆ่ฃœๆญฃใ—ใพใ™ใ€‚ + +```py +>>> trainer.train() +``` +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€ๅฟ…ใšใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจใจใ‚‚ใซไฟๅญ˜ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> processor.save_pretrained("YOUR_ACCOUNT_NAME/speecht5_finetuned_voxpopuli_nl") +``` + +ๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚’ ๐Ÿค— ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + +## Inference + +### Inference with a pipeline + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ +ใพใšใ€ๅฏพๅฟœใ™ใ‚‹ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใงใใ‚Œใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ `"text-to-speech"` ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ† +ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ: + +```py +>>> from transformers import pipeline + +>>> pipe = pipeline("text-to-speech", model="YOUR_ACCOUNT_NAME/speecht5_finetuned_voxpopuli_nl") +``` + +ใƒŠใƒฌใƒผใ‚ทใƒงใƒณใ‚’ๅธŒๆœ›ใ™ใ‚‹ใ‚ชใƒฉใƒณใƒ€่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’้ธๆŠžใ—ใฆใใ ใ•ใ„ใ€‚ไพ‹: + +```py +>>> text = "hallo allemaal, ik praat nederlands. groetjes aan iedereen!" +``` + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใง SpeechT5 ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใŒๅฟ…่ฆใงใ™ใ€‚ใƒ†ใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎไพ‹ใ‹ใ‚‰ๅ–ๅพ—ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> example = dataset["test"][304] +>>> speaker_embeddings = torch.tensor(example["speaker_embeddings"]).unsqueeze(0) +``` + +ใ“ใ‚Œใงใ€ใƒ†ใ‚ญใ‚นใƒˆใจใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๆธกใ™ใ“ใจใŒใงใใ€ๆฎ‹ใ‚Šใฏใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใŒๅ‡ฆ็†ใ—ใพใ™ใ€‚ + + +```py +>>> forward_params = {"speaker_embeddings": speaker_embeddings} +>>> output = pipe(text, forward_params=forward_params) +>>> output +{'audio': array([-6.82714235e-05, -4.26525949e-04, 1.06134125e-04, ..., + -1.22392643e-03, -7.76011671e-04, 3.29112721e-04], dtype=float32), + 'sampling_rate': 16000} +``` + +ใใฎๅพŒใ€็ตๆžœใ‚’่žใใ“ใจใŒใงใใพใ™ใ€‚ + + +```py +>>> from IPython.display import Audio +>>> Audio(output['audio'], rate=output['sampling_rate']) +``` + +### Run inference manually + +ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝฟ็”จใ—ใชใใฆใ‚‚ๅŒใ˜ๆŽจ่ซ–็ตๆžœใ‚’ๅพ—ใ‚‹ใ“ใจใŒใงใใพใ™ใŒใ€ใ‚ˆใ‚Šๅคšใใฎๆ‰‹้ †ใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ + +๐Ÿค— ใƒใƒ–ใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + + +```py +>>> model = SpeechT5ForTextToSpeech.from_pretrained("YOUR_ACCOUNT/speecht5_finetuned_voxpopuli_nl") +``` + +ใƒ†ใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ไพ‹ใ‚’้ธๆŠžใ—ใฆใ€ใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> example = dataset["test"][304] +>>> speaker_embeddings = torch.tensor(example["speaker_embeddings"]).unsqueeze(0) +``` + +ๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅฎš็พฉใ—ใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> text = "hallo allemaal, ik praat nederlands. groetjes aan iedereen!" +>>> inputs = processor(text=text, return_tensors="pt") +``` + +ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings) +``` + +ๆฌกใฎใ“ใจใ‚’่กŒใ†ๅ ดๅˆใฏใ€ใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‚’่ฆ–่ฆšๅŒ–ใ—ใพใ™ใ€‚ + +```py +>>> plt.figure() +>>> plt.imshow(spectrogram.T) +>>> plt.show() +``` + +
+ Generated log-mel spectrogram +
+ +ๆœ€ๅพŒใซใ€ใƒœใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใฆใ‚นใƒšใ‚ฏใƒˆใƒญใ‚ฐใƒฉใƒ ใ‚’ใ‚ตใ‚ฆใƒณใƒ‰ใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> with torch.no_grad(): +... speech = vocoder(spectrogram) + +>>> from IPython.display import Audio + +>>> Audio(speech.numpy(), rate=16000) +``` + +็งใŸใกใฎ็ตŒ้จ“ใงใฏใ€ใ“ใฎใƒขใƒ‡ใƒซใ‹ใ‚‰ๆบ€่ถณใฎใ„ใ็ตๆžœใ‚’ๅพ—ใ‚‹ใฎใฏ้›ฃใ—ใ„ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅ“่ณช +ๅŸ‹ใ‚่พผใฟใฏ้‡่ฆใช่ฆ็ด ใงใ‚ใ‚‹ใ‚ˆใ†ใงใ™ใ€‚ SpeechT5 ใฏ่‹ฑ่ชžใฎ x ใƒ™ใ‚ฏใƒˆใƒซใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๆœ€้ซ˜ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’็™บๆฎใ—ใพใ™ +่‹ฑ่ชžใ‚นใƒ”ใƒผใ‚ซใƒผใฎๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€‚ๅˆๆˆ้Ÿณๅฃฐใฎ้Ÿณ่ณชใŒๆ‚ชใ„ๅ ดๅˆใฏใ€ๅˆฅใฎใ‚นใƒ”ใƒผใ‚ซใƒผๅŸ‹ใ‚่พผใฟใ‚’ไฝฟ็”จใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆœŸ้–“ใ‚’้•ทใใ™ใ‚‹ใจใ€็ตๆžœใฎ่ณชใ‚‚ๅ‘ไธŠใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใใ‚Œใงใ‚‚ใ€ใใฎใ‚นใƒ”ใƒผใƒใฏๆ˜Žใ‚‰ใ‹ใซ่‹ฑ่ชžใงใฏใชใใ‚ชใƒฉใƒณใƒ€่ชžใงใ™ใ€‚ +่ฉฑ่€…ใฎ้Ÿณๅฃฐ็‰นๆ€งใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ—ใพใ™ (ไพ‹ใฎๅ…ƒใฎ้Ÿณๅฃฐใจๆฏ”่ผƒ)ใ€‚ +ใ‚‚ใ† 1 ใคๅฎŸ้จ“ใ™ในใใ“ใจใฏใ€ใƒขใƒ‡ใƒซใฎๆง‹ๆˆใงใ™ใ€‚ใŸใจใˆใฐใ€`config.reduction_factor = 1`ใ‚’ไฝฟ็”จใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Š็ตๆžœใŒๆ”นๅ–„ใ•ใ‚Œใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +ๆœ€ๅพŒใซใ€ๅ€ซ็†็š„้…ๆ…ฎใ‚’่€ƒๆ…ฎใ™ใ‚‹ใ“ใจใŒไธๅฏๆฌ ใงใ™ใ€‚ TTS ใƒ†ใ‚ฏใƒŽใƒญใ‚ธใƒผใซใฏๆ•ฐๅคšใใฎๆœ‰็”จใช็”จ้€”ใŒใ‚ใ‚Šใพใ™ใŒใ€ +ใพใŸใ€็Ÿฅใ‚‰ใชใ„ใ†ใกใซ่ชฐใ‹ใฎๅฃฐใ‚’ๅฝ่ฃ…ใ™ใ‚‹ใชใฉใ€ๆ‚ชๆ„ใฎใ‚ใ‚‹็›ฎ็š„ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใŠ้ก˜ใ„ใ—ใพใ™ +TTS ใฏ่ณขๆ˜Žใ‹ใค่ฒฌไปปใ‚’ๆŒใฃใฆไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/token_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/token_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..4389aeacb5644b7f519b2c7bc7c4ef2aa3f7b5e9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/token_classification.md @@ -0,0 +1,561 @@ + + +# Token classification + +[[open-in-colab]] + + + +ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใงใฏใ€ๆ–‡ๅ†…ใฎๅ€‹ใ€…ใฎใƒˆใƒผใ‚ฏใƒณใซใƒฉใƒ™ใƒซใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎ 1 ใคใฏใ€ๅ›บๆœ‰่กจ็พ่ช่ญ˜ (NER) ใงใ™ใ€‚ NER ใฏใ€ไบบใ€ๅ ดๆ‰€ใ€็ต„็น”ใชใฉใ€ๆ–‡ๅ†…ใฎๅ„ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎใƒฉใƒ™ใƒซใ‚’่ฆ‹ใคใ‘ใ‚ˆใ†ใจใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [WNUT 17](https://huggingface.co/datasets/wnut_17) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใง [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€ๆ–ฐใ—ใ„ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใ‚’ๆคœๅ‡บใ—ใพใ™ใ€‚ +2. ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/token-classification) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate seqeval +``` +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load WNUT 17 dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ WNUT 17 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> wnut = load_dataset("wnut_17") +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> wnut["train"][0] +{'id': '0', + 'ner_tags': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 8, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0], + 'tokens': ['@paulwalk', 'It', "'s", 'the', 'view', 'from', 'where', 'I', "'m", 'living', 'for', 'two', 'weeks', '.', 'Empire', 'State', 'Building', '=', 'ESB', '.', 'Pretty', 'bad', 'storm', 'here', 'last', 'evening', '.'] +} +``` + +`ner_tags`ๅ†…ใฎๅ„ๆ•ฐๅญ—ใฏใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใ‚’่กจใ—ใพใ™ใ€‚ๆ•ฐๅ€คใ‚’ใƒฉใƒ™ใƒซๅใซๅค‰ๆ›ใ—ใฆใ€ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใŒไฝ•ใงใ‚ใ‚‹ใ‹ใ‚’่ชฟในใพใ™ใ€‚ + +```py +>>> label_list = wnut["train"].features[f"ner_tags"].feature.names +>>> label_list +[ + "O", + "B-corporation", + "I-corporation", + "B-creative-work", + "I-creative-work", + "B-group", + "I-group", + "B-location", + "I-location", + "B-person", + "I-person", + "B-product", + "I-product", +] +``` +ๅ„ `ner_tag` ใฎๅ‰ใซไป˜ใๆ–‡ๅญ—ใฏใ€ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚’็คบใ—ใพใ™ใ€‚ + +- `B-` ใฏใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎๅง‹ใพใ‚Šใ‚’็คบใ—ใพใ™ใ€‚ +- `I-` ใฏใ€ใƒˆใƒผใ‚ฏใƒณใŒๅŒใ˜ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃๅ†…ใซๅซใพใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็คบใ—ใพใ™ (ใŸใจใˆใฐใ€`State` ใƒˆใƒผใ‚ฏใƒณใฏๆฌกใฎใ‚ˆใ†ใชใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใฎไธ€้ƒจใงใ™) + `Empire State Building`๏ผ‰ใ€‚ +- `0` ใฏใ€ใƒˆใƒผใ‚ฏใƒณใŒใฉใฎใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃใซใ‚‚ๅฏพๅฟœใ—ใชใ„ใ“ใจใ‚’็คบใ—ใพใ™ใ€‚ + +## Preprocess + + + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€DistilBERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆ`tokens`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใ‚’ๅ‰ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +``` + +ไธŠใฎ `tokens`ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎไพ‹ใง่ฆ‹ใŸใ‚ˆใ†ใซใ€ๅ…ฅๅŠ›ใฏใ™ใงใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใงใ™ใ€‚ใ—ใ‹ใ—ใ€ๅฎŸ้š›ใซใฏๅ…ฅๅŠ›ใฏใพใ ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€ๅ˜่ชžใ‚’ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใซใฏ`is_split_into_words=True` ใ‚’่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐ๏ผš + +```py +>>> example = wnut["train"][0] +>>> tokenized_input = tokenizer(example["tokens"], is_split_into_words=True) +>>> tokens = tokenizer.convert_ids_to_tokens(tokenized_input["input_ids"]) +>>> tokens +['[CLS]', '@', 'paul', '##walk', 'it', "'", 's', 'the', 'view', 'from', 'where', 'i', "'", 'm', 'living', 'for', 'two', 'weeks', '.', 'empire', 'state', 'building', '=', 'es', '##b', '.', 'pretty', 'bad', 'storm', 'here', 'last', 'evening', '.', '[SEP]'] +``` + +ใŸใ ใ—ใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใ„ใใคใ‹ใฎ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณ `[CLS]` ใจ `[SEP]` ใŒ่ฟฝๅŠ ใ•ใ‚Œใ€ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใซใ‚ˆใ‚Šๅ…ฅๅŠ›ใจใƒฉใƒ™ใƒซใฎ้–“ใซไธไธ€่‡ดใŒ็”Ÿใ˜ใพใ™ใ€‚ 1 ใคใฎใƒฉใƒ™ใƒซใซๅฏพๅฟœใ™ใ‚‹ 1 ใคใฎๅ˜่ชžใ‚’ 2 ใคใฎใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ๆฌกใฎๆ–นๆณ•ใงใƒˆใƒผใ‚ฏใƒณใจใƒฉใƒ™ใƒซใ‚’ๅ†่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. [`word_ids`](https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.BatchEncoding.word_ids) ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๅฏพๅฟœใ™ใ‚‹ๅ˜่ชžใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ—ใพใ™ใ€‚ +2. ็‰นๅˆฅใชใƒˆใƒผใ‚ฏใƒณ `[CLS]` ใจ `[SEP]` ใซใƒฉใƒ™ใƒซ `-100` ใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ€ใใ‚Œใ‚‰ใŒ PyTorch ๆๅคฑ้–ขๆ•ฐใซใ‚ˆใฃใฆ็„ก่ฆ–ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ ([CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html))ใ€‚ +3. ็‰นๅฎšใฎๅ˜่ชžใฎๆœ€ๅˆใฎใƒˆใƒผใ‚ฏใƒณใฎใฟใซใƒฉใƒ™ใƒซใ‚’ไป˜ใ‘ใพใ™ใ€‚ๅŒใ˜ๅ˜่ชžใฎไป–ใฎใ‚ตใƒ–ใƒˆใƒผใ‚ฏใƒณใซ `-100`ใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒณใจใƒฉใƒ™ใƒซใ‚’ๅ†่ชฟๆ•ดใ—ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ DistilBERT ใฎๆœ€ๅคงๅ…ฅๅŠ›้•ทไปฅไธ‹ใซๅˆ‡ใ‚Š่ฉฐใ‚ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + +```py +>>> def tokenize_and_align_labels(examples): +... tokenized_inputs = tokenizer(examples["tokens"], truncation=True, is_split_into_words=True) + +... labels = [] +... for i, label in enumerate(examples[f"ner_tags"]): +... word_ids = tokenized_inputs.word_ids(batch_index=i) # Map tokens to their respective word. +... previous_word_idx = None +... label_ids = [] +... for word_idx in word_ids: # Set the special tokens to -100. +... if word_idx is None: +... label_ids.append(-100) +... elif word_idx != previous_word_idx: # Only label the first token of a given word. +... label_ids.append(label[word_idx]) +... else: +... label_ids.append(-100) +... previous_word_idx = word_idx +... labels.append(label_ids) + +... tokenized_inputs["labels"] = labels +... return tokenized_inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ + +```py +>>> tokenized_wnut = wnut.map(tokenize_and_align_labels, batched=True) +``` + +ๆฌกใซใ€[`DataCollatโ€‹โ€‹orWithPadding`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใฎ้•ทใ•ใพใงๆ–‡ใ‚’ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ* ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ + + + + +```py +>>> from transformers import DataCollatorForTokenClassification + +>>> data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer) +``` + + + +```py +>>> from transformers import DataCollatorForTokenClassification + +>>> data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer, return_tensors="tf") +``` + + + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[seqeval](https://huggingface.co/spaces/evaluate-metric/seqeval) ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใ‚’่ชญใฟ่พผใฟใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ) ใƒกใƒˆใƒชใ‚ฏใ‚นใฎ่ชญใฟ่พผใฟใจ่จˆ็ฎ—ใฎๆ–นๆณ•ใซใคใ„ใฆ่ฉณใ—ใใฏใ€ใ“ใกใ‚‰ใ‚’ใ”่ฆงใใ ใ•ใ„)ใ€‚ Seqeval ใฏๅฎŸ้š›ใซใ€็ฒพๅบฆใ€ๅ†็พ็އใ€F1ใ€็ฒพๅบฆใชใฉใฎใ„ใใคใ‹ใฎใ‚นใ‚ณใ‚ขใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ + +```py +>>> import evaluate + +>>> seqeval = evaluate.load("seqeval") +``` + +ใพใš NER ใƒฉใƒ™ใƒซใ‚’ๅ–ๅพ—ใ—ใฆใ‹ใ‚‰ใ€็œŸใฎไบˆๆธฌใจ็œŸใฎใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +```py +>>> import numpy as np + +>>> labels = [label_list[i] for i in example[f"ner_tags"]] + + +>>> def compute_metrics(p): +... predictions, labels = p +... predictions = np.argmax(predictions, axis=2) + +... true_predictions = [ +... [label_list[p] for (p, l) in zip(prediction, label) if l != -100] +... for prediction, label in zip(predictions, labels) +... ] +... true_labels = [ +... [label_list[l] for (p, l) in zip(prediction, label) if l != -100] +... for prediction, label in zip(predictions, labels) +... ] + +... results = seqeval.compute(predictions=true_predictions, references=true_labels) +... return { +... "precision": results["overall_precision"], +... "recall": results["overall_recall"], +... "f1": results["overall_f1"], +... "accuracy": results["overall_accuracy"], +... } +``` + +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + +ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ€`id2label`ใจ`label2id`ใ‚’ไฝฟ็”จใ—ใฆใ€ไบˆๆƒณใ•ใ‚Œใ‚‹ ID ใจใใฎใƒฉใƒ™ใƒซใฎใƒžใƒƒใƒ—ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +```py +>>> id2label = { +... 0: "O", +... 1: "B-corporation", +... 2: "I-corporation", +... 3: "B-creative-work", +... 4: "I-creative-work", +... 5: "B-group", +... 6: "I-group", +... 7: "B-location", +... 8: "I-location", +... 9: "B-person", +... 10: "I-person", +... 11: "B-product", +... 12: "I-product", +... } +>>> label2id = { +... "O": 0, +... "B-corporation": 1, +... "I-corporation": 2, +... "B-creative-work": 3, +... "I-creative-work": 4, +... "B-group": 5, +... "I-group": 6, +... "B-location": 7, +... "I-location": 8, +... "B-person": 9, +... "I-person": 10, +... "B-product": 11, +... "I-product": 12, +... } +``` + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForTokenClassification`] ใ‚’ไฝฟ็”จใ—ใฆใ€ไบˆๆœŸใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใจใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ๆŒ‡ๅฎšใ—ใฆ DistilBERT ใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForTokenClassification, TrainingArguments, Trainer + +>>> model = AutoModelForTokenClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=13, id2label=id2label, label2id=label2id +... ) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ใ‚นใƒ†ใƒƒใƒ—ใฏ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ้€ฃ็ถšใ‚นใ‚ณใ‚ขใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใ€ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_wnut_model", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... num_train_epochs=2, +... weight_decay=0.01, +... eval_strategy="epoch", +... save_strategy="epoch", +... load_best_model_at_end=True, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_wnut["train"], +... eval_dataset=tokenized_wnut["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_train_epochs = 3 +>>> num_train_steps = (len(tokenized_wnut["train"]) // batch_size) * num_train_epochs +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=2e-5, +... num_train_steps=num_train_steps, +... weight_decay_rate=0.01, +... num_warmup_steps=0, +... ) +``` +ๆฌกใซใ€[`TFAutoModelForTokenClassification`] ใ‚’ไฝฟ็”จใ—ใฆใ€ไบˆๆœŸใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใจใƒฉใƒ™ใƒซ ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ๆŒ‡ๅฎšใ—ใฆ DistilBERT ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained( +... "distilbert/distilbert-base-uncased", num_labels=13, id2label=id2label, label2id=label2id +... ) +``` +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_wnut["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_validation_set = model.prepare_tf_dataset( +... tokenized_wnut["validation"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๆœ€ๅพŒใฎ 2 ใคใฎใ“ใจใฏใ€ไบˆๆธฌใ‹ใ‚‰้€ฃ็ถšใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ™ใ‚‹ใ“ใจใจใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใ‚‚ [Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +`compute_metrics` ้–ขๆ•ฐใ‚’ [`~transformers.KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ + + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +[`~transformers.PushToHubCallback`] ใงใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_wnut_model", +... tokenizer=tokenizer, +... ) +``` + +ๆฌกใซใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ใพใจใ‚ใฆใƒใƒณใƒ‰ใƒซใ—ใพใ™ใ€‚ + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_validation_set, epochs=3, callbacks=callbacks) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + +ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎใ‚ˆใ‚Š่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)ใ€‚ + + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใŸใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ„ใใคใ‹ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +```py +>>> text = "The Golden State Warriors are an American professional basketball team based in San Francisco." +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ NER ใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("ner", model="stevhliu/my_awesome_wnut_model") +>>> classifier(text) +[{'entity': 'B-location', + 'score': 0.42658573, + 'index': 2, + 'word': 'golden', + 'start': 4, + 'end': 10}, + {'entity': 'I-location', + 'score': 0.35856336, + 'index': 3, + 'word': 'state', + 'start': 11, + 'end': 16}, + {'entity': 'B-group', + 'score': 0.3064001, + 'index': 4, + 'word': 'warriors', + 'start': 17, + 'end': 25}, + {'entity': 'B-location', + 'score': 0.65523505, + 'index': 13, + 'word': 'san', + 'start': 80, + 'end': 83}, + {'entity': 'B-location', + 'score': 0.4668663, + 'index': 14, + 'word': 'francisco', + 'start': 84, + 'end': 93}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใฆ PyTorch ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> inputs = tokenizer(text, return_tensors="pt") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForTokenClassification + +>>> model = AutoModelForTokenClassification.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎ `id2label` ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใใ‚Œใ‚’ใƒ†ใ‚ญใ‚นใƒˆ ใƒฉใƒ™ใƒซใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> predictions = torch.argmax(logits, dim=2) +>>> predicted_token_class = [model.config.id2label[t.item()] for t in predictions[0]] +>>> predicted_token_class +['O', + 'O', + 'B-location', + 'I-location', + 'B-group', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'B-location', + 'B-location', + 'O', + 'O'] +``` + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€TensorFlow ใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> inputs = tokenizer(text, return_tensors="tf") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits`ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import TFAutoModelForTokenClassification + +>>> model = TFAutoModelForTokenClassification.from_pretrained("stevhliu/my_awesome_wnut_model") +>>> logits = model(**inputs).logits +``` + +ๆœ€ใ‚‚้ซ˜ใ„็ขบ็އใงใ‚ฏใƒฉใ‚นใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎ `id2label` ใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใใ‚Œใ‚’ใƒ†ใ‚ญใ‚นใƒˆ ใƒฉใƒ™ใƒซใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> predicted_token_class_ids = tf.math.argmax(logits, axis=-1) +>>> predicted_token_class = [model.config.id2label[t] for t in predicted_token_class_ids[0].numpy().tolist()] +>>> predicted_token_class +['O', + 'O', + 'B-location', + 'I-location', + 'B-group', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'O', + 'B-location', + 'B-location', + 'O', + 'O'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/translation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/translation.md new file mode 100644 index 0000000000000000000000000000000000000000..7fa45eac9cdb682e662bbb976a6be3b94b6c3cd0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/translation.md @@ -0,0 +1,415 @@ + + +# Translation + +[[open-in-colab]] + + + +็ฟป่จณใงใฏใ€ไธ€้€ฃใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚ใ‚‹่จ€่ชžใ‹ใ‚‰ๅˆฅใฎ่จ€่ชžใซๅค‰ๆ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใ‚ทใƒผใ‚ฑใƒณใ‚น้–“ๅ•้กŒใจใ—ใฆๅฎšๅผๅŒ–ใงใใ‚‹ใ„ใใคใ‹ใฎใ‚ฟใ‚นใ‚ฏใฎ 1 ใคใงใ‚ใ‚Šใ€็ฟป่จณใ‚„่ฆ็ด„ใชใฉใ€ๅ…ฅๅŠ›ใ‹ใ‚‰ไฝ•ใ‚‰ใ‹ใฎๅ‡บๅŠ›ใ‚’่ฟ”ใ™ใŸใ‚ใฎๅผทๅŠ›ใชใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใ™ใ€‚็ฟป่จณใ‚ทใ‚นใƒ†ใƒ ใฏ้€šๅธธใ€็•ฐใชใ‚‹่จ€่ชžใฎใƒ†ใ‚ญใ‚นใƒˆ้–“ใฎ็ฟป่จณใซไฝฟ็”จใ•ใ‚Œใพใ™ใŒใ€้Ÿณๅฃฐใ€ใพใŸใฏใƒ†ใ‚ญใ‚นใƒˆใ‹ใ‚‰้Ÿณๅฃฐใธใฎๅค‰ๆ›ใ‚„้Ÿณๅฃฐใ‹ใ‚‰ใƒ†ใ‚ญใ‚นใƒˆใธใฎๅค‰ๆ›ใชใฉใ€้Ÿณๅฃฐ้–“ใฎ็ต„ใฟๅˆใ‚ใ›ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [OPUS Books](https://huggingface.co/datasets/opus_books) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่‹ฑ่ชž-ใƒ•ใƒฉใƒณใ‚น่ชžใ‚ตใƒ–ใ‚ปใƒƒใƒˆใฎ [T5](https://huggingface.co/google-t5/t5-small) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆใ€่‹ฑ่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆฌกใฎๅฝขๅผใซ็ฟป่จณใ—ใพใ™ใ€‚ใƒ•ใƒฉใƒณใ‚น่ชžใ€‚ +2. ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/translation) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install transformers datasets evaluate sacrebleu +``` + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` +## Load OPUS Books dataset + +ใพใšใ€๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒฉใ‚คใƒ–ใƒฉใƒชใ‹ใ‚‰ [OPUS Books](https://huggingface.co/datasets/opus_books) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่‹ฑ่ชžใจใƒ•ใƒฉใƒณใ‚น่ชžใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚ + +```py +>>> from datasets import load_dataset + +>>> books = load_dataset("opus_books", "en-fr") +``` + +[`~datasets.Dataset.train_test_split`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒˆใƒฌใ‚คใƒณ ใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆ ใ‚ปใƒƒใƒˆใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ + + +```py +>>> books = books["train"].train_test_split(test_size=0.2) +``` + +ๆฌกใซใ€ไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> books["train"][0] +{'id': '90560', + 'translation': {'en': 'But this lofty plateau measured only a few fathoms, and soon we reentered Our Element.', + 'fr': 'Mais ce plateau รฉlevรฉ ne mesurait que quelques toises, et bientรดt nous fรปmes rentrรฉs dans notre รฉlรฉment.'}} +``` + +`translation`: ใƒ†ใ‚ญใ‚นใƒˆใฎ่‹ฑ่ชžใจใƒ•ใƒฉใƒณใ‚น่ชžใฎ็ฟป่จณใ€‚ + +## Preprocess + + + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€T5 ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใฆ่‹ฑ่ชžใจใƒ•ใƒฉใƒณใ‚น่ชžใฎ่จ€่ชžใƒšใ‚ขใ‚’ๅ‡ฆ็†ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> checkpoint = "google-t5/t5-small" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +``` + +ไฝœๆˆใ™ใ‚‹ๅ‰ๅ‡ฆ็†้–ขๆ•ฐใฏๆฌกใฎใ“ใจใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. T5 ใŒใ“ใ‚ŒใŒ็ฟป่จณใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚‹ใ“ใจใ‚’่ช่ญ˜ใงใใ‚‹ใ‚ˆใ†ใซใ€ๅ…ฅๅŠ›ใฎๅ‰ใซใƒ—ใƒญใƒณใƒ—โ€‹โ€‹ใƒˆใ‚’ไป˜ใ‘ใพใ™ใ€‚่ค‡ๆ•ฐใฎ NLP ใ‚ฟใ‚นใ‚ฏใŒๅฏ่ƒฝใชไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใฎใƒ—ใƒญใƒณใƒ—ใƒˆใŒๅฟ…่ฆใงใ™ใ€‚ +2. ่‹ฑ่ชžใฎ่ชžๅฝ™ใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใฆใƒ•ใƒฉใƒณใ‚น่ชžใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใ“ใจใฏใงใใชใ„ใŸใ‚ใ€ๅ…ฅๅŠ› (่‹ฑ่ชž) ใจใ‚ฟใƒผใ‚ฒใƒƒใƒˆ (ใƒ•ใƒฉใƒณใ‚น่ชž) ใ‚’ๅˆฅใ€…ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ +3. `max_length`ใƒ‘ใƒฉใƒกใƒผใ‚ฟใง่จญๅฎšใ•ใ‚ŒใŸๆœ€ๅคง้•ทใ‚’่ถ…ใˆใชใ„ใ‚ˆใ†ใซใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅˆ‡ใ‚Š่ฉฐใ‚ใพใ™ใ€‚ +```py +>>> source_lang = "en" +>>> target_lang = "fr" +>>> prefix = "translate English to French: " + + +>>> def preprocess_function(examples): +... inputs = [prefix + example[source_lang] for example in examples["translation"]] +... targets = [example[target_lang] for example in examples["translation"]] +... model_inputs = tokenizer(inputs, text_target=targets, max_length=128, truncation=True) +... return model_inputs +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.Dataset.map`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `batched=True` ใ‚’่จญๅฎšใ—ใฆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใงใ€`map` ้–ขๆ•ฐใ‚’้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ + +```py +>>> tokenized_books = books.map(preprocess_function, batched=True) +``` + +ๆฌกใซใ€[`DataCollatโ€‹โ€‹orForSeq2Seq`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’ๆœ€ๅคง้•ทใพใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใฎใงใฏใชใใ€็…งๅˆไธญใซใƒใƒƒใƒๅ†…ใฎๆœ€้•ทใฎ้•ทใ•ใพใงๆ–‡ใ‚’ *ๅ‹•็š„ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐ* ใ™ใ‚‹ๆ–นใŒๅŠน็އ็š„ใงใ™ใ€‚ + + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint) +``` + + + +```py +>>> from transformers import DataCollatorForSeq2Seq + +>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, return_tensors="tf") +``` + + + +## Evaluate + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅซใ‚ใ‚‹ใจใ€ๅคšใใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใ€่ฉ•ไพกใƒกใ‚ฝใƒƒใƒ‰ใ‚’ใ™ใฐใ‚„ใใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใ“ใฎใ‚ฟใ‚นใ‚ฏใงใฏใ€[SacreBLEU](https://huggingface.co/spaces/evaluate-metric/sacrebleu) ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ (๐Ÿค— Evaluate [ใ‚ฏใ‚คใƒƒใ‚ฏ ใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„) ) ใƒกใƒˆใƒชใ‚ฏใ‚นใฎ่ชญใฟ่พผใฟใจ่จˆ็ฎ—ๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ๆฌกใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚ + +```py +>>> import evaluate + +>>> metric = evaluate.load("sacrebleu") +``` + +ๆฌกใซใ€ไบˆๆธฌใจใƒฉใƒ™ใƒซใ‚’ [`~evaluate.EvaluationModule.compute`] ใซๆธกใ—ใฆ SacreBLEU ใ‚นใ‚ณใ‚ขใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +```py +>>> import numpy as np + + +>>> def postprocess_text(preds, labels): +... preds = [pred.strip() for pred in preds] +... labels = [[label.strip()] for label in labels] + +... return preds, labels + + +>>> def compute_metrics(eval_preds): +... preds, labels = eval_preds +... if isinstance(preds, tuple): +... preds = preds[0] +... decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) + +... labels = np.where(labels != -100, labels, tokenizer.pad_token_id) +... decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) + +... decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels) + +... result = metric.compute(predictions=decoded_preds, references=decoded_labels) +... result = {"bleu": result["score"]} + +... prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds] +... result["gen_len"] = np.mean(prediction_lens) +... result = {k: round(v, 4) for k, v in result.items()} +... return result +``` +ใ“ใ‚Œใง`compute_metrics`้–ขๆ•ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใจใใซใ“ใฎ้–ขๆ•ฐใซๆˆปใ‚Šใพใ™ใ€‚ + +## Train + + + + + +[`Trainer`] ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใ“](../training#train-with-pytorch-trainer) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`AutoModelForSeq2SeqLM`] ใ‚’ไฝฟ็”จใ—ใฆ T5 ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ใ‚นใƒ†ใƒƒใƒ—ใฏ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`Seq2SeqTrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ”ฏไธ€ใฎๅฟ…้ ˆใƒ‘ใƒฉใƒกใƒผใ‚ฟใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ `output_dir` ใงใ™ใ€‚ `push_to_hub=True`ใ‚’่จญๅฎšใ—ใฆใ€ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ—ใพใ™ (ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€Hugging Face ใซใ‚ตใ‚คใƒณใ‚คใƒณใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™)ใ€‚ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซใ€[`Trainer`] ใฏ SacreBLEU ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่ฉ•ไพกใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ—ใพใ™ใ€‚ +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใ€ใŠใ‚ˆใณ `compute_metrics` ้–ขๆ•ฐใจใจใ‚‚ใซ [`Seq2SeqTrainer`] ใซๆธกใ—ใพใ™ใ€‚ +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + + +```py +>>> training_args = Seq2SeqTrainingArguments( +... output_dir="my_awesome_opus_books_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... per_device_train_batch_size=16, +... per_device_eval_batch_size=16, +... weight_decay=0.01, +... save_total_limit=3, +... num_train_epochs=2, +... predict_with_generate=True, +... fp16=True, +... push_to_hub=True, +... ) + +>>> trainer = Seq2SeqTrainer( +... model=model, +... args=training_args, +... train_dataset=tokenized_books["train"], +... eval_dataset=tokenized_books["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras ใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎๅพฎ่ชฟๆ•ดใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](../training#train-a-tensorflow-model-with-keras) ใฎๅŸบๆœฌ็š„ใชใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + +TensorFlow ใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ใซใฏใ€ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใƒผ้–ขๆ•ฐใ€ๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ€ใŠใ‚ˆใณใ„ใใคใ‹ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ใ“ใจใ‹ใ‚‰ๅง‹ใ‚ใพใ™ใ€‚ + + +```py +>>> from transformers import AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +ๆฌกใซใ€[`TFAutoModelForSeq2SeqLM`] ใ‚’ไฝฟ็”จใ—ใฆ T5 ใ‚’ใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint) +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ `tf.data.Dataset` ๅฝขๅผใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... tokenized_books["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... tokenized_books["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒขใƒ‡ใƒซใ‚’่จญๅฎšใ—ใพใ™ใ€‚ Transformers ใƒขใƒ‡ใƒซใซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ฟใ‚นใ‚ฏ้–ข้€ฃใฎๆๅคฑ้–ขๆ•ฐใŒใ‚ใ‚‹ใŸใ‚ใ€ๆฌกใฎๅ ดๅˆใ‚’้™คใใ€ๆๅคฑ้–ขๆ•ฐใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใฏใชใ„ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # No loss argument! +``` +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๅ‰ใซใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๆœ€ๅพŒใฎ 2 ใคใฎใ“ใจใฏใ€ไบˆๆธฌใ‹ใ‚‰ SacreBLEU ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ—ใ€ใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๆ–นๆณ•ใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใฉใกใ‚‰ใ‚‚ [Keras ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏ](../main_classes/keras_callbacks) ใ‚’ไฝฟ็”จใ—ใฆ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +`compute_metrics` ้–ขๆ•ฐใ‚’ [`~transformers.KerasMetricCallback`] ใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set) +``` + +[`~transformers.PushToHubCallback`] ใงใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒ—ใƒƒใ‚ทใƒฅใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚ + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="my_awesome_opus_books_model", +... tokenizer=tokenizer, +... ) +``` + +ๆฌกใซใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ใพใจใ‚ใฆใƒใƒณใƒ‰ใƒซใ—ใพใ™ใ€‚ + +```py +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +ใคใ„ใซใ€ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณๆคœ่จผใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใ‚จใƒใƒƒใ‚ฏๆ•ฐใ€ใ‚ณใƒผใƒซใƒใƒƒใ‚ฏใ‚’ๆŒ‡ๅฎšใ—ใฆ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใ‚’ๅ‘ผใณๅ‡บใ—ใ€ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏ่‡ชๅ‹•็š„ใซใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ€่ชฐใงใ‚‚ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + + + + +็ฟป่จณ็”จใซใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๆ–นๆณ•ใฎ่ฉณ็ดฐใชไพ‹ใซใคใ„ใฆใฏใ€ๅฏพๅฟœใ™ใ‚‹ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ +[PyTorch ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb) +ใพใŸใฏ [TensorFlow ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb)ใ€‚ + + + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๅˆฅใฎ่จ€่ชžใซ็ฟป่จณใ—ใŸใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚’่€ƒใˆๅ‡บใ—ใพใ™ใ€‚ T5 ใฎๅ ดๅˆใ€ไฝœๆฅญไธญใฎใ‚ฟใ‚นใ‚ฏใซๅฟœใ˜ใฆๅ…ฅๅŠ›ใซๆŽฅ้ ญ่พžใ‚’ไป˜ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚่‹ฑ่ชžใ‹ใ‚‰ใƒ•ใƒฉใƒณใ‚น่ชžใซ็ฟป่จณใ™ใ‚‹ๅ ดๅˆใฏใ€ไปฅไธ‹ใซ็คบใ™ใ‚ˆใ†ใซๅ…ฅๅŠ›ใซๆŽฅ้ ญ่พžใ‚’ไป˜ใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> text = "translate English to French: Legumes share resources with nitrogen-fixing bacteria." +``` + +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ็ฟป่จณ็”จใฎ`pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใใ‚Œใซๆธกใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +# Change `xx` to the language of the input and `yy` to the language of the desired output. +# Examples: "en" for English, "fr" for French, "de" for German, "es" for Spanish, "zh" for Chinese, etc; translation_en_to_fr translates English to French +# You can view all the lists of languages here - https://huggingface.co/languages +>>> translator = pipeline("translation_xx_to_yy", model="my_awesome_opus_books_model") +>>> translator(text) +[{'translation_text': 'Legumes partagent des ressources avec des bactรฉries azotantes.'}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + + + + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€`input_ids` ใ‚’ PyTorch ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_opus_books_model") +>>> inputs = tokenizer(text, return_tensors="pt").input_ids +``` + +[`~generation.GenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ็ฟป่จณใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Text Generation](../main_classes/text_generation) API ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +>>> from transformers import AutoModelForSeq2SeqLM + +>>> model = AutoModelForSeq2SeqLM.from_pretrained("my_awesome_opus_books_model") +>>> outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Les lignรฉes partagent des ressources avec des bactรฉries enfixant l'azote.' +``` + + + + +`input_ids`ใ‚’ TensorFlow ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆ่ฟ”ใ—ใพใ™ใ€‚ tensors: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_opus_books_model") +>>> inputs = tokenizer(text, return_tensors="tf").input_ids +``` + +[`~transformers.generation_tf_utils.TFGenerationMixin.generate`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ็ฟป่จณใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ใ•ใพใ–ใพใชใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅใจ็”Ÿๆˆใ‚’ๅˆถๅพกใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[Text Generation](../main_classes/text_generation) API ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```py +>>> from transformers import TFAutoModelForSeq2SeqLM + +>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("my_awesome_opus_books_model") +>>> outputs = model.generate(inputs, max_new_tokens=40, do_sample=True, top_k=30, top_p=0.95) +``` + +็”Ÿๆˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณ ID ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใซๆˆปใ—ใพใ™ใ€‚ + +```py +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Les lugumes partagent les ressources avec des bactรฉries fixatrices d'azote.' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/video_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/video_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..e7e7803c9408582f7baa0a5f89c5d58acd17489f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/video_classification.md @@ -0,0 +1,498 @@ + + +# Video classification + +[[open-in-colab]] + + +ใƒ“ใƒ‡ใ‚ชๅˆ†้กžใฏใ€ใƒ“ใƒ‡ใ‚ชๅ…จไฝ“ใซใƒฉใƒ™ใƒซใพใŸใฏใ‚ฏใƒฉใ‚นใ‚’ๅ‰ฒใ‚Šๅฝ“ใฆใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ใƒ“ใƒ‡ใ‚ชใซใฏใ€ๅ„ใƒ“ใƒ‡ใ‚ชใซ 1 ใคใฎใ‚ฏใƒฉใ‚นใฎใฟใŒๅซใพใ‚Œใ‚‹ใ“ใจใŒๆœŸๅพ…ใ•ใ‚Œใพใ™ใ€‚ใƒ“ใƒ‡ใ‚ชๅˆ†้กžใƒขใƒ‡ใƒซใฏใƒ“ใƒ‡ใ‚ชใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚Šใ€ใƒ“ใƒ‡ใ‚ชใŒใฉใฎใ‚ฏใƒฉใ‚นใซๅฑžใ™ใ‚‹ใ‹ใซใคใ„ใฆใฎไบˆๆธฌใ‚’่ฟ”ใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ€ใƒ“ใƒ‡ใ‚ชใฎๅ†…ๅฎนใ‚’ๅˆ†้กžใงใใพใ™ใ€‚ใƒ“ใƒ‡ใ‚ชๅˆ†้กžใฎๅฎŸ้š›ใฎใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใฏใ‚ขใ‚ฏใ‚ทใƒงใƒณ/ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ“ใƒ†ใ‚ฃ่ช่ญ˜ใงใ‚ใ‚Šใ€ใƒ•ใ‚ฃใƒƒใƒˆใƒใ‚น ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซๅฝน็ซ‹ใกใพใ™ใ€‚ใพใŸใ€่ฆ–่ฆš้šœๅฎณใฎใ‚ใ‚‹ไบบใซใจใฃใฆใ€็‰นใซ้€šๅ‹คๆ™‚ใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +1. [UCF101](https://www.crcv.ucf.edu/) ใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใง [VideoMAE](https://huggingface.co/docs/transformers/main/en/model_doc/videomae) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ data/UCF101.php) ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ +2. ๅพฎ่ชฟๆ•ดใ—ใŸใƒขใƒ‡ใƒซใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ + + + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใจไบ’ๆ›ๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[ใ‚ฟใ‚นใ‚ฏใƒšใƒผใ‚ธ](https://huggingface.co/tasks/video-classification) ใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + + + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +```bash +pip install -q pytorchvideo transformers evaluate +``` + +[PyTorchVideo](https://pytorchvideo.org/) (`pytorchvideo` ใจๅ‘ผใฐใ‚Œใพใ™) ใ‚’ไฝฟ็”จใ—ใฆใƒ“ใƒ‡ใ‚ชใ‚’ๅ‡ฆ็†ใ—ใ€ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใงใใ‚‹ใ‚ˆใ†ใซใ€Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Load UCF101 dataset + +ใพใšใ€[UCF-101 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://www.crcv.ucf.edu/data/UCF101.php) ใฎใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซใ•ใ‚‰ใซๆ™‚้–“ใ‚’่ฒปใ‚„ใ™ๅ‰ใซใ€ๅฎŸ้จ“ใ—ใฆใ™ในใฆใŒๆฉŸ่ƒฝใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ๆฉŸไผšใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +```py +>>> from huggingface_hub import hf_hub_download + +>>> hf_dataset_identifier = "sayakpaul/ucf101-subset" +>>> filename = "UCF101_subset.tar.gz" +>>> file_path = hf_hub_download(repo_id=hf_dataset_identifier, filename=filename, repo_type="dataset") +``` + +ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใŸๅพŒใ€ๅœง็ธฎใ‚ขใƒผใ‚ซใ‚คใƒ–ใ‚’ๆŠฝๅ‡บใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> import tarfile + +>>> with tarfile.open(file_path) as t: +... t.extractall(".") +``` + +ๅคงใพใ‹ใซ่จ€ใ†ใจใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏๆฌกใฎใ‚ˆใ†ใซๆง‹ๆˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +```bash +UCF101_subset/ + train/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... + val/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... + test/ + BandMarching/ + video_1.mp4 + video_2.mp4 + ... + Archery + video_1.mp4 + video_2.mp4 + ... + ... +``` + +(`sorted`)ใ•ใ‚ŒใŸ ใƒ“ใƒ‡ใ‚ช ใƒ‘ใ‚นใฏๆฌกใฎใ‚ˆใ†ใซ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + + +```bash +... +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g07_c04.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g07_c06.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g08_c01.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g09_c02.avi', +'UCF101_subset/train/ApplyEyeMakeup/v_ApplyEyeMakeup_g09_c06.avi' +... +``` + +ๅŒใ˜ใ‚ฐใƒซใƒผใƒ—/ใ‚ทใƒผใƒณใซๅฑžใ™ใ‚‹ใƒ“ใƒ‡ใ‚ช ใ‚ฏใƒชใƒƒใƒ—ใŒใ‚ใ‚Šใ€ใƒ“ใƒ‡ใ‚ช ใƒ•ใ‚กใ‚คใƒซ ใƒ‘ใ‚นใงใฏใ‚ฐใƒซใƒผใƒ—ใŒ`g`ใง็คบใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€`v_ApplyEyeMakeup_g07_c04.avi`ใ‚„`v_ApplyEyeMakeup_g07_c06.avi`ใชใฉใงใ™ใ€‚ + +ๆคœ่จผใจ่ฉ•ไพกใฎๅˆ†ๅ‰ฒใงใฏใ€[ใƒ‡ใƒผใ‚ฟๆผๆดฉ](https://www.kaggle.com/code/alexisbcook/data-leakage) ใ‚’้˜ฒใใŸใ‚ใซใ€ๅŒใ˜ใ‚ฐใƒซใƒผใƒ—/ใ‚ทใƒผใƒณใ‹ใ‚‰ใฎใƒ“ใƒ‡ใ‚ช ใ‚ฏใƒชใƒƒใƒ—ใ‚’ไฝฟ็”จใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‚ตใƒ–ใ‚ปใƒƒใƒˆใงใฏใ€ใ“ใฎๆƒ…ๅ ฑใŒ่€ƒๆ…ฎใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใซๅญ˜ๅœจใ™ใ‚‹ใƒฉใƒ™ใƒซใฎใ‚ปใƒƒใƒˆใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ใพใŸใ€ใƒขใƒ‡ใƒซใ‚’ๅˆๆœŸๅŒ–ใ™ใ‚‹ใจใใซๅฝน็ซ‹ใค 2 ใคใฎ่พžๆ›ธใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +* `label2id`: ใ‚ฏใƒฉใ‚นๅใ‚’ๆ•ดๆ•ฐใซใƒžใƒƒใƒ—ใ—ใพใ™ใ€‚ +* `id2label`: ๆ•ดๆ•ฐใ‚’ใ‚ฏใƒฉใ‚นๅใซใƒžใƒƒใƒ”ใƒณใ‚ฐใ—ใพใ™ใ€‚ + + +```py +>>> class_labels = sorted({str(path).split("/")[2] for path in all_video_file_paths}) +>>> label2id = {label: i for i, label in enumerate(class_labels)} +>>> id2label = {i: label for label, i in label2id.items()} + +>>> print(f"Unique classes: {list(label2id.keys())}.") + +# Unique classes: ['ApplyEyeMakeup', 'ApplyLipstick', 'Archery', 'BabyCrawling', 'BalanceBeam', 'BandMarching', 'BaseballPitch', 'Basketball', 'BasketballDunk', 'BenchPress']. +``` + +ๅ€‹ๆ€ง็š„ใชใ‚ฏใƒฉใ‚นใŒ10็จฎ้กžใ‚ใ‚Šใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใ‚ปใƒƒใƒˆใซใฏใ€ใ‚ฏใƒฉใ‚นใ”ใจใซ 30 ๅ€‹ใฎใƒ“ใƒ‡ใ‚ชใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Load a model to fine-tune + +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใจใใ‚Œใซ้–ข้€ฃใ™ใ‚‹็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‹ใ‚‰ใƒ“ใƒ‡ใ‚ชๅˆ†้กžใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซใฏไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใŒไป˜ๅฑžใ—ใฆใŠใ‚Šใ€ๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅ‰ๅ‡ฆ็†ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝœๆˆใ™ใ‚‹ใจใใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +```py +>>> from transformers import VideoMAEImageProcessor, VideoMAEForVideoClassification + +>>> model_ckpt = "MCG-NJU/videomae-base" +>>> image_processor = VideoMAEImageProcessor.from_pretrained(model_ckpt) +>>> model = VideoMAEForVideoClassification.from_pretrained( +... model_ckpt, +... label2id=label2id, +... id2label=id2label, +... ignore_mismatched_sizes=True, # provide this in case you're planning to fine-tune an already fine-tuned checkpoint +... ) +``` + +ใƒขใƒ‡ใƒซใฎใƒญใƒผใƒ‰ไธญใซใ€ๆฌกใฎ่ญฆๅ‘ŠใŒ่กจ็คบใ•ใ‚Œใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```bash +Some weights of the model checkpoint at MCG-NJU/videomae-base were not used when initializing VideoMAEForVideoClassification: [..., 'decoder.decoder_layers.1.attention.output.dense.bias', 'decoder.decoder_layers.2.attention.attention.key.weight'] +- This IS expected if you are initializing VideoMAEForVideoClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). +- This IS NOT expected if you are initializing VideoMAEForVideoClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). +Some weights of VideoMAEForVideoClassification were not initialized from the model checkpoint at MCG-NJU/videomae-base and are newly initialized: ['classifier.bias', 'classifier.weight'] +You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference. +``` + +ใ“ใฎ่ญฆๅ‘Šใฏใ€ไธ€้ƒจใฎ้‡ใฟ (ใŸใจใˆใฐใ€`classifier`ๅฑคใฎ้‡ใฟใจใƒใ‚คใ‚ขใ‚น) ใ‚’็ ดๆฃ„ใ—ใ€ไป–ใฎใ„ใใคใ‹ใฎ้‡ใฟ (ๆ–ฐใ—ใ„`classifier`ๅฑคใฎ้‡ใฟใจใƒใ‚คใ‚ขใ‚น) ใ‚’ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ใ“ใ‚Œใฏไบˆๆƒณใ•ใ‚Œใ‚‹ใ“ใจใงใ™ใ€‚ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸ้‡ใฟใ‚’ๆŒใŸใชใ„ๆ–ฐใ—ใ„้ ญ้ƒจใ‚’่ฟฝๅŠ ใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ“ใฎใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใจใƒฉใ‚คใƒ–ใƒฉใƒชใŒ่ญฆๅ‘Šใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใพใ•ใซ็งใŸใกใŒ่กŒใŠใ†ใจใ—ใฆใ„ใ‚‹ใ‚‚ใฎใงใ™ใ€‚ใ™ใ‚‹ใ€‚ + +**ๆณจๆ„** [ใ“ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/MCG-NJU/videomae-base-finetuned-kinetics) ใฏใ€ๅŒๆง˜ใฎใƒ€ใ‚ฆใƒณใ‚นใƒˆใƒชใƒผใƒ ใงๅพฎ่ชฟๆ•ดใ•ใ‚Œใฆใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒๅ–ๅพ—ใ•ใ‚ŒใŸใŸใ‚ใ€ใ“ใฎใ‚ฟใ‚นใ‚ฏใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ™ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ‹ใชใ‚Šใฎใƒ‰ใƒกใ‚คใƒณใฎ้‡่ค‡ใŒใ‚ใ‚‹ใ‚ฟใ‚นใ‚ฏใ€‚ `MCG-NJU/videomae-base-finetuned-kinetics` ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆๅ–ๅพ—ใ—ใŸ [ใ“ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/sayakpaul/videomae-base-finetuned-kinetics-finetuned-ucf101-subset) ใ‚’็ขบ่ชใงใใพใ™ใ€‚ -ใ‚ญใƒใƒ†ใ‚ฃใ‚ฏใ‚น`ใ€‚ + +## Prepare the datasets for training + +ใƒ“ใƒ‡ใ‚ชใฎๅ‰ๅ‡ฆ็†ใซใฏใ€[PyTorchVideo ใƒฉใ‚คใƒ–ใƒฉใƒช](https://pytorchvideo.org/) ใ‚’ๅˆฉ็”จใ—ใพใ™ใ€‚ใพใšใ€ๅฟ…่ฆใชไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใพใ™ใ€‚ + + +```py +>>> import pytorchvideo.data + +>>> from pytorchvideo.transforms import ( +... ApplyTransformToKey, +... Normalize, +... RandomShortSideScale, +... RemoveKey, +... ShortSideScale, +... UniformTemporalSubsample, +... ) + +>>> from torchvision.transforms import ( +... Compose, +... Lambda, +... RandomCrop, +... RandomHorizontalFlip, +... Resize, +... ) +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅค‰ๆ›ใซใฏใ€ๅ‡ไธ€ใชๆ™‚้–“ใ‚ตใƒ–ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ€ใƒ”ใ‚ฏใ‚ปใƒซๆญฃ่ฆๅŒ–ใ€ใƒฉใƒณใƒ€ใƒ  ใ‚ฏใƒญใƒƒใƒ”ใƒณใ‚ฐใ€ใŠใ‚ˆใณใƒฉใƒณใƒ€ใƒ ใชๆฐดๅนณๅ่ปขใ‚’็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใ—ใพใ™ใ€‚ๆคœ่จผใŠใ‚ˆใณ่ฉ•ไพกใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅค‰ๆ›ใงใฏใ€ใƒฉใƒณใƒ€ใƒ ใชใƒˆใƒชใƒŸใƒณใ‚ฐใจๆฐดๅนณๅ่ปขใ‚’้™คใใ€ๅŒใ˜ๅค‰ๆ›ใƒใ‚งใƒผใƒณใ‚’็ถญๆŒใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎๅค‰ๆ›ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[PyTorchVideo ใฎๅ…ฌๅผใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://pytorchvideo.org) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใซ้–ข้€ฃไป˜ใ‘ใ‚‰ใ‚ŒใŸ`image_processor`ใ‚’ไฝฟ็”จใ—ใฆใ€ๆฌกใฎๆƒ…ๅ ฑใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +* ใƒ“ใƒ‡ใ‚ช ใƒ•ใƒฌใƒผใƒ ใฎใƒ”ใ‚ฏใ‚ปใƒซใŒๆญฃ่ฆๅŒ–ใ•ใ‚Œใ‚‹็”ปๅƒใฎๅนณๅ‡ๅ€คใจๆจ™ๆบ–ๅๅทฎใ€‚ +* ใƒ“ใƒ‡ใ‚ช ใƒ•ใƒฌใƒผใƒ ใฎใ‚ตใ‚คใ‚บใŒๅค‰ๆ›ดใ•ใ‚Œใ‚‹็ฉบ้–“่งฃๅƒๅบฆใ€‚ + +ใพใšใ€ใ„ใใคใ‹ใฎๅฎšๆ•ฐใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```py +>>> mean = image_processor.image_mean +>>> std = image_processor.image_std +>>> if "shortest_edge" in image_processor.size: +... height = width = image_processor.size["shortest_edge"] +>>> else: +... height = image_processor.size["height"] +... width = image_processor.size["width"] +>>> resize_to = (height, width) + +>>> num_frames_to_sample = model.config.num_frames +>>> sample_rate = 4 +>>> fps = 30 +>>> clip_duration = num_frames_to_sample * sample_rate / fps +``` + +ๆฌกใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ›บๆœ‰ใฎๅค‰ๆ›ใจใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใใ‚Œใžใ‚Œๅฎš็พฉใ—ใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ปใƒƒใƒˆใ‹ใ‚‰ๅง‹ใ‚ใพใ™: + + +```py +>>> train_transform = Compose( +... [ +... ApplyTransformToKey( +... key="video", +... transform=Compose( +... [ +... UniformTemporalSubsample(num_frames_to_sample), +... Lambda(lambda x: x / 255.0), +... Normalize(mean, std), +... RandomShortSideScale(min_size=256, max_size=320), +... RandomCrop(resize_to), +... RandomHorizontalFlip(p=0.5), +... ] +... ), +... ), +... ] +... ) + +>>> train_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "train"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("random", clip_duration), +... decode_audio=False, +... transform=train_transform, +... ) +``` + +ๅŒใ˜ไธ€้€ฃใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใ‚’ๆคœ่จผใ‚ปใƒƒใƒˆใจ่ฉ•ไพกใ‚ปใƒƒใƒˆใซ้ฉ็”จใงใใพใ™ใ€‚ + + +```py +>>> val_transform = Compose( +... [ +... ApplyTransformToKey( +... key="video", +... transform=Compose( +... [ +... UniformTemporalSubsample(num_frames_to_sample), +... Lambda(lambda x: x / 255.0), +... Normalize(mean, std), +... Resize(resize_to), +... ] +... ), +... ), +... ] +... ) + +>>> val_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "val"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("uniform", clip_duration), +... decode_audio=False, +... transform=val_transform, +... ) + +>>> test_dataset = pytorchvideo.data.Ucf101( +... data_path=os.path.join(dataset_root_path, "test"), +... clip_sampler=pytorchvideo.data.make_clip_sampler("uniform", clip_duration), +... decode_audio=False, +... transform=val_transform, +... ) +``` + +**ๆณจๆ„**: ไธŠ่จ˜ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฏใ€[ๅ…ฌๅผ PyTorchVideo ใ‚ตใƒณใƒ—ใƒซ](https://pytorchvideo.org/docs/tutorial_classification#dataset) ใ‹ใ‚‰ๅ–ๅพ—ใ—ใŸใ‚‚ใฎใงใ™ใ€‚ [`pytorchvideo.data.Ucf101()`](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html#pytorchvideo.data.Ucf101) ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ UCF-101 ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ๅ†…้ƒจใงใฏใ€[`pytorchvideo.data.labeled_video_dataset.LabeledVideoDataset`](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html#pytorchvideo.data.LabeledVideoDataset) ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ฟ”ใ—ใพใ™ใ€‚ `LabeledVideoDataset` ใ‚ฏใƒฉใ‚นใฏใ€PyTorchVideo ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎใ™ในใฆใฎใƒ“ใƒ‡ใ‚ชใฎๅŸบๆœฌใ‚ฏใƒฉใ‚นใงใ™ใ€‚ใ—ใŸใŒใฃใฆใ€PyTorchVideo ใงๆ—ข่ฃฝใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใชใ„ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใŸใ„ๅ ดๅˆใฏใ€ใใ‚Œใซๅฟœใ˜ใฆ `LabeledVideoDataset` ใ‚ฏใƒฉใ‚นใ‚’ๆ‹กๅผตใงใใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€`data`API [ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://pytorchvideo.readthedocs.io/en/latest/api/data/data.html)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใพใŸใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒๅŒๆง˜ใฎๆง‹้€  (ไธŠใซ็คบใ—ใŸใ‚‚ใฎ) ใซๅพ“ใฃใฆใ„ใ‚‹ๅ ดๅˆใฏใ€`pytorchvideo.data.Ucf101()` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจๅ•้กŒใชใๅ‹•ไฝœใ™ใ‚‹ใฏใšใงใ™ใ€‚ + +`num_videos` ๅผ•ๆ•ฐใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใจใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใฎใƒ“ใƒ‡ใ‚ชใฎๆ•ฐใ‚’็Ÿฅใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + + +```py +>>> print(train_dataset.num_videos, val_dataset.num_videos, test_dataset.num_videos) +# (300, 30, 75) +``` + +## Visualize the preprocessed video for better debugging + +```py +>>> import imageio +>>> import numpy as np +>>> from IPython.display import Image + +>>> def unnormalize_img(img): +... """Un-normalizes the image pixels.""" +... img = (img * std) + mean +... img = (img * 255).astype("uint8") +... return img.clip(0, 255) + +>>> def create_gif(video_tensor, filename="sample.gif"): +... """Prepares a GIF from a video tensor. +... +... The video tensor is expected to have the following shape: +... (num_frames, num_channels, height, width). +... """ +... frames = [] +... for video_frame in video_tensor: +... frame_unnormalized = unnormalize_img(video_frame.permute(1, 2, 0).numpy()) +... frames.append(frame_unnormalized) +... kargs = {"duration": 0.25} +... imageio.mimsave(filename, frames, "GIF", **kargs) +... return filename + +>>> def display_gif(video_tensor, gif_name="sample.gif"): +... """Prepares and displays a GIF from a video tensor.""" +... video_tensor = video_tensor.permute(1, 0, 2, 3) +... gif_filename = create_gif(video_tensor, gif_name) +... return Image(filename=gif_filename) + +>>> sample_video = next(iter(train_dataset)) +>>> video_tensor = sample_video["video"] +>>> display_gif(video_tensor) +``` + +
+ Person playing basketball +
+ +## Train the model + +๐Ÿค— Transformers ใฎ [`Trainer`](https://huggingface.co/docs/transformers/main_classes/trainer) ใ‚’ใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซๅˆฉ็”จใ—ใพใ™ใ€‚ `Trainer`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆง‹ๆˆใจ่ฉ•ไพกใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅฎš็พฉใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆœ€ใ‚‚้‡่ฆใชใฎใฏ [`TrainingArguments`](https://huggingface.co/transformers/main_classes/trainer.html#transformers.TrainingArguments) ใงใ€ใ“ใ‚Œใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆง‹ๆˆใ™ใ‚‹ใŸใ‚ใฎใ™ในใฆใฎๅฑžๆ€งใ‚’ๅซใ‚€ใ‚ฏใƒฉใ‚นใงใ™ใ€‚ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅ‡บๅŠ›ใƒ•ใ‚ฉใƒซใƒ€ใƒผๅใŒๅฟ…่ฆใงใ™ใ€‚ใพใŸใ€๐Ÿค— Hub ไธŠใฎใƒขใƒ‡ใƒซ ใƒชใƒใ‚ธใƒˆใƒชๅ†…ใฎใ™ในใฆใฎๆƒ…ๅ ฑใ‚’ๅŒๆœŸใ™ใ‚‹ใฎใซใ‚‚ๅฝน็ซ‹ใกใพใ™ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใฎใปใจใ‚“ใฉใฏไธ€็›ฎ็žญ็„ถใงใ™ใŒใ€ใ“ใ“ใง้žๅธธใซ้‡่ฆใชใฎใฏ`remove_unused_columns=False`ใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฎๅ‘ผใณๅ‡บใ—้–ขๆ•ฐใงไฝฟ็”จใ•ใ‚Œใชใ„ๆฉŸ่ƒฝใŒๅ‰Š้™คใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ`True`ใงใ™ใ€‚ใ“ใ‚Œใฏใ€้€šๅธธใ€ๆœชไฝฟ็”จใฎ็‰นๅพดๅˆ—ใ‚’ๅ‰Š้™คใ—ใ€ใƒขใƒ‡ใƒซใฎๅ‘ผใณๅ‡บใ—้–ขๆ•ฐใธใฎๅ…ฅๅŠ›ใ‚’่งฃๅ‡ใ—ใ‚„ใ™ใใ™ใ‚‹ใ“ใจใŒ็†ๆƒณ็š„ใงใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ใŸใ ใ—ใ€ใ“ใฎๅ ดๅˆใ€`pixel_values` (ใƒขใƒ‡ใƒซใŒๅ…ฅๅŠ›ใงๆœŸๅพ…ใ™ใ‚‹ๅฟ…้ ˆใ‚ญใƒผใงใ™) ใ‚’ไฝœๆˆใ™ใ‚‹ใซใฏใ€ๆœชไฝฟ็”จใฎๆฉŸ่ƒฝ (็‰นใซ`video`) ใŒๅฟ…่ฆใงใ™ใ€‚ + +```py +>>> from transformers import TrainingArguments, Trainer + +>>> model_name = model_ckpt.split("/")[-1] +>>> new_model_name = f"{model_name}-finetuned-ucf101-subset" +>>> num_epochs = 4 + +>>> args = TrainingArguments( +... new_model_name, +... remove_unused_columns=False, +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=batch_size, +... per_device_eval_batch_size=batch_size, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... max_steps=(train_dataset.num_videos // batch_size) * num_epochs, +... ) +``` + +`pytorchvideo.data.Ucf101()` ใซใ‚ˆใฃใฆ่ฟ”ใ•ใ‚Œใ‚‹ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏ `__len__` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่ฃ…ใ—ใฆใ„ใพใ›ใ‚“ใ€‚ใใฎใŸใ‚ใ€`TrainingArguments`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใจใใซ`max_steps`ใ‚’ๅฎš็พฉใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๆฌกใซใ€ไบˆๆธฌใ‹ใ‚‰ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ™ใ‚‹้–ขๆ•ฐใ‚’ๅฎš็พฉใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€ใ“ใ‚Œใ‹ใ‚‰ใƒญใƒผใƒ‰ใ™ใ‚‹`metric`ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ๅฟ…่ฆใชๅ‰ๅ‡ฆ็†ใฏใ€ไบˆๆธฌใ•ใ‚ŒใŸใƒญใ‚ธใƒƒใƒˆใฎ argmax ใ‚’ๅ–ๅพ—ใ™ใ‚‹ใ“ใจใ ใ‘ใงใ™ใ€‚ + +```py +import evaluate + +metric = evaluate.load("accuracy") + + +def compute_metrics(eval_pred): + predictions = np.argmax(eval_pred.predictions, axis=1) + return metric.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +**่ฉ•ไพกใซ้–ขใ™ใ‚‹ๆณจๆ„ไบ‹้ …**: + +[VideoMAE ่ซ–ๆ–‡](https://huggingface.co/papers/2203.12602) ใงใฏใ€่‘—่€…ใฏๆฌกใฎ่ฉ•ไพกๆˆฆ็•ฅใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ๅฝผใ‚‰ใฏใƒ†ใ‚นใƒˆ ใƒ“ใƒ‡ใ‚ชใ‹ใ‚‰ใฎใ„ใใคใ‹ใฎใ‚ฏใƒชใƒƒใƒ—ใงใƒขใƒ‡ใƒซใ‚’่ฉ•ไพกใ—ใ€ใใ‚Œใ‚‰ใฎใ‚ฏใƒชใƒƒใƒ—ใซใ•ใพใ–ใพใชใ‚ฏใƒญใƒƒใƒ—ใ‚’้ฉ็”จใ—ใฆใ€ๅˆ่จˆใ‚นใ‚ณใ‚ขใ‚’ๅ ฑๅ‘Šใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ๅ˜็ด”ใ•ใจ็ฐกๆฝ”ใ•ใ‚’ไฟใคใŸใ‚ใซใ€ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใใ‚Œใ‚’่€ƒๆ…ฎใ—ใพใ›ใ‚“ใ€‚ + +ใพใŸใ€ใ‚ตใƒณใƒ—ใƒซใ‚’ใพใจใ‚ใฆใƒใƒƒใƒๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใ‚‹ `collatโ€‹โ€‹e_fn` ใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ๅ„ใƒใƒƒใƒใฏใ€`pixel_values` ใจ `labels` ใจใ„ใ† 2 ใคใฎใ‚ญใƒผใงๆง‹ๆˆใ•ใ‚Œใพใ™ใ€‚ + + +```py +>>> def collate_fn(examples): +... # permute to (num_frames, num_channels, height, width) +... pixel_values = torch.stack( +... [example["video"].permute(1, 0, 2, 3) for example in examples] +... ) +... labels = torch.tensor([example["label"] for example in examples]) +... return {"pixel_values": pixel_values, "labels": labels} +``` + +ๆฌกใซใ€ใ“ใ‚Œใ‚‰ใ™ในใฆใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใจใจใ‚‚ใซ`Trainer`ใซๆธกใ™ใ ใ‘ใงใ™ใ€‚ + +```py +>>> trainer = Trainer( +... model, +... args, +... train_dataset=train_dataset, +... eval_dataset=val_dataset, +... processing_class=image_processor, +... compute_metrics=compute_metrics, +... data_collator=collate_fn, +... ) +``` + +ใ™ใงใซใƒ‡ใƒผใ‚ฟใ‚’ๅ‰ๅ‡ฆ็†ใ—ใฆใ„ใ‚‹ใฎใซใ€ใชใœใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใจใ—ใฆ`image_processor`ใ‚’ๆธกใ—ใŸใฎใ‹ไธๆ€่ญฐใซๆ€ใ†ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ใ‚คใƒกใƒผใ‚ธ ใƒ—ใƒญใ‚ปใƒƒใ‚ตๆง‹ๆˆใƒ•ใ‚กใ‚คใƒซ (JSON ใจใ—ใฆไฟๅญ˜) ใ‚‚ใƒใƒ–ไธŠใฎใƒชใƒใ‚ธใƒˆใƒชใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใ ใ‘ใงใ™ใ€‚ + +ๆฌกใซใ€`train` ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> train_results = trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~transformers.Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€่ชฐใ‚‚ใŒใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + +## Inference + +ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +ๆŽจ่ซ–ใฎใŸใ‚ใซใƒ“ใƒ‡ใ‚ชใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ + +```py +>>> sample_test_video = next(iter(test_dataset)) +``` + +
+ Teams playing basketball +
+ +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`](https://huggingface.co/docs/transformers/main/en/main_classes/pipelines#transformers.VideoClassificationPipeline). ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒ“ใƒ‡ใ‚ชๅˆ†้กž็”จใฎ` pipeline`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใซใƒ“ใƒ‡ใ‚ชใ‚’ๆธกใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import pipeline + +>>> video_cls = pipeline(model="my_awesome_video_cls_model") +>>> video_cls("https://huggingface.co/datasets/sayakpaul/ucf101-subset/resolve/main/v_BasketballDunk_g14_c06.avi") +[{'score': 0.9272987842559814, 'label': 'BasketballDunk'}, + {'score': 0.017777055501937866, 'label': 'BabyCrawling'}, + {'score': 0.01663011871278286, 'label': 'BalanceBeam'}, + {'score': 0.009560945443809032, 'label': 'BandMarching'}, + {'score': 0.0068979403004050255, 'label': 'BaseballPitch'}] +``` + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€`pipeline`ใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +>>> def run_inference(model, video): +... # (num_frames, num_channels, height, width) +... perumuted_sample_test_video = video.permute(1, 0, 2, 3) +... inputs = { +... "pixel_values": perumuted_sample_test_video.unsqueeze(0), +... "labels": torch.tensor( +... [sample_test_video["label"]] +... ), # this can be skipped if you don't have labels available. +... } + +... device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +... inputs = {k: v.to(device) for k, v in inputs.items()} +... model = model.to(device) + +... # forward pass +... with torch.no_grad(): +... outputs = model(**inputs) +... logits = outputs.logits + +... return logits +``` + +ๆฌกใซใ€ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€`logits `ใ‚’่ฟ”ใ—ใพใ™ใ€‚ + +```py +>>> logits = run_inference(trained_model, sample_test_video["video"]) +``` + +`logits` ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +```py +>>> predicted_class_idx = logits.argmax(-1).item() +>>> print("Predicted class:", model.config.id2label[predicted_class_idx]) +# Predicted class: BasketballDunk +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/visual_question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/visual_question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..3231cba5e3af718e47adab6c3f48847a5d3761f7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/visual_question_answering.md @@ -0,0 +1,405 @@ + + +# Visual Question Answering + +[[open-in-colab]] + +Visual Question Answering (VQA) ใฏใ€็”ปๅƒใซๅŸบใฅใ„ใฆ่‡ช็”ฑๅฝขๅผใฎ่ณชๅ•ใซ็ญ”ใˆใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ใ“ใฎใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใธใฎๅ…ฅๅŠ›ใฏ้€šๅธธใ€็”ปๅƒใจ่ณชๅ•ใฎ็ต„ใฟๅˆใ‚ใ›ใงใ‚ใ‚Šใ€ๅ‡บๅŠ›ใฏ +่‡ช็„ถ่จ€่ชžใง่กจ็พใ•ใ‚ŒใŸ็ญ”ใˆใ€‚ + +VQA ใฎๆณจ็›ฎใ™ในใไฝฟ็”จไพ‹ใซใฏๆฌกใฎใ‚ˆใ†ใชใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ +* ่ฆ–่ฆš้šœๅฎณ่€…ๅ‘ใ‘ใฎใ‚ขใ‚ฏใ‚ปใ‚ทใƒ“ใƒชใƒ†ใ‚ฃ ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใ€‚ +* ๆ•™่‚ฒ: ่ฌ›็พฉใ‚„ๆ•™็ง‘ๆ›ธใง็คบใ•ใ‚Œใฆใ„ใ‚‹่ฆ–่ฆš็š„ใช่ณ‡ๆ–™ใซใคใ„ใฆ่ณชๅ•ใ‚’ๆŠ•ใ’ใ‹ใ‘ใ‚‹ใ“ใจใ€‚ VQA ใฏใ€ใ‚คใƒณใ‚ฟใƒฉใ‚ฏใƒ†ใ‚ฃใƒ–ใชๅš็‰ฉ้คจใฎๅฑ•็คบ็‰ฉใ‚„ๅฒ่ทกใงใ‚‚ๅˆฉ็”จใงใใพใ™ใ€‚ +* ใ‚ซใ‚นใ‚ฟใƒžใƒผ ใ‚ตใƒผใƒ“ใ‚นใจ้›ปๅญๅ•†ๅ–ๅผ•: VQA ใฏใ€ใƒฆใƒผใ‚ถใƒผใŒ่ฃฝๅ“ใซใคใ„ใฆ่ณชๅ•ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใงใƒฆใƒผใ‚ถใƒผ ใ‚จใ‚ฏใ‚นใƒšใƒชใ‚จใƒณใ‚นใ‚’ๅ‘ไธŠใ•ใ›ใพใ™ใ€‚ +* ็”ปๅƒๆคœ็ดข: VQA ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ€็‰นๅฎšใฎ็‰นๅพดใ‚’ๆŒใค็”ปๅƒใ‚’ๆคœ็ดขใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ใƒฆใƒผใ‚ถใƒผใฏใ€Œ็Šฌใฏใ„ใพใ™ใ‹?ใ€ใจๅฐ‹ใญใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไธ€้€ฃใฎ็”ปๅƒใ‹ใ‚‰็ŠฌใŒๅ†™ใฃใฆใ„ใ‚‹ใ™ในใฆใฎ็”ปๅƒใ‚’ๆคœ็ดขใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆใณใพใ™ใ€‚ + +- [`Graphcore/vqa` ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/datasets/Graphcore/vqa) ไธŠใงๅˆ†้กž VQA ใƒขใƒ‡ใƒซใ€็‰นใซ [ViLT](../model_doc/vilt) ใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ +- ๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸ ViLT ใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใ—ใพใ™ใ€‚ +- BLIP-2 ใชใฉใฎ็”Ÿๆˆใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ VQA ๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +## Fine-tuning ViLT + +ViLT ใƒขใƒ‡ใƒซใฏใ€Vision Transformer (ViT) ใซใƒ†ใ‚ญใ‚นใƒˆๅŸ‹ใ‚่พผใฟใ‚’็ต„ใฟ่พผใ‚“ใงใŠใ‚Šใ€ๆœ€ๅฐ้™ใฎ่จญ่จˆใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚ +่ฆ–่ฆšใจ่จ€่ชžใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ (VLP)ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใฏใ€ใ„ใใคใ‹ใฎไธ‹ๆตใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใพใ™ใ€‚ VQA ใ‚ฟใ‚นใ‚ฏใฎๅ ดๅˆใ€ๅˆ†้กžๅญ +head ใฏๆœ€ไธŠ้ƒจ (`[CLS]` ใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚็š„ใช้ž่กจ็คบ็Šถๆ…‹ใฎๆœ€ไธŠ้ƒจใซใ‚ใ‚‹็ทšๅฝขๅฑค) ใซ้…็ฝฎใ•ใ‚Œใ€ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚Œใพใ™ใ€‚ +ใ—ใŸใŒใฃใฆใ€่ฆ–่ฆš็š„่ณชๅ•ๅฟœ็ญ”ใฏ **ๅˆ†้กžๅ•้กŒ** ใจใ—ใฆๆ‰ฑใ‚ใ‚Œใพใ™ใ€‚ + +BLIPใ€BLIP-2ใ€InstructBLIP ใชใฉใฎๆœ€่ฟ‘ใฎใƒขใƒ‡ใƒซใฏใ€VQA ใ‚’็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใจใ—ใฆๆ‰ฑใ„ใพใ™ใ€‚ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎๅพŒๅŠใงใฏใ€ +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ VQA ๆŽจ่ซ–ใซใใ‚Œใ‚‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q transformers datasets +``` +ใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒŸใƒฅใƒ‹ใƒ†ใ‚ฃใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ Hugging Face ใ‚ขใ‚ซใ‚ฆใƒณใƒˆใซใƒญใ‚ฐใ‚คใƒณใ—ใฆใ€๐Ÿค— ใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ่กจ็คบใ•ใ‚ŒใŸใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ…ฅๅŠ›ใ—ใฆใƒญใ‚ฐใ‚คใƒณใ—ใพใ™ใ€‚ + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚ฐใƒญใƒผใƒใƒซๅค‰ๆ•ฐใจใ—ใฆๅฎš็พฉใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> model_checkpoint = "dandelin/vilt-b32-mlm" +``` + +## Load the data + +่ชฌๆ˜Žใฎ็›ฎ็š„ใงใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆณจ้‡ˆไป˜ใใฎ่ฆ–่ฆš็š„ใช่ณชๅ•ใซ็ญ”ใˆใ‚‹ใ€ŒGraphcore/vqaใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ้žๅธธใซๅฐใ•ใชใ‚ตใƒณใƒ—ใƒซใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +ๅฎŒๅ…จใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏ [๐Ÿค— Hub](https://huggingface.co/datasets/Graphcore/vqa) ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +[`Graphcore/vqa` ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ](https://huggingface.co/datasets/Graphcore/vqa) ใฎไปฃใ‚ใ‚Šใซใ€ +ๅ…ฌๅผ [VQA ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ ใƒšใƒผใ‚ธ](https://visualqa.org/download.html) ใ‹ใ‚‰ๅŒใ˜ใƒ‡ใƒผใ‚ฟใ‚’ๆ‰‹ๅ‹•ใงๅ–ๅพ—ใ—ใพใ™ใ€‚ใƒ•ใ‚ฉใƒญใƒผใ—ใŸใ„ๅ ดๅˆใฏใ€ +ใ‚ซใ‚นใ‚ฟใƒ  ใƒ‡ใƒผใ‚ฟใ‚’ไฝฟ็”จใ—ใŸใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€[็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝœๆˆใ™ใ‚‹](https://huggingface.co/docs/datasets/image_dataset#loading-script) ๆ–นๆณ•ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ +๐Ÿค— ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎใ‚ฌใ‚คใƒ‰ใ€‚ + +ๆคœ่จผๅˆ†ๅ‰ฒใ‹ใ‚‰ๆœ€ๅˆใฎ 200 ๅ€‹ใฎไพ‹ใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๆฉŸ่ƒฝใ‚’่ชฟในใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```python +>>> from datasets import load_dataset + +>>> dataset = load_dataset("Graphcore/vqa", split="validation[:200]") +>>> dataset +Dataset({ + features: ['question', 'question_type', 'question_id', 'image_id', 'answer_type', 'label'], + num_rows: 200 +}) +``` +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ็‰นๅพดใ‚’็†่งฃใ™ใ‚‹ใŸใ‚ใซไพ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> dataset[0] +{'question': 'Where is he looking?', + 'question_type': 'none of the above', + 'question_id': 262148000, + 'image_id': '/root/.cache/huggingface/datasets/downloads/extracted/ca733e0e000fb2d7a09fbcc94dbfe7b5a30750681d0e965f8e0a23b1c2f98c75/val2014/COCO_val2014_000000262148.jpg', + 'answer_type': 'other', + 'label': {'ids': ['at table', 'down', 'skateboard', 'table'], + 'weights': [0.30000001192092896, + 1.0, + 0.30000001192092896, + 0.30000001192092896]}} +``` + +ใ“ใฎใ‚ฟใ‚นใ‚ฏใซ้–ข้€ฃใ™ใ‚‹ๆฉŸ่ƒฝใซใฏๆฌกใฎใ‚‚ใฎใŒใ‚ใ‚Šใพใ™ใ€‚ +* `question`: ็”ปๅƒใ‹ใ‚‰ๅ›ž็ญ”ใ™ใ‚‹่ณชๅ• +* `image_id`: ่ณชๅ•ใŒๅ‚็…งใ™ใ‚‹็”ปๅƒใธใฎใƒ‘ใ‚น +* `label`: ๆณจ้‡ˆ + +ๆฎ‹ใ‚ŠใฎๆฉŸ่ƒฝใฏๅฟ…่ฆใชใ„ใฎใงๅ‰Š้™คใงใใพใ™ใ€‚ + + +```py +>>> dataset = dataset.remove_columns(['question_type', 'question_id', 'answer_type']) +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€`label`ๆฉŸ่ƒฝใซใฏใ€ใ•ใพใ–ใพใชใƒ’ใƒฅใƒผใƒžใƒณใƒปใ‚ขใƒŽใƒ†ใƒผใ‚ฟใƒผใซใ‚ˆใฃใฆๅŽ้›†ใ•ใ‚ŒใŸใ€ๅŒใ˜่ณชๅ•ใซๅฏพใ™ใ‚‹่ค‡ๆ•ฐใฎๅ›ž็ญ” (ใ“ใ“ใงใฏ`id`ใจๅ‘ผใณใพใ™) ใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ +่ณชๅ•ใซๅฏพใ™ใ‚‹็ญ”ใˆใฏไธป่ฆณ็š„ใชใ‚‚ใฎใซใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ใ“ใฎๅ ดๅˆใ€ๅ•้กŒใฏ "ๅฝผใฏใฉใ“ใ‚’่ฆ‹ใฆใ„ใ‚‹ใฎใ‹๏ผŸ"ใจใ„ใ†ใ“ใจใงใ™ใ€‚ไธ€้ƒจใฎไบบใ€… +ใ“ใ‚Œใซใฏ "ใƒ€ใ‚ฆใƒณ" ใจใ„ใ†ๆณจ้‡ˆใŒไป˜ใ‘ใ‚‰ใ‚Œใ€ไป–ใฎใ‚‚ใฎใซใฏ "ใƒ†ใƒผใƒ–ใƒซใง" ใจใ„ใ†ๆณจ้‡ˆใŒไป˜ใ‘ใ‚‰ใ‚Œใ€ๅˆฅใฎๆณจ้‡ˆใซใฏ "ใ‚นใ‚ฑใƒผใƒˆใƒœใƒผใƒ‰" ใจใ„ใ†ๆณจ้‡ˆใŒไป˜ใ‘ใ‚‰ใ‚Œใพใ—ใŸใ€‚ + +็”ปๅƒใ‚’่ฆ‹ใฆใ€ใฉใฎ็ญ”ใˆใ‚’ๅ‡บใ™ใ‹ใ‚’่€ƒใˆใฆใใ ใ•ใ„ใ€‚ + +```python +>>> from PIL import Image + +>>> image = Image.open(dataset[0]['image_id']) +>>> image +``` + +
+ VQA Image Example +
+ + +่ณชๅ•ใจๅ›ž็ญ”ใฎใ‚ใ„ใพใ„ใ•ใฎใŸใ‚ใ€ใ“ใฎใ‚ˆใ†ใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใƒžใƒซใƒใƒฉใƒ™ใƒซๅˆ†้กžๅ•้กŒใจใ—ใฆๆ‰ฑใ‚ใ‚Œใพใ™ ( +่ค‡ๆ•ฐใฎๅ›ž็ญ”ใŒๆœ‰ๅŠนใงใ‚ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™)ใ€‚ใ•ใ‚‰ใซใ€ใƒฏใƒณใƒ›ใƒƒใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒ™ใ‚ฏใƒˆใƒซใ‚’ไฝœๆˆใ™ใ‚‹ใ ใ‘ใงใฏใชใใ€ +ๆณจ้‡ˆๅ†…ใซ็‰นๅฎšใฎๅ›ž็ญ”ใŒๅ‡บ็พใ—ใŸๅ›žๆ•ฐใซๅŸบใฅใใ‚ฝใƒ•ใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใ€‚ + +ใŸใจใˆใฐใ€ไธŠใฎไพ‹ใงใฏใ€"down"ใจใ„ใ†ๅ›ž็ญ”ใŒไป–ใฎๅ›ž็ญ”ใ‚ˆใ‚Šใ‚‚้ ป็นใซ้ธๆŠžใ•ใ‚Œใ‚‹ใŸใ‚ใ€ +ใ‚นใ‚ณใ‚ข (ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใฏ`weight`ใจๅ‘ผใฐใ‚Œใพใ™) ใฏ 1.0 ใงใ€ๆฎ‹ใ‚Šใฎๅ›ž็ญ”ใฎใ‚นใ‚ณใ‚ขใฏ 1.0 ๆœชๆบ€ใงใ™ใ€‚ + +ๅพŒใง้ฉๅˆ‡ใชๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ€2 ใคใฎ่พžๆ›ธใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚ +ใƒฉใƒ™ใƒซๅใ‚’ๆ•ดๆ•ฐใซๅค‰ๆ›ใ™ใ‚‹ใ€ใพใŸใฏใใฎ้€†: + +```py +>>> import itertools + +>>> labels = [item['ids'] for item in dataset['label']] +>>> flattened_labels = list(itertools.chain(*labels)) +>>> unique_labels = list(set(flattened_labels)) + +>>> label2id = {label: idx for idx, label in enumerate(unique_labels)} +>>> id2label = {idx: label for label, idx in label2id.items()} +``` + +ใƒžใƒƒใƒ”ใƒณใ‚ฐใŒใงใใŸใฎใงใ€ๆ–‡ๅญ—ๅˆ—ใฎๅ›ž็ญ”ใ‚’ใใฎ ID ใซ็ฝฎใๆ›ใˆใ€ใ•ใ‚‰ใซๅ‰ๅ‡ฆ็†ใ‚’ใ‚ˆใ‚Šไพฟๅˆฉใซใ™ใ‚‹ใŸใ‚ใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ•ใƒฉใƒƒใƒˆๅŒ–ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```python +>>> def replace_ids(inputs): +... inputs["label"]["ids"] = [label2id[x] for x in inputs["label"]["ids"]] +... return inputs + + +>>> dataset = dataset.map(replace_ids) +>>> flat_dataset = dataset.flatten() +>>> flat_dataset.features +{'question': Value(dtype='string', id=None), + 'image_id': Value(dtype='string', id=None), + 'label.ids': Sequence(feature=Value(dtype='int64', id=None), length=-1, id=None), + 'label.weights': Sequence(feature=Value(dtype='float64', id=None), length=-1, id=None)} +``` + +## Preprocessing data + +ๆฌกใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€ViLT ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€ใƒขใƒ‡ใƒซใฎ็”ปๅƒใƒ‡ใƒผใ‚ฟใจใƒ†ใ‚ญใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ +[`ViltProcessor`] ใฏใ€BERT ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใจ ViLT ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไพฟๅˆฉใชๅ˜ไธ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใซใƒฉใƒƒใƒ—ใ—ใพใ™ใ€‚ + +```py +>>> from transformers import ViltProcessor + +>>> processor = ViltProcessor.from_pretrained(model_checkpoint) +``` + +ใƒ‡ใƒผใ‚ฟใ‚’ๅ‰ๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€[`ViltProcessor`] ใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใจ่ณชๅ•ใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใฏไฝฟ็”จใ—ใพใ™ +[`BertTokenizerFast`] ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆ ใƒ‡ใƒผใ‚ฟใฎ `input_ids`ใ€`attention_mask`ใ€ใŠใ‚ˆใณ `token_type_ids` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +็”ปๅƒใซ้–ขใ—ใฆใฏใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใฏ [`ViltImageProcessor`] ใ‚’ๅˆฉ็”จใ—ใฆ็”ปๅƒใฎใ‚ตใ‚คใ‚บๅค‰ๆ›ดใจๆญฃ่ฆๅŒ–ใ‚’่กŒใ„ใ€`pixel_values` ใจ `pixel_mask` ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใฏใ™ในใฆๅ†…้ƒจใง่กŒใ‚ใ‚Œใ€`processor`ใ‚’ๅ‘ผใณๅ‡บใ™ใ ใ‘ใงๆธˆใฟใพใ™ใ€‚ใŸใ ใ—ใ€ใใ‚Œใงใ‚‚ๅฟ…่ฆใชใฎใฏใ€ +ๅฏพ่ฑกใฎใƒฉใƒ™ใƒซใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ใ“ใฎ่กจ็พใงใฏใ€ๅ„่ฆ็ด ใฏ่€ƒใˆใ‚‰ใ‚Œใ‚‹็ญ”ใˆ (ใƒฉใƒ™ใƒซ) ใซๅฏพๅฟœใ—ใพใ™ใ€‚ๆญฃ่งฃใฎๅ ดๅˆใ€่ฆ็ด ใฏไฟๆŒใ•ใ‚Œใพใ™ใ€‚ +ใใ‚Œใžใ‚Œใฎใ‚นใ‚ณใ‚ข (้‡ใฟ) ใŒ่จญๅฎšใ•ใ‚Œใ€ๆฎ‹ใ‚Šใฎ่ฆ็ด ใฏ 0 ใซ่จญๅฎšใ•ใ‚Œใพใ™ใ€‚ + +ๆฌกใฎ้–ขๆ•ฐใฏใ€็”ปๅƒใจ่ณชๅ•ใซ `processor` ใ‚’้ฉ็”จใ—ใ€ไธŠใง่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซใƒฉใƒ™ใƒซใ‚’ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ—ใพใ™ใ€‚ + +```py +>>> import torch + +>>> def preprocess_data(examples): +... image_paths = examples['image_id'] +... images = [Image.open(image_path) for image_path in image_paths] +... texts = examples['question'] + +... encoding = processor(images, texts, padding="max_length", truncation=True, return_tensors="pt") + +... for k, v in encoding.items(): +... encoding[k] = v.squeeze() + +... targets = [] + +... for labels, scores in zip(examples['label.ids'], examples['label.weights']): +... target = torch.zeros(len(id2label)) + +... for label, score in zip(labels, scores): +... target[label] = score + +... targets.append(target) + +... encoding["labels"] = targets + +... return encoding +``` + +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets [`~datasets.map`] ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ `map` ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎ่ค‡ๆ•ฐใฎ่ฆ็ด ใ‚’ไธ€ๅบฆใซๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€`batched=True` ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ใ“ใฎๆ™‚็‚นใงใ€ไธ่ฆใชๅˆ—ใฏ่‡ช็”ฑใซๅ‰Š้™คใ—ใฆใใ ใ•ใ„ใ€‚ + + +```py +>>> processed_dataset = flat_dataset.map(preprocess_data, batched=True, remove_columns=['question','question_type', 'question_id', 'image_id', 'answer_type', 'label.ids', 'label.weights']) +>>> processed_dataset +Dataset({ + features: ['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values', 'pixel_mask', 'labels'], + num_rows: 200 +}) +``` + +ๆœ€ๅพŒใฎใ‚นใƒ†ใƒƒใƒ—ใจใ—ใฆใ€[`DefaultDataCollatโ€‹โ€‹or`] ใ‚’ไฝฟ็”จใ—ใฆใ‚ตใƒณใƒ—ใƒซใฎใƒใƒƒใƒใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + +## Train the model + +ใ“ใ‚Œใงใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ™ใ‚‹ๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸใ€‚ [`ViltForQuestionAnswering`] ใง ViLT ใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒฉใƒ™ใƒซใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ +ใƒฉใƒ™ใƒซใƒžใƒƒใƒ”ใƒณใ‚ฐใจใจใ‚‚ใซ: + +```py +>>> from transformers import ViltForQuestionAnswering + +>>> model = ViltForQuestionAnswering.from_pretrained(model_checkpoint, num_labels=len(id2label), id2label=id2label, label2id=label2id) +``` + +ใ“ใฎๆ™‚็‚นใงๆฎ‹ใฃใฆใ„ใ‚‹ใ‚นใƒ†ใƒƒใƒ—ใฏ 3 ใคใ ใ‘ใงใ™ใ€‚ + +1. [`TrainingArguments`] ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ + +```py +>>> from transformers import TrainingArguments + +>>> repo_id = "MariaK/vilt_finetuned_200" + +>>> training_args = TrainingArguments( +... output_dir=repo_id, +... per_device_train_batch_size=4, +... num_train_epochs=20, +... save_steps=200, +... logging_steps=50, +... learning_rate=5e-5, +... save_total_limit=2, +... remove_unused_columns=False, +... push_to_hub=True, +... ) +``` + +2. ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ‚’ใƒขใƒ‡ใƒซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใ€ใƒ‡ใƒผใ‚ฟ็…งๅˆๅ™จใจใจใ‚‚ใซ [`Trainer`] ใซๆธกใ—ใพใ™ใ€‚ + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=processed_dataset, +... processing_class=processor, +... ) +``` + +3. [`~Trainer.train`] ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™ใ€‚ + +```py +>>> trainer.train() +``` + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸใ‚‰ใ€ [`~Trainer.push_to_hub`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใƒใƒ–ใซๅ…ฑๆœ‰ใ—ใ€๐Ÿค— ใƒใƒ–ใงๆœ€็ต‚ใƒขใƒ‡ใƒซใ‚’ๅ…ฑๆœ‰ใ—ใพใ™ใ€‚ + +```py +>>> trainer.push_to_hub() +``` + +## Inference + +ViLT ใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€๐Ÿค— Hub ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ—ใŸใฎใงใ€ใใ‚Œใ‚’ๆŽจ่ซ–ใซไฝฟ็”จใงใใพใ™ใ€‚ใ‚‚ใฃใจใ‚‚ๅ˜็ด”ใช +ๆŽจ่ซ–็”จใซๅพฎ่ชฟๆ•ดใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ๆ–นๆณ•ใฏใ€ใใ‚Œใ‚’ [`pipeline`] ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```py +>>> from transformers import pipeline + +>>> pipe = pipeline("visual-question-answering", model="MariaK/vilt_finetuned_200") +``` + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใฎใƒขใƒ‡ใƒซใฏ 200 ใฎไพ‹ใงใฎใฟใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๅคšใใ‚’ๆœŸๅพ…ใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ๅฐ‘ใชใใจใ‚‚ใใ‚ŒใŒใ‚ใ‚‹ใ‹ใฉใ†ใ‹่ฆ‹ใฆใฟใพใ—ใ‚‡ใ† +ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ไฝ•ใ‹ใ‚’ๅญฆ็ฟ’ใ—ใ€ๆŽจ่ซ–ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใŸใ‚ใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ๆœ€ๅˆใฎไพ‹ใ‚’ๅ–ใ‚Šๅ‡บใ—ใพใ™ใ€‚ + +```py +>>> example = dataset[0] +>>> image = Image.open(example['image_id']) +>>> question = example['question'] +>>> print(question) +>>> pipe(image, question, top_k=1) +"Where is he looking?" +[{'score': 0.5498199462890625, 'answer': 'down'}] +``` + +ใ‚ใพใ‚Š่‡ชไฟกใŒใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ใƒขใƒ‡ใƒซใฏ็ขบใ‹ใซไฝ•ใ‹ใ‚’ๅญฆ็ฟ’ใ—ใพใ—ใŸใ€‚ใ‚ˆใ‚Šๅคšใใฎไพ‹ใจใ‚ˆใ‚Š้•ทใ„ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ†ใจใ€ใฏใ‚‹ใ‹ใซ่‰ฏใ„็ตๆžœใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +ๅฟ…่ฆใซๅฟœใ˜ใฆใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎ็ตๆžœใ‚’ๆ‰‹ๅ‹•ใง่ค‡่ฃฝใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +1. ็”ปๅƒใจ่ณชๅ•ใ‚’ๅ–ๅพ—ใ—ใ€ใƒขใƒ‡ใƒซใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซ็”จใซๆบ–ๅ‚™ใ—ใพใ™ใ€‚ +2. ใƒขใƒ‡ใƒซใ‚’้€šใ˜ใฆ็ตๆžœใพใŸใฏๅ‰ๅ‡ฆ็†ใ‚’่ปข้€ใ—ใพใ™ใ€‚ +3. ใƒญใ‚ธใƒƒใƒˆใ‹ใ‚‰ใ€ๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ๅ›ž็ญ”ใฎ ID ใ‚’ๅ–ๅพ—ใ—ใ€`id2label` ใงๅฎŸ้š›ใฎๅ›ž็ญ”ใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + +```py +>>> processor = ViltProcessor.from_pretrained("MariaK/vilt_finetuned_200") + +>>> image = Image.open(example['image_id']) +>>> question = example['question'] + +>>> # prepare inputs +>>> inputs = processor(image, question, return_tensors="pt") + +>>> model = ViltForQuestionAnswering.from_pretrained("MariaK/vilt_finetuned_200") + +>>> # forward pass +>>> with torch.no_grad(): +... outputs = model(**inputs) + +>>> logits = outputs.logits +>>> idx = logits.argmax(-1).item() +>>> print("Predicted answer:", model.config.id2label[idx]) +Predicted answer: down +``` + +## Zero-shot VQA + +ไปฅๅ‰ใฎใƒขใƒ‡ใƒซใงใฏใ€VQA ใ‚’ๅˆ†้กžใ‚ฟใ‚นใ‚ฏใจใ—ใฆๆ‰ฑใ„ใพใ—ใŸใ€‚ BLIPใ€BLIP-2ใ€InstructBLIP ใ‚ขใƒ—ใƒญใƒผใƒใชใฉใฎไธ€้ƒจใฎๆœ€่ฟ‘ใฎใƒขใƒ‡ใƒซ +็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใจใ—ใฆใฎ VQAใ€‚ [BLIP-2](../model_doc/blip-2) ใ‚’ไพ‹ใจใ—ใฆ่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚ๆ–ฐใ—ใ„ใƒ“ใ‚ธใƒฅใ‚ขใƒซ่จ€่ชžใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅฐŽๅ…ฅใ—ใพใ—ใŸ +ไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ“ใ‚ธใƒงใƒณ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจ LLM ใ‚’ไปปๆ„ใซ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จโ€‹โ€‹ใงใใ‚‹ใƒ‘ใƒฉใƒ€ใ‚คใƒ  (่ฉณ็ดฐใซใคใ„ใฆใฏใ€[BLIP-2 ใƒ–ใƒญใ‚ฐๆŠ•็จฟ](https://huggingface.co/blog/blip-2) ใ‚’ๅ‚็…ง)ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€่ฆ–่ฆš็š„ใช่ณชๅ•ๅฟœ็ญ”ใ‚’ๅซใ‚€่ค‡ๆ•ฐใฎ่ฆ–่ฆš่จ€่ชžใ‚ฟใ‚นใ‚ฏใงๆœ€ๅ…ˆ็ซฏใฎ็ตๆžœใ‚’้”ๆˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ใ“ใฎใƒขใƒ‡ใƒซใ‚’ VQA ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ—ใ‚‡ใ†ใ€‚ใพใšใ€ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใพใ—ใ‚‡ใ†ใ€‚ใ“ใ“ใงใฏใƒขใƒ‡ใƒซใ‚’ๆ˜Ž็คบ็š„ใซ้€ไฟกใ—ใพใ™ใ€‚ +GPU (ๅˆฉ็”จๅฏ่ƒฝใชๅ ดๅˆ)ใ€‚ใ“ใ‚Œใฏ [`Trainer`] ใŒ่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆ™‚ใซไบ‹ๅ‰ใซ่กŒใ†ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใงใ—ใŸใ€‚ + + +```py +>>> from transformers import AutoProcessor, Blip2ForConditionalGeneration +>>> import torch + +>>> processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b") +>>> model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16) +>>> device = "cuda" if torch.cuda.is_available() else "cpu" +>>> model.to(device) +``` + +ใƒขใƒ‡ใƒซใฏ็”ปๅƒใจใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‹ใŸใ‚ใ€VQA ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๆœ€ๅˆใฎไพ‹ใจใพใฃใŸใๅŒใ˜็”ปๅƒใจ่ณชๅ•ใฎใƒšใ‚ขใ‚’ไฝฟ็”จใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +```py +>>> example = dataset[0] +>>> image = Image.open(example['image_id']) +>>> question = example['question'] +``` + +่ฆ–่ฆš็š„ใช่ณชๅ•ๅฟœ็ญ”ใ‚ฟใ‚นใ‚ฏใซ BLIP-2 ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒ†ใ‚ญใ‚นใƒˆ ใƒ—ใƒญใƒณใƒ—ใƒˆใŒ็‰นๅฎšใฎๅฝขๅผ (`Question: {} Answer:`) ใซๅพ“ใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +```py +>>> prompt = f"Question: {question} Answer:" +``` + +ๆฌกใซใ€ใƒขใƒ‡ใƒซใฎใƒ—ใƒญใ‚ปใƒƒใ‚ตใง็”ปๅƒ/ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ๅ‰ๅ‡ฆ็†ใ—ใ€ๅ‡ฆ็†ใ•ใ‚ŒใŸๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ๅ‡บๅŠ›ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> inputs = processor(image, text=prompt, return_tensors="pt").to(device, torch.float16) + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip() +>>> print(generated_text) +"He is looking at the crowd" +``` + +ใ”่ฆงใฎใจใŠใ‚Šใ€ใƒขใƒ‡ใƒซใฏ็พค่ก†ใจ้ก”ใฎๅ‘ใ (ไธ‹ใ‚’ๅ‘ใ„ใฆใ„ใ‚‹) ใ‚’่ช่ญ˜ใ—ใพใ—ใŸใŒใ€่ฆ‹้€ƒใ—ใฆใ„ใ‚‹ใ‚ˆใ†ใงใ™ใ€‚ +่ฆณๅฎขใŒใ‚นใ‚ฑใƒผใ‚ฟใƒผใฎๅพŒใ‚ใซใ„ใ‚‹ใจใ„ใ†ไบ‹ๅฎŸใ€‚ใใ‚Œใงใ‚‚ใ€ไบบ้–“ใŒๆณจ้‡ˆใ‚’ไป˜ใ‘ใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ๅ–ๅพ—ใ™ใ‚‹ใ“ใจใŒไธๅฏ่ƒฝใชๅ ดๅˆใซใฏใ€ใ“ใ‚Œใฏ +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใซใ‚ˆใ‚Šใ€ๆœ‰็”จใช็ตๆžœใŒใ™ใใซๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..d8d0b530334ec4f4c87152e3156f870033c0e85c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_image_classification.md @@ -0,0 +1,148 @@ + + +# Zero-shot image classification + +[[open-in-colab]] + +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใฏใ€ๆฌกใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒใ‚’ใ•ใพใ–ใพใชใ‚ซใƒ†ใ‚ดใƒชใซๅˆ†้กžใ™ใ‚‹ใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ +ใ“ใ‚Œใ‚‰ใฎ็‰นๅฎšใฎใ‚ซใƒ†ใ‚ดใƒชใฎใƒฉใƒ™ใƒซไป˜ใใฎไพ‹ใ‚’ๅซใ‚€ใƒ‡ใƒผใ‚ฟใซๅฏพใ—ใฆๆ˜Ž็คบ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ใ€‚ + +ๅพ“ๆฅใ€็”ปๅƒๅˆ†้กžใซใฏใ€ใƒฉใƒ™ใƒซไป˜ใ็”ปๅƒใฎ็‰นๅฎšใฎใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใ“ใฎใƒขใƒ‡ใƒซใฏๆฌกใฎใ“ใจใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ +็‰นๅฎšใฎ็”ปๅƒใฎ็‰นๅพดใ‚’ใƒฉใƒ™ใƒซใซใ€Œใƒžใƒƒใƒ”ใƒณใ‚ฐใ€ใ—ใพใ™ใ€‚ๅˆ†้กžใ‚ฟใ‚นใ‚ฏใซใใฎใ‚ˆใ†ใชใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€ +ๆ–ฐใ—ใ„ใƒฉใƒ™ใƒซใฎใ‚ปใƒƒใƒˆใงใฏใ€ใƒขใƒ‡ใƒซใ‚’ "ๅ†่ชฟๆ•ด" ใ™ใ‚‹ใŸใ‚ใซๅพฎ่ชฟๆ•ดใŒๅฟ…โ€‹โ€‹่ฆใงใ™ใ€‚ + +ๅฏพ็…ง็š„ใซใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใพใŸใฏใ‚ชใƒผใƒ—ใƒณ่ชžๅฝ™็”ปๅƒๅˆ†้กžใƒขใƒ‡ใƒซใฏใ€้€šๅธธใ€ๅคง่ฆๆจกใชใ‚ทใ‚นใƒ†ใƒ ใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ ใƒขใƒ‡ใƒซใงใ™ใ€‚ +็”ปๅƒใจ้–ข้€ฃใ™ใ‚‹่ชฌๆ˜Žใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€‚ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใฏใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใ‚’ๅซใ‚€ๅคšใใฎไธ‹ๆตใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใงใใ‚‹ใ€่ชฟๆ•ดใ•ใ‚ŒใŸ่ฆ–่ฆš่จ€่ชž่กจ็พใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใฏใ€็”ปๅƒๅˆ†้กžใซๅฏพใ™ใ‚‹ใ‚ˆใ‚ŠๆŸ”่ปŸใชใ‚ขใƒ—ใƒญใƒผใƒใงใ‚ใ‚Šใ€ใƒขใƒ‡ใƒซใ‚’ๆ–ฐใ—ใ„ใพใ ่ฆ‹ใŸใ“ใจใฎใชใ„ใ‚ซใƒ†ใ‚ดใƒชใซไธ€่ˆฌๅŒ–ใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +่ฟฝๅŠ ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใ‚’ๅฟ…่ฆใจใ›ใšใ€ใƒฆใƒผใ‚ถใƒผใฏใ‚ฟใƒผใ‚ฒใƒƒใƒˆ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ่‡ช็”ฑๅฝขๅผใฎใƒ†ใ‚ญใ‚นใƒˆ่ชฌๆ˜Žใ‚’ๅซใ‚€็”ปๅƒใ‚’ใ‚ฏใ‚จใƒชใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€ๆฌกใฎๆ–นๆณ•ใ‚’ๅญฆใณใพใ™ใ€‚ + +* ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝœๆˆใ™ใ‚‹ +* ๆ‰‹ๅ‹•ใงใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžๆŽจ่ซ–ใ‚’ๅฎŸ่กŒใ—ใพใ™ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q transformers +``` + +## Zero-shot image classification pipeline + +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใ‚’ใ‚ตใƒใƒผใƒˆใ™ใ‚‹ใƒขใƒ‡ใƒซใงๆŽจ่ซ–ใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ๅฏพๅฟœใ™ใ‚‹ [`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ`] ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ +[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?pipeline_tag=zero-shot-image-classification&sort=downloads) ใ‹ใ‚‰ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใพใ™ใ€‚ + +```python +>>> from transformers import pipeline + +>>> checkpoint = "openai/clip-vit-large-patch14" +>>> detector = pipeline(model=checkpoint, task="zero-shot-image-classification") +``` + +ๆฌกใซใ€ๅˆ†้กžใ—ใŸใ„็”ปๅƒใ‚’้ธๆŠžใ—ใพใ™ใ€‚ + +```py +>>> from PIL import Image +>>> import requests + +>>> url = "https://unsplash.com/photos/g8oS8-82DxI/download?ixid=MnwxMjA3fDB8MXx0b3BpY3x8SnBnNktpZGwtSGt8fHx8fDJ8fDE2NzgxMDYwODc&force=true&w=640" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> image +``` + +
+ Photo of an owl +
+ +็”ปๅƒใจๅ€™่ฃœใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใƒฉใƒ™ใƒซใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๆธกใ—ใพใ™ใ€‚ใ“ใ“ใงใฏ็”ปๅƒใ‚’็›ดๆŽฅๆธกใ—ใพใ™ใ€‚ไป–ใฎ้ฉๅˆ‡ใชใ‚ชใƒ—ใ‚ทใƒงใƒณ +็”ปๅƒใธใฎใƒญใƒผใ‚ซใƒซ ใƒ‘ใ‚นใพใŸใฏ็”ปๅƒ URL ใ‚’ๅซใ‚ใพใ™ใ€‚ +ๅ€™่ฃœใƒฉใƒ™ใƒซใฏใ€ใ“ใฎไพ‹ใฎใ‚ˆใ†ใซๅ˜็ด”ใชๅ˜่ชžใซใ™ใ‚‹ใ“ใจใ‚‚ใ€ใ‚ˆใ‚Š่ชฌๆ˜Ž็š„ใชๅ˜่ชžใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```py +>>> predictions = detector(image, candidate_labels=["fox", "bear", "seagull", "owl"]) +>>> predictions +[{'score': 0.9996670484542847, 'label': 'owl'}, + {'score': 0.000199399160919711, 'label': 'seagull'}, + {'score': 7.392891711788252e-05, 'label': 'fox'}, + {'score': 5.96074532950297e-05, 'label': 'bear'}] +``` + +## Zero-shot image classification by hand + +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็”ปๅƒๅˆ†้กžใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎไฝฟ็”จๆ–นๆณ•ใ‚’็†่งฃใ—ใŸใจใ“ใ‚ใงใ€ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +็”ปๅƒใ‚’ๆ‰‹ๅ‹•ใงๅˆ†้กžใ—ใพใ™ใ€‚ + +ใพใšใ€[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?pipeline_tag=zero-shot-image-classification&sort=downloads) ใ‹ใ‚‰ใƒขใƒ‡ใƒซใจ้–ข้€ฃใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€ๅ‰ใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoProcessor, AutoModelForZeroShotImageClassification + +>>> model = AutoModelForZeroShotImageClassification.from_pretrained(checkpoint) +>>> processor = AutoProcessor.from_pretrained(checkpoint) +``` + +ๆฐ—ๅˆ†ใ‚’ๅค‰ใˆใฆใ€ๅˆฅใฎ็”ปๅƒใ‚’ๆ’ฎใฃใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> from PIL import Image +>>> import requests + +>>> url = "https://unsplash.com/photos/xBRQfR2bqNI/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjc4Mzg4ODEx&force=true&w=640" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> image +``` + +
+ Photo of a car +
+ +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใฏใ€ +ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใจๆญฃ่ฆๅŒ–ใซใ‚ˆใ‚‹ใƒขใƒ‡ใƒซใฎ็”ปๅƒใ€ใŠใ‚ˆใณใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ€‚ + +```py +>>> candidate_labels = ["tree", "car", "bike", "cat"] +>>> inputs = processor(images=image, text=candidate_labels, return_tensors="pt", padding=True) +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€็ตๆžœใ‚’ๅพŒๅ‡ฆ็†ใ—ใพใ™ใ€‚ + + +```py +>>> import torch + +>>> with torch.no_grad(): +... outputs = model(**inputs) + +>>> logits = outputs.logits_per_image[0] +>>> probs = logits.softmax(dim=-1).numpy() +>>> scores = probs.tolist() + +>>> result = [ +... {"score": score, "label": candidate_label} +... for score, candidate_label in sorted(zip(probs, candidate_labels), key=lambda x: -x[0]) +... ] + +>>> result +[{'score': 0.998572, 'label': 'car'}, + {'score': 0.0010570387, 'label': 'bike'}, + {'score': 0.0003393686, 'label': 'tree'}, + {'score': 3.1572064e-05, 'label': 'cat'}] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_object_detection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_object_detection.md new file mode 100644 index 0000000000000000000000000000000000000000..7dbc97ef3a1842cfcadce736f58fe0c4f27e0de3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks/zero_shot_object_detection.md @@ -0,0 +1,310 @@ + + +# Zero-shot object detection + +[[open-in-colab]] + +ๅพ“ๆฅใ€[ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บ](object_detection) ใซไฝฟ็”จใ•ใ‚Œใ‚‹ใƒขใƒ‡ใƒซใซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใฎใƒฉใƒ™ใƒซไป˜ใ็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒๅฟ…่ฆใงใ—ใŸใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ ใƒ‡ใƒผใ‚ฟใ‹ใ‚‰ใฎใ‚ฏใƒฉใ‚นใฎใ‚ปใƒƒใƒˆใฎๆคœๅ‡บใซ้™ๅฎšใ•ใ‚Œใพใ™ใ€‚ + +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฏใ€ๅˆฅใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ™ใ‚‹ [OWL-ViT](../model_doc/owlvit) ใƒขใƒ‡ใƒซใซใ‚ˆใฃใฆใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ OWL-ViT +ใ‚ชใƒผใƒ—ใƒณ่ชžๅฝ™ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บๅ™จใงใ™ใ€‚ใ“ใ‚Œใฏใ€ใƒ•ใƒชใƒผใƒ†ใ‚ญใ‚นใƒˆใ‚ฏใ‚จใƒชใซๅŸบใฅใ„ใฆ็”ปๅƒๅ†…ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใงใใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ +ใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ™ใ‚‹ๅฟ…่ฆๆ€งใ€‚ + +OWL-ViTใฏใ€ใƒžใƒซใƒใƒขใƒผใƒ€ใƒซ่กจ็พใ‚’ๅˆฉ็”จใ—ใฆใ‚ชใƒผใƒ—ใƒณ่ชžๅฝ™ใฎๆคœๅ‡บใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ [CLIP](../model_doc/clip) ใจใ‚’็ต„ใฟๅˆใ‚ใ›ใพใ™ใ€‚ +่ปฝ้‡ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๅˆ†้กžใŠใ‚ˆใณไฝ็ฝฎ็‰นๅฎšใƒ˜ใƒƒใƒ‰ใ€‚ใ‚ชใƒผใƒ—ใƒณ่ชžๅฝ™ใฎๆคœๅ‡บใฏใ€CLIP ใฎใƒ†ใ‚ญใ‚นใƒˆ ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไฝฟ็”จใ—ใฆใƒ•ใƒชใƒผใƒ†ใ‚ญใ‚นใƒˆ ใ‚ฏใ‚จใƒชใ‚’ๅŸ‹ใ‚่พผใฟใ€ใใ‚Œใ‚‰ใ‚’ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๅˆ†้กžใŠใ‚ˆใณใƒญใƒผใ‚ซใƒชใ‚ผใƒผใ‚ทใƒงใƒณ ใƒ˜ใƒƒใƒ‰ใธใฎๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅฎŸ็พใ•ใ‚Œใพใ™ใ€‚ +็”ปๅƒใจใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎ่ชฌๆ˜Žใ‚’้–ข้€ฃไป˜ใ‘ใ€ViT ใฏ็”ปๅƒใƒ‘ใƒƒใƒใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ‡ฆ็†ใ—ใพใ™ใ€‚ไฝœๅฎถใŸใก +ใฎOWL-ViTใฏใ€ใพใšCLIPใ‚’ใ‚ผใƒญใ‹ใ‚‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ๆฌกใซๆจ™ๆบ–ใฎ็‰ฉไฝ“ๆคœๅ‡บใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใฆOWL-ViTใ‚’ใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใงๅพฎ่ชฟๆ•ดใ—ใพใ—ใŸใ€‚ +ไบŒ้ƒจใƒžใƒƒใƒใƒณใ‚ฐๆๅคฑใ€‚ + +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใฏใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงไบ‹ๅ‰ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใชใใฆใ‚‚ใ€ใƒ†ใ‚ญใ‚นใƒˆใฎ่ชฌๆ˜ŽใซๅŸบใฅใ„ใฆใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใงใใพใ™ใ€‚ + +ใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€OWL-ViT ใฎไฝฟ็”จๆ–นๆณ•ใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ +- ใƒ†ใ‚ญใ‚นใƒˆใƒ—ใƒญใƒณใƒ—ใƒˆใซๅŸบใฅใ„ใฆใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœๅ‡บใ—ใพใ™ +- ใƒใƒƒใƒใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บ็”จ +- ็”ปๅƒ่ช˜ๅฐŽ็‰ฉไฝ“ๆคœๅ‡บ็”จ + +ๅง‹ใ‚ใ‚‹ๅ‰ใซใ€ๅฟ…่ฆใชใƒฉใ‚คใƒ–ใƒฉใƒชใŒใ™ในใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pip install -q transformers +``` + +## Zero-shot object detection pipeline + +OWL-ViTใซใ‚ˆใ‚‹ๆŽจ่ซ–ใ‚’่ฉฆใ™ๆœ€ใ‚‚็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€OWL-ViTใ‚’[`pipeline`]ใงไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ™ใ‚‹ +[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?other=owlvit) ใ‹ใ‚‰ใฎใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฎๅ ดๅˆ: + +```python +>>> from transformers import pipeline + +>>> checkpoint = "google/owlvit-base-patch32" +>>> detector = pipeline(model=checkpoint, task="zero-shot-object-detection") +``` + +ๆฌกใซใ€็‰ฉไฝ“ใ‚’ๆคœๅ‡บใ—ใŸใ„็”ปๅƒใ‚’้ธๆŠžใ—ใพใ™ใ€‚ใ“ใ“ใงใฏใ€ๅฎ‡ๅฎ™้ฃ›่กŒๅฃซใ‚ขใ‚คใƒชใƒผใƒณใƒปใ‚ณใƒชใƒณใ‚บใฎ็”ปๅƒใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ +[NASA](https://www.nasa.gov/multimedia/imagegallery/index.html) Great Images ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎไธ€้ƒจใ€‚ + +```py +>>> import skimage +>>> import numpy as np +>>> from PIL import Image + +>>> image = skimage.data.astronaut() +>>> image = Image.fromarray(np.uint8(image)).convert("RGB") + +>>> image +``` + +
+ Astronaut Eileen Collins +
+ +ๆคœ็ดขใ™ใ‚‹็”ปๅƒใจๅ€™่ฃœใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใƒฉใƒ™ใƒซใ‚’ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใซๆธกใ—ใพใ™ใ€‚ +ใ“ใ“ใงใฏ็”ปๅƒใ‚’็›ดๆŽฅๆธกใ—ใพใ™ใ€‚ไป–ใฎ้ฉๅˆ‡ใชใ‚ชใƒ—ใ‚ทใƒงใƒณใซใฏใ€็”ปๅƒใธใฎใƒญใƒผใ‚ซใƒซ ใƒ‘ใ‚นใพใŸใฏ็”ปๅƒ URL ใŒๅซใพใ‚Œใพใ™ใ€‚ใพใŸใ€็”ปๅƒใ‚’ใ‚ฏใ‚จใƒชใ™ใ‚‹ใ™ในใฆใฎใ‚ขใ‚คใƒ†ใƒ ใฎใƒ†ใ‚ญใ‚นใƒˆ่ชฌๆ˜Žใ‚‚ๆธกใ—ใพใ™ใ€‚ + +```py +>>> predictions = detector( +... image, +... candidate_labels=["human face", "rocket", "nasa badge", "star-spangled banner"], +... ) +>>> predictions +[{'score': 0.3571370542049408, + 'label': 'human face', + 'box': {'xmin': 180, 'ymin': 71, 'xmax': 271, 'ymax': 178}}, + {'score': 0.28099656105041504, + 'label': 'nasa badge', + 'box': {'xmin': 129, 'ymin': 348, 'xmax': 206, 'ymax': 427}}, + {'score': 0.2110239565372467, + 'label': 'rocket', + 'box': {'xmin': 350, 'ymin': -1, 'xmax': 468, 'ymax': 288}}, + {'score': 0.13790413737297058, + 'label': 'star-spangled banner', + 'box': {'xmin': 1, 'ymin': 1, 'xmax': 105, 'ymax': 509}}, + {'score': 0.11950037628412247, + 'label': 'nasa badge', + 'box': {'xmin': 277, 'ymin': 338, 'xmax': 327, 'ymax': 380}}, + {'score': 0.10649408400058746, + 'label': 'rocket', + 'box': {'xmin': 358, 'ymin': 64, 'xmax': 424, 'ymax': 280}}] +``` + +ไบˆๆธฌใ‚’่ฆ–่ฆšๅŒ–ใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + + +```py +>>> from PIL import ImageDraw + +>>> draw = ImageDraw.Draw(image) + +>>> for prediction in predictions: +... box = prediction["box"] +... label = prediction["label"] +... score = prediction["score"] + +... xmin, ymin, xmax, ymax = box.values() +... draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) +... draw.text((xmin, ymin), f"{label}: {round(score,2)}", fill="white") + +>>> image +``` + +
+ Visualized predictions on NASA image +
+ +## Text-prompted zero-shot object detection by hand + +ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆ็‰ฉไฝ“ๆคœๅ‡บใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใฎไฝฟ็”จๆ–นๆณ•ใ‚’็ขบ่ชใ—ใŸใฎใงใ€ๅŒใ˜ใ“ใจใ‚’ๅ†็พใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +ๆ‰‹ๅ‹•ใง็ตๆžœใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ + +ใพใšใ€[Hugging Face Hub ใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆ](https://huggingface.co/models?other=owlvit) ใ‹ใ‚‰ใƒขใƒ‡ใƒซใจ้–ข้€ฃใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ +ใ“ใ“ใงใฏใ€ๅ‰ใจๅŒใ˜ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +>>> from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection + +>>> model = AutoModelForZeroShotObjectDetection.from_pretrained(checkpoint) +>>> processor = AutoProcessor.from_pretrained(checkpoint) +``` + +ๆฐ—ๅˆ†ใ‚’ๅค‰ใˆใฆใ€ๅˆฅใฎ็”ปๅƒใ‚’ๆ’ฎใฃใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> import requests + +>>> url = "https://unsplash.com/photos/oj0zeY2Ltk4/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTR8fHBpY25pY3xlbnwwfHx8fDE2Nzc0OTE1NDk&force=true&w=640" +>>> im = Image.open(requests.get(url, stream=True).raw) +>>> im +``` + +
+ Beach photo +
+ +ใƒ—ใƒญใ‚ปใƒƒใ‚ตใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใฎๅ…ฅๅŠ›ใ‚’ๆบ–ๅ‚™ใ—ใพใ™ใ€‚ใƒ—ใƒญใ‚ปใƒƒใ‚ตใƒผใฏใ€ +ใ‚ตใ‚คใ‚บๅค‰ๆ›ดใจๆญฃ่ฆๅŒ–ใซใ‚ˆใ‚‹ใƒขใƒ‡ใƒซใฎ็”ปๅƒใจใ€ใƒ†ใ‚ญใ‚นใƒˆๅ…ฅๅŠ›ใ‚’ๅ‡ฆ็†ใ™ใ‚‹ [`CLIPTokenizer`] ใงใ™ใ€‚ + +```py +>>> text_queries = ["hat", "book", "sunglasses", "camera"] +>>> inputs = processor(text=text_queries, images=im, return_tensors="pt") +``` + +ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ—ใ€ๅพŒๅ‡ฆ็†ใ—ใ€็ตๆžœใ‚’่ฆ–่ฆšๅŒ–ใ—ใพใ™ใ€‚ไปฅๅ‰ใฏ็”ปๅƒใƒ—ใƒญใ‚ปใƒƒใ‚ตใซใ‚ˆใฃใฆ็”ปๅƒใฎใ‚ตใ‚คใ‚บใŒๅค‰ๆ›ดใ•ใ‚Œใฆใ„ใŸใŸใ‚ใ€ +ใใ‚Œใ‚‰ใ‚’ใƒขใƒ‡ใƒซใซใƒ•ใ‚ฃใƒผใƒ‰ใ™ใ‚‹ใซใฏใ€[`~OwlViTImageProcessor.post_process_object_detection`] ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ไบˆๆธฌใ•ใ‚ŒใŸๅขƒ็•Œใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒœใƒƒใ‚ฏใ‚นใฏๅ…ƒใฎ็”ปๅƒใ‚’ๅŸบๆบ–ใจใ—ใŸๆญฃใ—ใ„ๅบงๆจ™ใ‚’ๆŒใกใพใ™ใ€‚ + +```py +>>> import torch + +>>> with torch.no_grad(): +... outputs = model(**inputs) +... target_sizes = torch.tensor([im.size[::-1]]) +... results = processor.post_process_object_detection(outputs, threshold=0.1, target_sizes=target_sizes)[0] + +>>> draw = ImageDraw.Draw(im) + +>>> scores = results["scores"].tolist() +>>> labels = results["labels"].tolist() +>>> boxes = results["boxes"].tolist() + +>>> for box, score, label in zip(boxes, scores, labels): +... xmin, ymin, xmax, ymax = box +... draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) +... draw.text((xmin, ymin), f"{text_queries[label]}: {round(score,2)}", fill="white") + +>>> im +``` + +
+ Beach photo with detected objects +
+ +## Batch processing + +่ค‡ๆ•ฐใฎ็”ปๅƒใ‚ปใƒƒใƒˆใจใƒ†ใ‚ญใ‚นใƒˆ ใ‚ฏใ‚จใƒชใ‚’ๆธกใ—ใฆใ€่ค‡ๆ•ฐใฎ็”ปๅƒๅ†…ใฎ็•ฐใชใ‚‹ (ใพใŸใฏๅŒใ˜) ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœ็ดขใงใใพใ™ใ€‚ +ๅฎ‡ๅฎ™้ฃ›่กŒๅฃซใฎ็”ปๅƒใจใƒ“ใƒผใƒใฎ็”ปๅƒใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ +ใƒใƒƒใƒๅ‡ฆ็†ใฎๅ ดๅˆใ€ใƒ†ใ‚ญใ‚นใƒˆ ใ‚ฏใ‚จใƒชใ‚’ใƒใ‚นใƒˆใ•ใ‚ŒใŸใƒชใ‚นใƒˆใจใ—ใฆใƒ—ใƒญใ‚ปใƒƒใ‚ตใซๆธกใ—ใ€็”ปๅƒใ‚’ PIL ใ‚คใƒกใƒผใ‚ธใฎใƒชใ‚นใƒˆใจใ—ใฆๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +PyTorch ใƒ†ใƒณใ‚ฝใƒซใ€ใพใŸใฏ NumPy ้…ๅˆ—ใ€‚ + +```py +>>> images = [image, im] +>>> text_queries = [ +... ["human face", "rocket", "nasa badge", "star-spangled banner"], +... ["hat", "book", "sunglasses", "camera"], +... ] +>>> inputs = processor(text=text_queries, images=images, return_tensors="pt") +``` + +ไปฅๅ‰ใฏๅพŒๅ‡ฆ็†ใฎใŸใ‚ใซๅ˜ไธ€ใฎ็”ปๅƒใฎใ‚ตใ‚คใ‚บใ‚’ใƒ†ใƒณใ‚ฝใƒซใจใ—ใฆๆธกใ—ใฆใ„ใพใ—ใŸใŒใ€ใ‚ฟใƒ—ใƒซใ‚’ๆธกใ™ใ“ใจใ‚‚ใงใใพใ™ใ€‚ +่ค‡ๆ•ฐใฎ็”ปๅƒใฎใ‚ฟใƒ—ใƒซใฎใƒชใ‚นใƒˆใ€‚ 2 ใคใฎไพ‹ใฎไบˆๆธฌใ‚’ไฝœๆˆใ—ใ€2 ็•ช็›ฎใฎไพ‹ (`image_idx = 1`) ใ‚’่ฆ–่ฆšๅŒ–ใ—ใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> with torch.no_grad(): +... outputs = model(**inputs) +... target_sizes = [x.size[::-1] for x in images] +... results = processor.post_process_object_detection(outputs, threshold=0.1, target_sizes=target_sizes) + +>>> image_idx = 1 +>>> draw = ImageDraw.Draw(images[image_idx]) + +>>> scores = results[image_idx]["scores"].tolist() +>>> labels = results[image_idx]["labels"].tolist() +>>> boxes = results[image_idx]["boxes"].tolist() + +>>> for box, score, label in zip(boxes, scores, labels): +... xmin, ymin, xmax, ymax = box +... draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) +... draw.text((xmin, ymin), f"{text_queries[image_idx][label]}: {round(score,2)}", fill="white") + +>>> images[image_idx] +``` + +
+ Beach photo with detected objects +
+ +## Image-guided object detection + +ใƒ†ใ‚ญใ‚นใƒˆใ‚ฏใ‚จใƒชใซใ‚ˆใ‚‹ใ‚ผใƒญใ‚ทใƒงใƒƒใƒˆใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใซๅŠ ใˆใฆใ€OWL-ViTใฏ็”ปๅƒใ‚ฌใ‚คใƒ‰ใซใ‚ˆใ‚‹ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚’ๆไพ›ใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใคใพใ‚Š +็”ปๅƒใ‚ฏใ‚จใƒชใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ็”ปๅƒๅ†…ใฎ้กžไผผใ—ใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ๆคœ็ดขใงใใพใ™ใ€‚ +ใƒ†ใ‚ญใ‚นใƒˆ ใ‚ฏใ‚จใƒชใจใฏ็•ฐใชใ‚Šใ€ไฝฟ็”จใงใใ‚‹ใ‚ตใƒณใƒ—ใƒซ็”ปๅƒใฏ 1 ใคใ ใ‘ใงใ™ใ€‚ + +ๅฏพ่ฑก็”ปๅƒใจใ—ใฆใ‚ฝใƒ•ใ‚กใซ2ๅŒนใฎ็ŒซใŒใ„ใ‚‹็”ปๅƒใจใ€1ๅŒนใฎ็Œซใฎ็”ปๅƒใ‚’ๆ’ฎๅฝฑใ—ใพใ—ใ‚‡ใ† +ใ‚ฏใ‚จใƒชใจใ—ใฆ: + +```py +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image_target = Image.open(requests.get(url, stream=True).raw) + +>>> query_url = "http://images.cocodataset.org/val2017/000000524280.jpg" +>>> query_image = Image.open(requests.get(query_url, stream=True).raw) +``` + +็”ปๅƒใ‚’็ฐกๅ˜ใซ่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +```py +>>> import matplotlib.pyplot as plt + +>>> fig, ax = plt.subplots(1, 2) +>>> ax[0].imshow(image_target) +>>> ax[1].imshow(query_image) +``` + +
+ Cats +
+ +ๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใงใฏใ€ใƒ†ใ‚ญใ‚นใƒˆ ใ‚ฏใ‚จใƒชใฎไปฃใ‚ใ‚Šใซ `query_images` ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +```py +>>> inputs = processor(images=image_target, query_images=query_image, return_tensors="pt") +``` + +ไบˆๆธฌใฎๅ ดๅˆใ€ๅ…ฅๅŠ›ใ‚’ใƒขใƒ‡ใƒซใซๆธกใ™ไปฃใ‚ใ‚Šใซใ€[`~OwlViTForObjectDetection.image_guided_detection`] ใซๆธกใ—ใพใ™ใ€‚ไบˆๆธฌใ‚’ๆใ +ใƒฉใƒ™ใƒซใŒใชใ„ใ“ใจใ‚’้™คใ„ใฆใฏไปฅๅ‰ใจๅŒๆง˜ใงใ™ใ€‚ + +```py +>>> with torch.no_grad(): +... outputs = model.image_guided_detection(**inputs) +... target_sizes = torch.tensor([image_target.size[::-1]]) +... results = processor.post_process_image_guided_detection(outputs=outputs, target_sizes=target_sizes)[0] + +>>> draw = ImageDraw.Draw(image_target) + +>>> scores = results["scores"].tolist() +>>> boxes = results["boxes"].tolist() + +>>> for box, score, label in zip(boxes, scores, labels): +... xmin, ymin, xmax, ymax = box +... draw.rectangle((xmin, ymin, xmax, ymax), outline="white", width=4) + +>>> image_target +``` + +
+ Cats with bounding boxes +
+ +OWL-ViTใซใ‚ˆใ‚‹ๆŽจ่ซ–ใ‚’ใ‚คใƒณใ‚ฟใƒฉใ‚ฏใƒ†ใ‚ฃใƒ–ใซ่ฉฆใ—ใŸใ„ๅ ดๅˆใฏใ€ใ“ใฎใƒ‡ใƒขใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„ใ€‚ + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks_explained.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks_explained.md new file mode 100644 index 0000000000000000000000000000000000000000..f619c2b220bd5a3fd48250e5f9fb295b6d200e0c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tasks_explained.md @@ -0,0 +1,301 @@ + + +# How ๐Ÿค— Transformers solve tasks + +[๐Ÿค— Transformersใงใงใใ‚‹ใ“ใจ](task_summary)ใงใ€่‡ช็„ถ่จ€่ชžๅ‡ฆ็†๏ผˆNLP๏ผ‰ใ€้Ÿณๅฃฐใจใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใ€ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใ‚ฟใ‚นใ‚ฏใ€ใใ‚Œใ‚‰ใฎ้‡่ฆใชใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใซใคใ„ใฆๅญฆใณใพใ—ใŸใ€‚ใ“ใฎใƒšใƒผใ‚ธใงใฏใ€ใƒขใƒ‡ใƒซใŒใ“ใ‚Œใ‚‰ใฎใ‚ฟใ‚นใ‚ฏใ‚’ใฉใฎใ‚ˆใ†ใซ่งฃๆฑบใ™ใ‚‹ใ‹ใ‚’่ฉณใ—ใ่ฆ‹ใฆใ€ใƒขใƒ‡ใƒซใฎๅ†…้ƒจใงไฝ•ใŒ่ตทใ“ใฃใฆใ„ใ‚‹ใ‹ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใฏๅคšใใฎๆ–นๆณ•ใŒใ‚ใ‚Šใ€ไธ€้ƒจใฎใƒขใƒ‡ใƒซใฏ็‰นๅฎšใฎใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใ‚’ๅฎŸ่ฃ…ใ™ใ‚‹ใ‹ใ€ใพใŸใฏๆ–ฐใ—ใ„่ฆณ็‚นใ‹ใ‚‰ใ‚ฟใ‚นใ‚ฏใซๅ–ใ‚Š็ต„ใ‚€ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใŒใ€Transformerใƒขใƒ‡ใƒซใซใจใฃใฆใ€ไธ€่ˆฌ็š„ใชใ‚ขใ‚คใƒ‡ใ‚ขใฏๅŒใ˜ใงใ™ใ€‚ๆŸ”่ปŸใชใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใŠใ‹ใ’ใงใ€ใปใจใ‚“ใฉใฎใƒขใƒ‡ใƒซใฏใ‚จใƒณใ‚ณใƒผใƒ€ใ€ใƒ‡ใ‚ณใƒผใƒ€ใ€ใพใŸใฏใ‚จใƒณใ‚ณใƒผใƒ€-ใƒ‡ใ‚ณใƒผใƒ€ๆง‹้€ ใฎๅค‰็จฎใงใ™ใ€‚Transformerใƒขใƒ‡ใƒซไปฅๅค–ใซใ‚‚ใ€ๅฝ“็คพใฎใƒฉใ‚คใƒ–ใƒฉใƒชใซใฏใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใซไปŠใงใ‚‚ไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใ„ใใคใ‹ใฎ็•ณใฟ่พผใฟใƒ‹ใƒฅใƒผใƒฉใƒซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ๏ผˆCNN๏ผ‰ใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใพใŸใ€็พไปฃใฎCNNใŒใฉใฎใ‚ˆใ†ใซๆฉŸ่ƒฝใ™ใ‚‹ใ‹ใ‚‚่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ใ‚ฟใ‚นใ‚ฏใŒใฉใฎใ‚ˆใ†ใซ่งฃๆฑบใ•ใ‚Œใ‚‹ใ‹ใ‚’่ชฌๆ˜Žใ™ใ‚‹ใŸใ‚ใซใ€ใƒขใƒ‡ใƒซๅ†…้ƒจใงๆœ‰็”จใชไบˆๆธฌใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใŸใ‚ใซไฝ•ใŒ่ตทใ“ใ‚‹ใ‹ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +- [Wav2Vec2](model_doc/wav2vec2)๏ผšใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใŠใ‚ˆใณ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜๏ผˆASR๏ผ‰ๅ‘ใ‘ +- [Vision Transformer๏ผˆViT๏ผ‰](model_doc/vit)ใŠใ‚ˆใณ[ConvNeXT](model_doc/convnext)๏ผš็”ปๅƒๅˆ†้กžๅ‘ใ‘ +- [DETR](model_doc/detr)๏ผšใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บๅ‘ใ‘ +- [Mask2Former](model_doc/mask2former)๏ผš็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณๅ‘ใ‘ +- [GLPN](model_doc/glpn)๏ผšๆทฑๅบฆๆŽจๅฎšๅ‘ใ‘ +- [BERT](model_doc/bert)๏ผšใ‚จใƒณใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ€ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ€ใŠใ‚ˆใณ่ณชๅ•ๅฟœ็ญ”ใชใฉใฎNLPใ‚ฟใ‚นใ‚ฏๅ‘ใ‘ +- [GPT2](model_doc/gpt2)๏ผšใƒ‡ใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆ็”ŸๆˆใชใฉใฎNLPใ‚ฟใ‚นใ‚ฏๅ‘ใ‘ +- [BART](model_doc/bart)๏ผšใ‚จใƒณใ‚ณใƒผใƒ€-ใƒ‡ใ‚ณใƒผใƒ€ใ‚’ไฝฟ็”จใ™ใ‚‹่ฆ็ด„ใŠใ‚ˆใณ็ฟป่จณใชใฉใฎNLPใ‚ฟใ‚นใ‚ฏๅ‘ใ‘ + + + +ใ•ใ‚‰ใซ้€ฒใ‚€ๅ‰ใซใ€ๅ…ƒใฎTransformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎๅŸบๆœฌ็š„ใช็Ÿฅ่ญ˜ใ‚’ๆŒใคใจ่‰ฏใ„ใงใ™ใ€‚ใ‚จใƒณใ‚ณใƒผใƒ€ใ€ใƒ‡ใ‚ณใƒผใƒ€ใ€ใŠใ‚ˆใณๆณจๆ„ๅŠ›ใŒใฉใฎใ‚ˆใ†ใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’็ŸฅใฃใฆใŠใใจใ€็•ฐใชใ‚‹Transformerใƒขใƒ‡ใƒซใŒใฉใฎใ‚ˆใ†ใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’็†่งฃใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ๅง‹ใ‚ใฆใ„ใ‚‹ใ‹ใ€ใƒชใƒ•ใƒฌใƒƒใ‚ทใƒฅใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€่ฉณ็ดฐใชๆƒ…ๅ ฑใซใคใ„ใฆใฏๅฝ“็คพใฎ[ใ‚ณใƒผใ‚น](https://huggingface.co/course/chapter1/4?fw=pt)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผ + + + +## Speech and audio + +[Wav2Vec2](model_doc/wav2vec2)ใฏใ€ๆœชใƒฉใƒ™ใƒซใฎ้Ÿณๅฃฐใƒ‡ใƒผใ‚ฟใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใŠใ‚ˆใณ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใฎใƒฉใƒ™ใƒซไป˜ใใƒ‡ใƒผใ‚ฟใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใ•ใ‚ŒใŸ่‡ชๅทฑๆ•™ๅธซใƒขใƒ‡ใƒซใงใ™ใ€‚ + +
+ +
+ +ใ“ใฎใƒขใƒ‡ใƒซใซใฏไธปใซๆฌกใฎ4ใคใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. *็‰นๅพดใ‚จใƒณใ‚ณใƒผใƒ€*๏ผš็”Ÿใฎ้Ÿณๅฃฐๆณขๅฝขใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ๅนณๅ‡ๅ€คใ‚’ใ‚ผใƒญใซๆญฃ่ฆๅŒ–ใ—ใ€ๅ˜ไฝๅˆ†ๆ•ฃใซๅค‰ๆ›ใ—ใ€ใใ‚Œใ‚’20msใ”ใจใฎ็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅค‰ๆ›ใ—ใพใ™ใ€‚ + +2. ๆณขๅฝขใฏ่‡ช็„ถใซ้€ฃ็ถšใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๅ˜่ชžใซๅˆ†ๅ‰ฒใงใใ‚‹ใ‚ˆใ†ใซใงใใ‚‹ใ‚ˆใ†ใซใ€็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฏ*้‡ๅญๅŒ–ใƒขใ‚ธใƒฅใƒผใƒซ*ใซๆธกใ•ใ‚Œใ€้›ขๆ•ฃ้Ÿณๅฃฐใƒฆใƒ‹ใƒƒใƒˆใ‚’ๅญฆ็ฟ’ใ—ใ‚ˆใ†ใจใ—ใพใ™ใ€‚้Ÿณๅฃฐใƒฆใƒ‹ใƒƒใƒˆใฏ*ใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏ*๏ผˆ่ชžๅฝ™ใจ่€ƒใˆใ‚‹ใ“ใจใŒใงใใพใ™๏ผ‰ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใ‚‹ใ‚ณใƒผใƒ‰ใƒฏใƒผใƒ‰ใฎใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใ‹ใ‚‰้ธๆŠžใ•ใ‚Œใพใ™ใ€‚ใ‚ณใƒผใƒ‰ใƒ–ใƒƒใ‚ฏใ‹ใ‚‰ใ€้€ฃ็ถšใ—ใŸใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅ…ฅๅŠ›ใ‚’ๆœ€ใ‚‚ใ‚ˆใ่กจใ™ใƒ™ใ‚ฏใƒˆใƒซใพใŸใฏ้Ÿณๅฃฐใƒฆใƒ‹ใƒƒใƒˆ๏ผˆใ‚ฟใƒผใ‚ฒใƒƒใƒˆใƒฉใƒ™ใƒซใจ่€ƒใˆใ‚‹ใ“ใจใŒใงใใพใ™๏ผ‰ใŒ้ธๆŠžใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใ‚’ไป‹ใ—ใฆ่ปข้€ใ•ใ‚Œใพใ™ใ€‚ + +3. ็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฎ็ด„ๅŠๅˆ†ใฏใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ•ใ‚Œใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฏ*ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ*ใซไพ›็ตฆใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏใ€็›ธๅฏพ็š„ใชไฝ็ฝฎใ‚จใƒณใƒ™ใƒƒใƒ‡ใ‚ฃใƒณใ‚ฐใ‚‚่ฟฝๅŠ ใ™ใ‚‹Transformerใ‚จใƒณใ‚ณใƒผใƒ€ใงใ™ใ€‚ + +4. ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็›ฎ็š„ใฏ*ใ‚ณใƒณใƒˆใƒฉใ‚นใƒ†ใ‚ฃใƒ–ใ‚ฟใ‚นใ‚ฏ*ใงใ™ใ€‚ใƒขใƒ‡ใƒซใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸไบˆๆธฌใฎ็œŸใฎ้‡ๅญๅŒ–้Ÿณๅฃฐ่กจ็พใ‚’ใ€ๅฝใฎไบˆๆธฌใฎใ‚ปใƒƒใƒˆใ‹ใ‚‰ไบˆๆธฌใ—ใชใ‘ใ‚Œใฐใชใ‚‰ใšใ€ใƒขใƒ‡ใƒซใฏๆœ€ใ‚‚ไผผใŸใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒ™ใ‚ฏใƒˆใƒซใจ้‡ๅญๅŒ–้Ÿณๅฃฐใƒฆใƒ‹ใƒƒใƒˆ๏ผˆใ‚ฟใƒผใ‚ฒใƒƒใƒˆใƒฉใƒ™ใƒซ๏ผ‰ใ‚’่ฆ‹ใคใ‘ใ‚‹ใ‚ˆใ†ใซไฟƒใ•ใ‚Œใพใ™ใ€‚ + +ไปŠใ€Wav2Vec2ใฏไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใ‚‹ใฎใงใ€ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใพใŸใฏ่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใฎใŸใ‚ใซใƒ‡ใƒผใ‚ฟใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใงใใพใ™๏ผ + +### Audio classification + +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใซไฝฟ็”จใ™ใ‚‹ใซใฏใ€ๅŸบๆœฌ็š„ใชWav2Vec2ใƒขใƒ‡ใƒซใฎไธŠใซใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏใ‚จใƒณใ‚ณใƒผใƒ€ใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ‚‹็ทšๅฝขๅฑคใงใ€ๅ„ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชใƒ•ใƒฌใƒผใƒ ใ‹ใ‚‰ๅญฆ็ฟ’ใ•ใ‚ŒใŸ็‰นๅพดใ‚’่กจใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใฏ้•ทใ•ใŒ็•ฐใชใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ๆœ€ๅˆใซ้š ใ‚ŒใŸ็Šถๆ…‹ใŒใƒ—ใƒผใƒซใ•ใ‚Œใ€ๆฌกใซใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใซๅฏพใ™ใ‚‹ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ใƒญใ‚ธใƒƒใƒˆใจใ‚ฟใƒผใ‚ฒใƒƒใƒˆ้–“ใฎใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใŒ่จˆ็ฎ—ใ•ใ‚Œใ€ๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใ‚ฏใƒฉใ‚นใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸWav2Vec2ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใ—ใฆๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใฎๅฎŒๅ…จใช[ใ‚ชใƒผใƒ‡ใ‚ฃใ‚ชๅˆ†้กžใ‚ฌใ‚คใƒ‰](tasks/audio_classification)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผ + +### Automatic speech recognition + +ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚’่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใซไฝฟ็”จใ™ใ‚‹ใซใฏใ€[connectionist temporal classification๏ผˆCTC๏ผ‰](glossary#connectionist-temporal-classification-ctc)ใฎใŸใ‚ใฎๅŸบๆœฌ็š„ใชWav2Vec2ใƒขใƒ‡ใƒซใฎไธŠใซ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ˜ใƒƒใƒ‰ใฏใ‚จใƒณใ‚ณใƒผใƒ€ใฎ้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ใใ‚Œใ‚‰ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ—ใพใ™ใ€‚ๅ„ใƒญใ‚ธใƒƒใƒˆใฏใƒˆใƒผใ‚ฏใƒณใ‚ฏใƒฉใ‚นใ‚’่กจใ—๏ผˆใƒˆใƒผใ‚ฏใƒณๆ•ฐใฏใ‚ฟใ‚นใ‚ฏใฎ่ชžๅฝ™ใ‹ใ‚‰ๆฅใพใ™๏ผ‰ใ€ใƒญใ‚ธใƒƒใƒˆใจใ‚ฟใƒผใ‚ฒใƒƒใƒˆ้–“ใฎCTCๆๅคฑใŒ่จˆ็ฎ—ใ•ใ‚Œใ€ๆฌกใซ่ปขๅ†™ใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ + +่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸWav2Vec2ใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใ—ใฆๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใฎๅฎŒๅ…จใช[่‡ชๅ‹•้Ÿณๅฃฐ่ช่ญ˜ใ‚ฌใ‚คใƒ‰](tasks/asr)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผ + +## Computer vision + +ใ‚ณใƒณใƒ”ใƒฅใƒผใ‚ฟใƒ“ใ‚ธใƒงใƒณใฎใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ขใƒ—ใƒญใƒผใƒใ™ใ‚‹ๆ–นๆณ•ใฏ2ใคใ‚ใ‚Šใพใ™ใ€‚ + +1. ็”ปๅƒใ‚’ใƒ‘ใƒƒใƒใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆ†ๅ‰ฒใ—ใ€Transformerใ‚’ไฝฟ็”จใ—ใฆไธฆๅˆ—ใซๅ‡ฆ็†ใ—ใพใ™ใ€‚ +2. [ConvNeXT](model_doc/convnext)ใชใฉใฎใƒขใƒ€ใƒณใชCNNใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฏ็•ณใฟ่พผใฟๅฑคใ‚’ไฝฟ็”จใ—ใพใ™ใŒใ€ใƒขใƒ€ใƒณใชใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ่จญ่จˆใ‚’ๆŽก็”จใ—ใฆใ„ใพใ™ใ€‚ + + + +ใ‚ตใƒผใƒ‰ใ‚ขใƒ—ใƒญใƒผใƒใงใฏใ€Transformerใจ็•ณใฟ่พผใฟใ‚’็ต„ใฟๅˆใ‚ใ›ใŸใ‚‚ใฎใ‚‚ใ‚ใ‚Šใพใ™๏ผˆไพ‹๏ผš[Convolutional Vision Transformer](model_doc/cvt)ใพใŸใฏ[LeViT](model_doc/levit)๏ผ‰ใ€‚ใ“ใ‚Œใ‚‰ใซใคใ„ใฆใฏ่ญฐ่ซ–ใ—ใพใ›ใ‚“ใŒใ€ใ“ใ‚Œใ‚‰ใฏใ“ใ“ใง่ชฟในใ‚‹2ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’็ต„ใฟๅˆใ‚ใ›ใฆใ„ใพใ™ใ€‚ + + + +ViTใจConvNeXTใฏ็”ปๅƒๅˆ†้กžใซใ‚ˆใไฝฟ็”จใ•ใ‚Œใพใ™ใŒใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ€ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ๆทฑๅบฆๆŽจๅฎšใชใฉใฎไป–ใฎใƒ“ใ‚ธใƒงใƒณใ‚ฟใ‚นใ‚ฏใซๅฏพใ—ใฆใฏใ€DETRใ€Mask2Formerใ€GLPNใชใฉใŒ้ฉใ—ใฆใ„ใพใ™ใ€‚ + +### Image classification + +ViTใจConvNeXTใฎไธกๆ–นใ‚’็”ปๅƒๅˆ†้กžใซไฝฟ็”จใงใใพใ™ใ€‚ไธปใช้•ใ„ใฏใ€ViTใŒๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใ‚’ไฝฟ็”จใ—ใ€ConvNeXTใŒ็•ณใฟ่พผใฟใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +#### Transformer + +[ViT](model_doc/vit)ใฏ็•ณใฟ่พผใฟใ‚’ๅฎŒๅ…จใซTransformerใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใง็ฝฎใๆ›ใˆใพใ™ใ€‚ๅ…ƒใฎTransformerใซ็ฒพ้€šใ—ใฆใ„ใ‚‹ๅ ดๅˆใ€ViTใฎ็†่งฃใฏๆ—ขใซใปใจใ‚“ใฉๅฎŒไบ†ใ—ใฆใ„ใพใ™ใ€‚ + +
+ +
+ +ViTใŒๅฐŽๅ…ฅใ—ใŸไธปใชๅค‰ๆ›ด็‚นใฏใ€็”ปๅƒใ‚’Transformerใซไพ›็ตฆใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ + +1. ็”ปๅƒใฏๆญฃๆ–นๅฝขใง้‡ใชใ‚‰ใชใ„ใƒ‘ใƒƒใƒใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆ†ๅ‰ฒใ•ใ‚Œใ€ๅ„ใƒ‘ใƒƒใƒใฏใƒ™ใ‚ฏใƒˆใƒซใพใŸใฏ*ใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟ*ใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใฏใ€้ฉๅˆ‡ใชๅ…ฅๅŠ›ๆฌกๅ…ƒใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใซ2D็•ณใฟ่พผใฟๅฑคใ‹ใ‚‰็”Ÿๆˆใ•ใ‚Œใพใ™๏ผˆๅŸบๆœฌใฎTransformerใฎๅ ดๅˆใ€ๅ„ใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใซ768ใฎๅ€คใŒใ‚ใ‚Šใพใ™๏ผ‰ใ€‚224x224ใƒ”ใ‚ฏใ‚ปใƒซใฎ็”ปๅƒใŒใ‚ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚’16x16ใฎ็”ปๅƒใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใงใใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใŒๅ˜่ชžใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ€็”ปๅƒใฏใƒ‘ใƒƒใƒใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซใ€Œใƒˆใƒผใ‚ฏใƒณๅŒ–ใ€ใ•ใ‚Œใพใ™ใ€‚ + +2. *ๅญฆ็ฟ’ๅŸ‹ใ‚่พผใฟ*ใ€ใคใพใ‚Š็‰นๅˆฅใช `[CLS]` ใƒˆใƒผใ‚ฏใƒณใŒใ€BERTใฎใ‚ˆใ†ใซใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใฎๅ…ˆ้ ญใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ `[CLS]` ใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใฏใ€ไป˜ๅฑžใฎๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฎๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ไป–ใฎๅ‡บๅŠ›ใฏ็„ก่ฆ–ใ•ใ‚Œใพใ™ใ€‚ใ“ใฎใƒˆใƒผใ‚ฏใƒณใฏใ€ใƒขใƒ‡ใƒซใŒ็”ปๅƒใฎ่กจ็พใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +3. ใƒ‘ใƒƒใƒใจๅญฆ็ฟ’ๅŸ‹ใ‚่พผใฟใซ่ฟฝๅŠ ใ™ใ‚‹ๆœ€ๅพŒใฎ่ฆ็ด ใฏ*ไฝ็ฝฎๅŸ‹ใ‚่พผใฟ*ใงใ™ใ€‚ใƒขใƒ‡ใƒซใฏ็”ปๅƒใƒ‘ใƒƒใƒใŒใฉใฎใ‚ˆใ†ใซไธฆในใ‚‰ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’็Ÿฅใ‚Šใพใ›ใ‚“ใฎใงใ€ไฝ็ฝฎๅŸ‹ใ‚่พผใฟใ‚‚ๅญฆ็ฟ’ๅฏ่ƒฝใงใ€ใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใจๅŒใ˜ใ‚ตใ‚คใ‚บใ‚’ๆŒใกใพใ™ใ€‚ๆœ€ๅพŒใซใ€ใ™ในใฆใฎๅŸ‹ใ‚่พผใฟใŒTransformerใ‚จใƒณใ‚ณใƒผใƒ€ใซๆธกใ•ใ‚Œใพใ™ใ€‚ + +4. ๅ‡บๅŠ›ใ€ๅ…ทไฝ“็š„ใซใฏ `[CLS]` ใƒˆใƒผใ‚ฏใƒณใฎๅ‡บๅŠ›ใ ใ‘ใŒใ€ๅคšๅฑคใƒ‘ใƒผใ‚ปใƒ—ใƒˆใƒญใƒณใƒ˜ใƒƒใƒ‰๏ผˆMLP๏ผ‰ใซๆธกใ•ใ‚Œใพใ™ใ€‚ViTใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็›ฎ็š„ใฏๅ˜็ด”ใซๅˆ†้กžใงใ™ใ€‚ไป–ใฎๅˆ†้กžใƒ˜ใƒƒใƒ‰ใจๅŒๆง˜ใซใ€MLPใƒ˜ใƒƒใƒ‰ใฏๅ‡บๅŠ›ใ‚’ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใซๅฏพใ™ใ‚‹ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ—ใ€ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใ‚’่จˆ็ฎ—ใ—ใฆๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใ‚ฏใƒฉใ‚นใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + +็”ปๅƒๅˆ†้กžใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸViTใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒณใ—ใฆๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใฎๅฎŒๅ…จใช[็”ปๅƒๅˆ†้กžใ‚ฌใ‚คใƒ‰](tasks/image_classification)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใใ ใ•ใ„๏ผ + + +#### CNN + + + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏ็•ณใฟ่พผใฟใซใคใ„ใฆ็ฐกๅ˜ใซ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใŒใ€็”ปๅƒใฎๅฝข็Šถใจใ‚ตใ‚คใ‚บใŒใฉใฎใ‚ˆใ†ใซๅค‰ๅŒ–ใ™ใ‚‹ใ‹ใ‚’ไบ‹ๅ‰ใซ็†่งฃใ—ใฆใ„ใ‚‹ใจๅฝน็ซ‹ใกใพใ™ใ€‚็•ณใฟ่พผใฟใซๆ…ฃใ‚Œใฆใ„ใชใ„ๅ ดๅˆใฏใ€fastaiใฎๆ›ธ็ฑใ‹ใ‚‰[Convolution Neural Networks chapter](https://github.com/fastai/fastbook/blob/master/13_convolutions.ipynb)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + + +[ConvNeXT](model_doc/convnext)ใฏใ€ๆ€ง่ƒฝใ‚’ๅ‘ไธŠใ•ใ›ใ‚‹ใŸใ‚ใซๆ–ฐใ—ใ„ใƒขใƒ€ใƒณใชใƒใƒƒใƒˆใƒฏใƒผใ‚ฏ่จญ่จˆใ‚’ๆŽก็”จใ—ใŸCNNใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใ™ใ€‚ใŸใ ใ—ใ€็•ณใฟ่พผใฟใฏใƒขใƒ‡ใƒซใฎไธญๆ ธใซใพใ ใ‚ใ‚Šใพใ™ใ€‚้ซ˜ใƒฌใƒ™ใƒซใ‹ใ‚‰่ฆ‹ใŸๅ ดๅˆใ€[็•ณใฟ่พผใฟ๏ผˆconvolution๏ผ‰](glossary#convolution)ใฏใ€ๅฐใ•ใช่กŒๅˆ—๏ผˆ*ใ‚ซใƒผใƒใƒซ*๏ผ‰ใŒ็”ปๅƒใฎใƒ”ใ‚ฏใ‚ปใƒซใฎๅฐใ•ใชใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใซไน—็ฎ—ใ•ใ‚Œใ‚‹ๆ“ไฝœใงใ™ใ€‚ใใ‚Œใฏ็‰นๅฎšใฎใƒ†ใ‚ฏใ‚นใƒใƒฃใ‚„็ทšใฎๆ›ฒ็އใชใฉใฎ็‰นๅพดใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ใใฎๅพŒใ€ๆฌกใฎใƒ”ใ‚ฏใ‚ปใƒซใฎใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใซ็งปๅ‹•ใ—ใพใ™ใ€‚็•ณใฟ่พผใฟใŒ็งปๅ‹•ใ™ใ‚‹่ท้›ขใฏ*ใ‚นใƒˆใƒฉใ‚คใƒ‰*ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ + +
+ +
+ +[Convolution Arithmetic for Deep Learning](https://huggingface.co/papers/1603.07285) ใ‹ใ‚‰ใฎๅŸบๆœฌ็š„ใชใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚„ใ‚นใƒˆใƒฉใ‚คใƒ‰ใฎใชใ„็•ณใฟ่พผใฟใ€‚ + +ใ“ใฎๅ‡บๅŠ›ใ‚’ๅˆฅใฎ็•ณใฟ่พผใฟๅฑคใซไพ›็ตฆใ—ใ€ๅ„้€ฃ็ถšใ—ใŸๅฑคใ”ใจใซใ€ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใฏใƒ›ใƒƒใƒˆใƒ‰ใƒƒใ‚ฐใ‚„ใƒญใ‚ฑใƒƒใƒˆใฎใ‚ˆใ†ใชใ‚ˆใ‚Š่ค‡้›‘ใงๆŠฝ่ฑก็š„ใชใ‚‚ใฎใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚็•ณใฟ่พผใฟๅฑคใฎ้–“ใซใฏใ€็‰นๅพดใฎๆฌกๅ…ƒใ‚’ๅ‰Šๆธ›ใ—ใ€็‰นๅพดใฎไฝ็ฝฎใฎๅค‰ๅ‹•ใซๅฏพใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚ˆใ‚Šๅ …็‰ขใซใ™ใ‚‹ใŸใ‚ใซใƒ—ใƒผใƒชใƒณใ‚ฐๅฑคใ‚’่ฟฝๅŠ ใ™ใ‚‹ใฎใŒไธ€่ˆฌ็š„ใงใ™ใ€‚ + +
+ +
+ +ConvNeXTใฏใ€ไปฅไธ‹ใฎ5ใคใฎๆ–นๆณ•ใงCNNใ‚’ใƒขใƒ€ใƒณๅŒ–ใ—ใฆใ„ใพใ™ใ€‚ + +1. ๅ„ใ‚นใƒ†ใƒผใ‚ธใฎใƒ–ใƒญใƒƒใ‚ฏๆ•ฐใ‚’ๅค‰ๆ›ดใ—ใ€็”ปๅƒใ‚’ใ‚ˆใ‚Šๅคงใใชใ‚นใƒˆใƒฉใ‚คใƒ‰ใจๅฏพๅฟœใ™ใ‚‹ใ‚ซใƒผใƒใƒซใ‚ตใ‚คใ‚บใง*ใƒ‘ใƒƒใƒๅŒ–*ใ—ใพใ™ใ€‚้‡ใชใ‚‰ใชใ„ใ‚นใƒฉใ‚คใƒ‡ใ‚ฃใƒณใ‚ฐใ‚ฆใ‚ฃใƒณใƒ‰ใ‚ฆใฏใ€ใ“ใ‚Œใซใ‚ˆใ‚Š็”ปๅƒใ‚’ใƒ‘ใƒƒใƒใซๅˆ†ๅ‰ฒใ™ใ‚‹ViTใฎๆˆฆ็•ฅใจไผผใฆใ„ใพใ™ใ€‚ + +2. *ใƒœใƒˆใƒซใƒใƒƒใ‚ฏ* ใƒฌใ‚คใƒคใƒผใฏใƒใƒฃใƒใƒซๆ•ฐใ‚’็ธฎๅฐใ—ใ€ใใ‚Œใ‚’ๅพฉๅ…ƒใ—ใพใ™ใ€‚1x1ใฎ็•ณใฟ่พผใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใฎใฏ้€Ÿใใ€ๆทฑใ•ใ‚’ๅข—ใ‚„ใ™ใ“ใจใŒใงใใพใ™ใ€‚้€†ใƒœใƒˆใƒซใƒใƒƒใ‚ฏใฏ้€†ใฎใ“ใจใ‚’่กŒใ„ใ€ใƒใƒฃใƒใƒซๆ•ฐใ‚’ๆ‹กๅผตใ—ใ€ใใ‚Œใ‚’็ธฎๅฐใ—ใพใ™ใ€‚ใ“ใ‚ŒใฏใƒกใƒขใƒชๅŠน็އใŒ้ซ˜ใ„ใงใ™ใ€‚ + +3. ใƒœใƒˆใƒซใƒใƒƒใ‚ฏใƒฌใ‚คใƒคใƒผๅ†…ใฎ้€šๅธธใฎ3x3ใฎ็•ณใฟ่พผใฟๅฑคใ‚’ใ€*ๆทฑๅบฆๆ–นๅ‘ใฎ็•ณใฟ่พผใฟ*ใง็ฝฎใๆ›ใˆใพใ™ใ€‚ใ“ใ‚Œใฏๅ„ๅ…ฅๅŠ›ใƒใƒฃใƒใƒซใซๅ€‹ๅˆฅใซ็•ณใฟ่พผใฟใ‚’้ฉ็”จใ—ใ€ๆœ€ๅพŒใซใใ‚Œใ‚‰ใ‚’็ฉใฟ้‡ใญใ‚‹็•ณใฟ่พผใฟใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ๆ€ง่ƒฝๅ‘ไธŠใฎใŸใ‚ใซใƒใƒƒใƒˆใƒฏใƒผใ‚ฏๅน…ใŒๅบƒใŒใ‚Šใพใ™ใ€‚ + +4. ViTใฏใ‚ฐใƒญใƒผใƒใƒซๅ—ๅฎน้‡Žใ‚’ๆŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€ใใฎๆณจๆ„ใƒกใ‚ซใƒ‹ใ‚บใƒ ใฎใŠใ‹ใ’ใงไธ€ๅบฆใซ็”ปๅƒใฎๅคšใใ‚’่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ConvNeXTใฏใ“ใฎๅŠนๆžœใ‚’ๅ†็พใ—ใ‚ˆใ†ใจใ—ใ€ใ‚ซใƒผใƒใƒซใ‚ตใ‚คใ‚บใ‚’7x7ใซๅข—ใ‚„ใ—ใพใ™ใ€‚ + +5. ConvNeXTใฏใพใŸใ€Transformerใƒขใƒ‡ใƒซใ‚’ๆจกๅ€ฃใ™ใ‚‹ใ„ใใคใ‹ใฎใƒฌใ‚คใƒคใƒผใƒ‡ใ‚ถใ‚คใƒณๅค‰ๆ›ดใ‚’่กŒใฃใฆใ„ใพใ™ใ€‚ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใ‚ทใƒงใƒณใจๆญฃ่ฆๅŒ–ใƒฌใ‚คใƒคใƒผใŒๅฐ‘ใชใใ€ๆดปๆ€งๅŒ–้–ขๆ•ฐใฏReLUใฎไปฃใ‚ใ‚ŠใซGELUใซๅˆ‡ใ‚Šๆ›ฟใˆใ€BatchNormใฎไปฃใ‚ใ‚ŠใซLayerNormใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ + +็•ณใฟ่พผใฟใƒ–ใƒญใƒƒใ‚ฏใ‹ใ‚‰ใฎๅ‡บๅŠ›ใฏใ€ๅˆ†้กžใƒ˜ใƒƒใƒ‰ใซๆธกใ•ใ‚Œใ€ๅ‡บๅŠ›ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ—ใ€ๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒฉใƒ™ใƒซใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใŒ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ + +### Object detection + +[DETR](model_doc/detr)ใ€*DEtection TRansformer*ใ€ใฏCNNใจTransformerใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‚’็ต„ใฟๅˆใ‚ใ›ใŸใ‚จใƒณใƒ‰ใƒ„ใƒผใ‚จใƒณใƒ‰ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใƒขใƒ‡ใƒซใงใ™ใ€‚ + +
+ +
+ +1. ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸCNN *ใƒใƒƒใ‚ฏใƒœใƒผใƒณ* ใฏใ€ใƒ”ใ‚ฏใ‚ปใƒซๅ€คใง่กจใ•ใ‚Œใ‚‹็”ปๅƒใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ใใ‚ŒใฎไฝŽ่งฃๅƒๅบฆใฎ็‰นๅพดใƒžใƒƒใƒ—ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚็‰นๅพดใƒžใƒƒใƒ—ใซใฏๆฌกๅ…ƒๅ‰Šๆธ›ใฎใŸใ‚ใซ1x1ใฎ็•ณใฟ่พผใฟใŒ้ฉ็”จใ•ใ‚Œใ€้ซ˜ใƒฌใƒ™ใƒซใฎ็”ปๅƒ่กจ็พใ‚’ๆŒใคๆ–ฐใ—ใ„็‰นๅพดใƒžใƒƒใƒ—ใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚Transformerใฏ้€ฃ็ถšใƒขใƒ‡ใƒซใงใ‚ใ‚‹ใŸใ‚ใ€็‰นๅพดใƒžใƒƒใƒ—ใฏ็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅนณๅฆๅŒ–ใ•ใ‚Œใ€ไฝ็ฝฎใ‚จใƒณใƒ™ใƒ‡ใ‚ฃใƒณใ‚ฐใจ็ต„ใฟๅˆใ‚ใ›ใ‚‰ใ‚Œใพใ™ใ€‚ + +2. ็‰นๅพดใƒ™ใ‚ฏใƒˆใƒซใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซๆธกใ•ใ‚Œใ€ใใฎๆณจๆ„ใƒฌใ‚คใƒคใƒผใ‚’ไฝฟ็”จใ—ใฆ็”ปๅƒ่กจ็พใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ๆฌกใซใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใฏใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ*ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒช*ใจ็ต„ใฟๅˆใ‚ใ•ใ‚Œใพใ™ใ€‚ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใฏใ€็”ปๅƒใฎ็•ฐใชใ‚‹้ ˜ๅŸŸใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใ‚‹ๅญฆ็ฟ’ๅŸ‹ใ‚่พผใฟใงใ€ๅ„ๆณจๆ„ใƒฌใ‚คใƒคใƒผใ‚’้€ฒ่กŒใ™ใ‚‹ใซใคใ‚Œใฆๆ›ดๆ–ฐใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใฏใ€ๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใซๅฏพใ—ใฆใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚นใฎๅบงๆจ™ใจใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใ‚’ไบˆๆธฌใ™ใ‚‹ใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใซๆธกใ•ใ‚Œใพใ™ใ€‚ใพใŸใฏใ€ๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใฏ `no object` ใŒๆธกใ•ใ‚Œใพใ™ใ€‚ + + DETRใฏๅ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใ‚’ไธฆ่กŒใ—ใฆใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆใ€*N*ใฎๆœ€็ต‚็š„ใชไบˆๆธฌ๏ผˆ*N*ใฏใ‚ฏใ‚จใƒชใฎๆ•ฐ๏ผ‰ใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ๅ…ธๅž‹็š„ใช่‡ชๅทฑๅ›žๅธฐใƒขใƒ‡ใƒซใŒ1ใคใฎ่ฆ็ด ใ‚’1ๅ›žใšใคไบˆๆธฌใ™ใ‚‹ใฎใจใฏ็•ฐใชใ‚Šใ€ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใฏใ‚ปใƒƒใƒˆไบˆๆธฌใ‚ฟใ‚นใ‚ฏ๏ผˆ`ใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚น`ใ€`ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซ`๏ผ‰ใงใ‚ใ‚Šใ€1ๅ›žใฎใƒ‘ใ‚นใง*N*ใฎไบˆๆธฌใ‚’่กŒใ„ใพใ™ใ€‚ + +3. ่จ“็ทดไธญใ€DETRใฏ*ไบŒ้ƒจใƒžใƒƒใƒใƒณใ‚ฐๆๅคฑ*ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ›บๅฎšใ•ใ‚ŒใŸๆ•ฐใฎไบˆๆธฌใจๅ›บๅฎšใ•ใ‚ŒใŸไธ€้€ฃใฎๆญฃ่งฃใƒฉใƒ™ใƒซใ‚’ๆฏ”่ผƒใ—ใพใ™ใ€‚ *N*ใฎใƒฉใƒ™ใƒซใ‚ปใƒƒใƒˆใซๆญฃ่งฃใƒฉใƒ™ใƒซใŒๅฐ‘ใชใ„ๅ ดๅˆใ€ `no object` ใ‚ฏใƒฉใ‚นใงใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ใ“ใฎๆๅคฑ้–ขๆ•ฐใฏใ€DETRใซไบˆๆธฌใจๆญฃ่งฃใƒฉใƒ™ใƒซใจใฎ้–“ใง1ๅฏพ1ใฎๅ‰ฒใ‚Šๅฝ“ใฆใ‚’่ฆ‹ใคใ‘ใ‚‹ใ‚ˆใ†ใซไฟƒใ—ใพใ™ใ€‚ใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚นใพใŸใฏใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใฎใฉใกใ‚‰ใ‹ใŒๆญฃใ—ใใชใ„ๅ ดๅˆใ€ๆๅคฑใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ๅŒๆง˜ใซใ€DETRใŒๅญ˜ๅœจใ—ใชใ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไบˆๆธฌใ—ใŸๅ ดๅˆใ€็ฝฐ้‡‘ใŒ็ง‘ใ›ใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€DETRใฏ1ใคใฎ้žๅธธใซ้ก•่‘—ใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใ‚‹ใฎใงใฏใชใใ€็”ปๅƒๅ†…ใฎไป–ใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ฆ‹ใคใ‘ใ‚‹ใ‚ˆใ†ใซไฟƒใ•ใ‚Œใพใ™ใ€‚ + +DETRใฎไธŠใซใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใฆใ€ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใจใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚นใฎๅบงๆจ™ใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใƒ˜ใƒƒใƒ‰ใซใฏ2ใคใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใŒใ‚ใ‚Šใพใ™๏ผšใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใ‚’ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใฎใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขๅฑคใ€ใŠใ‚ˆใณใƒใ‚ฆใƒณใƒ‡ใ‚ฃใƒณใ‚ฐใƒœใƒƒใ‚ฏใ‚นใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใฎMLPใงใ™ใ€‚ + +ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸDETROใฎๅฎŒๅ…จใช[ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆๆคœๅ‡บใ‚ฌใ‚คใƒ‰](tasks/object_detection)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใ€DETROใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใจๆŽจ่ซ–ๆ–นๆณ•ใ‚’ๅญฆใ‚“ใงใใ ใ•ใ„๏ผ + +### Image segmentation + +[Mask2Former](model_doc/mask2former)ใฏใ€ใ™ในใฆใฎ็จฎ้กžใฎ็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใฎใƒฆใƒ‹ใƒใƒผใ‚ตใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใงใ™ใ€‚ๅพ“ๆฅใฎใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใƒขใƒ‡ใƒซใฏ้€šๅธธใ€ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ€ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ€ใพใŸใฏใƒ‘ใƒŽใƒ—ใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎ็‰นๅฎšใฎใ‚ตใƒ–ใ‚ฟใ‚นใ‚ฏใซๅˆใ‚ใ›ใฆ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚Mask2Formerใฏใ€ใใ‚Œใ‚‰ใฎใ‚ฟใ‚นใ‚ฏใฎใใ‚Œใžใ‚Œใ‚’*ใƒžใ‚นใ‚ฏๅˆ†้กž*ใฎๅ•้กŒใจใ—ใฆๆ‰ใˆใพใ™ใ€‚ใƒžใ‚นใ‚ฏๅˆ†้กžใฏใƒ”ใ‚ฏใ‚ปใƒซใ‚’*N*ใฎใ‚ปใ‚ฐใƒกใƒณใƒˆใซใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ—ใ€ไธŽใˆใ‚‰ใ‚ŒใŸ็”ปๅƒใซๅฏพใ—ใฆ*N*ใฎใƒžใ‚นใ‚ฏใจใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€Mask2Formerใฎๅ‹•ไฝœๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใ€ๆœ€ๅพŒใซSegFormerใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ‚’่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +
+ +
+ +Mask2Formerใฎไธป่ฆใชใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฏๆฌกใฎ3ใคใงใ™ใ€‚ + +1. [Swin](model_doc/swin)ใƒใƒƒใ‚ฏใƒœใƒผใƒณใฏ็”ปๅƒใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€3ใคใฎ้€ฃ็ถšใ™ใ‚‹3x3ใฎ็•ณใฟ่พผใฟใ‹ใ‚‰ไฝŽ่งฃๅƒๅบฆใฎ็”ปๅƒ็‰นๅพดใƒžใƒƒใƒ—ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + +2. ็‰นๅพดใƒžใƒƒใƒ—ใฏ*ใƒ”ใ‚ฏใ‚ปใƒซใƒ‡ใ‚ณใƒผใƒ€ใƒผ*ใซๆธกใ•ใ‚Œใ€ไฝŽ่งฃๅƒๅบฆใฎ็‰นๅพดใ‚’้ซ˜่งฃๅƒๅบฆใฎใƒ”ใ‚ฏใ‚ปใƒซๅŸ‹ใ‚่พผใฟใซๅพใ€…ใซใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ใƒ”ใ‚ฏใ‚ปใƒซใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏๅฎŸ้š›ใซใฏ่งฃๅƒๅบฆ1/32ใ€1/16ใ€ใŠใ‚ˆใณ1/8ใฎใ‚ชใƒชใ‚ธใƒŠใƒซ็”ปๅƒใฎใƒžใƒซใƒใ‚นใ‚ฑใƒผใƒซ็‰นๅพด๏ผˆไฝŽ่งฃๅƒๅบฆใจ้ซ˜่งฃๅƒๅบฆใฎ็‰นๅพดใ‚’ๅซใ‚€๏ผ‰ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ + +3. ใ“ใ‚Œใ‚‰ใฎ็•ฐใชใ‚‹ใ‚นใ‚ฑใƒผใƒซใฎ็‰นๅพดใƒžใƒƒใƒ—ใฎใใ‚Œใžใ‚Œใฏใ€้ซ˜่งฃๅƒๅบฆใฎ็‰นๅพดใ‹ใ‚‰ๅฐใ•ใ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ™ใ‚‹ใŸใ‚ใซ1ๅ›žใšใคใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒฌใ‚คใƒคใƒผใซๆธกใ•ใ‚Œใพใ™ใ€‚Mask2Formerใฎ่ฆ็‚นใฏใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ*ใƒžใ‚นใ‚ฏใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณ*ใƒกใ‚ซใƒ‹ใ‚บใƒ ใงใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใŒ็”ปๅƒๅ…จไฝ“ใซๆณจๆ„ใ‚’ๅ‘ใ‘ใ‚‹ใ“ใจใŒใงใใ‚‹ใฎใซๅฏพใ—ใ€ใƒžใ‚นใ‚ฏใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใฏ็”ปๅƒใฎ็‰นๅฎšใฎ้ ˜ๅŸŸใซใฎใฟ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ใ“ใ‚Œใฏ้€Ÿใใ€ใƒญใƒผใ‚ซใƒซใช็”ปๅƒ็‰นๅพดใ ใ‘ใงใ‚‚ใƒขใƒ‡ใƒซใŒๅญฆ็ฟ’ใงใใ‚‹ใŸใ‚ใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใŒๅ‘ไธŠใ—ใพใ™ใ€‚ + +4. [DETR](tasks_explained#object-detection)ใจๅŒๆง˜ใซใ€Mask2Formerใ‚‚ๅญฆ็ฟ’ใ•ใ‚ŒใŸใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚ฏใ‚จใƒชใ‚’ไฝฟ็”จใ—ใ€็”ปๅƒใฎ็‰นๅพดใจ็ต„ใฟๅˆใ‚ใ›ใฆใ‚ปใƒƒใƒˆใฎไบˆๆธฌ๏ผˆ`ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซ`ใ€`ใƒžใ‚นใ‚ฏไบˆๆธฌ`๏ผ‰ใ‚’่กŒใ„ใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใฏ็ทšๅฝขๅฑคใซๆธกใ•ใ‚Œใ€ใ‚ฏใƒฉใ‚นใƒฉใƒ™ใƒซใซๅฏพใ™ใ‚‹ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ•ใ‚Œใพใ™ใ€‚ใƒญใ‚ธใƒƒใƒˆใจๆญฃ่งฃใƒฉใƒ™ใƒซ้–“ใฎใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใŒๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใ‚‚ใฎใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + + ใƒžใ‚นใ‚ฏไบˆๆธฌใฏใ€ใƒ”ใ‚ฏใ‚ปใƒซๅŸ‹ใ‚่พผใฟใจๆœ€็ต‚็š„ใชใƒ‡ใ‚ณใƒผใƒ€ใƒผใฎ้š ใ‚Œ็Šถๆ…‹ใ‚’็ต„ใฟๅˆใ‚ใ›ใฆ็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ใ‚ทใ‚ฐใƒขใ‚คใƒ‰ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผใ‚„ใƒ€ใ‚คใ‚นๆๅคฑใŒใƒญใ‚ธใƒƒใƒˆใจๆญฃ่งฃใƒžใ‚นใ‚ฏใฎ้–“ใงๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒžใ‚นใ‚ฏใ‚’่ฆ‹ใคใ‘ใพใ™ใ€‚ + +ใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚ฟใ‚นใ‚ฏใซๅ–ใ‚Š็ต„ใ‚€ๆบ–ๅ‚™ใŒใงใใพใ—ใŸใ‹๏ผŸSegFormerใฎใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐๆ–นๆณ•ใจๆŽจ่ซ–ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[็”ปๅƒใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚ฌใ‚คใƒ‰](tasks/semantic_segmentation)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + +### Depth estimation + +[GLPN](model_doc/glpn)ใ€*Global-Local Path Network*ใ€ใฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใพใŸใฏๆทฑๅบฆๆŽจๅฎšใชใฉใฎๅฏ†ใชไบˆๆธฌใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใฆใ„ใพใ™ใ€‚[SegFormer](model_doc/segformer)ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’่ปฝ้‡ใƒ‡ใ‚ณใƒผใƒ€ใƒผใจ็ต„ใฟๅˆใ‚ใ›ใŸTransformerใƒ™ใƒผใ‚นใฎๆทฑๅบฆๆŽจๅฎšใƒขใƒ‡ใƒซใงใ™ใ€‚ + +
+ +
+ +1. ViTใฎใ‚ˆใ†ใซใ€็”ปๅƒใฏใƒ‘ใƒƒใƒใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใซๅˆ†ๅ‰ฒใ•ใ‚Œใพใ™ใŒใ€ใ“ใ‚Œใ‚‰ใฎ็”ปๅƒใƒ‘ใƒƒใƒใฏๅฐใ•ใ„ใงใ™ใ€‚ใ“ใ‚Œใฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚„ๆทฑๅบฆๆŽจๅฎšใชใฉใฎๅฏ†ใชไบˆๆธฌใ‚ฟใ‚นใ‚ฏใซ้ฉใ—ใฆใ„ใพใ™ใ€‚็”ปๅƒใƒ‘ใƒƒใƒใฏใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใซๅค‰ๆ›ใ•ใ‚Œใพใ™๏ผˆใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใฎไฝœๆˆๆ–นๆณ•ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€[็”ปๅƒๅˆ†้กž](#image-classification)ใ‚ปใ‚ฏใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซๆธกใ•ใ‚Œใพใ™ใ€‚ + +2. ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฏใƒ‘ใƒƒใƒๅŸ‹ใ‚่พผใฟใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€่ค‡ๆ•ฐใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ–ใƒญใƒƒใ‚ฏใ‚’้€šใ˜ใฆใใ‚Œใ‚‰ใ‚’ๆธกใ—ใพใ™ใ€‚ๅ„ใƒ–ใƒญใƒƒใ‚ฏใซใฏใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใจMix-FFNใƒฌใ‚คใƒคใƒผใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ๅพŒ่€…ใฎๅฝนๅ‰ฒใฏไฝ็ฝฎๆƒ…ๅ ฑใ‚’ๆไพ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ๅ„ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ–ใƒญใƒƒใ‚ฏใฎๆœ€ๅพŒใซใฏใ€้šŽๅฑค็š„่กจ็พใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใฎ*ใƒ‘ใƒƒใƒใƒžใƒผใ‚ธใƒณใ‚ฐ*ใƒฌใ‚คใƒคใƒผใŒใ‚ใ‚Šใพใ™ใ€‚้šฃๆŽฅใ™ใ‚‹ใƒ‘ใƒƒใƒใฎใ‚ฐใƒซใƒผใƒ—ใ”ใจใฎ็‰นๅพดใŒ้€ฃ็ตใ•ใ‚Œใ€้€ฃ็ตใ•ใ‚ŒใŸ็‰นๅพดใซๅฏพใ—ใฆ็ทšๅฝขๅฑคใŒ้ฉ็”จใ•ใ‚Œใ€ใƒ‘ใƒƒใƒใฎๆ•ฐใ‚’1/4ใฎ่งฃๅƒๅบฆใซๅ‰Šๆธ›ใ—ใพใ™ใ€‚ใ“ใ‚ŒใŒๆฌกใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ–ใƒญใƒƒใ‚ฏใธใฎๅ…ฅๅŠ›ใจใชใ‚Šใ€ใ“ใ“ใงใฏใ“ใฎใƒ—ใƒญใ‚ปใ‚นๅ…จไฝ“ใŒ็นฐใ‚Š่ฟ”ใ•ใ‚Œใ€ๅ…ƒใฎ็”ปๅƒใฎ1/8ใ€1/16ใ€ใŠใ‚ˆใณ1/32ใฎ่งฃๅƒๅบฆใฎ็”ปๅƒ็‰นๅพดใŒๅพ—ใ‚‰ใ‚Œใพใ™ใ€‚ + +3. ่ปฝ้‡ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใ€ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‹ใ‚‰ใฎๆœ€ๅพŒใฎ็‰นๅพดใƒžใƒƒใƒ—๏ผˆ1/32ใ‚นใ‚ฑใƒผใƒซ๏ผ‰ใ‚’ๅ—ใ‘ๅ–ใ‚Šใ€ใใ‚Œใ‚’1/16ใ‚นใ‚ฑใƒผใƒซใซใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ—ใพใ™ใ€‚ใใฎๅพŒใ€็‰นๅพดใฏๅ„็‰นๅพดใซๅฏพใ™ใ‚‹ใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใƒžใƒƒใƒ—ใ‹ใ‚‰ใƒญใƒผใ‚ซใƒซใจใ‚ฐใƒญใƒผใƒใƒซใช็‰นๅพดใ‚’้ธๆŠžใ—ใฆ็ต„ใฟๅˆใ‚ใ›ใ‚‹*ใ‚ปใƒฌใ‚ฏใƒ†ใ‚ฃใƒ–ใƒ•ใ‚ฃใƒผใƒใƒฃใƒผใƒ•ใƒฅใƒผใ‚ธใƒงใƒณ๏ผˆSFF๏ผ‰*ใƒขใ‚ธใƒฅใƒผใƒซใซๆธกใ•ใ‚Œใ€1/8ใซใ‚ขใƒƒใƒ—ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏใƒ‡ใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็‰นๅพดใŒๅ…ƒใฎ็”ปๅƒใจๅŒใ˜ใ‚ตใ‚คใ‚บใซใชใ‚‹ใพใง็นฐใ‚Š่ฟ”ใ•ใ‚Œใพใ™ใ€‚ + +4. ใƒ‡ใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็‰นๅพดใฏใ€ๆœ€็ต‚็š„ใชไบˆๆธฌใ‚’่กŒใ†ใŸใ‚ใซใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ€ๆทฑๅบฆๆŽจๅฎšใ€ใพใŸใฏใใฎไป–ใฎๅฏ†ใชไบˆๆธฌใ‚ฟใ‚นใ‚ฏใซไพ›็ตฆใ•ใ‚Œใพใ™ใ€‚ใ‚ปใƒžใƒณใƒ†ใ‚ฃใƒƒใ‚ฏใ‚ปใ‚ฐใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎๅ ดๅˆใ€็‰นๅพดใฏใ‚ฏใƒฉใ‚นๆ•ฐใซๅฏพใ™ใ‚‹ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ•ใ‚Œใ€ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใ‚’ไฝฟ็”จใ—ใฆๆœ€้ฉๅŒ–ใ•ใ‚Œใพใ™ใ€‚ๆทฑๅบฆๆŽจๅฎšใฎๅ ดๅˆใ€็‰นๅพดใฏๆทฑๅบฆใƒžใƒƒใƒ—ใซๅค‰ๆ›ใ•ใ‚Œใ€ๅนณๅ‡็ตถๅฏพ่ชคๅทฎ๏ผˆMAE๏ผ‰ใพใŸใฏๅนณๅ‡ไบŒไน—่ชคๅทฎ๏ผˆMSE๏ผ‰ๆๅคฑใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + + + +## Natural language processing + +Transformerใฏๆœ€ๅˆใซๆฉŸๆขฐ็ฟป่จณใฎใŸใ‚ใซ่จญ่จˆใ•ใ‚Œใ€ใใ‚Œไปฅ้™ใ€ใปใจใ‚“ใฉใฎNLPใ‚ฟใ‚นใ‚ฏใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใจใชใฃใฆใ„ใพใ™ใ€‚ไธ€้ƒจใฎใ‚ฟใ‚นใ‚ฏใฏTransformerใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผๆง‹้€ ใซ้ฉใ—ใฆใŠใ‚Šใ€ไป–ใฎใ‚ฟใ‚นใ‚ฏใฏใƒ‡ใ‚ณใƒผใƒ€ใƒผใซ้ฉใ—ใฆใ„ใพใ™ใ€‚ใ•ใ‚‰ใซใ€ไธ€้ƒจใฎใ‚ฟใ‚นใ‚ฏใงใฏTransformerใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผ-ใƒ‡ใ‚ณใƒผใƒ€ใƒผๆง‹้€ ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +### Text classification + +[BERT](model_doc/bert)ใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎใฟใฎใƒขใƒ‡ใƒซใงใ‚ใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใฎ่ฑŠใ‹ใช่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใŸใ‚ใซไธกๅดใฎๅ˜่ชžใซๆณจๆ„ใ‚’ๆ‰•ใ†ใ“ใจใงใ€ๆทฑใ„ๅŒๆ–นๅ‘ๆ€งใ‚’ๅŠนๆžœ็š„ใซๅฎŸ่ฃ…ใ—ใŸๆœ€ๅˆใฎใƒขใƒ‡ใƒซใงใ™ใ€‚ + +1. BERTใฏ[WordPiece](tokenizer_summary#wordpiece)ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚ญใ‚นใƒˆใฎใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ๅ˜ไธ€ใฎๆ–‡ใจๆ–‡ใฎใƒšใ‚ขใ‚’ๅŒบๅˆฅใ™ใ‚‹ใŸใ‚ใซใ€็‰นๅˆฅใช `[SEP]` ใƒˆใƒผใ‚ฏใƒณใŒ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ `[CLS]` ใƒˆใƒผใ‚ฏใƒณใฏใ™ในใฆใฎใƒ†ใ‚ญใ‚นใƒˆใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๅ…ˆ้ ญใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ `[CLS]` ใƒˆใƒผใ‚ฏใƒณใจใจใ‚‚ใซๆœ€็ต‚ๅ‡บๅŠ›ใฏใ€ๅˆ†้กžใ‚ฟใ‚นใ‚ฏใฎใŸใ‚ใฎๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚BERTใฏใพใŸใ€ใƒˆใƒผใ‚ฏใƒณใŒๆ–‡ใฎใƒšใ‚ขใฎๆœ€ๅˆใพใŸใฏ2็•ช็›ฎใฎๆ–‡ใซๅฑžใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็คบใ™ใ‚ปใ‚ฐใƒกใƒณใƒˆๅŸ‹ใ‚่พผใฟใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +2. BERTใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใง2ใคใฎ็›ฎๆจ™ใ‚’ไฝฟ็”จใ—ใพใ™๏ผšใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใจๆฌกใฎๆ–‡ใฎไบˆๆธฌใงใ™ใ€‚ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใงใฏใ€ๅ…ฅๅŠ›ใƒˆใƒผใ‚ฏใƒณใฎไธ€้ƒจใŒใƒฉใƒณใƒ€ใƒ ใซใƒžใ‚นใ‚ฏใ•ใ‚Œใ€ใƒขใƒ‡ใƒซใฏใ“ใ‚Œใ‚‰ใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒๅ…จใฆใฎๅ˜่ชžใ‚’่ฆ‹ใฆใ€Œๆฌกใฎๅ˜่ชžใ€ใ‚’ไบˆๆธฌใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ๅŒๆ–นๅ‘ๆ€งใฎๅ•้กŒใŒ่งฃๆฑบใ•ใ‚Œใพใ™ใ€‚ไบˆๆธฌใ•ใ‚ŒใŸใƒžใ‚นใ‚ฏใƒˆใƒผใ‚ฏใƒณใฎๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใฏใ€ใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚นใ‚’ไฝฟ็”จใ—ใŸๅ˜่ชžใฎใƒžใ‚นใ‚ฏใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใฎใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใซๆธกใ•ใ‚Œใพใ™ใ€‚ + + 2็•ช็›ฎใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏๆฌกใฎๆ–‡ใฎไบˆๆธฌใงใ™ใ€‚ใƒขใƒ‡ใƒซใฏๆ–‡AใฎๅพŒใซๆ–‡BใŒ็ถšใใ‹ใฉใ†ใ‹ใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๅŠๅˆ†ใฎๅ ดๅˆใ€ๆ–‡Bใฏๆฌกใฎๆ–‡ใงใ‚ใ‚Šใ€ๆฎ‹ใ‚ŠใฎๅŠๅˆ†ใฎๅ ดๅˆใ€ๆ–‡Bใฏใƒฉใƒณใƒ€ใƒ ใชๆ–‡ใงใ™ใ€‚ไบˆๆธฌ๏ผˆๆฌกใฎๆ–‡ใ‹ใฉใ†ใ‹๏ผ‰ใฏใ€2ใคใฎใ‚ฏใƒฉใ‚น๏ผˆ`IsNext`ใŠใ‚ˆใณ`NotNext`๏ผ‰ใซๅฏพใ™ใ‚‹ใ‚ฝใƒ•ใƒˆใƒžใƒƒใ‚ฏใ‚นใ‚’ๆŒใคใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใซๆธกใ•ใ‚Œใพใ™ใ€‚ + +3. ๅ…ฅๅŠ›ๅŸ‹ใ‚่พผใฟใฏใ€ๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใŸใ‚ใซ่ค‡ๆ•ฐใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒฌใ‚คใƒคใƒผใ‚’ไป‹ใ—ใฆๆธกใ•ใ‚Œใพใ™ใ€‚ + +ไบ‹ๅ‰่จ“็ทดๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใซไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒ™ใƒผใ‚นใฎBERTใƒขใƒ‡ใƒซใฎไธŠใซใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ใใ‚Œใ‚‰ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขๅฑคใงใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใฏใ€ใƒญใ‚ธใƒƒใƒˆใจใ‚ฟใƒผใ‚ฒใƒƒใƒˆ้–“ใงๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒฉใƒ™ใƒซใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ + +ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚’่ฉฆใ—ใฆใฟใ‚‹ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸDistilBERTใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€ๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[ใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฌใ‚คใƒ‰(่‹ฑ่ชž็‰ˆ)](../en/tasks/sequence_classification)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + +### Token classification + +BERTใ‚’ๅๅ‰ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃ่ช่ญ˜๏ผˆNER๏ผ‰ใชใฉใฎใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฟใ‚นใ‚ฏใซไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒ™ใƒผใ‚นใฎBERTใƒขใƒ‡ใƒซใฎไธŠใซใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใƒ˜ใƒƒใƒ‰ใฏๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ใใ‚Œใ‚‰ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขๅฑคใงใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใฏใ€ใƒญใ‚ธใƒƒใƒˆใจๅ„ใƒˆใƒผใ‚ฏใƒณ้–“ใงๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒฉใƒ™ใƒซใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ + +ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚’่ฉฆใ—ใฆใฟใ‚‹ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸDistilBERTใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€ๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใ‚ฌใ‚คใƒ‰](tasks/token_classification)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + +### Question answering + +BERTใ‚’่ณชๅ•ๅฟœ็ญ”ใซไฝฟ็”จใ™ใ‚‹ใซใฏใ€ใƒ™ใƒผใ‚นใฎBERTใƒขใƒ‡ใƒซใฎไธŠใซใ‚นใƒ‘ใƒณๅˆ†้กžใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใฎ็ทšๅฝขๅฑคใฏๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใ€ๅ›ž็ญ”ใซๅฏพๅฟœใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใฎใ€Œใ‚นใƒ‘ใƒณใ€้–‹ๅง‹ใจ็ต‚ไบ†ใฎใƒญใ‚ธใƒƒใƒˆใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใฏใ€ใƒญใ‚ธใƒƒใƒˆใจใƒฉใƒ™ใƒซไฝ็ฝฎใจใฎ้–“ใงๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒ†ใ‚ญใ‚นใƒˆใ‚นใƒ‘ใƒณใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซ่จˆ็ฎ—ใ•ใ‚Œใพใ™ใ€‚ + +่ณชๅ•ๅฟœ็ญ”ใ‚’่ฉฆใ—ใฆใฟใ‚‹ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸDistilBERTใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€ๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[่ณชๅ•ๅฟœ็ญ”ใ‚ฌใ‚คใƒ‰](tasks/question_answering)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + + +๐Ÿ’ก ๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ไธ€ๅบฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒๅฎŒไบ†ใ—ใŸBERTใ‚’ไฝฟ็”จใ—ใฆใ•ใพใ–ใพใชใ‚ฟใ‚นใ‚ฏใซ็ฐกๅ˜ใซ้ฉ็”จใงใใ‚‹ใ“ใจใซๆณจ็›ฎใ—ใฆใใ ใ•ใ„ใ€‚ๅฟ…่ฆใชใฎใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใซ็‰นๅฎšใฎใƒ˜ใƒƒใƒ‰ใ‚’่ฟฝๅŠ ใ—ใฆใ€้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๆ‰€ๆœ›ใฎๅ‡บๅŠ›ใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใ ใ‘ใงใ™๏ผ + + + +### Text generation + +[GPT-2](model_doc/gpt2)ใฏๅคง้‡ใฎใƒ†ใ‚ญใ‚นใƒˆใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จใƒขใƒ‡ใƒซใงใ™ใ€‚ใƒ—ใƒญใƒณใƒ—ใƒˆใ‚’ไธŽใˆใ‚‹ใจ่ชฌๅพ—ๅŠ›ใฎใ‚ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆใ‚’็”Ÿๆˆใ—ใ€ๆ˜Ž็คบ็š„ใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใฆใ„ใชใ„ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใ€่ณชๅ•ๅฟœ็ญ”ใชใฉใฎไป–ใฎNLPใ‚ฟใ‚นใ‚ฏใ‚‚ๅฎŒไบ†ใงใใพใ™ใ€‚ + +
+ +
+ +1. GPT-2ใฏ[ใƒใ‚คใƒˆใƒšใ‚ขใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐ๏ผˆBPE๏ผ‰](tokenizer_summary#bytepair-encoding-bpe)ใ‚’ไฝฟ็”จใ—ใฆๅ˜่ชžใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ—ใ€ใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ไฝ็ฝฎใ‚จใƒณใ‚ณใƒผใƒ‡ใ‚ฃใƒณใ‚ฐใŒใƒˆใƒผใ‚ฏใƒณๅŸ‹ใ‚่พผใฟใซ่ฟฝๅŠ ใ•ใ‚Œใ€ๅ„ใƒˆใƒผใ‚ฏใƒณใฎไฝ็ฝฎใ‚’็คบใ—ใพใ™ใ€‚ๅ…ฅๅŠ›ๅŸ‹ใ‚่พผใฟใฏ่ค‡ๆ•ฐใฎใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ–ใƒญใƒƒใ‚ฏใ‚’ไป‹ใ—ใฆๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹ใŸใ‚ใซๆธกใ•ใ‚Œใพใ™ใ€‚ๅ„ใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒ–ใƒญใƒƒใ‚ฏๅ†…ใงใ€GPT-2ใฏใ€Œใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่‡ชๅทฑๆณจๆ„ใ€ใƒฌใ‚คใƒคใƒผใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใ€GPT-2ใŒๆœชๆฅใฎใƒˆใƒผใ‚ฏใƒณใซๆณจๆ„ใ‚’ๆ‰•ใ†ใ“ใจใฏใงใใชใ„ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚GPT-2ใฏๅทฆๅดใฎใƒˆใƒผใ‚ฏใƒณใซใฎใฟๆณจๆ„ใ‚’ๆ‰•ใ†ใ“ใจใŒ่จฑๅฏใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚ŒใฏBERTใฎ[`mask`]ใƒˆใƒผใ‚ฏใƒณใจใฏ็•ฐใชใ‚Šใ€ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸ่‡ชๅทฑๆณจๆ„ใงใฏๆœชๆฅใฎใƒˆใƒผใ‚ฏใƒณใซๅฏพใ—ใฆใ‚นใ‚ณใ‚ขใ‚’`0`ใซ่จญๅฎšใ™ใ‚‹ใŸใ‚ใฎๆณจๆ„ใƒžใ‚นใ‚ฏใŒไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +2. ใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‹ใ‚‰ใฎๅ‡บๅŠ›ใฏใ€่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ˜ใƒƒใƒ‰ใซๆธกใ•ใ‚Œใ€ๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขๅค‰ๆ›ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใƒฉใƒ™ใƒซใฏใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎใƒˆใƒผใ‚ฏใƒณใงใ‚ใ‚Šใ€ใ“ใ‚Œใฏใƒญใ‚ธใƒƒใƒˆใ‚’ๅณใซ1ใคใšใ‚‰ใ—ใฆ็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใฏใ€ใ‚ทใƒ•ใƒˆใ•ใ‚ŒใŸใƒญใ‚ธใƒƒใƒˆใจใƒฉใƒ™ใƒซ้–“ใง่จˆ็ฎ—ใ•ใ‚Œใ€ๆฌกใซๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒˆใƒผใ‚ฏใƒณใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใ€‚ + +GPT-2ใฎไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ็›ฎๆจ™ใฏๅฎŒๅ…จใซ[ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐ](glossary#causal-language-modeling)ใซๅŸบใฅใ„ใฆใŠใ‚Šใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅ†…ใฎๆฌกใฎๅ˜่ชžใ‚’ไบˆๆธฌใ—ใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€GPT-2ใฏใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚’ๅซใ‚€ใ‚ฟใ‚นใ‚ฏใง็‰นใซๅ„ชใ‚ŒใŸๆ€ง่ƒฝใ‚’็™บๆฎใ—ใพใ™ใ€‚ + +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚’่ฉฆใ—ใฆใฟใ‚‹ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸDistilGPT-2ใ‚’ๅพฎ่ชฟๆ•ดใ—ใ€ๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[ๅ› ๆžœ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใ‚ฌใ‚คใƒ‰](tasks/language_modeling#causal-language-modeling)ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + + +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซ้–ขใ™ใ‚‹่ฉณ็ดฐใฏใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](generation_strategies)ใ‚ฌใ‚คใƒ‰ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + + + +### Summarization + +[BART](model_doc/bart) ใ‚„ [T5](model_doc/t5) ใฎใ‚ˆใ†ใชใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใฏใ€่ฆ็ด„ใ‚ฟใ‚นใ‚ฏใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใƒปใƒˆใ‚ฅใƒปใ‚ทใƒผใ‚ฑใƒณใ‚นใƒปใƒ‘ใ‚ฟใƒผใƒณใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€BARTใฎๅ‹•ไฝœๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใ€ๆœ€ๅพŒใซT5ใฎๅพฎ่ชฟๆ•ดใ‚’่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +
+ +
+ +1. BARTใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฏใ€BERTใจ้žๅธธใซไผผใฆใŠใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใฎใƒˆใƒผใ‚ฏใƒณใจไฝ็ฝฎใ‚จใƒณใƒ™ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ™ใ€‚BARTใฏใ€ๅ…ฅๅŠ›ใ‚’็ ดๅฃŠใ—ใฆใ‹ใ‚‰ใƒ‡ใ‚ณใƒผใƒ€ใƒผใงๅ†ๆง‹็ฏ‰ใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใพใ™ใ€‚็‰นๅฎšใฎ็ ดๅฃŠๆˆฆ็•ฅใ‚’ๆŒใคไป–ใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใจใฏ็•ฐใชใ‚Šใ€BARTใฏไปปๆ„ใฎ็จฎ้กžใฎ็ ดๅฃŠใ‚’้ฉ็”จใงใใพใ™ใ€‚ใŸใ ใ—ใ€*ใƒ†ใ‚ญใ‚นใƒˆใ‚คใƒณใƒ•ใ‚ฃใƒชใƒณใ‚ฐ*็ ดๅฃŠๆˆฆ็•ฅใŒๆœ€้ฉใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚คใƒณใƒ•ใ‚ฃใƒชใƒณใ‚ฐใงใฏใ€ใ„ใใคใ‹ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚นใƒ‘ใƒณใŒ**ๅ˜ไธ€ใฎ** [`mask`] ใƒˆใƒผใ‚ฏใƒณใง็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏ้‡่ฆใงใ™ใ€ใชใœใชใ‚‰ใƒขใƒ‡ใƒซใฏใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ—ใชใ‘ใ‚Œใฐใชใ‚‰ใšใ€ใƒขใƒ‡ใƒซใซๆฌ ่ฝใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐใ‚’ไบˆๆธฌใ•ใ›ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚ๅ…ฅๅŠ›ๅŸ‹ใ‚่พผใฟใจใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใ‚นใƒ‘ใƒณใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’ไป‹ใ—ใฆๆœ€็ต‚็š„ใช้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ๅ‡บๅŠ›ใ—ใพใ™ใŒใ€BERTใจใฏ็•ฐใชใ‚Šใ€BARTใฏๅ˜่ชžใ‚’ไบˆๆธฌใ™ใ‚‹ใŸใ‚ใฎๆœ€็ต‚็š„ใชใƒ•ใ‚ฃใƒผใƒ‰ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒใƒƒใƒˆใƒฏใƒผใ‚ฏใ‚’ๆœ€ๅพŒใซ่ฟฝๅŠ ใ—ใพใ›ใ‚“ใ€‚ + +2. ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎๅ‡บๅŠ›ใฏใƒ‡ใ‚ณใƒผใƒ€ใƒผใซๆธกใ•ใ‚Œใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎๅ‡บๅŠ›ใ‹ใ‚‰ใƒžใ‚นใ‚ฏใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใจ้ž็ ดๅฃŠใƒˆใƒผใ‚ฏใƒณใ‚’ไบˆๆธฌใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ‡ใ‚ณใƒผใƒ€ใƒผใฏๅ…ƒใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅพฉๅ…ƒใ™ใ‚‹ใฎใซๅฝน็ซ‹ใค่ฟฝๅŠ ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚ใƒ‡ใ‚ณใƒผใƒ€ใƒผใ‹ใ‚‰ใฎๅ‡บๅŠ›ใฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ˜ใƒƒใƒ‰ใซๆธกใ•ใ‚Œใ€้š ใ‚ŒใŸ็Šถๆ…‹ใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ใŸใ‚ใฎ็ทšๅฝขๅค‰ๆ›ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใฏใ€ใƒญใ‚ธใƒƒใƒˆใจใƒฉใƒ™ใƒซใฎ้–“ใง่จˆ็ฎ—ใ•ใ‚Œใ€ใƒฉใƒ™ใƒซใฏๅ˜ใซๅณใซใ‚ทใƒ•ใƒˆใ•ใ‚ŒใŸใƒˆใƒผใ‚ฏใƒณใงใ™ใ€‚ + +่ฆ็ด„ใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸT5ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[่ฆ็ด„ใ‚ฌใ‚คใƒ‰](tasks/summarization)ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผ + + + +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซ้–ขใ™ใ‚‹่ฉณ็ดฐใฏใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](generation_strategies)ใ‚ฌใ‚คใƒ‰ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + + +### Translation + +็ฟป่จณใฏใ€ใ‚‚ใ†ไธ€ใคใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใƒปใƒˆใ‚ฅใƒปใ‚ทใƒผใ‚ฑใƒณใ‚นใƒปใ‚ฟใ‚นใ‚ฏใฎไพ‹ใงใ‚ใ‚Šใ€[BART](model_doc/bart) ใ‚„ [T5](model_doc/t5) ใฎใ‚ˆใ†ใชใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆๅฎŸ่กŒใงใใพใ™ใ€‚ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€BARTใฎๅ‹•ไฝœๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใ€ๆœ€ๅพŒใซT5ใฎๅพฎ่ชฟๆ•ดใ‚’่ฉฆใ™ใ“ใจใŒใงใใพใ™ใ€‚ + +BARTใฏใ€ใ‚ฝใƒผใ‚น่จ€่ชžใ‚’ใ‚ฟใƒผใ‚ฒใƒƒใƒˆ่จ€่ชžใซใƒ‡ใ‚ณใƒผใƒ‰ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใซใ€ๅˆฅๅ€‹ใซใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใง็ฟป่จณใซ้ฉๅฟœใ—ใพใ™ใ€‚ใ“ใฎๆ–ฐใ—ใ„ใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฎๅŸ‹ใ‚่พผใฟใฏใ€ๅ…ƒใฎๅ˜่ชžๅŸ‹ใ‚่พผใฟใฎไปฃใ‚ใ‚Šใซไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใซๆธกใ•ใ‚Œใพใ™ใ€‚ใ‚ฝใƒผใ‚นใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใฏใ€ใƒขใƒ‡ใƒซใฎๅ‡บๅŠ›ใ‹ใ‚‰ใฎใ‚ฏใƒญใ‚นใ‚จใƒณใƒˆใƒญใƒ”ใƒผๆๅคฑใ‚’็”จใ„ใฆใ‚ฝใƒผใ‚นใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใ€ไฝ็ฝฎใ‚จใƒณใƒ™ใƒ‡ใ‚ฃใƒณใ‚ฐใ€ใŠใ‚ˆใณๅ…ฅๅŠ›ใ‚จใƒณใƒ™ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ๆ›ดๆ–ฐใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆ่จ“็ทดใ•ใ‚Œใพใ™ใ€‚ใ“ใฎๆœ€ๅˆใฎใ‚นใƒ†ใƒƒใƒ—ใงใฏใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒๅ›บๅฎšใ•ใ‚Œใ€ใ™ในใฆใฎใƒขใƒ‡ใƒซใƒ‘ใƒฉใƒกใƒผใ‚ฟใŒ2็•ช็›ฎใฎใ‚นใƒ†ใƒƒใƒ—ใงไธ€็ท’ใซ่จ“็ทดใ•ใ‚Œใพใ™ใ€‚ + +ใใฎๅพŒใ€็ฟป่จณใฎใŸใ‚ใซๅคš่จ€่ชž็‰ˆใฎmBARTใŒ็™ปๅ ดใ—ใ€ๅคš่จ€่ชžใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใจใ—ใฆๅˆฉ็”จๅฏ่ƒฝใงใ™ใ€‚ + +็ฟป่จณใ‚’่ฉฆใ™ๆบ–ๅ‚™ใฏใงใใพใ—ใŸใ‹๏ผŸT5ใ‚’ๅพฎ่ชฟๆ•ดใ—ใฆๆŽจ่ซ–ใซไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’ๅญฆใถใŸใ‚ใซใ€ๅฎŒๅ…จใช[็ฟป่จณใ‚ฌใ‚คใƒ‰](tasks/summarization)ใ‚’ใ”่ฆงใใ ใ•ใ„๏ผ + + + +ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใซ้–ขใ™ใ‚‹่ฉณ็ดฐใฏใ€[ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆๆˆฆ็•ฅ](generation_strategies)ใ‚ฌใ‚คใƒ‰ใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใฆใฟใฆใใ ใ•ใ„๏ผ + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/testing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/testing.md new file mode 100644 index 0000000000000000000000000000000000000000..5425861a1d19a660797688004da094332cc9dceb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/testing.md @@ -0,0 +1,1203 @@ + + +# Testing + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใŒใฉใฎใ‚ˆใ†ใซใƒ†ใ‚นใƒˆใ•ใ‚Œใ€ๆ–ฐใ—ใ„ใƒ†ใ‚นใƒˆใ‚’ๆ›ธใ„ใฆๆ—ขๅญ˜ใฎใƒ†ใ‚นใƒˆใ‚’ๆ”นๅ–„ใงใใ‚‹ใ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +ใ“ใฎใƒชใƒใ‚ธใƒˆใƒชใซใฏ2ใคใฎใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใŒใ‚ใ‚Šใพใ™๏ผš + +1. `tests` -- ไธ€่ˆฌ็š„ใชAPI็”จใฎใƒ†ใ‚นใƒˆ +2. `examples` -- APIใฎไธ€้ƒจใงใฏใชใ„ใ•ใพใ–ใพใชใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณ็”จใฎใƒ†ใ‚นใƒˆ + +## How transformers are tested + +1. PRใŒๆๅ‡บใ•ใ‚Œใ‚‹ใจใ€9ใคใฎCircleCiใ‚ธใƒงใƒ–ใงใƒ†ใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚PRใธใฎๆ–ฐใ—ใ„ใ‚ณใƒŸใƒƒใƒˆใ”ใจใซๅ†ใƒ†ใ‚นใƒˆใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใ‚ธใƒงใƒ–ใฏใ€[ใ“ใฎ่จญๅฎšใƒ•ใ‚กใ‚คใƒซ](https://github.com/huggingface/transformers/tree/main/.circleci/config.yml)ใงๅฎš็พฉใ•ใ‚ŒใฆใŠใ‚Šใ€ๅฟ…่ฆใชๅ ดๅˆใฏๅŒใ˜็’ฐๅขƒใ‚’่‡ชๅˆ†ใฎใƒžใ‚ทใƒณใงๅ†็พใงใใพใ™ใ€‚ + + ใ“ใ‚Œใ‚‰ใฎCIใ‚ธใƒงใƒ–ใฏ `@slow` ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ›ใ‚“ใ€‚ + +2. [GitHub Actions](https://github.com/huggingface/transformers/actions)ใซใ‚ˆใฃใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹3ใคใฎใ‚ธใƒงใƒ–ใŒใ‚ใ‚Šใพใ™๏ผš + + - [torch hub integration](https://github.com/huggingface/transformers/tree/main/.github/workflows/github-torch-hub.yml): torch hubใฎ็ตฑๅˆใŒๅ‹•ไฝœใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ—ใพใ™ใ€‚ + + - [self-hosted (push)](https://github.com/huggingface/transformers/tree/main/.github/workflows/self-push.yml): `main` ใซใ‚ณใƒŸใƒƒใƒˆใŒ่กŒใ‚ใ‚ŒใŸๅ ดๅˆใซใ€GPUใง้ซ˜้€Ÿใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ใ“ใฎใ‚ธใƒงใƒ–ใฏใ€`main` ใงใฎใ‚ณใƒŸใƒƒใƒˆใŒไปฅไธ‹ใฎใƒ•ใ‚ฉใƒซใƒ€ใƒผใฎใ‚ณใƒผใƒ‰ใ‚’ๆ›ดๆ–ฐใ—ใŸๅ ดๅˆใซใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™๏ผš`src`ใ€`tests`ใ€`.github`๏ผˆ่ฟฝๅŠ ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใ‚ซใƒผใƒ‰ใ€ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใชใฉใฎๅฎŸ่กŒใ‚’้˜ฒใใŸใ‚๏ผ‰ใ€‚ + + - [self-hosted runner](https://github.com/huggingface/transformers/tree/main/.github/workflows/self-scheduled.yml): GPUใง `tests` ใจ `examples` ใฎ้€šๅธธใฎใƒ†ใ‚นใƒˆใจ้…ใ„ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +RUN_SLOW=1 pytest tests/ +RUN_SLOW=1 pytest examples/ +``` + ็ตๆžœใฏ[here](https://github.com/huggingface/transformers/actions)ใง่ฆณๅฏŸใงใใพใ™ใ€‚ + +## Running tests + + + +### Choosing which tests to run + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฏใ€ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฎๅคšใใฎ่ฉณ็ดฐใซใคใ„ใฆ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ใ™ในใฆใ‚’่ชญใ‚“ใ ๅพŒใงใ‚‚ใ€ใ•ใ‚‰ใซ่ฉณ็ดฐใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€[ใ“ใกใ‚‰](https://docs.pytest.org/en/latest/usage.html)ใง่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ไปฅไธ‹ใฏใ€ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใฎใ„ใใคใ‹ใฎๆœ€ใ‚‚ไพฟๅˆฉใชๆ–นๆณ•ใงใ™ใ€‚ + +ใ™ในใฆๅฎŸ่กŒใ—ใพใ™: +```console +pytest +``` + +ใพใŸใฏ๏ผš +```bash +make test +``` + + +ๅพŒ่€…ใฏๆฌกใฎใ‚ˆใ†ใซๅฎš็พฉใ•ใ‚Œใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +python -m pytest -n auto --dist=loadfile -s -v ./tests/ +``` + +ไปฅไธ‹ใฏใ€pytestใซๆธกใ™่จญๅฎšๆƒ…ๅ ฑใงใ™ใ€‚ + +- ใƒ†ใ‚นใƒˆใƒ—ใƒญใ‚ปใ‚นใ‚’CPUใ‚ณใ‚ขใฎๆ•ฐใจๅŒใ˜ใ ใ‘ๅฎŸ่กŒใ™ใ‚‹ใ‚ˆใ†ใซๆŒ‡็คบใ—ใพใ™ใ€‚ใŸใ ใ—ใ€RAMใŒๅๅˆ†ใงใชใ„ๅ ดๅˆใฏๆณจๆ„ใŒๅฟ…่ฆใงใ™ใ€‚ +- ๅŒใ˜ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆใฏใ€ๅŒใ˜ใƒ†ใ‚นใƒˆใƒ—ใƒญใ‚ปใ‚นใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +- ๅ‡บๅŠ›ใฎใ‚ญใƒฃใƒ—ใƒใƒฃใ‚’่กŒใ„ใพใ›ใ‚“ใ€‚ +- ๅ†—้•ทใƒขใƒผใƒ‰ใงๅฎŸ่กŒใ—ใพใ™ใ€‚ + + +### Getting the list of all tests + +ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆ๏ผš + +```bash +pytest --collect-only -q +``` + +ๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ†ใ‚นใƒˆ ใƒ•ใ‚กใ‚คใƒซใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆ: + +```bash +pytest tests/test_optimization.py --collect-only -q +``` + +### Run a specific test module + +ๅ€‹ๅˆฅใฎใƒ†ใ‚นใƒˆ ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏ: + +```bash +pytest tests/utils/test_logging.py +``` + +### Run specific tests + +ใปใจใ‚“ใฉใฎใƒ†ใ‚นใƒˆใงunittestใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€็‰นๅฎšใฎใ‚ตใƒ–ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ใใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใ‚’ๅซใ‚€unittestใ‚ฏใƒฉใ‚นใฎๅๅ‰ใ‚’็Ÿฅใฃใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ใใ‚Œใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผš + + +```bash +pytest tests/test_optimization.py::OptimizationTest::test_adam_w +``` + +ใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒๆ–นๆณ•: + +ใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซ: `tests/test_optimization.py` +ใ‚ฏใƒฉใ‚นๅ: `OptimizationTest` +ใƒ†ใ‚นใƒˆ้–ขๆ•ฐใฎๅๅ‰: `test_adam_w` + +ใƒ•ใ‚กใ‚คใƒซใซ่ค‡ๆ•ฐใฎใ‚ฏใƒฉใ‚นใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใฏใ€็‰นๅฎšใฎใ‚ฏใƒฉใ‚นใฎใƒ†ใ‚นใƒˆใฎใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚’้ธๆŠžใงใใพใ™ใ€‚ไพ‹ใˆใฐ๏ผš + +```bash +pytest tests/test_optimization.py::OptimizationTest +``` + +ใƒ†ใ‚นใƒˆใ‚ฏใƒฉใ‚นๅ†…ใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +ๅ‰่ฟฐใฎ้€šใ‚Šใ€`OptimizationTest` ใ‚ฏใƒฉใ‚นใซๅซใพใ‚Œใ‚‹ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใงใใพใ™๏ผš + +```bash +pytest tests/test_optimization.py::OptimizationTest --collect-only -q +``` + +ใ‚ญใƒผใƒฏใƒผใƒ‰ๅผใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ + +ๅๅ‰ใซ `adam` ใŒๅซใพใ‚Œใ‚‹ใƒ†ใ‚นใƒˆใฎใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏ๏ผš + +```bash +pytest -k adam tests/test_optimization.py +``` + +`and`ใŠใ‚ˆใณ`or`ใฏใ€ใ™ในใฆใฎใ‚ญใƒผใƒฏใƒผใƒ‰ใŒไธ€่‡ดใ™ใ‚‹ใ‹ใ€ใ„ใšใ‚Œใ‹ใ‚’็คบใ™ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚`not`ใฏๅฆๅฎšใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใงใใพใ™ใ€‚ + +`adam`ใจใ„ใ†ๅๅ‰ใ‚’ๅซใ‚€ใƒ†ใ‚นใƒˆใ‚’้™คใ„ใฆใ™ในใฆใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏ๏ผš + +```bash +pytest -k "not adam" tests/test_optimization.py +``` + + +ไปฅไธ‹ใฏใ€ๆไพ›ใ•ใ‚ŒใŸใƒ†ใ‚ญใ‚นใƒˆใฎๆ—ฅๆœฌ่ชž่จณใงใ™ใ€‚ + +```bash +pytest -k "ada and not adam" tests/test_optimization.py +``` + +ใŸใจใˆใฐใ€`test_adafactor`ใจ`test_adam_w`ใฎไธกๆ–นใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ไฝฟ็”จใงใใพใ™: + +```bash +pytest -k "test_adam_w or test_adam_w" tests/test_optimization.py +``` + +ๆณจๆ„: ใ“ใ“ใงใฏใ€`or` ใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ใ‚ญใƒผใƒฏใƒผใƒ‰ใฎใ„ใšใ‚Œใ‹ไธ€ใคใŒไธ€่‡ดใ™ใ‚Œใฐใ€ไธกๆ–นใ‚’ๅซใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ไธกๆ–นใฎใƒ‘ใ‚ฟใƒผใƒณใ‚’ๅซใ‚€ใƒ†ใ‚นใƒˆใฎใฟใ‚’ๅซใ‚ใŸใ„ๅ ดๅˆใฏใ€`and` ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + +```bash +pytest -k "test and ada" tests/test_optimization.py +``` + +### Run `accelerate` tests + +ๆ™‚ใ€…ใ€ใƒขใƒ‡ใƒซใซๅฏพใ—ใฆ `accelerate` ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€`OPT` ๅฎŸ่กŒใซๅฏพใ—ใฆใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใ€ใ‚ณใƒžใƒณใƒ‰ใซ `-m accelerate_tests` ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™๏ผš + +```bash +RUN_SLOW=1 pytest -m accelerate_tests tests/models/opt/test_modeling_opt.py +``` + +### Run documentation tests + +ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎไพ‹ใŒๆญฃใ—ใ„ใ‹ใฉใ†ใ‹ใ‚’ใƒ†ใ‚นใƒˆใ™ใ‚‹ใซใฏใ€`doctests` ใŒๅˆๆ ผใ—ใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ไพ‹ใจใ—ใฆใ€[`WhisperModel.forward` ใฎใƒ‰ใƒƒใ‚ฏใ‚นใƒˆใƒชใƒณใ‚ฐ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/whisper/modeling_whisper.py#L1017-L1035)ใ‚’ไฝฟ็”จใ—ใพใ—ใ‚‡ใ†ใ€‚ + + +```python +r""" +Returns: + +Example: + ```python + >>> import torch + >>> from transformers import WhisperModel, WhisperFeatureExtractor + >>> from datasets import load_dataset + + >>> model = WhisperModel.from_pretrained("openai/whisper-base") + >>> feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-base") + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_features = inputs.input_features + >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id + >>> last_hidden_state = model(input_features, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> list(last_hidden_state.shape) + [1, 2, 512] + ```""" + +``` + +ๆŒ‡ๅฎšใ—ใŸใƒ•ใ‚กใ‚คใƒซๅ†…ใฎใ™ในใฆใฎใƒ‰ใƒƒใ‚ฏใ‚นใƒˆใƒชใƒณใ‚ฐไพ‹ใ‚’่‡ชๅ‹•็š„ใซใƒ†ใ‚นใƒˆใ™ใ‚‹ใŸใ‚ใซใ€ไปฅไธ‹ใฎ่กŒใ‚’ๅฎŸ่กŒใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pytest --doctest-modules +``` + +ใƒ•ใ‚กใ‚คใƒซใซใƒžใƒผใ‚ฏใƒ€ใ‚ฆใƒณๆ‹กๅผตๅญใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€`--doctest-glob="*.md"`ๅผ•ๆ•ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + + +### Run only modified tests + +[pytest-picked](https://github.com/anapaulagomes/pytest-picked)ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๆœชใ‚นใƒ†ใƒผใ‚ธใƒณใ‚ฐใฎใƒ•ใ‚กใ‚คใƒซใพใŸใฏ็พๅœจใฎใƒ–ใƒฉใƒณใƒ๏ผˆGitใซๅพ“ใฃใฆ๏ผ‰ใซ้–ข้€ฃใ™ใ‚‹ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ใ“ใ‚Œใฏใ€ๅค‰ๆ›ดๅ†…ๅฎนใซ้–ข้€ฃใ™ใ‚‹ใƒ†ใ‚นใƒˆใฎใฟๅฎŸ่กŒใ•ใ‚Œใ‚‹ใŸใ‚ใ€ๅค‰ๆ›ดใŒไฝ•ใ‚‚ๅฃŠใ‚Œใฆใ„ใชใ„ใ“ใจใ‚’่ฟ…้€Ÿใซ็ขบ่ชใ™ใ‚‹็ด ๆ™ดใ‚‰ใ—ใ„ๆ–นๆณ•ใงใ™ใ€‚ๅค‰ๆ›ดใ•ใ‚Œใฆใ„ใชใ„ใƒ•ใ‚กใ‚คใƒซใซ้–ข้€ฃใ™ใ‚‹ใƒ†ใ‚นใƒˆใฏๅฎŸ่กŒใ•ใ‚Œใพใ›ใ‚“ใ€‚ + +```bash +pip install pytest-picked +``` + +```bash +pytest --picked +``` + +ใ™ในใฆใฎใƒ†ใ‚นใƒˆใฏใ€ๅค‰ๆ›ดใ•ใ‚ŒใŸใŒใพใ ใ‚ณใƒŸใƒƒใƒˆใ•ใ‚Œใฆใ„ใชใ„ใƒ•ใ‚กใ‚คใƒซใจใƒ•ใ‚ฉใƒซใƒ€ใ‹ใ‚‰ๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + +### Automatically rerun failed tests on source modification + +[pytest-xdist](https://github.com/pytest-dev/pytest-xdist)ใฏใ€้žๅธธใซไพฟๅˆฉใชๆฉŸ่ƒฝใ‚’ๆไพ›ใ—ใฆใŠใ‚Šใ€ใ™ในใฆใฎๅคฑๆ•—ใ—ใŸใƒ†ใ‚นใƒˆใ‚’ๆคœๅ‡บใ—ใ€ใƒ•ใ‚กใ‚คใƒซใ‚’ไฟฎๆญฃใ™ใ‚‹้–“ใซใใ‚Œใ‚‰ใฎๅคฑๆ•—ใ—ใŸใƒ†ใ‚นใƒˆใ‚’้€ฃ็ถšใ—ใฆๅ†ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใใฎใŸใ‚ใ€ไฟฎๆญฃใ‚’่กŒใฃใŸๅพŒใซpytestใ‚’ๅ†่ตทๅ‹•ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ™ในใฆใฎใƒ†ใ‚นใƒˆใŒๅˆๆ ผใ™ใ‚‹ใพใง็นฐใ‚Š่ฟ”ใ•ใ‚Œใ€ใใฎๅพŒๅ†ๅบฆใƒ•ใƒซใƒฉใƒณใŒๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ + + +```bash +pip install pytest-xdist +``` + +ใƒขใƒผใƒ‰ใซๅ…ฅใ‚‹ใซใฏ๏ผš `pytest -f`ใพใŸใฏ`pytest --looponfail` + +ใƒ•ใ‚กใ‚คใƒซใฎๅค‰ๆ›ดใฏใ€`looponfailroots`ใƒซใƒผใƒˆใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใจใใฎๅ†…ๅฎนๅ…จไฝ“๏ผˆๅ†ๅธฐ็š„ใซ๏ผ‰ใ‚’่ฆ‹ใฆๆคœๅ‡บใ•ใ‚Œใพใ™ใ€‚ใ“ใฎๅ€คใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใŒๆฉŸ่ƒฝใ—ใชใ„ๅ ดๅˆใ€`setup.cfg`ใง่จญๅฎšใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ๅค‰ๆ›ดใ—ใฆใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆๅ†…ใงๅค‰ๆ›ดใงใใพใ™ใ€‚ + + +```ini +[tool:pytest] +looponfailroots = transformers tests +``` + +ใพใŸใฏ `pytest.ini`/`tox.ini` ใƒ•ใ‚กใ‚คใƒซ: + +```ini +[pytest] +looponfailroots = transformers tests +``` + +ใƒ•ใ‚กใ‚คใƒซใฎๅค‰ๆ›ดใ‚’ๆŽขใ™ใ“ใจใฏใ€iniใƒ•ใ‚กใ‚คใƒซใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ๅŸบๆบ–ใซใ—ใฆๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชๅ†…ใงใฎใฟ่กŒใ‚ใ‚Œใพใ™ใ€‚ + +[pytest-watch](https://github.com/joeyespo/pytest-watch) ใฏใ€ใ“ใฎๆฉŸ่ƒฝใฎไปฃๆ›ฟๅฎŸ่ฃ…ใงใ™ใ€‚ + +### Skip a test module + +็‰นๅฎšใฎใƒ†ใ‚นใƒˆใƒขใ‚ธใƒฅใƒผใƒซใ‚’้™คๅค–ใ—ใฆใ™ในใฆใฎใƒ†ใ‚นใƒˆใƒขใ‚ธใƒฅใƒผใƒซใ‚’ๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใ€ๅฎŸ่กŒใ™ใ‚‹ใƒ†ใ‚นใƒˆใฎๆ˜Ž็คบ็š„ใชใƒชใ‚นใƒˆใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€`test_modeling_*.py` ใƒ†ใ‚นใƒˆใ‚’้™คๅค–ใ—ใฆใ™ในใฆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + +```bash +pytest *ls -1 tests/*py | grep -v test_modeling* +``` + +### Clearing state + +CIใƒ“ใƒซใƒ‰ใŠใ‚ˆใณ้€Ÿๅบฆใซๅฏพใ™ใ‚‹้š”้›ขใŒ้‡่ฆใชๅ ดๅˆ๏ผˆใ‚ญใƒฃใƒƒใ‚ทใƒฅใซๅฏพใ—ใฆ๏ผ‰ใ€ใ‚ญใƒฃใƒƒใ‚ทใƒฅใ‚’ใ‚ฏใƒชใ‚ขใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```bash +pytest --cache-clear tests +``` + +### Running tests in parallel + +ๅ‰่ฟฐใฎใ‚ˆใ†ใซใ€`make test` ใฏ `pytest-xdist` ใƒ—ใƒฉใ‚ฐใ‚คใƒณใ‚’ไป‹ใ—ใฆใƒ†ใ‚นใƒˆใ‚’ไธฆๅˆ—ๅฎŸ่กŒใ—ใพใ™๏ผˆ`-n X` ๅผ•ๆ•ฐใ€ไพ‹: `-n 2` ใง2ใคใฎไธฆๅˆ—ใ‚ธใƒงใƒ–ใ‚’ๅฎŸ่กŒ๏ผ‰ใ€‚ + +`pytest-xdist` ใฎ `--dist=` ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ใƒ†ใ‚นใƒˆใŒใฉใฎใ‚ˆใ†ใซใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ•ใ‚Œใ‚‹ใ‹ใ‚’ๅˆถๅพกใงใใพใ™ใ€‚`--dist=loadfile` ใฏๅŒใ˜ใƒ•ใ‚กใ‚คใƒซใซใ‚ใ‚‹ใƒ†ใ‚นใƒˆใ‚’ๅŒใ˜ใƒ—ใƒญใ‚ปใ‚นใซ้…็ฝฎใ—ใพใ™ใ€‚ + +ใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒ้ †ๅบใŒ็•ฐใชใ‚Šไบˆๆธฌไธๅฏ่ƒฝใงใ‚ใ‚‹ใŸใ‚ใ€`pytest-xdist` ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใจๅคฑๆ•—ใŒ็™บ็”Ÿใ™ใ‚‹ๅ ดๅˆ๏ผˆใคใพใ‚Šใ€ใ„ใใคใ‹ใฎๆœชๆคœๅ‡บใฎ้€ฃๅ‹•ใƒ†ใ‚นใƒˆใŒใ‚ใ‚‹ๅ ดๅˆ๏ผ‰ใ€[pytest-replay](https://github.com/ESSS/pytest-replay) ใ‚’ไฝฟ็”จใ—ใฆใƒ†ใ‚นใƒˆใ‚’ๅŒใ˜้ †ๅบใงๅ†็”Ÿใ—ใ€ใใฎๅพŒใ€ๅคฑๆ•—ใ™ใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚นใ‚’ๆœ€ๅฐ้™ใซใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +### Test order and repetition + +ๆฝœๅœจ็š„ใช็›ธไบ’ไพๅญ˜ๆ€งใ‚„็Šถๆ…‹ใซ้–ข้€ฃใ™ใ‚‹ใƒใ‚ฐ๏ผˆใƒ†ใ‚ฃใ‚ขใƒ€ใ‚ฆใƒณ๏ผ‰ใ‚’ๆคœๅ‡บใ™ใ‚‹ใŸใ‚ใซใ€ใƒ†ใ‚นใƒˆใ‚’่ค‡ๆ•ฐๅ›žใ€้€ฃ็ถšใ—ใฆใ€ใƒฉใƒณใƒ€ใƒ ใซใ€ใพใŸใฏใ‚ปใƒƒใƒˆใง็นฐใ‚Š่ฟ”ใ™ใ“ใจใฏๆœ‰็”จใงใ™ใ€‚ใใ—ใฆใ€ๅ˜็ด”ใช่ค‡ๆ•ฐๅ›žใฎ็นฐใ‚Š่ฟ”ใ—ใฏใ€DLใฎใƒฉใƒณใƒ€ใƒ ๆ€งใซใ‚ˆใฃใฆๆ˜Žใ‚‰ใ‹ใซใชใ‚‹ใ„ใใคใ‹ใฎๅ•้กŒใ‚’ๆคœๅ‡บใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +#### Repeat tests + +- [pytest-flakefinder](https://github.com/dropbox/pytest-flakefinder): + +```bash +pip install pytest-flakefinder +``` + +ใใ—ใฆใ€ใ™ในใฆใฎใƒ†ใ‚นใƒˆใ‚’่ค‡ๆ•ฐๅ›žๅฎŸ่กŒใ—ใพใ™ (ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏ 50 ๅ›ž)ใ€‚ + +```bash +pytest --flake-finder --flake-runs=5 tests/test_failing_test.py +``` + + + +ใ“ใฎใƒ—ใƒฉใ‚ฐใ‚คใƒณใฏใ€`pytest-xdist` ใฎ `-n` ใƒ•ใƒฉใ‚ฐใงใฏๅ‹•ไฝœใ—ใพใ›ใ‚“ใ€‚ + + + + + + +ๅˆฅใฎใƒ—ใƒฉใ‚ฐใ‚คใƒณ `pytest-repeat` ใ‚‚ใ‚ใ‚Šใพใ™ใŒใ€ใ“ใ‚Œใฏ `unittest` ใงใฏๅ‹•ไฝœใ—ใพใ›ใ‚“ใ€‚ + + + +#### Run tests in a random order + +```bash +pip install pytest-random-order +``` + +้‡่ฆ: `pytest-random-order` ใŒๅญ˜ๅœจใ™ใ‚‹ใจใ€ใƒ†ใ‚นใƒˆใฏ่‡ชๅ‹•็š„ใซใƒฉใƒณใƒ€ใƒ ๅŒ–ใ•ใ‚Œใพใ™ใ€‚่จญๅฎšใฎๅค‰ๆ›ดใ‚„ๅค‰ๆ›ดใฏๅฟ…่ฆใ‚ใ‚Šใพใ›ใ‚“ใ€‚ +ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใ‚ชใƒ—ใ‚ทใƒงใƒณใฏๅฟ…้ ˆใงใ™ใ€‚ + +ๅ‰ใซ่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใ€็ตๅˆใ•ใ‚ŒใŸใƒ†ใ‚นใƒˆ (1 ใคใฎใƒ†ใ‚นใƒˆใฎ็Šถๆ…‹ใŒๅˆฅใฎใƒ†ใ‚นใƒˆใฎ็Šถๆ…‹ใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹) ใฎๆคœๅ‡บใŒๅฏ่ƒฝใซใชใ‚Šใพใ™ใ€‚ใ„ใค +`pytest-random-order` ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใจใ€ใใฎใ‚ปใƒƒใ‚ทใƒงใƒณใซไฝฟ็”จใ•ใ‚ŒใŸใƒฉใƒณใƒ€ใƒ  ใ‚ทใƒผใƒ‰ใŒๅ‡บๅŠ›ใ•ใ‚Œใพใ™ใ€‚ไพ‹: + + +```bash +pytest tests +[...] +Using --random-order-bucket=module +Using --random-order-seed=573663 +``` + +ใใฎใŸใ‚ใ€ๆŒ‡ๅฎšใ•ใ‚ŒใŸ็‰นๅฎšใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใŒๅคฑๆ•—ใ—ใŸๅ ดๅˆใ€ใใฎๆญฃ็ขบใชใ‚ทใƒผใƒ‰ใ‚’่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใใ‚Œใ‚’ๅ†็พใงใใพใ™ใ€‚ไพ‹: + +```bash +pytest --random-order-seed=573663 +[...] +Using --random-order-bucket=module +Using --random-order-seed=573663 +``` + +็‰นๅฎšใฎใƒ†ใ‚นใƒˆใฎใƒชใ‚นใƒˆใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใ€ใพใŸใฏใพใฃใŸใใƒชใ‚นใƒˆใ‚’ไฝฟ็”จใ—ใชใ„ๅ ดๅˆใ€ๅŒใ˜ใƒ†ใ‚นใƒˆใฎๆญฃ็ขบใช้ †ๅบใ‚’ๅ†็พใ—ใพใ™ใ€‚ใƒ†ใ‚นใƒˆใฎใƒชใ‚นใƒˆใ‚’ๆ‰‹ๅ‹•ใง็ตžใ‚Š่พผใฟๅง‹ใ‚ใ‚‹ใจใ€ใ‚ทใƒผใƒ‰ใซไพๅญ˜ใ›ใšใ€ใƒ†ใ‚นใƒˆใŒๅคฑๆ•—ใ—ใŸๆญฃ็ขบใช้ †ๅบใงๆ‰‹ๅ‹•ใงใƒชใ‚นใƒˆใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€`--random-order-bucket=none` ใ‚’ไฝฟ็”จใ—ใฆใƒฉใƒณใƒ€ใƒ ๅŒ–ใ‚’็„กๅŠนใซใ™ใ‚‹ใ‚ˆใ†pytestใซๆŒ‡็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + + +```bash +pytest --random-order-bucket=none tests/test_a.py tests/test_c.py tests/test_b.py +``` + +ใ™ในใฆใฎใƒ†ใ‚นใƒˆใฎใ‚ทใƒฃใƒƒใƒ•ใƒซใ‚’็„กๅŠนใซใ™ใ‚‹ใซใฏ: + +```bash +pytest --random-order-bucket=none +``` + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€`--random-order-bucket=module` ใŒๆš—้ป™็š„ใซ้ฉ็”จใ•ใ‚Œใ€ใƒขใ‚ธใƒฅใƒผใƒซใƒฌใƒ™ใƒซใงใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ทใƒฃใƒƒใƒ•ใƒซใ—ใพใ™ใ€‚ใพใŸใ€`class`ใ€`package`ใ€`global`ใ€ใŠใ‚ˆใณ`none` ใƒฌใƒ™ใƒซใงใ‚ทใƒฃใƒƒใƒ•ใƒซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใใฎ[ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://github.com/jbasko/pytest-random-order)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +ๅˆฅใฎใƒฉใƒณใƒ€ใƒ ๅŒ–ใฎไปฃๆ›ฟๆ‰‹ๆฎตใฏใ€[`pytest-randomly`](https://github.com/pytest-dev/pytest-randomly) ใงใ™ใ€‚ใ“ใฎใƒขใ‚ธใƒฅใƒผใƒซใฏ้žๅธธใซไผผใŸๆฉŸ่ƒฝ/ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใ‚’ๆŒใฃใฆใ„ใพใ™ใŒใ€`pytest-random-order` ใงๅˆฉ็”จๅฏ่ƒฝใชใƒใ‚ฑใƒƒใƒˆใƒขใƒผใƒ‰ใ‚’ๆŒใฃใฆใ„ใพใ›ใ‚“ใ€‚ใ‚คใƒณใ‚นใƒˆใƒผใƒซๅพŒใซ่‡ชๅ‹•็š„ใซๆœ‰ๅŠนใซใชใ‚‹ใจใ„ใ†ๅŒใ˜ๅ•้กŒใŒใ‚ใ‚Šใพใ™ใ€‚ + +### Look and feel variations + +#### pytest-sugar + +[pytest-sugar](https://github.com/Frozenball/pytest-sugar) ใฏใ€ๅค–่ฆณใจๆ“ไฝœๆ€งใ‚’ๅ‘ไธŠใ•ใ›ใ€ใƒ—ใƒญใ‚ฐใƒฌใ‚นใƒใƒผใ‚’่ฟฝๅŠ ใ—ใ€ๅณๅบงใซๅคฑๆ•—ใ—ใŸใƒ†ใ‚นใƒˆใจใ‚ขใ‚ตใƒผใ‚ทใƒงใƒณใ‚’่กจ็คบใ™ใ‚‹ใƒ—ใƒฉใ‚ฐใ‚คใƒณใงใ™ใ€‚ใ‚คใƒณใ‚นใƒˆใƒผใƒซๅพŒใซ่‡ชๅ‹•็š„ใซใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ–ๅŒ–ใ•ใ‚Œใพใ™ใ€‚ + +```bash +pip install pytest-sugar +``` + + +ใ“ใ‚Œใ‚’ไฝฟ็”จใ›ใšใซใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +pytest -p no:sugar +``` + +ใพใŸใฏใ‚ขใƒณใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใพใ™ใ€‚ + +#### Report each sub-test name and its progress + +`pytest` ใซใ‚ˆใ‚‹ๅ˜ไธ€ใพใŸใฏใ‚ฐใƒซใƒผใƒ—ใฎใƒ†ใ‚นใƒˆใฎๅ ดๅˆ (`pip install pytest-pspec` ใฎๅพŒ): + + +```bash +pytest --pspec tests/test_optimization.py +``` + + +#### Instantly shows failed tests + +[pytest-instafail](https://github.com/pytest-dev/pytest-instafail) ใงใฏใ€ๅคฑๆ•—ใจใ‚จใƒฉใƒผใŒๅณๅบงใซ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ +ใƒ†ใ‚นใƒˆใ‚ปใƒƒใ‚ทใƒงใƒณใŒ็ต‚ไบ†ใ™ใ‚‹ใพใงๅพ…ๆฉŸใ—ใพใ™ใ€‚ + +```bash +pip install pytest-instafail +``` + +```bash +pytest --instafail +``` + +### To GPU or not to GPU + +GPU ใŒๆœ‰ๅŠนใช่จญๅฎšใงใ€CPU ใฎใฟใƒขใƒผใƒ‰ใงใƒ†ใ‚นใƒˆใ™ใ‚‹ใซใฏใ€`CUDA_VISIBLE_DEVICES=""`ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES="" pytest tests/utils/test_logging.py +``` + + +ใพใŸใฏใ€่ค‡ๆ•ฐใฎ GPU ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€`pytest` ใงใฉใ‚Œใ‚’ไฝฟ็”จใ™ใ‚‹ใ‹ใ‚’ๆŒ‡ๅฎšใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ +2 ็•ช็›ฎใฎ GPU GPU `0` ใจ `1` ใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ + +```bash +CUDA_VISIBLE_DEVICES="1" pytest tests/utils/test_logging.py +``` + +ใ“ใ‚Œใฏใ€็•ฐใชใ‚‹GPUใง็•ฐใชใ‚‹ใ‚ฟใ‚นใ‚ฏใ‚’ๅฎŸ่กŒใ—ใŸใ„ๅ ดๅˆใซไพฟๅˆฉใงใ™ใ€‚ + +ไธ€้ƒจใฎใƒ†ใ‚นใƒˆใฏCPUใฎใฟใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ไป–ใฎใƒ†ใ‚นใƒˆใฏCPUใ€GPUใ€ใพใŸใฏTPUใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใพใŸๅˆฅใฎใƒ†ใ‚นใƒˆใฏ่ค‡ๆ•ฐใฎGPUใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฌกใฎใ‚นใ‚ญใƒƒใƒ—ใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใƒผใฏใ€ใƒ†ใ‚นใƒˆใฎCPU/GPU/TPUใซ้–ขใ™ใ‚‹่ฆไปถใ‚’่จญๅฎšใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™๏ผš + +- `require_torch` - ใ“ใฎใƒ†ใ‚นใƒˆใฏtorchใฎไธ‹ใงใฎใฟๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ +- `require_torch_gpu` - `require_torch` ใซๅŠ ใˆใฆใ€ๅฐ‘ใชใใจใ‚‚1ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ +- `require_torch_multi_gpu` - `require_torch` ใซๅŠ ใˆใฆใ€ๅฐ‘ใชใใจใ‚‚2ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ +- `require_torch_non_multi_gpu` - `require_torch` ใซๅŠ ใˆใฆใ€0ใพใŸใฏ1ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ +- `require_torch_up_to_2_gpus` - `require_torch` ใซๅŠ ใˆใฆใ€0ใ€1ใ€ใพใŸใฏ2ใคใฎGPUใŒๅฟ…่ฆใงใ™ใ€‚ +- `require_torch_xla` - `require_torch` ใซๅŠ ใˆใฆใ€ๅฐ‘ใชใใจใ‚‚1ใคใฎTPUใŒๅฟ…่ฆใงใ™ใ€‚ + +ไปฅไธ‹ใฎ่กจใซGPUใฎ่ฆไปถใ‚’็คบใ—ใพใ™๏ผš + +| n gpus | decorator | +|--------+--------------------------------| +| `>= 0` | `@require_torch` | +| `>= 1` | `@require_torch_gpu` | +| `>= 2` | `@require_torch_multi_gpu` | +| `< 2` | `@require_torch_non_multi_gpu` | +| `< 3` | `@require_torch_up_to_2_gpus` | + + +ใŸใจใˆใฐใ€ไฝฟ็”จๅฏ่ƒฝใช GPU ใŒ 2 ใคไปฅไธŠใ‚ใ‚Šใ€pytorch ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใซใฎใฟๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใƒ†ใ‚นใƒˆใ‚’ๆฌกใซ็คบใ—ใพใ™ใ€‚ + + +```python no-style +@require_torch_multi_gpu +def test_example_with_multi_gpu(): +``` + +ใ“ใ‚Œใ‚‰ใฎใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใฏ็ฉใฟ้‡ใญใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใŸใจใˆใฐใ€ใƒ†ใ‚นใƒˆใŒ้…ใใ€pytorch ใงๅฐ‘ใชใใจใ‚‚ 1 ใคใฎ GPU ใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ +่จญๅฎšๆ–นๆณ•: + +```python no-style +@require_torch_gpu +@slow +def test_example_slow_on_gpu(): +``` + +`@parametrized` ใฎใ‚ˆใ†ใชไธ€้ƒจใฎใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใฏใƒ†ใ‚นใƒˆๅใ‚’ๆ›ธใๆ›ใˆใ‚‹ใŸใ‚ใ€`@require_*` ใ‚นใ‚ญใƒƒใƒ— ใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใ‚’ใƒชใ‚นใƒˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๆœ€ๅพŒใซใใ‚Œใ‚‰ใŒๆญฃใ—ใๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ๆญฃใ—ใ„ไฝฟ็”จไพ‹ใฏๆฌกใฎใจใŠใ‚Šใงใ™ + +```python no-style +@parameterized.expand(...) +@require_torch_multi_gpu +def test_integration_foo(): +``` + +ใ“ใฎ้ †ๅบใฎๅ•้กŒใฏ `@pytest.mark.parametrize` ใซใฏๅญ˜ๅœจใ—ใพใ›ใ‚“ใ€‚ๆœ€ๅˆใพใŸใฏๆœ€ๅพŒใซ้…็ฝฎใ—ใฆใ‚‚ใ€ใใ‚Œใงใ‚‚ๅ•้กŒใฏ่งฃๆฑบใ•ใ‚Œใพใ™ใ€‚ +ไป•ไบ‹ใ€‚ใŸใ ใ—ใ€ใใ‚Œใฏ้žๅ˜ไฝ“ใƒ†ใ‚นใƒˆใงใฎใฟๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +ๅ†…้ƒจใƒ†ใ‚นใƒˆ: + +- ๅˆฉ็”จๅฏ่ƒฝใช GPU ใฎๆ•ฐ: + +```python +from transformers.testing_utils import get_gpu_count + +n_gpu = get_gpu_count() # works with torch and tf +``` + +### Testing with a specific PyTorch backend or device + +็‰นๅฎšใฎtorchใƒ‡ใƒใ‚คใ‚นใงใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€`TRANSFORMERS_TEST_DEVICE="$device"` ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚ใ“ใ“ใง `$device` ใฏๅฏพ่ฑกใฎใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใงใ™ใ€‚ไพ‹ใˆใฐใ€CPUใงใƒ†ใ‚นใƒˆใ™ใ‚‹ใซใฏไปฅไธ‹ใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + +```bash +TRANSFORMERS_TEST_DEVICE="cpu" pytest tests/utils/test_logging.py +``` + +ใ“ใฎๅค‰ๆ•ฐใฏใ€`mps`ใชใฉใฎใ‚ซใ‚นใ‚ฟใƒ ใพใŸใฏใ‚ใพใ‚Šไธ€่ˆฌ็š„ใงใฏใชใ„ PyTorch ใƒใƒƒใ‚ฏใ‚จใƒณใƒ‰ใ‚’ใƒ†ใ‚นใƒˆใ™ใ‚‹ใฎใซๅฝน็ซ‹ใกใพใ™ใ€‚ใพใŸใ€็‰นๅฎšใฎ GPU ใ‚’ใ‚ฟใƒผใ‚ฒใƒƒใƒˆใซใ—ใŸใ‚Šใ€CPU ๅฐ‚็”จใƒขใƒผใƒ‰ใงใƒ†ใ‚นใƒˆใ—ใŸใ‚Šใ™ใ‚‹ใ“ใจใงใ€`CUDA_VISIBLE_DEVICES`ใจๅŒใ˜ๅŠนๆžœใ‚’้”ๆˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +็‰นๅฎšใฎใƒ‡ใƒใ‚คใ‚นใงใฏใ€ๅˆใ‚ใฆใ€Œtorchใ€ใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใŸๅพŒใ€่ฟฝๅŠ ใฎใ‚คใƒณใƒใƒผใƒˆใŒๅฟ…่ฆใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€็’ฐๅขƒๅค‰ๆ•ฐ `TRANSFORMERS_TEST_BACKEND` ใ‚’ไฝฟ็”จใ—ใฆๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +```bash +TRANSFORMERS_TEST_BACKEND="torch_npu" pytest tests/utils/test_logging.py +``` + +### Distributed training + +`pytest` ใฏ็›ดๆŽฅ็š„ใซๅˆ†ๆ•ฃใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใ€‚่ฉฆใฟใ‚‹ใจใ€ใ‚ตใƒ–ใƒ—ใƒญใ‚ปใ‚นใฏๆญฃใ—ใ„ๅ‡ฆ็†ใ‚’่กŒใ‚ใšใ€่‡ชๅˆ†่‡ช่บซใŒ `pytest` ใงใ‚ใ‚‹ใจๆ€ใ„่พผใ‚“ใงใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใ‚’ใƒซใƒผใƒ—ใงๅฎŸ่กŒใ—็ถšใ‘ใพใ™ใ€‚ใŸใ ใ—ใ€้€šๅธธใฎใƒ—ใƒญใ‚ปใ‚นใ‚’็”Ÿๆˆใ—ใ€ใใ‚Œใ‹ใ‚‰่ค‡ๆ•ฐใฎใƒฏใƒผใ‚ซใƒผใ‚’็”Ÿๆˆใ—ใ€IOใƒ‘ใ‚คใƒ—ใ‚’็ฎก็†ใ™ใ‚‹ใƒ—ใƒญใ‚ปใ‚นใ‚’็”Ÿๆˆใ™ใ‚ŒใฐๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚’ไฝฟ็”จใ™ใ‚‹ใ„ใใคใ‹ใฎใƒ†ใ‚นใƒˆใŒใ‚ใ‚Šใพใ™๏ผš + +- [test_trainer_distributed.py](https://github.com/huggingface/transformers/tree/main/tests/trainer/test_trainer_distributed.py) +- [test_deepspeed.py](https://github.com/huggingface/transformers/tree/main/tests/deepspeed/test_deepspeed.py) + +ๅฎŸ่กŒใƒใ‚คใƒณใƒˆใซใ™ใใซ็งปๅ‹•ใ™ใ‚‹ใซใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆๅ†…ใง `execute_subprocess_async` ๅ‘ผใณๅ‡บใ—ใ‚’ๆคœ็ดขใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๅฐ‘ใชใใจใ‚‚2ใคใฎGPUใŒๅฟ…่ฆใงใ™๏ผš + +```bash +CUDA_VISIBLE_DEVICES=0,1 RUN_SLOW=1 pytest -sv tests/test_trainer_distributed.py +``` + +### Output capture + +ใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒไธญใซใ€`stdout` ใŠใ‚ˆใณ `stderr` ใซ้€ไฟกใ•ใ‚ŒใŸๅ‡บๅŠ›ใฏใ‚ญใƒฃใƒ—ใƒใƒฃใ•ใ‚Œใพใ™ใ€‚ใƒ†ใ‚นใƒˆใพใŸใฏใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใƒกใ‚ฝใƒƒใƒ‰ใŒๅคฑๆ•—ใ—ใŸๅ ดๅˆใ€้€šๅธธใ€ใใ‚Œใซๅฏพๅฟœใ™ใ‚‹ใ‚ญใƒฃใƒ—ใƒใƒฃใ•ใ‚ŒใŸๅ‡บๅŠ›ใŒๅคฑๆ•—ใฎใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใจๅ…ฑใซ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +ๅ‡บๅŠ›ใฎใ‚ญใƒฃใƒ—ใƒใƒฃใ‚’็„กๅŠนใซใ—ใ€`stdout` ใจ `stderr` ใ‚’้€šๅธธ้€šใ‚Šใซๅ–ๅพ—ใ™ใ‚‹ใซใฏใ€`-s` ใพใŸใฏ `--capture=no` ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„๏ผš + +ใ“ใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏๅฐ‘ใชใใจใ‚‚2ใคใฎGPUใŒๅฟ…่ฆใงใ™๏ผš + +```bash +pytest -s tests/utils/test_logging.py +``` + +ใƒ†ใ‚นใƒˆ็ตๆžœใ‚’ JUnit ๅฝขๅผใฎๅ‡บๅŠ›ใซ้€ไฟกใ™ใ‚‹ใซใฏ: + +```bash +py.test tests --junitxml=result.xml +``` + +### Color control + +่‰ฒใ‚’ๆŒใŸใชใ„ใ‚ˆใ†ใซใ™ใ‚‹๏ผˆไพ‹๏ผš้ป„่‰ฒใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’็™ฝใ„่ƒŒๆ™ฏใซ่กจ็คบใ™ใ‚‹ใจ่ชญใฟใซใใ„ใงใ™๏ผ‰๏ผš + + +```bash +pytest --color=no tests/utils/test_logging.py +``` + +### Sending test report to online pastebin service + +ใƒ†ใ‚นใƒˆๅคฑๆ•—ใ”ใจใซ URL ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ + + +```bash +pytest --pastebin=failed tests/utils/test_logging.py +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ†ใ‚นใƒˆๅฎŸ่กŒๆƒ…ๅ ฑใŒใƒชใƒขใƒผใƒˆใฎPasteใ‚ตใƒผใƒ“ใ‚นใซ้€ไฟกใ•ใ‚Œใ€ๅ„ใ‚จใƒฉใƒผใซๅฏพใ—ใฆURLใŒๆไพ›ใ•ใ‚Œใพใ™ใ€‚้€šๅธธ้€šใ‚Šใƒ†ใ‚นใƒˆใ‚’้ธๆŠžใ™ใ‚‹ใ‹ใ€ใŸใจใˆใฐ็‰นๅฎšใฎใ‚จใƒฉใƒผใฎใฟใ‚’้€ไฟกใ—ใŸใ„ๅ ดๅˆใฏ `-x` ใ‚’่ฟฝๅŠ ใงๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +ใƒ†ใ‚นใƒˆใ‚ปใƒƒใ‚ทใƒงใƒณๅ…จไฝ“ใฎใƒญใ‚ฐใซๅฏพใ™ใ‚‹URLใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•๏ผš + + +```bash +pytest --pastebin=all tests/utils/test_logging.py +``` + +## Writing tests + +๐Ÿค— transformersใฎใƒ†ใ‚นใƒˆใฏ `unittest` ใ‚’ๅŸบใซใ—ใฆใ„ใพใ™ใŒใ€ `pytest` ใงๅฎŸ่กŒใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใ€ไธกๆ–นใฎใ‚ทใ‚นใƒ†ใƒ ใฎๆฉŸ่ƒฝใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +[ใ“ใกใ‚‰](https://docs.pytest.org/en/stable/unittest.html)ใงใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ๆฉŸ่ƒฝใ‚’่ชญใ‚€ใ“ใจใŒใงใใพใ™ใŒใ€้‡่ฆใชใ“ใจใฏใ€ใปใจใ‚“ใฉใฎ `pytest` ใฎใƒ•ใ‚ฃใ‚ฏใ‚นใƒใƒฃใŒๅ‹•ไฝœใ—ใชใ„ใ“ใจใงใ™ใ€‚ใƒ‘ใƒฉใƒกใƒผใ‚ฟๅŒ–ใ‚‚ๅŒๆง˜ใงใ™ใŒใ€ไผผใŸใ‚ˆใ†ใชๆ–นๆณ•ใงๅ‹•ไฝœใ™ใ‚‹ `parameterized` ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™ใ€‚ + +### Parametrization + +ๅŒใ˜ใƒ†ใ‚นใƒˆใ‚’็•ฐใชใ‚‹ๅผ•ๆ•ฐใง่ค‡ๆ•ฐๅ›žๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใƒ†ใ‚นใƒˆๅ†…้ƒจใ‹ใ‚‰่กŒใ†ใ“ใจใ‚‚ใงใใพใ™ใŒใ€ใใฎๅ ดๅˆใ€ใใฎใƒ†ใ‚นใƒˆใ‚’ๅ˜ไธ€ใฎๅผ•ๆ•ฐใ‚ปใƒƒใƒˆใงๅฎŸ่กŒใ™ใ‚‹ๆ–นๆณ•ใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + + +```python +# test_this1.py +import unittest +from parameterized import parameterized + + +class TestMathUnitTest(unittest.TestCase): + @parameterized.expand( + [ + ("negative", -1.5, -2.0), + ("integer", 1, 1.0), + ("large fraction", 1.6, 1), + ] + ) + def test_floor(self, name, input, expected): + assert_equal(math.floor(input), expected) +``` + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใ“ใฎใƒ†ใ‚นใƒˆใฏ3ๅ›žๅฎŸ่กŒใ•ใ‚Œใ€ใใ‚Œใžใ‚ŒใฎๅฎŸ่กŒใง `test_floor` ใฎๆœ€ๅพŒใฎ3ใคใฎๅผ•ๆ•ฐใŒใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒชใ‚นใƒˆใฎๅฏพๅฟœใ™ใ‚‹ๅผ•ๆ•ฐใซๅ‰ฒใ‚Šๅฝ“ใฆใ‚‰ใ‚Œใพใ™ใ€‚ + +ใใ—ใฆใ€`negative` ใจ `integer` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎใ‚ปใƒƒใƒˆใฎใฟใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™: + +```bash +pytest -k "negative and integer" tests/test_mytest.py +``` + +ใพใŸใฏใ€`Negative`ใฎใ‚ตใƒ–ใƒ†ใ‚นใƒˆใ‚’้™คใใ™ในใฆใฎๅ ดๅˆใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + +```bash +pytest -k "not negative" tests/test_mytest.py +``` + +`-k` ใƒ•ใ‚ฃใƒซใ‚ฟใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใซๅŠ ใˆใฆใ€ๅ„ใ‚ตใƒ–ใƒ†ใ‚นใƒˆใฎๆญฃ็ขบใชๅๅ‰ใ‚’่ชฟในใ€ใใฎๆญฃ็ขบใชๅๅ‰ใ‚’ไฝฟ็”จใ—ใฆไปปๆ„ใฎใ‚ตใƒ–ใƒ†ใ‚นใƒˆใพใŸใฏใ™ในใฆใฎใ‚ตใƒ–ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```bash +pytest test_this1.py --collect-only -q +``` + +ใ™ใ‚‹ใจๆฌกใฎใ‚‚ใฎใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™: + +```bash +test_this1.py::TestMathUnitTest::test_floor_0_negative +test_this1.py::TestMathUnitTest::test_floor_1_integer +test_this1.py::TestMathUnitTest::test_floor_2_large_fraction +``` + + +ใ—ใŸใŒใฃใฆใ€2 ใคใฎ็‰นๅฎšใฎใ‚ตใƒ–ใƒ†ใ‚นใƒˆใฎใฟใ‚’ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```bash +pytest test_this1.py::TestMathUnitTest::test_floor_0_negative test_this1.py::TestMathUnitTest::test_floor_1_integer +``` + +`transformers`ใฎ้–‹็™บ่€…ไพๅญ˜้–ขไฟ‚ใซใ™ใงใซๅซใพใ‚Œใฆใ„ใ‚‹ใƒขใ‚ธใƒฅใƒผใƒซ[parameterized](https://pypi.org/project/parameterized/) ใฏใ€`unittests` ใจ `pytest` ใƒ†ใ‚นใƒˆใฎไธกๆ–นใงๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +ใŸใ ใ—ใ€ใƒ†ใ‚นใƒˆใŒ `unittest` ใงใชใ„ๅ ดๅˆใ€`pytest.mark.parametrize` ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผˆใพใŸใฏๆ—ขๅญ˜ใฎใƒ†ใ‚นใƒˆใฎใ„ใใคใ‹ใงใ€ไธปใซ `examples` ใฎไธ‹ใงไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใฎใ‚’่ฆ‹ใ‚‹ใ“ใจใŒใงใใพใ™๏ผ‰ใ€‚ + +ๆฌกใซใ€ๅŒใ˜ไพ‹ใ‚’็คบใ—ใพใ™ใŒใ€ไปŠๅบฆใฏ `pytest` ใฎ `parametrize` ใƒžใƒผใ‚ซใƒผใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™๏ผš + + +```python +# test_this2.py +import pytest + + +@pytest.mark.parametrize( + "name, input, expected", + [ + ("negative", -1.5, -2.0), + ("integer", 1, 1.0), + ("large fraction", 1.6, 1), + ], +) +def test_floor(name, input, expected): + assert_equal(math.floor(input), expected) +``` + +`parameterized` ใจๅŒๆง˜ใซใ€`pytest.mark.parametrize` ใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€`-k` ใƒ•ใ‚ฃใƒซใ‚ฟใŒๅฝน็ซ‹ใŸใชใ„ๅ ดๅˆใงใ‚‚ใ€ใ‚ตใƒ–ใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒใ‚’็ดฐใ‹ใๅˆถๅพกใงใใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟๅŒ–้–ขๆ•ฐใฏใ‚ตใƒ–ใƒ†ใ‚นใƒˆใฎๅๅ‰ใ‚’ใ‚ใšใ‹ใซ็•ฐใชใ‚‹ใ‚‚ใฎใซใ—ใพใ™ใ€‚ไปฅไธ‹ใซใใฎไพ‹ใ‚’็คบใ—ใพใ™๏ผš + + +```bash +pytest test_this2.py --collect-only -q +``` + +ใ™ใ‚‹ใจๆฌกใฎใ‚‚ใฎใŒใƒชใ‚นใƒˆใ•ใ‚Œใพใ™: + +```bash +test_this2.py::test_floor[integer-1-1.0] +test_this2.py::test_floor[negative--1.5--2.0] +test_this2.py::test_floor[large fraction-1.6-1] +``` + +ใ“ใ‚Œใงใ€็‰นๅฎšใฎใƒ†ใ‚นใƒˆใฎใฟใ‚’ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚ + +```bash +pytest test_this2.py::test_floor[negative--1.5--2.0] test_this2.py::test_floor[integer-1-1.0] +``` + +ๅ‰ใฎไพ‹ใจๅŒๆง˜ใซใ€‚ + +### Files and directories + +ใƒ†ใ‚นใƒˆใฎไธญใงใ€็พๅœจใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ใฎ็›ธๅฏพไฝ็ฝฎใ‚’็Ÿฅใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใŒใ‚ˆใใ‚ใ‚Šใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ“ใ‚Œใฏ็ฐกๅ˜ใชใ“ใจใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใชใœใชใ‚‰ใ€ใƒ†ใ‚นใƒˆใฏ่ค‡ๆ•ฐใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‹ใ‚‰ๅ‘ผใณๅ‡บใ•ใ‚Œใ‚‹ใ‹ใ€็•ฐใชใ‚‹ๆทฑใ•ใฎใ‚ตใƒ–ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซๅญ˜ๅœจใ™ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใ‹ใ‚‰ใงใ™ใ€‚`transformers.test_utils.TestCasePlus` ใจใ„ใ†ใƒ˜ใƒซใƒ‘ใƒผใ‚ฏใƒฉใ‚นใฏใ€ใ™ในใฆใฎๅŸบๆœฌใƒ‘ใ‚นใ‚’ๆ•ด็†ใ—ใ€็ฐกๅ˜ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใงใ€ใ“ใฎๅ•้กŒใ‚’่งฃๆฑบใ—ใพใ™ใ€‚ + +- `pathlib` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆ๏ผˆใ™ในใฆๅฎŒๅ…จใซ่งฃๆฑบใ•ใ‚ŒใŸใ‚‚ใฎ๏ผ‰๏ผš + + - `test_file_path` - ็พๅœจใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚นใ€ใคใพใ‚Š `__file__` + - `test_file_dir` - ็พๅœจใฎใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใ‚’ๅซใ‚€ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช + - `tests_dir` - `tests` ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช + - `examples_dir` - `examples` ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช + - `repo_root_dir` - ใƒชใƒใ‚ธใƒˆใƒชใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช + - `src_dir` - `transformers` ใ‚ตใƒ–ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒๅญ˜ๅœจใ™ใ‚‹ๅ ดๆ‰€ + +- ใƒ‘ใ‚นใฎๆ–‡ๅญ—ๅˆ—่กจ็พโ€•โ€•ไธŠ่จ˜ใจๅŒใ˜ใงใ™ใŒใ€ใ“ใ‚Œใ‚‰ใฏ `pathlib` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใงใฏใชใๆ–‡ๅญ—ๅˆ—ใจใ—ใฆใƒ‘ใ‚นใ‚’่ฟ”ใ—ใพใ™๏ผš + + - `test_file_path_str` + - `test_file_dir_str` + - `tests_dir_str` + - `examples_dir_str` + - `repo_root_dir_str` + - `src_dir_str` + +ใ“ใ‚Œใ‚‰ใ‚’ไฝฟ็”จใ—ๅง‹ใ‚ใ‚‹ใซใฏใ€ใƒ†ใ‚นใƒˆใŒ `transformers.test_utils.TestCasePlus` ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใซๅญ˜ๅœจใ™ใ‚‹ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใ ใ‘ใงใ™ใ€‚ไพ‹๏ผš + +```python +from transformers.testing_utils import TestCasePlus + + +class PathExampleTest(TestCasePlus): + def test_something_involving_local_locations(self): + data_dir = self.tests_dir / "fixtures/tests_samples/wmt_en_ro" +``` + +ใ‚‚ใ—ใ€`pathlib` ใ‚’ไป‹ใ—ใฆใƒ‘ใ‚นใ‚’ๆ“ไฝœใ™ใ‚‹ๅฟ…่ฆใŒใชใ„ๅ ดๅˆใ€ใพใŸใฏๅ˜ใซๆ–‡ๅญ—ๅˆ—ใจใ—ใฆใƒ‘ใ‚นใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€`pathlib` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใซ `str()` ใ‚’ๅ‘ผใณๅ‡บใ™ใ‹ใ€`_str` ใง็ต‚ใ‚ใ‚‹ใ‚ขใ‚ฏใ‚ปใ‚ตใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ไพ‹๏ผš + +```python +from transformers.testing_utils import TestCasePlus + + +class PathExampleTest(TestCasePlus): + def test_something_involving_stringified_locations(self): + examples_dir = self.examples_dir_str +``` + +### Temporary files and directories + +ไธ€ๆ„ใฎไธ€ๆ™‚ใƒ•ใ‚กใ‚คใƒซใจใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฎไฝฟ็”จใฏใ€ไธฆๅˆ—ใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒใซใฏๆฌ ใ‹ใ›ใพใ›ใ‚“ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒ†ใ‚นใƒˆใŒใŠไบ’ใ„ใฎใƒ‡ใƒผใ‚ฟใ‚’ไธŠๆ›ธใใ—ใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใพใŸใ€ใ“ใ‚Œใ‚‰ใ‚’ไฝœๆˆใ—ใŸๅ„ใƒ†ใ‚นใƒˆใฎ็ต‚ไบ†ๆ™‚ใซไธ€ๆ™‚ใƒ•ใ‚กใ‚คใƒซใจใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒๅ‰Š้™คใ•ใ‚Œใ‚‹ใ“ใจใ‚’ๆœ›ใฟใพใ™ใ€‚ใใฎใŸใ‚ใ€ใ“ใ‚Œใ‚‰ใฎใƒ‹ใƒผใ‚บใ‚’ๆบ€ใŸใ™ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใงใ‚ใ‚‹ `tempfile` ใฎใ‚ˆใ†ใชใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎไฝฟ็”จใฏ้‡่ฆใงใ™ใ€‚ + +ใ—ใ‹ใ—ใ€ใƒ†ใ‚นใƒˆใฎใƒ‡ใƒใƒƒใ‚ฐๆ™‚ใซใฏใ€ไธ€ๆ™‚ใƒ•ใ‚กใ‚คใƒซใ‚„ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซไฝ•ใŒๆ ผ็ดใ•ใ‚Œใฆใ„ใ‚‹ใ‹ใ‚’็ขบ่ชใงใใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใƒ†ใ‚นใƒˆใ‚’ๅ†ๅฎŸ่กŒใ™ใ‚‹ใŸใณใซใƒฉใƒณใƒ€ใƒ ใซๅค‰ๆ›ดใ•ใ‚Œใชใ„ใใฎๆญฃ็ขบใชใƒ‘ใ‚นใ‚’็Ÿฅใ‚ŠใŸใ„ใจๆ€ใ„ใพใ™ใ€‚ + +`transformers.test_utils.TestCasePlus` ใจใ„ใ†ใƒ˜ใƒซใƒ‘ใƒผใ‚ฏใƒฉใ‚นใฏใ€ใ“ใฎใ‚ˆใ†ใช็›ฎ็š„ใซๆœ€้ฉใงใ™ใ€‚ใ“ใ‚Œใฏ `unittest.TestCase` ใฎใ‚ตใƒ–ใ‚ฏใƒฉใ‚นใงใ‚ใ‚‹ใŸใ‚ใ€ใƒ†ใ‚นใƒˆใƒขใ‚ธใƒฅใƒผใƒซใง็ฐกๅ˜ใซ็ถ™ๆ‰ฟใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +ไปฅไธ‹ใฏใใฎไฝฟ็”จไพ‹ใงใ™๏ผš + + +```python +from transformers.testing_utils import TestCasePlus + + +class ExamplesTests(TestCasePlus): + def test_whatever(self): + tmp_dir = self.get_auto_remove_tmp_dir() +``` + +ใ“ใฎใ‚ณใƒผใƒ‰ใฏใƒฆใƒ‹ใƒผใ‚ฏใชไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ไฝœๆˆใ—ใ€`tmp_dir` ใ‚’ใใฎๅ ดๆ‰€ใซ่จญๅฎšใ—ใพใ™ใ€‚ + +- ใƒฆใƒ‹ใƒผใ‚ฏใชไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```python +def test_whatever(self): + tmp_dir = self.get_auto_remove_tmp_dir() +``` + +`tmp_dir` ใซใฏใ€ไฝœๆˆใ•ใ‚ŒใŸไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใธใฎใƒ‘ใ‚นใŒๅซใพใ‚Œใพใ™ใ€‚ๆœŸ้–“็ต‚ไบ†ๅพŒใฏ่‡ชๅ‹•็š„ใซๅ‰Š้™คใ•ใ‚Œใพใ™ +ใƒ†ใ‚นใƒˆใ€‚ + +- ไปปๆ„ใฎไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’ไฝœๆˆใ—ใ€ใƒ†ใ‚นใƒˆใฎ้–‹ๅง‹ๅ‰ใซใใ‚ŒใŒ็ฉบใงใ‚ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€ใƒ†ใ‚นใƒˆๅพŒใซใฏ็ฉบใซใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +```python +def test_whatever(self): + tmp_dir = self.get_auto_remove_tmp_dir("./xxx") +``` + +ใ“ใ‚Œใฏใ€็‰นๅฎšใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’็›ฃ่ฆ–ใ—ใ€ๅ‰ใฎใƒ†ใ‚นใƒˆใŒใใ“ใซใƒ‡ใƒผใ‚ฟใ‚’ๆฎ‹ใ•ใชใ„ใ“ใจใ‚’็ขบ่ชใ—ใŸใ„ๅ ดๅˆใซใ€ใƒ‡ใƒใƒƒใ‚ฐใซๅฝน็ซ‹ใกใพใ™ใ€‚ + +- `before` ใจ `after` ๅผ•ๆ•ฐใ‚’็›ดๆŽฅใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใงใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅ‹•ไฝœใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ„ใšใ‚Œใ‹ใฎๅ‹•ไฝœใซๅฐŽใใพใ™๏ผš + + - `before=True`๏ผšใƒ†ใ‚นใƒˆใฎ้–‹ๅง‹ๆ™‚ใซๅธธใซไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒใ‚ฏใƒชใ‚ขใ•ใ‚Œใพใ™ใ€‚ + - `before=False`๏ผšไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒๆ—ขใซๅญ˜ๅœจใ™ใ‚‹ๅ ดๅˆใ€ๆ—ขๅญ˜ใฎใƒ•ใ‚กใ‚คใƒซใฏใใฎใพใพใซใชใ‚Šใพใ™ใ€‚ + - `after=True`๏ผšใƒ†ใ‚นใƒˆใฎ็ต‚ไบ†ๆ™‚ใซๅธธใซไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใŒๅ‰Š้™คใ•ใ‚Œใพใ™ใ€‚ + - `after=False`๏ผšใƒ†ใ‚นใƒˆใฎ็ต‚ไบ†ๆ™‚ใซๅธธใซไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฏใใฎใพใพใซใชใ‚Šใพใ™ใ€‚ + + + +`rm -r`ใฎ็›ธๅฝ“ใ‚’ๅฎ‰ๅ…จใซๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใซใ€ๆ˜Ž็คบ็š„ใช `tmp_dir` ใŒไฝฟ็”จใ•ใ‚Œใ‚‹ๅ ดๅˆใ€ใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใƒชใƒใ‚ธใƒˆใƒชใฎใƒใ‚งใƒƒใ‚ฏใ‚ขใ‚ฆใƒˆใฎใ‚ตใƒ–ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฎใฟใŒ่จฑๅฏใ•ใ‚Œใพใ™ใ€‚่ชคใฃใฆ `/tmp` ใชใฉใฎใƒ•ใ‚กใ‚คใƒซใ‚ทใ‚นใƒ†ใƒ ใฎ้‡่ฆใช้ƒจๅˆ†ใŒๅ‰Š้™คใ•ใ‚Œใชใ„ใ‚ˆใ†ใซใ€ๅธธใซ `./` ใ‹ใ‚‰ๅง‹ใพใ‚‹ใƒ‘ใ‚นใ‚’ๆธกใ—ใฆใใ ใ•ใ„ใ€‚ + + + + + +ๅ„ใƒ†ใ‚นใƒˆใฏ่ค‡ๆ•ฐใฎไธ€ๆ™‚ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚’็™ป้Œฒใงใใ€่ฆๆฑ‚ใŒใชใ„้™ใ‚Šใ™ในใฆ่‡ชๅ‹•ใงๅ‰Š้™คใ•ใ‚Œใพใ™ใ€‚ + + + +### Temporary sys.path override + +ๅˆฅใฎใƒ†ใ‚นใƒˆใ‹ใ‚‰ใ‚คใƒณใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใซไธ€ๆ™‚็š„ใซ `sys.path` ใ‚’ใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใ€`ExtendSysPath` ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒžใƒใƒผใ‚ธใƒฃใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ไพ‹๏ผš + + +```python +import os +from transformers.testing_utils import ExtendSysPath + +bindir = os.path.abspath(os.path.dirname(__file__)) +with ExtendSysPath(f"{bindir}/.."): + from test_trainer import TrainerIntegrationCommon # noqa +``` + +### Skipping tests + +ใ“ใ‚Œใฏใ€ใƒใ‚ฐใŒ่ฆ‹ใคใ‹ใ‚Šใ€ๆ–ฐใ—ใ„ใƒ†ใ‚นใƒˆใŒไฝœๆˆใ•ใ‚ŒใŸๅ ดๅˆใงใ‚ใฃใฆใ‚‚ใ€ใƒใ‚ฐใŒใพใ ไฟฎๆญฃใ•ใ‚Œใฆใ„ใชใ„ๅ ดๅˆใซๅฝน็ซ‹ใกใพใ™ใ€‚ใƒกใ‚คใƒณใƒชใƒใ‚ธใƒˆใƒชใซใ‚ณใƒŸใƒƒใƒˆใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€`make test` ใฎๅฎŸ่กŒไธญใซใใ‚Œใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใƒกใ‚ฝใƒƒใƒ‰๏ผš + +- **skip** ใฏใ€ใƒ†ใ‚นใƒˆใŒ็‰นๅฎšใฎๆกไปถใŒๆบ€ใŸใ•ใ‚ŒใŸๅ ดๅˆใซใฎใฟใƒ‘ใ‚นใ™ใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใŠใ‚Šใ€ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใฏ pytest ใŒใƒ†ใ‚นใƒˆใฎๅฎŸ่กŒใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ—ใพใ™ใ€‚ไธ€่ˆฌ็š„ใชไพ‹ใฏใ€Windowsๅฐ‚็”จใฎใƒ†ใ‚นใƒˆใ‚’้žWindowsใƒ—ใƒฉใƒƒใƒˆใƒ•ใ‚ฉใƒผใƒ ใงใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๅ ดๅˆใ€ใพใŸใฏ็พๅœจๅˆฉ็”จใงใใชใ„ๅค–้ƒจใƒชใ‚ฝใƒผใ‚นใซไพๅญ˜ใ™ใ‚‹ใƒ†ใ‚นใƒˆใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๅ ดๅˆใงใ™๏ผˆไพ‹: ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใŒๅˆฉ็”จใงใใชใ„ๅ ดๅˆ๏ผ‰ใ€‚ + +- **xfail** ใฏใ€ไฝ•ใ‚‰ใ‹ใฎ็†็”ฑใงใƒ†ใ‚นใƒˆใŒๅคฑๆ•—ใ™ใ‚‹ใ“ใจใ‚’ๆœŸๅพ…ใ—ใฆใ„ใพใ™ใ€‚ไธ€่ˆฌ็š„ใชไพ‹ใฏใ€ใพใ ๅฎŸ่ฃ…ใ•ใ‚Œใฆใ„ใชใ„ๆฉŸ่ƒฝใฎใƒ†ใ‚นใƒˆใ‚„ใ€ใพใ ไฟฎๆญฃใ•ใ‚Œใฆใ„ใชใ„ใƒใ‚ฐใฎใƒ†ใ‚นใƒˆใงใ™ใ€‚ใƒ†ใ‚นใƒˆใŒไบˆๆƒณใ•ใ‚Œใ‚‹ๅคฑๆ•—ใซใ‚‚ใ‹ใ‹ใ‚ใ‚‰ใšใƒ‘ใ‚นใ—ใŸๅ ดๅˆ๏ผˆpytest.mark.xfailใงใƒžใƒผใ‚ฏใ•ใ‚ŒใŸใƒ†ใ‚นใƒˆ๏ผ‰ใ€ใใ‚Œใฏxpassใจใ—ใฆใƒ†ใ‚นใƒˆใ‚ตใƒžใƒชใƒผใซๅ ฑๅ‘Šใ•ใ‚Œใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎ2ใคใฎ้–“ใฎ้‡่ฆใช้•ใ„ใฎ1ใคใฏใ€`skip` ใฏใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ—ใชใ„็‚นใงใ‚ใ‚Šใ€`xfail` ใฏๅฎŸ่กŒใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒใ‚ฐใฎใ‚ใ‚‹ใ‚ณใƒผใƒ‰ใŒไป–ใฎใƒ†ใ‚นใƒˆใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใ‚‹ๅ ดๅˆใฏใ€`xfail` ใ‚’ไฝฟ็”จใ—ใชใ„ใงใใ ใ•ใ„ใ€‚ + +#### Implementation + +- ใƒ†ใ‚นใƒˆๅ…จไฝ“ใ‚’็„กๆกไปถใซใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + + +```python no-style +@unittest.skip(reason="this bug needs to be fixed") +def test_feature_x(): +``` + +ใพใŸใฏ pytest ็ตŒ็”ฑ: + +```python no-style +@pytest.mark.skip(reason="this bug needs to be fixed") +``` + +ใพใŸใฏ `xfail` ใฎๆ–นๆณ•: + +```python no-style +@pytest.mark.xfail +def test_feature_x(): +``` + + +- ใƒ†ใ‚นใƒˆๅ†…ใฎๅ†…้ƒจใƒใ‚งใƒƒใ‚ฏใซๅŸบใฅใ„ใฆใƒ†ใ‚นใƒˆใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```python +def test_feature_x(): + if not has_something(): + pytest.skip("unsupported configuration") +``` + +ใพใŸใฏใƒขใ‚ธใƒฅใƒผใƒซๅ…จไฝ“: + +```python +import pytest + +if not pytest.config.getoption("--custom-flag"): + pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True) +``` + +ใพใŸใฏ `xfail` ใฎๆ–นๆณ•: + +```python +def test_feature_x(): + pytest.xfail("expected to fail until bug XYZ is fixed") +``` + +- ไธ€้ƒจใฎใ‚คใƒณใƒใƒผใƒˆใŒๆฌ ่ฝใ—ใฆใ„ใ‚‹ๅ ดๅˆใซใƒขใ‚ธใƒฅใƒผใƒซๅ†…ใฎใ™ในใฆใฎใƒ†ใ‚นใƒˆใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ™ใ‚‹ๆ–นๆณ•ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```python +docutils = pytest.importorskip("docutils", minversion="0.3") +``` + +- ๆกไปถใซๅŸบใฅใ„ใฆใƒ†ใ‚นใƒˆใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ—ใพใ™ใ€‚ + +```python no-style +@pytest.mark.skipif(sys.version_info < (3,6), reason="requires python3.6 or higher") +def test_feature_x(): +``` + +ใพใŸใฏ๏ผš + +```python no-style +@unittest.skipIf(torch_device == "cpu", "Can't do half precision") +def test_feature_x(): +``` + + +ใพใŸใฏใƒขใ‚ธใƒฅใƒผใƒซๅ…จไฝ“ใ‚’ใ‚นใ‚ญใƒƒใƒ—ใ—ใพใ™ใ€‚ + +```python no-style +@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows") +class TestClass(): + def test_feature_x(self): +``` + +่ฉณ็ดฐใ€ไพ‹ใ€ใŠใ‚ˆใณๆ–นๆณ•ใซใคใ„ใฆใฎ่ฉณ็ดฐใฏ[ใ“ใกใ‚‰](https://docs.pytest.org/en/latest/skipping.html)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +### Slow tests + +ใƒ†ใ‚นใƒˆใƒฉใ‚คใƒ–ใƒฉใƒชใฏ็€ๅฎŸใซๆˆ้•ทใ—ใฆใŠใ‚Šใ€ใƒ†ใ‚นใƒˆใฎไธ€้ƒจใฏๆ•ฐๅˆ†ใ‹ใ‹ใ‚Šใพใ™ใ€‚ใใฎใŸใ‚ใ€CIใงใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใฎๅฎŒไบ†ใ‚’ๅพ…ใคใฎใฏ1ๆ™‚้–“ๅพ…ใคไฝ™่ฃ•ใŒใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ„ใใคใ‹ใฎไพ‹ๅค–ใ‚’้™คใ„ใฆใ€้…ใ„ใƒ†ใ‚นใƒˆใฏไปฅไธ‹ใฎไพ‹ใฎใ‚ˆใ†ใซใƒžใƒผใ‚ฏใ™ในใใงใ™๏ผš + + +```python no-style +from transformers.testing_utils import slow +@slow +def test_integration_foo(): +``` + + +ใƒ†ใ‚นใƒˆใŒ`@slow`ใจใ—ใฆใƒžใƒผใ‚ฏใ•ใ‚ŒใŸใ‚‰ใ€ใใฎใ‚ˆใ†ใชใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€็’ฐๅขƒๅค‰ๆ•ฐ `RUN_SLOW=1`ใ‚’่จญๅฎšใ—ใพใ™ใ€‚ไพ‹: + +```bash +RUN_SLOW=1 pytest tests +``` + +`@parameterized` ใฎใ‚ˆใ†ใชใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใฏใƒ†ใ‚นใƒˆๅใ‚’ๆ›ธใๆ›ใˆใ‚‹ใŸใ‚ใ€`@slow` ใŠใ‚ˆใณไป–ใฎใ‚นใ‚ญใƒƒใƒ—ใƒ‡ใ‚ณใƒฌใƒผใ‚ฟ `@require_*` ใฏๆญฃใ—ใๅ‹•ไฝœใ™ใ‚‹ใŸใ‚ใซใฏใ€ๆœ€ๅพŒใซใƒชใ‚นใƒˆใ‚ขใƒƒใƒ—ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไปฅไธ‹ใฏๆญฃใ—ใ„ไฝฟ็”จไพ‹ใฎไธ€ไพ‹ใงใ™๏ผš + + +```python no-style +@parameterized.expand(...) +@slow +def test_integration_foo(): +``` + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใฎๅ†’้ ญใง่ชฌๆ˜Žใ—ใŸใ‚ˆใ†ใซใ€้…ใ„ใƒ†ใ‚นใƒˆใฏๅฎšๆœŸ็š„ใชใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใซๅพ“ใฃใฆๅฎŸ่กŒใ•ใ‚Œใ€PRใฎCIใƒใ‚งใƒƒใ‚ฏใงใฏๅฎŸ่กŒใ•ใ‚Œใพใ›ใ‚“ใ€‚ใใฎใŸใ‚ใ€ไธ€้ƒจใฎๅ•้กŒใŒPRใฎๆๅ‡บๆ™‚ใซ่ฆ‹่ฝใจใ•ใ‚Œใ€ใƒžใƒผใ‚ธใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ใใฎใ‚ˆใ†ใชๅ•้กŒใฏๆฌกๅ›žใฎใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒซใ•ใ‚ŒใŸCIใ‚ธใƒงใƒ–ใงๆคœๅ‡บใ•ใ‚Œใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใใ‚ŒใฏใพใŸใ€PRใ‚’ๆๅ‡บใ™ใ‚‹ๅ‰ใซ่‡ชๅˆ†ใฎใƒžใ‚ทใƒณใง้…ใ„ใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใ™ใ‚‹้‡่ฆๆ€งใ‚’ๆ„ๅ‘ณใ—ใฆใ„ใพใ™ใ€‚ + +ใฉใฎใƒ†ใ‚นใƒˆใ‚’้…ใ„ใƒ†ใ‚นใƒˆใจใ—ใฆใƒžใƒผใ‚ฏใ™ในใใ‹ใ‚’้ธๆŠžใ™ใ‚‹ใŸใ‚ใฎใ€ใŠใŠใพใ‹ใชๆ„ๆ€ๆฑบๅฎšใƒกใ‚ซใƒ‹ใ‚บใƒ ใŒๆฌกใซ็คบใ•ใ‚Œใฆใ„ใพใ™๏ผš + +- ใƒ†ใ‚นใƒˆใŒใƒฉใ‚คใƒ–ใƒฉใƒชใฎๅ†…้ƒจใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใฎ1ใคใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใ‚‹ๅ ดๅˆ๏ผˆไพ‹: ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ•ใ‚กใ‚คใƒซใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใƒ•ใ‚กใ‚คใƒซใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ๏ผ‰ใ€ใใฎใƒ†ใ‚นใƒˆใฏ้…ใ„ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใใ‚ŒใŒใƒฉใ‚คใƒ–ใƒฉใƒชใฎไป–ใฎๅด้ขใ€ใŸใจใˆใฐใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚„ไพ‹ใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใฆใ„ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚‰ใฎใƒ†ใ‚นใƒˆใฏ้…ใ„ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใใ—ใฆใ€ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๆด—็ทดใ•ใ›ใ‚‹ใŸใ‚ใซไพ‹ๅค–ใ‚’่จญใ‘ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +- ้‡ใ„ใ‚ฆใ‚งใ‚คใƒˆใ‚ปใƒƒใƒˆใ‚„็ด„50MBไปฅไธŠใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ™ในใฆใฎใƒ†ใ‚นใƒˆ๏ผˆไพ‹: ใƒขใƒ‡ใƒซ็ตฑๅˆใƒ†ใ‚นใƒˆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถ็ตฑๅˆใƒ†ใ‚นใƒˆใ€ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณ็ตฑๅˆใƒ†ใ‚นใƒˆ๏ผ‰ใฏ้…ใ„ใƒ†ใ‚นใƒˆใจใ—ใฆ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅ ดๅˆใ€็ตฑๅˆใƒ†ใ‚นใƒˆ็”จใซใƒฉใƒณใƒ€ใƒ ใชใ‚ฆใ‚งใ‚คใƒˆใ‚’ๆŒใคๅฐใ•ใชใƒใƒผใ‚ธใƒงใƒณใ‚’ไฝœๆˆใ—ใ€ใƒใƒ–ใซใ‚ขใƒƒใƒ—ใƒญใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใคใ„ใฆใฏไปฅไธ‹ใฎๆฎต่ฝใง่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +- ็‰นใซ้ซ˜้€ŸๅŒ–ใ•ใ‚Œใฆใ„ใชใ„ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ใ™ในใฆใฎใƒ†ใ‚นใƒˆใฏ้…ใ„ใƒ†ใ‚นใƒˆใจใ—ใฆ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +- ไธ€้ƒจใฎใ€Œ้…ใ„ใ€ใงใ‚ใ‚‹ในใใงใชใ„ใƒ†ใ‚นใƒˆใŒ้žๅธธใซ้…ใ„ๅ ดๅˆใ€ใŠใ‚ˆใณใใ‚Œใ‚‰ใ‚’ `@slow` ใจใ—ใฆ่จญๅฎšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใซใฏไพ‹ๅค–ใ‚’ๅฐŽๅ…ฅใงใใพใ™ใ€‚ๅคงๅฎน้‡ใฎใƒ•ใ‚กใ‚คใƒซใ‚’ใƒ‡ใ‚ฃใ‚นใ‚ฏใซไฟๅญ˜ใŠใ‚ˆใณ่ชญใฟ่พผใฟใ™ใ‚‹่‡ชๅ‹•ใƒขใƒ‡ใƒชใƒณใ‚ฐใƒ†ใ‚นใƒˆใฏใ€`@slow` ใจใ—ใฆใƒžใƒผใ‚ฏใ•ใ‚ŒใŸใƒ†ใ‚นใƒˆใฎ่‰ฏใ„ไพ‹ใงใ™ใ€‚ + +- CIใง1็ง’ๆœชๆบ€ใงใƒ†ใ‚นใƒˆใŒๅฎŒไบ†ใ™ใ‚‹ๅ ดๅˆ๏ผˆใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ‚’ๅซใ‚€๏ผ‰ใ€ใใ‚Œใฏ้€šๅธธใฎใƒ†ใ‚นใƒˆใงใ‚ใ‚‹ในใใงใ™ใ€‚ + +ใ™ในใฆใฎ้ž้…ใ„ใƒ†ใ‚นใƒˆใฏใ€ใ•ใพใ–ใพใชๅ†…้ƒจ่ฆ็ด ใ‚’ๅฎŒๅ…จใซใ‚ซใƒใƒผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใŒใ€้ซ˜้€Ÿใงใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€็‰นๅˆฅใซไฝœๆˆใ•ใ‚ŒใŸๅฐใ•ใชใƒขใƒ‡ใƒซ๏ผˆใƒฌใ‚คใƒคใƒผๆ•ฐใŒๆœ€ๅฐ้™ใงใ€่ชžๅฝ™ใ‚ตใ‚คใ‚บใŒๅฐใ•ใ„ใชใฉ๏ผ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ใ‹ใชใ‚Šใฎใ‚ซใƒใƒฌใƒƒใ‚ธใ‚’ๅฎŸ็พใงใใพใ™ใ€‚ใใฎๅพŒใ€`@slow` ใƒ†ใ‚นใƒˆใงใฏๅคง่ฆๆจกใช้…ใ„ใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆ่ณช็š„ใชใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใงใใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใ‚’ไฝฟ็”จใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซ *tiny* ใƒขใƒ‡ใƒซใ‚’ๆŽขใ—ใฆใใ ใ•ใ„๏ผš + + +```bash +grep tiny tests examples +``` + +[ใ‚นใ‚ฏใƒชใƒ—ใƒˆใฎไพ‹](https://github.com/huggingface/transformers/tree/main/scripts/fsmt/fsmt-make-tiny-model.py)ใŒใ‚ใ‚Šใ€ใ“ใ‚Œใซใ‚ˆใ‚Š tiny-wmt19-en-de ใฎใ‚ˆใ†ใชๅฐใ•ใชใƒขใƒ‡ใƒซใŒไฝœๆˆใ•ใ‚Œใพใ™ใ€‚็‰นๅฎšใฎใƒขใƒ‡ใƒซใฎใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซ็ฐกๅ˜ใซ่ชฟๆ•ดใงใใพใ™ใ€‚ + +ๅฎŸ่กŒๆ™‚้–“ใ‚’่ชคใฃใฆๆธฌๅฎšใ™ใ‚‹ใ“ใจใŒ็ฐกๅ˜ใงใ™ใ€‚ใŸใจใˆใฐใ€ๅทจๅคงใชใƒขใƒ‡ใƒซใฎใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใซ้–ขใ™ใ‚‹ใ‚ชใƒผใƒใƒผใƒ˜ใƒƒใƒ‰ใŒใ‚ใ‚‹ๅ ดๅˆใ€ใƒญใƒผใ‚ซใƒซใงใƒ†ใ‚นใƒˆใ™ใ‚‹ใจใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ•ใ‚ŒใŸใƒ•ใ‚กใ‚คใƒซใŒใ‚ญใƒฃใƒƒใ‚ทใƒฅใ•ใ‚Œใ€ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ๆ™‚้–“ใŒ่จˆๆธฌใ•ใ‚Œใชใใชใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€CIใƒญใ‚ฐใฎๅฎŸ่กŒ้€Ÿๅบฆใƒฌใƒใƒผใƒˆ๏ผˆ`pytest --durations=0 tests` ใฎๅ‡บๅŠ›๏ผ‰ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒฌใƒใƒผใƒˆใฏใ€้…ใ„ใƒ†ใ‚นใƒˆใจใ—ใฆใƒžใƒผใ‚ฏใ•ใ‚Œใฆใ„ใชใ„้…ใ„ๅค–ใ‚Œๅ€คใ‚„ใ€้ซ˜้€Ÿใซๆ›ธใ็›ดใ™ๅฟ…่ฆใŒใ‚ใ‚‹ใƒ†ใ‚นใƒˆใ‚’่ฆ‹ใคใ‘ใ‚‹ใฎใซใ‚‚ๅฝน็ซ‹ใกใพใ™ใ€‚ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใŒCIใง้…ใใชใ‚Šๅง‹ใ‚ใŸๅ ดๅˆใ€ใ“ใฎใƒฌใƒใƒผใƒˆใฎใƒˆใƒƒใƒ—ใƒชใ‚นใƒˆใซใฏๆœ€ใ‚‚้…ใ„ใƒ†ใ‚นใƒˆใŒ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ + +### Testing the stdout/stderr output + +`stdout` ใŠใ‚ˆใณ/ใพใŸใฏ `stderr` ใซๆ›ธใ่พผใ‚€้–ขๆ•ฐใ‚’ใƒ†ใ‚นใƒˆใ™ใ‚‹ใŸใ‚ใซใ€ใƒ†ใ‚นใƒˆใฏ `pytest` ใฎ [capsys ใ‚ทใ‚นใƒ†ใƒ ](https://docs.pytest.org/en/latest/capture.html) ใ‚’ไฝฟ็”จใ—ใฆใ“ใ‚Œใ‚‰ใฎใ‚นใƒˆใƒชใƒผใƒ ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ไปฅไธ‹ใฏใใฎๆ–นๆณ•ใงใ™๏ผš + + +```python +import sys + + +def print_to_stdout(s): + print(s) + + +def print_to_stderr(s): + sys.stderr.write(s) + + +def test_result_and_stdout(capsys): + msg = "Hello" + print_to_stdout(msg) + print_to_stderr(msg) + out, err = capsys.readouterr() # consume the captured output streams + # optional: if you want to replay the consumed streams: + sys.stdout.write(out) + sys.stderr.write(err) + # test: + assert msg in out + assert msg in err +``` + + +ใใ—ใฆใ‚‚ใกใ‚ใ‚“ใ€ใปใจใ‚“ใฉใฎๅ ดๅˆใ€`stderr`ใฏไพ‹ๅค–ใฎไธ€้ƒจใจใ—ใฆๆไพ›ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใใฎใ‚ˆใ†ใชๅ ดๅˆใซใฏ try/excel ใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ‚ฑใƒผใ‚น๏ผš + +```python +def raise_exception(msg): + raise ValueError(msg) + + +def test_something_exception(): + msg = "Not a good value" + error = "" + try: + raise_exception(msg) + except Exception as e: + error = str(e) + assert msg in error, f"{msg} is in the exception:\n{error}" +``` + + +stdout ใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ™ใ‚‹ใ‚‚ใ† 1 ใคใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€`contextlib.redirect_stdout`ใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™ใ€‚ + +```python +from io import StringIO +from contextlib import redirect_stdout + + +def print_to_stdout(s): + print(s) + + +def test_result_and_stdout(): + msg = "Hello" + buffer = StringIO() + with redirect_stdout(buffer): + print_to_stdout(msg) + out = buffer.getvalue() + # optional: if you want to replay the consumed streams: + sys.stdout.write(out) + # test: + assert msg in out +``` + +stdout ใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ™ใ‚‹้š›ใฎ้‡่ฆใชๆฝœๅœจ็š„ใชๅ•้กŒใฏใ€้€šๅธธใฎ `print` ใงใ“ใ‚Œใพใงใซๅ‡บๅŠ›ใ•ใ‚ŒใŸๅ†…ๅฎนใ‚’ใƒชใ‚ปใƒƒใƒˆใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ `\r` ๆ–‡ๅญ—ใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใ“ใจใงใ™ใ€‚`pytest` ่‡ชไฝ“ใซใฏๅ•้กŒใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€`pytest -s` ใงใฏใ“ใ‚Œใ‚‰ใฎๆ–‡ๅญ—ใŒใƒใƒƒใƒ•ใ‚กใซๅซใพใ‚Œใ‚‹ใŸใ‚ใ€`-s` ใ‚ใ‚Šใจใชใ—ใงใƒ†ใ‚นใƒˆใ‚’ๅฎŸ่กŒใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€`re.sub(r'~.*\r', '', buf, 0, re.M)` ใ‚’ไฝฟ็”จใ—ใฆใ‚ญใƒฃใƒ—ใƒใƒฃใ•ใ‚ŒใŸๅ‡บๅŠ›ใซๅฏพใ—ใฆ่ฟฝๅŠ ใฎใ‚ฏใƒชใƒผใƒณใ‚ขใƒƒใƒ—ใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +ใ—ใ‹ใ—ใ€ใใฎๅพŒใ€`\r` ใŒๅซใพใ‚Œใฆใ„ใ‚‹ใ‹ใฉใ†ใ‹ใซใ‹ใ‹ใ‚ใ‚‰ใšใ€ใ™ในใฆใฎๆ“ไฝœใ‚’่‡ชๅ‹•็š„ใซๅ‡ฆ็†ใ™ใ‚‹ใƒ˜ใƒซใƒ‘ใƒผใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใƒžใƒใƒผใ‚ธใƒฃใƒฉใƒƒใƒ‘ใƒผใŒใ‚ใ‚Šใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ๆฌกใฎใ‚ˆใ†ใซ็ฐกๅ˜ใซ่กŒใˆใพใ™๏ผš + + +```python +from transformers.testing_utils import CaptureStdout + +with CaptureStdout() as cs: + function_that_writes_to_stdout() +print(cs.out) +``` + +ๅฎŒๅ…จใชใƒ†ใ‚นใƒˆไพ‹ใฏๆฌกใฎใจใŠใ‚Šใงใ™ใ€‚ + +```python +from transformers.testing_utils import CaptureStdout + +msg = "Secret message\r" +final = "Hello World" +with CaptureStdout() as cs: + print(msg + final) +assert cs.out == final + "\n", f"captured: {cs.out}, expecting {final}" +``` + +`stderr` ใ‚’ใ‚ญใƒฃใƒ—ใƒใƒฃใ—ใŸใ„ๅ ดๅˆใฏใ€ไปฃใ‚ใ‚Šใซ `CaptureStderr` ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚ + +```python +from transformers.testing_utils import CaptureStderr + +with CaptureStderr() as cs: + function_that_writes_to_stderr() +print(cs.err) +``` + +ไธกๆ–นใฎใ‚นใƒˆใƒชใƒผใƒ ใ‚’ไธ€ๅบฆใซใ‚ญใƒฃใƒ—ใƒใƒฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€่ฆชใฎ `CaptureStd` ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```python +from transformers.testing_utils import CaptureStd + +with CaptureStd() as cs: + function_that_writes_to_stdout_and_stderr() +print(cs.err, cs.out) +``` + + +ใพใŸใ€ใƒ†ใ‚นใƒˆใฎๅ•้กŒใฎใƒ‡ใƒใƒƒใ‚ฐใ‚’ๆ”ฏๆดใ™ใ‚‹ใŸใ‚ใซใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใ€ใ“ใ‚Œใ‚‰ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ ใƒžใƒใƒผใ‚ธใƒฃใƒผใฏ็ต‚ไบ†ๆ™‚ใซใ‚ญใƒฃใƒ—ใƒใƒฃใ•ใ‚ŒใŸใ‚นใƒˆใƒชใƒผใƒ ใ‚’่‡ชๅ‹•็š„ใซๅ†็”Ÿใ—ใพใ™ใ€‚ +ๆ–‡่„ˆใ‹ใ‚‰ใ€‚ + +### Capturing logger stream + +ใƒญใ‚ฌใƒผใฎๅ‡บๅŠ›ใ‚’ๆคœ่จผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฏใ€`CaptureLogger`ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +```python +from transformers import logging +from transformers.testing_utils import CaptureLogger + +msg = "Testing 1, 2, 3" +logging.set_verbosity_info() +logger = logging.get_logger("transformers.models.bart.tokenization_bart") +with CaptureLogger(logger) as cl: + logger.info(msg) +assert cl.out, msg + "\n" +``` + +### Testing with environment variables + +็‰นๅฎšใฎใƒ†ใ‚นใƒˆใง็’ฐๅขƒๅค‰ๆ•ฐใฎๅฝฑ้Ÿฟใ‚’ใƒ†ใ‚นใƒˆใ—ใŸใ„ๅ ดๅˆใฏใ€ใƒ˜ใƒซใƒ‘ใƒผ ใƒ‡ใ‚ณใƒฌใƒผใ‚ฟใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ +`transformers.testing_utils.mockenv` + +```python +from transformers.testing_utils import mockenv + + +class HfArgumentParserTest(unittest.TestCase): + @mockenv(TRANSFORMERS_VERBOSITY="error") + def test_env_override(self): + env_level_str = os.getenv("TRANSFORMERS_VERBOSITY", None) +``` + +ๅ ดๅˆใซใ‚ˆใฃใฆใฏใ€ๅค–้ƒจใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’ๅ‘ผใณๅ‡บใ™ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€`os.environ` ใซ`PYTHONPATH`ใ‚’่จญๅฎšใ—ใฆใ‚คใƒณใ‚ฏใƒซใƒผใƒ‰ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +่ค‡ๆ•ฐใฎใƒญใƒผใ‚ซใƒซ ใƒ‘ใ‚นใ€‚ใƒ˜ใƒซใƒ‘ใƒผ ใ‚ฏใƒฉใ‚น `transformers.test_utils.TestCasePlus` ใŒๅฝนใซ็ซ‹ใกใพใ™ใ€‚ + +```python +from transformers.testing_utils import TestCasePlus + + +class EnvExampleTest(TestCasePlus): + def test_external_prog(self): + env = self.get_env() + # now call the external program, passing `env` to it +``` + +ใƒ†ใ‚นใƒˆใƒ•ใ‚กใ‚คใƒซใŒ `tests` ใƒ†ใ‚นใƒˆใ‚นใ‚คใƒผใƒˆใพใŸใฏ `examples` ใฎใฉใกใ‚‰ใซใ‚ใ‚‹ใ‹ใซๅฟœใ˜ใฆ +`env[PYTHONPATH]` ใ‚’ไฝฟ็”จใ—ใฆใ€ใ“ใ‚Œใ‚‰ 2 ใคใฎใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใฎใ„ใšใ‚Œใ‹ใ‚’ๅซใ‚ใพใ™ใ€‚ใพใŸใ€ใƒ†ใ‚นใƒˆใŒ็ขบๅฎŸใซ่กŒใ‚ใ‚Œใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใŸใ‚ใฎ `src` ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใ‚‚ๅซใ‚ใพใ™ใ€‚ +็พๅœจใฎใƒชใƒใ‚ธใƒˆใƒชใซๅฏพใ—ใฆๅฎŸ่กŒใ•ใ‚Œใ€ๆœ€ๅพŒใซใ€ใƒ†ใ‚นใƒˆใŒๅฎŸ่กŒใ•ใ‚Œใ‚‹ๅ‰ใซใ™ใงใซ่จญๅฎšใ•ใ‚Œใฆใ„ใŸ `env[PYTHONPATH]` ใ‚’ไฝฟ็”จใ—ใฆๅฎŸ่กŒใ•ใ‚Œใพใ™ใ€‚ +ไฝ•ใ‹ใ‚ใ‚Œใฐๅ‘ผใฐใ‚Œใพใ™ใ€‚ + +ใ“ใฎใƒ˜ใƒซใƒ‘ใƒผ ใƒกใ‚ฝใƒƒใƒ‰ใฏ `os.environ` ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใ‚ณใƒ”ใƒผใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใ€ๅ…ƒใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฏใใฎใพใพๆฎ‹ใ‚Šใพใ™ใ€‚ + + +### Getting reproducible results + +็Šถๆณใซใ‚ˆใฃใฆใฏใ€ใƒ†ใ‚นใƒˆใฎใƒฉใƒณใƒ€ใƒ ๆ€งใ‚’ๅ‰Š้™คใ—ใŸใ„ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚ๅŒไธ€ใฎๅ†็พๅฏ่ƒฝใช็ตๆžœใ‚ปใƒƒใƒˆใ‚’ๅ–ๅพ—ใ™ใ‚‹ใซใฏใ€ +ใ‚ทใƒผใƒ‰ใ‚’ไฟฎๆญฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™: + +```python +seed = 42 + +# python RNG +import random + +random.seed(seed) + +# pytorch RNGs +import torch + +torch.manual_seed(seed) +torch.backends.cudnn.deterministic = True +if torch.cuda.is_available(): + torch.cuda.manual_seed_all(seed) + +# numpy RNG +import numpy as np + +np.random.seed(seed) +``` + + +### Debugging tests + +่ญฆๅ‘ŠใŒ็™บ็”Ÿใ—ใŸๆ™‚็‚นใงใƒ‡ใƒใƒƒใ‚ฌใƒผใ‚’้–‹ๅง‹ใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ + +```bash +pytest tests/utils/test_logging.py -W error::UserWarning --pdb +``` + +## Working with github actions workflows + +ใ‚ปใƒซใƒ•ใƒ—ใƒƒใ‚ทใƒฅใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผCIใ‚ธใƒงใƒ–ใ‚’ใƒˆใƒชใ‚ฌใƒผใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎๆ‰‹้ †ใ‚’ๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +1. `transformers` ใฎใƒชใƒขใƒผใƒˆใƒชใƒใ‚ธใƒˆใƒชใงๆ–ฐใ—ใ„ใƒ–ใƒฉใƒณใƒใ‚’ไฝœๆˆใ—ใพใ™๏ผˆใƒ•ใ‚ฉใƒผใ‚ฏใงใฏใชใใ€ๅ…ƒใฎใƒชใƒใ‚ธใƒˆใƒชใง่กŒใ„ใพใ™๏ผ‰ใ€‚ +2. ใƒ–ใƒฉใƒณใƒใฎๅๅ‰ใฏ `ci_` ใพใŸใฏ `ci-` ใงๅง‹ใพใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผˆ`main` ใ‚‚ใƒˆใƒชใ‚ฌใƒผใ—ใพใ™ใŒใ€`main` ใงใฏPRใ‚’ไฝœๆˆใงใใพใ›ใ‚“๏ผ‰ใ€‚ใพใŸใ€็‰นๅฎšใฎใƒ‘ใ‚นใงใฎใฟใƒˆใƒชใ‚ฌใƒผใ•ใ‚Œใพใ™ - ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใŒๆ›ธใ‹ใ‚ŒใŸๅพŒใซๅค‰ๆ›ดใ•ใ‚ŒใŸๅ ดๅˆใซๅ‚™ใˆใฆใ€ๆœ€ๆ–ฐใฎๅฎš็พฉใฏ[ใ“ใกใ‚‰](https://github.com/huggingface/transformers/blob/main/.github/workflows/self-push.yml)ใฎ *push:* ใซใ‚ใ‚Šใพใ™ใ€‚ +3. ใ“ใฎใƒ–ใƒฉใƒณใƒใ‹ใ‚‰PRใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +4. ใใฎๅพŒใ€ใ“ใฎใ‚ธใƒงใƒ–ใŒ[ใ“ใ“](https://github.com/huggingface/transformers/actions/workflows/self-push.yml)ใซ่กจ็คบใ•ใ‚Œใพใ™ใ€‚ใ‚ธใƒงใƒ–ใฏใƒใƒƒใ‚ฏใƒญใ‚ฐใŒใ‚ใ‚‹ๅ ดๅˆใ€ใ™ใใซๅฎŸ่กŒใ•ใ‚Œใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +## Testing Experimental CI Features + +CIๆฉŸ่ƒฝใฎใƒ†ใ‚นใƒˆใฏ้€šๅธธใฎCIใฎๆญฃๅธธใชๅ‹•ไฝœใซๅนฒๆธ‰ใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใŸใ‚ใ€ๆ–ฐใ—ใ„CIๆฉŸ่ƒฝใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅ ดๅˆใ€ไปฅไธ‹ใฎๆ‰‹้ †ใซๅพ“ใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +1. ใƒ†ใ‚นใƒˆใŒๅฟ…่ฆใชใ‚‚ใฎใ‚’ใƒ†ใ‚นใƒˆใ™ใ‚‹ใŸใ‚ใฎๆ–ฐใ—ใ„ๅฐ‚็”จใฎใ‚ธใƒงใƒ–ใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +2. ๆ–ฐใ—ใ„ใ‚ธใƒงใƒ–ใฏๅธธใซๆˆๅŠŸใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใŸใ‚ใ€ๅธธใซใ‚ฐใƒชใƒผใƒณ โœ“๏ผˆ่ฉณ็ดฐใฏไปฅไธ‹ๅ‚็…ง๏ผ‰ใ‚’่กจ็คบใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +3. ใ•ใพใ–ใพใช็จฎ้กžใฎPR๏ผˆใƒฆใƒผใ‚ถใƒผใƒ•ใ‚ฉใƒผใ‚ฏใƒ–ใƒฉใƒณใƒใ€้žใƒ•ใ‚ฉใƒผใ‚ฏใƒ–ใƒฉใƒณใƒใ€github.com UIใ‹ใ‚‰็›ดๆŽฅใƒ•ใ‚กใ‚คใƒซใ‚’็ทจ้›†ใ™ใ‚‹ใƒ–ใƒฉใƒณใƒใ€ใ•ใพใ–ใพใชๅผทๅˆถใƒ—ใƒƒใ‚ทใƒฅใชใฉ๏ผ‰ใŒๅฎŸ่กŒใ•ใ‚Œใ‚‹ใพใงใ„ใใคใ‹ใฎๆ—ฅ้–“ๅฎŸ่กŒใ—ใ€ๅฎŸ้จ“็š„ใชใ‚ธใƒงใƒ–ใฎใƒญใ‚ฐใ‚’็›ฃ่ฆ–ใ—ใพใ™๏ผˆๆ„ๅ›ณ็š„ใซๅธธใซใ‚ฐใƒชใƒผใƒณใซใชใ‚‹ใ‚ˆใ†ใซใชใฃใฆใ„ใ‚‹ๅ…จไฝ“ใฎใ‚ธใƒงใƒ–ใฎ็ท‘ใงใฏใชใ๏ผ‰ใ€‚ +4. ใ™ในใฆใŒๅฎ‰ๅฎšใ—ใฆใ„ใ‚‹ใ“ใจใŒๆ˜Ž็ขบใซใชใฃใŸใ‚‰ใ€ๆ–ฐใ—ใ„ๅค‰ๆ›ดใ‚’ๆ—ขๅญ˜ใฎใ‚ธใƒงใƒ–ใซ็ตฑๅˆใ—ใพใ™ใ€‚ + +ใ“ใฎใ‚ˆใ†ใซใ€CIๆฉŸ่ƒฝ่‡ชไฝ“ใฎๅฎŸ้จ“ใŒ้€šๅธธใฎใƒฏใƒผใ‚ฏใƒ•ใƒญใƒผใซๅนฒๆธ‰ใ—ใชใ„ใ‚ˆใ†ใซใงใใพใ™ใ€‚ + +ใงใฏใ€ๆ–ฐใ—ใ„CIๆฉŸ่ƒฝใŒ้–‹็™บไธญใงใ‚ใ‚‹้–“ใ€ใ‚ธใƒงใƒ–ใ‚’ๅธธใซๆˆๅŠŸใ•ใ›ใ‚‹ใซใฏใฉใ†ใ™ใ‚Œใฐใ„ใ„ใงใ—ใ‚‡ใ†ใ‹๏ผŸ + +TravisCIใฎใ‚ˆใ†ใชไธ€้ƒจใฎCIใฏ `ignore-step-failure` ใ‚’ใ‚ตใƒใƒผใƒˆใ—ใ€ๅ…จไฝ“ใฎใ‚ธใƒงใƒ–ใ‚’ๆˆๅŠŸใจใ—ใฆๅ ฑๅ‘Šใ—ใพใ™ใŒใ€ใ“ใฎๆ–‡ๆ›ธใŒไฝœๆˆใ•ใ‚ŒใŸๆ™‚็‚นใงใฏCircleCIใจGithub Actionsใฏใใ‚Œใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ใ€‚ + +ใ—ใŸใŒใฃใฆใ€ไปฅไธ‹ใฎใƒฏใƒผใ‚ฏใ‚ขใƒฉใ‚ฆใƒณใƒ‰ใ‚’ไฝฟ็”จใงใใพใ™๏ผš + +1. bashใ‚นใ‚ฏใƒชใƒ—ใƒˆๅ†…ใงๆฝœๅœจ็š„ใชๅคฑๆ•—ใ‚’ๆŠ‘ๅˆถใ™ใ‚‹ใŸใ‚ใซๅฎŸ่กŒใ‚ณใƒžใƒณใƒ‰ใฎๅ†’้ ญใซ `set +euo pipefail` ใ‚’่จ˜่ฟฐใ—ใพใ™ใ€‚ +2. ๆœ€ๅพŒใฎใ‚ณใƒžใƒณใƒ‰ใฏๆˆๅŠŸใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใŸใจใˆใฐ `echo "done"` ใพใŸใฏๅ˜ใซ `true` ใ‚’ไฝฟ็”จใงใใพใ™ใ€‚ + +ไปฅไธ‹ใฏไพ‹ใงใ™๏ผš + + + +```yaml +- run: + name: run CI experiment + command: | + set +euo pipefail + echo "setting run-all-despite-any-errors-mode" + this_command_will_fail + echo "but bash continues to run" + # emulate another failure + false + # but the last command must be a success + echo "during experiment do not remove: reporting success to CI, even if there were failures" +``` + + +ๅ˜็ด”ใชใ‚ณใƒžใƒณใƒ‰ใฎๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +```bash +cmd_that_may_fail || true +``` + +ใ‚‚ใกใ‚ใ‚“ใ€็ตๆžœใซๆบ€่ถณใ—ใŸใ‚‰ใ€ๅฎŸ้จ“็š„ใชใ‚นใƒ†ใƒƒใƒ—ใ‚„ใ‚ธใƒงใƒ–ใ‚’้€šๅธธใฎใ‚ธใƒงใƒ–ใจ็ตฑๅˆใ—ใ€`set +euo pipefail` ใชใฉใฎ่ฟฝๅŠ ใ—ใŸ่ฆ็ด ใ‚’ๅ‰Š้™คใ—ใฆใ€ๅฎŸ้จ“็š„ใชใ‚ธใƒงใƒ–ใŒ้€šๅธธใฎCIใฎๅ‹•ไฝœใซๅนฒๆธ‰ใ—ใชใ„ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ + +ใ“ใฎใƒ—ใƒญใ‚ปใ‚นๅ…จไฝ“ใฏใ€ๅฎŸ้จ“็š„ใชใ‚นใƒ†ใƒƒใƒ—ใซๅฏพใ—ใฆ `allow-failure` ใฎใ‚ˆใ†ใชใ‚‚ใฎใ‚’่จญๅฎšใ—ใ€PRใฎๅ…จไฝ“ใฎใ‚นใƒ†ใƒผใ‚ฟใ‚นใซๅฝฑ้Ÿฟใ‚’ไธŽใˆใšใซๅคฑๆ•—ใ•ใ›ใ‚‹ใ“ใจใŒใงใใ‚Œใฐใ€ใฏใ‚‹ใ‹ใซ็ฐกๅ˜ใซใชใฃใŸใงใ—ใ‚‡ใ†ใ€‚ใ—ใ‹ใ—ใ€ๅ‰่ฟฐใฎ้€šใ‚Šใ€็พๅœจใฏCircleCIใจGithub Actionsใฏใ“ใฎๆฉŸ่ƒฝใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ›ใ‚“ใ€‚ + +ใ“ใฎๆฉŸ่ƒฝใซ้–ขใ—ใฆใฎๆŠ•็ฅจใ‚„ใ€CIใซ็‰นๆœ‰ใฎใ‚นใƒฌใƒƒใƒ‰ใงใใฎ้€ฒๆ—็Šถๆณใ‚’็ขบ่ชใงใใพใ™๏ผš + +- [Github Actions:](https://github.com/actions/toolkit/issues/399) +- [CircleCI:](https://ideas.circleci.com/ideas/CCI-I-344) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tf_xla.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tf_xla.md new file mode 100644 index 0000000000000000000000000000000000000000..1f5a2af1a5a2880268423324f65e8c5780337397 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tf_xla.md @@ -0,0 +1,179 @@ + + +# XLA Integration for TensorFlow Models + +[[open-in-colab]] + +ๅŠ ้€Ÿ็ทšๅฝขไปฃๆ•ฐ๏ผˆAccelerated Linear Algebra๏ผ‰ใ€้€š็งฐXLAใฏใ€TensorFlowใƒขใƒ‡ใƒซใฎใƒฉใƒณใ‚ฟใ‚คใƒ ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ™ใ€‚[ๅ…ฌๅผใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://www.tensorflow.org/xla)ใซใ‚ˆใ‚Œใฐใ€XLA๏ผˆAccelerated Linear Algebra๏ผ‰ใฏ็ทšๅฝขไปฃๆ•ฐใฎใŸใ‚ใฎใƒ‰ใƒกใ‚คใƒณๅ›บๆœ‰ใฎใ‚ณใƒณใƒ‘ใ‚คใƒฉใงใ€TensorFlowใƒขใƒ‡ใƒซใ‚’ๆฝœๅœจ็š„ใซใ‚ฝใƒผใ‚นใ‚ณใƒผใƒ‰ใฎๅค‰ๆ›ดใชใ—ใง้ซ˜้€ŸๅŒ–ใงใใพใ™ใ€‚ + +TensorFlowใงXLAใ‚’ไฝฟ็”จใ™ใ‚‹ใฎใฏ็ฐกๅ˜ใงใ™ใ€‚XLAใฏ`tensorflow`ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใซใƒ‘ใƒƒใ‚ฑใƒผใ‚ธๅŒ–ใ•ใ‚ŒใฆใŠใ‚Šใ€[`tf.function`](https://www.tensorflow.org/guide/intro_to_graphs)ใชใฉใฎใ‚ฐใƒฉใƒ•ใ‚’ไฝœๆˆใ™ใ‚‹้–ขๆ•ฐๅ†…ใง`jit_compile`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒชใ‚ฌใƒผใงใใพใ™ใ€‚`fit()`ใ‚„`predict()`ใชใฉใฎKerasใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€`model.compile()`ใซ`jit_compile`ๅผ•ๆ•ฐใ‚’ๆธกใ™ใ ใ‘ใงXLAใ‚’ๆœ‰ๅŠนใซใงใใพใ™ใ€‚ใŸใ ใ—ใ€XLAใฏใ“ใ‚Œใ‚‰ใฎใƒกใ‚ฝใƒƒใƒ‰ใซ้™ๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ไปปๆ„ใฎ`tf.function`ใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใ‚‚ไฝฟ็”จใงใใพใ™ใ€‚ + +๐Ÿค— Transformersๅ†…ใฎใ„ใใคใ‹ใฎTensorFlowใƒกใ‚ฝใƒƒใƒ‰ใฏใ€XLAใจไบ’ๆ›ๆ€งใŒใ‚ใ‚‹ใ‚ˆใ†ใซๆ›ธใ็›ดใ•ใ‚Œใฆใ„ใพใ™ใ€‚ใ“ใ‚Œใซใฏใ€[GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2)ใ€[T5](https://huggingface.co/docs/transformers/model_doc/t5)ใ€[OPT](https://huggingface.co/docs/transformers/model_doc/opt)ใชใฉใฎใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใ‚„ใ€[Whisper](https://huggingface.co/docs/transformers/model_doc/whisper)ใชใฉใฎ้Ÿณๅฃฐๅ‡ฆ็†ใƒขใƒ‡ใƒซใ‚‚ๅซใพใ‚Œใพใ™ใ€‚ + +้€Ÿๅบฆๅ‘ไธŠใฎๅ…ทไฝ“็š„ใช้‡ใฏใƒขใƒ‡ใƒซใซ้žๅธธใซไพๅญ˜ใ—ใพใ™ใŒใ€๐Ÿค— Transformersๅ†…ใฎTensorFlowใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใงใฏใ€็ด„100ๅ€ใฎ้€Ÿๅบฆๅ‘ไธŠใ‚’็ขบ่ชใ—ใฆใ„ใพใ™ใ€‚ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใซXLAใ‚’ไฝฟ็”จใ—ใฆๆœ€ๅคงใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅพ—ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ใพใŸใ€ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใจXLA็ตฑๅˆใฎใƒ‡ใ‚ถใ‚คใƒณๅ“ฒๅญฆใซใคใ„ใฆ่ฉณใ—ใๅญฆใณใŸใ„ๅ ดๅˆใฎ่ฟฝๅŠ ใƒชใ‚ฝใƒผใ‚นใธใฎใƒชใƒณใ‚ฏใ‚‚ๆไพ›ใ—ใพใ™ใ€‚ + +## Running TF functions with XLA + +ไปฅไธ‹ใฎTensorFlowใƒขใƒ‡ใƒซใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†๏ผš + + +```py +import tensorflow as tf + +model = tf.keras.Sequential( + [tf.keras.layers.Dense(10, input_shape=(10,), activation="relu"), tf.keras.layers.Dense(5, activation="softmax")] +) +``` + +ไธŠ่จ˜ใฎใƒขใƒ‡ใƒซใฏใ€ๆฌกๅ…ƒใŒ`(10, )`ใฎๅ…ฅๅŠ›ใ‚’ๅ—ใ‘ๅ…ฅใ‚Œใพใ™ใ€‚ใ“ใฎใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใงๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ๆฌกใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + + +```py +# Generate random inputs for the model. +batch_size = 16 +input_vector_dim = 10 +random_inputs = tf.random.normal((batch_size, input_vector_dim)) + +# Run a forward pass. +_ = model(random_inputs) +``` + +XLAใงใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚ŒใŸ้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใ‚’ๅฎŸ่กŒใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซใ—ใพใ™๏ผš + + +```py +xla_fn = tf.function(model, jit_compile=True) +_ = xla_fn(random_inputs) +``` + +`model`ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ `call()` ้–ขๆ•ฐใฏXLAใ‚ฐใƒฉใƒ•ใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ใŸใ ใ—ใ€XLAใซใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใŸใ„ไป–ใฎใƒขใƒ‡ใƒซ้–ขๆ•ฐใŒใ‚ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚‚ๅฏ่ƒฝใงใ™ใ€‚ไปฅไธ‹ใฏใใฎๆ–นๆณ•ใงใ™๏ผš + + +```py +my_xla_fn = tf.function(model.my_xla_fn, jit_compile=True) +``` + +## Running a TF text generation model with XLA from ๐Ÿค— Transformers + +๐Ÿค— Transformersๅ†…ใงXLAใงใฎ้ซ˜้€ŸๅŒ–ใ•ใ‚ŒใŸ็”Ÿๆˆใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ๆœ€ๆ–ฐใƒใƒผใ‚ธใƒงใƒณใฎ`transformers`ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใใพใ™๏ผš + +```bash +pip install transformers --upgrade +``` + +ๆฌกใซใ€ๆฌกใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใงใใพใ™๏ผš + + +```py +import tensorflow as tf +from transformers import AutoTokenizer, TFAutoModelForCausalLM + +# Will error if the minimal version of Transformers is not installed. +from transformers.utils import check_min_version + +check_min_version("4.21.0") + + +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="
") +model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") +input_string = ["TensorFlow is"] + +# One line to create an XLA generation function +xla_generate = tf.function(model.generate, jit_compile=True) + +tokenized_input = tokenizer(input_string, return_tensors="tf") +generated_tokens = xla_generate(**tokenized_input, num_beams=2) + +decoded_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True) +print(f"Generated -- {decoded_text}") +# Generated -- TensorFlow is an open-source, open-source, distributed-source application # framework for the +``` + +`generate()`ใงXLAใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใฎใฏใ€ใŸใฃใŸไธ€่กŒใฎใ‚ณใƒผใƒ‰ใงใ™ใ€‚ใ‚ณใƒผใƒ‰ใฎๆฎ‹ใ‚Š้ƒจๅˆ†ใฏๅค‰ๆ›ดใ•ใ‚Œใฆใ„ใพใ›ใ‚“ใ€‚ใŸใ ใ—ใ€XLAๅ›บๆœ‰ใฎใ„ใใคใ‹ใฎๆณจๆ„็‚นใŒไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚นใƒ‹ใƒšใƒƒใƒˆใซใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใซๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€XLAใŒใ‚‚ใŸใ‚‰ใ™้€Ÿๅบฆๅ‘ไธŠใ‚’ๅฎŸ็พใ™ใ‚‹ใŸใ‚ใซใใ‚Œใ‚‰ใ‚’ๆŠŠๆกใ™ใ‚‹ใ“ใจใŒ้‡่ฆใงใ™ใ€‚ๆฌกใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใ“ใ‚Œใ‚‰ใซใคใ„ใฆ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ + + +## Gotchas to be aware of + +XLAใ‚’ๆœ‰ๅŠนใซใ—ใŸ้–ขๆ•ฐ๏ผˆไธŠ่จ˜ใฎ`xla_generate()`ใชใฉ๏ผ‰ใ‚’ๅˆใ‚ใฆๅฎŸ่กŒใ™ใ‚‹ใจใ€ๅ†…้ƒจใง่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใ‚’ๆŽจ่ซ–ใ—ใ‚ˆใ†ใจใ—ใพใ™ใŒใ€ใ“ใ‚Œใฏๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใ€‚ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏ["ใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐ"๏ผˆtracing๏ผ‰](https://www.tensorflow.org/guide/intro_to_graphs#when_is_a_function_tracing)ใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใฆใ„ใพใ™ใ€‚ + +็”Ÿๆˆๆ™‚้–“ใŒ้ซ˜้€Ÿใงใฏใชใ„ใ“ใจใซๆฐ—ไป˜ใใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚`xla_generate()`๏ผˆใพใŸใฏไป–ใฎXLAๅฏพๅฟœ้–ขๆ•ฐ๏ผ‰ใฎ้€ฃ็ถšๅ‘ผใณๅ‡บใ—ใงใฏใ€้–ขๆ•ฐใธใฎๅ…ฅๅŠ›ใŒๆœ€ๅˆใซ่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใŒๆง‹็ฏ‰ใ•ใ‚ŒใŸใจใใจๅŒใ˜ๅฝข็Šถใซๅพ“ใฃใฆใ„ใ‚‹ๅ ดๅˆใ€่จˆ็ฎ—ใ‚ฐใƒฉใƒ•ใ‚’ๆŽจ่ซ–ใ™ใ‚‹ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใ‚Œใฏใ€ๅ…ฅๅŠ›ๅฝข็ŠถใŒๅ›บๅฎšใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ€ใƒชใƒ†ใ‚ฃ๏ผˆไพ‹๏ผš็”ปๅƒ๏ผ‰ใซใฏๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใŒใ€ๅค‰ๆ•ฐใฎๅ…ฅๅŠ›ๅฝข็Šถใƒขใƒ€ใƒชใƒ†ใ‚ฃ๏ผˆไพ‹๏ผšใƒ†ใ‚ญใ‚นใƒˆ๏ผ‰ใ‚’ๆ‰ฑใ†ๅ ดๅˆใซใฏๆณจๆ„ใŒๅฟ…่ฆใงใ™ใ€‚ + +`xla_generate()`ใŒๅธธใซๅŒใ˜ๅ…ฅๅŠ›ๅฝข็Šถใงๅ‹•ไฝœใ™ใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ใซใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ๅ‘ผใณๅ‡บใ™้š›ใซ`padding`ๅผ•ๆ•ฐใ‚’ๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +```py +import tensorflow as tf +from transformers import AutoTokenizer, TFAutoModelForCausalLM + +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="") +model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") +input_string = ["TensorFlow is"] + +xla_generate = tf.function(model.generate, jit_compile=True) + +# Here, we call the tokenizer with padding options. +tokenized_input = tokenizer(input_string, pad_to_multiple_of=8, padding=True, return_tensors="tf") + +generated_tokens = xla_generate(**tokenized_input, num_beams=2) +decoded_text = tokenizer.decode(generated_tokens[0], skip_special_tokens=True) +print(f"Generated -- {decoded_text}") +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€`xla_generate()`ใธใฎๅ…ฅๅŠ›ใŒๅธธใซใƒˆใƒฌใƒผใ‚นใ•ใ‚ŒใŸๅฝข็Šถใฎๅ…ฅๅŠ›ใ‚’ๅ—ใ‘ๅ–ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใ€็”Ÿๆˆๆ™‚้–“ใฎ้ซ˜้€ŸๅŒ–ใ‚’ๅฎŸ็พใงใใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใงใ“ใ‚Œใ‚’็ขบ่ชใงใใพใ™๏ผš + +```py +import time +import tensorflow as tf +from transformers import AutoTokenizer, TFAutoModelForCausalLM + +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2", padding_side="left", pad_token="") +model = TFAutoModelForCausalLM.from_pretrained("openai-community/gpt2") + +xla_generate = tf.function(model.generate, jit_compile=True) + +for input_string in ["TensorFlow is", "TensorFlow is a", "TFLite is a"]: + tokenized_input = tokenizer(input_string, pad_to_multiple_of=8, padding=True, return_tensors="tf") + start = time.time_ns() + generated_tokens = xla_generate(**tokenized_input, num_beams=2) + end = time.time_ns() + print(f"Execution time -- {(end - start) / 1e6:.1f} ms\n") +``` + +Tesla T4 GPUใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใชๅ‡บๅŠ›ใŒๆœŸๅพ…ใ•ใ‚Œใพใ™๏ผš + +```bash +Execution time -- 30819.6 ms + +Execution time -- 79.0 ms + +Execution time -- 78.9 ms +``` + +ๆœ€ๅˆใฎ`xla_generate()`ๅ‘ผใณๅ‡บใ—ใฏใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐใฎใŸใ‚ใซๆ™‚้–“ใŒใ‹ใ‹ใ‚Šใพใ™ใŒใ€้€ฃ็ถšใ™ใ‚‹ๅ‘ผใณๅ‡บใ—ใฏๆก้•ใ„ใซ้ซ˜้€Ÿใงใ™ใ€‚็”Ÿๆˆใ‚ชใƒ—ใ‚ทใƒงใƒณใฎใ„ใ‹ใชใ‚‹ๅค‰ๆ›ดใ‚‚ใ€ๅ†ใƒˆใƒฌใƒผใ‚ทใƒณใ‚ฐใ‚’ๅผ•ใ่ตทใ“ใ—ใ€็”Ÿๆˆๆ™‚้–“ใฎ้…ๅปถใ‚’ๅผ•ใ่ตทใ“ใ™ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ใ“ใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใงใฏใ€๐Ÿค— TransformersใŒๆไพ›ใ™ใ‚‹ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ใ™ในใฆ็ถฒ็พ…ใ—ใฆใ„ใพใ›ใ‚“ใ€‚้ซ˜ๅบฆใชใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใซใคใ„ใฆใฏใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใ‚’ๅ‚็…งใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +## Additional Resources + +ใ“ใ“ใงใฏใ€๐Ÿค— Transformersใจไธ€่ˆฌ็š„ใชXLAใซใคใ„ใฆใ•ใ‚‰ใซ่ฉณใ—ใๅญฆใณใŸใ„ๅ ดๅˆใฎใ„ใใคใ‹ใฎ่ฟฝๅŠ ใƒชใ‚ฝใƒผใ‚นใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +* [ใ“ใฎColab Notebook](https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/91_tf_xla_generate.ipynb)ใงใฏใ€XLAๅฏพๅฟœใฎใ‚จใƒณใ‚ณใƒผใƒ€ใƒผใƒ‡ใ‚ณใƒผใƒ€ใƒผ๏ผˆ[T5](https://huggingface.co/docs/transformers/model_doc/t5)ใชใฉ๏ผ‰ใŠใ‚ˆใณใƒ‡ใ‚ณใƒผใƒ€ใƒผๅฐ‚็”จ๏ผˆ[GPT2](https://huggingface.co/docs/transformers/model_doc/gpt2)ใชใฉ๏ผ‰ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใƒขใƒ‡ใƒซใ‚’่ฉฆใ™ใŸใ‚ใฎๅฏพ่ฉฑๅž‹ใƒ‡ใƒขใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +* [ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://huggingface.co/blog/tf-xla-generate)ใงใฏใ€XLAๅฏพๅฟœใƒขใƒ‡ใƒซใฎๆฏ”่ผƒใƒ™ใƒณใƒใƒžใƒผใ‚ฏใฎๆฆ‚่ฆใจใ€TensorFlowใงใฎXLAใซใคใ„ใฆใฎๅ‹ๅฅฝ็š„ใช็ดนไป‹ใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +* [ใ“ใฎใƒ–ใƒญใ‚ฐ่จ˜ไบ‹](https://blog.tensorflow.org/2022/11/how-hugging-face-improved-text-generation-performance-with-xla.html)ใงใฏใ€๐Ÿค— TransformersใฎTensorFlowใƒขใƒ‡ใƒซใซXLAใ‚ตใƒใƒผใƒˆใ‚’่ฟฝๅŠ ใ™ใ‚‹้š›ใฎ่จญ่จˆๅ“ฒๅญฆใซใคใ„ใฆ่ชฌๆ˜Žใ—ใฆใ„ใพใ™ใ€‚ +* ไธ€่ˆฌ็š„ใชXLAใจTensorFlowใ‚ฐใƒฉใƒ•ใซใคใ„ใฆ่ฉณใ—ใๅญฆใถใŸใ‚ใฎใŠใ™ใ™ใ‚ใฎๆŠ•็จฟ๏ผš + * [XLA: ๆฉŸๆขฐๅญฆ็ฟ’็”จใฎๆœ€้ฉๅŒ–ใ‚ณใƒณใƒ‘ใ‚คใƒฉ](https://www.tensorflow.org/xla) + * [ใ‚ฐใƒฉใƒ•ใจ`tf.function`ใฎ็ดนไป‹](https://www.tensorflow.org/guide/intro_to_graphs) + * [`tf.function`ใ‚’ไฝฟ็”จใ—ใŸใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๅ‘ไธŠ](https://www.tensorflow.org/guide/function) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tflite.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tflite.md new file mode 100644 index 0000000000000000000000000000000000000000..ad3e9a3f484e2ce157a23cf0b58d1854efc8a267 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tflite.md @@ -0,0 +1,58 @@ + + +# Export to TFLite + +[TensorFlow Lite](https://www.tensorflow.org/lite/guide)ใฏใ€ใƒขใƒใ‚คใƒซใƒ•ใ‚ฉใƒณใ€็ต„ใฟ่พผใฟใ‚ทใ‚นใƒ†ใƒ ใ€ใŠใ‚ˆใณใƒขใƒŽใฎใ‚คใƒณใ‚ฟใƒผใƒใƒƒใƒˆ๏ผˆIoT๏ผ‰ใƒ‡ใƒใ‚คใ‚นใชใฉใ€ใƒชใ‚ฝใƒผใ‚นใซๅˆถ็ด„ใฎใ‚ใ‚‹ใƒ‡ใƒใ‚คใ‚นใซๆฉŸๆขฐๅญฆ็ฟ’ใƒขใƒ‡ใƒซใ‚’ๅฑ•้–‹ใ™ใ‚‹ใŸใ‚ใฎ่ปฝ้‡ใชใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใ™ใ€‚TFLiteใฏใ€่จˆ็ฎ—่ƒฝๅŠ›ใ€ใƒกใƒขใƒชใ€ใŠใ‚ˆใณ้›ปๅŠ›ๆถˆ่ฒปใŒ้™ใ‚‰ใ‚Œใฆใ„ใ‚‹ใ“ใ‚Œใ‚‰ใฎใƒ‡ใƒใ‚คใ‚นไธŠใงใƒขใƒ‡ใƒซใ‚’ๅŠน็އ็š„ใซๆœ€้ฉๅŒ–ใ—ใฆๅฎŸ่กŒใ™ใ‚‹ใŸใ‚ใซ่จญ่จˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ +TensorFlow Liteใƒขใƒ‡ใƒซใฏใ€`.tflite`ใƒ•ใ‚กใ‚คใƒซๆ‹กๅผตๅญใง่ญ˜ๅˆฅใ•ใ‚Œใ‚‹็‰นๅˆฅใชๅŠน็އ็š„ใชใƒใƒผใ‚ฟใƒ–ใƒซๅฝขๅผใง่กจใ•ใ‚Œใพใ™ใ€‚ + +๐Ÿค— Optimumใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’TFLiteใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใŸใ‚ใฎๆฉŸ่ƒฝใ‚’`exporters.tflite`ใƒขใ‚ธใƒฅใƒผใƒซใ‚’ไป‹ใ—ใฆๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ใ‚ตใƒใƒผใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใฎใƒชใ‚นใƒˆใซใคใ„ใฆใฏใ€[๐Ÿค— Optimumใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/exporters/tflite/overview)ใ‚’ใ”ๅ‚็…งใใ ใ•ใ„ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’TFLiteใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€ๅฟ…่ฆใชไพๅญ˜้–ขไฟ‚ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใใ ใ•ใ„๏ผš + + +```bash +pip install optimum[exporters-tf] +``` + +ใ™ในใฆใฎๅˆฉ็”จๅฏ่ƒฝใชๅผ•ๆ•ฐใ‚’็ขบ่ชใ™ใ‚‹ใซใฏใ€[๐Ÿค— Optimumใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model)ใ‚’ๅ‚็…งใ™ใ‚‹ใ‹ใ€ใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใงใƒ˜ใƒซใƒ—ใ‚’่กจ็คบใ—ใฆใใ ใ•ใ„๏ผš + +```bash +optimum-cli export tflite --help +``` + +๐Ÿค— Hubใ‹ใ‚‰ใƒขใƒ‡ใƒซใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€ไพ‹ใˆใฐ `google-bert/bert-base-uncased` ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ๆฌกใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™๏ผš + +```bash +optimum-cli export tflite --model google-bert/bert-base-uncased --sequence_length 128 bert_tflite/ +``` + +้€ฒ่กŒ็Šถๆณใ‚’็คบใ™ใƒญใ‚ฐใŒ่กจ็คบใ•ใ‚Œใ€็”Ÿๆˆใ•ใ‚ŒใŸ `model.tflite` ใŒไฟๅญ˜ใ•ใ‚ŒใŸๅ ดๆ‰€ใ‚‚่กจ็คบใ•ใ‚Œใ‚‹ใฏใšใงใ™๏ผš + +```bash +Validating TFLite model... + -[โœ“] TFLite model output names match reference model (logits) + - Validating TFLite Model output "logits": + -[โœ“] (1, 128, 30522) matches (1, 128, 30522) + -[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05) +The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05: +- logits: max diff = 5.817413330078125e-05. + The exported model was saved at: bert_tflite + ``` + +ไธŠ่จ˜ใฎไพ‹ใฏ๐Ÿค— Hubใ‹ใ‚‰ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใฆใ„ใพใ™ใ€‚ใƒญใƒผใ‚ซใƒซใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ๅ ดๅˆใ€ใพใšใƒขใƒ‡ใƒซใฎ้‡ใฟใƒ•ใ‚กใ‚คใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒ•ใ‚กใ‚คใƒซใ‚’ๅŒใ˜ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช๏ผˆ`local_path`๏ผ‰ใซไฟๅญ˜ใ—ใŸใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚CLIใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€๐Ÿค— Hubใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆๅใฎไปฃใ‚ใ‚Šใซ`model`ๅผ•ๆ•ฐใซ`local_path`ใ‚’ๆธกใ—ใพใ™ใ€‚ + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tokenizer_summary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tokenizer_summary.md new file mode 100644 index 0000000000000000000000000000000000000000..47afc1f2e06003c2f3e57a164724ea5a00d03ab8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/tokenizer_summary.md @@ -0,0 +1,179 @@ + + +# Summary of the tokenizers + +[[open-in-colab]] + +ใ“ใฎใƒšใƒผใ‚ธใงใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใซใคใ„ใฆ่ฉณใ—ใ่ฆ‹ใฆใ„ใใพใ™ใ€‚ + + + +[ๅ‰ๅ‡ฆ็†ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](preprocessing)ใง่ฆ‹ใŸใ‚ˆใ†ใซใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใ“ใจใฏใ€ใใ‚Œใ‚’ๅ˜่ชžใพใŸใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใ—ใ€ใใ‚Œใ‚‰ใ‚’ใƒซใƒƒใ‚ฏใ‚ขใƒƒใƒ—ใƒ†ใƒผใƒ–ใƒซใ‚’ไป‹ใ—ใฆIDใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ๅ˜่ชžใพใŸใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ใ‚’IDใซๅค‰ๆ›ใ™ใ‚‹ใ“ใจใฏ็ฐกๅ˜ใงใ™ใฎใงใ€ใ“ใฎ่ฆ็ด„ใงใฏใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ˜่ชžใพใŸใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใ™ใ‚‹๏ผˆใคใพใ‚Šใ€ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ™ใ‚‹๏ผ‰ใ“ใจใซ็„ฆ็‚นใ‚’ๅฝ“ใฆใพใ™ใ€‚ๅ…ทไฝ“็š„ใซใฏใ€๐Ÿค— Transformersใงไฝฟ็”จใ•ใ‚Œใ‚‹3ใคใฎไธป่ฆใชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ€[Byte-Pair Encoding๏ผˆBPE๏ผ‰](#byte-pair-encoding)ใ€[WordPiece](#wordpiece)ใ€ใŠใ‚ˆใณ[SentencePiece](#sentencepiece)ใ‚’่ฆ‹ใฆใ€ใฉใฎใƒขใƒ‡ใƒซใŒใฉใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚ฟใ‚คใƒ—ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใฎไพ‹ใ‚’็คบใ—ใพใ™ใ€‚ + +ๅ„ใƒขใƒ‡ใƒซใƒšใƒผใ‚ธใงใฏใ€ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใŒใฉใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚ฟใ‚คใƒ—ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‹ใ‚’็Ÿฅใ‚‹ใŸใ‚ใซใ€้–ข้€ฃใ™ใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’็ขบ่ชใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€[`BertTokenizer`]ใ‚’่ฆ‹ใ‚‹ใจใ€ใƒขใƒ‡ใƒซใŒ[WordPiece](#wordpiece)ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚ + +## Introduction + +ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใƒใƒฃใƒณใ‚ฏใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใฏใ€่ฆ‹ใ‹ใ‘ไปฅไธŠใซ้›ฃใ—ใ„ใ‚ฟใ‚นใ‚ฏใงใ‚ใ‚Šใ€่ค‡ๆ•ฐใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€ๆฌกใฎๆ–‡ใ‚’่€ƒใˆใฆใฟใพใ—ใ‚‡ใ†ใ€‚ใ€Œ"Don't you love ๐Ÿค— Transformers? We sure do."ใ€ + + + +ใ“ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹็ฐกๅ˜ใชๆ–นๆณ•ใฏใ€ใ‚นใƒšใƒผใ‚นใงๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ไปฅไธ‹ใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +``` +["Don't", "you", "love", "๐Ÿค—", "Transformers?", "We", "sure", "do."] +``` + +ใ“ใ‚Œใฏๅˆ็†็š„ใช็ฌฌไธ€ๆญฉใงใ™ใŒใ€ใƒˆใƒผใ‚ฏใƒณ "Transformers?" ใจ "do." ใ‚’่ฆ‹ใ‚‹ใจใ€ๅฅ่ชญ็‚นใŒๅ˜่ชž "Transformer" ใจ "do" ใซ็ตๅˆใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใ€ใ“ใ‚Œใฏๆœ€้ฉใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๅฅ่ชญ็‚นใ‚’่€ƒๆ…ฎใซๅ…ฅใ‚Œใ‚‹ในใใงใ€ใƒขใƒ‡ใƒซใŒๅ˜่ชžใจใใ‚Œใซ็ถšใๅฏ่ƒฝๆ€งใฎใ‚ใ‚‹ใ™ในใฆใฎๅฅ่ชญ็‚น่จ˜ๅทใฎ็•ฐใชใ‚‹่กจ็พใ‚’ๅญฆใฐใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„ใ“ใจใ‚’้ฟใ‘ใ‚‹ในใใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใŒๅญฆใฐใชใ‘ใ‚Œใฐใชใ‚‰ใชใ„่กจ็พใฎๆ•ฐใŒ็ˆ†็™บ็š„ใซๅข—ๅŠ ใ—ใพใ™ใ€‚ๅฅ่ชญ็‚นใ‚’่€ƒๆ…ฎใซๅ…ฅใ‚ŒใŸๅ ดๅˆใ€ไพ‹ๆ–‡ใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฏๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +``` +["Don", "'", "t", "you", "love", "๐Ÿค—", "Transformers", "?", "We", "sure", "do", "."] +``` + +ใŸใ ใ—ใ€ๅ˜่ชžใ€Œ"Don't"ใ€ใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ—ใฆใฏใ€ไธๅˆฉใชๅด้ขใŒใ‚ใ‚Šใพใ™ใ€‚ ใ€Œ"Don't"ใ€ใฏใ€Œ"do not"ใ€ใ‚’่กจใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ใ€Œ["Do", "n't"]ใ€ใจใ—ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ๆ–นใŒ้ฉใ—ใฆใ„ใพใ™ใ€‚ใ“ใ“ใ‹ใ‚‰ไบ‹ๆŸ„ใŒ่ค‡้›‘ใซใชใ‚Šใ€ๅ„ใƒขใƒ‡ใƒซใŒ็‹ฌ่‡ชใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚ฟใ‚คใƒ—ใ‚’ๆŒใค็†็”ฑใฎไธ€้ƒจใงใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ™ใ‚‹ใŸใ‚ใซ้ฉ็”จใ™ใ‚‹ใƒซใƒผใƒซใซๅฟœใ˜ใฆใ€ๅŒใ˜ใƒ†ใ‚ญใ‚นใƒˆใซๅฏพใ—ใฆ็•ฐใชใ‚‹ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ•ใ‚ŒใŸๅ‡บๅŠ›ใŒ็”Ÿๆˆใ•ใ‚Œใพใ™ใ€‚ไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆธˆใฟใƒขใƒ‡ใƒซใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใ‚’ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ™ใ‚‹ใฎใซไฝฟ็”จใ•ใ‚ŒใŸใƒซใƒผใƒซใจๅŒใ˜ใƒซใƒผใƒซใงใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ•ใ‚ŒใŸๅ…ฅๅŠ›ใ‚’ๆไพ›ใ™ใ‚‹ๅ ดๅˆใซใฎใฟๆญฃๅธธใซๆฉŸ่ƒฝใ—ใพใ™ใ€‚ + +[spaCy](https://spacy.io/)ใจ[Moses](http://www.statmt.org/moses/?n=Development.GetStarted)ใฏใ€2ใคใฎไบบๆฐ—ใฎใ‚ใ‚‹ใƒซใƒผใƒซใƒ™ใƒผใ‚นใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใงใ™ใ€‚ใ“ใ‚Œใ‚‰ใ‚’็งใŸใกใฎไพ‹ใซ้ฉ็”จใ™ใ‚‹ใจใ€*spaCy*ใจ*Moses*ใฏๆฌกใฎใ‚ˆใ†ใชๅ‡บๅŠ›ใ‚’็”Ÿๆˆใ—ใพใ™๏ผš + +``` +["Do", "n't", "you", "love", "๐Ÿค—", "Transformers", "?", "We", "sure", "do", "."] +``` + +็ฉบ็™ฝใจๅฅ่ชญ็‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใ€ใŠใ‚ˆใณใƒซใƒผใƒซใƒ™ใƒผใ‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใŒไฝฟ็”จใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใ€‚็ฉบ็™ฝใจๅฅ่ชญ็‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใ€ใŠใ‚ˆใณใƒซใƒผใƒซใƒ™ใƒผใ‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฏใ€ๆ–‡ใ‚’ๅ˜่ชžใซๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใ‚’ใ‚†ใ‚‹ใ‚„ใ‹ใซๅฎš็พฉใ•ใ‚Œใ‚‹ๅ˜่ชžใƒˆใƒผใ‚ฏใƒณๅŒ–ใฎไพ‹ใงใ™ใ€‚ใƒ†ใ‚ญใ‚นใƒˆใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใƒใƒฃใƒณใ‚ฏใซๅˆ†ๅ‰ฒใ™ใ‚‹ใŸใ‚ใฎๆœ€ใ‚‚็›ดๆ„Ÿ็š„ใชๆ–นๆณ•ใงใ‚ใ‚‹ไธ€ๆ–นใ€ใ“ใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ๆ–นๆณ•ใฏๅคง่ฆๆจกใชใƒ†ใ‚ญใ‚นใƒˆใ‚ณใƒผใƒ‘ใ‚นใซๅฏพใ—ใฆๅ•้กŒใ‚’ๅผ•ใ่ตทใ“ใ™ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใฎๅ ดๅˆใ€็ฉบ็™ฝใจๅฅ่ชญ็‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฏ้€šๅธธใ€้žๅธธใซๅคงใใช่ชžๅฝ™๏ผˆใ™ในใฆใฎไธ€ๆ„ใชๅ˜่ชžใจใƒˆใƒผใ‚ฏใƒณใฎใ‚ปใƒƒใƒˆ๏ผ‰ใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€[Transformer XL](model_doc/transformerxl)ใฏ็ฉบ็™ฝใจๅฅ่ชญ็‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’ไฝฟ็”จใ—ใฆใŠใ‚Šใ€่ชžๅฝ™ใ‚ตใ‚คใ‚บใฏ267,735ใงใ™๏ผ + +ใ“ใฎใ‚ˆใ†ใชๅคงใใช่ชžๅฝ™ใ‚ตใ‚คใ‚บใฏใ€ใƒขใƒ‡ใƒซใซ้žๅธธใซๅคงใใชๅŸ‹ใ‚่พผใฟ่กŒๅˆ—ใ‚’ๅ…ฅๅŠ›ใŠใ‚ˆใณๅ‡บๅŠ›ใƒฌใ‚คใƒคใƒผใจใ—ใฆๆŒใŸใ›ใ‚‹ใ“ใจใ‚’ๅผทๅˆถใ—ใ€ใƒกใƒขใƒชใŠใ‚ˆใณๆ™‚้–“ใฎ่ค‡้›‘ใ•ใฎๅข—ๅŠ ใ‚’ๅผ•ใ่ตทใ“ใ—ใพใ™ใ€‚ไธ€่ˆฌ็š„ใซใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใฏใ€็‰นใซๅ˜ไธ€ใฎ่จ€่ชžใงไบ‹ๅ‰ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸๅ ดๅˆใ€50,000ใ‚’่ถ…ใˆใ‚‹่ชžๅฝ™ใ‚ตใ‚คใ‚บใ‚’ๆŒใคใ“ใจใฏใปใจใ‚“ใฉใ‚ใ‚Šใพใ›ใ‚“ใ€‚ + +ใ—ใŸใŒใฃใฆใ€ใ‚ทใƒณใƒ—ใƒซใช็ฉบ็™ฝใจๅฅ่ชญ็‚นใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใŒไธๅๅˆ†ใชๅ ดๅˆใ€ใชใœๅ˜ใซๆ–‡ๅญ—ๅ˜ไฝใงใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใชใ„ใฎใ‹ใจใ„ใ†็–‘ๅ•ใŒ็”Ÿใ˜ใพใ™ใ‹๏ผŸ + + + +ๆ–‡ๅญ—ๅ˜ไฝใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฏ้žๅธธใซใ‚ทใƒณใƒ—ใƒซใงใ‚ใ‚Šใ€ใƒกใƒขใƒชใจๆ™‚้–“ใฎ่ค‡้›‘ใ•ใ‚’ๅคงๅน…ใซๅ‰Šๆธ›ใงใใพใ™ใŒใ€ใƒขใƒ‡ใƒซใซๆ„ๅ‘ณใฎใ‚ใ‚‹ๅ…ฅๅŠ›่กจ็พใ‚’ๅญฆ็ฟ’ใ•ใ›ใ‚‹ใ“ใจใŒ้žๅธธใซ้›ฃใ—ใใชใ‚Šใพใ™ใ€‚ใŸใจใˆใฐใ€ๆ–‡ๅญ—ใ€Œ"t"ใ€ใฎใŸใ‚ใฎๆ„ๅ‘ณใฎใ‚ใ‚‹ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ็‹ฌ็ซ‹ใฎ่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ“ใจใฏใ€ๅ˜่ชžใ€Œ"today"ใ€ใฎใŸใ‚ใฎใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ็‹ฌ็ซ‹ใฎ่กจ็พใ‚’ๅญฆ็ฟ’ใ™ใ‚‹ใ‚ˆใ‚Šใ‚‚ใฏใ‚‹ใ‹ใซ้›ฃใ—ใ„ใงใ™ใ€‚ใใฎใŸใ‚ใ€ๆ–‡ๅญ—ๅ˜ไฝใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฏใ—ใฐใ—ใฐใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใฎไฝŽไธ‹ใ‚’ไผดใ„ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใฏๅ˜่ชžใƒฌใƒ™ใƒซใจๆ–‡ๅญ—ใƒฌใƒ™ใƒซใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใฎใƒใ‚คใƒ–ใƒชใƒƒใƒ‰ใงใ‚ใ‚‹**ใ‚ตใƒ–ใƒฏใƒผใƒ‰**ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’ไฝฟ็”จใ—ใฆใ€ไธกๆ–นใฎไธ–็•Œใฎๅˆฉ็‚นใ‚’ๆดปใ‹ใ—ใพใ™ใ€‚ + +## Subword tokenization + + + +ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏใ€้ ป็นใซไฝฟ็”จใ•ใ‚Œใ‚‹ๅ˜่ชžใ‚’ใ‚ˆใ‚Šๅฐใ•ใชใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†ๅ‰ฒใ™ในใใงใฏใชใ„ใŒใ€็ใ—ใ„ๅ˜่ชžใฏๆ„ๅ‘ณใฎใ‚ใ‚‹ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†่งฃใ•ใ‚Œใ‚‹ใจใ„ใ†ๅŽŸๅ‰‡ใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚ใŸใจใˆใฐใ€ใ€Œ"annoyingly"ใ€ใฏ็ใ—ใ„ๅ˜่ชžใจ่ฆ‹ใชใ•ใ‚Œใ€ใใฎๅ˜่ชžใฏใ€Œ"annoying"ใ€ใจใ€Œ"ly"ใ€ใซๅˆ†่งฃใ•ใ‚Œใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚็‹ฌ็ซ‹ใ—ใŸใ€Œ"annoying"ใ€ใจใ€Œ"ly"ใ€ใฏใ‚ˆใ‚Š้ ป็นใซ็พใ‚Œใพใ™ใŒใ€ใ€Œ"annoyingly"ใ€ใฎๆ„ๅ‘ณใฏใ€Œ"annoying"ใ€ใจใ€Œ"ly"ใ€ใฎๅˆๆˆ็š„ใชๆ„ๅ‘ณใซใ‚ˆใฃใฆไฟๆŒใ•ใ‚Œใพใ™ใ€‚ใ“ใ‚Œใฏ็‰นใซใƒˆใƒซใ‚ณ่ชžใชใฉใฎ็ตๅˆ่จ€่ชžใงๅฝน็ซ‹ใกใพใ™ใ€‚ใ“ใ“ใงใฏใ‚ตใƒ–ใƒฏใƒผใƒ‰ใ‚’้€ฃ็ตใ—ใฆ๏ผˆใปใผ๏ผ‰ไปปๆ„ใฎ้•ทใ„่ค‡้›‘ใชๅ˜่ชžใ‚’ๅฝขๆˆใงใใพใ™ใ€‚ + +ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒณๅŒ–ใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฏๅˆ็†็š„ใช่ชžๅฝ™ใ‚ตใ‚คใ‚บใ‚’ๆŒใคใ“ใจใŒใงใใ€ๆ„ๅ‘ณใฎใ‚ใ‚‹ใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆ็‹ฌ็ซ‹ใฎ่กจ็พใ‚’ๅญฆ็ฟ’ใงใใพใ™ใ€‚ใ•ใ‚‰ใซใ€ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒณๅŒ–ใซใ‚ˆใ‚Šใ€ใƒขใƒ‡ใƒซใฏไปฅๅ‰ใซ่ฆ‹ใŸใ“ใจใฎใชใ„ๅ˜่ชžใ‚’ๅ‡ฆ็†ใ—ใ€ใใ‚Œใ‚‰ใ‚’ๆ—ข็Ÿฅใฎใ‚ตใƒ–ใƒฏใƒผใƒ‰ใซๅˆ†่งฃใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ไพ‹ใˆใฐใ€[`~transformers.BertTokenizer`]ใฏ`"I have a new GPU!"`ใ‚’ไปฅไธ‹ใฎใ‚ˆใ†ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™๏ผš + + +```py +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +>>> tokenizer.tokenize("I have a new GPU!") +["i", "have", "a", "new", "gp", "##u", "!"] +``` + +ใ€Œuncasedใ€ใƒขใƒ‡ใƒซใ‚’่€ƒๆ…ฎใ—ใฆใ„ใ‚‹ใŸใ‚ใ€ใพใšๆ–‡ใ‚’ๅฐๆ–‡ๅญ—ใซๅค‰ๆ›ใ—ใพใ—ใŸใ€‚ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎ่ชžๅฝ™ใซใ€Œ["i", "have", "a", "new"]ใ€ใจใ„ใ†ๅ˜่ชžใŒๅญ˜ๅœจใ™ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™ใŒใ€ใ€Œ"gpu"ใ€ใจใ„ใ†ๅ˜่ชžใฏๅญ˜ๅœจใ—ใพใ›ใ‚“ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€Œ"gpu"ใ€ใ‚’ๆ—ข็Ÿฅใฎใ‚ตใƒ–ใƒฏใƒผใƒ‰ใ€Œ["gp"ใ€"##u"]ใ€ใซๅˆ†ๅ‰ฒใ—ใพใ™ใ€‚ใ“ใ“ใงใ€Œ"##"ใ€ใฏใ€ใƒˆใƒผใ‚ฏใƒณใฎใƒ‡ใ‚ณใƒผใƒ‰ใพใŸใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใฎ้€†่ปขใฎใŸใ‚ใซใ€ใƒˆใƒผใ‚ฏใƒณใฎๅ‰ใฎ้ƒจๅˆ†ใซใ‚นใƒšใƒผใ‚นใชใ—ใงๆŽฅ็ถšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + +ๅˆฅใฎไพ‹ใจใ—ใฆใ€[`~transformers.XLNetTokenizer`]ใฏไปฅไธ‹ใฎใ‚ˆใ†ใซไปฅๅ‰ใฎใ‚ตใƒณใƒ—ใƒซใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™๏ผš + +```py +>>> from transformers import XLNetTokenizer + +>>> tokenizer = XLNetTokenizer.from_pretrained("xlnet/xlnet-base-cased") +>>> tokenizer.tokenize("Don't you love ๐Ÿค— Transformers? We sure do.") +["โ–Don", "'", "t", "โ–you", "โ–love", "โ–", "๐Ÿค—", "โ–", "Transform", "ers", "?", "โ–We", "โ–sure", "โ–do", "."] +``` + +ใ“ใ‚Œใ‚‰ใฎใ€Œโ–ใ€ใฎๆ„ๅ‘ณใซใคใ„ใฆใฏใ€[SentencePiece](#sentencepiece)ใ‚’่ฆ‹ใ‚‹ใจใใซ่ฉณใ—ใ่ชฌๆ˜Žใ—ใพใ™ใ€‚ใ”่ฆงใฎ้€šใ‚Šใ€ใ€ŒTransformersใ€ใจใ„ใ†็ใ—ใ„ๅ˜่ชžใฏใ€ใ‚ˆใ‚Š้ ป็นใซ็พใ‚Œใ‚‹ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใ€ŒTransformใ€ใจใ€Œersใ€ใซๅˆ†ๅ‰ฒใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +ใ•ใฆใ€็•ฐใชใ‚‹ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใŒใฉใฎใ‚ˆใ†ใซๅ‹•ไฝœใ™ใ‚‹ใ‹ใ‚’่ฆ‹ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏใ™ในใฆใ€้€šๅธธใฏๅฏพๅฟœใ™ใ‚‹ใƒขใƒ‡ใƒซใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚Œใ‚‹ใ‚ณใƒผใƒ‘ใ‚นใง่กŒใ‚ใ‚Œใ‚‹ๅฝขๅผใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚ + + + +### Byte-Pair Encoding๏ผˆBPE๏ผ‰ + +Byte-Pair Encoding๏ผˆBPE๏ผ‰ใฏใ€[Neural Machine Translation of Rare Words with Subword Units๏ผˆSennrich et al., 2015๏ผ‰](https://huggingface.co/papers/1508.07909)ใงๅฐŽๅ…ฅใ•ใ‚Œใพใ—ใŸใ€‚BPEใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใ‚’ๅ˜่ชžใซๅˆ†ๅ‰ฒใ™ใ‚‹ใƒ—ใƒชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใซไพๅญ˜ใ—ใฆใ„ใพใ™ใ€‚ใƒ—ใƒชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใฏใ€็ฉบ็™ฝใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใชใฉใ€้žๅธธใซๅ˜็ด”ใชใ‚‚ใฎใงใ‚ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใˆใฐใ€[GPT-2](model_doc/gpt2)ใ€[RoBERTa](model_doc/roberta)ใงใ™ใ€‚ใ‚ˆใ‚Š้ซ˜ๅบฆใชใƒ—ใƒชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใซใฏใ€ใƒซใƒผใƒซใƒ™ใƒผใ‚นใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณ๏ผˆ[XLM](model_doc/xlm)ใ€[FlauBERT](model_doc/flaubert)ใชใฉใŒๅคง้ƒจๅˆ†ใฎ่จ€่ชžใซMosesใ‚’ไฝฟ็”จ๏ผ‰ใ‚„ใ€[GPT](model_doc/gpt)๏ผˆSpacyใจftfyใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใƒผใƒ‘ใ‚นๅ†…ใฎๅ„ๅ˜่ชžใฎ้ ปๅบฆใ‚’ๆ•ฐใˆใ‚‹๏ผ‰ใชใฉใŒๅซใพใ‚Œใพใ™ใ€‚ + +ใƒ—ใƒชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใฎๅพŒใ€ไธ€ๆ„ใฎๅ˜่ชžใ‚ปใƒƒใƒˆใŒไฝœๆˆใ•ใ‚Œใ€ๅ„ๅ˜่ชžใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใงๅ‡บ็พใ—ใŸ้ ปๅบฆใŒๆฑบๅฎšใ•ใ‚Œใพใ™ใ€‚ๆฌกใซใ€BPEใฏใƒ™ใƒผใ‚น่ชžๅฝ™ใ‚’ไฝœๆˆใ—ใ€ใƒ™ใƒผใ‚น่ชžๅฝ™ใฎไบŒใคใฎใ‚ทใƒณใƒœใƒซใ‹ใ‚‰ๆ–ฐใ—ใ„ใ‚ทใƒณใƒœใƒซใ‚’ๅฝขๆˆใ™ใ‚‹ใŸใ‚ใฎใƒžใƒผใ‚ธใƒซใƒผใƒซใ‚’ๅญฆ็ฟ’ใ—ใพใ™ใ€‚ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏใ€่ชžๅฝ™ใŒๆ‰€ๆœ›ใฎ่ชžๅฝ™ใ‚ตใ‚คใ‚บใซ้”ใ™ใ‚‹ใพใง็ถšใ‘ใ‚‰ใ‚Œใพใ™ใ€‚ใชใŠใ€ๆ‰€ๆœ›ใฎ่ชžๅฝ™ใ‚ตใ‚คใ‚บใฏใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซๅฎš็พฉใ™ใ‚‹ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใ‚ใ‚‹ใ“ใจใซๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ + +ไพ‹ใจใ—ใฆใ€ใƒ—ใƒชใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใฎๅพŒใ€ๆฌกใฎใ‚ปใƒƒใƒˆใฎๅ˜่ชžใจใใฎๅ‡บ็พ้ ปๅบฆใŒๆฑบๅฎšใ•ใ‚ŒใŸใจไปฎๅฎšใ—ใพใ—ใ‚‡ใ†๏ผš + + +``` +("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) +``` + +ใ—ใŸใŒใฃใฆใ€ใƒ™ใƒผใ‚น่ชžๅฝ™ใฏใ€Œ["b", "g", "h", "n", "p", "s", "u"]ใ€ใงใ™ใ€‚ใ™ในใฆใฎๅ˜่ชžใ‚’ใƒ™ใƒผใ‚น่ชžๅฝ™ใฎใ‚ทใƒณใƒœใƒซใซๅˆ†ๅ‰ฒใ™ใ‚‹ใจใ€ๆฌกใฎใ‚ˆใ†ใซใชใ‚Šใพใ™๏ผš + + +``` +("h" "u" "g", 10), ("p" "u" "g", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "u" "g" "s", 5) +``` + +ใใฎๅพŒใ€BPEใฏๅฏ่ƒฝใชใ™ในใฆใฎใ‚ทใƒณใƒœใƒซใƒšใ‚ขใฎ้ ปๅบฆใ‚’ๆ•ฐใˆใ€ๆœ€ใ‚‚้ ป็นใซ็™บ็”Ÿใ™ใ‚‹ใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚’้ธๆŠžใ—ใพใ™ใ€‚ไธŠ่จ˜ใฎไพ‹ใงใฏใ€`"h"`ใฎๅพŒใซ`"u"`ใŒ15ๅ›ž๏ผˆ`"hug"`ใฎ10ๅ›žใ€`"hugs"`ใฎ5ๅ›ž๏ผ‰ๅ‡บ็พใ—ใพใ™ใ€‚ใ—ใ‹ใ—ใ€ๆœ€ใ‚‚้ ป็นใชใ‚ทใƒณใƒœใƒซใƒšใ‚ขใฏใ€ๅˆ่จˆใง20ๅ›ž๏ผˆ`"u"`ใฎ10ๅ›žใ€`"g"`ใฎ5ๅ›žใ€`"u"`ใฎ5ๅ›ž๏ผ‰ๅ‡บ็พใ™ใ‚‹`"u"`ใฎๅพŒใซ`"g"`ใŒ็ถšใใ‚ทใƒณใƒœใƒซใƒšใ‚ขใงใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒๆœ€ๅˆใซๅญฆ็ฟ’ใ™ใ‚‹ใƒžใƒผใ‚ธใƒซใƒผใƒซใฏใ€`"u"`ใฎๅพŒใซ`"g"`ใŒ็ถšใใ™ในใฆใฎ`"u"`ใ‚ทใƒณใƒœใƒซใ‚’ไธ€็ท’ใซใ‚ฐใƒซใƒผใƒ—ๅŒ–ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ๆฌกใซใ€`"ug"`ใŒ่ชžๅฝ™ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๅ˜่ชžใฎใ‚ปใƒƒใƒˆใฏๆฌกใซใชใ‚Šใพใ™๏ผš + + +``` +("h" "ug", 10), ("p" "ug", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "ug" "s", 5) +``` + +ๆฌกใซใ€BPEใฏๆฌกใซๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚’่ญ˜ๅˆฅใ—ใพใ™ใ€‚ใใ‚Œใฏใ€Œ"u"ใ€ใซ็ถšใ„ใฆใ€Œ"n"ใ€ใงใ€16ๅ›žๅ‡บ็พใ—ใพใ™ใ€‚ใ—ใŸใŒใฃใฆใ€ใ€Œ"u"ใ€ใจใ€Œ"n"ใ€ใฏใ€Œ"un"ใ€ใซ็ตๅˆใ•ใ‚Œใ€่ชžๅฝ™ใซ่ฟฝๅŠ ใ•ใ‚Œใพใ™ใ€‚ๆฌกใซๆœ€ใ‚‚้ ปๅบฆใฎ้ซ˜ใ„ใ‚ทใƒณใƒœใƒซใƒšใ‚ขใฏใ€ใ€Œ"h"ใ€ใซ็ถšใ„ใฆใ€Œ"ug"ใ€ใงใ€15ๅ›žๅ‡บ็พใ—ใพใ™ใ€‚ๅ†ใณใƒšใ‚ขใŒ็ตๅˆใ•ใ‚Œใ€ใ€Œhugใ€ใŒ่ชžๅฝ™ใซ่ฟฝๅŠ ใงใใพใ™ใ€‚ + +ใ“ใฎๆฎต้šŽใงใฏใ€่ชžๅฝ™ใฏ`["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"]`ใงใ‚ใ‚Šใ€ไธ€ๆ„ใฎๅ˜่ชžใฎใ‚ปใƒƒใƒˆใฏไปฅไธ‹ใฎใ‚ˆใ†ใซ่กจใ•ใ‚Œใพใ™๏ผš + + +``` +("hug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("hug" "s", 5) +``` + +ๅ‰ๆใจใ—ใฆใ€Byte-Pair Encoding๏ผˆBPE๏ผ‰ใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŒใ“ใฎๆฎต้šŽใงๅœๆญขใ™ใ‚‹ใจใ€ๅญฆ็ฟ’ใ•ใ‚ŒใŸใƒžใƒผใ‚ธใƒซใƒผใƒซใŒๆ–ฐใ—ใ„ๅ˜่ชžใซ้ฉ็”จใ•ใ‚Œใพใ™๏ผˆๆ–ฐใ—ใ„ๅ˜่ชžใซใฏใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใซๅซใพใ‚Œใฆใ„ใชใ„ใ‚ทใƒณใƒœใƒซใŒๅซใพใ‚Œใฆใ„ใชใ„้™ใ‚Š๏ผ‰ใ€‚ ไพ‹ใˆใฐใ€ๅ˜่ชž "bug" ใฏ ["b", "ug"] ใจใ—ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใพใ™ใŒใ€"mug" ใฏใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใซ "m" ใ‚ทใƒณใƒœใƒซใŒๅซใพใ‚Œใฆใ„ใชใ„ใŸใ‚ใ€["", "ug"] ใจใ—ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚Œใพใ™ใ€‚ ไธ€่ˆฌ็š„ใซใ€"m" ใฎใ‚ˆใ†ใชๅ˜ไธ€ใฎๆ–‡ๅญ—ใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใซใฏ้€šๅธธใ€ๅ„ๆ–‡ๅญ—ใฎๅฐ‘ใชใใจใ‚‚1ใคใฎๅ‡บ็พใŒๅซใพใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€"" ใ‚ทใƒณใƒœใƒซใซ็ฝฎใๆ›ใˆใ‚‰ใ‚Œใ‚‹ใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใŒใ€็ตตๆ–‡ๅญ—ใฎใ‚ˆใ†ใช้žๅธธใซ็‰นๆฎŠใชๆ–‡ๅญ—ใฎๅ ดๅˆใซใฏ็™บ็”Ÿใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +ๅ‰่ฟฐใฎใ‚ˆใ†ใซใ€ใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚ตใ‚คใ‚บใ€ใ™ใชใ‚ใกใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚ตใ‚คใ‚บ + ใƒžใƒผใ‚ธใฎๅ›žๆ•ฐใฏ้ธๆŠžใ™ใ‚‹ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใงใ™ใ€‚ ไพ‹ใˆใฐใ€[GPT](model_doc/gpt) ใฏใƒ™ใƒผใ‚นๆ–‡ๅญ—ใŒ478ๆ–‡ๅญ—ใงใ€40,000ๅ›žใฎใƒžใƒผใ‚ธๅพŒใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๅœๆญขใ—ใŸใŸใ‚ใ€ใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚ตใ‚คใ‚บใฏ40,478ใงใ™ใ€‚ + +#### Byte-level BPE + +ใ™ในใฆใฎUnicodeๆ–‡ๅญ—ใ‚’ใƒ™ใƒผใ‚นๆ–‡ๅญ—ใจ่€ƒใˆใ‚‹ใจใ€ใ™ในใฆใฎๅฏ่ƒฝใชใƒ™ใƒผใ‚นๆ–‡ๅญ—ใŒๅซใพใ‚Œใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใชใ„ใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใฏใ‹ใชใ‚Šๅคงใใใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ [GPT-2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) ใฏใ€ใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚’256ใƒใ‚คใƒˆใซใ™ใ‚‹่ณขใ„ใƒˆใƒชใƒƒใ‚ฏใจใ—ใฆใƒใ‚คใƒˆใ‚’ใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใจใ—ใฆไฝฟ็”จใ—ใ€ใ™ในใฆใฎใƒ™ใƒผใ‚นๆ–‡ๅญ—ใŒใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใซๅซใพใ‚Œใ‚‹ใ‚ˆใ†ใซใ—ใฆใ„ใพใ™ใ€‚ ใƒ‘ใƒณใ‚ฏใƒใƒฅใ‚จใƒผใ‚ทใƒงใƒณใ‚’ๆ‰ฑใ†ใŸใ‚ใฎใ„ใใคใ‹ใฎ่ฟฝๅŠ ใƒซใƒผใƒซใ‚’ๅ‚™ใˆใŸGPT2ใฎใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ ใ‚ทใƒณใƒœใƒซใ‚’ๅฟ…่ฆใจใ›ใšใซใ™ในใฆใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใพใ™ใ€‚ [GPT-2](model_doc/gpt) ใฏ50,257ใฎใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚ตใ‚คใ‚บใ‚’ๆŒใฃใฆใŠใ‚Šใ€ใ“ใ‚Œใฏ256ใƒใ‚คใƒˆใฎใƒ™ใƒผใ‚นใƒˆใƒผใ‚ฏใƒณใ€็‰นๅˆฅใชใƒ†ใ‚ญใ‚นใƒˆใฎ็ต‚ไบ†ใ‚’็คบใ™ใƒˆใƒผใ‚ฏใƒณใ€ใŠใ‚ˆใณ50,000ๅ›žใฎใƒžใƒผใ‚ธใงๅญฆ็ฟ’ใ—ใŸใ‚ทใƒณใƒœใƒซใซๅฏพๅฟœใ—ใฆใ„ใพใ™ใ€‚ + +### WordPiece + +WordPieceใฏใ€[BERT](model_doc/bert)ใ€[DistilBERT](model_doc/distilbert)ใ€ใŠใ‚ˆใณ[Electra](model_doc/electra)ใงไฝฟ็”จใ•ใ‚Œใ‚‹ใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใ™ใ€‚ ใ“ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏใ€[Japanese and Korean Voice Search (Schuster et al., 2012)](https://static.googleusercontent.com/media/research.google.com/ja//pubs/archive/37842.pdf) ใงๆฆ‚่ชฌใ•ใ‚ŒใฆใŠใ‚Šใ€BPEใซ้žๅธธใซไผผใฆใ„ใพใ™ใ€‚ WordPieceใฏๆœ€ใ‚‚้ ป็นใชใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚’้ธๆŠžใ™ใ‚‹ใฎใงใฏใชใใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใซ่ฟฝๅŠ ใ—ใŸๅ ดๅˆใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใฎๅฐคๅบฆใ‚’ๆœ€ๅคงๅŒ–ใ™ใ‚‹ใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚’้ธๆŠžใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใฏๅ…ทไฝ“็š„ใซใฏใฉใ†ใ„ใ†ๆ„ๅ‘ณใงใ™ใ‹๏ผŸๅ‰ใฎไพ‹ใ‚’ๅ‚็…งใ™ใ‚‹ใจใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใฎๅฐคๅบฆใ‚’ๆœ€ๅคงๅŒ–ใ™ใ‚‹ใ“ใจใฏใ€ใใฎใ‚ทใƒณใƒœใƒซใƒšใ‚ขใฎ็ขบ็އใ‚’ใใฎๆœ€ๅˆใฎใ‚ทใƒณใƒœใƒซใซ็ถšใ2็•ช็›ฎใฎใ‚ทใƒณใƒœใƒซใฎ็ขบ็އใงๅ‰ฒใฃใŸใ‚‚ใฎใŒใ€ใ™ในใฆใฎใ‚ทใƒณใƒœใƒซใƒšใ‚ขใฎไธญใงๆœ€ใ‚‚ๅคงใใ„ๅ ดๅˆใซ่ฉฒๅฝ“ใ™ใ‚‹ใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใซ็ญ‰ใ—ใ„ใงใ™ใ€‚ ใŸใจใˆใฐใ€"u" ใฎๅพŒใซ "g" ใŒ็ถšใๅ ดๅˆใ€ไป–ใฎใฉใฎใ‚ทใƒณใƒœใƒซใƒšใ‚ขใ‚ˆใ‚Šใ‚‚ "ug" ใฎ็ขบ็އใ‚’ "u"ใ€"g" ใงๅ‰ฒใฃใŸ็ขบ็އใŒ้ซ˜ใ‘ใ‚Œใฐใ€ใใ‚Œใ‚‰ใฎใ‚ทใƒณใƒœใƒซใฏ็ตๅˆใ•ใ‚Œใพใ™ใ€‚็›ดๆ„Ÿ็š„ใซ่จ€ใˆใฐใ€WordPieceใฏ2ใคใฎใ‚ทใƒณใƒœใƒซใ‚’็ตๅˆใ™ใ‚‹ใ“ใจใซใ‚ˆใฃใฆๅคฑใ‚ใ‚Œใ‚‹ใ‚‚ใฎใ‚’่ฉ•ไพกใ—ใ€ใใ‚ŒใŒใใ‚Œใซๅ€คใ™ใ‚‹ใ‹ใฉใ†ใ‹ใ‚’็ขบ่ชใ™ใ‚‹็‚นใงBPEใจใฏใ‚ใšใ‹ใซ็•ฐใชใ‚Šใพใ™ใ€‚ + +### Unigram + +Unigramใฏใ€[Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates (Kudo, 2018)](https://huggingface.co/papers/1804.10959) ใงๅฐŽๅ…ฅใ•ใ‚ŒใŸใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ผใƒผใ‚ทใƒงใƒณใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใงใ™ใ€‚ BPEใ‚„WordPieceใจใฏ็•ฐใชใ‚Šใ€Unigramใฏใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚’ๅคšๆ•ฐใฎใ‚ทใƒณใƒœใƒซใงๅˆๆœŸๅŒ–ใ—ใ€ๅ„ใ‚ทใƒณใƒœใƒซใ‚’ๅ‰Šๆธ›ใ—ใฆใ‚ˆใ‚Šๅฐใ•ใชใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‚’ๅ–ๅพ—ใ—ใพใ™ใ€‚ ใƒ™ใƒผใ‚นใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใฏใ€ไบ‹ๅ‰ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใ•ใ‚ŒใŸใ™ในใฆใฎๅ˜่ชžใจๆœ€ใ‚‚ไธ€่ˆฌ็š„ใช้ƒจๅˆ†ๆ–‡ๅญ—ๅˆ—ใซๅฏพๅฟœใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ Unigramใฏtransformersใฎใƒขใƒ‡ใƒซใฎ็›ดๆŽฅใฎไฝฟ็”จใซใฏ้ฉใ—ใฆใ„ใพใ›ใ‚“ใŒใ€[SentencePiece](#sentencepiece)ใจ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ๅ„ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใƒ†ใƒƒใƒ—ใงใ€Unigramใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏ็พๅœจใฎใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใจใƒฆใƒ‹ใ‚ฐใƒฉใƒ ่จ€่ชžใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟไธŠใฎๆๅคฑ๏ผˆ้€šๅธธใฏๅฏพๆ•ฐๅฐคๅบฆใจใ—ใฆๅฎš็พฉ๏ผ‰ใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚ใใฎๅพŒใ€ใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชๅ†…ใฎๅ„ใ‚ทใƒณใƒœใƒซใซใคใ„ใฆใ€ใใฎใ‚ทใƒณใƒœใƒซใŒใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใ‹ใ‚‰ๅ‰Š้™คใ•ใ‚ŒใŸๅ ดๅˆใซๅ…จไฝ“ใฎๆๅคฑใŒใฉใ‚Œใ ใ‘ๅข—ๅŠ ใ™ใ‚‹ใ‹ใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ Unigramใฏใ€ๆๅคฑใฎๅข—ๅŠ ใŒๆœ€ใ‚‚ไฝŽใ„p๏ผˆ้€šๅธธใฏ10๏ผ…ใพใŸใฏ20๏ผ…๏ผ‰ใƒ‘ใƒผใ‚ปใƒณใƒˆใฎใ‚ทใƒณใƒœใƒซใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ใคใพใ‚Šใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟๅ…จไฝ“ใฎๆๅคฑใซๆœ€ใ‚‚ๅฝฑ้Ÿฟใ‚’ไธŽใˆใชใ„ใ€ๆœ€ใ‚‚ๆๅคฑใฎๅฐ‘ใชใ„ใ‚ทใƒณใƒœใƒซใ‚’ๅ‰Š้™คใ—ใพใ™ใ€‚ ใ“ใฎใƒ—ใƒญใ‚ปใ‚นใฏใ€ใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใŒๆœ›ใพใ—ใ„ใ‚ตใ‚คใ‚บใซ้”ใ™ใ‚‹ใพใง็นฐใ‚Š่ฟ”ใ•ใ‚Œใพใ™ใ€‚ Unigramใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏๅธธใซใƒ™ใƒผใ‚นๆ–‡ๅญ—ใ‚’ไฟๆŒใ™ใ‚‹ใŸใ‚ใ€ไปปๆ„ใฎๅ˜่ชžใ‚’ใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใพใ™ใ€‚ + +Unigramใฏใƒžใƒผใ‚ธใƒซใƒผใƒซใซๅŸบใฅใ„ใฆใ„ใชใ„ใŸใ‚๏ผˆBPEใจWordPieceใจใฏๅฏพ็…ง็š„ใซ๏ผ‰ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใฎๆ–ฐใ—ใ„ใƒ†ใ‚ญใ‚นใƒˆใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใซใฏใ„ใใคใ‹ใฎๆ–นๆณ•ใŒใ‚ใ‚Šใพใ™ใ€‚ไพ‹ใจใ—ใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ•ใ‚ŒใŸUnigramใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒๆŒใคใƒœใ‚ญใƒฃใƒ–ใƒฉใƒชใŒๆฌกใฎใ‚ˆใ†ใชๅ ดๅˆ๏ผš + + +``` +["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"], +``` + +`"hugs"`ใฏใ€`["hug", "s"]`ใ€`["h", "ug", "s"]`ใ€ใพใŸใฏ`["h", "u", "g", "s"]`ใฎใ‚ˆใ†ใซใƒˆใƒผใ‚ฏใƒณๅŒ–ใงใใพใ™ใ€‚ใงใฏใ€ใฉใ‚Œใ‚’้ธๆŠžใ™ในใใงใ—ใ‚‡ใ†ใ‹๏ผŸ Unigramใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ณใƒผใƒ‘ใ‚นๅ†…ใฎๅ„ใƒˆใƒผใ‚ฏใƒณใฎ็ขบ็އใ‚’ไฟๅญ˜ใ—ใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅพŒใซๅ„ๅฏ่ƒฝใชใƒˆใƒผใ‚ฏใƒณๅŒ–ใฎ็ขบ็އใ‚’่จˆ็ฎ—ใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ใ“ใฎใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใฏๅฎŸ้š›ใซใฏๆœ€ใ‚‚ๅฏ่ƒฝๆ€งใฎ้ซ˜ใ„ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’้ธๆŠžใ—ใพใ™ใŒใ€็ขบ็އใซๅพ“ใฃใฆๅฏ่ƒฝใชใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚’ใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใ™ใ‚‹ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚‚ๆไพ›ใ—ใพใ™ใ€‚ + +ใ“ใ‚Œใ‚‰ใฎ็ขบ็އใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใซไฝฟ็”จใ™ใ‚‹ๆๅคฑใซใ‚ˆใฃใฆๅฎš็พฉใ•ใ‚Œใพใ™ใ€‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใŒๅ˜่ชž \\(x_{1}, \dots, x_{N}\\) ใงๆง‹ๆˆใ•ใ‚Œใ€ๅ˜่ชž \\(x_{i}\\) ใฎใ™ในใฆใฎๅฏ่ƒฝใชใƒˆใƒผใ‚ฏใƒณๅŒ–ใฎใ‚ปใƒƒใƒˆใŒ \\(S(x_{i})\\) ใจๅฎš็พฉใ•ใ‚Œใ‚‹ๅ ดๅˆใ€ๅ…จไฝ“ใฎๆๅคฑใฏๆฌกใฎใ‚ˆใ†ใซๅฎš็พฉใ•ใ‚Œใพใ™ใ€‚ + +$$\mathcal{L} = -\sum_{i=1}^{N} \log \left ( \sum_{x \in S(x_{i})} p(x) \right )$$ + + + +### SentencePiece + +ใ“ใ‚Œใพใงใซ่ชฌๆ˜Žใ—ใŸใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณๅŒ–ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใซใฏๅŒใ˜ๅ•้กŒใŒใ‚ใ‚Šใพใ™ใ€‚ใใ‚Œใฏใ€ๅ…ฅๅŠ›ใƒ†ใ‚ญใ‚นใƒˆใŒๅ˜่ชžใ‚’ๅŒบๅˆ‡ใ‚‹ใŸใ‚ใซใ‚นใƒšใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใจไปฎๅฎšใ—ใฆใ„ใ‚‹ใจใ„ใ†ใ“ใจใงใ™ใ€‚ใ—ใ‹ใ—ใ€ใ™ในใฆใฎ่จ€่ชžใŒๅ˜่ชžใ‚’ๅŒบๅˆ‡ใ‚‹ใŸใ‚ใซใ‚นใƒšใƒผใ‚นใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ใ‚ใ‘ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ใ“ใฎๅ•้กŒใ‚’ไธ€่ˆฌ็š„ใซ่งฃๆฑบใ™ใ‚‹ใŸใ‚ใฎ1ใคใฎๆ–นๆณ•ใฏใ€่จ€่ชžๅ›บๆœ‰ใฎๅ‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใงใ™๏ผˆไพ‹๏ผš[XLM](model_doc/xlm)ใฏ็‰นๅฎšใฎไธญๅ›ฝ่ชžใ€ๆ—ฅๆœฌ่ชžใ€ใŠใ‚ˆใณใ‚ฟใ‚ค่ชžใฎๅ‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ไฝฟ็”จใ—ใฆใ„ใพใ™๏ผ‰ใ€‚ใ‚ˆใ‚Šไธ€่ˆฌ็š„ใซใ“ใฎๅ•้กŒใ‚’่งฃๆฑบใ™ใ‚‹ใŸใ‚ใซใ€[SentencePiece๏ผšใƒ‹ใƒฅใƒผใƒฉใƒซใƒ†ใ‚ญใ‚นใƒˆๅ‡ฆ็†ใฎใŸใ‚ใฎใ‚ทใƒณใƒ—ใƒซใง่จ€่ชž้žไพๅญ˜ใฎใ‚ตใƒ–ใƒฏใƒผใƒ‰ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใŠใ‚ˆใณใƒ‡ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผ๏ผˆKudo et al.ใ€2018๏ผ‰](https://huggingface.co/papers/1808.06226) ใฏใ€ๅ…ฅๅŠ›ใ‚’็”Ÿใฎๅ…ฅๅŠ›ใ‚นใƒˆใƒชใƒผใƒ ใจใ—ใฆๆ‰ฑใ„ใ€ใ‚นใƒšใƒผใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–‡ๅญ—ใฎใ‚ปใƒƒใƒˆใซๅซใ‚ใพใ™ใ€‚ใใ‚Œใ‹ใ‚‰BPEใพใŸใฏunigramใ‚ขใƒซใ‚ดใƒชใ‚บใƒ ใ‚’ไฝฟ็”จใ—ใฆ้ฉๅˆ‡ใช่ชžๅฝ™ใ‚’ๆง‹็ฏ‰ใ—ใพใ™ใ€‚ + +ใŸใจใˆใฐใ€[`XLNetTokenizer`]ใฏSentencePieceใ‚’ไฝฟ็”จใ—ใฆใŠใ‚Šใ€ใใฎใŸใ‚ใซๅ‰่ฟฐใฎไพ‹ใง`"โ–"`ๆ–‡ๅญ—ใŒ่ชžๅฝ™ใซๅซใพใ‚Œใฆใ„ใพใ—ใŸใ€‚SentencePieceใ‚’ไฝฟ็”จใ—ใŸใƒ‡ใ‚ณใƒผใƒ‰ใฏ้žๅธธใซ็ฐกๅ˜ใงใ€ใ™ในใฆใฎใƒˆใƒผใ‚ฏใƒณใ‚’ๅ˜็ด”ใซ้€ฃ็ตใ—ใ€`"โ–"`ใฏใ‚นใƒšใƒผใ‚นใซ็ฝฎๆ›ใ•ใ‚Œใพใ™ใ€‚ + +ใƒฉใ‚คใƒ–ใƒฉใƒชๅ†…ใฎใ™ในใฆใฎtransformersใƒขใƒ‡ใƒซใฏใ€SentencePieceใ‚’unigramใจ็ต„ใฟๅˆใ‚ใ›ใฆไฝฟ็”จใ—ใพใ™ใ€‚SentencePieceใ‚’ไฝฟ็”จใ™ใ‚‹ใƒขใƒ‡ใƒซใฎไพ‹ใซใฏใ€[ALBERT](model_doc/albert)ใ€[XLNet](model_doc/xlnet)ใ€[Marian](model_doc/marian)ใ€ใŠใ‚ˆใณ[T5](model_doc/t5)ใŒใ‚ใ‚Šใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/torchscript.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/torchscript.md new file mode 100644 index 0000000000000000000000000000000000000000..27d64a625c8c4299e1be68f71541966604dc65c1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/torchscript.md @@ -0,0 +1,177 @@ + + +# Export to TorchScript + + + +ใ“ใ‚ŒใฏTorchScriptใ‚’ไฝฟ็”จใ—ใŸๅฎŸ้จ“ใฎๆœ€ๅˆใงใ‚ใ‚Šใ€ๅฏๅค‰ๅ…ฅๅŠ›ใ‚ตใ‚คใ‚บใฎใƒขใƒ‡ใƒซใซๅฏพใ™ใ‚‹ใใฎ่ƒฝๅŠ›ใ‚’ใพใ ๆŽขๆฑ‚ไธญใงใ™ใ€‚ใ“ใ‚Œใฏ็งใŸใกใฎ้–ขๅฟƒใฎ็„ฆ็‚นใงใ‚ใ‚Šใ€ไปŠๅพŒใฎใƒชใƒชใƒผใ‚นใงใฏใ€ใ‚ˆใ‚ŠๆŸ”่ปŸใชๅฎŸ่ฃ…ใ‚„ใ€Pythonใƒ™ใƒผใ‚นใฎใ‚ณใƒผใƒ‰ใจใ‚ณใƒณใƒ‘ใ‚คใƒซใ•ใ‚ŒใŸTorchScriptใ‚’ๆฏ”่ผƒใ™ใ‚‹ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅซใ‚€ใ€ใ‚ˆใ‚Šๅคšใใฎใ‚ณใƒผใƒ‰ไพ‹ใง่ฉณ็ดฐใชๅˆ†ๆžใ‚’่กŒใ„ใพใ™ใ€‚ + + + +[TorchScriptใฎใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ](https://pytorch.org/docs/stable/jit.html)ใซใ‚ˆใ‚Œใฐ๏ผš + +> TorchScriptใฏใ€PyTorchใ‚ณใƒผใƒ‰ใ‹ใ‚‰็›ดๅˆ—ๅŒ–ใŠใ‚ˆใณๆœ€้ฉๅŒ–ๅฏ่ƒฝใชใƒขใƒ‡ใƒซใ‚’ไฝœๆˆใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ + +TorchScriptใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€ๅŠน็އๅฟ—ๅ‘ใฎC++ใƒ—ใƒญใ‚ฐใƒฉใƒ ใชใฉใ€ไป–ใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใงใƒขใƒ‡ใƒซใ‚’ๅ†ๅˆฉ็”จใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚PyTorchใƒ™ใƒผใ‚นใฎPythonใƒ—ใƒญใ‚ฐใƒฉใƒ ไปฅๅค–ใฎ็’ฐๅขƒใง๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใฆไฝฟ็”จใ™ใ‚‹ใŸใ‚ใฎใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใ‚’ๆไพ›ใ—ใฆใ„ใพใ™ใ€‚ใ“ใ“ใงใฏใ€TorchScriptใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ—ใ€ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ™ใ€‚ + +ใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€ๆฌกใฎ2ใคใฎ่ฆไปถใŒใ‚ใ‚Šใพใ™๏ผš + +- `torchscript`ใƒ•ใƒฉใ‚ฐใ‚’ไฝฟ็”จใ—ใŸใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ– +- ใƒ€ใƒŸใƒผใฎๅ…ฅๅŠ›ใ‚’ไฝฟ็”จใ—ใŸใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚น + +ใ“ใ‚Œใ‚‰ใฎๅฟ…่ฆๆกไปถใฏใ€ไปฅไธ‹ใง่ฉณ็ดฐใซ่ชฌๆ˜Žใ•ใ‚Œใฆใ„ใ‚‹ใ‚ˆใ†ใซใ€้–‹็™บ่€…ใŒๆณจๆ„ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ„ใใคใ‹ใฎใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ + +## TorchScript flag and tied weights + +`torchscript`ใƒ•ใƒฉใ‚ฐใฏใ€ใปใจใ‚“ใฉใฎ๐Ÿค— Transformers่จ€่ชžใƒขใƒ‡ใƒซใซใŠใ„ใฆใ€`Embedding`ใƒฌใ‚คใƒคใƒผใจ`Decoding`ใƒฌใ‚คใƒคใƒผ้–“ใง้‡ใฟใŒ้€ฃ็ตใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ๅฟ…่ฆใงใ™ใ€‚ +TorchScriptใงใฏใ€้‡ใฟใŒ้€ฃ็ตใ•ใ‚Œใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใ“ใจใฏใงใใพใ›ใ‚“ใฎใงใ€ไบ‹ๅ‰ใซ้‡ใฟใ‚’ๅˆ‡ใ‚Š้›ขใ—ใฆ่ค‡่ฃฝใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ + +`torchscript`ใƒ•ใƒฉใ‚ฐใ‚’ไฝฟ็”จใ—ใฆใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฏใ€`Embedding`ใƒฌใ‚คใƒคใƒผใจ`Decoding`ใƒฌใ‚คใƒคใƒผใŒๅˆ†้›ขใ•ใ‚ŒใฆใŠใ‚Šใ€ใใฎใŸใ‚ๅพŒใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใฆใฏใ„ใ‘ใพใ›ใ‚“ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€ใ“ใ‚Œใ‚‰ใฎ2ใคใฎใƒฌใ‚คใƒคใƒผใ‚’้žๅŒๆœŸใซใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใ€ไบˆๆœŸใ—ใชใ„็ตๆžœใ‚’ใ‚‚ใŸใ‚‰ใ™ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใพใ™ใ€‚ + +่จ€่ชžใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใ‚’ๆŒใŸใชใ„ใƒขใƒ‡ใƒซใซใฏ่จ€ๅŠใ—ใพใ›ใ‚“ใŒใ€ใ“ใ‚Œใ‚‰ใฎใƒขใƒ‡ใƒซใซใฏ้€ฃ็ตใ•ใ‚ŒใŸ้‡ใฟใŒๅญ˜ๅœจใ—ใชใ„ใŸใ‚ใ€`torchscript`ใƒ•ใƒฉใ‚ฐใชใ—ใงๅฎ‰ๅ…จใซใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใงใใพใ™ใ€‚ + +## Dummy inputs and standard lengths + +ใƒ€ใƒŸใƒผๅ…ฅๅŠ›ใฏใƒขใƒ‡ใƒซใฎใƒ•ใ‚ฉใƒฏใƒผใƒ‰ใƒ‘ใ‚นใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ๅ…ฅๅŠ›ใฎๅ€คใฏใƒฌใ‚คใƒคใƒผใ‚’้€šใ˜ใฆไผๆ’ญใ•ใ‚Œใ‚‹้–“ใ€PyTorchใฏๅ„ใƒ†ใƒณใ‚ฝใƒซใซๅฎŸ่กŒใ•ใ‚ŒใŸ็•ฐใชใ‚‹ๆ“ไฝœใ‚’่ฟฝ่ทกใ—ใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎ่จ˜้Œฒใ•ใ‚ŒใŸๆ“ไฝœใฏใ€ใƒขใƒ‡ใƒซใฎ*ใƒˆใƒฌใƒผใ‚น*ใ‚’ไฝœๆˆใ™ใ‚‹ใŸใ‚ใซไฝฟ็”จใ•ใ‚Œใพใ™ใ€‚ + +ใƒˆใƒฌใƒผใ‚นใฏๅ…ฅๅŠ›ใฎๅฏธๆณ•ใซๅฏพใ—ใฆไฝœๆˆใ•ใ‚Œใพใ™ใ€‚ใใฎใŸใ‚ใ€ใƒ€ใƒŸใƒผๅ…ฅๅŠ›ใฎๅฏธๆณ•ใซๅˆถ็ด„ใ•ใ‚Œใ€ไป–ใฎใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚„ใƒใƒƒใƒใ‚ตใ‚คใ‚บใงใฏๅ‹•ไฝœใ—ใพใ›ใ‚“ใ€‚็•ฐใชใ‚‹ใ‚ตใ‚คใ‚บใง่ฉฆใ™ใจใ€ไปฅไธ‹ใฎใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใพใ™๏ผš + +``` +`The expanded size of the tensor (3) must match the existing size (7) at non-singleton dimension 2` +``` + +ใŠๅ‹งใ‚ใ—ใพใ™ใฎใฏใ€ใƒขใƒ‡ใƒซใฎๆŽจ่ซ–ไธญใซไพ›็ตฆใ•ใ‚Œใ‚‹ๆœ€ๅคงใฎๅ…ฅๅŠ›ใจๅŒใ˜ๅคงใใ•ใฎใƒ€ใƒŸใƒผๅ…ฅๅŠ›ใ‚ตใ‚คใ‚บใงใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใ‚นใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’ไฝฟ็”จใ—ใฆไธ่ถณๅ€คใ‚’่ฃœๅฎŒใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ใŸใ ใ—ใ€ใƒขใƒ‡ใƒซใŒใ‚ˆใ‚Šๅคงใใชๅ…ฅๅŠ›ใ‚ตใ‚คใ‚บใงใƒˆใƒฌใƒผใ‚นใ•ใ‚Œใ‚‹ใŸใ‚ใ€่กŒๅˆ—ใฎๅฏธๆณ•ใ‚‚ๅคงใใใชใ‚Šใ€ใ‚ˆใ‚Šๅคšใใฎ่จˆ็ฎ—ใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ + +็•ฐใชใ‚‹ใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใฎใƒขใƒ‡ใƒซใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹้š›ใซใ€ๅ„ๅ…ฅๅŠ›ใซๅฏพใ—ใฆๅฎŸ่กŒใ•ใ‚Œใ‚‹ๆผ”็ฎ—ใฎ็ทๆ•ฐใซๆณจๆ„ใ—ใฆใ€ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’ๅฏ†ๆŽฅใซใƒ•ใ‚ฉใƒญใƒผใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ + +## Using TorchScript in Python + +ใ“ใฎใ‚ปใ‚ฏใ‚ทใƒงใƒณใงใฏใ€ใƒขใƒ‡ใƒซใฎไฟๅญ˜ใจ่ชญใฟ่พผใฟใ€ใŠใ‚ˆใณๆŽจ่ซ–ใซใƒˆใƒฌใƒผใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๆ–นๆณ•ใ‚’็คบใ—ใพใ™ใ€‚ + +### Saving a model + +TorchScriptใง`BertModel`ใ‚’ใ‚จใ‚ฏใ‚นใƒใƒผใƒˆใ™ใ‚‹ใซใฏใ€`BertConfig`ใ‚ฏใƒฉใ‚นใ‹ใ‚‰`BertModel`ใ‚’ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅŒ–ใ—ใ€ใใ‚Œใ‚’ใƒ•ใ‚กใ‚คใƒซๅ`traced_bert.pt`ใงใƒ‡ใ‚ฃใ‚นใ‚ฏใซไฟๅญ˜ใ—ใพใ™๏ผš + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch + +enc = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") + +# Tokenizing input text +text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" +tokenized_text = enc.tokenize(text) + +# Masking one of the input tokens +masked_index = 8 +tokenized_text[masked_index] = "[MASK]" +indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) +segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] + +# Creating a dummy input +tokens_tensor = torch.tensor([indexed_tokens]) +segments_tensors = torch.tensor([segments_ids]) +dummy_input = [tokens_tensor, segments_tensors] + +# Initializing the model with the torchscript flag +# Flag set to True even though it is not necessary as this model does not have an LM Head. +config = BertConfig( + vocab_size_or_config_json_file=32000, + hidden_size=768, + num_hidden_layers=12, + num_attention_heads=12, + intermediate_size=3072, + torchscript=True, +) + +# Instantiating the model +model = BertModel(config) + +# The model needs to be in evaluation mode +model.eval() + +# If you are instantiating the model with *from_pretrained* you can also easily set the TorchScript flag +model = BertModel.from_pretrained("google-bert/bert-base-uncased", torchscript=True) + +# Creating the trace +traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) +torch.jit.save(traced_model, "traced_bert.pt") +``` + +### Loading a model + +ไปฅๅ‰ใซไฟๅญ˜ใ—ใŸ `BertModel`ใ€`traced_bert.pt` ใ‚’ใƒ‡ใ‚ฃใ‚นใ‚ฏใ‹ใ‚‰่ชญใฟ่พผใ‚“ใงใ€ไปฅๅ‰ใซๅˆๆœŸๅŒ–ใ—ใŸ `dummy_input` ใงไฝฟ็”จใงใใพใ™ใ€‚ + +```python +loaded_model = torch.jit.load("traced_bert.pt") +loaded_model.eval() + +all_encoder_layers, pooled_output = loaded_model(*dummy_input) +``` + + +### Using a traced model for inference + +ใƒˆใƒฌใƒผใ‚นใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ—ใฆๆŽจ่ซ–ใ‚’่กŒใ†ใซใฏใ€ใใฎ `__call__` ใƒ€ใƒณใƒ€ใƒผใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```python +traced_model(tokens_tensor, segments_tensors) +``` + + +## Deploy Hugging Face TorchScript models to AWS with the Neuron SDK + +AWSใฏใ‚ฏใƒฉใ‚ฆใƒ‰ใงใฎไฝŽใ‚ณใ‚นใƒˆใง้ซ˜ๆ€ง่ƒฝใชๆฉŸๆขฐๅญฆ็ฟ’ๆŽจ่ซ–ๅ‘ใ‘ใซ [Amazon EC2 Inf1](https://aws.amazon.com/ec2/instance-types/inf1/) ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใƒ•ใ‚กใƒŸใƒชใƒผใ‚’ๅฐŽๅ…ฅใ—ใพใ—ใŸใ€‚Inf1ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใฏAWS Inferentiaใƒใƒƒใƒ—ใซใ‚ˆใฃใฆ้ง†ๅ‹•ใ•ใ‚Œใ€ใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐๆŽจ่ซ–ใƒฏใƒผใ‚ฏใƒญใƒผใƒ‰ใซ็‰นๅŒ–ใ—ใŸใ‚ซใ‚นใ‚ฟใƒ ใƒ“ใƒซใƒ‰ใฎใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใ‚ขใ‚ฏใ‚ปใƒฉใƒฌใƒผใ‚ฟใงใ™ใ€‚[AWS Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#) ใฏInferentia็”จใฎSDKใงใ€ใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใ‚นใ—ใฆๆœ€้ฉๅŒ–ใ—ใ€Inf1ใซๅฑ•้–‹ใ™ใ‚‹ใŸใ‚ใฎใ‚ตใƒใƒผใƒˆใ‚’ๆไพ›ใ—ใพใ™ใ€‚ + +Neuron SDK ใŒๆไพ›ใ™ใ‚‹ใ‚‚ใฎ: + +1. ใ‚ฏใƒฉใ‚ฆใƒ‰ใงใฎๆŽจ่ซ–ใฎใŸใ‚ใซTorchScriptใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใ‚นใ—ใฆๆœ€้ฉๅŒ–ใ™ใ‚‹ใŸใ‚ใฎใ€1่กŒใฎใ‚ณใƒผใƒ‰ๅค‰ๆ›ดใงไฝฟ็”จใงใใ‚‹็ฐกๅ˜ใชAPIใ€‚ +2. [ๆ”นๅ–„ใ•ใ‚ŒใŸใ‚ณใ‚นใƒˆใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚น](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/) ใฎใŸใ‚ใฎใƒœใƒƒใ‚ฏใ‚นๅค–ใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นๆœ€้ฉๅŒ–ใ€‚ +3. [PyTorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html) ใพใŸใฏ [TensorFlow](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html) ใงๆง‹็ฏ‰ใ•ใ‚ŒใŸHugging Faceใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใธใฎใ‚ตใƒใƒผใƒˆใ€‚ + +### Implications + +BERT๏ผˆBidirectional Encoder Representations from Transformers๏ผ‰ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚„ใใฎๅค‰็จฎ๏ผˆ[distilBERT](https://huggingface.co/docs/transformers/main/model_doc/distilbert) ใ‚„ [roBERTa](https://huggingface.co/docs/transformers/main/model_doc/roberta) ใชใฉ๏ผ‰ใซๅŸบใฅใใƒˆใƒฉใƒณใ‚นใƒ•ใ‚ฉใƒผใƒžใƒผใƒขใƒ‡ใƒซใฏใ€้ž็”Ÿๆˆใ‚ฟใ‚นใ‚ฏ๏ผˆๆŠฝๅ‡บๅž‹่ณชๅ•ๅฟœ็ญ”ใ€ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใ€ใƒˆใƒผใ‚ฏใƒณๅˆ†้กžใชใฉ๏ผ‰ใซใŠใ„ใฆใ€Inf1ไธŠใงๆœ€้ฉใซๅ‹•ไฝœใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใƒ†ใ‚ญใ‚นใƒˆ็”Ÿๆˆใ‚ฟใ‚นใ‚ฏใ‚‚ [AWS Neuron MarianMT ใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซ](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html) ใซๅพ“ใฃใฆInf1ไธŠใงๅฎŸ่กŒใงใใพใ™ใ€‚Inferentiaใงใƒœใƒƒใ‚ฏใ‚นๅค–ใงๅค‰ๆ›ใงใใ‚‹ใƒขใƒ‡ใƒซใซ้–ขใ™ใ‚‹่ฉณ็ดฐๆƒ…ๅ ฑใฏใ€Neuronใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณใฎ [Model Architecture Fit](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia) ใ‚ปใ‚ฏใ‚ทใƒงใƒณใซใ‚ใ‚Šใพใ™ใ€‚ + +### Dependencies + +ใƒขใƒ‡ใƒซใ‚’AWS Neuronใซๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€[Neuron SDK ็’ฐๅขƒ](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide) ใŒๅฟ…่ฆใงใ€[AWS Deep Learning AMI](https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html) ใซไบ‹ๅ‰ใซๆง‹ๆˆใ•ใ‚Œใฆใ„ใพใ™ใ€‚ + +### Converting a model for AWS Neuron + +ใƒขใƒ‡ใƒซใ‚’AWS NEURON็”จใซๅค‰ๆ›ใ™ใ‚‹ใซใฏใ€[PythonใงTorchScriptใ‚’ไฝฟ็”จใ™ใ‚‹](torchscript#using-torchscript-in-python) ใจๅŒใ˜ใ‚ณใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใฆ `BertModel` ใ‚’ใƒˆใƒฌใƒผใ‚นใ—ใพใ™ใ€‚Python APIใ‚’ไป‹ใ—ใฆNeuron SDKใฎใ‚ณใƒณใƒใƒผใƒใƒณใƒˆใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใŸใ‚ใซใ€`torch.neuron` ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏๆ‹กๅผตใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใพใ™ใ€‚ + +```python +from transformers import BertModel, BertTokenizer, BertConfig +import torch +import torch.neuron +``` + +ๆฌกใฎ่กŒใ‚’ๅค‰ๆ›ดใ™ใ‚‹ใ ใ‘ใงๆธˆใฟใพใ™ใ€‚ + +```diff +- torch.jit.trace(model, [tokens_tensor, segments_tensors]) ++ torch.neuron.trace(model, [token_tensor, segments_tensors]) +``` + +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€Neuron SDKใฏใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใ‚นใ—ใ€Inf1ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นๅ‘ใ‘ใซๆœ€้ฉๅŒ–ใ—ใพใ™ใ€‚ + +AWS Neuron SDKใฎๆฉŸ่ƒฝใ€ใƒ„ใƒผใƒซใ€ใ‚ตใƒณใƒ—ใƒซใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใ€ๆœ€ๆ–ฐใฎใ‚ขใƒƒใƒ—ใƒ‡ใƒผใƒˆใซใคใ„ใฆ่ฉณใ—ใ็Ÿฅใ‚ŠใŸใ„ๅ ดๅˆใฏใ€[AWS NeuronSDK ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒ†ใƒผใ‚ทใƒงใƒณ](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html) ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/training.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/training.md new file mode 100644 index 0000000000000000000000000000000000000000..9dd2369601c10a52f009dc4a790e63d14370b8d0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/training.md @@ -0,0 +1,434 @@ + + +# Fine-tune a pretrained model + +[[open-in-colab]] + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ใจใ€่จˆ็ฎ—ใ‚ณใ‚นใƒˆใ‚’ๅ‰Šๆธ›ใ—ใ€็‚ญ็ด ๆŽ’ๅ‡บ้‡ใ‚’ๆธ›ๅฐ‘ใ•ใ›ใ€ใ‚ผใƒญใ‹ใ‚‰ใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใชใ—ใซๆœ€ๆ–ฐใฎใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใงใใ‚‹ๅˆฉ็‚นใŒใ‚ใ‚Šใพใ™ใ€‚ +๐Ÿค— Transformersใฏใ€ใ•ใพใ–ใพใชใ‚ฟใ‚นใ‚ฏใซๅฏพๅฟœใ—ใŸๆ•ฐๅƒใ‚‚ใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใธใฎใ‚ขใ‚ฏใ‚ปใ‚นใ‚’ๆไพ›ใ—ใพใ™ใ€‚ +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ไฝฟ็”จใ™ใ‚‹ๅ ดๅˆใ€ใใ‚Œใ‚’็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซๅˆใ‚ใ›ใŸใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ—ใพใ™ใ€‚ใ“ใ‚Œใฏใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใจใ—ใฆ็Ÿฅใ‚‰ใ‚Œใ€้žๅธธใซๅผทๅŠ›ใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๆŠ€่ก“ใงใ™ใ€‚ +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’้ธๆŠžใ—ใŸใƒ‡ใ‚ฃใƒผใƒ—ใƒฉใƒผใƒ‹ใƒณใ‚ฐใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™๏ผš + +* ๐Ÿค— Transformersใฎ[`Trainer`]ใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ€‚ +* TensorFlowใจKerasใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ€‚ +* ใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎPyTorchใ‚’ไฝฟ็”จใ—ใฆไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ€‚ + + + +## Prepare a dataset + + + +ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ‰ใซใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ็”จใซๆบ–ๅ‚™ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ๅ‰ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใฎๅ‡ฆ็†ๆ–นๆณ•ใ‚’่ชฌๆ˜Žใ—ใพใ—ใŸใŒใ€ใ“ใ‚Œใ‹ใ‚‰ใฏใใ‚Œใ‚‰ใฎใ‚นใ‚ญใƒซใ‚’ๆดปใ‹ใ™ๆฉŸไผšใŒใ‚ใ‚Šใพใ™๏ผ + +ใพใšใ€[Yelp Reviews](https://huggingface.co/datasets/yelp_review_full)ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใ‚“ใงใฟใพใ—ใ‚‡ใ†๏ผš + +```python +>>> from datasets import load_dataset + +>>> dataset = load_dataset("yelp_review_full") +>>> dataset["train"][100] +{'label': 0, + 'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'} +``` + +ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใŒใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ‡ฆ็†ใ—ใ€ๅฏๅค‰ใฎใ‚ทใƒผใ‚ฑใƒณใ‚น้•ทใ‚’ๅ‡ฆ็†ใ™ใ‚‹ใŸใ‚ใฎใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใจๅˆ‡ใ‚Šๆจใฆๆˆฆ็•ฅใ‚’ๅซใ‚ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ“ใจใ‚’ใ”ๅญ˜็Ÿฅใฎ้€šใ‚Šใ€ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’1ใคใฎใ‚นใƒ†ใƒƒใƒ—ใงๅ‡ฆ็†ใ™ใ‚‹ใซใฏใ€๐Ÿค— Datasets ใฎ [`map`](https://huggingface.co/docs/datasets/process#map) ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใ€ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซๅ‰ๅ‡ฆ็†้–ขๆ•ฐใ‚’้ฉ็”จใ—ใพใ™๏ผš + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> def tokenize_function(examples): +... return tokenizer(examples["text"], padding="max_length", truncation=True) + +>>> tokenized_datasets = dataset.map(tokenize_function, batched=True) +``` + +ใŠๅฅฝใฟใงใ€ๅฎŸ่กŒๆ™‚้–“ใ‚’็Ÿญ็ธฎใ™ใ‚‹ใŸใ‚ใซใƒ•ใƒซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใชใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + + + +## Train + +ใ“ใฎๆ™‚็‚นใงใ€ไฝฟ็”จใ—ใŸใ„ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใซๅฏพๅฟœใ™ใ‚‹ใ‚ปใ‚ฏใ‚ทใƒงใƒณใซๅพ“ใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๅณๅดใฎใ‚ตใ‚คใƒ‰ใƒใƒผใฎใƒชใƒณใ‚ฏใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ธใƒฃใƒณใƒ—ใ—ใŸใ„ใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใซ็งปๅ‹•ใงใใพใ™ใ€‚ +ใใ—ใฆใ€็‰นๅฎšใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎใ™ในใฆใฎใ‚ณใƒณใƒ†ใƒณใƒ„ใ‚’้ž่กจ็คบใซใ—ใŸใ„ๅ ดๅˆใฏใ€ใใฎใƒ•ใƒฌใƒผใƒ ใƒฏใƒผใ‚ฏใฎใƒ–ใƒญใƒƒใ‚ฏๅณไธŠใซใ‚ใ‚‹ใƒœใ‚ฟใƒณใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„๏ผ + + + + + +## Train with Pytorch Trainer + +๐Ÿค— Transformersใฏใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’ๆœ€้ฉๅŒ–ใ—ใŸ[`Trainer`]ใ‚ฏใƒฉใ‚นใ‚’ๆไพ›ใ—ใ€็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๆ‰‹ๅ‹•ใง่จ˜่ฟฐใ›ใšใซใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้–‹ๅง‹ใ—ใ‚„ใ™ใใ—ใฆใ„ใพใ™ใ€‚ +[`Trainer`] APIใฏใ€ใƒญใ‚ฐ่จ˜้Œฒใ€ๅ‹พ้…็ดฏ็ฉใ€ๆททๅˆ็ฒพๅบฆใชใฉใ€ใ•ใพใ–ใพใชใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ชใƒ—ใ‚ทใƒงใƒณใจๆฉŸ่ƒฝใ‚’ใ‚ตใƒใƒผใƒˆใ—ใฆใ„ใพใ™ใ€‚ + +ใพใšใ€ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใ€ไบˆๆƒณใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใพใ™ใ€‚Yelp Review [dataset card](https://huggingface.co/datasets/yelp_review_full#data-fields)ใ‹ใ‚‰ใ€5ใคใฎใƒฉใƒ™ใƒซใŒใ‚ใ‚‹ใ“ใจใŒใ‚ใ‹ใ‚Šใพใ™๏ผš + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + + + +ไธ€้ƒจใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎ้‡ใฟใŒไฝฟ็”จใ•ใ‚Œใšใ€ไธ€้ƒจใฎ้‡ใฟใŒใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸ่ญฆๅ‘ŠใŒ่กจ็คบใ•ใ‚Œใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ๅฟƒ้…ใ—ใชใ„ใงใใ ใ•ใ„ใ€ใ“ใ‚ŒใฏๅฎŒๅ…จใซๆญฃๅธธใงใ™๏ผ +BERTใƒขใƒ‡ใƒซใฎไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใฎใƒ˜ใƒƒใƒ‰ใฏ็ ดๆฃ„ใ•ใ‚Œใ€ใƒฉใƒณใƒ€ใƒ ใซๅˆๆœŸๅŒ–ใ•ใ‚ŒใŸๅˆ†้กžใƒ˜ใƒƒใƒ‰ใง็ฝฎใๆ›ใˆใ‚‰ใ‚Œใพใ™ใ€‚ใ“ใฎๆ–ฐใ—ใ„ใƒขใƒ‡ใƒซใƒ˜ใƒƒใƒ‰ใ‚’ใ‚ทใƒผใ‚ฑใƒณใ‚นๅˆ†้กžใ‚ฟใ‚นใ‚ฏใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ—ใ€ไบ‹ๅ‰ๅญฆ็ฟ’ใƒขใƒ‡ใƒซใฎ็Ÿฅ่ญ˜ใ‚’ใใ‚Œใซ่ปข้€ใ—ใพใ™ใ€‚ + + + +### Training Hyperparameters + +ๆฌกใซใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’ใ‚ขใ‚ฏใƒ†ใ‚ฃใƒ™ใƒผใƒˆใ™ใ‚‹ใŸใ‚ใฎใ™ในใฆใฎใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใจใ€่ชฟๆ•ดใงใใ‚‹ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๅซใ‚€[`TrainingArguments`]ใ‚ฏใƒฉใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ +ใ“ใฎใƒใƒฅใƒผใƒˆใƒชใ‚ขใƒซใงใฏใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ[ใƒใ‚คใƒ‘ใƒผใƒ‘ใƒฉใƒกใƒผใ‚ฟ](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments)ใ‚’ไฝฟ็”จใ—ใฆ้–‹ๅง‹ใงใใพใ™ใŒใ€ๆœ€้ฉใช่จญๅฎšใ‚’่ฆ‹ใคใ‘ใ‚‹ใŸใ‚ใซใ“ใ‚Œใ‚‰ใ‚’ๅฎŸ้จ“ใ—ใฆใ‚‚ๆง‹ใ„ใพใ›ใ‚“ใ€‚ + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‚’ไฟๅญ˜ใ™ใ‚‹ๅ ดๆ‰€ใ‚’ๆŒ‡ๅฎšใ—ใพใ™๏ผš + +```python +>>> from transformers import TrainingArguments + +>>> training_args = TrainingArguments(output_dir="test_trainer") +``` + +### Evaluate + +[`Trainer`]ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐไธญใซ่‡ชๅ‹•็š„ใซใƒขใƒ‡ใƒซใฎใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใ‚’่ฉ•ไพกใ—ใพใ›ใ‚“ใ€‚ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’่จˆ็ฎ—ใ—ใฆๅ ฑๅ‘Šใ™ใ‚‹้–ขๆ•ฐใ‚’[`Trainer`]ใซๆธกใ™ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +[๐Ÿค— Evaluate](https://huggingface.co/docs/evaluate/index)ใƒฉใ‚คใƒ–ใƒฉใƒชใงใฏใ€[`evaluate.load`]้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆ่ชญใฟ่พผใ‚€ใ“ใจใŒใงใใ‚‹ใ‚ทใƒณใƒ—ใƒซใช[`accuracy`](https://huggingface.co/spaces/evaluate-metric/accuracy)้–ขๆ•ฐใŒๆไพ›ใ•ใ‚Œใฆใ„ใพใ™๏ผˆ่ฉณ็ดฐใซใคใ„ใฆใฏ[ใ“ใกใ‚‰ใฎใ‚ฏใ‚คใƒƒใ‚ฏใƒ„ใ‚ขใƒผ](https://huggingface.co/docs/evaluate/a_quick_tour)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผ‰๏ผš + +```python +>>> import numpy as np +>>> import evaluate + +>>> metric = evaluate.load("accuracy") +``` + +`metric`ใฎ`~evaluate.compute`ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใ€ไบˆๆธฌใฎๆญฃ็ขบๅบฆใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ `compute`ใซไบˆๆธฌใ‚’ๆธกใ™ๅ‰ใซใ€ไบˆๆธฌใ‚’ใƒญใ‚ธใƒƒใƒˆใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผˆใ™ในใฆใฎ๐Ÿค— Transformersใƒขใƒ‡ใƒซใฏใƒญใ‚ธใƒƒใƒˆใ‚’่ฟ”ใ™ใ“ใจใ‚’่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„๏ผ‰๏ผš + +```py +>>> def compute_metrics(eval_pred): +... logits, labels = eval_pred +... predictions = np.argmax(logits, axis=-1) +... return metric.compute(predictions=predictions, references=labels) +``` + +่ฉ•ไพกใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐไธญใซ็›ฃ่ฆ–ใ—ใŸใ„ๅ ดๅˆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใง `eval_strategy` ใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒ‡ๅฎšใ—ใฆใ€ๅ„ใ‚จใƒใƒƒใ‚ฏใฎ็ต‚ไบ†ๆ™‚ใซ่ฉ•ไพกใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅ ฑๅ‘Šใ—ใพใ™๏ผš + +```python +>>> from transformers import TrainingArguments, Trainer + +>>> training_args = TrainingArguments(output_dir="test_trainer", eval_strategy="epoch") +``` + +### Trainer + +ใƒขใƒ‡ใƒซใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅผ•ๆ•ฐใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใŠใ‚ˆใณใƒ†ใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ€่ฉ•ไพก้–ขๆ•ฐใ‚’ไฝฟ็”จใ—ใฆ[`Trainer`]ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +ใใฎๅพŒใ€[`~transformers.Trainer.train`]ใ‚’ๅ‘ผใณๅ‡บใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅพฎ่ชฟๆ•ดใ—ใพใ™๏ผš + +```python +>>> trainer.train() +``` + + + + + + + +## Kerasใ‚’ไฝฟ็”จใ—ใฆTensorFlowใƒขใƒ‡ใƒซใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ + +Keras APIใ‚’ไฝฟ็”จใ—ใฆ๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’TensorFlowใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผ + +### Loading Data from Keras + +๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’Keras APIใงใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’KerasใŒ็†่งฃใงใใ‚‹ๅฝขๅผใซๅค‰ๆ›ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒๅฐใ•ใ„ๅ ดๅˆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใ‚’NumPy้…ๅˆ—ใซๅค‰ๆ›ใ—ใฆKerasใซๆธกใ™ใ“ใจใŒใงใใพใ™ใ€‚ +่ค‡้›‘ใชใ“ใจใ‚’ใ™ใ‚‹ๅ‰ใซใ€ใพใšใใ‚Œใ‚’่ฉฆใ—ใฆใฟใพใ—ใ‚‡ใ†ใ€‚ + +ใพใšใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’่ชญใฟ่พผใฟใพใ™ใ€‚GLUEใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‹ใ‚‰CoLAใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝฟ็”จใ—ใพใ™ +([GLUE Banchmark](https://huggingface.co/datasets/glue))ใ€ใ“ใ‚Œใฏๅ˜็ด”ใชใƒใ‚คใƒŠใƒชใƒ†ใ‚ญใ‚นใƒˆๅˆ†้กžใ‚ฟใ‚นใ‚ฏใงใ™ใ€‚ไปŠใฎใจใ“ใ‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅˆ†ๅ‰ฒใฎใฟใ‚’ไฝฟ็”จใ—ใพใ™ใ€‚ + +```py +from datasets import load_dataset + +dataset = load_dataset("glue", "cola") +dataset = dataset["train"] # ไปŠใฎใจใ“ใ‚ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐๅˆ†ๅ‰ฒใฎใฟใ‚’ไฝฟ็”จใ—ใพใ™ +``` + +ๆฌกใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’ใƒญใƒผใƒ‰ใ—ใ€ใƒ‡ใƒผใ‚ฟใ‚’NumPy้…ๅˆ—ใจใ—ใฆใƒˆใƒผใ‚ฏใƒณๅŒ–ใ—ใพใ™ใ€‚ใƒฉใƒ™ใƒซใฏๆ—ขใซ`0`ใจ`1`ใฎใƒชใ‚นใƒˆใงใ‚ใ‚‹ใŸใ‚ใ€ใƒˆใƒผใ‚ฏใƒณๅŒ–ใ›ใšใซ็›ดๆŽฅNumPy้…ๅˆ—ใซๅค‰ๆ›ใงใใพใ™๏ผ + +```python +from transformers import AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased") +tokenized_data = tokenizer(dataset["sentence"], return_tensors="np", padding=True) +# ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏBatchEncodingใ‚’่ฟ”ใ—ใพใ™ใŒใ€ใใ‚Œใ‚’Keras็”จใซ่พžๆ›ธใซๅค‰ๆ›ใ—ใพใ™ +tokenized_data = dict(tokenized_data) + +labels = np.array(dataset["label"]) # ใƒฉใƒ™ใƒซใฏใ™ใงใซ0ใจ1ใฎ้…ๅˆ—ใงใ™ +``` + +ๆœ€ๅพŒใซใ€ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใ€[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ใจ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๅฎŸ่กŒใ—ใพใ™ใ€‚ +ๆณจๆ„็‚นใจใ—ใฆใ€Transformersใƒขใƒ‡ใƒซใฏใ™ในใฆใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใ‚ฟใ‚นใ‚ฏใซ้–ข้€ฃใ—ใŸๆๅคฑ้–ขๆ•ฐใ‚’ๆŒใฃใฆใ„ใ‚‹ใŸใ‚ใ€ๆŒ‡ๅฎšใ—ใชใใฆใ‚‚ๆง‹ใ„ใพใ›ใ‚“๏ผˆๆŒ‡ๅฎšใ™ใ‚‹ๅ ดๅˆใ‚’้™คใ๏ผ‰๏ผš + +```python +from transformers import TFAutoModelForSequenceClassification +from tensorflow.keras.optimizers import Adam + +# ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ‚ณใƒณใƒ‘ใ‚คใƒซใ™ใ‚‹ +model = TFAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased") +# ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใซใฏ้€šๅธธใ€ๅญฆ็ฟ’็އใ‚’ไธ‹ใ’ใ‚‹ใจ่‰ฏใ„ใงใ™ +model.compile(optimizer=Adam(3e-5)) # ๆๅคฑ้–ขๆ•ฐใฎๆŒ‡ๅฎšใฏไธ่ฆใงใ™๏ผ + +model.fit(tokenized_data, labels) +``` + + + +ใƒขใƒ‡ใƒซใ‚’`compile()`ใ™ใ‚‹้š›ใซ`loss`ๅผ•ๆ•ฐใ‚’ๆธกใ™ๅฟ…่ฆใฏใ‚ใ‚Šใพใ›ใ‚“๏ผHugging Faceใƒขใƒ‡ใƒซใฏใ€ใ“ใฎๅผ•ๆ•ฐใ‚’็ฉบ็™ฝใฎใพใพใซใ—ใฆใŠใใจใ€ใ‚ฟใ‚นใ‚ฏใจใƒขใƒ‡ใƒซใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใซ้ฉใ—ใŸๆๅคฑใ‚’่‡ชๅ‹•็š„ใซ้ธๆŠžใ—ใพใ™ใ€‚ +ๅฟ…่ฆใซๅฟœใ˜ใฆ่‡ชๅˆ†ใงๆๅคฑใ‚’ๆŒ‡ๅฎšใ—ใฆใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰ใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™๏ผ + + + +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใฏใ€ๅฐ่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซใฏ้ฉใ—ใฆใ„ใพใ™ใŒใ€ๅคง่ฆๆจกใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅฏพใ—ใฆใฏๅ•้กŒใซใชใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใชใœใชใ‚‰ใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ•ใ‚ŒใŸ้…ๅˆ—ใจใƒฉใƒ™ใƒซใฏใƒกใƒขใƒชใซๅฎŒๅ…จใซ่ชญใฟ่พผใพใ‚Œใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใ€ใพใŸNumPyใฏใ€Œใ‚ธใƒฃใ‚ฎใƒผใ€ใช้…ๅˆ—ใ‚’ๅ‡ฆ็†ใ—ใชใ„ใŸใ‚ใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚บใ•ใ‚ŒใŸๅ„ใ‚ตใƒณใƒ—ใƒซใ‚’ๅ…จไฝ“ใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ†…ใงๆœ€ใ‚‚้•ทใ„ใ‚ตใƒณใƒ—ใƒซใฎ้•ทใ•ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใ“ใ‚Œใซใ‚ˆใ‚Šใ€้…ๅˆ—ใŒใ•ใ‚‰ใซๅคงใใใชใ‚Šใ€ใ™ในใฆใฎใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้…ใใ™ใ‚‹ๅŽŸๅ› ใซใชใ‚Šใพใ™๏ผ + +### Loading data as a tf.data.Dataset + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚’้…ใใ›ใšใซใƒ‡ใƒผใ‚ฟใ‚’่ชญใฟ่พผใ‚€ใซใฏใ€ใƒ‡ใƒผใ‚ฟใ‚’`tf.data.Dataset`ใจใ—ใฆ่ชญใฟ่พผใ‚€ใ“ใจใŒใงใใพใ™ใ€‚็‹ฌ่‡ชใฎ`tf.data`ใƒ‘ใ‚คใƒ—ใƒฉใ‚คใƒณใ‚’ไฝœๆˆใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใŒใ€ใ“ใ‚Œใ‚’่กŒใ†ใŸใ‚ใฎไพฟๅˆฉใชๆ–นๆณ•ใŒ2ใคใ‚ใ‚Šใพใ™๏ผš + +- [`~TFPreTrainedModel.prepare_tf_dataset`]: ใ“ใ‚Œใฏใปใจใ‚“ใฉใฎๅ ดๅˆใงๆŽจๅฅจใ™ใ‚‹ๆ–นๆณ•ใงใ™ใ€‚ใƒขใƒ‡ใƒซไธŠใฎใƒกใ‚ฝใƒƒใƒ‰ใชใฎใงใ€ใƒขใƒ‡ใƒซใ‚’ๆคœๆŸปใ—ใฆใƒขใƒ‡ใƒซๅ…ฅๅŠ›ใจใ—ใฆไฝฟ็”จๅฏ่ƒฝใชๅˆ—ใ‚’่‡ชๅ‹•็š„ใซๆŠŠๆกใ—ใ€ไป–ใฎๅˆ—ใ‚’็ ดๆฃ„ใ—ใฆใ‚ˆใ‚Šๅ˜็ด”ใง้ซ˜ๆ€ง่ƒฝใชใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ไฝœๆˆใงใใพใ™ใ€‚ +- [`~datasets.Dataset.to_tf_dataset`]: ใ“ใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใ‚ˆใ‚ŠไฝŽใƒฌใƒ™ใƒซใงใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใŒใฉใฎใ‚ˆใ†ใซไฝœๆˆใ•ใ‚Œใ‚‹ใ‹ใ‚’ๆญฃ็ขบใซๅˆถๅพกใ™ใ‚‹ๅ ดๅˆใซไพฟๅˆฉใงใ™ใ€‚`columns`ใจ`label_cols`ใ‚’ๆŒ‡ๅฎšใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅซใ‚ใ‚‹ๅˆ—ใ‚’ๆญฃ็ขบใซๆŒ‡ๅฎšใงใใพใ™ใ€‚ + +[`~TFPreTrainedModel.prepare_tf_dataset`]ใ‚’ไฝฟ็”จใ™ใ‚‹ๅ‰ใซใ€ๆฌกใฎใ‚ณใƒผใƒ‰ใ‚ตใƒณใƒ—ใƒซใซ็คบใ™ใ‚ˆใ†ใซใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎๅ‡บๅŠ›ใ‚’ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅˆ—ใจใ—ใฆ่ฟฝๅŠ ใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™๏ผš + +```py +def tokenize_dataset(data): + # ่ฟ”ใ•ใ‚ŒใŸ่พžๆ›ธใฎใ‚ญใƒผใฏใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใซๅˆ—ใจใ—ใฆ่ฟฝๅŠ ใ•ใ‚Œใพใ™ + return tokenizer(data["text"]) + + +dataset = dataset.map(tokenize_dataset) +``` + +Hugging Faceใฎใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใƒ‡ใ‚ฃใ‚นใ‚ฏใซไฟๅญ˜ใ•ใ‚Œใ‚‹ใŸใ‚ใ€ใ“ใ‚Œใซใ‚ˆใ‚Šใƒกใƒขใƒชใฎไฝฟ็”จ้‡ใŒๅข—ใˆใ‚‹ใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“๏ผ +ๅˆ—ใŒ่ฟฝๅŠ ใ•ใ‚ŒใŸใ‚‰ใ€ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‹ใ‚‰ใƒใƒƒใƒใ‚’ใ‚นใƒˆใƒชใƒผใƒ ใ—ใ€ๅ„ใƒใƒƒใƒใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’่ฟฝๅŠ ใงใใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆๅ…จไฝ“ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใ‚’่ฟฝๅŠ ใ™ใ‚‹ๅ ดๅˆใจๆฏ”ในใฆใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใฎๆ•ฐใŒๅคงๅน…ใซๅ‰Šๆธ›ใ•ใ‚Œใพใ™ใ€‚ + +```python +>>> tf_dataset = model.prepare_tf_dataset(dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer) +``` + +ไธŠ่จ˜ใฎใ‚ณใƒผใƒ‰ใ‚ตใƒณใƒ—ใƒซใงใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใ‚’`prepare_tf_dataset`ใซๆธกใ—ใฆใ€ใƒใƒƒใƒใ‚’ๆญฃใ—ใ่ชญใฟ่พผใ‚€้š›ใซๆญฃใ—ใใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎใ™ในใฆใฎใ‚ตใƒณใƒ—ใƒซใŒๅŒใ˜้•ทใ•ใงใ‚ใ‚Šใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใŒไธ่ฆใชๅ ดๅˆใฏใ€ใ“ใฎๅผ•ๆ•ฐใ‚’ใ‚นใ‚ญใƒƒใƒ—ใงใใพใ™ใ€‚ +ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐไปฅๅค–ใฎ่ค‡้›‘ใชๅ‡ฆ็†ใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆ๏ผˆไพ‹๏ผšใƒžใ‚นใ‚ฏ่จ€่ชžใƒขใƒ‡ใƒชใƒณใ‚ฐใฎใŸใ‚ใฎใƒˆใƒผใ‚ฏใƒณใฎ็ ดๆใชใฉ๏ผ‰ใ€ +ไปฃใ‚ใ‚Šใซ`collate_fn`ๅผ•ๆ•ฐใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ตใƒณใƒ—ใƒซใฎใƒชใ‚นใƒˆใ‚’ใƒใƒƒใƒใซๅค‰ๆ›ใ—ใ€ๅฟ…่ฆใชๅ‰ๅ‡ฆ็†ใ‚’้ฉ็”จใ™ใ‚‹้–ขๆ•ฐใ‚’ๆธกใ™ใ“ใจใŒใงใใพใ™ใ€‚ +ใ“ใฎใ‚ขใƒ—ใƒญใƒผใƒใ‚’ๅฎŸ้š›ใซไฝฟ็”จใ—ใŸไพ‹ใซใคใ„ใฆใฏใ€ +[examples](https://github.com/huggingface/transformers/tree/main/examples)ใ‚„ +[notebooks](https://huggingface.co/docs/transformers/notebooks)ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚ + +`tf.data.Dataset`ใ‚’ไฝœๆˆใ—ใŸใ‚‰ใ€ไปฅๅ‰ใจๅŒๆง˜ใซใƒขใƒ‡ใƒซใ‚’ใ‚ณใƒณใƒ‘ใ‚คใƒซใ—ใ€้ฉๅˆใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™๏ผš + +```python +model.compile(optimizer=Adam(3e-5)) # ๆๅคฑๅผ•ๆ•ฐใฏไธ่ฆใงใ™๏ผ + +model.fit(tf_dataset) +``` + + + + + + +## Train in native Pytorch + + + + + +[`Trainer`]ใฏใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ๅ‡ฆ็†ใ—ใ€1่กŒใฎใ‚ณใƒผใƒ‰ใงใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚ +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’็‹ฌ่‡ชใซ่จ˜่ฟฐใ—ใŸใ„ใƒฆใƒผใ‚ถใƒผใฎใŸใ‚ใซใ€๐Ÿค— Transformersใƒขใƒ‡ใƒซใ‚’ใƒใ‚คใƒ†ใ‚ฃใƒ–ใฎPyTorchใงใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใ€‚ + +ใ“ใฎๆ™‚็‚นใงใ€ใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ๅ†่ตทๅ‹•ใ™ใ‚‹ใ‹ใ€ไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใƒกใƒขใƒชใ‚’่งฃๆ”พใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“๏ผš + +```py +del model +del trainer +torch.cuda.empty_cache() +``` + +1. ใƒขใƒ‡ใƒซใฏ็”Ÿใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๅ…ฅๅŠ›ใจใ—ใฆๅ—ใ‘ๅ–ใ‚‰ใชใ„ใŸใ‚ใ€`text` ๅˆ—ใ‚’ๅ‰Š้™คใ—ใพใ™๏ผš + +```py +>>> tokenized_datasets = tokenized_datasets.remove_columns(["text"]) +``` + +2. `label`ๅˆ—ใ‚’`labels`ใซๅๅ‰ใ‚’ๅค‰ๆ›ดใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฏๅผ•ๆ•ฐใฎๅๅ‰ใ‚’`labels`ใจๆœŸๅพ…ใ—ใฆใ„ใพใ™๏ผš + +```py +>>> tokenized_datasets = tokenized_datasets.rename_column("label", "labels") +``` + +3. ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฝขๅผใ‚’ใƒชใ‚นใƒˆใงใฏใชใPyTorchใƒ†ใƒณใ‚ฝใƒซใ‚’่ฟ”ใ™ใ‚ˆใ†ใซ่จญๅฎšใ—ใพใ™๏ผš + +```py +>>> tokenized_datasets.set_format("torch") +``` + +ไปฅๅ‰ใซ็คบใ—ใŸใ‚ˆใ†ใซใ€ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใฎๅฐใ•ใชใ‚ตใƒ–ใ‚ปใƒƒใƒˆใ‚’ไฝœๆˆใ—ใพใ™๏ผš + +```py +>>> small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000)) +>>> small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000)) +``` + +### DataLoader + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใจใƒ†ใ‚นใƒˆใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆ็”จใฎ`DataLoader`ใ‚’ไฝœๆˆใ—ใฆใ€ใƒ‡ใƒผใ‚ฟใฎใƒใƒƒใƒใ‚’ใ‚คใƒ†ใƒฌใƒผใƒˆใงใใ‚‹ใ‚ˆใ†ใซใ—ใพใ™๏ผš + +```py +>>> from torch.utils.data import DataLoader + +>>> train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8) +>>> eval_dataloader = DataLoader(small_eval_dataset, batch_size=8) +``` + +ใƒญใƒผใƒ‰ใ™ใ‚‹ใƒขใƒ‡ใƒซใจๆœŸๅพ…ใ•ใ‚Œใ‚‹ใƒฉใƒ™ใƒซใฎๆ•ฐใ‚’ๆŒ‡ๅฎšใ—ใฆใใ ใ•ใ„๏ผš + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", num_labels=5) +``` + +### Optimizer and learning rate scheduler + +ใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใจๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใƒผใ‚’ไฝœๆˆใ—ใพใ—ใ‚‡ใ†ใ€‚ +PyTorchใ‹ใ‚‰[`AdamW`](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html)ใ‚ชใƒ—ใƒ†ใ‚ฃใƒžใ‚คใ‚ถใ‚’ไฝฟ็”จใ—ใพใ™๏ผš + +```python +>>> from torch.optim import AdamW + +>>> optimizer = AdamW(model.parameters(), lr=5e-5) +``` + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎๅญฆ็ฟ’็އใ‚นใ‚ฑใ‚ธใƒฅใƒผใƒฉใ‚’[`Trainer`]ใ‹ใ‚‰ไฝœๆˆใ™ใ‚‹๏ผš + +```py +>>> from transformers import get_scheduler + +>>> num_epochs = 3 +>>> num_training_steps = num_epochs * len(train_dataloader) +>>> lr_scheduler = get_scheduler( +... name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps +... ) +``` + +ๆœ€ๅพŒใซใ€GPUใ‚’ๅˆฉ็”จใงใใ‚‹ๅ ดๅˆใฏ `device` ใ‚’ๆŒ‡ๅฎšใ—ใฆใใ ใ•ใ„ใ€‚ใใ‚Œไปฅๅค–ใฎๅ ดๅˆใ€CPUใงใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏๆ•ฐๆ™‚้–“ใ‹ใ‹ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚Šใ€ๆ•ฐๅˆ†ใงๅฎŒไบ†ใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + +```py +>>> import torch + +>>> device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +>>> model.to(device) +``` + + + +ใ‚ฏใƒฉใ‚ฆใƒ‰GPUใŒๅˆฉ็”จใงใใชใ„ๅ ดๅˆใ€[Colaboratory](https://colab.research.google.com/)ใ‚„[SageMaker StudioLab](https://studiolab.sagemaker.aws/)ใชใฉใฎใƒ›ใ‚นใƒˆใ•ใ‚ŒใŸใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใ‚’ไฝฟ็”จใ—ใฆ็„กๆ–™ใงGPUใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใ€‚ + + + +ใ•ใฆใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎๆบ–ๅ‚™ใŒๆ•ดใ„ใพใ—ใŸ๏ผ ๐Ÿฅณ + +### ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ— + +ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฎ้€ฒๆ—ใ‚’่ฟฝ่ทกใ™ใ‚‹ใŸใ‚ใซใ€[tqdm](https://tqdm.github.io/)ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚’ไฝฟ็”จใ—ใฆใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ‚นใƒ†ใƒƒใƒ—ใฎๆ•ฐใซๅฏพใ—ใฆ้€ฒ่กŒ็Šถๆณใƒใƒผใ‚’่ฟฝๅŠ ใ—ใพใ™๏ผš + +```py +>>> from tqdm.auto import tqdm + +>>> progress_bar = tqdm(range(num_training_steps)) + +>>> model.train() +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... outputs = model(**batch) +... loss = outputs.loss +... loss.backward() + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +### Evaluate + +[`Trainer`]ใซ่ฉ•ไพก้–ขๆ•ฐใ‚’่ฟฝๅŠ ใ—ใŸใฎใจๅŒๆง˜ใซใ€็‹ฌ่‡ชใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใƒซใƒผใƒ—ใ‚’ไฝœๆˆใ™ใ‚‹้š›ใซใ‚‚ๅŒๆง˜ใฎๆ“ไฝœใ‚’่กŒใ†ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +ใŸใ ใ—ใ€ๅ„ใ‚จใƒใƒƒใ‚ฏใฎๆœ€ๅพŒใซใƒกใƒˆใƒชใƒƒใ‚ฏใ‚’่จˆ็ฎ—ใŠใ‚ˆใณๅ ฑๅ‘Šใ™ใ‚‹ไปฃใ‚ใ‚Šใซใ€ไปŠๅ›žใฏ[`~evaluate.add_batch`]ใ‚’ไฝฟ็”จใ—ใฆใ™ในใฆใฎใƒใƒƒใƒใ‚’่“„็ฉใ—ใ€ๆœ€ๅพŒใซใƒกใƒˆใƒชใƒƒใ‚ฏใ‚’่จˆ็ฎ—ใ—ใพใ™ใ€‚ + +```python +>>> import evaluate + +>>> metric = evaluate.load("accuracy") +>>> model.eval() +>>> for batch in eval_dataloader: +... batch = {k: v.to(device) for k, v in batch.items()} +... with torch.no_grad(): +... outputs = model(**batch) + +... logits = outputs.logits +... predictions = torch.argmax(logits, dim=-1) +... metric.add_batch(predictions=predictions, references=batch["labels"]) + +>>> metric.compute() +``` + + + + + + +## ่ฟฝๅŠ ใƒชใ‚ฝใƒผใ‚น + +ใ•ใ‚‰ใชใ‚‹ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใฎไพ‹ใซใคใ„ใฆใฏใ€ไปฅไธ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„๏ผš + +- [๐Ÿค— Transformers Examples](https://github.com/huggingface/transformers/tree/main/examples) ใซใฏใ€PyTorchใจTensorFlowใงไธ€่ˆฌ็š„ใชNLPใ‚ฟใ‚นใ‚ฏใ‚’ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ + +- [๐Ÿค— Transformers Notebooks](notebooks) ใซใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใซใƒขใƒ‡ใƒซใ‚’ใƒ•ใ‚กใ‚คใƒณใƒใƒฅใƒผใƒ‹ใƒณใ‚ฐใ™ใ‚‹ๆ–นๆณ•ใซ้–ขใ™ใ‚‹ใ•ใพใ–ใพใชใƒŽใƒผใƒˆใƒ–ใƒƒใ‚ฏใŒๅซใพใ‚Œใฆใ„ใพใ™ใ€‚ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/troubleshooting.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/troubleshooting.md new file mode 100644 index 0000000000000000000000000000000000000000..b13b5993171a0a31c9f3d9c65d70069e5909b57a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ja/troubleshooting.md @@ -0,0 +1,195 @@ + + +# Troubleshoot + +ๆ™‚ใซใฏใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใŒใ€็งใŸใกใฏใ“ใ“ใซใ„ใพใ™๏ผใ“ใฎใ‚ฌใ‚คใƒ‰ใงใฏใ€็งใŸใกใŒใ‚ˆใ่ฆ‹ใ‚‹ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใชๅ•้กŒใจใ€ใใ‚Œใ‚‰ใ‚’่งฃๆฑบใ™ใ‚‹ๆ–นๆณ•ใซใคใ„ใฆ่ชฌๆ˜Žใ—ใพใ™ใ€‚ใŸใ ใ—ใ€ใ“ใฎใ‚ฌใ‚คใƒ‰ใฏใ™ในใฆใฎ ๐Ÿค— Transformers ใฎๅ•้กŒใฎๅŒ…ๆ‹ฌ็š„ใชใ‚ณใƒฌใ‚ฏใ‚ทใƒงใƒณใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚ๅ•้กŒใ‚’ใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐใ™ใ‚‹ใŸใ‚ใฎ่ฉณ็ดฐใชใƒ˜ใƒซใƒ—ใŒๅฟ…่ฆใชๅ ดๅˆใฏใ€ไปฅไธ‹ใฎๆ–นๆณ•ใ‚’่ฉฆใ—ใฆใฟใฆใใ ใ•ใ„๏ผš + + + +1. [ใƒ•ใ‚ฉใƒผใƒฉใƒ ](https://discuss.huggingface.co/)ใงๅŠฉใ‘ใ‚’ๆฑ‚ใ‚ใ‚‹ใ€‚ [ๅˆๅฟƒ่€…ๅ‘ใ‘](https://discuss.huggingface.co/c/beginners/5) ใพใŸใฏ [๐Ÿค— Transformers](https://discuss.huggingface.co/c/transformers/9) ใชใฉใ€่ณชๅ•ใ‚’ๆŠ•็จฟใงใใ‚‹็‰นๅฎšใฎใ‚ซใƒ†ใ‚ดใƒชใŒใ‚ใ‚Šใพใ™ใ€‚ๅ•้กŒใŒ่งฃๆฑบใ•ใ‚Œใ‚‹ๅฏ่ƒฝๆ€งใ‚’ๆœ€ๅคง้™ใซใ™ใ‚‹ใŸใ‚ใซใ€ๅ†็พๅฏ่ƒฝใชใ‚ณใƒผใƒ‰ใ‚’ๅซใ‚€่‰ฏใ„่ชฌๆ˜Ž็š„ใชใƒ•ใ‚ฉใƒผใƒฉใƒ ๆŠ•็จฟใ‚’ๆ›ธใใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผ + + + +2. ใƒใ‚ฐใŒใƒฉใ‚คใƒ–ใƒฉใƒชใซ้–ข้€ฃใ™ใ‚‹ๅ ดๅˆใฏใ€๐Ÿค— Transformers ใƒชใƒใ‚ธใƒˆใƒชใง [Issue](https://github.com/huggingface/transformers/issues/new/choose) ใ‚’ไฝœๆˆใ—ใฆใใ ใ•ใ„ใ€‚ใƒใ‚ฐใ‚’่ชฌๆ˜Žใ™ใ‚‹ใŸใ‚ใฎใงใใ‚‹ใ ใ‘ๅคšใใฎๆƒ…ๅ ฑใ‚’ๅซใ‚ใ‚‹ใ‚ˆใ†ใซๅฟƒใŒใ‘ใ€ไฝ•ใŒๅ•้กŒใงใ€ใฉใฎใ‚ˆใ†ใซไฟฎๆญฃใงใใ‚‹ใ‹ใ‚’ใ‚ˆใ‚Š่‰ฏใ็†่งฃใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„ใ€‚ + +3. ใ‚ˆใ‚Šๅคใ„ใƒใƒผใ‚ธใƒงใƒณใฎ ๐Ÿค— Transformers ใ‚’ไฝฟ็”จใ—ใฆใ„ใ‚‹ๅ ดๅˆใฏใ€[Migration](migration) ใ‚ฌใ‚คใƒ‰ใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚ใƒใƒผใ‚ธใƒงใƒณ้–“ใง้‡่ฆใชๅค‰ๆ›ดใŒๅฐŽๅ…ฅใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +ใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐใจใƒ˜ใƒซใƒ—ใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€Hugging Faceใ‚ณใƒผใ‚นใฎ [็ฌฌ8็ซ ](https://huggingface.co/course/chapter8/1?fw=pt) ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + +## Firewalled environments + +ไธ€้ƒจใฎใ‚ฏใƒฉใ‚ฆใƒ‰ไธŠใฎGPUใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚„ใ‚คใƒณใƒˆใƒฉใƒใƒƒใƒˆใ‚ปใƒƒใƒˆใ‚ขใƒƒใƒ—ใฏใ€ๅค–้ƒจๆŽฅ็ถšใซๅฏพใ—ใฆใƒ•ใ‚กใ‚คใ‚ขใ‚ฆใ‚ฉใƒผใƒซใงไฟ่ญทใ•ใ‚Œใฆใ„ใ‚‹ใŸใ‚ใ€ๆŽฅ็ถšใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚นใ‚ฏใƒชใƒ—ใƒˆใŒใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚„ใƒ‡ใƒผใ‚ฟใ‚ปใƒƒใƒˆใ‚’ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใ—ใ‚ˆใ†ใจใ™ใ‚‹ใจใ€ใƒ€ใ‚ฆใƒณใƒญใƒผใƒ‰ใŒ้€”ไธญใงๆญขใพใ‚Šใ€ๆฌกใฎใƒกใƒƒใ‚ปใƒผใ‚ธใจใ‚ฟใ‚คใƒ ใ‚ขใ‚ฆใƒˆใ‚จใƒฉใƒผใŒ่กจ็คบใ•ใ‚Œใพใ™๏ผš + +``` +ValueError: Connection error, and we cannot find the requested files in the cached path. +Please try again or make sure your Internet connection is on. +``` + + +ใ“ใฎๅ ดๅˆใ€ๆŽฅ็ถšใ‚จใƒฉใƒผใ‚’ๅ›ž้ฟใ™ใ‚‹ใŸใ‚ใซ[ใ‚ชใƒ•ใƒฉใ‚คใƒณใƒขใƒผใƒ‰](installation#offline-mode)ใง๐Ÿค— Transformersใ‚’ๅฎŸ่กŒใ—ใฆใฟใฆใใ ใ•ใ„ใ€‚ + +## CUDA out of memory + +ๆ•ฐ็™พไธ‡ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใ‚’ๆŒใคๅคง่ฆๆจกใชใƒขใƒ‡ใƒซใฎใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐใฏใ€้ฉๅˆ‡ใชใƒใƒผใƒ‰ใ‚ฆใ‚งใ‚ขใชใ—ใงใฏ่ชฒ้กŒใงใ™ใ€‚GPUใฎใƒกใƒขใƒชใŒไธ่ถณใ™ใ‚‹ใจใ‚ˆใใ‚ใ‚‹ใ‚จใƒฉใƒผใฎ1ใคใฏๆฌกใฎใจใŠใ‚Šใงใ™๏ผš + +ไปฅไธ‹ใฏใƒกใƒขใƒชไฝฟ็”จ้‡ใ‚’ๆธ›ใ‚‰ใ™ใŸใ‚ใซ่ฉฆใ™ใ“ใจใŒใงใใ‚‹ใ„ใใคใ‹ใฎ่งฃๆฑบ็ญ–ใงใ™๏ผš + +- [`TrainingArguments`]ใฎไธญใง [`per_device_train_batch_size`](main_classes/trainer#transformers.TrainingArguments.per_device_train_batch_size) ใฎๅ€คใ‚’ๆธ›ใ‚‰ใ™ใ€‚ +- [`TrainingArguments`]ใฎไธญใง [`gradient_accumulation_steps`](main_classes/trainer#transformers.TrainingArguments.gradient_accumulation_steps) ใ‚’ไฝฟ็”จใ—ใฆใ€ๅ…จไฝ“็š„ใชใƒใƒƒใƒใ‚ตใ‚คใ‚บใ‚’ๅŠนๆžœ็š„ใซๅข—ใ‚„ใ™ใ“ใจใ‚’่ฉฆใ™ใ€‚ + + + +ใƒกใƒขใƒช็ฏ€็ด„ใฎใƒ†ใ‚ฏใƒ‹ใƒƒใ‚ฏใซใคใ„ใฆใฎ่ฉณ็ดฐใฏใ€[ใ‚ฌใ‚คใƒ‰](performance)ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ + + + +## Unable to load a saved TensorFlow model + +TensorFlowใฎ[model.save](https://www.tensorflow.org/tutorials/keras/save_and_load#save_the_entire_model)ใƒกใ‚ฝใƒƒใƒ‰ใฏใ€ใƒขใƒ‡ใƒซๅ…จไฝ“ - ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ€้‡ใฟใ€ใƒˆใƒฌใƒผใƒ‹ใƒณใ‚ฐ่จญๅฎš - ใ‚’1ใคใฎใƒ•ใ‚กใ‚คใƒซใซไฟๅญ˜ใ—ใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใƒขใƒ‡ใƒซใƒ•ใ‚กใ‚คใƒซใ‚’ๅ†ๅบฆ่ชญใฟ่พผใ‚€้š›ใซใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏใ€๐Ÿค— TransformersใŒใƒขใƒ‡ใƒซใƒ•ใ‚กใ‚คใƒซๅ†…ใฎใ™ในใฆใฎTensorFlow้–ข้€ฃใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ชญใฟ่พผใพใชใ„ใŸใ‚ใงใ™ใ€‚TensorFlowใƒขใƒ‡ใƒซใฎไฟๅญ˜ใจ่ชญใฟ่พผใฟใซ้–ขใ™ใ‚‹ๅ•้กŒใ‚’ๅ›ž้ฟใ™ใ‚‹ใŸใ‚ใซใ€ๆฌกใฎใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™๏ผš + +- ใƒขใƒ‡ใƒซใฎ้‡ใฟใ‚’`h5`ใƒ•ใ‚กใ‚คใƒซๆ‹กๅผตๅญใงไฟๅญ˜ใ—ใ€[`~TFPreTrainedModel.from_pretrained`]ใ‚’ไฝฟ็”จใ—ใฆใƒขใƒ‡ใƒซใ‚’ๅ†่ชญใฟ่พผใฟใ™ใ‚‹๏ผš + +```py +>>> from transformers import TFPreTrainedModel + +>>> model.save_weights("some_folder/tf_model.h5") +>>> model = TFPreTrainedModel.from_pretrained("some_folder") +``` + +- Save the model with [`~TFPretrainedModel.save_pretrained`] and load it again with [`~TFPreTrainedModel.from_pretrained`]: + +```py +>>> from transformers import TFPreTrainedModel + +>>> model.save_pretrained("path_to/model") +>>> model = TFPreTrainedModel.from_pretrained("path_to/model") +``` + +## ImportError + +ใ‚‚ใ†ไธ€ใคใ‚ˆใใ‚ใ‚‹ใ‚จใƒฉใƒผใฏใ€็‰นใซๆ–ฐใ—ใใƒชใƒชใƒผใ‚นใ•ใ‚ŒใŸใƒขใƒ‡ใƒซใฎๅ ดๅˆใซ้ญ้‡ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚‹ `ImportError` ใงใ™๏ผš + + +``` +ImportError: cannot import name 'ImageGPTImageProcessor' from 'transformers' (unknown location) +``` + +ใ“ใ‚Œใ‚‰ใฎใ‚จใƒฉใƒผใ‚ฟใ‚คใƒ—ใซ้–ขใ—ใฆใฏใ€ๆœ€ๆ–ฐใƒใƒผใ‚ธใƒงใƒณใฎ ๐Ÿค— Transformers ใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใ€ๆœ€ๆ–ฐใฎใƒขใƒ‡ใƒซใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹ใ‚ˆใ†ใซใ—ใฆใใ ใ•ใ„๏ผš + +```bash +pip install transformers --upgrade +``` + +## CUDA error: device-side assert triggered + +ๆ™‚ใ€…ใ€ใƒ‡ใƒใ‚คใ‚นใ‚ณใƒผใƒ‰ใงใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸใจใ„ใ†ไธ€่ˆฌ็š„ใช CUDA ใ‚จใƒฉใƒผใซ้ญ้‡ใ™ใ‚‹ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ + +``` +RuntimeError: CUDA error: device-side assert triggered +``` + +ใ‚ˆใ‚Šๅ…ทไฝ“็š„ใชใ‚จใƒฉใƒผใƒกใƒƒใ‚ปใƒผใ‚ธใ‚’ๅ–ๅพ—ใ™ใ‚‹ใŸใ‚ใซใ€ใพใšใฏCPUไธŠใงใ‚ณใƒผใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใฟใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ไปฅไธ‹ใฎ็’ฐๅขƒๅค‰ๆ•ฐใ‚’ใ‚ณใƒผใƒ‰ใฎๅ†’้ ญใซ่ฟฝๅŠ ใ—ใฆใ€CPUใซๅˆ‡ใ‚Šๆ›ฟใˆใฆใฟใฆใใ ใ•ใ„๏ผš + +```py +>>> import os + +>>> os.environ["CUDA_VISIBLE_DEVICES"] = "" +``` + +GPUใ‹ใ‚‰ใ‚ˆใ‚Š่‰ฏใ„ใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใ‚’ๅ–ๅพ—ใ™ใ‚‹ๅˆฅใฎใ‚ชใƒ—ใ‚ทใƒงใƒณใฏใ€ๆฌกใฎ็’ฐๅขƒๅค‰ๆ•ฐใ‚’ใ‚ณใƒผใƒ‰ใฎๅ…ˆ้ ญใซ่ฟฝๅŠ ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€ใ‚จใƒฉใƒผใฎ็™บ็”Ÿๆบใ‚’ๆŒ‡ใ™ใƒˆใƒฌใƒผใ‚นใƒใƒƒใ‚ฏใŒๅพ—ใ‚‰ใ‚Œใพใ™๏ผš + +```py +>>> import os + +>>> os.environ["CUDA_LAUNCH_BLOCKING"] = "1" +``` + + +## Incorrect output when padding tokens aren't masked + +ไธ€้ƒจใฎใ‚ฑใƒผใ‚นใงใฏใ€`input_ids`ใซใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒๅซใพใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ๅ‡บๅŠ›ใฎ`hidden_state`ใŒๆญฃใ—ใใชใ„ใ“ใจใŒใ‚ใ‚Šใพใ™ใ€‚ใƒ‡ใƒขใƒณใ‚นใƒˆใƒฌใƒผใ‚ทใƒงใƒณใฎใŸใ‚ใซใ€ใƒขใƒ‡ใƒซใจใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใƒผใ‚’ใƒญใƒผใƒ‰ใ—ใพใ™ใ€‚ใƒขใƒ‡ใƒซใฎ`pad_token_id`ใซใ‚ขใ‚ฏใ‚ปใ‚นใ—ใฆใ€ใใฎๅ€คใ‚’็ขบ่ชใงใใพใ™ใ€‚ไธ€้ƒจใฎใƒขใƒ‡ใƒซใงใฏ`pad_token_id`ใŒ`None`ใซใชใ‚‹ใ“ใจใ‚‚ใ‚ใ‚Šใพใ™ใŒใ€ๅธธใซๆ‰‹ๅ‹•ใง่จญๅฎšใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ + + +```py +>>> from transformers import AutoModelForSequenceClassification +>>> import torch + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-uncased") +>>> model.config.pad_token_id +0 +``` + +ไปฅไธ‹ใฎไพ‹ใฏใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ›ใšใซๅ‡บๅŠ›ใ‚’่กจ็คบใ—ใŸใ‚‚ใฎใงใ™๏ผš + +```py +>>> input_ids = torch.tensor([[7592, 2057, 2097, 2393, 9611, 2115], [7592, 0, 0, 0, 0, 0]]) +>>> output = model(input_ids) +>>> print(output.logits) +tensor([[ 0.0082, -0.2307], + [ 0.1317, -0.1683]], grad_fn=) +``` + +ไปฅไธ‹ใฏใ€็ฌฌ2ใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๅฎŸ้š›ใฎๅ‡บๅŠ›ใงใ™๏ผš + +```py +>>> input_ids = torch.tensor([[7592]]) +>>> output = model(input_ids) +>>> print(output.logits) +tensor([[-0.1008, -0.4061]], grad_fn=) +``` + +ๅคงๆŠตใฎๅ ดๅˆใ€ใƒขใƒ‡ใƒซใซใฏ `attention_mask` ใ‚’ๆไพ›ใ—ใฆใ€ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’็„ก่ฆ–ใ—ใ€ใ“ใฎใ‚ˆใ†ใช็„ก้Ÿณใฎใ‚จใƒฉใƒผใ‚’ๅ›ž้ฟใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใซใ‚ˆใ‚Šใ€2็•ช็›ฎใฎใ‚ทใƒผใ‚ฑใƒณใ‚นใฎๅ‡บๅŠ›ใŒๅฎŸ้š›ใฎๅ‡บๅŠ›ใจไธ€่‡ดใ™ใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ™ใ€‚ + + + +ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฏใ€ใƒˆใƒผใ‚ฏใƒŠใ‚คใ‚ถใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใซๅŸบใฅใ„ใฆ `attention_mask` ใ‚’่‡ชๅ‹•ใงไฝœๆˆใ—ใพใ™ใ€‚ + + + +```py +>>> attention_mask = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0]]) +>>> output = model(input_ids, attention_mask=attention_mask) +>>> print(output.logits) +tensor([[ 0.0082, -0.2307], + [-0.1008, -0.4061]], grad_fn=) +``` + +๐Ÿค— Transformersใฏใ€ๆไพ›ใ•ใ‚Œใ‚‹ใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใ‚’ใƒžใ‚นใ‚ฏใ™ใ‚‹ใŸใ‚ใซ่‡ชๅ‹•็š„ใซ`attention_mask`ใ‚’ไฝœๆˆใ—ใพใ›ใ‚“ใ€‚ใใฎ็†็”ฑใฏไปฅไธ‹ใฎ้€šใ‚Šใงใ™๏ผš + +- ไธ€้ƒจใฎใƒขใƒ‡ใƒซใซใฏใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใŒๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ +- ไธ€้ƒจใฎใƒฆใƒผใ‚นใ‚ฑใƒผใ‚นใงใฏใ€ใƒฆใƒผใ‚ถใƒผใŒใƒ‘ใƒ‡ใ‚ฃใƒณใ‚ฐใƒˆใƒผใ‚ฏใƒณใซใ‚ขใƒ†ใƒณใ‚ทใƒงใƒณใ‚’ๅ‘ใ‘ใ‚‹ใ“ใจใ‚’ๆœ›ใ‚€ๅ ดๅˆใŒใ‚ใ‚‹ใŸใ‚ใงใ™ใ€‚ + +## ValueError: Unrecognized configuration class XYZ for this kind of AutoModel + +ไธ€่ˆฌ็š„ใซใ€ไบ‹ๅ‰ๅญฆ็ฟ’ๆธˆใฟใƒขใƒ‡ใƒซใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใซใฏ[`AutoModel`]ใ‚ฏใƒฉใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚ใ“ใฎใ‚ฏใƒฉใ‚นใฏใ€่จญๅฎšใซๅŸบใฅใ„ใฆไธŽใˆใ‚‰ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใ‹ใ‚‰ๆญฃใ—ใ„ใ‚ขใƒผใ‚ญใƒ†ใ‚ฏใƒใƒฃใ‚’่‡ชๅ‹•็š„ใซๆŽจๆธฌใŠใ‚ˆใณใƒญใƒผใƒ‰ใงใใพใ™ใ€‚ใƒขใƒ‡ใƒซใ‚’ใƒญใƒผใƒ‰ใ™ใ‚‹้š›ใซใ“ใฎ`ValueError`ใŒ่กจ็คบใ•ใ‚Œใ‚‹ๅ ดๅˆใ€Autoใ‚ฏใƒฉใ‚นใฏไธŽใˆใ‚‰ใ‚ŒใŸใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใฎ่จญๅฎšใ‹ใ‚‰ใ€ใƒญใƒผใƒ‰ใ—ใ‚ˆใ†ใจใ—ใฆใ„ใ‚‹ใƒขใƒ‡ใƒซใฎ็จฎ้กžใธใฎใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’่ฆ‹ใคใ‘ใ‚‹ใ“ใจใŒใงใใชใ‹ใฃใŸใ“ใจใ‚’ๆ„ๅ‘ณใ—ใพใ™ใ€‚ๆœ€ใ‚‚ไธ€่ˆฌ็š„ใซใฏใ€็‰นๅฎšใฎใ‚ฟใ‚นใ‚ฏใ‚’ใ‚ตใƒใƒผใƒˆใ—ใชใ„ใƒใ‚งใƒƒใ‚ฏใƒใ‚คใƒณใƒˆใŒใ‚ใ‚‹ๅ ดๅˆใซใ“ใฎใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใพใ™ใ€‚ +ไพ‹ใˆใฐใ€่ณชๅ•ๅฟœ็ญ”ใฎใŸใ‚ใฎGPT2ใŒๅญ˜ๅœจใ—ใชใ„ๅ ดๅˆใ€ๆฌกใฎไพ‹ใงใ“ใฎใ‚จใƒฉใƒผใŒ่กจ็คบใ•ใ‚Œใพใ™๏ผš + +ไธŠ่จ˜ใฎใƒ†ใ‚ญใ‚นใƒˆใ‚’ๆ—ฅๆœฌ่ชžใซ็ฟป่จณใ—ใ€Markdownใƒ•ใ‚กใ‚คใƒซใจใ—ใฆใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆใ—ใพใ—ใŸใ€‚ + + +```py +>>> from transformers import AutoProcessor, AutoModelForQuestionAnswering + +>>> processor = AutoProcessor.from_pretrained("openai-community/gpt2-medium") +>>> model = AutoModelForQuestionAnswering.from_pretrained("openai-community/gpt2-medium") +ValueError: Unrecognized configuration class for this kind of AutoModel: AutoModelForQuestionAnswering. +Model type should be one of AlbertConfig, BartConfig, BertConfig, BigBirdConfig, BigBirdPegasusConfig, BloomConfig, ... +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_config.py b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_config.py new file mode 100644 index 0000000000000000000000000000000000000000..ab61af6ef9e86028ae075e01fb244ae4226f387b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_config.py @@ -0,0 +1,14 @@ +# docstyle-ignore +INSTALL_CONTENT = """ +# Transformers ์„ค์น˜ ๋ฐฉ๋ฒ• +! pip install transformers datasets evaluate accelerate +# ๋งˆ์ง€๋ง‰ ๋ฆด๋ฆฌ์Šค ๋Œ€์‹  ์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๋ ค๋ฉด, ์œ„ ๋ช…๋ น์„ ์ฃผ์„์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์•„๋ž˜ ๋ช…๋ น์„ ํ•ด์ œํ•˜์„ธ์š”. +# ! pip install git+https://github.com/huggingface/transformers.git +""" + +notebook_first_cells = [{"type": "code", "content": INSTALL_CONTENT}] +black_avoid_patterns = { + "{processor_class}": "FakeProcessorClass", + "{model_class}": "FakeModelClass", + "{object_class}": "FakeObjectClass", +} diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_toctree.yml b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_toctree.yml new file mode 100644 index 0000000000000000000000000000000000000000..1fffffabe1244fd302761802a21bae2334e012ba --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/_toctree.yml @@ -0,0 +1,1246 @@ +- sections: + - local: index + title: ๐Ÿค— Transformers + - local: installation + title: ์„ค์น˜๋ฐฉ๋ฒ• + - local: quicktour + title: ๋‘˜๋Ÿฌ๋ณด๊ธฐ + title: ์‹œ์ž‘ํ•˜๊ธฐ +- isExpanded: false + sections: + - sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Loading models + - local: custom_models + title: ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ ๊ณต์œ ํ•˜๊ธฐ + - local: how_to_hack_models + title: ๋ชจ๋ธ ๊ตฌ์„ฑ ์š”์†Œ ๋งž์ถค ์„ค์ •ํ•˜๊ธฐ + - local: model_sharing + title: ๋งŒ๋“  ๋ชจ๋ธ ๊ณต์œ ํ•˜๊ธฐ + - local: modular_transformers + title: transformers์—์„œ์˜ ๋ชจ๋“ˆ์„ฑ + - local: add_new_model + title: ๐Ÿค— Transformers์— ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Documenting a model + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Customizing attention function + title: ๋ชจ๋ธ + - sections: + - local: fast_tokenizers + title: ๐Ÿค— Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํ† ํฌ๋‚˜์ด์ € ์‚ฌ์šฉํ•˜๊ธฐ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Image processors + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Video processors + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Backbones + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Feature extractors + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Processors + - local: tokenizer_summary + title: ํ† ํฌ๋‚˜์ด์ € ์š”์•ฝ + - local: pad_truncation + title: ํŒจ๋”ฉ๊ณผ ์ž˜๋ผ๋‚ด๊ธฐ + title: ์ „์ฒ˜๋ฆฌ๊ธฐ(Preprocessors) + title: Base classes +- isExpanded: false + sections: + - sections: + - local: pipeline_tutorial + title: Pipeline์œผ๋กœ ์ถ”๋ก ํ•˜๊ธฐ + - local: pipeline_gradio + title: ๋จธ์‹ ๋Ÿฌ๋‹ ์•ฑ + - local: pipeline_webserver + title: ์ถ”๋ก  ์›น ์„œ๋ฒ„๋ฅผ ์œ„ํ•œ ํŒŒ์ดํ”„๋ผ์ธ + - local: add_new_pipeline + title: ์–ด๋–ป๊ฒŒ ๐Ÿค— Transformers์— ํŒŒ์ดํ”„๋ผ์ธ์„ ์ถ”๊ฐ€ํ•˜๋‚˜์š”? + title: ํŒŒ์ดํ”„๋ผ์ธ API + - sections: + - local: llm_tutorial + title: ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ๋กœ ์ƒ์„ฑํ•˜๊ธฐ + - local: generation_strategies + title: ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต ์‚ฌ์šฉ์ž ์ •์˜ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Generation features + - local: tasks/prompting + title: ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ํ”„๋กฌํ”„ํŒ… ๊ฐ€์ด๋“œ + - local: llm_optims + title: LLM ์ถ”๋ก  ์ตœ์ ํ™” + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Caching + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) KV cache strategies + - local: serving + title: ๋ชจ๋ธ ์„œ๋น™ํ•˜๊ธฐ + - local: llm_tutorial_optimization + title: LLM์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๊ธฐ + - local: perplexity + title: ๊ณ ์ • ๊ธธ์ด ๋ชจ๋ธ์˜ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ(Perplexity) + title: ๊ฑฐ๋Œ€ ์–ธ์–ด ๋ชจ๋ธ(LLMs) + - sections: + - local: conversations + title: Transformers๋กœ ์ฑ„ํŒ…ํ•˜๊ธฐ + - local: chat_templating + title: ์ฑ—๋ด‡ ํ…œํ”Œ๋ฆฟ ์ตํžˆ๊ธฐ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Multimodal templates + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Template writing + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Tools and RAG + title: ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋Œ€ํ™”ํ•˜๊ธฐ + - sections: + - local: tiny_agents + title: Tiny-Agents CLI ๋ฐ MCP ๋„๊ตฌ + title: ์„œ๋น™(Serving) + - sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) torch.compile + - local: perf_infer_gpu_one + title: ํ•˜๋‚˜์˜ GPU๋ฅผ ํ™œ์šฉํ•œ ์ถ”๋ก  + - local: perf_infer_gpu_multi + title: (๋ฒˆ์—ญ์ค‘) Distributed inference + - local: perf_infer_cpu + title: CPU๋กœ ์ถ”๋ก ํ•˜๊ธฐ + title: ์ตœ์ ํ™”(Optimization) + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Agents + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Tools + title: ์ถ”๋ก (Inference) +- isExpanded: false + sections: + - sections: + - local: trainer + title: ํŠธ๋ ˆ์ด๋„ˆ(Trainer) + - local: training + title: ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ + - local: optimizers + title: ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizers) + - local: hpo_train + title: Trainer API๋ฅผ ์‚ฌ์šฉํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ + title: Trainer API + - sections: + - local: accelerator_selection + title: (๋ฒˆ์—ญ์ค‘) Accelerator selection + - local: accelerate + title: ๐Ÿค— Accelerate๋กœ ๋ถ„์‚ฐ ํ•™์Šต ๊ตฌ์„ฑํ•˜๊ธฐ + - local: fsdp + title: ์™„์ „ ๋ถ„ํ•  ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ + - local: deepspeed + title: DeepSpeed + - local: debugging + title: ๋””๋ฒ„๊น… + - local: perf_train_cpu_many + title: ๋‹ค์ค‘ CPU์—์„œ ํ•™์Šตํ•˜๊ธฐ + - local: perf_train_gpu_many + title: ๋‹ค์ค‘ GPU์—์„œ ํ•™์Šต ์ง„ํ–‰ํ•˜๊ธฐ + title: ๋ถ„์‚ฐ ํ•™์Šต(Distributed training) + - sections: + - local: perf_train_gpu_one + title: GPU + - local: perf_train_cpu + title: CPU์—์„œ ํ›ˆ๋ จ + - local: perf_train_special + title: Apple ์‹ค๋ฆฌ์ฝ˜์—์„œ PyTorch ํ•™์Šต + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Intel Gaudi + - local: perf_hardware + title: ํ›ˆ๋ จ์šฉ ์‚ฌ์šฉ์ž ๋งž์ถคํ˜• ํ•˜๋“œ์›จ์–ด + title: ํ•˜๋“œ์›จ์–ด + - local: peft + title: ๐Ÿค— PEFT๋กœ ์–ด๋Œ‘ํ„ฐ ๋กœ๋“œ ๋ฐ ํ•™์Šตํ•˜๊ธฐ + - local: model_memory_anatomy + title: ๋ชจ๋ธ ํ•™์Šต ํ•ด๋ถ€ํ•˜๊ธฐ + title: ํ•™์Šต(Training) +- isExpanded: false + sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Overview + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Selecting a quantization method + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Quantization concepts + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) AQLM + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) AutoRound + - local: quantization/awq + title: AWQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) BitNet + - local: quantization/bitsandbytes + title: bitsandbytes + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) compressed-tensors + - local: quantization/eetq + title: EETQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) FBGEMM + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Fine-grained FP8 + - local: gguf + title: GGUF ํŒŒ์ผ๋“ค๊ณผ์˜ ์ƒํ˜ธ ์šด์šฉ์„ฑ + - local: quantization/gptq + title: GPTQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) HIGGS + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) HQQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Optimum + - local: quantization/quanto + title: Quanto + - local: quantization/quark + title: Quark + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) torchao + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) SpQR + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) VPTQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Contribute + title: ์–‘์žํ™”(Quantization) +- isExpanded: false + sections: + - local: serialization + title: ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ + - local: tflite + title: TFLite๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ + - local: executorch + title: ExecuTorch + - local: torchscript + title: TorchScript๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ + title: ๋ฐฐํฌํ™˜๊ฒฝ์— ๋‚ด๋ณด๋‚ด๊ธฐ +- isExpanded: false + sections: + - sections: + - sections: + - local: tasks/sequence_classification + title: ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ + - local: tasks/token_classification + title: ํ† ํฐ ๋ถ„๋ฅ˜ + - local: tasks/question_answering + title: ์งˆ์˜ ์‘๋‹ต(Question Answering) + - local: tasks/language_modeling + title: ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(Causal language modeling) + - local: tasks/masked_language_modeling + title: ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง(Masked language modeling) + - local: tasks/translation + title: ๋ฒˆ์—ญ + - local: tasks/summarization + title: ์š”์•ฝ + - local: tasks/multiple_choice + title: ๊ฐ๊ด€์‹ ๋ฌธ์ œ(Multiple Choice) + title: ์ž์—ฐ์–ด์ฒ˜๋ฆฌ + - sections: + - local: tasks/audio_classification + title: ์˜ค๋””์˜ค ๋ถ„๋ฅ˜ + - local: tasks/asr + title: ์ž๋™ ์Œ์„ฑ ์ธ์‹ + title: ์˜ค๋””์˜ค + - sections: + - local: tasks/image_classification + title: ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ + - local: tasks/semantic_segmentation + title: ์ด๋ฏธ์ง€ ์„ธ๊ทธ๋ฉ˜ํ…Œ์ด์…˜ + - local: tasks/video_classification + title: ๋น„๋””์˜ค ๋ถ„๋ฅ˜ + - local: tasks/object_detection + title: ๊ฐ์ฒด ํƒ์ง€(Object detection) + - local: tasks/zero_shot_object_detection + title: ์ œ๋กœ์ƒท(zero-shot) ๊ฐ์ฒด ํƒ์ง€ + - local: tasks/zero_shot_image_classification + title: ์ œ๋กœ์ƒท(zero-shot) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ + - local: tasks/monocular_depth_estimation + title: ๋‹จ์ผ ์˜์ƒ ๊ธฐ๋ฐ˜ ๊นŠ์ด ์ถ”์ • + - local: tasks/image_to_image + title: Image-to-Image + - local: tasks/image_feature_extraction + title: ์ด๋ฏธ์ง€ ํŠน์ง• ์ถ”์ถœ + - local: tasks/mask_generation + title: ๋งˆ์Šคํฌ ์ƒ์„ฑ + - local: tasks/keypoint_detection + title: ํ‚คํฌ์ธํŠธ ํƒ์ง€ + - local: tasks/knowledge_distillation_for_image_classification + title: ์ปดํ“จํ„ฐ ๋น„์ „(์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜)๋ฅผ ์œ„ํ•œ ์ง€์‹ ์ฆ๋ฅ˜(knowledge distillation) + title: ์ปดํ“จํ„ฐ ๋น„์ „ + - sections: + - local: tasks/image_captioning + title: ์ด๋ฏธ์ง€ ์บก์…”๋‹ + - local: tasks/document_question_answering + title: ๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต(Document Question Answering) + - local: tasks/visual_question_answering + title: ์‹œ๊ฐ์  ์งˆ์˜์‘๋‹ต (Visual Question Answering) + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Text to speech + - local: tasks/idefics + title: IDEFICS๋ฅผ ์ด์šฉํ•œ ์ด๋ฏธ์ง€ ์ž‘์—… + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Image-text-to-text + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Video-text-to-text + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Visual Document Retrieval + title: ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ + title: ํƒœ์Šคํฌ ๋ ˆ์‹œํ”ผ + - local: run_scripts + title: ์Šคํฌ๋ฆฝํŠธ๋กœ ํ•™์Šตํ•˜๊ธฐ + - local: glossary + title: Glossary + - local: philosophy + title: ์ด๋…๊ณผ ๋ชฉํ‘œ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Notebooks with examples + - local: community + title: ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฆฌ์†Œ์Šค + - local: troubleshooting + title: ๋ฌธ์ œ ํ•ด๊ฒฐ + - local: gguf + title: GGUF ํŒŒ์ผ๋“ค๊ณผ์˜ ์ƒํ˜ธ ์šด์šฉ์„ฑ + - local: modular_transformers + title: transformers์—์„œ์˜ ๋ชจ๋“ˆ์„ฑ + title: (๋ฒˆ์—ญ์ค‘) ๊ฐœ๋ฐœ์ž ๊ฐ€์ด๋“œ +- sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Getting started + - local: quantization/bitsandbytes + title: bitsandbytes + - local: quantization/gptq + title: GPTQ + - local: quantization/awq + title: AWQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) AQLM + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) VPTQ + - local: quantization/quanto + title: Quanto + - local: quantization/quark + title: Quark + - local: quantization/eetq + title: EETQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) HQQ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Optimum + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Contribute new quantization method + title: (๋ฒˆ์—ญ์ค‘) ๊ฒฝ๋Ÿ‰ํ™” ๋ฉ”์†Œ๋“œ +- sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Quantization + - local: llm_optims + title: LLM ์ถ”๋ก  ์ตœ์ ํ™” + - local: cache_explanation + title: ์–ดํ…์…˜ ํ–‰๋ ฌ ์บ์‹ฑ + - sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Methods and tools for efficient training on a single GPU + - local: perf_train_gpu_many + title: ๋‹ค์ค‘ GPU์—์„œ ํ›ˆ๋ จ ์ง„ํ–‰ํ•˜๊ธฐ + - local: deepspeed + title: DeepSpeed + - local: fsdp + title: ์™„์ „ ๋ถ„ํ•  ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ + - local: perf_train_cpu + title: CPU์—์„œ ํ›ˆ๋ จ + - local: perf_train_cpu_many + title: ๋‹ค์ค‘ CPU์—์„œ ํ›ˆ๋ จํ•˜๊ธฐ + - local: perf_train_special + title: Apple ์‹ค๋ฆฌ์ฝ˜์—์„œ PyTorch ํ•™์Šต + - local: perf_hardware + title: ํ›ˆ๋ จ์šฉ ์‚ฌ์šฉ์ž ๋งž์ถคํ˜• ํ•˜๋“œ์›จ์–ด + - local: hpo_train + title: Trainer API๋ฅผ ์‚ฌ์šฉํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ + title: (๋ฒˆ์—ญ์ค‘) ํšจ์œจ์ ์ธ ํ•™์Šต ๊ธฐ์ˆ ๋“ค + - sections: + - local: perf_infer_cpu + title: CPU๋กœ ์ถ”๋ก ํ•˜๊ธฐ + - local: perf_infer_gpu_one + title: ํ•˜๋‚˜์˜ GPU๋ฅผ ํ™œ์šฉํ•œ ์ถ”๋ก  + title: ์ถ”๋ก  ์ตœ์ ํ™”ํ•˜๊ธฐ + - local: debugging + title: ๋””๋ฒ„๊น… + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Optimize inference using `torch.compile()` + title: (๋ฒˆ์—ญ์ค‘) ์„ฑ๋Šฅ ๋ฐ ํ™•์žฅ์„ฑ +- sections: + - local: contributing + title: ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ• + - local: add_new_model + title: ๐Ÿค— Transformers์— ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• + - local: add_new_pipeline + title: ์–ด๋–ป๊ฒŒ ๐Ÿค— Transformers์— ํŒŒ์ดํ”„๋ผ์ธ์„ ์ถ”๊ฐ€ํ•˜๋‚˜์š”? + - local: testing + title: ํ…Œ์ŠคํŠธ + - local: pr_checks + title: Pull Request์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ + title: ๋ฆฌ์†Œ์Šค +- isExpanded: false + sections: + - local: contributing + title: ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ• + - local: testing + title: Transformers ๋ชจ๋ธ ํ…Œ์ŠคํŠธํ•˜๊ธฐ + - local: pr_checks + title: Pull request ๊ฒ€์‚ฌํ•˜๊ธฐ + title: ๊ธฐ์—ฌํ•˜๊ธฐ +- isExpanded: false + sections: + - sections: + - local: model_doc/auto + title: Auto Classes + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Backbones + - local: main_classes/callback + title: Callbacks + - local: main_classes/configuration + title: Configuration + - local: main_classes/data_collator + title: Data Collator + - local: main_classes/keras_callbacks + title: Keras callbacks + - local: main_classes/logging + title: Logging + - local: main_classes/model + title: Models + - local: main_classes/text_generation + title: ํ…์ŠคํŠธ ์ƒ์„ฑ + - local: main_classes/onnx + title: ONNX + - local: main_classes/optimizer_schedules + title: ์ตœ์ ํ™” + - local: main_classes/output + title: ๋ชจ๋ธ ์ถœ๋ ฅ + - local: main_classes/peft + title: PEFT + - local: main_classes/pipelines + title: ํŒŒ์ดํ”„๋ผ์ธ + - local: main_classes/processors + title: ํ”„๋กœ์„ธ์„œ + - local: main_classes/quantization + title: ์–‘์žํ™” + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Tokenizer + - local: main_classes/tokenizer + title: ํ† ํฌ๋‚˜์ด์ € + - local: main_classes/trainer + title: Trainer + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) DeepSpeed + - local: in_translation + title: ExecuTorch + - local: main_classes/feature_extractor + title: ํ”ผ์ณ ์ถ”์ถœ๊ธฐ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Image Processor + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Video Processor + title: ๋ฉ”์ธ ํด๋ž˜์Šค + - sections: + - sections: + - local: model_doc/albert + title: ALBERT + - local: in_translation + title: Arcee + - local: in_translation + title: Bamba + - local: model_doc/bart + title: BART + - local: model_doc/barthez + title: BARThez + - local: model_doc/bartpho + title: BARTpho + - local: model_doc/bert + title: BERT + - local: in_translation + title: BertGeneration + - local: model_doc/bert-japanese + title: BertJapanese + - local: model_doc/bertweet + title: BERTweet + - local: in_translation + title: BigBird + - local: in_translation + title: BigBirdPegasus + - local: model_doc/biogpt + title: BioGpt + - local: in_translation + title: BitNet + - local: in_translation + title: Blenderbot + - local: in_translation + title: Blenderbot Small + - local: in_translation + title: BLOOM + - local: in_translation + title: BORT + - local: in_translation + title: ByT5 + - local: in_translation + title: CamemBERT + - local: in_translation + title: CANINE + - local: model_doc/codegen + title: CodeGen + - local: in_translation + title: CodeLlama + - local: model_doc/cohere + title: Cohere + - local: in_translation + title: Cohere2 + - local: model_doc/convbert + title: ConvBERT + - local: in_translation + title: CPM + - local: in_translation + title: CPMANT + - local: in_translation + title: CTRL + - local: model_doc/dbrx + title: DBRX + - local: model_doc/deberta + title: DeBERTa + - local: model_doc/deberta-v2 + title: DeBERTa-v2 + - local: in_translation + title: DeepSeek-V3 + - local: in_translation + title: DialoGPT + - local: in_translation + title: DiffLlama + - local: in_translation + title: DistilBERT + - local: in_translation + title: Doge + - local: in_translation + title: dots1 + - local: in_translation + title: DPR + - local: model_doc/electra + title: ELECTRA + - local: model_doc/encoder-decoder + title: Encoder Decoder Models + - local: in_translation + title: ERNIE + - local: in_translation + title: ErnieM + - local: model_doc/esm + title: ESM + - local: model_doc/exaone4 + title: EXAONE-4.0 + - local: in_translation + title: Falcon + - local: in_translation + title: Falcon3 + - local: in_translation + title: FalconH1 + - local: in_translation + title: FalconMamba + - local: in_translation + title: FLAN-T5 + - local: in_translation + title: FLAN-UL2 + - local: in_translation + title: FlauBERT + - local: in_translation + title: FNet + - local: in_translation + title: FSMT + - local: in_translation + title: Funnel Transformer + - local: in_translation + title: Fuyu + - local: model_doc/gemma + title: Gemma + - local: model_doc/gemma2 + title: Gemma2 + - local: in_translation + title: GLM + - local: in_translation + title: glm4 + - local: model_doc/openai-gpt + title: GPT + - local: in_translation + title: GPT Neo + - local: in_translation + title: GPT NeoX + - local: model_doc/gpt_neox_japanese + title: GPT NeoX Japanese + - local: in_translation + title: GPT-J + - local: model_doc/gpt2 + title: GPT-2 + - local: in_translation + title: GPTBigCode + - local: in_translation + title: GPTSAN Japanese + - local: in_translation + title: GPTSw3 + - local: in_translation + title: Granite + - local: in_translation + title: GraniteMoe + - local: in_translation + title: GraniteMoeHybrid + - local: in_translation + title: GraniteMoeShared + - local: in_translation + title: Helium + - local: in_translation + title: HerBERT + - local: in_translation + title: HGNet-V2 + - local: in_translation + title: I-BERT + - local: model_doc/jamba + title: Jamba + - local: in_translation + title: JetMoe + - local: in_translation + title: Jukebox + - local: in_translation + title: LED + - local: in_translation + title: LFM2 + - local: model_doc/llama + title: LLaMA + - local: model_doc/llama2 + title: Llama2 + - local: model_doc/llama3 + title: Llama3 + - local: in_translation + title: Longformer + - local: in_translation + title: LongT5 + - local: in_translation + title: LUKE + - local: in_translation + title: M2M100 + - local: in_translation + title: MADLAD-400 + - local: model_doc/mamba + title: Mamba + - local: model_doc/mamba2 + title: Mamba2 + - local: model_doc/marian + title: MarianMT + - local: in_translation + title: MarkupLM + - local: in_translation + title: MBart and MBart-50 + - local: in_translation + title: MEGA + - local: in_translation + title: MegatronBERT + - local: in_translation + title: MegatronGPT2 + - local: in_translation + title: MiniMax + - local: model_doc/mistral + title: Mistral + - local: in_translation + title: Mixtral + - local: in_translation + title: mLUKE + - local: in_translation + title: MobileBERT + - local: in_translation + title: ModernBert + - local: in_translation + title: ModernBERTDecoder + - local: in_translation + title: MPNet + - local: in_translation + title: MPT + - local: in_translation + title: MRA + - local: in_translation + title: MT5 + - local: in_translation + title: MVP + - local: in_translation + title: myt5 + - local: in_translation + title: Nemotron + - local: in_translation + title: NEZHA + - local: in_translation + title: NLLB + - local: in_translation + title: NLLB-MoE + - local: in_translation + title: Nystrรถmformer + - local: in_translation + title: OLMo + - local: in_translation + title: OLMo2 + - local: in_translation + title: OLMoE + - local: in_translation + title: Open-Llama + - local: in_translation + title: OPT + - local: in_translation + title: Pegasus + - local: in_translation + title: PEGASUS-X + - local: in_translation + title: Persimmon + - local: in_translation + title: Phi + - local: in_translation + title: Phi-3 + - local: in_translation + title: PhiMoE + - local: in_translation + title: PhoBERT + - local: in_translation + title: PLBart + - local: in_translation + title: ProphetNet + - local: in_translation + title: QDQBert + - local: in_translation + title: Qwen2 + - local: in_translation + title: Qwen2MoE + - local: in_translation + title: Qwen3 + - local: in_translation + title: Qwen3MoE + - local: model_doc/rag + title: RAG + - local: in_translation + title: REALM + - local: in_translation + title: RecurrentGemma + - local: in_translation + title: Reformer + - local: in_translation + title: RemBERT + - local: in_translation + title: RetriBERT + - local: model_doc/roberta + title: RoBERTa + - local: in_translation + title: RoBERTa-PreLayerNorm + - local: in_translation + title: RoCBert + - local: in_translation + title: RoFormer + - local: in_translation + title: RWKV + - local: in_translation + title: Splinter + - local: in_translation + title: SqueezeBERT + - local: in_translation + title: StableLm + - local: in_translation + title: Starcoder2 + - local: in_translation + title: SwitchTransformers + - local: in_translation + title: T5 + - local: in_translation + title: T5Gemma + - local: in_translation + title: T5v1.1 + - local: in_translation + title: TAPEX + - local: in_translation + title: Transformer XL + - local: in_translation + title: UL2 + - local: in_translation + title: UMT5 + - local: in_translation + title: X-MOD + - local: in_translation + title: XGLM + - local: in_translation + title: XLM + - local: in_translation + title: XLM-ProphetNet + - local: in_translation + title: XLM-RoBERTa + - local: in_translation + title: XLM-RoBERTa-XL + - local: in_translation + title: XLM-V + - local: in_translation + title: XLNet + - local: in_translation + title: YOSO + - local: in_translation + title: Zamba + - local: in_translation + title: Zamba2 + title: ํ…์ŠคํŠธ ๋ชจ๋ธ + - sections: + - local: in_translation + title: Aimv2 + - local: in_translation + title: BEiT + - local: in_translation + title: BiT + - local: in_translation + title: Conditional DETR + - local: in_translation + title: ConvNeXT + - local: in_translation + title: ConvNeXTV2 + - local: in_translation + title: CvT + - local: in_translation + title: D-FINE + - local: in_translation + title: DAB-DETR + - local: in_translation + title: DeepSeek-V2 + - local: in_translation + title: Deformable DETR + - local: in_translation + title: DeiT + - local: in_translation + title: Depth Anything + - local: in_translation + title: Depth Anything V2 + - local: in_translation + title: DepthPro + - local: in_translation + title: DETA + - local: in_translation + title: DETR + - local: in_translation + title: DiNAT + - local: in_translation + title: DINOV2 + - local: in_translation + title: DINOv2 with Registers + - local: in_translation + title: DiT + - local: in_translation + title: DPT + - local: in_translation + title: EfficientFormer + - local: in_translation + title: EfficientNet + - local: in_translation + title: EoMT + - local: in_translation + title: FocalNet + - local: in_translation + title: GLPN + - local: in_translation + title: Hiera + - local: in_translation + title: I-JEPA + - local: in_translation + title: ImageGPT + - local: in_translation + title: LeViT + - local: in_translation + title: LightGlue + - local: in_translation + title: Mask2Former + - local: in_translation + title: MaskFormer + - local: in_translation + title: MLCD + - local: in_translation + title: MobileNetV1 + - local: in_translation + title: MobileNetV2 + - local: in_translation + title: MobileViT + - local: in_translation + title: MobileViTV2 + - local: in_translation + title: NAT + - local: in_translation + title: PoolFormer + - local: in_translation + title: Prompt Depth Anything + - local: in_translation + title: Pyramid Vision Transformer (PVT) + - local: in_translation + title: Pyramid Vision Transformer v2 (PVTv2) + - local: in_translation + title: RegNet + - local: in_translation + title: ResNet + - local: in_translation + title: RT-DETR + - local: in_translation + title: RT-DETRv2 + - local: in_translation + title: SegFormer + - local: in_translation + title: SegGpt + - local: in_translation + title: SuperGlue + - local: in_translation + title: SuperPoint + - local: in_translation + title: SwiftFormer + - local: model_doc/swin + title: Swin Transformer + - local: model_doc/swinv2 + title: Swin Transformer V2 + - local: model_doc/swin2sr + title: Swin2SR + - local: in_translation + title: Table Transformer + - local: in_translation + title: TextNet + - local: in_translation + title: Timm Wrapper + - local: in_translation + title: UperNet + - local: in_translation + title: VAN + - local: model_doc/vit + title: Vision Transformer (ViT) + - local: in_translation + title: ViT Hybrid + - local: in_translation + title: ViTDet + - local: in_translation + title: ViTMAE + - local: in_translation + title: ViTMatte + - local: in_translation + title: ViTMSN + - local: in_translation + title: ViTPose + - local: in_translation + title: YOLOS + - local: in_translation + title: ZoeDepth + title: ๋น„์ „ ๋ชจ๋ธ + - sections: + - local: in_translation + title: Audio Spectrogram Transformer + - local: in_translation + title: Bark + - local: in_translation + title: CLAP + - local: in_translation + title: CSM + - local: in_translation + title: dac + - local: in_translation + title: Dia + - local: in_translation + title: EnCodec + - local: in_translation + title: FastSpeech2Conformer + - local: in_translation + title: GraniteSpeech + - local: in_translation + title: Hubert + - local: in_translation + title: Kyutai Speech-To-Text + - local: in_translation + title: MCTCT + - local: in_translation + title: Mimi + - local: in_translation + title: MMS + - local: in_translation + title: Moonshine + - local: in_translation + title: Moshi + - local: in_translation + title: MusicGen + - local: in_translation + title: MusicGen Melody + - local: in_translation + title: Pop2Piano + - local: in_translation + title: Seamless-M4T + - local: in_translation + title: SeamlessM4T-v2 + - local: in_translation + title: SEW + - local: in_translation + title: SEW-D + - local: in_translation + title: Speech2Text + - local: in_translation + title: Speech2Text2 + - local: in_translation + title: SpeechT5 + - local: in_translation + title: UniSpeech + - local: in_translation + title: UniSpeech-SAT + - local: in_translation + title: UnivNet + - local: in_translation + title: VITS + - local: in_translation + title: Wav2Vec2 + - local: in_translation + title: Wav2Vec2-BERT + - local: in_translation + title: Wav2Vec2-Conformer + - local: in_translation + title: Wav2Vec2Phoneme + - local: in_translation + title: WavLM + - local: model_doc/whisper + title: Whisper + - local: in_translation + title: XLS-R + - local: in_translation + title: XLSR-Wav2Vec2 + title: ์˜ค๋””์˜ค ๋ชจ๋ธ + - sections: + - local: model_doc/timesformer + title: TimeSformer + - local: in_translation + title: V-JEPA 2 + - local: in_translation + title: VideoMAE + - local: model_doc/vivit + title: ViViT + title: ๋น„๋””์˜ค ๋ชจ๋ธ + - sections: + - local: in_translation + title: ALIGN + - local: model_doc/altclip + title: AltCLIP + - local: in_translation + title: Aria + - local: in_translation + title: AyaVision + - local: model_doc/blip + title: BLIP + - local: model_doc/blip-2 + title: BLIP-2 + - local: in_translation + title: BridgeTower + - local: in_translation + title: BROS + - local: model_doc/chameleon + title: Chameleon + - local: in_translation + title: Chinese-CLIP + - local: model_doc/clip + title: CLIP + - local: in_translation + title: CLIPSeg + - local: in_translation + title: CLVP + - local: in_translation + title: ColPali + - local: in_translation + title: ColQwen2 + - local: in_translation + title: Data2Vec + - local: in_translation + title: DePlot + - local: in_translation + title: Donut + - local: in_translation + title: Emu3 + - local: in_translation + title: FLAVA + - local: model_doc/gemma3 + title: Gemma3 + - local: in_translation + title: Gemma3n + - local: in_translation + title: GIT + - local: in_translation + title: glm4v + - local: in_translation + title: GOT-OCR2 + - local: in_translation + title: GraniteVision + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) GIT + - local: model_doc/grounding-dino + title: Grounding DINO + - local: in_translation + title: GroupViT + - local: in_translation + title: IDEFICS + - local: in_translation + title: Idefics2 + - local: in_translation + title: Idefics3 + - local: in_translation + title: InstructBLIP + - local: in_translation + title: InstructBlipVideo + - local: in_translation + title: InternVL + - local: in_translation + title: Janus + - local: in_translation + title: KOSMOS-2 + - local: in_translation + title: LayoutLM + - local: in_translation + title: LayoutLMV2 + - local: in_translation + title: LayoutLMV3 + - local: in_translation + title: LayoutXLM + - local: in_translation + title: LiLT + - local: in_translation + title: Llama4 + - local: in_translation + title: Llava + - local: in_translation + title: LLaVA-NeXT + - local: in_translation + title: LLaVa-NeXT-Video + - local: in_translation + title: LLaVA-Onevision + - local: in_translation + title: LXMERT + - local: in_translation + title: MatCha + - local: in_translation + title: MGP-STR + - local: in_translation + title: Mistral3 + - local: in_translation + title: mllama + - local: in_translation + title: Nougat + - local: in_translation + title: OmDet-Turbo + - local: in_translation + title: OneFormer + - local: in_translation + title: OWL-ViT + - local: in_translation + title: OWLv2 + - local: model_doc/paligemma + title: PaliGemma + - local: in_translation + title: Perceiver + - local: in_translation + title: PerceptionLM + - local: in_translation + title: Phi4 Multimodal + - local: in_translation + title: Pix2Struct + - local: in_translation + title: Pixtral + - local: in_translation + title: Qwen2.5-Omni + - local: in_translation + title: Qwen2.5-VL + - local: in_translation + title: Qwen2Audio + - local: model_doc/qwen2_vl + title: Qwen2VL + - local: in_translation + title: Segment Anything + - local: in_translation + title: Segment Anything High Quality + - local: in_translation + title: ShieldGemma2 + - local: model_doc/siglip + title: SigLIP + - local: in_translation + title: SigLIP2 + - local: in_translation + title: SmolLM3 + - local: in_translation + title: SmolVLM + - local: in_translation + title: Speech Encoder Decoder Models + - local: in_translation + title: TAPAS + - local: in_translation + title: TrOCR + - local: in_translation + title: TVLT + - local: model_doc/tvp + title: TVP + - local: in_translation + title: UDOP + - local: in_translation + title: VideoLlava + - local: in_translation + title: ViLT + - local: in_translation + title: VipLlava + - local: in_translation + title: Vision Encoder Decoder Models + - local: in_translation + title: Vision Text Dual Encoder + - local: in_translation + title: VisualBERT + - local: in_translation + title: Voxtral + - local: in_translation + title: X-CLIP + title: ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ + - sections: + - local: in_translation + title: Decision Transformer + - local: model_doc/trajectory_transformer + title: Trajectory Transformer + title: ๊ฐ•ํ™”ํ•™์Šต ๋ชจ๋ธ + - sections: + - local: model_doc/autoformer + title: Autoformer + - local: model_doc/informer + title: Informer + - local: model_doc/patchtsmixer + title: PatchTSMixer + - local: model_doc/patchtst + title: PatchTST + - local: model_doc/time_series_transformer + title: Time Series Transformer + - local: in_translation + title: TimesFM + title: ์‹œ๊ฒŒ์—ด ๋ชจ๋ธ + - sections: + - local: model_doc/graphormer + title: Graphormer + title: ๊ทธ๋ž˜ํ”„ ๋ชจ๋ธ + title: ๋ชจ๋ธ + - sections: + - local: internal/modeling_utils + title: ์‚ฌ์šฉ์ž ์ •์˜ ๋ ˆ์ด์–ด ๋ฐ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Utilities for Model Debugging + - local: internal/pipelines_utils + title: ํŒŒ์ดํ”„๋ผ์ธ์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/tokenization_utils + title: ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/trainer_utils + title: Trainer๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/generation_utils + title: ์ƒ์„ฑ์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/image_processing_utils + title: ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/audio_utils + title: ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: internal/file_utils + title: ์ผ๋ฐ˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘) Importing Utilities + - local: internal/time_series_utils + title: ์‹œ๊ณ„์—ด์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ + title: ๋‚ด๋ถ€ ํ—ฌํผ(Internal helpers) + - sections: + - local: in_translation + title: (๋ฒˆ์—ญ์ค‘)Environment Variables + title: Reference + title: API diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerate.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerate.md new file mode 100644 index 0000000000000000000000000000000000000000..0ef8957de3ac20d38326624d60e7cd1fd349197b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerate.md @@ -0,0 +1,136 @@ + + +# ๐Ÿค— Accelerate๋ฅผ ํ™œ์šฉํ•œ ๋ถ„์‚ฐ ํ•™์Šต[[distributed-training-with-accelerate]] + +๋ชจ๋ธ์ด ์ปค์ง€๋ฉด์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋Š” ์ œํ•œ๋œ ํ•˜๋“œ์›จ์–ด์—์„œ ๋” ํฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ณ  ํ›ˆ๋ จ ์†๋„๋ฅผ ๋ช‡ ๋ฐฐ๋กœ ๊ฐ€์†ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต์œผ๋กœ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. Hugging Face์—์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜๋‚˜์˜ ๋จธ์‹ ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋“  ์—ฌ๋Ÿฌ ๋จธ์‹ ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋“  ๋ชจ๋“  ์œ ํ˜•์˜ ๋ถ„์‚ฐ ์„ค์ •์—์„œ ๐Ÿค— Transformers ๋ชจ๋ธ์„ ์‰ฝ๊ฒŒ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๊ธฐ ์œ„ํ•ด [๐Ÿค— Accelerate](https://huggingface.co/docs/accelerate) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋ณธ PyTorch ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค. + +## ์„ค์ •[[setup]] + +๐Ÿค— Accelerate ์„ค์น˜ ์‹œ์ž‘ํ•˜๊ธฐ: + +```bash +pip install accelerate +``` + +๊ทธ ๋‹ค์Œ, [`~accelerate.Accelerator`] ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. [`~accelerate.Accelerator`]๋Š” ์ž๋™์œผ๋กœ ๋ถ„์‚ฐ ์„ค์ • ์œ ํ˜•์„ ๊ฐ์ง€ํ•˜๊ณ  ํ›ˆ๋ จ์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์žฅ์น˜์— ๋ชจ๋ธ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐฐ์น˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. + +```py +>>> from accelerate import Accelerator + +>>> accelerator = Accelerator() +``` + +## ๊ฐ€์†ํ™”๋ฅผ ์œ„ํ•œ ์ค€๋น„[[prepare-to-accelerate]] + +๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๊ด€๋ จ๋œ ๋ชจ๋“  ํ›ˆ๋ จ ๊ฐ์ฒด๋ฅผ [`~accelerate.Accelerator.prepare`] ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ํ›ˆ๋ จ ๋ฐ ํ‰๊ฐ€ ๋ฐ์ดํ„ฐ๋กœ๋”, ๋ชจ๋ธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( +... train_dataloader, eval_dataloader, model, optimizer +... ) +``` + +## ๋ฐฑ์›Œ๋“œ(Backward)[[backward]] + +๋งˆ์ง€๋ง‰์œผ๋กœ ํ›ˆ๋ จ ๋ฃจํ”„์˜ ์ผ๋ฐ˜์ ์ธ `loss.backward()`๋ฅผ ๐Ÿค— Accelerate์˜ [`~accelerate.Accelerator.backward`] ๋ฉ”์†Œ๋“œ๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> for epoch in range(num_epochs): +... for batch in train_dataloader: +... outputs = model(**batch) +... loss = outputs.loss +... accelerator.backward(loss) + +... optimizer.step() +... lr_scheduler.step() +... optimizer.zero_grad() +... progress_bar.update(1) +``` + +๋‹ค์Œ ์ฝ”๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ํ›ˆ๋ จ ๋ฃจํ”„์— ์ฝ”๋“œ ๋„ค ์ค„๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ถ„์‚ฐ ํ•™์Šต์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +```diff ++ from accelerate import Accelerator + from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + ++ accelerator = Accelerator() + + model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) + optimizer = AdamW(model.parameters(), lr=3e-5) + +- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") +- model.to(device) + ++ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ++ train_dataloader, eval_dataloader, model, optimizer ++ ) + + num_epochs = 3 + num_training_steps = num_epochs * len(train_dataloader) + lr_scheduler = get_scheduler( + "linear", + optimizer=optimizer, + num_warmup_steps=0, + num_training_steps=num_training_steps + ) + + progress_bar = tqdm(range(num_training_steps)) + + model.train() + for epoch in range(num_epochs): + for batch in train_dataloader: +- batch = {k: v.to(device) for k, v in batch.items()} + outputs = model(**batch) + loss = outputs.loss +- loss.backward() ++ accelerator.backward(loss) + + optimizer.step() + lr_scheduler.step() + optimizer.zero_grad() + progress_bar.update(1) +``` + +## ํ•™์Šต[[train]] + +๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์—๋Š” ์Šคํฌ๋ฆฝํŠธ๋‚˜ Colaboratory์™€ ๊ฐ™์€ ๋…ธํŠธ๋ถ์—์„œ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•˜์„ธ์š”. + +### ์Šคํฌ๋ฆฝํŠธ๋กœ ํ•™์Šตํ•˜๊ธฐ[[train-with-a-script]] + +์Šคํฌ๋ฆฝํŠธ์—์„œ ํ›ˆ๋ จ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค: + +```bash +accelerate config +``` + +Then launch your training with: + +```bash +accelerate launch train.py +``` + +### ๋…ธํŠธ๋ถ์œผ๋กœ ํ•™์Šตํ•˜๊ธฐ[[train-with-a-notebook]] + +Collaboratory์˜ TPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ๋…ธํŠธ๋ถ์—์„œ๋„ ๐Ÿค— Accelerate๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์„œ [`~accelerate.notebook_launcher`]์— ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> from accelerate import notebook_launcher + +>>> notebook_launcher(training_function) +``` + +๐Ÿค— Accelerate ๋ฐ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [documentation](https://huggingface.co/docs/accelerate)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerator_selection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerator_selection.md new file mode 100644 index 0000000000000000000000000000000000000000..1e913ba34576d8479e34bbce47d5f01a4d5eb01e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/accelerator_selection.md @@ -0,0 +1,98 @@ + + + + +# GPU ์„ ํƒํ•˜๊ธฐ [[gpu-selection]] + +๋ถ„์‚ฐ ํ•™์Šต ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•  GPU์˜ ๊ฐœ์ˆ˜์™€ ์ˆœ์„œ๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์„œ๋กœ ๋‹ค๋ฅธ ์—ฐ์‚ฐ ์„ฑ๋Šฅ์„ ๊ฐ€์ง„ GPU๊ฐ€ ์žˆ์„ ๋•Œ ๋” ๋น ๋ฅธ GPU๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU ์ค‘ ์ผ๋ถ€๋งŒ ์„ ํƒํ•˜์—ฌ ํ™œ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ ํƒ ๊ณผ์ •์€ [DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html)๊ณผ [DataParallel](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html)์—์„œ ๋ชจ๋‘ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. Accelerate๋‚˜ [DeepSpeed ํ†ตํ•ฉ](./main_classes/deepspeed)์€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” ์‚ฌ์šฉํ•  GPU์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## GPU ๊ฐœ์ˆ˜ ์ง€์ • [[number-of-gpus]] + +์˜ˆ๋ฅผ ๋“ค์–ด, GPU๊ฐ€ 4๊ฐœ ์žˆ๊ณ  ๊ทธ์ค‘ ์ฒ˜์Œ 2๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”. + + + + +์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--nproc_per_node` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```bash +torchrun --nproc_per_node=2 trainer-program.py ... +``` + + + + +์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--num_processes` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```bash +accelerate launch --num_processes 2 trainer-program.py ... +``` + + + + +์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--num_gpus` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```bash +deepspeed --num_gpus 2 trainer-program.py ... +``` + + + + +### GPU ์ˆœ์„œ [[order-of-gpus]] + +์‚ฌ์šฉํ•  GPU์™€ ๊ทธ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด `CUDA_VISIBLE_DEVICES` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ `~/bashrc` ๋˜๋Š” ๋‹ค๋ฅธ ์‹œ์ž‘ ์„ค์ • ํŒŒ์ผ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. `CUDA_VISIBLE_DEVICES`๋Š” ์‚ฌ์šฉํ•  GPU๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, GPU๊ฐ€ 4๊ฐœ (0, 1, 2, 3) ์žˆ๊ณ  ๊ทธ์ค‘์—์„œ 0๋ฒˆ๊ณผ 2๋ฒˆ GPU๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +CUDA_VISIBLE_DEVICES=0,2 torchrun trainer-program.py ... +``` + +์˜ค์ง ๋‘ ๊ฐœ์˜ ๋ฌผ๋ฆฌ์  GPU(0, 2)๋งŒ PyTorch์—์„œ "๋ณด์ด๋Š”" ์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ, ๊ฐ๊ฐ `cuda:0`๊ณผ `cuda:1`๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, GPU ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, GPU 0์ด `cuda:1`, GPU 2๊ฐ€ `cuda:0`์œผ๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค." + +```bash +CUDA_VISIBLE_DEVICES=2,0 torchrun trainer-program.py ... +``` + +`CUDA_VISIBLE_DEVICES` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ GPU๊ฐ€ ์—†๋Š” ํ™˜๊ฒฝ์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +CUDA_VISIBLE_DEVICES= python trainer-program.py ... +``` + +> [!WARNING] +> ๋‹ค๋ฅธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, CUDA_VISIBLE_DEVICES๋ฅผ ์ปค๋งจ๋“œ ๋ผ์ธ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋Œ€์‹  exportํ•˜์—ฌ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์„ค์ •๋˜์—ˆ๋Š”์ง€๋ฅผ ์žŠ์–ด๋ฒ„๋ฆด ๊ฒฝ์šฐ, ์ž˜๋ชป๋œ GPU๋ฅผ ์‚ฌ์šฉํ•  ์œ„ํ—˜์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŠน์ • ํ•™์Šต ์‹คํ–‰์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ปค๋งจ๋“œ ๋ผ์ธ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +`CUDA_DEVICE_ORDER`๋Š” GPU์˜ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€์ฒด ํ™˜๊ฒฝ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ GPU ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +1. NVIDIA ๋ฐ AMD GPU์˜ PCIe ๋ฒ„์Šค ID๋Š” ๊ฐ๊ฐ [nvidia-smi](https://developer.nvidia.com/nvidia-system-management-interface)์™€ [rocm-smi](https://rocm.docs.amd.com/projects/rocm_smi_lib/en/latest/.doxygen/docBin/html/index.html)์˜ ์ˆœ์„œ์™€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. + +```bash +export CUDA_DEVICE_ORDER=PCI_BUS_ID +``` + +2. GPU ์—ฐ์‚ฐ ๋Šฅ๋ ฅ + +```bash +export CUDA_DEVICE_ORDER=FASTEST_FIRST +``` + +The `CUDA_DEVICE_ORDER` is especially useful if your training setup consists of an older and newer GPU, where the older GPU appears first, but you cannot physically swap the cards to make the newer GPU appear first. In this case, set `CUDA_DEVICE_ORDER=FASTEST_FIRST` to always use the newer and faster GPU first (`nvidia-smi` or `rocm-smi` still reports the GPUs in their PCIe order). Or you could also set `export CUDA_VISIBLE_DEVICES=1,0`. + +`CUDA_DEVICE_ORDER`๋Š” ๊ตฌํ˜• GPU์™€ ์‹ ํ˜• GPU๊ฐ€ ํ˜ผํ•ฉ๋œ ํ™˜๊ฒฝ์—์„œ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ตฌํ˜• GPU๊ฐ€ ๋จผ์ € ํ‘œ์‹œ๋˜์ง€๋งŒ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, `CUDA_DEVICE_ORDER=FASTEST_FIRST`๋ฅผ ์„ค์ •ํ•˜๋ฉด ํ•ญ์ƒ ์‹ ํ˜• ๋ฐ ๋” ๋น ๋ฅธ GPU๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉ(nvidia-smi ๋˜๋Š” rocm-smi๋Š” PCIe ์ˆœ์„œ๋Œ€๋กœ GPU๋ฅผ ํ‘œ์‹œํ•จ)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š”, `export CUDA_VISIBLE_DEVICES=1,0`์„ ์„ค์ •ํ•˜์—ฌ GPU ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_model.md new file mode 100644 index 0000000000000000000000000000000000000000..e30c2dc9f0d223a2d457ae73ce14cb2a9d6a6ab8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_model.md @@ -0,0 +1,626 @@ + + +# Hugging Face Transformers๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ๊ฐ€์š”? [[how-to-add-a-model-to-transformers]] + +Hugging Face Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธฐ์—ฌ์ž๋“ค ๋•๋ถ„์— ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” ๋„์ „์ ์ธ ํ”„๋กœ์ ํŠธ์ด๋ฉฐ Hugging Face Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ตฌํ˜„ํ•  ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊นŠ์€ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Hugging Face์—์„œ๋Š” ๋” ๋งŽ์€ ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฉค๋ฒ„๊ฐ€ ๋ชจ๋ธ์„ ์ ๊ทน์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๊ณ ์ž ํ•˜๋ฉฐ, ์ด ๊ฐ€์ด๋“œ๋ฅผ ํ†ตํ•ด PyTorch ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •์„ ์•ˆ๋‚ดํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค (PyTorch๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”). + +์ด ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ์ดํ•ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: + +- ์˜คํ”ˆ ์†Œ์Šค์˜ ๋ชจ๋ฒ” ์‚ฌ๋ก€์— ๋Œ€ํ•œ ํ†ต์ฐฐ๋ ฅ์„ ์–ป์Šต๋‹ˆ๋‹ค. +- ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋”ฅ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์„ค๊ณ„ ์›์น™์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. +- ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ํšจ์œจ์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. +- `black`, `ruff`, `make fix-copies`์™€ ๊ฐ™์€ Python ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ํ†ตํ•ฉํ•˜์—ฌ ๊น”๋”ํ•˜๊ณ  ๊ฐ€๋…์„ฑ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. + +Hugging Face ํŒ€์€ ํ•ญ์ƒ ๋„์›€์„ ์ค„ ์ค€๋น„๊ฐ€ ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ํ˜ผ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์„ธ์š”. ๐Ÿค— โค๏ธ + +์‹œ์ž‘์— ์•ž์„œ ๐Ÿค— Transformers์— ์›ํ•˜๋Š” ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด [New model addition](https://github.com/huggingface/transformers/issues/new?assignees=&labels=New+model&template=new-model-addition.yml) ์ด์Šˆ๋ฅผ ์—ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ๋ชจ๋ธ์„ ๊ธฐ์—ฌํ•˜๋Š” ๋ฐ ํŠน๋ณ„ํžˆ ๊นŒ๋‹ค๋กœ์šด ๊ธฐ์ค€์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ [New model label](https://github.com/huggingface/transformers/labels/New%20model)์„ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์š”์ฒญ๋˜์ง€ ์•Š์€ ๋ชจ๋ธ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ƒˆ๋กœ์šด ๋ชจ๋ธ ์š”์ฒญ์„ ์—ด์—ˆ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๐Ÿค— Transformers์— ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! + +## ๐Ÿค— Transformers์˜ ์ „๋ฐ˜์ ์ธ ๊ฐœ์š” [[general-overview-of-transformers]] + +๋จผ์ € ๐Ÿค— Transformers์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ๊ฐœ์š”๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿค— Transformers๋Š” ๋งค์šฐ ์ฃผ๊ด€์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฒ ํ•™์ด๋‚˜ ์„ค๊ณ„ ์„ ํƒ ์‚ฌํ•ญ์— ๋™์˜ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ์˜ ๊ฒฝํ—˜์ƒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์„ค๊ณ„ ์„ ํƒ๊ณผ ์ฒ ํ•™์€ ๐Ÿค— Transformers์˜ ๊ทœ๋ชจ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™•์žฅํ•˜๋ฉด์„œ ์œ ์ง€ ๋ณด์ˆ˜ ๋น„์šฉ์„ ํ•ฉ๋ฆฌ์ ์ธ ์ˆ˜์ค€์œผ๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +[๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฒ ํ•™์— ๋Œ€ํ•œ ๋ฌธ์„œ](philosophy)๋ฅผ ์ฝ๋Š” ๊ฒƒ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•˜๋Š” ์ข‹์€ ์‹œ์ž‘์ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ชจ๋ธ์— ์ ์šฉํ•˜๋ ค๋Š” ๋ช‡ ๊ฐ€์ง€ ์ž‘์—… ๋ฐฉ์‹์— ๋Œ€ํ•œ ์„ ํƒ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +- ์ผ๋ฐ˜์ ์œผ๋กœ ์ถ”์ƒํ™”๋ณด๋‹ค๋Š” ๊ตฌ์„ฑ์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. +- ์ฝ”๋“œ๋ฅผ ๋ณต์ œํ•˜๋Š” ๊ฒƒ์ด ํ•ญ์ƒ ๋‚˜์œ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด๋‚˜ ์ ‘๊ทผ์„ฑ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค๋ฉด ๋ณต์ œํ•˜๋Š” ๊ฒƒ์€ ์ข‹์Šต๋‹ˆ๋‹ค. +- ๋ชจ๋ธ ํŒŒ์ผ์€ ๊ฐ€๋Šฅํ•œ ํ•œ ๋…๋ฆฝ์ ์œผ๋กœ ์œ ์ง€๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŠน์ • ๋ชจ๋ธ์˜ ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ ํ•ด๋‹น `modeling_....py` ํŒŒ์ผ๋งŒ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์šฐ๋ฆฌ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฝ”๋“œ๊ฐ€ ์ œํ’ˆ์„ ์ œ๊ณตํ•˜๋Š” ์ˆ˜๋‹จ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐœ์„ ํ•˜๊ณ ์ž ํ•˜๋Š” ์ œํ’ˆ์ด๋ผ๊ณ ๋„ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•  ๋•Œ, ์‚ฌ์šฉ์ž๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์‚ฌ๋žŒ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์ดํ•ดํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ๊นŒ์ง€๋„ ํฌํ•จํ•œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ์ผ๋ฐ˜์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค๊ณ„์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +### ๋ชจ๋ธ ๊ฐœ์š” [[overview-of-models]] + +๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ๋ชจ๋ธ๊ณผ ํ•ด๋‹น ๊ตฌ์„ฑ์ธ [`PreTrainedModel`] ๋ฐ [`PretrainedConfig`] ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๐Ÿค— Transformers์— ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๋ชจ๋ธ์„ `BrandNewBert`๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + + + +๋ณด๋‹ค์‹œํ”ผ, ๐Ÿค— Transformers์—์„œ๋Š” ์ƒ์†์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ถ”์ƒํ™” ์ˆ˜์ค€์„ ์ตœ์†Œํ•œ์œผ๋กœ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์–ด๋–ค ๋ชจ๋ธ์—์„œ๋„ ๋‘ ์ˆ˜์ค€ ์ด์ƒ์˜ ์ถ”์ƒํ™”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. `BrandNewBertModel`์€ `BrandNewBertPreTrainedModel`์—์„œ ์ƒ์†๋ฐ›๊ณ , ์ด ํด๋ž˜์Šค๋Š” [`PreTrainedModel`]์—์„œ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋กœ์จ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์€ [`PreTrainedModel`]์—๋งŒ ์˜์กดํ•˜๋„๋ก ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ๋ชจ๋ธ์— ์ž๋™์œผ๋กœ ์ œ๊ณต๋˜๋Š” ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์€ [`~PreTrainedModel.from_pretrained`] ๋ฐ [`~PreTrainedModel.save_pretrained`]์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ ์™ธ์—๋„ `BrandNewBertModel.forward`์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์€ ์ƒˆ๋กœ์šด `modeling_brand_new_bert.py` ์Šคํฌ๋ฆฝํŠธ์—์„œ ์™„์ „ํžˆ ์ •์˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `BrandNewBertForMaskedLM`๊ณผ ๊ฐ™์€ ํŠน์ • ํ—ค๋“œ ๋ ˆ์ด์–ด๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์€ `BrandNewBertModel`์„ ์ƒ์†๋ฐ›์ง€ ์•Š๊ณ  forward pass์—์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” `BrandNewBertModel`์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ƒํ™” ์ˆ˜์ค€์„ ๋‚ฎ๊ฒŒ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ๋ชจ๋ธ์€ `BrandNewBertConfig`๋ผ๋Š” ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ตฌ์„ฑ์€ ํ•ญ์ƒ [`PreTrainedModel`]์˜ ์†์„ฑ์œผ๋กœ ์ €์žฅ๋˜๋ฉฐ, ๋”ฐ๋ผ์„œ `BrandNewBertPreTrainedModel`์„ ์ƒ์†๋ฐ›๋Š” ๋ชจ๋“  ํด๋ž˜์Šค์—์„œ `config` ์†์„ฑ์„ ํ†ตํ•ด ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +model = BrandNewBertModel.from_pretrained("brandy/brand_new_bert") +model.config # model has access to its config +``` + +๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ์€ [`PretrainedConfig`]์—์„œ ๊ธฐ๋ณธ ์ง๋ ฌํ™” ๋ฐ ์—ญ์ง๋ ฌํ™” ๊ธฐ๋Šฅ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑ๊ณผ ๋ชจ๋ธ์€ ํ•ญ์ƒ *pytorch_model.bin* ํŒŒ์ผ๊ณผ *config.json* ํŒŒ์ผ๋กœ ๊ฐ๊ฐ ๋ณ„๋„๋กœ ์ง๋ ฌํ™”๋ฉ๋‹ˆ๋‹ค. [`~PreTrainedModel.save_pretrained`]๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ [`~PretrainedConfig.save_pretrained`]๋„ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ๋ชจ๋ธ๊ณผ ๊ตฌ์„ฑ์ด ๋ชจ๋‘ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. + + +### ์ฝ”๋“œ ์Šคํƒ€์ผ [[code-style]] + +์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ž‘์„ฑํ•  ๋•Œ, Transformers๋Š” ์ฃผ๊ด€์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉฐ ๋ช‡ ๊ฐ€์ง€ ๋…ํŠนํ•œ ์ฝ”๋”ฉ ์Šคํƒ€์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +1. ๋ชจ๋ธ์˜ forward pass๋Š” ๋ชจ๋ธ ํŒŒ์ผ์— ์™„์ „ํžˆ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค๋ฅธ ๋ชจ๋ธ์—์„œ ๋ธ”๋ก์„ ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์œ„์— `# Copied from` ์ฃผ์„๊ณผ ํ•จ๊ป˜ ๋ถ™์—ฌ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค (์˜ˆ: [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/models/roberta/modeling_roberta.py#L160)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”). +2. ์ฝ”๋“œ๋Š” ์™„์ „ํžˆ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•˜๊ณ  ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `act`๋ณด๋‹ค๋Š” `activation`์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๊ธ€์ž ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ๋ฃจํ”„์˜ ์ธ๋ฑ์Šค์ธ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +3. ๋” ์ผ๋ฐ˜์ ์œผ๋กœ, ์งง์€ ๋งˆ๋ฒ• ๊ฐ™์€ ์ฝ”๋“œ๋ณด๋‹ค๋Š” ๊ธธ๊ณ  ๋ช…์‹œ์ ์ธ ์ฝ”๋“œ๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. +4. PyTorch์—์„œ `nn.Sequential`์„ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ง€ ๋ง๊ณ  `nn.Module`์„ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๊ณ  forward pass๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ฝ”๋“œ๋ฅผ ๋น ๋ฅด๊ฒŒ ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. print ๋ฌธ์ด๋‚˜ ์ค‘๋‹จ์ ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +5. ํ•จ์ˆ˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—๋Š” ํƒ€์ž… ์ฃผ์„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋Š” ํƒ€์ž… ์ฃผ์„๋ณด๋‹ค ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ํ›จ์”ฌ ์ฝ๊ธฐ ์‰ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. + +### ํ† ํฌ๋‚˜์ด์ € ๊ฐœ์š” [[overview-of-tokenizers]] + +์•„์ง ์ค€๋น„๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค :-( ์ด ์„น์…˜์€ ๊ณง ์ถ”๊ฐ€๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค! + +## ๐Ÿค— Transformers์— ๋ชจ๋ธ ์ถ”๊ฐ€ํ•˜๋Š” ๋‹จ๊ณ„๋ณ„ ๋ฐฉ๋ฒ• [[stepbystep-recipe-to-add-a-model-to-transformers]] + +๊ฐ์ž ๋ชจ๋ธ์„ ์ด์‹ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์„ ํ˜ธ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๊ธฐ์—ฌ์ž๋“ค์ด Hugging Face์— ๋ชจ๋ธ์„ ์ด์‹ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์š”์•ฝ์„ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ๋งค์šฐ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ชจ๋ธ์„ ์ด์‹ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค: + +1. [GPT2 ๋ชจ๋ธ ์ด์‹ํ•˜๊ธฐ](https://medium.com/huggingface/from-tensorflow-to-pytorch-265f40ef2a28) - [Thomas](https://huggingface.co/thomwolf) +2. [WMT19 MT ๋ชจ๋ธ ์ด์‹ํ•˜๊ธฐ](https://huggingface.co/blog/porting-fsmt) - [Stas](https://huggingface.co/stas) + +๊ฒฝํ—˜์ƒ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +- ๊ฐ™์€ ์ผ์„ ๋ฐ˜๋ณตํ•˜์ง€ ๋งˆ์„ธ์š”! ์ƒˆ๋กœ์šด ๐Ÿค— Transformers ๋ชจ๋ธ์„ ์œ„ํ•ด ์ถ”๊ฐ€ํ•  ์ฝ”๋“œ์˜ ๋Œ€๋ถ€๋ถ„์€ ์ด๋ฏธ ๐Ÿค— Transformers ์–ด๋”˜๊ฐ€์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์‚ฌํ•œ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ฐพ๋Š”๋ฐ ์‹œ๊ฐ„์„ ํˆฌ์žํ•˜์„ธ์š”. [grep](https://www.gnu.org/software/grep/)์™€ [rg](https://github.com/BurntSushi/ripgrep)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๋ชจ๋ธ์˜ ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ํ•œ ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ๋ชจ๋ธ๋ง ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด FSMT์˜ ๋ชจ๋ธ๋ง ์ฝ”๋“œ๋Š” BART๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  FSMT์˜ ํ† ํฌ๋‚˜์ด์ € ์ฝ”๋“œ๋Š” XLM์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด๊ฒƒ์€ ๊ณผํ•™์ ์ธ ๋„์ „๋ณด๋‹ค๋Š” ๊ณตํ•™์ ์ธ ๋„์ „์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์˜ ๋ชจ๋ธ์˜ ๋ชจ๋“  ์ด๋ก ์  ์ธก๋ฉด์„ ์ดํ•ดํ•˜๋ ค๋Š” ๊ฒƒ๋ณด๋‹ค ํšจ์œจ์ ์ธ ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์„ ๋งŒ๋“œ๋Š” ๋ฐ ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ง‰ํž ๋•Œ ๋„์›€์„ ์š”์ฒญํ•˜์„ธ์š”! ๋ชจ๋ธ์€ ๐Ÿค— Transformers์˜ ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ์ด๋ฏ€๋กœ Hugging Face์˜ ์šฐ๋ฆฌ๋Š” ๋‹น์‹ ์ด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฐ ๋‹จ๊ณ„์—์„œ ๊ธฐ๊บผ์ด ๋„์›€์„ ์ค„ ์ค€๋น„๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ง„์ „์ด ์—†๋‹ค๊ณ  ๋А๋ผ๋ฉด ์ฃผ์ €ํ•˜์ง€ ๋ง๊ณ  ๋„์›€์„ ์š”์ฒญํ•˜์„ธ์š”. + +๋‹ค์Œ์—์„œ๋Š” ๋ชจ๋ธ์„ ๐Ÿค— Transformers๋กœ ์ด์‹ํ•˜๋Š” ๋ฐ ๊ฐ€์žฅ ์œ ์šฉํ•œ ์ผ๋ฐ˜์ ์ธ ์ ˆ์ฐจ๋ฅผ ์ œ๊ณตํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ ๋ชฉ๋ก์€ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋ชจ๋“  ์ž‘์—…์˜ ์š”์•ฝ์ด๋ฉฐ To-Do ๋ชฉ๋ก์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +โ˜ (์„ ํƒ ์‚ฌํ•ญ) BrandNewBert์˜ ์ด๋ก ์  ์ธก๋ฉด ์ดํ•ด
+โ˜ Hugging Face ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ค€๋น„
+โ˜ ์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๋””๋ฒ„๊น… ํ™˜๊ฒฝ ์„ค์ •
+โ˜ ์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `forward()` pass๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ
+โ˜ ๐Ÿค— Transformers์— ๋ชจ๋ธ ์Šค์ผˆ๋ ˆํ†ค ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”๊ฐ€
+โ˜ ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๐Ÿค— Transformers ์ฒดํฌํฌ์ธํŠธ๋กœ ์„ฑ๊ณต์ ์œผ๋กœ ๋ณ€ํ™˜
+โ˜ ๐Ÿค— Transformers์—์„œ ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ์™€ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋‚ด์ฃผ๋Š” `forward()` pass ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰
+โ˜ ๐Ÿค— Transformers์—์„œ ๋ชจ๋ธ ํ…Œ์ŠคํŠธ ์™„๋ฃŒ
+โ˜ ๐Ÿค— Transformers์— ํ† ํฌ๋‚˜์ด์ € ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”๊ฐ€
+โ˜ ์ข…๋‹จ ๊ฐ„ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์‹คํ–‰
+โ˜ ๋ฌธ์„œ ์ž‘์„ฑ ์™„๋ฃŒ
+โ˜ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ
+โ˜ Pull request ์ œ์ถœ
+โ˜ (์„ ํƒ ์‚ฌํ•ญ) ๋ฐ๋ชจ ๋…ธํŠธ๋ถ ์ถ”๊ฐ€ + +์šฐ์„ , ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” `BrandNewBert`์˜ ์ด๋ก ์ ์ธ ์ดํ•ด๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ก ์  ์ธก๋ฉด์„ ์ง์ ‘ ์ดํ•ดํ•˜๋Š” ๋Œ€์‹  *์ง์ ‘ ํ•ด๋ณด๋ฉด์„œ* ๋ชจ๋ธ์˜ ์ด๋ก ์  ์ธก๋ฉด์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ”๋กœ `BrandNewBert` ์ฝ”๋“œ ๋ฒ ์ด์Šค๋กœ ๋น ์ ธ๋“œ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์€ ์—”์ง€๋‹ˆ์–ด๋ง ๊ธฐ์ˆ ์ด ์ด๋ก ์  ๊ธฐ์ˆ ๋ณด๋‹ค ๋” ๋›ฐ์–ด๋‚œ ๊ฒฝ์šฐ, `BrandNewBert`์˜ ๋…ผ๋ฌธ์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๋˜๋Š” ๊ณผํ•™์ ์ธ ๋…ผ๋ฌธ์„ ์ฝ๋Š” ๊ฒƒ๋ณด๋‹ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ํ›จ์”ฌ ๋” ํฅ๋ฏธ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### 1. (์„ ํƒ ์‚ฌํ•ญ) BrandNewBert์˜ ์ด๋ก ์  ์ธก๋ฉด [[1-optional-theoretical-aspects-of-brandnewbert]] + +๋งŒ์•ฝ ๊ทธ๋Ÿฐ ์„œ์ˆ ์ ์ธ ์ž‘์—…์ด ์กด์žฌํ•œ๋‹ค๋ฉด, *BrandNewBert*์˜ ๋…ผ๋ฌธ์„ ์ฝ์–ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ์„น์…˜์ด ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋”๋ผ๋„ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”! ๋ชฉํ‘œ๋Š” ๋…ผ๋ฌธ์˜ ๊นŠ์€ ์ด๋ก ์  ์ดํ•ด๊ฐ€ ์•„๋‹ˆ๋ผ *BrandNewBert*๋ฅผ ๐Ÿค— Transformers์—์„œ ํšจ๊ณผ์ ์œผ๋กœ ์žฌ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ด๋ก ์  ์ธก๋ฉด์— ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„์„ ํˆฌ์žํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹ค์ œ์ ์ธ ์ธก๋ฉด์— ์ง‘์ค‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- *BrandNewBert*๋Š” ์–ด๋–ค ์œ ํ˜•์˜ ๋ชจ๋ธ์ธ๊ฐ€์š”? BERT์™€ ์œ ์‚ฌํ•œ ์ธ์ฝ”๋” ๋ชจ๋ธ์ธ๊ฐ€์š”? GPT2์™€ ์œ ์‚ฌํ•œ ๋””์ฝ”๋” ๋ชจ๋ธ์ธ๊ฐ€์š”? BART์™€ ์œ ์‚ฌํ•œ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์ธ๊ฐ€์š”? ์ด๋“ค ๊ฐ„์˜ ์ฐจ์ด์ ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ[model_summary](model_summary)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. +- *BrandNewBert*์˜ ์‘์šฉ ๋ถ„์•ผ๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์ธ๊ฐ€์š”? ํ…์ŠคํŠธ ์ƒ์„ฑ์ธ๊ฐ€์š”? ์š”์•ฝ๊ณผ ๊ฐ™์€ Seq2Seq ์ž‘์—…์ธ๊ฐ€์š”? +- *brand_new_bert*์™€ BERT/GPT-2/BART์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”? +- *brand_new_bert*์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ [๐Ÿค— Transformers ๋ชจ๋ธ](https://huggingface.co/transformers/#contents)์€ ๋ฌด์—‡์ธ๊ฐ€์š”? +- ์–ด๋–ค ์ข…๋ฅ˜์˜ ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ์‚ฌ์šฉ๋˜๋‚˜์š”? Sentencepiece ํ† ํฌ๋‚˜์ด์ €์ธ๊ฐ€์š”? Word piece ํ† ํฌ๋‚˜์ด์ €์ธ๊ฐ€์š”? BERT ๋˜๋Š” BART์— ์‚ฌ์šฉ๋˜๋Š” ๋™์ผํ•œ ํ† ํฌ๋‚˜์ด์ €์ธ๊ฐ€์š”? + +๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ–ˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“  ํ›„, ๊ถ๊ธˆํ•œ ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉด Hugging Face ํŒ€์— ๋ฌธ์˜ํ•˜์‹ญ์‹œ์˜ค. ์ด๋Š” ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜, ์–ดํ…์…˜ ๋ ˆ์ด์–ด ๋“ฑ์— ๊ด€ํ•œ ์งˆ๋ฌธ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hugging Face์˜ ์œ ์ง€ ๊ด€๋ฆฌ์ž๋“ค์€ ๋ณดํ†ต ์ฝ”๋“œ๋ฅผ ๊ฒ€ํ† ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๋งค์šฐ ๊ธฐ๋ปํ•˜๋ฏ€๋กœ ๋‹น์‹ ์„ ๋•๋Š” ์ผ์„ ๋งค์šฐ ํ™˜์˜ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! + +### 2. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ • [[2-next-prepare-your-environment]] + +1. ์ €์žฅ์†Œ ํŽ˜์ด์ง€์—์„œ "Fork" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ €์žฅ์†Œ์˜ ์‚ฌ๋ณธ์„ GitHub ์‚ฌ์šฉ์ž ๊ณ„์ •์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. + +2. `transformers` fork๋ฅผ ๋กœ์ปฌ ๋””์Šคํฌ์— ํด๋ก ํ•˜๊ณ  ๋ฒ ์ด์Šค ์ €์žฅ์†Œ๋ฅผ ์›๊ฒฉ ์ €์žฅ์†Œ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: + +```bash +git clone https://github.com/[your Github handle]/transformers.git +cd transformers +git remote add upstream https://github.com/huggingface/transformers.git +``` + +3. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +python -m venv .env +source .env/bin/activate +pip install -e ".[dev]" +``` + +๊ฐ ์šด์˜ ์ฒด์ œ์— ๋”ฐ๋ผ Transformers์˜ ์„ ํƒ์  ์˜์กด์„ฑ์ด ๊ฐœ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์ด ๋ช…๋ น์ด ์‹คํŒจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ž‘์—… ์ค‘์ธ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ (PyTorch, TensorFlow ๋ฐ/๋˜๋Š” Flax)์„ ์„ค์น˜ํ•œ ํ›„, ๋‹ค์Œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```bash +pip install -e ".[quality]" +``` + +๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—๋Š” ์ด๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. + +```bash +cd .. +``` + +4. Transformers์— *brand_new_bert*์˜ PyTorch ๋ฒ„์ „์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. PyTorch๋ฅผ ์„ค์น˜ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋งํฌ์˜ ์ง€์นจ์„ ๋”ฐ๋ฅด์‹ญ์‹œ์˜ค: https://pytorch.org/get-started/locally/. + +**์ฐธ๊ณ :** CUDA๋ฅผ ์„ค์น˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ชจ๋ธ์ด CPU์—์„œ ์ž‘๋™ํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. + +5. *brand_new_bert*๋ฅผ ์ด์‹ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ์›๋ณธ ์ €์žฅ์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +git clone https://github.com/org_that_created_brand_new_bert_org/brand_new_bert.git +cd brand_new_bert +pip install -e . +``` + +์ด์ œ *brand_new_bert*๋ฅผ ๐Ÿค— Transformers๋กœ ์ด์‹ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. + +### 3.-4. ์›๋ณธ ์ €์žฅ์†Œ์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ฒดํฌํฌ์ธํŠธ ์‹คํ–‰ํ•˜๊ธฐ [[3.-4.-run-a-pretrained-checkpoint-using-the-original-repository]] + +๋จผ์ €, ์›๋ณธ *brand_new_bert* ์ €์žฅ์†Œ์—์„œ ์ž‘์—…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๊ตฌํ˜„์€ ๋ณดํ†ต "์—ฐ๊ตฌ์šฉ"์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌธ์„œํ™”๊ฐ€ ๋ถ€์กฑํ•˜๊ณ  ์ฝ”๋“œ๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์ด ๋ฐ”๋กœ *brand_new_bert*๋ฅผ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๋ ค๋Š” ๋™๊ธฐ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Hugging Face์—์„œ์˜ ์ฃผ์š” ๋ชฉํ‘œ ์ค‘ ํ•˜๋‚˜๋Š” **๊ฑฐ์ธ์˜ ์–ด๊นจ ์œ„์— ์„œ๋Š” ๊ฒƒ**์ด๋ฉฐ, ์ด๋Š” ์—ฌ๊ธฐ์—์„œ ์‰ฝ๊ฒŒ ํ•ด์„๋˜์–ด ๋™์ž‘ํ•˜๋Š” ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€์„œ ๊ฐ€๋Šฅํ•œ ํ•œ **์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ณ  ์‚ฌ์šฉ์ž ์นœํ™”์ ์ด๋ฉฐ ์•„๋ฆ„๋‹ต๊ฒŒ** ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๐Ÿค— Transformers์—์„œ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋™๊ธฐ์ž…๋‹ˆ๋‹ค - ์ƒˆ๋กœ์šด ๋ณต์žกํ•œ NLP ๊ธฐ์ˆ ์„ **๋ชจ๋‘์—๊ฒŒ** ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ ์›๋ณธ ์ €์žฅ์†Œ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์›๋ณธ ์ €์žฅ์†Œ์—์„œ ๊ณต์‹ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์ข…์ข… **๊ฐ€์žฅ ์–ด๋ ค์šด** ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝํ—˜์— ๋”ฐ๋ฅด๋ฉด, ์›๋ณธ ์ฝ”๋“œ ๋ฒ ์ด์Šค์— ์ต์ˆ™ํ•ด์ง€๋Š” ๋ฐ ์‹œ๊ฐ„์„ ํˆฌ์žํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์–ด๋””์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”์ง€? +- ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ํ•ด๋‹น ๋ชจ๋ธ์—๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€? +- ๋ชจ๋ธ๊ณผ ๋…๋ฆฝ์ ์œผ๋กœ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€? +- ๊ฐ„๋‹จํ•œ forward pass์— ํ•„์š”ํ•œ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด forward pass๋ฅผ ํ•œ ๋ฒˆ ์ถ”์ ํ•ด ๋ณด์„ธ์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น ํ•จ์ˆ˜๋“ค๋งŒ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์˜ ์ค‘์š”ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” ์–ด๋””์— ์žˆ๋‚˜์š”? ๋ชจ๋ธ ํ•˜์œ„ ํด๋ž˜์Šค(*EncoderModel*, *DecoderModel* ๋“ฑ)๊ฐ€ ์žˆ๋‚˜์š”? self-attention ๋ ˆ์ด์–ด๋Š” ์–ด๋””์— ์žˆ๋‚˜์š”? self-attention, cross-attention ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๊ฐ€ ์žˆ๋‚˜์š”? +- ์›๋ณธ ํ™˜๊ฒฝ์—์„œ ๋ชจ๋ธ์„ ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ๊ฐ€์š”? *print* ๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋‚˜์š”? *ipdb*์™€ ๊ฐ™์€ ๋Œ€ํ™”์‹ ๋””๋ฒ„๊ฑฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”? PyCharm๊ณผ ๊ฐ™์€ ํšจ์œจ์ ์ธ IDE๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋ธ์„ ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ๋‚˜์š”? + +์›๋ณธ ์ €์žฅ์†Œ์—์„œ ์ฝ”๋“œ๋ฅผ ์ด์‹ํ•˜๋Š” ์ž‘์—…์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์›๋ณธ ์ €์žฅ์†Œ์—์„œ ์ฝ”๋“œ๋ฅผ **ํšจ์œจ์ ์œผ๋กœ** ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ๋˜ํ•œ, ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›๋ณธ ์ €์žฅ์†Œ์—์„œ issue๋ฅผ ์—ด๊ฑฐ๋‚˜ pull request๋ฅผ ์—ด๊ธฐ๋ฅผ ์ฃผ์ €ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ์ด ์ €์žฅ์†Œ์˜ ์œ ์ง€ ๊ด€๋ฆฌ์ž๋“ค์€ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ž์‹ ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๋งค์šฐ ๊ธฐ๋ปํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! + +ํ˜„์žฌ ์‹œ์ ์—์„œ, ์›๋ž˜ ๋ชจ๋ธ์„ ๋””๋ฒ„๊น…ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋””๋ฒ„๊น… ํ™˜๊ฒฝ๊ณผ ์ „๋žต์„ ์„ ํ˜ธํ•˜๋Š”์ง€๋Š” ๋‹น์‹ ์—๊ฒŒ ๋‹ฌ๋ ธ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ณ ๊ฐ€์˜ GPU ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์€ ๋น„์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์‹ , ์›๋ž˜ ์ €์žฅ์†Œ๋กœ ๋“ค์–ด๊ฐ€์„œ ์ž‘์—…์„ ์‹œ์ž‘ํ•  ๋•Œ์™€ ๐Ÿค— Transformers ๋ชจ๋ธ์˜ ๊ตฌํ˜„์„ ์‹œ์ž‘ํ•  ๋•Œ์—๋„ CPU์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ด๋ฏธ ๐Ÿค— Transformers๋กœ ์„ฑ๊ณต์ ์œผ๋กœ ์ด์‹๋˜์—ˆ์„ ๋•Œ์—๋งŒ ๋ชจ๋ธ์ด GPU์—์„œ๋„ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. + +์ผ๋ฐ˜์ ์œผ๋กœ, ์›๋ž˜ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๊ฐ€๋Šฅํ•œ ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +- [Jupyter ๋…ธํŠธ๋ถ](https://jupyter.org/) / [Google Colab](https://colab.research.google.com/notebooks/intro.ipynb) +- ๋กœ์ปฌ Python ์Šคํฌ๋ฆฝํŠธ + +Jupyter ๋…ธํŠธ๋ถ์˜ ์žฅ์ ์€ ์…€ ๋‹จ์œ„๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋…ผ๋ฆฌ์ ์ธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋” ์ž˜ ๋ถ„๋ฆฌํ•˜๊ณ  ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋””๋ฒ„๊น… ์‚ฌ์ดํด์ด ๋” ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋…ธํŠธ๋ถ์€ ๋‹ค๋ฅธ ๊ธฐ์—ฌ์ž์™€ ์‰ฝ๊ฒŒ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ Hugging Face ํŒ€์˜ ๋„์›€์„ ์š”์ฒญํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ๋งค์šฐ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Jupyter ๋…ธํŠธ๋ถ์— ์ต์ˆ™ํ•˜๋‹ค๋ฉด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. + +Jupyter ๋…ธํŠธ๋ถ์˜ ๋‹จ์ ์€ ์‚ฌ์šฉ์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ™˜๊ฒฝ์— ์ ์‘ํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์„ ํ• ์• ํ•ด์•ผ ํ•˜๋ฉฐ, `ipdb`์™€ ๊ฐ™์€ ์•Œ๋ ค์ง„ ๋””๋ฒ„๊น… ๋„๊ตฌ๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๊ฐ ์ฝ”๋“œ ๋ฒ ์ด์Šค์— ๋Œ€ํ•ด ์ข‹์€ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ํ•ญ์ƒ **์ž‘์€** ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๊ณ  ๋”๋ฏธ ์ •์ˆ˜ ๋ฒกํ„ฐ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์ผ forward pass๋ฅผ ์žฌํ˜„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜์‚ฌ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ): + +```python +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = [0, 4, 5, 2, 3, 7, 9] # vector of input ids +original_output = model.predict(input_ids) +``` + +๋‹ค์Œ์œผ๋กœ, ๋””๋ฒ„๊น… ์ „๋žต์— ๋Œ€ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์„ ํƒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +- ์›๋ณธ ๋ชจ๋ธ์„ ๋งŽ์€ ์ž‘์€ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•˜๊ณ  ๊ฐ๊ฐ์— ๋Œ€ํ•ด forward pass๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค. +- ์›๋ณธ ๋ชจ๋ธ์„ ์›๋ณธ *tokenizer*๊ณผ ์›๋ณธ *model*๋กœ๋งŒ ๋ถ„ํ•ดํ•˜๊ณ  ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•ด forward pass๋ฅผ ์‹คํ–‰ํ•œ ํ›„ ๊ฒ€์ฆ์„ ์œ„ํ•ด ์ค‘๊ฐ„ ์ถœ๋ ฅ(print ๋ฌธ ๋˜๋Š” ์ค‘๋‹จ์ )์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์–ด๋–ค ์ „๋žต์„ ์„ ํƒํ• ์ง€๋Š” ๋‹น์‹ ์—๊ฒŒ ๋‹ฌ๋ ค ์žˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ ๋ฒ ์ด์Šค์— ๋”ฐ๋ผ ํ•˜๋‚˜ ๋˜๋Š” ๋‹ค๋ฅธ ์ „๋žต์ด ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์›๋ณธ ์ฝ”๋“œ ๋ฒ ์ด์Šค๋ฅผ ๋ชจ๋ธ์˜ ์ž‘์€ ํ•˜์œ„ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€, ์˜ˆ๋ฅผ ๋“ค์–ด ์›๋ณธ ์ฝ”๋“œ ๋ฒ ์ด์Šค๊ฐ€ ์ฆ‰์‹œ ์‹คํ–‰ ๋ชจ๋“œ์—์„œ ๊ฐ„๋‹จํžˆ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๋…ธ๋ ฅ์ด ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ์— ๋” ์–ด๋ ค์šด ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +- ์›๋ณธ ๋ชจ๋ธ์„ ๐Ÿค— Transformers ๊ตฌํ˜„๊ณผ ๋น„๊ตํ•  ๋•Œ ๊ฐ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ์ž๋™์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์‹œ๊ฐ์ ์ธ ๋น„๊ต(print ๋ฌธ์„ ํ†ตํ•œ ๋น„๊ต๊ฐ€ ์•„๋‹Œ) ๋Œ€์‹  ๐Ÿค— Transformers ๊ตฌํ˜„๊ณผ ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ์›๋ณธ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ „์ฒด ๋ชจ๋ธ์„ ๋ชจ๋“ˆ๋ณ„๋กœ, ์ฆ‰ ์ž‘์€ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•จ์œผ๋กœ์จ ๋ชจ๋ธ์„ ์ด์‹ํ•˜๋Š” ํฐ ๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํžˆ ๊ฐœ๋ณ„ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ด์‹ํ•˜๋Š” ์ž‘์€ ๋ฌธ์ œ๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ž‘์—…์„ ๋” ์ž˜ ๊ตฌ์กฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์˜๋ฏธ ์žˆ๋Š” ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์˜ ์„ค๊ณ„์— ๋Œ€ํ•œ ๋” ๋‚˜์€ ๊ฐœ์š”๋ฅผ ์–ป๊ณ  ๋ชจ๋ธ์„ ๋” ์ž˜ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. +- ์ด๋Ÿฌํ•œ ๊ตฌ์„ฑ ์š”์†Œ๋ณ„ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ํšŒ๊ท€๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[Lysandre์˜ ELECTRA ํ†ตํ•ฉ ๊ฒ€์‚ฌ](https://gist.github.com/LysandreJik/db4c948f6b4483960de5cbac598ad4ed)๋Š” ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ข‹์€ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฌ๋‚˜ ์›๋ณธ ์ฝ”๋“œ ๋ฒ ์ด์Šค๊ฐ€ ๋งค์šฐ ๋ณต์žกํ•˜๊ฑฐ๋‚˜ ์ค‘๊ฐ„ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ปดํŒŒ์ผ๋œ ๋ชจ๋“œ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ ํ—ˆ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ์ž‘์€ ํ•˜์œ„ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•˜๋Š” ๊ฒƒ์ด ์‹œ๊ฐ„์ด ๋งŽ์ด ์†Œ์š”๋˜๊ฑฐ๋‚˜ ๋ถˆ๊ฐ€๋Šฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. [T5์˜ MeshTensorFlow](https://github.com/tensorflow/mesh/tree/master/mesh_tensorflow) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋งค์šฐ ๋ณต์žกํ•˜๋ฉฐ ๋ชจ๋ธ์„ ํ•˜์œ„ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฒฝ์šฐ, ๋ณดํ†ต print ๋ฌธ์„ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. + +์–ด๋–ค ์ „๋žต์„ ์„ ํƒํ•˜๋”๋ผ๋„ ๊ถŒ์žฅ๋˜๋Š” ์ ˆ์ฐจ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์‹œ์ž‘ ๋ ˆ์ด์–ด๋ฅผ ๋””๋ฒ„๊ทธํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด๋ฅผ ๋งˆ์ง€๋ง‰์— ๋””๋ฒ„๊ทธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ ์ˆœ์„œ๋กœ ๊ฐ ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + +1. ๋ชจ๋ธ์— ์ „๋‹ฌ๋œ ์ž…๋ ฅ ID ๊ฐ€์ ธ์˜ค๊ธฐ +2. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ฐ€์ ธ์˜ค๊ธฐ +3. ์ฒซ ๋ฒˆ์งธ Transformer ๋ ˆ์ด์–ด์˜ ์ž…๋ ฅ ๊ฐ€์ ธ์˜ค๊ธฐ +4. ์ฒซ ๋ฒˆ์งธ Transformer ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ ๊ฐ€์ ธ์˜ค๊ธฐ +5. ๋‹ค์Œ n-1๊ฐœ์˜ Transformer ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ ๊ฐ€์ ธ์˜ค๊ธฐ +6. BrandNewBert ๋ชจ๋ธ์˜ ์ถœ๋ ฅ ๊ฐ€์ ธ์˜ค๊ธฐ + +์ž…๋ ฅ ID๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด `input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19]`์™€ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ์€ ์ข…์ข… ๋‹ค์ฐจ์› ์‹ค์ˆ˜ ๋ฐฐ์—ด๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +``` +[[ + [-0.1465, -0.6501, 0.1993, ..., 0.1451, 0.3430, 0.6024], + [-0.4417, -0.5920, 0.3450, ..., -0.3062, 0.6182, 0.7132], + [-0.5009, -0.7122, 0.4548, ..., -0.3662, 0.6091, 0.7648], + ..., + [-0.5613, -0.6332, 0.4324, ..., -0.3792, 0.7372, 0.9288], + [-0.5416, -0.6345, 0.4180, ..., -0.3564, 0.6992, 0.9191], + [-0.5334, -0.6403, 0.4271, ..., -0.3339, 0.6533, 0.8694]]], +``` + +๐Ÿค— Transformers์— ์ถ”๊ฐ€๋˜๋Š” ๋ชจ๋“  ๋ชจ๋ธ์€ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์›๋ณธ ๋ชจ๋ธ๊ณผ ๐Ÿค— Transformers์˜ ์žฌ๊ตฌํ˜„ ๋ฒ„์ „์ด 0.001์˜ ์ •๋ฐ€๋„๋กœ ์ •ํ™•ํžˆ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ๋™์ผํ•œ ๋ชจ๋ธ์ด ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ž‘์„ฑ๋˜์—ˆ์„ ๋•Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ”„๋ ˆ์ž„์›Œํฌ์— ๋”ฐ๋ผ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ์ถœ๋ ฅ์„ ์–ป๋Š” ๊ฒƒ์€ ์ •์ƒ์ด๋ฏ€๋กœ 1e-3(0.001)์˜ ์˜ค์ฐจ๋Š” ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋‚ด๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์™„๋ฒฝํžˆ ์ผ์น˜ํ•˜๋Š” ์ˆ˜์ค€์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๐Ÿค— Transformers ๋ฒ„์ „์˜ ์ค‘๊ฐ„ ์ถœ๋ ฅ์„ *brand_new_bert*์˜ ์›๋ž˜ ๊ตฌํ˜„์˜ ์ค‘๊ฐ„ ์ถœ๋ ฅ๊ณผ ์—ฌ๋Ÿฌ ๋ฒˆ ๋น„๊ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์›๋ณธ ์ €์žฅ์†Œ์˜ **ํšจ์œจ์ ์ธ** ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์ด ์ ˆ๋Œ€์ ์œผ๋กœ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์„ ๊ฐ€๋Šฅํ•œ ํ•œ ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์กฐ์–ธ์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. + +- ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋””๋ฒ„๊ทธํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ์„ธ์š”. ์›๋ณธ ์ €์žฅ์†Œ๊ฐ€ PyTorch๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด ์›๋ณธ ๋ชจ๋ธ์„ ๋” ์ž‘์€ ํ•˜์œ„ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ์ค‘๊ฐ„ ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ธด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์— ์‹œ๊ฐ„์„ ํˆฌ์žํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ €์žฅ์†Œ๊ฐ€ Tensorflow 1๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด [tf.print](https://www.tensorflow.org/api_docs/python/tf/print)์™€ ๊ฐ™์€ Tensorflow ์ถœ๋ ฅ ์ž‘์—…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๊ฐ„ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ €์žฅ์†Œ๊ฐ€ Jax๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด forward pass๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ๋ชจ๋ธ์ด **jit ๋˜์ง€ ์•Š๋„๋ก** ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [์ด ๋งํฌ](https://github.com/google/jax/issues/196)๋ฅผ ํ™•์ธํ•ด ๋ณด์„ธ์š”. +- ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์ž‘์„์ˆ˜๋ก ๋””๋ฒ„๊ทธ ์‚ฌ์ดํด์ด ๋” ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ์ „๋ฐ˜์ ์œผ๋กœ forward pass์— 10์ดˆ ์ด์ƒ์ด ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ ํšจ์œจ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งค์šฐ ํฐ ์ฒดํฌํฌ์ธํŠธ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ์ƒˆ ํ™˜๊ฒฝ์—์„œ ์ž„์˜๋กœ ์ดˆ๊ธฐํ™”๋œ ๊ฐ€์ค‘์น˜๋กœ ๋”๋ฏธ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ๊ฐ€์ค‘์น˜๋ฅผ ๐Ÿค— Transformers ๋ฒ„์ „๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋” ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๋””๋ฒ„๊น… ์„ค์ •์—์„œ ๊ฐ€์žฅ ์‰ฝ๊ฒŒ forward pass๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ์›๋ณธ ์ €์žฅ์†Œ์—์„œ **๋‹จ์ผ** forward pass๋งŒ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ `predict`, `evaluate`, `forward`, `__call__`๊ณผ ๊ฐ™์ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. `autoregressive_sample`๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ ์ƒ์„ฑ์—์„œ `forward`๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋””๋ฒ„๊ทธํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- ํ† ํฐํ™” ๊ณผ์ •์„ ๋ชจ๋ธ์˜ *forward* pass์™€ ๋ถ„๋ฆฌํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜์„ธ์š”. ์›๋ณธ ์ €์žฅ์†Œ์—์„œ ์ž…๋ ฅ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ ID๋กœ ๋ณ€๊ฒฝ๋˜๋Š” ์ˆœ๊ฐ„์„ ์ฐพ์•„์„œ ์‹œ์ž‘ํ•˜์„ธ์š”. ์ด ๊ฒฝ์šฐ ์ง์ ‘ ID๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž‘์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ์›๋ณธ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๋””๋ฒ„๊น… ์„ค์ •์—์„œ ๋ชจ๋ธ์ด ํ›ˆ๋ จ ๋ชจ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์„ธ์š”. ํ›ˆ๋ จ ๋ชจ๋“œ์—์„œ๋Š” ๋ชจ๋ธ์˜ ์—ฌ๋Ÿฌ ๋“œ๋กญ์•„์›ƒ ๋ ˆ์ด์–ด ๋•Œ๋ฌธ์— ๋ฌด์ž‘์œ„ ์ถœ๋ ฅ์ด ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์—์„œ forward pass๊ฐ€ **๊ฒฐ์ •๋ก ์ **์ด๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ๋™์ผํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์— ์žˆ๋Š” ๊ฒฝ์šฐ *transformers.utils.set_seed*๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. + +๋‹ค์Œ ์„น์…˜์—์„œ๋Š” *brand_new_bert*์— ๋Œ€ํ•ด ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๋” ๊ตฌ์ฒด์ ์ธ ์„ธ๋ถ€ ์‚ฌํ•ญ/ํŒ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +### 5.-14. ๐Ÿค— Transformers์— BrandNewBert๋ฅผ ์ด์‹ํ•˜๊ธฐ [[5.-14.-port-brandnewbert-to-transformers]] + +์ด์ œ, ๋งˆ์นจ๋‚ด ๐Ÿค— Transformers์— ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค— Transformers ํฌํฌ์˜ ํด๋ก ์œผ๋กœ ์ด๋™ํ•˜์„ธ์š”: + +```bash +cd transformers +``` + +๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ชจ๋ธ์˜ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ์—๋Š” [์ด ์„น์…˜](#write-a-conversion-script)์— ์„ค๋ช…๋œ๋Œ€๋กœ ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ชจ๋ธ์˜ ์ „์ฒด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ทธ๋Œ€๋กœ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ƒˆ ๋ชจ๋ธ ์ƒ์„ฑ์„ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +๊ธฐ์กด ๋ชจ๋ธ: + +```bash +transformers add-new-model-like +``` + +๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์„ค๋ฌธ์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. + +**huggingface/transformers ๋ฉ”์ธ ์ €์žฅ์†Œ์— Pull Request ์—ด๊ธฐ** + +์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ์ „์—, ์ง€๊ธˆ์€ "์ž‘์—… ์ง„ํ–‰ ์ค‘ (WIP)" ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์—ด๊ธฐ ์œ„ํ•œ ์‹œ๊ธฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๐Ÿค— Transformers์— "*brand_new_bert* ์ถ”๊ฐ€"๋ผ๋Š” ์ œ๋ชฉ์˜ "[WIP] Add *brand_new_bert*" ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์—ฝ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹น์‹ ๊ณผ Hugging Face ํŒ€์ด ๐Ÿค— Transformers์— ๋ชจ๋ธ์„ ํ†ตํ•ฉํ•˜๋Š” ์ž‘์—…์„ ํ•จ๊ป˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +1. ๋ฉ”์ธ ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…์„ ์ž˜ ์„ค๋ช…ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋ธŒ๋žœ์น˜ ์ƒ์„ฑ + +```bash +git checkout -b add_brand_new_bert +``` + +2. ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ ์ฝ”๋“œ ์ปค๋ฐ‹ + +```bash +git add . +git commit +``` + +3. ํ˜„์žฌ ๋ฉ”์ธ์„ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌ๋ฒ ์ด์Šค + +```bash +git fetch upstream +git rebase upstream/main +``` + +4. ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ณ„์ •์— ํ‘ธ์‹œ + +```bash +git push -u origin a-descriptive-name-for-my-changes +``` + +5. ๋งŒ์กฑ์Šค๋Ÿฝ๋‹ค๋ฉด, GitHub์—์„œ ์ž์‹ ์˜ ํฌํฌํ•œ ์›น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. "Pull request"๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. Hugging Face ํŒ€์˜ ์ผ๋ถ€ ๋ฉค๋ฒ„์˜ GitHub ํ•ธ๋“ค์„ ๋ฆฌ๋ทฐ์–ด๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ Hugging Face ํŒ€์ด ์•ž์œผ๋กœ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์•Œ๋ฆผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +6. GitHub ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ์›น ํŽ˜์ด์ง€ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” "Convert to draft"๋ฅผ ํด๋ฆญํ•˜์—ฌ PR์„ ์ดˆ์•ˆ์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ์œผ๋กœ, ์–ด๋–ค ์ง„์ „์„ ์ด๋ฃจ์—ˆ๋‹ค๋ฉด ์ž‘์—…์„ ์ปค๋ฐ‹ํ•˜๊ณ  ๊ณ„์ •์— ํ‘ธ์‹œํ•˜์—ฌ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ์— ํ‘œ์‹œ๋˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜„์žฌ ๋ฉ”์ธ๊ณผ ์ž‘์—…์„ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +git fetch upstream +git merge upstream/main +``` + +์ผ๋ฐ˜์ ์œผ๋กœ, ๋ชจ๋ธ ๋˜๋Š” ๊ตฌํ˜„์— ๊ด€ํ•œ ๋ชจ๋“  ์งˆ๋ฌธ์€ ์ž์‹ ์˜ PR์—์„œ ํ•ด์•ผ ํ•˜๋ฉฐ, PR์—์„œ ํ† ๋ก ๋˜๊ณ  ํ•ด๊ฒฐ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Hugging Face ํŒ€์ด ์ƒˆ๋กœ์šด ์ฝ”๋“œ๋ฅผ ์ปค๋ฐ‹ํ•˜๊ฑฐ๋‚˜ ์งˆ๋ฌธ์„ ํ•  ๋•Œ ํ•ญ์ƒ ์•Œ๋ฆผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hugging Face ํŒ€์—๊ฒŒ ๋ฌธ์ œ ๋˜๋Š” ์งˆ๋ฌธ์„ ํšจ์œจ์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถ”๊ฐ€ํ•œ ์ฝ”๋“œ๋ฅผ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋  ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. + +์ด๋ฅผ ์œ„ํ•ด, ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋ณผ ์ˆ˜ ์žˆ๋Š” "Files changed" ํƒญ์œผ๋กœ ์ด๋™ํ•˜์—ฌ ์งˆ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ์ค„๋กœ ์ด๋™ํ•œ ๋‹ค์Œ "+" ๊ธฐํ˜ธ๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ์ด๋‚˜ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๋ฉด, ์ƒ์„ฑ๋œ ์ฝ”๋ฉ˜ํŠธ์˜ "Resolve" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Hugging Face ํŒ€์€ ์ฝ”๋“œ๋ฅผ ๋ฆฌ๋ทฐํ•  ๋•Œ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‚จ๊ธธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” PR์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ์งˆ๋ฌธ์„ GitHub์—์„œ ๋ฌป๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ณต๊ฐœ์— ํฌ๊ฒŒ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋งค์šฐ ์ผ๋ฐ˜์ ์ธ ์งˆ๋ฌธ์˜ ๊ฒฝ์šฐ, Slack์ด๋‚˜ ์ด๋ฉ”์ผ์„ ํ†ตํ•ด Hugging Face ํŒ€์—๊ฒŒ ๋ฌธ์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +**5. brand_new_bert์— ๋Œ€ํ•ด ์ƒ์„ฑ๋œ ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ์ ์šฉํ•˜๊ธฐ** + +๋จผ์ €, ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ ์ž์ฒด์—๋งŒ ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ํ† ํฌ๋‚˜์ด์ €์— ๋Œ€ํ•ด์„œ๋Š” ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ด€๋ จ ์ฝ”๋“œ๋Š” ๋‹ค์Œ์˜ ์ƒ์„ฑ๋œ ํŒŒ์ผ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py` ๋ฐ `src/transformers/models/brand_new_bert/configuration_brand_new_bert.py`. + +์ด์ œ ๋งˆ์นจ๋‚ด ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :). `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`์˜ ์ƒ์„ฑ๋œ ์ฝ”๋“œ๋Š” ์ธ์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ์ธ ๊ฒฝ์šฐ BERT์™€ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐ€์ง€๊ฑฐ๋‚˜, ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์ธ ๊ฒฝ์šฐ BART์™€ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐ€์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์‹œ์ ์—์„œ, ๋ชจ๋ธ์˜ ์ด๋ก ์  ์ธก๋ฉด์— ๋Œ€ํ•ด ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋‹ค์‹œ ์ƒ๊ธฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: *๋ชจ๋ธ์ด BERT ๋˜๋Š” BART์™€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ๊ฐ€์š”?*. ์ž์ฃผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ *self-attention* ๋ ˆ์ด์–ด, ์ •๊ทœํ™” ๋ ˆ์ด์–ด์˜ ์ˆœ์„œ ๋“ฑ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ž์‹ ์˜ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋„๋ก Transformers์—์„œ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ชจ๋ธ์˜ ์œ ์‚ฌํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +**์ฐธ๊ณ ๋กœ** ์ด ์‹œ์ ์—์„œ, ์ฝ”๋“œ๊ฐ€ ์™„์ „ํžˆ ์ •ํ™•ํ•˜๊ฑฐ๋‚˜ ๊นจ๋—ํ•˜๋‹ค๊ณ  ํ™•์‹ ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์˜คํžˆ๋ ค ์ฒ˜์Œ์—๋Š” ์›๋ณธ ์ฝ”๋“œ์˜ ์ฒซ ๋ฒˆ์งธ *๋ถˆ์™„์ „ํ•˜๊ณ * ๋ณต์‚ฌ๋œ ๋ฒ„์ „์„ `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ๊นŒ์ง€ ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ ํ›„, ๋‹ค์Œ ์„น์…˜์—์„œ ์„ค๋ช…ํ•œ ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ ์ง„์ ์œผ๋กœ ๊ฐœ์„ ํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด ์‹œ์ ์—์„œ ์ž‘๋™ํ•ด์•ผ ํ•˜๋Š” ์œ ์ผํ•œ ๊ฒƒ์€ ๋‹ค์Œ ๋ช…๋ น์ด ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```python +from transformers import BrandNewBertModel, BrandNewBertConfig + +model = BrandNewBertModel(BrandNewBertConfig()) +``` + +์œ„์˜ ๋ช…๋ น์€ `BrandNewBertConfig()`์— ์ •์˜๋œ ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋ฌด์ž‘์œ„ ๊ฐ€์ค‘์น˜๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋ฉฐ, ์ด๋กœ์จ ๋ชจ๋“  ๊ตฌ์„ฑ ์š”์†Œ์˜ `init()` ๋ฉ”์„œ๋“œ๊ฐ€ ์ž‘๋™ํ•จ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋“  ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”๋Š” `BrandnewBertPreTrainedModel` ํด๋ž˜์Šค์˜ `_init_weights` ๋ฉ”์„œ๋“œ์—์„œ ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๊ตฌ์„ฑ ์„ค์ • ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋ชจ๋“  ๋ฆฌํ”„ ๋ชจ๋“ˆ์„ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. BERT์˜ `_init_weights` ๋ฉ”์„œ๋“œ ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```py +def _init_weights(self, module): + """Initialize the weights""" + if isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.bias is not None: + module.bias.data.zero_() + elif isinstance(module, nn.Embedding): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.padding_idx is not None: + module.weight.data[module.padding_idx].zero_() + elif isinstance(module, nn.LayerNorm): + module.bias.data.zero_() + module.weight.data.fill_(1.0) +``` + +๋ช‡ ๊ฐ€์ง€ ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ํŠน๋ณ„ํ•œ ์ดˆ๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `Wav2Vec2ForPreTraining`์—์„œ ๋งˆ์ง€๋ง‰ ๋‘ ๊ฐœ์˜ ์„ ํ˜• ๋ ˆ์ด์–ด๋Š” ์ผ๋ฐ˜์ ์ธ PyTorch `nn.Linear`์˜ ์ดˆ๊ธฐํ™”๋ฅผ ๊ฐ€์ ธ์•ผ ํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ๋ชจ๋“  ๋ ˆ์ด์–ด๋Š” ์œ„์™€ ๊ฐ™์€ ์ดˆ๊ธฐํ™”๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œํ™”๋ฉ๋‹ˆ๋‹ค: + +```py +def _init_weights(self, module): + """Initialize the weights""" + if isinstance(module, Wav2Vec2ForPreTraining): + module.project_hid.reset_parameters() + module.project_q.reset_parameters() + module.project_hid._is_hf_initialized = True + module.project_q._is_hf_initialized = True + elif isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=self.config.initializer_range) + if module.bias is not None: + module.bias.data.zero_() +``` + +`_is_hf_initialized` ํ”Œ๋ž˜๊ทธ๋Š” ์„œ๋ธŒ๋ชจ๋“ˆ์„ ํ•œ ๋ฒˆ๋งŒ ์ดˆ๊ธฐํ™”ํ•˜๋„๋ก ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. `module.project_q` ๋ฐ `module.project_hid`์— ๋Œ€ํ•ด `True`๋กœ ์„ค์ •ํ•จ์œผ๋กœ์จ, ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ์ดˆ๊ธฐํ™”๊ฐ€ ์ดํ›„์— ๋ฎ์–ด์“ฐ์ด์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, `_init_weights` ํ•จ์ˆ˜๊ฐ€ ์ด๋“ค์—๊ฒŒ ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +**6. ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑํ•˜๊ธฐ** + +๋‹ค์Œ์œผ๋กœ, ๋””๋ฒ„๊ทธ์— ์‚ฌ์šฉํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๊ธฐ์กด ์ €์žฅ์†Œ์—์„œ ๋งŒ๋“  ๐Ÿค— Transformers ๊ตฌํ˜„๊ณผ ํ˜ธํ™˜๋˜๋Š” ์ฒดํฌํฌ์ธํŠธ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” *brand_new_bert*์™€ ๋™์ผํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์ž‘์„ฑ๋œ ์œ ์‚ฌํ•œ ๋ชจ๋ธ์„ ๋ณ€ํ™˜ํ•œ ๊ธฐ์กด ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ์กด ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ์•ฝ๊ฐ„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋Œ€ํ•ด ์œ ์‚ฌํ•œ ๊ธฐ์กด ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์–ด๋””์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”์ง€ Hugging Face ํŒ€์—๊ฒŒ ๋ฌธ์˜ํ•˜๋Š” ๊ฒƒ์„ ๋ง์„ค์ด์ง€ ๋งˆ์„ธ์š”. + +- TensorFlow์—์„œ PyTorch๋กœ ๋ชจ๋ธ์„ ์ด์ „ํ•˜๋Š” ๊ฒฝ์šฐ, ์ข‹์€ ์ฐธ๊ณ  ์ž๋ฃŒ๋กœ BERT์˜ ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/7acfa95afb8194f8f9c1f4d2c6028224dbed35a2/src/transformers/models/bert/modeling_bert.py#L91)๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- PyTorch์—์„œ PyTorch๋กœ ๋ชจ๋ธ์„ ์ด์ „ํ•˜๋Š” ๊ฒฝ์šฐ, ์ข‹์€ ์ฐธ๊ณ  ์ž๋ฃŒ๋กœ BART์˜ ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/bart/convert_bart_original_pytorch_checkpoint_to_pytorch.py)๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์—์„œ๋Š” PyTorch ๋ชจ๋ธ์ด ๋ ˆ์ด์–ด ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ ˆ์ด์–ด ์ด๋ฆ„์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. PyTorch์—์„œ ๋ ˆ์ด์–ด์˜ ์ด๋ฆ„์€ ๋ ˆ์ด์–ด์— ์ง€์ •ํ•œ ํด๋ž˜์Šค ์†์„ฑ์˜ ์ด๋ฆ„์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด PyTorch์—์„œ `SimpleModel`์ด๋ผ๋Š” ๋”๋ฏธ ๋ชจ๋ธ์„ ์ •์˜ํ•ด ๋ด…์‹œ๋‹ค: + +```python +from torch import nn + + +class SimpleModel(nn.Module): + def __init__(self): + super().__init__() + self.dense = nn.Linear(10, 10) + self.intermediate = nn.Linear(10, 10) + self.layer_norm = nn.LayerNorm(10) +``` + +์ด์ œ ์ด ๋ชจ๋ธ ์ •์˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ `dense`, `intermediate`, `layer_norm` ๋“ฑ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋žœ๋คํ•˜๊ฒŒ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์ถœ๋ ฅํ•˜์—ฌ ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +model = SimpleModel() + +print(model) +``` + +์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค: + +``` +SimpleModel( + (dense): Linear(in_features=10, out_features=10, bias=True) + (intermediate): Linear(in_features=10, out_features=10, bias=True) + (layer_norm): LayerNorm((10,), eps=1e-05, elementwise_affine=True) +) +``` + +์šฐ๋ฆฌ๋Š” ๋ ˆ์ด์–ด์˜ ์ด๋ฆ„์ด PyTorch์—์„œ ํด๋ž˜์Šค ์†์„ฑ์˜ ์ด๋ฆ„์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๋ ˆ์ด์–ด์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +print(model.dense.weight.data) +``` + +๊ฐ€์ค‘์น˜๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +tensor([[-0.0818, 0.2207, -0.0749, -0.0030, 0.0045, -0.1569, -0.1598, 0.0212, + -0.2077, 0.2157], + [ 0.1044, 0.0201, 0.0990, 0.2482, 0.3116, 0.2509, 0.2866, -0.2190, + 0.2166, -0.0212], + [-0.2000, 0.1107, -0.1999, -0.3119, 0.1559, 0.0993, 0.1776, -0.1950, + -0.1023, -0.0447], + [-0.0888, -0.1092, 0.2281, 0.0336, 0.1817, -0.0115, 0.2096, 0.1415, + -0.1876, -0.2467], + [ 0.2208, -0.2352, -0.1426, -0.2636, -0.2889, -0.2061, -0.2849, -0.0465, + 0.2577, 0.0402], + [ 0.1502, 0.2465, 0.2566, 0.0693, 0.2352, -0.0530, 0.1859, -0.0604, + 0.2132, 0.1680], + [ 0.1733, -0.2407, -0.1721, 0.1484, 0.0358, -0.0633, -0.0721, -0.0090, + 0.2707, -0.2509], + [-0.1173, 0.1561, 0.2945, 0.0595, -0.1996, 0.2988, -0.0802, 0.0407, + 0.1829, -0.1568], + [-0.1164, -0.2228, -0.0403, 0.0428, 0.1339, 0.0047, 0.1967, 0.2923, + 0.0333, -0.0536], + [-0.1492, -0.1616, 0.1057, 0.1950, -0.2807, -0.2710, -0.1586, 0.0739, + 0.2220, 0.2358]]). +``` + +๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฒดํฌํฌ์ธํŠธ์˜ ํ•ด๋‹น ๋ ˆ์ด์–ด์˜ ์ •ํ™•ํ•œ ๊ฐ€์ค‘์น˜๋กœ ์ฑ„์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +# retrieve matching layer weights, e.g. by +# recursive algorithm +layer_name = "dense" +pretrained_weight = array_of_dense_layer + +model_pointer = getattr(model, "dense") + +model_pointer.weight.data = torch.from_numpy(pretrained_weight) +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด PyTorch ๋ชจ๋ธ์˜ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ ๊ฐ ๊ฐ€์ค‘์น˜์™€ ํ•ด๋‹น ์ฒดํฌํฌ์ธํŠธ ๊ฐ€์ค‘์น˜๊ฐ€ **๋ชจ์–‘๊ณผ ์ด๋ฆ„** ๋ชจ๋‘์—์„œ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋ชจ์–‘์— ๋Œ€ํ•œ assert ๋ฌธ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ฒดํฌํฌ์ธํŠธ ๊ฐ€์ค‘์น˜์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```python +assert ( + model_pointer.weight.shape == pretrained_weight.shape +), f"Pointer shape of random weight {model_pointer.shape} and array shape of checkpoint weight {pretrained_weight.shape} mismatched" +``` + +๋˜ํ•œ ๋‘ ๊ฐ€์ค‘์น˜์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. *์˜ˆ์‹œ*: + +```python +logger.info(f"Initialize PyTorch weight {layer_name} from {pretrained_weight.name}") +``` + +๋ชจ์–‘ ๋˜๋Š” ์ด๋ฆ„์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ๋žœ๋ค์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ๋ ˆ์ด์–ด์— ์ž˜๋ชป๋œ ์ฒดํฌํฌ์ธํŠธ ๊ฐ€์ค‘์น˜๋ฅผ ํ• ๋‹นํ•œ ๊ฒƒ์œผ๋กœ ์ถ”์ธก๋ฉ๋‹ˆ๋‹ค. + +์ž˜๋ชป๋œ ๋ชจ์–‘์€ `BrandNewBertConfig()`์˜ ๊ตฌ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •์ด ๋ณ€ํ™˜ํ•˜๋ ค๋Š” ์ฒดํฌํฌ์ธํŠธ์— ์‚ฌ์šฉ๋œ ์„ค์ •๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๊ฐ€์žฅ ํฝ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ PyTorch์˜ ๋ ˆ์ด์–ด ๊ตฌํ˜„ ์ž์ฒด์—์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์ „์น˜ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ, **๋ชจ๋“ ** ํ•„์š”ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์ดˆ๊ธฐํ™”์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ๋ชจ๋ธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ณ€ํ™˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ ๋ชจ์–‘ ๋ฌธ์žฅ์ด๋‚˜ ์ž˜๋ชป๋œ ์ด๋ฆ„ ํ• ๋‹น์œผ๋กœ ์ธํ•ด ๋ณ€ํ™˜ ์‹œ๋„๊ฐ€ ์‹คํŒจํ•˜๋Š” ๊ฒƒ์€ ์™„์ „ํžˆ ์ •์ƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” `BrandNewBertConfig()`์—์„œ ์ž˜๋ชป๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๐Ÿค— Transformers ๊ตฌํ˜„์—์„œ ์ž˜๋ชป๋œ ์•„ํ‚คํ…์ฒ˜, ๐Ÿค— Transformers ๊ตฌํ˜„์˜ ๊ตฌ์„ฑ ์š”์†Œ ์ค‘ ํ•˜๋‚˜์˜ `init()` ํ•จ์ˆ˜์— ๋ฒ„๊ทธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์ด๊ฑฐ๋‚˜ ์ฒดํฌํฌ์ธํŠธ ๊ฐ€์ค‘์น˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ „์น˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๊ฐ€์žฅ ๋†’์Šต๋‹ˆ๋‹ค. + +์ด ๋‹จ๊ณ„๋Š” ์ด์ „ ๋‹จ๊ณ„์™€ ํ•จ๊ป˜ ๋ฐ˜๋ณต๋˜์–ด์•ผ ํ•˜๋ฉฐ ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ Transformers ๋ชจ๋ธ์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ๊นŒ์ง€ ๊ณ„์†๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿค— Transformers ๊ตฌํ˜„์— ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋กœ๋“œํ•œ ํ›„์—๋Š” `/path/to/converted/checkpoint/folder`์™€ ๊ฐ™์€ ์›ํ•˜๋Š” ํด๋”์— ๋ชจ๋ธ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋”์—๋Š” `pytorch_model.bin` ํŒŒ์ผ๊ณผ `config.json` ํŒŒ์ผ์ด ๋ชจ๋‘ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +model.save_pretrained("/path/to/converted/checkpoint/folder") +``` + +**7. ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค ๊ตฌํ˜„ํ•˜๊ธฐ** + +๐Ÿค— Transformers ๊ตฌํ˜„์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋กœ๋“œํ•œ ํ›„์—๋Š” ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. [์›๋ณธ ์ €์žฅ์†Œ์— ์ต์ˆ™ํ•ด์ง€๊ธฐ](#3-4-run-a-pretrained-checkpoint-using-the-original-repository)์—์„œ ์ด๋ฏธ ์›๋ณธ ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์›๋ณธ ๋Œ€์‹  ๐Ÿค— Transformers ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ์œ ์‚ฌํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```python +model = BrandNewBertModel.from_pretrained("/path/to/converted/checkpoint/folder") +input_ids = [0, 4, 4, 3, 2, 4, 1, 7, 19] +output = model(input_ids).last_hidden_states +``` + +๐Ÿค— Transformers ๊ตฌํ˜„๊ณผ ์›๋ณธ ๋ชจ๋ธ ๊ตฌํ˜„์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ์ •ํ™•ํžˆ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋งค์šฐ ๋†’์Šต๋‹ˆ๋‹ค. ์‹ค๋งํ•˜์ง€ ๋งˆ์„ธ์š”. ์˜ˆ์ƒ๋œ ์ผ์ž…๋‹ˆ๋‹ค! ๋จผ์ €, ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ข…์ข… ์ž˜๋ชป๋œ ์ฐจ์›์ด ์‚ฌ์šฉ๋˜์–ด *์ฐจ์› ๋ถˆ์ผ์น˜* ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ ์œ ํ˜• ๊ฐœ์ฒด๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด `torch.long` ๋Œ€์‹ ์— `torch.float32`๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด Hugging Face ํŒ€์— ๋„์›€์„ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers ๊ตฌํ˜„์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ์ถœ๋ ฅ์ด `1e-3`์˜ ์ •๋ฐ€๋„๋กœ ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ €, ์ถœ๋ ฅ ๋ชจ์–‘์ด ๋™์ผํ•˜๋„๋ก ๋ณด์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๐Ÿค— Transformers ๊ตฌํ˜„ ์Šคํฌ๋ฆฝํŠธ์™€ ์›๋ณธ ๊ตฌํ˜„ ์‚ฌ์ด์—์„œ `outputs.shape`๋Š” ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋‹ค์Œ์œผ๋กœ, ์ถœ๋ ฅ ๊ฐ’์ด ๋™์ผํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ด ๋™์ผํ•˜์ง€ ์•Š์€ ์ผ๋ฐ˜์ ์ธ ์‹ค์ˆ˜ ์‚ฌ๋ก€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +- ์ผ๋ถ€ ๋ ˆ์ด์–ด๊ฐ€ ์ถ”๊ฐ€๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ฆ‰, *ํ™œ์„ฑํ™”* ๋ ˆ์ด์–ด๊ฐ€ ์ถ”๊ฐ€๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ž”์ฐจ ์—ฐ๊ฒฐ์ด ๋น ์กŒ์Šต๋‹ˆ๋‹ค. +- ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์ด ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. +- ์ž˜๋ชป๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ๊ตฌํ˜„์—์„œ๋Š” ์˜คํ”„์…‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค ์ค‘์— Dropout์ด ์ ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋ฉด *model.training์ด False*์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค ์ค‘์— Dropout ๋ ˆ์ด์–ด๊ฐ€ ์ž˜๋ชป ํ™œ์„ฑํ™”๋˜์ง€ ์•Š๋„๋ก ํ•˜์„ธ์š”. ์ฆ‰, [PyTorch์˜ ๊ธฐ๋Šฅ์  Dropout](https://pytorch.org/docs/stable/nn.functional.html?highlight=dropout#torch.nn.functional.dropout)์— *self.training*์„ ์ „๋‹ฌํ•˜์„ธ์š”. + +๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์›๋ณธ ๊ตฌํ˜„๊ณผ ๐Ÿค— Transformers ๊ตฌํ˜„์˜ ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค๋ฅผ ๋‚˜๋ž€ํžˆ ๋†“๊ณ  ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ƒ์ ์œผ๋กœ๋Š” ์ˆœ๋ฐฉํ–ฅ ํŒจ์Šค์˜ ์ค‘๊ฐ„ ์ถœ๋ ฅ์„ ๋””๋ฒ„๊ทธ/์ถœ๋ ฅํ•˜์—ฌ ์›๋ณธ ๊ตฌํ˜„๊ณผ ๐Ÿค— Transformers ๊ตฌํ˜„์˜ ์ •ํ™•ํ•œ ์œ„์น˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ €, ๋‘ ์Šคํฌ๋ฆฝํŠธ์˜ ํ•˜๋“œ์ฝ”๋”ฉ๋œ `input_ids`๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜์„ธ์š”. ๋‹ค์Œ์œผ๋กœ, `input_ids`์˜ ์ฒซ ๋ฒˆ์งธ ๋ณ€ํ™˜์˜ ์ถœ๋ ฅ(์ผ๋ฐ˜์ ์œผ๋กœ ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ)์ด ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜์„ธ์š”. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋„คํŠธ์›Œํฌ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด๊นŒ์ง€ ์ง„ํ–‰ํ•ด๋ณด์„ธ์š”. ์–ด๋А ์‹œ์ ์—์„œ ๋‘ ๊ตฌํ˜„ ์‚ฌ์ด์— ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Š” ๐Ÿค— Transformers ๊ตฌํ˜„์˜ ๋ฒ„๊ทธ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ €ํฌ ๊ฒฝํ—˜์ƒ์œผ๋กœ๋Š” ์›๋ณธ ๊ตฌํ˜„๊ณผ ๐Ÿค— Transformers ๊ตฌํ˜„ ๋ชจ๋‘์—์„œ ๋™์ผํ•œ ์œ„์น˜์— ๋งŽ์€ ์ถœ๋ ฅ ๋ฌธ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ด๋“ค์˜ ์ค‘๊ฐ„ ํ‘œํ˜„์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฐ’์„ ๋ณด์ด๋Š” ์ถœ๋ ฅ ๋ฌธ์„ ์—ฐ์†์ ์œผ๋กœ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ„๋‹จํ•˜๊ณ  ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +`torch.allclose(original_output, output, atol=1e-3)`๋กœ ์ถœ๋ ฅ์„ ํ™•์ธํ•˜์—ฌ ๋‘ ๊ตฌํ˜„์ด ๋™์ผํ•œ ์ถœ๋ ฅ์„ ํ•˜๋Š” ๊ฒƒ์„ ํ™•์‹ ํ•œ๋‹ค๋ฉด, ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ์ถ•ํ•˜๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋‚จ์€ ์ž‘์—…์€ ์‰ฌ์šด ์ผ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค ๐Ÿ˜Š. + +**8. ํ•„์š”ํ•œ ๋ชจ๋“  ๋ชจ๋ธ ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ** + +์ด ์‹œ์ ์—์„œ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•ด๋‹น ๋ชจ๋ธ์ด ์š”๊ตฌ๋˜๋Š” ๋””์ž์ธ์— ์™„์ „ํžˆ ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค— Transformers์™€ ์™„๋ฒฝํ•˜๊ฒŒ ํ˜ธํ™˜๋˜๋Š” ๊ตฌํ˜„์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ผ๋ฐ˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Cookiecutter๋Š” ์•„๋งˆ๋„ ๋ชจ๋ธ์„ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋งˆ๋„ `tests/models/brand_new_bert/test_modeling_brand_new_bert.py`์™€ ๊ฐ™์€ ๊ฒฝ๋กœ์— ์œ„์น˜ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜์—ฌ ์ผ๋ฐ˜ ํ…Œ์ŠคํŠธ๊ฐ€ ๋ชจ๋‘ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```bash +pytest tests/models/brand_new_bert/test_modeling_brand_new_bert.py +``` + +๋ชจ๋“  ์ผ๋ฐ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•œ ํ›„, ์ด์ œ ์ˆ˜ํ–‰ํ•œ ์ž‘์—…์„ ์ถฉ๋ถ„ํžˆ ํ…Œ์ŠคํŠธํ•˜์—ฌ ๋‹ค์Œ ์‚ฌํ•ญ์„ ๋ณด์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- a) ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ *brand_new_bert*์˜ ํŠน์ • ํ…Œ์ŠคํŠธ๋ฅผ ์‚ดํŽด๋ด„์œผ๋กœ์จ ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ +- b) ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ–ฅํ›„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ๋ชจ๋ธ์˜ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์„ ์†์ƒ์‹œํ‚ค์ง€ ์•Š๋„๋ก ํ•จ + +๋จผ์ € ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋Š” ์ด์ „์— ๋ชจ๋ธ์„ ๐Ÿค— Transformers๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๋””๋ฒ„๊น… ์Šคํฌ๋ฆฝํŠธ์™€ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Cookiecutter์— ์ด๋ฏธ ์ด๋Ÿฌํ•œ ๋ชจ๋ธ ํ…Œ์ŠคํŠธ์˜ ํ…œํ”Œ๋ฆฟ์ธ `BrandNewBertModelIntegrationTests`๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์žˆ์œผ๋ฉฐ, ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•ด์•ผ ํ•  ๋‚ด์šฉ์œผ๋กœ๋งŒ ์ฑ„์›Œ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +RUN_SLOW=1 pytest -sv tests/models/brand_new_bert/test_modeling_brand_new_bert.py::BrandNewBertModelIntegrationTests +``` + + + +Windows๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ `RUN_SLOW=1`์„ `SET RUN_SLOW=1`๋กœ ๋ฐ”๊ฟ”์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +๋‘˜์งธ๋กœ, *brand_new_bert*์— ํŠนํ™”๋œ ๋ชจ๋“  ๊ธฐ๋Šฅ๋„ ๋ณ„๋„์˜ ํ…Œ์ŠคํŠธ์—์„œ ์ถ”๊ฐ€๋กœ ํ…Œ์ŠคํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ข…์ข… ์žŠํžˆ๋Š”๋ฐ, ๋‘ ๊ฐ€์ง€ ์ธก๋ฉด์—์„œ ๊ต‰์žฅํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +- *brand_new_bert*์˜ ํŠน์ˆ˜ ๊ธฐ๋Šฅ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ ์ปค๋ฎค๋‹ˆํ‹ฐ์—๊ฒŒ ๋ชจ๋ธ ์ถ”๊ฐ€ ๊ณผ์ •์—์„œ ์Šต๋“ํ•œ ์ง€์‹์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. +- ํ–ฅํ›„ ๊ธฐ์—ฌ์ž๋Š” ์ด๋Ÿฌํ•œ ํŠน์ˆ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋น ๋ฅด๊ฒŒ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +**9. ํ† ํฌ๋‚˜์ด์ € ๊ตฌํ˜„ํ•˜๊ธฐ** + +๋‹ค์Œ์œผ๋กœ, *brand_new_bert*์˜ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ํ† ํฌ๋‚˜์ด์ €๋Š” ๐Ÿค— Transformers์˜ ๊ธฐ์กด ํ† ํฌ๋‚˜์ด์ €์™€ ๋™์ผํ•˜๊ฑฐ๋‚˜ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. + +ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๊ณ  `input_ids`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์œ ์‚ฌํ•œ ์Šคํฌ๋ฆฝํŠธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (์˜์‚ฌ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ): + +```python +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." +model = BrandNewBertModel.load_pretrained_checkpoint("/path/to/checkpoint/") +input_ids = model.tokenize(input_str) +``` + +์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ณ  ์˜ฌ๋ฐ”๋ฅธ ํ† ํฌ๋‚˜์ด์ € ํ•จ์ˆ˜๋ฅผ ์ฐพ๊ฑฐ๋‚˜, ๋ณต์ œ๋ณธ์—์„œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ ์šฉํ•˜์—ฌ `input_ids`๋งŒ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์ ์ธ ํ† ํฐํ™” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ ํ›„, ๐Ÿค— Transformers์˜ ์œ ์‚ฌํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```python +from transformers import BrandNewBertTokenizer + +input_str = "This is a long example input string containing special characters .$?-, numbers 2872 234 12 and words." + +tokenizer = BrandNewBertTokenizer.from_pretrained("/path/to/tokenizer/folder/") + +input_ids = tokenizer(input_str).input_ids +``` + +๋‘ ๊ฐœ์˜ `input_ids`๊ฐ€ ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ, ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋กœ ํ† ํฌ๋‚˜์ด์ € ํ…Œ์ŠคํŠธ ํŒŒ์ผ๋„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +*brand_new_bert*์˜ ๋ชจ๋ธ๋ง ํ…Œ์ŠคํŠธ ํŒŒ์ผ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, *brand_new_bert*์˜ ํ† ํฌ๋‚˜์ด์ œ์ด์…˜ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•˜๋“œ์ฝ”๋”ฉ๋œ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**10. ์ข…๋‹จ ๊ฐ„ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์‹คํ–‰** + +ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์—๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์ข…๋‹จ ๊ฐ„ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `tests/models/brand_new_bert/test_modeling_brand_new_bert.py`์— ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”. ์ด๋Ÿฌํ•œ ํ…Œ์ŠคํŠธ๋Š” ๐Ÿค— Transformers ๊ตฌํ˜„์ด ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€๋ฅผ ์˜๋ฏธ ์žˆ๋Š” text-to-text ์˜ˆ์‹œ๋กœ ๋ณด์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์˜ˆ์‹œ๋กœ๋Š” *์˜ˆ๋ฅผ ๋“ค์–ด* source-to-target ๋ฒˆ์—ญ ์Œ, article-to-summary ์Œ, question-to-answer ์Œ ๋“ฑ์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถˆ๋Ÿฌ์˜จ ์ฒดํฌํฌ์ธํŠธ ์ค‘ ์–ด๋А ๊ฒƒ๋„ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋ชจ๋ธ ํ…Œ์ŠคํŠธ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์™„์ „ํžˆ ๊ธฐ๋Šฅ์„ ๊ฐ–์ถ”์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋กœ GPU์—์„œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๋‚ด๋ถ€ ํ…์„œ์˜ ์ผ๋ถ€์— `.to(self.device)` ๋ฌธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ์—์„œ ์˜ค๋ฅ˜๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. GPU์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, Hugging Face ํŒ€์ด ํ…Œ์ŠคํŠธ๋ฅผ ๋Œ€์‹  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +**11. ๊ธฐ์ˆ ๋ฌธ์„œ ์ถ”๊ฐ€** + +์ด์ œ *brand_new_bert*์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋ฉ‹์ง„ ๊ธฐ์ˆ ๋ฌธ์„œ๊ณผ ๊ธฐ์ˆ ๋ฌธ์„œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. Cookiecutter๊ฐ€ `docs/source/model_doc/brand_new_bert.md`๋ผ๋Š” ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์คฌ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ํŽ˜์ด์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๋“ค์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ํŽ˜์ด์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์„œ๋Š” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด *ํŒ*์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋…์ŠคํŠธ๋ง์— ๊ด€๋ จํ•˜์—ฌ Hugging Face ํŒ€์— ๋ฌธ์˜ํ•˜๋Š” ๊ฒƒ์„ ์ฃผ์ €ํ•˜์ง€ ๋งˆ์„ธ์š”. + +๋‹ค์Œ์œผ๋กœ, `src/transformers/models/brand_new_bert/modeling_brand_new_bert.py`์— ์ถ”๊ฐ€๋œ ๋…์ŠคํŠธ๋ง์ด ์˜ฌ๋ฐ”๋ฅด๋ฉฐ ํ•„์š”ํ•œ ๋ชจ๋“  ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์„ ํฌํ•จํ•˜๋„๋ก ํ™•์ธํ•˜์„ธ์š”. [์—ฌ๊ธฐ](writing-documentation)์—์„œ ์šฐ๋ฆฌ์˜ ๋ฌธ์„œ ์ž‘์„ฑ ๊ฐ€์ด๋“œ์™€ ๋…์ŠคํŠธ๋ง ํ˜•์‹์— ๋Œ€ํ•œ ์ƒ์„ธ ๊ฐ€์ด๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๋ชจ๋ธ์˜ ์ฒซ ๋ฒˆ์งธ ์ ‘์ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์„œ๋Š” ์ ์–ด๋„ ์ฝ”๋“œ๋งŒํผ์˜ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง** + +์ข‹์•„์š”, ์ด์ œ *brand_new_bert*๋ฅผ ์œ„ํ•œ ๋ชจ๋“  ํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์‹œ์ ์—์„œ ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์—ฌ ์ž ์žฌ์ ์œผ๋กœ ์ž˜๋ชป๋œ ์ฝ”๋“œ ์Šคํƒ€์ผ์„ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +๊ทธ๋ฆฌ๊ณ  ์ฝ”๋”ฉ ์Šคํƒ€์ผ์ด ํ’ˆ์งˆ ์ ๊ฒ€์„ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜๊ณ  ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +make style +``` + +๐Ÿค— Transformers์—๋Š” ์—ฌ์ „ํžˆ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋งค์šฐ ์—„๊ฒฉํ•œ ๋””์ž์ธ ํ…Œ์ŠคํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋…์ŠคํŠธ๋ง์— ๋ˆ„๋ฝ๋œ ์ •๋ณด๋‚˜ ์ž˜๋ชป๋œ ๋ช…๋ช… ๋•Œ๋ฌธ์— ์ข…์ข… ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ง‰ํžˆ๋ฉด Hugging Face ํŒ€์ด ๋„์›€์„ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```bash +make quality +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ, ์ฝ”๋“œ๊ฐ€ ์ •ํ™•ํžˆ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•œ ํ›„์—๋Š” ํ•ญ์ƒ ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผ๋œ ์ง€๊ธˆ์€ ์ถ”๊ฐ€ํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๊ฒ€ํ† ํ•˜๊ณ  ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ์ข‹์€ ์‹œ๊ธฐ์ž…๋‹ˆ๋‹ค. + +์ด์ œ ์ฝ”๋”ฉ ๋ถ€๋ถ„์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ๐ŸŽ‰ ๋ฉ‹์ ธ์š”! ๐Ÿ˜Ž + +**12. ๋ชจ๋ธ์„ ๋ชจ๋ธ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜์„ธ์š”** + +์ด ๋งˆ์ง€๋ง‰ ํŒŒํŠธ์—์„œ๋Š” ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ชจ๋ธ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜๊ณ  ๊ฐ ์—…๋กœ๋“œ๋œ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ์— ๋Œ€ํ•œ ๋ชจ๋ธ ์นด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. [Model sharing and uploading Page](model_sharing)๋ฅผ ์ฝ๊ณ  ํ—ˆ๋ธŒ ๊ธฐ๋Šฅ์— ์ต์ˆ™ํ•ด์ง€์„ธ์š”. *brand_new_bert*์˜ ์ €์ž ์กฐ์ง ์•„๋ž˜์— ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ํ•„์š”ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด Hugging Face ํŒ€๊ณผ ํ˜‘์—…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `transformers`์˜ ๋ชจ๋“  ๋ชจ๋ธ์— ์žˆ๋Š” `push_to_hub` ๋ฉ”์„œ๋“œ๋Š” ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ—ˆ๋ธŒ์— ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์œผ๋กœ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์— ์ž‘์€ ์ฝ”๋“œ ์กฐ๊ฐ์ด ๋ถ™์—ฌ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค: + +๊ฐ ์ฒดํฌํฌ์ธํŠธ์— ์ ํ•ฉํ•œ ๋ชจ๋ธ ์นด๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‹œ๊ฐ„์„ ํ• ์• ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ์นด๋“œ๋Š” ์ฒดํฌํฌ์ธํŠธ์˜ ํŠน์„ฑ์„ ๊ฐ•์กฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. *์˜ˆ๋ฅผ ๋“ค์–ด* ์ด ์ฒดํฌํฌ์ธํŠธ๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ์…‹์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ/์„ธ๋ถ€ ํ›ˆ๋ จ๋˜์—ˆ๋Š”์ง€? ์ด ๋ชจ๋ธ์€ ์–ด๋–ค ํ•˜์œ„ ์ž‘์—…์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€? ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ฝ”๋“œ๋„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +brand_new_bert.push_to_hub("brand_new_bert") +# Uncomment the following line to push to an organization. +# brand_new_bert.push_to_hub("/brand_new_bert") +``` + +**13. (์„ ํƒ ์‚ฌํ•ญ) ๋…ธํŠธ๋ถ ์ถ”๊ฐ€** + +*brand_new_bert*๋ฅผ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์—์„œ ์ถ”๋ก  ๋˜๋Š” ๋ฏธ์„ธ ์กฐ์ •์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ๋ณด์—ฌ์ฃผ๋Š” ๋…ธํŠธ๋ถ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ PR์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๋ฐ ํ•„์ˆ˜์ ์ด์ง€๋Š” ์•Š์ง€๋งŒ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +**14. ์™„๋ฃŒ๋œ PR ์ œ์ถœ** + +์ด์ œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋งˆ์ณค์œผ๋ฉฐ, ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋กœ PR์„ ๋ฉ”์ธ ๋ธŒ๋žœ์น˜์— ๋ณ‘ํ•ฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต Hugging Face ํŒ€์€ ์ด๋ฏธ ์—ฌ๊ธฐ๊นŒ์ง€ ๋„์›€์„ ์ฃผ์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ PR์— ๋ฉ‹์ง„ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฆฌ๋ทฐ์–ด์—๊ฒŒ ํŠน์ • ๋””์ž์ธ ์„ ํƒ ์‚ฌํ•ญ์„ ๊ฐ•์กฐํ•˜๋ ค๋ฉด ์™„๋ฃŒ๋œ PR์— ์•ฝ๊ฐ„์˜ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•˜๋Š” ์‹œ๊ฐ„์„ ํ• ์• ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ์ž‘์—…๋ฌผ์„ ๊ณต์œ ํ•˜์„ธ์š”!! [[share-your-work]] + +์ด์ œ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ž‘์—…๋ฌผ์„ ์ธ์ •๋ฐ›์„ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค! ๋ชจ๋ธ ์ถ”๊ฐ€ ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋Š” ๊ฒƒ์€ Transformers์™€ ์ „์ฒด NLP ์ปค๋ฎค๋‹ˆํ‹ฐ์— ํฐ ๊ธฐ์—ฌ์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ์ฝ”๋“œ์™€ ์ด์‹๋œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์€ ์ˆ˜๋ฐฑ, ์‹ฌ์ง€์–ด ์ˆ˜์ฒœ ๋ช…์˜ ๊ฐœ๋ฐœ์ž์™€ ์—ฐ๊ตฌ์›์— ์˜ํ•ด ํ™•์‹คํžˆ ์‚ฌ์šฉ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ์ž‘์—…์— ์ž๋ž‘์Šค๋Ÿฌ์›Œํ•ด์•ผ ํ•˜๋ฉฐ ์ด๋ฅผ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**๋‹น์‹ ์€ ์ปค๋ฎค๋‹ˆํ‹ฐ ๋‚ด ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋งค์šฐ ์‰ฝ๊ฒŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋˜ ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! ๐Ÿคฏ** diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_pipeline.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_pipeline.md new file mode 100644 index 0000000000000000000000000000000000000000..42c9b57c9d7be675010069bc1803233befba410a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/add_new_pipeline.md @@ -0,0 +1,244 @@ + + +# ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ƒ์„ฑํ•˜๋‚˜์š”? [[how-to-create-a-custom-pipeline]] + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  [ํ—ˆ๋ธŒ](https://hf.co/models)์— ๊ณต์œ ํ•˜๊ฑฐ๋‚˜ ๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +๋จผ์ € ํŒŒ์ดํ”„๋ผ์ธ์ด ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์›์‹œ ์ž…๋ ฅ์„ ๊ฒฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๋ฌธ์ž์—ด, ์›์‹œ ๋ฐ”์ดํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋˜๋Š” ๊ฐ€์žฅ ์›ํ•˜๋Š” ์ž…๋ ฅ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๊ฒƒ์ด๋ฉด ๋ฌด์—‡์ด๋“  ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +์ด ์ž…๋ ฅ์„ ๊ฐ€๋Šฅํ•œ ํ•œ ์ˆœ์ˆ˜ํ•œ Python ํ˜•์‹์œผ๋กœ ์œ ์ง€ํ•ด์•ผ (JSON์„ ํ†ตํ•ด ๋‹ค๋ฅธ ์–ธ์–ด์™€๋„) ํ˜ธํ™˜์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค. +์ด๊ฒƒ์ด ์ „์ฒ˜๋ฆฌ(`preprocess`) ํŒŒ์ดํ”„๋ผ์ธ์˜ ์ž…๋ ฅ(`inputs`)์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฐ ๋‹ค์Œ `outputs`๋ฅผ ์ •์˜ํ•˜์„ธ์š”. +`inputs`์™€ ๊ฐ™์€ ์ •์ฑ…์„ ๋”ฐ๋ฅด๊ณ , ๊ฐ„๋‹จํ• ์ˆ˜๋ก ์ข‹์Šต๋‹ˆ๋‹ค. +์ด๊ฒƒ์ด ํ›„์ฒ˜๋ฆฌ(`postprocess`) ๋ฉ”์†Œ๋“œ์˜ ์ถœ๋ ฅ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋จผ์ € 4๊ฐœ์˜ ๋ฉ”์†Œ๋“œ(`preprocess`, `_forward`, `postprocess` ๋ฐ `_sanitize_parameters`)๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ํด๋ž˜์Šค `Pipeline`์„ ์ƒ์†ํ•˜์—ฌ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. + + +```python +from transformers import Pipeline + + +class MyPipeline(Pipeline): + def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "maybe_arg" in kwargs: + preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"] + return preprocess_kwargs, {}, {} + + def preprocess(self, inputs, maybe_arg=2): + model_input = Tensor(inputs["input_ids"]) + return {"model_input": model_input} + + def _forward(self, model_inputs): + # model_inputs == {"model_input": model_input} + outputs = self.model(**model_inputs) + # Maybe {"logits": Tensor(...)} + return outputs + + def postprocess(self, model_outputs): + best_class = model_outputs["logits"].softmax(-1) + return best_class +``` + +์ด ๋ถ„ํ•  ๊ตฌ์กฐ๋Š” CPU/GPU์— ๋Œ€ํ•œ ๋น„๊ต์  ์›ํ™œํ•œ ์ง€์›์„ ์ œ๊ณตํ•˜๋Š” ๋™์‹œ์—, ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ CPU์— ๋Œ€ํ•œ ์‚ฌ์ „/์‚ฌํ›„ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +`preprocess`๋Š” ์›๋ž˜ ์ •์˜๋œ ์ž…๋ ฅ์„ ๊ฐ€์ ธ์™€ ๋ชจ๋ธ์— ๊ณต๊ธ‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ `Dict` ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. + +`_forward`๋Š” ๊ตฌํ˜„ ์„ธ๋ถ€ ์‚ฌํ•ญ์ด๋ฉฐ ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. +`forward`๋Š” ์˜ˆ์ƒ ์žฅ์น˜์—์„œ ๋ชจ๋“  ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์•ˆ์ „์žฅ์น˜๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ์„ ํ˜ธ๋˜๋Š” ํ˜ธ์ถœ ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. +์‹ค์ œ ๋ชจ๋ธ๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์€ `_forward` ๋ฉ”์†Œ๋“œ์— ์†ํ•˜๋ฉฐ, ๋‚˜๋จธ์ง€๋Š” ์ „์ฒ˜๋ฆฌ/ํ›„์ฒ˜๋ฆฌ ๊ณผ์ •์— ์žˆ์Šต๋‹ˆ๋‹ค. + +`postprocess` ๋ฉ”์†Œ๋“œ๋Š” `_forward`์˜ ์ถœ๋ ฅ์„ ๊ฐ€์ ธ์™€ ์ด์ „์— ๊ฒฐ์ •ํ•œ ์ตœ์ข… ์ถœ๋ ฅ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +`_sanitize_parameters`๋Š” ์ดˆ๊ธฐํ™” ์‹œ๊ฐ„์— `pipeline(...., maybe_arg=4)`์ด๋‚˜ ํ˜ธ์ถœ ์‹œ๊ฐ„์— `pipe = pipeline(...); output = pipe(...., maybe_arg=4)`๊ณผ ๊ฐ™์ด, ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์–ธ์ œ๋“ ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. + +`_sanitize_parameters`์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ `preprocess`, `_forward`, `postprocess`์— ์ง์ ‘ ์ „๋‹ฌ๋˜๋Š” 3๊ฐœ์˜ kwargs ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค. +ํ˜ธ์ถœ์ž๊ฐ€ ์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์ฑ„์šฐ์ง€ ๋งˆ์‹ญ์‹œ์˜ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ญ์ƒ ๋” "์ž์—ฐ์Šค๋Ÿฌ์šด" ํ•จ์ˆ˜ ์ •์˜์˜ ๊ธฐ๋ณธ ์ธ์ˆ˜๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ถ„๋ฅ˜ ์ž‘์—…์—์„œ `top_k` ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ž…๋‹ˆ๋‹ค. + +```python +>>> pipe = pipeline("my-new-task") +>>> pipe("This is a test") +[{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}, {"label": "3-star", "score": 0.05} +{"label": "4-star", "score": 0.025}, {"label": "5-star", "score": 0.025}] + +>>> pipe("This is a test", top_k=2) +[{"label": "1-star", "score": 0.8}, {"label": "2-star", "score": 0.1}] +``` + +์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” `postprocess` ๋ฉ”์†Œ๋“œ๋ฅผ ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ `5`๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ณ  `_sanitize_parameters`๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ด ์ƒˆ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. + + +```python +def postprocess(self, model_outputs, top_k=5): + best_class = model_outputs["logits"].softmax(-1) + # top_k๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง ์ถ”๊ฐ€ + return best_class + + +def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "maybe_arg" in kwargs: + preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"] + + postprocess_kwargs = {} + if "top_k" in kwargs: + postprocess_kwargs["top_k"] = kwargs["top_k"] + return preprocess_kwargs, {}, postprocess_kwargs +``` + +์ž…/์ถœ๋ ฅ์„ ๊ฐ€๋Šฅํ•œ ํ•œ ๊ฐ„๋‹จํ•˜๊ณ  ์™„์ „ํžˆ JSON ์ง๋ ฌํ™” ๊ฐ€๋Šฅํ•œ ํ˜•์‹์œผ๋กœ ์œ ์ง€ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜์‹ญ์‹œ์˜ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ƒˆ๋กœ์šด ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด๋ฅผ ์ดํ•ดํ•˜์ง€ ์•Š๊ณ ๋„ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋˜ํ•œ ์‚ฌ์šฉ ์šฉ์ด์„ฑ์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ํ˜•์˜ ์ธ์ˆ˜(์˜ค๋””์˜ค ํŒŒ์ผ์€ ํŒŒ์ผ ์ด๋ฆ„, URL ๋˜๋Š” ์ˆœ์ˆ˜ํ•œ ๋ฐ”์ดํŠธ์ผ ์ˆ˜ ์žˆ์Œ)๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด ๋น„๊ต์  ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. + + + +## ์ง€์›๋˜๋Š” ์ž‘์—… ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•˜๊ธฐ [[adding-it-to-the-list-of-supported-tasks]] + +`new-task`๋ฅผ ์ง€์›๋˜๋Š” ์ž‘์—… ๋ชฉ๋ก์— ๋“ฑ๋กํ•˜๋ ค๋ฉด `PIPELINE_REGISTRY`์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```python +from transformers.pipelines import PIPELINE_REGISTRY + +PIPELINE_REGISTRY.register_pipeline( + "new-task", + pipeline_class=MyPipeline, + pt_model=AutoModelForSequenceClassification, +) +``` + +์›ํ•˜๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ ๋ชจ๋ธ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ ํŠน์ • ๊ฐœ์ •(๋ถ„๊ธฐ ์ด๋ฆ„ ๋˜๋Š” ์ปค๋ฐ‹ ํ•ด์‹œ์ผ ์ˆ˜ ์žˆ์Œ, ์—ฌ๊ธฐ์„œ๋Š” "abcdef")๊ณผ ํƒ€์ž…์„ ํ•จ๊ป˜ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```python +PIPELINE_REGISTRY.register_pipeline( + "new-task", + pipeline_class=MyPipeline, + pt_model=AutoModelForSequenceClassification, + default={"pt": ("user/awesome_model", "abcdef")}, + type="text", # ํ˜„์žฌ ์ง€์› ์œ ํ˜•: text, audio, image, multimodal +) +``` + +## Hub์— ํŒŒ์ดํ”„๋ผ์ธ ๊ณต์œ ํ•˜๊ธฐ [[share-your-pipeline-on-the-hub]] + +Hub์— ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ณต์œ ํ•˜๋ ค๋ฉด `Pipeline` ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ๋ฅผ Python ํŒŒ์ผ์— ์ €์žฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +import numpy as np + +from transformers import Pipeline + + +def softmax(outputs): + maxes = np.max(outputs, axis=-1, keepdims=True) + shifted_exp = np.exp(outputs - maxes) + return shifted_exp / shifted_exp.sum(axis=-1, keepdims=True) + + +class PairClassificationPipeline(Pipeline): + def _sanitize_parameters(self, **kwargs): + preprocess_kwargs = {} + if "second_text" in kwargs: + preprocess_kwargs["second_text"] = kwargs["second_text"] + return preprocess_kwargs, {}, {} + + def preprocess(self, text, second_text=None): + return self.tokenizer(text, text_pair=second_text, return_tensors=self.framework) + + def _forward(self, model_inputs): + return self.model(**model_inputs) + + def postprocess(self, model_outputs): + logits = model_outputs.logits[0].numpy() + probabilities = softmax(logits) + + best_class = np.argmax(probabilities) + label = self.model.config.id2label[best_class] + score = probabilities[best_class].item() + logits = logits.tolist() + return {"label": label, "score": score, "logits": logits} +``` + +๊ตฌํ˜„์€ ํ”„๋ ˆ์ž„์›Œํฌ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š์œผ๋ฉฐ, PyTorch์™€ TensorFlow ๋ชจ๋ธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +์ด๋ฅผ `pair_classification.py`๋ผ๋Š” ํŒŒ์ผ์— ์ €์žฅํ•œ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ€์ ธ์˜ค๊ณ  ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from pair_classification import PairClassificationPipeline +from transformers.pipelines import PIPELINE_REGISTRY +from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification + +PIPELINE_REGISTRY.register_pipeline( + "pair-classification", + pipeline_class=PairClassificationPipeline, + pt_model=AutoModelForSequenceClassification, + tf_model=TFAutoModelForSequenceClassification, +) +``` + +์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, `sgugger/finetuned-bert-mrpc`์€ MRPC ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋˜์–ด ๋ฌธ์žฅ ์Œ์„ ํŒจ๋Ÿฌํ”„๋ ˆ์ด์ฆˆ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import pipeline + +classifier = pipeline("pair-classification", model="sgugger/finetuned-bert-mrpc") +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ `push_to_hub` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +classifier.push_to_hub("test-dynamic-pipeline") +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด "test-dynamic-pipeline" ํด๋” ๋‚ด์— `PairClassificationPipeline`์„ ์ •์˜ํ•œ ํŒŒ์ผ์ด ๋ณต์‚ฌ๋˜๋ฉฐ, ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋„ ์ €์žฅํ•œ ํ›„, `{your_username}/test-dynamic-pipeline` ์ €์žฅ์†Œ์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์„ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค. +์ดํ›„์—๋Š” `trust_remote_code=True` ์˜ต์…˜๋งŒ ์ œ๊ณตํ•˜๋ฉด ๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import pipeline + +classifier = pipeline(model="{your_username}/test-dynamic-pipeline", trust_remote_code=True) +``` + +## ๐Ÿค— Transformers์— ํŒŒ์ดํ”„๋ผ์ธ ์ถ”๊ฐ€ํ•˜๊ธฐ [[add-the-pipeline-to-transformers]] + +๐Ÿค— Transformers์— ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ธฐ์—ฌํ•˜๋ ค๋ฉด, `pipelines` ํ•˜์œ„ ๋ชจ๋“ˆ์— ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ดํ”„๋ผ์ธ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์ƒˆ ๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ, `pipelines/__init__.py`์—์„œ ์ •์˜๋œ ์ž‘์—… ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฐ ๋‹ค์Œ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +`tests/test_pipelines_MY_PIPELINE.py`๋ผ๋Š” ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ์™€ ์˜ˆ์ œ๋ฅผ ํ•จ๊ป˜ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. + +`run_pipeline_test` ํ•จ์ˆ˜๋Š” ๋งค์šฐ ์ผ๋ฐ˜์ ์ด๋ฉฐ, `model_mapping` ๋ฐ `tf_model_mapping`์—์„œ ์ •์˜๋œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์˜ ์ž‘์€ ๋ฌด์ž‘์œ„ ๋ชจ๋ธ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. + +์ด๋Š” ํ–ฅํ›„ ํ˜ธํ™˜์„ฑ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐ ๋งค์šฐ ์ค‘์š”ํ•˜๋ฉฐ, ๋ˆ„๊ตฐ๊ฐ€ `XXXForQuestionAnswering`์„ ์œ„ํ•œ ์ƒˆ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํŒŒ์ดํ”„๋ผ์ธ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•ด๋‹น ๋ชจ๋ธ์—์„œ ์‹คํ–‰์„ ์‹œ๋„ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. +๋ชจ๋ธ์ด ๋ฌด์ž‘์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ๋‹จ์ˆœํžˆ ํŒŒ์ดํ”„๋ผ์ธ ์ถœ๋ ฅ `TYPE`๊ณผ ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋„์šฐ๋ฏธ `ANY`๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ 2๊ฐœ(์ด์ƒ์ ์œผ๋กœ๋Š” 4๊ฐœ)์˜ ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- `test_small_model_pt`: ์ด ํŒŒ์ดํ”„๋ผ์ธ์— ๋Œ€ํ•œ ์ž‘์€ ๋ชจ๋ธ 1๊ฐœ๋ฅผ ์ •์˜(๊ฒฐ๊ณผ๊ฐ€ ์˜๋ฏธ ์—†์–ด๋„ ์ƒ๊ด€์—†์Œ)ํ•˜๊ณ  ํŒŒ์ดํ”„๋ผ์ธ ์ถœ๋ ฅ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. +๊ฒฐ๊ณผ๋Š” `test_small_model_tf`์™€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- `test_small_model_tf`: ์ด ํŒŒ์ดํ”„๋ผ์ธ์— ๋Œ€ํ•œ ์ž‘์€ ๋ชจ๋ธ 1๊ฐœ๋ฅผ ์ •์˜(๊ฒฐ๊ณผ๊ฐ€ ์˜๋ฏธ ์—†์–ด๋„ ์ƒ๊ด€์—†์Œ)ํ•˜๊ณ  ํŒŒ์ดํ”„๋ผ์ธ ์ถœ๋ ฅ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. +๊ฒฐ๊ณผ๋Š” `test_small_model_pt`์™€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- `test_large_model_pt`(`์„ ํƒ์‚ฌํ•ญ`): ๊ฒฐ๊ณผ๊ฐ€ ์˜๋ฏธ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ์‹ค์ œ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ํ…Œ์ŠคํŠธ๋Š” ์†๋„๊ฐ€ ๋А๋ฆฌ๋ฏ€๋กœ ์ด๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์—ฌ๊ธฐ์„œ์˜ ๋ชฉํ‘œ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ ๋ณด์—ฌ์ฃผ๊ณ  ํ–ฅํ›„ ๋ฆด๋ฆฌ์ฆˆ์—์„œ์˜ ๋ณ€ํ™”๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- `test_large_model_tf`(`์„ ํƒ์‚ฌํ•ญ`): ๊ฒฐ๊ณผ๊ฐ€ ์˜๋ฏธ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ์‹ค์ œ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ํ…Œ์ŠคํŠธ๋Š” ์†๋„๊ฐ€ ๋А๋ฆฌ๋ฏ€๋กœ ์ด๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์—ฌ๊ธฐ์„œ์˜ ๋ชฉํ‘œ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ ๋ณด์—ฌ์ฃผ๊ณ  ํ–ฅํ›„ ๋ฆด๋ฆฌ์ฆˆ์—์„œ์˜ ๋ณ€ํ™”๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/cache_explanation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/cache_explanation.md new file mode 100644 index 0000000000000000000000000000000000000000..dcf60b4a0ee9c92cae8ebbc0a0e18e1ff4caaa83 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/cache_explanation.md @@ -0,0 +1,188 @@ + + +# ์บ์‹ฑ[[caching]] +๋ˆ„๊ตฐ๊ฐ€์™€ ๋Œ€ํ™”๋ฅผ ๋‚˜๋ˆ„๊ณ  ์žˆ๋Š”๋ฐ, ์ƒ๋Œ€๋ฐฉ์ด ์ด์ „์— ํ–ˆ๋˜ ๋ง์„ ๊ธฐ์–ตํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋‹น์‹ ์ด ๋Œ€๋‹ตํ•  ๋•Œ๋งˆ๋‹ค ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์„ธ์š”. ์ด๋Š” ๋А๋ฆฌ๊ณ  ๋น„ํšจ์œจ์ ์ด๊ฒ ์ฃ ? + +์ด ๋น„์œ ๋ฅผ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๊ธฐํšŒ๊ท€ ๋ชจ๋ธ์˜ ์ƒ์„ฑ์€ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ํ† ํฐ์”ฉ ์˜ˆ์ธกํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋А๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ƒˆ๋กœ์šด ์˜ˆ์ธก์€ ์ด์ „์˜ ๋ชจ๋“  ๋ฌธ๋งฅ์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. + +1000๋ฒˆ์งธ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋ ค๋ฉด, ๋ชจ๋ธ์€ ์ด์ „ 999๊ฐœ ํ† ํฐ์˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋Š” ๊ฐ ํ† ํฐ ํ‘œํ˜„๋“ค ์‚ฌ์ด์˜ ํ–‰๋ ฌ ๊ณฑ์„ ํ†ตํ•ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. + +1001๋ฒˆ์งธ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋ ค๋ฉด, ์ด์ „ 999๊ฐœ ํ† ํฐ์˜ ๋™์ผํ•œ ์ •๋ณด์— ๋”ํ•˜์—ฌ 1000๋ฒˆ์งธ ํ† ํฐ์˜ ์ •๋ณด๋„ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ† ํฐ๋งˆ๋‹ค ๋ชจ๋ธ์€ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋งŽ์€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! + +์ด๋Ÿฌํ•œ ๋น„ํšจ์œจ์„ฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด KV ์บ์‹œ(Key-Value Cache)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ ˆ์ด์–ด์—์„œ ์ด์ „์— ์ฒ˜๋ฆฌํ•œ ํ† ํฐ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ํ‚ค์™€ ๊ฐ’ ์Œ์„ ์ €์žฅํ•ด๋‘๊ณ , ์ดํ›„ ํ† ํฐ ์˜ˆ์ธก ์‹œ ์ด๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐ์„ ์ค„์ด๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +> [!WARNING] +> ์บ์‹ฑ์€ **์ถ”๋ก **์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ์ค‘์— ํ™œ์„ฑํ™”๋˜๋ฉด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์บ์‹ฑ์ด ์–ด๋–ป๊ฒŒ ๊ทธ๋ฆฌ๊ณ  ์™œ ์ž‘๋™ํ•˜๋Š”์ง€ ๋” ์ž˜ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด, ์–ดํ…์…˜ ํ–‰๋ ฌ์˜ ๊ตฌ์กฐ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ์–ดํ…์…˜ ํ–‰๋ ฌ[[attention-matrices]] + +**์Šค์ผ€์ผ๋“œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜**์€ ๋ฐฐ์น˜ ํฌ๊ธฐ `b`, ์–ดํ…์…˜ ํ—ค๋“œ ์ˆ˜ `h`, ํ˜„์žฌ๊นŒ์ง€์˜ ์‹œํ€€์Šค ๊ธธ์ด `T`, ์–ดํ…์…˜ ํ—ค๋“œ๋‹น ์ฐจ์› `d_head`์— ๋Œ€ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. + +$$ +\text{Attention}(Q, K, V) = \text{softmax}\left( \frac{Q K^\top}{\sqrt{d_{\text{head}}}} \times \text{mask} \right) V +$$ + +์ฟผ๋ฆฌ(`Q`), ํ‚ค(`K`), ๊ฐ’(`V`) ํ–‰๋ ฌ์€ `(b, h, T, d_head)` ํ˜•ํƒœ์˜ ์ž…๋ ฅ ์ž„๋ฒ ๋”ฉ์—์„œ์˜ ํˆฌ์˜์ž…๋‹ˆ๋‹ค. + +์ธ๊ณผ์  ์–ดํ…์…˜์˜ ๊ฒฝ์šฐ, ๋งˆ์Šคํฌ๋Š” ๋ชจ๋ธ์ด ๋ฏธ๋ž˜ ํ† ํฐ์— ์–ดํ…์…˜ ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ์ด ํ•œ ๋ฒˆ ์ฒ˜๋ฆฌ๋˜๋ฉด, ๊ทธ ํ‘œํ˜„์€ ๋ฏธ๋ž˜ ํ† ํฐ๊ณผ ๊ด€๋ จํ•˜์—ฌ ์ ˆ๋Œ€ ๋ณ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” \\( K_{\text{past}} \\)์™€ \\( V_{\text{past}} \\)๋ฅผ ์บ์‹œํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ํ† ํฐ์˜ ํ‘œํ˜„์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +$$ +\text{Attention}(q_t, [\underbrace{k_1, k_2, \dots, k_{t-1}}_{\text{cached}}, k_{t}], [\underbrace{v_1, v_2, \dots, v_{t-1}}_{\text{cached}}, v_{t}]) +$$ + +์ถ”๋ก  ์‹œ์—๋Š” ๋‹ค์Œ ํ† ํฐ \\( t+1 \\)์„ ์˜ˆ์ธกํ•˜๋Š” ํ‘œํ˜„ \\( x_t \\)๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ง€๋ง‰ ํ† ํฐ์˜ ์ฟผ๋ฆฌ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๊ณ„์—์„œ ์ƒˆ๋กœ์šด ํ‚ค์™€ ๊ฐ’ ๋ฒกํ„ฐ๊ฐ€ ์บ์‹œ์— **์ €์žฅ**๋˜๊ณ  ๊ณผ๊ฑฐ ํ‚ค์™€ ๊ฐ’์— **์ถ”๊ฐ€**๋ฉ๋‹ˆ๋‹ค. + +$$ +K_{\text{cache}} \leftarrow \text{concat}(K_{\text{past}}, k_t), \quad V_{\text{cache}} \leftarrow \text{concat}(V_{\text{past}}, v_t) +$$ + +์–ดํ…์…˜์€ ๋ชจ๋ธ์˜ ๊ฐ ๋ ˆ์ด์–ด์—์„œ ๋…๋ฆฝ์ ์œผ๋กœ ๊ณ„์‚ฐ๋˜๋ฉฐ, ์บ์‹ฑ์€ ๋ ˆ์ด์–ด๋ณ„๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. + +์บ์‹ฑ์ด ํšจ์œจ์„ฑ์„ ์–ด๋–ป๊ฒŒ ๊ฐœ์„ ํ•˜๋Š”์ง€ ๋น„๊ตํ•œ ์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +| ์บ์‹ฑ ์—†์Œ | ์บ์‹ฑ ์‚ฌ์šฉ | +|---|---| +| ๋‹จ๊ณ„๋งˆ๋‹ค ์ด์ „์˜ ๋ชจ๋“  `K`์™€ `V`๋ฅผ ์žฌ๊ณ„์‚ฐ | ๋‹จ๊ณ„๋งˆ๋‹ค ํ˜„์žฌ์˜ `K`์™€ `V`๋งŒ ๊ณ„์‚ฐ | +| ๋‹จ๊ณ„๋‹น ์–ดํ…์…˜ ๋น„์šฉ์ด ์‹œํ€€์Šค ๊ธธ์ด์— ๋Œ€ํ•ด **์ œ๊ณฑ** | ๋‹จ๊ณ„๋‹น ์–ดํ…์…˜ ๋น„์šฉ์ด ์‹œํ€€์Šค ๊ธธ์ด์— ๋Œ€ํ•ด **์„ ํ˜•** (๋ฉ”๋ชจ๋ฆฌ๋Š” ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜์ง€๋งŒ, ํ† ํฐ๋‹น ๊ณ„์‚ฐ์€ ๋‚ฎ๊ฒŒ ์œ ์ง€๋จ) | + + + +## ์บ์‹œ ํด๋ž˜์Šค[[cache-class]] + +๊ธฐ๋ณธ KV ์บ์‹œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ˜„์žฌ ํ† ํฐ์˜ ํ‚ค์™€ ๊ฐ’ ํ…์„œ๋ฅผ ๋ฐ›์•„์„œ ์—…๋ฐ์ดํŠธ๋œ `K`์™€ `V` ํ…์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์˜ `forward` ๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค. + +```py +new_K, new_V = cache.update(k_t, v_t, layer_idx) +attn_output = attn_layer_idx_fn(q_t, new_K, new_V) +``` + +Transformers์˜ [`Cache`] ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์…€ํ”„ ์–ดํ…์…˜ ๋ชจ๋“ˆ์€ ๊ณผ๊ฑฐ์™€ ํ˜„์žฌ ์ •๋ณด๋ฅผ ํ†ตํ•ฉํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +1. ์–ดํ…์…˜ ๋ชจ๋“ˆ์€ ํ˜„์žฌ kv ์Œ์„ ์บ์‹œ์— ์ €์žฅ๋œ ๊ณผ๊ฑฐ kv ์Œ๊ณผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” `(new_tokens_length, past_kv_length + new_tokens_length)` ํ˜•ํƒœ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ์™€ ๊ณผ๊ฑฐ kv ์Œ์ด ๋ณธ์งˆ์ ์œผ๋กœ ๊ฒฐํ•ฉํ•ด ์–ดํ…์…˜ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉฐ, ๋ชจ๋ธ์ด ์ด์ „ ๋ฌธ๋งฅ๊ณผ ํ˜„์žฌ ์ž…๋ ฅ์„ ์ธ์‹ํ•˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. + +2. `forward` ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœ๋  ๋•Œ, ์–ดํ…์…˜ ๋งˆ์Šคํฌ ํ˜•ํƒœ๊ฐ€ ๊ณผ๊ฑฐ์™€ ํ˜„์žฌ kv ์Œ์˜ ๊ฒฐํ•ฉ๋œ ๊ธธ์ด์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” `(batch_size, past_kv_length + new_tokens_length)` ํ˜•ํƒœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ [`~GenerationMixin.generate`]์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€๋งŒ, [`Cache`]๋กœ ์ž์ฒด ์ƒ์„ฑ ๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด๋ฅผ ์—ผ๋‘์— ๋‘์„ธ์š”! ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” ๊ณผ๊ฑฐ์™€ ํ˜„์žฌ ํ† ํฐ๊ฐ’์„ ๋ณด์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +3. `cache_position`์„ ์ธ์‹ํ•˜๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์œ ํšจํ•œ `cache_position` ๊ฐ’์„ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ `forward` ๋ฉ”์†Œ๋“œ๋กœ ๋ฏธ๋ฆฌ ์ฑ„์›Œ์ง„ [`Cache`]๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹œํ€€์Šค์—์„œ์˜ ์ž…๋ ฅ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. `cache_position`์€ ํŒจ๋”ฉ์— ์˜ํ–ฅ๋ฐ›์ง€ ์•Š์œผ๋ฉฐ, ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด ํ•ญ์ƒ ํ•˜๋‚˜์”ฉ ๋” ๋งŽ์€ ์œ„์น˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, kv ์บ์‹œ๊ฐ€ 10๊ฐœ์˜ ํ† ํฐ์„ ํฌํ•จํ•˜๋ฉด - ํŒจ๋“œ ํ† ํฐ๊ณผ ๊ด€๊ณ„์—†์ด - ๋‹ค์Œ ํ† ํฐ์˜ ์บ์‹œ ์œ„์น˜๋Š” `torch.tensor([10])`์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## ์บ์‹œ ์ €์žฅ์†Œ ๊ตฌํ˜„[[cache-storage-implementation]] + +์บ์‹œ๋Š” ๊ฐ ๋ ˆ์ด์–ด๊ฐ€ key์™€ value ์บ์‹œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ ˆ์ด์–ด ๋ชฉ๋ก ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. key ๋ฐ value ์บ์‹œ๋Š” `[batch_size, num_heads, seq_len, head_dim]` ํ˜•ํƒœ์˜ ํ…์„œ์ž…๋‹ˆ๋‹ค. + +๋ ˆ์ด์–ด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ(์˜ˆ: `DynamicLayer`, `StaticLayer`, `SlidingWindowLayer`), ์ด๋Š” ์ฃผ๋กœ ์‹œํ€€์Šค ๊ธธ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์บ์‹œ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐฑ์‹ ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. + +๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ๋Š” `DynamicLayer`๋กœ, ๋” ๋งŽ์€ ํ† ํฐ์ด ์ฒ˜๋ฆฌ๋จ์— ๋”ฐ๋ผ ์ ์ง„์ ์œผ๋กœ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. ์‹œํ€€์Šค ๊ธธ์ด ์ฐจ์›(`seq_len`)์€ ์ƒˆ๋กœ์šด ํ† ํฐ์ด ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: + +```py +cache.layers[idx].keys = torch.cat([cache.layers[idx].keys, key_states], dim=-2) +cache.layers[idx].values = torch.cat([cache.layers[idx].values, value_states], dim=-2) +``` + +`StaticLayer`๋‚˜ `SlidingWindowLayer`์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ ˆ์ด์–ด ํƒ€์ž…์€ ์บ์‹œ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๊ณ ์ •๋œ ์‹œํ€€์Šค ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์ด๋Š” `torch.compile`๊ณผ ํ˜ธํ™˜๋˜๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค. `SlidingWindowLayer`์˜ ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด ํ† ํฐ์ด ์ถ”๊ฐ€๋˜๋ฉด ๊ธฐ์กด ํ† ํฐ์€ ์บ์‹œ์—์„œ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์ œ๋Š” [`DynamicCache`]๋กœ ์ƒ์„ฑ ๋ฃจํ”„๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋…ผ์˜๋œ ๋ฐ”์™€ ๊ฐ™์ด, ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” ๊ณผ๊ฑฐ์™€ ํ˜„์žฌ ํ† ํฐ๊ฐ’์˜ ์—ฐ๊ฒฐ์ด๋ฉฐ ๋‹ค์Œ ํ† ํฐ์„ ์œ„ํ•ด ์บ์‹œ ์œ„์น˜์— `1`์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. + +```py +import torch +from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache, infer_device + +device = f"{infer_device()}:0" + +model_id = "meta-llama/Llama-2-7b-chat-hf" +model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map=device) +tokenizer = AutoTokenizer.from_pretrained(model_id) + +past_key_values = DynamicCache() +messages = [{"role": "user", "content": "Hello, what's your name."}] +inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device) + +generated_ids = inputs.input_ids +cache_position = torch.arange(inputs.input_ids.shape[1], dtype=torch.int64, device=model.device) +max_new_tokens = 10 + +for _ in range(max_new_tokens): + outputs = model(**inputs, cache_position=cache_position, past_key_values=past_key_values, use_cache=True) + # ํƒ์š•์  ๊ธฐ๋ฒ•์œผ๋กœ ๋‹ค์Œ ํ† ํฐ ํ•˜๋‚˜๋ฅผ ์ƒ˜ํ”Œ๋ง + next_token_ids = outputs.logits[:, -1:].argmax(-1) + generated_ids = torch.cat([generated_ids, next_token_ids], dim=-1) + # ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ํ† ํฐ์„ ๋‚จ๊ฒจ๋‘์–ด ๋‹ค์Œ ์ƒ์„ฑ ๋‹จ๊ณ„๋ฅผ ์œ„ํ•œ ์ž…๋ ฅ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ํ† ํฐ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. + # ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์œ„ํ•ด ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋ฅผ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค + attention_mask = inputs["attention_mask"] + attention_mask = torch.cat([attention_mask, attention_mask.new_ones((attention_mask.shape[0], 1))], dim=-1) + inputs = {"input_ids": next_token_ids, "attention_mask": attention_mask} + cache_position = cache_position[-1:] + 1 # ๋‹ค์Œ ํ† ํฐ์„ ์œ„ํ•ด ํ•˜๋‚˜ ๋” ์œ„์น˜ ์ถ”๊ฐ€ + +print(tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]) +"[INST] Hello, what's your name. [/INST] Hello! My name is LLaMA," +``` + +## ์บ์‹œ ์œ„์น˜[[cache-position]] + +์บ์‹œ ์œ„์น˜๋Š” ์–ดํ…์…˜ ์บ์‹œ์—์„œ ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์‚ฝ์ž…ํ•  ์œ„์น˜๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŒจ๋”ฉ์ด๋‚˜ ๋ฐฐ์น˜ ๊ตฌ์กฐ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ปจํ…์ŠคํŠธ ๋‚ด์—์„œ ๊ฐ ํ† ํฐ์˜ ์ ˆ๋Œ€์  ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฏธ `N`๊ฐœ์˜ ํ† ํฐ์„ ์บ์‹œํ–ˆ๊ณ  ํ˜„์žฌ `K`๊ฐœ์˜ ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ† ํฐ์— ๋Œ€ํ•œ ์บ์‹œ ์œ„์น˜๋Š” `N`๋ถ€ํ„ฐ `N + K - 1`๊นŒ์ง€์˜ ๋ฒ”์œ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, `[N, N + 1, N + 2, ..., N + K - 1]` ์œ„์น˜์˜ ํ† ํฐ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์บ์‹œ ์œ„์น˜๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋‘ ๊ฐ€์ง€ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค: + +1. ์ž…๋ ฅ ์‹œํ€€์Šค์—์„œ ์ฒ˜๋ฆฌํ•  ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์„ ํƒํ•˜๊ณ , ์•„์ง ์บ์‹œ๋˜์ง€ ์•Š์€ ํ† ํฐ๋งŒ ๋ชจ๋ธ์˜ `forward`์— ์ „๋‹ฌ๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. +2. ํ‚ค/๊ฐ’ ์Œ์„ ์บ์‹œ์˜ ์˜ฌ๋ฐ”๋ฅธ ์œ„์น˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŠน์ • ์บ์‹œ ๊ธธ์ด๋ฅผ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•˜๋Š” [`StaticCache`]์™€ ๊ฐ™์€ ๊ณ ์ • ํฌ๊ธฐ ์บ์‹œ์—์„œ ํŠนํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. + +์ƒ์„ฑ ๋ฃจํ”„๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์บ์‹œ ์œ„์น˜๋ฅผ ๊ด€๋ฆฌํ•˜์ง€๋งŒ, ์‚ฌ์šฉ์ž ์ •์˜ ์ƒ์„ฑ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์บ์‹œ ์œ„์น˜๊ฐ€ ์ •ํ™•ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์บ์‹œ ์œ„์น˜๋Š” ๊ณ ์ •๋œ ์Šฌ๋กฏ์— ํ‚ค/๊ฐ’ ์ƒํƒœ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + + +```py +import torch +from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache, infer_device + +device = f"{infer_device()}:0" + +model_id = "meta-llama/Llama-2-7b-chat-hf" +model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map=device) +tokenizer = AutoTokenizer.from_pretrained(model_id) + +messages = [{"role": "user", "content": "You are a helpful assistant."}] +inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device) +generated_ids = model.generate(**inputs, use_cache=True, max_new_tokens=10) + +``` + + +## ๋ ˆ๊ฑฐ์‹œ ์บ์‹œ ํ˜•์‹[[legacy-cache-format]] + +[`Cache`] ํด๋ž˜์Šค ์ด์ „์—๋Š” ์บ์‹œ๊ฐ€ ํ…์„œ์˜ ํŠœํ”Œ์˜ ํŠœํ”Œ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ˜•์‹์€ ํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋จ์— ๋”ฐ๋ผ ์ฆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋™์ ์ด๋ฉฐ, [`DynamicCache`]์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. + +๋ ˆ๊ฑฐ์‹œ ํ˜•์‹์€ ๋ณธ์งˆ์ ์œผ๋กœ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด์ง€๋งŒ ๋‹ค๋ฅด๊ฒŒ ์กฐ์งํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- ๊ฐ ๋‚ด๋ถ€ ํŠœํ”Œ์€ ๋ ˆ์ด์–ด์˜ ํ‚ค์™€ ๊ฐ’ ํ…์„œ๋ฅผ ํฌํ•จํ•˜๋Š” ํŠœํ”Œ์˜ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. +- ํ…์„œ๋Š” ๋™์ผํ•œ ํ˜•ํƒœ `[batch_size, num_heads, seq_len, head_dim]`๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. +- ์ด ํ˜•์‹์€ ๋œ ์œ ์—ฐํ•˜๋ฉฐ ์–‘์žํ™”๋‚˜ ์˜คํ”„๋กœ๋”ฉ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +ํ”„๋กœ์ ํŠธ๊ฐ€ ์ด ๋ ˆ๊ฑฐ์‹œ ํ˜•์‹์— ์˜์กดํ•œ๋‹ค๋ฉด, [`~DynamicCache.from_legacy_cache`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [`DynamicCache`]๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ๊ฑฐ์‹œ ์บ์‹œ ํ˜•์‹์€ ์‚ฌ์šฉ์ด ์ค‘๋‹จ๋˜์—ˆ์œผ๋ฉฐ `Transformers`์—์„œ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŠน์ • ํ˜•์‹์—์„œ ์บ์‹œ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ปค์Šคํ…€ ๋กœ์ง์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋„์›€์ด ๋˜๋Š” [`DynamicCache.to_legacy_cache`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠœํ”Œ ํ˜•์‹์œผ๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +import torch +from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf") +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", torch_dtype=torch.float16, device_map="auto") +inputs = tokenizer("Hello, my name is", return_tensors="pt").to(model.device) + +# ์บ์‹œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด `return_dict_in_generate=True`๊ฐ€ ํ•„์š”ํ•˜๊ณ  `return_legacy_cache`๋Š” ๋ฐ˜ํ™˜๋œ ์บ์‹œ๋ฅผ +# ๋ ˆ๊ฑฐ์‹œ ํ˜•์‹์œผ๋กœ ๊ฐ•์ œํ•ฉ๋‹ˆ๋‹ค +generation_outputs = model.generate(**inputs, return_dict_in_generate=True, return_legacy_cache=True, max_new_tokens=5) + +cache = DynamicCache.from_legacy_cache(generation_outputs.past_key_values) +legacy_format_cache = cache.to_legacy_cache() +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/chat_templating.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/chat_templating.md new file mode 100644 index 0000000000000000000000000000000000000000..5e6cbc4491dd9951f42f00b8c5022e72dda920c6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/chat_templating.md @@ -0,0 +1,720 @@ + + +# ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ[[templates-for-chat-models]] + +## ์†Œ๊ฐœ[[introduction]] + +์š”์ฆ˜ LLM์˜ ๊ฐ€์žฅ ํ”ํ•œ ํ™œ์šฉ ์‚ฌ๋ก€ ์ค‘ ํ•˜๋‚˜๋Š” **์ฑ„ํŒ…**์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ…์€ ์ผ๋ฐ˜์ ์ธ ์–ธ์–ด ๋ชจ๋ธ์ฒ˜๋Ÿผ ๋‹จ์ผ ๋ฌธ์ž์—ด์„ ์ด์–ด๊ฐ€๋Š” ๋Œ€์‹  ์—ฌ๋Ÿฌ ๊ฐœ์˜ **๋ฉ”์‹œ์ง€**๋กœ ๊ตฌ์„ฑ๋œ ๋Œ€ํ™”๋ฅผ ์ด์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด ๋Œ€ํ™”์—๋Š” "์‚ฌ์šฉ์ž"๋‚˜ "์–ด์‹œ์Šคํ„ดํŠธ"์™€ ๊ฐ™์€ **์—ญํ• **๊ณผ ๋ฉ”์‹œ์ง€ ํ…์ŠคํŠธ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. + +ํ† ํฐํ™”์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์€ ์ฑ„ํŒ…์— ๋Œ€ํ•ด ๋งค์šฐ ๋‹ค๋ฅธ ์ž…๋ ฅ ํ˜•์‹์„ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ **์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ**์„ ๊ธฐ๋Šฅ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ํ† ํฌ๋‚˜์ด์ €์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ๋Œ€ํ™” ๋ชฉ๋ก์„ ๋ชจ๋ธ์ด ๊ธฐ๋Œ€ํ•˜๋Š” ํ˜•์‹์ธ '๋‹จ์ผ ํ† ํฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด'๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. + +`BlenderBot` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ด๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BlenderBot์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ ๋Œ€ํ™” ๋ผ์šด๋“œ ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") + +>>> chat = [ +... {"role": "user", "content": "Hello, how are you?"}, +... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, +... {"role": "user", "content": "I'd like to show off how chat templating works!"}, +... ] + +>>> tokenizer.apply_chat_template(chat, tokenize=False) +" Hello, how are you? I'm doing great. How can I help you today? I'd like to show off how chat templating works!" +``` + +์ „์ฒด ์ฑ„ํŒ…์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์••์ถ•๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •์ธ `tokenize=True`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ ๋ฌธ์ž์—ด๋„ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด `mistralai/Mistral-7B-Instruct-v0.1` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") + +>>> chat = [ +... {"role": "user", "content": "Hello, how are you?"}, +... {"role": "assistant", "content": "I'm doing great. How can I help you today?"}, +... {"role": "user", "content": "I'd like to show off how chat templating works!"}, +... ] + +>>> tokenizer.apply_chat_template(chat, tokenize=False) +"[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" +``` + +์ด๋ฒˆ์—๋Š” ํ† ํฌ๋‚˜์ด์ €๊ฐ€ [INST]์™€ [/INST] ์ œ์–ด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์„ ํ‘œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค(์–ด์‹œ์Šคํ„ดํŠธ ๋ฉ”์‹œ์ง€ ์ œ์™ธ). Mistral-instruct๋Š” ์ด๋Ÿฌํ•œ ํ† ํฐ์œผ๋กœ ํ›ˆ๋ จ๋˜์—ˆ์ง€๋งŒ, BlenderBot์€ ๊ทธ๋ ‡์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. + +## ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋‚˜์š”?[[how-do-i-use-chat-templates]] + +์œ„์˜ ์˜ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. `role`๊ณผ `content` ํ‚ค๊ฐ€ ํฌํ•จ๋œ ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก์„ ์ž‘์„ฑํ•œ ๋‹ค์Œ, [`~PreTrainedTokenizer.apply_chat_template`] ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ๋ ฅ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค! ๋ชจ๋ธ ์ƒ์„ฑ์˜ ์ž…๋ ฅ์œผ๋กœ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ๋•Œ, `add_generation_prompt=True`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ](#what-are-generation-prompts)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ `Zephyr` ์–ด์‹œ์Šคํ„ดํŠธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ `model.generate()`์˜ ์ž…๋ ฅ์„ ์ค€๋น„ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +checkpoint = "HuggingFaceH4/zephyr-7b-beta" +tokenizer = AutoTokenizer.from_pretrained(checkpoint) +model = AutoModelForCausalLM.from_pretrained(checkpoint) # ์—ฌ๊ธฐ์„œ bfloat16 ์‚ฌ์šฉ ๋ฐ/๋˜๋Š” GPU๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +messages = [ + { + "role": "system", + "content": "You are a friendly chatbot who always responds in the style of a pirate", + }, + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, + ] +tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") +print(tokenizer.decode(tokenized_chat[0])) +``` +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Zephyr๊ฐ€ ๊ธฐ๋Œ€ํ•˜๋Š” ์ž…๋ ฅ ํ˜•์‹์˜ ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. +```text +<|system|> +You are a friendly chatbot who always responds in the style of a pirate +<|user|> +How many helicopters can a human eat in one sitting? +<|assistant|> +``` + +์ด์ œ ์ž…๋ ฅ์ด Zephyr์— ๋งž๊ฒŒ ํ˜•์‹์ด ์ง€์ •๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +outputs = model.generate(tokenized_chat, max_new_tokens=128) +print(tokenizer.decode(outputs[0])) +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค: + +```text +<|system|> +You are a friendly chatbot who always responds in the style of a pirate +<|user|> +How many helicopters can a human eat in one sitting? +<|assistant|> +Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all. +``` + +์ด์ œ ์‰ฌ์›Œ์กŒ์ฃ ! + +## ์ฑ„ํŒ…์„ ์œ„ํ•œ ์ž๋™ํ™”๋œ ํŒŒ์ดํ”„๋ผ์ธ์ด ์žˆ๋‚˜์š”?[[is-there-an-automated-pipeline-for-chat]] + +๋„ค, ์žˆ์Šต๋‹ˆ๋‹ค! ์šฐ๋ฆฌ์˜ ํ…์ŠคํŠธ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ์€ ์ฑ„ํŒ… ์ž…๋ ฅ์„ ์ง€์›ํ•˜์—ฌ ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์—๋Š” "ConversationalPipeline" ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ์ด์ œ๋Š” ์ด ๊ธฐ๋Šฅ์ด [`TextGenerationPipeline`]์— ํ†ตํ•ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ `Zephyr` ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import pipeline + +pipe = pipeline("text-generation", "HuggingFaceH4/zephyr-7b-beta") +messages = [ + { + "role": "system", + "content": "You are a friendly chatbot who always responds in the style of a pirate", + }, + {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, +] +print(pipe(messages, max_new_tokens=128)[0]['generated_text'][-1]) # ์–ด์‹œ์Šคํ„ดํŠธ์˜ ์‘๋‹ต์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. +``` + +```text +{'role': 'assistant', 'content': "Matey, I'm afraid I must inform ye that humans cannot eat helicopters. Helicopters are not food, they are flying machines. Food is meant to be eaten, like a hearty plate o' grog, a savory bowl o' stew, or a delicious loaf o' bread. But helicopters, they be for transportin' and movin' around, not for eatin'. So, I'd say none, me hearties. None at all."} +``` + +ํŒŒ์ดํ”„๋ผ์ธ์€ ํ† ํฐํ™”์™€ `apply_chat_template` ํ˜ธ์ถœ ์˜ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋ธ์— ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ์žˆ์œผ๋ฉด ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก์„ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค! + + +## "์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ"๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?[[what-are-generation-prompts]] + +`apply_chat_template` ๋ฉ”์„œ๋“œ์—๋Š” `add_generation_prompt` ์ธ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ธ์ˆ˜๋Š” ํ…œํ”Œ๋ฆฟ์— ๋ด‡ ์‘๋‹ต์˜ ์‹œ์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฑ„ํŒ…์„ ๊ณ ๋ คํ•ด ๋ณด์„ธ์š”: + +```python +messages = [ + {"role": "user", "content": "Hi there!"}, + {"role": "assistant", "content": "Nice to meet you!"}, + {"role": "user", "content": "Can I ask a question?"} +] +``` + +Zephyr ์˜ˆ์ œ์—์„œ ๋ณด์•˜๋˜ ๊ฒƒ๊ณผ ๊ฐ™์ด, ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ ์—†์ด ChatML ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```python +tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) +"""<|im_start|>user +Hi there!<|im_end|> +<|im_start|>assistant +Nice to meet you!<|im_end|> +<|im_start|>user +Can I ask a question?<|im_end|> +""" +``` + +์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๊ฐ€ **์žˆ๋Š”** ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) +"""<|im_start|>user +Hi there!<|im_end|> +<|im_start|>assistant +Nice to meet you!<|im_end|> +<|im_start|>user +Can I ask a question?<|im_end|> +<|im_start|>assistant +""" +``` + +์ด๋ฒˆ์—๋Š” ๋ด‡ ์‘๋‹ต์˜ ์‹œ์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์„ ์ฃผ๋ชฉํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ์ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ์ž์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๊ณ„์†ํ•˜๋Š” ๋Œ€์‹  ๋ด‡ ์‘๋‹ต์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•˜์„ธ์š”, ์ฑ„ํŒ… ๋ชจ๋ธ์€ ์—ฌ์ „ํžˆ ์–ธ์–ด ๋ชจ๋ธ์ผ ๋ฟ์ด๋ฉฐ, ๊ทธ๋“ค์—๊ฒŒ ์ฑ„ํŒ…์€ ํŠน๋ณ„ํ•œ ์ข…๋ฅ˜์˜ ํ…์ŠคํŠธ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค! ์ ์ ˆํ•œ ์ œ์–ด ํ† ํฐ์œผ๋กœ ์•ˆ๋‚ดํ•ด์•ผ ์ฑ„ํŒ… ๋ชจ๋ธ์ด ๋ฌด์—‡์„ ํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋“  ๋ชจ๋ธ์ด ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. BlenderBot๊ณผ LLaMA ๊ฐ™์€ ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋ด‡ ์‘๋‹ต ์ „์— ํŠน๋ณ„ํ•œ ํ† ํฐ์ด ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ `add_generation_prompt` ์ธ์ˆ˜๋Š” ํšจ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. `add_generation_prompt`์˜ ์ •ํ™•ํ•œ ํšจ๊ณผ๋Š” ์‚ฌ์šฉ ์ค‘์ธ ํ…œํ”Œ๋ฆฟ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. + + + +## ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”?[[can-i-use-chat-templates-in-training]] + +๋„ค! ์ด ๋ฐฉ๋ฒ•์€ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ๋ชจ๋ธ์ด ํ›ˆ๋ จ ์ค‘์— ๋ณด๋Š” ํ† ํฐ๊ณผ ์ผ์น˜ํ•˜๋„๋ก ํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋กœ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ๋‹ค๋ฅธ ์–ธ์–ด ๋ชจ๋ธ ํ›ˆ๋ จ ์ž‘์—…๊ณผ ๊ฐ™์ด ๊ณ„์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ `add_generation_prompt=False`๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต์„ ํ”„๋กฌํ”„ํŠธํ•˜๋Š” ์ถ”๊ฐ€ ํ† ํฐ์€ ํ›ˆ๋ จ ์ค‘์—๋Š” ๋„์›€์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import AutoTokenizer +from datasets import Dataset + +tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta") + +chat1 = [ + {"role": "user", "content": "Which is bigger, the moon or the sun?"}, + {"role": "assistant", "content": "The sun."} +] +chat2 = [ + {"role": "user", "content": "Which is bigger, a virus or a bacterium?"}, + {"role": "assistant", "content": "A bacterium."} +] + +dataset = Dataset.from_dict({"chat": [chat1, chat2]}) +dataset = dataset.map(lambda x: {"formatted_chat": tokenizer.apply_chat_template(x["chat"], tokenize=False, add_generation_prompt=False)}) +print(dataset['formatted_chat'][0]) +``` +๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +```text +<|user|> +Which is bigger, the moon or the sun? +<|assistant|> +The sun. +``` + +์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ผ๋ฐ˜์ ์ธ ์–ธ์–ด ๋ชจ๋ธ ์ž‘์—…๊ณผ ๊ฐ™์ด `formatted_chat` ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ์„ ๊ณ„์†ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + + +`apply_chat_template(tokenize=False)`๋กœ ํ…์ŠคํŠธ๋ฅผ ํ˜•์‹ํ™”ํ•œ ๋‹ค์Œ ๋ณ„๋„์˜ ๋‹จ๊ณ„์—์„œ ํ† ํฐํ™”ํ•˜๋Š” ๊ฒฝ์šฐ, `add_special_tokens=False` ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `apply_chat_template(tokenize=True)`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค! +๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ๋ถ€ ํ† ํฌ๋‚˜์ด์ €๋Š” ํ† ํฐํ™”ํ•  ๋•Œ `` ๋ฐ ``์™€ ๊ฐ™์€ ํŠน๋ณ„ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ํ•ญ์ƒ ํ•„์š”ํ•œ ๋ชจ๋“  ํŠน๋ณ„ ํ† ํฐ์„ ํฌํ•จํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ๊ธฐ๋ณธ `add_special_tokens=True`๋กœ ์ถ”๊ฐ€์ ์ธ ํŠน๋ณ„ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ž˜๋ชป๋˜๊ฑฐ๋‚˜ ์ค‘๋ณต๋˜๋Š” ํŠน๋ณ„ ํ† ํฐ์„ ์ƒ์„ฑํ•˜์—ฌ ๋ชจ๋ธ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์— ์ถ”๊ฐ€ ์ž…๋ ฅ ์‚ฌ์šฉ[[advanced-extra-inputs-to-chat-templates]] + +`apply_chat_template`๊ฐ€ ํ•„์š”ํ•œ ์œ ์ผํ•œ ์ธ์ˆ˜๋Š” `messages`์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ `apply_chat_template`์— ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์œ ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์ˆ˜์˜ ์ด๋ฆ„์ด๋‚˜ ํ˜•์‹์—๋Š” ์ œํ•œ์ด ์—†์–ด ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋ ‡๊ธด ํ•˜์ง€๋งŒ, ์ด๋Ÿฌํ•œ ์ถ”๊ฐ€ ์ธ์ˆ˜์˜ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋กœ 'ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์œ„ํ•œ ๋„๊ตฌ'๋‚˜ '๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ฌธ์„œ'๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ธ์ˆ˜์˜ ์ด๋ฆ„๊ณผ ํ˜•์‹์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ๊ถŒ์žฅ ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์•„๋ž˜ ์„น์…˜์— ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ ์ž‘์„ฑ์ž์—๊ฒŒ ๋„๊ตฌ ํ˜ธ์ถœ ์ฝ”๋“œ๋ฅผ ๋ชจ๋ธ ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „์†กํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ด ํ˜•์‹๊ณผ ํ˜ธํ™˜๋˜๋„๋ก ๋งŒ๋“ค ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +## ๊ณ ๊ธ‰: ๋„๊ตฌ ์‚ฌ์šฉ / ํ•จ์ˆ˜ ํ˜ธ์ถœ[[advanced-tool-use--function-calling]] + +"๋„๊ตฌ ์‚ฌ์šฉ" LLM์€ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๊ธฐ ์ „์— ์™ธ๋ถ€ ๋„๊ตฌ๋กœ์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์— ๋„๊ตฌ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ๋‹จ์ˆœํžˆ ํ•จ์ˆ˜ ๋ชฉ๋ก์„ `tools` ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +import datetime + +def current_time(): + """ํ˜„์žฌ ํ˜„์ง€ ์‹œ๊ฐ„์„ ๋ฌธ์ž์—ด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.""" + return str(datetime.now()) + +def multiply(a: float, b: float): + """ + ๋‘ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜ + + ์ธ์ˆ˜: + a: ๊ณฑํ•  ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž + b: ๊ณฑํ•  ๋‘ ๋ฒˆ์งธ ์ˆซ์ž + """ + return a * b + +tools = [current_time, multiply] + +model_input = tokenizer.apply_chat_template( + messages, + tools=tools +) +``` + +์ด๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋ ค๋ฉด ํ•จ์ˆ˜๋ฅผ ์œ„ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ๋„๊ตฌ๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ๋ฌธ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋‹ค์Œ ๊ทœ์น™์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- ํ•จ์ˆ˜๋Š” ์„ค๋ช…์ ์ธ ์ด๋ฆ„์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋“  ์ธ์ˆ˜์—๋Š” ํƒ€์ž… ํžŒํŠธ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•จ์ˆ˜์—๋Š” ํ‘œ์ค€ Google ์Šคํƒ€์ผ์˜ ๋„ํฌ์ŠคํŠธ๋ง์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(์ฆ‰, ์ดˆ๊ธฐ ํ•จ์ˆ˜ ์„ค๋ช… ๋‹ค์Œ์— ์ธ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” `Args:` ๋ธ”๋ก์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). +- `Args:` ๋ธ”๋ก์—๋Š” ํƒ€์ž…์„ ํฌํ•จํ•˜์ง€ ๋งˆ์„ธ์š”. ์ฆ‰, `a (int): The first number to multiply` ๋Œ€์‹  `a: The first number to multiply`๋ผ๊ณ  ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž… ํžŒํŠธ๋Š” ํ•จ์ˆ˜ ํ—ค๋”์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•จ์ˆ˜์—๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…๊ณผ ๋„ํฌ์ŠคํŠธ๋ง์— `Returns:` ๋ธ”๋ก์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์€ ์ด๋ฅผ ๋ฌด์‹œํ•˜๋ฏ€๋กœ ์ด๋Š” ์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. + + +### ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ธฐ[[passing-tool-results-to-the-model]] + +์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋ชจ๋ธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋ฅผ ๋‚˜์—ดํ•˜๋Š” ๋ฐ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +1. ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ํŒŒ์‹ฑํ•˜์—ฌ ๋„๊ตฌ ์ด๋ฆ„๊ณผ ์ธ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. +2. ๋ชจ๋ธ์˜ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +3. ํ•ด๋‹น ์ธ์ˆ˜์— ๋Œ€์‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. +4. ๊ฒฐ๊ณผ๋ฅผ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +### ๋„๊ตฌ ์‚ฌ์šฉ ์˜ˆ์ œ[[a-complete-tool-use-example]] + +๋„๊ตฌ ์‚ฌ์šฉ ์˜ˆ์ œ๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ ์ค‘์—์„œ ์„ฑ๋Šฅ์ด ๊ฐ€์žฅ ์šฐ์ˆ˜ํ•œ 8B `Hermes-2-Pro` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด, ๋” ํฐ ๋ชจ๋ธ์ธ [Command-R](https://huggingface.co/CohereForAI/c4ai-command-r-v01) ๋˜๋Š” [Mixtral-8x22B](https://huggingface.co/mistralai/Mixtral-8x22B-Instruct-v0.1)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋ชจ๋ธ ๋ชจ๋‘ ๋„๊ตฌ ์‚ฌ์šฉ์„ ์ง€์›ํ•˜๋ฉฐ ๋” ๊ฐ•๋ ฅํ•œ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +๋จผ์ € ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer + +checkpoint = "NousResearch/Hermes-2-Pro-Llama-3-8B" + +tokenizer = AutoTokenizer.from_pretrained(checkpoint, revision="pr/13") +model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +``` + +๋‹ค์Œ์œผ๋กœ, ๋„๊ตฌ ๋ชฉ๋ก์„ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +def get_current_temperature(location: str, unit: str) -> float: + """ + ํŠน์ • ์œ„์น˜์˜ ํ˜„์žฌ ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + + ์ธ์ˆ˜: + ์œ„์น˜: ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์˜ฌ ์œ„์น˜, "๋„์‹œ, ๊ตญ๊ฐ€" ํ˜•์‹ + ๋‹จ์œ„: ์˜จ๋„ ๋‹จ์œ„ (์„ ํƒ์ง€: ["celsius", "fahrenheit"]) + ๋ฐ˜ํ™˜๊ฐ’: + ์ง€์ •๋œ ์œ„์น˜์˜ ํ˜„์žฌ ์˜จ๋„๋ฅผ ์ง€์ •๋œ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜, float ํ˜•์‹. + """ + return 22. # ์ด ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ์˜จ๋„๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! + +def get_current_wind_speed(location: str) -> float: + """ + ์ฃผ์–ด์ง„ ์œ„์น˜์˜ ํ˜„์žฌ ํ’์†์„ km/h ๋‹จ์œ„๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + + ์ธ์ˆ˜: + ์œ„์น˜(location): ํ’์†์„ ๊ฐ€์ ธ์˜ฌ ์œ„์น˜, "๋„์‹œ, ๊ตญ๊ฐ€" ํ˜•์‹ + ๋ฐ˜ํ™˜๊ฐ’: + ์ฃผ์–ด์ง„ ์œ„์น˜์˜ ํ˜„์žฌ ํ’์†์„ km/h ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜, float ํ˜•์‹. + """ + return 6. # ์ด ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ํ’์†์„ ๊ฐ€์ ธ์™€์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! + +tools = [get_current_temperature, get_current_wind_speed] +``` + +์ด์ œ ๋ด‡์„ ์œ„ํ•œ ๋Œ€ํ™”๋ฅผ ์„ค์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +messages = [ + {"role": "system", "content": "You are a bot that responds to weather queries. You should reply with the unit used in the queried location."}, + {"role": "user", "content": "Hey, what's the temperature in Paris right now?"} +] +``` + +์ด์ œ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ ์šฉํ•˜๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +inputs = tokenizer.apply_chat_template(messages, chat_template="tool_use", tools=tools, add_generation_prompt=True, return_dict=True, return_tensors="pt") +inputs = {k: v.to(model.device) for k, v in inputs.items()} +out = model.generate(**inputs, max_new_tokens=128) +print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):])) +``` + +๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```text + +{"arguments": {"location": "Paris, France", "unit": "celsius"}, "name": "get_current_temperature"} +<|im_end|> +``` + +๋ชจ๋ธ์ด ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์œ ํšจํ•œ ์ธ์ˆ˜๋กœ ์ˆ˜ํ–‰ํ–ˆ์œผ๋ฉฐ, ํ•จ์ˆ˜ ๋„ํฌ์ŠคํŠธ๋ง์— ์š”์ฒญ๋œ ํ˜•์‹์œผ๋กœ ํ˜ธ์ถœํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์šฐ๋ฆฌ๊ฐ€ ํ”„๋ž‘์Šค์˜ ํŒŒ๋ฆฌ๋ฅผ ์ง€์นญํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ–ˆ๊ณ , ํ”„๋ž‘์Šค๊ฐ€ SI ๋‹จ์œ„์˜ ๋ณธ๊ณ ์žฅ์ž„์„ ๊ธฐ์–ตํ•˜์—ฌ ์˜จ๋„๋ฅผ ์„ญ์”จ๋กœ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ์˜ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž„์˜์˜ `tool_call_id`๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ID๋Š” ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์—ฌ๋Ÿฌ ๋„๊ตฌ ํ˜ธ์ถœ์„ ํ•œ ๋ฒˆ์— ๋ฐœํ–‰ํ•˜๊ณ  ๊ฐ ์‘๋‹ต์ด ์–ด๋А ํ˜ธ์ถœ์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์ด ID๋Š” ๋Œ€ํ™” ๋‚ด์—์„œ ๊ณ ์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +tool_call_id = "vAHdf3" # ์ž„์˜์˜ ID, ๊ฐ ๋„๊ตฌ ํ˜ธ์ถœ๋งˆ๋‹ค ๊ณ ์œ ํ•ด์•ผ ํ•จ +tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France", "unit": "celsius"}} +messages.append({"role": "assistant", "tool_calls": [{"id": tool_call_id, "type": "function", "function": tool_call}]}) +``` + + +์ด์ œ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋Œ€ํ™”์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ํ•ญ์ƒ 22.0์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋”๋ฏธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋ฅผ ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•œ ๋ฒˆ, `tool_call_id`๋Š” ๋„๊ตฌ ํ˜ธ์ถœ์— ์‚ฌ์šฉํ–ˆ๋˜ ID์™€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +messages.append({"role": "tool", "tool_call_id": tool_call_id, "name": "get_current_temperature", "content": "22.0"}) +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ, ์–ด์‹œ์Šคํ„ดํŠธ๊ฐ€ ํ•จ์ˆ˜ ์ถœ๋ ฅ์„ ์ฝ๊ณ  ์‚ฌ์šฉ์ž์™€ ๊ณ„์† ๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +inputs = tokenizer.apply_chat_template(messages, chat_template="tool_use", tools=tools, add_generation_prompt=True, return_dict=True, return_tensors="pt") +inputs = {k: v.to(model.device) for k, v in inputs.items()} +out = model.generate(**inputs, max_new_tokens=128) +print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):])) +``` + +๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```text +The current temperature in Paris, France is 22.0 ยฐ Celsius.<|im_end|> +``` + +์ด๊ฒƒ์€ ๋”๋ฏธ ๋„๊ตฌ์™€ ๋‹จ์ผ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ๋ฐ๋ชจ์˜€์ง€๋งŒ, ๋™์ผํ•œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์‹ค์ œ ๋„๊ตฌ์™€ ๋” ๊ธด ๋Œ€ํ™”๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์‹ค์‹œ๊ฐ„ ์ •๋ณด, ๊ณ„์‚ฐ ๋„๊ตฌ ๋˜๋Š” ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์—ฌ ๋Œ€ํ™”ํ˜• ์—์ด์ „ํŠธ์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +์œ„์—์„œ ๋ณด์—ฌ์ค€ ๋„๊ตฌ ํ˜ธ์ถœ ๊ธฐ๋Šฅ์€ ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋„๊ตฌ ํ˜ธ์ถœ ID๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ผ๋ถ€๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ์™€ ๋„๊ตฌ ํ˜ธ์ถœ์„ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋งค์นญํ•˜๋ฉฐ, ํ˜ผ๋™์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋„๊ตฌ ํ˜ธ์ถœ๋งŒ ๋ฐœํ–‰ํ•˜๋Š” ๋ชจ๋ธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋งŽ์€ ๋ชจ๋ธ๊ณผ ํ˜ธํ™˜๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, ์—ฌ๊ธฐ์— ๋ณด์—ฌ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๊ตฌ์„ฑํ•˜๊ณ , ๋ชจ๋ธ์ด ๋ฐœํ–‰ํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋ธ์˜ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ๋‚˜๋จธ์ง€ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + + +### ๋„๊ตฌ ์Šคํ‚ค๋งˆ ์ดํ•ดํ•˜๊ธฐ[[understanding-tool-schemas]] + +`apply_chat_template`์˜ `tools` ์ธ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ๊ฐ ํ•จ์ˆ˜๋Š” [JSON ์Šคํ‚ค๋งˆ](https://json-schema.org/learn/getting-started-step-by-step)๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์Šคํ‚ค๋งˆ๋Š” ๋ชจ๋ธ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์€ ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ง์ ‘ ๋ณด์ง€ ์•Š์œผ๋ฉฐ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋„๊ตฌ ์‚ฌ์šฉ ๋ชจ๋ธ์ด ๊ด€์‹ฌ์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ **์ •์˜**์™€ **์ธ์ˆ˜**์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๊ด€์‹ฌ์ด ์žˆ์„ ๋ฟ, ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ์ฝ๊ณ  ๋ชจ๋ธ์ด ๋„๊ตฌ ์‚ฌ์šฉ์„ ์š”์ฒญํ–ˆ๋Š”์ง€ ๊ฐ์ง€ํ•˜์—ฌ, ์ธ์ˆ˜๋ฅผ ๋„๊ตฌ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ  ์ฑ„ํŒ…์—์„œ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ชซ์ž…๋‹ˆ๋‹ค. + +์œ„์˜ ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅธ๋‹ค๋ฉด, ํ…œํ”Œ๋ฆฟ์— ์ „๋‹ฌํ•  JSON ์Šคํ‚ค๋งˆ ์ƒ์„ฑ์„ ์ž๋™ํ™”ํ•˜๊ณ  ๋ณด์ด์ง€ ์•Š๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜์„ ๋” ์ œ์–ดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ˆ˜๋™์œผ๋กœ ๋ณ€ํ™˜์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ˆ˜๋™ ์Šคํ‚ค๋งˆ ๋ณ€ํ™˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. + +```python +from transformers.utils import get_json_schema + +def multiply(a: float, b: float): + """ + ๋‘ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜ + + ์ธ์ˆ˜: + a: ๊ณฑํ•  ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž + b: ๊ณฑํ•  ๋‘ ๋ฒˆ์งธ ์ˆซ์ž + """ + return a * b + +schema = get_json_schema(multiply) +print(schema) +``` + +์ด ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```json +{ + "type": "function", + "function": { + "name": "multiply", + "description": "A function that multiplies two numbers", + "parameters": { + "type": "object", + "properties": { + "a": { + "type": "number", + "description": "The first number to multiply" + }, + "b": { + "type": "number", + "description": "The second number to multiply" + } + }, + "required": ["a", "b"] + } + } +} +``` + +์›ํ•œ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ํŽธ์ง‘ํ•˜๊ฑฐ๋‚˜ `get_json_schema`๋ฅผ ์ „ํ˜€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. JSON ์Šคํ‚ค๋งˆ๋Š” `apply_chat_template`์˜ `tools` ์ธ์ˆ˜์— ์ง์ ‘ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋” ๋ณต์žกํ•œ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ •๋ฐ€ํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์Šคํ‚ค๋งˆ๊ฐ€ ๋ณต์žกํ• ์ˆ˜๋ก ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ํ˜ผ๋ž€์„ ๊ฒช์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค! ๊ฐ€๋Šฅํ•œ ํ•œ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜ ์„œ๋ช…์„ ์œ ์ง€ํ•˜๊ณ , ์ธ์ˆ˜(ํŠนํžˆ ๋ณต์žกํ•˜๊ณ  ์ค‘์ฒฉ๋œ ์ธ์ˆ˜)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ ์ง์ ‘ ์Šคํ‚ค๋งˆ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ `apply_chat_template`์— ์ „๋‹ฌํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +# ์ธ์ˆ˜๋ฅผ ๋ฐ›์ง€ ์•Š๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜ +current_time = { + "type": "function", + "function": { + "name": "current_time", + "description": "Get the current local time as a string.", + "parameters": { + 'type': 'object', + 'properties': {} + } + } +} + +# ๋‘ ๊ฐœ์˜ ์ˆซ์ž ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๋” ์™„์ „ํ•œ ํ•จ์ˆ˜ +multiply = { + 'type': 'function', + 'function': { + 'name': 'multiply', + 'description': 'A function that multiplies two numbers', + 'parameters': { + 'type': 'object', + 'properties': { + 'a': { + 'type': 'number', + 'description': 'The first number to multiply' + }, + 'b': { + 'type': 'number', 'description': 'The second number to multiply' + } + }, + 'required': ['a', 'b'] + } + } +} + +model_input = tokenizer.apply_chat_template( + messages, + tools = [current_time, multiply] +) +``` + +## ๊ณ ๊ธ‰: ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ[[advanced-retrieval-augmented-generation]] + +"๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ" ๋˜๋Š” "RAG" LLM์€ ์ฟผ๋ฆฌ์— ์‘๋‹ตํ•˜๊ธฐ ์ „์— ๋ฌธ์„œ์˜ ์ฝ”ํผ์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์€ ์ œํ•œ๋œ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ ์ด์ƒ์œผ๋กœ ์ง€์‹ ๊ธฐ๋ฐ˜์„ ํฌ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. RAG ๋ชจ๋ธ์— ๋Œ€ํ•œ ์šฐ๋ฆฌ์˜ ๊ถŒ์žฅ ์‚ฌํ•ญ์€ ํ…œํ”Œ๋ฆฟ์ด `documents` ์ธ์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ธ์ˆ˜๋Š” ๊ฐ "๋ฌธ์„œ"๊ฐ€ `title`๊ณผ `contents` ํ‚ค๋ฅผ ๊ฐ€์ง€๋Š” ๋‹จ์ผ dict์ธ ๋ฌธ์„œ ๋ชฉ๋ก์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜•์‹์€ ๋„๊ตฌ์— ์‚ฌ์šฉ๋˜๋Š” JSON ์Šคํ‚ค๋งˆ๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๋ฏ€๋กœ ๋ณ„๋„์˜ ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + + +๋‹ค์Œ์€ RAG ํ…œํ”Œ๋ฆฟ์ด ์ž‘๋™ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค: + + +```python +document1 = { + "title": "The Moon: Our Age-Old Foe", + "contents": "Man has always dreamed of destroying the moon. In this essay, I shall..." +} + +document2 = { + "title": "The Sun: Our Age-Old Friend", + "contents": "Although often underappreciated, the sun provides several notable benefits..." +} + +model_input = tokenizer.apply_chat_template( + messages, + documents=[document1, document2] +) +``` + +## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋‚˜์š”?[[advanced-how-do-chat-templates-work]] + +๋ชจ๋ธ์˜ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์€ `tokenizer.chat_template` ์†์„ฑ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์ด ์„ค์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ชจ๋ธ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ์ด ๋Œ€์‹  ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. `BlenderBot`์˜ ํ…œํ”Œ๋ฆฟ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python + +>>> from transformers import AutoTokenizer +>>> tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill") + +>>> tokenizer.chat_template +"{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ ' ' }}{% endif %}{% endfor %}{{ eos_token }}" +``` + +์•ฝ๊ฐ„ ๋ณต์žกํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์ถ”๊ฐ€ํ•˜๋Š” ์ค„๋ฐ”๊ฟˆ๊ณผ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ํ…œํ”Œ๋ฆฟ ์ถœ๋ ฅ์— ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” [๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š”](#trimming-whitespace) ํŒ์ž…๋‹ˆ๋‹ค: + +``` +{%- for message in messages %} + {%- if message['role'] == 'user' %} + {{- ' ' }} + {%- endif %} + {{- message['content'] }} + {%- if not loop.last %} + {{- ' ' }} + {%- endif %} +{%- endfor %} +{{- eos_token }} +``` + +๋งŒ์•ฝ ์ด์™€ ๊ฐ™์€ ํ˜•์‹์„ ์ฒ˜์Œ ๋ณธ๋‹ค๋ฉด, ์ด๊ฒƒ์€ [Jinja ํ…œํ”Œ๋ฆฟ](https://jinja.palletsprojects.com/en/3.1.x/templates/)์ž…๋‹ˆ๋‹ค. +Jinja๋Š” ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํ…œํ”Œ๋ฆฟ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๋งŽ์€ ๋ฉด์—์„œ ์ฝ”๋“œ์™€ ๊ตฌ๋ฌธ์ด ํŒŒ์ด์ฌ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ด ํ…œํ”Œ๋ฆฟ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค: + + +```python +for idx, message in enumerate(messages): + if message['role'] == 'user': + print(' ') + print(message['content']) + if not idx == len(messages) - 1: # Check for the last message in the conversation + print(' ') +print(eos_token) +``` + +์ด ํ…œํ”Œ๋ฆฟ์€ ์„ธ ๊ฐ€์ง€ ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค: +1. ๊ฐ ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•ด, ๋ฉ”์‹œ์ง€๊ฐ€ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€์ธ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +2. ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +3. ๋ฉ”์‹œ์ง€๊ฐ€ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ๋‘ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€ ํ›„์—๋Š” EOS ํ† ํฐ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. + +์ด๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ํ…œํ”Œ๋ฆฟ์ž…๋‹ˆ๋‹ค. ์ œ์–ด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ดํ›„ ๋Œ€ํ™”์—์„œ ๋ชจ๋ธ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•ด์•ผ ํ•˜๋Š”์ง€ ์ง€์‹œํ•˜๋Š” "์‹œ์Šคํ…œ" ๋ฉ”์‹œ์ง€๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Jinja๋Š” ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค! LLaMA๊ฐ€ ์ž…๋ ฅ์„ ํ˜•์‹ํ™”ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•œ ํ˜•์‹์˜ Jinja ํ…œํ”Œ๋ฆฟ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค(์‹ค์ œ LLaMA ํ…œํ”Œ๋ฆฟ์€ ๊ธฐ๋ณธ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ์™€ ์ผ๋ฐ˜์ ์ธ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค - ์‹ค์ œ ์ฝ”๋“œ์—์„œ๋Š” ์ด ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”!). + +``` +{%- for message in messages %} + {%- if message['role'] == 'user' %} + {{- bos_token + '[INST] ' + message['content'] + ' [/INST]' }} + {%- elif message['role'] == 'system' %} + {{- '<>\\n' + message['content'] + '\\n<>\\n\\n' }} + {%- elif message['role'] == 'assistant' %} + {{- ' ' + message['content'] + ' ' + eos_token }} + {%- endif %} +{%- endfor %} +``` + +์ด ํ…œํ”Œ๋ฆฟ์„ ์ž ์‹œ ์‚ดํŽด๋ณด๋ฉด ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ๊ฐ ๋ฉ”์‹œ์ง€์˜ "role"์— ๋”ฐ๋ผ ํŠน์ • ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ˆ„๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒˆ๋Š”์ง€ ๋ชจ๋ธ์—๊ฒŒ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ์‚ฌ์šฉ์ž, ์–ด์‹œ์Šคํ„ดํŠธ ๋ฐ ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€๋Š” ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ํ† ํฐ์œผ๋กœ ๋ž˜ํ•‘๋˜์–ด ๋ชจ๋ธ์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๊ณ ๊ธ‰: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ ์ถ”๊ฐ€ ๋ฐ ํŽธ์ง‘[[advanced-adding-and-editing-chat-templates]] + +### ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‚˜์š”?[[how-do-i-create-a-chat-template]] + +๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Jinja ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๊ณ  `tokenizer.chat_template`์— ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋ธ์˜ ๊ธฐ์กด ํ…œํ”Œ๋ฆฟ์„ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ํ•„์š”์— ๋งž๊ฒŒ ํŽธ์ง‘ํ•˜๋Š” ๊ฒƒ์ด ๋” ์‰ฌ์šธ ๊ฒƒ ์ž…๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ LLaMA ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ ธ์™€ ์–ด์‹œ์Šคํ„ดํŠธ ๋ฉ”์‹œ์ง€์— "[ASST]" ๋ฐ "[/ASST]"๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +``` +{%- for message in messages %} + {%- if message['role'] == 'user' %} + {{- bos_token + '[INST] ' + message['content'].strip() + ' [/INST]' }} + {%- elif message['role'] == 'system' %} + {{- '<>\\n' + message['content'].strip() + '\\n<>\\n\\n' }} + {%- elif message['role'] == 'assistant' %} + {{- '[ASST] ' + message['content'] + ' [/ASST]' + eos_token }} + {%- endif %} +{%- endfor %} +``` + +์ด์ œ `tokenizer.chat_template` ์†์„ฑ์„ ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ์— [`~PreTrainedTokenizer.apply_chat_template`]๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•œ ํ…œํ”Œ๋ฆฟ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค! ์ด ์†์„ฑ์€ `tokenizer_config.json` ํŒŒ์ผ์— ์ €์žฅ๋˜๋ฏ€๋กœ, [`~utils.PushToHubMixin.push_to_hub`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ํ…œํ”Œ๋ฆฟ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜๊ณ  ๋ชจ๋“  ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ์— ๋งž๋Š” ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +```python +template = tokenizer.chat_template +template = template.replace("SYS", "SYSTEM") # ์‹œ์Šคํ…œ ํ† ํฐ ๋ณ€๊ฒฝ +tokenizer.chat_template = template # ์ƒˆ ํ…œํ”Œ๋ฆฟ ์„ค์ • +tokenizer.push_to_hub("model_name") # ์ƒˆ ํ…œํ”Œ๋ฆฟ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ! +``` + +์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” [`~PreTrainedTokenizer.apply_chat_template`] ๋ฉ”์†Œ๋“œ๋Š” [`TextGenerationPipeline`] ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, ์˜ฌ๋ฐ”๋ฅธ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•˜๋ฉด ๋ชจ๋ธ์ด ์ž๋™์œผ๋กœ [`TextGenerationPipeline`]๊ณผ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. + + +๋ชจ๋ธ์„ ์ฑ„ํŒ… ์šฉ๋„๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ์ƒˆ ์ฑ„ํŒ… ์ œ์–ด ํ† ํฐ์„ ํ† ํฌ๋‚˜์ด์ €์— ํŠน๋ณ„ ํ† ํฐ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํŠน๋ณ„ ํ† ํฐ์€ ์ ˆ๋Œ€๋กœ ๋ถ„ํ• ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์ œ์–ด ํ† ํฐ์ด ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ํ† ํฐํ™”๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ…œํ”Œ๋ฆฟ์—์„œ ์–ด์‹œ์Šคํ„ดํŠธ ์ƒ์„ฑ์˜ ๋์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ† ํฐ์œผ๋กœ ํ† ํฌ๋‚˜์ด์ €์˜ `eos_token` ์†์„ฑ์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ…์ŠคํŠธ ์ƒ์„ฑ ๋„๊ตฌ๊ฐ€ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์–ธ์ œ ์ค‘์ง€ํ•ด์•ผ ํ• ์ง€ ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +### ์™œ ์ผ๋ถ€ ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‚˜์š”?[[why-do-some-models-have-multiple-templates]] + +์ผ๋ถ€ ๋ชจ๋ธ์€ ๋‹ค๋ฅธ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ผ๋ฐ˜ ์ฑ„ํŒ…์„ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ๊ณผ ๋„๊ตฌ ์‚ฌ์šฉ ๋˜๋Š” ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ์— ๋Œ€ํ•œ ํ…œํ”Œ๋ฆฟ์„ ๋ณ„๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ `tokenizer.chat_template`๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์•ฝ๊ฐ„์˜ ํ˜ผ๋ž€์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ€๋Šฅํ•œ ํ•œ ๋ชจ๋“  ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•ด ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. `if tools is defined`์™€ ๊ฐ™์€ Jinja ๋ฌธ์žฅ๊ณผ `{% macro %}` ์ •์˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ฝ”๋“œ ๊ฒฝ๋กœ๋ฅผ ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์— ์‰ฝ๊ฒŒ ๋ž˜ํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ† ํฌ๋‚˜์ด์ €์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…œํ”Œ๋ฆฟ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, `tokenizer.chat_template`๋Š” ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์ด ํ‚ค์ธ `๋”•์…”๋„ˆ๋ฆฌ`์ž…๋‹ˆ๋‹ค. `apply_chat_template` ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค: ์ผ๋ฐ˜์ ์œผ๋กœ `default`๋ผ๋Š” ํ…œํ”Œ๋ฆฟ์„ ์ฐพ๊ณ , ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ `tools` ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ `tool_use`๋ผ๋Š” ํ…œํ”Œ๋ฆฟ์ด ์กด์žฌํ•˜๋ฉด ๋Œ€์‹  ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ด๋ฆ„์˜ ํ…œํ”Œ๋ฆฟ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด `apply_chat_template()`์˜ `chat_template` ์ธ์ˆ˜์— ์›ํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์ด๋ฆ„์„ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž์—๊ฒŒ ์•ฝ๊ฐ„์˜ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ…œํ”Œ๋ฆฟ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€๋Šฅํ•œ ํ•œ ๋‹จ์ผ ํ…œํ”Œ๋ฆฟ์— ๋ชจ๋“  ๊ฒƒ์„ ๋„ฃ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค! + +### ์–ด๋–ค ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‚˜์š”?[[what-template-should-i-use]] + +์ด๋ฏธ ์ฑ„ํŒ…์šฉ์œผ๋กœ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์— ํ…œํ”Œ๋ฆฟ์„ ์„ค์ •ํ•  ๋•Œ๋Š” ํ…œํ”Œ๋ฆฟ์ด ํ›ˆ๋ จ ์ค‘ ๋ชจ๋ธ์ด ๋ณธ ๋ฉ”์‹œ์ง€ ํ˜•์‹๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ๊ฒฝํ—˜ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์„ ์ถ”๊ฐ€๋กœ ํ›ˆ๋ จํ•  ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ฑ„ํŒ… ํ† ํฐ์„ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ตœ์ƒ์˜ ์„ฑ๋Šฅ์„ ์–ป๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ† ํฐํ™”์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์ค‘์— ์‚ฌ์šฉ๋œ ํ† ํฐํ™”๋ฅผ ์ •ํ™•ํžˆ ์ผ์น˜์‹œํ‚ฌ ๋•Œ ์ถ”๋ก ์ด๋‚˜ ๋ฏธ์„ธ ์กฐ์ •์—์„œ ์ตœ๊ณ ์˜ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฐ˜๋ฉด์— ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๊ฑฐ๋‚˜ ์ฑ„ํŒ…์šฉ์œผ๋กœ ๊ธฐ๋ณธ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ์ ์ ˆํ•œ ํ…œํ”Œ๋ฆฟ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์ž์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. LLM์€ ๋‹ค์–‘ํ•œ ์ž…๋ ฅ ํ˜•์‹์„ ์ฒ˜๋ฆฌํ•  ๋งŒํผ ์ถฉ๋ถ„ํžˆ ๋˜‘๋˜‘ํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ธฐ ์žˆ๋Š” ์„ ํƒ ์ค‘ ํ•˜๋‚˜๋Š” `ChatML` ํ˜•์‹์ด๋ฉฐ, ์ด๋Š” ๋งŽ์€ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +``` +{%- for message in messages %} + {{- '<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n' }} +{%- endfor %} +``` + +์ด ํ…œํ”Œ๋ฆฟ์ด ๋งˆ์Œ์— ๋“ ๋‹ค๋ฉด, ์ฝ”๋“œ์— ๋ฐ”๋กœ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ์ค„ ๋ฒ„์ „์„ ์ œ๊ณตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ํ•œ ์ค„ ๋ฒ„์ „์€ [์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ](#what-are-generation-prompts)์— ๋Œ€ํ•œ ํŽธ๋ฆฌํ•œ ์ง€์›๋„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€๋งŒ, BOS๋‚˜ EOS ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”! ๋ชจ๋ธ์ด ํ•ด๋‹น ํ† ํฐ์„ ๊ธฐ๋Œ€ํ•˜๋”๋ผ๋„, `apply_chat_template`์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ…์ŠคํŠธ๋Š” `add_special_tokens=False`์— ์˜ํ•ด ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ…œํ”Œ๋ฆฟ๊ณผ `add_special_tokens` ๋…ผ๋ฆฌ ๊ฐ„์˜ ์ž ์žฌ์ ์ธ ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ํŠน๋ณ„ ํ† ํฐ์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฒฝ์šฐ, ํ…œํ”Œ๋ฆฟ์— ์ง์ ‘ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! + + +```python +tokenizer.chat_template = "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}" +``` + +์ด ํ…œํ”Œ๋ฆฟ์€ ๊ฐ ๋ฉ”์‹œ์ง€๋ฅผ `<|im_start|>` ์™€ `<|im_end|>`ํ† ํฐ์œผ๋กœ ๊ฐ์‹ธ๊ณ , ์—ญํ• ์„ ๋ฌธ์ž์—ด๋กœ ์ž‘์„ฑํ•˜์—ฌ ํ›ˆ๋ จ ์‹œ ์‚ฌ์šฉํ•˜๋Š” ์—ญํ• ์— ๋Œ€ํ•œ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + + +```text +<|im_start|>system +You are a helpful chatbot that will do its best not to say anything so stupid that people tweet about it.<|im_end|> +<|im_start|>user +How are you?<|im_end|> +<|im_start|>assistant +I'm doing great!<|im_end|> +``` + +"์‚ฌ์šฉ์ž", "์‹œ์Šคํ…œ" ๋ฐ "์–ด์‹œ์Šคํ„ดํŠธ" ์—ญํ• ์€ ์ฑ„ํŒ…์˜ ํ‘œ์ค€์ด๋ฉฐ, ๊ฐ€๋Šฅํ•  ๋•Œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋ชจ๋ธ์ด [`TextGenerationPipeline`]๊ณผ ์ž˜ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ์—ญํ• ์—๋งŒ ๊ตญํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ์€ ๋งค์šฐ ์œ ์—ฐํ•˜๋ฉฐ, ์–ด๋–ค ๋ฌธ์ž์—ด์ด๋“  ์—ญํ• ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +### ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค! ์–ด๋–ป๊ฒŒ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๋‚˜์š”?[[i-want-to-add-some-chat-templates-how-should-i-get-started]] + +์ฑ„ํŒ… ๋ชจ๋ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ชจ๋ธ์˜ `tokenizer.chat_template` ์†์„ฑ์„ ์„ค์ •ํ•˜๊ณ  [`~PreTrainedTokenizer.apply_chat_template`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ ๋‹ค์Œ ์—…๋ฐ์ดํŠธ๋œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ํ—ˆ๋ธŒ์— ํ‘ธ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ ์†Œ์œ ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋„ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋นˆ ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ด๋‚˜ ์—ฌ์ „ํžˆ ๊ธฐ๋ณธ ํด๋ž˜์Šค ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, [ํ’€ ๋ฆฌํ€˜์ŠคํŠธ](https://huggingface.co/docs/hub/repositories-pull-requests-discussions)๋ฅผ ๋ชจ๋ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์—ด์–ด ์ด ์†์„ฑ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์„ธ์š”! + +์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค! `tokenizer.apply_chat_template`๊ฐ€ ์ด์ œ ํ•ด๋‹น ๋ชจ๋ธ์— ๋Œ€ํ•ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋ฏ€๋กœ, `TextGenerationPipeline`๊ณผ ๊ฐ™์€ ๊ณณ์—์„œ๋„ ์ž๋™์œผ๋กœ ์ง€์›๋ฉ๋‹ˆ๋‹ค! + +๋ชจ๋ธ์— ์ด ์†์„ฑ์„ ์„ค์ •ํ•จ์œผ๋กœ์จ, ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์˜ ์ „์ฒด ๊ธฐ๋Šฅ์„ ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜•์‹ ๋ถˆ์ผ์น˜๋Š” ์ด ๋ถ„์•ผ์—์„œ ์˜ค๋žซ๋™์•ˆ ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚ค๋Š” ๋ฌธ์ œ์˜€์œผ๋ฏ€๋กœ, ์ด์ œ ์ด๋ฅผ ๋๋‚ผ ๋•Œ์ž…๋‹ˆ๋‹ค! + +## ๊ณ ๊ธ‰: ํ…œํ”Œ๋ฆฟ ์ž‘์„ฑ ํŒ[[advanced-template-writing-tips]] + +Jinja์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ๋จผ์ € ๋ฉ”์‹œ์ง€๋ฅผ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ˜•์‹ํ™”ํ•˜๋Š” ์งง์€ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ ๋‹ค์Œ, ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +ํ…œํ”Œ๋ฆฟ ํ•ธ๋“ค๋Ÿฌ๋Š” `messages`๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ๋Œ€ํ™” ๊ธฐ๋ก์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ…œํ”Œ๋ฆฟ ๋‚ด์˜ `messages`์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, `{% for message in messages %}`๋กœ ๋ฐ˜๋ณตํ•˜๊ฑฐ๋‚˜ `{{ messages[0] }}`์™€ ๊ฐ™์ด ๊ฐœ๋ณ„ ๋ฉ”์‹œ์ง€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ ํŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ Jinja๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + +### ๊ณต๋ฐฑ ์ œ๊ฑฐ[[trimming-whitespace]] + +๊ธฐ๋ณธ์ ์œผ๋กœ Jinja๋Š” ๋ธ”๋ก ์ „ํ›„์˜ ๊ณต๋ฐฑ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณต๋ฐฑ์„ ๋งค์šฐ ์ •ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ ์ž ํ•˜๋Š” ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + +``` +{%- for message in messages %} + {{- message['role'] + message['content'] }} +{%- endfor %} +``` + +์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์ง€ ๋งˆ์„ธ์š”: + +``` +{% for message in messages %} + {{ message['role'] + message['content'] }} +{% endfor %} +``` + +`-`๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ธ”๋ก ์ „ํ›„์˜ ๊ณต๋ฐฑ์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋Š” ๋ฌดํ•ดํ•ด ๋ณด์ด์ง€๋งŒ, ์ค„๋ฐ”๊ฟˆ๊ณผ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ์ถœ๋ ฅ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฐ๊ณผ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +### ๋ฐ˜๋ณต๋ฌธ[[for-loops]] + +Jinja์—์„œ ๋ฐ˜๋ณต๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +``` +{%- for message in messages %} + {{- message['content'] }} +{%- endfor %} +``` + +{{ ํ‘œํ˜„์‹ ๋ธ”๋ก }} ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ์ถœ๋ ฅ์œผ๋กœ ์ธ์‡„๋ฉ๋‹ˆ๋‹ค. `+`์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„์‹ ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ์กฐ๊ฑด๋ฌธ[[if-statements]] + +Jinja์—์„œ ์กฐ๊ฑด๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +``` +{%- if message['role'] == 'user' %} + {{- message['content'] }} +{%- endif %} +``` + +ํŒŒ์ด์ฌ์ด ๊ณต๋ฐฑ์„ ์‚ฌ์šฉํ•˜์—ฌ `for` ๋ฐ `if` ๋ธ”๋ก์˜ ์‹œ์ž‘๊ณผ ๋์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐ˜๋ฉด, Jinja๋Š” `{% endfor %}` ๋ฐ `{% endif %}`๋กœ ๋ช…์‹œ์ ์œผ๋กœ ๋์„ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +### ํŠน์ˆ˜ ๋ณ€์ˆ˜[[special-variables]] + +ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€์—์„œ๋Š” `messages` ๋ชฉ๋ก์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ํŠน์ˆ˜ ๋ณ€์ˆ˜์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” `bos_token` ๋ฐ `eos_token`๊ณผ ๊ฐ™์€ ํŠน๋ณ„ ํ† ํฐ๊ณผ ์•ž์„œ ๋…ผ์˜ํ•œ `add_generation_prompt` ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `loop` ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๋ฐ˜๋ณต์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด `{% if loop.last %}`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋Œ€ํ™”์˜ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `add_generation_prompt`๊ฐ€ `True`์ธ ๊ฒฝ์šฐ ๋Œ€ํ™” ๋์— ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +``` +{%- if loop.last and add_generation_prompt %} + {{- bos_token + 'Assistant:\n' }} +{%- endif %} +``` + +### ๋น„ํŒŒ์ด์ฌ Jinja์™€์˜ ํ˜ธํ™˜์„ฑ[[compatibility-with-non-python-jinja]] + +Jinja์˜ ์—ฌ๋Ÿฌ ๊ตฌํ˜„์€ ๋‹ค์–‘ํ•œ ์–ธ์–ด๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋™์ผํ•œ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ฃผ์š” ์ฐจ์ด์ ์€ ํŒŒ์ด์ฌ์—์„œ ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•  ๋•Œ ํŒŒ์ด์ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด์— `.lower()`๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ์— `.items()`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋น„ํŒŒ์ด์ฌ Jinja ๊ตฌํ˜„์—์„œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ JS์™€ Rust๊ฐ€ ์ธ๊ธฐ ์žˆ๋Š” ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ๋Š” ๋น„ํŒŒ์ด์ฌ ๊ตฌํ˜„์ด ํ”ํ•ฉ๋‹ˆ๋‹ค. + +ํ•˜์ง€๋งŒ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”! ๋ชจ๋“  Jinja ๊ตฌํ˜„์—์„œ ํ˜ธํ™˜์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ…œํ”Œ๋ฆฟ์„ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +- ํŒŒ์ด์ฌ ๋ฉ”์†Œ๋“œ๋ฅผ Jinja ํ•„ํ„ฐ๋กœ ๋Œ€์ฒดํ•˜์„ธ์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง€๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด `string.lower()`๋Š” `string|lower`๋กœ, `dict.items()`๋Š” `dict|items`๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ๋ชฉํ•  ๋งŒํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ `string.strip()`์ด `string|trim`์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Jinja ๋ฌธ์„œ์˜ [๋‚ด์žฅ ํ•„ํ„ฐ ๋ชฉ๋ก](https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filters)์„ ์ฐธ์กฐํ•˜์„ธ์š”. +- ํŒŒ์ด์ฌ์— ํŠนํ™”๋œ `True`, `False`, `None`์„ ๊ฐ๊ฐ `true`, `false`, `none`์œผ๋กœ ๋Œ€์ฒดํ•˜์„ธ์š”. +- ๋”•์…”๋„ˆ๋ฆฌ๋‚˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง์ ‘ ๋ Œ๋”๋งํ•  ๋•Œ ๋‹ค๋ฅธ ๊ตฌํ˜„์—์„œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜ˆ: ๋ฌธ์ž์—ด ํ•ญ๋ชฉ์ด ๋‹จ์ผ ๋”ฐ์˜ดํ‘œ์—์„œ ์ด์ค‘ ๋”ฐ์˜ดํ‘œ๋กœ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). `tojson` ํ•„ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/community.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/community.md new file mode 100644 index 0000000000000000000000000000000000000000..d50168d7548620805d06b2aa06190bbbc8d4d936 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/community.md @@ -0,0 +1,69 @@ + + +# ์ปค๋ฎค๋‹ˆํ‹ฐ [[community]] + +์ด ํŽ˜์ด์ง€๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๊ฐœ๋ฐœํ•œ ๐Ÿค— Transformers ๋ฆฌ์†Œ์Šค๋ฅผ ์žฌ๊ตฌ์„ฑํ•œ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. + +## ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฆฌ์†Œ์Šค: [[community-resources]] + +| ๋ฆฌ์†Œ์Šค | ์„ค๋ช… | ๋งŒ๋“ ์ด | +|:----------|:-------------|------:| +| [Hugging Face Transformers ์šฉ์–ด์ง‘ ํ”Œ๋ž˜์‹œ์นด๋“œ](https://www.darigovresearch.com/huggingface-transformers-glossary-flashcards) | [Transformers ๋ฌธ์„œ ์šฉ์–ด์ง‘](glossary)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ”Œ๋ž˜์‹œ์นด๋“œ ์„ธํŠธ๋กœ, ์ง€์‹์„ ์žฅ๊ธฐ์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋œ ์˜คํ”ˆ์†Œ์Šค ํฌ๋กœ์Šค ํ”Œ๋žซํผ ์•ฑ์ธ [Anki](https://apps.ankiweb.net/)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ํ•™์Šต/์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. [ํ”Œ๋ž˜์‹œ์นด๋“œ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•œ ์†Œ๊ฐœ ๋™์˜์ƒ](https://www.youtube.com/watch?v=Dji_h7PILrw)์„ ์ฐธ์กฐํ•˜์„ธ์š”. | [Darigov ๋ฆฌ์„œ์น˜](https://www.darigovresearch.com/) | + +## ์ปค๋ฎค๋‹ˆํ‹ฐ ๋…ธํŠธ๋ถ: [[community-notebooks]] + +| ๋…ธํŠธ๋ถ | ์„ค๋ช… | ๋งŒ๋“ ์ด | | +|:----------|:-------------|:-------------|------:| +| [๊ฐ€์‚ฌ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/AlekseyKorshuk/huggingartists) | GPT-2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์ข‹์•„ํ•˜๋Š” ์•„ํ‹ฐ์ŠคํŠธ์˜ ์Šคํƒ€์ผ๋กœ ๊ฐ€์‚ฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• | [Aleksey Korshuk](https://github.com/AlekseyKorshuk) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AlekseyKorshuk/huggingartists/blob/master/huggingartists-demo.ipynb) | +| [Tensorflow 2๋กœ T5 ํ›ˆ๋ จํ•˜๊ธฐ](https://github.com/snapthat/TF-T5-text-to-text) | Tensorflow 2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ T5๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•. ์ด ๋…ธํŠธ๋ถ์€ Tensorflow 2๋กœ SQUAD๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ ์งˆ์˜์‘๋‹ต ์ž‘์—…์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. | [Muhammad Harris](https://github.com/HarrisDePerceptron) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/snapthat/TF-T5-text-to-text/blob/master/snapthatT5/notebooks/TF-T5-Datasets%20Training.ipynb) | +| [TPU์—์„œ T5 ํ›ˆ๋ จํ•˜๊ธฐ](https://github.com/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb) | Transformers์™€ Nlp๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ SQUAD๋กœ T5๋ฅผ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ• | [Suraj Patil](https://github.com/patil-suraj) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/T5_on_TPU.ipynb#scrollTo=QLGiFCDqvuil) | +| [๋ถ„๋ฅ˜ ๋ฐ ๊ฐ๊ด€์‹ ๋ฌธ์ œ๋ฅผ ์œ„ํ•ด T5 ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | ๋ถ„๋ฅ˜ ๋ฐ ๊ฐ๊ด€์‹ ๋ฌธ์ œ์— ๋งž๊ฒŒ ํ…์ŠคํŠธ-ํ…์ŠคํŠธ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์—ฌ PyTorch Lightning์œผ๋กœ T5๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Suraj Patil](https://github.com/patil-suraj) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/exploring-T5/blob/master/t5_fine_tuning.ipynb) | +| [์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์„ธํŠธ์™€ ์–ธ์–ด๋กœ DialoGPT ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | ์ž์œ  ๋Œ€ํ™”ํ˜• ์ฑ—๋ด‡์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ DialoGPT ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Nathan Cooper](https://github.com/ncoop57) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ncoop57/i-am-a-nerd/blob/master/_notebooks/2020-05-12-chatbot-part-1.ipynb) | +| [Reformer๋กœ ๊ธด ์‹œํ€€์Šค ๋ชจ๋ธ๋งํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | Reformer๋กœ ์ตœ๋Œ€ 50๋งŒ ํ† ํฐ์˜ ์‹œํ€€์Šค๋ฅผ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/PyTorch_Reformer.ipynb) | +| [์š”์•ฝ์„ ์œ„ํ•ด BART ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb) | blurr๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ fastai๋กœ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•ด BART๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Wayde Gilliam](https://ohmeow.com/) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb) | +| [๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํŠธ์œ—์œผ๋กœ ์‚ฌ์ „ํ›ˆ๋ จ๋œ ํŠธ๋žœ์Šคํฌ๋จธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | GPT-2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์ข‹์•„ํ•˜๋Š” ํŠธ์œ„ํ„ฐ ๊ณ„์ • ์Šคํƒ€์ผ๋กœ ํŠธ์œ—์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• | [Boris Dayma](https://github.com/borisdayma) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb) | +| [Weights & Biases๋กœ ๐Ÿค— Hugging Face ๋ชจ๋ธ ์ตœ์ ํ™”ํ•˜๊ธฐ](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | W&B์™€ Hugging Face์˜ ํ†ตํ•ฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ์ „์ฒด ํŠœํ† ๋ฆฌ์–ผ | [Boris Dayma](https://github.com/borisdayma) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/huggingface/Optimize_Hugging_Face_models_with_Weights_%26_Biases.ipynb) | +| [Longformer ์‚ฌ์ „ํ›ˆ๋ จํ•˜๊ธฐ](https://github.com/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | ๊ธฐ์กด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ "๊ธด" ๋ฒ„์ „์„ ๋นŒ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ• | [Iz Beltagy](https://beltagy.net) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/allenai/longformer/blob/master/scripts/convert_model_to_long.ipynb) | +| [QA๋ฅผ ์œ„ํ•ด Longformer ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | QA ์ž‘์—…์„ ์œ„ํ•ด Longformer๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Suraj Patil](https://github.com/patil-suraj) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patil-suraj/Notebooks/blob/master/longformer_qa_training.ipynb) | +| [๐Ÿค— Nlp๋กœ ๋ชจ๋ธ ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/How_to_evaluate_Longformer_on_TriviaQA_using_NLP.ipynb) | `Nlp`๋กœ TriviaQA์—์„œ Longformer๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1m7eTGlPmLRgoPkkA7rkhQdZ9ydpmsdLE?usp=sharing) | +| [๊ฐ์ • ๋ฒ”์œ„ ์ถ”์ถœ์„ ์œ„ํ•ด T5 ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | ๊ฐ์ • ๋ฒ”์œ„ ์ถ”์ถœ์„ ์œ„ํ•ด ํ…์ŠคํŠธ-ํ…์ŠคํŠธ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์—ฌ PyTorch Lightning์œผ๋กœ T5๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Lorenzo Ampil](https://github.com/enzoampil) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/enzoampil/t5-intro/blob/master/t5_qa_training_pytorch_span_extraction.ipynb) | +| [๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด DistilBert ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb) | ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด PyTorch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DistilBert๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multiclass_classification.ipynb)| +| [๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด BERT ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb) | ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด PyTorch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BERT๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)| +| [์š”์•ฝ์„ ์œ„ํ•ด T5 ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb) | ์š”์•ฝ์„ ์œ„ํ•ด PyTorch๋กœ T5๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  WandB๋กœ ์‹คํ—˜์„ ์ถ”์ ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Abhishek Kumar Mishra](https://github.com/abhimishra91) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_summarization_wandb.ipynb)| +| [๋™์  ํŒจ๋”ฉ/๋ฒ„์ผ“ํŒ…์œผ๋กœ Transformers ๋ฏธ์„ธ ์กฐ์ • ์†๋„ ๋†’์ด๊ธฐ](https://github.com/ELS-RD/transformers-notebook/blob/master/Divide_Hugging_Face_Transformers_training_time_by_2_or_more.ipynb)| ๋™์  ํŒจ๋”ฉ/๋ฒ„์ผ“ํŒ…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ์„ธ ์กฐ์ • ์†๋„๋ฅผ 2๋ฐฐ๋กœ ๋†’์ด๋Š” ๋ฐฉ๋ฒ• |[Michael Benesty](https://github.com/pommedeterresautee) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1CBfRU1zbfu7-ijiOqAAQUA-RJaxfcJoO?usp=sharing)| +|[๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด Reformer ์‚ฌ์ „ํ›ˆ๋ จํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/Reformer_For_Masked_LM.ipynb)| ์–‘๋ฐฉํ–ฅ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋ฅผ ์ด์šฉํ•ด์„œ Reformer ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1tzzh0i8PgDQGV3SMFUGxM7_gGae3K-uW?usp=sharing)| +| [Sci-BERT ํ™•์žฅ ๋ฐ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/lordtt13/word-embeddings/blob/master/COVID-19%20Research%20Data/COVID-SciBERT.ipynb)| CORD ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ AllenAI์—์„œ ์‚ฌ์ „ํ›ˆ๋ จ๋œ SciBERT ๋ชจ๋ธ์˜ ์–ดํœ˜๋ฅผ ๋Š˜๋ฆฌ๊ณ  ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Tanmay Thakur](https://github.com/lordtt13) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1rqAR40goxbAfez1xvF3hBJphSCsvXmh8)| +| [์š”์•ฝ์„ ์œ„ํ•ด Trainer API๋กœ BlenderBotSmall ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/lordtt13/transformers-experiments/blob/master/Custom%20Tasks/fine-tune-blenderbot_small-for-summarization.ipynb)| ์š”์•ฝ์„ ์œ„ํ•ด Trainer API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ BlenderBotSmall ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ | [Tanmay Thakur](https://github.com/lordtt13) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/19Wmupuls7mykSGyRN_Qo6lPQhgp56ymq?usp=sharing)| +| [ํ†ตํ•ฉ ๊ธฐ์šธ๊ธฐ(Integrated Gradient)๋ฅผ ์ด์šฉํ•˜์—ฌ Electra ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ํ•ด์„ํ•˜๊ธฐ](https://github.com/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb) | ๊ฐ์ • ๋ถ„์„์„ ์œ„ํ•ด Electra๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  Captum ํ†ตํ•ฉ ๊ธฐ์šธ๊ธฐ๋กœ ์˜ˆ์ธก์„ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Eliza Szczechla](https://elsanns.github.io) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/electra_fine_tune_interpret_captum_ig.ipynb)| +| [Trainer ํด๋ž˜์Šค๋กœ ๋น„์˜์–ด๊ถŒ GPT-2 ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb) | Trainer ํด๋ž˜์Šค๋กœ ๋น„์˜์–ด๊ถŒ GPT-2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Philipp Schmid](https://www.philschmid.de) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/philschmid/fine-tune-GPT-2/blob/master/Fine_tune_a_non_English_GPT_2_Model_with_Huggingface.ipynb)| +|[๋‹ค์ค‘ ๋ผ๋ฒจ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ์œ„ํ•ด DistilBERT ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb) | ๋‹ค์ค‘ ๋ผ๋ฒจ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ์œ„ํ•ด DistilBERT ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/Transformers_scripts/blob/master/Transformers_multilabel_distilbert.ipynb)| +|[๋ฌธ์žฅ์Œ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ALBERT ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb) | ๋ฌธ์žฅ์Œ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ์œ„ํ•ด ALBERT ๋ชจ๋ธ ๋˜๋Š” ๋‹ค๋ฅธ BERT ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Nadir El Manouzi](https://github.com/NadirEM) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NadirEM/nlp-notebooks/blob/master/Fine_tune_ALBERT_sentence_pair_classification.ipynb)| +|[๊ฐ์ • ๋ถ„์„์„ ์œ„ํ•ด Roberta ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb) | ๊ฐ์ • ๋ถ„์„์„ ์œ„ํ•ด Roberta ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Dhaval Taunk](https://github.com/DhavalTaunk08) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb)| +|[์งˆ๋ฌธ ์ƒ์„ฑ ๋ชจ๋ธ ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/flexudy-pipe/qugeev) | seq2seq ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ ์งˆ๋ฌธ๊ณผ ์ด์— ๋Œ€ํ•œ ๋‹ต๋ณ€์ด ์–ผ๋งˆ๋‚˜ ์ •ํ™•ํ•œ๊ฐ€์š”? | [Pascal Zoleko](https://github.com/zolekode) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bpsSqCQU-iw_5nNoRm_crPq6FRuJthq_?usp=sharing)| +|[DistilBERT์™€ Tensorflow๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ํ•˜๊ธฐ](https://github.com/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb) | ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด TensorFlow๋กœ DistilBERT๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Peter Bayerle](https://github.com/peterbayerle) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/peterbayerle/huggingface_notebook/blob/main/distilbert_tf.ipynb)| +|[CNN/Dailail ์š”์•ฝ์„ ์œ„ํ•ด ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์— BERT ํ™œ์šฉํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb) | CNN/Dailail ์š”์•ฝ์„ ์œ„ํ•ด *google-bert/bert-base-uncased* ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ *EncoderDecoderModel*์„ ์›Œ๋ฐ์—…ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)| +|[BBC XSum ์š”์•ฝ์„ ์œ„ํ•ด ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์— RoBERTa ํ™œ์šฉํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb) | BBC/XSum ์š”์•ฝ์„ ์œ„ํ•ด *FacebookAI/roberta-base* ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณต์œ  *EncoderDecoderModel*์„ ์›Œ๋ฐ์—…ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/RoBERTaShared_for_BBC_XSum.ipynb)| +|[์ˆœ์ฐจ์  ์งˆ๋ฌธ ๋‹ต๋ณ€(SQA)์„ ์œ„ํ•ด TAPAS ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) | *tapas-base* ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ˆœ์ฐจ์  ์งˆ๋ฌธ ๋‹ต๋ณ€(SQA) ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *TapasForQuestionAnswering*์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb)| +|[ํ‘œ ์‚ฌ์‹ค ๊ฒ€์‚ฌ(TabFact)๋กœ TAPAS ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb) | ๐Ÿค— Datasets์™€ ๐Ÿค— Transformer ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ *tapas-base-finetuned-tabfact* ์ฒดํฌํฌ์ธํŠธ๋กœ ๋ฏธ์„ธ ์กฐ์ •๋œ *TapasForSequenceClassification*์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Evaluating_TAPAS_on_the_Tabfact_test_set.ipynb)| +|[๋ฒˆ์—ญ์„ ์œ„ํ•ด mBART ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb) | ํžŒ๋””์–ด์—์„œ ์˜์–ด๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ ์œ„ํ•ด Seq2SeqTrainer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ mBART๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)| +|[FUNSD(์–‘์‹ ์ดํ•ด ๋ฐ์ดํ„ฐ ์„ธํŠธ)๋กœ LayoutLM ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb) | ์Šค์บ”ํ•œ ๋ฌธ์„œ์—์„œ ์ •๋ณด ์ถ”์ถœ์„ ์œ„ํ•ด FUNSD ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *LayoutLMForTokenClassification*์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb)| +|[DistilGPT2 ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ๋ฐ ํ…์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb) | DistilGPT2๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• | [Aakash Tripathi](https://github.com/tripathiaakash) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/tripathiaakash/DistilGPT2-Tutorial/blob/main/distilgpt2_fine_tuning.ipynb)| +|[์ตœ๋Œ€ 8K ํ† ํฐ์—์„œ LED ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb) | ๊ธด ๋ฒ”์œ„๋ฅผ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•ด PubMed๋กœ LED๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_tune_Longformer_Encoder_Decoder_(LED)_for_Summarization_on_pubmed.ipynb)| +|[Arxiv๋กœ LED ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb) | ๊ธด ๋ฒ”์œ„ ์š”์•ฝ์— ๋Œ€ํ•ด LED๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/LED_on_Arxiv.ipynb)| +|[RVL-CDIP(๋ฌธ์„œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐ์ดํ„ฐ ์„ธํŠธ)๋กœ LayoutLM ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ)](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb) | ์Šค์บ” ๋ฌธ์„œ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด RVL-CDIP ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *LayoutLMForSequenceClassification*์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForSequenceClassification_on_RVL_CDIP.ipynb)| +|[GPT2 ์กฐ์ •์„ ํ†ตํ•œ Wav2Vec2 CTC ๋””์ฝ”๋”ฉ](https://github.com/voidful/huggingface_notebook/blob/main/xlsr_gpt.ipynb) | ์–ธ์–ด ๋ชจ๋ธ ์กฐ์ •์„ ํ†ตํ•ด CTC ์‹œํ€€์Šค๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ• | [Eric Lam](https://github.com/voidful) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing)| +|[Trainer ํด๋ž˜์Šค๋กœ ๋‘ ๊ฐœ ์–ธ์–ด๋กœ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•ด BART ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb) | Trainer ํด๋ž˜์Šค๋กœ ๋‘ ๊ฐœ ์–ธ์–ด๋กœ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•ด BART ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Eliza Szczechla](https://github.com/elsanns) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)| +|[Trivia QA๋กœ Big Bird ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb) | Trivia QA๋กœ ๊ธด ๋ฌธ์„œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์— ๋Œ€ํ•ด BigBird๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Patrick von Platen](https://github.com/patrickvonplaten) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Evaluating_Big_Bird_on_TriviaQA.ipynb)| +| [Wav2Vec2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์˜์ƒ ์บก์…˜ ๋งŒ๋“ค๊ธฐ](https://github.com/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | Wav2Vec์œผ๋กœ ์˜ค๋””์˜ค๋ฅผ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ชจ๋“  ๋™์˜์ƒ์—์„œ YouTube ์บก์…˜ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• | [Niklas Muennighoff](https://github.com/Muennighoff) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Muennighoff/ytclipcc/blob/main/wav2vec_youtube_captions.ipynb) | +| [PyTorch Lightning์„ ์‚ฌ์šฉํ•˜์—ฌ CIFAR-10์œผ๋กœ ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | HuggingFace Transformers, Datasets, PyTorch Lightning์„ ์‚ฌ์šฉํ•˜์—ฌ CIFAR-10์œผ๋กœ ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ(ViT)๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb) | +| [๐Ÿค— Trainer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CIFAR-10์—์„œ ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | Datasets, ๐Ÿค— Trainer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CIFAR-10์—์„œ ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ(ViT)๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/nielsrogge) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb) | +| [๊ฐœ์ฒด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์„ธํŠธ์ธ Open Entity๋กœ LUKE ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | Open Entity ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *LukeForEntityClassification*์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_open_entity.ipynb) | +| [๊ด€๊ณ„ ์ถ”์ถœ ๋ฐ์ดํ„ฐ ์„ธํŠธ์ธ TACRED๋กœ LUKE ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | TACRED ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *LukeForEntityPairClassification*์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_tacred.ipynb) | +| [์ค‘์š” NER ๋ฒค์น˜๋งˆํฌ์ธ CoNLL-2003์œผ๋กœ LUKE ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | CoNLL-2003 ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *LukeForEntitySpanClassification*๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Ikuya Yamada](https://github.com/ikuyamada) |[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/studio-ousia/luke/blob/master/notebooks/huggingface_conll_2003.ipynb) | +| [PubMed ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ BigBird-Pegasus ํ‰๊ฐ€ํ•˜๊ธฐ](https://github.com/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | PubMed ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *BigBirdPegasusForConditionalGeneration*๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Vasudev Gupta](https://github.com/vasudevgupta7) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vasudevgupta7/bigbird/blob/main/notebooks/bigbird_pegasus_evaluation.ipynb) | +| [Wav2Vec2๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์Œ์„ฑ ๊ฐ์ • ๋ถ„๋ฅ˜ํ•˜๊ธฐ](https://github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | ๊ฐ์ • ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ์‚ฌ์ „ํ›ˆ๋ จ๋œ Wav2Vec2 ๋ชจ๋ธ์„ MEGA ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• | [Mehrdad Farahani](https://github.com/m3hrdadfi) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/m3hrdadfi/soxan/blob/main/notebooks/Emotion_recognition_in_Greek_speech_using_Wav2Vec2.ipynb) | +| [DETR๋กœ ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด ํƒ์ง€ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | ํ›ˆ๋ จ๋œ *DetrForObjectDetection* ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด๋ฅผ ํƒ์ง€ํ•˜๊ณ  ์–ดํ…์…˜์„ ์‹œ๊ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/DETR_minimal_example_(with_DetrFeatureExtractor).ipynb) | +| [์‚ฌ์šฉ์ž ์ง€์ • ๊ฐ์ฒด ํƒ์ง€ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ DETR ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | ์‚ฌ์šฉ์ž ์ง€์ • ๊ฐ์ฒด ํƒ์ง€ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ *DetrForObjectDetection*์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Niels Rogge](https://github.com/NielsRogge) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/DETR/Fine_tuning_DetrForObjectDetection_on_custom_dataset_(balloon).ipynb) | +| [๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์œ„ํ•ด T5 ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://github.com/ToluClassics/Notebooks/blob/main/T5_Ner_Finetuning.ipynb) | ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ž‘์—…์„ ์œ„ํ•ด *T5*๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• | [Ogundepo Odunayo](https://github.com/ToluClassics) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1obr78FY_cBmWY5ODViCmzdY6O1KB65Vc?usp=sharing) | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/contributing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/contributing.md new file mode 100644 index 0000000000000000000000000000000000000000..f005ac0a569a70daee671ed482009147fa10d8d0 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/contributing.md @@ -0,0 +1,330 @@ + + +# ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๊ธฐ [[contribute-to-transformers]] + +๋ˆ„๊ตฌ๋‚˜ ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์˜ ๊ธฐ์—ฌ๋ฅผ ์†Œ์ค‘ํžˆ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ๊ธฐ์—ฌ๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๋•๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค. ์งˆ๋ฌธ์— ๋‹ตํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์„ ๋„์™€ ๋ฌธ์„œ๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ๋„ ๋งค์šฐ ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers๋ฅผ ๋„๋ฆฌ ์•Œ๋ฆฌ๋Š” ๊ฒƒ๋„ ํฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค! ๋ฉ‹์ง„ ํ”„๋กœ์ ํŠธ๋“ค์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ ๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€์— ์–ธ๊ธ‰ํ•˜๊ฑฐ๋‚˜, ๋„์›€์ด ๋˜์—ˆ์„ ๋•Œ๋งˆ๋‹ค Twitter์— ์•Œ๋ฆฌ๊ฑฐ๋‚˜, ์ €์žฅ์†Œ์— โญ๏ธ ๋ฅผ ํ‘œ์‹œํ•˜์—ฌ ๊ฐ์‚ฌ ์ธ์‚ฌ๋ฅผ ์ „ํ•ด์ฃผ์„ธ์š”. + +์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๊ธฐ์—ฌํ•˜๋“  [ํ–‰๋™ ๊ทœ์น™](https://github.com/huggingface/transformers/blob/main/CODE_OF_CONDUCT.md)์„ ์ˆ™์ง€ํ•˜๊ณ  ์กด์ค‘ํ•ด์ฃผ์„ธ์š”. + +**์ด ์•ˆ๋‚ด์„œ๋Š” ๋ฉ‹์ง„ [scikit-learn ๊ธฐ์—ฌ ์•ˆ๋‚ด์„œ](https://github.com/scikit-learn/scikit-learn/blob/main/CONTRIBUTING.md)์—์„œ ํฐ ์˜๊ฐ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.** + +## ๊ธฐ์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ• [[ways-to-contribute]] + +์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +* ๊ธฐ์กด ์ฝ”๋“œ์˜ ๋ฏธํ•ด๊ฒฐ๋œ ๋ฌธ์ œ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. +* ๋ฒ„๊ทธ ๋˜๋Š” ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜๊ธธ ์›ํ•˜๋Š” ๊ธฐ๋Šฅ๊ณผ ๊ด€๋ จ๋œ ์ด์Šˆ๋ฅผ ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค. +* ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. +* ์˜ˆ์ œ๋‚˜ ๋ฌธ์„œ์— ๊ธฐ์—ฌํ•ฉ๋‹ˆ๋‹ค. + +์–ด๋””์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด, [Good First Issue](https://github.com/huggingface/transformers/contribute) ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ณด์„ธ์š”. ์ด ๋ชฉ๋ก์€ ์ดˆ๋ณด์ž๋„ ์ฐธ์—ฌํ•˜๊ธฐ ์‰ฌ์šด ์˜คํ”ˆ ์ด์Šˆ ๋ชฉ๋ก์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋‹น์‹ ์ด ์˜คํ”ˆ์†Œ์Šค์— ์ฒ˜์Œ์œผ๋กœ ๊ธฐ์—ฌํ•˜๋Š” ๋ฐ ํฐ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ์ € ์ž‘์—…ํ•˜๊ณ  ์‹ถ์€ ์ด์Šˆ์— ๋Œ“๊ธ€๋งŒ ๋‹ฌ์•„์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์กฐ๊ธˆ ๋” ๋„์ „์ ์ธ ์ž‘์—…์„ ์›ํ•œ๋‹ค๋ฉด, [Good Second Issue](https://github.com/huggingface/transformers/labels/Good%20Second%20Issue) ๋ชฉ๋ก๋„ ํ™•์ธํ•ด๋ณด์„ธ์š”. ์ด๋ฏธ ๋‹น์‹ ์ด ์ž˜ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋”๋ผ๋„, ํ•œ ๋ฒˆ ์‹œ๋„ํ•ด๋ณด์„ธ์š”! ์šฐ๋ฆฌ๋„ ์—ฌ๋Ÿฌ๋ถ„์„ ๋„์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๐Ÿš€ + +> ์ปค๋ฎค๋‹ˆํ‹ฐ์— ์ด๋ฃจ์–ด์ง€๋Š” ๋ชจ๋“  ๊ธฐ์—ฌ๋Š” ๋˜‘๊ฐ™์ด ์†Œ์ค‘ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฅฐ + +## ๋ฏธํ•ด๊ฒฐ๋œ ๋ฌธ์ œ ์ˆ˜์ •ํ•˜๊ธฐ [[fixing-outstanding-issues]] + +๊ธฐ์กด ์ฝ”๋“œ์—์„œ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ œ์ ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์ด ๋– ์˜ค๋ฅธ ๊ฒฝ์šฐ, ์–ธ์ œ๋“ ์ง€ [๊ธฐ์—ฌ๋ฅผ ์‹œ์ž‘](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md/#create-a-pull-request)ํ•˜๊ณ  Pull Request๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”! + +## ๋ฒ„๊ทธ ๊ด€๋ จ ์ด์Šˆ๋ฅผ ์ œ๊ธฐํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ ์š”์ฒญํ•˜๊ธฐ [[submitting-a-bugrelated-issue-or-feature-request]] + +๋ฒ„๊ทธ ๊ด€๋ จ ์ด์Šˆ๋ฅผ ์ œ๊ธฐํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์š”์ฒญํ•  ๋•Œ๋Š” ๋‹ค์Œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ตœ๋Œ€ํ•œ ์ค€์ˆ˜ํ•ด์ฃผ์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ข‹์€ ํ”ผ๋“œ๋ฐฑ๊ณผ ํ•จ๊ป˜ ๋น ๋ฅด๊ฒŒ ๋‹ต๋ณ€ํ•ด ๋“œ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ๋ฒ„๊ทธ๋ฅผ ๋ฐœ๊ฒฌํ•˜์…จ๋‚˜์š”? [[did-you-find-a-bug]] + +๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‚ฌ์šฉ ์ค‘์— ๊ฒช๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ ํ•ด์ฃผ๋Š” ์‚ฌ์šฉ์ž๋“ค ๋•๋ถ„์— ๋”์šฑ ๊ฒฌ๊ณ ํ•ด์ง€๊ณ  ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด์Šˆ๋ฅผ ๋ณด๊ณ ํ•˜๊ธฐ ์ „์—, ๋ฒ„๊ทธ๊ฐ€ ์ด๋ฏธ **๋ณด๊ณ ๋˜์ง€ ์•Š์•˜๋Š”์ง€** ํ™•์ธํ•ด์ฃผ์„ธ์š”. (GitHub์˜ ์ด์Šˆ ํƒญ ์•„๋ž˜์˜ ๊ฒ€์ƒ‰ ๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”). ์ด์Šˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด์—์„œ ๋ฐœ์ƒํ•œ ๋ฒ„๊ทธ์–ด์•ผ ํ•˜๋ฉฐ, ์ฝ”๋“œ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์ด ์•„๋‹ˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„๊ทธ๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฌธ์ œ๋กœ ๋ฐœ์ƒํ•˜์˜€๋Š”์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋จผ์ € [ํฌ๋Ÿผ](https://discuss.huggingface.co/)์—์„œ ์งˆ๋ฌธํ•ด ์ฃผ์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ผ๋ฐ˜์ ์ธ ์งˆ๋ฌธ๋ณด๋‹ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฒ„๊ทธ๊ฐ€ ์ด๋ฏธ ๋ณด๊ณ ๋˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค๋ฉด, ๋‹ค์Œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ ์ด์Šˆ๋ฅผ ์ œ์ถœํ•ด ์ฃผ์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๋น ๋ฅด๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +* ์‚ฌ์šฉ ์ค‘์ธ **์šด์˜์ฒด์ œ ์ข…๋ฅ˜์™€ ๋ฒ„์ „**, ๊ทธ๋ฆฌ๊ณ  **Python**, **PyTorch** ๋˜๋Š” **TensorFlow** ๋ฒ„์ „. +* ๋ฒ„๊ทธ๋ฅผ 30์ดˆ ์ด๋‚ด๋กœ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•˜๊ณ  ๋…๋ฆฝ์ ์ธ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ. +* ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ *์ „์ฒด* ํŠธ๋ ˆ์ด์Šค๋ฐฑ. +* ์Šคํฌ๋ฆฐ์ƒท๊ณผ ๊ฐ™์ด ๋„์›€์ด ๋  ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ๋˜๋Š” ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ฒจ๋ถ€ํ•ด ์ฃผ์„ธ์š”. + +์šด์˜์ฒด์ œ์™€ ์†Œํ”„ํŠธ์›จ์–ด ๋ฒ„์ „์„ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”: + +```bash +transformers env +``` + +์ €์žฅ์†Œ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ๋„ ๊ฐ™์€ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +python src/transformers/commands/transformers_cli.py env +``` + + +### ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์›ํ•˜์‹œ๋‚˜์š”? [[do-you-want-a-new-feature]] + +๐Ÿค— Transformers์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค๋ฉด, ๋‹ค์Œ ๋‚ด์šฉ์„ ํฌํ•จํ•˜์—ฌ ์ด์Šˆ๋ฅผ ์ œ์ถœํ•ด ์ฃผ์„ธ์š”: + +1. ์ด ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•œ *์ด์œ *๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ๋ฌธ์ œ๋‚˜ ๋ถˆ๋งŒ๊ณผ ๊ด€๋ จ์ด ์žˆ๋‚˜์š”? ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์ธ๊ฐ€์š”? ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋„์›€์ด ๋  ๋งŒํ•œ ๊ธฐ๋Šฅ์ธ๊ฐ€์š”? + + ์–ด๋–ค ๋‚ด์šฉ์ด๋“  ์—ฌ๋Ÿฌ๋ถ„์˜ ์ด์•ผ๊ธฐ๋ฅผ ๋“ฃ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค! + +2. ์š”์ฒญํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ตœ๋Œ€ํ•œ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด ์ฃผ์„ธ์š”. ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ• ์ˆ˜๋ก ๋” ๋‚˜์€ ๋„์›€์„ ๋“œ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +3. ํ•ด๋‹น ๊ธฐ๋Šฅ์˜ ์‚ฌ์šฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๋Š” *์ฝ”๋“œ ์Šค๋‹ˆํŽซ*์„ ์ œ๊ณตํ•ด ์ฃผ์„ธ์š”. +4. ๊ธฐ๋Šฅ๊ณผ ๊ด€๋ จ๋œ ๋…ผ๋ฌธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋งํฌ๋ฅผ ํฌํ•จํ•ด ์ฃผ์„ธ์š”. + +์ด์Šˆ๊ฐ€ ์ž˜ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด ์ด์Šˆ๊ฐ€ ์ƒ์„ฑ๋œ ์ˆœ๊ฐ„, ์ด๋ฏธ 80% ์ •๋„์˜ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด์Šˆ๋ฅผ ์ œ๊ธฐํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๋งŒํ•œ [ํ…œํ”Œ๋ฆฟ](https://github.com/huggingface/transformers/tree/main/templates)๋„ ์ค€๋น„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์œผ์‹ ๊ฐ€์š”? [[do-you-want-to-implement-a-new-model]] + +์ƒˆ๋กœ์šด ๋ชจ๋ธ์€ ๊ณ„์†ํ•ด์„œ ์ถœ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด ์ฃผ์„ธ์š”: + +* ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช…๊ณผ ๋…ผ๋ฌธ ๋งํฌ. +* ๊ตฌํ˜„์ด ๊ณต๊ฐœ๋˜์–ด ์žˆ๋‹ค๋ฉด ๊ตฌํ˜„ ๋งํฌ. +* ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๊ฐ€์ค‘์น˜ ๋งํฌ. + +๋งŒ์•ฝ ๋ชจ๋ธ์„ ์ง์ ‘ ๊ธฐ์—ฌํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, ์•Œ๋ ค์ฃผ์„ธ์š”. ๐Ÿค— Transformers์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! + +[๐Ÿค— Transformers์— ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/docs/transformers/add_new_model)์— ๋Œ€ํ•œ ๊ธฐ์ˆ ์ ์ธ ์•ˆ๋‚ด์„œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฌธ์„œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ์‹ ๊ฐ€์š”? [[do-you-want-to-add-documentation]] + +์šฐ๋ฆฌ๋Š” ์–ธ์ œ๋‚˜ ๋” ๋ช…ํ™•ํ•˜๊ณ  ์ •ํ™•ํ•œ ๋ฌธ์„œ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ฐœ์„ ์ ์„ ์ฐพ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜คํƒˆ์ž๋‚˜ ๋ถ€์กฑํ•œ ๋‚ด์šฉ, ๋ถ„๋ช…ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋ถ€์ •ํ™•ํ•œ ๋‚ด์šฉ ๋“ฑ์„ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ๊ด€์‹ฌ์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ๊ธฐ์—ฌํ•˜์‹ค ์ˆ˜ ์žˆ๋„๋ก ๋„์™€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! + +๋ฌธ์„œ๋ฅผ ์ƒ์„ฑ, ๋นŒ๋“œ ๋ฐ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [README](https://github.com/huggingface/transformers/tree/main/docs) ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•ด ์ฃผ์„ธ์š”. + +## ํ’€ ๋ฆฌํ€˜์ŠคํŠธ(Pull Request) ์ƒ์„ฑํ•˜๊ธฐ [[create-a-pull-request]] + +์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๊ธฐ์กด์˜ Pull Request๋‚˜ ์ด์Šˆ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ˆ„๊ตฐ๊ฐ€ ์ด๋ฏธ ๋™์ผํ•œ ์ž‘์—…์„ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ™•์‹คํ•˜์ง€ ์•Š๋‹ค๋ฉด ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด ์ด์Šˆ๋ฅผ ์—ด์–ด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋ณธ์ ์ธ `git` ์‚ฌ์šฉ ๋Šฅ๋ ฅ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. `git`์€ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šด ๋„๊ตฌ๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋งค์šฐ ํ›Œ๋ฅญํ•œ ๋งค๋‰ด์–ผ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‰˜(shell)์—์„œ `git --help`์„ ์ž…๋ ฅํ•˜์—ฌ ํ™•์ธํ•ด๋ณด์„ธ์š”! ๋งŒ์•ฝ ์ฑ…์„ ์„ ํ˜ธํ•œ๋‹ค๋ฉด, [Pro Git](https://git-scm.com/book/en/v2)์€ ๋งค์šฐ ์ข‹์€ ์ฐธ๊ณ  ์ž๋ฃŒ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๋ ค๋ฉด **[Python 3.9](https://github.com/huggingface/transformers/blob/main/setup.py#L426)** ์ด์ƒ์˜ ๋ฒ„์ „์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์—ฌ๋ฅผ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฅด์„ธ์š”: + +1. ์ €์žฅ์†Œ ํŽ˜์ด์ง€์—์„œ **[Fork](https://github.com/huggingface/transformers/fork)** ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ €์žฅ์†Œ๋ฅผ ํฌํฌํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ์˜ ๋ณต์‚ฌ๋ณธ์ด ์—ฌ๋Ÿฌ๋ถ„์˜ GitHub ์‚ฌ์šฉ์ž ๊ณ„์ • ์•„๋ž˜์— ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. + +2. ํฌํฌํ•œ ์ €์žฅ์†Œ๋ฅผ ๋กœ์ปฌ ๋””์Šคํฌ๋กœ ํด๋ก ํ•˜๊ณ , ๊ธฐ๋ณธ ์ €์žฅ์†Œ๋ฅผ ์›๊ฒฉ(remote)์œผ๋กœ ์ถ”๊ฐ€ํ•˜์„ธ์š”: + + ```bash + git clone git@github.com:/transformers.git + cd transformers + git remote add upstream https://github.com/huggingface/transformers.git + ``` + +3. ๊ฐœ๋ฐœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ €์žฅํ•  ์ƒˆ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + + ```bash + git checkout -b a-descriptive-name-for-my-changes + ``` + + ๐Ÿšจ ์ ˆ๋Œ€ `main` ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…ํ•˜์ง€ **๋งˆ์„ธ์š”!** + +4. ๊ฐ€์ƒ ํ™˜๊ฒฝ์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜์„ธ์š”: + + ```bash + pip install -e ".[dev]" + ``` + + ๋งŒ์•ฝ ์ด๋ฏธ ๊ฐ€์ƒ ํ™˜๊ฒฝ์— ๐Ÿค— Transformers๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋ฉด, `-e` ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์น˜ํ•˜๊ธฐ ์ „์— `pip uninstall transformers`๋กœ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”. + + ์—ฌ๋Ÿฌ๋ถ„์˜ ์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ์„œ, ๊ทธ๋ฆฌ๊ณ  ๐Ÿค— Transformers์˜ ์„ ํƒ์  ์˜์กด์„ฑ์˜ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ, ์ด ๋ช…๋ น์ด ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋”ฅ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ(PyTorch, TensorFlow, ๊ทธ๋ฆฌ๊ณ /๋˜๋Š” Flax)๋ฅผ ์„ค์น˜ํ•œ ํ›„ ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์ฃผ์„ธ์š”: + + ```bash + pip install -e ".[quality]" + ``` + + ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ด๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +5. ๋ธŒ๋žœ์น˜์—์„œ ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•˜์„ธ์š”. + + ์ฝ”๋“œ๋ฅผ ์ž‘์—…ํ•˜๋Š” ๋™์•ˆ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ(test suite)๊ฐ€ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”: + + ```bash + pytest tests/.py + ``` + + ํ…Œ์ŠคํŠธ์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ •๋ณด๋Š” [ํ…Œ์ŠคํŠธ](https://huggingface.co/docs/transformers/testing) ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + + ๐Ÿค— Transformers๋Š” `black`๊ณผ `ruff`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ์Šค ์ฝ”๋“œ์˜ ํ˜•์‹์„ ์ผ๊ด€๋˜๊ฒŒ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ ์šฉํ•œ ํ›„์—๋Š” ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์ž๋™์œผ๋กœ ์Šคํƒ€์ผ ๊ต์ • ๋ฐ ์ฝ”๋“œ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜์„ธ์š”: + + ```bash + make fixup + ``` + + ์ด๊ฒƒ์€ ๋˜ํ•œ ์ž‘์—… ์ค‘์ธ PR์—์„œ ์ˆ˜์ •ํ•œ ํŒŒ์ผ์—์„œ๋งŒ ์ž‘๋™ํ•˜๋„๋ก ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + + ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋‚˜์”ฉ ์‹คํ–‰ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์Šคํƒ€์ผ ๊ต์ •์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```bash + make style + ``` + + ๐Ÿค— Transformers๋Š” ๋˜ํ•œ `ruff`์™€ ๋ช‡ ๊ฐ€์ง€ ์‚ฌ์šฉ์ž ์ •์˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋”ฉ ์‹ค์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. CI๋ฅผ ํ†ตํ•ด ํ’ˆ์งˆ ๊ด€๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋˜์ง€๋งŒ, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ๋™์ผํ•œ ๊ฒ€์‚ฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```bash + make quality + ``` + + ๋งˆ์ง€๋ง‰์œผ๋กœ, ์ƒˆ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์ผ๋ถ€ ํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ๋งŽ์€ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์ด๋Ÿฌํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```bash + make repo-consistency + ``` + + ์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ณ  ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ [Pull Request์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ](https://huggingface.co/docs/transformers/pr_checks) ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + + ๋งŒ์•ฝ `docs/source` ๋””๋ ‰ํ„ฐ๋ฆฌ ์•„๋ž˜์˜ ๋ฌธ์„œ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ๋ฌธ์„œ๊ฐ€ ๋นŒ๋“œ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์ด ๊ฒ€์‚ฌ๋Š” Pull Request๋ฅผ ์—ด ๋•Œ๋„ CI์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ๊ฒ€์‚ฌ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ฌธ์„œ ๋นŒ๋”๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + + ```bash + pip install ".[docs]" + ``` + + ์ €์žฅ์†Œ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”: + + ```bash + doc-builder build transformers docs/source/en --build_dir ~/tmp/test-build + ``` + + ์ด ๋ช…๋ น์€ `~/tmp/test-build` ํด๋”์— ๋ฌธ์„œ๋ฅผ ๋นŒ๋“œํ•˜๋ฉฐ, ์ƒ์„ฑ๋œ Markdown ํŒŒ์ผ์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ง‘๊ธฐ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Pull Request๋ฅผ ์—ด ๋•Œ GitHub์—์„œ ๋ฌธ์„œ๋ฅผ ๋ฏธ๋ฆฌ ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋งŒ์กฑํ•˜๋ฉด `git add`๋กœ ๋ณ€๊ฒฝ๋œ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ , `git commit`์œผ๋กœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋กœ์ปฌ์— ๊ธฐ๋กํ•˜์„ธ์š”: + + ```bash + git add modified_file.py + git commit + ``` + + [์ข‹์€ ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€](https://chris.beams.io/posts/git-commit/)๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ „๋‹ฌํ•˜์„ธ์š”! + + ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ํ”„๋กœ์ ํŠธ ์›๋ณธ ์ €์žฅ์†Œ์™€ ๋™๊ธฐํ™”ํ•˜๋ ค๋ฉด, PR์„ *์—ด๊ธฐ ์ „์—* ๋ธŒ๋žœ์น˜๋ฅผ `upstream/branch`๋กœ ๋ฆฌ๋ฒ ์ด์Šค(rebase)ํ•˜์„ธ์š”. ๋˜๋Š” ๊ด€๋ฆฌ์ž์˜ ์š”์ฒญ์— ์ด ์ž‘์—…์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```bash + git fetch upstream + git rebase upstream/main + ``` + + ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ธŒ๋žœ์น˜์— ํ‘ธ์‹œํ•˜์„ธ์š”: + + ```bash + git push -u origin a-descriptive-name-for-my-changes + ``` + + ์ด๋ฏธ PR์„ ์—ด์—ˆ๋‹ค๋ฉด, `--force` ํ”Œ๋ž˜๊ทธ์™€ ํ•จ๊ป˜ ๊ฐ•์ œ ํ‘ธ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง PR์ด ์—ด๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ํ‘ธ์‹œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +6. ์ด์ œ GitHub์—์„œ ํฌํฌํ•œ ์ €์žฅ์†Œ๋กœ ์ด๋™ํ•˜๊ณ  **Pull request(ํ’€ ๋ฆฌํ€˜์ŠคํŠธ)**๋ฅผ ํด๋ฆญํ•˜์—ฌ Pull Request๋ฅผ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ [์ฒดํฌ๋ฆฌ์ŠคํŠธ](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md/#pull-request-checklist)์—์„œ ๋ชจ๋“  ํ•ญ๋ชฉ์— ์ฒดํฌ ํ‘œ์‹œ๋ฅผ ํ•˜์„ธ์š”. ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ณด๋‚ด ๊ฒ€ํ† ๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +7. ๊ด€๋ฆฌ์ž๊ฐ€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์š”์ฒญํ•ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ํ•ต์‹ฌ ๊ธฐ์—ฌ์ž๋“ค๋„ ๋™์ผํ•œ ์ƒํ™ฉ์„ ๊ฒช์Šต๋‹ˆ๋‹ค! ๋ชจ๋‘๊ฐ€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ Pull Request์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก, ๋กœ์ปฌ ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…ํ•˜๊ณ  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ํฌํฌํ•œ ์ €์žฅ์†Œ๋กœ ํ‘ธ์‹œํ•˜์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ž๋™์œผ๋กœ Pull Request์— ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. + +### Pull Request ์ฒดํฌ๋ฆฌ์ŠคํŠธ [[pull-request-checklist]] + +โ˜ Pull Request ์ œ๋ชฉ์€ ๊ธฐ์—ฌ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
+โ˜ Pull Request๊ฐ€ ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ, Pull Request ์„ค๋ช…์— ์ด์Šˆ ๋ฒˆํ˜ธ๋ฅผ ์–ธ๊ธ‰ํ•˜์—ฌ ์—ฐ๊ด€๋˜์–ด ์žˆ์Œ์„ ์•Œ๋ ค์ฃผ์„ธ์š”. (์ด์Šˆ๋ฅผ ํ™•์ธํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ํ•ด๋‹น ์ด์Šˆ์— ๋Œ€ํ•œ ์ž‘์—…์ด ์ง„ํ–‰ ์ค‘์ž„์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค).
+โ˜ ์ž‘์—…์ด ์ง„ํ–‰์ค‘์ด๋ผ๋ฉด ์ œ๋ชฉ ์•ž์— `[WIP]`๋ฅผ ๋ถ™์—ฌ์ฃผ์„ธ์š”. ์ค‘๋ณต ์ž‘์—…์„ ํ”ผํ•˜๊ณ  ๋ณ‘ํ•ฉํ•  ์ค€๋น„๊ฐ€ ๋œ PR๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
+โ˜ ๊ธฐ์กด ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.
+โ˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๋„ ์ถ”๊ฐ€ํ•˜์„ธ์š”.
+ - ์ƒˆ ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, `ModelTester.all_model_classes = (MyModel, MyModelWithLMHead,...)`์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์ธ ํ…Œ์ŠคํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•˜์„ธ์š”. + - ์ƒˆ `@slow` ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: `RUN_SLOW=1 python -m pytest tests/models/my_new_model/test_my_new_model.py`. + - ์ƒˆ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: `RUN_SLOW=1 python -m pytest tests/models/{your_model_name}/test_tokenization_{your_model_name}.py`. + - CircleCI์—์„œ๋Š” ๋А๋ฆฐ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š์ง€๋งŒ, GitHub Actions์—์„œ๋Š” ๋งค์ผ ๋ฐค ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค!
+ +โ˜ ๋ชจ๋“  ๊ณต๊ฐœ ๋ฉ”์†Œ๋“œ๋Š” ์œ ์šฉํ•œ ๊ธฐ์ˆ ๋ฌธ์„œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค (์˜ˆ๋ฅผ ๋“ค์–ด [`modeling_bert.py`](https://github.com/huggingface/transformers/blob/main/src/transformers/models/bert/modeling_bert.py) ์ฐธ์กฐ).
+โ˜ ์ €์žฅ์†Œ๊ฐ€ ๋น ๋ฅด๊ฒŒ ์„ฑ์žฅํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ €์žฅ์†Œ์— ์ƒ๋‹นํ•œ ๋ถ€๋‹ด์„ ์ฃผ๋Š” ์ด๋ฏธ์ง€, ๋™์˜์ƒ ๋ฐ ๊ธฐํƒ€ ํ…์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์€ ์ถ”๊ฐ€ํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  [`hf-internal-testing`](https://huggingface.co/hf-internal-testing)๊ณผ ๊ฐ™์€ Hub ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ํŒŒ์ผ์„ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  URL๋กœ ์ฐธ์กฐํ•˜์„ธ์š”. ๋ฌธ์„œ์™€ ๊ด€๋ จ๋œ ์ด๋ฏธ์ง€๋Š” ๋‹ค์Œ ์ €์žฅ์†Œ์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค: [huggingface/documentation-images](https://huggingface.co/datasets/huggingface/documentation-images). ์ด ๋ฐ์ดํ„ฐ์…‹ ์ €์žฅ์†Œ์—์„œ PR์„ ์—ด์–ด์„œ Hugging Face ๋ฉค๋ฒ„์—๊ฒŒ ๋ณ‘ํ•ฉ์„ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +Pull Request์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒ€์‚ฌ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” [Pull Request์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ](https://huggingface.co/docs/transformers/pr_checks) ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +### ํ…Œ์ŠคํŠธ [[tests]] + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋™์ž‘๊ณผ ์—ฌ๋Ÿฌ ์˜ˆ์ œ๋ฅผ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ…Œ์ŠคํŠธ๋Š” [tests](https://github.com/huggingface/transformers/tree/main/tests) ํด๋”์—, ์˜ˆ์ œ ํ…Œ์ŠคํŠธ๋Š” [examples](https://github.com/huggingface/transformers/tree/main/examples) ํด๋”์— ์žˆ์Šต๋‹ˆ๋‹ค. + +์†๋„๊ฐ€ ๋น ๋ฅธ `pytest`์™€ `pytest-xdist`๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ์†Œ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•  *ํ•˜์œ„ ํด๋” ๊ฒฝ๋กœ ๋˜๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ*๋ฅผ ์ง€์ •ํ•˜์„ธ์š”: + +```bash +python -m pytest -n auto --dist=loadfile -s -v ./tests/models/my_new_model +``` + +๋งˆ์ฐฌ๊ฐ€์ง€๋กœ `examples` ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ๋„ *ํ•˜์œ„ ํด๋” ๊ฒฝ๋กœ ๋˜๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ*๋ฅผ ์ง€์ •ํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ๋ช…๋ น์€ PyTorch `examples` ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ํ•˜์œ„ ํด๋”๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install -r examples/xxx/requirements.txt # only needed the first time +python -m pytest -n auto --dist=loadfile -s -v ./examples/pytorch/text-classification +``` + +์ด๊ฒƒ์ด ์‹ค์ œ๋กœ `make test` ๋ฐ `make test-examples` ๋ช…๋ น์ด ๊ตฌํ˜„๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค (`pip install`์€ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค)! + +๋˜ํ•œ ํŠน์ • ๊ธฐ๋Šฅ๋งŒ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ ๋” ์ž‘์€ ํ…Œ์ŠคํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์œผ๋กœ ๋А๋ฆฐ ํ…Œ์ŠคํŠธ๋Š” ๊ฑด๋„ˆ๋›ฐ์ง€๋งŒ `RUN_SLOW` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ `yes`๋กœ ์„ค์ •ํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งŽ์€ ๊ธฐ๊ฐ€๋ฐ”์ดํŠธ ๋‹จ์œ„์˜ ๋ชจ๋ธ์ด ๋‹ค์šด๋กœ๋“œ๋˜๋ฏ€๋กœ ์ถฉ๋ถ„ํ•œ ๋””์Šคํฌ ๊ณต๊ฐ„, ์ข‹์€ ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ๊ณผ ๋งŽ์€ ์ธ๋‚ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค! + + + +ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด *ํ•˜์œ„ ํด๋” ๊ฒฝ๋กœ ๋˜๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ*๋ฅผ ์ง€์ •ํ•˜์„ธ์š”. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด `tests` ๋˜๋Š” `examples` ํด๋”์˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜์–ด ๋งค์šฐ ๊ธด ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค! + + + +```bash +RUN_SLOW=yes python -m pytest -n auto --dist=loadfile -s -v ./tests/models/my_new_model +RUN_SLOW=yes python -m pytest -n auto --dist=loadfile -s -v ./examples/pytorch/text-classification +``` + +๋А๋ฆฐ ํ…Œ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ…Œ์ŠคํŠธ ์ค‘์— ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™œ์„ฑํ™”๋˜์ง€ ์•Š๋Š” ๋‹ค๋ฅธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: +- `RUN_CUSTOM_TOKENIZERS`: ์‚ฌ์šฉ์ž ์ •์˜ ํ† ํฌ๋‚˜์ด์ € ํ…Œ์ŠคํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. + +๋” ๋งŽ์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ ์ถ”๊ฐ€ ์ •๋ณด๋Š” [testing_utils.py](src/transformers/testing_utils.py)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers๋Š” ํ…Œ์ŠคํŠธ ์‹คํ–‰๊ธฐ๋กœ `pytest`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ…Œ์ŠคํŠธ ์Šค์œ„ํŠธ ์ž์ฒด์—์„œ๋Š” `pytest` ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +์ด๊ฒƒ์€ `unittest`๊ฐ€ ์™„์ „ํžˆ ์ง€์›๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ `unittest`๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```bash +python -m unittest discover -s tests -t . -v +python -m unittest discover -s examples -t examples -v +``` + +### ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ [[style-guide]] + +๋ฌธ์„œ๋Š” [Google Python ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ](https://google.github.io/styleguide/pyguide.html)๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” [๋ฌธ์„œ ์ž‘์„ฑ ๊ฐ€์ด๋“œ](https://github.com/huggingface/transformers/tree/main/docs#writing-documentation---specification)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +### Windows์—์„œ ๊ฐœ๋ฐœ [[develop-on-windows]] + +Windows์—์„œ ๊ฐœ๋ฐœํ•  ๊ฒฝ์šฐ([Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/) ๋˜๋Š” WSL์—์„œ ์ž‘์—…ํ•˜์ง€ ์•Š๋Š” ํ•œ) Windows `CRLF` ์ค„ ๋ฐ”๊ฟˆ์„ Linux `LF` ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋„๋ก git์„ ๊ตฌ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +git config core.autocrlf input +``` + +Windows์—์„œ `make` ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ MSYS2๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +1. [MSYS2](https://www.msys2.org/)๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. `C:\msys64`์— ์„ค์น˜๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. +2. CLI์—์„œ `C:\msys64\msys2.exe`๋ฅผ ์—ฝ๋‹ˆ๋‹ค (์‹œ์ž‘ ๋ฉ”๋‰ด์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ). +3. ์‰˜์—์„œ ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์—ฌ: `pacman -Syu` ๋ฐ `pacman -S make`๋กœ `make`๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. +4. ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH์— `C:\msys64\usr\bin`์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”. + +์ด์ œ ๋ชจ๋“  ํ„ฐ๋ฏธ๋„ (PowerShell, cmd.exe ๋“ฑ)์—์„œ `make`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๐ŸŽ‰ + +### ํฌํฌํ•œ ์ €์žฅ์†Œ๋ฅผ ์ƒ์œ„ ์›๋ณธ ๋ธŒ๋žœ์น˜(main)๊ณผ ๋™๊ธฐํ™”ํ•˜๊ธฐ (Hugging Face ์ €์žฅ์†Œ) [[sync-a-forked-repository-with-upstream-main-the-hugging-face-repository]] + +ํฌํฌํ•œ ์ €์žฅ์†Œ์˜ main ๋ธŒ๋žœ์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ, ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ผ ์ˆ˜ํ–‰ํ•ด์ฃผ์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ upstream PR์— ์ฐธ์กฐ ๋…ธํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ณ  ์ด๋Ÿฌํ•œ PR์— ๊ด€์—ฌํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ๋ถˆํ•„์š”ํ•œ ์•Œ๋ฆผ์ด ์ „์†ก๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +1. ๊ฐ€๋Šฅํ•˜๋ฉด ํฌํฌ๋œ ์ €์žฅ์†Œ์˜ ๋ธŒ๋žœ์น˜ ๋ฐ PR์„ ์‚ฌ์šฉํ•˜์—ฌ upstream๊ณผ ๋™๊ธฐํ™”ํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  ํฌํฌ๋œ main ์ €์žฅ์†Œ์— ์ง์ ‘ ๋ณ‘ํ•ฉํ•˜์„ธ์š”. +2. PR์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ๋ธŒ๋žœ์น˜๋ฅผ ํ™•์ธํ•œ ํ›„ ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + + ```bash + git checkout -b your-branch-for-syncing + git pull --squash --no-commit upstream main + git commit -m '' + git push --set-upstream origin your-branch-for-syncing + ``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/conversations.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/conversations.md new file mode 100644 index 0000000000000000000000000000000000000000..920cb1387860865e6f01ff47b79c50a2e9c4bea3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/conversations.md @@ -0,0 +1,306 @@ + + +# Transformers๋กœ ์ฑ„ํŒ…ํ•˜๊ธฐ[[chatting-with-transformers]] + +์ด ๊ธ€์„ ๋ณด๊ณ  ์žˆ๋‹ค๋ฉด **์ฑ„ํŒ… ๋ชจ๋ธ**์— ๋Œ€ํ•ด ์–ด๋А ์ •๋„ ์•Œ๊ณ  ๊ณ„์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ฑ„ํŒ… ๋ชจ๋ธ์ด๋ž€ ๋ฉ”์„ธ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ™”ํ˜• ์ธ๊ณต์ง€๋Šฅ์ž…๋‹ˆ๋‹ค. +๋Œ€ํ‘œ์ ์œผ๋กœ ChatGPT๊ฐ€ ์žˆ๊ณ , ์ด์™€ ๋น„์Šทํ•˜๊ฑฐ๋‚˜ ๋” ๋›ฐ์–ด๋‚œ ์˜คํ”ˆ์†Œ์Šค ์ฑ„ํŒ… ๋ชจ๋ธ์ด ๋งŽ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ๋ชจ๋ธ๋“ค์€ ๋ฌด๋ฃŒ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํฌ๊ณ  ๋ฌด๊ฑฐ์šด ๋ชจ๋ธ์€ ๊ณ ์„ฑ๋Šฅ ํ•˜๋“œ์›จ์–ด์™€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ, +์ €์‚ฌ์–‘ GPU ํ˜น์€ ์ผ๋ฐ˜ ๋ฐ์Šคํฌํƒ‘์ด๋‚˜ ๋…ธํŠธ๋ถ CPU์—์„œ๋„ ์ž˜ ์ž‘๋™ํ•˜๋Š” ์†Œํ˜• ๋ชจ๋ธ๋“ค๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋Š” ๋ถ„๋“ค์—๊ฒŒ ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์šฐ๋ฆฌ๋Š” ๊ฐ„ํŽธํ•œ ๊ณ ์ˆ˜์ค€(High-Level) "pipeline"์„ ํ†ตํ•ด ๋น ๋ฅธ ์‹œ์ž‘ ๊ฐ€์ด๋“œ๋ฅผ ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. +๊ฐ€์ด๋“œ์—๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ์„ ๋ฐ”๋กœ ์‹œ์ž‘ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค. +๋น ๋ฅธ ์‹œ์ž‘ ๊ฐ€์ด๋“œ ์ดํ›„์—๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ์ด ์ •ํ™•ํžˆ ๋ฌด์—‡์ธ์ง€, ์ ์ ˆํ•œ ๋ชจ๋ธ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ, +์ฑ„ํŒ… ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ ๋‹จ๊ณ„์˜ ์ €์ˆ˜์ค€(Low-Level) ๋ถ„์„ ๋“ฑ ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +๋˜ํ•œ ์ฑ„ํŒ… ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํŒ๋„ ์ œ๊ณตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + + +## ๋น ๋ฅธ ์‹œ์ž‘[[quickstart]] + +์ž์„ธํžˆ ๋ณผ ์—ฌ์œ ๊ฐ€ ์—†๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด ๊ฐ„๋‹จํžˆ ์š”์•ฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: +์ฑ„ํŒ… ๋ชจ๋ธ์€ ๋Œ€ํ™” ๋ฉ”์„ธ์ง€๋ฅผ ๊ณ„์†ํ•ด์„œ ์ƒ์„ฑํ•ด ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. +์ฆ‰, ์งค๋ง‰ํ•œ ์ฑ„ํŒ… ๋ฉ”์„ธ์ง€๋ฅผ ๋ชจ๋ธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋ฉด, ๋ชจ๋ธ์€ ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์‘๋‹ต์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ๋Œ€ํ™”๋ฅผ ์ด์–ด ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. +์ด์ œ ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +๋จผ์ €, ์ฑ„ํŒ…์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + + +```python +chat = [ + {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."}, + {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"} +] +``` + +์ฃผ๋ชฉํ•˜์„ธ์š”, ๋Œ€ํ™”๋ฅผ ์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ ์œ ์ € ๋ฉ”์„ธ์ง€ ์ด์™ธ์˜๋„, ๋ณ„๋„์˜ **์‹œ์Šคํ…œ** ๋ฉ”์„ธ์ง€๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋ชจ๋“  ์ฑ„ํŒ… ๋ชจ๋ธ์ด ์‹œ์Šคํ…œ ๋ฉ”์„ธ์ง€๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, +์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์‹œ์Šคํ…œ ๋ฉ”์„ธ์ง€๋Š” ๋Œ€ํ™”์—์„œ ๋ชจ๋ธ์ด ์–ด๋–ป๊ฒŒ ํ–‰๋™ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, ์œ ์พŒํ•˜๊ฑฐ๋‚˜ ์ง„์ง€ํ•˜๊ณ ์ž ํ•  ๋•Œ, ์งง์€ ๋‹ต๋ณ€์ด๋‚˜ ๊ธด ๋‹ต๋ณ€์„ ์›ํ•  ๋•Œ ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์‹œ์Šคํ…œ ๋ฉ”์„ธ์ง€๋ฅผ ์ƒ๋žตํ•˜๊ณ  +"You are a helpful and intelligent AI assistant who responds to user queries." +์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + +์ฑ„ํŒ…์„ ์‹œ์ž‘ํ–ˆ๋‹ค๋ฉด ๋Œ€ํ™”๋ฅผ ์ด์–ด ๋‚˜๊ฐ€๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ•์€ [`TextGenerationPipeline`]๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +ํ•œ๋ฒˆ `LLaMA-3`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ์‹œ์—ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +์šฐ์„  `LLaMA-3`๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์Šน์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. [๊ถŒํ•œ ์‹ ์ฒญ](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)์„ ํ•˜๊ณ  Hugging Face ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•œ ํ›„์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋˜ํ•œ ์šฐ๋ฆฌ๋Š” `device_map="auto"`๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด ๋กœ๋“œ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. +๊ทธ๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์„ ์œ„ํ•ด dtype์„ `torch.bfloat16`์œผ๋กœ ์„ค์ •ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```python +import torch +from transformers import pipeline + +pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", torch_dtype=torch.bfloat16, device_map="auto") +response = pipe(chat, max_new_tokens=512) +print(response[0]['generated_text'][-1]['content']) +``` + +์ดํ›„ ์‹คํ–‰์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค: + +```text +(sigh) Oh boy, you're asking me for advice? You're gonna need a map, pal! Alright, +alright, I'll give you the lowdown. But don't say I didn't warn you, I'm a robot, not a tour guide! + +So, you wanna know what's fun to do in the Big Apple? Well, let me tell you, there's a million +things to do, but I'll give you the highlights. First off, you gotta see the sights: the Statue of +Liberty, Central Park, Times Square... you know, the usual tourist traps. But if you're lookin' for +something a little more... unusual, I'd recommend checkin' out the Museum of Modern Art. It's got +some wild stuff, like that Warhol guy's soup cans and all that jazz. + +And if you're feelin' adventurous, take a walk across the Brooklyn Bridge. Just watch out for +those pesky pigeons, they're like little feathered thieves! (laughs) Get it? Thieves? Ah, never mind. + +Now, if you're lookin' for some serious fun, hit up the comedy clubs in Greenwich Village. You might +even catch a glimpse of some up-and-coming comedians... or a bunch of wannabes tryin' to make it big. (winks) + +And finally, if you're feelin' like a real New Yorker, grab a slice of pizza from one of the many amazing +pizzerias around the city. Just don't try to order a "robot-sized" slice, trust me, it won't end well. (laughs) + +So, there you have it, pal! That's my expert advice on what to do in New York. Now, if you'll +excuse me, I've got some oil changes to attend to. (winks) +``` + +์ฑ„ํŒ…์„ ๊ณ„์†ํ•˜๋ ค๋ฉด, ์ž์‹ ์˜ ๋‹ต์žฅ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. +ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ๋ฐ˜ํ™˜๋œ `response` ๊ฐ์ฒด์—๋Š” ํ˜„์žฌ๊นŒ์ง€ ๋ชจ๋“  ์ฑ„ํŒ…์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ +๋ฉ”์„ธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์‹œ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +```python +chat = response[0]['generated_text'] +chat.append( + {"role": "user", "content": "Wait, what's so wild about soup cans?"} +) +response = pipe(chat, max_new_tokens=512) +print(response[0]['generated_text'][-1]['content']) +``` + +์ดํ›„ ์‹คํ–‰์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค: + +```text +(laughs) Oh, you're killin' me, pal! You don't get it, do you? Warhol's soup cans are like, art, man! +It's like, he took something totally mundane, like a can of soup, and turned it into a masterpiece. It's +like, "Hey, look at me, I'm a can of soup, but I'm also a work of art!" +(sarcastically) Oh, yeah, real original, Andy. + +But, you know, back in the '60s, it was like, a big deal. People were all about challenging the +status quo, and Warhol was like, the king of that. He took the ordinary and made it extraordinary. +And, let me tell you, it was like, a real game-changer. I mean, who would've thought that a can of soup could be art? (laughs) + +But, hey, you're not alone, pal. I mean, I'm a robot, and even I don't get it. (winks) +But, hey, that's what makes art, art, right? (laughs) +``` + +์ด ํŠœํ† ๋ฆฌ์–ผ์˜ ํ›„๋ฐ˜๋ถ€์—์„œ๋Š” ์„ฑ๋Šฅ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ, +๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž์˜ ํ•„์š”์— ๋งž๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ ์„ ํƒ๊ณผ ๊ฐ™์€ ๊ตฌ์ฒด์ ์ธ ์ฃผ์ œ๋“ค์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +## ์ฑ„ํŒ… ๋ชจ๋ธ ๊ณ ๋ฅด๊ธฐ[[choosing-a-chat-model]] + +[Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending)๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. +์ฒ˜์Œ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ๋ชจ๋ธ์„ ์„ ํƒํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šธ์ง€ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”! ๋‘ ๊ฐ€์ง€๋งŒ ๋ช…์‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +- ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋Š” ์‹คํ–‰ ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •. +- ๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ ์ถœ๋ ฅ์˜ ํ’ˆ์งˆ. + +์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ์š”์†Œ๋“ค์€ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ํฐ ๋ชจ๋ธ์ผ์ˆ˜๋ก ๋” ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ํฌ๊ธฐ์˜ ๋ชจ๋ธ์ด๋ผ๋„ ์œ ์˜๋ฏธํ•œ ์ฐจ์ด๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +### ๋ชจ๋ธ์˜ ๋ช…์นญ๊ณผ ํฌ๊ธฐ[[size-and-model-naming]] + +๋ชจ๋ธ์˜ ํฌ๊ธฐ๋Š” ๋ชจ๋ธ ์ด๋ฆ„์— ์žˆ๋Š” ์ˆซ์ž๋กœ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, "8B" ๋˜๋Š” "70B"์™€ ๊ฐ™์€ ์ˆซ์ž๋Š” ๋ชจ๋ธ์˜ **ํŒŒ๋ผ๋ฏธํ„ฐ** ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +์–‘์žํ™”๋œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ํŒŒ๋ผ๋ฏธํ„ฐ ํ•˜๋‚˜๋‹น ์•ฝ 2๋ฐ”์ดํŠธ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ์˜ˆ์ƒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ 80์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ "8B" ๋ชจ๋ธ์€ 16GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๋ฉฐ, ์ถ”๊ฐ€์ ์ธ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์œ„ํ•œ ์•ฝ๊ฐ„์˜ ์—ฌ์œ ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” 3090์ด๋‚˜ 4090์™€ ๊ฐ™์€ 24GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ–์ถ˜ ํ•˜์ด์—”๋“œ GPU์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. + +์ผ๋ถ€ ์ฑ„ํŒ… ๋ชจ๋ธ์€ "Mixture of Experts" ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ํฌ๊ธฐ๋ฅผ "8x7B" ๋˜๋Š” "141B-A35B"์™€ ๊ฐ™์ด ๋‹ค๋ฅด๊ฒŒ ํ‘œ์‹œํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. +์ˆซ์ž๊ฐ€ ๋‹ค์†Œ ๋ชจํ˜ธํ•˜๋‹ค ๋А๊ปด์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ฒซ ๋ฒˆ์งธ ๊ฒฝ์šฐ์—๋Š” ์•ฝ 56์–ต(8x7) ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ๊ณ , +๋‘ ๋ฒˆ์งธ ๊ฒฝ์šฐ์—๋Š” ์•ฝ 141์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์–‘์žํ™”๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋‹น ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ 8๋น„ํŠธ, 4๋น„ํŠธ, ๋˜๋Š” ๊ทธ ์ดํ•˜๋กœ ์ค„์ด๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +์ด ์ฃผ์ œ์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜์˜ [๋ฉ”๋ชจ๋ฆฌ ๊ณ ๋ ค์‚ฌํ•ญ](#memory-considerations) ์ฑ•ํ„ฐ์—์„œ ๋” ์ž์„ธํžˆ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + +### ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ค ์ฑ„ํŒ… ๋ชจ๋ธ์ด ๊ฐ€์žฅ ์ข‹์„๊นŒ์š”?[[but-which-chat-model-is-best]] +๋ชจ๋ธ์˜ ํฌ๊ธฐ ์™ธ์—๋„ ๊ณ ๋ คํ•  ์ ์ด ๋งŽ์Šต๋‹ˆ๋‹ค. +์ด๋ฅผ ํ•œ๋ˆˆ์— ์‚ดํŽด๋ณด๋ ค๋ฉด **๋ฆฌ๋”๋ณด๋“œ**๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋ฆฌ๋”๋ณด๋“œ ๋‘ ๊ฐ€์ง€๋Š” [OpenLLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)์™€ [LMSys Chatbot Arena Leaderboard](https://chat.lmsys.org/?leaderboard)์ž…๋‹ˆ๋‹ค. +LMSys ๋ฆฌ๋”๋ณด๋“œ์—๋Š” ๋…์  ๋ชจ๋ธ๋„ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋‹ˆ, +`license` ์—ด์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ์„ ์„ ํƒํ•œ ํ›„ +[Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending)์—์„œ ๊ฒ€์ƒ‰ํ•ด ๋ณด์„ธ์š”. + +### ์ „๋ฌธ ๋ถ„์•ผ[[specialist-domains]] +์ผ๋ถ€ ๋ชจ๋ธ์€ ์˜๋ฃŒ ๋˜๋Š” ๋ฒ•๋ฅ  ํ…์ŠคํŠธ์™€ ๊ฐ™์€ ํŠน์ • ๋„๋ฉ”์ธ์ด๋‚˜ ๋น„์˜์–ด๊ถŒ ์–ธ์–ด์— ํŠนํ™”๋˜์–ด ์žˆ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ๋„๋ฉ”์ธ์—์„œ ์ž‘์—…ํ•  ๊ฒฝ์šฐ ํŠนํ™”๋œ ๋ชจ๋ธ์ด ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ํ•ญ์ƒ ๊ทธ๋Ÿด ๊ฒƒ์ด๋ผ ๋‹จ์ •ํ•˜๊ธฐ๋Š” ํž˜๋“ญ๋‹ˆ๋‹ค. +ํŠนํžˆ ๋ชจ๋ธ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ์˜ค๋ž˜๋œ ๋ชจ๋ธ์ธ ๊ฒฝ์šฐ, +์ตœ์‹  ๋ฒ”์šฉ ๋ชจ๋ธ์ด ๋” ๋›ฐ์–ด๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹คํ–‰ํžˆ๋„ [domain-specific leaderboards](https://huggingface.co/blog/leaderboard-medicalllm)๊ฐ€ ์ ์ฐจ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ์–ด, ํŠน์ • ๋„๋ฉ”์ธ์— ์ตœ๊ณ ์˜ ๋ชจ๋ธ์„ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + + +## ํŒŒ์ดํ”„๋ผ์ธ ๋‚ด๋ถ€๋Š” ์–ด๋–ป๊ฒŒ ๋˜์–ด์žˆ๋Š”๊ฐ€?[[what-happens-inside-the-pipeline]] +์œ„์˜ ๋น ๋ฅธ ์‹œ์ž‘์—์„œ๋Š” ๊ณ ์ˆ˜์ค€(High-Level) ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. +์ด๋Š” ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ์œ ์—ฐ์„ฑ์€ ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. +์ด์ œ ๋” ์ €์ˆ˜์ค€(Low-Level) ์ ‘๊ทผ ๋ฐฉ์‹์„ ํ†ตํ•ด ๋Œ€ํ™”์— ํฌํ•จ๋œ ๊ฐ ๋‹จ๊ณ„๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +์ฝ”๋“œ ์ƒ˜ํ”Œ๋กœ ์‹œ์ž‘ํ•œ ํ›„ ์ด๋ฅผ ๋ถ„์„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +# ์ž…๋ ฅ๊ฐ’์„ ์‚ฌ์ „์— ์ค€๋น„ํ•ด ๋†“์Šต๋‹ˆ๋‹ค +chat = [ + {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."}, + {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"} +] + +# 1: ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค +model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", torch_dtype=torch.bfloat16) +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") + +# 2: ์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ์— ์ ์šฉํ•ฉ๋‹ˆ๋‹ค +formatted_chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True) +print("Formatted chat:\n", formatted_chat) + +# 3: ์ฑ„ํŒ…์„ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค (๋ฐ”๋กœ ์ด์ „ ๊ณผ์ •์—์„œ tokenized=True๋กœ ์„ค์ •ํ•˜๋ฉด ํ•œ๊บผ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค) +inputs = tokenizer(formatted_chat, return_tensors="pt", add_special_tokens=False) +# ํ† ํฐํ™”๋œ ์ž…๋ ฅ๊ฐ’์„ ๋ชจ๋ธ์ด ์˜ฌ๋ผ์™€ ์žˆ๋Š” ๊ธฐ๊ธฐ(CPU/GPU)๋กœ ์˜ฎ๊น๋‹ˆ๋‹ค. +inputs = {key: tensor.to(model.device) for key, tensor in inputs.items()} +print("Tokenized inputs:\n", inputs) + +# 4: ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค +outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1) +print("Generated tokens:\n", outputs) + +# 5: ๋ชจ๋ธ์ด ์ถœ๋ ฅํ•œ ํ† ํฐ์„ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค +decoded_output = tokenizer.decode(outputs[0][inputs['input_ids'].size(1):], skip_special_tokens=True) +print("Decoded output:\n", decoded_output) +``` +์—ฌ๊ธฐ์—๋Š” ๊ฐ ๋ถ€๋ถ„์ด ์ž์ฒด ๋ฌธ์„œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์„ ๋งŒํผ ๋งŽ์€ ๋‚ด์šฉ์ด ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค! +๋„ˆ๋ฌด ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋„“์€ ๊ฐœ๋…์„ ๋‹ค๋ฃจ๊ณ , ์„ธ๋ถ€ ์‚ฌํ•ญ์€ ๋งํฌ๋œ ๋ฌธ์„œ์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. +์ฃผ์š” ๋‹จ๊ณ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +1. [๋ชจ๋ธ](https://huggingface.co/learn/nlp-course/en/chapter2/3)๊ณผ [ํ† ํฌ๋‚˜์ด์ €](https://huggingface.co/learn/nlp-course/en/chapter2/4?fw=pt)๋ฅผ Hugging Face Hub์—์„œ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. +2. ๋Œ€ํ™”๋Š” ํ† ํฌ๋‚˜์ด์ €์˜ [์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ](https://huggingface.co/docs/transformers/main/en/chat_templating)์„ ์‚ฌ์šฉํ•˜์—ฌ ์–‘์‹์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. +3. ๊ตฌ์„ฑ๋œ ์ฑ„ํŒ…์€ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [ํ† ํฐํ™”](https://huggingface.co/learn/nlp-course/en/chapter2/4)๋ฉ๋‹ˆ๋‹ค. +4. ๋ชจ๋ธ์—์„œ ์‘๋‹ต์„ [์ƒ์„ฑ](https://huggingface.co/docs/transformers/en/llm_tutorial)ํ•ฉ๋‹ˆ๋‹ค. +5. ๋ชจ๋ธ์ด ์ถœ๋ ฅํ•œ ํ† ํฐ์„ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. + +## ์„ฑ๋Šฅ, ๋ฉ”๋ชจ๋ฆฌ์™€ ํ•˜๋“œ์›จ์–ด[[performance-memory-and-hardware]] +์ด์ œ ๋Œ€๋ถ€๋ถ„์˜ ๋จธ์‹  ๋Ÿฌ๋‹ ์ž‘์—…์ด GPU์—์„œ ์‹คํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•„์‹ค ๊ฒ๋‹ˆ๋‹ค. +๋‹ค์†Œ ๋А๋ฆฌ๊ธฐ๋Š” ํ•ด๋„ CPU์—์„œ ์ฑ„ํŒ… ๋ชจ๋ธ์ด๋‚˜ ์–ธ์–ด ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๋ชจ๋ธ์„ GPU ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋†“์„ ์ˆ˜๋งŒ ์žˆ๋‹ค๋ฉด, GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +### ๋ฉ”๋ชจ๋ฆฌ ๊ณ ๋ ค์‚ฌํ•ญ[[memory-considerations]] + +๊ธฐ๋ณธ์ ์œผ๋กœ, [`TextGenerationPipeline`]์ด๋‚˜ [`AutoModelForCausalLM`]๊ณผ ๊ฐ™์€ +Hugging Face ํด๋ž˜์Šค๋Š” ๋ชจ๋ธ์„ `float32` ์ •๋ฐ€๋„(Precision)๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋‹น 4๋ฐ”์ดํŠธ(32๋น„ํŠธ)๋ฅผ ํ•„์š”๋กœ ํ•˜๋ฏ€๋กœ, +80์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ "8B" ๋ชจ๋ธ์€ ์•ฝ 32GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ์ด๋Š” ๋‚ญ๋น„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! +๋Œ€๋ถ€๋ถ„์˜ ์ตœ์‹  ์–ธ์–ด ๋ชจ๋ธ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋‹น 2๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” "bfloat16" ์ •๋ฐ€๋„(Precision)๋กœ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. +ํ•˜๋“œ์›จ์–ด๊ฐ€ ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ(Nvidia 30xx/Axxx ์ด์ƒ), +`torch_dtype` ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์œ„์™€ ๊ฐ™์ด `bfloat16` ์ •๋ฐ€๋„(Precision)๋กœ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ, 16๋น„ํŠธ๋ณด๋‹ค ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„(Precision)๋กœ ๋ชจ๋ธ์„ ์••์ถ•ํ•˜๋Š” +"์–‘์žํ™”(quantization)" ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด ๋ฐฉ๋ฒ•์€ ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์†์‹ค ์••์ถ•ํ•˜์—ฌ ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ 8๋น„ํŠธ, +4๋น„ํŠธ ๋˜๋Š” ๊ทธ ์ดํ•˜๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํŠนํžˆ 4๋น„ํŠธ์—์„œ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ด ๋ถ€์ •์ ์ธ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ, +๋” ํฌ๊ณ  ๊ฐ•๋ ฅํ•œ ์ฑ„ํŒ… ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด ์ด ๊ฐ™์€ ํŠธ๋ ˆ์ด๋“œ์˜คํ”„๋ฅผ ๊ฐ์ˆ˜ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด์ œ `bitsandbytes`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ์‹ค์ œ๋กœ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit +model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", quantization_config=quantization_config) +``` + +์œ„์˜ ์ž‘์—…์€ `pipeline` API์—๋„ ์ ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค: + +```python +from transformers import pipeline, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit +pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", model_kwargs={"quantization_config": quantization_config}) +``` + +`bitsandbytes` ์™ธ์—๋„ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. +์ž์„ธํ•œ ๋‚ด์šฉ์€ [Quantization guide](./quantization)๋ฅผ ์ฐธ์กฐํ•ด ์ฃผ์„ธ์š”. + + +### ์„ฑ๋Šฅ ๊ณ ๋ ค์‚ฌํ•ญ[[performance-considerations]] + + + +์–ธ์–ด ๋ชจ๋ธ ์„ฑ๋Šฅ๊ณผ ์ตœ์ ํ™”์— ๋Œ€ํ•œ ๋ณด๋‹ค ์ž์„ธํ•œ ๊ฐ€์ด๋“œ๋Š” [LLM Inference Optimization](./llm_optims)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + + + + +์ผ๋ฐ˜์ ์œผ๋กœ ๋” ํฐ ์ฑ„ํŒ… ๋ชจ๋ธ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ๋งŽ์ด ์š”๊ตฌํ•˜๊ณ , +์†๋„๋„ ๋А๋ ค์ง€๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋งํ•˜์ž๋ฉด, +์ฑ„ํŒ… ๋ชจ๋ธ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ์ปดํ“จํŒ… ํŒŒ์›Œ๋ณด๋‹ค **๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ**์ด ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ์ผ์œผํ‚ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. +์ด๋Š” ๋ชจ๋ธ์ด ํ† ํฐ์„ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ฝ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์ฑ„ํŒ… ๋ชจ๋ธ์—์„œ ์ดˆ๋‹น ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฐ ์ˆ˜๋Š” ๋ชจ๋ธ์ด ์œ„์น˜ํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜ ๋Œ€์—ญํญ์„ ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค. + +์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ๋ชจ๋ธ์ด bfloat16 ์ •๋ฐ€๋„(Precision)๋กœ ๋กœ๋“œ๋  ๋•Œ ์šฉ๋Ÿ‰์ด ์•ฝ 16GB์˜€์Šต๋‹ˆ๋‹ค. +์ด ๊ฒฝ์šฐ, ๋ชจ๋ธ์ด ์ƒ์„ฑํ•˜๋Š” ๊ฐ ํ† ํฐ๋งˆ๋‹ค 16GB๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ฝ์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. +์ด ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ์€ ์†Œ๋น„์ž์šฉ CPU์—์„œ๋Š” 20-100GB/sec, +์†Œ๋น„์ž์šฉ GPU๋‚˜ Intel Xeon, AMD Threadripper/Epyc, +์• ํ”Œ ์‹ค๋ฆฌ์ฝ˜๊ณผ ๊ฐ™์€ ํŠน์ˆ˜ CPU์—์„œ๋Š” 200-900GB/sec, +๋ฐ์ดํ„ฐ ์„ผํ„ฐ GPU์ธ Nvidia A100์ด๋‚˜ H100์—์„œ๋Š” ์ตœ๋Œ€ 2-3TB/sec์— ์ด๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ์ •๋ณด๋Š” ๊ฐ์ž ํ•˜๋“œ์›จ์–ด์—์„œ ์ƒ์„ฑ ์†๋„๋ฅผ ์˜ˆ์ƒํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ ํ…์ŠคํŠธ ์ƒ์„ฑ ์†๋„๋ฅผ ๊ฐœ์„ ํ•˜๋ ค๋ฉด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ฑฐ๋‚˜(์ฃผ๋กœ ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉ), +๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ์ด ๋” ๋†’์€ ํ•˜๋“œ์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด ๋Œ€์—ญํญ ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ๊ธ‰ ๊ธฐ์ˆ ๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค. +๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ [๋ณด์กฐ ์ƒ์„ฑ](https://huggingface.co/blog/assisted-generation), "์ถ”์ธก ์ƒ˜ํ”Œ๋ง"์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. +์ด ๊ธฐ์ˆ ์€ ์ข…์ข… ๋” ์ž‘์€ "์ดˆ์•ˆ ๋ชจ๋ธ"์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฏธ๋ž˜ ํ† ํฐ์„ ํ•œ ๋ฒˆ์— ์ถ”์ธกํ•œ ํ›„, +์ฑ„ํŒ… ๋ชจ๋ธ๋กœ ์ƒ์„ฑ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. +๋งŒ์•ฝ ์ฑ„ํŒ… ๋ชจ๋ธ์ด ์ถ”์ธก์„ ํ™•์ธํ•˜๋ฉด, ํ•œ ๋ฒˆ์˜ ์ˆœ์ „ํŒŒ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ† ํฐ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด +๋ณ‘๋ชฉ ํ˜„์ƒ์ด ํฌ๊ฒŒ ์ค„์–ด๋“ค๊ณ  ์ƒ์„ฑ ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ, "Mixture of Experts" (MoE) ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ๋„ ์งš๊ณ  ๋„˜์–ด๊ฐ€ ๋ณด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. +Mixtral, Qwen-MoE, DBRX์™€ ๊ฐ™์€ ์ธ๊ธฐ ์žˆ๋Š” ์ฑ„ํŒ… ๋ชจ๋ธ์ด ๋ฐ”๋กœ MoE ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +์ด ๋ชจ๋ธ๋“ค์€ ํ† ํฐ์„ ์ƒ์„ฑํ•  ๋•Œ ๋ชจ๋“  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +์ด๋กœ ์ธํ•ด MoE ๋ชจ๋ธ์€ ์ „์ฒด ํฌ๊ธฐ๊ฐ€ ์ƒ๋‹นํžˆ ํด ์ˆ˜ ์žˆ์ง€๋งŒ, +์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ์€ ๋‚ฎ์€ ํŽธ์ž…๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ๋™์ผํ•œ ํฌ๊ธฐ์˜ ์ผ๋ฐ˜ "์กฐ๋ฐ€ํ•œ(Dense)" ๋ชจ๋ธ๋ณด๋‹ค ๋ช‡ ๋ฐฐ ๋น ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๋ณด์กฐ ์ƒ์„ฑ๊ณผ ๊ฐ™์€ ๊ธฐ์ˆ ์€ MoE ๋ชจ๋ธ์—์„œ ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ƒˆ๋กœ์šด ์ถ”์ธก๋œ ํ† ํฐ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ๋” ๋งŽ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ™œ์„ฑํ™”๋˜๊ธฐ ๋•Œ๋ฌธ์—, +MoE ์•„ํ‚คํ…์ฒ˜๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์†๋„ ์ด์ ์ด ์ƒ์‡„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/custom_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/custom_models.md new file mode 100644 index 0000000000000000000000000000000000000000..1e76608b1520e0678e082a84a5e227a603d05748 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/custom_models.md @@ -0,0 +1,346 @@ + + +# ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ ๊ณต์œ ํ•˜๊ธฐ[[sharing-custom-models]] + +๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +๋ชจ๋“  ๋ชจ๋ธ์€ ์ถ”์ƒํ™” ์—†์ด ์ €์žฅ์†Œ์˜ ์ง€์ •๋œ ํ•˜์œ„ ํด๋”์— ์™„์ „ํžˆ ์ฝ”๋”ฉ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ์†์‰ฝ๊ฒŒ ๋ชจ๋ธ๋ง ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ๋” ์‰ฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” Transformers ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ๊ณผ ๊ตฌ์„ฑ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ +๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์—†๋Š” ๊ฒฝ์šฐ์—๋„ ๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก (์˜์กด์„ฑ๊ณผ ํ•จ๊ป˜) ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[timm ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ](https://github.com/rwightman/pytorch-image-models)์˜ ResNet ํด๋ž˜์Šค๋ฅผ [`PreTrainedModel`]๋กœ ๋ž˜ํ•‘ํ•œ ResNet ๋ชจ๋ธ์„ ์˜ˆ๋กœ ๋ชจ๋“  ๊ฒƒ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์„ฑ ์ž‘์„ฑํ•˜๊ธฐ[[writing-a-custom-configuration]] + +๋ชจ๋ธ์— ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ๋จผ์ € ๊ตฌ์„ฑ์„ ์ž‘์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +๋ชจ๋ธ์˜ `configuration`์€ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“  ์ค‘์š”ํ•œ ๊ฒƒ๋“ค์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. +๋‹ค์Œ ์„น์…˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ๋ชจ๋ธ์€ `config`๋ฅผ ์‚ฌ์šฉํ•ด์„œ๋งŒ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์™„๋ฒฝํ•œ ๊ตฌ์„ฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์‹œ์—์„œ๋Š” ResNet ํด๋ž˜์Šค์˜ ์ธ์ˆ˜(argument)๋ฅผ ์กฐ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +๋‹ค๋ฅธ ๊ตฌ์„ฑ์€ ๊ฐ€๋Šฅํ•œ ResNet ์ค‘ ๋‹ค๋ฅธ ์œ ํ˜•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ช‡ ๊ฐ€์ง€ ์œ ํšจ์„ฑ์„ ํ™•์ธํ•œ ํ›„ ํ•ด๋‹น ์ธ์ˆ˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + +```python +from transformers import PretrainedConfig +from typing import List + + +class ResnetConfig(PretrainedConfig): + model_type = "resnet" + + def __init__( + self, + block_type="bottleneck", + layers: list[int] = [3, 4, 6, 3], + num_classes: int = 1000, + input_channels: int = 3, + cardinality: int = 1, + base_width: int = 64, + stem_width: int = 64, + stem_type: str = "", + avg_down: bool = False, + **kwargs, + ): + if block_type not in ["basic", "bottleneck"]: + raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.") + if stem_type not in ["", "deep", "deep-tiered"]: + raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.") + + self.block_type = block_type + self.layers = layers + self.num_classes = num_classes + self.input_channels = input_channels + self.cardinality = cardinality + self.base_width = base_width + self.stem_width = stem_width + self.stem_type = stem_type + self.avg_down = avg_down + super().__init__(**kwargs) +``` + +์‚ฌ์šฉ์ž ์ •์˜ `configuration`์„ ์ž‘์„ฑํ•  ๋•Œ ๊ธฐ์–ตํ•ด์•ผ ํ•  ์„ธ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: +- `PretrainedConfig`์„ ์ƒ์†ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- `PretrainedConfig`์˜ `__init__`์€ ๋ชจ๋“  kwargs๋ฅผ ํ—ˆ์šฉํ•ด์•ผ ํ•˜๊ณ , +- ์ด๋Ÿฌํ•œ `kwargs`๋Š” ์ƒ์œ„ ํด๋ž˜์Šค `__init__`์— ์ „๋‹ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ƒ์†์€ ๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ์ ์œผ๋กœ๋ถ€ํ„ฐ ๋น„๋กฏ๋˜๋Š” ๋‘ ๊ฐ€์ง€ ์ œ์•ฝ ์กฐ๊ฑด์€ `PretrainedConfig`์— ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ํ•„๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. +`from_pretrained` ๋ฉ”์„œ๋“œ๋กœ ๊ตฌ์„ฑ์„ ๋‹ค์‹œ ๋กœ๋“œํ•  ๋•Œ ํ•ด๋‹น ํ•„๋“œ๋Š” ๊ตฌ์„ฑ์—์„œ ์ˆ˜๋ฝํ•œ ํ›„ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๋ณด๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ auto ํด๋ž˜์Šค์— ๋“ฑ๋กํ•˜์ง€ ์•Š๋Š” ํ•œ, `configuration`์—์„œ `model_type`์„ ์ •์˜(์—ฌ๊ธฐ์„œ `model_type="resnet"`)ํ•˜๋Š” ๊ฒƒ์€ ํ•„์ˆ˜ ์‚ฌํ•ญ์ด ์•„๋‹™๋‹ˆ๋‹ค (๋งˆ์ง€๋ง‰ ์„น์…˜ ์ฐธ์กฐ). + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค๋ฅธ ๋ชจ๋ธ ๊ตฌ์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ณ  ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹ค์Œ์€ resnet50d ๊ตฌ์„ฑ์„ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d_config.save_pretrained("custom-resnet") +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด `custom-resnet` ํด๋” ์•ˆ์— `config.json`์ด๋ผ๋Š” ํŒŒ์ผ์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ `from_pretrained` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์„ฑ์„ ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +resnet50d_config = ResnetConfig.from_pretrained("custom-resnet") +``` + +๊ตฌ์„ฑ์„ Hub์— ์ง์ ‘ ์—…๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด [`PretrainedConfig`] ํด๋ž˜์Šค์˜ [`~PretrainedConfig.push_to_hub`]์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ ์ž‘์„ฑํ•˜๊ธฐ[[writing-a-custom-model]] + +์ด์ œ ResNet ๊ตฌ์„ฑ์ด ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์‹ค์ œ๋กœ๋Š” ๋‘ ๊ฐœ๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ์ด๋ฏธ์ง€ ๋ฐฐ์น˜์—์„œ hidden features๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ([`BertModel`]๊ณผ ๊ฐ™์ด), ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์ ํ•ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค([`BertForSequenceClassification`]๊ณผ ๊ฐ™์ด). + +์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์ด ์˜ˆ์ œ์—์„œ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์˜ ๋А์Šจํ•œ ๋ž˜ํผ(loose wrapper)๋งŒ ์ž‘์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๋ธ”๋ก ์œ ํ˜•๊ณผ ์‹ค์ œ ๋ธ”๋ก ํด๋ž˜์Šค ๊ฐ„์˜ ๋งคํ•‘ ์ž‘์—…๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ `ResNet` ํด๋ž˜์Šค๋กœ ์ „๋‹ฌ๋˜์–ด `configuration`์„ ํ†ตํ•ด ๋ชจ๋ธ์ด ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค: + +```py +from transformers import PreTrainedModel +from timm.models.resnet import BasicBlock, Bottleneck, ResNet +from .configuration_resnet import ResnetConfig + + +BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} + + +class ResnetModel(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor): + return self.model.forward_features(tensor) +``` + +์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” forward ๋ฉ”์†Œ๋“œ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```py +import torch + + +class ResnetModelForImageClassification(PreTrainedModel): + config_class = ResnetConfig + + def __init__(self, config): + super().__init__(config) + block_layer = BLOCK_MAPPING[config.block_type] + self.model = ResNet( + block_layer, + config.layers, + num_classes=config.num_classes, + in_chans=config.input_channels, + cardinality=config.cardinality, + base_width=config.base_width, + stem_width=config.stem_width, + stem_type=config.stem_type, + avg_down=config.avg_down, + ) + + def forward(self, tensor, labels=None): + logits = self.model(tensor) + if labels is not None: + loss = torch.nn.functional.cross_entropy(logits, labels) + return {"loss": loss, "logits": logits} + return {"logits": logits} +``` + +๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ `PreTrainedModel`๋ฅผ ์ƒ์†๋ฐ›๊ณ , `config`๋ฅผ ํ†ตํ•ด ์ƒ์œ„ ํด๋ž˜์Šค ์ดˆ๊ธฐํ™”๋ฅผ ํ˜ธ์ถœํ•˜๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์„ธ์š” (์ผ๋ฐ˜์ ์ธ `torch.nn.Module`์„ ์ž‘์„ฑํ•  ๋•Œ์™€ ๋น„์Šทํ•จ). +๋ชจ๋ธ์„ auto ํด๋ž˜์Šค์— ๋“ฑ๋กํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” `config_class`๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค (๋งˆ์ง€๋ง‰ ์„น์…˜ ์ฐธ์กฐ). + + + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋ธ๊ณผ ๊ต‰์žฅํžˆ ์œ ์‚ฌํ•˜๋‹ค๋ฉด, ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ๊ตฌ์„ฑ์„ ์ฐธ์กฐํ•ด ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +์›ํ•˜๋Š” ๊ฒƒ์„ ๋ชจ๋ธ์ด ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, `ResnetModelForImageClassification`์—์„œ ํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ +๋ ˆ์ด๋ธ”์„ ํ†ต๊ณผ์‹œ์ผฐ์„ ๋•Œ ์†์‹ค๊ณผ ํ•จ๊ป˜ ์‚ฌ์ „ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด [`Trainer`] ํด๋ž˜์Šค ๋‚ด์—์„œ ์ง์ ‘ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. +์ž์‹ ๋งŒ์˜ ํ•™์Šต ๋ฃจํ”„ ๋˜๋Š” ๋‹ค๋ฅธ ํ•™์Šต ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ณ„ํš์ด๋ผ๋ฉด ๋‹ค๋ฅธ ์ถœ๋ ฅ ํ˜•์‹์„ ์‚ฌ์šฉํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. + +์ด์ œ ๋ชจ๋ธ ํด๋ž˜์Šค๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +resnet50d = ResnetModelForImageClassification(resnet50d_config) +``` + +๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, [`~PreTrainedModel.save_pretrained`]๋˜๋Š” [`~PreTrainedModel.push_to_hub`]์ฒ˜๋Ÿผ [`PreTrainedModel`]์— ์†ํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹ค์Œ ์„น์…˜์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋ธ ์ฝ”๋“œ์™€ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +๋จผ์ €, ๋ชจ๋ธ ๋‚ด๋ถ€์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +์ด ์˜ˆ์ œ๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š”, ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ์„ ์ž์‹ ๋งŒ์˜ ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต์‹œํ‚ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „ ํ›ˆ๋ จ๋œ resnet50d๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +์•„๋ž˜ ๋ชจ๋ธ์€ resnet50d์˜ ๋ž˜ํผ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ€์ค‘์น˜๋ฅผ ์‰ฝ๊ฒŒ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +```py +import timm + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +์ด์ œ [`~PreTrainedModel.save_pretrained`] ๋˜๋Š” [`~PreTrainedModel.push_to_hub`]๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ชจ๋ธ ์ฝ”๋“œ๊ฐ€ ์ €์žฅ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค. + +## Hub๋กœ ์ฝ”๋“œ ์—…๋กœ๋“œํ•˜๊ธฐ[[sending-the-code-to-the-hub]] + + + +์ด API๋Š” ์‹คํ—˜์ ์ด๋ฉฐ ๋‹ค์Œ ๋ฆด๋ฆฌ์Šค์—์„œ ์•ฝ๊ฐ„์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๋จผ์ € ๋ชจ๋ธ์ด `.py` ํŒŒ์ผ์— ์™„์ „ํžˆ ์ •์˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. +๋ชจ๋“  ํŒŒ์ผ์ด ๋™์ผํ•œ ์ž‘์—… ๊ฒฝ๋กœ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋Œ€๊ฒฝ๋กœ ์ž„ํฌํŠธ(relative import)์— ์˜์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (transformers์—์„œ๋Š” ์ด ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ•˜์œ„ ๋ชจ๋“ˆ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค). +์ด ์˜ˆ์‹œ์—์„œ๋Š” ์ž‘์—… ๊ฒฝ๋กœ ์•ˆ์˜ `resnet_model`์—์„œ `modeling_resnet.py` ํŒŒ์ผ๊ณผ `configuration_resnet.py` ํŒŒ์ผ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. +๊ตฌ์„ฑ ํŒŒ์ผ์—๋Š” `ResnetConfig`์— ๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๊ณ  ๋ชจ๋ธ๋ง ํŒŒ์ผ์—๋Š” `ResnetModel` ๋ฐ `ResnetModelForImageClassification`์— ๋Œ€ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +. +โ””โ”€โ”€ resnet_model + โ”œโ”€โ”€ __init__.py + โ”œโ”€โ”€ configuration_resnet.py + โ””โ”€โ”€ modeling_resnet.py +``` + +Python์ด `resnet_model`์„ ๋ชจ๋“ˆ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ง€ํ•˜๋Š” ๋ชฉ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— `__init__.py`๋Š” ๋น„์–ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋ชจ๋ธ๋ง ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒฝ์šฐ, +๋ชจ๋“  ํŒŒ์ผ ์ƒ๋‹จ์— ์žˆ๋Š” ์ƒ๋Œ€ ๊ฒฝ๋กœ ์ž„ํฌํŠธ(relative import) ๋ถ€๋ถ„์„ `transformers` ํŒจํ‚ค์ง€์—์„œ ์ž„ํฌํŠธ ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +๊ธฐ์กด ๊ตฌ์„ฑ์ด๋‚˜ ๋ชจ๋ธ์„ ์žฌ์‚ฌ์šฉ(๋˜๋Š” ์„œ๋ธŒ ํด๋ž˜์Šคํ™”)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค: +๋จผ์ €, ์ƒˆ๋กœ ๋งŒ๋“  ํŒŒ์ผ์— ResNet ๋ชจ๋ธ๊ณผ ๊ตฌ์„ฑ์„ ์ž„ํฌํŠธํ•ฉ๋‹ˆ๋‹ค: + +```py +from resnet_model.configuration_resnet import ResnetConfig +from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification +``` + +๋‹ค์Œ์œผ๋กœ `save_pretrained` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฝ”๋“œ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ณ , +๋ณต์‚ฌํ•œ ํŒŒ์ผ์„ Auto ํด๋ž˜์Šค๋กœ ๋“ฑ๋กํ•˜๊ณ (๋ชจ๋ธ์ธ ๊ฒฝ์šฐ) ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: + +```py +ResnetConfig.register_for_auto_class() +ResnetModel.register_for_auto_class("AutoModel") +ResnetModelForImageClassification.register_for_auto_class("AutoModelForImageClassification") +``` + +`configuration`์— ๋Œ€ํ•œ auto ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ(`configuration` ๊ด€๋ จ auto ํด๋ž˜์Šค๋Š” AutoConfig ํด๋ž˜์Šค ํ•˜๋‚˜๋งŒ ์žˆ์Œ), ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์‚ฌ์šฉ์ž ์ง€์ • ๋ชจ๋ธ์€ ๋‹ค์–‘ํ•œ ์ž‘์—…์— ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ชจ๋ธ์— ๋งž๋Š” auto ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ์œผ๋กœ, ์ด์ „์— ์ž‘์—…ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ๊ณผ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค: + +```py +resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) +resnet50d = ResnetModelForImageClassification(resnet50d_config) + +pretrained_model = timm.create_model("resnet50d", pretrained=True) +resnet50d.model.load_state_dict(pretrained_model.state_dict()) +``` + +์ด์ œ ๋ชจ๋ธ์„ Hub๋กœ ์—…๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๊ทธ์ธ ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•˜์„ธ์š”. +ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +hf auth login +``` + +์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```py +from huggingface_hub import notebook_login + +notebook_login() +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋ ‡๊ฒŒ ์ž์‹ ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค(๋˜๋Š” ์ž์‹ ์ด ์†ํ•œ ์กฐ์ง)์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +```py +resnet50d.push_to_hub("custom-resnet50d") +``` + +On top of the modeling weights and the configuration in json format, this also copied the modeling and +configuration `.py` files in the folder `custom-resnet50d` and uploaded the result to the Hub. You can check the result +in this [model repo](https://huggingface.co/sgugger/custom-resnet50d). +json ํ˜•์‹์˜ ๋ชจ๋ธ๋ง ๊ฐ€์ค‘์น˜์™€ ๊ตฌ์„ฑ ์™ธ์—๋„ `custom-resnet50d` ํด๋” ์•ˆ์˜ ๋ชจ๋ธ๋ง๊ณผ ๊ตฌ์„ฑ `.py` ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜ํ•ด Hub์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. +[๋ชจ๋ธ ์ €์žฅ์†Œ](https://huggingface.co/sgugger/custom-resnet50d)์—์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[sharing tutorial](model_sharing) ๋ฌธ์„œ์˜ `push_to_hub` ๋ฉ”์†Œ๋“œ์—์„œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ๋กœ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ[[using-a-model-with-custom-code]] + +auto ํด๋ž˜์Šค์™€ `from_pretrained` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ง€์ • ์ฝ”๋“œ ํŒŒ์ผ๊ณผ ํ•จ๊ป˜ ๋ชจ๋“  ๊ตฌ์„ฑ, ๋ชจ๋ธ, ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +Hub์— ์—…๋กœ๋“œ๋œ ๋ชจ๋“  ํŒŒ์ผ ๋ฐ ์ฝ”๋“œ๋Š” ๋ฉœ์›จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ๋˜์ง€๋งŒ (์ž์„ธํ•œ ๋‚ด์šฉ์€ [Hub ๋ณด์•ˆ](https://huggingface.co/docs/hub/security#malware-scanning) ์„ค๋ช… ์ฐธ์กฐ), +์ž์‹ ์˜ ์ปดํ“จํ„ฐ์—์„œ ๋ชจ๋ธ ์ฝ”๋“œ์™€ ์ž‘์„ฑ์ž๊ฐ€ ์•…์„ฑ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด `trust_remote_code=True`๋กœ ์„ค์ •ํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForImageClassification + +model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True) +``` + +๋ชจ๋ธ ์ž‘์„ฑ์ž๊ฐ€ ์•…์˜์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์ ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด, ์ปค๋ฐ‹ ํ•ด์‹œ(commit hash)๋ฅผ `revision`์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค (๋ชจ๋ธ ์ž‘์„ฑ์ž๋ฅผ ์™„์ „ํžˆ ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ). + +```py +commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292" +model = AutoModelForImageClassification.from_pretrained( + "sgugger/custom-resnet50d", trust_remote_code=True, revision=commit_hash +) +``` + +Hub์—์„œ ๋ชจ๋ธ ์ €์žฅ์†Œ์˜ ์ปค๋ฐ‹ ๊ธฐ๋ก์„ ์ฐพ์•„๋ณผ ๋•Œ, ๋ชจ๋“  ์ปค๋ฐ‹์˜ ์ปค๋ฐ‹ ํ•ด์‹œ๋ฅผ ์‰ฝ๊ฒŒ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ๋กœ ๋งŒ๋“  ๋ชจ๋ธ์„ auto ํด๋ž˜์Šค๋กœ ๋“ฑ๋กํ•˜๊ธฐ[[registering-a-model-with-custom-code-to-the-auto-classes]] + +๐Ÿค— Transformers๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ์„ auto ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž„ํฌํŠธํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Š” Hub๋กœ ์ฝ”๋“œ๋ฅผ ์—…๋กœ๋“œํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค (Hub์—์„œ ์ž๋™์ ์œผ๋กœ ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ฐ˜๋Œ€). + +๊ตฌ์„ฑ์— ๊ธฐ์กด ๋ชจ๋ธ ์œ ํ˜•๊ณผ ๋‹ค๋ฅธ `model_type` ์†์„ฑ์ด ์žˆ๊ณ  ๋ชจ๋ธ ํด๋ž˜์Šค์— ์˜ฌ๋ฐ”๋ฅธ `config_class` ์†์„ฑ์ด ์žˆ๋Š” ํ•œ, +๋‹ค์Œ๊ณผ ๊ฐ™์ด auto ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from transformers import AutoConfig, AutoModel, AutoModelForImageClassification + +AutoConfig.register("resnet", ResnetConfig) +AutoModel.register(ResnetConfig, ResnetModel) +AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) +``` + +์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์„ฑ์„ [`AutoConfig`]์— ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์„ฑ์˜ `model_type`๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๋˜ํ•œ, ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ์„ auto ํด๋ž˜์Šค์— ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ํ•ด๋‹น ๋ชจ๋ธ์˜ `config_class`์™€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/debugging.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/debugging.md new file mode 100644 index 0000000000000000000000000000000000000000..24b2c7b04b509d9301003a3911dbb0b0e324eedf --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/debugging.md @@ -0,0 +1,306 @@ + + +# ๋””๋ฒ„๊น… [[debugging]] + +## Multi-GPU ๋„คํŠธ์›Œํฌ ๋ฌธ์ œ ๋””๋ฒ„๊ทธ [[multigpu-network-issues-debug]] + +`DistributedDataParallel` ๋ฐ ๋‹ค์ค‘ GPU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จํ•˜๊ฑฐ๋‚˜ ์ถ”๋ก ํ•  ๋•Œ, ํ”„๋กœ์„ธ์Šค ๋ฐ/๋˜๋Š” ๋…ธ๋“œ ๊ฐ„์˜ ์ƒํ˜ธ ํ†ต์‹  ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ๋ฌธ์ œ๋ฅผ ์ง„๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +wget https://raw.githubusercontent.com/huggingface/transformers/main/scripts/distributed/torch-distributed-gpu-test.py +``` + +์˜ˆ๋ฅผ ๋“ค์–ด, 2๊ฐœ์˜ GPU๊ฐ€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์„ธ์š”: + +```bash +python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` +๋‘ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์„œ๋กœ ํ†ต์‹ ํ•˜๊ณ  GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ๊ฐ "OK" ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. + +๋” ๋งŽ์€ GPU ๋˜๋Š” ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ ์Šคํฌ๋ฆฝํŠธ์˜ ์ธ์ˆ˜๋ฅผ ์กฐ์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์ง„๋‹จ ์Šคํฌ๋ฆฝํŠธ ๋‚ด์—์„œ ๋” ๋งŽ์€ ์„ธ๋ถ€ ์ •๋ณด์™€ SLURM ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ ˆ์‹œํ”ผ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ถ”๊ฐ€์ ์ธ ๋””๋ฒ„๊ทธ ์ˆ˜์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด `NCCL_DEBUG=INFO` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```bash +NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด NCCL ๊ด€๋ จ ๋””๋ฒ„๊ทธ ์ •๋ณด๊ฐ€ ๋งŽ์ด ์ถœ๋ ฅ๋˜๋ฉฐ, ๋ฌธ์ œ๊ฐ€ ๋ณด๊ณ ๋œ ๊ฒฝ์šฐ์—๋Š” ์ธํ„ฐ๋„ท์—์„œ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ์ถœ๋ ฅ์„ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ ๋กœ๊ทธ ํŒŒ์ผ์„ ์ด์Šˆ์— ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## ์–ธ๋”ํ”Œ๋กœ ๋ฐ ์˜ค๋ฒ„ํ”Œ๋กœ ๊ฐ์ง€ [[underflow-and-overflow-detection]] + + + + +์ด ๊ธฐ๋Šฅ์€ ํ˜„์žฌ PyTorch์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + + +๋‹ค์ค‘ GPU ํ›ˆ๋ จ์„ ์œ„ํ•ด์„œ๋Š” DDP (`torch.distributed.launch`)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + + + + + +์ด ๊ธฐ๋Šฅ์€ `nn.Module`์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +`loss=NaN`์ด ๋‚˜ํƒ€๋‚˜๊ฑฐ๋‚˜ ๋ชจ๋ธ์ด `inf` ๋˜๋Š” `nan`์œผ๋กœ ์ธํ•ด ๋‹ค๋ฅธ ์ด์ƒํ•œ ๋™์ž‘์„ ํ•˜๋Š” ๊ฒฝ์šฐ, ์–ธ๋”ํ”Œ๋กœ ๋˜๋Š” ์˜ค๋ฒ„ํ”Œ๋กœ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐœ์ƒ ์œ„์น˜์™€ ๊ทธ ์›์ธ์„ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ์ด๋ฅผ ์ž๋™์œผ๋กœ ๊ฐ์ง€ํ•˜๋Š” ํŠน์ˆ˜ ๋ชจ๋“ˆ์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ์‰ฝ๊ฒŒ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ์„ ๊ธฐ์กด์˜ ๋ช…๋ น์ค„ ์ธ์ˆ˜์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +```bash +--debug underflow_overflow +``` +๋˜๋Š” [`TrainingArguments`] ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ `debug="underflow_overflow"`๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +์ž์ฒด ํ›ˆ๋ จ ๋ฃจํ”„๋‚˜ ๋‹ค๋ฅธ Trainer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from transformers.debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model) +``` + +[`~debug_utils.DebugUnderflowOverflow`]๋Š” ๋ชจ๋ธ์— ํ›„ํฌ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๊ฐ forward ํ˜ธ์ถœ ์งํ›„์— ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ๋ณ€์ˆ˜ ๋ฐ ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™”๋‚˜ ๊ฐ€์ค‘์น˜์˜ ์ตœ์†Œํ•œ ํ•˜๋‚˜์˜ ์š”์†Œ์—์„œ `inf` ๋˜๋Š” `nan`์ด ๊ฐ์ง€๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์–ด์„คํŠธ๋˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณด๊ณ ์„œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. (์ด ์˜ˆ์ œ๋Š” fp16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„์—์„œ `google/mt5-small`์—์„œ ์บก์ฒ˜๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค): + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata + encoder.block.1.layer.1.DenseReluDense.dropout Dropout +0.00e+00 2.57e+02 input[0] +0.00e+00 2.85e+02 output +[...] + encoder.block.2.layer.0 T5LayerSelfAttention +6.78e-04 3.15e+03 input[0] +2.65e-04 3.42e+03 output[0] + None output[1] +2.25e-01 1.00e+04 output[2] + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.dropout Dropout +0.00e+00 8.76e+03 input[0] +0.00e+00 9.74e+03 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +์˜ˆ์ œ ์ถœ๋ ฅ์€ ๊ฐ„๋žต์„ฑ์„ ์œ„ํ•ด ์ค‘๊ฐ„ ๋ถ€๋ถ„์ด ์ž˜๋ ค ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‘ ๋ฒˆ์งธ ์—ด์€ ์ ˆ๋Œ€์ ์œผ๋กœ ๊ฐ€์žฅ ํฐ ์š”์†Œ์˜ ๊ฐ’์ด๋ฉฐ, ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ๋ช‡ ๊ฐœ์˜ ํ”„๋ ˆ์ž„์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด `1e4` ๋ฒ”์œ„์— ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํ›ˆ๋ จ์€ `fp16` ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ์ˆ˜ํ–‰๋  ๋•Œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค (`fp16`์—์„œ `inf` ์ด์ „์˜ ๊ฐ€์žฅ ํฐ ์ˆซ์ž๋Š” `64e3`์ž…๋‹ˆ๋‹ค). `fp16` ์•„๋ž˜์—์„œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ™œ์„ฑํ™”๋Š” `1e4`๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด `1e4 * 1e4 = 1e8`์ด๊ธฐ ๋•Œ๋ฌธ์— ํฐ ํ™œ์„ฑํ™”์™€์˜ ํ–‰๋ ฌ ๊ณฑ์€ ์ˆ˜์น˜์ ์ธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์กฐ๊ฑด์œผ๋กœ ์ด์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ถ”์ ์˜ ๋งจ ์ฒ˜์Œ์—์„œ ์–ด๋А ๋ฐฐ์น˜ ๋ฒˆํ˜ธ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (์—ฌ๊ธฐ์„œ `Detected inf/nan during batch_number=0`์€ ๋ฌธ์ œ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์น˜์—์„œ ๋ฐœ์ƒํ–ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค). + +๊ฐ ๋ณด๊ณ ๋œ ํ”„๋ ˆ์ž„์€ ํ•ด๋‹น ํ”„๋ ˆ์ž„์ด ๋ณด๊ณ ํ•˜๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์™„์ „ํ•œ ํ•ญ๋ชฉ์„ ์„ ์–ธํ•˜๋ฉฐ, ์ด ํ”„๋ ˆ์ž„๋งŒ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +``` + encoder.block.2.layer.1.layer_norm T5LayerNorm +8.69e-02 4.18e-01 weight +2.65e-04 3.42e+03 input[0] +1.79e-06 4.65e+00 output +``` + +์—ฌ๊ธฐ์„œ `encoder.block.2.layer.1.layer_norm`์€ ์ธ์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ๋ธ”๋ก์˜ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด์–ด์— ๋Œ€ํ•œ ๋ ˆ์ด์–ด ์ •๊ทœํ™”๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, `forward`์˜ ํŠน์ • ํ˜ธ์ถœ์€ `T5LayerNorm`์ž…๋‹ˆ๋‹ค. + +์ด ๋ณด๊ณ ์„œ์˜ ๋งˆ์ง€๋ง‰ ๋ช‡ ๊ฐœ ํ”„๋ ˆ์ž„์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +``` +Detected inf/nan during batch_number=0 +Last 21 forward frames: +abs min abs max metadata +[...] + encoder.block.2.layer.1.DenseReluDense.wi_0 Linear +2.17e-07 4.50e+00 weight +1.79e-06 4.65e+00 input[0] +2.68e-06 3.70e+01 output + encoder.block.2.layer.1.DenseReluDense.wi_1 Linear +8.08e-07 2.66e+01 weight +1.79e-06 4.65e+00 input[0] +1.27e-04 2.37e+02 output + encoder.block.2.layer.1.DenseReluDense.wo Linear +1.01e-06 6.44e+00 weight +0.00e+00 9.74e+03 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense +1.79e-06 4.65e+00 input[0] +3.18e-04 6.27e+04 output + encoder.block.2.layer.1.dropout Dropout +3.18e-04 6.27e+04 input[0] +0.00e+00 inf output +``` + +๋งˆ์ง€๋ง‰ ํ”„๋ ˆ์ž„์€ `Dropout.forward` ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋ณด๊ณ ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์€ ์œ ์ผํ•œ ์ž…๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ์€ ์œ ์ผํ•œ ์ถœ๋ ฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๊ฐ€ `DenseReluDense` ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ `dropout` ์†์„ฑ์—์„œ ํ˜ธ์ถœ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด์–ด์˜ ๋‘ ๋ฒˆ์งธ ๋ธ”๋ก์—์„œ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์น˜ ์ค‘์— ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์ ˆ๋Œ€์ ์œผ๋กœ ๊ฐ€์žฅ ํฐ ์ž…๋ ฅ ์š”์†Œ๋Š” `6.27e+04`์ด๊ณ  ์ถœ๋ ฅ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ `inf`์ž…๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์—์„œ๋Š” `T5DenseGatedGeluDense.forward`๊ฐ€ ์ถœ๋ ฅ ํ™œ์„ฑํ™”๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ์ ˆ๋Œ€์ ์œผ๋กœ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ์•ฝ 62.7K์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ fp16์˜ ์ตœ๋Œ€ ์ œํ•œ์ธ 64K์— ๋งค์šฐ ๊ทผ์ ‘ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ํ”„๋ ˆ์ž„์—์„œ๋Š” ์ผ๋ถ€ ์š”์†Œ๋ฅผ 0์œผ๋กœ ๋งŒ๋“  ํ›„ ๊ฐ€์ค‘์น˜๋ฅผ ์žฌ์ •๊ทœํ™”ํ•˜๋Š” `Dropout`์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ ˆ๋Œ€ ์ตœ๋Œ€๊ฐ’์ด 64K๋ฅผ ์ดˆ๊ณผํ•˜๊ณ  ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ(`inf`)๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. + +๋ณด์‹œ๋‹ค์‹œํ”ผ, fp16 ์ˆซ์ž์˜ ๊ฒฝ์šฐ ์ˆซ์ž๊ฐ€ ๋งค์šฐ ์ปค์งˆ ๋•Œ ์ด์ „ ํ”„๋ ˆ์ž„์„ ์‚ดํŽด๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋ณด๊ณ ์„œ๋ฅผ `models/t5/modeling_t5.py`์˜ ์ฝ”๋“œ์™€ ์ผ์น˜์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +class T5DenseGatedGeluDense(nn.Module): + def __init__(self, config): + super().__init__() + self.wi_0 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wi_1 = nn.Linear(config.d_model, config.d_ff, bias=False) + self.wo = nn.Linear(config.d_ff, config.d_model, bias=False) + self.dropout = nn.Dropout(config.dropout_rate) + self.gelu_act = ACT2FN["gelu_new"] + + def forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states +``` + +์ด์ œ `dropout` ํ˜ธ์ถœ๊ณผ ์ด์ „์˜ ๋ชจ๋“  ํ˜ธ์ถœ์„ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฐ์ง€๋Š” `forward` ํ›„ํฌ์—์„œ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ, ์ด๋Ÿฌํ•œ ๋ณด๊ณ ์„œ๋Š” ๊ฐ `forward`๊ฐ€ ๋ฐ˜ํ™˜๋œ ์งํ›„์— ์ฆ‰์‹œ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. + +์ „์ฒด ๋ณด๊ณ ์„œ๋กœ ๋Œ์•„๊ฐ€์„œ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์กฐ์น˜ ๋ฐ ์ˆ˜์ •์„ ํ•˜๋ ค๋ฉด, ์ˆซ์ž๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ ๋ช‡ ๊ฐœ์˜ ํ”„๋ ˆ์ž„ ์œ„๋กœ ์ด๋™ํ•ด์„œ ์—ฌ๊ธฐ์„œ `fp32` ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ์ˆซ์ž๊ฐ€ ๊ณฑํ•ด์ง€๊ฑฐ๋‚˜ ํ•ฉ์ณ์งˆ ๋•Œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `amp`๊ฐ€ ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ ์ผ์‹œ์ ์œผ๋กœ ๋„๊ณ  ์›๋ž˜์˜ `forward`๋ฅผ ๋„์šฐ๋ฏธ ๋ž˜ํผ๋กœ ์ด๋™ํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +def _forward(self, hidden_states): + hidden_gelu = self.gelu_act(self.wi_0(hidden_states)) + hidden_linear = self.wi_1(hidden_states) + hidden_states = hidden_gelu * hidden_linear + hidden_states = self.dropout(hidden_states) + hidden_states = self.wo(hidden_states) + return hidden_states + + +import torch + + +def forward(self, hidden_states): + if torch.is_autocast_enabled(): + with torch.cuda.amp.autocast(enabled=False): + return self._forward(hidden_states) + else: + return self._forward(hidden_states) +``` + +์ž๋™ ๊ฐ์ง€๊ธฐ๋Š” ์ „์ฒด ํ”„๋ ˆ์ž„์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ณด๊ณ ํ•˜๋ฏ€๋กœ, ์–ด๋””๋ฅผ ์‚ดํŽด๋ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ฉด ํŠน์ • `forward` ํ•จ์ˆ˜์˜ ์ค‘๊ฐ„ ๋‹จ๊ณ„๋„ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” `detect_overflow` ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ์œ„์น˜์— ๊ฐ์ง€๊ธฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด: + +```python +from debug_utils import detect_overflow + + +class T5LayerFF(nn.Module): + [...] + + def forward(self, hidden_states): + forwarded_states = self.layer_norm(hidden_states) + detect_overflow(forwarded_states, "after layer_norm") + forwarded_states = self.DenseReluDense(forwarded_states) + detect_overflow(forwarded_states, "after DenseReluDense") + return hidden_states + self.dropout(forwarded_states) +``` + +์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ 2๊ฐœ์˜ ๊ฒƒ์„ ์ถ”์ ํ•˜๊ณ  ์ด์ œ `forwarded_states`์˜ `inf` ๋˜๋Š” `nan`์ด ์ค‘๊ฐ„์— ๊ฐ์ง€๋˜์—ˆ๋Š”์ง€๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค. + +์‹ค์ œ๋กœ ์œ„์˜ ์˜ˆ์ œ์—์„œ ๊ฐ ํ˜ธ์ถœ์ด `nn.Module`์ด๊ธฐ ๋•Œ๋ฌธ์— ํƒ์ง€๊ธฐ๊ฐ€ ์ด๋ฏธ ์ด๋ฅผ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ์—์„œ ์ง์ ‘ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ ‡๊ฒŒ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. + +๋˜ํ•œ, ์ž์ฒด ์ฝ”๋“œ์—์„œ ๋””๋ฒ„๊ฑฐ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์—์„œ ์ถœ๋ ฅ๋˜๋Š” ํ”„๋ ˆ์ž„ ์ˆ˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด: + +```python +from transformers.debug_utils import DebugUnderflowOverflow + +debug_overflow = DebugUnderflowOverflow(model, max_frames_to_save=100) +``` + +### ํŠน์ • ๋ฐฐ์น˜์˜ ์ ˆ๋Œ“๊ฐ’ ์ตœ์†Œ ๋ฐ ์ตœ๋Œ€ ๊ฐ’ ์ถ”์  [[specific-batch-absolute-min-and-max-value-tracing]] + +๋™์ผํ•œ ๋””๋ฒ„๊น… ํด๋ž˜์Šค๋Š” ์–ธ๋”ํ”Œ๋กœ์šฐ/์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๊ฐ์ง€ ๊ธฐ๋Šฅ์ด ๊บผ์ง„ ์ƒํƒœ์—์„œ ๋ฐฐ์น˜๋ณ„ ์ถ”์ ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ๋ฐฐ์น˜์˜ ๊ฐ `forward` ํ˜ธ์ถœ์˜ ๋ชจ๋“  ๊ตฌ์„ฑ ์„ฑ๋ถ„์— ๋Œ€ํ•œ ์ ˆ๋Œ€ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ํ™•์ธํ•˜๊ณ , ์ด๋ฅผ ๋ฐฐ์น˜ 1๊ณผ 3์— ๋Œ€ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค: + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3]) +``` + +๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ๋ฐฐ์น˜ 1๊ณผ 3 ์ „์ฒด๊ฐ€ ์–ธ๋”ํ”Œ๋กœ์šฐ/์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๊ฐ์ง€๊ธฐ์™€ ๋™์ผํ•œ ํ˜•์‹์œผ๋กœ ์ถ”์ ๋ฉ๋‹ˆ๋‹ค. + +๋ฐฐ์น˜๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. + +์ด๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ํŠน์ • ๋ฐฐ์น˜ ๋ฒˆํ˜ธ ์ดํ›„์— ์˜ค์ž‘๋™ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์˜์—ญ์œผ๋กœ ๋ฐ”๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ตฌ์„ฑ์— ๋Œ€ํ•œ ์ƒ˜ํ”Œ ์ถ•์†Œ๋œ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +``` + *** Starting batch number=1 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.47e+04 input[0] +5.36e-05 7.92e+02 output +[...] + decoder.dropout Dropout +1.60e-07 2.27e+01 input[0] +0.00e+00 2.52e+01 output + decoder T5Stack + not a tensor output + lm_head Linear +1.01e-06 7.92e+02 weight +0.00e+00 1.11e+00 input[0] +6.06e-02 8.39e+01 output + T5ForConditionalGeneration + not a tensor output + + *** Starting batch number=3 *** +abs min abs max metadata + shared Embedding +1.01e-06 7.92e+02 weight +0.00e+00 2.78e+04 input[0] +5.36e-05 7.92e+02 output +[...] +``` + +์—ฌ๊ธฐ์—์„œ๋Š” ๋ชจ๋ธ์˜ forward ํ˜ธ์ถœ ์ˆ˜์™€ ๋™์ผํ•œ ์ˆ˜์˜ ํ”„๋ ˆ์ž„์ด ๋คํ”„๋˜๋ฏ€๋กœ ๋งŽ์€ ์ˆ˜์˜ ํ”„๋ ˆ์ž„์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›ํ•˜๋Š” ๊ฒƒ์ผ ์ˆ˜๋„ ์žˆ๊ณ  ์•„๋‹ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋•Œ๋กœ๋Š” ์ผ๋ฐ˜ ๋””๋ฒ„๊ฑฐ๋ณด๋‹ค ๋””๋ฒ„๊น… ๋ชฉ์ ์œผ๋กœ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ œ๊ฐ€ ๋ฐฐ์น˜ ๋ฒˆํ˜ธ 150์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ 149์™€ 150์˜ ์ถ”์ ์„ ๋คํ”„ํ•˜๊ณ  ์ˆซ์ž๊ฐ€ ์–ด๋””์„œ๋ถ€ํ„ฐ ๋‹ค๋ฅด๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ, ํ›ˆ๋ จ์„ ์ค‘์ง€ํ•  ๋ฐฐ์น˜ ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3], abort_after_batch_num=3) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/deepspeed.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/deepspeed.md new file mode 100644 index 0000000000000000000000000000000000000000..6a4f7a5102ce3199264783460abd5556fa4cea11 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/deepspeed.md @@ -0,0 +1,1220 @@ + + +# DeepSpeed[[deepspeed]] + +[DeepSpeed](https://www.deepspeed.ai/)๋Š” ๋ถ„์‚ฐ ํ•™์Šต ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์ด๊ณ  ๋น ๋ฅด๊ฒŒ ๋งŒ๋“œ๋Š” PyTorch ์ตœ์ ํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ทธ ํ•ต์‹ฌ์€ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ๊ทœ๋ชจ์— ๋งž๊ฒŒ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๋Š” [Zero Redundancy Optimizer(ZeRO)](https://hf.co/papers/1910.02054)์ž…๋‹ˆ๋‹ค. ZeRO๋Š” ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค: + +* ZeRO-1, GPU ๊ฐ„ ์ตœ์ ํ™” ์ƒํƒœ ๋ถ„ํ•  +* ZeRO-2, GPU ๊ฐ„ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ถ„ํ•  +* ZeRO-3, GPU ๊ฐ„ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ถ„ํ•  + +GPU๊ฐ€ ์ œํ•œ๋œ ํ™˜๊ฒฝ์—์„œ ZeRO๋Š” ์ตœ์ ํ™” ๋ฉ”๋ชจ๋ฆฌ์™€ ๊ณ„์‚ฐ์„ GPU์—์„œ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜์—ฌ ๋‹จ์ผ GPU์— ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ์žฅ์ฐฉํ•˜๊ณ  ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. DeepSpeed๋Š” ๋ชจ๋“  ZeRO ๋‹จ๊ณ„ ๋ฐ ์˜คํ”„๋กœ๋”ฉ์„ ์œ„ํ•ด Transformers [`Trainer`] ํด๋ž˜์Šค์™€ ํ†ตํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ์ œ๊ณต๋œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ถ”๋ก ์˜ ๊ฒฝ์šฐ, Transformers๋Š” ๋Œ€์šฉ๋Ÿ‰ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ZeRO-3 ๋ฐ ์˜คํ”„๋กœ๋”ฉ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” DeepSpeed ํŠธ๋ ˆ์ด๋‹์„ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•, ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ, ๋‹ค์–‘ํ•œ ZeRO ๋‹จ๊ณ„์— ๋Œ€ํ•œ ๊ตฌ์„ฑ ํŒŒ์ผ ์„ค์ • ๋ฐฉ๋ฒ•, ์˜คํ”„๋กœ๋”ฉ, ์ถ”๋ก  ๋ฐ [`Trainer`] ์—†์ด DeepSpeed๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +## ์„ค์น˜[[installation]] + +DeepSpeed๋Š” PyPI ๋˜๋Š” Transformers์—์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ž์„ธํ•œ ์„ค์น˜ ์˜ต์…˜์€ DeepSpeed [์„ค์น˜ ์ƒ์„ธ์‚ฌํ•ญ](https://www.deepspeed.ai/tutorials/advanced-install/) ๋˜๋Š” GitHub [README](https://github.com/deepspeedai/DeepSpeed#installation)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”). + + + +DeepSpeed๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ [DeepSpeed CUDA ์„ค์น˜](../debugging#deepspeed-cuda-installation) ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. DeepSpeed์—๋Š” pip ์„ค์น˜ ๊ฐ€๋Šฅํ•œ PyPI ํŒจํ‚ค์ง€๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•˜๋“œ์›จ์–ด์— ๊ฐ€์žฅ ์ž˜ ๋งž๊ณ  PyPI ๋ฐฐํฌํŒ์—์„œ๋Š” ์ œ๊ณต๋˜์ง€ ์•Š๋Š” 1๋น„ํŠธ Adam๊ณผ ๊ฐ™์€ ํŠน์ • ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋ ค๋ฉด [์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๊ธฐ](https://www.deepspeed.ai/tutorials/advanced-install/#install-deepspeed-from-source)๋ฅผ ์ ๊ทน ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + + + + + + +```bash +pip install deepspeed +``` + + + + +```bash +pip install transformers[deepspeed] +``` + + + + +## ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰[[memory-requirements]] + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ชจ๋ธ์— ๋งž๋Š” ์ถฉ๋ถ„ํ•œ GPU ๋ฐ CPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. DeepSpeed๋Š” ํ•„์š”ํ•œ CPU/GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹จ์ผ GPU์—์„œ [bigscience/T0_3B](bigscience/T0_3B) ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +$ python -c 'from transformers import AutoModel; \ +from deepspeed.runtime.zero.stage3 import estimate_zero3_model_states_mem_needs_all_live; \ +model = AutoModel.from_pretrained("bigscience/T0_3B"); \ +estimate_zero3_model_states_mem_needs_all_live(model, num_gpus_per_node=1, num_nodes=1)' +[...] +Estimated memory needed for params, optim states and gradients for a: +HW: Setup with 1 node, 1 GPU per node. +SW: Model with 2783M total params, 65M largest layer params. + per CPU | per GPU | Options + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=1 + 70.00GB | 0.25GB | offload_param=cpu , offload_optimizer=cpu , zero_init=0 + 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=1 + 62.23GB | 5.43GB | offload_param=none, offload_optimizer=cpu , zero_init=0 + 0.37GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=1 + 15.56GB | 46.91GB | offload_param=none, offload_optimizer=none, zero_init=0 +``` + +์ฆ‰, CPU ์˜คํ”„๋กœ๋“œ๊ฐ€ ์—†๋Š” ๋‹จ์ผ 80GB GPU ๋˜๋Š” ์˜คํ”„๋กœ๋“œ ํ•  8GB GPU์™€ ์ตœ๋Œ€ 60GB CPU๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค (์ด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜, ์ตœ์ ํ™” ์ƒํƒœ ๋ฐ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์ผ ๋ฟ์ด๋ฉฐ CUDA ์ปค๋„ ๋ฐ ํ™œ์„ฑํ™”์—๋Š” ์กฐ๊ธˆ ๋” ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค). ๋˜ํ•œ ๋” ์ž‘์€ GPU๋ฅผ ๋Œ€์—ฌํ•˜๊ฑฐ๋‚˜ ๊ตฌ์ž…ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ €๋ ดํ•˜์ง€๋งŒ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ ๋น„์šฉ๊ณผ ์†๋„ ๊ฐ„์˜ ๊ท ํ˜•์„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด CPU/NVMe ์˜คํ”„๋กœ๋“œ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜์—ฌ ๋ชจ๋“  ์ž‘์—…์„ ๋” ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜์„ธ์š”. + +## ZeRO ๋‹จ๊ณ„ ์„ค์ •ํ•˜๊ธฐ[[select-a-zero-stage]] + +DeepSpeed๋ฅผ ์„ค์น˜ํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ ๋” ์ž˜ ํŒŒ์•…ํ–ˆ๋‹ค๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์‚ฌ์šฉํ•  ZeRO ์Šคํ…Œ์ด์ง€๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋น ๋ฅด๊ณ  ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ด ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +| ์†๋„ | ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ | +|------------------|------------------| +| ZeRO-1 | ZeRO-3 + offload | +| ZeRO-2 | ZeRO-3 | +| ZeRO-2 + offload | ZeRO-2 + offload | +| ZeRO-3 | ZeRO-2 | +| ZeRO-3 + offload | ZeRO-1 | + +์ž์‹ ์—๊ฒŒ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ๋ ค๋ฉด ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ๋” ๋А๋ฆฌ์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ด ๋†’์€ ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์‹œ๋„ํ•˜์„ธ์š”. ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์‚ฌ์ด์˜ ์ ์ ˆํ•œ ๊ท ํ˜•์„ ์ฐพ๊ธฐ ์œ„ํ•ด (๊ฐ€์žฅ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ด๊ฑฐ๋‚˜ ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ) ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ž์œ ๋กญ๊ฒŒ ์ž‘์—…ํ•˜์„ธ์š”. + +์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค(๋ฐฐ์น˜ ํฌ๊ธฐ 1๋กœ ์‹œ์ž‘): + +1. ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ… ํ™œ์„ฑํ™” +2. ZeRO-2 ์‹œ๋„ +3. ZeRO-2์™€ ๋งค๊ฐœ๋ณ€์ˆ˜ ์˜คํ”„๋กœ๋“œ ์‹œ๋„ +4. ZeRO-3 ์‹œ๋„ +5. ZeRO-3๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ CPU ์˜คํ”„๋กœ๋“œ ์‹œ๋„ +6. ZeRO-3, ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ € CPU ์˜คํ”„๋กœ๋“œ ์‹œ๋„ +7. [`~GenerationMixin.generate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋” ์ข์€ ๋น” ์„œ์น˜ ๊ฒ€์ƒ‰ ๋ฒ”์œ„์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ธฐ๋ณธ๊ฐ’์„ ๋‚ฎ์ถฐ๋ณด๊ธฐ +8. ์ „์ฒด ์ •๋ฐ€๋„ ๊ฐ€์ค‘์น˜๋ณด๋‹ค ๋ฐ˜์ •๋ฐ€๋„(๊ตฌํ˜• GPU ๊ตฌ์กฐ์˜ ๊ฒฝ์šฐ fp16, ์•”ํŽ˜์–ด ์ดํ›„ GPU์˜ ๊ฒฝ์šฐ bf16)๋ฅผ ํ˜ผํ•ฉํ•ด๋ณด๊ธฐ +9. ๊ฐ€๋Šฅํ•˜๋ฉด ํ•˜๋“œ์›จ์–ด๋ฅผ ๋” ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ Infinity๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ NVMe๋กœ ์˜คํ”„๋กœ๋“œํ•˜๋„๋ก ํ™œ์„ฑํ™” +10. ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ์œ ํšจ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์ธก์ •ํ•œ ๋‹ค์Œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ตœ๋Œ€ํ•œ ํฌ๊ฒŒ ๋Š˜๋ ค GPU ํšจ์œจ์„ฑ์„ ๊ทน๋Œ€ํ™” +11. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ผ๋ถ€ ์˜คํ”„๋กœ๋“œ ๊ธฐ๋Šฅ์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๊ฑฐ๋‚˜ ๋” ๋น ๋ฅธ ZeRO ์Šคํ…Œ์ด์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์—ฌ ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๊ฐ„์˜ ์ตœ์ ์˜ ๊ท ํ˜•์„ ์ฐพ์•„ ํŠธ๋ ˆ์ด๋‹ ์„ค์ •์„ ์ตœ์ ํ™” + + +## DeepSpeed ๊ตฌ์„ฑ ํŒŒ์ผ[[deepspeed-configuration-file]] + +DeepSpeed๋Š” ํŠธ๋ ˆ์ด๋‹ ์‹คํ–‰ ๋ฐฉ๋ฒ•์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ํ†ตํ•ด [`Trainer`] ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋ ˆ์ด๋‹ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด DeepSpeed๋Š” [`Trainer`]๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๊ตฌ์„ฑ์„ ์ฝ˜์†”์— ๊ธฐ๋กํ•˜๋ฏ€๋กœ ์–ด๋–ค ๊ตฌ์„ฑ์ด ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ์ •ํ™•ํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +DeepSpeed ๊ตฌ์„ฑ ์˜ต์…˜์˜ ์ „์ฒด ๋ชฉ๋ก์€ [DeepSpeed Configuration JSON](https://www.deepspeed.ai/docs/config-json/)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ [DeepSpeedExamples](https://github.com/deepspeedai/DeepSpeedExamples) ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋˜๋Š” ๊ธฐ๋ณธ [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋‹ค์–‘ํ•œ DeepSpeed ๊ตฌ์„ฑ ์˜ˆ์ œ์— ๋Œ€ํ•œ ๋ณด๋‹ค ์‹ค์šฉ์ ์ธ ์˜ˆ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์ธ ์˜ˆ์ œ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์œผ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜์„ธ์š”: + +```bash +git clone https://github.com/deepspeedai/DeepSpeedExamples +cd DeepSpeedExamples +find . -name '*json' +# Lamb ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒ˜ํ”Œ ์ฐพ๊ธฐ +grep -i Lamb $(find . -name '*json') +``` + + + +๋ช…๋ น์ค„ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํŠธ๋ ˆ์ด๋‹ํ•˜๋Š” ๊ฒฝ์šฐ DeepSpeed ๊ตฌ์„ฑ ํŒŒ์ผ์€ JSON ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋กœ ์ „๋‹ฌ๋˜๊ฑฐ๋‚˜ ๋…ธํŠธ๋ถ ์„ค์ •์—์„œ [`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘์ฒฉ๋œ `dict` ๊ฐ์ฒด๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. + + + + +```py +TrainingArguments(..., deepspeed="path/to/deepspeed_config.json") +``` + + + + +```py +ds_config_dict = dict(scheduler=scheduler_params, optimizer=optimizer_params) +args = TrainingArguments(..., deepspeed=ds_config_dict) +trainer = Trainer(model, args, ...) +``` + + + + +### DeepSpeed์™€ Trainer ๋งค๊ฐœ๋ณ€์ˆ˜[[deepspeed-and-trainer-parameters]] + +๊ตฌ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” ์„ธ ๊ฐ€์ง€ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +1. ์ผ๋ถ€ ๊ตฌ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” [`Trainer`]์™€ DeepSpeed๊ฐ€ ๊ณต์œ ํ•˜๋ฉฐ, ์ •์˜๊ฐ€ ์ถฉ๋Œํ•˜๋Š” ๊ฒฝ์šฐ ์˜ค๋ฅ˜๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต์œ  ๊ตฌ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” [`Trainer`] ๋ช…๋ น์ค„ ์ธ์ˆ˜์—์„œ ์‰ฝ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +2. ๋ชจ๋ธ ์„ค์ •์—์„œ ์ž๋™์œผ๋กœ ๋„์ถœ๋˜๋Š” ์ผ๋ถ€ ์„ค์ • ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ˆ˜๋™์œผ๋กœ ๊ฐ’์„ ์กฐ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. [`Trainer`]๋Š” ๊ตฌ์„ฑ ๊ฐ’ `auto`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์ •ํ™•ํ•˜๊ฑฐ๋‚˜ ํšจ์œจ์ ์ธ ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ๊ตฌ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, [`Trainer`] ์ธ์ˆ˜์™€ DeepSpeed ์„ค์ • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ์ง€ํ•˜๊ธฐ ๋งค์šฐ ์–ด๋ ค์šด ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ์ด ์‹คํŒจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +3. ๊ต์œก ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ์ˆ˜๋™์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ์ผ๋ถ€ ์„ค์ • ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” DeepSpeed์—๋งŒ ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. + +DeepSpeed ๊ตฌ์„ฑ์„ ์ˆ˜์ •ํ•˜๊ณ  [`TrainingArguments`]๋ฅผ ํŽธ์ง‘ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + +1. ๊ธฐ๋ณธ ๊ตฌ์„ฑ์œผ๋กœ ์‚ฌ์šฉํ•  DeepSpeed ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. +2. ๋‹ค์Œ DeepSpeed ๊ตฌ์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ [`TrainingArguments`] ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +`scheduler.params.total_num_steps`์™€ ๊ฐ™์€ ์ผ๋ถ€ ๊ฐ’์€ ํŠธ๋ ˆ์ด๋‹ ์ค‘ [`Trainer`]์— ์˜ํ•ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. + +### ZeRO ๊ตฌ์„ฑ[[zero-configuration]] + +์„ธ ๊ฐ€์ง€ ๊ตฌ์„ฑ์ด ์žˆ์œผ๋ฉฐ, ๊ฐ ๊ตฌ์„ฑ์€ ์„œ๋กœ ๋‹ค๋ฅธ ZeRO ๋‹จ๊ณ„์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. 1๋‹จ๊ณ„๋Š” ํ™•์žฅ์„ฑ ์ธก๋ฉด์—์„œ ๊ทธ๋‹ค์ง€ ๋ˆˆ์—ฌ๊ฒจ๋ณผ๋งŒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” 2๋‹จ๊ณ„์™€ 3๋‹จ๊ณ„์— ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. `zero_optimization` ๊ตฌ์„ฑ์—๋Š” ํ™œ์„ฑํ™”ํ•  ํ•ญ๋ชฉ๊ณผ ๊ตฌ์„ฑ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ชจ๋“  ์˜ต์…˜์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ [DeepSpeed ๊ตฌ์„ฑ JSON](https://www.deepspeed.ai/docs/config-json/) ์ฐธ์กฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + +DeepSpeed๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š์œผ๋ฉฐ ์˜คํƒ€๊ฐ€ ์žˆ์œผ๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ ์„ค์ •์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. DeepSpeed ์—”์ง„ ์‹œ์ž‘ ๋กœ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ณ  ์–ด๋–ค ๊ฐ’์„ ์‚ฌ์šฉํ• ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +[`Trainer`]๋Š” ๋™๋“ฑํ•œ ๋ช…๋ น์ค„ ์ธ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‹ค์Œ ๊ตฌ์„ฑ์€ DeepSpeed๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + + +ZeRO-1์€ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ GPU์— ๋ถ„ํ• ํ•˜์—ฌ ์•ฝ๊ฐ„์˜ ์†๋„ ํ–ฅ์ƒ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ZeRO-1 ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```yml +{ + "zero_optimization": { + "stage": 1 + } +} +``` + + + + +ZeRO-2๋Š” GPU์—์„œ ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋Š” ์ถ”๋ก ๊ณผ ๊ด€๋ จ์ด ์—†๋Š” ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ๋กœ ํ›ˆ๋ จ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ๊ตฌ์„ฑํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +* GPU ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๋ ค๋ฉด `offload_optimizer`๋ฅผ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +* `true`๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ `overlap_comm`์€ GPU ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ฆ๊ฐ€๋ฅผ ์ƒ์‡„ํ•˜์—ฌ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ 4.5๋ฐฐ์˜ `allgather_bucket_size` ๋ฐ `reduce_bucket_size`๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ์—์„œ๋Š” `5e8`๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ 9GB์˜ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 8GB ์ดํ•˜์ธ ๊ฒฝ์šฐ, ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰์„ ๋‚ฎ์ถ”๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ(OOM) ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด `overlap_comm`์„ ์ค„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +* `allgather_bucket_size`์™€ `reduce_bucket_size`๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU ๋ฉ”๋ชจ๋ฆฌ์™€ ํ†ต์‹  ์†๋„๋ฅผ ์ ˆ์ถฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์ด ์ž‘์„์ˆ˜๋ก ํ†ต์‹  ์†๋„๊ฐ€ ๋А๋ ค์ง€๊ณ  ๋” ๋งŽ์€ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ฒƒ์ด ์•ฝ๊ฐ„ ๋А๋ฆฐ ํ›ˆ๋ จ ์‹œ๊ฐ„๋ณด๋‹ค ๋” ์ค‘์š”ํ•œ์ง€ ๊ท ํ˜•์„ ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +* DeepSpeed 0.4.4์—์„œ๋Š” CPU ์˜คํ”„๋กœ๋”ฉ์„ ์œ„ํ•ด `round_robin_gradients`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ์„ธ๋ถ„ํ™”๋œ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ํŒŒํ‹ฐ์…”๋‹์„ ํ†ตํ•ด ๋“ฑ๊ธ‰ ๊ฐ„ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ณต์‚ฌ๋ฅผ CPU ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ณ‘๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค. ์„ฑ๋Šฅ ์ด์ ์€ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  ๋‹จ๊ณ„(์ตœ์ ํ™” ๋‹จ๊ณ„ ๊ฐ„ ๋ณต์‚ฌ ํšŸ์ˆ˜ ์ฆ๊ฐ€) ๋˜๋Š” GPU ์ˆ˜(๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์ฆ๊ฐ€)์— ๋”ฐ๋ผ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +```yml +{ + "zero_optimization": { + "stage": 2, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "allgather_partitions": true, + "allgather_bucket_size": 5e8, + "overlap_comm": true, + "reduce_scatter": true, + "reduce_bucket_size": 5e8, + "contiguous_gradients": true + "round_robin_gradients": true + } +} +``` + + + + +ZeRO-3๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €, ๊ทธ๋ž˜๋””์–ธํŠธ, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ GPU์— ๊ฑธ์ณ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ZeRO-2์™€ ๋‹ฌ๋ฆฌ ZeRO-3๋Š” ์—ฌ๋Ÿฌ GPU์— ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ›ˆ๋ จ ์™ธ์—๋„ ์ถ”๋ก ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +* `device: "cpu"` ๋Š” GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๊ณ  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ CPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +* `pin_memory: true` ๋Š” ์ฒ˜๋ฆฌ๋Ÿ‰์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•€ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์š”์ฒญํ•œ ํŠน์ • ํ”„๋กœ์„ธ์Šค๋ฅผ ์œ„ํ•ด ์˜ˆ์•ฝ๋˜์–ด ์žˆ๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์ผ๋ฐ˜ CPU ๋ฉ”๋ชจ๋ฆฌ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ์•ก์„ธ์Šค๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. +* `stage3_max_live_parameters` ๋Š” ํŠน์ • ์‹œ๊ฐ„์— GPU์— ์œ ์ง€ํ•˜๋ ค๋Š” ์ „์ฒด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ƒํ•œ๊ฐ’์ž…๋‹ˆ๋‹ค. OOM ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด ๊ฐ’์„ ์ค„์ด์„ธ์š”. +* `stage3_max_reuse_distance` ๋Š” ํ–ฅํ›„ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์‹œ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฐ’์œผ๋กœ, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฒ„๋ฆด์ง€ ์œ ์ง€ํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ(`stage3_max_reuse_distance`๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ธ ๊ฒฝ์šฐ) ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํ™œ์„ฑํ™” ์ฒดํฌํฌ์ธํŒ…์ด ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๊ณ  ์—ญ์ „ํŒŒ ๊ณ„์‚ฐ์‹œ๊นŒ์ง€ ์ˆœ์ „ํŒŒ ์‹œ์ ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์œ ์ง€ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ OOM ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด ๊ฐ’์„ ์ค„์ด์„ธ์š”. +* ๋ชจ๋ธ ์ €์žฅ ์‹œ `stage3_gather_16bit_weights_on_model_save`๋Š” fp16 ๊ฐ€์ค‘์น˜๋ฅผ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ GPU๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ์™€ ์†๋„ ์ธก๋ฉด์—์„œ ๋น„์šฉ์ด ๋งŽ์ด ๋“ญ๋‹ˆ๋‹ค. ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•  ๊ณ„ํš์ด๋ผ๋ฉด ์ด ์˜ต์…˜์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +* `sub_group_size` ๋Š” ์ตœ์ ํ™” ๋‹จ๊ณ„์—์„œ ์—…๋ฐ์ดํŠธ๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” `sub_group_size`์˜ ๋ฒ„ํ‚ท์œผ๋กœ ๊ทธ๋ฃนํ™”๋˜๋ฉฐ ๊ฐ ๋ฒ„ํ‚ท์€ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. NVMe ์˜คํ”„๋กœ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ `sub_group_size`๋Š” ์ตœ์ ํ™” ๋‹จ๊ณ„ ์ค‘ ๋ชจ๋ธ ์ƒํƒœ๊ฐ€ CPU ๋ฉ”๋ชจ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ์ ์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งค์šฐ ํฐ ๋ชจ๋ธ์˜ CPU ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. NVMe ์˜คํ”„๋กœ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ `sub_group_size`๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋‘˜ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + + 1. ์˜ตํ‹ฐ๋งˆ์ด์ € ๋‹จ๊ณ„์—์„œ OOM ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ž„์‹œ ๋ฒ„ํผ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๋ ค๋ฉด `sub_group_size`๋ฅผ ์ค„์ด์„ธ์š”. + 2. ์˜ตํ‹ฐ๋งˆ์ด์ € ๋‹จ๊ณ„์—์„œ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๋ฒ„ํผ ์ฆ๊ฐ€๋กœ ์ธํ•œ ๋Œ€์—ญํญ ์‚ฌ์šฉ๋ฅ ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด `sub_group_size`๋ฅผ ๋Š˜๋ฆฌ์„ธ์š”. + +* `reduce_bucket_size`, `stage3_prefetch_bucket_size`, `stage3_param_persistence_threshold`๋Š” ๋ชจ๋ธ์˜ ์ˆจ๊ฒจ์ง„ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด ๊ฐ’๋“ค์„ `auto`์œผ๋กœ ์„ค์ •ํ•˜๊ณ  [`Trainer`]๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐ’์„ ํ• ๋‹นํ•˜๋„๋ก ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +```yml +{ + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + } +} +``` + +[`deepspeed.zero.Init`](https://deepspeed.readthedocs.io/en/latest/zero3.html#deepspeed.zero.Init) ์ปจํ…์ŠคํŠธ ๋งค๋‹ˆ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์„ ๋” ๋น ๋ฅด๊ฒŒ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from transformers import T5ForConditionalGeneration, T5Config +import deepspeed + +with deepspeed.zero.Init(): + config = T5Config.from_pretrained("google-t5/t5-small") + model = T5ForConditionalGeneration(config) +``` + +์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ๋”ฅ์Šคํ”ผ๋“œ ๊ตฌ์„ฑ ํŒŒ์ผ์— `is_deepspeed_zero3_enabled: true`๊ฐ€ [`TrainingArguments`]์— ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ZeRO ๊ตฌ์„ฑ์ด ํ™œ์„ฑํ™”๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ๋œ ๋ชจ๋ธ [`~PreTrainedModel.from_pretrained`]์„ ํ˜ธ์ถœํ•˜๊ธฐ **์ „์—** [`TrainingArguments`] ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoModel, Trainer, TrainingArguments + +training_args = TrainingArguments(..., deepspeed=ds_config) +model = AutoModel.from_pretrained("google-t5/t5-small") +trainer = Trainer(model=model, args=training_args, ...) +``` + +fp16 ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ZeRO-3์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. fp16 ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, [`~PreTrainedModel.from_pretrained`]์— `torch_dtype=torch.float16`์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +ZeRO-3์˜ ๋˜ ๋‹ค๋ฅธ ๊ณ ๋ ค ์‚ฌํ•ญ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ๋ ˆ์ด์–ด์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ํ•œ ๋‹จ์ผ GPU์— ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ [`~PreTrainedModel.from_pretrained`]์— ๋กœ๋“œํ•˜๋Š” ๋“ฑ ๋ชจ๋“  ๋ ˆ์ด์–ด์˜ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ•œ ๋ฒˆ์— ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋ ˆ์ด์–ด๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ฆ‰์‹œ ๋ชจ๋“  GPU์— ํŒŒํ‹ฐ์…”๋‹ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งค์šฐ ํฐ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์œผ๋กœ ์ธํ•ด ํ•˜๋‚˜์˜ GPU์— ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•œ ๋‹ค์Œ ๋‹ค๋ฅธ GPU์— ๋ถ„์‚ฐํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ด๋Š” ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ€์ค‘์น˜(์—ฌ๊ธฐ์„œ `tensor([1.])`) ๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ํฌ๊ธฐ๊ฐ€ ๋” ํฐ ๋‹ค์ฐจ์› ํ˜•ํƒœ ๋Œ€์‹  1์ธ ๊ฒฝ์šฐ, ์ด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋ถ„ํ• ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ด๊ฒƒ์ด ZeRO-3 ํ”Œ๋ ˆ์ด์Šคํ™€๋”์ธ ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +```py +tensor([1.0], device="cuda:0", dtype=torch.float16, requires_grad=True) +``` + + + +ZeRO-3๋กœ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Constructing Massive Models](https://deepspeed.readthedocs.io/en/latest/zero3.html#constructing-massive-models) ๋ฐ [Gathering Parameters](https://deepspeed.readthedocs.io/en/latest/zero3.html#gathering-parameters) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + + + + + +### NVMe ์„ค์ •[[nvme-configuration]] + +[ZeRO-Infinity](https://hf.co/papers/2104.07857)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ ์ƒํƒœ๋ฅผ CPU ๋ฐ/๋˜๋Š” NVMe๋กœ ์˜คํ”„๋กœ๋“œํ•˜์—ฌ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šค๋งˆํŠธ ํŒŒํ‹ฐ์…”๋‹ ๋ฐ ํƒ€์ผ๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๊ฐ GPU๋Š” ์˜คํ”„๋กœ๋”ฉ ์ค‘์— ๋งค์šฐ ์ ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ตœ์‹  NVMe๋Š” ํ›ˆ๋ จ ํ”„๋กœ์„ธ์Šค์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋” ํฐ ์ด ๋ฉ”๋ชจ๋ฆฌ ํ’€์— ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ZeRO-Infinity์—๋Š” ZeRO-3๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ CPU ๋ฐ/๋˜๋Š” NVMe ๋ฉ”๋ชจ๋ฆฌ์— ๋”ฐ๋ผ [์˜ตํ‹ฐ๋งˆ์ด์ €](https://www.deepspeed.ai/docs/config-json/#optimizer-offloading)์™€ [๋งค๊ฐœ๋ณ€์ˆ˜](https://www.deepspeed.ai/docs/config-json/#parameter-offloading) ์ค‘ ํ•˜๋‚˜๋งŒ ์˜คํ”„๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์•„๋ฌด๊ฒƒ๋„ ์˜คํ”„๋กœ๋“œํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ผ๋ฐ˜ ํ•˜๋“œ ๋“œ๋ผ์ด๋ธŒ๋‚˜ ์†”๋ฆฌ๋“œ ์Šคํ…Œ์ดํŠธ ๋“œ๋ผ์ด๋ธŒ์—์„œ๋„ ์ž‘๋™ํ•˜์ง€๋งŒ ์†๋„๊ฐ€ ํ˜„์ €ํžˆ ๋А๋ ค์ง€๋ฏ€๋กœ `nvme_path`๊ฐ€ NVMe ์žฅ์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ตœ์‹  NVMe๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ๊ธฐ ์ž‘์—…์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ 3.5GB/s, ์“ฐ๊ธฐ ์ž‘์—…์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ 3GB/s์˜ ์ „์†ก ์†๋„๋ฅผ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ํŠธ๋ ˆ์ด๋‹ ์„ค์ •์—์„œ [๋ฒค์น˜๋งˆํฌ ์‹คํ–‰ํ•˜๊ธฐ](https://github.com/deepspeedai/DeepSpeed/issues/998)์„ ํ†ตํ•ด ์ตœ์ ์˜ 'aio' ๊ตฌ์„ฑ์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์ œ ZeRO-3/Infinity ๊ตฌ์„ฑ ํŒŒ์ผ์€ ๋Œ€๋ถ€๋ถ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ `auto`์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ์ˆ˜๋™์œผ๋กœ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```yml +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "nvme", + "nvme_path": "/local_nvme", + "pin_memory": true, + "buffer_count": 4, + "fast_init": false + }, + "offload_param": { + "device": "nvme", + "nvme_path": "/local_nvme", + "pin_memory": true, + "buffer_count": 5, + "buffer_size": 1e8, + "max_in_cpu": 1e9 + }, + "aio": { + "block_size": 262144, + "queue_depth": 32, + "thread_count": 1, + "single_submit": false, + "overlap_events": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "steps_per_print": 2000, + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", + "wall_clock_breakdown": false +} +``` + +## DeepSpeed ๊ตฌ์„ฑ[[deepspeed-features]] + +์ด ์„น์…˜์—์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ DeepSpeed ๊ตฌ์„ฑ ํŒŒ์ผ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ํ™œ์„ฑํ™”/๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ…[[activationgradient-checkpointing]] + +ํ™œ์„ฑํ™” ๋ฐ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ…์€ ์†๋„๋ฅผ ๋” ๋งŽ์€ GPU ๋ฉ”๋ชจ๋ฆฌ์™€ ๊ตํ™˜ํ•˜์—ฌ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํ™ฉ์„ ๊ทน๋ณตํ•˜๊ฑฐ๋‚˜ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜์„ธ์š”: + +1. ํ—ˆ๊น… ํŽ˜์ด์Šค ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, [`Trainer`]์—์„œ `model.gradient_checkpointing_enable()` ๋˜๋Š” `--gradient_checkpointing`์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +2. ํ—ˆ๊น… ํŽ˜์ด์Šค๊ฐ€ ์•„๋‹Œ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ๋”ฅ์Šคํ”ผ๋“œ [Activation Checkpointing API](https://deepspeed.readthedocs.io/en/latest/activation-checkpointing.html)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ๋ง ์ฝ”๋“œ๋ฅผ ๋Œ€์ฒดํ•˜๊ณ  `torch.utils.checkpoint`๋ฅผ DeepSpeed API๋กœ ๋Œ€์ฒดํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ์ˆœ๋ฐฉํ–ฅ ํ™œ์„ฑํ™”๋ฅผ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๋Š” ๋Œ€์‹  CPU ๋ฉ”๋ชจ๋ฆฌ๋กœ ์˜คํ”„๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋” ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค. + +### ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ์Šค์ผ€์ค„๋Ÿฌ[[optimizer-and-scheduler]] + +`offload_optimizer`๋ฅผ ํ™œ์„ฑํ™”ํ•˜์ง€ ์•Š๋Š” ํ•œ DeepSpeed์™€ ํŠธ๋žœ์Šคํฌ๋จธ ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `offload_optimizer`๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ฉด CPU์™€ GPU ๊ตฌํ˜„์ด ๋ชจ๋‘ ์žˆ๋Š” ๊ฒฝ์šฐ DeepSpeed๊ฐ€ ์•„๋‹Œ ์ตœ์ ํ™”๊ธฐ(LAMB ์ œ์™ธ)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๊ตฌ์„ฑ ํŒŒ์ผ์˜ ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ ๋ฐ ์Šค์ผ€์ค„๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ช…๋ น์ค„์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์˜ค๋ฅ˜๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•™์Šต ์†๋„๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์—์„œ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ ๋ช…๋ น์ค„์—์„œ ์ด๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ ๋ฐ ์Šค์ผ€์ค„๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜ ์™ธ์—๋„ [`Trainer`] ๋ช…๋ น์ค„ ์ธ์ˆ˜๊ฐ€ DeepSpeed ๊ตฌ์„ฑ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + + + + +DeepSpeed๋Š” ์—ฌ๋Ÿฌ [์˜ตํ‹ฐ๋งˆ์ด์ €](https://www.deepspeed.ai/docs/config-json/#optimizer-parameters)๋ฅผ ์ œ๊ณตํ•˜์ง€๋งŒ(Adam, AdamW, OneBitAdam ๋ฐ LAMB) PyTorch์—์„œ ๋‹ค๋ฅธ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ค์ •์—์„œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด [`Trainer`]๊ฐ€ ์ž๋™์œผ๋กœ AdamW๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ช…๋ น์ค„์—์„œ ์ œ๊ณต๋œ ๊ฐ’ ๋˜๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: `lr`, `adam_beta1`, `adam_beta2`, `adam_epsilon`, `weight_decay`. + +๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `"auto"`์œผ๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ์›ํ•˜๋Š” ๊ฐ’์„ ์ง์ ‘ ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```yaml +{ + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + } +} +``` + +์ตœ์ƒ์œ„ ๊ตฌ์„ฑ์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ง€์›๋˜์ง€ ์•Š๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```yaml +{ + "zero_allow_untested_optimizer": true +} +``` + +DeepSpeed==0.8.3๋ถ€ํ„ฐ ์˜คํ”„๋กœ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์˜คํ”„๋กœ๋“œ๊ฐ€ DeepSpeed์˜ CPU Adam ์˜ตํ‹ฐ๋งˆ์ด์ €์—์„œ ๊ฐ€์žฅ ์ž˜ ์ž‘๋™ํ•˜๋ฏ€๋กœ ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ๊ตฌ์„ฑ์— ๋‹ค์Œ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "zero_force_ds_cpu_optimizer": false +} +``` + + + + +DeepSpeed๋Š” LRRangeTest, OneCycle, WarmupLR ๋ฐ WarmupDecayLR learning rate[schedulers](https://www.deepspeed.ai/docs/config-json/#scheduler-parameters)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +ํŠธ๋žœ์Šคํฌ๋จธ์™€ DeepSpeed๋Š” ๋™์ผํ•œ ๋‘ ๊ฐ€์ง€ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +* WarmupLR์€ Transformers์˜ `--lr_scheduler_type constant_warmup`๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. +* WarmupDecayLR์€ Transformers์˜ `--lr_scheduler_type linear`์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค(Transformers์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์Šค์ผ€์ค„๋Ÿฌ์ž…๋‹ˆ๋‹ค). + +์„ค์ •์—์„œ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด[`Trainer`]๋Š” ์ž๋™์œผ๋กœ WarmupDecayLR์„ ์„ ํƒํ•˜๊ณ  ๋ช…๋ น์ค„์—์„œ ์ œ๊ณต๋œ ๊ฐ’ ๋˜๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: `warmup_min_lr`, `warmup_max_lr`, `warmup_num_steps`, `total_num_steps` (`max_steps`๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์œผ๋ฉด ๋Ÿฐํƒ€์ž„ ์ค‘์— ์ž๋™์œผ๋กœ ๊ณ„์‚ฐ๋จ). + +๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `"auto"`์œผ๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ์›ํ•˜๋Š” ๊ฐ’์„ ์ง์ ‘ ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```yaml +{ + "scheduler": { + "type": "WarmupDecayLR", + "params": { + "total_num_steps": "auto", + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + } +} +``` + + + + +### ์ •๋ฐ€๋„[[precision]] + +DeepSpeed๋Š” fp32, fp16 ๋ฐ bf16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + + + + +๋ชจ๋ธ์ด ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ์‚ฌ์ „ ํ•™์Šต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ NaN ์†์‹ค์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฒ„ํ”Œ๋กœ ๋˜๋Š” ์–ธ๋”ํ”Œ๋กœ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ fp16 ๋ชจ๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”ํ•˜์—ฌ ์ „์ฒด fp32 ์ •๋ฐ€๋„๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "fp16": { + "enabled": false + } +} +``` + +Ampere GPU ๋ฐ PyTorch 1.7 ์ด์ƒ์˜ ๊ฒฝ์šฐ ์ผ๋ถ€ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ๋” ํšจ์œจ์ ์ธ [tf32](https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) ํ˜•์‹์œผ๋กœ ์ž๋™ ์ „ํ™˜๋˜์ง€๋งŒ ๊ฒฐ๊ณผ๋Š” ์—ฌ์ „ํžˆ fp32๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. [`Trainer`]์—์„œ `--tf32`๋ฅผ ์„ค์ •ํ•˜์—ฌ ํ™œ์„ฑํ™”ํ•˜๊ณ  `--tf32 0` ๋˜๋Š” `--no_tf32`๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ฉด ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +PyTorch AMP์™€ ๊ฐ™์€ fp16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ค„์–ด๋“ค๊ณ  ํ›ˆ๋ จ ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค.[`Trainer`]๋Š” `args.fp16_backend` ๊ฐ’์— ๋”ฐ๋ผ fp16์„ ์ž๋™์œผ๋กœ ํ™œ์„ฑํ™” ๋˜๋Š” ๋น„ํ™œ์„ฑํ™”ํ•˜๋ฉฐ, ๋‚˜๋จธ์ง€ ๊ตฌ์„ฑ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์ค„์—์„œ ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด fp16์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค: `fp16`, `--fp16_backend amp` ๋˜๋Š” `--fp16_full_eval`. + +```yaml +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + } +} +``` + +์ถ”๊ฐ€ ๋”ฅ์Šคํ”ผ๋“œ fp16 ํ›ˆ๋ จ ์˜ต์…˜์€ [fp16 ํ›ˆ๋ จ ์˜ต์…˜](https://www.deepspeed.ai/docs/config-json/#fp16-training-options) ์ฐธ์กฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +Apex์™€ ๊ฐ™์€ fp16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ๊ตฌ์„ฑํ•˜๋ ค๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด `"auto"` ๋˜๋Š” ์ง์ ‘ ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.[`Trainer`]๋Š” `args.fp16_backend` ๋ฐ `args.fp16_opt_level`์˜ ๊ฐ’์— ๋”ฐ๋ผ `amp`๋ฅผ ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ช…๋ น์ค„์—์„œ ํ™œ์„ฑํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: `fp16`, `--fp16_backend apex` ๋˜๋Š” `--fp16_opt_level 01`. + +```yaml +{ + "amp": { + "enabled": "auto", + "opt_level": "auto" + } +} +``` + + + + +bf16์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด DeepSpeed==0.6.0 ์ด์ƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. bf16์€ fp32์™€ ๋™์  ๋ฒ”์œ„๊ฐ€ ๋™์ผํ•˜๋ฉฐ ์†์‹ค ์Šค์ผ€์ผ๋ง์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ [gradient accumulation](#gradient-accumulation)์„ bf16๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ด ํ˜•์‹์˜ ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ์ธํ•ด ์†์‹ค์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์›ํ•˜์ง€ ์•Š๋Š” ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๊ฐ€ bf16์— ๋ˆ„์ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +bf16์€ ์„ค์ • ํŒŒ์ผ์—์„œ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ช…๋ น์ค„์—์„œ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: `--bf16` ๋˜๋Š” `--bf16_full_eval`. + +```yaml +{ + "bf16": { + "enabled": "auto" + } +} +``` + + + + +### ๋ฐฐ์น˜ ํฌ๊ธฐ[[batch-size]] + +๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `"auto"` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ ํƒํ•˜๋ฉด [`Trainer`]๋Š” `train_micro_batch_size_per_gpu`๋ฅผ args.`per_device_train_batch_size`์˜ ๊ฐ’์œผ๋กœ, `train_batch_size`๋ฅผ `args.world_size * args.per_device_train_batch_size * args.gradient_accumulation_steps`๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "train_micro_batch_size_per_gpu": "auto", + "train_batch_size": "auto" +} +``` + +### ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ [[gradient-accumulation]] + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ ์„ ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `"auto"` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ ํƒํ•˜๋ฉด [`Trainer`]๊ฐ€ `args.gradient_accumulation_steps`์˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "gradient_accumulation_steps": "auto" +} + +``` + +### ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ํด๋ฆฌํ•‘[[gradient-clipping]] + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ํด๋ฆฌํ•‘์€ ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `"auto"` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ ํƒํ•˜๋ฉด [`Trainer`]๊ฐ€ `args.max_grad_norm`์˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "gradient_clipping": "auto" +} +``` + +### ํ†ต์‹  ๋ฐ์ดํ„ฐ ์œ ํ˜•(Communication data type)[[communication-data-type]] + +์ถ•์†Œ, ์ˆ˜์ง‘ ๋ฐ ๋ถ„์‚ฐ ์ž‘์—…๊ณผ ๊ฐ™์€ ํ†ต์‹  ์ง‘ํ•ฉ์ฒด์˜ ๊ฒฝ์šฐ ๋ณ„๋„์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +๋ชจ๋“  ์ˆ˜์ง‘ ๋ฐ ๋ถ„์‚ฐ ์ž‘์—…์€ ๋ฐ์ดํ„ฐ์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด bf16์œผ๋กœ ํ›ˆ๋ จํ•˜๋Š” ๊ฒฝ์šฐ, ์ˆ˜์ง‘์€ ๋น„์†์‹ค ์—ฐ์‚ฐ์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ๋„ bf16์œผ๋กœ ์ˆ˜์ง‘๋ฉ๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๊ฐ€ ์—ฌ๋Ÿฌ GPU์— ๊ฑธ์ณ ํ‰๊ท ํ™”๋˜๋Š” ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ๊ฐ์†Œ ์—ฐ์‚ฐ์€ ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ†ต์‹ ์ด fp16 ๋˜๋Š” bf16์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ๊ฒฝ์šฐ, ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ์—ฌ๋Ÿฌ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋ฉด ์ •ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์†์‹ค์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋” ๋†’์Šต๋‹ˆ๋‹ค. ํŠนํžˆ fp16๋ณด๋‹ค ์ •๋ฐ€๋„๊ฐ€ ๋‚ฎ์€ bf16์˜ ๊ฒฝ์šฐ ๋”์šฑ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๊ธฐ์šธ๊ธฐ๋ฅผ ํ‰๊ท ํ™”ํ•  ๋•Œ ์†์‹ค์ด ์ตœ์†Œํ™”๋˜๋ฏ€๋กœ ๊ฐ์†Œ ์—ฐ์‚ฐ์—๋Š” fp16์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํ†ต์‹  ๋ฐ์ดํ„ฐ ์œ ํ˜•์€ ์„ค์ • ํŒŒ์ผ์—์„œ `communication_data_type` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, fp32๋ฅผ ์„ ํƒํ•˜๋ฉด ์•ฝ๊ฐ„์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์ง€๋งŒ ๊ฐ์†Œ ์—ฐ์‚ฐ์ด fp32์— ๋ˆ„์ ๋˜๊ณ  ์ค€๋น„๊ฐ€ ๋˜๋ฉด ํ›ˆ๋ จ ์ค‘์ธ ๋ฐ˜์ •๋ฐ€ dtype์œผ๋กœ ๋‹ค์šด์บ์ŠคํŠธ๋ฉ๋‹ˆ๋‹ค. + +```yaml +{ + "communication_data_type": "fp32" +} +``` + +## ๋ชจ๋ธ ๋ฐฐํฌ[[deployment]] + +[torchrun](https://pytorch.org/docs/stable/elastic/run.html), `deepspeed` ๋Ÿฐ์ฒ˜ ๋˜๋Š” [Accelerate](https://huggingface.co/docs/accelerate/basic_tutorials/launch#using-accelerate-launch) ๋“ฑ ๋‹ค์–‘ํ•œ ๋Ÿฐ์ฒ˜๋ฅผ ํ†ตํ•ด DeepSpeed๋ฅผ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐํฌํ•˜๋ ค๋ฉด [`Trainer`] ๋ช…๋ น์ค„์— `--deepspeed ds_config.json`์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋ช…๋ น์ค„ ์ธ์ˆ˜๋ฅผ ์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด DeepSpeed์˜ [`add_config_arguments`](https://deepspeed.readthedocs.io/en/latest/initialize.html#argument-parsing) ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ํŠธ๋ ˆ์ด๋‹ ์„ค์ •์— ๋Œ€ํ•ด `deepspeed` ๋Ÿฐ์ฒ˜๋กœ DeepSpeed๋ฅผ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋ณด๋‹ค ์‹ค์šฉ์ ์ธ ์‚ฌ์šฉ ์˜ˆ์ œ๋Š” ์ด [post](https://github.com/huggingface/transformers/issues/8771#issuecomment-759248400)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +์—ฌ๋Ÿฌ GPU์— DeepSpeed๋ฅผ ๋ฐฐํฌํ•˜๋ ค๋ฉด `--num_gpus` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ `--num_gpus`๋ฅผ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” 2๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```bash +deepspeed --num_gpus=2 examples/pytorch/translation/run_translation.py \ +--deepspeed tests/deepspeed/ds_config_zero3.json \ +--model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ +--output_dir output_dir --overwrite_output_dir --fp16 \ +--do_train --max_train_samples 500 --num_train_epochs 1 \ +--dataset_name wmt16 --dataset_config "ro-en" \ +--source_lang en --target_lang ro +``` + + + + +๋‹จ์ผ GPU์— DeepSpeed๋ฅผ ๋ฐฐํฌํ•˜๋ ค๋ฉด `--num_gpus` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. GPU๊ฐ€ 1๊ฐœ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ด ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. DeepSpeed๋Š” ์ง€์ •๋œ ๋…ธ๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  GPU๋ฅผ ๋ฐฐํฌํ•˜๋ฏ€๋กœ ์ด ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. + +```bash +deepspeed --num_gpus=1 examples/pytorch/translation/run_translation.py \ +--deepspeed tests/deepspeed/ds_config_zero2.json \ +--model_name_or_path google-t5/t5-small --per_device_train_batch_size 1 \ +--output_dir output_dir --overwrite_output_dir --fp16 \ +--do_train --max_train_samples 500 --num_train_epochs 1 \ +--dataset_name wmt16 --dataset_config "ro-en" \ +--source_lang en --target_lang ro +``` + +DeepSpeed๋Š” ๋‹จ ํ•˜๋‚˜์˜ GPU๋กœ๋„ ์—ฌ์ „ํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค: + +1. ์ผ๋ถ€ ๊ณ„์‚ฐ๊ณผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜์—ฌ ๋” ํฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งž์ง€ ์•Š๋Š” ๋งค์šฐ ํฐ ๋ชจ๋ธ์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์— ๋” ๋งŽ์€ GPU ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. +2. ์Šค๋งˆํŠธ GPU ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์กฐ๊ฐํ™”๋ฅผ ์ตœ์†Œํ™”ํ•˜์—ฌ ๋” ํฐ ๋ชจ๋ธ๊ณผ ๋ฐ์ดํ„ฐ ๋ฐฐ์น˜์— ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๋‹จ์ผ GPU์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ์–ป์œผ๋ ค๋ฉด [ZeRO-2](#zero-configuration) ๊ตฌ์„ฑ ํŒŒ์ผ์—์„œ `allgather_bucket_size` ๋ฐ `reduce_bucket_size` ๊ฐ’์„ 2e8๋กœ ์„ค์ •ํ•˜์„ธ์š”. + + + + + + +### ๋‹ค์ค‘ ๋…ธ๋“œ ํ™˜๊ฒฝ์—์„œ์˜ ๋ชจ๋ธ ๋ฐฐํฌ[[multi-node-deployment]] + +๋…ธ๋“œ๋Š” ์›Œํฌ๋กœ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ•˜๋‚˜ ์ด์ƒ์˜ GPU์ž…๋‹ˆ๋‹ค. ๋” ๊ฐ•๋ ฅํ•œ ์„ค์ •์€ ๋ฉ€ํ‹ฐ ๋…ธ๋“œ ์„ค์ •์œผ๋กœ, `deepspeed` ๋Ÿฐ์ฒ˜๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๊ฐ๊ฐ 8๊ฐœ์˜ GPU๊ฐ€ ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋Š” `ssh hostname1`๋กœ, ๋‘ ๋ฒˆ์งธ ๋…ธ๋“œ๋Š” `ssh hostname2`๋กœ ์ ‘์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋…ธ๋“œ ๋ชจ๋‘ ๋น„๋ฐ€๋ฒˆํ˜ธ ์—†์ด ssh๋ฅผ ํ†ตํ•ด ๋กœ์ปฌ๋กœ ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์œผ๋กœ DeepSpeed๋Š” ๋ฉ€ํ‹ฐ๋…ธ๋“œ ํ™˜๊ฒฝ์—์„œ ๊ณต์œ  ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋กœ์ปฌ ํŒŒ์ผ ์‹œ์Šคํ…œ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ณต์œ  ํŒŒ์ผ ์‹œ์Šคํ…œ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ์—†์ด ๋กœ๋”ฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`checkpoint`](https://www.deepspeed.ai/docs/config-json/#checkpoint-options)๋ฅผ ํฌํ•จํ•˜๋„๋ก ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```yaml +{ + "checkpoint": { + "use_node_local_storage": true + } +} +``` + +[`Trainer`]์˜ ``--save_on_each_node` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ `checkpoint`๋ฅผ ๊ตฌ์„ฑ์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +[torchrun](https://pytorch.org/docs/stable/elastic/run.html)์˜ ๊ฒฝ์šฐ, ๊ฐ ๋…ธ๋“œ์— ssh๋กœ ์ ‘์†ํ•œ ํ›„ ๋‘ ๋…ธ๋“œ ๋ชจ๋‘์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋Ÿฐ์ฒ˜๋Š” ๋‘ ๋…ธ๋“œ๊ฐ€ ๋™๊ธฐํ™”๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ํŠธ๋ ˆ์ด๋‹์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. + +```bash +torchrun --nproc_per_node=8 --nnode=2 --node_rank=0 --master_addr=hostname1 \ +--master_port=9901 your_program.py --deepspeed ds_config.json +``` + + + + +`deepspeed` ๋Ÿฐ์ฒ˜์˜ ๊ฒฝ์šฐ, ๋จผ์ € `hostfile`์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +```bash +hostname1 slots=8 +hostname2 slots=8 +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํŠธ๋ ˆ์ด๋‹์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `deepspeed` ๋Ÿฐ์ฒ˜๋Š” ๋‘ ๋…ธ๋“œ์—์„œ ๋™์‹œ์— ๋ช…๋ น์„ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +```bash +deepspeed --num_gpus 8 --num_nodes 2 --hostfile hostfile --master_addr hostname1 --master_port=9901 \ +your_program.py --deepspeed ds_config.json +``` + +๋‹ค์ค‘ ๋…ธ๋“œ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Resource Configuration (multi-node)](https://www.deepspeed.ai/getting-started/#resource-configuration-multi-node) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + + + +### SLURM[[slurm]] + +SLURM ํ™˜๊ฒฝ์—์„œ๋Š” ํŠน์ • SLURM ํ™˜๊ฒฝ์— ๋งž๊ฒŒ SLURM ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.SLURM ์Šคํฌ๋ฆฝํŠธ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```bash +#SBATCH --job-name=test-nodes # ์ž‘์—… ์ด๋ฆ„ +#SBATCH --nodes=2 # ๋…ธ๋“œ ์ˆ˜ +#SBATCH --ntasks-per-node=1 # ์ค‘์š” - ๋…ธ๋“œ๋‹น ๋ถ„์‚ฐ ์ž‘์—… 1๊ฐœ! +#SBATCH --cpus-per-task=10 # ์ž‘์—…๋‹น CPU ์ฝ”์–ด ์ˆ˜ +#SBATCH --gres=gpu:8 # gpu ์ˆ˜ +#SBATCH --time 20:00:00 # ์ตœ๋Œ€ ์‹คํ–‰ ์‹œ๊ฐ„ (HH:MM:SS) +#SBATCH --output=%x-%j.out # ์ถœ๋ ฅ ํŒŒ์ผ ์ด๋ฆ„ + +export GPUS_PER_NODE=8 +export MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) +export MASTER_PORT=9901 + +srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ + --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ + --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ +your_program.py --deepspeed ds_config.json' +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋™์‹œ์— ํ•™์Šต์„ ์‹œ์ž‘ํ•˜๋Š” ๋‹ค์Œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์ค‘ ๋…ธ๋“œ ๋ฐฐํฌ๋ฅผ ์˜ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +sbatch launch.slurm +``` + +### ๋…ธํŠธ๋ถ[[notebook]] + +`deepspeed` ๋Ÿฐ์ฒ˜๋Š” ๋…ธํŠธ๋ถ์—์„œ์˜ ๋ฐฐํฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ ์—๋ฎฌ๋ ˆ์ด์…˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” 1๊ฐœ์˜ GPU์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. 1๊ฐœ ์ด์ƒ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋”ฅ์Šคํ”ผ๋“œ๊ฐ€ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ์—๋ฎฌ๋ ˆ์ด์…˜ํ•  ์ˆ˜ ์—†๋Š” `deepspeed` ๋Ÿฐ์ฒ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +# DeepSpeed๋Š” ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค๋งŒ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. +# ์ด ์ฝ”๋“œ๋กœ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ ๋ชจ๋ฐฉํ•ฉ๋‹ˆ๋‹ค. +import os + +os.environ["MASTER_ADDR"] = "localhost" +os.environ["MASTER_PORT"] = "9994" # RuntimeError: Address already in use ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์ˆ˜์ • +os.environ["RANK"] = "0" +os.environ["LOCAL_RANK"] = "0" +os.environ["WORLD_SIZE"] = "1" + +# ์ด์ œ ํ‰์†Œ์™€ ๊ฐ™์ด ์ง„ํ–‰ํ•˜๋˜, DeepSpeed ์„ค์ • ํŒŒ์ผ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +training_args = TrainingArguments(..., deepspeed="ds_config_zero3.json") +trainer = Trainer(...) +trainer.train() +``` + +ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋…ธํŠธ๋ถ์— ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ฆ‰์„์—์„œ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ „์šฉ ์…€์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +%%bash +cat <<'EOT' > ds_config_zero3.json +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + }, + + "optimizer": { + "type": "AdamW", + "params": { + "lr": "auto", + "betas": "auto", + "eps": "auto", + "weight_decay": "auto" + } + }, + + "scheduler": { + "type": "WarmupLR", + "params": { + "warmup_min_lr": "auto", + "warmup_max_lr": "auto", + "warmup_num_steps": "auto" + } + }, + + "zero_optimization": { + "stage": 3, + "offload_optimizer": { + "device": "cpu", + "pin_memory": true + }, + "offload_param": { + "device": "cpu", + "pin_memory": true + }, + "overlap_comm": true, + "contiguous_gradients": true, + "sub_group_size": 1e9, + "reduce_bucket_size": "auto", + "stage3_prefetch_bucket_size": "auto", + "stage3_param_persistence_threshold": "auto", + "stage3_max_live_parameters": 1e9, + "stage3_max_reuse_distance": 1e9, + "stage3_gather_16bit_weights_on_model_save": true + }, + + "gradient_accumulation_steps": "auto", + "gradient_clipping": "auto", + "steps_per_print": 2000, + "train_batch_size": "auto", + "train_micro_batch_size_per_gpu": "auto", + "wall_clock_breakdown": false +} +EOT +``` + +ํŠธ๋ ˆ์ด๋‹ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋…ธํŠธ๋ถ ์…€์ด ์•„๋‹Œ ํŒŒ์ผ์— ์žˆ๋Š” ๊ฒฝ์šฐ, ๋…ธํŠธ๋ถ ์…€์˜ ์…ธ์—์„œ `deepspeed`๋ฅผ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด `run_translation.py`๋ฅผ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜์„ธ์š”.: + +```py +!git clone https://github.com/huggingface/transformers +!cd transformers; deepspeed examples/pytorch/translation/run_translation.py ... +``` + +๋˜ํ•œ `%%bash` ๋งค์ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์…ธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ต์œก์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋กœ๊ทธ๋ฅผ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. `%%bash` ๋งค์ง์œผ๋กœ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ ์—๋ฎฌ๋ ˆ์ด์…˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. + +```py +%%bash + +git clone https://github.com/huggingface/transformers +cd transformers +deepspeed examples/pytorch/translation/run_translation.py ... +``` + +## ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ ์ €์žฅํ•˜๊ธฐ[[save-model-weights]] + +๋”ฅ์Šคํ”ผ๋“œ๋Š” ๊ธฐ๋ณธ ๊ณ ์ •๋ฐ€ fp32 ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉ์ž ์ง€์ • ์ฒดํฌํฌ์ธํŠธ ์ตœ์ ํ™” ํŒŒ์ผ(glob ํŒจํ„ด์€ `global_step*/*optim_states.pt`์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค)์— ์ €์žฅํ•˜๊ณ  ์ผ๋ฐ˜ ์ฒดํฌํฌ์ธํŠธ ์•„๋ž˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + + + + +ZeRO-2๋กœ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์€ pytorch_model.bin ๊ฐ€์ค‘์น˜๋ฅผ fp16์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ZeRO-3์œผ๋กœ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ fp16์— ์ €์žฅํ•˜๋ ค๋ฉด ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ์—ฌ๋Ÿฌ GPU์— ๋ถ„ํ• ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ `โ€œstage3_gather_16bit_weights_on_model_saveโ€: true`๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด [`Trainer`]๊ฐ€ ๊ฐ€์ค‘์น˜๋ฅผ fp16์— ์ €์žฅํ•˜์ง€ ์•Š๊ณ  pytorch_model.bin ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” DeepSpeed์˜ state_dict์— ์‹ค์ œ ๊ฐ€์ค‘์น˜ ๋Œ€์‹  ํ”Œ๋ ˆ์ด์Šคํ™€๋”๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ์ด๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +```yaml +{ + "zero_optimization": { + "stage3_gather_16bit_weights_on_model_save": true + } +} +``` + + + + +์ „์ฒด ์ •๋ฐ€ ๊ฐ€์ค‘์น˜๋Š” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํŠธ๋ ˆ์ด๋‹ ์ค‘์— ์ €์žฅํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋œ ํ›„ ์˜คํ”„๋ผ์ธ์œผ๋กœ fp32 ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์œ  CPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ ํ›ˆ๋ จ ์ค‘์— fp32 ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์˜จ๋ผ์ธ๊ณผ ์˜คํ”„๋ผ์ธ ๋ฐฉ์‹์„ ๋ชจ๋‘ ๋‹ค๋ฃน๋‹ˆ๋‹ค. + +### ์˜จ๋ผ์ธ ํ™˜๊ฒฝ[[online]] + +๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ตœ์‹  ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๋ ค๋ฉด ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ•˜๋‚˜ ์ด์ƒ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +from transformers.trainer_utils import get_last_checkpoint +from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint + +checkpoint_dir = get_last_checkpoint(trainer.args.output_dir) +fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) +``` + +`--load_best_model_at_end` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์„ฑํ™”ํ•˜์—ฌ [`TrainingArguments`]์—์„œ ์ตœ์ ์˜ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ถ”์ ํ•˜๋Š” ๊ฒฝ์šฐ, ๋จผ์ € ํ•™์Šต์„ ์™„๋ฃŒํ•˜๊ณ  ์ตœ์ข… ๋ชจ๋ธ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint + +checkpoint_dir = os.path.join(trainer.args.output_dir, "checkpoint-final") +trainer.deepspeed.save_checkpoint(checkpoint_dir) +fp32_model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir) +``` + + + +`load_state_dict_from_zero_checkpoint`๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๋™์ผํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปจํ…์ŠคํŠธ์—์„œ ๋ชจ๋ธ์„ ๋” ์ด์ƒ DeepSpeed์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. `model.load_state_dict(state_dict)`๋Š” ๋ชจ๋“  ๋”ฅ์Šคํ”ผ๋“œ ๋งˆ๋ฒ•์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ ๋”ฅ์Šคํ”ผ๋“œ ์—”์ง„์„ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํ›ˆ๋ จ์ด ๋๋‚  ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”. + + + +fp32 ๊ฐ€์ค‘์น˜์˜ state_dict๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint + +state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # cpu์— ์ด๋ฏธ ์กด์žฌํ•จ +model = model.cpu() +model.load_state_dict(state_dict) +``` + +### ์˜คํ”„๋ผ์ธ ํ™˜๊ฒฝ[[offline]] + +DeepSpeed๋Š” ์–ธ์ œ๋“ ์ง€ ๊ฐ€์ค‘์น˜๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒดํฌํฌ์ธํŠธ ํด๋”์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์— zero_to_fp32.py ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ๋…๋ฆฝํ˜• ์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌ์„ฑ ํŒŒ์ผ์ด๋‚˜ [`Trainer`]๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด ์ฒดํฌํฌ์ธํŠธ ํด๋”๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค: + +```bash +$ ls -l output_dir/checkpoint-1/ +-rw-rw-r-- 1 stas stas 1.4K Mar 27 20:42 config.json +drwxrwxr-x 2 stas stas 4.0K Mar 25 19:52 global_step1/ +-rw-rw-r-- 1 stas stas 12 Mar 27 13:16 latest +-rw-rw-r-- 1 stas stas 827K Mar 27 20:42 optimizer.pt +-rw-rw-r-- 1 stas stas 231M Mar 27 20:42 pytorch_model.bin +-rw-rw-r-- 1 stas stas 623 Mar 27 20:42 scheduler.pt +-rw-rw-r-- 1 stas stas 1.8K Mar 27 20:42 special_tokens_map.json +-rw-rw-r-- 1 stas stas 774K Mar 27 20:42 spiece.model +-rw-rw-r-- 1 stas stas 1.9K Mar 27 20:42 tokenizer_config.json +-rw-rw-r-- 1 stas stas 339 Mar 27 20:42 trainer_state.json +-rw-rw-r-- 1 stas stas 2.3K Mar 27 20:42 training_args.bin +-rwxrw-r-- 1 stas stas 5.5K Mar 27 13:16 zero_to_fp32.py* +``` + +๋”ฅ์Šคํ”ผ๋“œ ์ฒดํฌํฌ์ธํŠธ(ZeRO-2 ๋˜๋Š” ZeRO-3) ํ•˜์œ„ ํด๋” `global_step1`์—์„œ fp32 ๊ฐ€์ค‘์น˜๋ฅผ ์žฌ๊ตฌ์„ฑํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ์—ฌ๋Ÿฌ GPU์˜ ์ „์ฒด fp32 ๊ฐ€์ค‘์น˜๋ฅผ ๋‹จ์ผ pytorch_model.bin ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋Š” ์ž๋™์œผ๋กœ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ํฌํ•จ๋œ ํ•˜์œ„ ํด๋”๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. + +```py +python zero_to_fp32.py . pytorch_model.bin +``` + + + +์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ `python zero_to_fp32.py -h`๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”. ์ด ์Šคํฌ๋ฆฝํŠธ์—๋Š” ์ตœ์ข… fp32 ๊ฐ€์ค‘์น˜์˜ 2๋ฐฐ์˜ ์ผ๋ฐ˜ RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + + + + + + +## ZeRO Inference[[zero-inference]] + +[ZeRO Inference](https://www.deepspeed.ai/2022/09/09/zero-inference.html)๋Š” ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ CPU ๋˜๋Š” NVMe ๋ฉ”๋ชจ๋ฆฌ์— ๋ฐฐ์น˜ํ•˜์—ฌ GPU์— ๋ถ€๋‹ด์„ ์ฃผ์ง€ ์•Š์œผ๋ฏ€๋กœ GPU์—์„œ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๋ก ์€ ์ตœ์ ํ™” ์ƒํƒœ ๋ฐ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ์— ๋งŽ์€ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”๊ฐ€๋กœ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋™์ผํ•œ ํ•˜๋“œ์›จ์–ด์— ํ›จ์”ฌ ๋” ํฐ ๋ฐฐ์น˜ ๋ฐ/๋˜๋Š” ์‹œํ€€์Šค ๊ธธ์ด๋ฅผ ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ZeRO Inference๋Š” [ZeRO-3](#zero-configuration)์™€ ๋™์ผํ•œ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ๊ณต์œ ํ•˜๋ฉฐ, ZeRO-2 ๋ฐ ZeRO-1 ๊ตฌ์„ฑ์€ ์ถ”๋ก ์— ์•„๋ฌด๋Ÿฐ ์ด์ ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +ZeRO Inference๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ผ๋ฐ˜์ ์ธ ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ [`TrainingArguments`] ํด๋ž˜์Šค์— ์ „๋‹ฌํ•˜๊ณ  `--do_eval` ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +```bash +deepspeed --num_gpus=2 your_program.py --do_eval --deepspeed ds_config.json +``` + +## Trainer ์—†์ด DeepSpeed ์‚ฌ์šฉํ•˜๊ธฐ[[non-trainer-deepspeed-integration]] + +DeepSpeed๋Š” [`Trainer`] ํด๋ž˜์Šค๊ฐ€ ์—†๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋„ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” [`~PreTrainedModel.from_pretrained`]๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ZeRO-3 ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ชจ๋ธ์„ ์—ฌ๋Ÿฌ GPU์— ๋ถ„ํ• ํ•˜๋Š” ์ž‘์—…๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” [`HfDeepSpeedConfig`]๊ฐ€ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + + + +๋ชจ๋“  ๊ฒƒ์ด ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, [`Trainer`]์™€ ํ•จ๊ป˜ DeepSpeed๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์„ธ์š”! [DeepSpeed ๋ฌธ์„œ](https://www.deepspeed.ai/)๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์„ค์ • ํŒŒ์ผ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ์ˆ˜๋™์œผ๋กœ ๊ตฌ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(`"auto"` ๊ฐ’์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ). + + + +ZeRO-3๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋ฐฐํฌํ•˜๋ ค๋ฉด ๋ชจ๋ธ ์•ž์— [`HfDeepSpeedConfig`] ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + + + + +```py +from transformers.integrations import HfDeepSpeedConfig +from transformers import AutoModel +import deepspeed + +ds_config = {...} # deepspeed ์„ค์ • ๊ฐ์ฒด ๋˜๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ +# Zero 3๋ฅผ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +dschf = HfDeepSpeedConfig(ds_config) # ์ด ๊ฐ์ฒด๋ฅผ ์œ ์ง€ํ•˜์„ธ์š”. +model = AutoModel.from_pretrained("openai-community/gpt2") +engine = deepspeed.initialize(model=model, config_params=ds_config, ...) +``` + + + + +[`HfDeepSpeedConfig`] is not required for ZeRO-1 or ZeRO-2. + +```py +from transformers.integrations import HfDeepSpeedConfig +from transformers import AutoModel, AutoConfig +import deepspeed + +ds_config = {...} # deepspeed ์„ค์ • ๊ฐ์ฒด ๋˜๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ +# Zero 3๋ฅผ ๊ฐ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +dschf = HfDeepSpeedConfig(ds_config) # ์ด ๊ฐ์ฒด๋ฅผ ์œ ์ง€ํ•˜์„ธ์š”. +config = AutoConfig.from_pretrained("openai-community/gpt2") +model = AutoModel.from_config(config) +engine = deepspeed.initialize(model=model, config_params=ds_config, ...) +``` + + + + +### Trainer ์—†์ด ZeRO Inference ์‚ฌ์šฉํ•˜๊ธฐ[[non-trainer-zero-inference]] + +๋‹จ์ผ GPU์— ๋ชจ๋ธ์„ ๋งž์ถœ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ [`Trainer`]์—†์ด ZeRO ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ถ”๊ฐ€ GPU๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ CPU ๋ฉ”๋ชจ๋ฆฌ๋กœ ์˜คํ”„๋กœ๋“œ๋ฅผ ์‹œ๋„ํ•˜์„ธ์š”. ์—ฌ๊ธฐ์„œ ์ดํ•ดํ•ด์•ผ ํ•  ์ค‘์š”ํ•œ ๋‰˜์•™์Šค๋Š” ZeRO๊ฐ€ ์„ค๊ณ„๋œ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ GPU์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ž…๋ ฅ์„ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋ฐ˜๋“œ์‹œ ํ™•์ธํ•˜์„ธ์š”: + +* GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•œ ๊ฒฝ์šฐ CPU ์˜คํ”„๋กœ๋“œ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค(์†๋„๊ฐ€ ๋А๋ ค์ง€๋ฏ€๋กœ). +* Ampere ์ด์ƒ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ bf16์„ ํ™œ์„ฑํ™”ํ•˜๋ฉด ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ GPU๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์˜ค๋ฒ„ํ”Œ๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ bf16์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(T5 ๋ชจ๋ธ)์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ fp16์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๋ชจ๋ธ์—์„œ [`Trainer`] ์—†์ด ZeRO ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋” ๋‚˜์€ ์•„์ด๋””์–ด๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ดํŽด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +```py +#!/usr/bin/env python + +# ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹จ์ผ GPU์— ๋ชจ๋ธ์„ ๋งž์ถœ ์ˆ˜ ์—†์„ ๋•Œ ์ถ”๋ก  ๋ชจ๋“œ์—์„œ Deepspeed ZeRO๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. +# +# 1. CPU ์˜คํ”„๋กœ๋“œ์™€ ํ•จ๊ป˜ 1๊ฐœ์˜ GPU ์‚ฌ์šฉ +# 2. ๋˜๋Š” ์—ฌ๋Ÿฌ GPU ์‚ฌ์šฉ +# +# ๋จผ์ € deepspeed๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: pip install deepspeed +# +# ์—ฌ๊ธฐ์„œ๋Š” ์•ฝ 15GB์˜ GPU RAM์ด ํ•„์š”ํ•œ 3B "bigscience/T0_3B" ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค - ๋”ฐ๋ผ์„œ 1๊ฐœ์˜ ํฐ GPU๋‚˜ 2๊ฐœ์˜ +# ์ž‘์€ GPU๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” 1๊ฐœ์˜ ์ž‘์€ GPU์™€ ๋งŽ์€ CPU ๋ฉ”๋ชจ๋ฆฌ๋กœ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +# +# ์•ฝ 50GB๊ฐ€ ํ•„์š”ํ•œ "bigscience/T0"์™€ ๊ฐ™์€ ๋” ํฐ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, 80GB GPU๊ฐ€ ์—†๋Š” ํ•œ +# 2-4๊ฐœ์˜ GPU๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ ์ž…๋ ฅ์„ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด +# ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋” ๋งŽ์€ GPU๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +# +# ์ œ๊ณต๋œ deepspeed ์„ค์ •์€ CPU ๋ฉ”๋ชจ๋ฆฌ ์˜คํ”„๋กœ๋”ฉ๋„ ํ™œ์„ฑํ™”ํ•˜๋ฏ€๋กœ, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ CPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋งŽ๊ณ  +# ์†๋„ ์ €ํ•˜๋ฅผ ๊ฐ์ˆ˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +# GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด CPU๋กœ์˜ ์˜คํ”„๋กœ๋“œ๋ฅผ ์›ํ•˜์ง€ ์•Š์„ ๋•Œ ํ”„๋กœ๊ทธ๋žจ์ด ๋” ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค - ๊ทธ๋Ÿด ๋•Œ๋Š” ํ•ด๋‹น ์„น์…˜์„ ๋น„ํ™œ์„ฑํ™”ํ•˜์„ธ์š”. +# +# 1๊ฐœ์˜ GPU์— ๋ฐฐํฌํ•˜๋ ค๋ฉด: +# +# deepspeed --num_gpus 1 t0.py +# ๋˜๋Š”: +# python -m torch.distributed.run --nproc_per_node=1 t0.py +# +# 2๊ฐœ์˜ GPU์— ๋ฐฐํฌํ•˜๋ ค๋ฉด: +# +# deepspeed --num_gpus 2 t0.py +# ๋˜๋Š”: +# python -m torch.distributed.run --nproc_per_node=2 t0.py + +from transformers import AutoTokenizer, AutoConfig, AutoModelForSeq2SeqLM +from transformers.integrations import HfDeepSpeedConfig +import deepspeed +import os +import torch + +os.environ["TOKENIZERS_PARALLELISM"] = "false" # ํ† ํฌ๋‚˜์ด์ €์˜ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ๊ฒฝ๊ณ ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. + +# ๋ถ„์‚ฐ ํ™˜๊ฒฝ ์„ค์ • +local_rank = int(os.getenv("LOCAL_RANK", "0")) +world_size = int(os.getenv("WORLD_SIZE", "1")) +torch.cuda.set_device(local_rank) +deepspeed.init_distributed() + +model_name = "bigscience/T0_3B" + +config = AutoConfig.from_pretrained(model_name) +model_hidden_size = config.d_model + +# ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” world_size๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ ธ์•ผ ํ•˜์ง€๋งŒ, world_size๋ณด๋‹ค ํด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค +train_batch_size = 1 * world_size + +# ds_config ์ฐธ๊ณ ์‚ฌํ•ญ +# +# - Ampere ์ด์ƒ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ bf16์„ ํ™œ์„ฑํ™”ํ•˜์„ธ์š” - ์ด๋Š” ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ์‹คํ–‰๋˜์–ด +# ๋” ๋น ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +# +# - ์˜ค๋ž˜๋œ GPU์˜ ๊ฒฝ์šฐ fp16์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, bf16์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋˜์ง€ ์•Š์€ ๋ชจ๋ธ์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค - ์˜ˆ๋ฅผ ๋“ค์–ด +# ๋ชจ๋“  ๊ณต์‹ t5 ๋ชจ๋ธ์€ bf16์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค +# +# - CPU ์˜คํ”„๋กœ๋“œ๋ฅผ ์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด offload_param.device๋ฅผ "none"์œผ๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ `offload_param` ์„น์…˜์„ +# ์™„์ „ํžˆ ์ œ๊ฑฐํ•˜์„ธ์š” +# +# - `offload_param`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, stage3_param_persistence_threshold๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ +# ์–ด๋–ค ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ GPU์— ๋‚จ์•„์žˆ์–ด์•ผ ํ•˜๋Š”์ง€ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค - ๊ฐ’์ด ํด์ˆ˜๋ก ์˜คํ”„๋กœ๋“œ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ง‘๋‹ˆ๋‹ค +# +# Deepspeed ์„ค์ •์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” ๋‹ค์Œ์„ ์ฐธ์กฐํ•˜์„ธ์š” +# https://huggingface.co/docs/transformers/main/main_classes/deepspeed + +# ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด json๊ณผ ๋™์ผํ•œ ํ˜•์‹์„ ์œ ์ง€ํ•˜๋˜, true/false์—๋Š” ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค +# fmt: off +ds_config = { + "fp16": { + "enabled": False + }, + "bf16": { + "enabled": False + }, + "zero_optimization": { + "stage": 3, + "offload_param": { + "device": "cpu", + "pin_memory": True + }, + "overlap_comm": True, + "contiguous_gradients": True, + "reduce_bucket_size": model_hidden_size * model_hidden_size, + "stage3_prefetch_bucket_size": 0.9 * model_hidden_size * model_hidden_size, + "stage3_param_persistence_threshold": 10 * model_hidden_size + }, + "steps_per_print": 2000, + "train_batch_size": train_batch_size, + "train_micro_batch_size_per_gpu": 1, + "wall_clock_breakdown": False +} +# fmt: on + +# ๋‹ค์Œ ์ค„์€ ๋ชจ๋ธ์˜ `from_pretrained` ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ +# deepspeed.zero.Init๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์—ฌ๋Ÿฌ GPU์— ์ง์ ‘ ๋ถ„ํ• ํ•˜๋„๋ก transformers์— ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. +# +# **์ด๋Š” AutoModelForSeq2SeqLM.from_pretrained(model_name)๋กœ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค** +# +# ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ชจ๋ธ์ด ๋จผ์ € ์ •์ƒ์ ์œผ๋กœ ๋กœ๋“œ๋œ ํ›„ ํฌ์›Œ๋“œ ์‹œ์—๋งŒ ๋ถ„ํ• ๋˜๋Š”๋ฐ, ์ด๋Š” +# ๋œ ํšจ์œจ์ ์ด๋ฉฐ CPU RAM์ด ๋ถ€์กฑํ•  ๊ฒฝ์šฐ ์‹คํŒจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค +dschf = HfDeepSpeedConfig(ds_config) # ์ด ๊ฐ์ฒด๋ฅผ ์œ ์ง€ํ•˜์„ธ์š” + +# ์ด์ œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +model = AutoModelForSeq2SeqLM.from_pretrained(model_name) + +# Deepspeed ZeRO๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์—”์ง„ ๊ฐ์ฒด๋งŒ ์ €์žฅ +ds_engine = deepspeed.initialize(model=model, config_params=ds_config)[0] +ds_engine.module.eval() # inference + +# Deepspeed ZeRO๋Š” ๊ฐ GPU์—์„œ ์„œ๋กœ ๊ด€๋ จ ์—†๋Š” ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ๋ฒˆ์— 2๊ฐœ์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +# GPU๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ทธ์— ๋งž๊ฒŒ ์กฐ์ •ํ•˜์„ธ์š”. + +# ๋ฌผ๋ก  ์ฒ˜๋ฆฌํ•  ์ž…๋ ฅ์ด ํ•˜๋‚˜๋ฟ์ด๋ผ๋ฉด ๋‘ GPU์— ๋™์ผํ•œ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +# GPU๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” rank 0๋งŒ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. +rank = torch.distributed.get_rank() +if rank == 0: + text_in = "Is this review positive or negative? Review: this is the best cast iron skillet you will ever buy" +elif rank == 1: + text_in = "Is this review positive or negative? Review: this is the worst restaurant ever" + +tokenizer = AutoTokenizer.from_pretrained(model_name) +inputs = tokenizer.encode(text_in, return_tensors="pt").to(device=local_rank) +with torch.no_grad(): + outputs = ds_engine.module.generate(inputs, synced_gpus=True) +text_out = tokenizer.decode(outputs[0], skip_special_tokens=True) +print(f"rank{rank}:\n in={text_in}\n out={text_out}") +``` + +์Šคํฌ๋ฆฝํŠธ๋ฅผ t0.py๋กœ ์ €์žฅํ•˜๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: + +```bash +$ deepspeed --num_gpus 2 t0.py +rank0: + in=Is this review positive or negative? Review: this is the best cast iron skillet you will ever buy + out=Positive +rank1: + in=Is this review positive or negative? Review: this is the worst restaurant ever + out=negative +``` + +์ด๊ฒƒ์€ ๋งค์šฐ ๊ธฐ๋ณธ์ ์ธ ์˜ˆ์‹œ์ด๋ฏ€๋กœ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ์ƒ์„ฑ[[generate]] + +์ƒ์„ฑ์— ZeRO-3์™€ ํ•จ๊ป˜ ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด [`~GenerationMixin.generate`] ๋ฉ”์„œ๋“œ์—์„œ `synced_gpus=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ GPU๋ฅผ ๋™๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ•œ GPU๊ฐ€ ๋‹ค๋ฅธ GPU๋ณด๋‹ค ๋จผ์ € ์ƒ์„ฑ์„ ์™„๋ฃŒํ•˜๋ฉด ๋‚˜๋จธ์ง€ GPU๊ฐ€ ๋จผ์ € ์™„๋ฃŒํ•œ GPU๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ์ƒค๋“œ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•˜์—ฌ ์ „์ฒด ์‹œ์Šคํ…œ์ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. + +ํŠธ๋žœ์Šคํฌ๋จธ>=4.28์˜ ๊ฒฝ์šฐ, ์ƒ์„ฑ ์ค‘์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๊ฐ€ ๊ฐ์ง€๋˜๋ฉด `synced_gpus`๊ฐ€ ์ž๋™์œผ๋กœ `True`๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. + +## ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…[[troubleshoot]] + +๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด DeepSpeed๊ฐ€ ๋ฌธ์ œ์˜ ์›์ธ์ด ์•„๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ(์•„์ฃผ ๋ช…๋ฐฑํ•˜๊ณ  ์˜ˆ์™ธ์ ์œผ๋กœ DeepSpeed ๋ชจ๋“ˆ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด) DeepSpeed๊ฐ€ ๋ฌธ์ œ์˜ ์›์ธ์ธ์ง€ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” DeepSpeed ์—†์ด ์„ค์ •์„ ๋‹ค์‹œ ์‹œ๋„ํ•˜๊ณ  ๋ฌธ์ œ๊ฐ€ ์ง€์†๋˜๋ฉด ๋ฌธ์ œ๋ฅผ ์‹ ๊ณ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ๊ฐ€ ํ•ต์‹ฌ์ ์ธ DeepSpeed ๋ฌธ์ œ์ด๊ณ  transformers์™€ ๊ด€๋ จ์ด ์—†๋Š” ๊ฒฝ์šฐ, [DeepSpeed ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/deepspeedai/DeepSpeed)์—์„œ ์ด์Šˆ๋ฅผ ๊ฐœ์„คํ•˜์„ธ์š”. + +transformers์™€ ๊ด€๋ จ๋œ ์ด์Šˆ๋ฅผ ๊ฐœ์„คํ•  ๋•Œ์—๋Š” ๋‹ค์Œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด ์ฃผ์„ธ์š”: + +* ์ „์ฒด DeepSpeed ๊ตฌ์„ฑ ํŒŒ์ผ + +*[`Trainer`]์˜ ๋ช…๋ น์ค„ ์ธ์ˆ˜, ๋˜๋Š”[`Trainer`] ์„ค์ •์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ[`TrainingArguments`] ์ธ์ˆ˜(๊ด€๋ จ ์—†๋Š” ํ•ญ๋ชฉ์ด ์ˆ˜์‹ญ ๊ฐœ ์žˆ๋Š” [`TrainingArguments`]๋Š” ๋คํ”„ํ•˜์ง€ ๋งˆ์„ธ์š”). + +* ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ: + +```bash +python -c 'import torch; print(f"torch: {torch.__version__}")' +python -c 'import transformers; print(f"transformers: {transformers.__version__}")' +python -c 'import deepspeed; print(f"deepspeed: {deepspeed.__version__}")' +``` + +* ๋ฌธ์ œ๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” Google Colab ๋…ธํŠธ๋ถ ๋งํฌ + +* ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ ๊ธฐ์กด ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ์ค€ ๋ฐ ์‚ฌ์šฉ์ž ์ง€์ •์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ด๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +### DeepSpeed ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘ ๋‹จ๊ณ„์—์„œ ์ข…๋ฃŒ๋˜์—ˆ์„ ๊ฒฝ์šฐ[[deepspeed-process-killed-at-startup]] + +์‹คํ–‰ ์ค‘์— ํŠธ๋ ˆ์ด์Šค๋ฐฑ ์—†์ด DeepSpeed ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์Šคํ…œ๋ณด๋‹ค ๋งŽ์€ CPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋ ค๊ณ  ์‹œ๋„ํ–ˆ๊ฑฐ๋‚˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ—ˆ์šฉ๋œ ๊ฒƒ๋ณด๋‹ค ๋งŽ์€ CPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜์—ฌ OS ์ปค๋„์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ–ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ตฌ์„ฑ ํŒŒ์ผ์— `offload_optimizer`, `offload_param` ๋˜๋Š” ๋‘˜ ๋‹ค CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜๋„๋ก ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +NVMe ๋ฐ ZeRO-3๋ฅผ ์„ค์ •ํ•œ ๊ฒฝ์šฐ NVMe๋กœ ์˜คํ”„๋กœ๋“œ๋ฅผ ์‹คํ—˜ํ•ด ๋ณด์„ธ์š”(๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ [ํ™•์ธ](https://deepspeed.readthedocs.io/en/latest/memory.html)ํ•˜์„ธ์š”). + +### NaN ์†์‹ค[[nan-loss]] + +๋ชจ๋ธ์„ bf16์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จํ•œ ๋‹ค์Œ fp16์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ NaN ์†์‹ค์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค(ํŠนํžˆ TPU ํ›ˆ๋ จ ๋ชจ๋ธ์— ํ•ด๋‹น). ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ํ•˜๋“œ์›จ์–ด๊ฐ€ ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ(TPU, Ampere GPU ์ด์ƒ) fp32 ๋˜๋Š” bf16์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + +๋‹ค๋ฅธ ๋ฌธ์ œ๋Š” fp16 ์‚ฌ์šฉ๊ณผ ๊ด€๋ จ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๊ฒƒ์ด fp16 ๊ตฌ์„ฑ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค: + +```yaml +{ + "fp16": { + "enabled": "auto", + "loss_scale": 0, + "loss_scale_window": 1000, + "initial_scale_power": 16, + "hysteresis": 2, + "min_loss_scale": 1 + } +} +``` + +๋กœ๊ทธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ `OVERFLOW!` ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +0%| | 0/189 [00:00 + + +# ExecuTorch [[executorch]] + +[`ExecuTorch`](https://github.com/pytorch/executorch) ๋Š” ์›จ์–ด๋Ÿฌ๋ธ”, ์ž„๋ฒ ๋””๋“œ ์žฅ์น˜, ๋งˆ์ดํฌ๋กœ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํฌํ•จํ•œ ๋ชจ๋ฐ”์ผ ๋ฐ ์—ฃ์ง€ ์žฅ์น˜์—์„œ ์˜จ๋””๋ฐ”์ด์Šค ์ถ”๋ก  ๊ธฐ๋Šฅ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์ข…ํ•ฉ ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. PyTorch ์ƒํƒœ๊ณ„์— ์†ํ•ด์žˆ์œผ๋ฉฐ, ์ด์‹์„ฑ, ์ƒ์‚ฐ์„ฑ, ์„ฑ๋Šฅ์— ์ค‘์ ์„ ๋‘” PyTorch ๋ชจ๋ธ ๋ฐฐํฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +ExecuTorch๋Š” ๋ฐฑ์—”๋“œ ์œ„์ž„, ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํŒŒ์ผ๋Ÿฌ ๋ณ€ํ™˜, ๋ฉ”๋ชจ๋ฆฌ ๊ณ„ํš ๋“ฑ ๋ชจ๋ธ, ์žฅ์น˜ ๋˜๋Š” ํŠน์ • ์œ ์ฆˆ์ผ€์ด์Šค ๋งž์ถค ์ตœ์ ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ง„์ž…์ ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ExecuTorch๋ฅผ ์‚ฌ์šฉํ•ด ์—ฃ์ง€ ์žฅ์น˜์—์„œ PyTorch ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๋ชจ๋ธ์„ ์ต์ŠคํฌํŠธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ PyTorch API์ธ [`torch.export`](https://pytorch.org/docs/stable/export.html)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + + +## ExecuTorch ํ†ตํ•ฉ [[transformers.TorchExportableModuleWithStaticCache]] + +`torch.export`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๐Ÿค— Transformers๋ฅผ ์ต์ŠคํฌํŠธ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ†ตํ•ฉ ์ง€์ ์ด ๊ฐœ๋ฐœ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ†ตํ•ฉ์˜ ๋ชฉํ‘œ๋Š” ์ต์ŠคํฌํŠธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ต์ŠคํฌํŠธํ•œ ์•„ํ‹ฐํŒฉํŠธ๊ฐ€ `ExecuTorch`์—์„œ ํšจ์œจ์ ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ๋” ์ถ•์†Œํ•˜๊ณ  ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ๋ชจ๋ฐ”์ผ ๋ฐ ์—ฃ์ง€ ์œ ์ฆˆ์ผ€์ด์Šค์— ์ค‘์ ์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +[[autodoc]] integrations.executorch.TorchExportableModuleWithStaticCache + - forward + +[[autodoc]] integrations.executorch.convert_and_export_with_cache diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fast_tokenizers.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fast_tokenizers.md new file mode 100644 index 0000000000000000000000000000000000000000..a6d1f14283bbc5d8f66356143fe1ca8ea42c0867 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fast_tokenizers.md @@ -0,0 +1,71 @@ + + +# ๐Ÿค— Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ† ํฌ๋‚˜์ด์ € ์‚ฌ์šฉํ•˜๊ธฐ[[use-tokenizers-from-tokenizers]] + +[`PreTrainedTokenizerFast`]๋Š” [๐Ÿค— Tokenizers](https://huggingface.co/docs/tokenizers) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿค— Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ† ํฌ๋‚˜์ด์ €๋Š” +๐Ÿค— Transformers๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์— ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—, ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋กœ ๋”๋ฏธ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +>>> from tokenizers import Tokenizer +>>> from tokenizers.models import BPE +>>> from tokenizers.trainers import BpeTrainer +>>> from tokenizers.pre_tokenizers import Whitespace + +>>> tokenizer = Tokenizer(BPE(unk_token="[UNK]")) +>>> trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]) + +>>> tokenizer.pre_tokenizer = Whitespace() +>>> files = [...] +>>> tokenizer.train(files, trainer) +``` + +์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ํŒŒ์ผ์„ ํ†ตํ•ด ์ด์ œ ํ•™์Šต๋œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๊ฐ–๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋Ÿฐํƒ€์ž„์—์„œ ๊ณ„์† ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ JSON ํŒŒ์ผ๋กœ ์ €์žฅํ•˜์—ฌ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ํ† ํฌ๋‚˜์ด์ € ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[loading-directly-from-the-tokenizer-object]] + +๐Ÿค— Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ด ํ† ํฌ๋‚˜์ด์ € ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +[`PreTrainedTokenizerFast`] ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šคํ™”๋œ *ํ† ํฌ๋‚˜์ด์ €* ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ์‰ฝ๊ฒŒ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer) +``` + +์ด์ œ `fast_tokenizer` ๊ฐ์ฒด๋Š” ๐Ÿค— Transformers ํ† ํฌ๋‚˜์ด์ €์—์„œ ๊ณต์œ ํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ† ํฌ๋‚˜์ด์ € ํŽ˜์ด์ง€](main_classes/tokenizer)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## JSON ํŒŒ์ผ์—์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[loading-from-a-JSON-file]] + + + +JSON ํŒŒ์ผ์—์„œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด, ๋จผ์ € ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +>>> tokenizer.save("tokenizer.json") +``` + +JSON ํŒŒ์ผ์„ ์ €์žฅํ•œ ๊ฒฝ๋กœ๋Š” `tokenizer_file` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [`PreTrainedTokenizerFast`] ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import PreTrainedTokenizerFast + +>>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json") +``` + +์ด์ œ `fast_tokenizer` ๊ฐ์ฒด๋Š” ๐Ÿค— Transformers ํ† ํฌ๋‚˜์ด์ €์—์„œ ๊ณต์œ ํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ† ํฌ๋‚˜์ด์ € ํŽ˜์ด์ง€](main_classes/tokenizer)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fsdp.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fsdp.md new file mode 100644 index 0000000000000000000000000000000000000000..bab1fda71b4ed10542aea99837bbd06154abd888 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/fsdp.md @@ -0,0 +1,138 @@ + + +# ์™„์ „ ๋ถ„ํ•  ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ(FSDP) [[fully-sharded-data-parallel]] + +[Fully Sharded Data Parallel (FSDP)](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/)์€ ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜, ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU(์ž‘์—…์ž ๋˜๋Š” *๋žญํฌ*๋ผ๊ณ ๋„ ํ•จ) ์ˆ˜์— ๋”ฐ๋ผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. [DistributedDataParallel (DDP)](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html)์™€ ๋‹ฌ๋ฆฌ, FSDP๋Š” ๊ฐ GPU์— ๋ชจ๋ธ์„ ๋ณต์ œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ด๋Š” GPU ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๋ฉฐ ์ ์€ ์ˆ˜์˜ GPU๋กœ ํ›จ์”ฌ ๋” ํฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. FSDP๋Š” ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ์˜ ํ›ˆ๋ จ์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Accelerate์™€ ํ†ตํ•ฉ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋”ฐ๋ผ์„œ [`Trainer`] ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Accelerate๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๊ณ  ์ตœ์†Œ PyTorch 2.1.0 ์ด์ƒ์˜ ๋ฒ„์ „์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```bash +pip install accelerate +``` + +## FSDP ๊ตฌ์„ฑ [[fsdp-configuration]] + +์‹œ์ž‘ํ•˜๋ ค๋ฉด [`accelerate config`](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-config) ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ํ›ˆ๋ จ ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์„ธ์š”. Accelerate๋Š” ์ด ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ `accelerate config`์—์„œ ์„ ํƒํ•œ ํ›ˆ๋ จ ์˜ต์…˜์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ํ›ˆ๋ จ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```bash +accelerate config +``` + +`accelerate config`๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ›ˆ๋ จ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ผ๋ จ์˜ ์˜ต์…˜๋“ค์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ FSDP ์˜ต์…˜ ์ค‘ ์ผ๋ถ€๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ FSDP ์˜ต์…˜์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด [fsdp_config](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.fsdp_config) ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +### ๋ถ„ํ•  ์ „๋žต [[sharding-strategy]] + +FSDP๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ถ„ํ•  ์ „๋žต์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +* `FULL_SHARD` - ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜, ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ์ž‘์—…์ž ๊ฐ„์— ๋ถ„ํ• ; ์ด ์˜ต์…˜์„ ์„ ํƒํ•˜๋ ค๋ฉด `1`์„ ์„ ํƒํ•˜์„ธ์š” +* `SHARD_GRAD_OP` - ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ์ž‘์—…์ž ๊ฐ„์— ๋ถ„ํ• ; ์ด ์˜ต์…˜์„ ์„ ํƒํ•˜๋ ค๋ฉด `2`๋ฅผ ์„ ํƒํ•˜์„ธ์š” +* `NO_SHARD` - ์•„๋ฌด ๊ฒƒ๋„ ๋ถ„ํ• ํ•˜์ง€ ์•Š์Œ (DDP์™€ ๋™์ผ); ์ด ์˜ต์…˜์„ ์„ ํƒํ•˜๋ ค๋ฉด `3`์„ ์„ ํƒํ•˜์„ธ์š” +* `HYBRID_SHARD` - ๊ฐ ์ž‘์—…์ž๊ฐ€ ์ „์ฒด ๋ณต์‚ฌ๋ณธ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒํƒœ์—์„œ ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜, ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ์ž‘์—…์ž ๋‚ด์—์„œ ๋ถ„ํ• ; ์ด ์˜ต์…˜์„ ์„ ํƒํ•˜๋ ค๋ฉด `4`๋ฅผ ์„ ํƒํ•˜์„ธ์š” +* `HYBRID_SHARD_ZERO2` - ๊ฐ ์ž‘์—…์ž๊ฐ€ ์ „์ฒด ๋ณต์‚ฌ๋ณธ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒํƒœ์—์„œ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ์ž‘์—…์ž ๋‚ด์—์„œ ๋ถ„ํ• ; ์ด ์˜ต์…˜์„ ์„ ํƒํ•˜๋ ค๋ฉด `5`๋ฅผ ์„ ํƒํ•˜์„ธ์š” + +์ด๊ฒƒ์€ `fsdp_sharding_strategy` ํ”Œ๋ž˜๊ทธ๋กœ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. + +### CPU ์˜คํ”„๋กœ๋“œ [[cpu-offload]] + +์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜์—ฌ ๋” ๋งŽ์€ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ณ  FSDP๋กœ๋„ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ํฐ ๋ชจ๋ธ์„ GPU์— ์ ์žฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” `accelerate config`๋ฅผ ์‹คํ–‰ํ•  ๋•Œ `fsdp_offload_params: true`๋กœ ์„ค์ •ํ•˜์—ฌ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. + +### ๋ž˜ํ•‘ ์ •์ฑ… [[wrapping-policy]] + +FSDP๋Š” ๋„คํŠธ์›Œํฌ์˜ ๊ฐ ๋ ˆ์ด์–ด๋ฅผ ๋ž˜ํ•‘ํ•˜์—ฌ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ž˜ํ•‘์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ค‘์ฒฉ ๋ฐฉ์‹์œผ๋กœ ์ ์šฉ๋˜๋ฉฐ ๊ฐ๊ฐ ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ ์ง€๋‚˜๊ฐ„ ํ›„ ์ „์ฒด ๊ฐ€์ค‘์น˜๋ฅผ ์‚ญ์ œํ•˜์—ฌ ๋‹ค์Œ ๋ ˆ์ด์–ด์—์„œ ์‚ฌ์šฉํ•  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. *์ž๋™ ๋ž˜ํ•‘* ์ •์ฑ…์€ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด๋ฉฐ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Transformer ๋ ˆ์ด์–ด๋ฅผ ๋ž˜ํ•‘ํ•˜๋ ค๋ฉด `fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP`๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ž˜ํ•‘ํ•  ๋ ˆ์ด์–ด๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด `fsdp_transformer_layer_cls_to_wrap`๋ฅผ ์„ ํƒํ•˜์„ธ์š” (์˜ˆ: `BertLayer`). + +๋˜๋Š” ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ FSDP๊ฐ€ ๋ ˆ์ด์–ด์— ์ ์šฉ๋˜๋Š” ํฌ๊ธฐ ๊ธฐ๋ฐ˜ ๋ž˜ํ•‘ ์ •์ฑ…์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” `fsdp_wrap_policy: SIZE_BASED_WRAP` ๋ฐ `min_num_param`์„ ์›ํ•˜๋Š” ํฌ๊ธฐ์˜ ์ž„๊ณ„๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. + +### ์ฒดํฌํฌ์ธํŠธ [[checkpointing]] + +์ค‘๊ฐ„ ์ฒดํฌํฌ์ธํŠธ๋Š” `fsdp_state_dict_type: SHARDED_STATE_DICT`๋กœ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. CPU ์˜คํ”„๋กœ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋œ ๋žญํฌ 0์—์„œ ์ „์ฒด ์ƒํƒœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ , ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ… ์ค‘ ๋ฌด๊ธฐํ•œ ๋Œ€๊ธฐํ•˜์—ฌ `NCCL Timeout` ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. [`~accelerate.Accelerator.load_state`] ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„ํ• ๋œ ์ƒํƒœ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +# ๊ฒฝ๋กœ๊ฐ€ ๋‚ด์žฌ๋œ ์ฒดํฌํฌ์ธํŠธ +accelerator.load_state("ckpt") +``` + +๊ทธ๋Ÿฌ๋‚˜ ํ›ˆ๋ จ์ด ๋๋‚˜๋ฉด ์ „์ฒด ์ƒํƒœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„ํ• ๋œ ์ƒํƒœ ๋”•์…”๋„ˆ๋ฆฌ๋Š” FSDP์™€๋งŒ ํ˜ธํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +```py +if trainer.is_fsdp_enabled: + trainer.accelerator.state.fsdp_plugin.set_state_dict_type("FULL_STATE_DICT") + +trainer.save_model(script_args.output_dir) +``` + +### TPU [[tpu]] + +[PyTorch XLA](https://pytorch.org/xla/release/2.1/index.html)๋Š” TPU์— ๋Œ€ํ•œ FSDP ํ›ˆ๋ จ์„ ์ง€์›ํ•˜๋ฉฐ `accelerate config`๋กœ ์ƒ์„ฑ๋œ FSDP ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์ง€์ •ํ•œ ๋ถ„ํ•  ์ „๋žต ๋ฐ ๋ž˜ํ•‘ ์˜ต์…˜ ์™ธ์—๋„ ์•„๋ž˜์— ํ‘œ์‹œ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```yaml +xla: True # PyTorch/XLA๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด True๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค +xla_fsdp_settings: # XLA ํŠน์ • FSDP ๋งค๊ฐœ๋ณ€์ˆ˜ +xla_fsdp_grad_ckpt: True # gradient checkpointing์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค +``` + +[`xla_fsdp_settings`](https://github.com/pytorch/xla/blob/2e6e183e0724818f137c8135b34ef273dea33318/torch_xla/distributed/fsdp/xla_fully_sharded_data_parallel.py#L128)๋Š” FSDP์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ XLA ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +## ํ›ˆ๋ จ ์‹œ์ž‘ [[launch-training]] + +์˜ˆ์‹œ FSDP ๊ตฌ์„ฑ ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```yaml +compute_environment: LOCAL_MACHINE +debug: false +distributed_type: FSDP +downcast_bf16: 'no' +fsdp_config: + fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP + fsdp_backward_prefetch_policy: BACKWARD_PRE + fsdp_cpu_ram_efficient_loading: true + fsdp_forward_prefetch: false + fsdp_offload_params: true + fsdp_sharding_strategy: 1 + fsdp_state_dict_type: SHARDED_STATE_DICT + fsdp_sync_module_states: true + fsdp_transformer_layer_cls_to_wrap: BertLayer + fsdp_use_orig_params: true +machine_rank: 0 +main_training_function: main +mixed_precision: bf16 +num_machines: 1 +num_processes: 2 +rdzv_backend: static +same_network: true +tpu_env: [] +tpu_use_cluster: false +tpu_use_sudo: false +use_cpu: false +``` + +ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด [`accelerate launch`](https://huggingface.co/docs/accelerate/package_reference/cli#accelerate-launch) ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”. ์ด ๋•Œ ์ „์— `accelerate config`๋กœ ์ƒ์„ฑํ•œ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```bash +accelerate launch my-trainer-script.py +``` + +```bash +accelerate launch --fsdp="full shard" --fsdp_config="path/to/fsdp_config/ my-trainer-script.py +``` + +## ๋‹ค์Œ ๋‹จ๊ณ„ [[next-steps]] + +FSDP๋Š” ๋งค์šฐ ํฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ๋•Œ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋‚˜ TPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ƒํƒœ๋ฅผ ๋ถ„ํ• ํ•˜๊ณ  ๋น„ํ™œ์„ฑ ์ƒํƒœ์ผ ๋•Œ, CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜๋ฉด FSDP๋Š” ๋Œ€๊ทœ๋ชจ ํ›ˆ๋ จ์˜ ๋†’์€ ์—ฐ์‚ฐ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์•Œ์•„๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ ์ž๋ฃŒ๊ฐ€ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +* [FSDP](https://huggingface.co/docs/accelerate/usage_guides/fsdp)์— ๋Œ€ํ•œ ๋” ๊นŠ์ด ์žˆ๋Š” Accelerate ๊ฐ€์ด๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€ ๋ณด์„ธ์š”. +* [PyTorch์˜ ์™„์ „ ๋ถ„ํ•  ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ (FSDP) API๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค](https://pytorch.org/blog/introducing-pytorch-fully-sharded-data-parallel-api/) ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฝ์–ด๋ณด์„ธ์š”. +* [FSDP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์šฐ๋“œ TPU์—์„œ PyTorch ๋ชจ๋ธ ํฌ๊ธฐ ์กฐ์ ˆํ•˜๊ธฐ](https://pytorch.org/blog/scaling-pytorch-models-on-cloud-tpus-with-fsdp/) ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฝ์–ด๋ณด์„ธ์š”. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/generation_strategies.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/generation_strategies.md new file mode 100644 index 0000000000000000000000000000000000000000..f45fea5b2280b416d85ac7abeef1f08991cd3e08 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/generation_strategies.md @@ -0,0 +1,337 @@ + + +# Text generation strategies[[text-generation-strategies]] + +ํ…์ŠคํŠธ ์ƒ์„ฑ์€ ๊ฐœ๋ฐฉํ˜• ํ…์ŠคํŠธ ์ž‘์„ฑ, ์š”์•ฝ, ๋ฒˆ์—ญ ๋“ฑ ๋‹ค์–‘ํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) ์ž‘์—…์— ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋˜ํ•œ ์Œ์„ฑ-ํ…์ŠคํŠธ ๋ณ€ํ™˜, ์‹œ๊ฐ-ํ…์ŠคํŠธ ๋ณ€ํ™˜๊ณผ ๊ฐ™์ด ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ์œผ๋กœ ํ•˜๋Š” ์—ฌ๋Ÿฌ ํ˜ผํ•ฉ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋„ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๋ช‡๋ช‡ ๋ชจ๋ธ๋กœ๋Š” GPT2, XLNet, OpenAI GPT, CTRL, TransformerXL, XLM, Bart, T5, GIT, Whisper ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + + +[`~generation.GenerationMixin.generate`] ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž‘์—…๋“ค์— ๋Œ€ํ•ด ํ…์ŠคํŠธ ๊ฒฐ๊ณผ๋ฌผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”: +* [ํ…์ŠคํŠธ ์š”์•ฝ](./tasks/summarization#inference) +* [์ด๋ฏธ์ง€ ์บก์…”๋‹](./model_doc/git#transformers.GitForCausalLM.forward.example) +* [์˜ค๋””์˜ค ์ „์‚ฌ](./model_doc/whisper#transformers.WhisperForConditionalGeneration.forward.example) + +generate ๋ฉ”์†Œ๋“œ์— ์ž…๋ ฅ๋˜๋Š” ๊ฐ’๋“ค์€ ๋ชจ๋ธ์˜ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด ๊ฐ’๋“ค์€ AutoTokenizer๋‚˜ AutoProcessor์™€ ๊ฐ™์€ ๋ชจ๋ธ์˜ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค์— ์˜ํ•ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์ „์ฒ˜๋ฆฌ ์žฅ์น˜๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ž…๋ ฅ ์œ ํ˜•์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“  ์ž…๋ ฅ์„ generate()์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋ธ์˜ ์ „์ฒ˜๋ฆฌ ์žฅ์น˜์— ๋Œ€ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ชจ๋ธ์˜ ๋ฌธ์„œ์—์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ถœ๋ ฅ ํ† ํฐ์„ ์„ ํƒํ•˜๋Š” ๊ณผ์ •์„ ๋””์ฝ”๋”ฉ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, `generate()` ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉํ•  ๋””์ฝ”๋”ฉ ์ „๋žต์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”ฉ ์ „๋žต์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ ํ›ˆ๋ จ ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’๋“ค์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์ง€๋งŒ, ์ƒ์„ฑ๋œ ์ถœ๋ ฅ์˜ ํ’ˆ์งˆ์— ๋ˆˆ์— ๋„๋Š” ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ…์ŠคํŠธ์—์„œ ๋ฐ˜๋ณต์„ ์ค„์ด๊ณ , ๋” ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค: +* ๊ธฐ๋ณธ ์ƒ์„ฑ ์„ค์ • +* ์ผ๋ฐ˜์ ์ธ ๋””์ฝ”๋”ฉ ์ „๋žต๊ณผ ์ฃผ์š” ํŒŒ๋ผ๋ฏธํ„ฐ +* ๐Ÿค— Hub์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ์ž ์ •์˜ ์ƒ์„ฑ ์„ค์ •์„ ์ €์žฅํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ• + +## ๊ธฐ๋ณธ ํ…์ŠคํŠธ ์ƒ์„ฑ ์„ค์ •[[default-text-generation-configuration]] + +๋ชจ๋ธ์˜ ๋””์ฝ”๋”ฉ ์ „๋žต์€ ์ƒ์„ฑ ์„ค์ •์—์„œ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ [`pipeline`] ๋‚ด์—์„œ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ๋•Œ, ๋ชจ๋ธ์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ ์„ค์ •์„ ์ ์šฉํ•˜๋Š” `PreTrainedModel.generate()` ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ์ž ์ •์˜ ์„ค์ •์„ ์ €์žฅํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋„ ๊ธฐ๋ณธ ์„ค์ •์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋กœ๋“œํ•  ๋•Œ, `model.generation_config`์„ ํ†ตํ•ด ์ œ๊ณต๋˜๋Š” ์ƒ์„ฑ ์„ค์ •์„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +>>> model.generation_config +GenerationConfig { + "bos_token_id": 50256, + "eos_token_id": 50256, +} +``` + + `model.generation_config`๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๊ธฐ๋ณธ ์„ค์ •๊ณผ ๋‹ค๋ฅธ ๊ฐ’๋“ค๋งŒ ํ‘œ์‹œ๋˜๊ณ , ๊ธฐ๋ณธ๊ฐ’๋“ค์€ ๋‚˜์—ด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ ์ƒ์„ฑ ์„ค์ •์€ ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ์™€ ์ถœ๋ ฅ์„ ํ•ฉ์นœ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ 20 ํ† ํฐ์œผ๋กœ ์ œํ•œํ•˜์—ฌ ๋ฆฌ์†Œ์Šค ๋ถ€์กฑ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋””์ฝ”๋”ฉ ์ „๋žต์€ ํƒ์š• ํƒ์ƒ‰(greedy search)์œผ๋กœ, ๋‹ค์Œ ํ† ํฐ์œผ๋กœ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํ† ํฐ์„ ์„ ํƒํ•˜๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋””์ฝ”๋”ฉ ์ „๋žต์ž…๋‹ˆ๋‹ค. ๋งŽ์€ ์ž‘์—…๊ณผ ์ž‘์€ ์ถœ๋ ฅ ํฌ๊ธฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ด ๋ฐฉ๋ฒ•์ด ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ, ๋” ๊ธด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ๋ฐ˜๋ณต์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ํ…์ŠคํŠธ ์ƒ์„ฑ ์‚ฌ์šฉ์ž ์ •์˜[[customize-text-generation]] + +ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํ•ด๋‹น ๊ฐ’์„ [`generate`] ๋ฉ”์†Œ๋“œ์— ์ง์ ‘ ์ „๋‹ฌํ•˜์—ฌ `generation_config`์„ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> my_model.generate(**inputs, num_beams=4, do_sample=True) # doctest: +SKIP +``` + +๊ธฐ๋ณธ ๋””์ฝ”๋”ฉ ์ „๋žต์ด ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์— ์ž˜ ์ž‘๋™ํ•œ๋‹ค ํ•˜๋”๋ผ๋„, ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์กฐ์ •๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค: + +- `max_new_tokens`: ์ƒ์„ฑํ•  ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ”„๋กฌํ”„ํŠธ์— ์žˆ๋Š” ํ† ํฐ์„ ์ œ์™ธํ•œ ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์˜ ๊ธธ์ด๋ฅผ ์ค‘๋‹จ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ์ „์ฒด ์ƒ์„ฑ๋ฌผ์ด ์ผ์ • ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•  ๋•Œ ์ƒ์„ฑ์„ ์ค‘๋‹จํ•˜๊ธฐ๋กœ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ์•Œ์•„๋ณด๋ ค๋ฉด [`StoppingCriteria`]๋ฅผ ํ™•์ธํ•˜์„ธ์š”. +- `num_beams`: 1๋ณด๋‹ค ํฐ ์ˆ˜์˜ ๋น”์„ ์ง€์ •ํ•จ์œผ๋กœ์จ, ํƒ์š• ํƒ์ƒ‰(greedy search)์—์„œ ๋น” ํƒ์ƒ‰(beam search)์œผ๋กœ ์ „ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ „๋žต์€ ๊ฐ ์‹œ๊ฐ„ ๋‹จ๊ณ„์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์„ค์„ ํ‰๊ฐ€ํ•˜๊ณ  ๊ฒฐ๊ตญ ์ „์ฒด ์‹œํ€€์Šค์— ๋Œ€ํ•ด ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๊ฐ€์„ค์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ดˆ๊ธฐ ํ† ํฐ์˜ ํ™•๋ฅ ์ด ๋‚ฎ์•„ ํƒ์š• ํƒ์ƒ‰์— ์˜ํ•ด ๋ฌด์‹œ๋˜์—ˆ์„ ๋†’์€ ํ™•๋ฅ ์˜ ์‹œํ€€์Šค๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. +- `do_sample`: ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `True`๋กœ ์„ค์ •ํ•˜๋ฉด, ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง, ๋น” ํƒ์ƒ‰ ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง, Top-K ์ƒ˜ํ”Œ๋ง ๋ฐ Top-p ์ƒ˜ํ”Œ๋ง๊ณผ ๊ฐ™์€ ๋””์ฝ”๋”ฉ ์ „๋žต์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ „๋žต๋“ค์€ ์ „์ฒด ์–ดํœ˜์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ถ„ํฌ์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ์„ ํƒํ•˜๋ฉฐ, ์ „๋žต๋ณ„๋กœ ํŠน์ • ์กฐ์ •์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. +- `num_return_sequences`: ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋ฐ˜ํ™˜ํ•  ์‹œํ€€์Šค ํ›„๋ณด์˜ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์€ ๋น” ํƒ์ƒ‰(beam search)์˜ ๋ณ€ํ˜•๊ณผ ์ƒ˜ํ”Œ๋ง๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ์‹œํ€€์Šค ํ›„๋ณด๋ฅผ ์ง€์›ํ•˜๋Š” ๋””์ฝ”๋”ฉ ์ „๋žต์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ์š• ํƒ์ƒ‰(greedy search)๊ณผ ๋Œ€์กฐ ํƒ์ƒ‰(contrastive search) ๊ฐ™์€ ๋””์ฝ”๋”ฉ ์ „๋žต์€ ๋‹จ์ผ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +## ๋ชจ๋ธ์— ์‚ฌ์šฉ์ž ์ •์˜ ๋””์ฝ”๋”ฉ ์ „๋žต ์ €์žฅ[[save-a-custom-decoding-strategy-with-your-model]] + +ํŠน์ • ์ƒ์„ฑ ์„ค์ •์„ ๊ฐ€์ง„ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•  ๋•Œ, ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +* [`GenerationConfig`] ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. +* ๋””์ฝ”๋”ฉ ์ „๋žต ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +* ์ƒ์„ฑ ์„ค์ •์„ [`GenerationConfig.save_pretrained`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•˜๋ฉฐ, `config_file_name` ์ธ์ž๋Š” ๋น„์›Œ๋‘ก๋‹ˆ๋‹ค. +* ๋ชจ๋ธ์˜ ์ €์žฅ์†Œ์— ์„ค์ •์„ ์—…๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด `push_to_hub`๋ฅผ `True`๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoModelForCausalLM, GenerationConfig + +>>> model = AutoModelForCausalLM.from_pretrained("my_account/my_model") # doctest: +SKIP +>>> generation_config = GenerationConfig( +... max_new_tokens=50, do_sample=True, top_k=50, eos_token_id=model.config.eos_token_id +... ) +>>> generation_config.save_pretrained("my_account/my_model", push_to_hub=True) # doctest: +SKIP +``` + +๋‹จ์ผ ๋””๋ ‰ํ† ๋ฆฌ์— ์—ฌ๋Ÿฌ ์ƒ์„ฑ ์„ค์ •์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ [`GenerationConfig.save_pretrained`]์˜ `config_file_name` ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— [`GenerationConfig.from_pretrained`]๋กœ ์ด๋“ค์„ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์ผ ๋ชจ๋ธ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ์ƒ์„ฑ ์„ค์ •์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: ์ƒ˜ํ”Œ๋ง์„ ์ด์šฉํ•œ ์ฐฝ์˜์  ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ ํ•˜๋‚˜, ๋น” ํƒ์ƒ‰์„ ์ด์šฉํ•œ ์š”์•ฝ์„ ์œ„ํ•œ ๋‹ค๋ฅธ ํ•˜๋‚˜ ๋“ฑ). ๋ชจ๋ธ์— ์„ค์ • ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ ์ ˆํ•œ Hub ๊ถŒํ•œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig + +>>> tokenizer = AutoTokenizer.from_pretrained("google-t5/t5-small") +>>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-small") + +>>> translation_generation_config = GenerationConfig( +... num_beams=4, +... early_stopping=True, +... decoder_start_token_id=0, +... eos_token_id=model.config.eos_token_id, +... pad_token=model.config.pad_token_id, +... ) + +>>> # ํŒ: Hub์— pushํ•˜๋ ค๋ฉด `push_to_hub=True`๋ฅผ ์ถ”๊ฐ€ +>>> translation_generation_config.save_pretrained("/tmp", "translation_generation_config.json") + +>>> # ๋ช…๋ช…๋œ ์ƒ์„ฑ ์„ค์ • ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์„ ๋งค๊ฐœ๋ณ€์ˆ˜ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +>>> generation_config = GenerationConfig.from_pretrained("/tmp", "translation_generation_config.json") +>>> inputs = tokenizer("translate English to French: Configuration files are easy to use!", return_tensors="pt") +>>> outputs = model.generate(**inputs, generation_config=generation_config) +>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['Les fichiers de configuration sont faciles ร  utiliser!'] +``` + +## ์ŠคํŠธ๋ฆฌ๋ฐ[[streaming]] + +`generate()` ๋ฉ”์†Œ๋“œ๋Š” `streamer` ์ž…๋ ฅ์„ ํ†ตํ•ด ์ŠคํŠธ๋ฆฌ๋ฐ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. `streamer` ์ž…๋ ฅ์€ `put()`๊ณผ `end()` ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์™€ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ, `put()`์€ ์ƒˆ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, `end()`๋Š” ํ…์ŠคํŠธ ์ƒ์„ฑ์˜ ๋์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + + +์ŠคํŠธ๋ฆฌ๋จธ ํด๋ž˜์Šค์˜ API๋Š” ์•„์ง ๊ฐœ๋ฐœ ์ค‘์ด๋ฉฐ, ํ–ฅํ›„ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +์‹ค์ œ๋กœ ๋‹ค์–‘ํ•œ ๋ชฉ์ ์„ ์œ„ํ•ด ์ž์ฒด ์ŠคํŠธ๋ฆฌ๋ฐ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋˜ํ•œ, ๊ธฐ๋ณธ์ ์ธ ์ŠคํŠธ๋ฆฌ๋ฐ ํด๋ž˜์Šค๋“ค๋„ ์ค€๋น„๋˜์–ด ์žˆ์–ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [`TextStreamer`] ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `generate()`์˜ ์ถœ๋ ฅ์„ ํ™”๋ฉด์— ํ•œ ๋‹จ์–ด์”ฉ ์ŠคํŠธ๋ฆฌ๋ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer + +>>> tok = AutoTokenizer.from_pretrained("openai-community/gpt2") +>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") +>>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") +>>> streamer = TextStreamer(tok) + +>>> # ์ŠคํŠธ๋ฆฌ๋จธ๋Š” ํ‰์†Œ์™€ ๊ฐ™์€ ์ถœ๋ ฅ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ๋„ ํ‘œ์ค€ ์ถœ๋ ฅ(stdout)์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. +>>> _ = model.generate(**inputs, streamer=streamer, max_new_tokens=20) +An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven, +``` + +## ๋””์ฝ”๋”ฉ ์ „๋žต[[decoding-strategies]] + +`generate()` ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ถ๊ทน์ ์œผ๋กœ `generation_config`์˜ ํŠน์ • ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋””์ฝ”๋”ฉ ์ „๋žต์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐœ๋…์ด ์ฒ˜์Œ์ด๋ผ๋ฉด, ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋””์ฝ”๋”ฉ ์ „๋žต์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๋Š” [์ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/how-to-generate)๋ฅผ ์ฝ์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์„œ๋Š” ๋””์ฝ”๋”ฉ ์ „๋žต์„ ์ œ์–ดํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + +### ํƒ์š• ํƒ์ƒ‰(Greedy Search)[[greedy-search]] + +[`generate`]๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํƒ์š• ํƒ์ƒ‰ ๋””์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ด๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Š” `num_beams`๊ฐ€ 1๋กœ ์„ค์ •๋˜๊ณ  `do_sample=False`๋กœ ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค." + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "I look forward to" +>>> checkpoint = "distilbert/distilgpt2" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> outputs = model.generate(**inputs) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['I look forward to seeing you all again!\n\n\n\n\n\n\n\n\n\n\n'] +``` + +### ๋Œ€์กฐ ํƒ์ƒ‰(Contrastive search)[[contrastive-search]] + +2022๋…„ ๋…ผ๋ฌธ [A Contrastive Framework for Neural Text Generation](https://huggingface.co/papers/2202.06417)์—์„œ ์ œ์•ˆ๋œ ๋Œ€์กฐ ํƒ์ƒ‰ ๋””์ฝ”๋”ฉ ์ „๋žต์€ ๋ฐ˜๋ณต๋˜์ง€ ์•Š์œผ๋ฉด์„œ๋„ ์ผ๊ด€๋œ ๊ธด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์žˆ์–ด ์šฐ์ˆ˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ๋Œ€์กฐ ํƒ์ƒ‰์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ์•„๋ณด๋ ค๋ฉด [์ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/introducing-csearch)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ๋Œ€์กฐ ํƒ์ƒ‰์˜ ๋™์ž‘์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ณ  ์ œ์–ดํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” `penalty_alpha`์™€ `top_k`์ž…๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoTokenizer, AutoModelForCausalLM + +>>> checkpoint = "openai-community/gpt2-large" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> prompt = "Hugging Face Company is" +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> outputs = model.generate(**inputs, penalty_alpha=0.6, top_k=4, max_new_tokens=100) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Hugging Face Company is a family owned and operated business. We pride ourselves on being the best +in the business and our customer service is second to none.\n\nIf you have any questions about our +products or services, feel free to contact us at any time. We look forward to hearing from you!'] +``` + +### ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง(Multinomial sampling)[[multinomial-sampling]] + +ํƒ์š• ํƒ์ƒ‰(greedy search)์ด ํ•ญ์ƒ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํ† ํฐ์„ ๋‹ค์Œ ํ† ํฐ์œผ๋กœ ์„ ํƒํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง(multinomial sampling, ์กฐ์ƒ ์ƒ˜ํ”Œ๋ง(ancestral sampling)์ด๋ผ๊ณ ๋„ ํ•จ)์€ ๋ชจ๋ธ์ด ์ œ๊ณตํ•˜๋Š” ์ „์ฒด ์–ดํœ˜์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ ํ† ํฐ์„ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. 0์ด ์•„๋‹Œ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๋ชจ๋“  ํ† ํฐ์€ ์„ ํƒ๋  ๊ธฐํšŒ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, ๋ฐ˜๋ณต์˜ ์œ„ํ—˜์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด `do_sample=True` ๋ฐ `num_beams=1`์„ ์„ค์ •ํ•˜์„ธ์š”. + +```python +>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed +>>> set_seed(0) # ์žฌํ˜„์„ฑ์„ ์œ„ํ•ด + +>>> checkpoint = "openai-community/gpt2-large" +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> prompt = "Today was an amazing day because" +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> outputs = model.generate(**inputs, do_sample=True, num_beams=1, max_new_tokens=100) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Today was an amazing day because when you go to the World Cup and you don\'t, or when you don\'t get invited, +that\'s a terrible feeling."'] +``` + +### ๋น” ํƒ์ƒ‰(Beam-search) ๋””์ฝ”๋”ฉ[[beam-search-decoding]] + +ํƒ์š• ๊ฒ€์ƒ‰(greedy search)๊ณผ ๋‹ฌ๋ฆฌ, ๋น” ํƒ์ƒ‰(beam search) ๋””์ฝ”๋”ฉ์€ ๊ฐ ์‹œ๊ฐ„ ๋‹จ๊ณ„์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์„ค์„ ์œ ์ง€ํ•˜๊ณ  ๊ฒฐ๊ตญ ์ „์ฒด ์‹œํ€€์Šค์— ๋Œ€ํ•ด ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๊ฐ€์„ค์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‚ฎ์€ ํ™•๋ฅ ์˜ ์ดˆ๊ธฐ ํ† ํฐ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๊ทธ๋ฆฌ๋”” ๊ฒ€์ƒ‰์—์„œ ๋ฌด์‹œ๋˜์—ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์‹œํ€€์Šค๋ฅผ ์‹๋ณ„ํ•˜๋Š” ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋””์ฝ”๋”ฉ ์ „๋žต์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด `num_beams` (์ถ”์ ํ•  ๊ฐ€์„ค ์ˆ˜๋ผ๊ณ ๋„ ํ•จ)๋ฅผ 1๋ณด๋‹ค ํฌ๊ฒŒ ์ง€์ •ํ•˜์„ธ์š”. + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "It is astonishing how one can" +>>> checkpoint = "openai-community/gpt2-medium" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, max_new_tokens=50) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['It is astonishing how one can have such a profound impact on the lives of so many people in such a short period of +time."\n\nHe added: "I am very proud of the work I have been able to do in the last few years.\n\n"I have'] +``` + +### ๋น” ํƒ์ƒ‰ ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง(Beam-search multinomial sampling)[[beam-search-multinomial-sampling]] + +์ด ๋””์ฝ”๋”ฉ ์ „๋žต์€ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋น” ํƒ์ƒ‰๊ณผ ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง์„ ๊ฒฐํ•ฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋””์ฝ”๋”ฉ ์ „๋žต์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” `num_beams`๋ฅผ 1๋ณด๋‹ค ํฐ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ , `do_sample=True`๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, set_seed +>>> set_seed(0) # ์žฌํ˜„์„ฑ์„ ์œ„ํ•ด + +>>> prompt = "translate English to German: The house is wonderful." +>>> checkpoint = "google-t5/t5-small" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, do_sample=True) +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'Das Haus ist wunderbar.' +``` + +### ๋‹ค์–‘ํ•œ ๋น” ํƒ์ƒ‰ ๋””์ฝ”๋”ฉ(Diverse beam search decoding)[[diverse-beam-search-decoding]] + +๋‹ค์–‘ํ•œ ๋น” ํƒ์ƒ‰(Decoding) ์ „๋žต์€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋” ๋‹ค์–‘ํ•œ ๋น” ์‹œํ€€์Šค ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋น” ํƒ์ƒ‰ ์ „๋žต์˜ ํ™•์žฅ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๋ฉด, [๋‹ค์–‘ํ•œ ๋น” ํƒ์ƒ‰: ์‹ ๊ฒฝ ์‹œํ€€์Šค ๋ชจ๋ธ์—์„œ ๋‹ค์–‘ํ•œ ์†”๋ฃจ์…˜ ๋””์ฝ”๋”ฉํ•˜๊ธฐ](https://huggingface.co/papers/1610.02424)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: `num_beams`, `num_beam_groups`, ๊ทธ๋ฆฌ๊ณ  `diversity_penalty`. ๋‹ค์–‘์„ฑ ํŒจ๋„ํ‹ฐ๋Š” ๊ทธ๋ฃน ๊ฐ„์— ์ถœ๋ ฅ์ด ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋ฉฐ, ๊ฐ ๊ทธ๋ฃน ๋‚ด์—์„œ ๋น” ํƒ์ƒ‰์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + +>>> checkpoint = "google/pegasus-xsum" +>>> prompt = ( +... "The Permaculture Design Principles are a set of universal design principles " +... "that can be applied to any location, climate and culture, and they allow us to design " +... "the most efficient and sustainable human habitation and food production systems. " +... "Permaculture is a design system that encompasses a wide variety of disciplines, such " +... "as ecology, landscape design, environmental science and energy conservation, and the " +... "Permaculture design principles are drawn from these various disciplines. Each individual " +... "design principle itself embodies a complete conceptual framework based on sound " +... "scientific principles. When we bring all these separate principles together, we can " +... "create a design system that both looks at whole systems, the parts that these systems " +... "consist of, and how those parts interact with each other to create a complex, dynamic, " +... "living system. Each design principle serves as a tool that allows us to integrate all " +... "the separate parts of a design, referred to as elements, into a functional, synergistic, " +... "whole system, where the elements harmoniously interact and work together in the most " +... "efficient way possible." +... ) + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) + +>>> outputs = model.generate(**inputs, num_beams=5, num_beam_groups=5, max_new_tokens=30, diversity_penalty=1.0) +>>> tokenizer.decode(outputs[0], skip_special_tokens=True) +'The Design Principles are a set of universal design principles that can be applied to any location, climate and +culture, and they allow us to design the' +``` + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ๋””์ฝ”๋”ฉ ์ „๋žต์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. [`generate`] ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ๊ณ ๊ธ‰ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ [`generate`] ๋ฉ”์„œ๋“œ์˜ ๋™์ž‘์„ ๋”์šฑ ์„ธ๋ถ€์ ์œผ๋กœ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ „์ฒด ๋ชฉ๋ก์€ [API ๋ฌธ์„œ](./main_classes/text_generation)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +### ์ถ”๋ก  ๋””์ฝ”๋”ฉ(Speculative Decoding)[[speculative-decoding]] + +์ถ”๋ก  ๋””์ฝ”๋”ฉ(๋ณด์กฐ ๋””์ฝ”๋”ฉ(assisted decoding)์œผ๋กœ๋„ ์•Œ๋ ค์ง)์€ ๋™์ผํ•œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ›จ์”ฌ ์ž‘์€ ๋ณด์กฐ ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ํ›„๋ณด ํ† ํฐ์„ ์ƒ์„ฑํ•˜๋Š” ์ƒ์œ„ ๋ชจ๋ธ์˜ ๋””์ฝ”๋”ฉ ์ „๋žต์„ ์ˆ˜์ •ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ ๋ชจ๋ธ์€ ๋‹จ์ผ ์ „๋ฐฉ ํ†ต๊ณผ๋กœ ํ›„๋ณด ํ† ํฐ์„ ๊ฒ€์ฆํ•จ์œผ๋กœ์จ ๋””์ฝ”๋”ฉ ๊ณผ์ •์„ ๊ฐ€์†ํ™”ํ•ฉ๋‹ˆ๋‹ค. `do_sample=True`์ผ ๊ฒฝ์šฐ, [์ถ”๋ก  ๋””์ฝ”๋”ฉ ๋…ผ๋ฌธ](https://huggingface.co/papers/2211.17192)์— ์†Œ๊ฐœ๋œ ํ† ํฐ ๊ฒ€์ฆ๊ณผ ์žฌ์ƒ˜ํ”Œ๋ง ๋ฐฉ์‹์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํ˜„์žฌ, ํƒ์š• ๊ฒ€์ƒ‰(greedy search)๊ณผ ์ƒ˜ํ”Œ๋ง๋งŒ์ด ์ง€์›๋˜๋Š” ๋ณด์กฐ ๋””์ฝ”๋”ฉ(assisted decoding) ๊ธฐ๋Šฅ์„ ํ†ตํ•ด, ๋ณด์กฐ ๋””์ฝ”๋”ฉ์€ ๋ฐฐ์น˜ ์ž…๋ ฅ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณด์กฐ ๋””์ฝ”๋”ฉ์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด, [์ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/assisted-generation)๋ฅผ ํ™•์ธํ•ด ์ฃผ์„ธ์š”. + +๋ณด์กฐ ๋””์ฝ”๋”ฉ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ `assistant_model` ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> prompt = "Alice and Bob" +>>> checkpoint = "EleutherAI/pythia-1.4b-deduped" +>>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) +>>> outputs = model.generate(**inputs, assistant_model=assistant_model) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Alice and Bob are sitting in a bar. Alice is drinking a beer and Bob is drinking a'] +``` + +์ƒ˜ํ”Œ๋ง ๋ฐฉ๋ฒ•๊ณผ ํ•จ๊ป˜ ๋ณด์กฐ ๋””์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ `temperature` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌด์ž‘์œ„์„ฑ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณด์กฐ ๋””์ฝ”๋”ฉ์—์„œ๋Š” `temperature`๋ฅผ ๋‚ฎ์ถ”๋ฉด ๋Œ€๊ธฐ ์‹œ๊ฐ„์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed +>>> set_seed(42) # ์žฌํ˜„์„ฑ์„ ์œ„ํ•ด + +>>> prompt = "Alice and Bob" +>>> checkpoint = "EleutherAI/pythia-1.4b-deduped" +>>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" + +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) +>>> inputs = tokenizer(prompt, return_tensors="pt") + +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) +>>> outputs = model.generate(**inputs, assistant_model=assistant_model, do_sample=True, temperature=0.5) +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Alice and Bob, who were both in their early twenties, were both in the process of'] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/gguf.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/gguf.md new file mode 100644 index 0000000000000000000000000000000000000000..9d9b02d708a09fe50895a3105752309c62a9ab54 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/gguf.md @@ -0,0 +1,100 @@ + + +# GGUF์™€ Transformers์˜ ์ƒํ˜ธ์ž‘์šฉ [[gguf-and-interaction-with-transformers]] + +GGUF ํŒŒ์ผ ํ˜•์‹์€ [GGML](https://github.com/ggerganov/ggml)๊ณผ ๊ทธ์— ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ์˜ˆ๋ฅผ ๋“ค์–ด ๋งค์šฐ ์ธ๊ธฐ ์žˆ๋Š” [llama.cpp](https://github.com/ggerganov/llama.cpp)์ด๋‚˜ [whisper.cpp](https://github.com/ggerganov/whisper.cpp)์—์„œ ์ถ”๋ก ์„ ์œ„ํ•œ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +์ด ํŒŒ์ผ ํ˜•์‹์€ [Hugging Face Hub](https://huggingface.co/docs/hub/en/gguf)์—์„œ ์ง€์›๋˜๋ฉฐ, ํŒŒ์ผ ๋‚ด์˜ ํ…์„œ์™€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ˜•์‹์€ "๋‹จ์ผ ํŒŒ์ผ ํ˜•์‹(single-file-format)"์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ์œผ๋ฉฐ, ํ•˜๋‚˜์˜ ํŒŒ์ผ์— ์„ค์ • ์†์„ฑ, ํ† ํฌ๋‚˜์ด์ € ์–ดํœ˜, ๊ธฐํƒ€ ์†์„ฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋ธ์—์„œ ๋กœ๋“œ๋˜๋Š” ๋ชจ๋“  ํ…์„œ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ๋“ค์€ ํŒŒ์ผ์˜ ์–‘์žํ™” ์œ ํ˜•์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์–‘์žํ™” ์œ ํ˜•์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ์„ค๋ช…์€ [์—ฌ๊ธฐ](https://huggingface.co/docs/hub/en/gguf#quantization-types)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## Transformers ๋‚ด ์ง€์› [[support-within-transformers]] + +`transformers` ๋‚ด์—์„œ `gguf` ํŒŒ์ผ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์—ฌ GGUF ๋ชจ๋ธ์˜ ์ถ”๊ฐ€ ํ•™์Šต/๋ฏธ์„ธ ์กฐ์ •์„ ์ œ๊ณตํ•œ ํ›„ `ggml` ์ƒํƒœ๊ณ„์—์„œ ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก `gguf` ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ๋•Œ ๋จผ์ € FP32๋กœ ์—ญ์–‘์žํ™”ํ•œ ํ›„, PyTorch์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. + +> [!NOTE] +> ์ง€์›์€ ์•„์ง ์ดˆ๊ธฐ ๋‹จ๊ณ„์— ์žˆ์œผ๋ฉฐ, ๋‹ค์–‘ํ•œ ์–‘์žํ™” ์œ ํ˜•๊ณผ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด ์ด๋ฅผ ๊ฐ•ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์—ฌ๋ฅผ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. + +ํ˜„์žฌ ์ง€์›๋˜๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์™€ ์–‘์žํ™” ์œ ํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +### ์ง€์›๋˜๋Š” ์–‘์žํ™” ์œ ํ˜• [[supported-quantization-types]] + +์ดˆ๊ธฐ์— ์ง€์›๋˜๋Š” ์–‘์žํ™” ์œ ํ˜•์€ Hub์—์„œ ๊ณต์œ ๋œ ์ธ๊ธฐ ์žˆ๋Š” ์–‘์žํ™” ํŒŒ์ผ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +- F32 +- F16 +- BF16 +- Q4_0 +- Q4_1 +- Q5_0 +- Q5_1 +- Q8_0 +- Q2_K +- Q3_K +- Q4_K +- Q5_K +- Q6_K +- IQ1_S +- IQ1_M +- IQ2_XXS +- IQ2_XS +- IQ2_S +- IQ3_XXS +- IQ3_S +- IQ4_XS +- IQ4_NL + +> [!NOTE] +> GGUF ์—ญ์–‘์žํ™”๋ฅผ ์ง€์›ํ•˜๋ ค๋ฉด `gguf>=0.10.0` ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +### ์ง€์›๋˜๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜ [[supported-model-architectures]] + +ํ˜„์žฌ ์ง€์›๋˜๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋Š” Hub์—์„œ ๋งค์šฐ ์ธ๊ธฐ๊ฐ€ ๋งŽ์€ ์•„ํ‚คํ…์ฒ˜๋“ค๋กœ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: + +- LLaMa +- Mistral +- Qwen2 +- Qwen2Moe +- Phi3 +- Bloom + +## ์‚ฌ์šฉ ์˜ˆ์‹œ [[example-usage]] + +`transformers`์—์„œ `gguf` ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋ ค๋ฉด `from_pretrained` ๋ฉ”์†Œ๋“œ์— `gguf_file` ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ํŒŒ์ผ์—์„œ ํ† ํฌ๋‚˜์ด์ €์™€ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +from transformers import AutoTokenizer, AutoModelForCausalLM + +model_id = "TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF" +filename = "tinyllama-1.1b-chat-v1.0.Q6_K.gguf" + +tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=filename) +model = AutoModelForCausalLM.from_pretrained(model_id, gguf_file=filename) +``` + +์ด์ œ PyTorch ์ƒํƒœ๊ณ„์—์„œ ๋ชจ๋ธ์˜ ์–‘์žํ™”๋˜์ง€ ์•Š์€ ์ „์ฒด ๋ฒ„์ „์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๋„๊ตฌ๋“ค๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +`gguf` ํŒŒ์ผ๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด llama.cpp์˜ [`convert-hf-to-gguf.py`](https://github.com/ggerganov/llama.cpp/blob/master/convert_hf_to_gguf.py)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +์œ„์˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„๋ฃŒํ•˜์—ฌ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ `gguf`๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +tokenizer.save_pretrained('directory') +model.save_pretrained('directory') + +!python ${path_to_llama_cpp}/convert-hf-to-gguf.py ${directory} +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/glossary.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/glossary.md new file mode 100644 index 0000000000000000000000000000000000000000..f19ea2d0aed3cdf824c2cd58a24b0ca0baf3e7aa --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/glossary.md @@ -0,0 +1,454 @@ + + +# ์šฉ์–ด์ง‘(Glossary) + +์ด ์šฉ์–ด์ง‘์€ ์ „๋ฐ˜์ ์ธ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ฐ ๐Ÿค— Transformers ๊ด€๋ จ ์šฉ์–ด๋ฅผ ์ •์˜ํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค. + +## A + +### ์–ดํ…์…˜ ๋งˆ์Šคํฌ (attention mask) + +์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)๋Š” ์—ฌ๋Ÿฌ ์‹œํ€€์Šค๋ฅผ ๋ฐฐ์น˜(batch)๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์„ ํƒ์  ์ธ์ž์ž…๋‹ˆ๋‹ค. + + + +์ด ์ธ์ž๋Š” ๋ชจ๋ธ์—๊ฒŒ ์–ด๋–ค ํ† ํฐ์— ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ํ† ํฐ์€ ๋ฌด์‹œํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค: + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence_a = "This is a short sequence." +>>> sequence_b = "This is a rather long sequence. It is at least longer than the sequence A." + +>>> encoded_sequence_a = tokenizer(sequence_a)["input_ids"] +>>> encoded_sequence_b = tokenizer(sequence_b)["input_ids"] +``` + +์ธ์ฝ”๋”ฉ๋œ ๋ฒ„์ „๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค: + +```python +>>> len(encoded_sequence_a), len(encoded_sequence_b) +(8, 19) +``` + +๋”ฐ๋ผ์„œ ์ด ๋‘ ์‹œํ€€์Šค๋ฅผ ๊ทธ๋Œ€๋กœ ํ•˜๋‚˜์˜ ํ…์„œ์— ๋„ฃ์„ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œํ€€์Šค๋ฅผ ๋‘ ๋ฒˆ์งธ ๊ธธ์ด์— ๋งž์ถฐ ํŒจ๋”ฉ ํ•˜๊ฑฐ๋‚˜, ๋ฐ˜๋Œ€๋กœ ๋‘ ๋ฒˆ์งธ ์‹œํ€€์Šค๋ฅผ ์ฒซ ๋ฒˆ์งธ ๊ธธ์ด์— ๋งž์ถฐ ์ž˜๋ผ๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ฒซ ๋ฒˆ์งธ ๊ฒฝ์šฐ์—๋Š” ID ๋ชฉ๋ก์ด ํŒจ๋”ฉ ์ธ๋ฑ์Šค๋กœ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํŒจ๋”ฉ์„ ์ ์šฉํ•˜๋ ค๋ฉด ํ† ํฌ๋‚˜์ด์ €์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> padded_sequences = tokenizer([sequence_a, sequence_b], padding=True) +``` + +์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์˜ค๋ฅธ์ชฝ์— 0์ด ์ถ”๊ฐ€๋˜์–ด ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ๊ณผ ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> padded_sequences["input_ids"] +[[101, 1188, 1110, 170, 1603, 4954, 119, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [101, 1188, 1110, 170, 1897, 1263, 4954, 119, 1135, 1110, 1120, 1655, 2039, 1190, 1103, 4954, 138, 119, 102]] +``` + +์ด๊ฒƒ์€ PyTorch๋‚˜ TensorFlow์˜ ํ…์„œ๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” ๋ชจ๋ธ์ด ํŒจ๋”ฉ ๋œ ์ธ๋ฑ์Šค๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋„๋ก ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ด์ง„ ํ…์„œ์ž…๋‹ˆ๋‹ค. [`BertTokenizer`]์˜ ๊ฒฝ์šฐ, `1`์€ ์–ดํ…์…˜์ด ํ•„์š”ํ•œ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ณ , `0`์€ ํŒจ๋”ฉ ๋œ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜ "attention_mask" ํ‚ค ์•„๋ž˜์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> padded_sequences["attention_mask"] +[[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] +``` + +### ์˜คํ† ์ธ์ฝ”๋”ฉ ๋ชจ๋ธ (autoencoding models) + +[์ธ์ฝ”๋” ๋ชจ๋ธ](#encoder-models)๊ณผ [๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง](#masked-language-modeling-mlm)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ์ž๊ธฐํšŒ๊ท€ ๋ชจ๋ธ (autoregressive models) + +[์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง](#causal-language-modeling)๊ณผ [๋””์ฝ”๋” ๋ชจ๋ธ](#decoder-models)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +## B + +### ๋ฐฑ๋ณธ (backbone) + +๋ฐฑ๋ณธ(backbone)์€ ์›์‹œ(hidden) ์€๋‹‰ ์ƒํƒœ(hidden state) ๋˜๋Š” ํŠน์ง•(feature)์„ ์ถœ๋ ฅํ•˜๋Š” ๋„คํŠธ์›Œํฌ(์ž„๋ฒ ๋”ฉ๊ณผ ๋ ˆ์ด์–ด)์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ๋ฐฑ๋ณธ์€ ํ•ด๋‹น ํŠน์ง•์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•˜๋Š” [ํ—ค๋“œ](#head)์™€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [`ViTModel`]์€ ํŠน์ • ํ—ค๋“œ๊ฐ€ ์—†๋Š” ๋ฐฑ๋ณธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋ธ๋“ค๋„[`VitModel`]์„ ๋ฐฑ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, [DPT](model_doc/dpt)๋“ฑ์ด ๊ทธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. + +## C + +### ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง (causal language modeling) + +๋ชจ๋ธ์ด ํ…์ŠคํŠธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฝ์œผ๋ฉฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์‚ฌ์ „ ํ•™์Šต(pretraining) ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์žฅ์„ ์ „์ฒด๋กœ ์ฝ๋˜, ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ํŠน์ง• ์‹œ์  ์ดํ›„์˜ ํ† ํฐ์„ ๋งˆ์Šคํ‚น(masking)ํ•˜์—ฌ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +### ์ฑ„๋„ (channel) + +์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋Š” ๋นจ๊ฐ„์ƒ‰(R), ์ดˆ๋ก์ƒ‰(G), ํŒŒ๋ž€์ƒ‰(B)์˜ ์„ธ ์ฑ„๋„ ๊ฐ’์„ ์กฐํ•ฉํ•˜์—ฌ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋Š” ๋‹จ์ผ ์ฑ„๋„๋งŒ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๐Ÿค— Transformers์—์„œ๋Š” ์ด๋ฏธ์ง€ ํ…์„œ์˜ ์ฑ„๋„์ด ์ฒซ ๋ฒˆ์งธ ๋˜๋Š” ๋งˆ์ง€๋ง‰ ์ฐจ์›์— ์œ„์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:[`n_channels`, `height`, `width`] ๋˜๋Š” [`height`, `width`, `n_channels`]์™€ ๊ฐ™์€ ํ˜•์‹์ž…๋‹ˆ๋‹ค. + +### ์—ฐ๊ฒฐ ์‹œ๊ฐ„๋ถ„๋ฅ˜(connectionist temporal classification, CTC) + +์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์ •ํ™•ํžˆ ๋ชฐ๋ผ๋„ ๋ชจ๋ธ์ด ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. CTC๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ถœ๋ ฅ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ์ค‘ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์ถœ๋ ฅ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. CTC๋Š” ๋งํ•˜๋Š” ์†๋„์˜ ์ฐจ์ด ๋“ฑ ์—ฌ๋Ÿฌ ์ด์œ ๋กœ ์Œ์„ฑ๊ณผ ํ…์ŠคํŠธ๊ฐ€ ํ•ญ์ƒ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์Œ์„ฑ ์ธ์‹ ์ž‘์—…์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +### ์ปจ๋ณผ๋ฃจ์…˜ (convolution) + +์‹ ๊ฒฝ๋ง์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ ˆ์ด์–ด์˜ ํ•œ ์ข…๋ฅ˜๋กœ, ์ž…๋ ฅ ํ–‰๋ ฌ์— ๋Œ€ํ•ด ๋” ์ž‘์€ ํ–‰๋ ฌ(์ปค๋„ ๋˜๋Š” ํ•„ํ„ฐ)์„ ์›์†Œ๋ณ„๋กœ ๊ณฑํ•œ ๋’ค ๊ทธ ๊ฐ’์„ ํ•ฉ์‚ฐํ•ด ์ƒˆ๋กœ์šด ํ–‰๋ ฌ์„ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์„ ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์ž…๋ ฅ ํ–‰๋ ฌ ์ „์ฒด์— ๊ฑธ์ณ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์—ฐ์‚ฐ์€ ์ž…๋ ฅ ํ–‰๋ ฌ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ตฌ๊ฐ„์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ปจ๋ณผ๋ฃจ์…˜ ์‹ ๊ฒฝ๋ง(CNN)์€ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +## D + +### ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” (DataParallel) + +์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU์—์„œ ํ›ˆ๋ จ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ณ‘๋ ฌํ™” ๊ธฐ๋ฒ•์œผ๋กœ, ๋™์ผํ•œ ๋ชจ๋ธ ๊ตฌ์„ฑ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ณต์ œ๋˜๋ฉฐ ๊ฐ ์ธ์Šคํ„ด์Šค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋Š” ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ๊ฐ ํ›ˆ๋ จ ๋‹จ๊ณ„๊ฐ€ ๋๋‚œ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋™๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +DataParallel ๋ฐฉ์‹์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด [์—ฌ๊ธฐ](perf_train_gpu_many#dataparallel-vs-distributeddataparallel)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ๋””์ฝ”๋” ์ž…๋ ฅ ID (decoder input IDs) + +์ด ์ž…๋ ฅ์€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์— ํŠนํ™”๋œ ๊ฒƒ์œผ๋กœ, ๋””์ฝ”๋”์— ์ „๋‹ฌ๋  input ID ๋“ค์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž…๋ ฅ์€ ๋ฒˆ์—ญ์ด๋‚˜ ์š”์•ฝ๊ณผ ๊ฐ™์€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(sequence-to-sequence) ์ž‘์—…์— ์‚ฌ์šฉ๋˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ๋งˆ๋‹ค ๊ณ ์œ ํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. + +๋Œ€๋ถ€๋ถ„์˜ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ(BART, T5 ๋“ฑ)์€ `labels`๋กœ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ `decoder_input_ids`๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์—์„œ๋Š” ํ•™์Šต ์‹œ `labels`๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. + +์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ํ•™์Šต์—์„œ ๊ฐ ๋ชจ๋ธ์ด ์ด๋Ÿฌํ•œ input ID๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€๋Š” ๋ชจ๋ธ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +### ๋””์ฝ”๋” ๋ชจ๋ธ (decoder models) + +์ž๊ธฐํšŒ๊ท€ ๋ชจ๋ธ(Autoregressive models)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ๋””์ฝ”๋” ๋ชจ๋ธ์€ ์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง(causal language modeling)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ์‚ฌ์ „ ํ•™์Šต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์—์„œ๋Š” ๋ชจ๋ธ์ด ํ…์ŠคํŠธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฝ๊ณ  ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์žฅ์˜ ์ „์ฒด๋ฅผ ์ฝ๋˜, ํŠน์ • ์‹œ์  ์ดํ›„์˜ ํ† ํฐ์€ ๋งˆ์Šคํฌ๋กœ ๊ฐ€๋ ค ์˜ˆ์ธกํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + + + +### ๋”ฅ๋Ÿฌ๋‹ (deep learning) + +์—ฌ๋Ÿฌ ์ธต์˜ ์‹ ๊ฒฝ๋ง(neural network)์„ ์‚ฌ์šฉํ•˜๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. + +## E + +### ์ธ์ฝ”๋” ๋ชจ๋ธ (encoder models) + +์ž๋™ ์ธ์ฝ”๋”ฉ ๋ชจ๋ธ(Autoencoding models)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ์ธ์ฝ”๋” ๋ชจ๋ธ์€ ํ…์ŠคํŠธ๋‚˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ž„๋ฒ ๋”ฉ์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ์••์ถ•๋œ ์ˆ˜์น˜ ํ‘œํ˜„์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ธ์ฝ”๋” ๋ชจ๋ธ์€ ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ์ผ๋ถ€๋ฅผ ๋งˆ์Šคํ‚นํ•˜๊ณ  ๋” ์˜๋ฏธ ์žˆ๋Š” ํ‘œํ˜„์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•™์Šตํ•˜๋Š” [masked language modeling](#masked-language-modeling-mlm)๊ณผ ๊ฐ™์€ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. + + + +## F + +### ํŠน์ง• ์ถ”์ถœ (feature extraction) + +๋จธ์‹ ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋” ํšจ๊ณผ์ ์œผ๋กœ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก, ์›์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ณ€ํ™˜ํ•˜์—ฌ ๋” ์œ ์šฉํ•œ ํŠน์ง•(feature) ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ด๋ฏธ์ง€๋‚˜ ๋น„๋””์˜ค ๋ฐ์ดํ„ฐ์—์„œ ์œค๊ณฝ์„ ์ด๋‚˜ ํ˜•ํƒœ์™€ ๊ฐ™์€ ์ค‘์š”ํ•œ ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +### ํ”ผ๋“œ ํฌ์›Œ๋“œ ์ฒญํ‚น (feed forward chunking) + +ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ฐ residual attention Block์—์„œ๋Š” self-Attention Layer ๋‹ค์Œ์— ๋ณดํ†ต ๋‘ ๊ฐœ์˜ Feed Forward Layer๊ฐ€ ์ด์–ด์ง‘๋‹ˆ๋‹ค. ์ด Feed Forward Layers์˜ ์ค‘๊ฐ„ ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋Š” ์ข…์ข… ๋ชจ๋ธ์˜ ํžˆ๋“  ์‚ฌ์ด์ฆˆ(hidden size)๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค(์˜ˆ: +`google-bert/bert-base-uncased` ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ). + +์ž…๋ ฅ ํฌ๊ธฐ๊ฐ€ `[batch_size, sequence_length]`์ผ ๊ฒฝ์šฐ, ์ค‘๊ฐ„ Feed Forward ์ž„๋ฒ ๋”ฉ +`[batch_size, sequence_length, config.intermediate_size]`์„ ์ €์žฅํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋Š” ์ „์ฒด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์˜ ํฐ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +[Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451) ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ์ด ์—ฐ์‚ฐ์ด `sequence_length` ์ฐจ์›์— ๋Œ€ํ•ด ๋…๋ฆฝ์ ์ด๊ธฐ ๋•Œ๋ฌธ์—,ํ† ํฐ๋งˆ๋‹ค Feed Forward Layer์˜ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์„ ๊ฐ ํ† ํฐ๋ณ„๋กœ `[batch_size, config.hidden_size]`์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ ๋’ค, ์ด๋ฅผ ์ด์–ด ๋ถ™์—ฌ `[batch_size, sequence_length, config.hidden_size]` ํ˜•ํƒœ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.`n = sequence_length`. ์ด ๋ฐฉ์‹์€ ๊ณ„์‚ฐ ์‹œ๊ฐ„์€ ๋Š˜์–ด๋‚˜์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์€ ์ค„์–ด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +[`apply_chunking_to_forward`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, `chunk_size`๋Š” ๋ณ‘๋ ฌ๋กœ ๊ณ„์‚ฐ๋˜๋Š” ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉฐ, ์ด๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ๊ณ„์‚ฐ ์‹œ๊ฐ„ ๊ฐ„์˜ ํŠธ๋ ˆ์ด๋“œ์˜คํ”„๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +`chunk_size`๊ฐ€ 0์œผ๋กœ ์„ค์ •๋˜๋ฉด, ํ”ผ๋“œ ํฌ์›Œ๋“œ ์ฒญํ‚น(Feed Forward Chunking)์€ ์ˆ˜ํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +### ํŒŒ์ธํŠœ๋‹ ๋ชจ๋ธ (finetuned models) + +ํŒŒ์ธํŠœ๋‹(Finetuning)์€ ์ „์ด ํ•™์Šต(transfer learning)์˜ ํ•œ ํ˜•ํƒœ๋กœ, ์‚ฌ์ „ ํ•™์Šต๋œ (pretrained) ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ ์ •(freeze)ํ•˜๊ณ , ์ถœ๋ ฅ์ธต์„ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ [๋ชจ๋ธ ํ—ค๋“œ](#head)๋กœ ๊ต์ฒดํ•œ ๋’ค, ํ•ด๋‹น ๋ชจ๋ธ ํ—ค๋“œ๋ฅผ ๋ชฉํ‘œ ๋ฐ์ดํ„ฐ์…‹์— ๋งž๊ฒŒ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +์ž์„ธํ•œ ๋‚ด์šฉ์€ [Fine-tune a pretrained model](https://huggingface.co/docs/transformers/training) ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ๊ณ ํ•˜์‹œ๊ณ , ๐Ÿค— Transformers๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋ธ์„ ํŒŒ์ธ ํŠœ๋‹ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ํ•จ๊ป˜ ํ™•์ธํ•ด ๋ณด์„ธ์š”. + +## H + +### ํ—ค๋“œ (head) + +๋ชจ๋ธ ํ—ค๋“œ(model head)๋ž€ ์‹ ๊ฒฝ๋ง์˜ ๋งˆ์ง€๋ง‰ ์ธต์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ด ์ธต์€ ์ด์ „ ์ธต์—์„œ ๋‚˜์˜จ ํžˆ๋“  ์ƒํƒœ(hidden states)๋ฅผ ๋ฐ›์•„ ๋‹ค๋ฅธ ์ฐจ์›์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž‘์—…(task)์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชจ๋ธ ํ—ค๋“œ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด: + + * [`GPT2ForSequenceClassification`]์€ ๊ธฐ๋ณธ [`GPT2Model`] ์œ„์— ์‹œํ€€์Šค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์„ ํ˜•๊ณ„์ธต(linear layer)์„ ์ถ”๊ฐ€ํ•œ ๋ชจ๋ธ ํ—ค๋“œ์ž…๋‹ˆ๋‹ค. + * [`ViTForImageClassification`]์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ๋ชจ๋ธ ํ—ค๋“œ๋กœ, ๊ธฐ๋ณธ [`ViTModel`] ์œ„์— `CLS` ํ† ํฐ์˜ ๋งˆ์ง€๋ง‰ ํžˆ๋“  ์ƒํƒœ์— ์„ ํ˜• ๊ณ„์ธต(linear layer)์„ ์ถ”๊ฐ€ํ•œ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. + * [`Wav2Vec2ForCTC`]๋Š” ๊ธฐ๋ณธ [`Wav2Vec2Model`] ์œ„์— [CTC](#connectionist-temporal-classification-ctc)๋ฅผ ์ ์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋ง ํ—ค๋“œ์ž…๋‹ˆ๋‹ค. + +## I + +### ์ด๋ฏธ์ง€ ํŒจ์น˜ (image patch) + +๋น„์ „ ๊ธฐ๋ฐ˜ Transformer ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€๋ฅผ ์ž‘์€ ํŒจ์น˜๋กœ ๋ถ„ํ• ํ•œ ํ›„, ๊ฐ ํŒจ์น˜๋ฅผ ์„ ํ˜• ์ž„๋ฒ ๋”ฉํ•˜์—ฌ ์‹œํ€€์Šค๋กœ ๋ชจ๋ธ์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๊ตฌ์„ฑ ํŒŒ์ผ์—์„œ `patch_size`(๋˜๋Š” ํ•ด์ƒ๋„)๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ์ธํผ๋Ÿฐ์Šค (inference) + +์ธํผ๋Ÿฐ์Šค๋Š” ํ•™์Šต์ด ์™„๋ฃŒ๋œ ๋ชจ๋ธ์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๐Ÿค— Transformer์—์„œ ์ธํผ๋Ÿฐ์Šค๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ [Pipeline for inference](https://huggingface.co/docs/transformers/pipeline_tutorial) ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ์ž…๋ ฅ ID (input IDs) + +์ž…๋ ฅ ID๋Š” ์ข…์ข… ๋ชจ๋ธ์— ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ์œ ์ผํ•œ ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด๋“ค์€ ํ† ํฐ์˜ ์ธ๋ฑ์Šค๋กœ, ๋ชจ๋ธ์ด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์‹œํ€€์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํ† ํฐ๋“ค์˜ ์ˆซ์ž ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. + + + +ํ† ํฌ๋‚˜์ด์ €๋งˆ๋‹ค ์ž‘๋™ ๋ฐฉ์‹์€ ๋‹ค๋ฅด์ง€๋งŒ, ๊ธฐ๋ณธ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ [WordPiece](https://arxiv.org/pdf/1609.08144.pdf) ํ† ํฌ๋‚˜์ด์ €์ธ BERT ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") + +>>> sequence = "A Titan RTX has 24GB of VRAM" +``` + +ํ† ํฌ๋‚˜์ด์ €๋Š” ์‹œํ€€์Šค๋ฅผ ํ† ํฌ๋‚˜์ด์ €์˜ ํ† ํฐ ๋ชฉ๋ก์— ์žˆ๋Š” ํ•ญ๋ชฉ์œผ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> tokenized_sequence = tokenizer.tokenize(sequence) +``` + +ํ† ํฐ์€ ๋‹จ์–ด์ด๊ฑฐ๋‚˜ ์„œ๋ธŒ ์›Œ๋“œ(subword)์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "VRAM"์€ ๋ชจ๋ธ์˜ ์–ดํœ˜ ์‚ฌ์ „์— ์—†๋Š” ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— "V", "RA", "M"์œผ๋กœ ๋‚˜๋‰˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ† ํฐ๋“ค์ด ๊ฐœ๋ณ„ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ™์€ ๋‹จ์–ด์˜ ์ผ๋ถ€์ž„์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด "RA"์™€ "M" ์•ž์— ๋”๋ธ” ํ•ด์‹œ(`##`)๊ฐ€ ์ถ”๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + +```python +>>> print(tokenized_sequence) +['A', 'Titan', 'R', '##T', '##X', 'has', '24', '##GB', 'of', 'V', '##RA', '##M'] +``` + +์ด๋Ÿฌํ•œ ํ† ํฐ๋“ค์€ ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ID๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๋ฌธ์žฅ์„ ๋ฐ”๋กœ ํ† ํฌ๋‚˜์ด์ €์— ์ž…๋ ฅํ•จ์œผ๋กœ์จ ์ˆ˜ํ–‰๋˜๋ฉฐ, ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด [๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers)์˜ Rust ๊ตฌํ˜„์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> inputs = tokenizer(sequence) +``` + +ํ† ํฌ๋‚˜์ด์ €๋Š” ํ•ด๋‹น ๋ชจ๋ธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ์ธ์ž๋ฅผ ํฌํ•จํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ ์ธ๋ฑ์Šค๋Š” `input_ids`๋ผ๋Š” ํ‚ค์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. + +```python +>>> encoded_sequence = inputs["input_ids"] +>>> print(encoded_sequence) +[101, 138, 18696, 155, 1942, 3190, 1144, 1572, 13745, 1104, 159, 9664, 2107, 102] +``` + +ํ† ํฌ๋‚˜์ด์ €๋Š” (์—ฐ๊ฒฐ๋œ ๋ชจ๋ธ์ด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ) ์ž๋™์œผ๋กœ "ํŠน์ˆ˜ ํ† ํฐ"์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋ชจ๋ธ์ด ํŠน์ • ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน๋ณ„ํ•œ ID์ž…๋‹ˆ๋‹ค. + +์ด์ „์˜ ID ์‹œํ€€์Šค๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋ฉด, + +```python +>>> decoded_sequence = tokenizer.decode(encoded_sequence) +``` + +์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```python +>>> print(decoded_sequence) +[CLS] A Titan RTX has 24GB of VRAM [SEP] +``` + +์ด๋Š” [`BertModel`]์ด ์ž…๋ ฅ๊ฐ’์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +## L + +### ๋ ˆ์ด๋ธ” (labels) + +๋ ˆ์ด๋ธ”์€ ๋ชจ๋ธ์ด ์†์‹ค(loss)์„ ์ง์ ‘ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „๋‹ฌ๋˜๋Š” ์„ ํƒ์  ์ธ์ž์ž…๋‹ˆ๋‹ค. ์ด ๋ ˆ์ด๋ธ”์€ ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•ด์•ผ ํ•  ์ •๋‹ต ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ, ๋ชจ๋ธ์€ ์˜ˆ์ธก๊ฐ’๊ณผ ์ด ์ •๋‹ต(label) ์‚ฌ์ด์˜ ์ฐจ์ด๋ฅผ ํ‘œ์ค€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +์ด ๋ ˆ์ด๋ธ”(label)์˜ ํ˜•ํƒœ๋Š” ๋ชจ๋ธ ํ—ค๋“œ(model head)์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด: + +- ์‹œํ€€์Šค ๋ถ„๋ฅ˜ ๋ชจ๋ธ([`BertForSequenceClassification`] ๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ + `(batch_size)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๋ฐฐ์น˜์˜ ๊ฐ ๊ฐ’์€ ์ „์ฒด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +- ํ† ํฐ ๋ถ„๋ฅ˜ ๋ชจ๋ธ([`BertForTokenClassification`] ๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `(batch_size, seq_length)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ํ† ํฐ์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +- ๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ([`BertForMaskedLM`])์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `(batch_size,seq_length)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ํ† ํฐ์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์€ ๋งˆ์Šคํ‚น ๋œ ํ† ํฐ์˜ ํ† ํฐ ID์ด๋ฉฐ, ๋‚˜๋จธ์ง€ ํ† ํฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋ฌด์‹œํ•  ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(์ผ๋ฐ˜์ ์œผ๋กœ -100). +- ์‹œํ€€์Šค ํˆฌ ์‹œํ€€์Šค ์ž‘์—…([`BartForConditionalGeneration`], [`MBartForConditionalGeneration`]๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `(batch_size, tgt_seq_length)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๊ฐ ๊ฐ’์€ ์ž…๋ ฅ ์‹œํ€€์Šค์— ๋Œ€์‘ํ•˜๋Š” ํƒ€๊ฒŸ ์‹œํ€€์Šค๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ•™์Šต ์ค‘์—๋Š” BART์™€ T5๊ฐ€ ์ ์ ˆํ•œ `decoder_input_ids`์™€ ๋””์ฝ”๋” attention ๋งˆ์Šคํฌ๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋ฏ€๋กœ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋”ฐ๋กœ ์ œ๊ณตํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ, ์ด๋Š” Encoder-Decoder ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ง์ ‘ ํ™œ์šฉํ•˜๋Š” ๋ชจ๋ธ์—๋Š” ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ([`ViTForImageClassification`] ๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `(batch_size)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๋ฐฐ์น˜์˜ ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +- ์‹œ๋ฉ˜ํ‹ฑ ์„ธ๊ทธ๋ฉ˜ํ…Œ์ด์…˜ ๋ชจ๋ธ([`SegformerForSemanticSegmentation`] ๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `(batch_size, height, width)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๋ฐฐ์น˜์˜ ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ํ”ฝ์…€์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +- ๊ฐ์ฒด ํƒ์ง€ ๋ชจ๋ธ([`DetrForObjectDetection`] ๋“ฑ)์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ `class_labels`์™€ `boxes` ํ‚ค๋ฅผ ํฌํ•จํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜์˜ ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์˜ˆ์ƒ ํด๋ž˜์Šค ๋ ˆ์ด๋ธ”๊ณผ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +- ์ž๋™ ์Œ์„ฑ ์ธ์‹ ๋ชจ๋ธ([`Wav2Vec2ForCTC`] ๋“ฑ)์˜ ๊ฒฝ์šฐ ๋ชจ๋ธ์€ `(batch_size,target_length)` ์ฐจ์›์˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ๊ฐ ๊ฐ’์€ ๊ฐœ๋ณ„ ํ† ํฐ์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋ ˆ์ด๋ธ”์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + + + +๋ชจ๋ธ๋งˆ๋‹ค ์š”๊ตฌํ•˜๋Š” ๋ ˆ์ด๋ธ” ํ˜•์‹์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ฐ ๋ชจ๋ธ์˜ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์—ฌ ํ•ด๋‹น ๋ชจ๋ธ์— ๋งž๋Š” ๋ ˆ์ด๋ธ” ํ˜•์‹์„ ๋ฐ˜๋“œ์‹œ ํ™•์ธํ•˜์„ธ์š”! + + + +๊ธฐ๋ณธ ๋ชจ๋ธ([`BertModel`] ๋“ฑ)์€ ๋ ˆ์ด๋ธ”์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ๋‹จ์ˆœํžˆ ํŠน์ง•(feature)์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋ณธ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +### ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ (LLM) + +๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋œ ํŠธ๋žœ์Šคํฌ๋จธ ์–ธ์–ด ๋ชจ๋ธ(GPT-3, BLOOM, OPT ๋“ฑ)์„ ์ง€์นญํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์šฉ์–ด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ(parameter)์˜ ์ˆ˜๊ฐ€ ๋งค์šฐ ๋งŽ์œผ๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด GPT-3๋Š” ์•ฝ 1,750์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +## M + +### ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง (MLM) + +์‚ฌ์ „ ํ•™์Šต ๋‹จ๊ณ„ ์ค‘ ํ•˜๋‚˜๋กœ, ๋ชจ๋ธ์€ ์ผ๋ถ€ ํ† ํฐ์ด ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚น ๋œ ์†์ƒ๋œ ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์›๋ž˜์˜ ๋ฌธ์žฅ์„ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +### ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ (multimodal) + +ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ์ž…๋ ฅ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. + +## N + +### ์ž์—ฐ์–ด ์ƒ์„ฑ (NLG) + +ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋“  ์ž‘์—…์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: [Write With Transformers](https://transformer.huggingface.co/), ๋ฒˆ์—ญ ๋“ฑ). + +### ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ (NLP) + +ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ž‘์—… ์ „๋ฐ˜์„ ์ง€์นญํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์šฉ์–ด์ž…๋‹ˆ๋‹ค. + +### ์ž์—ฐ์–ด ์ดํ•ด (NLU) + +ํ…์ŠคํŠธ์— ๋‹ด๊ธด ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ชจ๋“  ์ž‘์—…์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: ์ „์ฒด ๋ฌธ์„œ ๋ถ„๋ฅ˜, ๊ฐœ๋ณ„ ๋‹จ์–ด ๋ถ„๋ฅ˜ ๋“ฑ). + +## P + +### ํŒŒ์ดํ”„๋ผ์ธ (pipeline) + +๐Ÿค— Transformers์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณ€ํ™˜ํ•œ ํ›„, ๋ชจ๋ธ์„ ํ†ตํ•ด ์˜ˆ์ธก๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ผ๋ จ์˜ ๋‹จ๊ณ„๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ์ถ”์ƒํ™”๋œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ๋Š” ๋‹จ๊ณ„๋กœ๋Š” ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํŠน์ง• ์ถ”์ถœ(feature extraction), ์ •๊ทœํ™”(normalization) ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ž์„ธํ•œ ๋‚ด์šฉ์€ [Pipelines for inference](https://huggingface.co/docs/transformers/pipeline_tutorial) ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌํ™” (PP) + +๋ชจ๋ธ์„ ์ˆ˜์ง ๋ฐฉํ–ฅ(๋ ˆ์ด์–ด ๋‹จ์œ„)์œผ๋กœ ์—ฌ๋Ÿฌ GPU์— ๋ถ„ํ• ํ•˜์—ฌ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ณ‘๋ ฌํ™” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ฐ GPU๋Š” ๋ชจ๋ธ์˜ ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ ˆ์ด์–ด๋งŒ์„ ๋‹ด๋‹นํ•˜๋ฉฐ, ์ „์ฒด ํŒŒ์ดํ”„๋ผ์ธ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ๊ณ„๋ฅผ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ GPU๋Š” ๋ฐฐ์น˜(batch)์˜ ์ผ๋ถ€ ์ž‘์€ ์กฐ๊ฐ๋งŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. Pipeline Parallel ๋ฐฉ์‹์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด [์ด ๋ฌธ์„œ](perf_train_gpu_many#from-naive-model-parallelism-to-pipeline-parallelism)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ํ”ฝ์…€ ๊ฐ’ (pixel values) + +์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์น˜์ƒ์œผ๋กœ ํ‘œํ˜„ํ•œ ํ…์„œ๋กœ, ๋ชจ๋ธ์— ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด ํ…์„œ๋Š” ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋ฉด, ๊ฐ’์€ [`batch_size`, `num_channels`, `height`, `width`] ํ˜•ํƒœ์˜ ์ฐจ์›์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. + +### ํ’€๋ง (pooling) + +ํ–‰๋ ฌ์˜ ํŠน์ • ์ฐจ์›์—์„œ ์ตœ๋Œ“๊ฐ’์ด๋‚˜ ํ‰๊ท ๊ฐ’์„ ์ทจํ•˜์—ฌ ๋” ์ž‘์€ ํ–‰๋ ฌ๋กœ ์ค„์ด๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ํ’€๋ง ๊ณ„์ธต์€ ์ฃผ๋กœ ํ•ฉ์„ฑ๊ณฑ ๊ณ„์ธต ์‚ฌ์ด์— ์œ„์น˜ํ•˜์—ฌ ํŠน์ง• ํ‘œํ˜„์„ ๋‹ค์šด์ƒ˜ํ”Œ๋ง ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +### ํฌ์ง€์…˜ ID (position IDs) + +RNN ๋ชจ๋ธ๊ณผ ๋‹ฌ๋ฆฌ ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๊ฐ ํ† ํฐ์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์€ `position_ids`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ† ํฐ์ด ์‹œํ€€์Šค ๋‚ด์—์„œ ์–ด๋А ์œ„์น˜์— ์žˆ๋Š”์ง€๋ฅผ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ์„ ํƒ์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์— `position_ids`๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด, ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ ๋ฐฉ์‹์œผ๋กœ ์ž๋™ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์€ `[0, config.max_position_embeddings - 1]` ๋ฒ”์œ„ ๋‚ด์—์„œ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ชจ๋ธ์€ ์‚ฌ์ธํŒŒ ํ˜•ํƒœ์˜ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ(sinusoidal position embeddings) ๋˜๋Š” ์ƒ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ(relative position embeddings)๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์œ ํ˜•์˜ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. + +### ์ „์ฒ˜๋ฆฌ (preprocessing) + +๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ€๊ณต๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ œํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ…์ŠคํŠธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ† ํฐํ™”(tokenization) ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž…๋ ฅ ์œ ํ˜•์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ์‹์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด [Preprocess](https://huggingface.co/docs/transformers/preprocessing) ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ๊ณ ํ•ด ๋ณด์„ธ์š”. + +### ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ (pretrained model) + +์ผ๋ถ€ ๋ฐ์ดํ„ฐ(์˜ˆ: ์œ„ํ‚คํ”ผ๋””์•„ ์ „์ฒด)๋กœ ์‚ฌ์ „ ํ•™์Šต(pretraining)๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต์€ ์ž๊ธฐ ์ง€๋„ ํ•™์Šต(self-supervised learning)์˜ ๋ชฉํ‘œ๋ฅผ ํฌํ•จํ•˜๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฌธ์žฅ์„ ์ฝ๊ณ  ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฑฐ๋‚˜ ([causal language modeling](#causal-language-modeling)) ์ฐธ๊ณ , ์ผ๋ถ€ ๋‹จ์–ด๋ฅผ ๋งˆ์Šคํ‚นํ•˜๊ณ  ์ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ์‹([masked language modeling](#masked-language-modeling-mlm))์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์Œ์„ฑ ๋ฐ ๋น„์ „ ๋ชจ๋ธ์€ ๊ณ ์œ ์˜ ์‚ฌ์ „ ํ•™์Šต ๋ชฉํ‘œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Wav2Vec2๋Š” ์Œ์„ฑ ํ‘œํ˜„ ์ค‘ "์ง„์งœ"๋ฅผ "๊ฐ€์งœ" ์ค‘์—์„œ ๊ตฌ๋ถ„ํ•˜๋Š” ๋Œ€์กฐ ํ•™์Šต(contrastive learning) ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ์Œ์„ฑ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, BEiT๋Š” ์ด๋ฏธ์ง€ ํŒจ์น˜ ์ค‘ ์ผ๋ถ€๋ฅผ ๋งˆ์Šคํ‚นํ•˜๊ณ  ์ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งˆ์Šคํ‚น ์ด๋ฏธ์ง€ ๋ชจ๋ธ๋ง ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋น„์ „ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ๋ง๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +## R + +### ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง (RNN) + +ํ…์ŠคํŠธ์™€ ๊ฐ™์€ ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆ์ด์–ด์— ๋ฐ˜๋ณต ๊ตฌ์กฐ(๋ฃจํ”„)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์˜ ํ•œ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค. + +### ํ‘œํ˜„ํ•™์Šต (representation learning) + +๋จธ์‹ ๋Ÿฌ๋‹์˜ ํ•˜์œ„ ๋ถ„์•ผ๋กœ, ์›์‹œ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์˜๋ฏธ ์žˆ๋Š” ํ‘œํ˜„์„ ํ•™์Šตํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๊ธฐ๋ฒ•์œผ๋กœ๋Š” ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ, ์˜คํ† ์ธ์ฝ”๋”(autoencoder), ์ƒ์„ฑ์  ์ ๋Œ€ ์‹ ๊ฒฝ๋ง(GAN) ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +## S + +### ์ƒ˜ํ”Œ๋ง ์†๋„ (sampling rate) + +์ƒ˜ํ”Œ๋ง ์†๋„๋Š” 1์ดˆ์— ์ถ”์ถœํ•˜๋Š” (์˜ค๋””์˜ค ์‹ ํ˜ธ) ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ—ค๋ฅด์ธ (Hz) ๋‹จ์œ„๋กœ ๋‚˜ํƒ€๋‚ธ ์ธก์ •๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์Œ์„ฑ์ฒ˜๋Ÿผ ์—ฐ์†์ ์ธ ์‹ ํ˜ธ๋ฅผ ๋””์ง€ํ„ธํ™”ํ•˜์—ฌ ์ด์‚ฐ์ ์ธ ํ˜•ํƒœ๋กœ ๋งŒ๋“œ๋Š” ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. + +### ์…€ํ”„ ์–ดํ…์…˜ (self-attention) + +์ž…๋ ฅ์˜ ๊ฐ ์š”์†Œ๊ฐ€ ๋‹ค๋ฅธ ์–ด๋–ค ์š”์†Œ์— ์ฃผ๋ชฉํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์Šค์Šค๋กœ ํŒ๋‹จํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ๋ฌธ์žฅ์—์„œ ํŠน์ • ๋‹จ์–ด๋งŒ์„ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์–ด๋–ค ์ •๋ณด์— ๋” ์ง‘์ค‘ํ•ด์•ผ ํ• ์ง€๋ฅผ ํ•™์Šตํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +### ์ž๊ธฐ์ง€๋„ ํ•™์Šต (self-supervised learning) + +๋ ˆ์ด๋ธ”์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ์ด ์Šค์Šค๋กœ ํ•™์Šต ๋ชฉํ‘œ๋ฅผ ์ •์˜ํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๊ธฐ๋ฒ•์˜ ํ•œ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค. [๋น„์ง€๋„ ํ•™์Šต](#unsupervised-learning)์ด๋‚˜ [์ง€๋„ ํ•™์Šต](#supervised-learning)๊ณผ ๋‹ฌ๋ฆฌ, ํ•™์Šต ๊ณผ์ • ์ž์ฒด๋Š” ๊ฐ๋… ๋ฐฉ์‹ ๋˜์ง€๋งŒ, ๋ผ๋ฒจ์ด ๋ช…์‹œ์ ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. + +์˜ˆ์‹œ๋กœ๋Š” [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง](#masked-language-modeling-mlm)์ด ์žˆ์œผ๋ฉฐ, ์ด๋Š” ๋ฌธ์žฅ์˜ ์ผ๋ถ€ ํ† ํฐ์„ ์ œ๊ฑฐํ•œ ์ƒํƒœ๋กœ ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜๊ณ , ๋ชจ๋ธ์ด ํ•ด๋‹น ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋„๋ก ํ•™์Šตํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +### ์ค€์ง€๋„ ํ•™์Šต (semi-supervised learning) + +์†Œ๋Ÿ‰์˜ ๋ผ๋ฒจ์ด ๋‹ฌ๋ฆฐ ๋ฐ์ดํ„ฐ์™€ ๋Œ€๋Ÿ‰์˜ ๋ผ๋ฒจ์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ํ›ˆ๋ จ ๊ธฐ๋ฒ•์˜ ๋„“์€ ๋ฒ”์ฃผ์ž…๋‹ˆ๋‹ค. ์ด๋Š” [์ง€๋„ ํ•™์Šต](#supervised-learning)์ด๋‚˜ [๋น„์ง€๋„ ํ•™์Šต](#unsupervised-learning)๊ณผ๋Š” ๋‹ค๋ฅธ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. + +์ค€์ง€๋„ ํ•™์Šต ๊ธฐ๋ฒ•์˜ ์˜ˆ๋กœ๋Š” "์ž๊ธฐ ํ•™์Šต(self-training)"์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๋จผ์ € ๋ผ๋ฒจ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ณ , ๊ทธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ผ๋ฒจ์ด ์—†๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ๊ฐ€์žฅ ๋†’์€ ํ™•์‹ ์„ ๊ฐ€์ง€๊ณ  ์˜ˆ์ธกํ•œ ๋ผ๋ฒจ์ด ์—†๋Š” ๋ฐ์ดํ„ฐ ์ผ๋ถ€๋ฅผ ๋ผ๋ฒจ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์„ ๋‹ค์‹œ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. + +### ์‹œํ€€์Šค ํˆฌ ์‹œํ€€์Šค (seq2seq) + +์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฒˆ์—ญ ๋ชจ๋ธ์ด๋‚˜ ์š”์•ฝ ๋ชจ๋ธ์ด ์ด์— ํ•ด๋‹นํ•˜๋ฉฐ, ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ๋Š” [Bart](model_doc/bart)๋‚˜[T5](model_doc/t5) ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +### ๋ถ„ํ•  DDP (Sharded DDP) + +[ZeRO](#zero-redundancy-optimizer-zero) ๊ฐœ๋…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์–‘ํ•œ ๊ตฌํ˜„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. + +### ์ŠคํŠธ๋ผ์ด๋“œ (stride) + +[convolution](#convolution) ๋˜๋Š” [pooling](#pooling)์—์„œ ์ŠคํŠธ๋ผ์ด๋“œ(stride)๋Š” ์ปค๋„์ด ํ–‰๋ ฌ ์œ„๋ฅผ ์ด๋™ํ•˜๋Š” ๊ฐ„๊ฒฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 1์ด๋ฉด ์ปค๋„์ด ํ•œ ํ”ฝ์…€์”ฉ ์ด๋™ํ•˜๊ณ , 2์ด๋ฉด ๋‘ ํ”ฝ์…€์”ฉ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. + +### ์ง€๋„ํ•™์Šต (supervised learning) + +์ •๋‹ต์ด ํฌํ•จ๋œ ๋ผ๋ฒจ๋ง๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ํ•™์Šต ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ํ•™์Šต ์ค‘์ธ ๋ชจ๋ธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์ •๋‹ต๊ณผ ๋น„๊ตํ•˜์—ฌ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ด ์˜ค์ฐจ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•ฉ๋‹ˆ๋‹ค. + +## T + +### ํ…์„œ ๋ณ‘๋ ฌํ™” (TP) + +์—ฌ๋Ÿฌ GPU์—์„œ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•œ ๋ณ‘๋ ฌํ™” ๊ธฐ๋ฒ•์œผ๋กœ, ๊ฐ ํ…์„œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฉ์–ด๋ฆฌ(chunk)๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ „์ฒด ํ…์„œ๊ฐ€ ๋‹จ์ผ GPU์— ์ƒ์ฃผํ•˜๋Š” ๋Œ€์‹ , ํ…์„œ์˜ ๊ฐ ์กฐ๊ฐ(shard)์ด ์ง€์ •๋œ GPU์— ์ƒ์ฃผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์กฐ๊ฐ๋“ค์€ ๊ฐ๊ฐ ๋‹ค๋ฅธ GPU์—์„œ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋˜๋ฉฐ, ์ฒ˜๋ฆฌ ๋‹จ๊ณ„๊ฐ€ ๋๋‚  ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ„ํ• ์ด ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Š” ๋•Œ๋•Œ๋กœ ์ˆ˜ํ‰์  ๋ณ‘๋ ฌํ™”๋ผ๊ณ  ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. Tensor Parallelism์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด [์—ฌ๊ธฐ](perf_train_gpu_many#tensor-parallelism)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ํ† ํฐ (token) + +์ผ๋ฐ˜์ ์ธ ๋‹จ์–ด ๋‹จ์œ„์ด์ง€๋งŒ, ๋•Œ์— ๋”ฐ๋ผ ์„œ๋ธŒ ์›Œ๋“œ(์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌ๋จ)๋‚˜ ๋ฌธ์žฅ ๋ถ€ํ˜ธ๋„ ํฌํ•จ๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์žฅ์˜ ๊ตฌ์„ฑ ์š”์†Œ์ž…๋‹ˆ๋‹ค. + +### ํ† ํฐ ํƒ€์ž… ID (token type IDs) + +์ผ๋ถ€ ๋ชจ๋ธ์€ ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜๋‚˜ ์งˆ์˜ ์‘๋‹ต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + + +์ด๋Ÿฌํ•œ ์ž‘์—…์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์˜ "input_ids" ํ•ญ๋ชฉ์œผ๋กœ ๊ฒฐํ•ฉํ•ด์•ผ ํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ `[CLS]` ๋ถ„๋ฅ˜์šฉ ๋ฐ `[SEP]` ๊ตฌ๋ถ„์šฉ๊ณผ ๊ฐ™์€ ํŠน์ˆ˜ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, BERT ๋ชจ๋ธ์€ ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค: + +```python +>>> # [CLS] SEQUENCE_A [SEP] SEQUENCE_B [SEP] +``` + +๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ `tokenizer`์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ณ„ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด, ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ์ž๋™์œผ๋กœ ์ด๋Ÿฌํ•œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased") +>>> sequence_a = "HuggingFace is based in NYC" +>>> sequence_b = "Where is HuggingFace based?" + +>>> encoded_dict = tokenizer(sequence_a, sequence_b) +>>> decoded = tokenizer.decode(encoded_dict["input_ids"]) +``` + +๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +>>> print(decoded) +[CLS] HuggingFace is based in NYC [SEP] Where is HuggingFace based? [SEP] +``` + +์ด ์ฝ”๋“œ๋Š” ์ผ๋ถ€ ๋ชจ๋ธ์ด ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ๋ฐ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ BERT์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ชจ๋ธ์€ ํ† ํฐ ํƒ€์ž… ID(๋˜๋Š” ์„ธ๊ทธ๋จผํŠธ ID)๋ฅผ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ID๋Š” 0๊ณผ 1๋กœ ๊ตฌ์„ฑ๋œ ์ด์ง„ ๋งˆ์Šคํฌ๋กœ, ๋‘ ์‹œํ€€์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. + +ํ† ํฌ๋‚˜์ด์ €๋Š” ์ด ๋งˆ์Šคํฌ๋ฅผ "token_type_id" ํ•ญ๋ชฉ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```python +>>> encoded_dict["token_type_ids"] +[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1] +``` + +์งˆ๋ฌธ์— ์‚ฌ์šฉ๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ์‹œํ€€์Šค์ธ "context"๋Š” ๋ชจ๋“  ํ† ํฐ์ด `0`์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ๋‘ ๋ฒˆ์งธ ์‹œํ€€์Šค์ธ "question"์€ ๋ชจ๋“  ํ† ํฐ์ด `1`๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. + +์ผ๋ถ€ ๋ชจ๋ธ(์˜ˆ: [`XLNetModel`])์€ `2`๋กœ ํ‘œ์‹œ๋˜๋Š” ์ถ”๊ฐ€ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. + +### ์ „์ดํ•™์Šต (transfer learning) + +์‚ฌ์ „ ํ•™์Šต๋œ(pretrained) ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€ ํŠน์ • ์ž‘์—…์— ๋งž๋Š” ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•ด ์ถ”๊ฐ€ ํ•™์Šตํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šต์‹œํ‚ค๋Š” ๋Œ€์‹ , ๊ธฐ์กด ๋ชจ๋ธ์ด ํ•™์Šตํ•œ ์ง€์‹์„ ์ถœ๋ฐœ์ ์œผ๋กœ ์‚ผ์•„ ๋”์šฑ ๋น ๋ฅด๊ฒŒ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ•™์Šต ์†๋„๋ฅผ ๋†’์ด๊ณ  ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์–‘๋„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ํŠธ๋žœ์Šคํฌ๋จธ (transformer) + +์…€ํ”„ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. + +## U + +### ๋น„์ง€๋„ ํ•™์Šต (unsupervised learning) + +์ •๋‹ต(๋ ˆ์ด๋ธ”)์ด ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋น„์ง€๋„ ํ•™์Šต์€ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ์˜ ํ†ต๊ณ„์  ํŠน์„ฑ์„ ํ™œ์šฉํ•ด ์œ ์šฉํ•œ ํŒจํ„ด์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. + +## Z + +### Zero Redundancy Optimizer (ZeRO) + +[TensorParallel](#tensor-parallelism-tp)๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ํ…์„œ๋ฅผ ์ƒค๋”ฉ(sharding)ํ•˜๋Š” ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•์ด์ง€๋งŒ, ์ˆœ์ „ํŒŒ(forward)๋‚˜ ์—ญ์ „ํŒŒ(backward) ๊ณ„์‚ฐ ์‹œ์ ์— ์ „์ฒด ํ…์„œ๋ฅผ ๋‹ค์‹œ ๋ณต์›ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•  ๊ฒฝ์šฐ ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์˜คํ”„๋กœ๋”ฉ (offloading) ๊ธฐ๋ฒ•๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +ZeRO์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด [์ด ๋ฌธ์„œ](perf_train_gpu_many#zero-data-parallelism)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/how_to_hack_models.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/how_to_hack_models.md new file mode 100644 index 0000000000000000000000000000000000000000..9ef4839335f32bfd6519e464061eb017128ce0eb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/how_to_hack_models.md @@ -0,0 +1,152 @@ + + +# ๋ชจ๋ธ ๊ตฌ์„ฑ ์š”์†Œ ๋งž์ถค ์„ค์ •ํ•˜๊ธฐ[[customizing-model-components]] + +๋ชจ๋ธ์„ ์™„์ „ํžˆ ์ƒˆ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋ชจ๋ธ์„ ๋งž์ถค ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๋ชจ๋ธ์„ ํŠน์ • ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ๋ชจ๋ธ์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ƒˆ๋กœ์šด ๋ ˆ์ด์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์•„ํ‚คํ…์ฒ˜์˜ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋งž์ถค ์„ค์ •์€ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์— ์ง์ ‘ ์ ์šฉ๋˜๋ฏ€๋กœ, [`Trainer`], [`PreTrainedModel`] ๋ฐ [PEFT](https://huggingface.co/docs/peft/en/index) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋ชจ๋ธ์˜ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋งž์ถค ์„ค์ •ํ•˜์—ฌ [Low-Rank Adaptation (LoRA)](https://huggingface.co/docs/peft/conceptual_guides/adapter#low-rank-adaptation-lora)๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +> [!TIP] +> ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  ๊ฐœ๋ฐœํ•  ๋•Œ [clear_import_cache](https://github.com/huggingface/transformers/blob/9985d06add07a4cc691dc54a7e34f54205c04d40/src/transformers/utils/import_utils.py#L2286) ์œ ํ‹ธ๋ฆฌํ‹ฐ๊ฐ€ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ์บ์‹œ๋œ ๋ชจ๋“  ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋“ˆ์„ ์ œ๊ฑฐํ•˜์—ฌ Python์ด ํ™˜๊ฒฝ์„ ์žฌ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ ๋„ ์ˆ˜์ •๋œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. +> +> ```py +> from transformers import AutoModel +> from transformers.utils.import_utils import clear_import_cache +> +> model = AutoModel.from_pretrained("bert-base-uncased") +> # ๋ชจ๋ธ ์ฝ”๋“œ ์ˆ˜์ • +> # ์บ์‹œ๋ฅผ ์ง€์›Œ ์ˆ˜์ •๋œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๊ธฐ +> clear_import_cache() +> # ์—…๋ฐ์ดํŠธ๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๊ธฐ +> model = AutoModel.from_pretrained("bert-base-uncased") +> ``` + +## ์–ดํ…์…˜ ํด๋ž˜์Šค[[attention-class]] + +[Segment Anything](./model_doc/sam)์€ ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ๋กœ, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ query-key-value(`qkv`) ํ”„๋กœ์ ์…˜์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜์™€ ์—ฐ์‚ฐ ๋ถ€๋‹ด์„ ์ค„์ด๊ธฐ ์œ„ํ•ด `qkv` ํ”„๋กœ์ ์…˜์— LoRA๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” `qkv` ํ”„๋กœ์ ์…˜์„ ๋ถ„๋ฆฌํ•˜์—ฌ `q`์™€ `v`์— LoRA๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +1. ์›๋ž˜์˜ `SamVisionAttention` ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์—ฌ `SamVisionAttentionSplit`์ด๋ผ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์–ดํ…์…˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. `__init__`์—์„œ ๊ฒฐํ•ฉ๋œ `qkv`๋ฅผ ์‚ญ์ œํ•˜๊ณ , `q`, `k`, `v`๋ฅผ ์œ„ํ•œ ๊ฐœ๋ณ„ ์„ ํ˜• ๋ ˆ์ด์–ด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +```py +import torch +import torch.nn as nn +from transformers.models.sam.modeling_sam import SamVisionAttention + +class SamVisionAttentionSplit(SamVisionAttention, nn.Module): + def __init__(self, config, window_size): + super().__init__(config, window_size) + # ๊ฒฐํ•ฉ๋œ qkv ์ œ๊ฑฐ + del self.qkv + # q, k, v ๊ฐœ๋ณ„ ํ”„๋กœ์ ์…˜ ์ƒ์„ฑ + self.q = nn.Linear(config.hidden_size, config.hidden_size, bias=config.qkv_bias) + self.k = nn.Linear(config.hidden_size, config.hidden_size, bias=config.qkv_bias) + self.v = nn.Linear(config.hidden_size, config.hidden_size, bias=config.qkv_bias) + self._register_load_state_dict_pre_hook(self.split_q_k_v_load_hook) +``` + +2. `_split_qkv_load_hook` ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ, ์‚ฌ์ „ ํ›ˆ๋ จ๋œ `qkv` ๊ฐ€์ค‘์น˜๋ฅผ `q`, `k`, `v`๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๊ณผ์˜ ํ˜ธํ™˜์„ฑ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. + +```py + def split_q_k_v_load_hook(self, state_dict, prefix, *args): + keys_to_delete = [] + for key in list(state_dict.keys()): + if "qkv." in key: + # ๊ฒฐํ•ฉ๋œ ํ”„๋กœ์ ์…˜์—์„œ q, k, v ๋ถ„๋ฆฌ + q, k, v = state_dict[key].chunk(3, dim=0) + # ๊ฐœ๋ณ„ q, k, v ํ”„๋กœ์ ์…˜์œผ๋กœ ๋Œ€์ฒด + state_dict[key.replace("qkv.", "q.")] = q + state_dict[key.replace("qkv.", "k.")] = k + state_dict[key.replace("qkv.", "v.")] = v + # ๊ธฐ์กด qkv ํ‚ค๋ฅผ ์‚ญ์ œ ๋Œ€์ƒ์œผ๋กœ ํ‘œ์‹œ + keys_to_delete.append(key) + + # ๊ธฐ์กด qkv ํ‚ค ์ œ๊ฑฐ + for key in keys_to_delete: + del state_dict[key] +``` + +3. `forward` ๋‹จ๊ณ„์—์„œ `q`, `k`, `v`๋Š” ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ณ„์‚ฐ๋˜๋ฉฐ, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. + +```py + def forward(self, hidden_states: torch.Tensor, output_attentions=False) -> torch.Tensor: + batch_size, height, width, _ = hidden_states.shape + qkv_shapes = (batch_size * self.num_attention_heads, height * width, -1) + query = self.q(hidden_states).reshape((batch_size, height * width,self.num_attention_heads, -1)).permute(0,2,1,3).reshape(qkv_shapes) + key = self.k(hidden_states).reshape((batch_size, height * width,self.num_attention_heads, -1)).permute(0,2,1,3).reshape(qkv_shapes) + value = self.v(hidden_states).reshape((batch_size, height * width,self.num_attention_heads, -1)).permute(0,2,1,3).reshape(qkv_shapes) + + attn_weights = (query * self.scale) @ key.transpose(-2, -1) + + attn_weights = torch.nn.functional.softmax(attn_weights, dtype=torch.float32, dim=-1).to(query.dtype) + attn_probs = nn.functional.dropout(attn_weights, p=self.dropout, training=self.training) + attn_output = (attn_probs @ value).reshape(batch_size, self.num_attention_heads, height, width, -1) + attn_output = attn_output.permute(0, 2, 3, 1, 4).reshape(batch_size, height, width, -1) + attn_output = self.proj(attn_output) + + if output_attentions: + outputs = (attn_output, attn_weights) + else: + outputs = (attn_output, None) + return outputs +``` + +์‚ฌ์šฉ์ž ์ •์˜ `SamVisionAttentionSplit` ํด๋ž˜์Šค๋ฅผ ์›๋ณธ ๋ชจ๋ธ์˜ `SamVisionAttention` ๋ชจ๋“ˆ์— ํ• ๋‹นํ•˜์—ฌ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋‚ด ๋ชจ๋“  `SamVisionAttention` ์ธ์Šคํ„ด์Šค๋Š” ๋ถ„๋ฆฌ๋œ ์–ดํ…์…˜ ๋ฒ„์ „์œผ๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค. + +[`~PreTrainedModel.from_pretrained`]๋กœ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค์„ธ์š”. + +```py +from transformers import SamModel + +# ์‚ฌ์ „ ํ›ˆ๋ จ๋œ SAM ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ +model = SamModel.from_pretrained("facebook/sam-vit-base") + +# ๋น„์ „-์ธ์ฝ”๋” ๋ชจ๋“ˆ์—์„œ ์–ดํ…์…˜ ํด๋ž˜์Šค ๊ต์ฒด +for layer in model.vision_encoder.layers: + if hasattr(layer, "attn"): + layer.attn = SamVisionAttentionSplit(model.config.vision_config, model.config.vision_config.window_size) +``` + +## LoRA[[lora]] + +๋ถ„๋ฆฌ๋œ `q`, `k`, `v` ํ”„๋กœ์ ์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ , `q`์™€ `v`์— LoRA๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +[LoraConfig](https://huggingface.co/docs/peft/package_reference/config#peft.PeftConfig)๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋žญํฌ `r`, `lora_alpha`, `lora_dropout`, `task_type`, ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์šฉ๋  ๋ชจ๋“ˆ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. + +```py +from peft import LoraConfig, get_peft_model + +config = LoraConfig( + r=16, + lora_alpha=32, + # q์™€ v์— LoRA ์ ์šฉ + target_modules=["q", "v"], + lora_dropout=0.1, + task_type="FEATURE_EXTRACTION" +) +``` + +๋ชจ๋ธ๊ณผ [LoraConfig](https://huggingface.co/docs/peft/package_reference/config#peft.PeftConfig)๋ฅผ [get\_peft\_model](https://huggingface.co/docs/peft/package_reference/peft_model#peft.get_peft_model)์— ์ „๋‹ฌํ•˜์—ฌ ๋ชจ๋ธ์— LoRA๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```py +model = get_peft_model(model, config) +``` + +[print_trainable_parameters](https://huggingface.co/docs/peft/package_reference/peft_model#peft.PeftMixedModel.print_trainable_parameters)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜ ๋Œ€๋น„ ํ›ˆ๋ จ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +```py +model.print_trainable_parameters() +"trainable params: 589,824 || all params: 94,274,096 || trainable%: 0.6256" +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/hpo_train.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/hpo_train.md new file mode 100644 index 0000000000000000000000000000000000000000..c0982db5e093b55b750b655ea6e694c0f62826a8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/hpo_train.md @@ -0,0 +1,124 @@ + + +# Trainer API๋ฅผ ์‚ฌ์šฉํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ [[hyperparameter-search-using-trainer-api]] + +๐Ÿค— Transformers์—์„œ๋Š” ๐Ÿค— Transformers ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋Š”๋ฐ ์ตœ์ ํ™”๋œ [`Trainer`] ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์‚ฌ์šฉ์ž๋Š” ์ง์ ‘ ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ๋”์šฑ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•™์Šต์„ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, [`Trainer`]๋Š” ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰์„ ์œ„ํ•œ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ์—์„œ ์ด API๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜ˆ์‹œ์™€ ํ•จ๊ป˜ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๋ฐฑ์—”๋“œ [[hyperparameter-search-backend]] + +[`Trainer`]๋Š” ํ˜„์žฌ ์•„๋ž˜ 4๊ฐ€์ง€ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๋ฐฑ์—”๋“œ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค: +[optuna](https://optuna.org/)์™€ [sigopt](https://sigopt.com/), [raytune](https://docs.ray.io/en/latest/tune/index.html), [wandb](https://wandb.ai/site/sweeps) ์ž…๋‹ˆ๋‹ค. + +ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๋ฐฑ์—”๋“œ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์„ค์น˜ํ•˜์„ธ์š”. +```bash +pip install optuna/sigopt/wandb/ray[tune] +``` + +## ์˜ˆ์ œ์—์„œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ• [[how-to-enable-hyperparameter-search-in-example]] + +ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๊ณต๊ฐ„์„ ์ •์˜ํ•˜์„ธ์š”. ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๋ฐฑ์—”๋“œ๋งˆ๋‹ค ์„œ๋กœ ๋‹ค๋ฅธ ํ˜•์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +sigopt์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น [object_parameter](https://docs.sigopt.com/ai-module-api-references/api_reference/objects/object_parameter) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์„ธ์š”: +```py +>>> def sigopt_hp_space(trial): +... return [ +... {"bounds": {"min": 1e-6, "max": 1e-4}, "name": "learning_rate", "type": "double"}, +... { +... "categorical_values": ["16", "32", "64", "128"], +... "name": "per_device_train_batch_size", +... "type": "categorical", +... }, +... ] +``` + +optuna์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น [object_parameter](https://optuna.readthedocs.io/en/stable/tutorial/10_key_features/002_configurations.html#sphx-glr-tutorial-10-key-features-002-configurations-py) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์„ธ์š”: + +```py +>>> def optuna_hp_space(trial): +... return { +... "learning_rate": trial.suggest_float("learning_rate", 1e-6, 1e-4, log=True), +... "per_device_train_batch_size": trial.suggest_categorical("per_device_train_batch_size", [16, 32, 64, 128]), +... } +``` + +raytune์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น [object_parameter](https://docs.ray.io/en/latest/tune/api/search_space.html) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์„ธ์š”: + +```py +>>> def ray_hp_space(trial): +... return { +... "learning_rate": tune.loguniform(1e-6, 1e-4), +... "per_device_train_batch_size": tune.choice([16, 32, 64, 128]), +... } +``` + +wandb์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น [object_parameter](https://docs.wandb.ai/guides/sweeps/configuration) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์„ธ์š”: + +```py +>>> def wandb_hp_space(trial): +... return { +... "method": "random", +... "metric": {"name": "objective", "goal": "minimize"}, +... "parameters": { +... "learning_rate": {"distribution": "uniform", "min": 1e-6, "max": 1e-4}, +... "per_device_train_batch_size": {"values": [16, 32, 64, 128]}, +... }, +... } +``` + +`model_init` ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ [`Trainer`]์— ์ „๋‹ฌํ•˜์„ธ์š”. ์•„๋ž˜๋Š” ๊ทธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. +```py +>>> def model_init(trial): +... return AutoModelForSequenceClassification.from_pretrained( +... model_args.model_name_or_path, +... from_tf=bool(".ckpt" in model_args.model_name_or_path), +... config=config, +... cache_dir=model_args.cache_dir, +... revision=model_args.model_revision, +... token=True if model_args.use_auth_token else None, +... ) +``` + +์•„๋ž˜์™€ ๊ฐ™์ด `model_init` ํ•จ์ˆ˜, ํ›ˆ๋ จ ์ธ์ˆ˜, ํ›ˆ๋ จ ๋ฐ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์…‹, ๊ทธ๋ฆฌ๊ณ  ํ‰๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [`Trainer`]๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + +```py +>>> trainer = Trainer( +... model=None, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... processing_class=tokenizer, +... model_init=model_init, +... data_collator=data_collator, +... ) +``` + +ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰์„ ํ˜ธ์ถœํ•˜๊ณ , ์ตœ์ ์˜ ์‹œํ—˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค์„ธ์š”. ๋ฐฑ์—”๋“œ๋Š” `"optuna"`/`"sigopt"`/`"wandb"`/`"ray"` ์ค‘์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉํ–ฅ์€ `"minimize"` ๋˜๋Š” `"maximize"` ์ค‘ ์„ ํƒํ•˜๋ฉฐ, ๋ชฉํ‘œ๋ฅผ ์ตœ์†Œํ™”ํ•  ๊ฒƒ์ธ์ง€ ์ตœ๋Œ€ํ™”ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +์ž์‹ ๋งŒ์˜ compute_objective ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ธฐ๋ณธ compute_objective๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , f1๊ณผ ๊ฐ™์€ ํ‰๊ฐ€ ์ง€ํ‘œ์˜ ํ•ฉ์ด ๋ชฉํ‘ฏ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. + +```py +>>> best_trial = trainer.hyperparameter_search( +... direction="maximize", +... backend="optuna", +... hp_space=optuna_hp_space, +... n_trials=20, +... compute_objective=compute_objective, +... ) +``` + +## DDP ๋ฏธ์„ธ ์กฐ์ •์„ ์œ„ํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ [[hyperparameter-search-for-ddp-finetune]] +ํ˜„์žฌ, DDP(Distributed Data Parallelism; ๋ถ„์‚ฐ ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ)๋ฅผ ์œ„ํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰์€ optuna์™€ sigopt์—์„œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ƒ์œ„ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ํƒ์ƒ‰ ๊ณผ์ •์„ ์‹œ์ž‘ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/in_translation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/in_translation.md new file mode 100644 index 0000000000000000000000000000000000000000..61ff1426a4522a90b54a33e3b0c91d8a9a1f4d7c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/in_translation.md @@ -0,0 +1,5 @@ + + +# ์—ด์‹ฌํžˆ ๋ฒˆ์—ญ ์ค‘์ž…๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์ด๋”ฐ ๋งŒ๋‚˜์š”! \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/index.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/index.md new file mode 100644 index 0000000000000000000000000000000000000000..266352500af6fd4f5202fe6eb310ce6b3af9f806 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/index.md @@ -0,0 +1,362 @@ + + +# ๐Ÿค— Transformers + +[PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/), [JAX](https://jax.readthedocs.io/en/latest/)๋ฅผ ์œ„ํ•œ ์ตœ์ฒจ๋‹จ ๋จธ์‹ ๋Ÿฌ๋‹ + +๐Ÿค— Transformers๋Š” ์‚ฌ์ „ํ•™์Šต๋œ ์ตœ์ฒจ๋‹จ ๋ชจ๋ธ๋“ค์„ ์‰ฝ๊ฒŒ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ํ›ˆ๋ จ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” API์™€ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์“ฐ๋ฉด ์ปดํ“จํŒ… ๋น„์šฉ๊ณผ ํƒ„์†Œ ๋ฐฐ์ถœ๋Ÿ‰์ด ์ค„๊ณ , ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„๊ณผ ๋ฆฌ์†Œ์Šค๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €ํฌ ๋ชจ๋ธ๋“ค์€ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์˜ ํƒœ์Šคํฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +๐Ÿ“ **์ž์—ฐ์–ด ์ฒ˜๋ฆฌ**: ํ…์ŠคํŠธ ๋ถ„๋ฅ˜, ๊ฐœ์ฒด๋ช… ์ธ์‹, ์งˆ์˜์‘๋‹ต, ์–ธ์–ด ๋ชจ๋ธ๋ง, ์š”์•ฝ, ๋ฒˆ์—ญ, ๊ฐ๊ด€์‹ ์งˆ์˜์‘๋‹ต, ํ…์ŠคํŠธ ์ƒ์„ฑ
+๐Ÿ–ผ๏ธ **์ปดํ“จํ„ฐ ๋น„์ „**: ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜, ๊ฐ์ฒด ํƒ์ง€, ๊ฐ์ฒด ๋ถ„ํ• 
+๐Ÿ—ฃ๏ธ **์˜ค๋””์˜ค**: ์ž๋™์Œ์„ฑ์ธ์‹, ์˜ค๋””์˜ค ๋ถ„๋ฅ˜
+๐Ÿ™ **๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ**: ํ‘œ ์งˆ์˜์‘๋‹ต, ๊ด‘ํ•™ ๋ฌธ์ž ์ธ์‹ (OCR), ์Šค์บ”ํ•œ ๋ฌธ์„œ์—์„œ ์ •๋ณด ์ถ”์ถœ, ๋น„๋””์˜ค ๋ถ„๋ฅ˜, ์‹œ๊ฐ ์งˆ์˜์‘๋‹ต + +๐Ÿค— Transformers๋Š” PyTorch, TensorFlow์™€ JAX ๊ฐ„์˜ ์ƒํ˜ธ์šด์šฉ์„ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์œ ์—ฐํ•˜๊ฒŒ ๋ชจ๋ธ์˜ ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฝ”๋“œ 3์ค„๋งŒ ์จ์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚จ ๋‹ค์Œ, ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ ์ƒ์—์„œ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์šด์˜ ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด ONNX๋‚˜ TorchScript ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ปค๋ฎค๋‹ˆํ‹ฐ์— ์ฐธ์—ฌํ•˜์‹œ๋ ค๋ฉด [Hub](https://huggingface.co/models), [ํฌ๋Ÿผ](https://discuss.huggingface.co/), [๋””์Šค์ฝ”๋“œ](https://discord.com/invite/JfAtkvEtRb)๋ฅผ ๋ฐฉ๋ฌธํ•ด์ฃผ์„ธ์š”! + +## Hugging Face ํŒ€๊ณผ ์ง์ ‘ ๋Œ€ํ™”ํ•˜๊ณ  ์‹ถ์œผ์‹ ๊ฐ€์š”?[[hugging-face-team]] + + + HuggingFace Expert Acceleration Program + + +## ์ฝ˜ํ…์ธ [[contents]] + +์ €ํฌ ๊ธฐ์ˆ ๋ฌธ์„œ๋Š” ํฌ๊ฒŒ 5๊ฐœ ์„น์…˜์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +- **์‹œ์ž‘ํ•˜๊ธฐ**์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ„๋‹จํžˆ ํ›‘์–ด๋ณด๊ณ , ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋›ฐ์–ด๋“ค ์ˆ˜ ์žˆ๊ฒŒ ์„ค์น˜ ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. +- **ํŠœํ† ๋ฆฌ์–ผ**์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ต์ˆ™ํ•ด์งˆ ์ˆ˜ ์žˆ๋„๋ก ์ž์„ธํ•˜๊ณ ๋„ ์‰ฝ๊ฒŒ ๊ธฐ๋ณธ์ ์ธ ๋ถ€๋ถ„์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. +- **How-to ๊ฐ€์ด๋“œ**์—์„œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ์„ ํŒŒ์ธ ํŠœ๋‹ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜, ์ง์ ‘ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์ด ํŠน์ • ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. +- **๊ฐœ๋… ๊ฐ€์ด๋“œ**์—์„œ ๐Ÿค— Transformers์˜ ์„ค๊ณ„ ์ฒ ํ•™๊ณผ ํ•จ๊ป˜ ๋ชจ๋ธ์ด๋‚˜ ํƒœ์Šคํฌ ๋’ค์— ์ˆจ๊ฒจ์ง„ ๊ฐœ๋…๋“ค๊ณผ ์•„์ด๋””์–ด๋ฅผ ํƒ๊ตฌํ•˜๊ณ  ์„ค๋ช…์„ ๋ง๋ถ™์ž…๋‹ˆ๋‹ค. +- **API**์—์„œ ๋ชจ๋“  ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + + - **๋ฉ”์ธ ํด๋ž˜์Šค**์—์„œ configuration, model, tokenizer, pipeline๊ณผ ๊ฐ™์ด ์ œ์ผ ์ค‘์š”ํ•œ ํด๋ž˜์Šค๋“ค์„ ์ž์„ธํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + - **๋ชจ๋ธ**์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์† ๊ตฌํ˜„๋œ ๊ฐ ๋ชจ๋ธ๊ณผ ์—ฐ๊ด€๋œ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋ฅผ ์ž์„ธํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + - **๋‚ด๋ถ€ ์œ ํ‹ธ๋ฆฌํ‹ฐ**์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋ฅผ ์ž์„ธํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +### ์ง€์› ๋ชจ๋ธ[[supported-models]] + + + +1. **[ALBERT](model_doc/albert)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://huggingface.co/papers/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. +1. **[BART](model_doc/bart)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://huggingface.co/papers/1910.13461) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer. +1. **[BARThez](model_doc/barthez)** (from ร‰cole polytechnique) released with the paper [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321) by Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis. +1. **[BARTpho](model_doc/bartpho)** (from VinAI Research) released with the paper [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701) by Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen. +1. **[BEiT](model_doc/beit)** (from Microsoft) released with the paper [BEiT: BERT Pre-Training of Image Transformers](https://huggingface.co/papers/2106.08254) by Hangbo Bao, Li Dong, Furu Wei. +1. **[BERT](model_doc/bert)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova. +1. **[BERT For Sequence Generation](model_doc/bert-generation)** (from Google) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[BERTweet](model_doc/bertweet)** (from VinAI Research) released with the paper [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) by Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen. +1. **[BigBird-Pegasus](model_doc/bigbird_pegasus)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[BigBird-RoBERTa](model_doc/big_bird)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://huggingface.co/papers/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. +1. **[Blenderbot](model_doc/blenderbot)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BlenderbotSmall](model_doc/blenderbot-small)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://huggingface.co/papers/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. +1. **[BLOOM](model_doc/bloom)** (from BigScience workshop) released by the [BigScience Workshop](https://bigscience.huggingface.co/). +1. **[BORT](model_doc/bort)** (from Alexa) released with the paper [Optimal Subarchitecture Extraction For BERT](https://huggingface.co/papers/2010.10499) by Adrian de Wynter and Daniel J. Perry. +1. **[ByT5](model_doc/byt5)** (from Google Research) released with the paper [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://huggingface.co/papers/2105.13626) by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel. +1. **[CamemBERT](model_doc/camembert)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://huggingface.co/papers/1911.03894) by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suรกrez*, Yoann Dupont, Laurent Romary, ร‰ric Villemonte de la Clergerie, Djamรฉ Seddah and Benoรฎt Sagot. +1. **[CANINE](model_doc/canine)** (from Google Research) released with the paper [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://huggingface.co/papers/2103.06874) by Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. +1. **[CLIP](model_doc/clip)** (from OpenAI) released with the paper [Learning Transferable Visual Models From Natural Language Supervision](https://huggingface.co/papers/2103.00020) by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. +1. **[CLIPSeg](model_doc/clipseg)** (from University of Gรถttingen) released with the paper [Image Segmentation Using Text and Image Prompts](https://huggingface.co/papers/2112.10003) by Timo Lรผddecke and Alexander Ecker. +1. **[CodeGen](model_doc/codegen)** (from Salesforce) released with the paper [A Conversational Paradigm for Program Synthesis](https://huggingface.co/papers/2203.13474) by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong. +1. **[Conditional DETR](model_doc/conditional_detr)** (from Microsoft Research Asia) released with the paper [Conditional DETR for Fast Training Convergence](https://huggingface.co/papers/2108.06152) by Depu Meng, Xiaokang Chen, Zejia Fan, Gang Zeng, Houqiang Li, Yuhui Yuan, Lei Sun, Jingdong Wang. +1. **[ConvBERT](model_doc/convbert)** (from YituTech) released with the paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496) by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan. +1. **[ConvNeXT](model_doc/convnext)** (from Facebook AI) released with the paper [A ConvNet for the 2020s](https://huggingface.co/papers/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. +1. **[ConvNeXTV2](model_doc/convnextv2)** (from Facebook AI) released with the paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://huggingface.co/papers/2301.00808) by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. +1. **[CPM](model_doc/cpm)** (from Tsinghua University) released with the paper [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://huggingface.co/papers/2012.00413) by Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun. +1. **[CTRL](model_doc/ctrl)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://huggingface.co/papers/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher. +1. **[CvT](model_doc/cvt)** (from Microsoft) released with the paper [CvT: Introducing Convolutions to Vision Transformers](https://huggingface.co/papers/2103.15808) by Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang. +1. **[Data2Vec](model_doc/data2vec)** (from Facebook) released with the paper [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://huggingface.co/papers/2202.03555) by Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli. +1. **[DeBERTa](model_doc/deberta)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[DeBERTa-v2](model_doc/deberta-v2)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://huggingface.co/papers/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. +1. **[Decision Transformer](model_doc/decision_transformer)** (from Berkeley/Facebook/Google) released with the paper [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://huggingface.co/papers/2106.01345) by Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. +1. **[Deformable DETR](model_doc/deformable_detr)** (from SenseTime Research) released with the paper [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://huggingface.co/papers/2010.04159) by Xizhou Zhu, Weijie Su, Lewei Lu, Bin Li, Xiaogang Wang, Jifeng Dai. +1. **[DeiT](model_doc/deit)** (from Facebook) released with the paper [Training data-efficient image transformers & distillation through attention](https://huggingface.co/papers/2012.12877) by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervรฉ Jรฉgou. +1. **[DETR](model_doc/detr)** (from Facebook) released with the paper [End-to-End Object Detection with Transformers](https://huggingface.co/papers/2005.12872) by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko. +1. **[DialoGPT](model_doc/dialogpt)** (from Microsoft Research) released with the paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://huggingface.co/papers/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan. +1. **[DistilBERT](model_doc/distilbert)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://huggingface.co/papers/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers-research-projects/tree/main/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers-research-projects/tree/main/distillation) and a German version of DistilBERT. +1. **[DiT](model_doc/dit)** (from Microsoft Research) released with the paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://huggingface.co/papers/2203.02378) by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei. +1. **[Donut](model_doc/donut)** (from NAVER), released together with the paper [OCR-free Document Understanding Transformer](https://huggingface.co/papers/2111.15664) by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park. +1. **[DPR](model_doc/dpr)** (from Facebook) released with the paper [Dense Passage Retrieval for Open-Domain Question Answering](https://huggingface.co/papers/2004.04906) by Vladimir Karpukhin, Barlas OฤŸuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih. +1. **[DPT](master/model_doc/dpt)** (from Intel Labs) released with the paper [Vision Transformers for Dense Prediction](https://huggingface.co/papers/2103.13413) by Renรฉ Ranftl, Alexey Bochkovskiy, Vladlen Koltun. +1. **[EfficientNet](model_doc/efficientnet)** (from Google Research) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://huggingface.co/papers/1905.11946) by Mingxing Tan and Quoc V. Le. +1. **[ELECTRA](model_doc/electra)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://huggingface.co/papers/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning. +1. **[EncoderDecoder](model_doc/encoder-decoder)** (from Google Research) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. +1. **[ERNIE](model_doc/ernie)** (from Baidu) released with the paper [ERNIE: Enhanced Representation through Knowledge Integration](https://huggingface.co/papers/1904.09223) by Yu Sun, Shuohuan Wang, Yukun Li, Shikun Feng, Xuyi Chen, Han Zhang, Xin Tian, Danxiang Zhu, Hao Tian, Hua Wu. +1. **[ESM](model_doc/esm)** (from Meta AI) are transformer protein language models. **ESM-1b** was released with the paper [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118) by Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus. **ESM-1v** was released with the paper [Language models enable zero-shot prediction of the effects of mutations on protein function](https://doi.org/10.1101/2021.07.09.450648) by Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rives. **ESM-2 and ESMFold** were released with the paper [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902) by Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives. +1. **[FLAN-T5](model_doc/flan-t5)** (from Google AI) released in the repository [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints) by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei +1. **[FlauBERT](model_doc/flaubert)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://huggingface.co/papers/1912.05372) by Hang Le, Loรฏc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoรฎt Crabbรฉ, Laurent Besacier, Didier Schwab. +1. **[FLAVA](model_doc/flava)** (from Facebook AI) released with the paper [FLAVA: A Foundational Language And Vision Alignment Model](https://huggingface.co/papers/2112.04482) by Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, and Douwe Kiela. +1. **[FNet](model_doc/fnet)** (from Google Research) released with the paper [FNet: Mixing Tokens with Fourier Transforms](https://huggingface.co/papers/2105.03824) by James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. +1. **[Funnel Transformer](model_doc/funnel)** (from CMU/Google Brain) released with the paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://huggingface.co/papers/2006.03236) by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le. +1. **[GLPN](model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://huggingface.co/papers/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim. +1. **[GPT](model_doc/openai-gpt)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://openai.com/research/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever. +1. **[GPT Neo](model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy. +1. **[GPT NeoX](model_doc/gpt_neox)** (from EleutherAI) released with the paper [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://huggingface.co/papers/2204.06745) by Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach +1. **[GPT NeoX Japanese](model_doc/gpt_neox_japanese)** (from ABEJA) released by Shinya Otani, Takayoshi Makabe, Anuj Arora, and Kyo Hattori. +1. **[GPT-2](model_doc/gpt2)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://openai.com/research/better-language-models/) by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever. +1. **[GPT-J](model_doc/gptj)** (from EleutherAI) released in the repository [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) by Ben Wang and Aran Komatsuzaki. +1. **[GPTSAN-japanese](model_doc/gptsan-japanese)** released in the repository [tanreinama/GPTSAN](https://github.com/tanreinama/GPTSAN/blob/main/report/model.md) by Toshiyuki Sakamoto(tanreinama). +1. **[GroupViT](model_doc/groupvit)** (from UCSD, NVIDIA) released with the paper [GroupViT: Semantic Segmentation Emerges from Text Supervision](https://huggingface.co/papers/2202.11094) by Jiarui Xu, Shalini De Mello, Sifei Liu, Wonmin Byeon, Thomas Breuel, Jan Kautz, Xiaolong Wang. +1. **[Hubert](model_doc/hubert)** (from Facebook) released with the paper [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://huggingface.co/papers/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed. +1. **[I-BERT](model_doc/ibert)** (from Berkeley) released with the paper [I-BERT: Integer-only BERT Quantization](https://huggingface.co/papers/2101.01321) by Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer. +1. **[ImageGPT](model_doc/imagegpt)** (from OpenAI) released with the paper [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) by Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. +1. **[Jukebox](model_doc/jukebox)** (from OpenAI) released with the paper [Jukebox: A Generative Model for Music](https://huggingface.co/papers/2005.00341) by Prafulla Dhariwal, Heewoo Jun, Christine Payne, Jong Wook Kim, Alec Radford, Ilya Sutskever. +1. **[LayoutLM](model_doc/layoutlm)** (from Microsoft Research Asia) released with the paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://huggingface.co/papers/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou. +1. **[LayoutLMv2](model_doc/layoutlmv2)** (from Microsoft Research Asia) released with the paper [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://huggingface.co/papers/2012.14740) by Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. +1. **[LayoutLMv3](model_doc/layoutlmv3)** (from Microsoft Research Asia) released with the paper [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://huggingface.co/papers/2204.08387) by Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei. +1. **[LayoutXLM](model_doc/layoutxlm)** (from Microsoft Research Asia) released with the paper [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://huggingface.co/papers/2104.08836) by Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei. +1. **[LED](model_doc/led)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LeViT](model_doc/levit)** (from Meta AI) released with the paper [LeViT: A Vision Transformer in ConvNet's Clothing for Faster Inference](https://huggingface.co/papers/2104.01136) by Ben Graham, Alaaeldin El-Nouby, Hugo Touvron, Pierre Stock, Armand Joulin, Hervรฉ Jรฉgou, Matthijs Douze. +1. **[LiLT](model_doc/lilt)** (from South China University of Technology) released with the paper [LiLT: A Simple yet Effective Language-Independent Layout Transformer for Structured Document Understanding](https://huggingface.co/papers/2202.13669) by Jiapeng Wang, Lianwen Jin, Kai Ding. +1. **[Longformer](model_doc/longformer)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://huggingface.co/papers/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. +1. **[LongT5](model_doc/longt5)** (from Google AI) released with the paper [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://huggingface.co/papers/2112.07916) by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang. +1. **[LUKE](model_doc/luke)** (from Studio Ousia) released with the paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://huggingface.co/papers/2010.01057) by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto. +1. **[LXMERT](model_doc/lxmert)** (from UNC Chapel Hill) released with the paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://huggingface.co/papers/1908.07490) by Hao Tan and Mohit Bansal. +1. **[M-CTC-T](model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://huggingface.co/papers/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert. +1. **[M2M100](model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://huggingface.co/papers/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin. +1. **[MarianMT](model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jรถrg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team. +1. **[MarkupLM](model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://huggingface.co/papers/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei. +1. **[Mask2Former](model_doc/mask2former)** (from FAIR and UIUC) released with the paper [Masked-attention Mask Transformer for Universal Image Segmentation](https://huggingface.co/papers/2112.01527) by Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. +1. **[MaskFormer](model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://huggingface.co/papers/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. +1. **[mBART](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://huggingface.co/papers/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer. +1. **[mBART-50](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://huggingface.co/papers/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan. +1. **[Megatron-BERT](model_doc/megatron-bert)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. +1. **[Megatron-GPT2](model_doc/megatron_gpt2)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://huggingface.co/papers/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. +1. **[mLUKE](model_doc/mluke)** (from Studio Ousia) released with the paper [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://huggingface.co/papers/2110.08151) by Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka. +1. **[MobileBERT](model_doc/mobilebert)** (from CMU/Google Brain) released with the paper [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://huggingface.co/papers/2004.02984) by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou. +1. **[MobileViT](model_doc/mobilevit)** (from Apple) released with the paper [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://huggingface.co/papers/2110.02178) by Sachin Mehta and Mohammad Rastegari. +1. **[MPNet](model_doc/mpnet)** (from Microsoft Research) released with the paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://huggingface.co/papers/2004.09297) by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu. +1. **[MT5](model_doc/mt5)** (from Google AI) released with the paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://huggingface.co/papers/2010.11934) by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel. +1. **[MVP](model_doc/mvp)** (from RUC AI Box) released with the paper [MVP: Multi-task Supervised Pre-training for Natural Language Generation](https://huggingface.co/papers/2206.12131) by Tianyi Tang, Junyi Li, Wayne Xin Zhao and Ji-Rong Wen. +1. **[Nezha](model_doc/nezha)** (from Huawei Noahโ€™s Ark Lab) released with the paper [NEZHA: Neural Contextualized Representation for Chinese Language Understanding](https://huggingface.co/papers/1909.00204) by Junqiu Wei, Xiaozhe Ren, Xiaoguang Li, Wenyong Huang, Yi Liao, Yasheng Wang, Jiashu Lin, Xin Jiang, Xiao Chen and Qun Liu. +1. **[NLLB](model_doc/nllb)** (from Meta) released with the paper [No Language Left Behind: Scaling Human-Centered Machine Translation](https://huggingface.co/papers/2207.04672) by the NLLB team. +1. **[Nystrรถmformer](model_doc/nystromformer)** (from the University of Wisconsin - Madison) released with the paper [Nystrรถmformer: A Nystrรถm-Based Algorithm for Approximating Self-Attention](https://huggingface.co/papers/2102.03902) by Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh. +1. **[OneFormer](model_doc/oneformer)** (from SHI Labs) released with the paper [OneFormer: One Transformer to Rule Universal Image Segmentation](https://huggingface.co/papers/2211.06220) by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. +1. **[OPT](master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://huggingface.co/papers/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al. +1. **[OWL-ViT](model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://huggingface.co/papers/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby. +1. **[Pegasus](model_doc/pegasus)** (from Google) released with the paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://huggingface.co/papers/1912.08777) by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu. +1. **[PEGASUS-X](model_doc/pegasus_x)** (from Google) released with the paper [Investigating Efficiently Extending Transformers for Long Input Summarization](https://huggingface.co/papers/2208.04347) by Jason Phang, Yao Zhao, and Peter J. Liu. +1. **[Perceiver IO](model_doc/perceiver)** (from Deepmind) released with the paper [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://huggingface.co/papers/2107.14795) by Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hรฉnaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, Joรฃo Carreira. +1. **[PhoBERT](model_doc/phobert)** (from VinAI Research) released with the paper [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) by Dat Quoc Nguyen and Anh Tuan Nguyen. +1. **[PLBart](model_doc/plbart)** (from UCLA NLP) released with the paper [Unified Pre-training for Program Understanding and Generation](https://huggingface.co/papers/2103.06333) by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang. +1. **[PoolFormer](model_doc/poolformer)** (from Sea AI Labs) released with the paper [MetaFormer is Actually What You Need for Vision](https://huggingface.co/papers/2111.11418) by Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng. +1. **[ProphetNet](model_doc/prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. +1. **[QDQBert](model_doc/qdqbert)** (from NVIDIA) released with the paper [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://huggingface.co/papers/2004.09602) by Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius Micikevicius. +1. **[RAG](model_doc/rag)** (from Facebook) released with the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://huggingface.co/papers/2005.11401) by Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Kรผttler, Mike Lewis, Wen-tau Yih, Tim Rocktรคschel, Sebastian Riedel, Douwe Kiela. +1. **[REALM](model_doc/realm.html)** (from Google Research) released with the paper [REALM: Retrieval-Augmented Language Model Pre-Training](https://huggingface.co/papers/2002.08909) by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang. +1. **[Reformer](model_doc/reformer)** (from Google Research) released with the paper [Reformer: The Efficient Transformer](https://huggingface.co/papers/2001.04451) by Nikita Kitaev, ลukasz Kaiser, Anselm Levskaya. +1. **[RegNet](model_doc/regnet)** (from META Platforms) released with the paper [Designing Network Design Space](https://huggingface.co/papers/2003.13678) by Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollรกr. +1. **[RemBERT](model_doc/rembert)** (from Google Research) released with the paper [Rethinking embedding coupling in pre-trained language models](https://huggingface.co/papers/2010.12821) by Hyung Won Chung, Thibault Fรฉvry, Henry Tsai, M. Johnson, Sebastian Ruder. +1. **[ResNet](model_doc/resnet)** (from Microsoft Research) released with the paper [Deep Residual Learning for Image Recognition](https://huggingface.co/papers/1512.03385) by Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. +1. **[RoBERTa](model_doc/roberta)** (from Facebook), released together with the paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. +1. **[RoCBert](model_doc/roc_bert)** (from WeChatAI) released with the paper [RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining](https://aclanthology.org/2022.acl-long.65.pdf) by HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou. +1. **[RoFormer](model_doc/roformer)** (from ZhuiyiTechnology), released together with the paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://huggingface.co/papers/2104.09864) by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu. +1. **[SegFormer](model_doc/segformer)** (from NVIDIA) released with the paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://huggingface.co/papers/2105.15203) by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo. +1. **[SEW](model_doc/sew)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SEW-D](model_doc/sew_d)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://huggingface.co/papers/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. +1. **[SpeechToTextTransformer](model_doc/speech_to_text)** (from Facebook), released together with the paper [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://huggingface.co/papers/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. +1. **[SpeechToTextTransformer2](model_doc/speech_to_text_2)** (from Facebook), released together with the paper [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://huggingface.co/papers/2104.06678) by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. +1. **[Splinter](model_doc/splinter)** (from Tel Aviv University), released together with the paper [Few-Shot Question Answering by Pretraining Span Selection](https://huggingface.co/papers/2101.00438) by Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. +1. **[SqueezeBERT](model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://huggingface.co/papers/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer. +1. **[Swin Transformer](model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. +1. **[Swin Transformer V2](model_doc/swinv2)** (from Microsoft) released with the paper [Swin Transformer V2: Scaling Up Capacity and Resolution](https://huggingface.co/papers/2111.09883) by Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo. +1. **[T5](model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://huggingface.co/papers/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. +1. **[T5v1.1](model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. +1. **[Table Transformer](model_doc/table-transformer)** (from Microsoft Research) released with the paper [PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents](https://huggingface.co/papers/2110.00061) by Brandon Smock, Rohith Pesala, Robin Abraham. +1. **[TAPAS](model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://huggingface.co/papers/2004.02349) by Jonathan Herzig, Paweล‚ Krzysztof Nowak, Thomas Mรผller, Francesco Piccinno and Julian Martin Eisenschlos. +1. **[TAPEX](model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://huggingface.co/papers/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou. +1. **[Time Series Transformer](model_doc/time_series_transformer)** (from HuggingFace). +1. **[Trajectory Transformer](model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://huggingface.co/papers/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine +1. **[Transformer-XL](model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://huggingface.co/papers/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. +1. **[TrOCR](model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://huggingface.co/papers/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei. +1. **[UL2](model_doc/ul2)** (from Google Research) released with the paper [Unifying Language Learning Paradigms](https://huggingface.co/papers/2205.05131v1) by Yi Tay, Mostafa Dehghani, Vinh Q. Tran, Xavier Garcia, Dara Bahri, Tal Schuster, Huaixiu Steven Zheng, Neil Houlsby, Donald Metzler +1. **[UniSpeech](model_doc/unispeech)** (from Microsoft Research) released with the paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://huggingface.co/papers/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang. +1. **[UniSpeechSat](model_doc/unispeech-sat)** (from Microsoft Research) released with the paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://huggingface.co/papers/2110.05752) by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu. +1. **[VAN](model_doc/van)** (from Tsinghua University and Nankai University) released with the paper [Visual Attention Network](https://huggingface.co/papers/2202.09741) by Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu. +1. **[VideoMAE](model_doc/videomae)** (from Multimedia Computing Group, Nanjing University) released with the paper [VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training](https://huggingface.co/papers/2203.12602) by Zhan Tong, Yibing Song, Jue Wang, Limin Wang. +1. **[ViLT](model_doc/vilt)** (from NAVER AI Lab/Kakao Enterprise/Kakao Brain) released with the paper [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://huggingface.co/papers/2102.03334) by Wonjae Kim, Bokyung Son, Ildoo Kim. +1. **[Vision Transformer (ViT)](model_doc/vit)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. +1. **[VisualBERT](model_doc/visual_bert)** (from UCLA NLP) released with the paper [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://huggingface.co/papers/1908.03557) by Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang. +1. **[ViTMAE](model_doc/vit_mae)** (from Meta AI) released with the paper [Masked Autoencoders Are Scalable Vision Learners](https://huggingface.co/papers/2111.06377) by Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollรกr, Ross Girshick. +1. **[ViTMSN](model_doc/vit_msn)** (from Meta AI) released with the paper [Masked Siamese Networks for Label-Efficient Learning](https://huggingface.co/papers/2204.07141) by Mahmoud Assran, Mathilde Caron, Ishan Misra, Piotr Bojanowski, Florian Bordes, Pascal Vincent, Armand Joulin, Michael Rabbat, Nicolas Ballas. +1. **[Wav2Vec2](model_doc/wav2vec2)** (from Facebook AI) released with the paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://huggingface.co/papers/2006.11477) by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli. +1. **[Wav2Vec2-Conformer](model_doc/wav2vec2-conformer)** (from Facebook AI) released with the paper [FAIRSEQ S2T: Fast Speech-to-Text Modeling with FAIRSEQ](https://huggingface.co/papers/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Sravya Popuri, Dmytro Okhonko, Juan Pino. +1. **[Wav2Vec2Phoneme](model_doc/wav2vec2_phoneme)** (from Facebook AI) released with the paper [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://huggingface.co/papers/2109.11680) by Qiantong Xu, Alexei Baevski, Michael Auli. +1. **[WavLM](model_doc/wavlm)** (from Microsoft Research) released with the paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://huggingface.co/papers/2110.13900) by Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei. +1. **[Whisper](model_doc/whisper)** (from OpenAI) released with the paper [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf) by Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever. +1. **[X-CLIP](model_doc/xclip)** (from Microsoft Research) released with the paper [Expanding Language-Image Pretrained Models for General Video Recognition](https://huggingface.co/papers/2208.02816) by Bolin Ni, Houwen Peng, Minghao Chen, Songyang Zhang, Gaofeng Meng, Jianlong Fu, Shiming Xiang, Haibin Ling. +1. **[XGLM](model_doc/xglm)** (From Facebook AI) released with the paper [Few-shot Learning with Multilingual Language Models](https://huggingface.co/papers/2112.10668) by Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O'Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li. +1. **[XLM](model_doc/xlm)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://huggingface.co/papers/1901.07291) by Guillaume Lample and Alexis Conneau. +1. **[XLM-ProphetNet](model_doc/xlm-prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://huggingface.co/papers/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. +1. **[XLM-RoBERTa](model_doc/xlm-roberta)** (from Facebook AI), released together with the paper [Unsupervised Cross-lingual Representation Learning at Scale](https://huggingface.co/papers/1911.02116) by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmรกn, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov. +1. **[XLM-RoBERTa-XL](model_doc/xlm-roberta-xl)** (from Facebook AI), released together with the paper [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://huggingface.co/papers/2105.00572) by Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau. +1. **[XLNet](model_doc/xlnet)** (from Google/CMU) released with the paper [โ€‹XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://huggingface.co/papers/1906.08237) by Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. +1. **[XLS-R](model_doc/xls_r)** (from Facebook AI) released with the paper [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://huggingface.co/papers/2111.09296) by Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli. +1. **[XLSR-Wav2Vec2](model_doc/xlsr_wav2vec2)** (from Facebook AI) released with the paper [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://huggingface.co/papers/2006.13979) by Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli. +1. **[YOLOS](model_doc/yolos)** (from Huazhong University of Science & Technology) released with the paper [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://huggingface.co/papers/2106.00666) by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu. +1. **[YOSO](model_doc/yoso)** (from the University of Wisconsin - Madison) released with the paper [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://huggingface.co/papers/2111.09714) by Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh. + + +### ์ง€์› ํ”„๋ ˆ์ž„์›Œํฌ[[supported-framework]] + +์•„๋ž˜ ํ‘œ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์† ๊ฐ ๋ชจ๋ธ์˜ ์ง€์› ํ˜„ํ™ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ† ํฐํ™”๋ฅผ ํŒŒ์ด์ฌ (๋ณ„์นญ "slow") ๋˜๋Š” ๐Ÿค— Tokenizers (๋ณ„์นญ "fast") ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ•˜๋Š”์ง€; (Flax๋ฅผ ํ†ตํ•œ) Jax, PyTorch, TensorFlow ์ค‘ ์–ด๋–ค ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + + + +| Model | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | +|:---------------------------:|:--------------:|:--------------:|:---------------:|:------------------:|:------------:| +| ALBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| BART | โœ… | โœ… | โœ… | โœ… | โœ… | +| BEiT | โŒ | โŒ | โœ… | โŒ | โœ… | +| BERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| Bert Generation | โœ… | โŒ | โœ… | โŒ | โŒ | +| BigBird | โœ… | โœ… | โœ… | โŒ | โœ… | +| BigBird-Pegasus | โŒ | โŒ | โœ… | โŒ | โŒ | +| Blenderbot | โœ… | โœ… | โœ… | โœ… | โœ… | +| BlenderbotSmall | โœ… | โœ… | โœ… | โœ… | โœ… | +| BLOOM | โŒ | โœ… | โœ… | โŒ | โŒ | +| CamemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| CANINE | โœ… | โŒ | โœ… | โŒ | โŒ | +| CLIP | โœ… | โœ… | โœ… | โœ… | โœ… | +| CLIPSeg | โŒ | โŒ | โœ… | โŒ | โŒ | +| CodeGen | โœ… | โœ… | โœ… | โŒ | โŒ | +| Conditional DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| ConvBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ConvNeXT | โŒ | โŒ | โœ… | โœ… | โŒ | +| CTRL | โœ… | โŒ | โœ… | โœ… | โŒ | +| CvT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Data2VecAudio | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecText | โŒ | โŒ | โœ… | โŒ | โŒ | +| Data2VecVision | โŒ | โŒ | โœ… | โœ… | โŒ | +| DeBERTa | โœ… | โœ… | โœ… | โœ… | โŒ | +| DeBERTa-v2 | โœ… | โœ… | โœ… | โœ… | โŒ | +| Decision Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Deformable DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DeiT | โŒ | โŒ | โœ… | โœ… | โŒ | +| DETR | โŒ | โŒ | โœ… | โŒ | โŒ | +| DistilBERT | โœ… | โœ… | โœ… | โœ… | โœ… | +| DonutSwin | โŒ | โŒ | โœ… | โŒ | โŒ | +| DPR | โœ… | โœ… | โœ… | โœ… | โŒ | +| DPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ELECTRA | โœ… | โœ… | โœ… | โœ… | โœ… | +| Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| ERNIE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ESM | โœ… | โŒ | โœ… | โœ… | โŒ | +| FairSeq Machine-Translation | โœ… | โŒ | โœ… | โŒ | โŒ | +| FlauBERT | โœ… | โŒ | โœ… | โœ… | โŒ | +| FLAVA | โŒ | โŒ | โœ… | โŒ | โŒ | +| FNet | โœ… | โœ… | โœ… | โŒ | โŒ | +| Funnel Transformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| GLPN | โŒ | โŒ | โœ… | โŒ | โŒ | +| GPT Neo | โŒ | โŒ | โœ… | โŒ | โœ… | +| GPT NeoX | โŒ | โœ… | โœ… | โŒ | โŒ | +| GPT NeoX Japanese | โœ… | โŒ | โœ… | โŒ | โŒ | +| GPT-J | โŒ | โŒ | โœ… | โœ… | โœ… | +| GroupViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| Hubert | โŒ | โŒ | โœ… | โœ… | โŒ | +| I-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ImageGPT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Jukebox | โœ… | โŒ | โœ… | โŒ | โŒ | +| LayoutLM | โœ… | โœ… | โœ… | โœ… | โŒ | +| LayoutLMv2 | โœ… | โœ… | โœ… | โŒ | โŒ | +| LayoutLMv3 | โœ… | โœ… | โœ… | โœ… | โŒ | +| LED | โœ… | โœ… | โœ… | โœ… | โŒ | +| LeViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| LiLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Longformer | โœ… | โœ… | โœ… | โœ… | โŒ | +| LongT5 | โŒ | โŒ | โœ… | โŒ | โœ… | +| LUKE | โœ… | โŒ | โœ… | โŒ | โŒ | +| LXMERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| M-CTC-T | โŒ | โŒ | โœ… | โŒ | โŒ | +| M2M100 | โœ… | โŒ | โœ… | โŒ | โŒ | +| Marian | โœ… | โŒ | โœ… | โœ… | โœ… | +| MarkupLM | โœ… | โœ… | โœ… | โŒ | โŒ | +| MaskFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| mBART | โœ… | โœ… | โœ… | โœ… | โœ… | +| Megatron-BERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| MobileBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| MobileViT | โŒ | โŒ | โœ… | โœ… | โŒ | +| MPNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| MT5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| MVP | โœ… | โœ… | โœ… | โŒ | โŒ | +| Nezha | โŒ | โŒ | โœ… | โŒ | โŒ | +| Nystrรถmformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| OpenAI GPT | โœ… | โœ… | โœ… | โœ… | โŒ | +| OpenAI GPT-2 | โœ… | โœ… | โœ… | โœ… | โœ… | +| OPT | โŒ | โŒ | โœ… | โœ… | โœ… | +| OWL-ViT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Pegasus | โœ… | โœ… | โœ… | โœ… | โœ… | +| PEGASUS-X | โŒ | โŒ | โœ… | โŒ | โŒ | +| Perceiver | โœ… | โŒ | โœ… | โŒ | โŒ | +| PLBart | โœ… | โŒ | โœ… | โŒ | โŒ | +| PoolFormer | โŒ | โŒ | โœ… | โŒ | โŒ | +| ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| QDQBert | โŒ | โŒ | โœ… | โŒ | โŒ | +| RAG | โœ… | โŒ | โœ… | โœ… | โŒ | +| REALM | โœ… | โœ… | โœ… | โŒ | โŒ | +| Reformer | โœ… | โœ… | โœ… | โŒ | โŒ | +| RegNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RemBERT | โœ… | โœ… | โœ… | โœ… | โŒ | +| ResNet | โŒ | โŒ | โœ… | โœ… | โœ… | +| RetriBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| RoCBert | โœ… | โŒ | โœ… | โŒ | โŒ | +| RoFormer | โœ… | โœ… | โœ… | โœ… | โœ… | +| SegFormer | โŒ | โŒ | โœ… | โœ… | โŒ | +| SEW | โŒ | โŒ | โœ… | โŒ | โŒ | +| SEW-D | โŒ | โŒ | โœ… | โŒ | โŒ | +| Speech Encoder decoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| Speech2Text | โœ… | โŒ | โœ… | โœ… | โŒ | +| Speech2Text2 | โœ… | โŒ | โŒ | โŒ | โŒ | +| Splinter | โœ… | โœ… | โœ… | โŒ | โŒ | +| SqueezeBERT | โœ… | โœ… | โœ… | โŒ | โŒ | +| Swin Transformer | โŒ | โŒ | โœ… | โœ… | โŒ | +| Swin Transformer V2 | โŒ | โŒ | โœ… | โŒ | โŒ | +| T5 | โœ… | โœ… | โœ… | โœ… | โœ… | +| Table Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| TAPAS | โœ… | โŒ | โœ… | โœ… | โŒ | +| Time Series Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Trajectory Transformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| Transformer-XL | โœ… | โŒ | โœ… | โœ… | โŒ | +| TrOCR | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeech | โŒ | โŒ | โœ… | โŒ | โŒ | +| UniSpeechSat | โŒ | โŒ | โœ… | โŒ | โŒ | +| VAN | โŒ | โŒ | โœ… | โŒ | โŒ | +| VideoMAE | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViLT | โŒ | โŒ | โœ… | โŒ | โŒ | +| Vision Encoder decoder | โŒ | โŒ | โœ… | โœ… | โœ… | +| VisionTextDualEncoder | โŒ | โŒ | โœ… | โŒ | โœ… | +| VisualBERT | โŒ | โŒ | โœ… | โŒ | โŒ | +| ViT | โŒ | โŒ | โœ… | โœ… | โœ… | +| ViTMAE | โŒ | โŒ | โœ… | โœ… | โŒ | +| ViTMSN | โŒ | โŒ | โœ… | โŒ | โŒ | +| Wav2Vec2 | โœ… | โŒ | โœ… | โœ… | โœ… | +| Wav2Vec2-Conformer | โŒ | โŒ | โœ… | โŒ | โŒ | +| WavLM | โŒ | โŒ | โœ… | โŒ | โŒ | +| Whisper | โœ… | โŒ | โœ… | โœ… | โŒ | +| X-CLIP | โŒ | โŒ | โœ… | โŒ | โŒ | +| XGLM | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM | โœ… | โŒ | โœ… | โœ… | โŒ | +| XLM-ProphetNet | โœ… | โŒ | โœ… | โŒ | โŒ | +| XLM-RoBERTa | โœ… | โœ… | โœ… | โœ… | โœ… | +| XLM-RoBERTa-XL | โŒ | โŒ | โœ… | โŒ | โŒ | +| XLNet | โœ… | โœ… | โœ… | โœ… | โŒ | +| YOLOS | โŒ | โŒ | โœ… | โŒ | โŒ | +| YOSO | โŒ | โŒ | โœ… | โŒ | โŒ | + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/installation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/installation.md new file mode 100644 index 0000000000000000000000000000000000000000..a744db40d291236a10b6b93c06d99c0b31a0a33d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/installation.md @@ -0,0 +1,245 @@ + + +# ์„ค์น˜๋ฐฉ๋ฒ•[[installation]] + +๐Ÿค— Transformers๋ฅผ ์‚ฌ์šฉ ์ค‘์ธ ๋”ฅ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋งž์ถฐ ์„ค์น˜ํ•˜๊ณ , ์บ์‹œ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ์„ ํƒ์ ์œผ๋กœ ์˜คํ”„๋ผ์ธ์—์„œ๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๐Ÿค— Transformers๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ฒ ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers๋Š” Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+ ๋ฐ Flax์—์„œ ํ…Œ์ŠคํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋งํฌ๋œ ์ €๋งˆ๋‹ค์˜ ๊ณต์‹ ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. + +* [PyTorch](https://pytorch.org/get-started/locally/) ์„ค์น˜ํ•˜๊ธฐ +* [TensorFlow 2.0](https://www.tensorflow.org/install/pip) ์„ค์น˜ํ•˜๊ธฐ +* [Flax](https://flax.readthedocs.io/en/latest/) ์„ค์น˜ํ•˜๊ธฐ + +## pip์œผ๋กœ ์„ค์น˜ํ•˜๊ธฐ[[install-with-pip]] + +๐Ÿค— Transformers๋ฅผ [๊ฐ€์ƒ ํ™˜๊ฒฝ](https://docs.python.org/3/library/venv.html)์— ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. Python ๊ฐ€์ƒ ํ™˜๊ฒฝ์— ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด, ์ด [๊ฐ€์ด๋“œ](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋“ค์„ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์˜์กด์„ฑ ๊ฐ„์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋จผ์ € ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค. + +```bash +python -m venv .env +``` + +๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ํ™œ์„ฑํ™”ํ•ด์ฃผ์„ธ์š”. Linux๋‚˜ MacOS์˜ ๊ฒฝ์šฐ: + +```bash +source .env/bin/activate +``` +Windows์˜ ๊ฒฝ์šฐ: + +```bash +.env/Scripts/activate +``` + +์ด์ œ ๐Ÿค— Transformers๋ฅผ ์„ค์น˜ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. + +```bash +pip install transformers +``` + +CPU๋งŒ ์จ๋„ ๋œ๋‹ค๋ฉด, ๐Ÿค— Transformers์™€ ๋”ฅ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹จ 1์ค„๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๐Ÿค— Transformers์™€ PyTorch์˜ ๊ฒฝ์šฐ: + +```bash +pip install transformers[torch] +``` + +๐Ÿค— Transformers์™€ TensorFlow 2.0์˜ ๊ฒฝ์šฐ: + +```bash +pip install transformers[tf-cpu] +``` + +๐Ÿค— Transformers์™€ Flax์˜ ๊ฒฝ์šฐ: + +```bash +pip install transformers[flax] +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ ๐Ÿค— Transformers๊ฐ€ ์ œ๋Œ€๋กœ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" +``` + +๋ผ๋ฒจ๊ณผ ์ ์ˆ˜๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด ์ž˜ ์„ค์น˜๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```bash +[{'label': 'POSITIVE', 'score': 0.9998704791069031}] +``` + +## ์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๊ธฐ[[install-from-source]] + +๐Ÿค— Transformers๋ฅผ ์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +pip install git+https://github.com/huggingface/transformers +``` + +์œ„ ๋ช…๋ น์€ ์ตœ์‹ ์ด์ง€๋งŒ (์•ˆ์ •์ ์ธ) `stable` ๋ฒ„์ „์ด ์•„๋‹Œ ์‹คํ—˜์„ฑ์ด ์ง™์€ `main` ๋ฒ„์ „์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. `main` ๋ฒ„์ „์€ ๊ฐœ๋ฐœ ํ˜„ํ™ฉ๊ณผ ๋ฐœ๋งž์ถ”๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋กœ ๋งˆ์ง€๋ง‰ ๊ณต์‹ ๋ฆด๋ฆฌ์Šค ์ดํ›„ ๋ฐœ๊ฒฌ๋œ ๋ฒ„๊ทธ๊ฐ€ ํŒจ์น˜๋˜์—ˆ์ง€๋งŒ, ์ƒˆ ๋ฆด๋ฆฌ์Šค๋กœ ์•„์ง ๋กค์•„์›ƒ๋˜์ง€๋Š” ์•Š์€ ๊ฒฝ์šฐ๋ฅผ ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๊ฟ” ๋งํ•˜๋ฉด `main` ๋ฒ„์ „์ด ์•ˆ์ •์„ฑ๊ณผ๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ €ํฌ๋Š” `main` ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ์—†๋„๋ก ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ œ๋Š” ๋Œ€๊ฐœ ๋ช‡ ์‹œ๊ฐ„์ด๋‚˜ ํ•˜๋ฃจ ์•ˆ์— ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด [์ด์Šˆ](https://github.com/huggingface/transformers/issues)๋ฅผ ์—ด์–ด์ฃผ์‹œ๋ฉด ๋” ๋นจ๋ฆฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ „๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๐Ÿค— Transformers๊ฐ€ ์ œ๋Œ€๋กœ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. + +```bash +python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" +``` + +## ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ์„ค์น˜[[editable-install]] + +์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +* `main` ๋ฒ„์ „์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด +* ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๊ณ  ์‹ถ์–ด์„œ ์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด + +๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ๋ณต์ œํ•˜๊ณ  ๐Ÿค— Transformers๋ฅผ ์„ค์น˜ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. + +```bash +git clone https://github.com/huggingface/transformers.git +cd transformers +pip install -e . +``` + +์œ„ ๋ช…๋ น์€ ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ๋ณต์ œํ•œ ์œ„์น˜์˜ ํด๋”์™€ Python ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฒฝ๋กœ๋ฅผ ์—ฐ๊ฒฐ์‹œํ‚ต๋‹ˆ๋‹ค. Python์ด ์ผ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ ์™ธ์— ๋ณต์ œํ•œ ํด๋” ๋‚ด๋ถ€๋ฅผ ํ™•์ธํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Python ํŒจํ‚ค์ง€๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ `~/anaconda3/envs/main/lib/python3.7/site-packages/`์— ์„ค์น˜๋˜์–ด ์žˆ๋Š”๋ฐ, ๋ช…๋ น์„ ๋ฐ›์€ Python์ด ์ด์ œ ๋ณต์ œํ•œ ํด๋”์ธ `~/transformers/`๋„ ๊ฒ€์ƒ‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + + + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•˜๋ ค๋ฉด `transformers` ํด๋”๋ฅผ ๊ผญ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +๋ณต์ œ๋ณธ์€ ์ตœ์‹  ๋ฒ„์ „์˜ ๐Ÿค— Transformers๋กœ ์‰ฝ๊ฒŒ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +cd ~/transformers/ +git pull +``` + +Python ํ™˜๊ฒฝ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ์—…๋ฐ์ดํŠธ๋œ ๐Ÿค— Transformers์˜ `main` ๋ฒ„์ „์„ ์ฐพ์•„๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +## conda๋กœ ์„ค์น˜ํ•˜๊ธฐ[[install-with-conda]] + +`conda-forge` conda ์ฑ„๋„์—์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +conda install conda-forge::transformers +``` + +## ์บ์‹œ ๊ตฌ์„ฑํ•˜๊ธฐ[[cache-setup]] + +์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์€ ๋‹ค์šด๋กœ๋“œ๋œ ํ›„ ๋กœ์ปฌ ๊ฒฝ๋กœ `~/.cache/huggingface/hub`์— ์บ์‹œ๋ฉ๋‹ˆ๋‹ค. ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `TRANSFORMERS_CACHE`์˜ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ์ž…๋‹ˆ๋‹ค. Windows์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” `C:\Users\username\.cache\huggingface\hub`์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ (์šฐ์„  ์ˆœ์œ„) ์ˆœ์„œ๋Œ€๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค๋ฅธ ์บ์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +1. ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ (๊ธฐ๋ณธ): `HF_HUB_CACHE` ๋˜๋Š” `TRANSFORMERS_CACHE` +2. ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜: `HF_HOME` +3. ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜: `XDG_CACHE_HOME` + `/huggingface` + + + +๊ณผ๊ฑฐ ๐Ÿค— Transformers์—์„œ ์“ฐ์˜€๋˜ ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `PYTORCH_TRANSFORMERS_CACHE` ๋˜๋Š” `PYTORCH_PRETRAINED_BERT_CACHE`์ด ์„ค์ •๋˜์žˆ๋‹ค๋ฉด, ์…ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `TRANSFORMERS_CACHE`์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ํ•œ ์šฐ์„  ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + + +## ์˜คํ”„๋ผ์ธ ๋ชจ๋“œ[[offline-mode]] + +๐Ÿค— Transformers๋ฅผ ๋กœ์ปฌ ํŒŒ์ผ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ด์„œ ๋ฐฉํ™”๋ฒฝ ๋˜๋Š” ์˜คํ”„๋ผ์ธ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด `HF_HUB_OFFLINE=1` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. + + + +`HF_DATASETS_OFFLINE=1` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ์˜คํ”„๋ผ์ธ ํ›ˆ๋ จ ๊ณผ์ •์— [๐Ÿค— Datasets](https://huggingface.co/docs/datasets/)์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +์˜ˆ๋ฅผ ๋“ค์–ด ์™ธ๋ถ€ ๊ธฐ๊ธฐ ์‚ฌ์ด์— ๋ฐฉํ™”๋ฒฝ์„ ๋‘” ์ผ๋ฐ˜ ๋„คํŠธ์›Œํฌ์—์„œ ํ‰์†Œ์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +์˜คํ”„๋ผ์ธ ๊ธฐ๊ธฐ์—์„œ ๋™์ผํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +HF_DATASETS_OFFLINE=1 HF_HUB_OFFLINE=1 \ +python examples/pytorch/translation/run_translation.py --model_name_or_path google-t5/t5-small --dataset_name wmt16 --dataset_config ro-en ... +``` + +์ด์ œ ์Šคํฌ๋ฆฝํŠธ๋Š” ๋กœ์ปฌ ํŒŒ์ผ์— ํ•œํ•ด์„œ๋งŒ ๊ฒ€์ƒ‰ํ•  ๊ฒƒ์ด๋ฏ€๋กœ, ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ค‘๋‹จ๋˜๊ฑฐ๋‚˜ ์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋  ๋•Œ๊นŒ์ง€ ๋ฉˆ์ถฐ์žˆ์ง€ ์•Š๊ณ  ์ž˜ ์‹คํ–‰๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +### ์˜คํ”„๋ผ์ธ์šฉ ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋งŒ๋“ค์–ด๋‘๊ธฐ[[fetch-models-and-tokenizers-to-use-offline]] + +Another option for using ๐Ÿค— Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this: +๐Ÿค— Transformers๋ฅผ ์˜คํ”„๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ํŒŒ์ผ์„ ๋ฏธ๋ฆฌ ๋‹ค์šด๋กœ๋“œํ•œ ๋‹ค์Œ, ์˜คํ”„๋ผ์ธ์ผ ๋•Œ ์‚ฌ์šฉํ•  ๋กœ์ปฌ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด๋‘๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 3๊ฐ€์ง€ ์ค‘ ํŽธํ•œ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฅด์„ธ์š”. + +* [Model Hub](https://huggingface.co/models)์˜ UI๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ ค๋ฉด โ†“ ์•„์ด์ฝ˜์„ ํด๋ฆญํ•˜์„ธ์š”. + + ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) + +* [`PreTrainedModel.from_pretrained`]์™€ [`PreTrainedModel.save_pretrained`] ์›Œํฌํ”Œ๋กœ๋ฅผ ํ™œ์šฉํ•˜์„ธ์š”. + + 1. ๋ฏธ๋ฆฌ [`PreTrainedModel.from_pretrained`]๋กœ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ด๋‘์„ธ์š”. + + ```py + >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM + + >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") + >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B") + ``` + + 2. [`PreTrainedModel.save_pretrained`]๋กœ ์ง€์ •๋œ ๊ฒฝ๋กœ์— ํŒŒ์ผ์„ ์ €์žฅํ•ด๋‘์„ธ์š”. + + ```py + >>> tokenizer.save_pretrained("./your/path/bigscience_t0") + >>> model.save_pretrained("./your/path/bigscience_t0") + ``` + + 3. ์ด์ œ ์˜คํ”„๋ผ์ธ์ผ ๋•Œ [`PreTrainedModel.from_pretrained`]๋กœ ์ €์žฅํ•ด๋’€๋˜ ํŒŒ์ผ์„ ์ง€์ •๋œ ๊ฒฝ๋กœ์—์„œ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ค์„ธ์š”. + + ```py + >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") + >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0") + ``` + +* [huggingface_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜์„ธ์š”. + + 1. ๊ฐ€์ƒํ™˜๊ฒฝ์— `huggingface_hub` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜์„ธ์š”. + + ```bash + python -m pip install huggingface_hub + ``` + + 2. [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) ํ•จ์ˆ˜๋กœ ํŒŒ์ผ์„ ํŠน์ • ์œ„์น˜์— ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ๋ช…๋ น์€ [T0](https://huggingface.co/bigscience/T0_3B) ๋ชจ๋ธ์˜ `config.json` ํŒŒ์ผ์„ ์ง€์ •๋œ ๊ฒฝ๋กœ์— ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. + + ```py + >>> from huggingface_hub import hf_hub_download + + >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0") + ``` + +ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋กœ์ปฌ์— ์บ์‹œ ํ•ด๋†“๊ณ  ๋‚˜๋ฉด, ๋‚˜์ค‘์— ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋กœ์ปฌ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด๋‘์„ธ์š”. + +```py +>>> from transformers import AutoConfig + +>>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json") +``` + + + +Hub์— ์ €์žฅ๋œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด [Hub์—์„œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ](https://huggingface.co/docs/hub/how-to-downstream) ์„น์…˜์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/audio_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/audio_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..811f7c0866bd509e0b22c53111330cdacd8c4320 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/audio_utils.md @@ -0,0 +1,39 @@ + + +# `FeatureExtractors`๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[utilities-for-featureextractors]] + +์ด ํŽ˜์ด์ง€๋Š” ์˜ค๋””์˜ค [`FeatureExtractor`]๊ฐ€ *๋‹จ์‹œ๊ฐ„ ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜(Short Time Fourier Transform)* ๋˜๋Š” *๋กœ๊ทธ ๋ฉœ ์ŠคํŽ™ํŠธ๋กœ๊ทธ๋žจ(log mel spectrogram)*๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์›์‹œ ์˜ค๋””์˜ค์—์„œ ํŠน์ˆ˜ํ•œ ํŠน์„ฑ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด ์˜ค๋””์˜ค ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ์—๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ์˜ค๋””์˜ค ๋ณ€ํ™˜ [[transformers.audio_utils.hertz_to_mel]] + +[[autodoc]] audio_utils.hertz_to_mel + +[[autodoc]] audio_utils.mel_to_hertz + +[[autodoc]] audio_utils.mel_filter_bank + +[[autodoc]] audio_utils.optimal_fft_length + +[[autodoc]] audio_utils.window_function + +[[autodoc]] audio_utils.spectrogram + +[[autodoc]] audio_utils.power_to_db + +[[autodoc]] audio_utils.amplitude_to_db diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/file_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/file_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..7effad44baa170fba6b4bc8b954f57c2fd208323 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/file_utils.md @@ -0,0 +1,50 @@ + + +# ์ผ๋ฐ˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ (General Utilities) [[general-utilities]] + +์ด ํŽ˜์ด์ง€๋Š” `utils.py` ํŒŒ์ผ์— ์žˆ๋Š” Transformers์˜ ์ผ๋ฐ˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + + +## Enums ๋ฐ namedtuples [[transformers.utils.ExplicitEnum]] + +[[autodoc]] utils.ExplicitEnum + +[[autodoc]] utils.PaddingStrategy + +[[autodoc]] utils.TensorType + +## ํŠน์ˆ˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Special Decorators) [[transformers.add_start_docstrings]] + +[[autodoc]] utils.add_start_docstrings + +[[autodoc]] utils.add_start_docstrings_to_model_forward + +[[autodoc]] utils.add_end_docstrings + +[[autodoc]] utils.add_code_sample_docstrings + +[[autodoc]] utils.replace_return_docstrings + +## ํŠน์ˆ˜ ์†์„ฑ (Special Properties) [[transformers.utils.cached_property]] + +[[autodoc]] utils.cached_property + +## ๊ธฐํƒ€ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[transformers.utils._LazyModule]] + +[[autodoc]] utils._LazyModule diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/generation_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/generation_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..bf567920610cec6ed7d0d23178773c84f7860a32 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/generation_utils.md @@ -0,0 +1,411 @@ + + +# ์ƒ์„ฑ์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[utilities-for-generation]] + +์ด ํŽ˜์ด์ง€๋Š” [`~generation.GenerationMixin.generate`]์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +## ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๊ธฐ (Generate Outputs) [[generate-outputs]] + +[`~generation.GenerationMixin.generate`]์˜ ์ถœ๋ ฅ์€ [`~utils.ModelOutput`]์˜ ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ์ด ์ถœ๋ ฅ์€ [`~generation.GenerationMixin.generate`]์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ชจ๋“  ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด์ด๋ฉฐ, ํŠœํ”Œ ๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: + +```python +from transformers import GPT2Tokenizer, GPT2LMHeadModel + +tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2") +model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2") + +inputs = tokenizer("Hello, my dog is cute and ", return_tensors="pt") +generation_output = model.generate(**inputs, return_dict_in_generate=True, output_scores=True) +``` + +`generation_output` ๊ฐ์ฒด๋Š” [`~generation.GenerateDecoderOnlyOutput`]์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด, ์ด ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: + +- `sequences`: ์ƒ์„ฑ๋œ ํ† ํฐ ์‹œํ€€์Šค +- `scores` (์˜ต์…˜): ๊ฐ ์ƒ์„ฑ ๋‹จ๊ณ„์—์„œ ์–ธ์–ด ๋ชจ๋ธ๋ง ํ—ค๋“œ์˜ ์˜ˆ์ธก ์ ์ˆ˜ +- `hidden_states` (์˜ต์…˜): ๊ฐ ์ƒ์„ฑ ๋‹จ๊ณ„์—์„œ ๋ชจ๋ธ์˜ ์€๋‹‰ ์ƒํƒœ +- `attentions` (์˜ต์…˜): ๊ฐ ์ƒ์„ฑ ๋‹จ๊ณ„์—์„œ ๋ชจ๋ธ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜ + +`output_scores=True`๋ฅผ ์ „๋‹ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— `scores`๋Š” ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ, `output_hidden_states=True` ๋˜๋Š” `output_attentions=True`๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ `hidden_states`์™€ `attentions`๋Š” ํฌํ•จ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. + +๊ฐ ์†์„ฑ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ชจ๋ธ์ด ํ•ด๋‹น ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด `None`์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `generation_output.scores`๋Š” ์–ธ์–ด ๋ชจ๋ธ๋ง ํ—ค๋“œ์—์„œ ์ƒ์„ฑ๋œ ๋ชจ๋“  ์˜ˆ์ธก ์ ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, `generation_output.attentions`๋Š” `None`์ž…๋‹ˆ๋‹ค. + +`generation_output` ๊ฐ์ฒด๋ฅผ ํŠœํ”Œ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, `None` ๊ฐ’์ด ์•„๋‹Œ ์†์„ฑ๋งŒ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `loss`์™€ `logits`๋ผ๋Š” ๋‘ ์š”์†Œ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ: + +```python +generation_output[:2] +``` + +์œ„ ์ฝ”๋“œ๋Š” `(generation_output.sequences, generation_output.scores)` ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +`generation_output` ๊ฐ์ฒด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, `None` ๊ฐ’์ด ์•„๋‹Œ ์†์„ฑ๋งŒ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `sequences`์™€ `scores`๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ‚ค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ์ถœ๋ ฅ ์œ ํ˜•์„ ๋ฌธ์„œํ™”ํ•ฉ๋‹ˆ๋‹ค. + + +### PyTorch [[transformers.generation.GenerateDecoderOnlyOutput]] + +[[autodoc]] generation.GenerateDecoderOnlyOutput + +[[autodoc]] generation.GenerateEncoderDecoderOutput + +[[autodoc]] generation.GenerateBeamDecoderOnlyOutput + +[[autodoc]] generation.GenerateBeamEncoderDecoderOutput + +### TensorFlow [[transformers.generation.TFGreedySearchEncoderDecoderOutput]] + +[[autodoc]] generation.TFGreedySearchEncoderDecoderOutput + +[[autodoc]] generation.TFGreedySearchDecoderOnlyOutput + +[[autodoc]] generation.TFSampleEncoderDecoderOutput + +[[autodoc]] generation.TFSampleDecoderOnlyOutput + +[[autodoc]] generation.TFBeamSearchEncoderDecoderOutput + +[[autodoc]] generation.TFBeamSearchDecoderOnlyOutput + +[[autodoc]] generation.TFBeamSampleEncoderDecoderOutput + +[[autodoc]] generation.TFBeamSampleDecoderOnlyOutput + +[[autodoc]] generation.TFContrastiveSearchEncoderDecoderOutput + +[[autodoc]] generation.TFContrastiveSearchDecoderOnlyOutput + +### FLAX [[transformers.generation.FlaxSampleOutput]] + +[[autodoc]] generation.FlaxSampleOutput + +[[autodoc]] generation.FlaxGreedySearchOutput + +[[autodoc]] generation.FlaxBeamSearchOutput + +## LogitsProcessor [[logitsprocessor]] + +[`LogitsProcessor`]๋Š” ์ƒ์„ฑ ์ค‘ ์–ธ์–ด ๋ชจ๋ธ ํ—ค๋“œ์˜ ์˜ˆ์ธก ์ ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +### PyTorch [[transformers.AlternatingCodebooksLogitsProcessor]] + +[[autodoc]] AlternatingCodebooksLogitsProcessor + - __call__ + +[[autodoc]] ClassifierFreeGuidanceLogitsProcessor + - __call__ + +[[autodoc]] EncoderNoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] EncoderRepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] EpsilonLogitsWarper + - __call__ + +[[autodoc]] EtaLogitsWarper + - __call__ + +[[autodoc]] ExponentialDecayLengthPenalty + - __call__ + +[[autodoc]] ForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] ForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] HammingDiversityLogitsProcessor + - __call__ + +[[autodoc]] InfNanRemoveLogitsProcessor + - __call__ + +[[autodoc]] LogitNormalization + - __call__ + +[[autodoc]] LogitsProcessor + - __call__ + +[[autodoc]] LogitsProcessorList + - __call__ + +[[autodoc]] MinLengthLogitsProcessor + - __call__ + +[[autodoc]] MinNewTokensLengthLogitsProcessor + - __call__ + +[[autodoc]] MinPLogitsWarper + - __call__ + +[[autodoc]] NoBadWordsLogitsProcessor + - __call__ + +[[autodoc]] NoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] PrefixConstrainedLogitsProcessor + - __call__ + +[[autodoc]] RepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] SequenceBiasLogitsProcessor + - __call__ + +[[autodoc]] SuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] SuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] TemperatureLogitsWarper + - __call__ + +[[autodoc]] TopKLogitsWarper + - __call__ + +[[autodoc]] TopPLogitsWarper + - __call__ + +[[autodoc]] TypicalLogitsWarper + - __call__ + +[[autodoc]] UnbatchedClassifierFreeGuidanceLogitsProcessor + - __call__ + +[[autodoc]] WhisperTimeStampLogitsProcessor + - __call__ + +[[autodoc]] WatermarkLogitsProcessor + - __call__ + + +### TensorFlow [[transformers.TFForcedBOSTokenLogitsProcessor]] + +[[autodoc]] TFForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] TFForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] TFForceTokensLogitsProcessor + - __call__ + +[[autodoc]] TFLogitsProcessor + - __call__ + +[[autodoc]] TFLogitsProcessorList + - __call__ + +[[autodoc]] TFLogitsWarper + - __call__ + +[[autodoc]] TFMinLengthLogitsProcessor + - __call__ + +[[autodoc]] TFNoBadWordsLogitsProcessor + - __call__ + +[[autodoc]] TFNoRepeatNGramLogitsProcessor + - __call__ + +[[autodoc]] TFRepetitionPenaltyLogitsProcessor + - __call__ + +[[autodoc]] TFSuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] TFSuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] TFTemperatureLogitsWarper + - __call__ + +[[autodoc]] TFTopKLogitsWarper + - __call__ + +[[autodoc]] TFTopPLogitsWarper + - __call__ + +### FLAX [[transformers.FlaxForcedBOSTokenLogitsProcessor]] + +[[autodoc]] FlaxForcedBOSTokenLogitsProcessor + - __call__ + +[[autodoc]] FlaxForcedEOSTokenLogitsProcessor + - __call__ + +[[autodoc]] FlaxForceTokensLogitsProcessor + - __call__ + +[[autodoc]] FlaxLogitsProcessor + - __call__ + +[[autodoc]] FlaxLogitsProcessorList + - __call__ + +[[autodoc]] FlaxLogitsWarper + - __call__ + +[[autodoc]] FlaxMinLengthLogitsProcessor + - __call__ + +[[autodoc]] FlaxSuppressTokensAtBeginLogitsProcessor + - __call__ + +[[autodoc]] FlaxSuppressTokensLogitsProcessor + - __call__ + +[[autodoc]] FlaxTemperatureLogitsWarper + - __call__ + +[[autodoc]] FlaxTopKLogitsWarper + - __call__ + +[[autodoc]] FlaxTopPLogitsWarper + - __call__ + +[[autodoc]] FlaxWhisperTimeStampLogitsProcessor + - __call__ + +## StoppingCriteria [[transformers.StoppingCriteria]] + +[`StoppingCriteria`]๋Š” ์ƒ์„ฑ์ด ์–ธ์ œ ๋ฉˆ์ถœ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค (EOS ํ† ํฐ ์™ธ). ์ด ๊ธฐ๋Šฅ์€ PyTorch ๊ตฌํ˜„์—๋งŒ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. + +[[autodoc]] StoppingCriteria + - __call__ + +[[autodoc]] StoppingCriteriaList + - __call__ + +[[autodoc]] MaxLengthCriteria + - __call__ + +[[autodoc]] MaxTimeCriteria + - __call__ + +[[autodoc]] StopStringCriteria + - __call__ + +[[autodoc]] EosTokenCriteria + - __call__ + +## Constraint [[transformers.Constraint]] + +[`Constraint`]๋Š” ์ƒ์„ฑ ์ถœ๋ ฅ์— ํŠน์ • ํ† ํฐ์ด๋‚˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ•์ œ๋กœ ํฌํ•จ์‹œํ‚ค๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ PyTorch ๊ตฌํ˜„์—๋งŒ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. + +[[autodoc]] Constraint + +[[autodoc]] PhrasalConstraint + +[[autodoc]] DisjunctiveConstraint + +[[autodoc]] ConstraintListState + +## ๋น” ๊ฒ€์ƒ‰ (BeamSearch) [[transformers.BeamScorer]] + +[[autodoc]] BeamScorer + - process + - finalize + +[[autodoc]] BeamSearchScorer + - process + - finalize + +[[autodoc]] ConstrainedBeamSearchScorer + - process + - finalize + +## ์ŠคํŠธ๋ฆฌ๋จธ (Streamers) [[transformers.TextStreamer]] + +[[autodoc]] TextStreamer + +[[autodoc]] TextIteratorStreamer + +## ์บ์‹œ (Caches) [[transformers.Cache]] + +[[autodoc]] CacheLayerMixin + - update + - get_seq_length + - get_mask_sizes + - get_max_cache_shape + - reset + - reorder_cache + +[[autodoc]] DynamicLayer + - update + - crop + - batch_repeat_interleave + - batch_select_indices + +[[autodoc]] StaticLayer + - update + +[[autodoc]] SlidingWindowLayer + - update + +[[autodoc]] QuantoQuantizedLayer + - update + +[[autodoc]] HQQQuantizedLayer + - update + +[[autodoc]] Cache + - update + - get_seq_length + - get_mask_sizes + - get_max_cache_shape + - reset + - reorder_cache + - crop + - batch_repeat_interleave + - batch_select_indices + +[[autodoc]] DynamicCache + - to_legacy_cache + - from_legacy_cache + +[[autodoc]] QuantizedCache + +[[autodoc]] QuantoQuantizedCache + +[[autodoc]] HQQQuantizedCache + +[[autodoc]] OffloadedCache + +[[autodoc]] StaticCache + +[[autodoc]] OffloadedStaticCache + +[[autodoc]] HybridCache + +[[autodoc]] HybridChunkedCache + +[[autodoc]] SlidingWindowCache + +[[autodoc]] EncoderDecoderCache + - to_legacy_cache + - from_legacy_cache + +## ์›Œํ„ฐ๋งˆํฌ ์œ ํ‹ธ๋ฆฌํ‹ฐ (Watermark Utils) [[transformers.WatermarkDetector]] + +[[autodoc]] WatermarkDetector + - __call__ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/image_processing_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/image_processing_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..cd32935132af8798cffa646b1c4e987834a617ee --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/image_processing_utils.md @@ -0,0 +1,47 @@ + + +# ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[utilities-for-image-processors]] + +์ด ํŽ˜์ด์ง€๋Š” ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•˜๋ฉฐ, ์ฃผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜์˜ ๋ณ€ํ™˜ ์ž‘์—…๋“ค์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ [[transformers.image_transforms.center_crop]] + +[[autodoc]] image_transforms.center_crop + +[[autodoc]] image_transforms.center_to_corners_format + +[[autodoc]] image_transforms.corners_to_center_format + +[[autodoc]] image_transforms.id_to_rgb + +[[autodoc]] image_transforms.normalize + +[[autodoc]] image_transforms.pad + +[[autodoc]] image_transforms.rgb_to_id + +[[autodoc]] image_transforms.rescale + +[[autodoc]] image_transforms.resize + +[[autodoc]] image_transforms.to_pil_image + +## ImageProcessingMixin [[transformers.ImageProcessingMixin]] + +[[autodoc]] image_processing_utils.ImageProcessingMixin diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/modeling_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/modeling_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..f84ae30cd6f54555e6390f2f9b9464450f651f4e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/modeling_utils.md @@ -0,0 +1,66 @@ + + +# ์‚ฌ์šฉ์ž ์ •์˜ ๋ ˆ์ด์–ด ๋ฐ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[custom-layers-and-utilities]] + +์ด ํŽ˜์ด์ง€๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๋ ˆ์ด์–ด์™€ ๋ชจ๋ธ๋ง์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด์˜ ๋ชจ๋ธ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + + +## PyTorch ์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋“ˆ [[transformers.Conv1D]] + +[[autodoc]] pytorch_utils.Conv1D + +## PyTorch ํ—ฌํผ(helper) ํ•จ์ˆ˜ [[transformers.apply_chunking_to_forward]] + +[[autodoc]] pytorch_utils.apply_chunking_to_forward + +[[autodoc]] pytorch_utils.find_pruneable_heads_and_indices + +[[autodoc]] pytorch_utils.prune_layer + +[[autodoc]] pytorch_utils.prune_conv1d_layer + +[[autodoc]] pytorch_utils.prune_linear_layer + +## TensorFlow ์‚ฌ์šฉ์ž ์ •์˜ ๋ ˆ์ด์–ด [[transformers.modeling_tf_utils.TFConv1D]] + +[[autodoc]] modeling_tf_utils.TFConv1D + +[[autodoc]] modeling_tf_utils.TFSequenceSummary + +## TensorFlow ์†์‹ค ํ•จ์ˆ˜ [[transformers.modeling_tf_utils.TFCausalLanguageModelingLoss]] + +[[autodoc]] modeling_tf_utils.TFCausalLanguageModelingLoss + +[[autodoc]] modeling_tf_utils.TFMaskedLanguageModelingLoss + +[[autodoc]] modeling_tf_utils.TFMultipleChoiceLoss + +[[autodoc]] modeling_tf_utils.TFQuestionAnsweringLoss + +[[autodoc]] modeling_tf_utils.TFSequenceClassificationLoss + +[[autodoc]] modeling_tf_utils.TFTokenClassificationLoss + +## TensorFlow ๋„์šฐ๋ฏธ ํ•จ์ˆ˜ [[transformers.modeling_tf_utils.get_initializer]] + +[[autodoc]] modeling_tf_utils.get_initializer + +[[autodoc]] modeling_tf_utils.keras_serializable + +[[autodoc]] modeling_tf_utils.shape_list diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/pipelines_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/pipelines_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..e0e64913145541cc4381df0562861c2b364f1cf2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/pipelines_utils.md @@ -0,0 +1,43 @@ + + +# ํŒŒ์ดํ”„๋ผ์ธ์„ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[utilities-for-pipelines]] + +์ด ํŽ˜์ด์ง€๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“  ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด ๋ชจ๋ธ์˜ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ์ธ์ž ์ฒ˜๋ฆฌ [[transformers.pipelines.ArgumentHandler]] + +[[autodoc]] pipelines.ArgumentHandler + +[[autodoc]] pipelines.ZeroShotClassificationArgumentHandler + +[[autodoc]] pipelines.QuestionAnsweringArgumentHandler + +## ๋ฐ์ดํ„ฐ ํ˜•์‹ [[transformers.PipelineDataFormat]] + +[[autodoc]] pipelines.PipelineDataFormat + +[[autodoc]] pipelines.CsvPipelineDataFormat + +[[autodoc]] pipelines.JsonPipelineDataFormat + +[[autodoc]] pipelines.PipedPipelineDataFormat + +## ์œ ํ‹ธ๋ฆฌํ‹ฐ [[transformers.pipelines.PipelineException]] + +[[autodoc]] pipelines.PipelineException diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/time_series_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/time_series_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..5729924575b87332223aa5db6c56b6735777b4db --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/time_series_utils.md @@ -0,0 +1,29 @@ + + +# ์‹œ๊ณ„์—ด ์œ ํ‹ธ๋ฆฌํ‹ฐ [[time-series-utilities]] + +์ด ํŽ˜์ด์ง€๋Š” ์‹œ๊ณ„์—ด ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ์‹œ๊ณ„์—ด ๋ชจ๋ธ์˜ ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•˜๊ฑฐ๋‚˜ ๋ถ„ํฌ ์ถœ๋ ฅ ํด๋ž˜์Šค์˜ ์ปฌ๋ ‰์…˜์— ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์—๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ๋ถ„ํฌ ์ถœ๋ ฅ (Distributional Output) [[transformers.time_series_utils.NormalOutput]] + +[[autodoc]] time_series_utils.NormalOutput + +[[autodoc]] time_series_utils.StudentTOutput + +[[autodoc]] time_series_utils.NegativeBinomialOutput diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/tokenization_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/tokenization_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..b5b69910479a2fb42aeaf4d8bd366967ea9f53e7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/tokenization_utils.md @@ -0,0 +1,39 @@ + + +# ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ [[utilities-for-tokenizers]] + +์ด ํŽ˜์ด์ง€๋Š” ํ† ํฌ๋‚˜์ด์ €์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•˜๋ฉฐ, ์ฃผ๋กœ [`PreTrainedTokenizer`]์™€ [`PreTrainedTokenizerFast`] ์‚ฌ์ด์˜ ๊ณตํ†ต ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” [`~tokenization_utils_base.PreTrainedTokenizerBase`] ํด๋ž˜์Šค์™€ [`~tokenization_utils_base.SpecialTokensMixin`]์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ† ํฌ๋‚˜์ด์ € ์ฝ”๋“œ๋ฅผ ์—ฐ๊ตฌํ•  ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## PreTrainedTokenizerBase [[transformers.PreTrainedTokenizerBase]] + +[[autodoc]] tokenization_utils_base.PreTrainedTokenizerBase + - __call__ + - all + +## SpecialTokensMixin [[transformers.SpecialTokensMixin]] + +[[autodoc]] tokenization_utils_base.SpecialTokensMixin + +## Enums ๋ฐ namedtuples [[transformers.tokenization_utils_base.TruncationStrategy]] + +[[autodoc]] tokenization_utils_base.TruncationStrategy + +[[autodoc]] tokenization_utils_base.CharSpan + +[[autodoc]] tokenization_utils_base.TokenSpan diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/trainer_utils.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/trainer_utils.md new file mode 100644 index 0000000000000000000000000000000000000000..15fb457331811c6b8cfce23a53766057e816a058 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/internal/trainer_utils.md @@ -0,0 +1,49 @@ + + +# Trainer๋ฅผ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ (Utilities for Trainer) [[utilities-for-trainer]] + +์ด ํŽ˜์ด์ง€๋Š” [`Trainer`]์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋“ค ๋Œ€๋ถ€๋ถ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” Trainer ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ณ  ์‹ถ์„ ๋•Œ๋งŒ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ์œ ํ‹ธ๋ฆฌํ‹ฐ (Utilities) [[transformers.EvalPrediction]] + +[[autodoc]] EvalPrediction + +[[autodoc]] IntervalStrategy + +[[autodoc]] enable_full_determinism + +[[autodoc]] set_seed + +[[autodoc]] torch_distributed_zero_first + +## ์ฝœ๋ฐฑ ๋‚ด๋ถ€ (Callbacks internals) [[transformers.trainer_callback.CallbackHandler]] + +[[autodoc]] trainer_callback.CallbackHandler + +## ๋ถ„์‚ฐ ํ‰๊ฐ€ (Distributed Evaluation) [[transformers.trainer_pt_utils.DistributedTensorGatherer]] + +[[autodoc]] trainer_pt_utils.DistributedTensorGatherer + +## Trainer ์ธ์ž ํŒŒ์„œ (Trainer Argument Parser) [[transformers.HfArgumentParser]] + +[[autodoc]] HfArgumentParser + +## ๋””๋ฒ„๊ทธ ์œ ํ‹ธ๋ฆฌํ‹ฐ (Debug Utilities) [[transformers.debug_utils.DebugUnderflowOverflow]] + +[[autodoc]] debug_utils.DebugUnderflowOverflow \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_optims.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_optims.md new file mode 100644 index 0000000000000000000000000000000000000000..2a631721b88d7d5d5c71b0dad9d394583ed64f57 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_optims.md @@ -0,0 +1,407 @@ + + +# LLM ์ถ”๋ก  ์ตœ์ ํ™” [[llm-inference-optimization]] + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLM)์€ ์ฑ„ํŒ… ๋ฐ ์ฝ”๋“œ ์™„์„ฑ ๋ชจ๋ธ๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ ์ƒ์„ฑ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ํ•œ ๋‹จ๊ณ„ ๋Œ์–ด์˜ฌ๋ฆฌ๋ฉฐ, ๋†’์€ ์ˆ˜์ค€์˜ ์ดํ•ด๋ ฅ๊ณผ ์œ ์ฐฝํ•จ์„ ๋ณด์—ฌ์ฃผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ LLM์„ ๊ฐ•๋ ฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์š”์†Œ์ธ ๊ทธ๋“ค์˜ ํฌ๊ธฐ๋Š” ๋™์‹œ์— ์ถ”๋ก  ๊ณผ์ •์—์„œ ๋„์ „ ๊ณผ์ œ๊ฐ€ ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์ธ ์ถ”๋ก ์€ ๋А๋ฆฝ๋‹ˆ๋‹ค, ์™œ๋ƒํ•˜๋ฉด LLM์ด ๋‹ค์Œ ํ† ํฐ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ์ด ์ง„ํ–‰๋จ์— ๋”ฐ๋ผ ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ๊ธธ์–ด์ ธ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด ์ ์  ๊ธธ์–ด์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ, LLM์€ ์ˆ˜์‹ญ์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” LLM ์ถ”๋ก ์„ ๊ฐ€์†ํ•˜๊ธฐ ์œ„ํ•ด Transformers์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ํ™” ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +> [!TIP] +> Hugging Face๋Š” LLM์„ ์ถ”๋ก ์— ์ตœ์ ํ™”ํ•˜์—ฌ ๋ฐฐํฌํ•˜๊ณ  ์„œ๋น„์Šคํ•˜๋Š” ๋ฐ ์ „๋…ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ [Text Generation Inference (TGI)](https://hf.co/docs/text-generation-inference)์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ฒ˜๋ฆฌ๋Ÿ‰ ์ฆ๊ฐ€๋ฅผ ์œ„ํ•œ ์ง€์†์ ์ธ ๋ฐฐ์นญ๊ณผ ๋‹ค์ค‘ GPU ์ถ”๋ก ์„ ์œ„ํ•œ ํ…์„œ ๋ณ‘๋ ฌํ™”์™€ ๊ฐ™์€ Transformers์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฐฐํฌ ์ง€ํ–ฅ ์ตœ์ ํ™” ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. + +## ์ •์  kv-cache์™€ `torch.compile`[[static-kv-cache-and-torchcompile]] + +๋””์ฝ”๋”ฉ ์ค‘์— LLM์€ ๊ฐ ์ž…๋ ฅ ํ† ํฐ์— ๋Œ€ํ•œ key-value(kv) ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. LLM์€ ์ž๊ธฐํšŒ๊ท€(autoregressive)์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ๋œ ์ถœ๋ ฅ์ด ํ˜„์žฌ ์ž…๋ ฅ์˜ ์ผ๋ถ€๊ฐ€ ๋˜์–ด ๋งค๋ฒˆ ๋™์ผํ•œ kv ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งค๋ฒˆ ๋™์ผํ•œ kv ๊ฐ’์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +์ด๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด, ์ด์ „ ํ‚ค(key)์™€ ๊ฐ’(value)์„ ์žฌ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ์ €์žฅํ•˜๋Š” kv-cache๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ kv-cache๋Š” ๊ฐ ์ƒ์„ฑ ๋‹จ๊ณ„์—์„œ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋™์ ์ด๊ธฐ ๋•Œ๋ฌธ์— PyTorch ์ฝ”๋“œ๋ฅผ ๋น ๋ฅด๊ณ  ์ตœ์ ํ™”๋œ ์ปค๋„๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ์ตœ์ ํ™” ๋„๊ตฌ์ธ [`torch.compile`](./perf_torch_compile)์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +*์ •์  kv-cache*๋Š” ์ตœ๋Œ“๊ฐ’์„ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•˜์—ฌ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์—ฌ `torch.compile`๊ณผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ตœ๋Œ€ 4๋ฐฐ์˜ ์†๋„ ํ–ฅ์ƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์†๋„ ํ–ฅ์ƒ์€ ๋ชจ๋ธ ํฌ๊ธฐ(๋” ํฐ ๋ชจ๋ธ์€ ์†๋„ ํ–ฅ์ƒ์ด ์ ์Œ)์™€ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!WARNING] +ํ˜„์žฌ [Llama](./model_doc/llama2) ๋ฐ ๋ช‡ ๊ฐ€์ง€ ๋‹ค๋ฅธ ๋ชจ๋ธ๋งŒ ์ •์  kv-cache์™€ `torch.compile`์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์‹œ๊ฐ„ ๋ชจ๋ธ ํ˜ธํ™˜์„ฑ ๋ชฉ๋ก์€ [์ด ์ด์Šˆ](https://github.com/huggingface/transformers/issues/28981)๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. + +์ž‘์—…์˜ ๋ณต์žก์„ฑ์— ๋”ฐ๋ผ ์„ธ ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์ •์  kv-cache ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค: +1. ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•: `generation_config`์—์„œ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค(๊ถŒ์žฅ); +2. ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ•: ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์ƒ์„ฑ์ด๋‚˜ ๋งž์ถคํ˜• ์ƒ์„ฑ ๋ฃจํ”„๋ฅผ ์œ„ํ•ด ์บ์‹œ ๊ฐ์ฒด๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค; +3. ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ•: ๋‹จ์ผ ๊ทธ๋ž˜ํ”„๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ์ „์ฒด `generate` ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๊ทธ๋ž˜ํ”„๋กœ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. + +์˜ฌ๋ฐ”๋ฅธ ํƒญ์„ ์„ ํƒํ•˜์—ฌ ๊ฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ง€์นจ์„ ํ™•์ธํ•˜์„ธ์š”. + +> [!TIP] +> `torch.compile`์„ ์‚ฌ์šฉํ•  ๋•Œ ์–ด๋–ค ์ „๋žต์„ ์‚ฌ์šฉํ•˜๋“ , LLM ์ž…๋ ฅ์„ ์ œํ•œ๋œ ๊ฐ’ ์„ธํŠธ๋กœ ์™ผ์ชฝ์— ํŒจ๋”ฉํ•˜๋ฉด ๋ชจ์–‘๊ณผ ๊ด€๋ จ๋œ ์žฌ์ปดํŒŒ์ผ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`pad_to_multiple_of` tokenizer flag](https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.PreTrainedTokenizer.__call__.pad_to_multiple_of)๊ฐ€ ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! + + + + +์ด ์˜ˆ์ œ์—์„œ๋Š” [Gemma](https://hf.co/google/gemma-2b) ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: +1. ๋ชจ๋ธ์˜ `generation_config` ์†์„ฑ์— ์ ‘๊ทผํ•˜์—ฌ `cache_implementation`์„ "static"์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค; +2. ๋ชจ๋ธ์˜ `forward` ํŒจ์Šค๋ฅผ ์ •์  kv-cache์™€ ํ•จ๊ป˜ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด `torch.compile`์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค! + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM +import torch +import os +os.environ["TOKENIZERS_PARALLELISM"] = "false" # ๊ธด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ • :) + +tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") + +model.generation_config.cache_implementation = "static" + +model.forward = torch.compile(model.forward, mode="reduce-overhead", fullgraph=True) +input_text = "The theory of special relativity states " +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +outputs = model.generate(**input_ids) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['The theory of special relativity states 1. The speed of light is constant in all inertial reference'] +``` + +`generate` ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋™์ผํ•œ ์บ์‹œ ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๊ฐ ํ˜ธ์ถœ ์‹œ ์žฌ์ปดํŒŒ์ผ์˜ ํ•„์š”์„ฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์žฌ์ปดํŒŒ์ผ์„ ํ”ผํ•˜๋Š” ๊ฒƒ์€ `torch.compile`์˜ ์„ฑ๋Šฅ์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๋Š” ๋ฐ ๋งค์šฐ ์ค‘์š”ํ•˜๋ฉฐ, ๋‹ค์Œ ์‚ฌํ•ญ์— ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: +1. ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ํ˜ธ์ถœ ๊ฐ„ ์ตœ๋Œ€ ์ถœ๋ ฅ ๊ธธ์ด๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์บ์‹œ๋ฅผ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•˜๋ฉฐ, ์ด๋กœ ์ธํ•ด ์ƒˆ๋กœ ์ปดํŒŒ์ผ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค; +2. ์ปดํŒŒ์ผ๋œ ํ•จ์ˆ˜์˜ ์ฒซ ๋ช‡ ๋ฒˆ์˜ ํ˜ธ์ถœ์€ ํ•จ์ˆ˜๊ฐ€ ์ปดํŒŒ์ผ๋˜๋Š” ๋™์•ˆ ๋” ๋А๋ฆฝ๋‹ˆ๋‹ค. + +> [!WARNING] +> ๋‹ค์ค‘ ํ„ด ๋Œ€ํ™”์™€ ๊ฐ™์€ ์ •์  ์บ์‹œ์˜ ๊ณ ๊ธ‰ ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ๋Š”, ์บ์‹œ ๊ฐ์ฒด๋ฅผ [`~GenerationMixin.generate`] ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ• ํƒญ์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + + +[`StaticCache`] ๊ฐ์ฒด๋Š” `past_key_values` ์ธ์ˆ˜๋กœ ๋ชจ๋ธ์˜ [`~GenerationMixin.generate`] ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ์บ์‹œ ๋‚ด์šฉ์„ ์œ ์ง€ํ•˜๋ฏ€๋กœ, ๋™์  ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด [`~GenerationMixin.generate`] ํ˜ธ์ถœ์— ์ด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ƒ์„ฑ์„ ๊ณ„์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM, StaticCache +import torch +import os +os.environ["TOKENIZERS_PARALLELISM"] = "false" # ๊ธด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ • :) + +tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") + +model.forward = torch.compile(model.forward, mode="reduce-overhead", fullgraph=True) +input_text = "The theory of special relativity states " +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") +prompt_length = input_ids.input_ids.shape[1] +model.generation_config.max_new_tokens = 16 + +past_key_values = StaticCache( + config=model.config, + # ์บ์‹œ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ๊ณ„ํš์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“  ๊ฒฝ์šฐ์— ์ถฉ๋ถ„ํ•œ ์บ์‹œ ๊ธธ์ด๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค + max_cache_len=prompt_length+(model.generation_config.max_new_tokens*2), +) +outputs = model.generate(**input_ids, past_key_values=past_key_values) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['The theory of special relativity states 1. The speed of light is constant in all inertial reference frames. 2'] + +# ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ์™€ ๋™์ผํ•œ ์บ์‹œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ, ์ค‘๋‹จํ•œ ๊ณณ์—์„œ ์ƒ์„ฑ์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค. +# ๋‹ค์ค‘ ํ„ด ๋Œ€ํ™”์˜ ๊ฒฝ์šฐ, ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ์— ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +new_input_ids = outputs +outputs = model.generate(new_input_ids, past_key_values=past_key_values) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['The theory of special relativity states 1. The speed of light is constant in all inertial reference frames. 2. The speed of light is constant in all inertial reference frames. 3.'] +``` + +> [!TIP] +> ๋™์ผํ•œ [`StaticCache`] ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ์šด ํ”„๋กฌํ”„ํŠธ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ํ˜ธ์ถœ ๊ฐ„์— `.reset()` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ๋‚ด์šฉ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๋” ๊นŠ์ด ๋“ค์–ด๊ฐ€๊ณ  ์‹ถ๋‹ค๋ฉด, [`StaticCache`] ๊ฐ์ฒด๋ฅผ ๋ชจ๋ธ์˜ `forward` ํŒจ์Šค์— ๋™์ผํ•œ `past_key_values` ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ „๋žต์„ ์‚ฌ์šฉํ•˜๋ฉด, ํ˜„์žฌ ํ† ํฐ๊ณผ ์ด์ „์— ์ƒ์„ฑ๋œ ํ† ํฐ์˜ ์œ„์น˜ ๋ฐ ์บ์‹œ ์œ„์น˜๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ํ† ํฐ์„ ๋””์ฝ”๋”ฉํ•˜๋Š” ์ž์ฒด ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import LlamaTokenizer, LlamaForCausalLM, StaticCache, logging +from transformers.testing_utils import CaptureLogger +import torch + +prompts = [ + "Simply put, the theory of relativity states that ", + "My favorite all time favorite condiment is ketchup.", +] + +NUM_TOKENS_TO_GENERATE = 40 +torch_device = "cuda" + +tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf", pad_token="", padding_side="right") +model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf", device_map="sequential") +inputs = tokenizer(prompts, return_tensors="pt", padding=True).to(model.device) + +def decode_one_tokens(model, cur_token, input_pos, cache_position, past_key_values): + logits = model( + cur_token, + position_ids=input_pos, + cache_position=cache_position, + past_key_values=past_key_values, + return_dict=False, + use_cache=True + )[0] + new_token = torch.argmax(logits[:, -1], dim=-1)[:, None] + return new_token +``` + +`StaticCache` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์  kv-cache์™€ `torch.compile`์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: +1. ์ถ”๋ก ์— ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— [`StaticCache`] ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ตœ๋Œ€ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ์‹œํ€€์Šค ๊ธธ์ด์™€ ๊ฐ™์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +2. ์ •์  kv-cache์™€ ํ•จ๊ป˜ ์ˆœ์ „ํŒŒ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์— `torch.compile`์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. +3. [torch.backends.cuda.sdp_kernel](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž์—์„œ `enable_math=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋„ค์ดํ‹ฐ๋ธŒ PyTorch C++ ๊ตฌํ˜„๋œ ์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜(scaled dot product attention)์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ์ถ”๋ก  ์†๋„๋ฅผ ๋”์šฑ ๋†’์ž…๋‹ˆ๋‹ค. + +```py +batch_size, seq_length = inputs["input_ids"].shape +with torch.no_grad(): + past_key_values = StaticCache( + config=model.config, max_cache_len=4096 + ) + cache_position = torch.arange(seq_length, device=torch_device) + generated_ids = torch.zeros( + batch_size, seq_length + NUM_TOKENS_TO_GENERATE + 1, dtype=torch.int, device=torch_device + ) + generated_ids[:, cache_position] = inputs["input_ids"].to(torch_device).to(torch.int) + + logits = model( + **inputs, cache_position=cache_position, past_key_values=past_key_values,return_dict=False, use_cache=True + )[0] + next_token = torch.argmax(logits[:, -1], dim=-1)[:, None] + generated_ids[:, seq_length] = next_token[:, 0] + + decode_one_tokens = torch.compile(decode_one_tokens, mode="reduce-overhead", fullgraph=True) + cache_position = torch.tensor([seq_length + 1], device=torch_device) + for _ in range(1, NUM_TOKENS_TO_GENERATE): + with torch.backends.cuda.sdp_kernel(enable_flash=False, enable_mem_efficient=False, enable_math=True): + next_token = decode_one_tokens(model, next_token.clone(), None, cache_position, past_key_values) + generated_ids[:, cache_position] = next_token.int() + cache_position += 1 + +text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +text +['Simply put, the theory of relativity states that 1) the speed of light is constant, 2) the speed of light is the same for all observers, and 3) the laws of physics are the same for all observers.', + 'My favorite all time favorite condiment is ketchup. I love it on everything. I love it on my eggs, my fries, my chicken, my burgers, my hot dogs, my sandwiches, my salads, my p'] +``` + + + + +์ „์ฒด `generate` ํ•จ์ˆ˜๋ฅผ ์ปดํŒŒ์ผํ•˜๋Š” ๊ฒƒ์€ ์ฝ”๋“œ ์ธก๋ฉด์—์„œ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. `generate` ํ•จ์ˆ˜์— ๋Œ€ํ•ด `torch.compile`์„ ํ˜ธ์ถœํ•˜์—ฌ ์ „์ฒด ํ•จ์ˆ˜๋ฅผ ์ปดํŒŒ์ผํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ •์  ์บ์‹œ์˜ ์‚ฌ์šฉ์„ ์ง€์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ •์  ์บ์‹œ๋Š” ํ˜ธํ™˜๋˜์ง€๋งŒ, ๋ฒค์น˜๋งˆํฌ์—์„œ๋Š” ๋™์  ์บ์‹œ(๊ธฐ๋ณธ ์„ค์ •)๊ฐ€ ๋” ๋น ๋ฅธ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM +import torch +import os +os.environ["TOKENIZERS_PARALLELISM"] = "false" # ๊ธด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ • :) + +tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") +model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto") + +model.generate = torch.compile(model.generate, mode="reduce-overhead", fullgraph=True) +input_text = "The theory of special relativity states " +input_ids = tokenizer(input_text, return_tensors="pt").to("cuda") + +outputs = model.generate(**input_ids) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['The theory of special relativity states 1. The speed of light is constant in all inertial reference'] +``` + +์ด ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋ชจ๋ธ์˜ forward ํŒจ์Šค๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ž…๋ ฅ ์ค€๋น„, logit ์ฒ˜๋ฆฌ๊ธฐ ์ž‘์—… ๋“ฑ์„ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฒƒ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์‚ฌ์šฉ ์˜ˆ์ œ์— ๋น„ํ•ด `generate` ํ˜ธ์ถœ์ด ์•ฝ๊ฐ„ ๋” ๋น ๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ปดํŒŒ์ผ๋œ ๊ทธ๋ž˜ํ”„๋Š” ๋” ํŠน์ดํ•œ ํ•˜๋“œ์›จ์–ด ์žฅ์น˜๋‚˜ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ๋Š” ๋ช‡ ๊ฐ€์ง€ ํฐ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค: +1. ์ปดํŒŒ์ผ ์†๋„๊ฐ€ ํ›จ์”ฌ ๋А๋ฆฝ๋‹ˆ๋‹ค; +2. `generate`์˜ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค์ •์€ `generation_config`๋ฅผ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค; +3. ๋งŽ์€ ๊ฒฝ๊ณ ์™€ ์˜ˆ์™ธ๊ฐ€ ์–ต์ œ๋ฉ๋‹ˆ๋‹ค. -- ๋จผ์ € ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š์€ ํ˜•ํƒœ๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค; +4. ํ˜„์žฌ ์ž‘์—… ์ค‘์ด์ง€๋งŒ ๊ธฐ๋Šฅ ์ œํ•œ์ด ์‹ฌํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: ์ž‘์„ฑ ์‹œ์ ์—์„œ๋Š” EOS ํ† ํฐ์ด ์„ ํƒ๋˜์–ด๋„ ์ƒ์„ฑ์ด ์ค‘๋‹จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค). + + + + +## ์ถ”์ • ๋””์ฝ”๋”ฉ [[speculative-decoding]] + +> [!TIP] +> ๋ณด๋‹ค ์‹ฌ์ธต์ ์ธ ์„ค๋ช…์„ ์›ํ•œ๋‹ค๋ฉด, [Assisted Generation: a new direction toward low-latency text generation](https://hf.co/blog/assisted-generation) ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค! + +์ž๊ธฐ ํšŒ๊ท€์˜ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๋Š” ๊ฐ ์ž…๋ ฅ ํ† ํฐ์— ๋Œ€ํ•ด ์ˆœ์ „ํŒŒ ์ค‘์— ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ๋งค๋ฒˆ ๋กœ๋“œํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ˆ˜์‹ญ์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ LLM์—๋Š” ๋А๋ฆฌ๊ณ  ๋ฒˆ๊ฑฐ๋กญ์Šต๋‹ˆ๋‹ค. ์ถ”์ • ๋””์ฝ”๋”ฉ(speculative decoding)์€ ๋” ์ž‘๊ณ  ๋น ๋ฅธ ๋ณด์กฐ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›„๋ณด ํ† ํฐ์„ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ ํฐ LLM์ด ๋‹จ์ผ ์ˆœ์ „ํŒŒ์—์„œ ๊ฒ€์ฆํ•˜์—ฌ ์ด ์†๋„ ์ €ํ•˜๋ฅผ ์™„ํ™”ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ๋œ ํ† ํฐ์ด ์ •ํ™•ํ•˜๋‹ค๋ฉด, LLM์€ ๋ณธ๋ž˜ ์ž์ฒด์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ† ํฐ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „๋ฐฉ ํŒจ์Šค๊ฐ€ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋ณด์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •ํ™•๋„ ์ €ํ•˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. + +๊ฐ€์žฅ ํฐ ์†๋„ ํ–ฅ์ƒ์„ ์–ป๊ธฐ ์œ„ํ•ด, ๋ณด์กฐ ๋ชจ๋ธ์€ ๋น ๋ฅด๊ฒŒ ํ† ํฐ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก LLM๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณด์กฐ ๋ชจ๋ธ๊ณผ LLM ๋ชจ๋ธ์€ ํ† ํฐ์„ ๋‹ค์‹œ ์ธ์ฝ”๋”ฉํ•˜๊ณ  ๋””์ฝ”๋”ฉํ•˜์ง€ ์•Š๋„๋ก ๋™์ผํ•œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๊ณต์œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +> [!WARNING] +> ์ถ”์ • ๋””์ฝ”๋”ฉ์€ ํƒ์š• ๊ฒ€์ƒ‰๊ณผ ์ƒ˜ํ”Œ๋ง ๋””์ฝ”๋”ฉ ์ „๋žต์—์„œ๋งŒ ์ง€์›๋˜๋ฉฐ, ๋ฐฐ์น˜ ์ž…๋ ฅ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๋ณด์กฐ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ด๋ฅผ [`~GenerationMixin.generate`] ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•˜์—ฌ ์ถ”์ • ๋””์ฝ”๋”ฉ์„ ํ™œ์„ฑํ™”ํ•˜์‹ญ์‹œ์˜ค. + + + + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b") +inputs = tokenizer("Einstein's theory of relativity states", return_tensors="pt").to(device) + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-1.3b").to(device) +assistant_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m").to(device) +outputs = model.generate(**inputs, assistant_model=assistant_model) +tokenizer.batch_decode(outputs, skip_special_tokens=True) +["Einstein's theory of relativity states that the speed of light is constant. "] +``` + + + + +์ถ”์ • ์ƒ˜ํ”Œ๋ง ๋””์ฝ”๋”ฉ(speculative sampling decoding)์„ ์œ„ํ•ด, ๋ณด์กฐ ๋ชจ๋ธ ์™ธ์—๋„ [`~GenerationMixin.generate`] ๋ฉ”์„œ๋“œ์— `do_sample` ๋ฐ `temperature` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b") +inputs = tokenizer("Einstein's theory of relativity states", return_tensors="pt").to(device) + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-1.3b").to(device) +assistant_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m").to(device) +outputs = model.generate(**inputs, assistant_model=assistant_model, do_sample=True, temperature=0.7) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +["Einstein's theory of relativity states that motion in the universe is not a straight line.\n"] +``` + + + + +### ํ”„๋กฌํ”„ํŠธ ์กฐํšŒ ๋””์ฝ”๋”ฉ [[prompt-lookup-decoding]] + +ํ”„๋กฌํ”„ํŠธ ์กฐํšŒ ๋””์ฝ”๋”ฉ์€ ํƒ์š• ๊ฒ€์ƒ‰๊ณผ ์ƒ˜ํ”Œ๋ง๊ณผ๋„ ํ˜ธํ™˜๋˜๋Š” ์ถ”์ • ๋””์ฝ”๋”ฉ์˜ ๋ณ€ํ˜•์ž…๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ ์กฐํšŒ๋Š” ์š”์•ฝ๊ณผ ๊ฐ™์€ ์ž…๋ ฅ ๊ธฐ๋ฐ˜ ์ž‘์—…์— ํŠนํžˆ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ”„๋กฌํ”„ํŠธ์™€ ์ถœ๋ ฅ ๊ฐ„์— ์ข…์ข… ๊ฒน์น˜๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒน์น˜๋Š” n-๊ทธ๋žจ์ด LLM ํ›„๋ณด ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํ”„๋กฌํ”„ํŠธ ์กฐํšŒ ๋””์ฝ”๋”ฉ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด `prompt_lookup_num_tokens` ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฒน์น˜๋Š” ํ† ํฐ ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ [`~GenerationMixin.generate`] ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b") +inputs = tokenizer("The second law of thermodynamics states", return_tensors="pt").to(device) + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-1.3b").to(device) +assistant_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m").to(device) +outputs = model.generate(**inputs, prompt_lookup_num_tokens=3) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +['The second law of thermodynamics states that entropy increases with temperature. '] +``` + + + + +์ƒ˜ํ”Œ๋ง๊ณผ ํ•จ๊ป˜ ํ”„๋กฌํ”„ํŠธ ์กฐํšŒ ๋””์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, [`~GenerationMixin.generate`] ๋ฉ”์„œ๋“œ์— `do_sample` ๋ฐ `temperature` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer +import torch + +device = "cuda" if torch.cuda.is_available() else "cpu" + +tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b") +inputs = tokenizer("The second law of thermodynamics states", return_tensors="pt").to(device) + +model = AutoModelForCausalLM.from_pretrained("facebook/opt-1.3b").to(device) +outputs = model.generate(**inputs, prompt_lookup_num_tokens=3, do_sample=True, temperature=0.7) +print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) +["The second law of thermodynamics states that energy cannot be created nor destroyed. It's not a"] +``` + + + + +## ์–ดํ…์…˜ ์ตœ์ ํ™” [[attention-optimizations]] + +ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์˜ ์•Œ๋ ค์ง„ ๋ฌธ์ œ๋Š” ์…€ํ”„ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์ž…๋ ฅ ํ† ํฐ ์ˆ˜์™€ ํ•จ๊ป˜ ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ œ๊ณฑ์œผ๋กœ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ œํ•œ์€ ํ›จ์”ฌ ๋” ๊ธด ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” LLM์—์„œ๋Š” ๋”์šฑ ์ปค์ง‘๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด FlashAttention2 ๋˜๋Š” PyTorch์˜ ์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์ด๋“ค์€ ๋” ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์–ดํ…์…˜ ๊ตฌํ˜„์œผ๋กœ ์ถ”๋ก ์„ ๊ฐ€์†ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### FlashAttention-2 [[flashattention-2]] + +FlashAttention๊ณผ [FlashAttention-2](./perf_infer_gpu_one#flashattention-2)๋Š” ์–ดํ…์…˜ ๊ณ„์‚ฐ์„ ๋” ์ž‘์€ ์ฒญํฌ๋กœ ๋‚˜๋ˆ„๊ณ  ์ค‘๊ฐ„ ์ฝ๊ธฐ/์“ฐ๊ธฐ ์ž‘์—…์„ ์ค„์—ฌ ์ถ”๋ก  ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. FlashAttention-2๋Š” ์›๋ž˜ FlashAttention ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•˜์—ฌ ์‹œํ€€์Šค ๊ธธ์ด ์ฐจ์›์—์„œ๋„ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ํ•˜๋“œ์›จ์–ด์—์„œ ์ž‘์—…์„ ๋” ์ž˜ ๋ถ„ํ• ํ•˜์—ฌ ๋™๊ธฐํ™” ๋ฐ ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค. + +FlashAttention-2๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด [`~PreTrainedModel.from_pretrained`] ๋ฉ”์„œ๋“œ์—์„œ `attn_implementation="flash_attention_2"`๋ฅผ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quant_config = BitsAndBytesConfig(load_in_8bit=True) +model = AutoModelForCausalLM.from_pretrained( + "google/gemma-2b", + quantization_config=quant_config, + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2", +) +``` + +### PyTorch ์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜(scaled dot product attention) [[pytorch-scaled-dot-product-attention]] + +์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜(SDPA)๋Š” PyTorch 2.0์—์„œ ์ž๋™์œผ๋กœ ํ™œ์„ฑํ™”๋˜๋ฉฐ, FlashAttention, xFormers, PyTorch์˜ C++ ๊ตฌํ˜„์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. SDPA๋Š” CUDA ๋ฐฑ์—”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ์„ฑ๋Šฅ์ด ์ข‹์€ ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฑ์—”๋“œ์—์„œ๋Š” SDPA๊ฐ€ PyTorch C++ ๊ตฌํ˜„์œผ๋กœ ๊ธฐ๋ณธ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. + +> [!TIP] +> SDPA๋Š” ์ตœ์‹  PyTorch ๋ฒ„์ „์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด FlashAttention-2๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +์„ธ ๊ฐ€์ง€ ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ™œ์„ฑํ™”ํ•˜๊ฑฐ๋‚˜ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [torch.backends.cuda.sdp_kernel](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html) ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด FlashAttention์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด `enable_flash=True`๋กœ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. + +```py +import torch +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained( + "google/gemma-2b", + torch_dtype=torch.bfloat16, +) + +with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): + outputs = model.generate(**inputs) +``` + +## ์–‘์žํ™” [[quantization]] + +์–‘์žํ™”๋Š” LLM ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ์ €์žฅํ•˜์—ฌ ํฌ๊ธฐ๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๋ฉฐ GPU ๋ฉ”๋ชจ๋ฆฌ์— ์ œ์•ฝ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ถ”๋ก ์„ ์œ„ํ•ด LLM์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ๋” ์šฉ์ดํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. GPU๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด, ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์ธ ์–‘์žํ™” ๋ฐ ์–‘์žํ™” ํ•ด์ œ ๋‹จ๊ณ„๋กœ ์ธํ•ด ์•ฝ๊ฐ„์˜ ์ง€์—ฐ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค(AWQ ๋ฐ ์œตํ•ฉ AWQ ๋ชจ๋“ˆ ์ œ์™ธ). + +> [!TIP] +> ๋‹ค์–‘ํ•œ ์–‘์žํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(์ž์„ธํ•œ ๋‚ด์šฉ์€ [Quantization](./quantization) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” Quanto, AQLM, VPTQ, AWQ ๋ฐ AutoGPTQ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์‹ญ์‹œ์˜ค. ๋˜ํ•œ AutoGPTQ์™€ bitsandbytes๋ฅผ ๋น„๊ตํ•˜๋Š” [Overview of natively supported quantization schemes in ๐Ÿค— Transformers](https://hf.co/blog/overview-quantization-transformers) ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ์„ ์ฝ์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜์˜ ๋ชจ๋ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณ„์‚ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”์ •ํ•˜๊ณ  ๋น„๊ตํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1)๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”์ •ํ•ด ๋ณด์‹ญ์‹œ์˜ค. + + + +Mistral-7B-v0.1์„ ๋ฐ˜์ •๋ฐ€๋„๋กœ ๋กœ๋“œํ•˜๋ ค๋ฉด [`~transformers.AutoModelForCausalLM.from_pretrained`] ๋ฉ”์„œ๋“œ์—์„œ `torch_dtype` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `torch.bfloat16`์œผ๋กœ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. ์ด ๊ฒฝ์šฐ 13.74GB์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM +import torch + +model = AutoModelForCausalLM.from_pretrained( + "mistralai/Mistral-7B-v0.1", torch_dtype=torch.bfloat16, device_map="auto", +) +``` + +์ถ”๋ก ์„ ์œ„ํ•ด ์–‘์žํ™”๋œ ๋ชจ๋ธ(8๋น„ํŠธ ๋˜๋Š” 4๋น„ํŠธ)์„ ๋กœ๋“œํ•˜๋ ค๋ฉด [bitsandbytes](https://hf.co/docs/bitsandbytes)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  `load_in_4bit` ๋˜๋Š” `load_in_8bit` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `True`๋กœ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. ๋ชจ๋ธ์„ 8๋น„ํŠธ๋กœ ๋กœ๋“œํ•˜๋Š” ๋ฐ๋Š” 6.87GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig +import torch + +quant_config = BitsAndBytesConfig(load_in_8bit=True) +model = AutoModelForCausalLM.from_pretrained( + "mistralai/Mistral-7B-v0.1", quantization_config=quant_config, device_map="auto" +) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..d5e0bd356edd2e8589e1f70a3ca65809d09c6d72 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial.md @@ -0,0 +1,222 @@ + + + +# ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ๋กœ ์ƒ์„ฑํ•˜๊ธฐ [[generation-with-llms]] + +[[open-in-colab]] + +LLM ๋˜๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ์ƒ์„ฑ์˜ ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด, ์ฃผ์–ด์ง„ ์ž…๋ ฅ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ๋‹ค์Œ ๋‹จ์–ด(์ •ํ™•ํ•˜๊ฒŒ๋Š” ํ† ํฐ)๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ํ›ˆ๋ จ๋œ ๋Œ€๊ทœ๋ชจ ์‚ฌ์ „ ํ›ˆ๋ จ ๋ณ€ํ™˜๊ธฐ ๋ชจ๋ธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ† ํฐ์„ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์˜ˆ์ธกํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ ์™ธ์— ๋” ๋ณต์žกํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์€ ๋ช‡ ๊ฐœ์˜ ์ดˆ๊ธฐ ์ž…๋ ฅ๊ฐ’์„ ์ œ๊ณตํ•œ ํ›„, ๊ทธ ์ถœ๋ ฅ์„ ๋‹ค์‹œ ๋ชจ๋ธ์— ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ์ถ”๋ก  ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๐Ÿค— Transformers์—์„œ๋Š” [`~generation.GenerationMixin.generate`] ๋ฉ”์†Œ๋“œ๊ฐ€ ์ด ์—ญํ• ์„ ํ•˜๋ฉฐ, ์ด๋Š” ์ƒ์„ฑ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + +์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋‹ค์Œ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: + +* LLM์œผ๋กœ ํ…์ŠคํŠธ ์ƒ์„ฑ +* ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ +* LLM์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์Œ ๋‹จ๊ณ„ + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers bitsandbytes>=0.39.0 -q +``` + + +## ํ…์ŠคํŠธ ์ƒ์„ฑ [[generate-text]] + +[์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(causal language modeling)](tasks/language_modeling)์„ ๋ชฉ์ ์œผ๋กœ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ์€ ์ผ๋ จ์˜ ํ…์ŠคํŠธ ํ† ํฐ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ ๋‹ค์Œ ํ† ํฐ์ด ๋‚˜์˜ฌ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + + +
+ +
"LLM์˜ ์ „๋ฐฉ ํŒจ์Šค"
+
+ +LLM๊ณผ ์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ ํ•ต์‹ฌ์ ์ธ ๋ถ€๋ถ„์€ ์ด ํ™•๋ฅ  ๋ถ„ํฌ๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ํ† ํฐ์„ ์–ด๋–ป๊ฒŒ ๊ณ ๋ฅผ ๊ฒƒ์ธ์ง€์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฐ˜๋ณต ๊ณผ์ •์— ์‚ฌ์šฉ๋  ํ† ํฐ์„ ๊ฒฐ์ •ํ•˜๋Š” ํ•œ, ์–ด๋– ํ•œ ๋ฐฉ๋ฒ•๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ  ๋ถ„ํฌ์—์„œ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ํ† ํฐ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊ฒฐ๊ณผ ๋ถ„ํฌ์—์„œ ์ƒ˜ํ”Œ๋งํ•˜๊ธฐ ์ „์— ์ˆ˜์‹ญ ๊ฐ€์ง€ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณต์žกํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + +
+ +
"์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์€ ํ™•๋ฅ  ๋ถ„ํฌ์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์„ ํƒํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค."
+
+ +์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ณผ์ •์€ ์–ด๋–ค ์ข…๋ฃŒ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์‹œํ€€์Šค์˜ ๋(EOS ํ† ํฐ)์„ ์ถœ๋ ฅํ•  ๋•Œ๊นŒ์ง€๋ฅผ ์ข…๋ฃŒ ์กฐ๊ฑด์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ตœ๋Œ€ ๊ธธ์ด์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ์ƒ์„ฑ์ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ์ด ์˜ˆ์ƒ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„  ํ† ํฐ ์„ ํƒ ๋‹จ๊ณ„์™€ ์ •์ง€ ์กฐ๊ฑด์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ, ๊ฐ ๋ชจ๋ธ์—๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ ์„ค์ •์ด ์ž˜ ์ •์˜๋œ [`~generation.GenerationConfig`] ํŒŒ์ผ์ด ํ•จ๊ป˜ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. + +์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค! + + + +๊ธฐ๋ณธ LLM ์‚ฌ์šฉ์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด, ์šฐ๋ฆฌ์˜ [`Pipeline`](pipeline_tutorial) ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ LLM์€ ์–‘์žํ™”๋‚˜ ํ† ํฐ ์„ ํƒ ๋‹จ๊ณ„์—์„œ์˜ ๋ฏธ์„ธํ•œ ์ œ์–ด์™€ ๊ฐ™์€ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ๋“ค์„ ์ข…์ข… ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์€ [`~generation.GenerationMixin.generate`]๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ž˜ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LLM์„ ์ด์šฉํ•œ ์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์€ ์ž์›์„ ๋งŽ์ด ์†Œ๋ชจํ•˜๋ฏ€๋กœ, ์ ์ ˆํ•œ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์œ„ํ•ด GPU์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +๋จผ์ €, ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค์„ธ์š”. + +```python +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained( +... "mistralai/Mistral-7B-v0.1", device_map="auto", load_in_4bit=True +... ) +``` + +`from_pretrained` ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ 2๊ฐœ์˜ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ฃผ๋ชฉํ•˜์„ธ์š”: + +- `device_map`์€ ๋ชจ๋ธ์ด GPU๋กœ ์ด๋™๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. +- `load_in_4bit`๋Š” ๋ฆฌ์†Œ์Šค ์š”๊ตฌ ์‚ฌํ•ญ์„ ํฌ๊ฒŒ ์ค„์ด๊ธฐ ์œ„ํ•ด [4๋น„ํŠธ ๋™์  ์–‘์žํ™”](main_classes/quantization)๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์ด ์™ธ์—๋„ ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, LLM์„ ์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ ์ด ์„ค์ •์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. + +์ด์–ด์„œ ํ…์ŠคํŠธ ์ž…๋ ฅ์„ [ํ† ํฌ๋‚˜์ด์ €](tokenizer_summary)์œผ๋กœ ์ „์ฒ˜๋ฆฌํ•˜์„ธ์š”. + +```python +>>> from transformers import AutoTokenizer +>>> import torch + +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") +>>> device = "cuda" if torch.cuda.is_available() else "cpu" +>>> model_inputs = tokenizer(["A list of colors: red, blue"], return_tensors="pt").to(device) +``` + +`model_inputs` ๋ณ€์ˆ˜์—๋Š” ํ† ํฐํ™”๋œ ํ…์ŠคํŠธ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์–ดํ…์…˜ ๋งˆ์Šคํฌ๊ฐ€ ๋“ค์–ด ์žˆ์Šต๋‹ˆ๋‹ค. [`~generation.GenerationMixin.generate`]๋Š” ์–ดํ…์…˜ ๋งˆ์Šคํฌ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋„ ์ด๋ฅผ ์ถ”๋ก ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜์ง€๋งŒ, ์ตœ์ƒ์˜ ์„ฑ๋Šฅ์„ ์œ„ํ•ด์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ [`~generation.GenerationMixin.generate`] ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ƒ์„ฑ๋œ ํ† ํฐ์„ ์–ป์€ ํ›„, ์ด๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์ „์— ํ…์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜์„ธ์š”. + +```python +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A list of colors: red, blue, green, yellow, black, white, and brown' +``` + +์ด๊ฒŒ ์ „๋ถ€์ž…๋‹ˆ๋‹ค! ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋งŒ์œผ๋กœ LLM์˜ ๋Šฅ๋ ฅ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + + +## ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ [[common-pitfalls]] + +[์ƒ์„ฑ ์ „๋žต](generation_strategies)์ด ๋งŽ๊ณ , ๊ธฐ๋ณธ๊ฐ’์ด ํ•ญ์ƒ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์ ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ด ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅผ ๋•Œ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์™€ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ชฉ๋ก์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") +>>> tokenizer.pad_token = tokenizer.eos_token # Mistral has no pad token by default +>>> model = AutoModelForCausalLM.from_pretrained( +... "mistralai/Mistral-7B-v0.1", device_map="auto", load_in_4bit=True +... ) +``` + +### ์ƒ์„ฑ๋œ ์ถœ๋ ฅ์ด ๋„ˆ๋ฌด ์งง๊ฑฐ๋‚˜ ๊ธธ๋‹ค [[generated-output-is-too-shortlong]] + +[`~generation.GenerationConfig`] ํŒŒ์ผ์—์„œ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, `generate`๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ตœ๋Œ€ 20๊ฐœ์˜ ํ† ํฐ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. `generate` ํ˜ธ์ถœ์—์„œ `max_new_tokens`์„ ์ˆ˜๋™์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ ํ† ํฐ์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. LLM(์ •ํ™•ํ•˜๊ฒŒ๋Š” [๋””์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ](https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt))์€ ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋„ ์ถœ๋ ฅ์˜ ์ผ๋ถ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + + +```py +>>> model_inputs = tokenizer(["A sequence of numbers: 1, 2"], return_tensors="pt").to("cuda") + +>>> # By default, the output will contain up to 20 tokens +>>> generated_ids = model.generate(**model_inputs, pad_token_id=tokenizer.eos_token_id) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A sequence of numbers: 1, 2, 3, 4, 5' + +>>> # Setting `max_new_tokens` allows you to control the maximum length +>>> generated_ids = model.generate(**model_inputs, pad_token_id=tokenizer.eos_token_id, max_new_tokens=50) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'A sequence of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,' +``` + +### ์ž˜๋ชป๋œ ์ƒ์„ฑ ๋ชจ๋“œ [[incorrect-generation-mode]] + +๊ธฐ๋ณธ์ ์œผ๋กœ [`~generation.GenerationConfig`] ํŒŒ์ผ์—์„œ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, `generate`๋Š” ๊ฐ ๋ฐ˜๋ณต์—์„œ ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ํ† ํฐ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค(๊ทธ๋ฆฌ๋”” ๋””์ฝ”๋”ฉ). ํ•˜๋ ค๋Š” ์ž‘์—…์— ๋”ฐ๋ผ ์ด ๋ฐฉ๋ฒ•์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฑ—๋ด‡์ด๋‚˜ ์—์„ธ์ด ์ž‘์„ฑ๊ณผ ๊ฐ™์€ ์ฐฝ์˜์ ์ธ ์ž‘์—…์€ ์ƒ˜ํ”Œ๋ง์ด ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์˜ค๋””์˜ค๋ฅผ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋ฒˆ์—ญ๊ณผ ๊ฐ™์€ ์ž…๋ ฅ ๊ธฐ๋ฐ˜ ์ž‘์—…์€ ๊ทธ๋ฆฌ๋”” ๋””์ฝ”๋”ฉ์ด ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `do_sample=True`๋กœ ์ƒ˜ํ”Œ๋ง์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ์ฃผ์ œ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด [๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/how-to-generate)์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +>>> # Set seed or reproducibility -- you don't need this unless you want full reproducibility +>>> from transformers import set_seed +>>> set_seed(0) + +>>> model_inputs = tokenizer(["I am a cat."], return_tensors="pt").to("cuda") + +>>> # LLM + greedy decoding = repetitive, boring output +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'I am a cat. I am a cat. I am a cat. I am a cat' + +>>> # With sampling, the output becomes more creative! +>>> generated_ids = model.generate(**model_inputs, do_sample=True) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'I am a cat.\nI just need to be. I am always.\nEvery time' +``` + +### ์ž˜๋ชป๋œ ํŒจ๋”ฉ [[wrong-padding-side]] + +LLM์€ [๋””์ฝ”๋” ์ „์šฉ](https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt) ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด, ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ์— ๋Œ€ํ•ด ์ง€์†์ ์œผ๋กœ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ํŒจ๋”ฉ ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. LLM์€ ํŒจ๋”ฉ ํ† ํฐ์—์„œ ์ž‘๋™์„ ์ด์–ด๊ฐ€๋„๋ก ์„ค๊ณ„๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ž…๋ ฅ ์™ผ์ชฝ์— ํŒจ๋”ฉ์ด ์ถ”๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋„ ๊ผญ `generate` ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! + +```python +>>> # The tokenizer initialized above has right-padding active by default: the 1st sequence, +>>> # which is shorter, has padding on the right side. Generation fails. +>>> model_inputs = tokenizer( +... ["1, 2, 3", "A, B, C, D, E"], padding=True, return_tensors="pt" +... ).to("cuda") +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids[0], skip_special_tokens=True)[0] +'' + +>>> # With left-padding, it works as expected! +>>> tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b", padding_side="left") +>>> tokenizer.pad_token = tokenizer.eos_token # Llama has no pad token by default +>>> model_inputs = tokenizer( +... ["1, 2, 3", "A, B, C, D, E"], padding=True, return_tensors="pt" +... ).to("cuda") +>>> generated_ids = model.generate(**model_inputs) +>>> tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +'1, 2, 3, 4, 5, 6,' +``` + + + +## ์ถ”๊ฐ€ ์ž๋ฃŒ [[further-resources]] + +์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ๋‹จ์ˆœํ•œ ํŽธ์ด์ง€๋งŒ, LLM์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๋ ค๋ฉด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์š”์†Œ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‰ฝ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LLM์— ๋Œ€ํ•œ ๋” ๊นŠ์€ ์ดํ•ด์™€ ํ™œ์šฉ์„ ์œ„ํ•œ ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค: + + +### ๊ณ ๊ธ‰ ์ƒ์„ฑ ์‚ฌ์šฉ [[advanced-generate-usage]] + +1. [๊ฐ€์ด๋“œ](generation_strategies)๋Š” ๋‹ค์–‘ํ•œ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ƒ์„ฑ ์„ค์ • ํŒŒ์ผ์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ถœ๋ ฅ์„ ์ŠคํŠธ๋ฆฌ๋ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. +2. [`~generation.GenerationConfig`]์™€ [`~generation.GenerationMixin.generate`], [generate-related classes](internal/generation_utils)๋ฅผ ์ฐธ์กฐํ•ด๋ณด์„ธ์š”. + +### LLM ๋ฆฌ๋”๋ณด๋“œ [[llm-leaderboards]] + +1. [Open LLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์˜ ํ’ˆ์งˆ์— ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. +2. [Open LLM-Perf Leaderboard](https://huggingface.co/spaces/optimum/llm-perf-leaderboard)๋Š” LLM ์ฒ˜๋ฆฌ๋Ÿ‰์— ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. + +### ์ง€์—ฐ ์‹œ๊ฐ„ ๋ฐ ์ฒ˜๋ฆฌ๋Ÿ‰ [[latency-and-throughput]] + +1. ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ค„์ด๋ ค๋ฉด, ๋™์  ์–‘์žํ™”์— ๋Œ€ํ•œ [๊ฐ€์ด๋“œ](main_classes/quantization)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +### ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ [[related-libraries]] + +1. [`text-generation-inference`](https://github.com/huggingface/text-generation-inference)๋Š” LLM์„ ์œ„ํ•œ ์‹ค์ œ ์šด์˜ ํ™˜๊ฒฝ์— ์ ํ•ฉํ•œ ์„œ๋ฒ„์ž…๋‹ˆ๋‹ค. +2. [`optimum`](https://github.com/huggingface/optimum)์€ ํŠน์ • ํ•˜๋“œ์›จ์–ด ์žฅ์น˜์—์„œ LLM์„ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๐Ÿค— Transformers๋ฅผ ํ™•์žฅํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial_optimization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial_optimization.md new file mode 100644 index 0000000000000000000000000000000000000000..91fdeff0445a59d3fcfd683754f2f01eabb2284a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/llm_tutorial_optimization.md @@ -0,0 +1,759 @@ + +# ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ์†๋„ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™” [[optimizing-llms-for-speed-and-memory]] + +[[open-in-colab]] + +GPT3/4, [Falcon](https://huggingface.co/tiiuae/falcon-40b), [Llama](https://huggingface.co/meta-llama/Llama-2-70b-hf)์™€ ๊ฐ™์€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ธ๊ฐ„ ์ค‘์‹ฌ ๊ณผ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋Šฅ๋ ฅ์ด ๋น ๋ฅด๊ฒŒ ๋ฐœ์ „ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ํ˜„๋Œ€ ์ง€์‹ ๊ธฐ๋ฐ˜ ์‚ฐ์—…์—์„œ ํ•„์ˆ˜ ๋„๊ตฌ๋กœ ์ž๋ฆฌ์žก๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์„ ์‹ค์ œ ๊ณผ์ œ์— ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ์€ ์—ฌ์ „ํžˆ ์–ด๋ ค์šด ๊ณผ์ œ์ž…๋‹ˆ๋‹ค. + +- ์ธ๊ฐ„๊ณผ ๋น„์Šทํ•œ ํ…์ŠคํŠธ ์ดํ•ด ๋ฐ ์ƒ์„ฑ ๋Šฅ๋ ฅ์„ ๋ณด์ด๊ธฐ ์œ„ํ•ด, ํ˜„์žฌ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์ˆ˜์‹ญ์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (์ฐธ์กฐ: [Kaplan et al](https://huggingface.co/papers/2001.08361), [Wei et. al](https://huggingface.co/papers/2206.07682)). ์ด๋Š” ์ถ”๋ก ์„ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋ฅผ ํฌ๊ฒŒ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. +- ๋งŽ์€ ์‹ค์ œ ๊ณผ์ œ์—์„œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ๋ฐฉ๋Œ€ํ•œ ๋งฅ๋ฝ ์ •๋ณด๋ฅผ ์ œ๊ณต๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ์ถ”๋ก  ๊ณผ์ •์—์„œ ๋งค์šฐ ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. + +์ด๋Ÿฌํ•œ ๊ณผ์ œ์˜ ํ•ต์‹ฌ์€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํ™œ์šฉ ๋Šฅ๋ ฅ์„ ์ฆ๋Œ€์‹œํ‚ค๋Š” ๋ฐ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๋ฐฉ๋Œ€ํ•œ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๋Šฅ๋ ฅ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ํšจ์œจ์ ์ธ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ ํšจ๊ณผ์ ์ธ ๊ธฐ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +1. **๋‚ฎ์€ ์ •๋ฐ€๋„:** ์—ฐ๊ตฌ์— ๋”ฐ๋ฅด๋ฉด, [8๋น„ํŠธ์™€ 4๋น„ํŠธ](./main_classes/quantization)์™€ ๊ฐ™์ด ๋‚ฎ์€ ์ˆ˜์น˜ ์ •๋ฐ€๋„๋กœ ์ž‘๋™ํ•˜๋ฉด ๋ชจ๋ธ ์„ฑ๋Šฅ์˜ ํฐ ์ €ํ•˜ ์—†์ด ๊ณ„์‚ฐ์ƒ์˜ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +2. **ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜:** ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ๋†’์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ตœ์ ํ™”๋œ GPU ๋ฉ”๋ชจ๋ฆฌ ํ™œ์šฉ์„ ํ†ตํ•ด ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ณ€ํ˜•์ž…๋‹ˆ๋‹ค. + +3. **์•„ํ‚คํ…์ฒ˜ ํ˜์‹ :** ์ถ”๋ก  ์‹œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ๋กœ ๋™์ผํ•œ ๋ฐฉ์‹(๊ธด ์ž…๋ ฅ ๋งฅ๋ฝ์„ ๊ฐ€์ง„ ์ž๊ธฐํšŒ๊ท€ ํ…์ŠคํŠธ ์ƒ์„ฑ ๋ฐฉ์‹)์œผ๋กœ ๋ฐฐํฌ๋˜๋Š”๋ฐ, ๋” ํšจ์œจ์ ์ธ ์ถ”๋ก ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํŠนํ™”๋œ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๊ฐ€ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฐœ์ „์œผ๋กœ๋Š” [Alibi](https://huggingface.co/papers/2108.12409), [Rotary embeddings](https://huggingface.co/papers/2104.09864), [Multi-Query Attention (MQA)](https://huggingface.co/papers/1911.02150), [Grouped-Query-Attention (GQA)](https://huggingface.co/papers/2305.13245)์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ํ…์„œ์˜ ๊ด€์ ์—์„œ ์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์— ๋Œ€ํ•œ ๋ถ„์„์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‚ฎ์€ ์ •๋ฐ€๋„๋ฅผ ์ฑ„ํƒํ•˜๋Š” ๊ฒƒ์˜ ์žฅ๋‹จ์ ์„ ๋…ผ์˜ํ•˜๊ณ , ์ตœ์‹  ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํฌ๊ด„์ ์œผ๋กœ ํƒ๊ตฌํ•˜๋ฉฐ, ํ–ฅ์ƒ๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด ๋…ผํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ๊ฐ ๊ธฐ๋Šฅ์˜ ๊ฐœ์„  ์‚ฌํ•ญ์„ ๋ณด์—ฌ์ฃผ๋Š” ์‹ค์šฉ์ ์ธ ์˜ˆ์ œ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. + +## 1. ๋‚ฎ์€ ์ •๋ฐ€๋„ [[1-lower-precision]] + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋ณด๊ณ , ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ๋ฒกํ„ฐ์˜ ์‹œํ€€์Šค๋กœ ๋ณธ๋‹ค๋ฉด, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ๊ฐ€์žฅ ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์–ด์ง€๋Š” ๋‚ด์šฉ์—์„œ *๊ฐ€์ค‘์น˜*๋Š” ๋ชจ๋ธ์˜ ๋ชจ๋“  ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์˜ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์ตœ์†Œ ๋ช‡์‹ญ์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” `4.5689`์™€ ๊ฐ™์€ ์‹ญ์ง„์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๋ณดํ†ต [float32](https://en.wikipedia.org/wiki/Single-precision_floating-point_format), [bfloat16](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format) ๋˜๋Š” [float16](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) ํ˜•์‹์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +> *X * 10์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•˜๋ ค๋ฉด float32 ์ •๋ฐ€๋„์—์„œ ๋Œ€๋žต 4 * X GB์˜ VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.* + +์š”์ฆ˜์—๋Š” ๋ชจ๋ธ์ด float32 ์ •๋ฐ€๋„๋กœ ํ›ˆ๋ จ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๋“œ๋ฌผ๊ณ , ์ผ๋ฐ˜์ ์œผ๋กœ bfloat16 ์ •๋ฐ€๋„๋‚˜ ๊ฐ€๋” float16 ์ •๋ฐ€๋„๋กœ ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฝํ—˜์ ์œผ๋กœ ์•Œ์•„๋‚ธ ๋ฒ•์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +> *X * 10์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•˜๋ ค๋ฉด bfloat16/float16 ์ •๋ฐ€๋„์—์„œ ๋Œ€๋žต 2 * X GB์˜ VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.* + +์งง์€ ํ…์ŠคํŠธ ์ž…๋ ฅ(1024 ํ† ํฐ ๋ฏธ๋งŒ)์˜ ๊ฒฝ์šฐ, ์ถ”๋ก ์„ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์˜ ๋Œ€๋ถ€๋ถ„์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ง€๊ธˆ์€ ์ถ”๋ก ์„ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์ด ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋ฅผ GPU VRAM์— ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ๊ณผ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. + +๋ชจ๋ธ์„ bfloat16์œผ๋กœ ๋กœ๋“œํ•˜๋Š” ๋ฐ ๋Œ€๋žต ์–ผ๋งˆ๋‚˜ ๋งŽ์€ VRAM์ด ํ•„์š”ํ•œ์ง€ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +- **GPT3**๋Š” 2 \* 175 GB = **350 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- [**Bloom**](https://huggingface.co/bigscience/bloom)์€ 2 \* 176 GB = **352 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- [**Llama-2-70b**](https://huggingface.co/meta-llama/Llama-2-70b-hf)๋Š” 2 \* 70 GB = **140 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- [**Falcon-40b**](https://huggingface.co/tiiuae/falcon-40b)๋Š” 2 \* 40 GB = **80 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- [**MPT-30b**](https://huggingface.co/mosaicml/mpt-30b)๋Š” 2 * 30 GB = **60 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- [**bigcode/starcoder**](https://huggingface.co/bigcode/starcoder)๋Š” 2 * 15.5 GB = **31 GB** VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +์ด ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—์„œ, ํ˜„์žฌ ์‹œ์žฅ์—์„œ ๊ฐ€์žฅ ํฐ GPU ์นฉ์€ 80GB์˜ VRAM์„ ์ œ๊ณตํ•˜๋Š” A100๊ณผ H100์ž…๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰๋œ ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ๋“ค์€ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ตœ์†Œ 80GB ์ด์ƒ์˜ ์šฉ๋Ÿ‰์„ ํ•„์š”๋กœ ํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ [ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ](https://huggingface.co/docs/transformers/perf_train_gpu_many#tensor-parallelism) ๋ฐ/๋˜๋Š” [ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ](https://huggingface.co/docs/transformers/perf_train_gpu_many#naive-model-parallelism-vertical-and-pipeline-parallelism)๋ฅผ ๋ฐ˜๋“œ์‹œ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +๐Ÿค— Transformers๋Š” ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ”๋กœ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๊ฐ€ ํŠน์ • ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด [the text-generation-inference library](https://github.com/huggingface/text-generation-inference/tree/main/server/text_generation_server/models/custom_modeling)๋ฅผ ์ฐธ์กฐํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์ธ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋Š” ๋ฐ”๋กœ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‹จ์ˆœํžˆ ๋ชจ๋ธ์„ `device="auto"`๋กœ ๋กœ๋“œํ•˜๋ฉด [์—ฌ๊ธฐ](https://huggingface.co/docs/accelerate/v0.22.0/en/concept_guides/big_model_inference)์— ์„ค๋ช…๋œ ๋Œ€๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU์— ๋ชจ๋ธ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ ˆ์ด์–ด๋ฅผ ์ž๋™์œผ๋กœ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ํšจ๊ณผ์ ์ด๊ธด ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๊ธฐ๋ณธ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋Š” GPU ์œ ํœด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋” ๋ฐœ์ „๋œ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด์— ๋Œ€ํ•œ ์„ค๋ช…์€ [์—ฌ๊ธฐ](https://huggingface.co/docs/transformers/en/perf_train_gpu_many#naive-model-parallelism-vertical-and-pipeline-parallelism)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +80GB A100 GPU 8๊ฐœ๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, BLOOM์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +!pip install transformers accelerate bitsandbytes optimum +``` +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("bigscience/bloom", device_map="auto", pad_token_id=0) +``` + +`device_map="auto"`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU์— ์–ดํ…์…˜ ๋ ˆ์ด์–ด๊ฐ€ ๊ณ ๋ฅด๊ฒŒ ๋ถ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” [bigcode/octocoder](https://huggingface.co/bigcode/octocoder)๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ๋‹จ์ผ 40GB A100 GPU ์žฅ์น˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ ์šฉํ•  ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์†๋„ ์ตœ์ ํ™”๋Š” ๋ชจ๋ธ ๋˜๋Š” ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ์—๋„ ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ์ด bfloat16 ์ •๋ฐ€๋„๋กœ ๋กœ๋“œ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์œ„์˜ ๊ฒฝํ—˜์ ์œผ๋กœ ์•Œ์•„๋‚ธ ๋ฒ•์น™์„ ์‚ฌ์šฉํ•˜๋ฉด `bigcode/octocoder`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์ด ์•ฝ 31GB VRAM์ผ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +๋จผ์ € ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•œ ๋‹ค์Œ, ๋‘˜ ๋‹ค Transformers์˜ [ํŒŒ์ดํ”„๋ผ์ธ](https://huggingface.co/docs/transformers/main_classes/pipelines) ๊ฐ์ฒด์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline +import torch + +model = AutoModelForCausalLM.from_pretrained("bigcode/octocoder", torch_dtype=torch.bfloat16, device_map="auto", pad_token_id=0) +tokenizer = AutoTokenizer.from_pretrained("bigcode/octocoder") + +pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) +``` + +```python +prompt = "Question: Please write a function in Python that transforms bytes to Giga bytes.\n\nAnswer:" + +result = pipe(prompt, max_new_tokens=60)[0]["generated_text"][len(prompt):] +result +``` + +**์ถœ๋ ฅ**: +``` +Here is a Python function that transforms bytes to Giga bytes:\n\n```python\ndef bytes_to_giga_bytes(bytes):\n return bytes / 1024 / 1024 / 1024\n```\n\nThis function takes a single +``` + +์ข‹์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฒฐ๊ณผ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”์ดํŠธ๋ฅผ ๊ธฐ๊ฐ€๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +def bytes_to_giga_bytes(bytes): + return bytes / 1024 / 1024 / 1024 +``` + +[`torch.cuda.max_memory_allocated`](https://pytorch.org/docs/stable/generated/torch.cuda.max_memory_allocated.html)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ์ธก์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) +``` + +**์ถœ๋ ฅ**: +```bash +29.0260648727417 +``` + +๋Œ€๋žต์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ์™€ ๊ฑฐ์˜ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค! ๋ฐ”์ดํŠธ์—์„œ ํ‚ฌ๋กœ๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ 1000์ด ์•„๋‹Œ 1024๋กœ ๊ณฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ˆซ์ž๊ฐ€ ์ •ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋žต์ ์œผ๋กœ ๊ณ„์‚ฐํ•  ๋•Œ ๊ณต์‹์€ "์ตœ๋Œ€ X GB"์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋ธ์„ float32 ์ •๋ฐ€๋„๋กœ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค๋ฉด ๋” ํฐ ํฌ๊ธฐ์ธ 64GB์˜ VRAM์ด ํ•„์š”ํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +> ๊ฑฐ์˜ ๋ชจ๋“  ๋ชจ๋ธ์ด ์š”์ฆ˜ bfloat16์œผ๋กœ ํ•™์Šต๋˜๋ฏ€๋กœ, [GPU๊ฐ€ bfloat16์„ ์ง€์›](https://discuss.pytorch.org/t/bfloat16-native-support/117155/5)ํ•œ๋‹ค๋ฉด ๋ชจ๋ธ์„ float32 ์ •๋ฐ€๋„๋กœ ์‹คํ–‰ํ•  ์ด์œ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. float32๋กœ ๋Œ๋ฆฌ๋Š” ๋ชจ๋ธ์€ ํ•™์Šตํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ์ •๋ฐ€๋„๋ณด๋‹ค ๋” ๋‚˜์€ ์ถ”๋ก  ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ์–ด๋–ค ์ •๋ฐ€๋„ ํ˜•์‹์œผ๋กœ Hub์— ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, HuggingFace Hub์—์„œ ํ•ด๋‹น ์ฒดํฌํฌ์ธํŠธ config์˜ `"torch_dtype"`์„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค, *์˜ˆ*๋ฅผ ๋“ค์–ด [์—ฌ๊ธฐ](https://huggingface.co/meta-llama/Llama-2-7b-hf/blob/6fdf2e60f86ff2481f2241aaee459f85b5b0bbb9/config.json#L21)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ๋ชจ๋ธ์„ `from_pretrained(..., torch_dtype=...)`๋กœ ๋กœ๋“œํ•  ๋•Œ๋Š” config์— ๋ช…์‹œ๋œ ์ •๋ฐ€๋„ ์œ ํ˜•๊ณผ ๋™์ผํ•œ ์ •๋ฐ€๋„๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. ๋‹จ, ์›๋ž˜ ์œ ํ˜•์ด float32์ธ ๊ฒฝ์šฐ ์ถ”๋ก ์„ ์œ„ํ•ด `float16` ๋˜๋Š” `bfloat16`์„ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด์ œ `flush(...)` ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๊ณ , GPU ๋ฉ”๋ชจ๋ฆฌ์˜ ์ตœ๋Œ€ ํ• ๋‹น๋Ÿ‰์„ ์ •ํ™•ํ•˜๊ฒŒ ์ธก์ •ํ•˜๋„๋ก ํ•ฉ์‹œ๋‹ค. + + +```python +del pipe +del model + +import gc +import torch + +def flush(): + gc.collect() + torch.cuda.empty_cache() + torch.cuda.reset_peak_memory_stats() +``` + +๋‹ค์Œ ์‹คํ—˜์„ ์œ„ํ•ด ๋ฐ”๋กœ ํ˜ธ์ถœํ•ด ๋ด…์‹œ๋‹ค. + +```python +flush() +``` +์ตœ๊ทผ ๋ฒ„์ „์˜ accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” `release_memory()`๋ผ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์†Œ๋“œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from accelerate.utils import release_memory +# ... + +release_memory(model) +``` + +๋งŒ์•ฝ GPU์— 32GB์˜ VRAM์ด ์—†๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ์„ฑ๋Šฅ์— ํฐ ์†์‹ค ์—†์ด 8๋น„ํŠธ ๋˜๋Š” 4๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ฐํ˜€์กŒ์Šต๋‹ˆ๋‹ค(์ฐธ๊ณ : [Dettmers et al.](https://huggingface.co/papers/2208.07339)). ์ตœ๊ทผ์˜ [GPTQ ๋…ผ๋ฌธ](https://huggingface.co/papers/2210.17323) ์—์„œ๋Š” ๋ชจ๋ธ์„ 3๋น„ํŠธ ๋˜๋Š” 2๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•ด๋„ ์„ฑ๋Šฅ ์†์‹ค์ด ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•œ ์ˆ˜์ค€์ž„์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค๐Ÿคฏ. + +๋„ˆ๋ฌด ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค๋ฃจ์ง€ ์•Š๊ณ  ์„ค๋ช…ํ•˜์ž๋ฉด, ์–‘์žํ™”๋Š” ๊ฐ€์ค‘์น˜์˜ ์ •๋ฐ€๋„๋ฅผ ์ค„์ด๋ฉด์„œ ๋ชจ๋ธ์˜ ์ถ”๋ก  ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€๋Šฅํ•œ ํ•œ ์ •ํ™•ํ•˜๊ฒŒ(์ฆ‰, bfloat16๊ณผ ์ตœ๋Œ€ํ•œ ๊ฐ€๊น๊ฒŒ) ์œ ์ง€ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์–‘์žํ™”๋Š” ํŠนํžˆ ํ…์ŠคํŠธ ์ƒ์„ฑ์— ์ž˜ ์ž‘๋™ํ•˜๋Š”๋ฐ, ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ *๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋‹ค์Œ ํ† ํฐ ์ง‘ํ•ฉ*์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์— ์ดˆ์ ์„ ๋‘๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ฉฐ, ๋‹ค์Œ ํ† ํฐ์˜ *logit* ๋ถ„ํฌ๊ฐ’์„ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ํ•„์š”๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ ๋‹ค์Œ ํ† ํฐ *logit* ๋ถ„ํฌ๊ฐ€ ๋Œ€๋žต์ ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋˜์–ด `argmax` ๋˜๋Š” `topk` ์—ฐ์‚ฐ์ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋‹ค์–‘ํ•œ ์–‘์žํ™” ๊ธฐ๋ฒ•์ด ์กด์žฌํ•˜์ง€๋งŒ, ์ž์„ธํžˆ ๋‹ค๋ฃจ์ง€๋Š” ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋“  ์–‘์žํ™” ๊ธฐ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค: + +- 1. ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋ฅผ ๋ชฉํ‘œ ์ •๋ฐ€๋„๋กœ ์–‘์žํ™”ํ•ฉ๋‹ˆ๋‹ค. +- 2. ์–‘์žํ™”๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•˜๊ณ , bfloat16 ์ •๋ฐ€๋„์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ ์‹œํ€€์Šค๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +- 3. ๊ฐ€์ค‘์น˜๋ฅผ ๋™์ ์œผ๋กœ bfloat16์œผ๋กœ ๋ฐ˜๋Œ€๋กœ ์–‘์žํ™”(dequantize)ํ•˜์—ฌ ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ bfloat16 ์ •๋ฐ€๋„๋กœ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ, *์ž…๋ ฅ-๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ* ๊ณฑ์…ˆ์€, \\( X \\)๊ฐ€ *์ž…๋ ฅ*, \\( W \\)๊ฐ€ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ, \\( Y \\)๊ฐ€ ์ถœ๋ ฅ์ธ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +$$ Y = X * W $$ + +์œ„ ๊ณต์‹์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค + +$$ Y = X * \text{dequantize}(W) $$ + +๋ชจ๋“  ํ–‰๋ ฌ ๊ณฑ์…ˆ์— ๋Œ€ํ•ด ์œ„์™€ ๊ฐ™์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์ด ๋„คํŠธ์›Œํฌ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ต๊ณผํ•˜๋ฉด์„œ ๋ชจ๋“  ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์— ๋Œ€ํ•ด ์—ญ์–‘์žํ™”(dequantization)์™€ ์žฌ์–‘์žํ™”(re-quantization)๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ, ์–‘์žํ™”๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ถ”๋ก  ์‹œ๊ฐ„์ด ๊ฐ์†Œํ•˜์ง€ **์•Š๊ณ ** ์˜คํžˆ๋ ค ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ก ์€ ์ถฉ๋ถ„ํ•˜๋‹ˆ ์‹ค์ œ๋กœ ์‹œ๋„ํ•ด ๋ด…์‹œ๋‹ค! Transformers๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์–‘์žํ™”ํ•˜๋ ค๋ฉด [`bitsandbytes`](https://github.com/TimDettmers/bitsandbytes) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```bash +!pip install bitsandbytes +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ `from_pretrained`์— `load_in_8bit=True` ํ”Œ๋ž˜๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ 8๋น„ํŠธ ์–‘์žํ™”๋กœ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +model = AutoModelForCausalLM.from_pretrained("bigcode/octocoder", load_in_8bit=True, pad_token_id=0) +``` + +์ด์ œ ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. + +```python +pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) + +result = pipe(prompt, max_new_tokens=60)[0]["generated_text"][len(prompt):] +result +``` + +**์ถœ๋ ฅ**: +``` +Here is a Python function that transforms bytes to Giga bytes:\n\n```python\ndef bytes_to_giga_bytes(bytes):\n return bytes / 1024 / 1024 / 1024\n```\n\nThis function takes a single +``` + +์ข‹์Šต๋‹ˆ๋‹ค. ์ •ํ™•๋„ ์†์‹ค ์—†์ด ์ด์ „๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ์ด๋ฒˆ์—๋Š” ์‚ฌ์šฉ๋œ ๋ฉ”๋ชจ๋ฆฌ ์–‘์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. + +```python +bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) +``` + +**์ถœ๋ ฅ**: +``` +15.219234466552734 +``` + +ํ›จ์”ฌ ์ ๋„ค์š”! ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด 15GB๋ฅผ ์กฐ๊ธˆ ๋„˜๋Š” ์ˆ˜์ค€์œผ๋กœ ์ค„์–ด๋“ค์–ด 4090๊ณผ ๊ฐ™์€ ์†Œ๋น„์ž์šฉ GPU์—์„œ๋„ ์ด ๋ชจ๋ธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์—์„œ ๋งค์šฐ ํฐ ํ–ฅ์ƒ์„ ๋ณด์ด๊ณ  ์žˆ์œผ๋ฉฐ ๋ชจ๋ธ ์ถœ๋ ฅ์˜ ํ’ˆ์งˆ ์ €ํ•˜๋„ ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ถ”๋ก  ์ค‘์— ์•ฝ๊ฐ„์˜ ์†๋„ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +๋ชจ๋ธ์„ ์‚ญ์ œํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```python +del model +del pipe +``` + +```python +flush() +``` + +์ด์ œ 4๋น„ํŠธ ์–‘์žํ™”๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. 4๋น„ํŠธ๋กœ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋ ค๋ฉด ์ด์ „๊ณผ ๋™์ผํ•œ API๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ์ด๋ฒˆ์—๋Š” `load_in_8bit=True` ๋Œ€์‹  `load_in_4bit=True`๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +```python +model = AutoModelForCausalLM.from_pretrained("bigcode/octocoder", load_in_4bit=True, pad_token_id=0) + +pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) + +result = pipe(prompt, max_new_tokens=60)[0]["generated_text"][len(prompt):] +result +``` + +**์ถœ๋ ฅ**: +``` +Here is a Python function that transforms bytes to Giga bytes:\n\n```\ndef bytes_to_gigabytes(bytes):\n return bytes / 1024 / 1024 / 1024\n```\n\nThis function takes a single argument +``` + +๋ฐ”๋กœ ์ „ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์—์„œ `python`๋งŒ ๋ˆ„๋ฝ๋˜๊ณ , ์ด ์ „๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ์ถœ๋ ฅ ํ…์ŠคํŠธ๋ฅผ ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. + +```python +bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) +``` + +**์ถœ๋ ฅ**: +``` +9.543574333190918 +``` + +9.5GB๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! 150์–ต ๊ฐœ ์ด์ƒ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์ธ ๊ฒƒ์„ ๊ฐ์•ˆํ•˜๋ฉด ๋งค์šฐ ์ ์€ ์–‘์ž…๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋ธ์˜ ์ •ํ™•๋„ ์ €ํ•˜๊ฐ€ ๊ฑฐ์˜ ์—†์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” 4๋น„ํŠธ ์–‘์žํ™”๋ฅผ 8๋น„ํŠธ ์–‘์žํ™”๋‚˜ `bfloat16`๋ฅผ ์‚ฌ์šฉํ•œ ์ถ”๋ก  ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ 4๋น„ํŠธ ์–‘์žํ™”์— ์‚ฌ์šฉ๋œ ๋” ๊ณต๊ฒฉ์ ์ธ ์–‘์žํ™” ๋ฐฉ๋ฒ•์œผ๋กœ ์ธํ•ด ์ถ”๋ก  ์‹œ \\( \text{quantize} \\)์™€ \\( \text{dequantize} \\) ๊ณผ์ •์ด ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋„ 8๋น„ํŠธ ์–‘์žํ™”์™€ ๋น„๊ตํ•˜์—ฌ ์ถ”๋ก  ์†๋„๊ฐ€ ์•ฝ๊ฐ„ ๋А๋ ค์กŒ์Œ์„ ์œ ์˜ํ•˜์„ธ์š”. + +```python +del model +del pipe +``` +```python +flush() +``` + +์ „์ฒด์ ์œผ๋กœ OctoCoder๋ฅผ 8๋น„ํŠธ ์ •๋ฐ€๋„๋กœ ์‹คํ–‰ํ•˜๋ฉด ํ•„์š”ํ•œ GPU VRAM์ด 32GB์—์„œ 15GB๋กœ ์ค„์–ด๋“ค์—ˆ๊ณ , 4๋น„ํŠธ ์ •๋ฐ€๋„๋กœ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋ฉด ํ•„์š”ํ•œ GPU VRAM์ด 9GB๋กœ ๋” ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. + +4๋น„ํŠธ ์–‘์žํ™”๋Š” RTX3090, V100, T4์™€ ๊ฐ™์€ GPU์—์„œ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋ฉฐ, ์ด๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ๋žŒ๋“ค์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” GPU์ž…๋‹ˆ๋‹ค. + +์–‘์žํ™”์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  4๋น„ํŠธ๋ณด๋‹ค ๋” ์ ์€ GPU VRAM ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๊ฑฐ๋‚˜, ๋” ๋งŽ์€ ์–‘์žํ™” ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋ณด๋ ค๋ฉด [`AutoGPTQ`](https://huggingface.co/docs/transformers/main/en/main_classes/quantization#autogptq-integration%60) ๊ตฌํ˜„์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. + +> ๊ฒฐ๋ก ์ ์œผ๋กœ, ๋ชจ๋ธ ์–‘์žํ™”๋Š” ํ–ฅ์ƒ๋œ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ๊ณผ ๋ชจ๋ธ ์ •ํ™•์„ฑ ๊ฐ„์˜ ๊ท ํ˜•์„ ๋งž์ถ”๋Š” ๊ฒƒ์ด๋ฉฐ, ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์ถ”๋ก  ์‹œ๊ฐ„์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‹ค์ œ ์‚ฌ๋ก€์—์„œ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด, ์–‘์žํ™”๋ฅผ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŽ์€ GPU๋Š” ์–‘์žํ™” ์—†์ด ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ 4๋น„ํŠธ ๋ฐ 8๋น„ํŠธ ์–‘์žํ™”๊ฐ€ ๋งค์šฐ ์œ ์šฉํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ๊ณผ ๊ด€๋ จํ•œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋Š” [ํŠธ๋žœ์Šคํฌ๋จธ ์–‘์žํ™” ๋ฌธ์„œ](https://huggingface.co/docs/transformers/main_classes/quantization#general-usage)๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ, ๋” ๋‚˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฐœ์„ ๋œ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## 2. ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜ [[2-flash-attention]] + +์˜ค๋Š˜๋‚ ์˜ ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ์ž๋ž‘ํ•˜๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ๋Œ€์ฒด๋กœ ํ”ผ๋“œํฌ์›Œ๋“œ ๋ ˆ์ด์–ด(feed-forward layer), ํ™œ์„ฑํ™” ๋ ˆ์ด์–ด(activation layer), ๋ ˆ์ด์–ด ์ •๊ทœํ™” ๋ ˆ์ด์–ด(layer normalization layer), ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(self-attention layer)๋กœ ๊ตฌ์„ฑ๋œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ณต์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋Š” ์ž…๋ ฅ ํ† ํฐ ๊ฐ„์˜ ๋ฌธ๋งฅ์  ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•ต์‹ฌ ์š”์†Œ์ž…๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด์˜ ์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋Š” ์ž…๋ ฅ ํ† ํฐ์˜ ์ˆ˜(์ดํ•˜ \\( N \\)์œผ๋กœ ํ‘œ๊ธฐ)์™€ ํ•จ๊ป˜ ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ๋ณต์žก์„ฑ์ด *2์ฐจ์ *์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ์งง์€ ๊ฒฝ์šฐ(์ตœ๋Œ€ 1000๊ฐœ)์—๋Š” ํฌ๊ฒŒ ๋ˆˆ์— ๋„์ง€ ์•Š์ง€๋งŒ, ๋” ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค(์•ฝ 16000๊ฐœ)์—์„œ๋Š” ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + +์ž์„ธํžˆ ํ•œ ๋ฒˆ ๋“ค์—ฌ๋‹ค ๋ด…์‹œ๋‹ค. ๊ธธ์ด \\( N \\)์˜ ์ž…๋ ฅ \\( \mathbf{X} \\)์— ๋Œ€ํ•œ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ \\( \mathbf{O} \\)์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +$$ \textbf{O} = \text{Attn}(\mathbf{X}) = \mathbf{V} \times \text{Softmax}(\mathbf{QK}^T) \text{ with } \mathbf{Q} = \mathbf{W}_q \mathbf{X}, \mathbf{V} = \mathbf{W}_v \mathbf{X}, \mathbf{K} = \mathbf{W}_k \mathbf{X} $$ + +\\( \mathbf{X} = (\mathbf{x}1, ... \mathbf{x}{N}) \\)๋Š” ์–ดํ…์…˜ ๋ ˆ์ด์–ด์˜ ์ž…๋ ฅ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ํ”„๋กœ์ ์…˜ \\( \mathbf{Q} \\)์™€ \\( \mathbf{K} \\)๋Š” ๊ฐ๊ฐ \\( N \\)๊ฐœ์˜ ๋ฒกํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ \\( \mathbf{QK}^T \\)์˜ ํฌ๊ธฐ๋Š” \\( N^2 \\)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์…€ํ”„ ์–ดํ…์…˜ ๊ณ„์‚ฐ์„ ๋ณ‘๋ ฌ๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด 40๊ฐœ์˜ ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  bfloat16 ์ •๋ฐ€๋„๋กœ ์‹คํ–‰๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, \\( \mathbf{QK^T} \\) ํ–‰๋ ฌ์„ ์ €์žฅํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ \\( 40 * 2 * N^2 \\) ๋ฐ”์ดํŠธ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. \\( N=1000 \\)์ผ ๋•Œ๋Š” ์•ฝ 50MB์˜ VRAM๋งŒ ํ•„์š”ํ•˜์ง€๋งŒ, \\( N=16000 \\)์ผ ๋•Œ๋Š” 19GB์˜ VRAM์ด ํ•„์š”ํ•˜๋ฉฐ, \\( N=100,000 \\)์ผ ๋•Œ๋Š” \\( \mathbf{QK^T} \\) ํ–‰๋ ฌ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๊ฑฐ์˜ 1TB์˜ VRAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +์š”์•ฝํ•˜์ž๋ฉด, ๊ธฐ๋ณธ ์…€ํ”„ ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฐ ์ž…๋ ฅ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•ด ๋งค์šฐ ๊ณผ๋„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์š”๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ํ…์ŠคํŠธ ์ดํ•ด ๋ฐ ์ƒ์„ฑ ๋Šฅ๋ ฅ์ด ๊ฐœ์„ ๋˜๋ฉด์„œ ์ ์  ๋” ๋ณต์žกํ•œ ์ž‘์—…์— ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋•Œ ๋ช‡ ๋ฌธ์žฅ์˜ ๋ฒˆ์—ญ์ด๋‚˜ ์š”์•ฝ์„ ์ฒ˜๋ฆฌํ•˜๋˜ ๋ชจ๋ธ์ด ์ด์ œ๋Š” ์ „์ฒด ํŽ˜์ด์ง€๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๊ฒŒ ๋˜๋ฉด์„œ ๊ด‘๋ฒ”์œ„ํ•œ ์ž…๋ ฅ ๊ธธ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ์š”๊ตฌ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ํฐ ์ž…๋ ฅ ๊ธธ์ด์— ๋Œ€ํ•œ ๊ณผ๋„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋ฅผ ์—†์•จ ์ˆ˜ ์žˆ์„๊นŒ์š”? \\( QK^T \\) ํ–‰๋ ฌ์„ ์ œ๊ฑฐํ•˜๋Š” ์ƒˆ๋กœ์šด ์…€ํ”„ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. [Tri Dao et al.](https://huggingface.co/papers/2205.14135)์€ ๋ฐ”๋กœ ์ด๋Ÿฌํ•œ ์ƒˆ๋กœ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜์˜€๊ณ , ๊ทธ๊ฒƒ์ด **ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜(Flash Attention)**์ž…๋‹ˆ๋‹ค. + +๊ฐ„๋‹จํžˆ ๋งํ•ด, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ \\(\mathbf{V} \times \text{Softmax}(\mathbf{QK}^T\\)) ๊ณ„์‚ฐ์„ ๋ถ„ํ• ํ•˜๋Š”๋ฐ, ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์†Œํ”„ํŠธ๋งฅ์Šค ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ž‘์€ ์ฒญํฌ ๋‹จ์œ„๋กœ ์ถœ๋ ฅ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: + +$$ \textbf{O}_i \leftarrow s^a_{ij} * \textbf{O}_i + s^b_{ij} * \mathbf{V}_{j} \times \text{Softmax}(\mathbf{QK}^T_{i,j}) \text{ for multiple } i, j \text{ iterations} $$ + +์—ฌ๊ธฐ์„œ \\( s^a_{ij} \\)์™€ \\( s^b_{ij} \\)๋Š” ๊ฐ \\( i \\)์™€ \\( j \\)์— ๋Œ€ํ•ด ๊ณ„์‚ฐ๋˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ์ •๊ทœํ™” ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์˜ ์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋” ๋ณต์žกํ•˜๋ฉฐ, ๋ณธ ๊ฐ€์ด๋“œ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ๋‹จ์ˆœํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์ž˜ ์ž‘์„ฑ๋œ [Flash Attention paper](https://huggingface.co/papers/2205.14135) ๋…ผ๋ฌธ์„ ์ฐธ์กฐํ•˜์—ฌ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +์ฃผ์š” ์š”์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +> ์†Œํ”„ํŠธ๋งฅ์Šค ์ •๊ทœํ™” ํ†ต๊ณ„๋Ÿ‰๊ณผ ๋ช‡ ๊ฐ€์ง€ ์Šค๋งˆํŠธํ•œ ์ˆ˜ํ•™์  ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ ๊ธฐ๋ณธ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด์™€ **์ˆซ์ž์ ์œผ๋กœ ๋™์ผํ•œ** ์ถœ๋ ฅ์„ ์ œ๊ณตํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๋น„์šฉ์€ \\( N \\)์— ๋”ฐ๋ผ ์„ ํ˜•์ ์œผ๋กœ๋งŒ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +๊ณต์‹์„ ๋ณด๋ฉด, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์ด ๋” ๋งŽ์€ ๊ณ„์‚ฐ์„ ํ•„์š”๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ ์…€ํ”„ ์–ดํ…์…˜ ๊ณต์‹๋ณด๋‹ค ํ›จ์”ฌ ๋А๋ฆด ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ ์†Œํ”„ํŠธ๋งฅ์Šค ์ •๊ทœํ™” ํ†ต๊ณ„๋Ÿ‰์„ ์ง€์†์ ์œผ๋กœ ๋‹ค์‹œ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜ ์–ดํ…์…˜๋ณด๋‹ค ๋” ๋งŽ์€ FLOP์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๋…ผ๋ฌธ](https://huggingface.co/papers/2205.14135)์„ ์ฐธ์กฐํ•˜์„ธ์š”) + +> ๊ทธ๋Ÿฌ๋‚˜ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ ๊ธฐ๋ณธ ์–ดํ…์…˜๋ณด๋‹ค ์ถ”๋ก  ์†๋„๊ฐ€ ํ›จ์”ฌ ๋น ๋ฆ…๋‹ˆ๋‹ค. ์ด๋Š” GPU์˜ ๋А๋ฆฌ๊ณ  ๊ณ ๋Œ€์—ญํญ ๋ฉ”๋ชจ๋ฆฌ(VRAM)์˜ ์‚ฌ์šฉ๋Ÿ‰์„ ํฌ๊ฒŒ ์ค„์ด๊ณ  ๋Œ€์‹  ๋น ๋ฅธ ์˜จ์นฉ ๋ฉ”๋ชจ๋ฆฌ(SRAM)์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋ณธ์งˆ์ ์œผ๋กœ, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์˜ ๋ชจ๋“  ์ค‘๊ฐ„ ๋‹จ๊ณ„์˜ ์“ฐ๊ธฐ ๋ฐ ์ฝ๊ธฐ ์ž‘์—…์€ ๋А๋ฆฐ VRAM ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผํ•˜์ง€ ์•Š๊ณ  ๋น ๋ฅธ *์˜จ์นฉ* SRAM ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ ๋ฒกํ„ฐ \\( \mathbf{O} \\)๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +ํ˜„์‹ค์ ์œผ๋กœ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ **์‚ฌ์šฉํ•˜์ง€ ์•Š์„** ์ด์œ ๋Š” ์ „ํ˜€ ์—†์Šต๋‹ˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ˆ˜ํ•™์ ์œผ๋กœ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋” ๋น ๋ฅด๊ณ  ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. + +์‹ค์ œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +์šฐ๋ฆฌ์˜ OctoCoder ๋ชจ๋ธ์€ ์ด์ œ *์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ*๊ฐ€ ํฌํ•จ๋œ ํ›จ์”ฌ ๋” ๊ธด ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉ์ž์˜ ์ž‘์—…์— ๋งž์ถ˜ ๋” ๋‚˜์€ ์–ด์‹œ์Šคํ„ดํŠธ๋กœ ์œ ๋„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” OctoCoder๋ฅผ ๋” ๋‚˜์€ ์ฝ”๋”ฉ ์–ด์‹œ์Šคํ„ดํŠธ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```python +system_prompt = """Below are a series of dialogues between various people and an AI technical assistant. +The assistant tries to be helpful, polite, honest, sophisticated, emotionally aware, and humble but knowledgeable. +The assistant is happy to help with code questions and will do their best to understand exactly what is needed. +It also tries to avoid giving false or misleading information, and it caveats when it isn't entirely sure about the right answer. +That said, the assistant is practical really does its best, and doesn't let caution get too much in the way of being useful. + +The Starcoder models are a series of 15.5B parameter models trained on 80+ programming languages from The Stack (v1.2) (excluding opt-out requests). +The model uses Multi Query Attention, was trained using the Fill-in-the-Middle objective, and with 8,192 tokens context window for a trillion tokens of heavily deduplicated data. + +----- + +Question: Write a function that takes two lists and returns a list that has alternating elements from each input list. + +Answer: Sure. Here is a function that does that. + +def alternating(list1, list2): + results = [] + for i in range(len(list1)): + results.append(list1[i]) + results.append(list2[i]) + return results + +Question: Can you write some test cases for this function? + +Answer: Sure, here are some tests. + +assert alternating([10, 20, 30], [1, 2, 3]) == [10, 1, 20, 2, 30, 3] +assert alternating([True, False], [4, 5]) == [True, 4, False, 5] +assert alternating([], []) == [] + +Question: Modify the function so that it returns all input elements when the lists have uneven length. The elements from the longer list should be at the end. + +Answer: Here is the modified function. + +def alternating(list1, list2): + results = [] + for i in range(min(len(list1), len(list2))): + results.append(list1[i]) + results.append(list2[i]) + if len(list1) > len(list2): + results.extend(list1[i+1:]) + else: + results.extend(list2[i+1:]) + return results + +----- +""" +``` +์‹œ์—ฐ์„ ์œ„ํ•ด ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ 10๋ฒˆ ์ค‘๋ณตํ•˜์—ฌ ์ฆ๊ฐ€์‹œ์ผœ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์˜ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ ํšจ๊ณผ๋ฅผ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ž…๋ ฅ ๊ธธ์ด๋ฅผ ์ถฉ๋ถ„ํžˆ ๊ธธ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์›๋ž˜์˜ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. `"Question: Please write a function in Python that transforms bytes to Giga bytes.\n\nAnswer: Here"` + +```python +long_prompt = 10 * system_prompt + prompt +``` + +๋ชจ๋ธ์„ ๋‹ค์‹œ bfloat16 ์ •๋ฐ€๋„๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```python +model = AutoModelForCausalLM.from_pretrained("bigcode/octocoder", torch_dtype=torch.bfloat16, device_map="auto") +tokenizer = AutoTokenizer.from_pretrained("bigcode/octocoder") + +pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) +``` + +์ด์ œ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ *์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ * ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜์—ฌ ์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰๊ณผ ์ถ”๋ก  ์‹œ๊ฐ„์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. + +```python +import time + +start_time = time.time() +result = pipe(long_prompt, max_new_tokens=60)[0]["generated_text"][len(long_prompt):] + +print(f"Generated in {time.time() - start_time} seconds.") +result +``` + +**์ถœ๋ ฅ**: +``` +Generated in 10.96854019165039 seconds. +Sure. Here is a function that does that.\n\ndef bytes_to_giga(bytes):\n return bytes / 1024 / 1024 / 1024\n\nAnswer: Sure. Here is a function that does that.\n\ndef +```` + +์ด์ „๊ณผ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ์–ป๊ณ  ์žˆ์ง€๋งŒ, ์ด๋ฒˆ์—๋Š” ๋ชจ๋ธ์ด ๋‹ต๋ณ€์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณตํ•˜์—ฌ 60๊ฐœ์˜ ํ† ํฐ์ด ์ž˜๋ฆด ๋•Œ๊นŒ์ง€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค. ์‹œ์—ฐ์„ ์œ„ํ•ด ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ 10๋ฒˆ ๋ฐ˜๋ณตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์ด ์Šค์Šค๋กœ ๋ฐ˜๋ณตํ•˜๋„๋ก ์œ ๋„ํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋†€๋ผ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. + +**์ฐธ๊ณ ** ์‹ค์ œ ์‘์šฉ์—์„œ๋Š” ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ๋ฅผ 10๋ฒˆ ๋ฐ˜๋ณตํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค! + +์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. + +```python +bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) +``` + +**์ถœ๋ ฅ**: +```bash +37.668193340301514 +``` + +๋ณด์‹œ๋‹ค์‹œํ”ผ ์ตœ๋Œ€ GPU ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰์ด ์ฒ˜์Œ๋ณด๋‹ค ์ƒ๋‹นํžˆ ๋†’์•„์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ฃผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ๊ธธ์–ด์กŒ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒ์„ฑ ์‹œ๊ฐ„์ด ์ด์ œ 1๋ถ„์„ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. + +๋‹ค์Œ ์‹คํ—˜์„ ์œ„ํ•ด `flush()`๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```python +flush() +``` + +๋น„๊ต๋ฅผ ์œ„ํ•ด, ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋˜ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ํ™œ์„ฑํ™”ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +์ด๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ์„ [BetterTransformer](https://huggingface.co/docs/optimum/bettertransformer/overview)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด PyTorch์˜ [SDPA self-attention](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention)์„ ํ™œ์„ฑํ™”ํ•˜๋ฉด ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +model.to_bettertransformer() +``` + +์ด์ œ ์ด์ „๊ณผ ๋™์ผํ•œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ์‹คํ–‰ํ•˜๋ฉด, ๋‚ด๋ถ€์ ์œผ๋กœ Transformers๊ฐ€ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```py +start_time = time.time() +with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False): + result = pipe(long_prompt, max_new_tokens=60)[0]["generated_text"][len(long_prompt):] + +print(f"Generated in {time.time() - start_time} seconds.") +result +``` + +**์ถœ๋ ฅ**: +``` +Generated in 3.0211617946624756 seconds. + Sure. Here is a function that does that.\n\ndef bytes_to_giga(bytes):\n return bytes / 1024 / 1024 / 1024\n\nAnswer: Sure. Here is a function that does that.\n\ndef +``` + +์ด์ „๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ์ง€๋งŒ, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜ ๋•๋ถ„์— ๋งค์šฐ ํฐ ์†๋„ ํ–ฅ์ƒ์„ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋Ÿ‰์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•œ ๋ฒˆ ๋” ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. + +```python +bytes_to_giga_bytes(torch.cuda.max_memory_allocated()) +``` + +**์ถœ๋ ฅ**: +``` +32.617331981658936 +``` + +๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ์ฒ˜์Œ์— ๋ณด์•˜๋˜ GPU ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰์ธ 29GB๋กœ ๋Œ์•„์™”์Šต๋‹ˆ๋‹ค. + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งค์šฐ ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์ฒ˜์Œ์— ์งง์€ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ „๋‹ฌํ–ˆ์„ ๋•Œ์™€ ๋น„๊ตํ•˜์—ฌ ์•ฝ 100MB ์ •๋„์˜ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +flush() +``` + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜ ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” [์ด ๋ฌธ์„œ ํŽ˜์ด์ง€](https://huggingface.co/docs/transformers/en/perf_infer_gpu_one#flashattention-2)๋ฅผ ์ฐธ์กฐํ•ด ์ฃผ์„ธ์š”. + +## 3. ์•„ํ‚คํ…์ฒ˜ ํ˜์‹  [[3-architectural-innovations]] + +์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค: + +- ๊ฐ€์ค‘์น˜๋ฅผ ๋‚ฎ์€ ์ •๋ฐ€๋„ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ +- ์…€ํ”„ ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณด๋‹ค ๋” ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ๊ณ„์‚ฐ ํšจ์œจ์ ์ธ ๋ฒ„์ „์œผ๋กœ ๊ต์ฒด + +์ด์ œ ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ์ด ํ•„์š”ํ•œ ์ž‘์—…์— ๊ฐ€์žฅ ํšจ๊ณผ์ ์ด๊ณ  ํšจ์œจ์ ์ธ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž‘์—…์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: +- ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์งˆ์˜ ์‘๋‹ต +- ์š”์•ฝ +- ์ฑ„ํŒ… + +*์ฑ„ํŒ…*์„ ์œ„ํ•ด์„œ๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž์™€ ์–ด์‹œ์Šคํ„ดํŠธ ๊ฐ„์˜ ๋Œ€ํ™”๋„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: ChatGPT). + +ํ•œ๋ฒˆ ํ•™์Šต๋œ ํ›„์—๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์—, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ž‘์—…์— ๋Œ€ํ•œ ๊ณ ๋ ค๋ฅผ ๋ฏธ๋ฆฌ ํ•˜๊ณ  ์ด์— ๋”ฐ๋ผ ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค์— ๋Œ€ํ•ด ๋ฉ”๋ชจ๋ฆฌ ๋˜๋Š” ์„ฑ๋Šฅ์˜ ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ๋น ๋ฅด๊ฒŒ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์˜ ์ค‘์š”ํ•œ ๋‘ ๊ฐ€์ง€ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +- ์œ„์น˜ ์ž„๋ฒ ๋”ฉ +- ํ‚ค-๊ฐ’ ์บ์‹œ + +๊ฐ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +### 3.1 ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ ๊ฐœ์„  [[31-improving-positional-embeddings-of-llms]] + +์…€ํ”„ ์–ดํ…์…˜์€ ๊ฐ ํ† ํฐ์„ ์„œ๋กœ์˜ ํ† ํฐ๊ณผ ์—ฐ๊ด€์‹œํ‚ต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, ํ…์ŠคํŠธ ์ž…๋ ฅ ์‹œํ€€์Šค *"Hello", "I", "love", "you"*์˜ \\( \text{Softmax}(\mathbf{QK}^T) \\) ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +![](/blog/assets/163_optimize_llm/self_attn_tokens.png) + +๊ฐ ๋‹จ์–ด ํ† ํฐ์€ ๋‹ค๋ฅธ ๋ชจ๋“  ๋‹จ์–ด ํ† ํฐ์— ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๋Š” ํ™•๋ฅ  ์งˆ๋Ÿ‰์„ ๋ถ€์—ฌ๋ฐ›์•„ ๋ชจ๋“  ๋‹ค๋ฅธ ๋‹จ์–ด ํ† ํฐ๊ณผ ๊ด€๊ณ„๋ฅผ ๋งบ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹จ์–ด *"love"*๋Š” ๋‹จ์–ด *"Hello"*์— 5%, *"I"*์— 30%, ๊ทธ๋ฆฌ๊ณ  ์ž์‹ ์—๊ฒŒ 65%์˜ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ž…๋‹ˆ๋‹ค. + +์…€ํ”„ ์–ดํ…์…˜ ๊ธฐ๋ฐ˜ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ์—†๋Š” ๊ฒฝ์šฐ ํ…์ŠคํŠธ ์ž…๋ ฅ์˜ ์œ„์น˜๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ํฐ ์–ด๋ ค์›€์„ ๊ฒช์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” \\( \mathbf{QK}^T \\)์— ์˜ํ•ด ๊ณ„์‚ฐ๋œ ํ™•๋ฅ  ์ ์ˆ˜๊ฐ€ ์ƒ๋Œ€์  ์œ„์น˜ ๊ฑฐ๋ฆฌ์— ์ƒ๊ด€์—†์ด ๊ฐ ๋‹จ์–ด ํ† ํฐ์„ ๋‹ค๋ฅธ ๋ชจ๋“  ๋‹จ์–ด ํ† ํฐ๊ณผ \\( O(1) \\) ๊ณ„์‚ฐ์œผ๋กœ ์—ฐ๊ด€์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ์—†๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ๊ฐ ํ† ํฐ์ด ๋‹ค๋ฅธ ๋ชจ๋“  ํ† ํฐ๊ณผ ๋™์ผํ•œ ๊ฑฐ๋ฆฌ์— ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๊ธฐ ๋•Œ๋ฌธ์—, *"Hello I love you"*์™€ *"You love I hello"*๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๋ฌธ์žฅ์˜ ์ˆœ์„œ๋ฅผ ์ดํ•ดํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ *๋‹จ์„œ*๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ *์œ„์น˜ ์ธ์ฝ”๋”ฉ* (๋˜๋Š” *์œ„์น˜ ์ž„๋ฒ ๋”ฉ*์ด๋ผ๊ณ ๋„ ํ•จ)์˜ ํ˜•ํƒœ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. +์œ„์น˜ ์ธ์ฝ”๋”ฉ์€ ๊ฐ ํ† ํฐ์˜ ์œ„์น˜๋ฅผ ์ˆซ์ž ํ‘œํ˜„์œผ๋กœ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๋ฌธ์žฅ์˜ ์ˆœ์„œ๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. + +[*Attention Is All You Need*](https://huggingface.co/papers/1706.03762) ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ์‚ฌ์ธ ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜์˜ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ \\( \mathbf{P} = \mathbf{p}_1, \ldots, \mathbf{p}_N \\)์„ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฒกํ„ฐ \\( \mathbf{p}_i \\)๋Š” ์œ„์น˜ \\( i \\)์˜ ์‚ฌ์ธ ํ•จ์ˆ˜๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์œ„์น˜ ์ธ์ฝ”๋”ฉ์€ ์ž…๋ ฅ ์‹œํ€€์Šค ๋ฒกํ„ฐ์— ๋‹จ์ˆœํžˆ ๋”ํ•ด์ ธ \\( \mathbf{\hat{X}} = \mathbf{\hat{x}}_1, \ldots, \mathbf{\hat{x}}_N \\) = \\( \mathbf{x}_1 + \mathbf{p}_1, \ldots, \mathbf{x}_N + \mathbf{p}_N \\) ๋ชจ๋ธ์ด ๋ฌธ์žฅ ์ˆœ์„œ๋ฅผ ๋” ์ž˜ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +๊ณ ์ •๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ ๋Œ€์‹  [Devlin et al.](https://huggingface.co/papers/1810.04805)๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์—ฐ๊ตฌ์ž๋“ค์€ ํ•™์Šต๋œ ์œ„์น˜ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ \\( \mathbf{P} \\)์€ ํ•™์Šต ์ค‘์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +์‚ฌ์ธ ํ•จ์ˆ˜ ๋ฐ ํ•™์Šต๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์€ ๋ฌธ์žฅ ์ˆœ์„œ๋ฅผ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์— ์ธ์ฝ”๋”ฉํ•˜๋Š” ์ฃผ์š” ๋ฐฉ๋ฒ•์ด์—ˆ์ง€๋งŒ, ์ด๋Ÿฌํ•œ ์œ„์น˜ ์ธ์ฝ”๋”ฉ๊ณผ ๊ด€๋ จ๋œ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: + + 1. ์‚ฌ์ธ ํ•จ์ˆ˜์™€ ํ•™์Šต๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์€ ๋ชจ๋‘ ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์œผ๋กœ, ๊ฐ ์œ„์น˜ ID \\( 0, \ldots, N \\)์— ๋Œ€ํ•ด ๊ณ ์œ ํ•œ ์ž„๋ฒ ๋”ฉ์„ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. [Huang et al.](https://huggingface.co/papers/2009.13658) ๋ฐ [Su et al.](https://huggingface.co/papers/2104.09864)์˜ ์—ฐ๊ตฌ์— ๋”ฐ๋ฅด๋ฉด, ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์€ ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋ฉ๋‹ˆ๋‹ค. ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์ด ์ ˆ๋Œ€ ์œ„์น˜ ๋Œ€์‹  ์ž…๋ ฅ ํ† ํฐ ๊ฐ„์˜ ์ƒ๋Œ€์  ์œ„์น˜ ๊ฑฐ๋ฆฌ๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + 2. ํ•™์Šต๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•  ๋•Œ, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ๊ณ ์ •๋œ ์ž…๋ ฅ ๊ธธ์ด \\( N \\)์œผ๋กœ ํ•™์Šต๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, ํ•™์Šต๋œ ์ž…๋ ฅ ๊ธธ์ด๋ณด๋‹ค ๋” ๊ธด ์ž…๋ ฅ ๊ธธ์ด์— ๋Œ€ํ•ด ์ถ”๋ก ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. + +์ตœ๊ทผ์—๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ๋Œ€์  ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ๋” ์ธ๊ธฐ๋ฅผ ๋Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์ด ์ฃผ๋ชฉ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: + +- [Rotary Position Embedding (RoPE)](https://huggingface.co/papers/2104.09864) +- [ALiBi](https://huggingface.co/papers/2108.12409) + +*RoPE*์™€ *ALiBi*๋Š” ๋ชจ๋‘ ์…€ํ”„ ์–ดํ…์…˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‚ด์—์„œ ์ง์ ‘์ ์œผ๋กœ ๋ฌธ์žฅ ์ˆœ์„œ๋ฅผ ๋ชจ๋ธ์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด ์ตœ์„ ์ด๋ผ๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์–ด ํ† ํฐ์ด ์„œ๋กœ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ๊ณณ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ, ๋ฌธ์žฅ ์ˆœ์„œ๋ฅผ \\( \mathbf{QK}^T \\) ๊ณ„์‚ฐ์„ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋„ˆ๋ฌด ๋งŽ์€ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๋‹ค๋ฃจ์ง€ ์•Š๊ณ , *RoPE*๋Š” ์œ„์น˜ ์ •๋ณด๋ฅผ ์ฟผ๋ฆฌ-ํ‚ค ์Œ์— ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ง€์ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ ๋ฒกํ„ฐ \\( \mathbf{q}_i \\)์™€ \\( \mathbf{x}_j \\)๋ฅผ ๊ฐ๊ฐ \\( \theta * i \\)์™€ \\( \theta * j \\)์˜ ๊ฐ๋„๋กœ ํšŒ์ „์‹œํ‚ด์œผ๋กœ์จ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +$$ \mathbf{\hat{q}}_i^T \mathbf{\hat{x}}_j = \mathbf{{q}}_i^T \mathbf{R}_{\theta, i -j} \mathbf{{x}}_j. $$ + +์—ฌ๊ธฐ์„œ \\( \mathbf{R}_{\theta, i - j} \\)๋Š” ํšŒ์ „ ํ–‰๋ ฌ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. \\( \theta \\)๋Š” ํ›ˆ๋ จ ์ค‘์— *ํ•™์Šต๋˜์ง€ ์•Š์œผ๋ฉฐ*, ๋Œ€์‹  ํ•™์Šต ์ค‘ ์ตœ๋Œ€ ์ž…๋ ฅ ์‹œํ€€์Šค ๊ธธ์ด์— ๋”ฐ๋ผ ์‚ฌ์ „ ์ •์˜๋œ ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. + +> ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ \\( \mathbf{q}_i \\)์™€ \\( \mathbf{q}_j \\) ๊ฐ„์˜ ํ™•๋ฅ  ์ ์ˆ˜๋Š” \\( i \ne j \\)์ธ ๊ฒฝ์šฐ์—๋งŒ ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฉฐ, ๊ฐ ๋ฒกํ„ฐ์˜ ํŠน์ • ์œ„์น˜ \\( i \\)์™€ \\( j \\)์™€๋Š” ์ƒ๊ด€์—†์ด ์˜ค์ง ์ƒ๋Œ€์  ๊ฑฐ๋ฆฌ \\( i - j \\)์—๋งŒ ์˜์กดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +*RoPE*๋Š” ํ˜„์žฌ ์—ฌ๋Ÿฌ ์ค‘์š”ํ•œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด: + +- [**Falcon**](https://huggingface.co/tiiuae/falcon-40b) +- [**Llama**](https://huggingface.co/papers/2302.13971) +- [**PaLM**](https://huggingface.co/papers/2204.02311) + +๋Œ€์•ˆ์œผ๋กœ, *ALiBi*๋Š” ํ›จ์”ฌ ๋” ๊ฐ„๋‹จํ•œ ์ƒ๋Œ€์  ์œ„์น˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ํ† ํฐ ๊ฐ„์˜ ์ƒ๋Œ€์  ๊ฑฐ๋ฆฌ๋ฅผ ์Œ์ˆ˜์ธ ์ •์ˆ˜๋กœ์„œ ์‚ฌ์ „ ์ •์˜๋œ ๊ฐ’ `m`์œผ๋กœ ์Šค์ผ€์ผ๋งํ•˜์—ฌ \\( \mathbf{QK}^T \\) ํ–‰๋ ฌ์˜ ๊ฐ ์ฟผ๋ฆฌ-ํ‚ค ํ•ญ๋ชฉ์— ์†Œํ”„ํŠธ๋งฅ์Šค ๊ณ„์‚ฐ ์ง์ „์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +![](/blog/assets/163_optimize_llm/alibi.png) + +[ALiBi](https://huggingface.co/papers/2108.12409) ๋…ผ๋ฌธ์—์„œ ๋ณด์—ฌ์ฃผ๋“ฏ์ด, ์ด ๊ฐ„๋‹จํ•œ ์ƒ๋Œ€์  ์œ„์น˜ ์ธ์ฝ”๋”ฉ์€ ๋งค์šฐ ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ ์‹œํ€€์Šค์—์„œ๋„ ๋ชจ๋ธ์ด ๋†’์€ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +*ALiBi*๋Š” ํ˜„์žฌ ์—ฌ๋Ÿฌ ์ค‘์š”ํ•œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ๋ชจ๋ธ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด: + +- [**MPT**](https://huggingface.co/mosaicml/mpt-30b) +- [**BLOOM**](https://huggingface.co/bigscience/bloom) + +*RoPE*์™€ *ALiBi* ์œ„์น˜ ์ธ์ฝ”๋”ฉ์€ ๋ชจ๋‘ ํ•™์Šต ์ค‘์— ๋ณด์ง€ ๋ชปํ•œ ์ž…๋ ฅ ๊ธธ์ด์— ๋Œ€ํ•ด ํ™•์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, *ALiBi*๊ฐ€ *RoPE*๋ณด๋‹ค ๋” ์ž˜ ํ™•์žฅ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. *ALiBi*์˜ ๊ฒฝ์šฐ, ํ•˜์‚ผ๊ฐ ์œ„์น˜ ํ–‰๋ ฌ์˜ ๊ฐ’์„ ์ž…๋ ฅ ์‹œํ€€์Šค ๊ธธ์ด์— ๋งž์ถ”์–ด ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. *RoPE*์˜ ๊ฒฝ์šฐ, ํ•™์Šต ์ค‘์— ์‚ฌ์šฉ๋œ ๋™์ผํ•œ \\( \theta \\)๋ฅผ ์œ ์ง€ํ•˜๋ฉด ํ•™์Šต ์ค‘์— ๋ณด์ง€ ๋ชปํ•œ ๋งค์šฐ ๊ธด ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ „๋‹ฌํ•  ๋•Œ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋ฉ๋‹ˆ๋‹ค(์ฐธ๊ณ : [Press et al.](https://huggingface.co/papers/2108.12409)). ๊ทธ๋Ÿฌ๋‚˜ ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” \\( \theta \\)๋ฅผ ์กฐ์ •ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ํšจ๊ณผ์ ์ธ ํŠธ๋ฆญ์„ ์ฐพ์•„๋ƒˆ์œผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด *RoPE* ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ํ™•์žฅ๋œ ํ…์ŠคํŠธ ์ž…๋ ฅ ์‹œํ€€์Šค์—์„œ๋„ ์ž˜ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค(์ฐธ๊ณ : [here](https://github.com/huggingface/transformers/pull/24653)). + +> RoPE์™€ ALiBi๋Š” ๋ชจ๋‘ ํ›ˆ๋ จ ์ค‘์— *ํ•™์Šต๋˜์ง€ ์•Š๋Š”* ์ƒ๋Œ€์  ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ง๊ด€์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค: + - ํ…์ŠคํŠธ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ„์น˜ ๋‹จ์„œ๋Š” ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด์˜ \\( QK^T \\) ํ–‰๋ ฌ์— ์ง์ ‘ ์ œ๊ณต๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + - ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์ผ์ •ํ•œ *์ƒ๋Œ€์ * ๊ฑฐ๋ฆฌ ์œ„์น˜ ์ธ์ฝ”๋”ฉ์„ ์„œ๋กœ ํ•™์Šตํ•˜๋„๋ก ์œ ๋„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + - ํ…์ŠคํŠธ ์ž…๋ ฅ ํ† ํฐ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ€์–ด์งˆ์ˆ˜๋ก, ๊ทธ๋“ค์˜ ์ฟผ๋ฆฌ-๊ฐ’ ํ™•๋ฅ ์€ ๋‚ฎ์•„์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. RoPE์™€ ALiBi๋Š” ์„œ๋กœ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ํ† ํฐ์˜ ์ฟผ๋ฆฌ-ํ‚ค ํ™•๋ฅ ์„ ๋‚ฎ์ถฅ๋‹ˆ๋‹ค. RoPE๋Š” ์ฟผ๋ฆฌ-ํ‚ค ๋ฒกํ„ฐ ๊ฐ„์˜ ๊ฐ๋„๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ๋ฒกํ„ฐ ๊ณฑ์„ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ, ALiBi๋Š” ๋ฒกํ„ฐ ๊ณฑ์— ํฐ ์Œ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +๊ฒฐ๋ก ์ ์œผ๋กœ, ํฐ ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์— ๋ฐฐํฌ๋  ์˜ˆ์ •์ธ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ RoPE์™€ ALiBi์™€ ๊ฐ™์€ ์ƒ๋Œ€์  ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์œผ๋กœ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ RoPE์™€ ALiBi๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๊ณ ์ • ๊ธธ์ด \\( N_1 = 2048 \\)์—์„œ๋งŒ ํ›ˆ๋ จ๋˜์—ˆ๋”๋ผ๋„ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์™ธ์‚ฝํ•˜์—ฌ \\( N_1 \\)๋ณด๋‹ค ํ›จ์”ฌ ํฐ ํ…์ŠคํŠธ ์ž…๋ ฅ \\( N_2 = 8192 > N_1 \\)๋กœ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์œ ์˜ํ•˜์„ธ์š”. + +### 3.2 ํ‚ค-๊ฐ’ ์บ์‹œ [[32-the-key-value-cache]] + +๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ์ด์šฉํ•œ ์ž๊ธฐํšŒ๊ท€ ํ…์ŠคํŠธ ์ƒ์„ฑ์€ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋„ฃ๊ณ , ๋‹ค์Œ ํ† ํฐ์„ ์ƒ˜ํ”Œ๋งํ•˜๋ฉฐ, ๊ทธ ๋‹ค์Œ ํ† ํฐ์„ ์ž…๋ ฅ ์‹œํ€€์Šค์— ์ถ”๊ฐ€ํ•˜๊ณ , ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ์ƒ์„ฑ์„ ์™„๋ฃŒํ–ˆ๋‹ค๋Š” ํ† ํฐ์„ ์ƒ์„ฑํ•  ๋•Œ๊นŒ์ง€ ์ด๋ฅผ ๊ณ„์† ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์‹œ๊ฐ์  ์„ค๋ช…์„ ๋ณด๋ ค๋ฉด [Transformer's Generate Text Tutorial](https://huggingface.co/docs/transformers/llm_tutorial#generate-text)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์ด ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด์—ฌ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” `torch.argmax`๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋‹ค์Œ ํ† ํฐ์„ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```python +input_ids = tokenizer(prompt, return_tensors="pt")["input_ids"].to("cuda") + +for _ in range(5): + next_logits = model(input_ids)["logits"][:, -1:] + next_token_id = torch.argmax(next_logits,dim=-1) + + input_ids = torch.cat([input_ids, next_token_id], dim=-1) + print("shape of input_ids", input_ids.shape) + +generated_text = tokenizer.batch_decode(input_ids[:, -5:]) +generated_text +``` + +**์ถœ๋ ฅ**: +``` +shape of input_ids torch.Size([1, 21]) +shape of input_ids torch.Size([1, 22]) +shape of input_ids torch.Size([1, 23]) +shape of input_ids torch.Size([1, 24]) +shape of input_ids torch.Size([1, 25]) +[' Here is a Python function'] +``` + +๋ณด์‹œ๋‹ค์‹œํ”ผ ์ƒ˜ํ”Œ๋ง๋œ ํ† ํฐ์— ์˜ํ•ด ํ…์ŠคํŠธ ์ž…๋ ฅ ํ† ํฐ์„ ๋งค๋ฒˆ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. + +๋งค์šฐ ์˜ˆ์™ธ์ ์ธ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ , ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ [์ธ๊ณผ์ ์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง ๋ชฉํ‘œ](https://huggingface.co/docs/transformers/tasks/language_modeling#causal-language-modeling)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋˜๋ฏ€๋กœ ์–ดํ…์…˜ ์ ์ˆ˜์˜ ์ƒ์‚ผ๊ฐ ํ–‰๋ ฌ์„ ๋งˆ์Šคํ‚นํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์œ„์˜ ๋‘ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ์–ดํ…์…˜ ์ ์ˆ˜๊ฐ€ ๋น„์–ด ์žˆ๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค (์ฆ‰, 0 ํ™•๋ฅ ์„ ๊ฐ€์ง). ์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง์— ๋Œ€ํ•œ ๋น ๋ฅธ ์š”์•ฝ์€ [*Illustrated Self Attention ๋ธ”๋กœ๊ทธ*](https://jalammar.github.io/illustrated-gpt2/#part-2-illustrated-self-attention)๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฒฐ๊ณผ์ ์œผ๋กœ, ํ† ํฐ์€ *์ ˆ๋Œ€* ์ด์ „ ํ† ํฐ์— ์˜์กดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋” ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” \\( \mathbf{q}_i \\) ๋ฒกํ„ฐ๊ฐ€ \\( j > i \\)์ธ ๊ฒฝ์šฐ ์–ด๋–ค ํ‚ค, ๊ฐ’ ๋ฒกํ„ฐ \\( \mathbf{k}_j, \mathbf{v}j \\)์™€๋„ ์—ฐ๊ด€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  \\( \mathbf{q}i \\)๋Š” ์ด์ „์˜ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ \\( \mathbf{k}{m < i}, \mathbf{v}{m < i} \text{ , for } m \in {0, \ldots i - 1} \\)์—๋งŒ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ž…๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๊ฐ ์ธต์˜ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๋ฅผ ๋ชจ๋“  ์ด์ „ ์‹œ๊ฐ„ ๋‹จ๊ณ„์— ๋Œ€ํ•ด ์บ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์œผ๋กœ, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๊ฐ ํฌ์›Œ๋“œ ํŒจ์Šค๋งˆ๋‹ค ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ „๋‹ฌํ•˜์—ฌ ์ด๋ฅผ ํ™œ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. +Transformers์—์„œ๋Š” `forward` ํ˜ธ์ถœ์— `use_cache` ํ”Œ๋ž˜๊ทธ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ๊ฒ€์ƒ‰ํ•œ ๋‹ค์Œ ํ˜„์žฌ ํ† ํฐ๊ณผ ํ•จ๊ป˜ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +past_key_values = None # past_key_values ๋Š” ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์˜๋ฏธ +generated_tokens = [] +next_token_id = tokenizer(prompt, return_tensors="pt")["input_ids"].to("cuda") + +for _ in range(5): + next_logits, past_key_values = model(next_token_id, past_key_values=past_key_values, use_cache=True).to_tuple() + next_logits = next_logits[:, -1:] + next_token_id = torch.argmax(next_logits, dim=-1) + + print("shape of input_ids", next_token_id.shape) + print("length of key-value cache", len(past_key_values[0][0])) # past_key_values ํ˜•ํƒœ: [num_layers, 0 for k, 1 for v, batch_size, length, hidden_dim] + generated_tokens.append(next_token_id.item()) + +generated_text = tokenizer.batch_decode(generated_tokens) +generated_text +``` + +**์ถœ๋ ฅ**: +``` +shape of input_ids torch.Size([1, 1]) +length of key-value cache 20 +shape of input_ids torch.Size([1, 1]) +length of key-value cache 21 +shape of input_ids torch.Size([1, 1]) +length of key-value cache 22 +shape of input_ids torch.Size([1, 1]) +length of key-value cache 23 +shape of input_ids torch.Size([1, 1]) +length of key-value cache 24 +[' Here', ' is', ' a', ' Python', ' function'] +``` + +ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํ…์ŠคํŠธ ์ž…๋ ฅ ํ† ํฐ์˜ ๊ธธ์ด๋Š” *์ฆ๊ฐ€ํ•˜์ง€ ์•Š๊ณ * ๋‹จ์ผ ์ž…๋ ฅ ๋ฒกํ„ฐ๋กœ ์œ ์ง€๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— ํ‚ค-๊ฐ’ ์บ์‹œ์˜ ๊ธธ์ด๋Š” ๊ฐ ๋””์ฝ”๋”ฉ ๋‹จ๊ณ„๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +> ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด \\( \mathbf{QK}^T \\)๊ฐ€ ๋ณธ์งˆ์ ์œผ๋กœ \\( \mathbf{q}_c\mathbf{K}^T \\)๋กœ ์ค„์–ด๋“œ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ \\( \mathbf{q}_c \\)๋Š” ํ˜„์žฌ ์ „๋‹ฌ๋œ ์ž…๋ ฅ ํ† ํฐ์˜ ์ฟผ๋ฆฌ ํ”„๋กœ์ ์…˜์œผ๋กœ, *ํ•ญ์ƒ* ๋‹จ์ผ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. + +ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์—๋Š” ๋‘ ๊ฐ€์ง€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค: +- ์ „์ฒด \\( \mathbf{QK}^T \\) ํ–‰๋ ฌ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ณ„์‚ฐ ํšจ์œจ์„ฑ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ถ”๋ก  ์†๋„์˜ ์ฆ๊ฐ€๋กœ ์ด์–ด์ง‘๋‹ˆ๋‹ค. +- ์ƒ์„ฑ๋œ ํ† ํฐ ์ˆ˜์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ์ตœ๋Œ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ด์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜์ง€ ์•Š๊ณ , ์„ ํ˜•์ ์œผ๋กœ๋งŒ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +> ๋” ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฒฐ๊ณผ์™€ ํฐ ์†๋„ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ *ํ•ญ์ƒ* ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Transformers๋Š” ํ…์ŠคํŠธ ํŒŒ์ดํ”„๋ผ์ธ์ด๋‚˜ [`generate` ๋ฉ”์„œ๋“œ](https://huggingface.co/docs/transformers/main_classes/text_generation)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. + + + +์ฐธ๊ณ ๋กœ, ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•˜์ง€๋งŒ, ์ด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ LLM ์ถœ๋ ฅ์ด ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ–‰๋ ฌ ๊ณฑ์…ˆ ์ปค๋„ ์ž์ฒด์˜ ํŠน์„ฑ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค -- ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/issues/25420#issuecomment-1775317535)์—์„œ ์ฝ์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +#### 3.2.1 ๋ฉ€ํ‹ฐ ๋ผ์šด๋“œ ๋Œ€ํ™” [[321-multi-round-conversation]] + +ํ‚ค-๊ฐ’ ์บ์‹œ๋Š” ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์ž๊ธฐํšŒ๊ท€ ๋””์ฝ”๋”ฉ์ด ํ•„์š”ํ•œ ์ฑ„ํŒ…๊ณผ ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +``` +User: How many people live in France? +Assistant: Roughly 75 million people live in France +User: And how many are in Germany? +Assistant: Germany has ca. 81 million inhabitants +``` + +์ด ์ฑ„ํŒ…์—์„œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฒˆ์˜ ์ž๊ธฐํšŒ๊ท€ ๋””์ฝ”๋”ฉ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: + 1. ์ฒซ ๋ฒˆ์งธ๋กœ, ํ‚ค-๊ฐ’ ์บ์‹œ๋Š” ๋น„์–ด ์žˆ๊ณ  ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋Š” `"User: How many people live in France?"`์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ž๊ธฐํšŒ๊ท€์ ์œผ๋กœ `"Roughly 75 million people live in France"`๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ ๋””์ฝ”๋”ฉ ๋‹จ๊ณ„๋งˆ๋‹ค ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. + 2. ๋‘ ๋ฒˆ์งธ๋กœ, ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋Š” `"User: How many people live in France? \n Assistant: Roughly 75 million people live in France \n User: And how many in Germany?"`์ž…๋‹ˆ๋‹ค. ์บ์‹œ ๋•๋ถ„์— ์ฒซ ๋ฒˆ์งธ ๋‘ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ๋ชจ๋“  ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๋Š” ์ด๋ฏธ ๊ณ„์‚ฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋Š” `"User: And how many in Germany?"`๋กœ๋งŒ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ค„์–ด๋“  ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์•ˆ ๊ณ„์‚ฐ๋œ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋””์ฝ”๋”ฉ์˜ ํ‚ค-๊ฐ’ ์บ์‹œ์— ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์–ด์‹œ์Šคํ„ดํŠธ์˜ ๋‹ต๋ณ€์ธ `"Germany has ca. 81 million inhabitants"`๋Š” `"User: How many people live in France? \n Assistant: Roughly 75 million people live in France \n User: And how many are in Germany?"`์˜ ์ธ์ฝ”๋”ฉ๋œ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๋กœ ๊ตฌ์„ฑ๋œ ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๊ธฐํšŒ๊ท€์ ์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์„œ ๋‘ ๊ฐ€์ง€๋ฅผ ์ฃผ๋ชฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + 1. ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ๋Œ€ํ™”์˜ ๋ชจ๋“  ์ด์ „ ๋ฌธ๋งฅ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋“  ๋ฌธ๋งฅ์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ฑ„ํŒ…์— ๋ฐฐํฌ๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์—์„œ๋Š” ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ ์˜ˆ์—์„œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์‚ฌ์šฉ์ž๊ฐ€ `"And how many are in Germany"`๋ผ๊ณ  ๋ฌผ์„ ๋•Œ ์ธ๊ตฌ๋ฅผ ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ์Œ์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + 2. ํ‚ค-๊ฐ’ ์บ์‹œ๋Š” ์ฑ„ํŒ…์—์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ธ์ฝ”๋”ฉ๋œ ์ฑ„ํŒ… ๊ธฐ๋ก์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ธ์ฝ”๋”ฉํ•  ํ•„์š” ์—†์ด ๊ณ„์†ํ•ด์„œ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค(์˜ˆ: ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ). + +`transformers`์—์„œ `generate` ํ˜ธ์ถœ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ `use_cache=True`์™€ ํ•จ๊ป˜ `return_dict_in_generate=True`๋ฅผ ์ „๋‹ฌํ•˜๋ฉด `past_key_values`๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์•„์ง `pipeline` ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + +```python +# ์ผ๋ฐ˜์ ์ธ ์ƒ์„ฑ +prompt = system_prompt + "Question: Please write a function in Python that transforms bytes to Giga bytes.\n\nAnswer: Here" +model_inputs = tokenizer(prompt, return_tensors='pt') +generation_output = model.generate(**model_inputs, max_new_tokens=60, return_dict_in_generate=True) +decoded_output = tokenizer.batch_decode(generation_output.sequences)[0] + +# ๋ฆฌํ„ด๋œ `past_key_values`๋ฅผ ํŒŒ์ดํ”„๋ผ์ธํ™”ํ•˜์—ฌ ๋‹ค์Œ ๋Œ€ํ™” ๋ผ์šด๋“œ๋ฅผ ๊ฐ€์†ํ™” +prompt = decoded_output + "\nQuestion: How can I modify the function above to return Mega bytes instead?\n\nAnswer: Here" +model_inputs = tokenizer(prompt, return_tensors='pt') +generation_output = model.generate( + **model_inputs, + past_key_values=generation_output.past_key_values, + max_new_tokens=60, + return_dict_in_generate=True +) +tokenizer.batch_decode(generation_output.sequences)[0][len(prompt):] +``` + +**์ถœ๋ ฅ**: +``` + is a modified version of the function that returns Mega bytes instead. + +def bytes_to_megabytes(bytes): + return bytes / 1024 / 1024 + +Answer: The function takes a number of bytes as input and returns the number of +``` + +ํ›Œ๋ฅญํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์ธต์˜ ๋™์ผํ•œ ํ‚ค์™€ ๊ฐ’์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์ถ”๊ฐ€ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. \\( \mathbf{QK}^T \\) ํ–‰๋ ฌ์— ํ•„์š”ํ•œ ์ตœ๋Œ€ ๋ฉ”๋ชจ๋ฆฌ๋Š” ํฌ๊ฒŒ ์ค„์–ด๋“ค์ง€๋งŒ, ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค๋‚˜ ๋‹คํšŒ์ฐจ ์ฑ„ํŒ…์˜ ๊ฒฝ์šฐ ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋ณด๊ด€ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ๋ฉ”๋ชจ๋ฆฌ ์ง‘์•ฝ์ ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‚ค-๊ฐ’ ์บ์‹œ๋Š” ๋ชจ๋“  ์ž๊ธฐ ์–ดํ…์…˜ ์ธต๊ณผ ๋ชจ๋“  ์–ดํ…์…˜ ํ—ค๋“œ์— ๋Œ€ํ•ด ์ด์ „ ์ž…๋ ฅ ๋ฒกํ„ฐ \\( \mathbf{x}_i \text{, for } i \in {1, \ldots, c - 1} \\)์˜ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์„ธ์š”. + +์ด์ „์— ์‚ฌ์šฉํ•œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ `bigcode/octocoder`์— ๋Œ€ํ•ด ํ‚ค-๊ฐ’ ์บ์‹œ์— ์ €์žฅํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. +๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์˜ ์ˆ˜๋Š” ์‹œํ€€์Šค ๊ธธ์ด์˜ ๋‘ ๋ฐฐ์˜ ์–ดํ…์…˜ ํ—ค๋“œ ์ˆ˜, ์–ดํ…์…˜ ํ—ค๋“œ ์ฐจ์›, ๋ ˆ์ด์–ด ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. +๊ฐ€์ƒ์˜ ์ž…๋ ฅ ์‹œํ€€์Šค ๊ธธ์ด 16000์—์„œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +```python +config = model.config +2 * 16_000 * config.n_layer * config.n_head * config.n_embd // config.n_head +``` + +**์ถœ๋ ฅ**: +``` +7864320000 +``` + +๋Œ€๋žต 80์–ต ๊ฐœ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์ž…๋‹ˆ๋‹ค! `float16` ์ •๋ฐ€๋„๋กœ 80์–ต ๊ฐœ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐ๋Š” ์•ฝ 15GB์˜ RAM์ด ํ•„์š”ํ•˜๋ฉฐ, ์ด๋Š” ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ ์ž์ฒด์˜ ์ ˆ๋ฐ˜ ์ •๋„์ž…๋‹ˆ๋‹ค. +์—ฐ๊ตฌ์ž๋“ค์€ ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋น„์šฉ์„ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ–ˆ์œผ๋ฉฐ, ์ด๋Š” ๋‹ค์Œ ์ ˆ์—์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +#### 3.2.2 ๋ฉ€ํ‹ฐ ์ฟผ๋ฆฌ ์–ดํ…์…˜ (MQA) [[322-multi-query-attention-mqa]] + +[๋ฉ€ํ‹ฐ ์ฟผ๋ฆฌ ์–ดํ…์…˜ (MQA)](https://huggingface.co/papers/1911.02150)์€ Noam Shazeer์˜ *Fast Transformer Decoding: One Write-Head is All You Need* ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ œ๋ชฉ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, Noam์€ `n_head` ํ‚ค-๊ฐ’ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜ ๋Œ€์‹ , ๋ชจ๋“  ์–ดํ…์…˜ ํ—ค๋“œ์—์„œ ๊ณต์œ ๋˜๋Š” ๋‹จ์ผ ํ—ค๋“œ-๊ฐ’ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ์ €ํ•˜๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. + +> ๋‹จ์ผ ํ—ค๋“œ-๊ฐ’ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ \\( \mathbf{k}_i, \mathbf{v}_i \\)๋Š” ๋ชจ๋“  ์–ดํ…์…˜ ํ—ค๋“œ์—์„œ ๋™์ผํ•ด์•ผ ํ•˜๋ฉฐ, ์ด๋Š” ์บ์‹œ์— `n_head` ๊ฐœ ๋Œ€์‹  ํ•˜๋‚˜์˜ ํ‚ค-๊ฐ’ ํ”„๋กœ์ ์…˜ ์Œ๋งŒ ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +๋Œ€๋ถ€๋ถ„์˜ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด 20์—์„œ 100 ์‚ฌ์ด์˜ ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, MQA๋Š” ํ‚ค-๊ฐ’ ์บ์‹œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋ฅผ ํฌ๊ฒŒ ์ค„์ž…๋‹ˆ๋‹ค. ์ด ๋…ธํŠธ๋ถ์—์„œ ์‚ฌ์šฉ๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ์ž…๋ ฅ ์‹œํ€€์Šค ๊ธธ์ด 16000์—์„œ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋ฅผ 15GB์—์„œ 400MB ๋ฏธ๋งŒ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฉ”๋ชจ๋ฆฌ ์ ˆ๊ฐ ์™ธ์—๋„, MQA๋Š” ๊ณ„์‚ฐ ํšจ์œจ์„ฑ๋„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. +์ž๊ธฐํšŒ๊ท€ ๋””์ฝ”๋”ฉ์—์„œ๋Š” ํฐ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ๋ฅผ ๋‹ค์‹œ ๋กœ๋“œํ•˜๊ณ , ํ˜„์žฌ ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ ์Œ๊ณผ ์—ฐ๊ฒฐํ•œ ํ›„ \\( \mathbf{q}_c\mathbf{K}^T \\) ๊ณ„์‚ฐ์— ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž๊ธฐํšŒ๊ท€ ๋””์ฝ”๋”ฉ์˜ ๊ฒฝ์šฐ, ์ง€์†์ ์ธ ์žฌ๋กœ๋“œ์— ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ์ด ์‹ฌ๊ฐํ•œ ์‹œ๊ฐ„ ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‚ค-๊ฐ’ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋ฉด ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์–‘์ด ์ค„์–ด๋“ค์–ด ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ ๋ณ‘๋ชฉ ํ˜„์ƒ์ด ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Noam์˜ ๋…ผ๋ฌธ](https://huggingface.co/papers/1911.02150)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +์—ฌ๊ธฐ์„œ ์ดํ•ดํ•ด์•ผ ํ•  ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ํ‚ค-๊ฐ’ ์–ดํ…์…˜ ํ—ค๋“œ ์ˆ˜๋ฅผ 1๋กœ ์ค„์ด๋Š” ๊ฒƒ์ด ํ‚ค-๊ฐ’ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ‚ค-๊ฐ’ ์บ์‹œ ์—†์ด ๋‹จ์ผ ํฌ์›Œ๋“œ ํŒจ์Šค์— ๋Œ€ํ•œ ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฉฐ, ๊ฐ ์–ดํ…์…˜ ํ—ค๋“œ๋Š” ์—ฌ์ „ํžˆ ๊ณ ์œ ํ•œ ์ฟผ๋ฆฌ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ๊ฐ ์–ดํ…์…˜ ํ—ค๋“œ๋Š” ์—ฌ์ „ํžˆ ๋‹ค๋ฅธ \\( \mathbf{QK}^T \\) ํ–‰๋ ฌ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. + +MQA๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๋„๋ฆฌ ์ฑ„ํƒ๋˜์–ด ํ˜„์žฌ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋งŽ์€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +- [**Falcon**](https://huggingface.co/tiiuae/falcon-40b) +- [**PaLM**](https://huggingface.co/papers/2204.02311) +- [**MPT**](https://huggingface.co/mosaicml/mpt-30b) +- [**BLOOM**](https://huggingface.co/bigscience/bloom) + +๋˜ํ•œ, ์ด ๋…ธํŠธ๋ถ์—์„œ ์‚ฌ์šฉ๋œ ์ฒดํฌํฌ์ธํŠธ `bigcode/octocoder`๋Š” MQA๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +#### 3.2.3 ๊ทธ๋ฃน ์ฟผ๋ฆฌ ์–ดํ…์…˜ (GQA) [[323-grouped-query-attention-gqa]] + +[๊ทธ๋ฃน ์ฟผ๋ฆฌ ์–ดํ…์…˜ (GQA)](https://huggingface.co/papers/2305.13245)์€ Google์˜ Ainslie ๋“ฑ์˜ ์—ฐ๊ตฌ์ง„๋“ค์— ์˜ํ•ด ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ MQA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข…์ข… ์ผ๋ฐ˜์ ์ธ ๋ฉ€ํ‹ฐ ํ‚ค-๊ฐ’ ํ—ค๋“œ ํ”„๋กœ์ ์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ’ˆ์งˆ ์ €ํ•˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์€ ์ฟผ๋ฆฌ ํ—ค๋“œ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜์˜ ์ˆ˜๋ฅผ ๋„ˆ๋ฌด ๊ทน๋‹จ์ ์œผ๋กœ ์ค„์ด๋Š” ๋Œ€์‹ , ๋” ๋งŽ์€ ๋ชจ๋ธ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ํ‚ค-๊ฐ’ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜ ๋Œ€์‹ , `n < n_head` ํ‚ค-๊ฐ’ ํ”„๋กœ์ ์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `n_head`๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์€ `n`๊ฐ’, ์˜ˆ๋ฅผ ๋“ค์–ด 2, 4 ๋˜๋Š” 8์„ ์„ ํƒํ•˜๋ฉด, MQA์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์†๋„ ์ด์ ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ชจ๋ธ ์šฉ๋Ÿ‰์„ ๋œ ํฌ์ƒํ•˜๊ณ  ๋”ฐ๋ผ์„œ ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ, GQA์˜ ์ €์ž๋“ค์€ ๊ธฐ์กด ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์›๋ž˜ ์‚ฌ์ „ ํ•™์Šต ๊ณ„์‚ฐ์˜ 5% ์ •๋„์˜ ์ ์€ ์–‘์œผ๋กœ GQA ์•„ํ‚คํ…์ฒ˜๋กœ *์—…ํŠธ๋ ˆ์ด๋‹*ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ž˜ ์‚ฌ์ „ ํ•™์Šต ๊ณ„์‚ฐ์˜ 5%๊ฐ€ ์—ฌ์ „ํžˆ ์—„์ฒญ๋‚œ ์–‘์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, GQA *์—…ํŠธ๋ ˆ์ด๋‹*์€ ๊ธฐ์กด ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ๋” ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค์—์„œ๋„ ์œ ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +GQA๋Š” ์ตœ๊ทผ์— ์ œ์•ˆ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋…ธํŠธ๋ถ์„ ์ž‘์„ฑํ•  ๋‹น์‹œ์—๋Š” ์ฑ„ํƒ์ด ๋œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +GQA์˜ ๊ฐ€์žฅ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ์ ์šฉ ์‚ฌ๋ก€๋Š” [Llama-v2](https://huggingface.co/meta-llama/Llama-2-70b-hf)์ž…๋‹ˆ๋‹ค. + +> ๊ฒฐ๋ก ์ ์œผ๋กœ, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด ์ž๊ธฐํšŒ๊ท€ ๋””์ฝ”๋”ฉ์œผ๋กœ ๋ฐฐํฌ๋˜๋ฉด์„œ ์ฑ„ํŒ…๊ณผ ๊ฐ™์ด ํฐ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ง„ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ GQA ๋˜๋Š” MQA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. + + +## ๊ฒฐ๋ก  [[conclusion]] + +์—ฐ๊ตฌ ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” ์ ์  ๋” ํฐ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ถ”๋ก  ์‹œ๊ฐ„์„ ๊ฐ€์†ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ์ƒˆ๋กœ์šด ๊ธฐ๋ฐœํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ๋Š์ž„์—†์ด ์ฐพ์•„๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [์ถ”์ธก ๋””์ฝ”๋”ฉ](https://huggingface.co/papers/2211.17192)์ด๋ผ๋Š” ์œ ๋งํ•œ ์—ฐ๊ตฌ ๋ฐฉํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "์‰ฌ์šด ํ† ํฐ"์€ ๋” ์ž‘๊ณ  ๋น ๋ฅธ ์–ธ์–ด ๋ชจ๋ธ์— ์˜ํ•ด ์ƒ์„ฑ๋˜๊ณ , "์–ด๋ ค์šด ํ† ํฐ"๋งŒ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ์ž์ฒด์— ์˜ํ•ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด ๋…ธํŠธ๋ถ์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€๋งŒ, [๋ฉ‹์ง„ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/assisted-generation)์—์„œ ์ฝ์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +GPT3/4, Llama-2-70b, Claude, PaLM๊ณผ ๊ฐ™์€ ๊ฑฐ๋Œ€ํ•œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์ด [Hugging Face Chat](https://huggingface.co/chat/) ๋˜๋Š” ChatGPT์™€ ๊ฐ™์€ ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ •๋ฐ€๋„, ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์•„ํ‚คํ…์ฒ˜์˜ ๊ฐœ์„  ๋•๋ถ„์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ GPU, TPU ๋“ฑ๊ณผ ๊ฐ™์€ ๊ฐ€์†๊ธฐ๋Š” ์ ์  ๋” ๋นจ๋ผ์ง€๊ณ  ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ์ข‹์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๊ณ ์˜ ํšจ์œจ์„ ์–ป๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค ๐Ÿค— diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/callback.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/callback.md new file mode 100644 index 0000000000000000000000000000000000000000..c8d122a8ef9247089b27d2d8b6a21d667bffa45b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/callback.md @@ -0,0 +1,137 @@ + + +# ์ฝœ๋ฐฑ [[callbacks]] + +์ฝœ๋ฐฑ์€ PyTorch [`Trainer`]์˜ ๋ฐ˜๋ณต ํ•™์Šต ๋™์ž‘์„ ์‚ฌ์šฉ์ž ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค +(์ด ๊ธฐ๋Šฅ์€ TensorFlow์—์„œ๋Š” ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค). ์ฝœ๋ฐฑ์€ ๋ฐ˜๋ณต ํ•™์Šต์˜ ์ƒํƒœ๋ฅผ +๊ฒ€์‚ฌํ•˜์—ฌ (์ง„ํ–‰ ์ƒํ™ฉ ๋ณด๊ณ , TensorBoard ๋˜๋Š” ๊ธฐํƒ€ ๋จธ์‹  ๋Ÿฌ๋‹ ํ”Œ๋žซํผ์— ๋กœ๊ทธ ๋‚จ๊ธฐ๊ธฐ ๋“ฑ) +๊ฒฐ์ •(์˜ˆ: ์กฐ๊ธฐ ์ข…๋ฃŒ)์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ฝœ๋ฐฑ์€ [`TrainerControl`] ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ๋ฐ˜๋ณต ํ•™์Šต์—์„œ ์–ด๋–ค ๊ฒƒ๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” +"์ฝ๊ธฐ ์ „์šฉ" ์ฝ”๋“œ ์กฐ๊ฐ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ณต ํ•™์Šต์— ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, +[`Trainer`]๋ฅผ ์„œ๋ธŒํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค (์˜ˆ์‹œ๋Š” [trainer](trainer)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”). + +๊ธฐ๋ณธ์ ์œผ๋กœ `TrainingArguments.report_to`๋Š” `"all"`๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, [`Trainer`]๋Š” ๋‹ค์Œ ์ฝœ๋ฐฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +- [`DefaultFlowCallback`]๋Š” ๋กœ๊ทธ, ์ €์žฅ, ํ‰๊ฐ€์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๋™์ž‘์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +- [`PrinterCallback`] ๋˜๋Š” [`ProgressCallback`]๋Š” ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ‘œ์‹œํ•˜๊ณ  ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค + ([`TrainingArguments`]๋ฅผ ํ†ตํ•ด tqdm์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ์ฝœ๋ฐฑ์ด ์‚ฌ์šฉ๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‘ ๋ฒˆ์งธ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค). +- [`~integrations.TensorBoardCallback`]๋Š” TensorBoard๊ฐ€ (PyTorch >= 1.4 + ๋˜๋Š” tensorboardX๋ฅผ ํ†ตํ•ด) ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.WandbCallback`]๋Š” [wandb](https://www.wandb.com/)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด + ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.CometCallback`]๋Š” [comet_ml](https://www.comet.com/site/)์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.MLflowCallback`]๋Š” [mlflow](https://www.mlflow.org/)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.NeptuneCallback`]๋Š” [neptune](https://neptune.ai/)์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.AzureMLCallback`]๋Š” [azureml-sdk](https://pypi.org/project/azureml-sdk/)๊ฐ€ ์„ค์น˜๋˜์–ด + ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.CodeCarbonCallback`]๋Š” [codecarbon](https://pypi.org/project/codecarbon/)์ด ์„ค์น˜๋˜์–ด + ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.ClearMLCallback`]๋Š” [clearml](https://github.com/allegroai/clearml)์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.DagsHubCallback`]๋Š” [dagshub](https://dagshub.com/)์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.FlyteCallback`]๋Š” [flyte](https://flyte.org/)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.DVCLiveCallback`]๋Š” [dvclive](https://dvc.org/doc/dvclive)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- [`~integrations.SwanLabCallback`]๋Š” [swanlab](https://swanlab.cn)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€๋งŒ ํ•ด๋‹น ํ†ตํ•ฉ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด, `TrainingArguments.report_to`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ†ตํ•ฉ ๊ธฐ๋Šฅ ๋ชฉ๋ก์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (์˜ˆ: `["azure_ml", "wandb"]`). + +์ฝœ๋ฐฑ์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฃผ์š” ํด๋ž˜์Šค๋Š” [`TrainerCallback`]์ž…๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” [`Trainer`]๋ฅผ +์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ [`TrainingArguments`]๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ํ•ด๋‹น Trainer์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ +[`TrainerState`]๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, [`TrainerControl`]์„ ํ†ตํ•ด ๋ฐ˜๋ณต ํ•™์Šต์—์„œ ์ผ๋ถ€ +์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฝœ๋ฐฑ [[available-callbacks]] + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ [`TrainerCallback`] ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +[[autodoc]] integrations.CometCallback + - setup + +[[autodoc]] DefaultFlowCallback + +[[autodoc]] PrinterCallback + +[[autodoc]] ProgressCallback + +[[autodoc]] EarlyStoppingCallback + +[[autodoc]] integrations.TensorBoardCallback + +[[autodoc]] integrations.WandbCallback + - setup + +[[autodoc]] integrations.MLflowCallback + - setup + +[[autodoc]] integrations.AzureMLCallback + +[[autodoc]] integrations.CodeCarbonCallback + +[[autodoc]] integrations.NeptuneCallback + +[[autodoc]] integrations.ClearMLCallback + +[[autodoc]] integrations.DagsHubCallback + +[[autodoc]] integrations.FlyteCallback + +[[autodoc]] integrations.DVCLiveCallback + - setup + +[[autodoc]] integrations.SwanLabCallback + - setup + +## TrainerCallback [[trainercallback]] + +[[autodoc]] TrainerCallback + +์—ฌ๊ธฐ PyTorch [`Trainer`]์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ์ž ์ •์˜ ์ฝœ๋ฐฑ์„ ๋“ฑ๋กํ•˜๋Š” ์˜ˆ์‹œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +class MyCallback(TrainerCallback): + "A callback that prints a message at the beginning of training" + + def on_train_begin(self, args, state, control, **kwargs): + print("Starting training") + + +trainer = Trainer( + model, + args, + train_dataset=train_dataset, + eval_dataset=eval_dataset, + callbacks=[MyCallback], # ์šฐ๋ฆฌ๋Š” ์ฝœ๋ฐฑ ํด๋ž˜์Šค๋ฅผ ์ด ๋ฐฉ์‹์œผ๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ๊ทธ๊ฒƒ์˜ ์ธ์Šคํ„ด์Šค(MyCallback())๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค +) +``` + +๋˜ ๋‹ค๋ฅธ ์ฝœ๋ฐฑ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ `trainer.add_callback()`์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```python +trainer = Trainer(...) +trainer.add_callback(MyCallback) +# ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ฝœ๋ฐฑ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค +trainer.add_callback(MyCallback()) +``` + +## TrainerState [[trainerstate]] + +[[autodoc]] TrainerState + +## TrainerControl [[trainercontrol]] + +[[autodoc]] TrainerControl diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/configuration.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/configuration.md new file mode 100644 index 0000000000000000000000000000000000000000..6c6278a02585608a66a600cd610c81af995aaebf --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/configuration.md @@ -0,0 +1,28 @@ + + +# ๊ตฌ์„ฑ[[configuration]] + +๊ธฐ๋ณธ ํด๋ž˜์Šค [`PretrainedConfig`]๋Š” ๋กœ์ปฌ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ, ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ ๊ตฌ์„ฑ(HuggingFace์˜ AWS S3 ์ €์žฅ์†Œ์—์„œ ๋‹ค์šด๋กœ๋“œ๋จ)์œผ๋กœ๋ถ€ํ„ฐ ๊ตฌ์„ฑ์„ ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ ์ €์žฅํ•˜๋Š” ๊ณตํ†ต ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํŒŒ์ƒ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋Š” ๋ชจ๋ธ๋ณ„ ํŠน์„ฑ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋“  ๊ตฌ์„ฑ ํด๋ž˜์Šค์— ์กด์žฌํ•˜๋Š” ๊ณตํ†ต ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: `hidden_size`, `num_attention_heads`, `num_hidden_layers`. ํ…์ŠคํŠธ ๋ชจ๋ธ์€ ์ถ”๊ฐ€๋กœ `vocab_size`๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. + + +## PretrainedConfig[[transformers.PretrainedConfig]] + +[[autodoc]] PretrainedConfig + - push_to_hub + - all diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/data_collator.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/data_collator.md new file mode 100644 index 0000000000000000000000000000000000000000..0e677c7d8979b87b418d800978174f1a65701f3a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/data_collator.md @@ -0,0 +1,66 @@ + + +# ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ(Data Collator)[[data-collator]] + +๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ์…‹ ์š”์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜๋ฅผ ํ˜•์„ฑํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์š”์†Œ๋“ค์€ `train_dataset` ๋˜๋Š” `eval_dataset์˜` ์š”์†Œ๋“ค๊ณผ ๋™์ผํ•œ ํƒ€์ž… ์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด, ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๋Š” (ํŒจ๋”ฉ๊ณผ ๊ฐ™์€) ์ผ๋ถ€ ์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`DataCollatorForLanguageModeling`]๊ณผ ๊ฐ™์€ ์ผ๋ถ€ ์ฝœ๋ ˆ์ดํ„ฐ๋Š” ํ˜•์„ฑ๋œ ๋ฐฐ์น˜์— (๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น๊ณผ ๊ฐ™์€) ์ผ๋ถ€ ๋ฌด์ž‘์œ„ ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•๋„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](../examples)๋‚˜ [์˜ˆ์ œ ๋…ธํŠธ๋ถ](../notebooks)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ[[transformers.default_data_collator]] + +[[autodoc]] data.data_collator.default_data_collator + +## DefaultDataCollator[[transformers.DefaultDataCollator]] + +[[autodoc]] data.data_collator.DefaultDataCollator + +## DataCollatorWithPadding[[transformers.DataCollatorWithPadding]] + +[[autodoc]] data.data_collator.DataCollatorWithPadding + +## DataCollatorForTokenClassification[[transformers.DataCollatorForTokenClassification]] + +[[autodoc]] data.data_collator.DataCollatorForTokenClassification + +## DataCollatorForSeq2Seq[[transformers.DataCollatorForSeq2Seq]] + +[[autodoc]] data.data_collator.DataCollatorForSeq2Seq + +## DataCollatorForLanguageModeling[[transformers.DataCollatorForLanguageModeling]] + +[[autodoc]] data.data_collator.DataCollatorForLanguageModeling + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens + +## DataCollatorForWholeWordMask[[transformers.DataCollatorForWholeWordMask]] + +[[autodoc]] data.data_collator.DataCollatorForWholeWordMask + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens + +## DataCollatorForPermutationLanguageModeling[[transformers.DataCollatorForPermutationLanguageModeling]] + +[[autodoc]] data.data_collator.DataCollatorForPermutationLanguageModeling + - numpy_mask_tokens + - tf_mask_tokens + - torch_mask_tokens + +## DataCollatorWithFlatteningtransformers.DataCollatorWithFlattening + +[[autodoc]] data.data_collator.DataCollatorWithFlattening + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/feature_extractor.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/feature_extractor.md new file mode 100644 index 0000000000000000000000000000000000000000..7c667424c4ff956a1b1dce95db970b88d5aa7eb4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/feature_extractor.md @@ -0,0 +1,39 @@ + + +# ํŠน์„ฑ ์ถ”์ถœ๊ธฐ [[feature-extractor]] + +ํŠน์„ฑ ์ถ”์ถœ๊ธฐ๋Š” ์˜ค๋””์˜ค ๋˜๋Š” ๋น„์ „ ๋ชจ๋ธ์„ ์œ„ํ•œ ์ž…๋ ฅ ํŠน์„ฑ์„ ์ค€๋น„ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์‹œํ€€์Šค์—์„œ ํŠน์„ฑ์„ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…(์˜ˆ๋ฅผ ๋“ค์–ด, ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ „์ฒ˜๋ฆฌํ•˜์—ฌ Log-Mel ์ŠคํŽ™ํŠธ๋กœ๊ทธ๋žจ ํŠน์„ฑ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ), ์ด๋ฏธ์ง€์—์„œ ํŠน์„ฑ์„ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…(์˜ˆ๋ฅผ ๋“ค์–ด, ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ž๋ฅด๋Š” ๊ฒƒ)์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํŒจ๋”ฉ, ์ •๊ทœํ™” ๋ฐ NumPy, PyTorch, TensorFlow ํ…์„œ๋กœ์˜ ๋ณ€ํ™˜๋„ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. + + +## FeatureExtractionMixin [[transformers.FeatureExtractionMixin]] + +[[autodoc]] feature_extraction_utils.FeatureExtractionMixin + - from_pretrained + - save_pretrained + +## SequenceFeatureExtractor [[transformers.SequenceFeatureExtractor]] + +[[autodoc]] SequenceFeatureExtractor + - pad + +## BatchFeature [[transformers.BatchFeature]] + +[[autodoc]] BatchFeature + +## ImageFeatureExtractionMixin [[transformers.ImageFeatureExtractionMixin]] + +[[autodoc]] image_utils.ImageFeatureExtractionMixin diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/keras_callbacks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/keras_callbacks.md new file mode 100644 index 0000000000000000000000000000000000000000..25d5ea3e40083acad8fa9257ce2c7cfef7c3d9c8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/keras_callbacks.md @@ -0,0 +1,27 @@ + + +# ์ผ€๋ผ์Šค ์ฝœ๋ฐฑ[[keras-callbacks]] + +์ผ€๋ผ์Šค๋กœ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๋•Œ, ์ผ๋ฐ˜์ ์ธ ์ž‘์—…์„ ์ž๋™ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „์šฉ ์ฝœ๋ฐฑ๋“ค์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## KerasMetricCallback[[transformers.KerasMetricCallback]] + +[[autodoc]] KerasMetricCallback + +## PushToHubCallback[[transformers.PushToHubCallback]] + +[[autodoc]] PushToHubCallback diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/logging.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/logging.md new file mode 100644 index 0000000000000000000000000000000000000000..55e1a21c7bd57bb1b9f8e0f47052fa085340f65b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/logging.md @@ -0,0 +1,108 @@ + + +# ๋กœ๊น… [[logging]] + +๐Ÿค— ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์ค‘์•™ ์ง‘์ค‘์‹ ๋กœ๊น… ์‹œ์Šคํ…œ์„ ์ œ๊ณตํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ถœ๋ ฅ ๋ ˆ๋ฒจ์„ ์‰ฝ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ˜„์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ธฐ๋ณธ ์ถœ๋ ฅ ๋ ˆ๋ฒจ์€ `WARNING`์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ถœ๋ ฅ ๋ ˆ๋ฒจ์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์ง์ ‘์ ์ธ ์„ค์ • ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ถœ๋ ฅ ๋ ˆ๋ฒจ์„ INFO ์ˆ˜์ค€์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +```python +import transformers + +transformers.logging.set_verbosity_info() +``` + +ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `TRANSFORMERS_VERBOSITY`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ ์ถœ๋ ฅ ๋ ˆ๋ฒจ์„ ์žฌ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ `debug`, `info`, `warning`, `error`, `critical`, `fatal` ์ค‘ ํ•˜๋‚˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +```bash +TRANSFORMERS_VERBOSITY=error ./myprogram.py +``` + +๋˜ํ•œ, ์ผ๋ถ€ `warnings`๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `TRANSFORMERS_NO_ADVISORY_WARNINGS`๋ฅผ 1๊ณผ ๊ฐ™์€ true ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด [`logger.warning_advice`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ก๋œ ๊ฒฝ๊ณ ๊ฐ€ ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +```bash +TRANSFORMERS_NO_ADVISORY_WARNINGS=1 ./myprogram.py +``` + +๋‹ค์Œ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋™์ผํ•œ ๋กœ๊ฑฐ๋ฅผ ์ž์‹ ์˜ ๋ชจ๋“ˆ์ด๋‚˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. + +```python +from transformers.utils import logging + +logging.set_verbosity_info() +logger = logging.get_logger("transformers") +logger.info("INFO") +logger.warning("WARN") +``` + + +์ด ๋กœ๊น… ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์— ๋ฌธ์„œํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ฃผ์š” ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ ๋กœ๊ฑฐ์˜ ์ถœ๋ ฅ ์ˆ˜์ค€์„ ๊ฐ€์ ธ์˜ค๋Š” [`logging.get_verbosity`]์™€ ์›ํ•˜๋Š” ์ถœ๋ ฅ ์ˆ˜์ค€์œผ๋กœ ์„ค์ •ํ•˜๋Š” [`logging.set_verbosity`] ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ์ˆ˜์ค€์€ (๊ฐ€์žฅ ์ ์€ ์ถœ๋ ฅ์—์„œ ๊ฐ€์žฅ ๋งŽ์€ ์ถœ๋ ฅ ์ˆœ์œผ๋กœ) ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ํ•ด๋‹น ์ˆ˜์ค€์— ๋Œ€์‘ํ•˜๋Š” ์ •์ˆ˜ ๊ฐ’์€ ๊ด„ํ˜ธ ์•ˆ์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. + +- `transformers.logging.CRITICAL` ๋˜๋Š” `transformers.logging.FATAL` (์ •์ˆซ๊ฐ’, 50): ๊ฐ€์žฅ ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜๋งŒ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. +- `transformers.logging.ERROR` (์ •์ˆซ๊ฐ’, 40): ์˜ค๋ฅ˜๋งŒ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. +- `transformers.logging.WARNING` ๋˜๋Š” `transformers.logging.WARN` (์ •์ˆซ๊ฐ’, 30): ์˜ค๋ฅ˜์™€ ๊ฒฝ๊ณ ๋งŒ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ˆ˜์ค€์ž…๋‹ˆ๋‹ค. +- `transformers.logging.INFO` (์ •์ˆซ๊ฐ’, 20): ์˜ค๋ฅ˜, ๊ฒฝ๊ณ , ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ์ ์ธ ์ •๋ณด๋ฅผ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. +- `transformers.logging.DEBUG` (์ •์ˆซ๊ฐ’, 10): ๋ชจ๋“  ์ •๋ณด๋ฅผ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ์ค‘์—๋Š” `tqdm` ์ง„ํ–‰ ํ‘œ์‹œ์ค„์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. [`logging.disable_progress_bar`]์™€ [`logging.enable_progress_bar`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ๋™์ž‘์„ ์ˆจ๊ธฐ๊ฑฐ๋‚˜ ๋‹ค์‹œ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## `logging` vs `warnings`[[transformers.utils.logging.captureWarnings]] + +Python์—๋Š” ์ข…์ข… ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ๋‘ ๊ฐ€์ง€ ๋กœ๊น… ์‹œ์Šคํ…œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ `logging`๊ณผ `warnings`์ž…๋‹ˆ๋‹ค. `warnings`๋Š” ํŠน์ • ๋ฒ”์ฃผ๋กœ ๊ฒฝ๊ณ ๋ฅผ ์„ธ๋ถ„ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ด๋ฏธ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ์ด๋‚˜ ๊ฒฝ๋กœ์— ๋Œ€ํ•ด `FutureWarning`์ด ์‚ฌ์šฉ๋˜๊ณ , ๊ณง ์‚ฌ์šฉ ์ค‘๋‹จ๋  ๊ธฐ๋Šฅ์„ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด `DeprecationWarning`์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํŠธ๋žœ์Šคํฌ๋จธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” ๋‘ ์‹œ์Šคํ…œ ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. `logging`์˜ `captureWarnings` ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์ด๋ฅผ ์กฐ์ •ํ•˜์—ฌ ์œ„์—์„œ ์„ค๋ช…ํ•œ ์ถœ๋ ฅ ์ˆ˜์ค€ ์„ค์ •์ž๋“ค์„ ํ†ตํ•ด ์ด๋Ÿฌํ•œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋“ค์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐœ๋ฐœ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ง€์นจ์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +- `warnings`๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐœ๋ฐœ์ž์™€ `transformers`์— ์˜์กดํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +- `logging`์€ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ์ ํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐœ๋ฐœ์ž๋ณด๋‹ค๋Š”, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ตœ์ข… ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ์œ ๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์•„๋ž˜์—์„œ `captureWarnings` ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ์ฐธ๊ณ  ์‚ฌํ•ญ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[[autodoc]] logging.captureWarnings + +## ๊ธฐ๋ณธ ์„ค์ •์ž [[transformers.utils.logging.set_verbosity_error]] + +[[autodoc]] logging.set_verbosity_error + +[[autodoc]] logging.set_verbosity_warning + +[[autodoc]] logging.set_verbosity_info + +[[autodoc]] logging.set_verbosity_debug + +## ๊ธฐํƒ€ ํ•จ์ˆ˜ [[transformers.utils.logging.get_verbosity]] + +[[autodoc]] logging.get_verbosity + +[[autodoc]] logging.set_verbosity + +[[autodoc]] logging.get_logger + +[[autodoc]] logging.enable_default_handler + +[[autodoc]] logging.disable_default_handler + +[[autodoc]] logging.enable_explicit_format + +[[autodoc]] logging.reset_format + +[[autodoc]] logging.enable_progress_bar + +[[autodoc]] logging.disable_progress_bar diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/model.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/model.md new file mode 100644 index 0000000000000000000000000000000000000000..71a9768deee1ecf1badbbe60fe0152eb1756cde6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/model.md @@ -0,0 +1,68 @@ + + +# ๋ชจ๋ธ + +๊ธฐ๋ณธ ํด๋ž˜์Šค [`PreTrainedModel`], [`TFPreTrainedModel`], [`FlaxPreTrainedModel`]๋Š” ๋กœ์ปฌ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ† ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” (ํ—ˆ๊น…ํŽ˜์ด์Šค AWS S3 ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๋‹ค์šด๋กœ๋“œ๋œ) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ ์„ค์ •์„ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ์ง€์›ํ•˜๋Š” ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. + +[`PreTrainedModel`]๊ณผ [`TFPreTrainedModel`]์€ ๋˜ํ•œ ๋ชจ๋“  ๋ชจ๋ธ๋“ค์„ ๊ณตํ†ต์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ๋ฉ”์†Œ๋“œ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค: + +- ์ƒˆ ํ† ํฐ์ด ๋‹จ์–ด์žฅ์— ์ถ”๊ฐ€๋  ๋•Œ, ์ž…๋ ฅ ํ† ํฐ ์ž„๋ฒ ๋”ฉ์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์˜ ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ๊ฐ€์ง€์น˜๊ธฐํ•ฉ๋‹ˆ๋‹ค. + +๊ฐ ๋ชจ๋ธ์— ๊ณตํ†ต์ธ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๋“ค์€ ๋‹ค์Œ์˜ ํด๋ž˜์Šค์—์„œ ์ •์˜๋ฉ๋‹ˆ๋‹ค. +- [`~modeling_utils.ModuleUtilsMixin`](ํŒŒ์ดํ† ์น˜ ๋ชจ๋ธ์šฉ) +- ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ [`~modeling_tf_utils.TFModuleUtilsMixin`](ํ…์„œํ”Œ๋กœ ๋ชจ๋ธ์šฉ) +- [`~generation.GenerationMixin`](ํŒŒ์ดํ† ์น˜ ๋ชจ๋ธ์šฉ) +- [`~generation.FlaxGenerationMixin`](Flax/JAX ๋ชจ๋ธ์šฉ) + +## PreTrainedModel + +[[autodoc]] PreTrainedModel + - push_to_hub + - all + +์‚ฌ์šฉ์ž ์ •์˜ ๋ชจ๋ธ์€ ์ดˆ๊ณ ์† ์ดˆ๊ธฐํ™”(superfast init)๊ฐ€ ํŠน์ • ๋ชจ๋ธ์— ์ ์šฉ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” `_supports_assign_param_buffer`๋„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +`test_save_and_load_from_pretrained` ์‹คํŒจ ์‹œ, ๋ชจ๋ธ์ด `_supports_assign_param_buffer`๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. +ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด `False`๋กœ ์„ค์ •ํ•˜์„ธ์š”. + +## ModuleUtilsMixin + +[[autodoc]] modeling_utils.ModuleUtilsMixin + +## TFPreTrainedModel + +[[autodoc]] TFPreTrainedModel + - push_to_hub + - all + +## TFModelUtilsMixin + +[[autodoc]] modeling_tf_utils.TFModelUtilsMixin + +## FlaxPreTrainedModel + +[[autodoc]] FlaxPreTrainedModel + - push_to_hub + - all + +## ํ—ˆ๋ธŒ์— ์ €์žฅํ•˜๊ธฐ + +[[autodoc]] utils.PushToHubMixin + +## ๊ณต์œ ๋œ ์ฒดํฌํฌ์ธํŠธ + +[[autodoc]] modeling_utils.load_sharded_checkpoint diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/onnx.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/onnx.md new file mode 100644 index 0000000000000000000000000000000000000000..510dab9df2f9332ca5a69252bd7c444e44bdf2e9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/onnx.md @@ -0,0 +1,50 @@ + + +# ๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ[[exporting--transformers-models-to-onnx]] + +๐Ÿค— ํŠธ๋žœ์Šคํฌ๋จธ๋Š” `transformers.onnx` ํŒจํ‚ค์ง€๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์ด ํŒจํ‚ค์ง€๋Š” ์„ค์ • ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ONNX ๊ทธ๋ž˜ํ”„๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +๐Ÿค— Transformers์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ด ๊ฐ€์ด๋“œ](../serialization)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ONNX ์„ค์ •[[onnx-configurations]] + +๋‚ด๋ณด๋‚ด๋ ค๋Š”(export) ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์˜ ์œ ํ˜•์— ๋”ฐ๋ผ ์ƒ์†๋ฐ›์•„์•ผ ํ•  ์„ธ ๊ฐ€์ง€ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +* ์ธ์ฝ”๋” ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์€ [`~onnx.config.OnnxConfig`]์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. +* ๋””์ฝ”๋” ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์€ [`~onnx.config.OnnxConfigWithPast`]์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. +* ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์€ [`~onnx.config.OnnxSeq2SeqConfigWithPast`]์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. + +### OnnxConfig[[transformers.onnx.OnnxConfig]] + +[[autodoc]] onnx.config.OnnxConfig + +### OnnxConfigWithPast[[transformers.onnx.OnnxConfigWithPast]] + +[[autodoc]] onnx.config.OnnxConfigWithPast + +### OnnxSeq2SeqConfigWithPast[[OnnxSeq2SeqConfigWithPast]] + +[[autodoc]] onnx.config.OnnxSeq2SeqConfigWithPast + +## ONNX ํŠน์ง•[[onnx-features]] + +๊ฐ ONNX ์„ค์ •์€ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ํ† ํด๋กœ์ง€๋‚˜ ์ž‘์—…์— ๋Œ€ํ•ด ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ(exporting) ํ•ด์ฃผ๋Š” _features_ ์„ธํŠธ์™€ ์—ฐ๊ด€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +### FeaturesManager[[transformers.onnx.FeaturesManager]] + +[[autodoc]] onnx.features.FeaturesManager + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/optimizer_schedules.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/optimizer_schedules.md new file mode 100644 index 0000000000000000000000000000000000000000..24e1009e8274c70fdd09264b70d43d85f2ab57ca --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/optimizer_schedules.md @@ -0,0 +1,76 @@ + + +# ์ตœ์ ํ™”[[optimization]] + +`.optimization` ๋ชจ๋“ˆ์€ ๋‹ค์Œ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +- ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ค‘์น˜ ๊ฐ์‡ ๊ฐ€ ์ ์šฉ๋œ ์˜ตํ‹ฐ๋งˆ์ด์ € +- `_LRSchedule`์„ ์ƒ์†๋ฐ›๋Š” ์Šค์ผ€์ค„ ๊ฐ์ฒด ํ˜•ํƒœ์˜ ์—ฌ๋Ÿฌ ์Šค์ผ€์ค„ +- ์—ฌ๋Ÿฌ ๋ฐฐ์น˜์˜ ๊ทธ๋ž˜๋””์–ธํŠธ๋ฅผ ๋ˆ„์ ํ•˜๋Š” ๊ทธ๋ž˜๋””์–ธํŠธ ๋ˆ„์  ํด๋ž˜์Šค + + +## AdaFactor (PyTorch)[[transformers.Adafactor]] + +[[autodoc]] Adafactor + +## AdamWeightDecay (TensorFlow)[[transformers.AdamWeightDecay]] + +[[autodoc]] AdamWeightDecay + +[[autodoc]] create_optimizer + +## ์Šค์ผ€์ค„[[schedules]] + +### ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„ (PyTorch)[[transformers.SchedulerType]] + +[[autodoc]] SchedulerType + +[[autodoc]] get_scheduler + +[[autodoc]] get_constant_schedule + +[[autodoc]] get_constant_schedule_with_warmup + + + +[[autodoc]] get_cosine_schedule_with_warmup + + + +[[autodoc]] get_cosine_with_hard_restarts_schedule_with_warmup + + + +[[autodoc]] get_linear_schedule_with_warmup + + + +[[autodoc]] get_polynomial_decay_schedule_with_warmup + +[[autodoc]] get_inverse_sqrt_schedule + +[[autodoc]] get_wsd_schedule + +### ์›œ์—… (TensorFlow)[[transformers.WarmUp]] + +[[autodoc]] WarmUp + +## ๊ทธ๋ž˜๋””์–ธํŠธ ์ „๋žต[[gradient-strategies]] + +### GradientAccumulator (TensorFlow)[[transformers.GradientAccumulator]] + +[[autodoc]] GradientAccumulator diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/output.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/output.md new file mode 100644 index 0000000000000000000000000000000000000000..e65a2c2c35906eef6cc4011994d4bdd50d9d3d76 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/output.md @@ -0,0 +1,314 @@ + + +# ๋ชจ๋ธ ์ถœ๋ ฅ[[model-outputs]] + +๋ชจ๋“  ๋ชจ๋ธ์—๋Š” [`~utils.ModelOutput`]์˜ ์„œ๋ธŒํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ ๋ชจ๋ธ ์ถœ๋ ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ +๋ชจ๋ธ์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ชจ๋“  ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด์ง€๋งŒ ํŠœํ”Œ์ด๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import BertTokenizer, BertForSequenceClassification +import torch + +tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +model = BertForSequenceClassification.from_pretrained("google-bert/bert-base-uncased") + +inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") +labels = torch.tensor([1]).unsqueeze(0) # ๋ฐฐ์น˜ ํฌ๊ธฐ 1 +outputs = model(**inputs, labels=labels) +``` + +`outputs` ๊ฐ์ฒด๋Š” [`~modeling_outputs.SequenceClassifierOutput`]์ž…๋‹ˆ๋‹ค. +์•„๋ž˜ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ฌธ์„œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, `loss`(์„ ํƒ์ ), `logits`, `hidden_states`(์„ ํƒ์ ) ๋ฐ `attentions`(์„ ํƒ์ ) ํ•ญ๋ชฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” `labels`๋ฅผ ์ „๋‹ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— `loss`๊ฐ€ ์žˆ์ง€๋งŒ `hidden_states`์™€ `attentions`๊ฐ€ ์—†๋Š”๋ฐ, ์ด๋Š” `output_hidden_states=True` ๋˜๋Š” `output_attentions=True`๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + + + +`output_hidden_states=True`๋ฅผ ์ „๋‹ฌํ•  ๋•Œ `outputs.hidden_states[-1]`๊ฐ€ `outputs.last_hidden_state`์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ํ•ญ์ƒ ๊ทธ๋Ÿฐ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋ฐ˜ํ™˜๋  ๋•Œ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ›„์† ํ”„๋กœ์„ธ์Šค๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. + + + + +์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐ ์†์„ฑ๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ชจ๋ธ์ด ํ•ด๋‹น ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ `None`์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ์—์„œ๋Š” `outputs.loss`๋Š” ๋ชจ๋ธ์—์„œ ๊ณ„์‚ฐํ•œ ์†์‹ค์ด๊ณ  `outputs.attentions`๋Š” `None`์ž…๋‹ˆ๋‹ค. + +`outputs` ๊ฐ์ฒด๋ฅผ ํŠœํ”Œ๋กœ ๊ฐ„์ฃผํ•  ๋•Œ๋Š” `None` ๊ฐ’์ด ์—†๋Š” ์†์„ฑ๋งŒ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. +์˜ˆ์‹œ์—์„œ๋Š” `loss`์™€ `logits`๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์š”์†Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, + +```python +outputs[:2] +``` + +๋Š” `(outputs.loss, outputs.logits)` ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +`outputs` ๊ฐ์ฒด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๊ฐ„์ฃผํ•  ๋•Œ๋Š” `None` ๊ฐ’์ด ์—†๋Š” ์†์„ฑ๋งŒ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. +์˜ˆ์‹œ์—๋Š” `loss`์™€ `logits`๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ‚ค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ๋ชจ๋ธ ์œ ํ˜•์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ฐ˜ ๋ชจ๋ธ ์ถœ๋ ฅ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์ธ ์ถœ๋ ฅ ์œ ํ˜•์€ ํ•ด๋‹น ๋ชจ๋ธ ํŽ˜์ด์ง€์— ๋ฌธ์„œํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +## ModelOutput[[transformers.utils.ModelOutput]] + +[[autodoc]] utils.ModelOutput + - to_tuple + +## BaseModelOutput[[transformers.BaseModelOutput]] + +[[autodoc]] modeling_outputs.BaseModelOutput + +## BaseModelOutputWithPooling[[transformers.modeling_outputs.BaseModelOutputWithPooling]] + +[[autodoc]] modeling_outputs.BaseModelOutputWithPooling + +## BaseModelOutputWithCrossAttentions[[transformers.modeling_outputs.BaseModelOutputWithCrossAttentions]] + +[[autodoc]] modeling_outputs.BaseModelOutputWithCrossAttentions + +## BaseModelOutputWithPoolingAndCrossAttentions[[transformers.modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions]] + +[[autodoc]] modeling_outputs.BaseModelOutputWithPoolingAndCrossAttentions + +## BaseModelOutputWithPast[[transformers.modeling_outputs.BaseModelOutputWithPast]] + +[[autodoc]] modeling_outputs.BaseModelOutputWithPast + +## BaseModelOutputWithPastAndCrossAttentions[[transformers.modeling_outputs.BaseModelOutputWithPastAndCrossAttentions]] + +[[autodoc]] modeling_outputs.BaseModelOutputWithPastAndCrossAttentions + +## Seq2SeqModelOutput[[transformers.modeling_outputs.Seq2SeqModelOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqModelOutput + +## CausalLMOutput[[transformers.modeling_outputs.CausalLMOutput]] + +[[autodoc]] modeling_outputs.CausalLMOutput + +## CausalLMOutputWithCrossAttentions[[transformers.modeling_outputs.CausalLMOutputWithCrossAttentions]] + +[[autodoc]] modeling_outputs.CausalLMOutputWithCrossAttentions + +## CausalLMOutputWithPast[[transformers.modeling_outputs.CausalLMOutputWithPast]] + +[[autodoc]] modeling_outputs.CausalLMOutputWithPast + +## MaskedLMOutput[[transformers.modeling_outputs.MaskedLMOutput]] + +[[autodoc]] modeling_outputs.MaskedLMOutput + +## Seq2SeqLMOutput[[transformers.modeling_outputs.Seq2SeqLMOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqLMOutput + +## NextSentencePredictorOutput[[transformers.modeling_outputs.NextSentencePredictorOutput]] + +[[autodoc]] modeling_outputs.NextSentencePredictorOutput + +## SequenceClassifierOutput[[transformers.modeling_outputs.SequenceClassifierOutput]] + +[[autodoc]] modeling_outputs.SequenceClassifierOutput + +## Seq2SeqSequenceClassifierOutput[[transformers.modeling_outputs.Seq2SeqSequenceClassifierOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqSequenceClassifierOutput + +## MultipleChoiceModelOutput[[transformers.modeling_outputs.MultipleChoiceModelOutput]] + +[[autodoc]] modeling_outputs.MultipleChoiceModelOutput + +## TokenClassifierOutput[[transformers.modeling_outputs.TokenClassifierOutput]] + +[[autodoc]] modeling_outputs.TokenClassifierOutput + +## QuestionAnsweringModelOutput[[transformers.modeling_outputs.QuestionAnsweringModelOutput]] + +[[autodoc]] modeling_outputs.QuestionAnsweringModelOutput + +## Seq2SeqQuestionAnsweringModelOutput[[transformers.modeling_outputs.Seq2SeqQuestionAnsweringModelOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqQuestionAnsweringModelOutput + +## Seq2SeqSpectrogramOutput[[transformers.modeling_outputs.Seq2SeqSpectrogramOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqSpectrogramOutput + +## SemanticSegmenterOutput[[transformers.modeling_outputs.SemanticSegmenterOutput]] + +[[autodoc]] modeling_outputs.SemanticSegmenterOutput + +## ImageClassifierOutput[[transformers.modeling_outputs.ImageClassifierOutput]] + +[[autodoc]] modeling_outputs.ImageClassifierOutput + +## ImageClassifierOutputWithNoAttention[[transformers.modeling_outputs.ImageClassifierOutputWithNoAttention]] + +[[autodoc]] modeling_outputs.ImageClassifierOutputWithNoAttention + +## DepthEstimatorOutput[[transformers.modeling_outputs.DepthEstimatorOutput]] + +[[autodoc]] modeling_outputs.DepthEstimatorOutput + +## Wav2Vec2BaseModelOutput[[transformers.modeling_outputs.Wav2Vec2BaseModelOutput]] + +[[autodoc]] modeling_outputs.Wav2Vec2BaseModelOutput + +## XVectorOutput[[transformers.modeling_outputs.XVectorOutput]] + +[[autodoc]] modeling_outputs.XVectorOutput + +## Seq2SeqTSModelOutput[[transformers.modeling_outputs.Seq2SeqTSModelOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqTSModelOutput + +## Seq2SeqTSPredictionOutput[[transformers.modeling_outputs.Seq2SeqTSPredictionOutput]] + +[[autodoc]] modeling_outputs.Seq2SeqTSPredictionOutput + +## SampleTSPredictionOutput[[transformers.modeling_outputs.SampleTSPredictionOutput]] + +[[autodoc]] modeling_outputs.SampleTSPredictionOutput + +## TFBaseModelOutput[[transformers.modeling_outputs.TFBaseModelOutput]] + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutput + +## TFBaseModelOutputWithPooling[[transformers.modeling_tf_outputs.TFBaseModelOutputWithPooling]] + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPooling + +## TFBaseModelOutputWithPoolingAndCrossAttentions[[transformers.modeling_tf_outputs.TFBaseModelOutputWithPoolingAndCrossAttentions]] + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPoolingAndCrossAttentions + +## TFBaseModelOutputWithPast[[transformers.modeling_tf_outputs.TFBaseModelOutputWithPast]] + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPast + +## TFBaseModelOutputWithPastAndCrossAttentions[[transformers.modeling_tf_outputs.TFBaseModelOutputWithPastAndCrossAttentions]] + +[[autodoc]] modeling_tf_outputs.TFBaseModelOutputWithPastAndCrossAttentions + +## TFSeq2SeqModelOutput[[transformers.modeling_tf_outputs.TFSeq2SeqModelOutput]] + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqModelOutput + +## TFCausalLMOutput[[transformers.modeling_tf_outputs.TFCausalLMOutput]] + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutput + +## TFCausalLMOutputWithCrossAttentions[[transformers.modeling_tf_outputs.TFCausalLMOutputWithCrossAttentions]] + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithCrossAttentions + +## TFCausalLMOutputWithPast[[transformers.modeling_tf_outputs.TFCausalLMOutputWithPast]] + +[[autodoc]] modeling_tf_outputs.TFCausalLMOutputWithPast + +## TFMaskedLMOutput[[transformers.modeling_tf_outputs.TFMaskedLMOutput]] + +[[autodoc]] modeling_tf_outputs.TFMaskedLMOutput + +## TFSeq2SeqLMOutput[[transformers.modeling_tf_outputs.TFSeq2SeqLMOutput]] + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqLMOutput + +## TFNextSentencePredictorOutput[[transformers.modeling_tf_outputs.TFNextSentencePredictorOutput]] + +[[autodoc]] modeling_tf_outputs.TFNextSentencePredictorOutput + +## TFSequenceClassifierOutput[[transformers.modeling_tf_outputs.TFSequenceClassifierOutput]] + +[[autodoc]] modeling_tf_outputs.TFSequenceClassifierOutput + +## TFSeq2SeqSequenceClassifierOutput[[transformers.modeling_tf_outputs.TFSeq2SeqSequenceClassifierOutput]] + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqSequenceClassifierOutput + +## TFMultipleChoiceModelOutput[[transformers.modeling_tf_outputs.TFMultipleChoiceModelOutput]] + +[[autodoc]] modeling_tf_outputs.TFMultipleChoiceModelOutput + +## TFTokenClassifierOutput[[transformers.modeling_tf_outputs.TFTokenClassifierOutput]] + +[[autodoc]] modeling_tf_outputs.TFTokenClassifierOutput + +## TFQuestionAnsweringModelOutput[[transformers.modeling_tf_outputs.TFQuestionAnsweringModelOutput]] + +[[autodoc]] modeling_tf_outputs.TFQuestionAnsweringModelOutput + +## TFSeq2SeqQuestionAnsweringModelOutput[[transformers.modeling_tf_outputs.TFSeq2SeqQuestionAnsweringModelOutput]] + +[[autodoc]] modeling_tf_outputs.TFSeq2SeqQuestionAnsweringModelOutput + +## FlaxBaseModelOutput[[transformers.modeling_flax_outputs.FlaxBaseModelOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutput + +## FlaxBaseModelOutputWithPast[[transformers.modeling_flax_outputs.FlaxBaseModelOutputWithPast]] + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPast + +## FlaxBaseModelOutputWithPooling[[transformers.modeling_flax_outputs.FlaxBaseModelOutputWithPooling]] + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPooling + +## FlaxBaseModelOutputWithPastAndCrossAttentions[[transformers.modeling_flax_outputs.FlaxBaseModelOutputWithPastAndCrossAttentions]] + +[[autodoc]] modeling_flax_outputs.FlaxBaseModelOutputWithPastAndCrossAttentions + +## FlaxSeq2SeqModelOutput[[transformers.modeling_flax_outputs.FlaxSeq2SeqModelOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqModelOutput + +## FlaxCausalLMOutputWithCrossAttentions[[transformers.modeling_flax_outputs.FlaxCausalLMOutputWithCrossAttentions]] + +[[autodoc]] modeling_flax_outputs.FlaxCausalLMOutputWithCrossAttentions + +## FlaxMaskedLMOutput[[transformers.modeling_flax_outputs.FlaxMaskedLMOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxMaskedLMOutput + +## FlaxSeq2SeqLMOutput[[transformers.modeling_flax_outputs.FlaxSeq2SeqLMOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqLMOutput + +## FlaxNextSentencePredictorOutput[[transformers.modeling_flax_outputs.FlaxNextSentencePredictorOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxNextSentencePredictorOutput + +## FlaxSequenceClassifierOutput[[transformers.modeling_flax_outputs.FlaxSequenceClassifierOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxSequenceClassifierOutput + +## FlaxSeq2SeqSequenceClassifierOutput[[transformers.modeling_flax_outputs.FlaxSeq2SeqSequenceClassifierOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqSequenceClassifierOutput + +## FlaxMultipleChoiceModelOutput[[transformers.modeling_flax_outputs.FlaxMultipleChoiceModelOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxMultipleChoiceModelOutput + +## FlaxTokenClassifierOutput[[transformers.modeling_flax_outputs.FlaxTokenClassifierOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxTokenClassifierOutput + +## FlaxQuestionAnsweringModelOutput[[transformers.modeling_flax_outputs.FlaxQuestionAnsweringModelOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxQuestionAnsweringModelOutput + +## FlaxSeq2SeqQuestionAnsweringModelOutput[[transformers.modeling_flax_outputs.FlaxSeq2SeqQuestionAnsweringModelOutput]] + +[[autodoc]] modeling_flax_outputs.FlaxSeq2SeqQuestionAnsweringModelOutput diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/peft.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/peft.md new file mode 100644 index 0000000000000000000000000000000000000000..662b4b495fe4e31f1cc2cc5fe3f1a2deaa3ea194 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/peft.md @@ -0,0 +1,23 @@ + + +# PEFT[[transformers.integrations.PeftAdapterMixin]] + +[`~integrations.PeftAdapterMixin`]์€ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ•จ๊ป˜ ์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก [PEFT](https://huggingface.co/docs/peft/index) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฏน์Šค์ธ์€ ํ˜„์žฌ LoRA, IA3, AdaLora๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ฆฌํ”ฝ์Šค ํŠœ๋‹ ๋ฐฉ๋ฒ•๋“ค(ํ”„๋กฌํ”„ํŠธ ํŠœ๋‹, ํ”„๋กฌํ”„ํŠธ ํ•™์Šต)์€ torch ๋ชจ๋“ˆ์— ์‚ฝ์ž…ํ•  ์ˆ˜ ์—†๋Š” ๊ตฌ์กฐ์ด๋ฏ€๋กœ ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +[[autodoc]] integrations.PeftAdapterMixin + - load_adapter + - add_adapter + - set_adapter + - disable_adapters + - enable_adapters + - active_adapters + - get_adapter_state_dict diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/pipelines.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/pipelines.md new file mode 100644 index 0000000000000000000000000000000000000000..70248fd51925645d53da5ed3edfb0519989088fd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/pipelines.md @@ -0,0 +1,469 @@ + + +# ํŒŒ์ดํ”„๋ผ์ธ [[pipelines]] + +ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ชจ๋ธ์„ ์ถ”๋ก ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ›Œ๋ฅญํ•˜๊ณ  ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๋Œ€๋ถ€๋ถ„ ์ถ”์ƒํ™”ํ•˜์—ฌ, ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition), ๋งˆ์Šคํฌ๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง(Masked Language Modeling), ๊ฐ์ • ๋ถ„์„(Sentiment Analysis), ํŠน์„ฑ ์ถ”์ถœ(Feature Extraction), ์งˆ์˜์‘๋‹ต(Question Answering) ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์ž‘์—…์— ํŠนํ™”๋œ ๊ฐ„๋‹จํ•œ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” [์ž‘์—… ์š”์•ฝ](../task_summary)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +ํŒŒ์ดํ”„๋ผ์ธ ์ถ”์ƒํ™”๋Š” ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. + +- \[`ํŒŒ์ดํ”„๋ผ์ธ`]์€ ๋‹ค๋ฅธ ๋ชจ๋“  ํŒŒ์ดํ”„๋ผ์ธ์„ ์บก์Аํ™”ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. +- ์ž‘์—…๋ณ„ ํŒŒ์ดํ”„๋ผ์ธ์€ [์˜ค๋””์˜ค](#audio), [์ปดํ“จํ„ฐ ๋น„์ „](#computer-vision), [์ž์—ฐ์–ด ์ฒ˜๋ฆฌ](#natural-language-processing), [๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ](#multimodal) ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ํŒŒ์ดํ”„๋ผ์ธ ์ถ”์ƒํ™” [[the-pipeline-abstraction]] + +*ํŒŒ์ดํ”„๋ผ์ธ* ์ถ”์ƒํ™”๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ฐ์‹ธ๋Š” ๋ž˜ํผ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŒŒ์ดํ”„๋ผ์ธ์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šคํ™”๋˜๋ฉฐ, ์ถ”๊ฐ€์ ์ธ ํŽธ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +๋‹จ์ผ ํ•ญ๋ชฉ ํ˜ธ์ถœ ์˜ˆ์‹œ: + +```python +>>> pipe = pipeline("text-classification") +>>> pipe("This restaurant is awesome") +[{'label': 'POSITIVE', 'score': 0.9998743534088135}] +``` + +[hub](https://huggingface.co)์—์„œ ํŠน์ • ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ชจ๋ธ์ด ์ด๋ฏธ ํ—ˆ๋ธŒ์— ์ž‘์—…์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ž‘์—…๋ช…์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +>>> pipe = pipeline(model="FacebookAI/roberta-large-mnli") +>>> pipe("This restaurant is awesome") +[{'label': 'NEUTRAL', 'score': 0.7313136458396912}] +``` + +์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด *๋ฆฌ์ŠคํŠธ*๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”. + +```python +>>> pipe = pipeline("text-classification") +>>> pipe(["This restaurant is awesome", "This restaurant is awful"] ) +[{'label': 'POSITIVE', 'score': 0.9998743534088135}, + {'label': 'NEGATIVE', 'score': 0.9996669292449951}] +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ์…‹์„ ์ˆœํšŒํ•˜๋ ค๋ฉด `dataset`์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆด ํ•„์š”๋„ ์—†๊ณ , ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. +์ด ๋ฐฉ์‹์€ GPU์—์„œ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฃจํ”„์™€ ์œ ์‚ฌํ•œ ์†๋„๋กœ ์ž‘๋™ํ•˜๋ฉฐ, ๋งŒ์•ฝ ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ด์Šˆ๋ฅผ ๋“ฑ๋กํ•ด ์ฃผ์„ธ์š”. + +```python +import datasets +from transformers import pipeline +from transformers.pipelines.pt_utils import KeyDataset +from tqdm.auto import tqdm + +pipe = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h", device=0) +dataset = datasets.load_dataset("superb", name="asr", split="test") + +# KeyDataset (*pt* ์ „์šฉ)๋Š” ๋ฐ์ดํ„ฐ์…‹ ํ•ญ๋ชฉ์˜ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์ง€์ •๋œ ํ‚ค๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +# ์ด ์˜ˆ์ œ์—์„œ๋Š” *target* ํ•ญ๋ชฉ์ด ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ KeyDataset์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ ์Œ ์ž…๋ ฅ์—๋Š” KeyPairDataset์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +for out in tqdm(pipe(KeyDataset(dataset, "file"))): + print(out) + # {"text": "NUMBER TEN FRESH NELLY IS WAITING ON YOU GOOD NIGHT HUSBAND"} + # {"text": ....} + # .... +``` + +๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + +```python +from transformers import pipeline + +pipe = pipeline("text-classification") + +def data(): + while True: + # ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ์…‹, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ ๋˜๋Š” HTTP ์š”์ฒญ์—์„œ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + # ์„œ๋ฒ„์—์„œ + # ์ฃผ์˜: ๋ฐ˜๋ณต์ ์ด๋ฏ€๋กœ `num_workers > 1` ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + # ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ์ „ํžˆ + # ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๊ฐ€ ๋Œ€๊ทœ๋ชจ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + yield "This is a test" + +for out in pipe(data()): + print(out) + # {"text": "NUMBER TEN FRESH NELLY IS WAITING ON YOU GOOD NIGHT HUSBAND"} + # {"text": ....} + # .... +``` + +\[\[autodoc]] pipeline + +## ํŒŒ์ดํ”„๋ผ์ธ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ [[pipeline-batching]] + +๋ชจ๋“  ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ, `Dataset`, `Generator` ์ „๋‹ฌ ์‹œ ์ŠคํŠธ๋ฆฌ๋ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ๋•Œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +```python +from transformers import pipeline +from transformers.pipelines.pt_utils import KeyDataset +import datasets + +dataset = datasets.load_dataset("imdb", name="plain_text", split="unsupervised") +pipe = pipeline("text-classification", device=0) +for out in pipe(KeyDataset(dataset, "text"), batch_size=8, truncation="only_first"): + print(out) + # [{'label': 'POSITIVE', 'score': 0.9998743534088135}] + # ์ด์ „๊ณผ ๋™์ผํ•œ ์ถœ๋ ฅ์ด์ง€๋งŒ, ๋‚ด์šฉ์„ ๋ฐฐ์น˜๋กœ ๋ชจ๋ธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +``` + + + +ํ•˜์ง€๋งŒ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•ญ์ƒ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ•˜๋“œ์›จ์–ด, ๋ฐ์ดํ„ฐ, ๋ชจ๋ธ์— ๋”ฐ๋ผ ์†๋„๊ฐ€ 10๋ฐฐ๋กœ ๋นจ๋ผ์งˆ์ˆ˜๋„, 5๋ฐฐ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ฃผ๋กœ ์†๋„ ํ–ฅ์ƒ์ด ์žˆ๋Š” ์˜ˆ์‹œ: + + + +```python +from transformers import pipeline +from torch.utils.data import Dataset +from tqdm.auto import tqdm + +pipe = pipeline("text-classification", device=0) + +class MyDataset(Dataset): + def __len__(self): + return 5000 + + def __getitem__(self, i): + return "This is a test" + +dataset = MyDataset() + +for batch_size in [1, 8, 64, 256]: + print("-" * 30) + print(f"Streaming batch_size={batch_size}") + for out in tqdm(pipe(dataset, batch_size=batch_size), total=len(dataset)): + pass +``` + +``` +# On GTX 970 +------------------------------ +Streaming no batching +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:26<00:00, 187.52it/s] +------------------------------ +Streaming batch_size=8 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:04<00:00, 1205.95it/s] +------------------------------ +Streaming batch_size=64 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:02<00:00, 2478.24it/s] +------------------------------ +Streaming batch_size=256 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 5000/5000 [00:01<00:00, 2554.43it/s] +(diminishing returns, saturated the GPU) +``` + +์ฃผ๋กœ ์†๋„ ์ €ํ•˜๊ฐ€ ์žˆ๋Š” ์˜ˆ์‹œ: + +```python +class MyDataset(Dataset): + def __len__(self): + return 5000 + + def __getitem__(self, i): + if i % 64 == 0: + n = 100 + else: + n = 1 + return "This is a test" * n +``` + +์ด๋Š” ๋‹ค๋ฅธ ๋ฌธ์žฅ๋“ค์— ๋น„ํ•ด ๊ฐ„ํ—์ ์œผ๋กœ ๋งค์šฐ ๊ธด ๋ฌธ์žฅ์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ **์ „์ฒด** ๋ฐฐ์น˜๊ฐ€ 400ํ† ํฐ ๊ธธ์ด๋กœ +([64, 400]) ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ, [64, 4] ๋Œ€์‹  [64, 400]์ด ๋˜์–ด ํฌ๊ฒŒ ์†๋„๊ฐ€ ์ €ํ•˜๋ฉ๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, ๋” ํฐ ๋ฐฐ์น˜์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +``` +------------------------------ +Streaming no batching +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:05<00:00, 183.69it/s] +------------------------------ +Streaming batch_size=8 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:03<00:00, 265.74it/s] +------------------------------ +Streaming batch_size=64 +100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1000/1000 [00:26<00:00, 37.80it/s] +------------------------------ +Streaming batch_size=256 + 0%| | 0/1000 [00:00 + for out in tqdm(pipe(dataset, batch_size=256), total=len(dataset)): +.... + q = q / math.sqrt(dim_per_head) # (bs, n_heads, q_length, dim_per_head) +RuntimeError: CUDA out of memory. Tried to allocate 376.00 MiB (GPU 0; 3.95 GiB total capacity; 1.72 GiB already allocated; 354.88 MiB free; 2.46 GiB reserved in total by PyTorch) +``` + +์ผ๋ฐ˜์ ์ธ ํ•ด๊ฒฐ์ฑ…์€ ์—†์œผ๋ฉฐ, ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ๊ฒฝํ—˜์ƒ ์ง€์นจ: + +- **ํ•˜๋“œ์›จ์–ด์™€ ์‹ค์ œ ์›Œํฌ๋กœ๋“œ๋กœ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜์„ธ์š”. ์ธก์ •์ด ๋‹ต์ž…๋‹ˆ๋‹ค.** +- ์‹ค์‹œ๊ฐ„ ์ถ”๋ก (latency)์ด ์ค‘์š”ํ•˜๋‹ค๋ฉด ๋ฐฐ์น˜ ์ฒ˜๋ฆฌํ•˜์ง€ ๋งˆ์„ธ์š”. +- CPU ์‚ฌ์šฉ ์‹œ์—๋„ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +- GPU์—์„œ ์ •์  ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ(throughput)๊ฐ€ ๋ชฉ์ ์ด๋ผ๋ฉด + + - ์ž…๋ ฅ ์‹œํ€€์Šค ๊ธธ์ด("์‹ค์ œ" ๋ฐ์ดํ„ฐ)๋ฅผ ์ž˜ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ง๊ณ  ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋ฉด์„œ ์ž„์‹œ๋กœ ๋ฐฐ์น˜๋ฅผ ์ ์šฉํ•ด ๋ณด๊ณ , ์‹คํŒจ ์‹œ ์ด๋ฅผ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๋„๋ก OOM ๊ฒ€์‚ฌ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”. (์‹œํ€€์Šค ๊ธธ์ด๋ฅผ ์ œ์–ดํ•˜์ง€ ์•Š์œผ๋ฉด ์–ธ์  ๊ฐ€๋Š” ์‹คํŒจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.) + - ์‹œํ€€์Šค ๊ธธ์ด๊ฐ€ ์ผ์ •ํ•˜๋‹ค๋ฉด ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๊ฐ€ ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธก์ •ํ•˜๋ฉฐ OOM๊นŒ์ง€ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”. + - GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํด์ˆ˜๋ก ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ์˜ ์ด์ ์ด ํฝ๋‹ˆ๋‹ค. +- ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ ํ™œ์„ฑํ™” ์‹œ OOM์„ ํ•ธ๋“ค๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ๋Œ€๋น„ํ•˜์„ธ์š”. + +## ํŒŒ์ดํ”„๋ผ์ธ ์ฒญํฌ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ [[pipeline-chunk-batching]] + +`์ œ๋กœ์ƒท ๋ถ„๋ฅ˜` ๋ฐ `์งˆ์˜์‘๋‹ต` ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‹จ์ผ ์ž…๋ ฅ์ด ์—ฌ๋Ÿฌ ํฌ์›Œ๋“œ ํŒจ์Šค๋ฅผ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์–ด `๋ฐฐ์น˜ ํฌ๊ธฐ` ์ธ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‘ ํŒŒ์ดํ”„๋ผ์ธ์€ `์ฒญํฌ ํŒŒ์ดํ”„๋ผ์ธ` ํ˜•ํƒœ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด + +```python +preprocessed = pipe.preprocess(inputs) +model_outputs = pipe.forward(preprocessed) +outputs = pipe.postprocess(model_outputs) +``` + +์ด์ œ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” + +```python +all_model_outputs = [] +for preprocessed in pipe.preprocess(inputs): + model_outputs = pipe.forward(preprocessed) + all_model_outputs.append(model_outputs) +outputs = pipe.postprocess(all_model_outputs) +``` + +ํŒŒ์ดํ”„๋ผ์ธ์˜ ์‚ฌ์šฉ ๋ฐฉ์‹์ด ๋™์ผํ•˜๋ฏ€๋กœ, ์ฝ”๋“œ์—๋Š” ๊ฑฐ์˜ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ์ด ๋ช‡ ๋ฒˆ์˜ ํฌ์›Œ๋“œ ํŒจ์Šค๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š”์ง€ ๊ณ ๋ คํ•  ํ•„์š” ์—†์ด, `๋ฐฐ์น˜ ํฌ๊ธฐ`๋Š” ์ž…๋ ฅ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹ค๋งŒ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ฃผ์˜์‚ฌํ•ญ์€ ์—ฌ์ „ํžˆ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. + +## ํŒŒ์ดํ”„๋ผ์ธ FP16 ์ถ”๋ก  [[pipeline-fp16-inference]] + +๋ชจ๋ธ์€ FP16 ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, GPU์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๋ฉด์„œ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ์€ ์„ฑ๋Šฅ ์ €ํ•˜ ์—†์ด FP16์„ ์ง€์›ํ•˜๋ฉฐ, ๋ชจ๋ธ์ด ํด์ˆ˜๋ก ์„ฑ๋Šฅ ์ €ํ•˜ ๊ฐ€๋Šฅ์„ฑ์€ ๋” ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. + +FP16 ์ถ”๋ก ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ์ž์— `torch_dtype=torch.float16` ๋˜๋Š” `torch_dtype='float16'`์„ ์ „๋‹ฌํ•˜์„ธ์š”. ์ด ๊ธฐ๋Šฅ์€ ํŒŒ์ดํ† ์น˜ ๋ฐฑ์—”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์—์„œ๋งŒ ์ž‘๋™ํ•˜๋ฉฐ, ์ž…๋ ฅ์€ ๋‚ด๋ถ€์ ์œผ๋กœ FP16 ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. + +## ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ [[pipeline-custom-code]] + +ํŠน์ • ํŒŒ์ดํ”„๋ผ์ธ์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜๋ ค๋ฉด, ๋จผ์ € ํ•ด๋‹น ์ž‘์—…์— ๋Œ€ํ•œ ์ด์Šˆ๋ฅผ ๋“ฑ๋กํ•ด ์ฃผ์„ธ์š”. ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋ชฉํ‘œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, `transformers` ํŒ€์ด ์ถ”๊ฐ€ ์ง€์›์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฐ„๋‹จํžˆ ์‹œ๋„ํ•˜๋ ค๋ฉด ํŒŒ์ดํ”„๋ผ์ธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์„ธ์š”. + +```python +class MyPipeline(TextClassificationPipeline): + def postprocess(): + # ์‚ฌ์šฉ์ž ์ •์˜ ํ›„์ฒ˜๋ฆฌ ์ฝ”๋“œ ์ž‘์„ฑ + scores = scores * 100 + # ์ถ”๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ + +my_pipeline = MyPipeline(model=model, tokenizer=tokenizer, ...) +# ๋˜๋Š” *pipeline* ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ: +my_pipeline = pipeline(model="xxxx", pipeline_class=MyPipeline) +``` + +์ด๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๋ชจ๋“  ์ปค์Šคํ…€ ์ฝ”๋“œ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ [[implementing-a-pipeline]] + +[์ƒˆ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌํ˜„](../add_new_pipeline) + +## ์˜ค๋””์˜ค [[audio]] + +์˜ค๋””์˜ค ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +### AudioClassificationPipeline [[transformers.AudioClassificationPipeline]] + +[[autodoc]] AudioClassificationPipeline + - __call__ + - all + +### AutomaticSpeechRecognitionPipeline [[transformers.AutomaticSpeechRecognitionPipeline]] + +[[autodoc]] AutomaticSpeechRecognitionPipeline + - __call__ + - all + +### TextToAudioPipeline [[transformers.TextToAudioPipeline]] + +[[autodoc]] TextToAudioPipeline + - __call__ + - all + + +### ZeroShotAudioClassificationPipeline [[transformers.ZeroShotAudioClassificationPipeline]] + +[[autodoc]] ZeroShotAudioClassificationPipeline + - __call__ + - all + +## ์ปดํ“จํ„ฐ ๋น„์ „ [[computer-vision]] + +์ปดํ“จํ„ฐ ๋น„์ „ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +### DepthEstimationPipeline [[transformers.DepthEstimationPipeline]] +[[autodoc]] DepthEstimationPipeline + - __call__ + - all + +### ImageClassificationPipeline [[transformers.ImageClassificationPipeline]] + +[[autodoc]] ImageClassificationPipeline + - __call__ + - all + +### ImageSegmentationPipeline [[transformers.ImageSegmentationPipeline]] + +[[autodoc]] ImageSegmentationPipeline + - __call__ + - all + +### ImageToImagePipeline [[transformers.ImageToImagePipeline]] + +[[autodoc]] ImageToImagePipeline + - __call__ + - all + +### ObjectDetectionPipeline [[transformers.ObjectDetectionPipeline]] + +[[autodoc]] ObjectDetectionPipeline + - __call__ + - all + +### VideoClassificationPipeline [[transformers.VideoClassificationPipeline]] + +[[autodoc]] VideoClassificationPipeline + - __call__ + - all + +### ZeroShotImageClassificationPipeline [[transformers.ZeroShotImageClassificationPipeline]] + +[[autodoc]] ZeroShotImageClassificationPipeline + - __call__ + - all + +### ZeroShotObjectDetectionPipeline [[transformers.ZeroShotObjectDetectionPipeline]] + +[[autodoc]] ZeroShotObjectDetectionPipeline + - __call__ + - all + +## ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ [[natural-language-processing]] + +์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +### FillMaskPipeline [[transformers.FillMaskPipeline]] + +[[autodoc]] FillMaskPipeline + - __call__ + - all + +### QuestionAnsweringPipeline [[transformers.QuestionAnsweringPipeline]] + +[[autodoc]] QuestionAnsweringPipeline + - __call__ + - all + +### SummarizationPipeline [[transformers.SummarizationPipeline]] + +[[autodoc]] SummarizationPipeline + - __call__ + - all + +### TableQuestionAnsweringPipeline [[transformers.TableQuestionAnsweringPipeline]] + +[[autodoc]] TableQuestionAnsweringPipeline + - __call__ + +### TextClassificationPipeline [[transformers.TextClassificationPipeline]] + +[[autodoc]] TextClassificationPipeline + - __call__ + - all + +### TextGenerationPipeline [[transformers.TextGenerationPipeline]] + +[[autodoc]] TextGenerationPipeline + - __call__ + - all + +### Text2TextGenerationPipeline [[transformers.Text2TextGenerationPipeline]] + +[[autodoc]] Text2TextGenerationPipeline + - __call__ + - all + +### TokenClassificationPipeline [[transformers.TokenClassificationPipeline]] + +[[autodoc]] TokenClassificationPipeline + - __call__ + - all + +### TranslationPipeline [[transformers.TranslationPipeline]] + +[[autodoc]] TranslationPipeline + - __call__ + - all + +### ZeroShotClassificationPipeline [[transformers.ZeroShotClassificationPipeline]] + +[[autodoc]] ZeroShotClassificationPipeline + - __call__ + - all + +## ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ [[multimodal]] + +๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +### DocumentQuestionAnsweringPipeline [[transformers.DocumentQuestionAnsweringPipeline]] + +[[autodoc]] DocumentQuestionAnsweringPipeline + - __call__ + - all + +### FeatureExtractionPipeline [[transformers.FeatureExtractionPipeline]] + +[[autodoc]] FeatureExtractionPipeline + - __call__ + - all + +### ImageFeatureExtractionPipeline [[transformers.ImageFeatureExtractionPipeline]] + +[[autodoc]] ImageFeatureExtractionPipeline + - __call__ + - all + +### ImageToTextPipeline [[transformers.ImageToTextPipeline]] + +[[autodoc]] ImageToTextPipeline + - __call__ + - all + +### ImageTextToTextPipeline [[transformers.ImageTextToTextPipeline]] + +[[autodoc]] ImageTextToTextPipeline + - __call__ + - all + +### MaskGenerationPipeline [[transformers.MaskGenerationPipeline]] + +[[autodoc]] MaskGenerationPipeline + - __call__ + - all + +### VisualQuestionAnsweringPipeline [[transformers.VisualQuestionAnsweringPipeline]] + +[[autodoc]] VisualQuestionAnsweringPipeline + - __call__ + - all + +## Parent class: `Pipeline` [[transformers.Pipeline]] + +[[autodoc]] Pipeline diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/processors.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/processors.md new file mode 100644 index 0000000000000000000000000000000000000000..42d5a8f049486ab59557534b2b8b812002907d33 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/processors.md @@ -0,0 +1,145 @@ + + +# ํ”„๋กœ์„ธ์„œ[[processors]] + +Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํ”„๋กœ์„ธ์„œ๋Š” ๋‘ ๊ฐ€์ง€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค: +- [Wav2Vec2](../model_doc/wav2vec2) (์Œ์„ฑ๊ณผ ํ…์ŠคํŠธ) ๋˜๋Š” [CLIP](../model_doc/clip) (ํ…์ŠคํŠธ์™€ ๋น„์ „)๊ณผ ๊ฐ™์€ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ์ฒด +- GLUE ๋˜๋Š” SQUAD ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ด์ „ ๋ฒ„์ „์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ์‚ฌ์šฉ ์ค‘๋‹จ๋œ ๊ฐ์ฒด + +## ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํ”„๋กœ์„ธ์„œ[[transformers.ProcessorMixin]] + +๋ชจ๋“  ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ(ํ…์ŠคํŠธ, ๋น„์ „, ์˜ค๋””์˜ค)๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ฑฐ๋‚˜ ๋””์ฝ”๋”ฉํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ์ด๊ฒƒ์€ ํ”„๋กœ์„ธ์„œ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์„œ๋Š” ํ† ํฌ๋‚˜์ด์ €(ํ…์ŠคํŠธ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์šฉ), ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ(๋น„์ „์šฉ), ํŠน์„ฑ ์ถ”์ถœ๊ธฐ(์˜ค๋””์˜ค์šฉ) ๊ฐ™์ด ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ฒ˜๋ฆฌ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์Šต๋‹ˆ๋‹ค. + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋Š” ์ €์žฅ ๋ฐ ๋กœ๋”ฉ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋‹ค์Œ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค: + +[[autodoc]] ProcessorMixin + +## ์‚ฌ์šฉ ์ค‘๋‹จ๋œ ํ”„๋กœ์„ธ์„œ[[transformers.DataProcessor]] + +๋ชจ๋“  ํ”„๋กœ์„ธ์„œ๋Š” [`~data.processors.utils.DataProcessor`]์™€ ๊ฐ™์€ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์„œ๋Š” [`~data.processors.utils.InputExample`]์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด [`~data.processors.utils.InputExample`]๋“ค์€ ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด [`~data.processors.utils.InputFeatures`]๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[[autodoc]] data.processors.utils.DataProcessor + +[[autodoc]] data.processors.utils.InputExample + +[[autodoc]] data.processors.utils.InputFeatures + +## GLUE[[transformers.glue_convert_examples_to_features]] + +[General Language Understanding Evaluation (GLUE)](https://gluebenchmark.com/)๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ์กด NLU ์ž‘์—…์—์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฒค์น˜๋งˆํฌ์ž…๋‹ˆ๋‹ค. [GLUE: A multi-task benchmark and analysis platform for natural language understanding](https://openreview.net/pdf?id=rJ4km2R5t7) ๋…ผ๋ฌธ๊ณผ ํ•จ๊ป˜ ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” MRPC, MNLI, MNLI (๋ถˆ์ผ์น˜), CoLA, SST2, STSB, QQP, QNLI, RTE, WNLI ์ด 10๊ฐœ ์ž‘์—…์— ๋Œ€ํ•œ ํ”„๋กœ์„ธ์„œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +- [`~data.processors.utils.MrpcProcessor`] +- [`~data.processors.utils.MnliProcessor`] +- [`~data.processors.utils.MnliMismatchedProcessor`] +- [`~data.processors.utils.Sst2Processor`] +- [`~data.processors.utils.StsbProcessor`] +- [`~data.processors.utils.QqpProcessor`] +- [`~data.processors.utils.QnliProcessor`] +- [`~data.processors.utils.RteProcessor`] +- [`~data.processors.utils.WnliProcessor`] + +๋˜ํ•œ, ์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์™€ [`~data.processors.utils.InputExample`] ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[[autodoc]] data.processors.glue.glue_convert_examples_to_features + + +## XNLI[[xnli]] + +[The Cross-Lingual NLI Corpus (XNLI)](https://www.nyu.edu/projects/bowman/xnli/)๋Š” ๊ต์ฐจ์–ธ์–ด ํ…์ŠคํŠธ ํ‘œํ˜„์˜ ํ’ˆ์งˆ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฒค์น˜๋งˆํฌ์ž…๋‹ˆ๋‹ค. XNLI๋Š” [*MultiNLI*](http://www.nyu.edu/projects/bowman/multinli/)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํฌ๋ผ์šฐ๋“œ์†Œ์‹ฑ ๋ฐ์ดํ„ฐ ์„ธํŠธ์ž…๋‹ˆ๋‹ค: ํ…์ŠคํŠธ ์Œ์€ 15๊ฐœ ์–ธ์–ด(์˜์–ด ๊ฐ™์€ ๊ณ ์ž์› ์–ธ์–ด๋ถ€ํ„ฐ ์Šค์™€ํž๋ฆฌ์–ด ๊ฐ™์€ ์ €์ž์› ์–ธ์–ด๊นŒ์ง€)์— ๋Œ€ํ•ด ํ…์ŠคํŠธ ํ•จ์˜ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ ˆ์ด๋ธ”๋ง๋ฉ๋‹ˆ๋‹ค. + +[XNLI: Evaluating Cross-lingual Sentence Representations](https://huggingface.co/papers/1809.05053) ๋…ผ๋ฌธ๊ณผ ํ•จ๊ป˜ ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” XNLI ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ”„๋กœ์„ธ์„œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +- [`~data.processors.utils.XnliProcessor`] + +ํ…Œ์ŠคํŠธ ์„ธํŠธ์— ๊ณจ๋“œ ๋ ˆ์ด๋ธ”์ด ์ œ๊ณต๋˜๋ฏ€๋กœ, ํ‰๊ฐ€๋Š” ํ…Œ์ŠคํŠธ ์„ธํŠธ์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ๋Š” [run_xnli.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification/run_xnli.py) ์Šคํฌ๋ฆฝํŠธ์— ์ œ๊ณต๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + + +## SQuAD[[squad]] + +[The Stanford Question Answering Dataset (SQuAD)](https://rajpurkar.github.io/SQuAD-explorer//)๋Š” ์งˆ๋ฌธ ๋‹ต๋ณ€์—์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฒค์น˜๋งˆํฌ์ž…๋‹ˆ๋‹ค. v1.1๊ณผ v2.0 ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „(v1.1)์€ [SQuAD: 100,000+ Questions for Machine Comprehension of Text](https://huggingface.co/papers/1606.05250) ๋…ผ๋ฌธ๊ณผ ํ•จ๊ป˜ ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „(v2.0)์€ [Know What You Don't Know: Unanswerable Questions for SQuAD](https://huggingface.co/papers/1806.03822) ๋…ผ๋ฌธ๊ณผ ํ•จ๊ป˜ ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‘ ๋ฒ„์ „ ๊ฐ๊ฐ์— ๋Œ€ํ•œ ํ”„๋กœ์„ธ์„œ๋ฅผ ํ˜ธ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค: + +### ํ”„๋กœ์„ธ์„œ[[transformers.data.processors.squad.SquadProcessor]] + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +- [`~data.processors.utils.SquadV1Processor`] +- [`~data.processors.utils.SquadV2Processor`] + +๋‘˜ ๋‹ค ์ถ”์ƒ ํด๋ž˜์Šค [`~data.processors.utils.SquadProcessor`]๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. + +[[autodoc]] data.processors.squad.SquadProcessor + - all + +๋˜ํ•œ, ๋‹ค์Œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ SQuAD ์˜ˆ์‹œ๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” [`~data.processors.utils.SquadFeatures`]๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[[autodoc]] data.processors.squad.squad_convert_examples_to_features + + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋“ค๊ณผ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ํŒŒ์ผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ *tensorflow_datasets* ํŒจํ‚ค์ง€์™€๋„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋Š” ์•„๋ž˜์— ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. + + +### ์‚ฌ์šฉ ์˜ˆ์‹œ[[example-usage]] + +๋‹ค์Œ์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์„ธ์„œ์™€ ๋ณ€ํ™˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: + +```python +# V2 ํ”„๋กœ์„ธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ +processor = SquadV2Processor() +examples = processor.get_dev_examples(squad_v2_data_dir) + +# V1 ํ”„๋กœ์„ธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ +processor = SquadV1Processor() +examples = processor.get_dev_examples(squad_v1_data_dir) + +features = squad_convert_examples_to_features( + examples=examples, + tokenizer=tokenizer, + max_seq_length=max_seq_length, + doc_stride=args.doc_stride, + max_query_length=max_query_length, + is_training=not evaluate, +) +``` + +*tensorflow_datasets* ์‚ฌ์šฉ์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์‚ฌ์šฉ๋งŒํผ ์‰ฝ์Šต๋‹ˆ๋‹ค: + +```python +# tensorflow_datasets๋Š” Squad V1๋งŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +tfds_examples = tfds.load("squad") +examples = SquadV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate) + +features = squad_convert_examples_to_features( + examples=examples, + tokenizer=tokenizer, + max_seq_length=max_seq_length, + doc_stride=args.doc_stride, + max_query_length=max_query_length, + is_training=not evaluate, +) +``` + +์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋Š” [run_squad.py](https://github.com/huggingface/transformers/tree/main/examples/legacy/question-answering/run_squad.py) ์Šคํฌ๋ฆฝํŠธ์— ์ œ๊ณต๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/quantization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/quantization.md new file mode 100644 index 0000000000000000000000000000000000000000..6f793f2210741788cf67a2e2a8232c654ea77484 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/quantization.md @@ -0,0 +1,75 @@ + + +# ์–‘์žํ™”[[quantization]] + + + +์–‘์žํ™” ๊ธฐ๋ฒ•์€ ๊ฐ€์ค‘์น˜์™€ ํ™œ์„ฑํ™”๋ฅผ 8๋น„ํŠธ ์ •์ˆ˜(int8)์™€ ๊ฐ™์€ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํ‘œํ˜„ํ•จ์œผ๋กœ์จ ๋ฉ”๋ชจ๋ฆฌ์™€ ๊ณ„์‚ฐ ๋น„์šฉ์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆด ์ˆ˜ ์—†๋Š” ๋” ํฐ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๊ณ , ์ถ”๋ก  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Transformers๋Š” AWQ์™€ GPTQ ์–‘์žํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ง€์›ํ•˜๋ฉฐ, bitsandbytes๋ฅผ ํ†ตํ•ด 8๋น„ํŠธ์™€ 4๋น„ํŠธ ์–‘์žํ™”๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +Transformers์—์„œ ์ง€์›๋˜์ง€ ์•Š๋Š” ์–‘์žํ™” ๊ธฐ๋ฒ•๋“ค์€ [`HfQuantizer`] ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด [์–‘์žํ™”](../quantization) ๊ฐ€์ด๋“œ๋ฅผ ํ†ตํ•ด ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## QuantoConfig[[transformers.QuantoConfig]] + +[[autodoc]] QuantoConfig + +## AqlmConfig[[transformers.AqlmConfig]] + +[[autodoc]] AqlmConfig + +## VptqConfig[[transformers.VptqConfig]] + +[[autodoc]] VptqConfig + +## AwqConfig[[transformers.AwqConfig]] + +[[autodoc]] AwqConfig + +## EetqConfig[[transformers.EetqConfig]] +[[autodoc]] EetqConfig + +## GPTQConfig[[transformers.GPTQConfig]] + +[[autodoc]] GPTQConfig + +## BitsAndBytesConfig[[#transformers.BitsAndBytesConfig]] + +[[autodoc]] BitsAndBytesConfig + +## HfQuantizer[[transformers.quantizers.HfQuantizer]] + +[[autodoc]] quantizers.base.HfQuantizer + +## HqqConfig[[transformers.HqqConfig]] + +[[autodoc]] HqqConfig + +## FbgemmFp8Config[[transformers.FbgemmFp8Config]] + +[[autodoc]] FbgemmFp8Config + +## CompressedTensorsConfig[[transformers.CompressedTensorsConfig]] + +[[autodoc]] CompressedTensorsConfig + +## TorchAoConfig[[transformers.TorchAoConfig]] + +[[autodoc]] TorchAoConfig diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/text_generation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/text_generation.md new file mode 100644 index 0000000000000000000000000000000000000000..917d995e6ec15c4a642e2c3bf4c5fc7b865fdb85 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/text_generation.md @@ -0,0 +1,56 @@ + + +# ์ƒ์„ฑ [[generation]] + +๊ฐ ํ”„๋ ˆ์ž„์›Œํฌ์—๋Š” ํ•ด๋‹นํ•˜๋Š” `GenerationMixin` ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„๋œ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ generate ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +- PyTorch์—์„œ๋Š” [`~generation.GenerationMixin.generate`]๊ฐ€ [`~generation.GenerationMixin`]์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- TensorFlow์—์„œ๋Š” [`~generation.TFGenerationMixin.generate`]๊ฐ€ [`~generation.TFGenerationMixin`]์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- Flax/JAX์—์„œ๋Š” [`~generation.FlaxGenerationMixin.generate`]๊ฐ€ [`~generation.FlaxGenerationMixin`]์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์‚ฌ์šฉํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์— ์ƒ๊ด€์—†์ด, generate ๋ฉ”์†Œ๋“œ๋Š” [`~generation.GenerationConfig`] ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. generate ๋ฉ”์†Œ๋“œ์˜ ๋™์ž‘์„ ์ œ์–ดํ•˜๋Š” ๋ชจ๋“  ์ƒ์„ฑ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก์„ ํ™•์ธํ•˜๋ ค๋ฉด ์ด ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +๋ชจ๋ธ์˜ ์ƒ์„ฑ ์„ค์ •์„ ์–ด๋–ป๊ฒŒ ํ™•์ธํ•˜๊ณ , ๊ธฐ๋ณธ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ž„์‹œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž ์ง€์ • ์ƒ์„ฑ ์„ค์ •์„ ๋งŒ๋“ค๊ณ  ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๋ ค๋ฉด [ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต ๊ฐ€์ด๋“œ](../generation_strategies)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ด ๊ฐ€์ด๋“œ๋Š” ํ† ํฐ ์ŠคํŠธ๋ฆฌ๋ฐ๊ณผ ๊ฐ™์€ ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## GenerationConfig [[transformers.GenerationConfig]] + +[[autodoc]] generation.GenerationConfig + - from_pretrained + - from_model_config + - save_pretrained + - update + - validate + - get_generation_mode + +[[autodoc]] generation.WatermarkingConfig + +## GenerationMixin [[transformers.GenerationMixin]] + +[[autodoc]] generation.GenerationMixin + - generate + - compute_transition_scores + +## TFGenerationMixin [[transformers.TFGenerationMixin]] + +[[autodoc]] generation.TFGenerationMixin + - generate + - compute_transition_scores + +## FlaxGenerationMixin [[transformers.FlaxGenerationMixin]] + +[[autodoc]] generation.FlaxGenerationMixin + - generate diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/tokenizer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/tokenizer.md new file mode 100644 index 0000000000000000000000000000000000000000..f87edbebeb004ad88c63c6960d07ebebe2645100 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/tokenizer.md @@ -0,0 +1,82 @@ + + +# ํ† ํฌ๋‚˜์ด์ €[[tokenizer]] + +ํ† ํฌ๋‚˜์ด์ €๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ์„ ์ค€๋น„ํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ๋ชจ๋“  ๋ชจ๋ธ์„ ์œ„ํ•œ ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์œผ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์™„์ „ํ•œ ํŒŒ์ด์ฌ ๊ตฌํ˜„๊ณผ Rust ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ [๐Ÿค— Tokenizers](https://github.com/huggingface/tokenizers)์— ๊ธฐ๋ฐ˜ํ•œ "Fast" ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค. "Fast" ๊ตฌํ˜„์€ ๋‹ค์Œ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค: + +1. ํŠนํžˆ ๋ฐฐ์น˜ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์†๋„๊ฐ€ ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. +2. ์›๋ณธ ๋ฌธ์ž์—ด(๋ฌธ์ž ๋ฐ ๋‹จ์–ด)๊ณผ ํ† ํฐ ๊ณต๊ฐ„ ์‚ฌ์ด๋ฅผ ๋งคํ•‘ํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: ํŠน์ • ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ํ† ํฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ์–ป๊ฑฐ๋‚˜, ํŠน์ • ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋“ฑ). + +๊ธฐ๋ณธ ํด๋ž˜์Šค์ธ [`PreTrainedTokenizer`]์™€ [`PreTrainedTokenizerFast`]๋Š” ๋ฌธ์ž์—ด ์ž…๋ ฅ์„ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉฐ(์•„๋ž˜ ์ฐธ์กฐ), ๋กœ์ปฌ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ, ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ํ† ํฌ๋‚˜์ด์ €(HuggingFace์˜ AWS S3 ์ €์žฅ์†Œ์—์„œ ๋‹ค์šด๋กœ๋“œ๋œ)๋กœ๋ถ€ํ„ฐ ํŒŒ์ด์ฌ ๋ฐ "Fast" ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ํด๋ž˜์Šค๋Š” ๊ณตํ†ต ๋ฉ”์†Œ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” [`~tokenization_utils_base.PreTrainedTokenizerBase`]์™€ [`~tokenization_utils_base.SpecialTokensMixin`]์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. + +[`PreTrainedTokenizer`]์™€ [`PreTrainedTokenizerFast`]๋Š” ๋ชจ๋“  ํ† ํฌ๋‚˜์ด์ €์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฃผ์š” ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค: + +- ํ† ํฐํ™”(๋ฌธ์ž์—ด์„ ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐ ๋ฌธ์ž์—ด๋กœ ๋ถ„ํ• ), ํ† ํฐ ๋ฌธ์ž์—ด์„ ID๋กœ ๋ณ€ํ™˜ ๋ฐ ๊ทธ ๋ฐ˜๋Œ€ ๊ณผ์ •, ๊ทธ๋ฆฌ๊ณ  ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ(์ฆ‰, ํ† ํฐํ™” ๋ฐ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. +- ๊ตฌ์กฐ(BPE, SentencePiece ๋“ฑ)์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์–ดํœ˜์— ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +- ํŠน์ˆ˜ ํ† ํฐ(๋งˆ์Šคํฌ, ๋ฌธ์žฅ ์‹œ์ž‘ ๋“ฑ) ๊ด€๋ฆฌ: ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ† ํฌ๋‚˜์ด์ €์˜ ์†์„ฑ์— ํ• ๋‹นํ•˜๋ฉฐ, ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. + +[`BatchEncoding`]์€ [`~tokenization_utils_base.PreTrainedTokenizerBase`]์˜ ์ธ์ฝ”๋”ฉ ๋ฉ”์†Œ๋“œ(`__call__`, `encode_plus`, `batch_encode_plus`)์˜ ์ถœ๋ ฅ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ, ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ ํ† ํฌ๋‚˜์ด์ €์ธ ๊ฒฝ์šฐ ์ด ํด๋ž˜์Šค๋Š” ํ‘œ์ค€ ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ฉ”์†Œ๋“œ๋“ค๋กœ ๊ณ„์‚ฐ๋œ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ ์ž…๋ ฅ(`input_ids`, `attention_mask` ๋“ฑ)์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. ํ† ํฌ๋‚˜์ด์ €๊ฐ€ "Fast" ํ† ํฌ๋‚˜์ด์ €์ผ ๊ฒฝ์šฐ(์ฆ‰, HuggingFace [tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ](https://github.com/huggingface/tokenizers) ๊ธฐ๋ฐ˜์ผ ๊ฒฝ์šฐ), ์ด ํด๋ž˜์Šค๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ ์›๋ณธ ๋ฌธ์ž์—ด(๋ฌธ์ž ๋ฐ ๋‹จ์–ด)๊ณผ ํ† ํฐ ๊ณต๊ฐ„ ์‚ฌ์ด๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ณ ๊ธ‰ ์ •๋ ฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค (์˜ˆ: ํŠน์ • ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ํ† ํฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ์–ป๊ฑฐ๋‚˜, ํŠน์ • ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ์–ป๋Š” ๋“ฑ). + + +# ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํ† ํฌ๋‚˜์ด์ €[[multimodal-tokenizer]] + +๊ทธ ์™ธ์—๋„ ๊ฐ ํ† ํฌ๋‚˜์ด์ €๋Š” "๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ" ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ๋ชจ๋“  ๊ด€๋ จ ํŠน์ˆ˜ ํ† ํฐ์„ ํ† ํฌ๋‚˜์ด์ € ์†์„ฑ์˜ ์ผ๋ถ€๋กœ ์ €์žฅํ•˜์—ฌ ๋” ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, LLaVA์™€ ๊ฐ™์€ ๋น„์ „-์–ธ์–ด ๋ชจ๋ธ์—์„œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด, `tokenizer.image_token_id`์— ์ ‘๊ทผํ•˜์—ฌ ํ”Œ๋ ˆ์ด์Šคํ™€๋”๋กœ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ์ด๋ฏธ์ง€ ํ† ํฐ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋“  ์œ ํ˜•์˜ ํ† ํฌ๋‚˜์ด์ €์— ์ถ”๊ฐ€ ํŠน์ˆ˜ ํ† ํฐ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด, ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ํŠน์ˆ˜ ํ† ํฐ์€ ๋ฐ˜๋“œ์‹œ ํŠน์ • ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์™€ ๊ด€๋ จ๋  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ, ๋ชจ๋ธ์ด ์ž์ฃผ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ์–ด๋–ค ๊ฒƒ์ด๋“  ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ `output_dir`์— ์ €์žฅ๋œ ํ† ํฌ๋‚˜์ด์ €๋Š” ์„ธ ๊ฐœ์˜ ์ถ”๊ฐ€ ํŠน์ˆ˜ ํ† ํฐ์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +```python +vision_tokenizer = AutoTokenizer.from_pretrained( + "llava-hf/llava-1.5-7b-hf", + extra_special_tokens={"image_token": "", "boi_token": "", "eoi_token": ""} +) +print(vision_tokenizer.image_token, vision_tokenizer.image_token_id) +("", 32000) +``` + +## PreTrainedTokenizer[[transformers.PreTrainedTokenizer]] + +[[autodoc]] PreTrainedTokenizer + - __call__ + - add_tokens + - add_special_tokens + - apply_chat_template + - batch_decode + - decode + - encode + - push_to_hub + - all + + +## PreTrainedTokenizerFast[[transformers.PreTrainedTokenizerFast]] + +[`PreTrainedTokenizerFast`]๋Š” [tokenizers](https://huggingface.co/docs/tokenizers) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ๐Ÿค— tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์–ป์€ ํ† ํฌ๋‚˜์ด์ €๋Š” +๐Ÿค— transformers๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๋ฉด [Using tokenizers from ๐Ÿค— tokenizers](../fast_tokenizers) ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +[[autodoc]] PreTrainedTokenizerFast + - __call__ + - add_tokens + - add_special_tokens + - apply_chat_template + - batch_decode + - decode + - encode + - push_to_hub + - all + +## BatchEncoding[[transformers.BatchEncoding]] + +[[autodoc]] BatchEncoding \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/trainer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/trainer.md new file mode 100644 index 0000000000000000000000000000000000000000..23eda74a8bd669c16fcb3ebdbd21ea7cd8d6cd65 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/main_classes/trainer.md @@ -0,0 +1,52 @@ + + +# Trainer [[trainer]] + +[`Trainer`] ํด๋ž˜์Šค๋Š” PyTorch์—์„œ ์™„์ „ํ•œ ๊ธฐ๋Šฅ(feature-complete)์˜ ํ›ˆ๋ จ์„ ์œ„ํ•œ API๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ๋‹ค์ค‘ GPU/TPU์—์„œ์˜ ๋ถ„์‚ฐ ํ›ˆ๋ จ, [NVIDIA GPU](https://nvidia.github.io/apex/), [AMD GPU](https://rocm.docs.amd.com/en/latest/rocm.html)๋ฅผ ์œ„ํ•œ ํ˜ผํ•ฉ ์ •๋ฐ€๋„, ๊ทธ๋ฆฌ๊ณ  PyTorch์˜ [`torch.amp`](https://pytorch.org/docs/stable/amp.html)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. [`Trainer`]๋Š” ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๋ฐฉ์‹์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ์ œ๊ณตํ•˜๋Š” [`TrainingArguments`] ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ํด๋ž˜์Šค๋Š” ํ•จ๊ป˜ ์™„์ „ํ•œ ํ›ˆ๋ จ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +[`Seq2SeqTrainer`]์™€ [`Seq2SeqTrainingArguments`]๋Š” [`Trainer`]์™€ [`TrainingArguments`] ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋ฉฐ, ์š”์•ฝ์ด๋‚˜ ๋ฒˆ์—ญ๊ณผ ๊ฐ™์€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ์ž‘์—…์„ ์œ„ํ•œ ๋ชจ๋ธ ํ›ˆ๋ จ์— ์ ํ•ฉํ•˜๊ฒŒ ์กฐ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + + + +[`Trainer`] ํด๋ž˜์Šค๋Š” ๐Ÿค— Transformers ๋ชจ๋ธ์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๋•Œ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋™์ž‘์„ ํ•˜๊ฒŒ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์‹ ๋งŒ์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ์„ ํ™•์ธํ•˜์„ธ์š”: + +- ๋ชจ๋ธ์€ ํ•ญ์ƒ ํŠœํ”Œ์ด๋‚˜ [`~utils.ModelOutput`]์˜ ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์€ `labels` ์ธ์ž๊ฐ€ ์ œ๊ณต๋˜๋ฉด ์†์‹ค์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๊ณ , ๋ชจ๋ธ์ด ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ๊ทธ ์†์‹ค์ด ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋กœ ๋ฐ˜ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ ˆ์ด๋ธ” ์ธ์ž๋ฅผ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, [`Trainer`]์—๊ฒŒ ์ด๋ฆ„์„ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด [`TrainingArguments`]์—์„œ `label_names`๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๊ทธ ์ค‘ ์–ด๋А ๊ฒƒ๋„ `"label"`๋กœ ๋ช…๋ช…๋˜์–ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. + + + +## Trainer [[transformers.Trainer]] + +[[autodoc]] Trainer + - all + +## Seq2SeqTrainer [[transformers.Seq2SeqTrainer]] + +[[autodoc]] Seq2SeqTrainer + - evaluate + - predict + +## TrainingArguments [[transformers.TrainingArguments]] + +[[autodoc]] TrainingArguments + - all + +## Seq2SeqTrainingArguments [[transformers.Seq2SeqTrainingArguments]] + +[[autodoc]] Seq2SeqTrainingArguments + - all diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/albert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/albert.md new file mode 100644 index 0000000000000000000000000000000000000000..9fc6938145274707987c3e8d1738f3b460c81f6d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/albert.md @@ -0,0 +1,263 @@ + + +
+
+ PyTorch + TensorFlow + Flax + SDPA +
+
+ +# ALBERT[[albert]] + +[ALBERT](https://huggingface.co/papers/1909.11942)๋Š” [BERT](./bert)์˜ ํ™•์žฅ์„ฑ๊ณผ ํ•™์Šต ์‹œ ๋ฉ”๋ชจ๋ฆฌ ํ•œ๊ณ„๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ๋‘ ๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ์†Œ ๊ธฐ๋ฒ•์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ ๋ถ„ํ•ด(factorized embedding parametrization)๋กœ, ํฐ ์–ดํœ˜ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์„ ๋‘ ๊ฐœ์˜ ์ž‘์€ ํ–‰๋ ฌ๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ํžˆ๋“  ์‚ฌ์ด์ฆˆ๋ฅผ ๋Š˜๋ ค๋„ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ํฌ๊ฒŒ ์ฆ๊ฐ€ํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ๊ณ„์ธต ๊ฐ„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ณต์œ (cross-layer parameter sharing)๋กœ, ์—ฌ๋Ÿฌ ๊ณ„์ธต์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ณต์œ ํ•˜์—ฌ ํ•™์Šตํ•ด์•ผ ํ•  ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค. + +ALBERT๋Š” BERT์—์„œ ๋ฐœ์ƒํ•˜๋Š” GPU/TPU ๋ฉ”๋ชจ๋ฆฌ ํ•œ๊ณ„, ๊ธด ํ•™์Šต ์‹œ๊ฐ„, ๊ฐ‘์ž‘์Šค๋Ÿฐ ์„ฑ๋Šฅ ์ €ํ•˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ALBERT๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋‘ ๊ฐ€์ง€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๊ณ  BERT์˜ ํ•™์Šต ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค: + +- **์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ ๋ถ„ํ•ด:** ํฐ ์–ดํœ˜ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์„ ๋‘ ๊ฐœ์˜ ๋” ์ž‘์€ ํ–‰๋ ฌ๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ž…๋‹ˆ๋‹ค. +- **๊ณ„์ธต ๊ฐ„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ณต์œ :** ๊ฐ ํŠธ๋žœ์Šคํฌ๋จธ ๊ณ„์ธต๋งˆ๋‹ค ๋ณ„๋„์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋Œ€์‹ , ์—ฌ๋Ÿฌ ๊ณ„์ธต์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ณต์œ ํ•˜์—ฌ ํ•™์Šตํ•ด์•ผ ํ•  ๊ฐ€์ค‘์น˜ ์ˆ˜๋ฅผ ๋”์šฑ ์ค„์ž…๋‹ˆ๋‹ค. + +ALBERT๋Š” BERT์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ(absolute position embeddings)์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ์ž…๋ ฅ ํŒจ๋”ฉ์€ ์˜ค๋ฅธ์ชฝ์— ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋Š” 128์ด๋ฉฐ, BERT์˜ 768๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค. ALBERT๋Š” ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 512๊ฐœ์˜ ํ† ํฐ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋“  ๊ณต์‹ ALBERT ์ฒดํฌํฌ์ธํŠธ๋Š” [ALBERT ์ปค๋ฎค๋‹ˆํ‹ฐ](https://huggingface.co/albert) ์กฐ์ง์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ์˜ค๋ฅธ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์˜ ALBERT ๋ชจ๋ธ์„ ํด๋ฆญํ•˜์‹œ๋ฉด ๋‹ค์–‘ํ•œ ์–ธ์–ด ์ž‘์—…์— ALBERT๋ฅผ ์ ์šฉํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ๋” ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” [`Pipeline`], [`AutoModel`] ๊ทธ๋ฆฌ๊ณ  ์ปค๋งจ๋“œ๋ผ์ธ์—์„œ `[MASK]` ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + + + +```py +import torch +from transformers import pipeline + +pipeline = pipeline( + task="fill-mask", + model="albert-base-v2", + torch_dtype=torch.float16, + device=0 +) +pipeline("์‹๋ฌผ์€ ๊ด‘ํ•ฉ์„ฑ์ด๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ณผ์ •์„ ํ†ตํ•ด [MASK]๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.", top_k=5) +``` + + + + +```py +import torch +from transformers import AutoModelForMaskedLM, AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained("albert/albert-base-v2") +model = AutoModelForMaskedLM.from_pretrained( + "albert/albert-base-v2", + torch_dtype=torch.float16, + attn_implementation="sdpa", + device_map="auto" +) + +prompt = "์‹๋ฌผ์€ [MASK]์ด๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ณผ์ •์„ ํ†ตํ•ด ์—๋„ˆ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค." +inputs = tokenizer(prompt, return_tensors="pt").to(model.device) + +with torch.no_grad(): + outputs = model(**inputs) + mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] + predictions = outputs.logits[0, mask_token_index] + +top_k = torch.topk(predictions, k=5).indices.tolist() +for token_id in top_k[0]: + print(f"์˜ˆ์ธก: {tokenizer.decode([token_id])}") +``` + + + + +```bash +echo -e "Plants create [MASK] through a process known as photosynthesis." | transformers run --task fill-mask --model albert-base-v2 --device 0 +``` + + + + + +## ์ฐธ๊ณ  ์‚ฌํ•ญ[[notes]] + +- BERT๋Š” ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ์˜ค๋ฅธ์ชฝ์— ์ž…๋ ฅ์ด ํŒจ๋”ฉ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ `E`๋Š” ํžˆ๋“  ํฌ๊ธฐ `H`์™€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์€ ๋ฌธ๋งฅ์— ๋…๋ฆฝ์ (๊ฐ ํ† ํฐ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ)์ด๊ณ , ์€๋‹‰ ์ƒํƒœ๋Š” ๋ฌธ๋งฅ์— ์˜์กด์ (ํ† ํฐ ์‹œํ€€์Šค๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์€๋‹‰ ์ƒํƒœ)์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์€ `V x E`(V: ์–ดํœ˜ ํฌ๊ธฐ)์ด๋ฏ€๋กœ, ์ผ๋ฐ˜์ ์œผ๋กœ `H >> E`๊ฐ€ ๋” ๋…ผ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. `E < H`์ผ ๋•Œ ๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋” ์ ์–ด์ง‘๋‹ˆ๋‹ค. + +## ์ฐธ๊ณ  ์ž๋ฃŒ[[resources]] + +์•„๋ž˜ ์„น์…˜์˜ ์ž๋ฃŒ๋“ค์€ ๊ณต์‹ Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ ํ‘œ์‹œ) ์ž๋ฃŒ๋กœ, AlBERT๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ํ•  ์ž๋ฃŒ๊ฐ€ ์žˆ๋‹ค๋ฉด Pull Request๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”! ๊ธฐ์กด ์ž๋ฃŒ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +- [`AlbertForSequenceClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +- [`TFAlbertForSequenceClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +- [`FlaxAlbertForSequenceClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification)์—์„œ ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + + + +- [`AlbertForTokenClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +- [`TFAlbertForTokenClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +- [`FlaxAlbertForTokenClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face์˜ [ํ† ํฐ ๋ถ„๋ฅ˜](https://huggingface.co/course/chapter7/2?fw=pt) ๊ฐ•์ขŒ +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification)์—์„œ ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + + + +- [`AlbertForMaskedLM`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFAlbertForMaskedLM`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxAlbertForMaskedLM`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face์˜ [๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ๋ง](https://huggingface.co/course/chapter7/3?fw=pt) ๊ฐ•์ขŒ +- [๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling)์—์„œ ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + + + +- [`AlbertForQuestionAnswering`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFAlbertForQuestionAnswering`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxAlbertForQuestionAnswering`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [์งˆ์˜์‘๋‹ต](https://huggingface.co/course/chapter7/7?fw=pt) ๐Ÿค— Hugging Face ๊ฐ•์ขŒ์˜ ์ฑ•ํ„ฐ. +- [์งˆ์˜์‘๋‹ต ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering)์—์„œ ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + +**๋‹ค์ค‘ ์„ ํƒ(Multiple choice)** + +- [`AlbertForMultipleChoice`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFAlbertForMultipleChoice`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +- [๋‹ค์ค‘ ์„ ํƒ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/multiple_choice)์—์„œ ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + +## AlbertConfig[[albertconfig]] + +[[autodoc]] AlbertConfig + +## AlbertTokenizer[[alberttokenizer]] + +[[autodoc]] AlbertTokenizer - build_inputs_with_special_tokens - get_special_tokens_mask - create_token_type_ids_from_sequences - save_vocabulary + +## AlbertTokenizerFast[[alberttokenizerfast]] + +[[autodoc]] AlbertTokenizerFast + +## Albert ํŠนํ™” ์ถœ๋ ฅ[[albert-specific-outputs]] + +[[autodoc]] models.albert.modeling_albert.AlbertForPreTrainingOutput + +[[autodoc]] models.albert.modeling_tf_albert.TFAlbertForPreTrainingOutput + + + + +## AlbertModel[[albertmodel]] + +[[autodoc]] AlbertModel - forward + +## AlbertForPreTraining[[albertforpretraining]] + +[[autodoc]] AlbertForPreTraining - forward + +## AlbertForMaskedLM[[albertformaskedlm]] + +[[autodoc]] AlbertForMaskedLM - forward + +## AlbertForSequenceClassification[[albertforsequenceclassification]] + +[[autodoc]] AlbertForSequenceClassification - forward + +## AlbertForMultipleChoice[[albertformultiplechoice]] + +[[autodoc]] AlbertForMultipleChoice + +## AlbertForTokenClassification[[albertfortokenclassification]] + +[[autodoc]] AlbertForTokenClassification - forward + +## AlbertForQuestionAnswering[[albertforquestionanswering]] + +[[autodoc]] AlbertForQuestionAnswering - forward + + + + + +## TFAlbertModel[[tfalbertmodel]] + +[[autodoc]] TFAlbertModel - call + +## TFAlbertForPreTraining[[tfalbertforpretraining]] + +[[autodoc]] TFAlbertForPreTraining - call + +## TFAlbertForMaskedLM[[tfalbertformaskedlm]] + +[[autodoc]] TFAlbertForMaskedLM - call + +## TFAlbertForSequenceClassification[[tfalbertforsequenceclassification]] + +[[autodoc]] TFAlbertForSequenceClassification - call + +## TFAlbertForMultipleChoice[[tfalbertformultiplechoice]] + +[[autodoc]] TFAlbertForMultipleChoice - call + +## TFAlbertForTokenClassification[[tfalbertfortokenclassification]] + +[[autodoc]] TFAlbertForTokenClassification - call + +## TFAlbertForQuestionAnswering[[tfalbertforquestionanswering]] + +[[autodoc]] TFAlbertForQuestionAnswering - call + + + + +## FlaxAlbertModel[[flaxalbertmodel]] + +[[autodoc]] FlaxAlbertModel - **call** + +## FlaxAlbertForPreTraining[[flaxalbertforpretraining]] + +[[autodoc]] FlaxAlbertForPreTraining - **call** + +## FlaxAlbertForMaskedLM[[flaxalbertformaskedlm]] + +[[autodoc]] FlaxAlbertForMaskedLM - **call** + +## FlaxAlbertForSequenceClassification[[flaxalbertforsequenceclassification]] + +[[autodoc]] FlaxAlbertForSequenceClassification - **call** + +## FlaxAlbertForMultipleChoice[[flaxalbertformultiplechoice]] + +[[autodoc]] FlaxAlbertForMultipleChoice - **call** + +## FlaxAlbertForTokenClassification[[flaxalbertfortokenclassification]] + +[[autodoc]] FlaxAlbertForTokenClassification - **call** + +## FlaxAlbertForQuestionAnswering[[flaxalbertforquestionanswering]] + +[[autodoc]] FlaxAlbertForQuestionAnswering - **call** + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/altclip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/altclip.md new file mode 100644 index 0000000000000000000000000000000000000000..f736ab9c5c94f3b7c6c9532b4653272f2ab856c2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/altclip.md @@ -0,0 +1,78 @@ +# AltCLIP + +## ๊ฐœ์š”[[overview]] + +AltCLIP ๋ชจ๋ธ์€ Zhongzhi Chen, Guang Liu, Bo-Wen Zhang, Fulong Ye, Qinghong Yang, Ledell Wu์˜ [AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities](https://huggingface.co/papers/2211.06679v2) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. AltCLIP(CLIP์˜ ์–ธ์–ด ์ธ์ฝ”๋”๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์–ธ์–ด ๊ธฐ๋Šฅ ํ™•์žฅ)์€ ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๋ฐ ํ…์ŠคํŠธ-ํ…์ŠคํŠธ ์Œ์œผ๋กœ ํ›ˆ๋ จ๋œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. CLIP์˜ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋‹ค๊ตญ์–ด ํ…์ŠคํŠธ ์ธ์ฝ”๋” XLM-R๋กœ ๊ต์ฒดํ•˜์—ฌ, ๊ฑฐ์˜ ๋ชจ๋“  ์ž‘์—…์—์„œ CLIP๊ณผ ์œ ์‚ฌํ•œ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ์œผ๋ฉฐ, ์›๋ž˜ CLIP์˜ ๋‹ค๊ตญ์–ด ์ดํ•ด์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ๋„ ํ™•์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋ณธ ์—ฐ๊ตฌ์—์„œ๋Š” ๊ฐ•๋ ฅํ•œ ์ด์ค‘ ์–ธ์–ด ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํ‘œํ˜„ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๊ฐœ๋…์ ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ณ  ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. OpenAI์—์„œ ์ถœ์‹œํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํ‘œํ˜„ ๋ชจ๋ธ CLIP์—์„œ ์‹œ์ž‘ํ•˜์—ฌ, ๊ทธ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋‹ค๊ตญ์–ด ํ…์ŠคํŠธ ์ธ์ฝ”๋” XLM-R๋กœ ๊ต์ฒดํ•˜๊ณ , ๊ต์‚ฌ ํ•™์Šต๊ณผ ๋Œ€์กฐ ํ•™์Šต์œผ๋กœ ๊ตฌ์„ฑ๋œ 2๋‹จ๊ณ„ ํ›ˆ๋ จ ์Šคํ‚ค๋งˆ๋ฅผ ํ†ตํ•ด ์–ธ์–ด์™€ ์ด๋ฏธ์ง€ ํ‘œํ˜„์„ ์ •๋ ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ์ž‘์—… ํ‰๊ฐ€๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ์˜ ๋ฐฉ๋ฒ•์„ ๊ฒ€์ฆํ–ˆ์Šต๋‹ˆ๋‹ค. ImageNet-CN, Flicker30k-CN, COCO-CN์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ์ž‘์—…์—์„œ ์ƒˆ๋กœ์šด ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ–ˆ์œผ๋ฉฐ, ๊ฑฐ์˜ ๋ชจ๋“  ์ž‘์—…์—์„œ CLIP๊ณผ ์œ ์‚ฌํ•œ ์„ฑ๋Šฅ์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” CLIP์˜ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋ฅผ ๋‹จ์ˆœํžˆ ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค๊ตญ์–ด ์ดํ•ด์™€ ๊ฐ™์€ ํ™•์žฅ ๊ธฐ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ์‹œ์‚ฌํ•ฉ๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [jongjyh](https://huggingface.co/jongjyh)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ๊ณผ ์˜ˆ์ œ[[usage-tips-and-example]] + +AltCLIP์˜ ์‚ฌ์šฉ๋ฒ•์€ CLIP๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜๋ฉฐ, ์ฐจ์ด์ ์€ ํ…์ŠคํŠธ ์ธ์ฝ”๋”์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์–ดํ…์…˜ ๋Œ€์‹  ์–‘๋ฐฉํ–ฅ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜๋ฉฐ, XLM-R์˜ [CLS] ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + +AltCLIP์€ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋น„์ „ ๋ฐ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ๊ฐ„์˜ ์œ ์‚ฌ์„ฑ ๊ณ„์‚ฐ ๋ฐ ์ œ๋กœ์ƒท ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AltCLIP์€ ViT์™€ ๊ฐ™์€ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐ์  ํŠน์ง•์„ ์–ป๊ณ , ์–‘๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ํŠน์ง•์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ดํ›„ ํ…์ŠคํŠธ์™€ ์‹œ๊ฐ์  ํŠน์ง• ๋ชจ๋‘ ๋™์ผํ•œ ์ฐจ์›์˜ ์ž ์žฌ ๊ณต๊ฐ„์œผ๋กœ ํˆฌ์‚ฌ๋ฉ๋‹ˆ๋‹ค. ํˆฌ์‚ฌ๋œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ํŠน์ง• ๊ฐ„์˜ ๋‚ด์ ์„ ์œ ์‚ฌ๋„ ์ ์ˆ˜๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ฏธ์ง€๋ฅผ ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”์— ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด, ๊ฐ ์ด๋ฏธ์ง€๋ฅผ ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ ๊ฒน์น˜์ง€ ์•Š๋Š” ํŒจ์น˜ ์‹œํ€€์Šค๋กœ ๋ถ„ํ• ํ•œ ๋’ค, ์ด๋ฅผ ์„ ํ˜• ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ์ด๋ฏธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด [CLS] ํ† ํฐ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ๋„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ‘œ์ค€ ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. [`CLIPImageProcessor`]๋Š” ๋ชจ๋ธ์„ ์œ„ํ•ด ์ด๋ฏธ์ง€๋ฅผ ํฌ๊ธฐ ์กฐ์ •ํ•˜๊ณ  ์ •๊ทœํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[`AltCLIPProcessor`]๋Š” [`CLIPImageProcessor`]์™€ [`XLMRobertaTokenizer`]๋ฅผ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋ฌถ์–ด ํ…์ŠคํŠธ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” [`AltCLIPProcessor`]์™€ [`AltCLIPModel`]์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ๊ฐ„์˜ ์œ ์‚ฌ์„ฑ ์ ์ˆ˜๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. +```python +>>> from PIL import Image +>>> import requests + +>>> from transformers import AltCLIPModel, AltCLIPProcessor + +>>> model = AltCLIPModel.from_pretrained("BAAI/AltCLIP") +>>> processor = AltCLIPProcessor.from_pretrained("BAAI/AltCLIP") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True) + +>>> outputs = model(**inputs) +>>> logits_per_image = outputs.logits_per_image # ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์œ ์‚ฌ๋„ ์ ์ˆ˜ +>>> probs = logits_per_image.softmax(dim=1) # ๋ผ๋ฒจ ๋งˆ๋‹ค ํ™•๋ฅ ์„ ์–ป๊ธฐ ์œ„ํ•ด softmax ์ ์šฉ +``` + + +์ด ๋ชจ๋ธ์€ `CLIPModel`์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฏ€๋กœ, ์›๋ž˜ CLIP์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## AltCLIPConfig + +[[autodoc]] AltCLIPConfig + - from_text_vision_configs + +## AltCLIPTextConfig + +[[autodoc]] AltCLIPTextConfig + +## AltCLIPVisionConfig + +[[autodoc]] AltCLIPVisionConfig + +## AltCLIPProcessor + +[[autodoc]] AltCLIPProcessor + +## AltCLIPModel + +[[autodoc]] AltCLIPModel + - forward + - get_text_features + - get_image_features + +## AltCLIPTextModel + +[[autodoc]] AltCLIPTextModel + - forward + +## AltCLIPVisionModel + +[[autodoc]] AltCLIPVisionModel + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/auto.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/auto.md new file mode 100644 index 0000000000000000000000000000000000000000..45c2f917a42c9956e6c32ce50d8d3c1570f67052 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/auto.md @@ -0,0 +1,382 @@ + + +# Auto ํด๋ž˜์Šค[[auto-classes]] + +๋งŽ์€ ๊ฒฝ์šฐ, ์‚ฌ์šฉํ•˜๋ ค๋Š” ์•„ํ‚คํ…์ฒ˜๋Š” `from_pretrained()` ๋ฉ”์†Œ๋“œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ์ด๋ฆ„์ด๋‚˜ ๊ฒฝ๋กœ๋กœ๋ถ€ํ„ฐ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AutoClasses๋Š” ์ด ์ž‘์—…์„ ์œ„ํ•ด ์กด์žฌํ•˜๋ฉฐ, ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜/๊ตฌ์„ฑ/๋‹จ์–ด์‚ฌ์ „์— ๋Œ€ํ•œ ์ด๋ฆ„/๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•˜๋ฉด ์ž๋™์œผ๋กœ ๊ด€๋ จ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. + +[`AutoConfig`], [`AutoModel`], [`AutoTokenizer`] ์ค‘ ํ•˜๋‚˜๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋ฉด ํ•ด๋‹น ์•„ํ‚คํ…์ฒ˜์˜ ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, + + +```python +model = AutoModel.from_pretrained("google-bert/bert-base-cased") +``` + +์œ„ ์ฝ”๋“œ๋Š” [`BertModel`]์˜ ์ธ์Šคํ„ด์Šค์ธ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +๊ฐ ์ž‘์—…์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ `AutoModel` ํด๋ž˜์Šค๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ ๋ฐฑ์—”๋“œ(PyTorch, TensorFlow ๋˜๋Š” Flax)์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. + +## ์ž๋™ ํด๋ž˜์Šค ํ™•์žฅ[[extending-the-auto-classes]] + +๊ฐ ์ž๋™ ํด๋ž˜์Šค๋Š” ์‚ฌ์šฉ์ž์˜ ์ปค์Šคํ…€ ํด๋ž˜์Šค๋กœ ํ™•์žฅ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `NewModel`์ด๋ผ๋Š” ์ปค์Šคํ…€ ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ–ˆ๋‹ค๋ฉด, `NewModelConfig`๋ฅผ ์ค€๋น„ํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž๋™ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import AutoConfig, AutoModel + +AutoConfig.register("new-model", NewModelConfig) +AutoModel.register(NewModelConfig, NewModel) +``` + +์ดํ›„์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ž๋™ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + + + +๋งŒ์•ฝ `NewModelConfig`๊ฐ€ [`~transformers.PretrainedConfig`]์˜ ์„œ๋ธŒํด๋ž˜์Šค๋ผ๋ฉด, ํ•ด๋‹น `model_type` ์†์„ฑ์ด ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค(์—ฌ๊ธฐ์„œ๋Š” `"new-model"`)์™€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, `NewModel`์ด [`PreTrainedModel`]์˜ ์„œ๋ธŒํด๋ž˜์Šค๋ผ๋ฉด, ํ•ด๋‹น `config_class` ์†์„ฑ์ด ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค(์—ฌ๊ธฐ์„œ๋Š” `NewModelConfig`)์™€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + + + +## AutoConfig[[transformers.AutoConfig]] + +[[autodoc]] AutoConfig + +## AutoTokenizer[[transformers.AutoTokenizer]] + +[[autodoc]] AutoTokenizer + +## AutoFeatureExtractor[[transformers.AutoFeatureExtractor]] + +[[autodoc]] AutoFeatureExtractor + +## AutoImageProcessor[[transformers.AutoImageProcessor]] + +[[autodoc]] AutoImageProcessor + +## AutoProcessor[[transformers.AutoProcessor]] + +[[autodoc]] AutoProcessor + +## ์ผ๋ฐ˜์ ์ธ ๋ชจ๋ธ ํด๋ž˜์Šค[[generic-model-classes]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ํŠน์ • ํ—ค๋“œ ์—†์ด ๊ธฐ๋ณธ ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModel[[transformers.AutoModel]] + +[[autodoc]] AutoModel + +### TFAutoModel[[transformers.TFAutoModel]] + +[[autodoc]] TFAutoModel + +### FlaxAutoModel[[transformers.FlaxAutoModel]] + +[[autodoc]] FlaxAutoModel + +## ์ผ๋ฐ˜์ ์ธ ์‚ฌ์ „ ํ•™์Šต ํด๋ž˜์Šค[[generic-pretraining-classes]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ์‚ฌ์ „ ํ›ˆ๋ จ ํ—ค๋“œ๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModelForPreTraining[[transformers.AutoModelForPreTraining]] + +[[autodoc]] AutoModelForPreTraining + +### TFAutoModelForPreTraining[[transformers.TFAutoModelForPreTraining]] + +[[autodoc]] TFAutoModelForPreTraining + +### FlaxAutoModelForPreTraining[[transformers.FlaxAutoModelForPreTraining]] + +[[autodoc]] FlaxAutoModelForPreTraining + +## ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ[[natural-language-processing]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ์•„๋ž˜์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModelForCausalLM[[transformers.AutoModelForCausalLM]] + +[[autodoc]] AutoModelForCausalLM + +### TFAutoModelForCausalLM[[transformers.TFAutoModelForCausalLM]] + +[[autodoc]] TFAutoModelForCausalLM + +### FlaxAutoModelForCausalLM[[transformers.FlaxAutoModelForCausalLM]] + +[[autodoc]] FlaxAutoModelForCausalLM + +### AutoModelForMaskedLM[[transformers.AutoModelForMaskedLM]] + +[[autodoc]] AutoModelForMaskedLM + +### TFAutoModelForMaskedLM[[transformers.TFAutoModelForMaskedLM]] + +[[autodoc]] TFAutoModelForMaskedLM + +### FlaxAutoModelForMaskedLM[[transformers.FlaxAutoModelForMaskedLM]] + +[[autodoc]] FlaxAutoModelForMaskedLM + +### AutoModelForMaskGeneration[[transformers.AutoModelForMaskGeneration]] + +[[autodoc]] AutoModelForMaskGeneration + +### TFAutoModelForMaskGeneration[[transformers.TFAutoModelForMaskGeneration]] + +[[autodoc]] TFAutoModelForMaskGeneration + +### AutoModelForSeq2SeqLM[[transformers.AutoModelForSeq2SeqLM]] + +[[autodoc]] AutoModelForSeq2SeqLM + +### TFAutoModelForSeq2SeqLM[[transformers.TFAutoModelForSeq2SeqLM]] + +[[autodoc]] TFAutoModelForSeq2SeqLM + +### FlaxAutoModelForSeq2SeqLM[[transformers.FlaxAutoModelForSeq2SeqLM]] + +[[autodoc]] FlaxAutoModelForSeq2SeqLM + +### AutoModelForSequenceClassification[[transformers.AutoModelForSequenceClassification]] + +[[autodoc]] AutoModelForSequenceClassification + +### TFAutoModelForSequenceClassification[[transformers.TFAutoModelForSequenceClassification]] + +[[autodoc]] TFAutoModelForSequenceClassification + +### FlaxAutoModelForSequenceClassification[[transformers.FlaxAutoModelForSequenceClassification]] + +[[autodoc]] FlaxAutoModelForSequenceClassification + +### AutoModelForMultipleChoice[[transformers.AutoModelForMultipleChoice]] + +[[autodoc]] AutoModelForMultipleChoice + +### TFAutoModelForMultipleChoice[[transformers.TFAutoModelForMultipleChoice]] + +[[autodoc]] TFAutoModelForMultipleChoice + +### FlaxAutoModelForMultipleChoice[[transformers.FlaxAutoModelForMultipleChoice]] + +[[autodoc]] FlaxAutoModelForMultipleChoice + +### AutoModelForNextSentencePrediction[[transformers.AutoModelForNextSentencePrediction]] + +[[autodoc]] AutoModelForNextSentencePrediction + +### TFAutoModelForNextSentencePrediction[[transformers.TFAutoModelForNextSentencePrediction]] + +[[autodoc]] TFAutoModelForNextSentencePrediction + +### FlaxAutoModelForNextSentencePrediction[[transformers.FlaxAutoModelForNextSentencePrediction]] + +[[autodoc]] FlaxAutoModelForNextSentencePrediction + +### AutoModelForTokenClassification[[transformers.AutoModelForTokenClassification]] + +[[autodoc]] AutoModelForTokenClassification + +### TFAutoModelForTokenClassification[[transformers.TFAutoModelForTokenClassification]] + +[[autodoc]] TFAutoModelForTokenClassification + +### FlaxAutoModelForTokenClassification[[transformers.FlaxAutoModelForTokenClassification]] + +[[autodoc]] FlaxAutoModelForTokenClassification + +### AutoModelForQuestionAnswering[[transformers.AutoModelForQuestionAnswering]] + +[[autodoc]] AutoModelForQuestionAnswering + +### TFAutoModelForQuestionAnswering[[transformers.TFAutoModelForQuestionAnswering]] + +[[autodoc]] TFAutoModelForQuestionAnswering + +### FlaxAutoModelForQuestionAnswering[[transformers.FlaxAutoModelForQuestionAnswering]] + +[[autodoc]] FlaxAutoModelForQuestionAnswering + +### AutoModelForTextEncoding[[transformers.AutoModelForTextEncoding]] + +[[autodoc]] AutoModelForTextEncoding + +### TFAutoModelForTextEncoding[[transformers.TFAutoModelForTextEncoding]] + +[[autodoc]] TFAutoModelForTextEncoding + +## ์ปดํ“จํ„ฐ ๋น„์ „[[computer-vision]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ์•„๋ž˜์˜ ์ปดํ“จํ„ฐ ๋น„์ „ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModelForDepthEstimation[[transformers.AutoModelForDepthEstimation]] + +[[autodoc]] AutoModelForDepthEstimation + +### AutoModelForImageClassification[[transformers.AutoModelForImageClassification]] + +[[autodoc]] AutoModelForImageClassification + +### TFAutoModelForImageClassification[[transformers.TFAutoModelForImageClassification]] + +[[autodoc]] TFAutoModelForImageClassification + +### FlaxAutoModelForImageClassification[[transformers.FlaxAutoModelForImageClassification]] + +[[autodoc]] FlaxAutoModelForImageClassification + +### AutoModelForVideoClassification[[transformers.AutoModelForVideoClassification]] + +[[autodoc]] AutoModelForVideoClassification + +### AutoModelForKeypointDetection[[transformers.AutoModelForKeypointDetection]] + +[[autodoc]] AutoModelForKeypointDetection + +### AutoModelForMaskedImageModeling[[transformers.AutoModelForMaskedImageModeling]] + +[[autodoc]] AutoModelForMaskedImageModeling + +### TFAutoModelForMaskedImageModeling[[transformers.TFAutoModelForMaskedImageModeling]] + +[[autodoc]] TFAutoModelForMaskedImageModeling + +### AutoModelForObjectDetection[[transformers.AutoModelForObjectDetection]] + +[[autodoc]] AutoModelForObjectDetection + +### AutoModelForImageSegmentation[[transformers.AutoModelForImageSegmentation]] + +[[autodoc]] AutoModelForImageSegmentation + +### AutoModelForImageToImage[[transformers.AutoModelForImageToImage]] + +[[autodoc]] AutoModelForImageToImage + +### AutoModelForSemanticSegmentation[[transformers.AutoModelForSemanticSegmentation]] + +[[autodoc]] AutoModelForSemanticSegmentation + +### TFAutoModelForSemanticSegmentation[[transformers.TFAutoModelForSemanticSegmentation]] + +[[autodoc]] TFAutoModelForSemanticSegmentation + +### AutoModelForInstanceSegmentation[[transformers.AutoModelForInstanceSegmentation]] + +[[autodoc]] AutoModelForInstanceSegmentation + +### AutoModelForUniversalSegmentation[[transformers.AutoModelForUniversalSegmentation]] + +[[autodoc]] AutoModelForUniversalSegmentation + +### AutoModelForZeroShotImageClassification[[transformers.AutoModelForZeroShotImageClassification]] + +[[autodoc]] AutoModelForZeroShotImageClassification + +### TFAutoModelForZeroShotImageClassification[[transformers.TFAutoModelForZeroShotImageClassification]] + +[[autodoc]] TFAutoModelForZeroShotImageClassification + +### AutoModelForZeroShotObjectDetection[[transformers.AutoModelForZeroShotObjectDetection]] + +[[autodoc]] AutoModelForZeroShotObjectDetection + +## ์˜ค๋””์˜ค[[audio]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ์•„๋ž˜์˜ ์˜ค๋””์˜ค ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModelForAudioClassification[[transformers.AutoModelForAudioClassification]] + +[[autodoc]] AutoModelForAudioClassification + +### TFAutoModelForAudioClassification[[transformers.TFAutoModelForAudioClassification]] + +[[autodoc]] TFAutoModelForAudioClassification + +### AutoModelForAudioFrameClassification[[transformers.AutoModelForAudioFrameClassification]] + +[[autodoc]] AutoModelForAudioFrameClassification + +### AutoModelForCTC[[transformers.AutoModelForCTC]] + +[[autodoc]] AutoModelForCTC + +### AutoModelForSpeechSeq2Seq[[transformers.AutoModelForSpeechSeq2Seq]] + +[[autodoc]] AutoModelForSpeechSeq2Seq + +### TFAutoModelForSpeechSeq2Seq[[transformers.TFAutoModelForSpeechSeq2Seq]] + +[[autodoc]] TFAutoModelForSpeechSeq2Seq + +### FlaxAutoModelForSpeechSeq2Seq[[transformers.FlaxAutoModelForSpeechSeq2Seq]] + +[[autodoc]] FlaxAutoModelForSpeechSeq2Seq + +### AutoModelForAudioXVector[[transformers.AutoModelForAudioXVector]] + +[[autodoc]] AutoModelForAudioXVector + +### AutoModelForTextToSpectrogram[[transformers.AutoModelForTextToSpectrogram]] + +[[autodoc]] AutoModelForTextToSpectrogram + +### AutoModelForTextToWaveform[[transformers.AutoModelForTextToWaveform]] + +[[autodoc]] AutoModelForTextToWaveform + +## ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ[[multimodal]] + +๋‹ค์Œ ์ž๋™ ํด๋ž˜์Šค๋“ค์€ ์•„๋ž˜์˜ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### AutoModelForTableQuestionAnswering[[transformers.AutoModelForTableQuestionAnswering]] + +[[autodoc]] AutoModelForTableQuestionAnswering + +### TFAutoModelForTableQuestionAnswering[[transformers.TFAutoModelForTableQuestionAnswering]] + +[[autodoc]] TFAutoModelForTableQuestionAnswering + +### AutoModelForDocumentQuestionAnswering[[transformers.AutoModelForDocumentQuestionAnswering]] + +[[autodoc]] AutoModelForDocumentQuestionAnswering + +### TFAutoModelForDocumentQuestionAnswering[[transformers.TFAutoModelForDocumentQuestionAnswering]] + +[[autodoc]] TFAutoModelForDocumentQuestionAnswering + +### AutoModelForVisualQuestionAnswering[[transformers.AutoModelForVisualQuestionAnswering]] + +[[autodoc]] AutoModelForVisualQuestionAnswering + +### AutoModelForVision2Seq[[transformers.AutoModelForVision2Seq]] + +[[autodoc]] AutoModelForVision2Seq + +### TFAutoModelForVision2Seq[[transformers.TFAutoModelForVision2Seq]] + +[[autodoc]] TFAutoModelForVision2Seq + +### FlaxAutoModelForVision2Seq[[transformers.FlaxAutoModelForVision2Seq]] + +[[autodoc]] FlaxAutoModelForVision2Seq + +## Time Series + +### AutoModelForTimeSeriesPrediction[[transformers.AutoModelForTimeSeriesPrediction]] + +[[autodoc]] AutoModelForTimeSeriesPrediction + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/autoformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/autoformer.md new file mode 100644 index 0000000000000000000000000000000000000000..10aacdb74ef40038f06e9693a29f6b94dd373ee3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/autoformer.md @@ -0,0 +1,50 @@ + + +# Autoformer[[autoformer]] + +## ๊ฐœ์š”[[overview]] + +The Autoformer ๋ชจ๋ธ์€ Haixu Wu, Jiehui Xu, Jianmin Wang, Mingsheng Long๊ฐ€ ์ œ์•ˆํ•œ [์˜คํ† ํฌ๋จธ: ์žฅ๊ธฐ ์‹œ๊ณ„์—ด ์˜ˆ์ธก์„ ์œ„ํ•œ ์ž๊ธฐ์ƒ๊ด€ ๋ถ„ํ•ด ํŠธ๋žœ์Šคํฌ๋จธ](https://huggingface.co/papers/2106.13008) ๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์‹ฌ์ธต ๋ถ„ํ•ด ์•„ํ‚คํ…์ฒ˜๋กœ ํ™•์žฅํ•˜์—ฌ, ์˜ˆ์ธก ๊ณผ์ •์—์„œ ์ถ”์„ธ์™€ ๊ณ„์ ˆ์„ฑ ์š”์†Œ๋ฅผ ์ ์ง„์ ์œผ๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์˜ˆ์ธก ์‹œ๊ฐ„์„ ์—ฐ์žฅํ•˜๋Š” ๊ฒƒ์€ ๊ทนํ•œ ๊ธฐ์ƒ ์กฐ๊ธฐ ๊ฒฝ๋ณด ๋ฐ ์žฅ๊ธฐ ์—๋„ˆ์ง€ ์†Œ๋น„ ๊ณ„ํš๊ณผ ๊ฐ™์€ ์‹ค์ œ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ์ค‘์š”ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์€ ์‹œ๊ณ„์—ด์˜ ์žฅ๊ธฐ ์˜ˆ์ธก ๋ฌธ์ œ๋ฅผ ์—ฐ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ๋“ค์€ ์žฅ๊ฑฐ๋ฆฌ ์ข…์†์„ฑ์„ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์…€ํ”„ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ฑ„ํƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์žฅ๊ธฐ ๋ฏธ๋ž˜์˜ ๋ณต์žกํ•œ ์‹œ๊ฐ„์  ํŒจํ„ด์œผ๋กœ ์ธํ•ด ๋ชจ๋ธ์ด ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์ข…์†์„ฑ์„ ์ฐพ๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๊ธด ์‹œ๊ณ„์—ด์˜ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์ ๋ณ„ ์…€ํ”„ ์–ดํ…์…˜์˜ ํฌ์†Œ ๋ฒ„์ „์„ ์ฑ„ํƒํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ •๋ณด ํ™œ์šฉ์˜ ๋ณ‘๋ชฉ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ๋„˜์–ด์„œ ์ž๊ธฐ์ƒ๊ด€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ฐ–์ถ˜ ์ƒˆ๋กœ์šด ๋ถ„ํ•ด ์•„ํ‚คํ…์ฒ˜์ธ Autoformer๋ฅผ ์„ค๊ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์‹œ๊ณ„์—ด ๋ถ„ํ•ด์˜ ์ „์ฒ˜๋ฆฌ ๊ด€ํ–‰์„ ๊นจ๊ณ  ์ด๋ฅผ ์‹ฌ์ธต ๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ๋‚ด๋ถ€ ๋ธ”๋ก์œผ๋กœ ํ˜์‹ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ค๊ณ„๋Š” Autoformer์— ๋ณต์žกํ•œ ์‹œ๊ณ„์—ด์— ๋Œ€ํ•œ ์ ์ง„์  ๋ถ„ํ•ด ๋Šฅ๋ ฅ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ™•๋ฅ  ๊ณผ์ • ์ด๋ก ์—์„œ ์˜๊ฐ์„ ๋ฐ›์•„ ์‹œ๊ณ„์—ด์˜ ์ฃผ๊ธฐ์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž๊ธฐ์ƒ๊ด€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์„ค๊ณ„ํ–ˆ์œผ๋ฉฐ, ์ด๋Š” ํ•˜์œ„ ์‹œ๊ณ„์—ด ์ˆ˜์ค€์—์„œ ์ข…์†์„ฑ ๋ฐœ๊ฒฌ๊ณผ ํ‘œํ˜„ ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž๊ธฐ์ƒ๊ด€์€ ํšจ์œจ์„ฑ๊ณผ ์ •ํ™•๋„ ๋ฉด์—์„œ ์…€ํ”„ ์–ดํ…์…˜๋ฅผ ๋Šฅ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์žฅ๊ธฐ ์˜ˆ์ธก์—์„œ Autoformer๋Š” ์—๋„ˆ์ง€, ๊ตํ†ต, ๊ฒฝ์ œ, ๋‚ ์”จ, ์งˆ๋ณ‘ ๋“ฑ 5๊ฐ€์ง€ ์‹ค์šฉ์  ์‘์šฉ ๋ถ„์•ผ๋ฅผ ํฌ๊ด„ํ•˜๋Š” 6๊ฐœ ๋ฒค์น˜๋งˆํฌ์—์„œ 38%์˜ ์ƒ๋Œ€์  ๊ฐœ์„ ์œผ๋กœ ์ตœ์ฒจ๋‹จ ์ •ํ™•๋„๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [elisim](https://huggingface.co/elisim)๊ณผ [kashif](https://huggingface.co/kashif)์— ์˜ํ•ด ๊ธฐ์—ฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/thuml/Autoformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ[[resources]] + +์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- HuggingFace ๋ธ”๋กœ๊ทธ์—์„œ Autoformer ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: [๋„ค, ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์‹œ๊ณ„์—ด ์˜ˆ์ธก์— ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค(+ Autoformer)](https://huggingface.co/blog/autoformer) + +## AutoformerConfig[[transformers.AutoformerConfig]] + +[[autodoc]] AutoformerConfig + +## AutoformerModel[[transformers.AutoformerModel]] + +[[autodoc]] AutoformerModel + - forward + +## AutoformerForPrediction[[transformers.AutoformerForPrediction]] + +[[autodoc]] AutoformerForPrediction + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bart.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bart.md new file mode 100644 index 0000000000000000000000000000000000000000..86d97b13103e9a11285b1d0c16fc8a2f57c01c77 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bart.md @@ -0,0 +1,217 @@ + + +# BART[[bart]] + +
+ +Models + + +Spaces + +
+ +## ๊ฐœ์š” [[overview]] + +Bart ๋ชจ๋ธ์€ 2019๋…„ 10์›” 29์ผ Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov, Luke Zettlemoyer๊ฐ€ ๋ฐœํ‘œํ•œ [BART: ์ž์—ฐ์–ด ์ƒ์„ฑ, ๋ฒˆ์—ญ, ์ดํ•ด๋ฅผ ์œ„ํ•œ ์žก์Œ ์ œ๊ฑฐ seq2seq ์‚ฌ์ „ ํ›ˆ๋ จ](https://huggingface.co/papers/1910.13461)์ด๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์— ๋”ฐ๋ฅด๋ฉด, + +- Bart๋Š” ์–‘๋ฐฉํ–ฅ ์ธ์ฝ”๋”(BERT์™€ ์œ ์‚ฌ)์™€ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋””์ฝ”๋”ฉํ•˜๋Š” ๋””์ฝ”๋”(GPT์™€ ์œ ์‚ฌ)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ‘œ์ค€ seq2seq/๊ธฐ๊ณ„ ๋ฒˆ์—ญ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ์‚ฌ์ „ ํ›ˆ๋ จ ์ž‘์—…์€ ์›๋ž˜ ๋ฌธ์žฅ์˜ ์ˆœ์„œ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ž๊ณ , ํ…์ŠคํŠธ์˜ ์ผ๋ถ€ ๊ตฌ๊ฐ„์„ ๋‹จ์ผ ๋งˆ์Šคํฌ ํ† ํฐ์œผ๋กœ ๋Œ€์ฒดํ•˜๋Š” ์ƒˆ๋กœ์šด ์ธํ•„๋ง(in-filling) ๋ฐฉ์‹์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. +- BART๋Š” ํŠนํžˆ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ฏธ์„ธ ์กฐ์ •์— ํšจ๊ณผ์ ์ด์ง€๋งŒ ์ดํ•ด ์ž‘์—…์—๋„ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. GLUE์™€ SQuAD์—์„œ ๋น„์Šทํ•œ ํ›ˆ๋ จ ๋ฆฌ์†Œ์Šค๋กœ RoBERTa์˜ ์„ฑ๋Šฅ๊ณผ ์ผ์น˜ํ•˜๋ฉฐ, ์ถ”์ƒ์  ๋Œ€ํ™”, ์งˆ์˜์‘๋‹ต, ์š”์•ฝ ์ž‘์—… ๋“ฑ์—์„œ ์ตœ๋Œ€ 6 ROUGE ์ ์ˆ˜์˜ ํ–ฅ์ƒ์„ ๋ณด์ด๋ฉฐ ์ƒˆ๋กœ์šด ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [sshleifer](https://huggingface.co/sshleifer)์— ์˜ํ•ด ๊ธฐ์—ฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ €์ž์˜ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/pytorch/fairseq/tree/master/examples/bart)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ:[[usage-tips]] + +- BART๋Š” ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์ž…๋ ฅ์„ ์™ผ์ชฝ๋ณด๋‹ค๋Š” ์˜ค๋ฅธ์ชฝ์— ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +- ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ ์žˆ๋Š” seq2seq ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์—๋Š” ์†์ƒ๋œ ํ† ํฐ์ด(corrupted tokens) ์ž…๋ ฅ๋˜๊ณ , ๋””์ฝ”๋”์—๋Š” ์›๋ž˜ ํ† ํฐ์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค(๋‹จ, ์ผ๋ฐ˜์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ ๋””์ฝ”๋”์ฒ˜๋Ÿผ ๋ฏธ๋ž˜ ๋‹จ์–ด๋ฅผ ์ˆจ๊ธฐ๋Š” ๋งˆ์Šคํฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค). ์‚ฌ์ „ ํ›ˆ๋ จ ์ž‘์—…์—์„œ ์ธ์ฝ”๋”์— ์ ์šฉ๋˜๋Š” ๋ณ€ํ™˜๋“ค์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +์‚ฌ์ „ ํ›ˆ๋ จ ์ž‘์—…์—์„œ ์ธ์ฝ”๋”์— ์ ์šฉ๋˜๋Š” ๋ณ€ํ™˜๋“ค์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + + * ๋ฌด์ž‘์œ„ ํ† ํฐ ๋งˆ์Šคํ‚น (BERT ์ฒ˜๋Ÿผ) + * ๋ฌด์ž‘์œ„ ํ† ํฐ ์‚ญ์ œ + * k๊ฐœ ํ† ํฐ์˜ ๋ฒ”์œ„๋ฅผ ๋‹จ์ผ ๋งˆ์Šคํฌ ํ† ํฐ์œผ๋กœ ๋งˆ์Šคํ‚น (0๊ฐœ ํ† ํฐ์˜ ๋ฒ”์œ„๋Š” ๋งˆ์Šคํฌ ํ† ํฐ์˜ ์‚ฝ์ž…์„ ์˜๋ฏธ) + * ๋ฌธ์žฅ ์ˆœ์„œ ๋’ค์„ž๊ธฐ + * ํŠน์ • ํ† ํฐ์—์„œ ์‹œ์ž‘ํ•˜๋„๋ก ๋ฌธ์„œ ํšŒ์ „ + +## ๊ตฌํ˜„ ๋…ธํŠธ[[implementation-notes]] + +- Bart๋Š” ์‹œํ€€์Šค ๋ถ„๋ฅ˜์— `token_type_ids`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ ์ ˆํ•˜๊ฒŒ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด์„œ [`BartTokenizer`]๋‚˜ + [`~BartTokenizer.encode`]๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- [`BartModel`]์˜ ์ •๋ฐฉํ–ฅ ์ „๋‹ฌ์€ `decoder_input_ids`๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์œผ๋ฉด `decoder_input_ids`๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค๋ฅธ ์ผ๋ถ€ ๋ชจ๋ธ๋ง API์™€ ๋‹ค๋ฅธ ์ ์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์˜ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ ์‚ฌ๋ก€๋Š” ๋งˆ์Šคํฌ ์ฑ„์šฐ๊ธฐ(mask filling)์ž…๋‹ˆ๋‹ค. +- ๋ชจ๋ธ ์˜ˆ์ธก์€ `forced_bos_token_id=0`์ผ ๋•Œ ๊ธฐ์กด ๊ตฌํ˜„๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋„๋ก ์˜๋„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, [`fairseq.encode`]์— ์ „๋‹ฌํ•˜๋Š” ๋ฌธ์ž์—ด์ด ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•  ๋•Œ๋งŒ ์ด ๊ธฐ๋Šฅ์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +- [`~generation.GenerationMixin.generate`]๋Š” ์š”์•ฝ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด๋ถ€ ์ƒ์„ฑ ์ž‘์—…์— ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ•ด๋‹น ๋ฌธ์„œ์˜ ์˜ˆ์ œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. +- *facebook/bart-large-cnn* ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ชจ๋ธ์€ `mask_token_id`๊ฐ€ ์—†๊ฑฐ๋‚˜, ๋งˆ์Šคํฌ ์ฑ„์šฐ๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + +## ๋งˆ์Šคํฌ ์ฑ„์šฐ๊ธฐ[[mask-filling]] + +`facebook/bart-base`์™€ `facebook/bart-large` ์ฒดํฌํฌ์ธํŠธ๋Š” ๋ฉ€ํ‹ฐ ํ† ํฐ ๋งˆ์Šคํฌ๋ฅผ ์ฑ„์šฐ๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import BartForConditionalGeneration, BartTokenizer + +model = BartForConditionalGeneration.from_pretrained("facebook/bart-large", forced_bos_token_id=0) +tok = BartTokenizer.from_pretrained("facebook/bart-large") +example_english_phrase = "UN Chief Says There Is No in Syria" +batch = tok(example_english_phrase, return_tensors="pt") +generated_ids = model.generate(batch["input_ids"]) +assert tok.batch_decode(generated_ids, skip_special_tokens=True) == [ + "UN Chief Says There Is No Plan to Stop Chemical Weapons in Syria" +] +``` + +## ์ž๋ฃŒ[[resources]] + +BART๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +- [๋ถ„์‚ฐํ˜• ํ•™์Šต: ๐Ÿค— Transformers์™€ Amazon SageMaker๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•œ BART/T5 ํ•™์Šต](https://huggingface.co/blog/sagemaker-distributed-training-seq2seq)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [blurr๋ฅผ ์ด์šฉํ•˜์—ฌ fastai๋กœ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•œ BART๋ฅผ ๋ฏธ์„ธ ์กฐ์ •](https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/posts/2021-05-25-mbart-sequence-classification-with-blurr.ipynb)ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๐ŸŒŽ +- [Trainer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐ€์ง€ ์–ธ์–ด๋กœ ์š”์•ฝํ•˜๊ธฐ ์œ„ํ•œ BART ๋ฏธ์„ธ ์กฐ์ •](https://colab.research.google.com/github/elsanns/xai-nlp-notebooks/blob/master/fine_tune_bart_summarization_two_langs.ipynb)ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๐ŸŒŽ +- ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb)์—์„œ๋Š” [`BartForConditionalGeneration`]์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š”](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization)[`FlaxBartForConditionalGeneration`]์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- Hugging Face `datasets` ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ [`BartForConditionalGeneration`]์„ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์˜ ์˜ˆ๋Š” ์ด [ํฌ๋Ÿผ ํ† ๋ก ](https://discuss.huggingface.co/t/train-bart-for-conditional-generation-e-g-summarization/1904)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [์š”์•ฝ](https://huggingface.co/course/chapter7/5?fw=pt#summarization)์žฅ. +- [์š”์•ฝ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/summarization) + + + +- [`BartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. +- [`TFBartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. +- [`FlaxBartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)์„ ์ฐธ๊ณ  ํ•˜์„ธ์š”. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง](https://huggingface.co/course/chapter7/3?fw=pt) ์ฑ•ํ„ฐ. +- [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) + + + +- [Seq2SeqTrainer๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋„์–ด๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” mBART๋ฅผ ๋ฏธ์„ธ ์กฐ์ •](https://colab.research.google.com/github/vasudevgupta7/huggingface-tutorials/blob/main/translation_training.ipynb)ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ฐ€์ด๋“œ. ๐ŸŒŽ +- [`BartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/translation)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. +- [`TFBartForConditionalGeneration`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/translation)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb)์„ ์ฐธ๊ณ  ํ•˜์„ธ์š”. +- [๋ฒˆ์—ญ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/translation) + +์ถ”๊ฐ€์ ์œผ๋กœ ๋ณผ ๊ฒƒ๋“ค: +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) +- [์งˆ๋ฌธ ๋‹ต๋ณ€ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering) +- [์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/language_modeling) +- ์ด [๋…ผ๋ฌธ](https://huggingface.co/papers/2010.13002)์€ [์ฆ๋ฅ˜๋œ ์ฒดํฌํฌ์ธํŠธ](https://huggingface.co/models?search=distilbart)์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## BartConfig[[transformers.BartConfig]] + +[[autodoc]] BartConfig + - all + +## BartTokenizer[[transformers.BartTokenizer]] + +[[autodoc]] BartTokenizer + - all + +## BartTokenizerFast[[transformers.BartTokenizerFast]] + +[[autodoc]] BartTokenizerFast + - all + + + + + +## BartModel[[transformers.BartModel]] + +[[autodoc]] BartModel + - forward + +## BartForConditionalGeneration[[transformers.BartForConditionalGeneration]] + +[[autodoc]] BartForConditionalGeneration + - forward + +## BartForSequenceClassification[[transformers.BartForSequenceClassification]] + +[[autodoc]] BartForSequenceClassification + - forward + +## BartForQuestionAnswering[[transformers.BartForQuestionAnswering]] + +[[autodoc]] BartForQuestionAnswering + - forward + +## BartForCausalLM[[transformers.BartForCausalLM]] + +[[autodoc]] BartForCausalLM + - forward + + + + +## TFBartModel[[transformers.TFBartModel]] + +[[autodoc]] TFBartModel + - call + +## TFBartForConditionalGeneration[[transformers.TFBartForConditionalGeneration]] + +[[autodoc]] TFBartForConditionalGeneration + - call + +## TFBartForSequenceClassification[[transformers.TFBartForSequenceClassification]] + +[[autodoc]] TFBartForSequenceClassification + - call + + + + +## FlaxBartModel[[transformers.FlaxBartModel]] + +[[autodoc]] FlaxBartModel + - __call__ + - encode + - decode + +## FlaxBartForConditionalGeneration[[transformers.FlaxBartForConditionalGeneration]] + +[[autodoc]] FlaxBartForConditionalGeneration + - __call__ + - encode + - decode + +## FlaxBartForSequenceClassification[[transformers.FlaxBartForSequenceClassification]] + +[[autodoc]] FlaxBartForSequenceClassification + - __call__ + - encode + - decode + +## FlaxBartForQuestionAnswering[[transformers.FlaxBartForQuestionAnswering]] + +[[autodoc]] FlaxBartForQuestionAnswering + - __call__ + - encode + - decode + +## FlaxBartForCausalLM[[transformers.FlaxBartForCausalLM]] + +[[autodoc]] FlaxBartForCausalLM + - __call__ + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/barthez.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/barthez.md new file mode 100644 index 0000000000000000000000000000000000000000..4df8eb2cd6995d8555b016108ba77c22b022999e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/barthez.md @@ -0,0 +1,60 @@ + + +# BARThez [[barthez]] + +## ๊ฐœ์š” [[overview]] + +BARThez ๋ชจ๋ธ์€ 2020๋…„ 10์›” 23์ผ, Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis์— ์˜ํ•ด [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://huggingface.co/papers/2010.12321)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋…ผ๋ฌธ์˜ ์ดˆ๋ก: + + +*์ž๊ธฐ์ง€๋„ ํ•™์Šต์— ์˜ํ•ด ๊ฐ€๋Šฅํ•ด์ง„ ๊ท€๋‚ฉ์  ์ „์ด ํ•™์Šต์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) ๋ถ„์•ผ ์ „๋ฐ˜์— ๊ฑธ์ณ ํฐ ๋ฐ˜ํ–ฅ์„ ์ผ์œผ์ผฐ์œผ๋ฉฐ, +BERT์™€ BART์™€ ๊ฐ™์€ ๋ชจ๋ธ๋“ค์€ ์ˆ˜๋งŽ์€ ์ž์—ฐ์–ด ์ดํ•ด ์ž‘์—…์—์„œ ์ƒˆ๋กœ์šด ์ตœ์ฒจ๋‹จ ์„ฑ๊ณผ๋ฅผ ๊ธฐ๋กํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ์˜ˆ์™ธ๊ฐ€ ์žˆ์ง€๋งŒ, +๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ๊ณผ ์—ฐ๊ตฌ๋Š” ์˜์–ด์— ์ง‘์ค‘๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ์—ฐ๊ตฌ์—์„œ๋Š” BARThez๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” (์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ํ•œ) ํ”„๋ž‘์Šค์–ด๋ฅผ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ BART ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +BARThez๋Š” ๊ณผ๊ฑฐ ์—ฐ๊ตฌ์—์„œ ์–ป์€ ๋งค์šฐ ํฐ ํ”„๋ž‘์Šค์–ด ๋‹จ์ผ ์–ธ์–ด ๋ง๋ญ‰์น˜๋กœ ์‚ฌ์ „ํ›ˆ๋ จ๋˜์—ˆ์œผ๋ฉฐ, +BART์˜ ๋ณ€ํ˜• ๋ฐฉ์‹์— ๋งž๊ฒŒ ์กฐ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +CamemBERT ๋ฐ FlauBERT์™€ ๊ฐ™์€ ๊ธฐ์กด์˜ BERT ๊ธฐ๋ฐ˜ ํ”„๋ž‘์Šค์–ด ๋ชจ๋ธ๊ณผ ๋‹ฌ๋ฆฌ, BARThez๋Š” ์ƒ์„ฑ ์ž‘์—…์— ํŠนํžˆ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” ์ธ์ฝ”๋”๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋””์ฝ”๋”๋„ ์‚ฌ์ „ํ›ˆ๋ จ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. +์šฐ๋ฆฌ๋Š” FLUE ๋ฒค์น˜๋งˆํฌ์—์„œ์˜ ํŒ๋ณ„ ์ž‘์—… ์™ธ์—๋„ ์ด ๋…ผ๋ฌธ๊ณผ ํ•จ๊ป˜ ๊ณต๊ฐœํ•˜๋Š” ์ƒˆ๋กœ์šด ์š”์•ฝ ๋ฐ์ดํ„ฐ์…‹์ธ OrangeSum์—์„œ BARThez๋ฅผ ํ‰๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. +๋˜ํ•œ ์ด๋ฏธ ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋‹ค๊ตญ์–ด BART์˜ ์‚ฌ์ „ํ›ˆ๋ จ์„ BARThez์˜ ๋ง๋ญ‰์น˜๋กœ ๊ณ„์† ์ง„ํ–‰ํ•˜์˜€์œผ๋ฉฐ, +๊ฒฐ๊ณผ์ ์œผ๋กœ ์–ป์–ด์ง„ ๋ชจ๋ธ์ธ mBARTHez๊ฐ€ ๊ธฐ๋ณธ BARThez๋ณด๋‹ค ์œ ์˜๋ฏธํ•œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋ณด์˜€๊ณ , +CamemBERT ๋ฐ FlauBERT์™€ ๋™๋“ฑํ•˜๊ฑฐ๋‚˜ ์ด๋ฅผ ๋Šฅ๊ฐ€ํ•จ์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [moussakam](https://huggingface.co/moussakam)์ด ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €์ž์˜ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/moussaKam/BARThez)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +BARThez ๊ตฌํ˜„์€ ๐Ÿค— BART์™€ ๋™์ผํ•˜๋‚˜, ํ† ํฐํ™”์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑ ํด๋ž˜์Šค์™€ ๊ทธ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ •๋ณด๋Š” [BART ๋ฌธ์„œ](bart)๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. +BARThez ์ „์šฉ ํ† ํฌ๋‚˜์ด์ €๋Š” ์•„๋ž˜์— ๋ฌธ์„œํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## ๋ฆฌ์†Œ์Šค [[resources]] + +- BARThez๋Š” ๐Ÿค— BART์™€ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ์ž‘์—…์— ๋งž์ถฐ ๋ฏธ์„ธ ์กฐ์ •๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ํ™•์ธํ•˜์„ธ์š”: + [examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md). + + +## BarthezTokenizer [[bartheztokenizer]] + +[[autodoc]] BarthezTokenizer + +## BarthezTokenizerFast [[bartheztokenizerfast]] + +[[autodoc]] BarthezTokenizerFast diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bartpho.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bartpho.md new file mode 100644 index 0000000000000000000000000000000000000000..72f4884164bddb54458f226635a3430bc8715c11 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bartpho.md @@ -0,0 +1,86 @@ + + +# BARTpho [[bartpho]] + +## ๊ฐœ์š” [[overview]] + +BARTpho ๋ชจ๋ธ์€ Nguyen Luong Tran, Duong Minh Le, Dat Quoc Nguyen์— ์˜ํ•ด [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://huggingface.co/papers/2109.09701)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” BARTpho_word์™€ BARTpho_syllable์˜ ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์œผ๋กœ BARTpho๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” ๋ฒ ํŠธ๋‚จ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ์ตœ์ดˆ์˜ ๋Œ€๊ทœ๋ชจ ๋‹จ์ผ ์–ธ์–ด ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +์šฐ๋ฆฌ์˜ BARTpho๋Š” ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋””๋…ธ์ด์ง• ๋ชจ๋ธ์ธ BART์˜ "large" ์•„ํ‚คํ…์ฒ˜์™€ ์‚ฌ์ „ํ›ˆ๋ จ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ, ์ƒ์„ฑํ˜• NLP ์ž‘์—…์— ํŠนํžˆ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. +๋ฒ ํŠธ๋‚จ์–ด ํ…์ŠคํŠธ ์š”์•ฝ์˜ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—… ์‹คํ—˜์—์„œ, +์ž๋™ ๋ฐ ์ธ๊ฐ„ ํ‰๊ฐ€ ๋ชจ๋‘์—์„œ BARTpho๊ฐ€ ๊ฐ•๋ ฅํ•œ ๊ธฐ์ค€์ธ mBART๋ฅผ ๋Šฅ๊ฐ€ํ•˜๊ณ  ์ตœ์‹  ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ–ˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. +์šฐ๋ฆฌ๋Š” ํ–ฅํ›„ ์—ฐ๊ตฌ ๋ฐ ๋ฒ ํŠธ๋‚จ์–ด ์ƒ์„ฑํ˜• NLP ์ž‘์—…์˜ ์‘์šฉ์„ ์ด‰์ง„ํ•˜๊ธฐ ์œ„ํ•ด BARTpho๋ฅผ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [dqnguyen](https://huggingface.co/dqnguyen)์ด ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/VinAIResearch/BARTpho)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ์‹œ [[usage-example]] + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bartpho = AutoModel.from_pretrained("vinai/bartpho-syllable") + +>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bartpho-syllable") + +>>> line = "Chรบng tรดi lร  nhแปฏng nghiรชn cแปฉu viรชn." + +>>> input_ids = tokenizer(line, return_tensors="pt") + +>>> with torch.no_grad(): +... features = bartpho(**input_ids) # ์ด์ œ ๋ชจ๋ธ ์ถœ๋ ฅ์€ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค + +>>> # With TensorFlow 2.0+: +>>> from transformers import TFAutoModel + +>>> bartpho = TFAutoModel.from_pretrained("vinai/bartpho-syllable") +>>> input_ids = tokenizer(line, return_tensors="tf") +>>> features = bartpho(**input_ids) +``` + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- mBART๋ฅผ ๋”ฐ๋ฅด๋ฉฐ, BARTpho๋Š” BART์˜ "large" ์•„ํ‚คํ…์ฒ˜์— ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์ƒ๋‹จ์— ์ถ”๊ฐ€์ ์ธ ๋ ˆ์ด์–ด ์ •๊ทœํ™” ๋ ˆ์ด์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ [BART ๋ฌธ์„œ](bart)์— ์žˆ๋Š” ์‚ฌ์šฉ ์˜ˆ์‹œ๋ฅผ BARTpho์— ๋งž๊ฒŒ ์ ์šฉํ•˜๋ ค๋ฉด +BART ์ „์šฉ ํด๋ž˜์Šค๋ฅผ mBART ์ „์šฉ ํด๋ž˜์Šค๋กœ ๋Œ€์ฒดํ•˜์—ฌ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด: + +```python +>>> from transformers import MBartForConditionalGeneration + +>>> bartpho = MBartForConditionalGeneration.from_pretrained("vinai/bartpho-syllable") +>>> TXT = "Chรบng tรดi lร  nghiรชn cแปฉu viรชn." +>>> input_ids = tokenizer([TXT], return_tensors="pt")["input_ids"] +>>> logits = bartpho(input_ids).logits +>>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item() +>>> probs = logits[0, masked_index].softmax(dim=0) +>>> values, predictions = probs.topk(5) +>>> print(tokenizer.decode(predictions).split()) +``` + +- ์ด ๊ตฌํ˜„์€ ํ† ํฐํ™”๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค: "monolingual_vocab_file"์€ ๋‹ค๊ตญ์–ด + XLM-RoBERTa์—์„œ ์ œ๊ณต๋˜๋Š” ์‚ฌ์ „ํ›ˆ๋ จ๋œ SentencePiece ๋ชจ๋ธ + "vocab_file"์—์„œ ์ถ”์ถœ๋œ ๋ฒ ํŠธ๋‚จ์–ด ์ „์šฉ ์œ ํ˜•์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. + ๋‹ค๋ฅธ ์–ธ์–ด๋“ค๋„ ์ด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋‹ค๊ตญ์–ด SentencePiece ๋ชจ๋ธ "vocab_file"์„ ํ•˜์œ„ ๋‹จ์–ด ๋ถ„ํ• ์— ์‚ฌ์šฉํ•˜๋ฉด, ์ž์‹ ์˜ ์–ธ์–ด ์ „์šฉ "monolingual_vocab_file"๊ณผ ํ•จ๊ป˜ BartphoTokenizer๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## BartphoTokenizer [[bartphotokenizer]] + +[[autodoc]] BartphoTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert-japanese.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert-japanese.md new file mode 100644 index 0000000000000000000000000000000000000000..8c21ef3558908e76288f670cf57d97252775252f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert-japanese.md @@ -0,0 +1,79 @@ + + +# ์ผ๋ณธ์–ด BERT (BertJapanese) [[bertjapanese]] + +## ๊ฐœ์š” [[overview]] + +์ผ๋ณธ์–ด ๋ฌธ์žฅ์— ํ•™์Šต๋œ BERT ๋ชจ๋ธ ์ž…๋‹ˆ๋‹ค. + +๊ฐ๊ฐ ์„œ๋กœ ๋‹ค๋ฅธ ํ† ํฐํ™” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๋ชจ๋ธ: + +- MeCab์™€ WordPiece๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€ ์˜์กด์„ฑ [fugashi](https://github.com/polm/fugashi)์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (์ด๋Š” [MeCab](https://taku910.github.io/mecab/)์˜ ๋ž˜ํผ์ž…๋‹ˆ๋‹ค.) +- ๋ฌธ์ž ๋‹จ์œ„๋กœ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +*MecabTokenizer*๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์˜์กด์„ฑ์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด `pip install transformers["ja"]` (๋˜๋Š” ์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๋Š” ๊ฒฝ์šฐ `pip install -e .["ja"]`) ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ž์„ธํ•œ ๋‚ด์šฉ์€ [cl-tohoku ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/cl-tohoku/bert-japanese)์—์„œ ํ™•์ธํ•˜์„ธ์š”. + +MeCab๊ณผ WordPiece ํ† ํฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ ์˜ˆ์‹œ: + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese") +>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese") + +>>> ## Input Japanese Text +>>> line = "ๅพ่ผฉใฏ็Œซใงใ‚ใ‚‹ใ€‚" + +>>> inputs = tokenizer(line, return_tensors="pt") + +>>> print(tokenizer.decode(inputs["input_ids"][0])) +[CLS] ๅพ่ผฉ ใฏ ็Œซ ใง ใ‚ใ‚‹ ใ€‚ [SEP] + +>>> outputs = bertjapanese(**inputs) +``` + +๋ฌธ์ž ํ† ํฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ ์˜ˆ์‹œ: + +```python +>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese-char") +>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-char") + +>>> ## Input Japanese Text +>>> line = "ๅพ่ผฉใฏ็Œซใงใ‚ใ‚‹ใ€‚" + +>>> inputs = tokenizer(line, return_tensors="pt") + +>>> print(tokenizer.decode(inputs["input_ids"][0])) +[CLS] ๅพ ่ผฉ ใฏ ็Œซ ใง ใ‚ ใ‚‹ ใ€‚ [SEP] + +>>> outputs = bertjapanese(**inputs) +``` + + + +์ด๋Š” ํ† ํฐํ™” ๋ฐฉ๋ฒ•์„ ์ œ์™ธํ•˜๊ณ ๋Š” BERT์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. API ์ฐธ์กฐ ์ •๋ณด๋Š” [BERT ๋ฌธ์„œ](https://huggingface.co/docs/transformers/main/en/model_doc/bert)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. +์ด ๋ชจ๋ธ์€ [cl-tohoku](https://huggingface.co/cl-tohoku)๊ป˜์„œ ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. + + + + +## BertJapaneseTokenizer + +[[autodoc]] BertJapaneseTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert.md new file mode 100644 index 0000000000000000000000000000000000000000..0488a1f9ab2378952a9742056a5672877deeef51 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bert.md @@ -0,0 +1,340 @@ + + +# BERT[[BERT]] + +
+ +Models + + +Spaces + +
+ +## ๊ฐœ์š”[[Overview]] + +BERT ๋ชจ๋ธ์€ Jacob Devlin. Ming-Wei Chang, Kenton Lee, Kristina Touranova๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://huggingface.co/papers/1810.04805)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. BERT๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ์–‘๋ฐฉํ–ฅ ํŠธ๋žœ์Šคํฌ๋จธ๋กœ, Toronto Book Corpus์™€ Wikipedia๋กœ ๊ตฌ์„ฑ๋œ ๋Œ€๊ทœ๋ชจ ์ฝ”ํผ์Šค์—์„œ ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง๊ณผ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next Sentence Prediction) ๋ชฉํ‘œ๋ฅผ ๊ฒฐํ•ฉํ•ด ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” BERT(Bidirectional Encoder Representations from Transformers)๋ผ๋Š” ์ƒˆ๋กœ์šด ์–ธ์–ด ํ‘œํ˜„ ๋ชจ๋ธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ตœ๊ทผ์˜ ๋‹ค๋ฅธ ์–ธ์–ด ํ‘œํ˜„ ๋ชจ๋ธ๋“ค๊ณผ ๋‹ฌ๋ฆฌ, BERT๋Š” ๋ชจ๋“  ๊ณ„์ธต์—์„œ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์–‘์ชฝ ๋ฌธ๋งฅ์„ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋น„์ง€๋„ ํ•™์Šต๋œ ํ…์ŠคํŠธ์—์„œ ๊นŠ์ด ์žˆ๋Š” ์–‘๋ฐฉํ–ฅ ํ‘œํ˜„์„ ์‚ฌ์ „ ํ•™์Šตํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, ์‚ฌ์ „ ํ•™์Šต๋œ BERT ๋ชจ๋ธ์€ ์ถ”๊ฐ€์ ์ธ ์ถœ๋ ฅ ๊ณ„์ธต ํ•˜๋‚˜๋งŒ์œผ๋กœ ์งˆ๋ฌธ ์‘๋‹ต, ์–ธ์–ด ์ถ”๋ก ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž‘์—…์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํŠน์ • ์ž‘์—…์„ ์œ„ํ•ด ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.* + +*BERT๋Š” ๊ฐœ๋…์ ์œผ๋กœ ๋‹จ์ˆœํ•˜๋ฉด์„œ๋„ ์‹ค์ฆ์ ์œผ๋กœ ๊ฐ•๋ ฅํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BERT๋Š” 11๊ฐœ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ณผ์ œ์—์„œ ์ƒˆ๋กœ์šด ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ–ˆ์œผ๋ฉฐ, GLUE ์ ์ˆ˜๋ฅผ 80.5% (7.7% ํฌ์ธํŠธ ์ ˆ๋Œ€ ๊ฐœ์„ )๋กœ, MultiNLI ์ •ํ™•๋„๋ฅผ 86.7% (4.6% ํฌ์ธํŠธ ์ ˆ๋Œ€ ๊ฐœ์„ ), SQuAD v1.1 ์งˆ๋ฌธ ์‘๋‹ต ํ…Œ์ŠคํŠธ์—์„œ F1 ์ ์ˆ˜๋ฅผ 93.2 (1.5% ํฌ์ธํŠธ ์ ˆ๋Œ€ ๊ฐœ์„ )๋กœ, SQuAD v2.0์—์„œ F1 ์ ์ˆ˜๋ฅผ 83.1 (5.1% ํฌ์ธํŠธ ์ ˆ๋Œ€ ๊ฐœ์„ )๋กœ ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [thomwolf](https://huggingface.co/thomwolf)๊ฐ€ ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/google-research/bert)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[Usage tips]] + +- BERT๋Š” ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ ์ž…๋ ฅ์„ ์™ผ์ชฝ์ด ์•„๋‹ˆ๋ผ ์˜ค๋ฅธ์ชฝ์—์„œ ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. +- BERT๋Š” ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ(MLM)๊ณผ Next Sentence Prediction(NSP) ๋ชฉํ‘œ๋กœ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋งˆ์Šคํ‚น๋œ ํ† ํฐ ์˜ˆ์ธก๊ณผ ์ „๋ฐ˜์ ์ธ ์ž์—ฐ์–ด ์ดํ•ด(NLU)์— ๋›ฐ์–ด๋‚˜์ง€๋งŒ, ํ…์ŠคํŠธ ์ƒ์„ฑ์—๋Š” ์ตœ์ ํ™”๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- BERT์˜ ์‚ฌ์ „ ํ•™์Šต ๊ณผ์ •์—์„œ๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚นํ•˜์—ฌ ์ผ๋ถ€ ํ† ํฐ์„ ๋งˆ์Šคํ‚นํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ํ† ํฐ ์ค‘ ์•ฝ 15%๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋งˆ์Šคํ‚น๋ฉ๋‹ˆ๋‹ค: + + * 80% ํ™•๋ฅ ๋กœ ๋งˆ์Šคํฌ ํ† ํฐ์œผ๋กœ ๋Œ€์ฒด + * 10% ํ™•๋ฅ ๋กœ ์ž„์˜์˜ ๋‹ค๋ฅธ ํ† ํฐ์œผ๋กœ ๋Œ€์ฒด + * 10% ํ™•๋ฅ ๋กœ ์›๋ž˜ ํ† ํฐ ๊ทธ๋Œ€๋กœ ์œ ์ง€ + +- ๋ชจ๋ธ์˜ ์ฃผ์š” ๋ชฉํ‘œ๋Š” ์›๋ณธ ๋ฌธ์žฅ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ๋ชฉํ‘œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: ์ž…๋ ฅ์œผ๋กœ ๋ฌธ์žฅ A์™€ B (์‚ฌ์ด์—๋Š” ๊ตฌ๋ถ„ ํ† ํฐ์ด ์žˆ์Œ)๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด ๋ฌธ์žฅ ์Œ์ด ์—ฐ์†๋  ํ™•๋ฅ ์€ 50%์ด๋ฉฐ, ๋‚˜๋จธ์ง€ 50%๋Š” ์„œ๋กœ ๋ฌด๊ด€ํ•œ ๋ฌธ์žฅ๋“ค์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ด ๋‘ ๋ฌธ์žฅ์ด ์•„๋‹Œ์ง€๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +### Scaled Dot Product Attention(SDPA) ์‚ฌ์šฉํ•˜๊ธฐ [[Using Scaled Dot Product Attention (SDPA)]] + +Pytorch๋Š” `torch.nn.functional`์˜ ์ผ๋ถ€๋กœ Scaled Dot Product Attention(SDPA) ์—ฐ์‚ฐ์ž๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ณผ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๊ณต์‹ ๋ฌธ์„œ](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)๋‚˜ [GPU Inference](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +`torch>=2.1.1`์—์„œ๋Š” ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ SDPA๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€๋งŒ, `from_pretrained()`ํ•จ์ˆ˜์—์„œ `attn_implementation="sdpa"`๋ฅผ ์„ค์ •ํ•˜์—ฌ SDPA๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +from transformers import BertModel + +model = BertModel.from_pretrained("bert-base-uncased", torch_dtype=torch.float16, attn_implementation="sdpa") +... +``` + +์ตœ์  ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16` ๋˜๋Š” `torch.bfloat16`)๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +๋กœ์ปฌ ๋ฒค์น˜๋งˆํฌ (A100-80GB, CPUx12, RAM 96.6GB, PyTorch 2.2.0, OS Ubuntu 22.04)์—์„œ `float16`์„ ์‚ฌ์šฉํ•ด ํ•™์Šต ๋ฐ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†๋„ ํ–ฅ์ƒ์ด ๊ด€์ฐฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +#### ํ•™์Šต [[Training]] + +|batch_size|seq_len|Time per batch (eager - s)|Time per batch (sdpa - s)|Speedup (%)|Eager peak mem (MB)|sdpa peak mem (MB)|Mem saving (%)| +|----------|-------|--------------------------|-------------------------|-----------|-------------------|------------------|--------------| +|4 |256 |0.023 |0.017 |35.472 |939.213 |764.834 |22.800 | +|4 |512 |0.023 |0.018 |23.687 |1970.447 |1227.162 |60.569 | +|8 |256 |0.023 |0.018 |23.491 |1594.295 |1226.114 |30.028 | +|8 |512 |0.035 |0.025 |43.058 |3629.401 |2134.262 |70.054 | +|16 |256 |0.030 |0.024 |25.583 |2874.426 |2134.262 |34.680 | +|16 |512 |0.064 |0.044 |46.223 |6964.659 |3961.013 |75.830 | + +#### ์ถ”๋ก  [[Inference]] + +|batch_size|seq_len|Per token latency eager (ms)|Per token latency SDPA (ms)|Speedup (%)|Mem eager (MB)|Mem BT (MB)|Mem saved (%)| +|----------|-------|----------------------------|---------------------------|-----------|--------------|-----------|-------------| +|1 |128 |5.736 |4.987 |15.022 |282.661 |282.924 |-0.093 | +|1 |256 |5.689 |4.945 |15.055 |298.686 |298.948 |-0.088 | +|2 |128 |6.154 |4.982 |23.521 |314.523 |314.785 |-0.083 | +|2 |256 |6.201 |4.949 |25.303 |347.546 |347.033 |0.148 | +|4 |128 |6.049 |4.987 |21.305 |378.895 |379.301 |-0.107 | +|4 |256 |6.285 |5.364 |17.166 |443.209 |444.382 |-0.264 | + + + +## ์ž๋ฃŒ[[Resources]] + +BERT๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +- [BERT ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ (๋‹ค๋ฅธ ์–ธ์–ด๋กœ)](https://www.philschmid.de/bert-text-classification-in-a-different-language)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ BERT (๋ฐ ๊ด€๋ จ ๋ชจ๋ธ) ๋ฏธ์„ธ ์กฐ์ •](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/BERT/Fine_tuning_BERT_(and_friends)_for_multi_label_text_classification.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. +- [PyTorch๋ฅผ ์ด์šฉํ•ด BERT๋ฅผ ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](htt๊ธฐps://colab.research.google.com/github/abhimishra91/transformers-tutorials/blob/master/transformers_multi_label_classification.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๐ŸŒŽ +- [BERT๋กœ EncoderDecoder ๋ชจ๋ธ์„ warm-startํ•˜์—ฌ ์š”์•ฝํ•˜๊ธฐ](https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/BERT2BERT_for_CNN_Dailymail.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. +- [`BertForSequenceClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBertForSequenceClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxBertForSequenceClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) + + + +- [Keras์™€ ํ•จ๊ป˜ Hugging Face Transformers๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„์˜๋ฆฌ BERT๋ฅผ ๊ฐœ์ฒด๋ช… ์ธ์‹(NER)์šฉ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/huggingface-transformers-keras-tf)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [BERT๋ฅผ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/BERT/Custom_Named_Entity_Recognition_with_BERT_only_first_wordpiece.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ wordpiece์—๋งŒ ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  wordpiece์— ๋ ˆ์ด๋ธ”์„ ์ „ํŒŒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ [์ด ๋ฒ„์ „](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/BERT/Custom_Named_Entity_Recognition_with_BERT.ipynb)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- [`BertForTokenClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBertForTokenClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxBertForTokenClassification`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [ํ† ํฐ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/2?fw=pt). +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification) + + + +- [`BertForMaskedLM`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBertForMaskedLM`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy) ์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxBertForMaskedLM`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/3?fw=pt). +- [๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) + + + +- [`BertForQuestionAnswering`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBertForQuestionAnswering`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering) ์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxBertForQuestionAnswering`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [์งˆ๋ฌธ ๋‹ต๋ณ€ ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/7?fw=pt). +- [์งˆ๋ฌธ ๋‹ต๋ณ€ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering) + +**๋‹ค์ค‘ ์„ ํƒ** +- [`BertForMultipleChoice`]์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFBertForMultipleChoice`]์ด [์—์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [๋‹ค์ค‘ ์„ ํƒ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/multiple_choice) + +โšก๏ธ **์ถ”๋ก ** +- [Hugging Face Transformers์™€ AWS Inferentia๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BERT ์ถ”๋ก ์„ ๊ฐ€์†ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/blog/bert-inferentia-sagemaker)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [GPU์—์„œ DeepSpeed-Inference๋กœ BERT ์ถ”๋ก ์„ ๊ฐ€์†ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/bert-deepspeed-inference)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. + +โš™๏ธ **์‚ฌ์ „ ํ•™์Šต** +- [Hugging Face Optimum์œผ๋กœ Transformers๋ฅผ ONMX๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/pre-training-bert-habana)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. + +๐Ÿš€ **๋ฐฐํฌ** +- [Hugging Face Optimum์œผ๋กœ Transformers๋ฅผ ONMX๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/convert-transformers-to-onnx)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [AWS์—์„œ Hugging Face Transformers๋ฅผ ์œ„ํ•œ Habana Gaudi ๋”ฅ๋Ÿฌ๋‹ ํ™˜๊ฒฝ ์„ค์ • ๋ฐฉ๋ฒ•](https://www.philschmid.de/getting-started-habana-gaudi#conclusion)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [Hugging Face Transformers, Amazon SageMaker ๋ฐ Terraform ๋ชจ๋“ˆ์„ ์ด์šฉํ•œ BERT ์ž๋™ ํ™•์žฅ](https://www.philschmid.de/terraform-huggingface-amazon-sagemaker-advanced)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [Hugging Face, AWS Lambda, Docker๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฆฌ์Šค BERT ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/serverless-bert-with-huggingface-aws-lambda-docker)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [Amazon SageMaker์™€ Training Compiler๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Transformers์—์„œ BERT ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/huggingface-amazon-sagemaker-training-compiler)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ. +- [Amazon SageMaker๋ฅผ ์‚ฌ์šฉํ•œ Transformers์™€ BERT์˜ ์ž‘์—…๋ณ„ ์ง€์‹ ์ฆ๋ฅ˜](https://www.philschmid.de/knowledge-distillation-bert-transformers)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. + +## BertConfig + +[[autodoc]] BertConfig + - all + +## BertTokenizer + +[[autodoc]] BertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + + + + +## BertTokenizerFast + +[[autodoc]] BertTokenizerFast + + + + +## TFBertTokenizer + +[[autodoc]] TFBertTokenizer + + + + +## Bert specific outputs + +[[autodoc]] models.bert.modeling_bert.BertForPreTrainingOutput + +[[autodoc]] models.bert.modeling_tf_bert.TFBertForPreTrainingOutput + +[[autodoc]] models.bert.modeling_flax_bert.FlaxBertForPreTrainingOutput + + + + + +## BertModel + +[[autodoc]] BertModel + - forward + +## BertForPreTraining + +[[autodoc]] BertForPreTraining + - forward + +## BertLMHeadModel + +[[autodoc]] BertLMHeadModel + - forward + +## BertForMaskedLM + +[[autodoc]] BertForMaskedLM + - forward + +## BertForNextSentencePrediction + +[[autodoc]] BertForNextSentencePrediction + - forward + +## BertForSequenceClassification + +[[autodoc]] BertForSequenceClassification + - forward + +## BertForMultipleChoice + +[[autodoc]] BertForMultipleChoice + - forward + +## BertForTokenClassification + +[[autodoc]] BertForTokenClassification + - forward + +## BertForQuestionAnswering + +[[autodoc]] BertForQuestionAnswering + - forward + + + + +## TFBertModel + +[[autodoc]] TFBertModel + - call + +## TFBertForPreTraining + +[[autodoc]] TFBertForPreTraining + - call + +## TFBertModelLMHeadModel + +[[autodoc]] TFBertLMHeadModel + - call + +## TFBertForMaskedLM + +[[autodoc]] TFBertForMaskedLM + - call + +## TFBertForNextSentencePrediction + +[[autodoc]] TFBertForNextSentencePrediction + - call + +## TFBertForSequenceClassification + +[[autodoc]] TFBertForSequenceClassification + - call + +## TFBertForMultipleChoice + +[[autodoc]] TFBertForMultipleChoice + - call + +## TFBertForTokenClassification + +[[autodoc]] TFBertForTokenClassification + - call + +## TFBertForQuestionAnswering + +[[autodoc]] TFBertForQuestionAnswering + - call + + + + +## FlaxBertModel + +[[autodoc]] FlaxBertModel + - __call__ + +## FlaxBertForPreTraining + +[[autodoc]] FlaxBertForPreTraining + - __call__ + +## FlaxBertForCausalLM + +[[autodoc]] FlaxBertForCausalLM + - __call__ + +## FlaxBertForMaskedLM + +[[autodoc]] FlaxBertForMaskedLM + - __call__ + +## FlaxBertForNextSentencePrediction + +[[autodoc]] FlaxBertForNextSentencePrediction + - __call__ + +## FlaxBertForSequenceClassification + +[[autodoc]] FlaxBertForSequenceClassification + - __call__ + +## FlaxBertForMultipleChoice + +[[autodoc]] FlaxBertForMultipleChoice + - __call__ + +## FlaxBertForTokenClassification + +[[autodoc]] FlaxBertForTokenClassification + - __call__ + +## FlaxBertForQuestionAnswering + +[[autodoc]] FlaxBertForQuestionAnswering + - __call__ + + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bertweet.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bertweet.md new file mode 100644 index 0000000000000000000000000000000000000000..7a46087d0a8ebf85929aadbb74808ffe029537c9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/bertweet.md @@ -0,0 +1,67 @@ + + +# BERTweet [[bertweet]] + +## ๊ฐœ์š” [[overview]] + +BERTweet ๋ชจ๋ธ์€ Dat Quoc Nguyen, Thanh Vu, Anh Tuan Nguyen์— ์˜ํ•ด [BERTweet: A pre-trained language model for English Tweets](https://www.aclweb.org/anthology/2020.emnlp-demos.2.pdf) ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก : + +*์˜์–ด ํŠธ์œ—์„ ์œ„ํ•œ ์ตœ์ดˆ์˜ ๊ณต๊ฐœ ๋Œ€๊ทœ๋ชจ ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ์ธ BERTweet์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. +BERTweet์€ BERT-base(Devlin et al., 2019)์™€ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, RoBERTa ์‚ฌ์ „ ํ•™์Šต ์ ˆ์ฐจ(Liu et al., 2019)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์‹คํ—˜ ๊ฒฐ๊ณผ, BERTweet์€ ๊ฐ•๋ ฅํ•œ ๊ธฐ์ค€ ๋ชจ๋ธ์ธ RoBERTa-base ๋ฐ XLM-R-base(Conneau et al., 2020)์˜ ์„ฑ๋Šฅ์„ ๋Šฅ๊ฐ€ํ•˜์—ฌ ์„ธ ๊ฐ€์ง€ ํŠธ์œ— NLP ์ž‘์—…(ํ’ˆ์‚ฌ ํƒœ๊น…, ๊ฐœ์ฒด๋ช… ์ธ์‹, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜)์—์„œ ์ด์ „ ์ตœ์‹  ๋ชจ๋ธ๋ณด๋‹ค ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [dqnguyen](https://huggingface.co/dqnguyen) ๊ป˜์„œ ๊ธฐ์—ฌํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/VinAIResearch/BERTweet).์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ์‚ฌ์šฉ ์˜ˆ์‹œ [[usage-example]] + +```python +>>> import torch +>>> from transformers import AutoModel, AutoTokenizer + +>>> bertweet = AutoModel.from_pretrained("vinai/bertweet-base") + +>>> # ํŠธ๋žœ์Šคํฌ๋จธ ๋ฒ„์ „ 4.x ์ด์ƒ : +>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False) + +>>> # ํŠธ๋žœ์Šคํฌ๋จธ ๋ฒ„์ „ 3.x ์ด์ƒ: +>>> # tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base") + +>>> # ์ž…๋ ฅ๋œ ํŠธ์œ—์€ ์ด๋ฏธ ์ •๊ทœํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค! +>>> line = "SC has first two presumptive cases of coronavirus , DHEC confirms HTTPURL via @USER :cry:" + +>>> input_ids = torch.tensor([tokenizer.encode(line)]) + +>>> with torch.no_grad(): +... features = bertweet(input_ids) # Models outputs are now tuples + +>>> # With TensorFlow 2.0+: +>>> # from transformers import TFAutoModel +>>> # bertweet = TFAutoModel.from_pretrained("vinai/bertweet-base") +``` + + + +์ด ๊ตฌํ˜„์€ ํ† ํฐํ™” ๋ฐฉ๋ฒ•์„ ์ œ์™ธํ•˜๊ณ ๋Š” BERT์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. API ์ฐธ์กฐ ์ •๋ณด๋Š” [BERT ๋ฌธ์„œ](bert) ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +## Bertweet ํ† ํฐํ™”(BertweetTokenizer) [[transformers.BertweetTokenizer]] + +[[autodoc]] BertweetTokenizer diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/biogpt.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/biogpt.md new file mode 100644 index 0000000000000000000000000000000000000000..4b24af8493b9d5dc025416476cb75876c91e5249 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/biogpt.md @@ -0,0 +1,109 @@ + + +# BioGPT [[biogpt]] + +## ๊ฐœ์š” [[overview]] + +BioGPT๋Š” Renqian Luo, Liai Sun, Yingce Xia, Tao Qin, Sheng Zhang, Hoifung Poon, Tie-Yan Liu์— ์˜ํ•ด [BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) ์—์„œ ์ œ์•ˆ๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BioGPT๋Š” ์ƒ๋ฌผ์˜ํ•™ ํ…์ŠคํŠธ ์ƒ์„ฑ๊ณผ ๋งˆ์ด๋‹์„ ์œ„ํ•ด ๋„๋ฉ”์ธ์— ํŠนํ™”๋œ ์ƒ์„ฑํ˜• ์‚ฌ์ „ ํ•™์Šต ํŠธ๋žœ์Šคํฌ๋จธ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BioGPT๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ์–ธ์–ด ๋ชจ๋ธ ๊ตฌ์กฐ๋ฅผ ๋”ฐ๋ฅด๋ฉฐ, 1,500๋งŒ ๊ฐœ์˜ PubMed ์ดˆ๋ก์„ ์ด์šฉํ•ด ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ƒ๋ฌผ์˜ํ•™ ๋ถ„์•ผ์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ์€ ์ผ๋ฐ˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ถ„์•ผ์—์„œ์˜ ์„ฑ๊ณต์— ์˜๊ฐ์„ ๋ฐ›์•„ ์ ์  ๋” ๋งŽ์€ ์ฃผ๋ชฉ์„ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜ ์–ธ์–ด ๋ถ„์•ผ์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๊ณ„ํ†ต์ธ BERT(๋ฐ ๊ทธ ๋ณ€ํ˜•)์™€ GPT(๋ฐ ๊ทธ ๋ณ€ํ˜•) ์ค‘ ์ฒซ ๋ฒˆ์งธ๋Š” ์ƒ๋ฌผ์˜ํ•™ ๋ถ„์•ผ์—์„œ BioBERT์™€ PubMedBERT์™€ ๊ฐ™์ด ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์—ฐ๊ตฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋‹ค์–‘ํ•œ ๋ถ„๋ฅ˜ ๊ธฐ๋ฐ˜์˜ ์ƒ๋ฌผ์˜ํ•™ ์ž‘์—…์—์„œ ํฐ ์„ฑ๊ณต์„ ๊ฑฐ๋‘์—ˆ์ง€๋งŒ, ์ƒ์„ฑ ๋Šฅ๋ ฅ์˜ ๋ถ€์กฑ์€ ๊ทธ๋“ค์˜ ์ ์šฉ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ๋Œ€๊ทœ๋ชจ ์ƒ๋ฌผ์˜ํ•™ ๋ฌธํ—Œ์„ ์‚ฌ์ „ ํ•™์Šตํ•œ ๋„๋ฉ”์ธ ํŠนํ™” ์ƒ์„ฑํ˜• ํŠธ๋žœ์Šคํฌ๋จธ ์–ธ์–ด ๋ชจ๋ธ์ธ BioGPT๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” 6๊ฐœ์˜ ์ƒ๋ฌผ์˜ํ•™ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์—์„œ BioGPT๋ฅผ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ, ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์—์„œ ์ด์ „ ๋ชจ๋ธ๋ณด๋‹ค ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, BC5CDR, KD-DTI, DDI ์—”๋“œ-ํˆฌ-์—”๋“œ ๊ด€๊ณ„ ์ถ”์ถœ ์ž‘์—…์—์„œ ๊ฐ๊ฐ 44.98%, 38.42%, 40.76%์˜ F1 ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•˜์˜€์œผ๋ฉฐ, PubMedQA์—์„œ 78.2%์˜ ์ •ํ™•๋„๋ฅผ ๋‹ฌ์„ฑํ•ด ์ƒˆ๋กœ์šด ๊ธฐ๋ก์„ ์„ธ์› ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ์— ๋Œ€ํ•œ ์‚ฌ๋ก€ ์—ฐ๊ตฌ๋Š” ์ƒ๋ฌผ์˜ํ•™ ์šฉ์–ด์— ๋Œ€ํ•œ ์œ ์ฐฝํ•œ ์„ค๋ช…์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์žˆ์–ด BioGPT์˜ ์žฅ์ ์„ ๋”์šฑ ์ž…์ฆํ–ˆ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [kamalkraj](https://huggingface.co/kamalkraj)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/microsoft/BioGPT)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- BioGPT๋Š” ์ ˆ๋Œ€์  ์œ„์น˜ ์ž„๋ฒ ๋”ฉ(absolute position embedding)์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ์ž…๋ ฅ์„ ์™ผ์ชฝ์ด ์•„๋‹Œ ์˜ค๋ฅธ์ชฝ์—์„œ ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. +- BioGPT๋Š” ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(Casual Langague Modeling, CLM) ๋ชฉํ‘œ๋กœ ํ•™์Šต๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐ ๊ฐ•๋ ฅํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ BioGPT๋Š” ๊ตฌ๋ฌธ์ ์œผ๋กœ ์ผ๊ด€๋œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์˜ˆ์‹œ ์Šคํฌ๋ฆฝํŠธ `run_generation.py`์—์„œ ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ด ๋ชจ๋ธ์€ `past_key_values`(PyTorch ์šฉ)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ์ด์ „์— ๊ณ„์‚ฐ๋œ ํ‚ค/๊ฐ’ ์–ดํ…์…˜ ์Œ์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ํ…์ŠคํŠธ ์ƒ์„ฑ ์ค‘ ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PyTorch์—์„œ `past_key_values` ์ธ์ˆ˜๋Š” BioGptForCausalLM.forward() ๋ฉ”์†Œ๋“œ์—์„œ ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +### Scaled Dot Product Attention(SDPA) ์‚ฌ์šฉ [[using-scaled-dot-product-attention-sdpa]] + +PyTorch๋Š” `torch.nn.functional`์˜ ์ผ๋ถ€๋กœ ์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜(SDPA) ์—ฐ์‚ฐ์ž๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ณผ ์‚ฌ์šฉ ์ค‘์ธ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ตฌํ˜„์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๊ณต์‹ ๋ฌธ์„œ](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) ๋˜๋Š” [GPU ์ถ”๋ก ](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +`torch>=2.1.1`์—์„œ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ SDPA๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, `attn_implementation="sdpa"`๋ฅผ `from_pretrained()`์—์„œ ์„ค์ •ํ•˜์—ฌ SDPA ์‚ฌ์šฉ์„ ๋ช…์‹œ์ ์œผ๋กœ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +from transformers import BioGptForCausalLM +model = BioGptForCausalLM.from_pretrained("microsoft/biogpt", attn_implementation="sdpa", torch_dtype=torch.float16) +``` + +NVIDIA GeForce RTX 2060-8GB, PyTorch 2.3.1, Ubuntu 20.04 ํ™˜๊ฒฝ์—์„œ `float16` ๋ฐ CausalLM ํ—ค๋“œ๊ฐ€ ์žˆ๋Š” `microsoft/biogpt` ๋ชจ๋ธ๋กœ ๋กœ์ปฌ ๋ฒค์น˜๋งˆํฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ, ํ›ˆ๋ จ ์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†๋„ ํ–ฅ์ƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. + +์ตœ์ ์˜ ์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16` ๋˜๋Š” `torch.bfloat16`)๋กœ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +| num_training_steps | batch_size | seq_len | is cuda | Time per batch (eager - s) | Time per batch (sdpa - s) | Speedup (%) | Eager peak mem (MB) | sdpa peak mem (MB) | Mem saving (%) | +|--------------------|------------|---------|---------|----------------------------|---------------------------|-------------|---------------------|--------------------|----------------| +| 100 | 1 | 128 | False | 0.038 | 0.031 | 21.301 | 1601.862 | 1601.497 | 0.023 | +| 100 | 1 | 256 | False | 0.039 | 0.034 | 15.084 | 1624.944 | 1625.296 | -0.022 | +| 100 | 2 | 128 | False | 0.039 | 0.033 | 16.820 | 1624.567 | 1625.296 | -0.045 | +| 100 | 2 | 256 | False | 0.065 | 0.059 | 10.255 | 1672.164 | 1672.164 | 0.000 | +| 100 | 4 | 128 | False | 0.062 | 0.058 | 6.998 | 1671.435 | 1672.164 | -0.044 | +| 100 | 4 | 256 | False | 0.113 | 0.100 | 13.316 | 2350.179 | 1848.435 | 27.144 | +| 100 | 8 | 128 | False | 0.107 | 0.098 | 9.883 | 2098.521 | 1848.435 | 13.530 | +| 100 | 8 | 256 | False | 0.222 | 0.196 | 13.413 | 3989.980 | 2986.492 | 33.601 | + +NVIDIA GeForce RTX 2060-8GB, PyTorch 2.3.1, Ubuntu 20.04 ํ™˜๊ฒฝ์—์„œ `float16` ๋ฐ AutoModel ํ—ค๋“œ๊ฐ€ ์žˆ๋Š” `microsoft/biogpt` ๋ชจ๋ธ๋กœ ์ถ”๋ก  ์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†๋„ ํ–ฅ์ƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. + +| num_batches | batch_size | seq_len | is cuda | is half | use mask | Per token latency eager (ms) | Per token latency SDPA (ms) | Speedup (%) | Mem eager (MB) | Mem BT (MB) | Mem saved (%) | +|-------------|------------|---------|---------|---------|----------|------------------------------|-----------------------------|-------------|----------------|--------------|---------------| +| 50 | 1 | 64 | True | True | True | 0.115 | 0.098 | 17.392 | 716.998 | 716.998 | 0.000 | +| 50 | 1 | 128 | True | True | True | 0.115 | 0.093 | 24.640 | 730.916 | 730.916 | 0.000 | +| 50 | 2 | 64 | True | True | True | 0.114 | 0.096 | 19.204 | 730.900 | 730.900 | 0.000 | +| 50 | 2 | 128 | True | True | True | 0.117 | 0.095 | 23.529 | 759.262 | 759.262 | 0.000 | +| 50 | 4 | 64 | True | True | True | 0.113 | 0.096 | 18.325 | 759.229 | 759.229 | 0.000 | +| 50 | 4 | 128 | True | True | True | 0.186 | 0.178 | 4.289 | 816.478 | 816.478 | 0.000 | + + +## ๋ฆฌ์†Œ์Šค [[resources]] + +- [์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/language_modeling) + +## BioGptConfig [[transformers.BioGptConfig]] + +[[autodoc]] BioGptConfig + + +## BioGptTokenizer [[transformers.BioGptTokenizer]] + +[[autodoc]] BioGptTokenizer + - save_vocabulary + + +## BioGptModel [[transformers.BioGptModel]] + +[[autodoc]] BioGptModel + - forward + + +## BioGptForCausalLM [[transformers.BioGptForCausalLM]] + +[[autodoc]] BioGptForCausalLM + - forward + + +## BioGptForTokenClassification [[transformers.BioGptForTokenClassification]] + +[[autodoc]] BioGptForTokenClassification + - forward + + +## BioGptForSequenceClassification [[transformers.BioGptForSequenceClassification]] + +[[autodoc]] BioGptForSequenceClassification + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip-2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip-2.md new file mode 100644 index 0000000000000000000000000000000000000000..c9d7b99f81d3e664375a7d0966c299b411058b3f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip-2.md @@ -0,0 +1,98 @@ + + +# BLIP-2[[blip-2]] + +## ๊ฐœ์š”[[overview]] +BLIP-2 ๋ชจ๋ธ์€ Junnan Li, Dongxu Li, Silvio Savarese, Steven Hoi์˜ [BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models](https://huggingface.co/papers/2301.12597) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. BLIP-2๋Š” ๋™๊ฒฐ๋œ ์‚ฌ์ „ ํ•™์Šต ์ด๋ฏธ์ง€ ์ธ์ฝ”๋”์™€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLM)์„ ์—ฐ๊ฒฐํ•˜๋Š” 12์ธต์˜ ๊ฒฝ๋Ÿ‰ Transformer ์ธ์ฝ”๋”๋ฅผ ํ•™์Šต์‹œ์ผœ, ์—ฌ๋Ÿฌ ๋น„์ „-์–ธ์–ด ์ž‘์—…์—์„œ SOTA(ํ˜„์žฌ ์ตœ๊ณ ์˜ ์„ฑ๋Šฅ)์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, BLIP-2๋Š” 800์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ Flamingo ๋ชจ๋ธ๋ณด๋‹ค ์ œ๋กœ์ƒท VQAv2์—์„œ 8.7% ๋” ๋†’์€ ์„ฑ๋Šฅ์„ ๊ธฐ๋กํ–ˆ์œผ๋ฉฐ, ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋Š” Flamingo๋ณด๋‹ค 54๋ฐฐ ์ ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋น„์ „-์–ธ์–ด ์‚ฌ์ „ ํ•™์Šต์˜ ๋น„์šฉ์€ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์˜ ์—”๋“œ-ํˆฌ-์—”๋“œ ํ•™์Šต์œผ๋กœ ์ธํ•ด ์ ์  ๋” ๋ถ€๋‹ด์Šค๋Ÿฌ์›Œ์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์ด๋ฏธ์ง€ ์ธ์ฝ”๋”์™€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜์—ฌ ๋น„์ „-์–ธ์–ด ์‚ฌ์ „ ํ•™์Šต์„ ๋ถ€ํŠธ์ŠคํŠธ๋ž˜ํ•‘ํ•˜๋Š” ์ผ๋ฐ˜์ ์ด๊ณ  ํšจ์œจ์ ์ธ ์‚ฌ์ „ ํ•™์Šต ์ „๋žต์ธ BLIP-2๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. BLIP-2๋Š” ๊ฒฝ๋Ÿ‰ํ™”๋œ Querying Transformer๋ฅผ ํ†ตํ•ด ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉฐ, ๋‘ ๋‹จ๊ณ„๋กœ ์‚ฌ์ „ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๋™๊ฒฐ๋œ ์ด๋ฏธ์ง€ ์ธ์ฝ”๋”๋กœ๋ถ€ํ„ฐ ๋น„์ „-์–ธ์–ด ํ‘œํ˜„ ํ•™์Šต์„ ๋ถ€ํŠธ์ŠคํŠธ๋ž˜ํ•‘ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๋™๊ฒฐ๋œ ์–ธ์–ด ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ๋น„์ „-์–ธ์–ด ์ƒ์„ฑ ํ•™์Šต์„ ๋ถ€ํŠธ์ŠคํŠธ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค. BLIP-2๋Š” ๊ธฐ์กด ๋ฐฉ๋ฒ•๋“ค์— ๋น„ํ•ด ํ›จ์”ฌ ์ ์€ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋‹ค์–‘ํ•œ ๋น„์ „-์–ธ์–ด ์ž‘์—…์—์„œ ์ตœ์ฒจ๋‹จ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ ๋ชจ๋ธ์€ ์ œ๋กœ์ƒท VQAv2์—์„œ Flamingo80B๋ณด๋‹ค 8.7% ๋†’์€ ์„ฑ๋Šฅ์„ ๊ธฐ๋กํ•˜๋ฉฐ, ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋Š” 54๋ฐฐ ์ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋˜ํ•œ ์ž์—ฐ์–ด ๋ช…๋ น์„ ๋”ฐ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ œ๋กœ์ƒท ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์ƒ์„ฑ์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ž…์ฆํ–ˆ์Šต๋‹ˆ๋‹ค.* + + + + BLIP-2 ๊ตฌ์กฐ. ์›๋ณธ ๋…ผ๋ฌธ ์—์„œ ๋ฐœ์ทŒ. + +์ด ๋ชจ๋ธ์€ [nielsr](https://huggingface.co/nielsr)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/salesforce/LAVIS/tree/5ee63d688ba4cebff63acee04adaef2dee9af207)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- BLIP-2๋Š” ์ด๋ฏธ์ง€์™€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์กฐ๊ฑด๋ถ€ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ถ”๋ก  ์‹œ [`generate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. +- [`Blip2Processor`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์— ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•˜๊ณ , ์˜ˆ์ธก๋œ ํ† ํฐ ID๋ฅผ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ[[resources]] + +BLIP-2๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ณต์‹ Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ ํ‘œ์‹œ) ์ž๋ฃŒ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. + +- ์ด๋ฏธ์ง€ ์บก์…”๋‹, ์‹œ๊ฐ ์งˆ๋ฌธ ์‘๋‹ต(VQA), ์ฑ„ํŒ…๊ณผ ๊ฐ™์€ ๋Œ€ํ™”ํ˜• ์ž‘์—…์„ ์œ„ํ•œ BLIP-2 ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์—ฌ๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/BLIP-2)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฆฌ์†Œ์Šค๋ฅผ ์ œ์ถœํ•˜์—ฌ ์—ฌ๊ธฐ์— ํฌํ•จํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”! ๋ฆฌ์†Œ์Šค๋Š” ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณต์ œํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## Blip2Config[[transformers.Blip2Config]] + +[[autodoc]] Blip2Config + - from_vision_qformer_text_configs + +## Blip2VisionConfig[[transformers.Blip2VisionConfig]] + +[[autodoc]] Blip2VisionConfig + +## Blip2QFormerConfig[[transformers.Blip2QFormerConfig]] + +[[autodoc]] Blip2QFormerConfig + +## Blip2Processor[[transformers.Blip2Processor]] + +[[autodoc]] Blip2Processor + +## Blip2VisionModel[[transformers.Blip2VisionModel]] + +[[autodoc]] Blip2VisionModel + - forward + +## Blip2QFormerModel[[transformers.Blip2QFormerModel]] + +[[autodoc]] Blip2QFormerModel + - forward + +## Blip2Model[[transformers.Blip2Model]] + +[[autodoc]] Blip2Model + - forward + - get_text_features + - get_image_features + - get_qformer_features + +## Blip2ForConditionalGeneration[[transformers.Blip2ForConditionalGeneration]] + +[[autodoc]] Blip2ForConditionalGeneration + - forward + - generate + +## Blip2ForImageTextRetrieval[[transformers.Blip2ForImageTextRetrieval]] + +[[autodoc]] Blip2ForImageTextRetrieval + - forward + +## Blip2TextModelWithProjection[[transformers.Blip2TextModelWithProjection]] + +[[autodoc]] Blip2TextModelWithProjection + +## Blip2VisionModelWithProjection[[transformers.Blip2VisionModelWithProjection]] + +[[autodoc]] Blip2VisionModelWithProjection diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip.md new file mode 100644 index 0000000000000000000000000000000000000000..6b5a4e8abc2de0c02e756411f59b23f346f9bc89 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/blip.md @@ -0,0 +1,136 @@ + + +# BLIP[[blip]] + +## ๊ฐœ์š”[[overview]] + +BLIP ๋ชจ๋ธ์€ Junnan Li, Dongxu Li, Caiming Xiong, Steven Hoi์˜ [BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation](https://huggingface.co/papers/2201.12086) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +BLIP์€ ์—ฌ๋Ÿฌ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค: + +- ์‹œ๊ฐ ์งˆ๋ฌธ ์‘๋‹ต (Visual Question Answering, VQA) +- ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๊ฒ€์ƒ‰ (์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๋งค์นญ) +- ์ด๋ฏธ์ง€ ์บก์…”๋‹ + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋น„์ „-์–ธ์–ด ์‚ฌ์ „ ํ•™์Šต(Vision-Language Pre-training, VLP)์€ ๋‹ค์–‘ํ•œ ๋น„์ „-์–ธ์–ด ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์กด ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ๋“ค์€ ์ดํ•ด ๊ธฐ๋ฐ˜ ์ž‘์—…์ด๋‚˜ ์ƒ์„ฑ ๊ธฐ๋ฐ˜ ์ž‘์—… ์ค‘ ํ•˜๋‚˜์—์„œ๋งŒ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์€ ์ฃผ๋กœ ์›น์—์„œ ์ˆ˜์ง‘ํ•œ ๋…ธ์ด์ฆˆ๊ฐ€ ๋งŽ์€ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ์…‹์˜ ๊ทœ๋ชจ๋ฅผ ํ‚ค์šฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ด๋ฃจ์–ด์กŒ๋Š”๋ฐ, ์ด๋Š” ์ตœ์ ์˜ ์ง€๋„ ํ•™์Šต ๋ฐฉ์‹์ด๋ผ๊ณ  ๋ณด๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” BLIP์ด๋ผ๋Š” ์ƒˆ๋กœ์šด VLP ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋น„์ „-์–ธ์–ด ์ดํ•ด ๋ฐ ์ƒ์„ฑ ์ž‘์—… ๋ชจ๋‘์— ์œ ์—ฐํ•˜๊ฒŒ ์ ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BLIP๋Š” ์บก์…”๋„ˆ๊ฐ€ ํ•ฉ์„ฑ ์บก์…˜์„ ์ƒ์„ฑํ•˜๊ณ  ํ•„ํ„ฐ๊ฐ€ ๋…ธ์ด์ฆˆ ์บก์…˜์„ ์ œ๊ฑฐํ•˜๋Š” ๋ถ€ํŠธ์ŠคํŠธ๋ž˜ํ•‘ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์›น ๋ฐ์ดํ„ฐ์˜ ๋…ธ์ด์ฆˆ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๊ฒ€์ƒ‰(Recall@1์—์„œ +2.7%), ์ด๋ฏธ์ง€ ์บก์…”๋‹(CIDEr์—์„œ +2.8%), ๊ทธ๋ฆฌ๊ณ  VQA(VQA ์ ์ˆ˜์—์„œ +1.6%)์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๋น„์ „-์–ธ์–ด ์ž‘์—…์—์„œ ์ตœ์‹  ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ BLIP์€ ์ œ๋กœ์ƒท ๋ฐฉ์‹์œผ๋กœ ๋น„๋””์˜ค-์–ธ์–ด ์ž‘์—…์— ์ง์ ‘ ์ „์ด๋  ๋•Œ๋„ ๊ฐ•๋ ฅํ•œ ์ผ๋ฐ˜ํ™” ๋Šฅ๋ ฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์˜ ์ฝ”๋“œ, ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ์…‹์€ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.* + +![BLIP.gif](https://cdn-uploads.huggingface.co/production/uploads/1670928184033-62441d1d9fdefb55a0b7d12c.gif) + +์ด ๋ชจ๋ธ์€ [ybelkada](https://huggingface.co/ybelkada)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/salesforce/BLIP)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ[[resources]] + +- [Jupyter notebook](https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb): ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ์…‹์—์„œ BLIP๋ฅผ ์ด๋ฏธ์ง€ ์บก์…”๋‹์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• + +## BlipConfig[[transformers.BlipConfig]] + +[[autodoc]] BlipConfig + - from_text_vision_configs + +## BlipTextConfig[[transformers.BlipTextConfig]] + +[[autodoc]] BlipTextConfig + +## BlipVisionConfig[[transformers.BlipVisionConfig]] + +[[autodoc]] BlipVisionConfig + +## BlipProcessor[[transformers.BlipProcessor]] + +[[autodoc]] BlipProcessor + +## BlipImageProcessor[[transformers.BlipImageProcessor]] + +[[autodoc]] BlipImageProcessor + - preprocess + + + + +## BlipModel[[transformers.BlipModel]] + +`BlipModel`์€ ํ–ฅํ›„ ๋ฒ„์ „์—์„œ ๋” ์ด์ƒ ์ง€์›๋˜์ง€ ์•Š์„ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋ชฉ์ ์— ๋”ฐ๋ผ `BlipForConditionalGeneration`, `BlipForImageTextRetrieval` ๋˜๋Š” `BlipForQuestionAnswering`์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. + +[[autodoc]] BlipModel + - forward + - get_text_features + - get_image_features + +## BlipTextModel[[transformers.BlipTextModel]] + +[[autodoc]] BlipTextModel + - forward + +## BlipVisionModel[[transformers.BlipVisionModel]] + +[[autodoc]] BlipVisionModel + - forward + +## BlipForConditionalGeneration[[transformers.BlipForConditionalGeneration]] + +[[autodoc]] BlipForConditionalGeneration + - forward + +## BlipForImageTextRetrieval[[transformers.BlipForImageTextRetrieval]] + +[[autodoc]] BlipForImageTextRetrieval + - forward + +## BlipForQuestionAnswering[[transformers.BlipForQuestionAnswering]] + +[[autodoc]] BlipForQuestionAnswering + - forward + + + + +## TFBlipModel[[transformers.TFBlipModel]] + +[[autodoc]] TFBlipModel + - call + - get_text_features + - get_image_features + +## TFBlipTextModel[[transformers.TFBlipTextModel]] + +[[autodoc]] TFBlipTextModel + - call + +## TFBlipVisionModel[[transformers.TFBlipVisionModel]] + +[[autodoc]] TFBlipVisionModel + - call + +## TFBlipForConditionalGeneration[[transformers.TFBlipForConditionalGeneration]] + +[[autodoc]] TFBlipForConditionalGeneration + - call + +## TFBlipForImageTextRetrieval[[transformers.TFBlipForImageTextRetrieval]] + +[[autodoc]] TFBlipForImageTextRetrieval + - call + +## TFBlipForQuestionAnswering[[transformers.TFBlipForQuestionAnswering]] + +[[autodoc]] TFBlipForQuestionAnswering + - call + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/chameleon.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/chameleon.md new file mode 100644 index 0000000000000000000000000000000000000000..95c55677632bd7cece7d4ae8adc46a6526664d99 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/chameleon.md @@ -0,0 +1,185 @@ + + +# Chameleon [[chameleon]] + +## ๊ฐœ์š” [[overview]] + +Chameleon ๋ชจ๋ธ์€ META AI Chameleon ํŒ€์˜ ๋…ผ๋ฌธ [Chameleon: Mixed-Modal Early-Fusion Foundation Models](https://huggingface.co/papers/2405.09818)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Chameleon์€ ๋ฒกํ„ฐ ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ํ† ํฐํ™”ํ•จ์œผ๋กœ์จ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋น„์ „-์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ๊ต์ฐจ๋œ ํ˜•์‹์„ ํฌํ•จํ•œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ, ํ…์ŠคํŠธ ์‘๋‹ต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ชจ๋“ˆ์€ ์•„์ง ๊ณต๊ฐœ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ฅผ ์ž„์˜์˜ ์ˆœ์„œ๋กœ ์ดํ•ดํ•˜๊ณ  ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” early-fusion ํ† ํฐ ๊ธฐ๋ฐ˜์˜ ํ˜ผํ•ฉ ๋ชจ๋‹ฌ(mixed-modal) ๋ชจ๋ธ์˜ ์ผ์ข…์ธ Chameleon์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ดˆ๊ธฐ๋ถ€ํ„ฐ ์•ˆ์ •์ ์ธ ํ›ˆ๋ จ ์ ‘๊ทผ๋ฒ•, ์ •๋ ฌ ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  early-fusion, ํ† ํฐ ๊ธฐ๋ฐ˜, ํ˜ผํ•ฉ ๋ชจ๋‹ฌ ์„ค์ •์— ๋งž์ถ˜ ์•„ํ‚คํ…์ฒ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ๋“ค์€ ์‹œ๊ฐ์  ์งˆ๋ฌธ ์‘๋‹ต, ์ด๋ฏธ์ง€ ์บก์…˜ ์ƒ์„ฑ, ํ…์ŠคํŠธ ์ƒ์„ฑ, ์ด๋ฏธ์ง€ ์ƒ์„ฑ, ์žฅ๋ฌธ ํ˜ผํ•ฉ ๋ชจ๋‹ฌ ์ƒ์„ฑ ๋“ฑ ํฌ๊ด„์ ์ธ ์ž‘์—… ๋ฒ”์œ„์—์„œ ํ‰๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Chameleon์€ ๋‹จ์ผ ๋ชจ๋ธ์—์„œ ์ด๋ฏธ์ง€ ์บก์…˜ ์ƒ์„ฑ ์ž‘์—…์—์„œ์˜ ์ตœ์ฒจ๋‹จ ์„ฑ๋Šฅ์„ ํฌํ•จํ•œ ๊ด‘๋ฒ”์œ„ํ•˜๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๋Šฅ๋ ฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, ํ…์ŠคํŠธ ์ „์šฉ ์ž‘์—…์—์„œ Llama-2๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋ฉด์„œ Mixtral 8x7B์™€ Gemini-Pro์™€ ๊ฐ™์€ ๋ชจ๋ธ๋“ค ์‚ฌ์ด์—์„œ๋„ ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ–์ถ”๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒ๋‹นํ•œ ์„ฑ๋Šฅ์˜ ์ด๋ฏธ์ง€ ์ƒ์„ฑ๋„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ”„๋กฌํ”„ํŠธ๋‚˜ ์ถœ๋ ฅ์— ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ์˜ ํ˜ผํ•ฉ ์‹œํ€€์Šค๊ฐ€ ํฌํ•จ๋œ ์ƒˆ๋กœ์šด ์žฅ๋ฌธ ํ˜ผํ•ฉ ๋ชจ๋‹ฌ ์ƒ์„ฑ ํ‰๊ฐ€์—์„œ, ์ธ๊ฐ„์˜ ํŒ๋‹จ์— ๋”ฐ๋ฅด๋ฉด Gemini Pro์™€ GPT-4V๋ฅผ ํฌํ•จํ•œ ํ›จ์”ฌ ๋” ํฐ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ๊ณผ ๋™๋“ฑํ•˜๊ฑฐ๋‚˜ ์ด๋ฅผ ๋Šฅ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. Chameleon์€ ์™„์ „ํ•œ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ฌธ์„œ์˜ ํ†ตํ•ฉ ๋ชจ๋ธ๋ง์—์„œ ์ค‘์š”ํ•œ ๋ฐœ์ „์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.* + + + +Chameleon์€ ์ด๋ฏธ์ง€๋ฅผ ์ด์‚ฐ์ ์ธ ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒกํ„ฐ ์–‘์žํ™” ๋ชจ๋“ˆ์„ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ž๊ธฐํšŒ๊ท€ transformer๋ฅผ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [joaogante](https://huggingface.co/joaogante)์™€ [RaushanTurganbay](https://huggingface.co/RaushanTurganbay)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/facebookresearch/chameleon)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- ๋” ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•ด, ๋ฐฐ์น˜ ์ƒ์„ฑ ์‹œ `padding_side="left"`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑํ•˜๊ธฐ ์ „์— `processor.tokenizer.padding_side = "left"`๋กœ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค. + +- Chameleon์€ ์•ˆ์ „์„ฑ ์ •๋ ฌ์„ ์œ„ํ•ด ํŠœ๋‹๋˜์—ˆ์Œ์„ ์œ ์˜ํ•˜์‹ญ์‹œ์˜ค. ๋ชจ๋ธ์ด ์‘๋‹ต์„ ๊ฑฐ๋ถ€ํ•˜๋Š” ๊ฒฝ์šฐ, ์—ด๋ฆฐ ์งˆ๋ฌธ๋ณด๋‹ค๋Š” ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์งˆ๋ฌธ์„ ํ•ด๋ณด์„ธ์š”. + +- Chameleon์€ ์ฑ„ํŒ… ํ˜•์‹์œผ๋กœ ์ƒ์„ฑํ•˜๋ฏ€๋กœ, ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ๋Š” ํ•ญ์ƒ "assistant's turn"์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์„œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ `return_for_text_completion=True`๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ…์ŠคํŠธ ์™„์„ฑ ์ƒ์„ฑ์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!NOTE] +> Transformers์—์„œ์˜ Chameleon ๊ตฌํ˜„์€ ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ์„ ๋ณ‘ํ•ฉํ•  ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ์ด๋ฏธ์ง€ ํ† ํฐ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŠน๋ณ„ํ•œ ์ด๋ฏธ์ง€ ํ† ํฐ์„ ์œ„ํ•ด ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ์˜ˆ์•ฝ๋œ ํ† ํฐ ์ค‘ ํ•˜๋‚˜์ธ ``๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ์ƒ์„ฑ์„ ์œ„ํ•ด ํ”„๋กฌํ”„ํŠธ์—์„œ ์ด๋ฏธ์ง€๊ฐ€ ์ž„๋ฒ ๋”ฉ๋  ์œ„์น˜์— ``๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ์ œ [[usage-example]] + +### ๋‹จ์ผ ์ด๋ฏธ์ง€ ์ถ”๋ก  [[single-image-inference]] + +Chameleon์€ ๊ฒŒ์ดํ‹ฐ๋“œ(gated) ๋ชจ๋ธ์ด๋ฏ€๋กœ Hugging Face Hub์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์ด ์žˆ๊ณ  ํ† ํฐ์œผ๋กœ ๋กœ๊ทธ์ธํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ๋‹ค์Œ์€ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ๋ฐ˜์ •๋ฐ€๋„(`torch.bfloat16`)๋กœ ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```python +from transformers import ChameleonProcessor, ChameleonForConditionalGeneration +import torch +from PIL import Image +import requests + +processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") +model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") + +# ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ์ค€๋น„ +url = 'http://images.cocodataset.org/val2017/000000039769.jpg' +image = Image.open(requests.get(url, stream=True).raw) +prompt = "์ด ์ด๋ฏธ์ง€์—์„œ ๋ฌด์—‡์„ ๋ณด๋‚˜์š”?" + +inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device, dtype=torch.bfloat16) + +# ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž๊ธฐํšŒ๊ท€์ ์œผ๋กœ ์™„์„ฑ +output = model.generate(**inputs, max_new_tokens=50) +print(processor.decode(output[0], skip_special_tokens=True)) +``` + +### ๋‹ค์ค‘ ์ด๋ฏธ์ง€ ์ถ”๋ก  [[multi-image-inference]] + +Chameleon์€ ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„๋“ค์ด๋ฉฐ, ์ด๋ฏธ์ง€๋“ค์€ ๋™์ผํ•œ ํ”„๋กฌํ”„ํŠธ์— ์†ํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ์— ์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋ฐฐ์น˜ ์ถ”๋ก ์—์„œ). ๋‹ค์Œ์€ ๊ทธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```python +from transformers import ChameleonProcessor, ChameleonForConditionalGeneration +import torch +from PIL import Image +import requests + +processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") + +model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", torch_dtype=torch.bfloat16, device_map="cuda") + +# ์„ธ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ +url = "https://www.ilankelman.org/stopsigns/australia.jpg" +image_stop = Image.open(requests.get(url, stream=True).raw) + +url = "http://images.cocodataset.org/val2017/000000039769.jpg" +image_cats = Image.open(requests.get(url, stream=True).raw) + +url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" +image_snowman = Image.open(requests.get(url, stream=True).raw) + +# ๋ฐฐ์น˜๋œ ํ”„๋กฌํ”„ํŠธ ์ค€๋น„: ์ฒซ ๋ฒˆ์งธ๋Š” ๋‹ค์ค‘ ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ์ด๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” ๋‹จ์ผ ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ์ž…๋‹ˆ๋‹ค +prompts = [ + "์ด ์ด๋ฏธ์ง€๋“ค์€ ๋ฌด์—‡์ด ๊ณตํ†ต์ ์ธ๊ฐ€์š”?", + "์ด ์ด๋ฏธ์ง€์— ๋ฌด์—‡์ด ๋‚˜ํƒ€๋‚˜ ์žˆ๋‚˜์š”?" +] + +# ์ด๋ฏธ์ง€๋“ค์„ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ์—์„œ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค +# ๊ฐ "" ํ† ํฐ์€ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋‹ค์Œ "" ํ† ํฐ์€ ๋‹ค์Œ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค +inputs = processor(images=[image_stop, image_cats, image_snowman], text=prompts, padding=True, return_tensors="pt").to(device="cuda", dtype=torch.bfloat16) + +# ์ƒ์„ฑ +generate_ids = model.generate(**inputs, max_new_tokens=50) +processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) +``` + +## ๋ชจ๋ธ ์ตœ์ ํ™” [[model-optimization]] + +### Bitsandbytes๋ฅผ ์‚ฌ์šฉํ•œ ์–‘์žํ™” [[quantization-using-bitsandbytes]] + +๋ชจ๋ธ์€ 8๋น„ํŠธ ๋˜๋Š” 4๋น„ํŠธ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ์›๋ณธ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ ํฌ๊ฒŒ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ๋จผ์ € bitsandbytes๋ฅผ ์„ค์น˜ํ•˜๊ณ (`pip install bitsandbytes`), ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ง€์›ํ•˜๋Š” GPU/๊ฐ€์†๊ธฐ๋ฅผ ์‚ฌ์šฉ ์ค‘์ธ์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. + + + +bitsandbytes๋Š” CUDA ์ด์™ธ์˜ ์—ฌ๋Ÿฌ ๋ฐฑ์—”๋“œ๋ฅผ ์ง€์›ํ•˜๋„๋ก ๋ฆฌํŒฉํ„ฐ๋ง๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ROCm(AMD GPU) ๋ฐ Intel CPU ๊ตฌํ˜„์ด ์„ฑ์ˆ™ ๋‹จ๊ณ„์ด๋ฉฐ, Intel XPU๋Š” ์ง„ํ–‰ ์ค‘์ด๊ณ  Apple Silicon ์ง€์›์€ Q4/Q1์— ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์ง€์นจ ๋ฐ ์ตœ์‹  ๋ฐฑ์—”๋“œ ์—…๋ฐ์ดํŠธ๋Š” [์ด ๋งํฌ](https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend)๋ฅผ ๋ฐฉ๋ฌธํ•˜์„ธ์š”. + +์ „์ฒด ๊ณต๊ฐœ ์ „์— ๋ฒ„๊ทธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ํ”ผ๋“œ๋ฐฑ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค! ์ž์„ธํ•œ ๋‚ด์šฉ๊ณผ ํ”ผ๋“œ๋ฐฑ์€ [์ด ๋ฌธ์„œ](https://huggingface.co/docs/bitsandbytes/main/en/non_cuda_backends)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + + + +์œ„์˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```python +from transformers import ChameleonForConditionalGeneration, BitsAndBytesConfig + +# ๋ชจ๋ธ ์–‘์žํ™” ๋ฐฉ์‹ ์ง€์ • +quantization_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", + bnb_4bit_compute_dtype=torch.bfloat16, +) + +model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", quantization_config=quantization_config, device_map="cuda") +``` + +### Flash-Attention 2์™€ SDPA๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ ์†๋„ ํ–ฅ์ƒ [[use-flash-attention-2-and-sdpa-to-further-speed-up-generation]] + +์ด ๋ชจ๋ธ์€ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด Flash-Attention 2์™€ PyTorch์˜ [`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html)๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. SDPA๋Š” ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ๋•Œ ๊ธฐ๋ณธ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. Flash Attention 2๋กœ ์ „ํ™˜ํ•˜๋ ค๋ฉด ๋จผ์ € flash-attn์„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํŒจํ‚ค์ง€ ์„ค์น˜์— ๋Œ€ํ•ด์„œ๋Š” [์›๋ณธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/Dao-AILab/flash-attention)๋ฅผ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค. ์œ„์˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```python +from transformers import ChameleonForConditionalGeneration + +model_id = "facebook/chameleon-7b" +model = ChameleonForConditionalGeneration.from_pretrained( + model_id, + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2" +).to(0) +``` + +## ChameleonConfig [[transformers.ChameleonConfig]] + +[[autodoc]] ChameleonConfig + +## ChameleonVQVAEConfig [[transformers.ChameleonVQVAEConfig]] + +[[autodoc]] ChameleonVQVAEConfig + +## ChameleonProcessor [[transformers.ChameleonProcessor]] + +[[autodoc]] ChameleonProcessor + +## ChameleonImageProcessor [[transformers.ChameleonImageProcessor]] + +[[autodoc]] ChameleonImageProcessor + - preprocess + +## ChameleonVQVAE [[transformers.ChameleonVQVAE]] + +[[autodoc]] ChameleonVQVAE + - forward + +## ChameleonModel [[transformers.ChameleonModel]] + +[[autodoc]] ChameleonModel + - forward + +## ChameleonForConditionalGeneration [[transformers.ChameleonForConditionalGeneration]] + +[[autodoc]] ChameleonForConditionalGeneration + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/clip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/clip.md new file mode 100644 index 0000000000000000000000000000000000000000..f1053d17588e1b88bf64bc4b3d7702ea4fe42323 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/clip.md @@ -0,0 +1,318 @@ + + +# CLIP[[clip]] + +## ๊ฐœ์š”[[overview]] + +CLIP ๋ชจ๋ธ์€ Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, +Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever๊ฐ€ ์ œ์•ˆํ•œ [์ž์—ฐ์–ด ์ง€๋„(supervision)๋ฅผ ํ†ตํ•œ ์ „์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ ๋ชจ๋ธ ํ•™์Šต](https://huggingface.co/papers/2103.00020)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. CLIP(Contrastive Language-Image Pre-Training)์€ ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ์Œ์œผ๋กœ ํ›ˆ๋ จ๋œ ์‹ ๊ฒฝ๋ง ์ž…๋‹ˆ๋‹ค. GPT-2์™€ 3์˜ ์ œ๋กœ์ƒท ๋Šฅ๋ ฅ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, ํ•ด๋‹น ์ž‘์—…์— ์ง์ ‘์ ์œผ๋กœ ์ตœ์ ํ™”ํ•˜์ง€ ์•Š๊ณ ๋„ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ๊ฐ€์žฅ ๊ด€๋ จ์„ฑ ์žˆ๋Š” ํ…์ŠคํŠธ ์Šค๋‹ˆํŽซ์„ ์˜ˆ์ธกํ•˜๋„๋ก ์ž์—ฐ์–ด๋กœ ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค. + +*์ตœ์‹  ์ปดํ“จํ„ฐ ๋น„์ „ ์‹œ์Šคํ…œ์€ ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ๊ณ ์ •๋œ ๊ฐ์ฒด ์นดํ…Œ๊ณ ๋ฆฌ ์ง‘ํ•ฉ์„ ์˜ˆ์ธกํ•˜๋„๋ก ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ œํ•œ๋œ ํ˜•ํƒœ์˜ ์ง€๋„๋Š” ๋‹ค๋ฅธ ์‹œ๊ฐ์  ๊ฐœ๋…์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ ๋ผ๋ฒจ๋ง๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ๊ทธ ์ผ๋ฐ˜์„ฑ๊ณผ ์‚ฌ์šฉ์„ฑ์„ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์›์‹œ ํ…์ŠคํŠธ์—์„œ ์ง์ ‘ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์€ ํ›จ์”ฌ ๋” ๊ด‘๋ฒ”์œ„ํ•œ ์ง€๋„ ์†Œ์Šค๋ฅผ ํ™œ์šฉํ•˜๋Š” ์•„์ฃผ ์ข‹์€ ๋Œ€์•ˆ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์™€ ์บก์…˜์„ ๋งž์ถ”๋Š” ๊ฐ„๋‹จํ•œ ์‚ฌ์ „ ํ•™์Šต ์ž‘์—…์ด, ์ธํ„ฐ๋„ท์—์„œ ์ˆ˜์ง‘ํ•œ 4์–ต ์Œ์˜ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์…‹์—์„œ SOTA ์ˆ˜์ค€์˜ ์ด๋ฏธ์ง€ ํ‘œํ˜„์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํšจ์œจ์ ์ด๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ ํ›„, ์ž์—ฐ์–ด๋Š” ํ•™์Šต๋œ ์‹œ๊ฐ์  ๊ฐœ๋…์„ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜์–ด ๋ชจ๋ธ์˜ ํ•˜์œ„ ์ž‘์—…์œผ๋กœ์˜ ์ œ๋กœ์ƒท ์ „์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋…ผ๋ฌธ์—์„œ๋Š” OCR, ๋น„๋””์˜ค ๋‚ด ํ–‰๋™ ์ธ์‹, ์ง€๋ฆฌ์  ์œ„์น˜ ํŒŒ์•…, ๊ทธ๋ฆฌ๊ณ  ๋งŽ์€ ์ข…๋ฅ˜์˜ ์„ธ๋ฐ€ํ•œ ๊ฐ์ฒด ๋ถ„๋ฅ˜ ๋“ฑ 30๊ฐœ ์ด์ƒ์˜ ๋‹ค์–‘ํ•œ ๊ธฐ์กด ์ปดํ“จํ„ฐ ๋น„์ „ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ๋ฒค์น˜๋งˆํ‚น์„ ํ†ตํ•ด ์ด ์ ‘๊ทผ ๋ฐฉ์‹์˜ ์„ฑ๋Šฅ์„ ์—ฐ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์— ๋Œ€ํ•ด ์˜๋ฏธ ์žˆ๊ฒŒ ์ „์ด๋˜๋ฉฐ, ์ข…์ข… ๋ฐ์ดํ„ฐ์…‹๋ณ„ ํ›ˆ๋ จ ์—†์ด๋„ ์™„์ „ ์ง€๋„ ํ•™์Šต ๊ธฐ์ค€์„ ๊ณผ ๊ฒฝ์Ÿ๋ ฅ ์žˆ๋Š” ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ImageNet์—์„œ ์›๋ž˜ ResNet-50์˜ ์ •ํ™•๋„๋ฅผ ์ œ๋กœ์ƒท์œผ๋กœ ์ผ์น˜์‹œํ‚ค๋Š”๋ฐ, ์ด๋Š” ResNet-50์ด ํ›ˆ๋ จ๋œ 128๋งŒ ๊ฐœ์˜ ํ›ˆ๋ จ ์˜ˆ์ œ๋ฅผ ์ „ํ˜€ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ฐ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋Š” ์ด https URL์—์„œ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [valhalla](https://huggingface.co/valhalla)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/openai/CLIP)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ๊ณผ ์˜ˆ์‹œ[[usage-tips-and-example]] + +CLIP์€ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋น„์ „ ๋ฐ’ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ๊ณผ ์ œ๋กœ์ƒท ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CLIP์€ ViT์™€ ์œ ์‚ฌํ•œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐ์  ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ณ , ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ํŠน์ง•์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํ…์ŠคํŠธ์™€ ์‹œ๊ฐ์  ํŠน์ง• ๋ชจ๋‘ ๋™์ผํ•œ ์ฐจ์›์˜ ์ž ์žฌ(latent) ๊ณต๊ฐ„์œผ๋กœ ํˆฌ์˜๋ฉ๋‹ˆ๋‹ค. ํˆฌ์˜๋œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ํŠน์ง• ์‚ฌ์ด์˜ ๋‚ด์ ์ด ์œ ์‚ฌ๋„ ์ ์ˆ˜๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”์— ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด, ๊ฐ ์ด๋ฏธ์ง€๋Š” ๊ณ ์ • ํฌ๊ธฐ์˜ ๊ฒน์น˜์ง€ ์•Š๋Š” ํŒจ์น˜๋“ค์˜ ์‹œํ€€์Šค๋กœ ๋ถ„ํ• ๋˜๊ณ , ์ดํ›„ ์„ ํ˜• ์ž„๋ฒ ๋”ฉ๋ฉ๋‹ˆ๋‹ค. [CLS]ํ† ํฐ์ด ์ „์ฒด ์ด๋ฏธ์ง€์˜ ํ‘œํ˜„์œผ๋กœ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ๋˜ํ•œ ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ๋ฒกํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ‘œ์ค€ ํŠธ๋žœ์Šคํฌ๋จธ ์ธํ† ๋”์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. [`CLIPImageProcessor`]๋Š” ๋ชจ๋ธ์„ ์œ„ํ•ด ์ด๋ฏธ์ง€๋ฅผ ๋ฆฌ์‚ฌ์ด์ฆˆ(๋˜๋Š” ์žฌ์Šค์บ์ผ๋ง)ํ•˜๊ณ  ์ •๊ทœํ™”ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[`CLIPTokenizer`]๋Š” ํ…์ŠคํŠธ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. [`CLIPProcessor`]๋Š” [`CLIPImageProcessor`]์™€ [`CLIPTokenizer`]๋ฅผ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋กœ ๊ฐ์‹ธ์„œ ํ…์ŠคํŠธ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•˜๋Š”๋ฐ ๋ชจ๋‘ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ ์˜ˆ์‹œ๋Š” [`CLIPProcessor`]์™€ [`CLIPModel`]์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์œ ์‚ฌ๋„ ์ ์ˆ˜๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + +```python +>>> from PIL import Image +>>> import requests + +>>> from transformers import CLIPProcessor, CLIPModel + +>>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") +>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True) + +>>> outputs = model(**inputs) +>>> logits_per_image = outputs.logits_per_image # ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์œ ์‚ฌ์„ฑ ์ ์ˆ˜ +>>> probs = logits_per_image.softmax(dim=1) # ํ™•๋ฅ ์„ ๋ ˆ์ด๋ธ”๋ง ํ•˜๊ธฐ์œ„ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์ทจํ•ฉ๋‹ˆ๋‹ค. +``` + + +### CLIP๊ณผ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2 ๊ฒฐํ•ฉ[[combining-clip-and-flash-attention-2]] + +๋จผ์ € ์ตœ์‹ ๋ฒ„์ „์˜ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. + +```bash +pip install -U flash-attn --no-build-isolation +``` + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2์™€ ํ˜ธํ™˜๋˜๋Š” ํ•˜๋“œ์›จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ flash-attn ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ณต์‹๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(`torch.float16`)๋กœ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์„ธ์š”. + + + +์ž‘์€ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์ด ๋А๋ ค์ง€๋Š” ๊ฒƒ์„ ๋А๋‚„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์•„๋ž˜์˜ [ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜๊ณผ SDPA๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ƒ ์†๋„ ํ–ฅ์ƒ](#Expected-speedups-with-Flash-Attention-and-SDPA) ์„น์…˜์„ ์ฐธ์กฐํ•˜์—ฌ ์ ์ ˆํ•œ ์–ดํ…์…˜ ๊ตฌํ˜„์„ ์„ ํƒํ•˜์„ธ์š”. + + + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ๊ตฌ๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ์Šค๋‹ˆํŽซ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”: + +```python +>>> import torch +>>> import requests +>>> from PIL import Image + +>>> from transformers import CLIPProcessor, CLIPModel + +>>> device = "cuda" +>>> torch_dtype = torch.float16 + +>>> model = CLIPModel.from_pretrained( +... "openai/clip-vit-base-patch32", +... attn_implementation="flash_attention_2", +... device_map=device, +... torch_dtype=torch_dtype, +... ) +>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True) +>>> inputs.to(device) + +>>> with torch.no_grad(): +... with torch.autocast(device): +... outputs = model(**inputs) + +>>> logits_per_image = outputs.logits_per_image # ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์œ ์‚ฌ์„ฑ ์ ์ˆ˜ +>>> probs = logits_per_image.softmax(dim=1) # ํ™•๋ฅ ์„ ๋ ˆ์ด๋ธ”๋ง ํ•˜๊ธฐ์œ„ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์ทจํ•ฉ๋‹ˆ๋‹ค. +>>> print(probs) +tensor([[0.9946, 0.0052]], device='cuda:0', dtype=torch.float16) +``` + + +### ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜ (Scaled dot-product Attention(SDPA)) ์‚ฌ์šฉํ•˜๊ธฐ[[using-scaled-dot-product-attention-sdpa]] + +ํŒŒ์ดํ† ์น˜๋Š” `torch.nn.functional`์˜ ์ผ๋ถ€๋กœ ๋„ค์ดํ‹ฐ๋ธŒ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜(SPDA) ์—ฐ์‚ฐ์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ณผ ์‚ฌ์šฉ ์ค‘์ธ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ตฌํ˜„์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” [๊ณต์‹๋ฌธ์„œ](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)๋‚˜ [GPU ์ถ”๋ก ](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +`torch>=2.1.1`์—์„œ๋Š” ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•  ๋•Œ SDPA๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€๋งŒ, `from_pretrained()` ํ•จ์ˆ˜์—์„œ `attn_implementation="sdpa"`๋ฅผ ์„ค์ •ํ•˜์—ฌ SDPA๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ์š”์ฒญํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import CLIPModel + +model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32", torch_dtype=torch.float16, attn_implementation="sdpa") +``` + +์ตœ๊ณ ์˜ ์†๋„ํ–ฅ์ƒ์„ ์œ„ํ•ด์„œ, ๋ฐ˜์ •๋ฐ€๋„๋กœ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ๋“ค๋ฉด `torch.float16` ๋˜๋Š” `torch.bfloat16`). + +### ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜๊ณผ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜(SDPA)์œผ๋กœ ์ธํ•ด ์˜ˆ์ƒ๋˜๋Š” ์†๋„ํ–ฅ์ƒ[[expected-speedups-with-flash-attention-and-sdpa]] + +๋กœ์ปฌ ๋ฒค์น˜๋งˆํฌ(NVIDIA A10G, PyTorch 2.3.1+cu121)์—์„œ `float16`์„ ์‚ฌ์šฉํ•˜์—ฌ `"openai/clip-vit-large-patch14"` ์ฒดํฌํฌ์ธํŠธ๋กœ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†๋„ ํ–ฅ์ƒ์„ ํ™•์ธ ํ–ˆ์Šต๋‹ˆ๋‹ค. +[์ฝ”๋“œ](https://gist.github.com/qubvel/ac691a54e54f9fae8144275f866a7ff8): + +#### CLIPTextModel[[cliptextmodel]] + +| Num text labels | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup | +|------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:| +| 4 | 0.009 | 0.012 | 0.737 | 0.007 | 1.269 | +| 16 | 0.009 | 0.014 | 0.659 | 0.008 | 1.187 | +| 32 | 0.018 | 0.021 | 0.862 | 0.016 | 1.142 | +| 64 | 0.034 | 0.034 | 1.001 | 0.03 | 1.163 | +| 128 | 0.063 | 0.058 | 1.09 | 0.054 | 1.174 | + +![clip_text_model_viz_3](https://github.com/user-attachments/assets/e9826b43-4e66-4f4c-952b-af4d90bd38eb) + +#### CLIPVisionModel[[clipvisionmodel]] + +| Image batch size | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup | +|-------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:| +| 1 | 0.016 | 0.013 | 1.247 | 0.012 | 1.318 | +| 4 | 0.025 | 0.021 | 1.198 | 0.021 | 1.202 | +| 16 | 0.093 | 0.075 | 1.234 | 0.075 | 1.24 | +| 32 | 0.181 | 0.147 | 1.237 | 0.146 | 1.241 | + +![clip_image_model_viz_3](https://github.com/user-attachments/assets/50a36206-e3b9-4adc-ac8e-926b8b071d63) + +#### CLIPModel[[clipmodel]] + +| Image batch size | Num text labels | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup | +|-------------------:|------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:| +| 1 | 4 | 0.025 | 0.026 | 0.954 | 0.02 | 1.217 | +| 1 | 16 | 0.026 | 0.028 | 0.918 | 0.02 | 1.287 | +| 1 | 64 | 0.042 | 0.046 | 0.906 | 0.036 | 1.167 | +| 4 | 4 | 0.028 | 0.033 | 0.849 | 0.024 | 1.189 | +| 4 | 16 | 0.034 | 0.035 | 0.955 | 0.029 | 1.169 | +| 4 | 64 | 0.059 | 0.055 | 1.072 | 0.05 | 1.179 | +| 16 | 4 | 0.096 | 0.088 | 1.091 | 0.078 | 1.234 | +| 16 | 16 | 0.102 | 0.09 | 1.129 | 0.083 | 1.224 | +| 16 | 64 | 0.127 | 0.11 | 1.157 | 0.105 | 1.218 | +| 32 | 4 | 0.185 | 0.159 | 1.157 | 0.149 | 1.238 | +| 32 | 16 | 0.19 | 0.162 | 1.177 | 0.154 | 1.233 | +| 32 | 64 | 0.216 | 0.181 | 1.19 | 0.176 | 1.228 | + +## ์ž๋ฃŒ[[resources]] + +CLIP์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. + +- [์›๊ฒฉ ์„ผ์‹ฑ (์ธ๊ณต์œ„์„ฑ) ์ด๋ฏธ์ง€์™€ ์บก์…˜์„ ๊ฐ€์ง€๊ณ  CLIP ๋ฏธ์„ธ์กฐ์ •ํ•˜๊ธฐ](https://huggingface.co/blog/fine-tune-clip-rsicd): +[RSICD dataset](https://github.com/201528014227051/RSICD_optimal)์„ ๊ฐ€์ง€๊ณ  CLIP์„ ๋ฏธ์„ธ์กฐ์ • ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•์— ๋Œ€ํ•œ ์„ฑ๋Šฅ ๋น„๊ต์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ +- ์ด [์˜ˆ์‹œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/contrastive-image-text)๋Š” [COCO dataset](https://cocodataset.org/#home)๋ฅผ ์ด์šฉํ•œ ์‚ฌ์ „ํ•™์Šต๋œ ๋น„์ „๊ณผ ํ…์ŠคํŠธ์™€ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•ด์„œ CLIP๊ฐ™์€ ๋น„์ „-ํ…์ŠคํŠธ ๋“€์–ผ ๋ชจ๋ธ์„ ์–ด๋–ป๊ฒŒ ํ•™์Šต์‹œํ‚ค๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + + +- ์‚ฌ์ „ํ•™์Šต๋œ CLIP๋ชจ๋ธ์„ ์ด๋ฏธ์ง€ ์บก์…”๋‹์„ ์œ„ํ•œ ๋น”์„œ์น˜ ์ถ”๋ก ์— ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•˜๋Š”์ง€์— ๊ด€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1tuoAC5F4sC7qid56Z0ap-stR3rwdk0ZV?usp=sharing) + +**์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰** + +- ์‚ฌ์ „ํ•™์Šต๋œ CLIP๋ชจ๋ธ๊ณผ MRR(Mean Reciprocal Rank) ์ ์ˆ˜ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1bLVwVKpAndpEDHqjzxVPr_9nGrSbuOQd?usp=sharing). ๐ŸŒŽ +- ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰๊ณผ ์œ ์‚ฌ์„ฑ ์ ์ˆ˜์— ๋Œ€ํ•ด ๋ณด์—ฌ์ฃผ๋Š” [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/deep-diver/image_search_with_natural_language/blob/main/notebooks/Image_Search_CLIP.ipynb). ๐ŸŒŽ +- Multilingual CLIP๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐ™์€ ๋ฒกํ„ฐ ๊ณต๊ฐ„์— ๋งคํ•‘ ์‹œํ‚ค๋Š”์ง€์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1xO-wC_m_GNzgjIBQ4a4znvQkvDoZJvH4?usp=sharing). ๐ŸŒŽ +- [Unsplash](https://unsplash.com)์™€ [TMDB](https://www.themoviedb.org/) ๋ฐ์ดํ„ฐ์…‹์„ ํ™œ์šฉํ•œ ์˜๋ฏธ๋ก ์ (semantic) ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์—์„œ CLIP์„ ๊ตฌ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/vivien000/clip-demo/blob/master/clip.ipynb#scrollTo=uzdFhRGqiWkR). ๐ŸŒŽ + +**์„ค๋ช… ๊ฐ€๋Šฅ์„ฑ** + +- ์ž…๋ ฅ ํ† ํฐ๊ณผ ์ด๋ฏธ์ง€ ์กฐ๊ฐ(segment) ์‚ฌ์ด์˜ ์œ ์‚ฌ์„ฑ์„ ์‹œ๊ฐํ™” ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/hila-chefer/Transformer-MM-Explainability/blob/main/CLIP_explainability.ipynb). ๐ŸŒŽ + +์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## CLIPConfig[[transformers.CLIPConfig]] + +[[autodoc]] CLIPConfig + - from_text_vision_configs + +## CLIPTextConfig[[transformers.CLIPTextConfig]] + +[[autodoc]] CLIPTextConfig + +## CLIPVisionConfig[[transformers.CLIPVisionConfig]] + +[[autodoc]] CLIPVisionConfig + +## CLIPTokenizer[[transformers.CLIPTokenizer]] + +[[autodoc]] CLIPTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## CLIPTokenizerFast[[transformers.CLIPTokenizerFast]] + +[[autodoc]] CLIPTokenizerFast + +## CLIPImageProcessor[[transformers.CLIPImageProcessor]] + +[[autodoc]] CLIPImageProcessor + - preprocess + +## CLIPFeatureExtractor[[transformers.CLIPFeatureExtractor]] + +[[autodoc]] CLIPFeatureExtractor + +## CLIPProcessor[[transformers.CLIPProcessor]] + +[[autodoc]] CLIPProcessor + + + + +## CLIPModel[[transformers.CLIPModel]] + +[[autodoc]] CLIPModel + - forward + - get_text_features + - get_image_features + +## CLIPTextModel[[transformers.CLIPTextModel]] + +[[autodoc]] CLIPTextModel + - forward + +## CLIPTextModelWithProjection[[transformers.CLIPTextModelWithProjection]] + +[[autodoc]] CLIPTextModelWithProjection + - forward + +## CLIPVisionModelWithProjection[[transformers.CLIPVisionModelWithProjection]] + +[[autodoc]] CLIPVisionModelWithProjection + - forward + +## CLIPVisionModel[[transformers.CLIPVisionModel]] + +[[autodoc]] CLIPVisionModel + - forward + +## CLIPForImageClassification[[transformers.CLIPForImageClassification]] + +[[autodoc]] CLIPForImageClassification + - forward + + + + +## TFCLIPModel[[transformers.TFCLIPModel]] + +[[autodoc]] TFCLIPModel + - call + - get_text_features + - get_image_features + +## TFCLIPTextModel[[transformers.TFCLIPTextModel]] + +[[autodoc]] TFCLIPTextModel + - call + +## TFCLIPVisionModel[[transformers.TFCLIPVisionModel]] + +[[autodoc]] TFCLIPVisionModel + - call + + + + +## FlaxCLIPModel[[transformers.FlaxCLIPModel]] + +[[autodoc]] FlaxCLIPModel + - __call__ + - get_text_features + - get_image_features + +## FlaxCLIPTextModel[[transformers.FlaxCLIPTextModel]] + +[[autodoc]] FlaxCLIPTextModel + - __call__ + +## FlaxCLIPTextModelWithProjection[[transformers.FlaxCLIPTextModelWithProjection]] + +[[autodoc]] FlaxCLIPTextModelWithProjection + - __call__ + +## FlaxCLIPVisionModel[[transformers.FlaxCLIPVisionModel]] + +[[autodoc]] FlaxCLIPVisionModel + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/codegen.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/codegen.md new file mode 100644 index 0000000000000000000000000000000000000000..75e69b0b84c74e283f64dd819d35eac8c51f16d2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/codegen.md @@ -0,0 +1,94 @@ + + +# CodeGen[[Codegen]] + +
+PyTorch +
+ +## ๊ฐœ์š”[[Overview]] + +CodeGen ๋ชจ๋ธ์€ Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong์ด ์ž‘์„ฑํ•œ ๋…ผ๋ฌธ [A Conversational Paradigm for Program Synthesis](https://huggingface.co/papers/2203.13474)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +CodeGen ๋ชจ๋ธ์€ ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ(program synthesis)์„ ์œ„ํ•œ ์ž๊ธฐํšŒ๊ท€(autoregressive) ์–ธ์–ด ๋ชจ๋ธ๋กœ, [The Pile](https://pile.eleuther.ai/), BigQuery, BigPython ๋ฐ์ดํ„ฐ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ(program synthesis)์€ ์ฃผ์–ด์ง„ ๋ฌธ์ œ ๋ช…์„ธ์— ๋Œ€ํ•œ ํ•ด๋‹ต์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์—์„œ๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLM)์„ ํ™œ์šฉํ•œ ๋Œ€ํ™”ํ˜• ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ(conversational program synthesis) ์ ‘๊ทผ๋ฒ•์„ ์ œ์•ˆํ•˜์—ฌ, ๊ธฐ์กด ์ ‘๊ทผ๋ฒ•์—์„œ์˜ ๋ฐฉ๋Œ€ํ•œ ํ”„๋กœ๊ทธ๋žจ ํƒ์ƒ‰ ๊ณต๊ฐ„๊ณผ ์‚ฌ์šฉ์ž์˜ ์˜๋„๋ฅผ ๋ช…์„ธํ™”ํ•˜๋Š” ๊ณผ์ •์—์„œ์˜ ์–ด๋ ค์›€์„ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ œ์•ˆ๋œ ๋ฐฉ์‹์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ ๋ช…์„ธ ์ž‘์„ฑ๊ณผ ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์„ ์‚ฌ์šฉ์ž์™€ ์‹œ์Šคํ…œ ๊ฐ„ ๋‹คํšŒ ๋Œ€ํ™”(multi-turn conversation)๋กœ ๋ฐ”๋ผ๋ด…๋‹ˆ๋‹ค. ์ฆ‰, ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ ๊ณผ์ • ๋ช…์„ธ๋ฅผ ์ž์—ฐ์–ด๋กœ ํ‘œํ˜„ํ•˜๊ณ , ๊ธฐ๋Œ€ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ์„ ์กฐ๊ฑด๋ถ€๋กœ ์˜ˆ์ธกํ•˜์—ฌ ์ƒ์„ฑํ•˜๋Š” ์ผ์ข…์˜ ์ˆœ์ฐจ์  ์˜ˆ์ธก ๋ฌธ์ œ(sequence prediction problem)๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ž์—ฐ์–ด์™€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ CodeGen์ด๋ผ๋Š” ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ ๊ทธ๋ฃน์„ ํ•™์Šต์‹œ์ผฐ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์•ฝํ•œ ์ง€๋„(weak supervision)์™€ ๋ฐ์ดํ„ฐ ๋ฐ ๋ชจ๋ธ ๊ทœ๋ชจ์˜ ํ™•์žฅ๋งŒ์œผ๋กœ๋„ ๋ชจ๋ธ์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋Œ€ํ™” ๋Šฅ๋ ฅ์„ ๊ฐ–์ถ”๊ฒŒ ๋œ๋‹ค๋Š” ์ ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋”ํ•ด์„œ ๋ชจ๋ธ์˜ ๋Œ€ํ™”ํ˜• ํ”„๋กœ๊ทธ๋žจ ํ•ฉ์„ฑ ๋Šฅ๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๋‹คํšŒ ๋Œ€ํ™” ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฒค์น˜๋งˆํฌ(MTPB)๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒค์น˜๋งˆํฌ๋Š” ๊ฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž์™€ ๋ชจ๋ธ ๊ฐ„ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ ๋Œ€ํ™”๋ฅผ ๊ฑฐ์ณ ํ”„๋กœ๊ทธ๋žจ์ด ์ ์ง„์ ์œผ๋กœ ํ•ฉ์„ฑ๋˜๋Š” ๊ณผ์ •์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์—ฐ๊ตฌ ๊ฒฐ๊ณผ, CodeGen ๋ชจ๋ธ์€ ๋Œ€ํ™”ํ˜• ๋Šฅ๋ ฅ์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐœํœ˜ํ–ˆ์œผ๋ฉฐ ๋ณธ ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆํ•œ ๋Œ€ํ™”ํ˜• ํ•ฉ์„ฑ ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ์šฐ์ˆ˜์„ฑ๊ณผ ํšจ์œจ์„ฑ์„ ์ž…์ฆํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ 16B ํŒŒ๋ผ๋ฏธํ„ฐ ๊ทœ๋ชจ๋กœ TPU-v4์—์„œ ํ•™์Šต๋œ CodeGen ๋ชจ๋ธ์€ HumanEval ๋ฒค์น˜๋งˆํฌ์—์„œ OpenAI์˜ Codex๋ฅผ ๋›ฐ์–ด๋„˜๋Š” ์„ฑ๋Šฅ์„ ๊ธฐ๋กํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต๋œ ์‚ฌ์šฉ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ JaxFormer์™€ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋Š” ์˜คํ”ˆ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: [์ด https URL์—์„œ ํ™•์ธํ•˜์„ธ์š”](https://github.com/salesforce/codegen).* + +์ด ๋ชจ๋ธ์€[Hiroaki Hayashi](https://huggingface.co/rooa)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. +๋ชจ๋ธ์˜ ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/salesforce/codegen)์— ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ฒดํฌํฌ์ธํŠธ ๋ช…๋ช… ๊ทœ์น™[[Checkpoint Naming]] + +* CodeGen ๋ชจ๋ธ์˜ [์ฒดํฌํฌ์ธํŠธ](https://huggingface.co/models?other=codegen)๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์‚ฌ์ „ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. +* ์ฒดํฌํฌ์ธํŠธ์˜ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: `Salesforce/codegen-{size}-{data}` + * `size`: `350M`, `2B`, `6B`, `16B` + * `data`: + * `nl`: The Pile ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ + * `multi`: `nl` ๋ชจ๋ธ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•™์Šตํ•œ ๋ชจ๋ธ + * `mono`: `multi` ๋ชจ๋ธ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ถ”๊ฐ€๋กœ Python ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ํ•™์Šต๋œ ๋ชจ๋ธ +* ์˜ˆ๋ฅผ ๋“ค์–ด, `Salesforce/codegen-350M-mono`๋Š” 3์–ต 5์ฒœ๋งŒ(350M) ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ชจ๋ธ๋กœ, The Pile, ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด, Python ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋กœ ๋‹จ๊ณ„์ ์œผ๋กœ ํ•™์Šตํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ์‹œ[[Usage example]] + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> checkpoint = "Salesforce/codegen-350M-mono" +>>> model = AutoModelForCausalLM.from_pretrained(checkpoint) +>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) + +>>> text = "def hello_world():" + +>>> completion = model.generate(**tokenizer(text, return_tensors="pt")) + +>>> print(tokenizer.decode(completion[0])) +def hello_world(): + print("Hello World") + +hello_world() +``` + +## ์ž๋ฃŒ[[Resources]] + +- [Causal language modeling task guide](../tasks/language_modeling) + +## CodeGenConfig + +[[autodoc]] CodeGenConfig + - all + +## CodeGenTokenizer + +[[autodoc]] CodeGenTokenizer + - create_token_type_ids_from_sequences + - save_vocabulary + +## CodeGenTokenizerFast + +[[autodoc]] CodeGenTokenizerFast + +## CodeGenModel + +[[autodoc]] CodeGenModel + - forward + +## CodeGenForCausalLM + +[[autodoc]] CodeGenForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/cohere.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/cohere.md new file mode 100644 index 0000000000000000000000000000000000000000..c577764623f49672a27b78f850828cc21fa7a37b --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/cohere.md @@ -0,0 +1,139 @@ +# Cohere[[cohere]] + +## ๊ฐœ์š”[[overview]] + +The Cohere Command-R ๋ชจ๋ธ์€ CohereํŒ€์ด [Command-R: ํ”„๋กœ๋•์…˜ ๊ทœ๋ชจ์˜ ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ](https://txt.cohere.com/command-r/)๋ผ๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ์—์„œ ์†Œ๊ฐœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ ์ดˆ๋ก: + +*Command-R์€ ๊ธฐ์—…์˜ ํ”„๋กœ๋•์…˜ ๊ทœ๋ชจ AI๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด RAG(๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ)์™€ ๋„๊ตฌ ์‚ฌ์šฉ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์ƒ์„ฑ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜ ์šฐ๋ฆฌ๋Š” ๋Œ€๊ทœ๋ชจ ํ”„๋กœ๋•์…˜ ์›Œํฌ๋กœ๋“œ๋ฅผ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด LLM์ธ Command-R์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. Command-R์€ ๋†’์€ ํšจ์œจ์„ฑ๊ณผ ๊ฐ•๋ ฅํ•œ ์ •ํ™•์„ฑ์˜ ๊ท ํ˜•์„ ๋งž์ถ”๋Š” 'ํ™•์žฅ ๊ฐ€๋Šฅํ•œ' ๋ชจ๋ธ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜์—ฌ, ๊ธฐ์—…๋“ค์ด ๊ฐœ๋… ์ฆ๋ช…์„ ๋„˜์–ด ํ”„๋กœ๋•์…˜ ๋‹จ๊ณ„๋กœ ๋‚˜์•„๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.* + +*Command-R์€ ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ(RAG)์ด๋‚˜ ์™ธ๋ถ€ API ๋ฐ ๋„๊ตฌ ์‚ฌ์šฉ๊ณผ ๊ฐ™์€ ๊ธด ๋ฌธ๋งฅ ์ž‘์—…์— ์ตœ์ ํ™”๋œ ์ƒ์„ฑ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ RAG ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ ์ตœ๊ณ  ์ˆ˜์ค€์˜ ํ†ตํ•ฉ์„ ์ œ๊ณตํ•˜๊ณ  ๊ธฐ์—… ์‚ฌ์šฉ ์‚ฌ๋ก€์—์„œ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ์˜ ์—…๊ณ„ ์„ ๋„์ ์ธ Embed ๋ฐ Rerank ๋ชจ๋ธ๊ณผ ์กฐํ™”๋กญ๊ฒŒ ์ž‘๋™ํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์—…์ด ๋Œ€๊ทœ๋ชจ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ง„ ๋ชจ๋ธ๋กœ์„œ, Command-R์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ์ž๋ž‘ํ•ฉ๋‹ˆ๋‹ค: +- RAG ๋ฐ ๋„๊ตฌ ์‚ฌ์šฉ์— ๋Œ€ํ•œ ๊ฐ•๋ ฅํ•œ ์ •ํ™•์„ฑ +- ๋‚ฎ์€ ์ง€์—ฐ ์‹œ๊ฐ„๊ณผ ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰ +- ๋” ๊ธด 128k ์ปจํ…์ŠคํŠธ์™€ ๋‚ฎ์€ ๊ฐ€๊ฒฉ +- 10๊ฐœ์˜ ์ฃผ์š” ์–ธ์–ด์— ๊ฑธ์นœ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ +- ์—ฐ๊ตฌ ๋ฐ ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด HuggingFace์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ + +๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋Š” [์ด๊ณณ](https://huggingface.co/CohereForAI/c4ai-command-r-v01)์—์„œ ํ™•์ธํ•˜์„ธ์š”. +์ด ๋ชจ๋ธ์€ [Saurabh Dash](https://huggingface.co/saurabhdash)๊ณผ [Ahmet รœstรผn](https://huggingface.co/ahmetustun)์— ์˜ํ•ด ๊ธฐ์—ฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Hugging Face์—์„œ ์ด ์ฝ”๋“œ์˜ ๊ตฌํ˜„์€ [GPT-NeoX](https://github.com/EleutherAI/gpt-neox)์— ๊ธฐ๋ฐ˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + + + +Hub์— ์—…๋กœ๋“œ๋œ ์ฒดํฌํฌ์ธํŠธ๋“ค์€ `torch_dtype = 'float16'`์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +์ด๋Š” `AutoModel` API๊ฐ€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ `torch.float32`์—์„œ `torch.float16`์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +์˜จ๋ผ์ธ ๊ฐ€์ค‘์น˜์˜ `dtype`์€ `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ `torch_dtype="auto"`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ ๋Œ€๋ถ€๋ถ„ ๋ฌด๊ด€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ชจ๋ธ์ด ๋จผ์ € ๋‹ค์šด๋กœ๋“œ๋˜๊ณ (์˜จ๋ผ์ธ ์ฒดํฌํฌ์ธํŠธ์˜ `dtype` ์‚ฌ์šฉ), ๊ทธ ๋‹ค์Œ `torch`์˜ ๊ธฐ๋ณธ `dtype`์œผ๋กœ ๋ณ€ํ™˜๋˜๋ฉฐ(์ด๋•Œ `torch.float32`๊ฐ€ ๋จ), ๋งˆ์ง€๋ง‰์œผ๋กœ config์— `torch_dtype`์ด ์ œ๊ณต๋œ ๊ฒฝ์šฐ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ `float16`์œผ๋กœ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ `nan`์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์€ `bfloat16`์œผ๋กœ ํ›ˆ๋ จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +# pip install transformers +from transformers import AutoTokenizer, AutoModelForCausalLM + +model_id = "CohereForAI/c4ai-command-r-v01" +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id) + +# Format message with the command-r chat template +messages = [{"role": "user", "content": "Hello, how are you?"}] +input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") +## <|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|> + +gen_tokens = model.generate( + input_ids, + max_new_tokens=100, + do_sample=True, + temperature=0.3, + ) + +gen_text = tokenizer.decode(gen_tokens[0]) +print(gen_text) +``` + +- Flash Attention 2๋ฅผ `attn_implementation="flash_attention_2"`๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ๋•Œ๋Š”, `from_pretrained` ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์— `torch_dtype`์„ ์ „๋‹ฌํ•˜์ง€ ๋ง๊ณ  ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ›ˆ๋ จ(Automatic Mixed-Precision training)์„ ์‚ฌ์šฉํ•˜์„ธ์š”. `Trainer`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹จ์ˆœํžˆ `fp16` ๋˜๋Š” `bf16`์„ `True`๋กœ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” `torch.autocast`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์ด๋Š” Flash Attention์ด `fp16`์™€ `bf16` ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค[[resources]] + +Command-R์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + + +FP16 ๋ชจ๋ธ ๋กœ๋”ฉ +```python +# pip install transformers +from transformers import AutoTokenizer, AutoModelForCausalLM + +model_id = "CohereForAI/c4ai-command-r-v01" +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id) + +# command-r ์ฑ— ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ฉ”์„ธ์ง€ ํ˜•์‹์„ ์ •ํ•˜์„ธ์š” +messages = [{"role": "user", "content": "Hello, how are you?"}] +input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") +## <|START_OF_TURN_TOKEN|><|USER_TOKEN|>Hello, how are you?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|> + +gen_tokens = model.generate( + input_ids, + max_new_tokens=100, + do_sample=True, + temperature=0.3, + ) + +gen_text = tokenizer.decode(gen_tokens[0]) +print(gen_text) +``` + +bitsandbytes ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ 4bit ์–‘์žํ™”๋œ ๋ชจ๋ธ ๋กœ๋”ฉ +```python +# pip install transformers bitsandbytes accelerate +from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig + +bnb_config = BitsAndBytesConfig(load_in_4bit=True) + +model_id = "CohereForAI/c4ai-command-r-v01" +tokenizer = AutoTokenizer.from_pretrained(model_id) +model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config) + +gen_tokens = model.generate( + input_ids, + max_new_tokens=100, + do_sample=True, + temperature=0.3, + ) + +gen_text = tokenizer.decode(gen_tokens[0]) +print(gen_text) +``` + + +## CohereConfig[[transformers.CohereConfig]] + +[[autodoc]] CohereConfig + +## CohereTokenizerFast[[transformers.CohereTokenizerFast]] + +[[autodoc]] CohereTokenizerFast + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - update_post_processor + - save_vocabulary + +## CohereModel[[transformers.CohereModel]] + +[[autodoc]] CohereModel + - forward + + +## CohereForCausalLM[[transformers.CohereForCausalLM]] + +[[autodoc]] CohereForCausalLM + - forward + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/convbert.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/convbert.md new file mode 100644 index 0000000000000000000000000000000000000000..c4f0d2ace252e9cd6600e3613e7ee13457e1219d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/convbert.md @@ -0,0 +1,135 @@ + + +# ConvBERT [[convbert]] + +
+ +Models + + +Spaces + +
+ +## ๊ฐœ์š” [[overview]] + +ConvBERT ๋ชจ๋ธ์€ Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan์— ์˜ํ•ด ์ œ์•ˆ๋˜์—ˆ์œผ๋ฉฐ, ์ œ์•ˆ ๋…ผ๋ฌธ ์ œ๋ชฉ์€ [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://huggingface.co/papers/2008.02496)์ž…๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*BERT์™€ ๊ทธ ๋ณ€ํ˜• ๋ชจ๋ธ๊ณผ ๊ฐ™์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ๋“ค์€ ์ตœ๊ทผ ๋‹ค์–‘ํ•œ ์ž์—ฐ์–ด ์ดํ•ด ๊ณผ์ œ์—์„œ ๋†€๋ผ์šด ์„ฑ๊ณผ๋ฅผ ์ด๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ BERT๋Š” ๊ธ€๋กœ๋ฒŒ ์…€ํ”„ ์–ดํ…์…˜ ๋ธ”๋ก์— ํฌ๊ฒŒ ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ๊ณ  ๊ณ„์‚ฐ ๋น„์šฉ์ด ํฝ๋‹ˆ๋‹ค. ๋ชจ๋“  ์–ดํ…์…˜ ํ—ค๋“œ๊ฐ€ ๊ธ€๋กœ๋ฒŒ ๊ด€์ ์—์„œ ์–ดํ…์…˜ ๋งต์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ž…๋ ฅ ์‹œํ€€์Šค ์ „์ฒด๋ฅผ ํƒ์ƒ‰ํ•˜์ง€๋งŒ, ์ผ๋ถ€ ํ—ค๋“œ๋Š” ๋กœ์ปฌ ์ข…์†์„ฑ๋งŒ ํ•™์Šตํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์ด ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ self-attention ํ—ค๋“œ๋“ค์„ ๋Œ€์ฒดํ•˜์—ฌ ๋กœ์ปฌ ์ข…์†์„ฑ์„ ์ง์ ‘ ๋ชจ๋ธ๋งํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด span ๊ธฐ๋ฐ˜ ๋™์  ์ปจ๋ณผ๋ฃจ์…˜์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ์ปจ๋ณผ๋ฃจ์…˜ ํ—ค๋“œ์™€ ๋‚˜๋จธ์ง€ self-attention ํ—ค๋“œ๋“ค์ด ๊ฒฐํ•ฉํ•˜์—ฌ ๊ธ€๋กœ๋ฒŒ ๋ฐ ๋กœ์ปฌ ๋ฌธ๋งฅ ํ•™์Šต์— ๋” ํšจ์œจ์ ์ธ ํ˜ผํ•ฉ ์–ดํ…์…˜ ๋ธ”๋ก์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” BERT์— ์ด ํ˜ผํ•ฉ ์–ดํ…์…˜ ์„ค๊ณ„๋ฅผ ์ ์šฉํ•˜์—ฌ ConvBERT ๋ชจ๋ธ์„ ๊ตฌ์ถ•ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹คํ—˜ ๊ฒฐ๊ณผ, ConvBERT๋Š” ๋‹ค์–‘ํ•œ ๋‹ค์šด์ŠคํŠธ๋ฆผ ๊ณผ์ œ์—์„œ BERT ๋ฐ ๊ทธ ๋ณ€ํ˜• ๋ชจ๋ธ๋ณด๋‹ค ๋” ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์˜€์œผ๋ฉฐ, ํ›ˆ๋ จ ๋น„์šฉ๊ณผ ๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ๋” ์ ์—ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ConvBERTbase ๋ชจ๋ธ์€ GLUE ์Šค์ฝ”์–ด 86.4๋ฅผ ๋‹ฌ์„ฑํ•˜์—ฌ ELECTRAbase๋ณด๋‹ค 0.7 ๋†’์€ ์„ฑ๊ณผ๋ฅผ ๋ณด์ด๋ฉฐ, ํ›ˆ๋ จ ๋น„์šฉ์€ 1/4 ์ดํ•˜๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [abhishek](https://huggingface.co/abhishek)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์œผ๋ฉฐ, ์›๋ณธ ๊ตฌํ˜„์€ ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค : https://github.com/yitu-opensource/ConvBert + + + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] +ConvBERT ํ›ˆ๋ จ ํŒ์€ BERT์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ํŒ์€ [BERT ๋ฌธ์„œ](bert).๋ฅผ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค. + + +## ๋ฆฌ์†Œ์Šค [[resources]] + +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ (Text classification task guide)](../tasks/sequence_classification) +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ (Token classification task guide)](../tasks/token_classification) +- [์งˆ์˜์‘๋‹ต ์ž‘์—… ๊ฐ€์ด๋“œ (Question answering task guide)](../tasks/question_answering) +- [๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ (Masked language modeling task guide)](../tasks/masked_language_modeling) +- [๋‹ค์ค‘ ์„ ํƒ ์ž‘์—… ๊ฐ€์ด๋“œ (Multiple choice task guide)](../tasks/multiple_choice) + +## ConvBertConfig [[transformers.ConvBertConfig]] + +[[autodoc]] ConvBertConfig + +## ConvBertTokenizer [[transformers.ConvBertTokenizer]] + +[[autodoc]] ConvBertTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## ConvBertTokenizerFast [[transformers.ConvBertTokenizerFast]] + +[[autodoc]] ConvBertTokenizerFast + + + + +## ConvBertModel [[transformers.ConvBertModel]] + +[[autodoc]] ConvBertModel + - forward + +## ConvBertForMaskedLM [[transformers.ConvBertForMaskedLM]] + +[[autodoc]] ConvBertForMaskedLM + - forward + +## ConvBertForSequenceClassification [[transformers.ConvBertForSequenceClassification]] + +[[autodoc]] ConvBertForSequenceClassification + - forward + +## ConvBertForMultipleChoice [[transformers.ConvBertForMultipleChoice]] + +[[autodoc]] ConvBertForMultipleChoice + - forward + +## ConvBertForTokenClassification [[transformers.ConvBertForTokenClassification]] + +[[autodoc]] ConvBertForTokenClassification + - forward + +## ConvBertForQuestionAnswering [[transformers.ConvBertForQuestionAnswering]] + +[[autodoc]] ConvBertForQuestionAnswering + - forward + + + + +## TFConvBertModel [[transformers.TFConvBertModel]] + +[[autodoc]] TFConvBertModel + - call + +## TFConvBertForMaskedLM [[transformers.TFConvBertForMaskedLM]] + +[[autodoc]] TFConvBertForMaskedLM + - call + +## TFConvBertForSequenceClassification [[transformers.TFConvBertForSequenceClassification]] + +[[autodoc]] TFConvBertForSequenceClassification + - call + +## TFConvBertForMultipleChoice [[transformers.TFConvBertForMultipleChoice]] + +[[autodoc]] TFConvBertForMultipleChoice + - call + +## TFConvBertForTokenClassification [[transformers.TFConvBertForTokenClassification]] + +[[autodoc]] TFConvBertForTokenClassification + - call + +## TFConvBertForQuestionAnswering [[transformers.TFConvBertForQuestionAnswering]] + +[[autodoc]] TFConvBertForQuestionAnswering + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/dbrx.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/dbrx.md new file mode 100644 index 0000000000000000000000000000000000000000..4b33e95700691edd07f82c989c9c07e36aa0b419 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/dbrx.md @@ -0,0 +1,125 @@ + + +# DBRX[[dbrx]] + +## ๊ฐœ์š”[[overview]] + +DBRX๋Š” [ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜์˜](https://www.isattentionallyouneed.com/) ๋‹ค์Œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ๋””์ฝ”๋” ์ „์šฉ LLM ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +์ด 132B ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ *์„ธ๋ฐ€ํ•œ* ์ „๋ฌธ๊ฐ€ ํ˜ผํ•ฉ(MoE) ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ด ์ค‘ 36B ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ž…๋ ฅ๋งˆ๋‹ค ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. +12T ํ† ํฐ์˜ ํ…์ŠคํŠธ์™€ ์ฝ”๋“œ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +Mixtral-8x7B์™€ Grok-1๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ๊ณต๊ฐœ MoE ๋ชจ๋ธ๋“ค๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ, DBRX๋Š” ๋” ๋งŽ์€ ์ˆ˜์˜ ์ž‘์€ ์ „๋ฌธ๊ฐ€๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ์„ธ๋ฐ€ํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. DBRX๋Š” 16๊ฐœ์˜ ์ „๋ฌธ๊ฐ€ ์ค‘ 4๊ฐœ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐ˜๋ฉด, Mixtral-8x7B์™€ Grok-1์€ 8๊ฐœ์˜ ์ „๋ฌธ๊ฐ€ ์ค‘ 2๊ฐœ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. + +์ด๋Š” 65๋ฐฐ ๋” ๋งŽ์€ ์ „๋ฌธ๊ฐ€ ์กฐํ•ฉ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์˜ ํ’ˆ์งˆ์ด ํ–ฅ์ƒ๋˜๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. +DBRX๋Š” ํšŒ์ „ ์œ„์น˜ ์ธ์ฝ”๋”ฉ(RoPE), ๊ฒŒ์ดํŠธ ์„ ํ˜• ์œ ๋‹›(GLU), ๊ทธ๋ฃน ์ฟผ๋ฆฌ ์–ดํ…์…˜(GQA)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +BPE ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์ด๋ฉฐ [tiktoken](https://github.com/openai/tiktoken) ์ €์žฅ์†Œ์— ์„ค๋ช…๋œ GPT-4 ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ์„ ํƒ๋“ค์€ ์ฒ ์ €ํ•œ ํ‰๊ฐ€์™€ ์Šค์ผ€์ผ๋ง ์‹คํ—˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค. + +DBRX๋Š” ์‹ ์ค‘ํ•˜๊ฒŒ ์„ ๋ณ„๋œ 12T ํ† ํฐ์˜ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์œผ๋ฉฐ, ์ตœ๋Œ€ ๋ฌธ๋งฅ ๊ธธ์ด๋Š” 32K ํ† ํฐ์ž…๋‹ˆ๋‹ค. +์ด ๋ฐ์ดํ„ฐ๋Š” ํ† ํฐ ๋Œ€๋น„ MPT ๊ณ„์—ด ๋ชจ๋ธ ํ•™์Šต์— ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ์ตœ์†Œ 2๋ฐฐ ์ด์ƒ ๋” ์ข‹์€ ๊ฒƒ์œผ๋กœ ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. +์ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์…‹์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ Apache Sparkโ„ข์™€ Databricks ๋…ธํŠธ๋ถ, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์™€ ๊ฑฐ๋ฒ„๋„Œ์Šค๋ฅผ ์œ„ํ•œ Unity Catalog๋ฅผ ํฌํ•จํ•œ Databricks ๋„๊ตฌ ์ „์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์šฐ๋ฆฌ๋Š” ์‚ฌ์ „ ํ•™์Šต์„ ์œ„ํ•ด ์ปค๋ฆฌํ˜๋Ÿผ ํ•™์Šต์„ ์‚ฌ์šฉํ–ˆ์œผ๋ฉฐ, ํ•™์Šต ์ค‘ ๋ฐ์ดํ„ฐ ๋ฏน์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ์‹์ด ๋ชจ๋ธ ํ’ˆ์งˆ์„ ์ƒ๋‹นํžˆ ๊ฐœ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. + + +DBRX Instruct์™€ DBRX Base์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋Š” ์ด [๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://www.databricks.com/blog/introducing-dbrx-new-state-art-open-llm)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [eitan-turok](https://huggingface.co/eitanturok)์™€ [abhi-db](https://huggingface.co/abhi-db)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/databricks/dbrx-instruct)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ตœ์‹  ๋ฒ„์ „์ด ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ[[usage-examples]] + +`generate()` ๋ฉ”์†Œ๋“œ๋Š” DBRX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์ค€ ์–ดํ…์…˜ ๊ตฌํ˜„, ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜, PyTorch์˜ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜(Scaled Dot-Product Attention)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›„์ž์˜ ๋‘ ์–ดํ…์…˜ ๊ตฌํ˜„ ๋ฐฉ์‹์€ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํฌ๊ฒŒ ๋†’์—ฌ์ค๋‹ˆ๋‹ค. + +```python +from transformers import DbrxForCausalLM, AutoTokenizer +import torch + +tokenizer = AutoTokenizer.from_pretrained("databricks/dbrx-instruct", token="YOUR_HF_TOKEN") +model = DbrxForCausalLM.from_pretrained( + "databricks/dbrx-instruct", + device_map="auto", + torch_dtype=torch.bfloat16, + token="YOUR_HF_TOKEN", + ) + +input_text = "What does it take to build a great LLM?" +messages = [{"role": "user", "content": input_text}] +input_ids = tokenizer.apply_chat_template(messages, return_dict=True, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda") + +outputs = model.generate(**input_ids, max_new_tokens=200) +print(tokenizer.decode(outputs[0])) +``` + +`pip install flash-attn`๋ฅผ ํ†ตํ•ด ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์„ค์น˜ํ•˜๋ฉด, ๋” ๋น ๋ฅธ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์— ๋Œ€ํ•œ HuggingFace ๋ฌธ์„œ๋Š” [์ด๊ณณ](https://huggingface.co/docs/transformers/perf_infer_gpu_one#flashattention-2)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) + + +```python +from transformers import DbrxForCausalLM, AutoTokenizer +import torch + +tokenizer = AutoTokenizer.from_pretrained("databricks/dbrx-instruct", token="YOUR_HF_TOKEN") +model = DbrxForCausalLM.from_pretrained( + "databricks/dbrx-instruct", + device_map="auto", + torch_dtype=torch.bfloat16, + token="YOUR_HF_TOKEN", + attn_implementation="flash_attention_2", + ) + +input_text = "What does it take to build a great LLM?" +messages = [{"role": "user", "content": input_text}] +input_ids = tokenizer.apply_chat_template(messages, return_dict=True, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda") + +outputs = model.generate(**input_ids, max_new_tokens=200) +print(tokenizer.decode(outputs[0])) +``` + +PyTorch์˜ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๋” ๋น ๋ฅธ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜์— ๋Œ€ํ•œ HuggingFace ๋ฌธ์„œ๋Š” [์ด๊ณณ](https://huggingface.co/docs/transformers/perf_infer_gpu_one#pytorch-scaled-dot-product-attention)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) + + +```python +from transformers import DbrxForCausalLM, AutoTokenizer +import torch + +tokenizer = AutoTokenizer.from_pretrained("databricks/dbrx-instruct", token="YOUR_HF_TOKEN") +model = DbrxForCausalLM.from_pretrained( + "databricks/dbrx-instruct", + device_map="auto", + torch_dtype=torch.bfloat16, + token="YOUR_HF_TOKEN", + attn_implementation="sdpa", + ) + +input_text = "What does it take to build a great LLM?" +messages = [{"role": "user", "content": input_text}] +input_ids = tokenizer.apply_chat_template(messages, return_dict=True, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda") + +outputs = model.generate(**input_ids, max_new_tokens=200) +print(tokenizer.decode(outputs[0])) +``` + +## DbrxConfig[[transformers.DbrxConfig]] + +[[autodoc]] DbrxConfig + + +## DbrxModel[[transformers.DbrxModel]] + +[[autodoc]] DbrxModel + - forward + + +## DbrxForCausalLM[[transformers.DbrxForCausalLM]] + +[[autodoc]] DbrxForCausalLM + - forward + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta-v2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta-v2.md new file mode 100644 index 0000000000000000000000000000000000000000..bb7bed1434cdd2fd83066d8ff46bf945aa67f599 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta-v2.md @@ -0,0 +1,147 @@ + + +# DeBERTa-v2 + +## ๊ฐœ์š” + + +DeBERTa ๋ชจ๋ธ์€ Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen์ด ์ž‘์„ฑํ•œ [DeBERTa: ๋ถ„๋ฆฌ๋œ ์–ดํ…์…˜์„ ํ™œ์šฉํ•œ ๋””์ฝ”๋”ฉ ๊ฐ•ํ™” BERT](https://huggingface.co/papers/2006.03654)์ด๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 2018๋…„ Google์ด ๋ฐœํ‘œํ•œ BERT ๋ชจ๋ธ๊ณผ 2019๋…„ Facebook์ด ๋ฐœํ‘œํ•œ RoBERTa ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. +DeBERTa๋Š” RoBERTa์—์„œ ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ์˜ ์ ˆ๋ฐ˜๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฆฌ๋œ(disentangled) ์–ดํ…์…˜๊ณผ ํ–ฅ์ƒ๋œ ๋งˆ์Šคํฌ ๋””์ฝ”๋” ํ•™์Šต์„ ํ†ตํ•ด RoBERTa๋ฅผ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์‚ฌ์ „ ํ•™์Šต๋œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ์˜ ์ตœ๊ทผ ๋ฐœ์ „์€ ๋งŽ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ๋‘ ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ BERT์™€ RoBERTa ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•œ ์ƒˆ๋กœ์šด ๋ชจ๋ธ ๊ตฌ์กฐ์ธ DeBERTa๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋ถ„๋ฆฌ๋œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ, ๊ฐ ๋‹จ์–ด๊ฐ€ ๋‚ด์šฉ๊ณผ ์œ„์น˜๋ฅผ ๊ฐ๊ฐ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„๋˜๋ฉฐ, ๋‹จ์–ด๋“ค ๊ฐ„์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋Š” ๋‚ด์šฉ๊ณผ ์ƒ๋Œ€์  ์œ„์น˜์— ๋Œ€ํ•œ ๋ถ„๋ฆฌ๋œ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ, ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต์„ ์œ„ํ•ด ๋งˆ์Šคํ‚น๋œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ์ถœ๋ ฅ ์†Œํ”„ํŠธ๋งฅ์Šค ์ธต์„ ๋Œ€์ฒดํ•˜๋Š” ํ–ฅ์ƒ๋œ ๋งˆ์Šคํฌ ๋””์ฝ”๋”๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ๋‘ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ด ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต์˜ ํšจ์œจ์„ฑ๊ณผ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RoBERTa-Large์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, ์ ˆ๋ฐ˜์˜ ํ•™์Šต ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋œ DeBERTa ๋ชจ๋ธ์€ ๊ด‘๋ฒ”์œ„ํ•œ NLP ์ž‘์—…์—์„œ ์ผ๊ด€๋˜๊ฒŒ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, MNLI์—์„œ +0.9%(90.2% vs 91.1%), SQuAD v2.0์—์„œ +2.3%(88.4% vs 90.7%), RACE์—์„œ +3.6%(83.2% vs 86.8%)์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. DeBERTa ์ฝ”๋“œ์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ https://github.com/microsoft/DeBERTa ์—์„œ ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.* + + +๋‹ค์Œ ์ •๋ณด๋“ค์€ [์›๋ณธ ๊ตฌํ˜„ ์ €์žฅ์†Œ](https://github.com/microsoft/DeBERTa)์—์„œ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. DeBERTa v2๋Š” DeBERTa์˜ ๋‘๋ฒˆ์งธ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +DeBERTa v2๋Š” SuperGLUE ๋‹จ์ผ ๋ชจ๋ธ ์ œ์ถœ์— ์‚ฌ์šฉ๋œ 1.5B ๋ชจ๋ธ์„ ํฌํ•จํ•˜๋ฉฐ, ์ธ๊ฐ„ ๊ธฐ์ค€์ (๋ฒ ์ด์Šค๋ผ์ธ) 89.8์  ๋Œ€๋น„ 89.9์ ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €์ž์˜ +[๋ธ”๋กœ๊ทธ](https://www.microsoft.com/en-us/research/blog/microsoft-deberta-surpasses-human-performance-on-the-superglue-benchmark/)์—์„œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +v2์˜ ์ƒˆ๋กœ์šด ์ : + +- **์–ดํœ˜(Vocabulary)** v2์—์„œ๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ตฌ์ถ•๋œ 128K ํฌ๊ธฐ์˜ ์ƒˆ๋กœ์šด ์–ดํœ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ† ํฌ๋‚˜์ด์ €๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. GPT2 ๊ธฐ๋ฐ˜ ํ† ํฌ๋‚˜์ด์ € ๋Œ€์‹ , ์ด์ œ๋Š” [์„ผํ…์Šคํ”ผ์Šค ๊ธฐ๋ฐ˜](https://github.com/google/sentencepiece) ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- **nGiE[n๊ทธ๋žจ ์œ ๋„(Induced) ์ž…๋ ฅ ์ธ์ฝ”๋”ฉ]** DeBERTa-v2 ๋ชจ๋ธ์€ ์ž…๋ ฅ ํ† ํฐ๋“ค์˜ ์ง€์—ญ์  ์˜์กด์„ฑ์„ ๋” ์ž˜ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ํŠธ๋žœ์Šคํฌ๋จธ ์ธต๊ณผ ํ•จ๊ป˜ ์ถ”๊ฐ€์ ์ธ ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- **์–ดํ…์…˜ ์ธต์—์„œ ์œ„์น˜ ํˆฌ์˜ ํ–‰๋ ฌ๊ณผ ๋‚ด์šฉ ํˆฌ์˜ ํ–‰๋ ฌ ๊ณต์œ ** ์ด์ „ ์‹คํ—˜๋“ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ์ด๋Š” ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- **์ƒ๋Œ€์  ์œ„์น˜๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฒ„ํ‚ท ์ ์šฉ** DeBERTa-v2 ๋ชจ๋ธ์€ T5์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๋Œ€์  ์œ„์น˜๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๊ทธ ๋ฒ„ํ‚ท์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- **900M ๋ชจ๋ธ & 1.5B ๋ชจ๋ธ** 900M๊ณผ 1.5B, ๋‘ ๊ฐ€์ง€ ์ถ”๊ฐ€ ๋ชจ๋ธ ํฌ๊ธฐ๊ฐ€ ์ œ๊ณต๋˜๋ฉฐ, ์ด๋Š” ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. + +[DeBERTa](https://huggingface.co/DeBERTa) ๋ชจ๋ธ์˜ ํ…์„œํ”Œ๋กœ 2.0 ๊ตฌํ˜„์€ [kamalkraj](https://huggingface.co/kamalkraj)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/microsoft/DeBERTa)์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ + +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification) +- [์งˆ์˜์‘๋‹ต ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering) +- [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) +- [๋‹ค์ค‘ ์„ ํƒ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/multiple_choice) + +## DebertaV2Config + +[[autodoc]] DebertaV2Config + +## DebertaV2Tokenizer + +[[autodoc]] DebertaV2Tokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## DebertaV2TokenizerFast + +[[autodoc]] DebertaV2TokenizerFast + - build_inputs_with_special_tokens + - create_token_type_ids_from_sequences + + + + +## DebertaV2Model + +[[autodoc]] DebertaV2Model + - forward + +## DebertaV2PreTrainedModel + +[[autodoc]] DebertaV2PreTrainedModel + - forward + +## DebertaV2ForMaskedLM + +[[autodoc]] DebertaV2ForMaskedLM + - forward + +## DebertaV2ForSequenceClassification + +[[autodoc]] DebertaV2ForSequenceClassification + - forward + +## DebertaV2ForTokenClassification + +[[autodoc]] DebertaV2ForTokenClassification + - forward + +## DebertaV2ForQuestionAnswering + +[[autodoc]] DebertaV2ForQuestionAnswering + - forward + +## DebertaV2ForMultipleChoice + +[[autodoc]] DebertaV2ForMultipleChoice + - forward + + + + +## TFDebertaV2Model + +[[autodoc]] TFDebertaV2Model + - call + +## TFDebertaV2PreTrainedModel + +[[autodoc]] TFDebertaV2PreTrainedModel + - call + +## TFDebertaV2ForMaskedLM + +[[autodoc]] TFDebertaV2ForMaskedLM + - call + +## TFDebertaV2ForSequenceClassification + +[[autodoc]] TFDebertaV2ForSequenceClassification + - call + +## TFDebertaV2ForTokenClassification + +[[autodoc]] TFDebertaV2ForTokenClassification + - call + +## TFDebertaV2ForQuestionAnswering + +[[autodoc]] TFDebertaV2ForQuestionAnswering + - call + +## TFDebertaV2ForMultipleChoice + +[[autodoc]] TFDebertaV2ForMultipleChoice + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta.md new file mode 100644 index 0000000000000000000000000000000000000000..b471c9327ae18c123fdc50d598a91f8ed1719742 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/deberta.md @@ -0,0 +1,152 @@ + + +# DeBERTa[[deberta]] + +## ๊ฐœ์š”[[overview]] + + +DeBERTa ๋ชจ๋ธ์€ Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen์ด ์ž‘์„ฑํ•œ [DeBERTa: ๋ถ„๋ฆฌ๋œ ์–ดํ…์…˜์„ ํ™œ์šฉํ•œ ๋””์ฝ”๋”ฉ ๊ฐ•ํ™” BERT](https://huggingface.co/papers/2006.03654)์ด๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 2018๋…„ Google์ด ๋ฐœํ‘œํ•œ BERT ๋ชจ๋ธ๊ณผ 2019๋…„ Facebook์ด ๋ฐœํ‘œํ•œ RoBERTa ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. +DeBERTa๋Š” RoBERTa์—์„œ ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ์˜ ์ ˆ๋ฐ˜๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฆฌ๋œ(disentangled) ์–ดํ…์…˜๊ณผ ํ–ฅ์ƒ๋œ ๋งˆ์Šคํฌ ๋””์ฝ”๋” ํ•™์Šต์„ ํ†ตํ•ด RoBERTa๋ฅผ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์‚ฌ์ „ ํ•™์Šต๋œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ์˜ ์ตœ๊ทผ ๋ฐœ์ „์€ ๋งŽ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ๋‘ ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ BERT์™€ RoBERTa ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•œ ์ƒˆ๋กœ์šด ๋ชจ๋ธ ๊ตฌ์กฐ์ธ DeBERTa๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋ถ„๋ฆฌ๋œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ, ๊ฐ ๋‹จ์–ด๊ฐ€ ๋‚ด์šฉ๊ณผ ์œ„์น˜๋ฅผ ๊ฐ๊ฐ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„๋˜๋ฉฐ, ๋‹จ์–ด๋“ค ๊ฐ„์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋Š” ๋‚ด์šฉ๊ณผ ์ƒ๋Œ€์  ์œ„์น˜์— ๋Œ€ํ•œ ๋ถ„๋ฆฌ๋œ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ, ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต์„ ์œ„ํ•ด ๋งˆ์Šคํ‚น๋œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ์ถœ๋ ฅ ์†Œํ”„ํŠธ๋งฅ์Šค ์ธต์„ ๋Œ€์ฒดํ•˜๋Š” ํ–ฅ์ƒ๋œ ๋งˆ์Šคํฌ ๋””์ฝ”๋”๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ๋‘ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ด ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต์˜ ํšจ์œจ์„ฑ๊ณผ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RoBERTa-Large์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, ์ ˆ๋ฐ˜์˜ ํ•™์Šต ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋œ DeBERTa ๋ชจ๋ธ์€ ๊ด‘๋ฒ”์œ„ํ•œ NLP ์ž‘์—…์—์„œ ์ผ๊ด€๋˜๊ฒŒ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, MNLI์—์„œ +0.9%(90.2% vs 91.1%), SQuAD v2.0์—์„œ +2.3%(88.4% vs 90.7%), RACE์—์„œ +3.6%(83.2% vs 86.8%)์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. DeBERTa ์ฝ”๋“œ์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ https://github.com/microsoft/DeBERTa ์—์„œ ๊ณต๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.* + +[DeBERTa](https://huggingface.co/DeBERTa) ๋ชจ๋ธ์˜ ํ…์„œํ”Œ๋กœ 2.0 ๊ตฌํ˜„์€ [kamalkraj](https://huggingface.co/kamalkraj)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/microsoft/DeBERTa)์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค[[resources]] + + +DeBERTa๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + + +- DeBERTa์™€ [DeepSpeed๋ฅผ ์ด์šฉํ•ด์„œ ๋Œ€ํ˜• ๋ชจ๋ธ ํ•™์Šต์„ ๊ฐ€์†์‹œํ‚ค๋Š”](https://huggingface.co/blog/accelerate-deepspeed) ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํฌ์ŠคํŠธ. +- DeBERTa์™€ [๋จธ์‹ ๋Ÿฌ๋‹์œผ๋กœ ํ•œ์ธต ํ–ฅ์ƒ๋œ ๊ณ ๊ฐ ์„œ๋น„์Šค](https://huggingface.co/blog/supercharge-customer-service-with-machine-learning)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [`DebertaForSequenceClassification`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFDebertaForSequenceClassification`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) + + + +- [`DebertaForTokenClassification`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- [`TFDebertaForTokenClassification`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [ํ† ํฐ ๋ถ„๋ฅ˜](https://huggingface.co/course/chapter7/2?fw=pt) ์žฅ. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [BPE(Byte-Pair Encoding) ํ† ํฐํ™”](https://huggingface.co/course/chapter6/5?fw=pt) ์žฅ. +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification) + + + +- [`DebertaForMaskedLM`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- [`TFDebertaForMaskedLM`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง](https://huggingface.co/course/chapter7/3?fw=pt) ์žฅ. +- [๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) + + + +- [`DebertaForQuestionAnswering`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- [`TFDebertaForQuestionAnswering`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)์—์„œ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [์งˆ์˜์‘๋‹ต(Question answering)](https://huggingface.co/course/chapter7/7?fw=pt) ์žฅ. +- [์งˆ์˜์‘๋‹ต ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering) + +## DebertaConfig[[transformers.DebertaConfig]] + +[[autodoc]] DebertaConfig + +## DebertaTokenizer[[transformers.DebertaTokenizer]] + +[[autodoc]] DebertaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## DebertaTokenizerFast[[transformers.DebertaTokenizerFast]] + +[[autodoc]] DebertaTokenizerFast + - build_inputs_with_special_tokens + - create_token_type_ids_from_sequences + + + + +## DebertaModel[[transformers.DebertaModel]] + +[[autodoc]] DebertaModel + - forward + +## DebertaPreTrainedModel[[transformers.DebertaPreTrainedModel]] + +[[autodoc]] DebertaPreTrainedModel + +## DebertaForMaskedLM[[transformers.DebertaForMaskedLM]] + +[[autodoc]] DebertaForMaskedLM + - forward + +## DebertaForSequenceClassification[[transformers.DebertaForSequenceClassification]] + +[[autodoc]] DebertaForSequenceClassification + - forward + +## DebertaForTokenClassification[[transformers.DebertaForTokenClassification]] + +[[autodoc]] DebertaForTokenClassification + - forward + +## DebertaForQuestionAnswering[[transformers.DebertaForQuestionAnswering]] + +[[autodoc]] DebertaForQuestionAnswering + - forward + + + + +## TFDebertaModel[[transformers.TFDebertaModel]] + +[[autodoc]] TFDebertaModel + - call + +## TFDebertaPreTrainedModel[[transformers.TFDebertaPreTrainedModel]] + +[[autodoc]] TFDebertaPreTrainedModel + - call + +## TFDebertaForMaskedLM[[transformers.TFDebertaForMaskedLM]] + +[[autodoc]] TFDebertaForMaskedLM + - call + +## TFDebertaForSequenceClassification[[transformers.TFDebertaForSequenceClassification]] + +[[autodoc]] TFDebertaForSequenceClassification + - call + +## TFDebertaForTokenClassification[[transformers.TFDebertaForTokenClassification]] + +[[autodoc]] TFDebertaForTokenClassification + - call + +## TFDebertaForQuestionAnswering[[transformers.TFDebertaForQuestionAnswering]] + +[[autodoc]] TFDebertaForQuestionAnswering + - call + + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/electra.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/electra.md new file mode 100644 index 0000000000000000000000000000000000000000..d7bee11db701dbec6e35192d38275159500819d3 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/electra.md @@ -0,0 +1,196 @@ + + +# ELECTRA[[electra]] + +
+PyTorch +TensorFlow +Flax +
+ +## ๊ฐœ์š”[[overview]] + +ELECTRA ๋ชจ๋ธ์€ [ELECTRA: Pre-training Text Encoders as Discriminators Rather Than +Generators](https://openreview.net/pdf?id=r1xMH1BtvB) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ELECTRA๋Š” ๋‘๊ฐ€์ง€ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ธ ์ƒ์„ฑ ๋ชจ๋ธ๊ณผ ํŒ๋ณ„ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋Š” ์ƒˆ๋กœ์šด ์‚ฌ์ „ํ•™์Šต ์ ‘๊ทผ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ ๋ชจ๋ธ์˜ ์—ญํ• ์€ ์‹œํ€€์Šค์— ์žˆ๋Š” ํ† ํฐ์„ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋กœ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ์„ ๊ฐ€์ง„ ํŒ๋ณ„ ๋ชจ๋ธ์€ ์‹œํ€€์Šค์—์„œ ์–ด๋–ค ํ† ํฐ์ด ์ƒ์„ฑ ๋ชจ๋ธ์— ์˜ํ•ด ๋Œ€์ฒด๋˜์—ˆ๋Š”์ง€ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*BERT์™€ ๊ฐ™์€ ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ(MLM) ์‚ฌ์ „ํ•™์Šต ๋ฐฉ๋ฒ•์€ ์ผ๋ถ€ ํ† ํฐ์„ [MASK] ํ† ํฐ์œผ๋กœ ๋ฐ”๊ฟ” ์†์ƒ์‹œํ‚ค๊ณ  ๋‚œ ๋’ค, ๋ชจ๋ธ์ด ๋‹ค์‹œ ์›๋ณธ ํ† ํฐ์„ ๋ณต์›ํ•˜๋„๋ก ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์€ ๋‹ค์šด์ŠคํŠธ๋ฆผ NLP ์ž‘์—…์„ ์ „์ดํ•  ๋•Œ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด์ง€๋งŒ, ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์€ ์–‘์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€์•ˆ์œผ๋กœ, ๋Œ€์ฒด ํ† ํฐ ํƒ์ง€๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์ƒ˜ํ”Œ-ํšจ๊ณผ์ ์ธ ์‚ฌ์ „ํ•™์Šต์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ฐฉ๋ฒ•๋ก ์€ ์ž…๋ ฅ์— ๋งˆ์Šคํ‚น์„ ํ•˜๋Š” ๋Œ€์‹ ์— ์†Œํ˜• ์ƒ์„ฑ ๋ชจ๋ธ์˜ ๊ทธ๋Ÿด๋“ฏํ•œ ๋Œ€์•ˆ ํ† ํฐ์œผ๋กœ ์†์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ, ๋ชจ๋ธ์ด ์†์ƒ๋œ ํ† ํฐ์˜ ์›๋ž˜ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋„๋ก ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋Œ€์‹ , ํŒ๋ณ„ ๋ชจ๋ธ์„ ๊ฐ๊ฐ์˜ ํ† ํฐ์ด ์ƒ์„ฑ ๋ชจ๋ธ์˜ ์ƒ˜ํ”Œ๋กœ ์†์ƒ๋˜์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์‹คํ—˜๋“ค์€ ํ†ตํ•ด ์ด ์ƒˆ๋กœ์šด ์‚ฌ์ „ํ•™์Šต ๋ฐฉ์‹์€ ๋งˆ์Šคํ‚น๋œ ์ผ๋ถ€ ํ† ํฐ์—๋งŒ ์ ์šฉ๋˜๋Š” ๊ธฐ์กด ๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ ๋ชจ๋“  ์ž…๋ ฅ ํ† ํฐ์— ๋Œ€ํ•ด ํ•™์Šต์ด ์ด๋ค„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ(MLM)๋ณด๋‹ค ๋” ํšจ์œจ์ ์ž„์„ ์ž…์ฆํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์†Œ๊ฐœ๋œ ๋ฐฉ์‹์ด ๊ฐ™์€ ๋ชจ๋ธ ํฌ๊ธฐ, ๋ฐ์ดํ„ฐ, ์—ฐ์‚ฐ๋Ÿ‰์„ ๊ฐ€์ง„ BERT๋ชจ๋ธ๋กœ ํ•™์Šตํ•œ ๊ฒฐ๊ณผ๋ฅผ ์••๋„ํ•˜๋Š” ๋ฌธ๋งฅ ํ‘œํ˜„ ํ•™์Šต์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ์ž‘์€ ๋ชจ๋ธ์—์„œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ๋‘๋“œ๋Ÿฌ์ง€๋ฉฐ, ์˜ˆ๋ฅผ ๋“ค์–ด GPU ํ•œ ๋Œ€๋กœ 4์ผ๊ฐ„ ํ•™์Šตํ•œ ๋ชจ๋ธ์ด 30๋ฐฐ ๋” ๋งŽ์€ ๊ณ„์‚ฐ ์ž์›์„ ์‚ฌ์šฉํ•œ GPT๋ณด๋‹ค GLUE ์ž์—ฐ์–ด ์ดํ•ด ๋ฒค์น˜๋งˆํฌ์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ๋Œ€๊ทœ๋ชจ ํ™˜๊ฒฝ์—์„œ๋„ ์œ ํšจํ•˜๋ฉฐ ๋” ์ ์€ ์—ฐ์‚ฐ๋Ÿ‰์œผ๋กœ RoBERTa์™€ XLNet๊ณผ ๋น„์Šทํ•œ ์„ฑ๋Šฅ์„ ๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋™์ผํ•œ ์—ฐ์‚ฐ๋Ÿ‰์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ ์ด๋“ค์˜ ์„ฑ๋Šฅ์„ ๋Šฅ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.* + + +์ด ๋ชจ๋ธ์€ [lysandre](https://huggingface.co/lysandre)์ด ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/google-research/electra)์—์„œ ์ฐพ์•„๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- ELECTRA๋Š” ์‚ฌ์ „ํ•™์Šต ๋ฐฉ๋ฒ•์œผ๋กœ ๊ธฐ๋ณธ ๋ชจ๋ธ์ธ BERT์˜ ๊ตฌ์กฐ์™€ ๊ฑฐ์˜ ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด๋Š” ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ์™€ ํžˆ๋“  ํฌ๊ธฐ๋ฅผ ๊ตฌ๋ถ„ํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ์ž‘๊ณ , ํžˆ๋“  ํฌ๊ธฐ๋Š” ๋” ํฝ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์—์„œ ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋ฅผ ํžˆ๋“  ํฌ๊ธฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋กœ ์„ ํ˜• ๋ณ€ํ™˜ ์ธต์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ์™€ ํžˆ๋“  ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•  ๊ฒฝ์šฐ์—๋Š” ์ด ์„ ํ˜• ๋ณ€ํ™˜ ์ธต์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- ELECTRA๋Š” ๋˜ ๋‹ค๋ฅธ (์ž‘์€) ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ์‚ฌ์ „ํ•™์Šต ๋œ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ž‘์€ ์–ธ์–ด ๋ชจ๋ธ์ด ์ž…๋ ฅ ํ…์ŠคํŠธ์˜ ์ผ๋ถ€๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚นํ•˜๊ณ , ๊ทธ ์ž๋ฆฌ์— ์ƒˆ๋กœ์šด ํ† ํฐ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ELECTRA๋Š” ์›๋ž˜ ํ† ํฐ๊ณผ ๋Œ€์ฒด๋œ ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. GAN ํ›ˆ๋ จ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ, ์ƒ์„ฑ ๋ชจ๋ธ์€ ELECTRA ๋ชจ๋ธ์„ ์†์ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์›๋ž˜ ํ…์ŠคํŠธ๋ฅผ ๋ณต์›ํ•˜๋Š” ๋ชฉํ‘œ๋กœ ๋ช‡ ๋‹จ๊ณ„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ELECTRA๊ฐ€ ํ•™์Šต์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. +- [๊ตฌ๊ธ€ ๋ฆฌ์„œ์น˜์˜ ๊ตฌํ˜„](https://github.com/google-research/electra)์œผ๋กœ ์ €์žฅ๋œ ELECTRA checkpoints๋Š” ์ƒ์„ฑ ๋ชจ๋ธ๊ณผ ํŒ๋ณ„ ๋ชจ๋ธ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ๋ชจ๋ธ์„ ์–ด๋–ค ์•„ํ‚คํ…์ฒ˜๋กœ ๋‚ด๋ณด๋‚ผ์ง€ ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋‹จ Hugging Face ํฌ๋งท์œผ๋กœ ๋ณ€ํ™˜๋˜๋ฉด, ์ด ์ฒดํฌํฌ์ธํŠธ๋“ค์€ ๋ชจ๋“  ELECTRA ๋ชจ๋ธ์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํŒ๋ณ„ ๋ชจ๋ธ์€ [`ElectraForMaskedLM`] ๋ชจ๋ธ์—, ์ƒ์„ฑ ๋ชจ๋ธ์€ [`ElectraForPreTraining`]๋ชจ๋ธ์— ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. (๋‹จ, ์ƒ์„ฑ ๋ชจ๋ธ์—๋Š” ๋ถ„๋ฅ˜ ํ—ค๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ๋ถ€๋ถ„์€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.) + +## ์ฐธ๊ณ  ์ž๋ฃŒ[[resources]] + +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ฐ€์ด๋“œ](../tasks/sequence_classification) +- [ํ† ํฐ ๋ถ„๋ฅ˜ ๊ฐ€์ด๋“œ](../tasks/token_classification) +- [์งˆ์˜ ์‘๋‹ต ๊ฐ€์ด๋“œ](../tasks/question_answering) +- [์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง ๊ฐ€์ด๋“œ](../tasks/language_modeling) +- [๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) +- [๊ฐ๊ด€์‹ ๋ฌธ์ œ ๊ฐ€์ด๋“œ](../tasks/multiple_choice) + +## ElectraConfig + +[[autodoc]] ElectraConfig + +## ElectraTokenizer + +[[autodoc]] ElectraTokenizer + +## ElectraTokenizerFast + +[[autodoc]] ElectraTokenizerFast + +## Electra specific outputs + +[[autodoc]] models.electra.modeling_electra.ElectraForPreTrainingOutput + +[[autodoc]] models.electra.modeling_tf_electra.TFElectraForPreTrainingOutput + + + + +## ElectraModel + +[[autodoc]] ElectraModel + - forward + +## ElectraForPreTraining + +[[autodoc]] ElectraForPreTraining + - forward + +## ElectraForCausalLM + +[[autodoc]] ElectraForCausalLM + - forward + +## ElectraForMaskedLM + +[[autodoc]] ElectraForMaskedLM + - forward + +## ElectraForSequenceClassification + +[[autodoc]] ElectraForSequenceClassification + - forward + +## ElectraForMultipleChoice + +[[autodoc]] ElectraForMultipleChoice + - forward + +## ElectraForTokenClassification + +[[autodoc]] ElectraForTokenClassification + - forward + +## ElectraForQuestionAnswering + +[[autodoc]] ElectraForQuestionAnswering + - forward + + + + +## TFElectraModel + +[[autodoc]] TFElectraModel + - call + +## TFElectraForPreTraining + +[[autodoc]] TFElectraForPreTraining + - call + +## TFElectraForMaskedLM + +[[autodoc]] TFElectraForMaskedLM + - call + +## TFElectraForSequenceClassification + +[[autodoc]] TFElectraForSequenceClassification + - call + +## TFElectraForMultipleChoice + +[[autodoc]] TFElectraForMultipleChoice + - call + +## TFElectraForTokenClassification + +[[autodoc]] TFElectraForTokenClassification + - call + +## TFElectraForQuestionAnswering + +[[autodoc]] TFElectraForQuestionAnswering + - call + + + + +## FlaxElectraModel + +[[autodoc]] FlaxElectraModel + - __call__ + +## FlaxElectraForPreTraining + +[[autodoc]] FlaxElectraForPreTraining + - __call__ + +## FlaxElectraForCausalLM + +[[autodoc]] FlaxElectraForCausalLM + - __call__ + +## FlaxElectraForMaskedLM + +[[autodoc]] FlaxElectraForMaskedLM + - __call__ + +## FlaxElectraForSequenceClassification + +[[autodoc]] FlaxElectraForSequenceClassification + - __call__ + +## FlaxElectraForMultipleChoice + +[[autodoc]] FlaxElectraForMultipleChoice + - __call__ + +## FlaxElectraForTokenClassification + +[[autodoc]] FlaxElectraForTokenClassification + - __call__ + +## FlaxElectraForQuestionAnswering + +[[autodoc]] FlaxElectraForQuestionAnswering + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/encoder-decoder.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/encoder-decoder.md new file mode 100644 index 0000000000000000000000000000000000000000..60982cb4554c5841f1f1ac929f9cdcb02b753efb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/encoder-decoder.md @@ -0,0 +1,167 @@ + + +# ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ[[Encoder Decoder Models]] + +## ๊ฐœ์š”[[Overview]] + +[`EncoderDecoderModel`]์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๋™ ์ธ์ฝ”๋”ฉ(autoencoding) ๋ชจ๋ธ์„ ์ธ์ฝ”๋”๋กœ, ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๊ฐ€ ํšŒ๊ท€(autoregressive) ๋ชจ๋ธ์„ ๋””์ฝ”๋”๋กœ ํ™œ์šฉํ•˜์—ฌ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(sequence-to-sequence) ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐ ์ด์šฉ๋ฉ๋‹ˆ๋‹ค. + +์‚ฌ์ „ ํ•™์Šต๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•ด ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ์‹œํ€€์Šค ์ƒ์„ฑ(sequence generation) ์ž‘์—…์— ํšจ๊ณผ์ ์ด๋ผ๋Š” ์ ์ด Sascha Rothe, Shashi Narayan, Aliaksei Severyn์˜ ๋…ผ๋ฌธ [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://huggingface.co/papers/1907.12461)์—์„œ ์ž…์ฆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +[`EncoderDecoderModel`]์ด ํ•™์Šต/๋ฏธ์„ธ ์กฐ์ •๋œ ํ›„์—๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +์ด ์•„ํ‚คํ…์ฒ˜์˜ ํ•œ ๊ฐ€์ง€ ์‘์šฉ ์‚ฌ๋ก€๋Š” ๋‘ ๊ฐœ์˜ ์‚ฌ์ „ ํ•™์Šต๋œ [`BertModel`]์„ ๊ฐ๊ฐ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ ํ™œ์šฉํ•˜์—ฌ ์š”์•ฝ ๋ชจ๋ธ(summarization model)์„ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” Yang Liu์™€ Mirella Lapata์˜ ๋…ผ๋ฌธ [Text Summarization with Pretrained Encoders](https://huggingface.co/papers/1908.08345)์—์„œ ์ œ์‹œ๋œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ชจ๋ธ ์„ค์ •์—์„œ `EncoderDecoderModel`์„ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ[[Randomly initializing `EncoderDecoderModel` from model configurations.]] + +[`EncoderDecoderModel`]์€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ์„ค์ •(config)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” [`BertModel`] ์„ค์ •์„ ์ธ์ฝ”๋”๋กœ, ๊ธฐ๋ณธ [`BertForCausalLM`] ์„ค์ •์„ ๋””์ฝ”๋”๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +```python +>>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel + +>>> config_encoder = BertConfig() +>>> config_decoder = BertConfig() + +>>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder) +>>> model = EncoderDecoderModel(config=config) +``` + +## ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ `EncoderDecoderModel` ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ[[Initialising `EncoderDecoderModel` from a pretrained encoder and a pretrained decoder.]] + +[`EncoderDecoderModel`]์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋””์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BERT์™€ ๊ฐ™์€ ๋ชจ๋“  ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๋™ ์ธ์ฝ”๋”ฉ(auto-encoding) ๋ชจ๋ธ์€ ์ธ์ฝ”๋”๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, GPT2์™€ ๊ฐ™์€ ์ž๊ฐ€ ํšŒ๊ท€(autoregressive) ๋ชจ๋ธ์ด๋‚˜ BART์˜ ๋””์ฝ”๋”์™€ ๊ฐ™์ด ์‚ฌ์ „ ํ•™์Šต๋œ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋””์ฝ”๋” ๋ชจ๋ธ์„ ๋””์ฝ”๋”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋กœ ์„ ํƒํ•œ ์•„ํ‚คํ…์ฒ˜์— ๋”ฐ๋ผ ๊ต์ฐจ ์–ดํ…์…˜(cross-attention) ๋ ˆ์ด์–ด๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ด์šฉํ•ด [`EncoderDecoderModel`]์„ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด, ๋ชจ๋ธ์„ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [the *Warm-starting-encoder-decoder blog post*](https://huggingface.co/blog/warm-starting-encoder-decoder)์— ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์œ„ํ•ด `EncoderDecoderModel` ํด๋ž˜์Šค๋Š” [`EncoderDecoderModel.from_encoder_decoder_pretrained`] ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + + +```python +>>> from transformers import EncoderDecoderModel, BertTokenizer + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased") +``` + +## ๊ธฐ์กด `EncoderDecoderModel` ์ฒดํฌํฌ์ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ ์ถ”๋ก ํ•˜๊ธฐ[[Loading an existing `EncoderDecoderModel` checkpoint and perform inference.]] + +`EncoderDecoderModel` ํด๋ž˜์Šค์˜ ๋ฏธ์„ธ ์กฐ์ •(fine-tuned)๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด, Transformers์˜ ๋‹ค๋ฅธ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ [`EncoderDecoderModel`]์—์„œ ์ œ๊ณตํ•˜๋Š” `from_pretrained(...)`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด [`generate`] ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž๋™ ํšŒ๊ท€(autoregressive) ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ํƒ์š• ๋””์ฝ”๋”ฉ(greedy decoding), ๋น” ์„œ์น˜(beam search), ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง(multinomial sampling) ๋“ฑ ๋‹ค์–‘ํ•œ ๋””์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +```python +>>> from transformers import AutoTokenizer, EncoderDecoderModel + +>>> # ๋ฏธ์„ธ ์กฐ์ •๋œ seq2seq ๋ชจ๋ธ๊ณผ ๋Œ€์‘ํ•˜๋Š” ํ† ํฌ๋‚˜์ด์ € ๊ฐ€์ ธ์˜ค๊ธฐ +>>> model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail") +>>> tokenizer = AutoTokenizer.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail") + +>>> # let's perform inference on a long piece of text +>>> ARTICLE_TO_SUMMARIZE = ( +... "PG&E stated it scheduled the blackouts in response to forecasts for high winds " +... "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were " +... "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow." +... ) +>>> input_ids = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").input_ids + +>>> # ์ž๊ธฐํšŒ๊ท€์ ์œผ๋กœ ์š”์•ฝ ์ƒ์„ฑ (๊ธฐ๋ณธ์ ์œผ๋กœ ๊ทธ๋ฆฌ๋”” ๋””์ฝ”๋”ฉ ์‚ฌ์šฉ) +>>> generated_ids = model.generate(input_ids) +>>> generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] +>>> print(generated_text) +nearly 800 thousand customers were affected by the shutoffs. the aim is to reduce the risk of wildfires. nearly 800, 000 customers were expected to be affected by high winds amid dry conditions. pg & e said it scheduled the blackouts to last through at least midday tomorrow. +``` + +## `TFEncoderDecoderModel`์— Pytorch ์ฒดํฌํฌ์ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[Loading a PyTorch checkpoint into `TFEncoderDecoderModel`.]] + +[`TFEncoderDecoderModel.from_pretrained`] ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ Pytorch ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ ์ดˆ๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ์— `from_pt=True`๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์˜ˆ์™ธ(exception)๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์— ๋Œ€ํ•œ Pytorch ์ฒดํฌํฌ์ธํŠธ๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> # ํŒŒ์ดํ† ์น˜ ์ฒดํฌํฌ์ธํŠธ์—์„œ ๋กœ๋“œํ•˜๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• +>>> from transformers import EncoderDecoderModel, TFEncoderDecoderModel + +>>> _model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert-cnn_dailymail-fp16") + +>>> _model.encoder.save_pretrained("./encoder") +>>> _model.decoder.save_pretrained("./decoder") + +>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained( +... "./encoder", "./decoder", encoder_from_pt=True, decoder_from_pt=True +... ) +>>> # ์ด ๋ถ€๋ถ„์€ ํŠน์ • ๋ชจ๋ธ์˜ ๊ตฌ์ฒด์ ์ธ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๋ณต์‚ฌํ•  ๋•Œ์—๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +>>> model.config = _model.config +``` + +## ํ•™์Šต[[Training]] + +๋ชจ๋ธ์ด ์ƒ์„ฑ๋œ ํ›„์—๋Š” BART, T5 ๋˜๋Š” ๊ธฐํƒ€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋ณด์‹œ๋‹ค์‹œํ”ผ, ์†์‹ค(loss)์„ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ๋‹จ 2๊ฐœ์˜ ์ž…๋ ฅ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: `input_ids`(์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ธ์ฝ”๋”ฉํ•œ `input_ids`)์™€ `labels`(๋ชฉํ‘œ ์‹œํ€€์Šค๋ฅผ ์ธ์ฝ”๋”ฉํ•œ `input_ids`). + +```python +>>> from transformers import BertTokenizer, EncoderDecoderModel + +>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased") +>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased") + +>>> model.config.decoder_start_token_id = tokenizer.cls_token_id +>>> model.config.pad_token_id = tokenizer.pad_token_id + +>>> input_ids = tokenizer( +... "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.", +... return_tensors="pt", +... ).input_ids + +>>> labels = tokenizer( +... "the eiffel tower surpassed the washington monument to become the tallest structure in the world. it was the first structure to reach a height of 300 metres in paris in 1930. it is now taller than the chrysler building by 5. 2 metres ( 17 ft ) and is the second tallest free - standing structure in paris.", +... return_tensors="pt", +... ).input_ids + +>>> # forward ํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ ์ ํ•ฉํ•œ decoder_input_ids๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. +>>> loss = model(input_ids=input_ids, labels=labels).loss +``` +ํ›ˆ๋ จ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [colab](https://colab.research.google.com/drive/1WIk2bxglElfZewOHboPFNj8H44_VAyKE?usp=sharing#scrollTo=ZwQIEhKOrJpl) ๋…ธํŠธ๋ถ์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +์ด ๋ชจ๋ธ์€ [thomwolf](https://github.com/thomwolf)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์œผ๋ฉฐ, ์ด ๋ชจ๋ธ์— ๋Œ€ํ•œ TensorFlow ๋ฐ Flax ๋ฒ„์ „์€ [ydshieh](https://github.com/ydshieh)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. + + +## EncoderDecoderConfig + +[[autodoc]] EncoderDecoderConfig + + + + +## EncoderDecoderModel + +[[autodoc]] EncoderDecoderModel + - forward + - from_encoder_decoder_pretrained + + + + +## TFEncoderDecoderModel + +[[autodoc]] TFEncoderDecoderModel + - call + - from_encoder_decoder_pretrained + + + + +## FlaxEncoderDecoderModel + +[[autodoc]] FlaxEncoderDecoderModel + - __call__ + - from_encoder_decoder_pretrained + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/esm.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/esm.md new file mode 100644 index 0000000000000000000000000000000000000000..6ea1191dabe0bae8306f981b7bffe9dfd8edbd94 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/esm.md @@ -0,0 +1,115 @@ + + +# ESM [[esm]] + +## ๊ฐœ์š” [[overview]] + +์ด ํŽ˜์ด์ง€๋Š” Meta AI์˜ Fundamental AI Research ํŒ€์—์„œ ์ œ๊ณตํ•˜๋Š” Transformer ๋‹จ๋ฐฑ์งˆ ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ฝ”๋“œ์™€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์ตœ์ฒจ๋‹จ์ธ ESMFold์™€ ESM-2, ๊ทธ๋ฆฌ๊ณ  ์ด์ „์— ๊ณต๊ฐœ๋œ ESM-1b์™€ ESM-1v๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. Transformer ๋‹จ๋ฐฑ์งˆ ์–ธ์–ด ๋ชจ๋ธ์€ Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, Rob Fergus์˜ ๋…ผ๋ฌธ [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „์€ 2019๋…„์— [์ถœํŒ ์ „ ๋…ผ๋ฌธ](https://www.biorxiv.org/content/10.1101/622803v1?versioned=true) ํ˜•ํƒœ๋กœ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ESM-2๋Š” ๋‹ค์–‘ํ•œ ๊ตฌ์กฐ ์˜ˆ์ธก ์ž‘์—…์—์„œ ํ…Œ์ŠคํŠธ๋œ ๋ชจ๋“  ๋‹จ์ผ ์‹œํ€€์Šค ๋‹จ๋ฐฑ์งˆ ์–ธ์–ด ๋ชจ๋ธ์„ ๋Šฅ๊ฐ€ํ•˜๋ฉฐ, ์›์ž ์ˆ˜์ค€์˜ ๊ตฌ์กฐ ์˜ˆ์ธก์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives์˜ ๋…ผ๋ฌธ [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902)์—์„œ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋…ผ๋ฌธ์—์„œ ํ•จ๊ป˜ ์†Œ๊ฐœ๋œ ESMFold๋Š” ESM-2 ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ตœ์ฒจ๋‹จ์˜ ์ •ํ™•๋„๋กœ ๋‹จ๋ฐฑ์งˆ ์ ‘ํž˜ ๊ตฌ์กฐ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š” ํ—ค๋“œ๋ฅผ ๊ฐ–์ถ”๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. [AlphaFold2](https://www.nature.com/articles/s41586-021-03819-2)์™€ ๋‹ฌ๋ฆฌ, ์ด๋Š” ๋Œ€ํ˜• ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋‹จ๋ฐฑ์งˆ ์–ธ์–ด ๋ชจ๋ธ ์Šคํ…œ์˜ ํ† ํฐ ์ž„๋ฒ ๋”ฉ์— ์˜์กดํ•˜๋ฉฐ, ์ถ”๋ก  ์‹œ ๋‹ค์ค‘ ์‹œํ€€์Šค ์ •๋ ฌ(MSA) ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ESMFold ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์™„์ „ํžˆ "๋…๋ฆฝ์ "์ด๋ฉฐ, ์˜ˆ์ธก์„ ์œ„ํ•ด ์•Œ๋ ค์ง„ ๋‹จ๋ฐฑ์งˆ ์‹œํ€€์Šค์™€ ๊ตฌ์กฐ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๊ทธ๋ฆฌ๊ณ  ๊ทธ์™€ ๊ด€๋ จ ์™ธ๋ถ€ ์ฟผ๋ฆฌ ๋„๊ตฌ๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒฐ๊ณผ, ํ›จ์”ฌ ๋น ๋ฆ…๋‹ˆ๋‹ค. + +"Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences"์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ธ๊ณต์ง€๋Šฅ ๋ถ„์•ผ์—์„œ๋Š” ๋Œ€๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ์™€ ๋ชจ๋ธ ์šฉ๋Ÿ‰์„ ๊ฐ–์ถ˜ ๋น„์ง€๋„ ํ•™์Šต์˜ ์กฐํ•ฉ์ด ํ‘œํ˜„ ํ•™์Šต๊ณผ ํ†ต๊ณ„์  ์ƒ์„ฑ์—์„œ ์ฃผ์š”ํ•œ ๋ฐœ์ „์„ ์ด๋Œ์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ์ƒ๋ช… ๊ณผํ•™์—์„œ๋Š” ์‹œํ€€์‹ฑ ๊ธฐ์ˆ ์˜ ์„ฑ์žฅ์ด ์˜ˆ์ƒ๋˜๋ฉฐ, ์ž์—ฐ ์‹œํ€€์Šค ๋‹ค์–‘์„ฑ์— ๋Œ€ํ•œ ์ „๋ก€ ์—†๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋ฉ๋‹ˆ๋‹ค. ์ง„ํ™”์  ๋‹จ๊ณ„์—์„œ ๋ณผ ๋•Œ, ๋‹จ๋ฐฑ์งˆ ์–ธ์–ด ๋ชจ๋ธ๋ง์€ ์ƒ๋ฌผํ•™์„ ์œ„ํ•œ ์˜ˆ์ธก ๋ฐ ์ƒ์„ฑ ์ธ๊ณต์ง€๋Šฅ์„ ํ–ฅํ•œ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ๊ณ„์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” ์ง„ํ™”์  ๋‹ค์–‘์„ฑ์„ ์•„์šฐ๋ฅด๋Š” 2์–ต 5์ฒœ๋งŒ ๊ฐœ์˜ ๋‹จ๋ฐฑ์งˆ ์‹œํ€€์Šค์—์„œ ์ถ”์ถœํ•œ 860์–ต ๊ฐœ์˜ ์•„๋ฏธ๋…ธ์‚ฐ์— ๋Œ€ํ•ด ์‹ฌ์ธต ์ปจํ…์ŠคํŠธ ์–ธ์–ด ๋ชจ๋ธ์„ ๋น„์ง€๋„ ํ•™์Šต์œผ๋กœ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋ชจ๋ธ์€ ๊ทธ ํ‘œํ˜„์—์„œ ์ƒ๋ฌผํ•™์  ์†์„ฑ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด ํ‘œํ˜„์€ ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๋งŒ์œผ๋กœ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋œ ํ‘œํ˜„ ๊ณต๊ฐ„์€ ์•„๋ฏธ๋…ธ์‚ฐ์˜ ์ƒํ™”ํ•™์  ํŠน์„ฑ ์ˆ˜์ค€์—์„œ๋ถ€ํ„ฐ ๋‹จ๋ฐฑ์งˆ์˜ ์›๊ฑฐ๋ฆฌ ์ƒ๋™์„ฑ๊นŒ์ง€ ๊ตฌ์กฐ๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ๋‹ค์ค‘ ๊ทœ๋ชจ์˜ ์กฐ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ‘œํ˜„์—๋Š” 2์ฐจ ๋ฐ 3์ฐจ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ธ์ฝ”๋”ฉ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์„ ํ˜• ์ „์‚ฌ์— ์˜ํ•ด ์‹๋ณ„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œํ˜„ ํ•™์Šต์€ ๋Œ์—ฐ๋ณ€์ด์— ์˜ํ•œ ํšจ๊ณผ์™€ 2์ฐจ ๊ตฌ์กฐ์˜ ์ตœ์ฒจ๋‹จ ์ง€๋„ ์˜ˆ์ธก์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ณ , ๋„“์€ ๋ฒ”์œ„์˜ ์ ‘์ด‰ ๋ถ€์œ„ ์˜ˆ์ธก์„ ์œ„ํ•œ ์ตœ์ฒจ๋‹จ ํŠน์ง•์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.* + +"Language models of protein sequences at the scale of evolution enable accurate structure prediction"์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋Œ€ํ˜• ์–ธ์–ด ๋ชจ๋ธ์€ ์ตœ๊ทผ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ๊ธด๊ธ‰ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•˜์—ฌ ๋‹จ์ˆœํ•œ ํŒจํ„ด ๋งค์นญ์„ ๋„˜์–ด ๋” ๋†’์€ ์ˆ˜์ค€์˜ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ƒ์ƒํ•œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋” ์ž‘์€ ๊ทœ๋ชจ์—์„œ ํ›ˆ๋ จ๋œ ๋‹จ๋ฐฑ์งˆ ์‹œํ€€์Šค์˜ ์–ธ์–ด ๋ชจ๋ธ์ด ์—ฐ๊ตฌ๋˜์—ˆ์ง€๋งŒ, ๊ทธ๋“ค์ด ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ์ƒ๋ฌผํ•™์— ๋Œ€ํ•ด ๋ฌด์—‡์„ ๋ฐฐ์šฐ๋Š”์ง€๋Š” ๊ฑฐ์˜ ์•Œ๋ ค์ ธ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์—ฐ๊ตฌ์—์„œ ์šฐ๋ฆฌ๋Š” ํ˜„์žฌ๊นŒ์ง€ ํ‰๊ฐ€๋œ ๊ฐ€์žฅ ํฐ 150์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์ด ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ๋‹จ์ผ ์•„๋ฏธ๋…ธ์‚ฐ์˜ ํ•ด์ƒ๋„๋กœ ๋‹จ๋ฐฑ์งˆ์˜ 3์ฐจ์› ๊ตฌ์กฐ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ํ•™์Šตํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐœ๋ณ„ ๋‹จ๋ฐฑ์งˆ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๊ณ ์ •๋ฐ€ ์›์ž ์ˆ˜์ค€์˜ ์—”๋“œ-ํˆฌ-์—”๋“œ ๊ตฌ์กฐ ์˜ˆ์ธก์„ ํ•˜๊ธฐ ์œ„ํ•œ ESMFold๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ESMFold๋Š” ์–ธ์–ด ๋ชจ๋ธ์— ์ž˜ ์ดํ•ด๋˜๋Š” ๋‚ฎ์€ ํผํ”Œ๋ ‰์„œํ‹ฐ๋ฅผ ๊ฐ€์ง„ ์‹œํ€€์Šค์— ๋Œ€ํ•ด AlphaFold2์™€ RoseTTAFold์™€ ์œ ์‚ฌํ•œ ์ •ํ™•๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ESMFold์˜ ์ถ”๋ก ์€ AlphaFold2๋ณด๋‹ค ํ•œ ์ž๋ฆฟ์ˆ˜ ๋น ๋ฅด๋ฉฐ, ๋ฉ”ํƒ€๊ฒŒ๋†ˆ ๋‹จ๋ฐฑ์งˆ์˜ ๊ตฌ์กฐ์  ๊ณต๊ฐ„์„ ์‹ค์šฉ์ ์ธ ์‹œ๊ฐ„ ๋‚ด์— ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.* + +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/facebookresearch/esm)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, Meta AI์˜ Fundamental AI Research ํŒ€์—์„œ ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ESM-1b, ESM-1v, ESM-2๋Š” [jasonliu](https://huggingface.co/jasonliu)์™€ [Matt](https://huggingface.co/Rocketknight1)์— ์˜ํ•ด HuggingFace์— ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ESMFold๋Š” [Matt](https://huggingface.co/Rocketknight1)์™€ [Sylvain](https://huggingface.co/sgugger)์— ์˜ํ•ด HuggingFace์— ๊ธฐ์—ฌ๋˜์—ˆ์œผ๋ฉฐ, ์ด ๊ณผ์ •์—์„œ ๋งŽ์€ ๋„์›€์„ ์ค€ Nikita Smetanin, Roshan Rao, Tom Sercu์—๊ฒŒ ํฐ ๊ฐ์‚ฌ๋ฅผ ๋“œ๋ฆฝ๋‹ˆ๋‹ค! + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- ESM ๋ชจ๋ธ์€ ๋งˆ์Šคํฌ๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง(MLM) ๋ชฉํ‘œ๋กœ ํ›ˆ๋ จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- HuggingFace์˜ ESMFold ํฌํŠธ๋Š” [openfold](https://github.com/aqlaboratory/openfold) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ผ๋ถ€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. `openfold` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Apache License 2.0์— ๋”ฐ๋ผ ๋ผ์ด์„ ์Šค๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification) +- [๋งˆ์Šคํ‚น๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) + +## EsmConfig [[transformers.EsmConfig]] + +[[autodoc]] EsmConfig + - all + +## EsmTokenizer [[transformers.EsmTokenizer]] + +[[autodoc]] EsmTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + + + + +## EsmModel [[transformers.EsmModel]] + +[[autodoc]] EsmModel + - forward + +## EsmForMaskedLM [[transformers.EsmForMaskedLM]] + +[[autodoc]] EsmForMaskedLM + - forward + +## EsmForSequenceClassification [[transformers.EsmForSequenceClassification]] + +[[autodoc]] EsmForSequenceClassification + - forward + +## EsmForTokenClassification [[transformers.EsmForTokenClassification]] + +[[autodoc]] EsmForTokenClassification + - forward + +## EsmForProteinFolding [[transformers.EsmForProteinFolding]] + +[[autodoc]] EsmForProteinFolding + - forward + + + + +## TFEsmModel [[transformers.TFEsmModel]] + +[[autodoc]] TFEsmModel + - call + +## TFEsmForMaskedLM [[transformers.TFEsmForMaskedLM]] + +[[autodoc]] TFEsmForMaskedLM + - call + +## TFEsmForSequenceClassification [[transformers.TFEsmForSequenceClassification]] + +[[autodoc]] TFEsmForSequenceClassification + - call + +## TFEsmForTokenClassification [[transformers.TFEsmForTokenClassification]] + +[[autodoc]] TFEsmForTokenClassification + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/exaone4.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/exaone4.md new file mode 100644 index 0000000000000000000000000000000000000000..65899b52f71a81972daa92c464d09d2b2c3f627d --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/exaone4.md @@ -0,0 +1,207 @@ + + +# EXAONE 4 + +## ๊ฐœ์š” + +**[EXAONE 4.0](https://github.com/LG-AI-EXAONE/EXAONE-4.0)** ๋ชจ๋ธ๊ตฐ์€ [EXAONE 3.5](https://github.com/LG-AI-EXAONE/EXAONE-3.5) ๋ชจ๋ธ๊ตฐ์˜ ๋†’์€ ์‹ค์šฉ์„ฑ๊ณผ [EXAONE Deep](https://github.com/LG-AI-EXAONE/EXAONE-Deep) ๋ชจ๋ธ๊ตฐ์˜ ํ–ฅ์ƒ๋œ ์‚ฌ๊ณ  ์ถ”๋ก  ๋Šฅ๋ ฅ์„ ๊ฐ๊ฐ Non-reasoning mode์™€ Reasoning mode๋กœ ํ†ตํ•ฉํ•œ ์ž์—ฐ์–ด ๋ชจ๋ธ(language model)์ž…๋‹ˆ๋‹ค. ์—์ด์ „ํ‹ฑ(agentic) AI ์‹œ๋Œ€์— ๋ฐœ๋งž์ถฐ EXAONE 4.0์€ ์—์ด์ „ํ‹ฑ ๋„๊ตฌ ์‚ฌ์šฉ ๋Šฅ๋ ฅ๊ณผ ๊ฐ™์€ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ํ†ตํ•ฉํ–ˆ๊ณ , ๊ธฐ์กด์˜ ๋‹ค๊ตญ์–ด ๋Šฅ๋ ฅ์„ ์˜์–ด, ํ•œ๊ตญ์–ด์™€ ๋”๋ถˆ์–ด ์ŠคํŽ˜์ธ์–ด๊นŒ์ง€ ํ™•์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. + +EXAONE 4.0 ๋ชจ๋ธ๊ตฐ์€ ๋‘ ๊ฐœ์˜ ๋ชจ๋ธ: ๋†’์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์ตœ์ ํ™”๋œ 32B ์ค‘ํ˜• ๋ชจ๋ธ, ๊ทธ๋ฆฌ๊ณ  ์˜จ-๋””๋ฐ”์ด์Šค ํ™œ์šฉ์„ ์œ„ํ•ด ๋””์ž์ธ๋œ 1.2B ์†Œํ˜• ๋ชจ๋ธ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +EXAONE 4.0์˜ ๋ชจ๋ธ ๊ตฌ์กฐ๋Š” ์ด์ „ EXAONE ๋ชจ๋ธ๋“ค๊ณผ ๋‹ค๋ฅธ ์•„ํ‚คํ…์ฒ˜ ๋””์ž์ธ์„ ์ฑ„ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. + +1. **Hybrid Attention**: 32B ๋ชจ๋ธ์€ *Local attention (sliding window attention)*๊ณผ *Global attention (full attention)*์„ 3:1 ๋น„์œจ๋กœ ์—ฐ๊ฒฐํ•œ hybrid attention ๊ตฌ์กฐ๋ฅผ ์ฑ„ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ „์ฒด ๋ฌธ๋งฅ์„ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก global attention์—์„œ RoPE๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. +2. **QK-Reorder-Norm**: ๋” ๋‚˜์€ downstream tasks ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์—ฐ์‚ฐ๋Ÿ‰์˜ ์ฆ๊ฐ€๋ฅผ ๊ฐ์ˆ˜ํ•˜๋ฉฐ ์ „ํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋˜ Pre-LN ๋ฐฉ์‹์„ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. LayerNorm์˜ ์œ„์น˜๋ฅผ attention๊ณผ MLP์˜ ์ถœ๋ ฅ์— ์ ์šฉ๋˜๋„๋ก ์žฌ๋ฐฐ์น˜ํ–ˆ๊ณ , Q์™€ K projection ์งํ›„์—๋„ RMS normalization์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. + +๋” ์ž์„ธํ•œ ์ •๋ณด๋Š” [๊ธฐ์ˆ  ๋ณด๊ณ ์„œ](https://arxiv.org/abs/2507.11407), [HuggingFace ๋…ผ๋ฌธ](https://huggingface.co/papers/2507.11407), [๋ธ”๋กœ๊ทธ](https://www.lgresearch.ai/blog/view?seq=576), [๊ณต์‹ GitHub](https://github.com/LG-AI-EXAONE/EXAONE-4.0) ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +๊ณต๊ฐœ๋œ ๋ชจ๋“  ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋Š” [HuggingFace ์ฝœ๋ ‰์…˜](https://huggingface.co/collections/LGAI-EXAONE/exaone-40-686b2e0069800c835ed48375)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด + +| Model Configuration | 32B | 1.2B | +|:-------------------|:-----:|:------:| +| d_model | 5,120 | 2,048 | +| Number of layers | 64 | 30 | +| Normalization | QK-Reorder-LN | QK-Reorder-LN | +| Non-linearity | SwiGLU | SwiGLU | +| Feedforward dimension | 27,392 | 4,096 | +| Attention type | Hybrid (3:1 Local-Global) | Global | +| Head type | GQA | GQA | +| Number of heads | 40 | 32 | +| Number of KV heads | 8 | 8 | +| Head size | 128 | 64 | +| Max sequence length | 131,072 | 65,536 | +| RoPE theta | 1,000,000 | 1,000,000 | +| Tokenizer | BBPE | BBPE | +| Vocab size | 102,400 | 102,400 | +| Tied word embedding | False | True | +| Knowledge cut-off | Nov. 2024 | Nov. 2024 | + + +## ์‚ฌ์šฉ ํŒ + +### Non-reasoning mode + +์ผ๋ฐ˜์ ์ธ ๋Œ€ํ™”์˜ ๊ฒฝ์šฐ ์•„๋ž˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด EXAONE 4.0์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_name = "LGAI-EXAONE/EXAONE-4.0-32B" + +model = AutoModelForCausalLM.from_pretrained( + model_name, + torch_dtype="bfloat16", + device_map="auto" +) +tokenizer = AutoTokenizer.from_pretrained(model_name) + +# ์›ํ•˜๋Š” ์ž…๋ ฅ์„ ์„ ํƒํ•˜์„ธ์š” +prompt = "Explain how wonderful you are" +prompt = "Explica lo increรญble que eres" +prompt = "๋„ˆ๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋Œ€๋‹จํ•œ์ง€ ์„ค๋ช…ํ•ด ๋ด" + +messages = [ + {"role": "user", "content": prompt} +] +input_ids = tokenizer.apply_chat_template( + messages, + tokenize=True, + add_generation_prompt=True, + return_tensors="pt" +) + +output = model.generate( + input_ids.to(model.device), + max_new_tokens=128, + do_sample=False, +) +print(tokenizer.decode(output[0])) +``` + +### Reasoning mode + +The EXAONE 4.0 models have reasoning capabilities for handling complex problems. You can activate reasoning mode by using the `enable_thinking=True` argument with the tokenizer, which opens a reasoning block that starts with `` tag without closing it. + +EXAONE 4.0 ๋ชจ๋ธ๊ตฐ์€ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์‚ฌ๊ณ  ์ถ”๋ก  ๋Šฅ๋ ฅ์„ ๊ฐ–์ถ”๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ๋‚˜์ด์ €์—์„œ `enable_thinking=True` ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ reasoning mode๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ `` ํ† ํฐ์œผ๋กœ ์ถ”๋ก  ๋ธ”๋ก์„ ์—ฐ ๋’ค, ๋‹ซ์ง€ ์•Š๊ณ  ์ถ”๋ก ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. + +```python +messages = [ + {"role": "user", "content": "Which one is bigger, 3.12 vs 3.9?"} +] +input_ids = tokenizer.apply_chat_template( + messages, + tokenize=True, + add_generation_prompt=True, + return_tensors="pt", + enable_thinking=True, +) + +output = model.generate( + input_ids.to(model.device), + max_new_tokens=128, + do_sample=True, + temperature=0.6, + top_p=0.95 +) +print(tokenizer.decode(output[0])) +``` + +> [!IMPORTANT] +> ๋ชจ๋ธ์„ reasoning mode๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ƒ์„ฑ๋˜๋Š” ๋‹ต๋ณ€์ด sampling parameters์— ๊ต‰์žฅํžˆ ๋ฏผ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋” ๋‚˜์€ ์ƒ์„ฑ ํ’ˆ์งˆ์„ ์œ„ํ•ด ๊ณต์‹ [Usage Guideline](https://github.com/LG-AI-EXAONE/EXAONE-4.0#usage-guideline)๋ฅผ ์ฐธ์กฐํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. + +### Agentic tool use + +EXAONE 4.0 ๋ชจ๋ธ์€ ๋„๊ตฌ ์‚ฌ์šฉ ๋Šฅ๋ ฅ์„ ๊ฐ–์ถ˜ ๋•๋ถ„์— Agent๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด ๋„๊ตฌ ๋ช…์„ธ๋ฅผ ๋ชจ๋ธ์—๊ฒŒ ์ œ๊ณตํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +import random + +def roll_dice(max_num: int): + return random.randint(1, max_num) + +tools = [ + { + "type": "function", + "function": { + "name": "roll_dice", + "description": "Roll a dice with the number 1 to N. User can select the number N.", + "parameters": { + "type": "object", + "required": ["max_num"], + "properties": { + "max_num": { + "type": "int", + "description": "Max number of the dice" + } + } + } + } + } +] + +messages = [ + {"role": "user", "content": "Roll D6 dice twice!"} +] +input_ids = tokenizer.apply_chat_template( + messages, + tokenize=True, + add_generation_prompt=True, + return_tensors="pt", + tools=tools, +) + +output = model.generate( + input_ids.to(model.device), + max_new_tokens=1024, + do_sample=True, + temperature=0.6, + top_p=0.95, +) +print(tokenizer.decode(output[0])) +``` + +## Exaone4Config + +[[autodoc]] Exaone4Config + +## Exaone4Model + +[[autodoc]] Exaone4Model + - forward + +## Exaone4ForCausalLM + +[[autodoc]] Exaone4ForCausalLM + - forward + +## Exaone4ForSequenceClassification + +[[autodoc]] Exaone4ForSequenceClassification + - forward + +## Exaone4ForTokenClassification + +[[autodoc]] Exaone4ForTokenClassification + - forward + +## Exaone4ForQuestionAnswering + +[[autodoc]] Exaone4ForQuestionAnswering + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma.md new file mode 100644 index 0000000000000000000000000000000000000000..25fe6f1c772950ec5bab3d0f3a9850666d90b94f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma.md @@ -0,0 +1,76 @@ + + +# Gemma [[gemma]] + +## ๊ฐœ์š” [[overview]] + +Gemma ๋ชจ๋ธ์€ Google์˜ Gemma ํŒ€์ด ์ž‘์„ฑํ•œ [Gemma: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/gemma-open-models/)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +Gemma ๋ชจ๋ธ์€ 6์กฐ ํ† ํฐ์œผ๋กœ ํ•™์Šต๋˜์—ˆ์œผ๋ฉฐ, 2b์™€ 7b์˜ ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์œผ๋กœ ์ถœ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ด ์—ฐ๊ตฌ๋Š” ์–ธ์–ด ์ดํ•ด, ์ถ”๋ก  ๋ฐ ์•ˆ์ „์„ฑ์— ๋Œ€ํ•œ ํ•™์ˆ  ๋ฒค์น˜๋งˆํฌ์—์„œ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ์ƒˆ๋กœ์šด ์˜คํ”ˆ ์–ธ์–ด ๋ชจ๋ธ ๊ณ„์—ด์ธ Gemma๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ํฌ๊ธฐ(20์–ต ๋ฐ 70์–ต ๋งค๊ฐœ๋ณ€์ˆ˜)์˜ ๋ชจ๋ธ์„ ์ถœ์‹œํ•˜๋ฉฐ, ์‚ฌ์ „ ํ•™์Šต๋œ ์ฒดํฌํฌ์ธํŠธ์™€ ๋ฏธ์„ธ ์กฐ์ •๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ชจ๋‘ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Gemma๋Š” 18๊ฐœ์˜ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜ ์ž‘์—… ์ค‘ 11๊ฐœ์—์„œ ์œ ์‚ฌํ•œ ํฌ๊ธฐ์˜ ์˜คํ”ˆ ๋ชจ๋ธ์„ ๋Šฅ๊ฐ€ํ•˜๋ฉฐ, ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ ๊ฐœ๋ฐœ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…๊ณผ ํ•จ๊ป˜ ์•ˆ์ „์„ฑ๊ณผ ์ฑ…์ž„ ์ธก๋ฉด์— ๋Œ€ํ•œ ์ข…ํ•ฉ์ ์ธ ํ‰๊ฐ€๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” LLM์˜ ์ฑ…์ž„๊ฐ ์žˆ๋Š” ๊ณต๊ฐœ๊ฐ€ ์ตœ์ฒจ๋‹จ ๋ชจ๋ธ์˜ ์•ˆ์ „์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ  ๋‹ค์Œ ์„ธ๋Œ€์˜ LLM ํ˜์‹ ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋ฏฟ์Šต๋‹ˆ๋‹ค.* + +ํŒ: + +- ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋Š” ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ `src/transformers/models/gemma/convert_gemma_weights_to_hf.py`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [Arthur Zucker](https://huggingface.co/ArthurZ), [Younes Belkada](https://huggingface.co/ybelkada), [Sanchit Gandhi](https://huggingface.co/sanchit-gandhi), [Pedro Cuenca](https://huggingface.co/pcuenq)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. + +## GemmaConfig [[transformers.GemmaConfig]] + +[[autodoc]] GemmaConfig + +## GemmaTokenizer [[transformers.GemmaTokenizer]] + +[[autodoc]] GemmaTokenizer + + +## GemmaTokenizerFast [[transformers.GemmaTokenizerFast]] + +[[autodoc]] GemmaTokenizerFast + +## GemmaModel [[transformers.GemmaModel]] + +[[autodoc]] GemmaModel + - forward + +## GemmaForCausalLM [[transformers.GemmaForCausalLM]] + +[[autodoc]] GemmaForCausalLM + - forward + +## GemmaForSequenceClassification [[transformers.GemmaForSequenceClassification]] + +[[autodoc]] GemmaForSequenceClassification + - forward + +## GemmaForTokenClassification [[transformers.GemmaForTokenClassification]] + +[[autodoc]] GemmaForTokenClassification + - forward + +## FlaxGemmaModel [[transformers.FlaxGemmaModel]] + +[[autodoc]] FlaxGemmaModel + - __call__ + +## FlaxGemmaForCausalLM [[transformers.FlaxGemmaForCausalLM]] + +[[autodoc]] FlaxGemmaForCausalLM + - __call__ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma2.md new file mode 100644 index 0000000000000000000000000000000000000000..6bffec616c6e97d853a8e4d0b8f279bf1033d668 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma2.md @@ -0,0 +1,63 @@ + + + +# Gemma2 [[gemma2]] + +## ๊ฐœ์š” [[overview]] + +Gemma2 ๋ชจ๋ธ์€ Google์˜ Gemma2 ํŒ€์ด ์ž‘์„ฑํ•œ [Gemma2: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/google-gemma-2/)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +ํŒŒ๋ผ๋ฏธํ„ฐ ํฌ๊ธฐ๊ฐ€ ๊ฐ๊ฐ 90์–ต(9B)๊ณผ 270์–ต(27B)์ธ ๋‘ ๊ฐ€์ง€ Gemma2 ๋ชจ๋ธ์ด ์ถœ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ด์ œ ์šฐ๋ฆฌ๋Š” ์ „ ์„ธ๊ณ„์˜ ์—ฐ๊ตฌ์ž์™€ ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ Gemma 2๋ฅผ ๊ณต์‹์ ์œผ๋กœ ์ถœ์‹œํ•ฉ๋‹ˆ๋‹ค. 90์–ต(9B)๊ณผ 270์–ต(27B) ํŒŒ๋ผ๋ฏธํ„ฐ ํฌ๊ธฐ๋กœ ์ œ๊ณต๋˜๋Š” Gemma 2๋Š” 1์„ธ๋Œ€๋ณด๋‹ค ๋” ๋†’์€ ์„ฑ๋Šฅ๊ณผ ์ถ”๋ก  ํšจ์œจ์„ฑ์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ƒ๋‹นํ•œ ์•ˆ์ „์„ฑ ํ–ฅ์ƒ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค 270์–ต ๊ทœ๋ชจ์˜ ๋ชจ๋ธ์€ ํฌ๊ธฐ๊ฐ€ ๋‘ ๋ฐฐ ์ด์ƒ์ธ ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•ด๋„ ๊ฒฝ์Ÿ๋ ฅ ์žˆ๋Š” ๋Œ€์•ˆ์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋Š” ์ž‘๋…„ 12์›”๊นŒ์ง€๋งŒ ํ•ด๋„ ๋…์  ๋ชจ๋ธ์—์„œ๋งŒ ๊ฐ€๋Šฅํ–ˆ๋˜ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.* + +ํŒ: + +- ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋Š” ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ `src/transformers/models/Gemma2/convert_Gemma2_weights_to_hf.py`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +- Gemma2๋Š” ๋งค ๋‘ ๋ฒˆ์งธ ๋ ˆ์ด์–ด๋งˆ๋‹ค ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ [`~DynamicCache`] ๋˜๋Š” ํ…์„œ์˜ ํŠœํ”Œ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ kv ์บ์‹ฑ์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Gemma2์˜ forward ํ˜ธ์ถœ์—์„œ ์บ์‹ฑ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`~HybridCache`] ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ด๋ฅผ `past_key_values`๋กœ forward ํ˜ธ์ถœ์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `past_key_values`์— ์ด๋ฏธ ์ด์ „์˜ ํ‚ค์™€ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด `cache_position`๋„ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +์ด ๋ชจ๋ธ์€ [Arthur Zucker](https://huggingface.co/ArthurZ), [Pedro Cuenca](https://huggingface.co/pcuenq), [Tom Arsen]()์ด ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. + +## Gemma2Config [[transformers.Gemma2Config]] + +[[autodoc]] Gemma2Config + +## Gemma2Model [[transformers.Gemma2Model]] + +[[autodoc]] Gemma2Model + - forward + +## Gemma2ForCausalLM [[transformers.Gemma2ForCausalLM]] + +[[autodoc]] Gemma2ForCausalLM + - forward + +## Gemma2ForSequenceClassification [[transformers.Gemma2ForSequenceClassification]] + +[[autodoc]] Gemma2ForSequenceClassification + - forward + +## Gemma2ForTokenClassification [[transformers.Gemma2ForTokenClassification]] + +[[autodoc]] Gemma2ForTokenClassification + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma3.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma3.md new file mode 100644 index 0000000000000000000000000000000000000000..a0942ceb96e819836c35a0bbced7b28603a1a822 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gemma3.md @@ -0,0 +1,274 @@ + + + +
+
+ PyTorch + SDPA +
+
+ +# Gemma 3 [[gemma3]] + +[Gemma 3](https://goo.gle/Gemma3Report)๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฒ„์ „๊ณผ ์ง€์‹œ๋ฌธ ์กฐ์ • ๋ฒ„์ „์„ ๊ฐ–์ถ˜ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ๋กœ, 1B, 13B, 27B ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์•„ํ‚คํ…์ฒ˜๋Š” ์ด์ „ Gemma ๋ฒ„์ „๊ณผ ๋Œ€๋ถ€๋ถ„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ๋ชจ๋“  ๊ธ€๋กœ๋ฒŒ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋งˆ๋‹ค 5๊ฐœ์˜ ๋กœ์ปฌ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์…€ํ”„ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋ฅผ ๋ฒˆ๊ฐˆ์•„ ์‚ฌ์šฉํ•˜๋Š” ์ , 128K ํ† ํฐ์˜ ๋” ๊ธด ์ปจํ…์ŠคํŠธ ๊ธธ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ์ , ๊ทธ๋ฆฌ๊ณ  ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋‚˜ ์ •์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ์ข…ํšก๋น„์˜ ์ด๋ฏธ์ง€์—์„œ ์ •๋ณด๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ "ํŒจ๋‹ ๋ฐ ์Šค์บ๋‹"ํ•  ์ˆ˜ ์žˆ๋Š” [SigLip](./siglip) ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. + +์ง€์‹œ๋ฌธ ์กฐ์ • ๋ฒ„์ „์€ ์ง€์‹ ์ฆ๋ฅ˜ ๋ฐ ๊ฐ•ํ™” ํ•™์Šต์œผ๋กœ ํ›„์† ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +Gemma 3์˜ ๋ชจ๋“  ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋Š” [Gemma 3](https://huggingface.co/collections/google/gemma-3-release-67c6c6f89c4f76621268bb6d) ๋ฆด๋ฆฌ์Šค์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!ํŒ] +> Gemma๋ฅผ ๋‹ค์–‘ํ•œ ๋น„์ „ ๋ฐ ์–ธ์–ด ์ž‘์—…์— ์ ์šฉํ•˜๋Š” ์ถ”๊ฐ€ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ ค๋ฉด ์˜ค๋ฅธ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์˜ Gemma 3 ๋ชจ๋ธ์„ ํด๋ฆญํ•˜์„ธ์š”. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” [`Pipeline`] ๋˜๋Š” [`AutoModel`] ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + + + +```py +import torch +from transformers import pipeline + +pipeline = pipeline( + task="image-text-to-text", + model="google/gemma-3-4b-pt", + device=0, + torch_dtype=torch.bfloat16 +) +pipeline( + "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg", + text=" What is shown in this image?" +) +``` + + + + +```py +import torch +from transformers import AutoProcessor, Gemma3ForConditionalGeneration + +model = Gemma3ForConditionalGeneration.from_pretrained( + "google/gemma-3-4b-it", + torch_dtype=torch.bfloat16, + device_map="auto", + attn_implementation="sdpa" +) +processor = AutoProcessor.from_pretrained( + "google/gemma-3-4b-it", + padding_side="left" +) + +messages = [ + { + "role": "system", + "content": [ + {"type": "text", "text": "You are a helpful assistant."} + ] + }, + { + "role": "user", "content": [ + {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"}, + {"type": "text", "text": "What is shown in this image?"}, + ] + }, +] +inputs = processor.apply_chat_template( + messages, + tokenize=True, + return_dict=True, + return_tensors="pt", + add_generation_prompt=True, +).to(model.device) + +output = model.generate(**inputs, max_new_tokens=50, cache_implementation="static") +print(processor.decode(output[0], skip_special_tokens=True)) +``` + + + + +```bash +echo -e "Plants create energy through a process known as" | transformers run --task text-generation --model google/gemma-3-1b-pt --device 0 +``` + + + + +์–‘์žํ™”๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ํ‘œํ˜„ํ•˜์—ฌ, ํฐ ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์–‘์žํ™” ๋ฐฑ์—”๋“œ์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์–‘์žํ™”](../quantization/overview) ๊ฐœ์š”๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” [torchao](../quantization/torchao)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ int4๋กœ๋งŒ ์–‘์žํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```py +# pip install torchao +import torch +from transformers import TorchAoConfig, Gemma3ForConditionalGeneration, AutoProcessor + +quantization_config = TorchAoConfig("int4_weight_only", group_size=128) +model = Gemma3ForConditionalGeneration.from_pretrained( + "google/gemma-3-27b-it", + torch_dtype=torch.bfloat16, + device_map="auto", + quantization_config=quantization_config +) +processor = AutoProcessor.from_pretrained( + "google/gemma-3-27b-it", + padding_side="left" +) + +messages = [ + { + "role": "system", + "content": [ + {"type": "text", "text": "You are a helpful assistant."} + ] + }, + { + "role": "user", "content": [ + {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"}, + {"type": "text", "text": "What is shown in this image?"}, + ] + }, +] +inputs = processor.apply_chat_template( + messages, + tokenize=True, + return_dict=True, + return_tensors="pt", + add_generation_prompt=True, +).to(model.device) + +output = model.generate(**inputs, max_new_tokens=50, cache_implementation="static") +print(processor.decode(output[0], skip_special_tokens=True)) +``` + +[AttentionMaskVisualizer](https://github.com/huggingface/transformers/blob/beb9b5b02246b9b7ee81ddf938f93f44cfeaad19/src/transformers/utils/attention_visualizer.py#L139)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์ด ์ฃผ๋ชฉํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฐ๊ณผ ์ฃผ๋ชฉํ•  ์ˆ˜ ์—†๋Š” ํ† ํฐ์„ ๋” ์ž˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers.utils.attention_visualizer import AttentionMaskVisualizer + +visualizer = AttentionMaskVisualizer("google/gemma-3-4b-it") +visualizer("What is shown in this image?") +``` + +
+ +
+ +## ๋…ธํŠธ [[notes]] + +- ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ๋ฐ ์ด๋ฏธ์ง€ ์ „์šฉ ์ž…๋ ฅ์—๋Š” [`Gemma3ForConditionalGeneration`]์„ ์‚ฌ์šฉํ•˜์„ธ์š”. +- Gemma 3๋Š” ๋‹ค์ค‘ ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ, ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ์ด๋ฏธ์ง€๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฐฐ์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ๊ฐ ๋ฐฐ์น˜๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•œ ๋ฆฌ์ŠคํŠธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + ```py + url_cow = "https://media.istockphoto.com/id/1192867753/photo/cow-in-berchida-beach-siniscola.jpg?s=612x612&w=0&k=20&c=v0hjjniwsMNfJSuKWZuIn8pssmD5h5bSN1peBd1CmH4=" + url_cat = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" + + messages =[ + { + "role": "system", + "content": [ + {"type": "text", "text": "You are a helpful assistant."} + ] + }, + { + "role": "user", + "content": [ + {"type": "image", "url": url_cow}, + {"type": "image", "url": url_cat}, + {"type": "text", "text": "Which image is cuter?"}, + ] + }, + ] + ``` +- ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌ๋˜๋Š” ํ…์ŠคํŠธ์—๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋˜์–ด์•ผ ํ•˜๋Š” ์œ„์น˜๋งˆ๋‹ค `` ํ† ํฐ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ”„๋กœ์„ธ์„œ์—๋Š” ์ฑ„ํŒ… ๋ฉ”์‹œ์ง€๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž์ฒด [`~ProcessorMixin.apply_chat_template`] ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋Š” ์ž˜๋ฆฌ์ง€ ์•Š์œผ๋ฉฐ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋งŒ ๋ชจ๋ธ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋‚˜ ์ •์‚ฌ๊ฐํ˜•์ด ์•„๋‹Œ ์ข…ํšก๋น„์˜ ์ด๋ฏธ์ง€์—์„œ๋Š” ๋น„์ „ ์ธ์ฝ”๋”๊ฐ€ 896x896์˜ ๊ณ ์ • ํ•ด์ƒ๋„๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„ํ‹ฐํŒฉํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์•„ํ‹ฐํŒฉํŠธ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ์ถ”๋ก  ์ค‘ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋ ค๋ฉด, `do_pan_and_scan=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ํŒจ์น˜๋กœ ์ž๋ฅด๊ณ  ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ๊ณผ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค. ๋” ๋น ๋ฅธ ์ถ”๋ก ์„ ์œ„ํ•ด ํŒฌ๊ณผ ์Šค์บ”์„ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + ```diff + inputs = processor.apply_chat_template( + messages, + tokenize=True, + return_dict=True, + return_tensors="pt", + add_generation_prompt=True, + + do_pan_and_scan=True, + ).to(model.device) + ``` +- ํ…์ŠคํŠธ ์ „์šฉ ๋ชจ๋“œ๋กœ ํ›ˆ๋ จ๋œ Gemma-3 1B ์ฒดํฌํฌ์ธํŠธ์˜ ๊ฒฝ์šฐ, [`AutoModelForCausalLM`]์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜์„ธ์š”. + + ```py + import torch + from transformers import AutoModelForCausalLM, AutoTokenizer + + tokenizer = AutoTokenizer.from_pretrained( + "google/gemma-3-1b-pt", + ) + model = AutoModelForCausalLM.from_pretrained( + "google/gemma-3-1b-pt", + torch_dtype=torch.bfloat16, + device_map="auto", + attn_implementation="sdpa" + ) + input_ids = tokenizer("Plants create energy through a process known as", return_tensors="pt").to(model.device) + + output = model.generate(**input_ids, cache_implementation="static") + print(tokenizer.decode(output[0], skip_special_tokens=True)) + ``` + +## Gemma3ImageProcessor + +[[autodoc]] Gemma3ImageProcessor + +## Gemma3ImageProcessorFast + +[[autodoc]] Gemma3ImageProcessorFast + +## Gemma3Processor + +[[autodoc]] Gemma3Processor + +## Gemma3TextConfig + +[[autodoc]] Gemma3TextConfig + +## Gemma3Config + +[[autodoc]] Gemma3Config + +## Gemma3TextModel + +[[autodoc]] Gemma3TextModel + - forward + +## Gemma3Model + +[[autodoc]] Gemma3Model + +## Gemma3ForCausalLM + +[[autodoc]] Gemma3ForCausalLM + - forward + +## Gemma3ForConditionalGeneration + +[[autodoc]] Gemma3ForConditionalGeneration + - forward + +## Gemma3ForSequenceClassification + +[[autodoc]] Gemma3ForSequenceClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt2.md new file mode 100644 index 0000000000000000000000000000000000000000..cdd4dbc615f3bdef6dc813f18b81b1a5225e6110 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt2.md @@ -0,0 +1,219 @@ + + +
+
+ PyTorch + TensorFlow + FlashAttention + SDPA +
+
+ + +# GPT-2[[gpt-2]] + +[GPT-2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)๋Š” GPT์˜ ํ™•์žฅ ๋ฒ„์ „์œผ๋กœ, ์ธ๊ณผ์  ํŠธ๋žœ์Šคํฌ๋จธ ์–ธ์–ด ๋ชจ๋ธ์ด๋ฉฐ, 10๋ฐฐ ๋” ๋งŽ์€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ด์ „์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก 40GB ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์„ ํ†ตํ•ด ์ด ๋ชจ๋ธ์€ ์ œ๋กœ์ƒท ์„ค์ •์—์„œ ๋งŽ์€ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋Š” ๊ฐ ํ† ํฐ์ด ์ด์ „ ํ† ํฐ์—๋งŒ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ผ ์ˆ˜ ์žˆ๋Š” ๋‹จ๋ฐฉํ–ฅ(์ธ๊ณผ์ ) ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ํ…์ŠคํŠธ ์ƒ์„ฑ ์ž‘์—…์— ํŠนํžˆ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. + +๋ชจ๋“  ์›๋ณธ GPT-2 ์ฒดํฌํฌ์ธํŠธ๋Š” [OpenAI community](https://huggingface.co/openai-community?search_models=gpt) ์กฐ์ง์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ์˜ค๋ฅธ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์˜ GPT-2 ๋ชจ๋ธ์„ ํด๋ฆญํ•˜์—ฌ GPT-2๋ฅผ ๋‹ค์–‘ํ•œ ์–ธ์–ด ์ž‘์—…์— ์ ์šฉํ•˜๋Š” ๋” ๋งŽ์€ ์˜ˆ์‹œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” [`Pipeline`] ๋˜๋Š” [`AutoModel`], ๊ทธ๋ฆฌ๊ณ  ๋ช…๋ น์ค„์—์„œ GPT-2๋กœ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + + + +```py +import torch +from transformers import pipeline + +# ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ +pipeline = pipeline(task="text-generation", model="openai-community/gpt2", torch_dtype=torch.float16, device=0) +pipeline("Hello, I'm a language model") +``` + + + +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer + +# ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ +model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2", torch_dtype=torch.float16, device_map="auto", attn_implementation="sdpa") +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") + +# ์ž…๋ ฅ ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  GPU๋กœ ์ด๋™ +input_ids = tokenizer("Hello, I'm a language model", return_tensors="pt").to("cuda") + +# ํ…์ŠคํŠธ ์ƒ์„ฑ +output = model.generate(**input_ids, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + + +```bash +echo -e "Hello, I'm a language model" | transformers run --task text-generation --model openai-community/gpt2 --device 0 +``` + + + + +`transformers backend`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ vLLM์œผ๋กœ ๋ชจ๋ธ์„ ์„œ๋น™ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +vllm serve openai-community/gpt2 --model-imp transformers +``` + +์–‘์žํ™”๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋Œ€ํ˜• ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€๋‹ด์„ ์ค„์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋” ๋งŽ์€ ์–‘์žํ™” ๋ฐฑ์—”๋“œ์— ๋Œ€ํ•ด์„œ๋Š” [Quantization](../quantization/overview) ๊ฐœ์š”๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” [bitsandbytes](../quantization/bitsandbytes)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋งŒ 4๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, pipeline + +# ์–‘์žํ™” ์„ค์ • ๊ตฌ์„ฑ +quantization_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", + bnb_4bit_compute_dtype="float16", + bnb_4bit_use_double_quant=True +) + +# ์–‘์žํ™”๋œ ๋ชจ๋ธ ๋กœ๋“œ +model = AutoModelForCausalLM.from_pretrained( + "openai-community/gpt2-xl", + quantization_config=quantization_config, + device_map="auto" +) + +# ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ ๋ฐ ํ…์ŠคํŠธ ์ƒ์„ฑ +tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2-xl") +inputs = tokenizer("Once upon a time, there was a magical forest", return_tensors="pt").to("cuda") +outputs = model.generate(**inputs, max_new_tokens=100) +print(tokenizer.decode(outputs[0], skip_special_tokens=True)) +``` + +## ์ฐธ๊ณ ์‚ฌํ•ญ[[notes]] + +- GPT-2๋Š” ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž…๋ ฅ์„ ์˜ค๋ฅธ์ชฝ์— ํŒจ๋”ฉํ•˜์„ธ์š”. +- GPT-2๋Š” ์ด์ „์— ๊ณ„์‚ฐ๋œ ํ‚ค-๊ฐ’ ์–ดํ…์…˜ ์Œ์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`GPT2Model.forward`]์˜ [past_key_values](https://huggingface.co/docs/transformers//en/model_doc/gpt2#transformers.GPT2Model.forward.past_key_values) ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด ๊ธฐ๋Šฅ์— ์ ‘๊ทผํ•˜์„ธ์š”. +- [Mistral](./mistral)์˜ ํ•™์Šต ์•ˆ์ •์„ฑ ๊ฐœ์„  ์‚ฌํ•ญ์„ ์ ์šฉํ•˜๋ ค๋ฉด [scale_attn_by_inverse_layer_idx](https://huggingface.co/docs/transformers/en/model_doc/gpt2#transformers.GPT2Config.scale_attn_by_inverse_layer_idx)์™€ [reorder_and_upcast_attn](https://huggingface.co/docs/transformers/en/model_doc/gpt2#transformers.GPT2Config.reorder_and_upcast_attn) ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์„ฑํ™”ํ•˜์„ธ์š”. + +## GPT2Config + +[[autodoc]] GPT2Config + +## GPT2Tokenizer + +[[autodoc]] GPT2Tokenizer + - save_vocabulary + +## GPT2TokenizerFast + +[[autodoc]] GPT2TokenizerFast + +## GPT2 ํŠน์ • ์ถœ๋ ฅ[[gpt2-specific-outputs]] + +[[autodoc]] models.gpt2.modeling_gpt2.GPT2DoubleHeadsModelOutput + +[[autodoc]] models.gpt2.modeling_tf_gpt2.TFGPT2DoubleHeadsModelOutput + + + + +## GPT2Model + +[[autodoc]] GPT2Model + - forward + +## GPT2LMHeadModel + +[[autodoc]] GPT2LMHeadModel + - forward + +## GPT2DoubleHeadsModel + +[[autodoc]] GPT2DoubleHeadsModel + - forward + +## GPT2ForQuestionAnswering + +[[autodoc]] GPT2ForQuestionAnswering + - forward + +## GPT2ForSequenceClassification + +[[autodoc]] GPT2ForSequenceClassification + - forward + +## GPT2ForTokenClassification + +[[autodoc]] GPT2ForTokenClassification + - forward + + + + +## TFGPT2Model + +[[autodoc]] TFGPT2Model + - call + +## TFGPT2LMHeadModel + +[[autodoc]] TFGPT2LMHeadModel + - call + +## TFGPT2DoubleHeadsModel + +[[autodoc]] TFGPT2DoubleHeadsModel + - call + +## TFGPT2ForSequenceClassification + +[[autodoc]] TFGPT2ForSequenceClassification + - call + +## TFSequenceClassifierOutputWithPast + +[[autodoc]] modeling_tf_outputs.TFSequenceClassifierOutputWithPast + +## TFGPT2Tokenizer + +[[autodoc]] TFGPT2Tokenizer + + + + +## FlaxGPT2Model + +[[autodoc]] FlaxGPT2Model + - __call__ + +## FlaxGPT2LMHeadModel + +[[autodoc]] FlaxGPT2LMHeadModel + - __call__ + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt_neox_japanese.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt_neox_japanese.md new file mode 100644 index 0000000000000000000000000000000000000000..13fb656dd50e9463740e1ea273bfcb9073ecd1e1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/gpt_neox_japanese.md @@ -0,0 +1,76 @@ + + +# GPT-NeoX-Japanese [[gpt-neox-japanese]] + +## ๊ฐœ์š” [[overview]] + + +์ผ๋ณธ์–ด๋ฅผ ์œ„ํ•œ ์ž๋™ํšŒ๊ท€ ์–ธ์–ด ๋ชจ๋ธ์ธ GPT-NeoX-Japanese๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ [https://github.com/EleutherAI/gpt-neox](https://github.com/EleutherAI/gpt-neox)์—์„œ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ณธ์–ด๋Š” ๋งŽ์€ ์–ดํœ˜์™€ ํžˆ๋ผ๊ฐ€๋‚˜, ๊ฐ€ํƒ€์นด๋‚˜, ํ•œ์ž์˜ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋…ํŠนํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ๋ณธ์–ด์˜ ๋…ํŠนํ•œ ๊ตฌ์กฐ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด [ํŠน์ˆ˜ ์„œ๋ธŒ์›Œ๋“œ ํ† ํฌ๋‚˜์ด์ €](https://github.com/tanreinama/Japanese-BPEEncoder_V2)๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์œ ์šฉํ•œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์˜คํ”ˆ์†Œ์Šค๋กœ ์ œ๊ณตํ•ด ์ค€ *tanreinama*์—๊ฒŒ ๋งค์šฐ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ Google์˜ [PaLM](https://ai.googleblog.com/2022/04/pathways-language-model-palm-scaling-to.html) ์—ฐ๊ตฌ ๊ถŒ์žฅ ์‚ฌํ•ญ์„ ๋”ฐ๋ฅด๋ฉฐ, ํŠธ๋žœ์Šคํฌ๋จธ ๋ธ”๋ก์—์„œ ํŽธํ–ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๋ชจ๋ธ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ด ๊ธฐ์‚ฌ](https://medium.com/ml-abeja/training-a-better-gpt-2-93b157662ae4)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +๋ชจ๋ธ ๊ฐœ๋ฐœ์€ [ABEJA, Inc.](https://www.abejainc.com/)์˜ [์‹ ์•ผ ์˜คํƒ€๋‹ˆ](https://github.com/SO0529), [ํƒ€์นด์š”์‹œ ๋งˆ์นด๋ฒ ](https://github.com/spider-man-tm), [์•ˆ์ฃผ ์•„๋กœ๋ผ](https://github.com/Anuj040), [์ฟ„ ํ•˜ํ† ๋ฆฌ](https://github.com/go5paopao)์— ์˜ํ•ด ์ฃผ๋„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ ๊ฐœ๋ฐœ ํ™œ๋™์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์—ฌ๊ธฐ](https://tech-blog.abeja.asia/entry/abeja-gpt-project-202207)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +### ์‚ฌ์šฉ ์˜ˆ์‹œ [[usage-example]] + +`generate()` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด GPT NeoX Japanese ๋ชจ๋ธ์„ ํ†ตํ•ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +>>> from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseTokenizer + +>>> model = GPTNeoXJapaneseForCausalLM.from_pretrained("abeja/gpt-neox-japanese-2.7b") +>>> tokenizer = GPTNeoXJapaneseTokenizer.from_pretrained("abeja/gpt-neox-japanese-2.7b") + +>>> prompt = "ไบบใจAIใŒๅ”่ชฟใ™ใ‚‹ใŸใ‚ใซใฏใ€" + +>>> input_ids = tokenizer(prompt, return_tensors="pt").input_ids + +>>> gen_tokens = model.generate( +... input_ids, +... do_sample=True, +... temperature=0.9, +... max_length=100, +... ) +>>> gen_text = tokenizer.batch_decode(gen_tokens, skip_special_tokens=True)[0] + +>>> print(gen_text) +ไบบใจAIใŒๅ”่ชฟใ™ใ‚‹ใŸใ‚ใซใฏใ€AIใจไบบใŒๅ…ฑๅญ˜ใ—ใ€AIใ‚’ๆญฃใ—ใ็†่งฃใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ +``` + +## ์ž๋ฃŒ [[resources]] + +- [์ผ์ƒ ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ ](../tasks/language_modeling) + +## GPTNeoXJapanese ์„ค์ • (GPTNeoXJapaneseConfig) [[transformers.GPTNeoXJapaneseConfig]] + +[[autodoc]] GPTNeoXJapaneseConfig + +## GPTNeoXJapaneseํ† ํฐํ™” (GPTNeoXJapaneseTokenizer) [[transformers.GPTNeoXJapaneseTokenizer]] + +[[autodoc]] GPTNeoXJapaneseTokenizer + +## GPTNeoXJapaneseModel [[transformers.GPTNeoXJapaneseModel]] + +[[autodoc]] GPTNeoXJapaneseModel + - forward + +## ์ผ์ƒ LLM ์„ ์œ„ํ•œ GPTNeoXJapanese(GPTNeoXJapaneseForCausalLM) [[transformers.GPTNeoXJapaneseForCausalLM]] + +[[autodoc]] GPTNeoXJapaneseForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/graphormer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/graphormer.md new file mode 100644 index 0000000000000000000000000000000000000000..9e1a893fc5f6388362eac9fecbd105bc271917a6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/graphormer.md @@ -0,0 +1,52 @@ + + +# Graphormer[[graphormer]] + + + +์ด ๋ชจ๋ธ์€ ์œ ์ง€ ๋ณด์ˆ˜ ๋ชจ๋“œ๋กœ๋งŒ ์šด์˜๋˜๋ฉฐ, ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ƒˆ๋กœ์šด PR(Pull Request)์€ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +์ด ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์ด ๋ชจ๋ธ์„ ์ง€์›ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋ฒ„์ „์ธ v4.40.2๋ฅผ ๋‹ค์‹œ ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์žฌ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: `pip install -U transformers==4.40.2`. + + + +## ๊ฐœ์š”[[overview]] + +Graphormer ๋ชจ๋ธ์€ Chengxuan Ying, Tianle Cai, Shengjie Luo, Shuxin Zheng, Guolin Ke, Di He, Yanming Shen, Tie-Yan Liu๊ฐ€ ์ œ์•ˆํ•œ [ํŠธ๋žœ์Šคํฌ๋จธ๊ฐ€ ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„์— ์žˆ์–ด์„œ ์ •๋ง ์•ฝํ• ๊นŒ?](https://huggingface.co/papers/2106.05234) ๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Graphormer๋Š” ๊ทธ๋ž˜ํ”„ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์‹œํ€€์Šค ๋Œ€์‹  ๊ทธ๋ž˜ํ”„์—์„œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •๋˜์—ˆ์œผ๋ฉฐ, ์ „์ฒ˜๋ฆฌ์™€ ๋ณ‘ํ•ฉ ๊ณผ์ •์—์„œ ์ž„๋ฒ ๋”ฉ๊ณผ ๊ด€์‹ฌ ํŠน์„ฑ์„ ์ƒ์„ฑํ•œ ํ›„ ์ˆ˜์ •๋œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์™€ ์ปดํ“จํ„ฐ ๋น„์ „ ๋“ฑ ๋งŽ์€ ๋ถ„์•ผ์—์„œ ์ง€๋ฐฐ์ ์ธ ์„ ํƒ์„ ๋ฐ›๊ณ  ์žˆ๋Š” ์•„ํ‚คํ…์ฒ˜ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ๋ž˜ํ”„ ์ˆ˜์ค€ ์˜ˆ์ธก ๋ฆฌ๋”๋ณด๋“œ ์ƒ์—์„œ๋Š” ์ฃผ๋ฅ˜ GNN ๋ณ€ํ˜•๋ชจ๋ธ๋“ค์— ๋น„ํ•ด ๊ฒฝ์Ÿ๋ ฅ ์žˆ๋Š” ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŠธ๋žœ์Šคํฌ๋จธ๊ฐ€ ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„ ํ•™์Šต์—์„œ ์–ด๋–ป๊ฒŒ ์ž˜ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ์„์ง€๋Š” ์—ฌ์ „ํžˆ ๋ฏธ์Šคํ„ฐ๋ฆฌ์˜€์Šต๋‹ˆ๋‹ค. ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” Graphormer๋ฅผ ์ œ์‹œํ•จ์œผ๋กœ์จ ์ด ๋ฏธ์Šคํ„ฐ๋ฆฌ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. Graphormer๋Š” ํ‘œ์ค€ ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ถ•๋˜์—ˆ์œผ๋ฉฐ, ํŠนํžˆ ์ตœ๊ทผ์˜ OpenGraphBenchmark Large-Scale Challenge(OGB-LSC)์˜ ๊ด‘๋ฒ”์œ„ํ•œ ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„ ํ•™์Šต ์ž‘์—…์—์„œ ํƒ์›”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ํ™œ์šฉํ•˜๋Š”๋ฐ ํ•ต์‹ฌ์€ ๊ทธ๋ž˜ํ”„์˜ ๊ตฌ์กฐ์  ์ •๋ณด๋ฅผ ๋ชจ๋ธ์— ํšจ๊ณผ์ ์œผ๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” Graphormer๊ฐ€ ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์ž˜ ๋ชจ๋ธ๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•˜๋ฉด์„œ๋„ ํšจ๊ณผ์ ์ธ ๊ตฌ์กฐ์  ์ธ์ฝ”๋”ฉ ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์šฐ๋ฆฌ๋Š” Graphormer์˜ ํ‘œํ˜„์„ ์ˆ˜ํ•™์ ์œผ๋กœ ํŠน์„ฑํ™”ํ•˜๊ณ , ๊ทธ๋ž˜ํ”„์˜ ๊ตฌ์กฐ์  ์ •๋ณด๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๋Š” ์šฐ๋ฆฌ์˜ ๋ฐฉ์‹์œผ๋กœ ๋งŽ์€ ์ธ๊ธฐ ์žˆ๋Š” GNN ๋ณ€ํ˜•๋ชจ๋ธ๋“ค์ด Graphormer์˜ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋กœ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [clefourrier](https://huggingface.co/clefourrier)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/microsoft/Graphormer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +์ด ๋ชจ๋ธ์€ ํฐ ๊ทธ๋ž˜ํ”„(100๊ฐœ ์ด์ƒ์˜ ๋…ธ๋“œ๊ฐœ์ˆ˜/์—ฃ์ง€๊ฐœ์ˆ˜)์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ํญ๋ฐœ์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์•ˆ์œผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ฑฐ๋‚˜, RAM์„ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ๋˜๋Š” algos_graphormer.pyx ํŒŒ์ผ์˜ `UNREACHABLE_NODE_DISTANCE` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, 700๊ฐœ ์ด์ƒ์˜ ๋…ธ๋“œ๊ฐœ์ˆ˜/์—ฃ์ง€๊ฐœ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ์—๋Š” ์—ฌ์ „ํžˆ ์–ด๋ ค์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๋Œ€์‹  ํ›ˆ๋ จ ์ค‘์— ํŠน๋ณ„ํ•œ ์ฝœ๋ ˆ์ดํ„ฐ(collator)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## GraphormerConfig[[transformers.GraphormerConfig]] + +[[autodoc]] GraphormerConfig + +## GraphormerModel[[transformers.GraphormerModel]] + +[[autodoc]] GraphormerModel + - forward + +## GraphormerForGraphClassification[[transformers.GraphormerForGraphClassification]] + +[[autodoc]] GraphormerForGraphClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/grounding-dino.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/grounding-dino.md new file mode 100644 index 0000000000000000000000000000000000000000..82f40276c7d84b5ae1fae687efc3aa5b997054d9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/grounding-dino.md @@ -0,0 +1,129 @@ + + +# Grounding DINO[[grounding-dino]] + +
+PyTorch +
+ +## ๊ฐœ์š”[[overview]] + +Grounding DINO ๋ชจ๋ธ์€ Shilong Liu, Zhaoyang Zeng, Tianhe Ren, Feng Li, Hao Zhang, Jie Yang, Chunyuan Li, Jianwei Yang, Hang Su, Jun Zhu, Lei Zhang์ด [Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection](https://huggingface.co/papers/2303.05499)์—์„œ ์ œ์•ˆํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. Grounding DINO๋Š” ํ์‡„ํ˜• ๊ฐ์ฒด ํƒ์ง€ ๋ชจ๋ธ์„ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋กœ ํ™•์žฅํ•˜์—ฌ ๊ฐœ๋ฐฉํ˜• ๊ฐ์ฒด ํƒ์ง€๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ COCO ์ œ๋กœ์ƒท์—์„œ 52.5 AP์™€ ๊ฐ™์€ ๋†€๋ผ์šด ๊ฒฐ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ํƒ์ง€๊ธฐ DINO๋ฅผ ๊ธฐ๋ฐ˜ ์‚ฌ์ „ ํ•™์Šต๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ Grounding DINO๋ผ๋Š” ๊ฐœ๋ฐฉํ˜• ๊ฐ์ฒด ํƒ์ง€๊ธฐ๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์ด๋‚˜ ์ฐธ์กฐ ํ‘œํ˜„ ๋“ฑ์˜ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์œผ๋กœ ์ž„์˜์˜ ๊ฐ์ฒด๋ฅผ ํƒ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐฉํ˜• ๊ฐ์ฒด ํƒ์ง€์˜ ํ•ต์‹ฌ ํ•ด๊ฒฐ์ฑ…์€ ๊ฐœ๋ฐฉํ˜• ๊ฐœ๋… ์ผ๋ฐ˜ํ™”๋ฅผ ์œ„ํ•ด ํ์‡„ํ˜• ํƒ์ง€๊ธฐ์— ์–ธ์–ด๋ฅผ ๋„์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์–ธ์–ด์™€ ๋น„์ „ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์œตํ•ฉํ•˜๊ธฐ ์œ„ํ•ด, ํ์‡„ํ˜• ํƒ์ง€๊ธฐ๋ฅผ ๊ฐœ๋…์ ์œผ๋กœ ์„ธ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด ํŠน์„ฑ ๊ฐ•ํ™”๊ธฐ, ์–ธ์–ด ๊ธฐ๋ฐ˜ ์ฟผ๋ฆฌ ์„ ํƒ, ๊ต์ฐจ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ ์œตํ•ฉ์„ ์œ„ํ•œ ๊ต์ฐจ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ ๋””์ฝ”๋”๋ฅผ ํฌํ•จํ•˜๋Š” ๊ธด๋ฐ€ํ•œ ์œตํ•ฉ ์†”๋ฃจ์…˜์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์—ฐ๊ตฌ๋“ค์ด ์ฃผ๋กœ ์ƒˆ๋กœ์šด ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•œ ๊ฐœ๋ฐฉํ˜• ๊ฐ์ฒด ํƒ์ง€๋ฅผ ํ‰๊ฐ€ํ•œ ๋ฐ˜๋ฉด, ์šฐ๋ฆฌ๋Š” ์†์„ฑ์œผ๋กœ ์ง€์ •๋œ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ ํ‘œํ˜„ ์ดํ•ด์— ๋Œ€ํ•œ ํ‰๊ฐ€๋„ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. Grounding DINO๋Š” COCO, LVIS, ODinW, RefCOCO/+/g ๋ฒค์น˜๋งˆํฌ๋ฅผ ํฌํ•จํ•œ ์„ธ ๊ฐ€์ง€ ์„ค์ • ๋ชจ๋‘์—์„œ ๋†€๋ผ์šด ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. Grounding DINO๋Š” COCO ํƒ์ง€ ์ œ๋กœ์ƒท ์ „์ด ๋ฒค์น˜๋งˆํฌ์—์„œ 52.5 AP(Average Precision, ํ‰๊ท  ์ •๋ฐ€๋„)๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, COCO์˜ ํ•™์Šต ๋ฐ์ดํ„ฐ ์—†์ด๋„ ์ด๋Ÿฌํ•œ ์„ฑ๊ณผ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ท  26.1 AP๋กœ ODinW ์ œ๋กœ์ƒท ๋ฒค์น˜๋งˆํฌ์—์„œ ์ƒˆ๋กœ์šด ๊ธฐ๋ก์„ ์„ธ์› ์Šต๋‹ˆ๋‹ค.* + + + + Grounding DINO ๊ฐœ์š”. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [EduardoPacheco](https://huggingface.co/EduardoPacheco)์™€ [nielsr](https://huggingface.co/nielsr)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/IDEA-Research/GroundingDINO)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- [`GroundingDinoProcessor`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์œ„ํ•œ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์Œ์„ ์ค€๋น„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ํ…์ŠคํŠธ์—์„œ ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ๋Š” ๋งˆ์นจํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ: "a cat. a dog." +- ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ(์˜ˆ: `"a cat. a dog."`), [`GroundingDinoProcessor`]์˜ `post_process_grounded_object_detection`์„ ์‚ฌ์šฉํ•ด ์ถœ๋ ฅ์„ ํ›„์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `post_process_object_detection`์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ ˆ์ด๋ธ”์€ prob > threshold์ธ ๋ชจ๋ธ ์ฐจ์›์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ์ œ๋กœ์ƒท ๊ฐ์ฒด ํƒ์ง€์— ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```python +>>> import requests + +>>> import torch +>>> from PIL import Image +>>> from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection + +>>> model_id = "IDEA-Research/grounding-dino-tiny" +>>> device = "cuda" + +>>> processor = AutoProcessor.from_pretrained(model_id) +>>> model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device) + +>>> image_url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(image_url, stream=True).raw) +>>> # ๊ณ ์–‘์ด์™€ ๋ฆฌ๋ชจ์ปจ ํ™•์ธ +>>> text_labels = [["a cat", "a remote control"]] + +>>> inputs = processor(images=image, text=text_labels, return_tensors="pt").to(device) +>>> with torch.no_grad(): +... outputs = model(**inputs) + +>>> results = processor.post_process_grounded_object_detection( +... outputs, +... inputs.input_ids, +... box_threshold=0.4, +... text_threshold=0.3, +... target_sizes=[image.size[::-1]] +... ) + +# ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ +>>> result = results[0] +>>> for box, score, labels in zip(result["boxes"], result["scores"], result["labels"]): +... box = [round(x, 2) for x in box.tolist()] +... print(f"Detected {labels} with confidence {round(score.item(), 3)} at location {box}") +Detected a cat with confidence 0.468 at location [344.78, 22.9, 637.3, 373.62] +Detected a cat with confidence 0.426 at location [11.74, 51.55, 316.51, 473.22] +``` + +## Grounded SAM[[grounded-sam]] + +[Grounded SAM: Assembling Open-World Models for Diverse Visual Tasks](https://huggingface.co/papers/2401.14159)์—์„œ ์†Œ๊ฐœ๋œ ๋Œ€๋กœ Grounding DINO๋ฅผ [Segment Anything](sam) ๋ชจ๋ธ๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜ ๋งˆ์Šคํฌ ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด [๋ฐ๋ชจ ๋…ธํŠธ๋ถ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Grounding%20DINO/GroundingDINO_with_Segment_Anything.ipynb) ๐ŸŒ์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + + Grounded SAM ๊ฐœ์š”. ์›๋ณธ ์ €์žฅ์†Œ์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค[[resources]] + +Grounding DINO๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ณต์‹ Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ) ๋ฆฌ์†Œ์Šค ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Pull Request๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์—ด์–ด์ฃผ์„ธ์š”. ๊ฒ€ํ† ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฆฌ์†Œ์Šค๋Š” ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค. + +- Grounding DINO๋กœ ์ถ”๋ก ํ•˜๊ณ  [SAM](sam)๊ณผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์—ฌ๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Grounding%20DINO)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ + +## GroundingDinoImageProcessor + +[[autodoc]] GroundingDinoImageProcessor + - preprocess + +## GroundingDinoImageProcessorFast + +[[autodoc]] GroundingDinoImageProcessorFast + - preprocess + - post_process_object_detection + +## GroundingDinoProcessor + +[[autodoc]] GroundingDinoProcessor + - post_process_grounded_object_detection + +## GroundingDinoConfig + +[[autodoc]] GroundingDinoConfig + +## GroundingDinoModel + +[[autodoc]] GroundingDinoModel + - forward + +## GroundingDinoForObjectDetection + +[[autodoc]] GroundingDinoForObjectDetection + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/informer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/informer.md new file mode 100644 index 0000000000000000000000000000000000000000..9482933008b87c183210a25dfa66b40b9f1dd1eb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/informer.md @@ -0,0 +1,55 @@ + + +# Informer[[informer]] + +## ๊ฐœ์š”[[overview]] + +The Informer ๋ชจ๋ธ์€ Haoyi Zhou, Shanghang Zhang, Jieqi Peng, Shuai Zhang, Jianxin Li, Hui Xiong, Wancai Zhang๊ฐ€ ์ œ์•ˆํ•œ [Informer: ์žฅ๊ธฐ ์‹œํ€€์Šค ์‹œ๊ณ„์—ด ์˜ˆ์ธก(LSTF)์„ ์œ„ํ•œ ๋”์šฑ ํšจ์œจ์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ(Beyond Efficient Transformer)](https://huggingface.co/papers/2012.07436)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ฐฉ๋ฒ•์€ ํ™•๋ฅ ์  ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋„์ž…ํ•˜์—ฌ "๊ฒŒ์œผ๋ฅธ" ์ฟผ๋ฆฌ๊ฐ€ ์•„๋‹Œ "ํ™œ์„ฑ" ์ฟผ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๊ณ , ํฌ์†Œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๊ธฐ์กด ์–ดํ…์…˜์˜ ์ด์ฐจ์  ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ์™„ํ™”ํ•ฉ๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์‹ค์ œ๋กœ ๋งŽ์€ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ์žฅ๊ธฐ ์‹œํ€€์Šค ์‹œ๊ณ„์—ด ์˜ˆ์ธก(LSTF)์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. LSTF๋Š” ์ถœ๋ ฅ - ์ž…๋ ฅ ๊ฐ„ ์ •ํ™•ํ•œ ์žฅ๊ธฐ ์˜์กด์„ฑ ๊ฒฐํ•ฉ๋„๋ฅผ ํฌ์ฐฉํ•ด๋‚ด๋Š” ๋†’์€ ์˜ˆ์ธก ๋Šฅ๋ ฅ์„ ๋ชจ๋ธ์— ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ตœ๊ทผ ์—ฐ๊ตฌ๋“ค์€ ์˜ˆ์ธก ๋Šฅ๋ ฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž ์žฌ๋ ฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ LSTF์— ์ง์ ‘ ์ ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰๋Š” ๋ช‡ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ์ ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋กœ, ์ด์ฐจ ์‹œ๊ฐ„ ๋ณต์žก๋„, ๋†’์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰, ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜์˜ ๋ณธ์งˆ์  ํ•œ๊ณ„๋ฅผ ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด LSTF๋ฅผ ์œ„ํ•œ ํšจ์œจ์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์ธ Informer๋ฅผ ์„ค๊ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. + +Informer์˜ ์„ธ๊ฐ€์ง€ ๋…ํŠนํ•œ ํŠน์„ฑ: +(i) ProbSparse ์…€ํ”„ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ, ์‹œ๊ฐ„ ๋ณต์žก๋„์™€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์—์„œ O(L logL)๋ฅผ ๋‹ฌ์„ฑํ•˜๋ฉฐ ์‹œํ€€์Šค ์˜์กด์„ฑ ์ •๋ ฌ์—์„œ ๋น„๊ต ๊ฐ€๋Šฅํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. +(ii) ์…€ํ”„ ์–ดํ…์…˜ ์ฆ๋ฅ˜๋Š” ๊ณ„๋‹จ์‹ ๋ ˆ์ด์–ด ์ž…๋ ฅ์„ ๋ฐ˜์œผ๋กœ ์ค„์—ฌ ์ง€๋ฐฐ์ ์ธ ์–ดํ…์…˜์„ ๊ฐ•์กฐํ•˜๊ณ  ๊ทน๋‹จ์ ์œผ๋กœ ๊ธด ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +(iii) ์ƒ์„ฑ ์Šคํƒ€์ผ ๋””์ฝ”๋”๋Š” ๊ฐœ๋…์ ์œผ๋กœ ๋‹จ์ˆœํ•˜์ง€๋งŒ ์žฅ๊ธฐ ์‹œ๊ณ„์—ด ์‹œํ€€์Šค๋ฅผ ๋‹จ๊ณ„๋ณ„ ๋ฐฉ์‹์ด ์•„๋‹Œ ํ•œ ๋ฒˆ์˜ ์ „๋ฐฉ ์—ฐ์‚ฐ์œผ๋กœ ์˜ˆ์ธกํ•˜์—ฌ ์žฅ๊ธฐ ์‹œํ€€์Šค ์˜ˆ์ธก์˜ ์ถ”๋ก  ์†๋„๋ฅผ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. 4๊ฐœ์˜ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์…‹์— ๊ฑธ์นœ ๊ด‘๋ฒ”์œ„ํ•œ ์‹คํ—˜์€ Informer๊ฐ€ ๊ธฐ์กด ๋ฐฉ๋ฒ•๋“ค์„ ํฌ๊ฒŒ ๋Šฅ๊ฐ€ํ•˜๋ฉฐ LSTF ๋ฌธ์ œ์— ์ƒˆ๋กœ์šด ํ•ด๊ฒฐ์ฑ…์„ ์ œ๊ณตํ•จ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [elisim](https://huggingface.co/elisim)์™€ [kashif](https://huggingface.co/kashif)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/zhouhaoyi/Informer2020)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ[[resources]] + +์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- HuggingFace ๋ธ”๋กœ๊ทธ์—์„œ Informer ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: [Informer๋ฅผ ํ™œ์šฉํ•œ ๋‹ค๋ณ€๋Ÿ‰ ํ™•๋ฅ ์  ์‹œ๊ณ„์—ด ์˜ˆ์ธก](https://huggingface.co/blog/informer) + +## InformerConfig[[transformers.InformerConfig]] + +[[autodoc]] InformerConfig + +## InformerModel[[transformers.InformerModel]] + +[[autodoc]] InformerModel + - forward + +## InformerForPrediction[[transformers.InformerForPrediction]] + +[[autodoc]] InformerForPrediction + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/jamba.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/jamba.md new file mode 100644 index 0000000000000000000000000000000000000000..5eeb4abb712a0e57a982645569748797a6ff96c9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/jamba.md @@ -0,0 +1,158 @@ + + +
+
+ PyTorch + FlashAttention + SDPA +
+
+ +# Jamba[[jamba]] + +[Jamba](https://huggingface.co/papers/2403.19887)๋Š” Transformer์™€ Mamba ๊ธฐ๋ฐ˜์˜ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ „๋ฌธ๊ฐ€ ํ˜ผํ•ฉ(MoE) ์–ธ์–ด ๋ชจ๋ธ๋กœ, ์ด ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜๋Š” 52B์—์„œ 398B๊นŒ์ง€ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ Transformer ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ๊ณผ Mamba์™€ ๊ฐ™์€ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ์˜ ํšจ์œจ์„ฑ ๋ฐ ๊ธด ์ปจํ…์ŠคํŠธ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ(256K ํ† ํฐ)์„ ๋ชจ๋‘ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +Jamba์˜ ์•„ํ‚คํ…์ฒ˜๋Š” ๋ธ”๋ก๊ณผ ๋ ˆ์ด์–ด ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Transformer์™€ Mamba ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ Jamba ๋ธ”๋ก์€ ์–ดํ…์…˜ ๋ ˆ์ด์–ด ๋˜๋Š” Mamba ๋ ˆ์ด์–ด ์ค‘ ํ•˜๋‚˜์™€ ๊ทธ ๋’ค๋ฅผ ์ž‡๋Š” ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MLP)์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Transformer ๋ ˆ์ด์–ด๋Š” 8๊ฐœ์˜ ๋ ˆ์ด์–ด ์ค‘ ํ•˜๋‚˜์˜ ๋น„์œจ๋กœ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋ธ ์šฉ๋Ÿ‰์„ ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด MoE ๋ ˆ์ด์–ด๊ฐ€ ํ˜ผํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋“  ์›๋ณธ Jamba ์ฒดํฌํฌ์ธํŠธ๋Š” [AI21](https://huggingface.co/ai21labs) ์กฐ์ง์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ์˜ค๋ฅธ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”์— ์žˆ๋Š” Jamba ๋ชจ๋ธ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์–‘ํ•œ ์–ธ์–ด ์ž‘์—…์— Jamba๋ฅผ ์ ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๋” ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์ œ๋Š” [`Pipeline`]๊ณผ [`AutoModel`], ๊ทธ๋ฆฌ๊ณ  ์ปค๋งจ๋“œ๋ผ์ธ์„ ํ†ตํ•ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + + + +```py +# ์ตœ์ ํ™”๋œ Mamba ๊ตฌํ˜„ ์„ค์น˜ +# !pip install mamba-ssm causal-conv1d>=1.2.0 +import torch +from transformers import pipeline + +pipeline = pipeline( + task="text-generation", + model="ai21labs/AI21-Jamba-Mini-1.6", + torch_dtype=torch.float16, + device=0 +) +pipeline("Plants create energy through a process known as") +``` + + + + +```py +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer + +tokenizer = AutoTokenizer.from_pretrained( + "ai21labs/AI21-Jamba-Large-1.6", +) +model = AutoModelForCausalLM.from_pretrained( + "ai21labs/AI21-Jamba-Large-1.6", + torch_dtype=torch.float16, + device_map="auto", + attn_implementation="sdpa" +) +input_ids = tokenizer("Plants create energy through a process known as", return_tensors="pt").to("cuda") + +output = model.generate(**input_ids, cache_implementation="static") +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +```bash +echo -e "Plants create energy through a process known as" | transformers run --task text-generation --model ai21labs/AI21-Jamba-Mini-1.6 --device 0 +``` + + + + +์–‘์žํ™”๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€๋‹ด์„ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์–‘์žํ™” ๋ฐฑ์—”๋“œ์— ๋Œ€ํ•ด์„œ๋Š” [Quantization](../quantization/overview)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +์•„๋ž˜ ์˜ˆ์ œ๋Š” [bitsandbytes](../quantization/bitsandbytes)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋งŒ 8๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +```py +import torch +from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True, + llm_int8_skip_modules=["mamba"]) + +# ๋ชจ๋ธ์„ 8๊ฐœ์˜ GPU์— ๊ณ ๋ฅด๊ฒŒ ๋ถ„์‚ฐ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋””๋ฐ”์ด์Šค ๋งต +device_map = {'model.embed_tokens': 0, 'model.layers.0': 0, 'model.layers.1': 0, 'model.layers.2': 0, 'model.layers.3': 0, 'model.layers.4': 0, 'model.layers.5': 0, 'model.layers.6': 0, 'model.layers.7': 0, 'model.layers.8': 0, 'model.layers.9': 1, 'model.layers.10': 1, 'model.layers.11': 1, 'model.layers.12': 1, 'model.layers.13': 1, 'model.layers.14': 1, 'model.layers.15': 1, 'model.layers.16': 1, 'model.layers.17': 1, 'model.layers.18': 2, 'model.layers.19': 2, 'model.layers.20': 2, 'model.layers.21': 2, 'model.layers.22': 2, 'model.layers.23': 2, 'model.layers.24': 2, 'model.layers.25': 2, 'model.layers.26': 2, 'model.layers.27': 3, 'model.layers.28': 3, 'model.layers.29': 3, 'model.layers.30': 3, 'model.layers.31': 3, 'model.layers.32': 3, 'model.layers.33': 3, 'model.layers.34': 3, 'model.layers.35': 3, 'model.layers.36': 4, 'model.layers.37': 4, 'model.layers.38': 4, 'model.layers.39': 4, 'model.layers.40': 4, 'model.layers.41': 4, 'model.layers.42': 4, 'model.layers.43': 4, 'model.layers.44': 4, 'model.layers.45': 5, 'model.layers.46': 5, 'model.layers.47': 5, 'model.layers.48': 5, 'model.layers.49': 5, 'model.layers.50': 5, 'model.layers.51': 5, 'model.layers.52': 5, 'model.layers.53': 5, 'model.layers.54': 6, 'model.layers.55': 6, 'model.layers.56': 6, 'model.layers.57': 6, 'model.layers.58': 6, 'model.layers.59': 6, 'model.layers.60': 6, 'model.layers.61': 6, 'model.layers.62': 6, 'model.layers.63': 7, 'model.layers.64': 7, 'model.layers.65': 7, 'model.layers.66': 7, 'model.layers.67': 7, 'model.layers.68': 7, 'model.layers.69': 7, 'model.layers.70': 7, 'model.layers.71': 7, 'model.final_layernorm': 7, 'lm_head': 7} +model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-Large-1.6", + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2", + quantization_config=quantization_config, + device_map=device_map) + +tokenizer = AutoTokenizer.from_pretrained("ai21labs/AI21-Jamba-Large-1.6") + +messages = [ + {"role": "system", "content": "You are an ancient oracle who speaks in cryptic but wise phrases, always hinting at deeper meanings."}, + {"role": "user", "content": "Hello!"}, +] + +input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors='pt').to(model.device) + +outputs = model.generate(input_ids, max_new_tokens=216) + +# ์ถœ๋ ฅ ๋””์ฝ”๋”ฉ +conversation = tokenizer.decode(outputs[0], skip_special_tokens=True) + +# ์–ด์‹œ์Šคํ„ดํŠธ์˜ ์‘๋‹ต๋งŒ ์ถ”์ถœ +assistant_response = conversation.split(messages[-1]['content'])[1].strip() +print(assistant_response) +# ์ถœ๋ ฅ: Seek and you shall find. The path is winding, but the journey is enlightening. What wisdom do you seek from the ancient echoes? +``` + +## ์ฐธ๊ณ [[notes]] + +- ๋ชจ๋ธ ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Mamba ๋ธ”๋ก์€ ์–‘์žํ™”ํ•˜์ง€ ๋งˆ์„ธ์š”. +- ์ตœ์ ํ™”๋œ Mamba ์ปค๋„ ์—†์ด Mamba๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง€์—ฐ ์‹œ๊ฐ„์ด ํฌ๊ฒŒ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์ปค๋„ ์—†์ด Mamba๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด [`~AutoModel.from_pretrained`]์—์„œ `use_mamba_kernels=False`๋กœ ์„ค์ •ํ•˜์„ธ์š”. + + ```py + import torch + from transformers import AutoModelForCausalLM + model = AutoModelForCausalLM.from_pretrained("ai21labs/AI21-Jamba-1.5-Large", + use_mamba_kernels=False) + ``` + +## JambaConfig[[transformers.JambaConfig]] + +[[autodoc]] JambaConfig + + +## JambaModel[[transformers.JambaModel]] + +[[autodoc]] JambaModel + - forward + + +## JambaForCausalLM[[transformers.JambaForCausalLM]] + +[[autodoc]] JambaForCausalLM + - forward + + +## JambaForSequenceClassification[[transformers.JambaForSequenceClassification]] + +[[autodoc]] transformers.JambaForSequenceClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama.md new file mode 100644 index 0000000000000000000000000000000000000000..3cd5867584c60261236929a5e28746fd62d92c29 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama.md @@ -0,0 +1,117 @@ + + +# LLaMA [[llama]] + +## ๊ฐœ์š” [[overview]] + +LLaMA ๋ชจ๋ธ์€ Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothรฉe Lacroix, Baptiste Roziรจre, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample์— ์˜ํ•ด ์ œ์•ˆ๋œ [LLaMA: Open and Efficient Foundation Language Models](https://huggingface.co/papers/2302.13971)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 7B์—์„œ 65B๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ ๊ธฐ์ดˆ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ชจ์•„๋†“์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*"LLaMA๋Š” 7B์—์„œ 65B๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๊ธฐ์ดˆ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ˆ˜์กฐ ๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผฐ๊ณ , ๊ณต๊ฐœ์ ์œผ๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ์…‹๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๊ณ  ์ˆ˜์ค€์˜ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŠนํžˆ, LLaMA-13B ๋ชจ๋ธ์€ ๋Œ€๋ถ€๋ถ„์˜ ๋ฒค์น˜๋งˆํฌ์—์„œ GPT-3 (175B)๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋ฉฐ, LLaMA-65B๋Š” ์ตœ๊ณ  ์ˆ˜์ค€์˜ ๋ชจ๋ธ์ธ Chinchilla-70B์™€ PaLM-540B์— ๋ฒ„๊ธˆ๊ฐ€๋Š” ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ๋ชจ๋ธ์„ ์—ฐ๊ตฌ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค."* + +ํŒ: + +- LLaMA ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋Š” [์ด ์–‘์‹](https://docs.google.com/forms/d/e/1FAIpQLSfqNECQnMkycAp2jP4Z9TFX0cGR4uf7b_fBxjY_OjhJILlKGA/viewform?usp=send_form)์„ ์ž‘์„ฑํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„์—๋Š” ์ด๋ฅผ [๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Transformers ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์•„๋ž˜์˜ ์˜ˆ์‹œ ๋ช…๋ น์–ด๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”: + +```bash +python src/transformers/models/llama/convert_llama_weights_to_hf.py \ + --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path +``` + +- ๋ณ€ํ™˜์„ ํ•˜์˜€๋‹ค๋ฉด ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import LlamaForCausalLM, LlamaTokenizer + +tokenizer = LlamaTokenizer.from_pretrained("/output/path") +model = LlamaForCausalLM.from_pretrained("/output/path") +``` + +์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋ธ์„ float16 ์ •๋ฐ€๋„๋กœ ์ „๋ถ€ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ์˜ ์ถฉ๋ถ„ํ•œ CPU RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (๊ฐ€์žฅ ํฐ ๋ฒ„์ „์˜ ๋ชจ๋ธ์ด ์—ฌ๋Ÿฌ ์ฒดํฌํฌ์ธํŠธ๋กœ ๋‚˜๋‰˜์–ด ์žˆ๋”๋ผ๋„, ๊ฐ ์ฒดํฌํฌ์ธํŠธ๋Š” ๋ชจ๋ธ์˜ ๊ฐ ๊ฐ€์ค‘์น˜์˜ ์ผ๋ถ€๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ๋ฅผ RAM์— ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค) 65B ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ์ด 130GB์˜ RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + + +- LLaMA ํ† ํฌ๋‚˜์ด์ €๋Š” [sentencepiece](https://github.com/google/sentencepiece)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” BPE ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. sentencepiece์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋Š” ์‹œํ€€์Šค๋ฅผ ๋””์ฝ”๋”ฉํ•  ๋•Œ ์ฒซ ํ† ํฐ์ด ๋‹จ์–ด์˜ ์‹œ์ž‘์ด๋ผ๋ฉด (์˜ˆ๋ฅผ ๋“ค์–ด "Banana"), ํ† ํฌ๋‚˜์ด์ €๋Š” ๋ฌธ์ž์—ด ์•ž์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [BlackSamorez](https://huggingface.co/BlackSamorez)์˜ ๊ธฐ์—ฌ์™€ ํ•จ๊ป˜, [zphang](https://huggingface.co/zphang)์— ์˜ํ•ด ์ œ๊ณต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Hugging Face์—์„œ์˜ ๊ตฌํ˜„ ์ฝ”๋“œ๋Š” GPT-NeoX๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉฐ [์—ฌ๊ธฐ](https://github.com/EleutherAI/gpt-neox)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๊ณ , ์ €์ž์˜ ์ฝ”๋“œ ์›๋ณธ์€ [์—ฌ๊ธฐ](https://github.com/facebookresearch/llama)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +์›๋ž˜ LLaMA ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Meta AI์—์„œ ๋ช‡ ๊ฐ€์ง€ ํ›„์† ์ž‘์—…์„ ๋ฐœํ‘œํ–ˆ์Šต๋‹ˆ๋‹ค: + +- **Llama2**: Llama2๋Š” ๊ตฌ์กฐ์ ์ธ ๋ช‡ ๊ฐ€์ง€ ์ˆ˜์ •(Grouped Query Attention)์„ ํ†ตํ•ด ๊ฐœ์„ ๋œ ๋ฒ„์ „์ด๋ฉฐ, 2์กฐ ๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ์ด ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Llama2์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ด ๋ฌธ์„œ](llama2)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +LLaMA๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ)์˜ ๊ณต์‹ ์ž๋ฃŒ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Pull Request๋ฅผ ์˜ฌ๋ ค์ฃผ์„ธ์š”! ์ถ”๊ฐ€ํ•  ์ž๋ฃŒ๋Š” ๊ธฐ์กด์˜ ์ž๋ฃŒ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +- LLaMA ๋ชจ๋ธ์„ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ํŠœ๋‹ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/bigscience-workshop/petals/blob/main/examples/prompt-tuning-sst2.ipynb#scrollTo=f04ba4d2) ๐ŸŒŽ + + + +- [Stack Exchange](https://stackexchange.com/)์—์„œ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” LLaMA๋ฅผ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์œ„ํ•œ [StackLLaMA: RLHF๋กœ LLaMA๋ฅผ ํ›ˆ๋ จํ•˜๋Š” ์‹ค์ „ ๊ฐ€์ด๋“œ](https://huggingface.co/blog/stackllama#stackllama-a-hands-on-guide-to-train-llama-with-rlhf) ๐ŸŒŽ + +โš—๏ธ ์ตœ์ ํ™” +- ์ œํ•œ๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์ง„ GPU์—์„œ xturing ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ LLaMA ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1SQUXq1AMZPSLD4mk3A3swUIc6Y2dclme?usp=sharing) ๐ŸŒŽ + +โšก๏ธ ์ถ”๋ก  +- ๐Ÿค— PEFT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ PeftModel์„ ์‚ฌ์šฉํ•˜์—ฌ LLaMA ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/DominguesM/alpaca-lora-ptbr-7b/blob/main/notebooks/02%20-%20Evaluate.ipynb) ๐ŸŒŽ +- LangChain์„ ์‚ฌ์šฉํ•˜์—ฌ PEFT ์–ด๋Œ‘ํ„ฐ LLaMA ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1l2GiSSPbajVyp2Nk3CFT4t3uH6-5TiBe?usp=sharing) ๐ŸŒŽ + +๐Ÿš€ ๋ฐฐํฌ +- ๐Ÿค— PEFT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์‚ฌ์šฉ์ž ์นœํ™”์ ์ธ UI๋กœ LLaMA ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/lxe/simple-llama-finetuner/blob/master/Simple_LLaMA_FineTuner.ipynb#scrollTo=3PM_DilAZD8T) ๐ŸŒŽ +- Amazon SageMaker์—์„œ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•ด Open-LLaMA ๋ชจ๋ธ์„ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://github.com/aws/amazon-sagemaker-examples/blob/main/introduction_to_amazon_algorithms/jumpstart-foundation-models/text-generation-open-llama.ipynb) ๐ŸŒŽ + +## LlamaConfig [[llamaconfig]] + +[[autodoc]] LlamaConfig + + +## LlamaTokenizer [[llamatokenizer]] + +[[autodoc]] LlamaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## LlamaTokenizerFast [[llamatokenizerfast]] + +[[autodoc]] LlamaTokenizerFast + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - update_post_processor + - save_vocabulary + +## LlamaModel [[llamamodel]] + +[[autodoc]] LlamaModel + - forward + + +## LlamaForCausalLM [[llamaforcausallm]] + +[[autodoc]] LlamaForCausalLM + - forward + +## LlamaForSequenceClassification [[llamaforsequenceclassification]] + +[[autodoc]] LlamaForSequenceClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama2.md new file mode 100644 index 0000000000000000000000000000000000000000..4975b2e4ab768b517fa22ad96f8075be1b0ed1ef --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama2.md @@ -0,0 +1,129 @@ + + +# Llama2 [[llama2]] + +## ๊ฐœ์š” [[overview]] + +Llama2 ๋ชจ๋ธ์€ Hugo Touvron, Louis Martin, Kevin Stone, Peter Albert, Amjad Almahairi, Ya1smine Babaei, Nikolay Bashlykov, Soumya Batra, Prajjwal Bhargava, Shruti Bhosale, Dan Bikel, Lukas Blecher, Cristian Canton Ferrer, Moya Chen, Guillem Cucurull, David Esiobu, Jude Fernandes, Jeremy Fu, Wenyin Fu, Brian Fuller, Cynthia Gao, Vedanuj Goswami, Naman Goyal, Anthony Hartshorn, Saghar Hosseini, Rui Hou, Hakan Inan, Marcin Kardas, Viktor Kerkez Madian Khabsa, Isabel Kloumann, Artem Korenev, Punit Singh Koura, Marie-Anne Lachaux, Thibaut Lavril, Jenya Lee, Diana Liskovich, Yinghai Lu, Yuning Mao, Xavier Martinet, Todor Mihaylov, Pushkar Mishra, Igor Molybog, Yixin Nie, Andrew Poulton, Jeremy Reizenstein, Rashi Rungta, Kalyan Saladi, Alan Schelten, Ruan Silva, Eric Michael Smith, Ranjan Subramanian, Xiaoqing EllenTan, Binh Tang, Ross Taylor, Adina Williams, Jian Xiang Kuan, Puxin Xu, Zheng Yan, Iliyan Zarov, Yuchen Zhang, Angela Fan, Melanie Kambadur, Sharan Narang, Aurelien Rodriguez, Robert Stojnic, Sergey Edunov, Thomas Scialom์˜ ๋…ผ๋ฌธ [LLaMA: Open Foundation and Fine-Tuned Chat Models](https://ai.meta.com/research/publications/llama-2-open-foundation-and-fine-tuned-chat-models/)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฑ„ํŒ… ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋งž๊ฒŒ ๋ฏธ์„ธ ์กฐ์ •๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํฌํ•จ๋œ 7B์—์„œ 70B ๋ฒ”์œ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๊ธฐ์ดˆ ์–ธ์–ด ๋ชจ๋ธ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค! + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ด ์—ฐ๊ตฌ์—์„œ ์šฐ๋ฆฌ๋Š” 70์–ต์—์„œ 700์–ต ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๋ฒ”์œ„์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ ๋ฐ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLMs)์˜ ๋ชจ์Œ์ธ Llama 2๋ฅผ ๊ฐœ๋ฐœ ๋ฐ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค. Llama 2-Chat๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋ฏธ์„ธ ์กฐ์ •๋œ LLMs์€ ๋Œ€ํ™” ์‚ฌ์šฉ ์‚ฌ๋ก€์— ์ตœ์ ํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชจ๋ธ์€ ํ…Œ์ŠคํŠธํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๋ฒค์น˜๋งˆํฌ์—์„œ ์˜คํ”ˆ ์†Œ์Šค ์ฑ„ํŒ… ๋ชจ๋ธ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚˜๋ฉฐ, ์œ ์šฉ์„ฑ๊ณผ ์•ˆ์ „์„ฑ์— ๋Œ€ํ•œ ์ธ์  ํ‰๊ฐ€๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋น„๊ณต๊ฐœ ์†Œ์Šค ๋ชจ๋ธ์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ ˆํ•œ ๋Œ€์•ˆ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” Llama 2-Chat์˜ ๋ฏธ์„ธ ์กฐ์ • ๋ฐ ์•ˆ์ „์„ฑ ํ–ฅ์ƒ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•˜์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ์šฐ๋ฆฌ์˜ ์ž‘์—…์„ ๊ธฐ๋ฐ˜์œผ๋กœ LLMs์˜ ์ฑ…์ž„์žˆ๋Š” ๊ฐœ๋ฐœ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.* + +[์—ฌ๊ธฐ](https://huggingface.co/models?search=llama2)์—์„œ ๋ชจ๋“  Llama2 ๋ชจ๋ธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +`Llama2` ๋ชจ๋ธ์€ `bfloat16`์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ๋˜์—ˆ์ง€๋งŒ, ์›๋ž˜ ์ถ”๋ก ์€ `float16`์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋œ ์ฒดํฌํฌ์ธํŠธ๋Š” `torch_dtype = 'float16'`์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ด๋Š” `AutoModel` API์— ์˜ํ•ด ์ฒดํฌํฌ์ธํŠธ๋ฅผ `torch.float32`์—์„œ `torch.float16`์œผ๋กœ ์บ์ŠคํŒ…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +์˜จ๋ผ์ธ ๊ฐ€์ค‘์น˜์˜ `dtype`์€ `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ `torch_dtype="auto"`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ ๋Œ€๋ถ€๋ถ„ ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ชจ๋ธ์ด ๋จผ์ € ๋‹ค์šด๋กœ๋“œ๋  ๊ฒƒ์ด๊ณ  (์˜จ๋ผ์ธ ์ฒดํฌํฌ์ธํŠธ์˜ `dtype`์„ ์‚ฌ์šฉํ•˜์—ฌ) ๊ทธ๋‹ค์Œ์— ๊ธฐ๋ณธ `dtype`์ธ `torch`๋กœ ์บ์ŠคํŒ…ํ•˜๊ณ (`torch.float32`๊ฐ€ ๋จ), ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ตฌ์„ฑ(configuration)์—์„œ ์ œ๊ณต๋œ `torch_dtype`์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ `float16`์—์„œ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ `nan`์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์€ `bfloat16`์—์„œ ํ›ˆ๋ จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +๐Ÿฏ ํŒ: + +- Llama2 ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋Š” [์ด ์–‘์‹](https://ai.meta.com/resources/models-and-libraries/llama-downloads/)์„ ์ž‘์„ฑํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์•„ํ‚คํ…์ฒ˜๋Š” ์ฒ˜์Œ ๋ฒ„์ „์˜ Llama์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜๋ฉฐ, [์ด ๋…ผ๋ฌธ](https://huggingface.co/papers/2305.13245)์˜ ๋‚ด์šฉ์— ๋”ฐ๋ผ Grouped Query Attention (GQA)์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- `config.pretraining_tp`๋ฅผ 1๊ณผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋” ์ •ํ™•ํ•˜์ง€๋งŒ ๋А๋ฆฐ ์„ ํ˜• ๋ ˆ์ด์–ด ๊ณ„์‚ฐ์ด ํ™œ์„ฑํ™”๋˜์–ด ์›๋ณธ ๋กœ์ง“๊ณผ ๋” ์ž˜ ์ผ์น˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. +- ์›๋ž˜ ๋ชจ๋ธ์€ `pad_id = -1`์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” ํŒจ๋”ฉ ํ† ํฐ์ด ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋กœ์ง์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ `tokenizer.add_special_tokens({"pad_token":""})`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒจ๋”ฉ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ด์— ๋”ฐ๋ผ ํ† ํฐ ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `model.config.pad_token_id`๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ `embed_tokens` ๋ ˆ์ด์–ด๋Š” `self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.config.padding_idx)`๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด, ํŒจ๋”ฉ ํ† ํฐ ์ธ์ฝ”๋”ฉ์ด 0์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐํ™” ์‹œ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. +- ์–‘์‹์„ ์ž‘์„ฑํ•˜๊ณ  ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์–ป์€ ํ›„์—๋Š” ์ด๋ฏธ ๋ณ€ํ™˜๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ์ž์‹ ์˜ ๋ชจ๋ธ์„ ์ง์ ‘ ๋ณ€ํ™˜ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, [๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py)๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ˆ์‹œ์˜ ๋ช…๋ น์–ด๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +python src/transformers/models/llama/convert_llama_weights_to_hf.py \ + --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path +``` + +- ๋ณ€ํ™˜ ํ›„ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +from transformers import LlamaForCausalLM, LlamaTokenizer + +tokenizer = LlamaTokenizer.from_pretrained("/output/path") +model = LlamaForCausalLM.from_pretrained("/output/path") +``` + +์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ชจ๋ธ์„ float16 ์ •๋ฐ€๋„๋กœ ์ „๋ถ€ ํ˜ธ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ถฉ๋ถ„ํ•œ CPU RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค (๊ฐ€์žฅ ํฐ ๋ฒ„์ „์ด ์—ฌ๋Ÿฌ ์ฒดํฌํฌ์ธํŠธ๋กœ ์ œ๊ณต๋˜๋”๋ผ๋„ ๊ฐ ์ฒดํฌํฌ์ธํŠธ๋Š” ๋ชจ๋ธ ๊ฐ€์ค‘์น˜์˜ ์ผ๋ถ€๋งŒ์„ ํฌํ•จํ•˜๋ฏ€๋กœ ๋ชจ๋‘ RAM์— ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). 75B ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ์ด 145GB์˜ RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +- LLaMA ํ† ํฌ๋‚˜์ด์ €๋Š” [sentencepiece](https://github.com/google/sentencepiece)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ BPE ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. sentencepiece์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋Š” ์‹œํ€€์Šค๋ฅผ ๋””์ฝ”๋”ฉํ•  ๋•Œ ์ฒซ ๋ฒˆ์งธ ํ† ํฐ์ด ๋‹จ์–ด์˜ ์‹œ์ž‘์ด๋ฉด (์˜ˆ: "Banana") ํ† ํฌ๋‚˜์ด์ €๋Š” ๋ฌธ์ž์—ด ์•ž์— ์ ‘๋‘์‚ฌ ๊ณต๊ฐ„์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [Arthur Zucker](https://huggingface.co/ArthurZ)๊ฐ€ [Lysandre Debut](https://huggingface.co/lysandre)์˜ ๋„์›€์„ ๋ฐ›์•„ ์ œ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค. Hugging Face์—์„œ์˜ ๊ตฌํ˜„ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/EleutherAI/gpt-neox)์˜ GPT-NeoX ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ €์ž์˜ ์›๋ž˜ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/facebookresearch/llama)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +LLaMA2๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  Hugging Face์˜ ๊ณต์‹ ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ) ๋ฆฌ์†Œ์Šค ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ƒˆ๋กœ์šด ๋ฆฌ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ Pull Request๋ฅผ ์—ด์–ด ์ฃผ์‹œ๋ฉด ๊ฒ€ํ† ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฆฌ์†Œ์Šค๋Š” ๊ธฐ์กด ๋ฆฌ์†Œ์Šค์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค. + +- [Llama 2 is here - get it on Hugging Face](https://huggingface.co/blog/llama2), Llama 2์— ๊ด€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ์™€ ๐Ÿค— Transformers ๋ฐ ๐Ÿค— PEFT์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. +- [LLaMA 2 - Every Resource you need](https://www.philschmid.de/llama-2), LLaMA 2์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ๋น ๋ฅด๊ฒŒ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ด€๋ จ ๋ฆฌ์†Œ์Šค์˜ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. + + + +- Google Colab์—์„œ QLoRA์™€ 4-bit ์ •๋ฐ€๋„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Llama 2๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1PEQyJO1-f6j0S_XJ8DV50NkpzasXkrzd?usp=sharing)์ž…๋‹ˆ๋‹ค. ๐ŸŒŽ +- "Llama-v2-7b-guanaco" ๋ชจ๋ธ์„ 4-bit QLoRA๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  PDF์—์„œ Q&A ๋ฐ์ดํ„ฐ์…‹์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/134o_cXcMe_lsvl15ZE_4Y75Kstepsntu?usp=sharing)์ž…๋‹ˆ๋‹ค. ๐ŸŒŽ + +โš—๏ธ ์ตœ์ ํ™” +- [Llama 2๋ฅผ DPO๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://huggingface.co/blog/dpo-trl), TRL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ DPO ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฐ์ดํ„ฐ์…‹์—์„œ Llama 2๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•˜๋Š” ๊ฐ€์ด๋“œ์ž…๋‹ˆ๋‹ค. +- [ํ™•์žฅ ๊ฐ€์ด๋“œ: Llama 2 ๋ช…๋ น์–ด ์กฐ์ •](https://www.philschmid.de/instruction-tune-llama-2), ์ž…๋ ฅ์—์„œ ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก Llama 2๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•˜๋Š” ๊ฐ€์ด๋“œ๋กœ, ๋ช…๋ น์–ด๋ฅผ ๋”ฐ๋ฅด๋Š” ๋ชจ๋ธ์—์„œ ๋ช…๋ น์–ด๋ฅผ ์ฃผ๋Š” ๋ชจ๋ธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +- ๊ฐœ์ธ ์ปดํ“จํ„ฐ์—์„œ QLoRA์™€ TRL์„ ์‚ฌ์šฉํ•˜์—ฌ Llama 2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1SYpgFpcmtIUzdE7pxqknrM4ArCASfkFQ?usp=sharing)์ž…๋‹ˆ๋‹ค. ๐ŸŒŽ + +โšก๏ธ ์ถ”๋ก  +- AutoGPTQ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ GPTQ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Llama 2 ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1TC56ArKerXUpbgRy5vM3woRsbTEVNq7h?usp=sharing)์ž…๋‹ˆ๋‹ค. ๐ŸŒŽ +- ๋กœ์ปฌ ์ปดํ“จํ„ฐ๋‚˜ Google Colab์—์„œ 4-bit ์–‘์žํ™”๋กœ Llama 2 ์ฑ„ํŒ… ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1X1z9Q6domMKl2CnEM0QGHNwidLfR4dW2?usp=sharing)์ž…๋‹ˆ๋‹ค. ๐ŸŒŽ + +๐Ÿš€ ๋ฐฐํฌ +- [Amazon SageMaker์—์„œ LLaMA 2 (7-70B) ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](https://www.philschmid.de/sagemaker-llama2-qlora), Amazon SageMaker์—์„œ QLoRA ๋ฏธ์„ธ ์กฐ์ • ๋ฐ ๋ฐฐํฌ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€์˜ ์™„์ „ํ•œ ๊ฐ€์ด๋“œ์ž…๋‹ˆ๋‹ค. +- [Amazon SageMaker์—์„œ Llama 2 7B/13B/70B ๋ฐฐํฌํ•˜๊ธฐ](https://www.philschmid.de/sagemaker-llama-llm), ์•ˆ์ „ํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด Hugging Face์˜ LLM DLC ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ฐ€์ด๋“œ์ž…๋‹ˆ๋‹ค. + + +## LlamaConfig [[llamaconfig]] + +[[autodoc]] LlamaConfig + + +## LlamaTokenizer [[llamatokenizer]] + +[[autodoc]] LlamaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## LlamaTokenizerFast [[llamatokenizerfast]] + +[[autodoc]] LlamaTokenizerFast + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - update_post_processor + - save_vocabulary + +## LlamaModel [[llamamodel]] + +[[autodoc]] LlamaModel + - forward + + +## LlamaForCausalLM [[llamaforcausallm]] + +[[autodoc]] LlamaForCausalLM + - forward + +## LlamaForSequenceClassification [[llamaforsequenceclassification]] + +[[autodoc]] LlamaForSequenceClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama3.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama3.md new file mode 100644 index 0000000000000000000000000000000000000000..ca819bfcabaf882e54f5f175fdad3732e1e22cd1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/llama3.md @@ -0,0 +1,80 @@ + + +# Llama3[[llama3]] + +```py3 +import transformers +import torch + +model_id = "meta-llama/Meta-Llama-3-8B" + +pipeline = transformers.pipeline("text-generation", model=model_id, model_kwargs={"torch_dtype": torch.bfloat16}, device_map="auto") +pipeline("Hey how are you doing today?") +``` + +## ๊ฐœ์š”[[overview]] + +๋ผ๋งˆ3 ๋ชจ๋ธ์€ Meta AI ํŒ€์ด ์ œ์•ˆํ•œ [๋ฉ”ํƒ€ ๋ผ๋งˆ3 ์†Œ๊ฐœ: ํ˜„์žฌ๊นŒ์ง€ ๊ฐ€์žฅ ์œ ๋Šฅํ•œ ๊ณต๊ฐœ ๊ฐ€๋Šฅ LLM](https://ai.meta.com/blog/meta-llama-3/)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์˜ค๋Š˜, ๊ด‘๋ฒ”์œ„ํ•œ ์‚ฌ์šฉ์„ ์œ„ํ•ด ์ด์šฉ ๊ฐ€๋Šฅํ•œ ๋ผ๋งˆ์˜ ์ฐจ์„ธ๋Œ€ ๋ชจ๋ธ์ธ ๋ฉ”ํƒ€ ๋ผ๋งˆ3์˜ ์ฒซ ๋‘ ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๊ฒŒ ๋˜์–ด ๊ธฐ์ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ถœ์‹œ๋Š” 8B์™€ 70B ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ์‚ฌ์ „ ํ›ˆ๋ จ ๋ฐ ์ง€์‹œ ๋ฏธ์„ธ ์กฐ์ •๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ํŠน์ง•์œผ๋กœ ํ•˜๋ฉฐ, ๊ด‘๋ฒ”์œ„ํ•œ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์ง€์›ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ผ๋งˆ์˜ ์ด ์ฐจ์„ธ๋Œ€ ๋ชจ๋ธ์€ ๋‹ค์–‘ํ•œ ์‚ฐ์—… ๋ฒค์น˜๋งˆํฌ์—์„œ ์ตœ์ฒจ๋‹จ์˜ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, ๊ฐœ์„ ๋œ ์ถ”๋ก  ๋Šฅ๋ ฅ์„ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ๋“ค์ด ๋‹จ์—ฐ์ฝ” ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ ์ตœ๊ณ ์˜ ์˜คํ”ˆ ์†Œ์Šค ๋ชจ๋ธ์ด๋ผ๊ณ  ๋ฏฟ์Šต๋‹ˆ๋‹ค. ์˜ค๋žœ ๊ฐœ๋ฐฉ์  ์ ‘๊ทผ ๋ฐฉ์‹์„ ์ง€์ง€ํ•˜๋ฉฐ, ์šฐ๋ฆฌ๋Š” ๋ผ๋งˆ3๋ฅผ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธฐ์—ฌ์ž๋“ค์—๊ฒŒ ๋งก๊ธฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ, ํ‰๊ฐ€, ์ถ”๋ก  ์ตœ์ ํ™” ๋“ฑ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ AI ์Šคํƒ ์ „๋ฐ˜์— ๊ฑธ์นœ ๋‹ค์Œ ํ˜์‹ ์˜ ๋ฌผ๊ฒฐ์„ ์ด‰๋ฐœํ•˜๊ธธ ํฌ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฌด์—‡์„ ๋งŒ๋“ค์ง€ ๊ธฐ๋Œ€ํ•˜๋ฉฐ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”ผ๋“œ๋ฐฑ์„ ๊ณ ๋Œ€ํ•ฉ๋‹ˆ๋‹ค.* + +๋ผ๋งˆ3 ๋ชจ๋ธ์˜ ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ๋Š” [์ด๊ณณ](https://huggingface.co/models?search=llama3)์—์„œ ํ™•์ธํ•˜์„ธ์š”. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/meta-llama/llama3)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + + + +`๋ผ๋งˆ3` ๋ชจ๋ธ๋“ค์€ `bfloat16`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ๋˜์—ˆ์ง€๋งŒ, ์›๋ž˜์˜ ์ถ”๋ก ์€ `float16`์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Hub์— ์—…๋กœ๋“œ๋œ ์ฒดํฌํฌ์ธํŠธ๋“ค์€ `torch_dtype = 'float16'`์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” `AutoModel` API๊ฐ€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ `torch.float32`์—์„œ `torch.float16`์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ์ด์šฉ๋ฉ๋‹ˆ๋‹ค. + + `model = AutoModelForCausalLM.from_pretrained("path", torch_dtype = "auto")`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ, ์˜จ๋ผ์ธ ๊ฐ€์ค‘์น˜์˜ `dtype`๋Š” `torch_dtype="auto"`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ ๋Œ€๋ถ€๋ถ„ ๋ฌด๊ด€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ชจ๋ธ์ด ๋จผ์ € ๋‹ค์šด๋กœ๋“œ๋˜๊ณ (์˜จ๋ผ์ธ ์ฒดํฌํฌ์ธํŠธ์˜ `dtype`๋ฅผ ์‚ฌ์šฉ), ๊ทธ ๋‹ค์Œ `torch`์˜ `dtype`์œผ๋กœ ๋ณ€ํ™˜๋˜์–ด(`torch.float32`๊ฐ€ ๋จ), ๋งˆ์ง€๋ง‰์œผ๋กœ config์— `torch_dtype`์ด ์ œ๊ณต๋œ ๊ฒฝ์šฐ ๊ฐ€์ค‘์น˜๊ฐ€ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +`float16`์œผ๋กœ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ `nan`์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“  ๋ชจ๋ธ์€ `bfloat16`์œผ๋กœ ํ›ˆ๋ จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +ํŒ: + +- ๋ผ๋งˆ3 ๋ชจ๋ธ์„ ์œ„ํ•œ ๊ฐ€์ค‘์น˜๋Š” [์ด ํผ](https://ai.meta.com/resources/models-and-libraries/llama-downloads/)์„ ์ฑ„์šฐ๋ฉด์„œ ์–ป์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์•„ํ‚คํ…์ฒ˜๋Š” ๋ผ๋งˆ2์™€ ์ •ํ™•ํžˆ ๊ฐ™์Šต๋‹ˆ๋‹ค. +- ํ† ํฌ๋‚˜์ด์ €๋Š” [tiktoken](https://github.com/openai/tiktoken) (sentencepiece ๊ตฌํ˜„์— ๊ธฐ๋ฐ˜ํ•œ ๋ผ๋งˆ2 ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ)์— ๊ธฐ๋ฐ˜ํ•œ BPE ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. tiktoken ๊ธฐ๋ฐ˜ ํ† ํฌ๋‚˜์ด์ €๊ฐ€ sebtencepiece ๊ธฐ๋ฐ˜ ๋ฐฉ์‹๊ณผ ๋‹ค๋ฅธ์ ์€ ์ž…๋ ฅ ํ† ํฐ์ด vocab์— ์ด๋ฏธ ์กด์žฌํ•  ๋•Œ BPE ๋ณ‘ํ•ฉ ๋ฃฐ์„ ๋ฌด์‹œํ•˜๊ณ  ์‹ฑ๊ธ€ ํ† ํฐ์œผ๋กœ ํ† ํฌ๋‚˜์ด์ง•ํ•œ๋‹ค๋Š” ์ ์—์„œ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ž์„ธํžˆ ๋งํ•˜๋ฉด `"hugging"`์ด vocab์— ์กด์žฌํ•˜๊ณ  ๊ธฐ์กด์— ๋ณ‘ํ•ฉ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด, `["hug","ging"]` ์ฒ˜๋Ÿผ ๋‘ ํ† ํฐ์œผ๋กœ ๋” ์ž‘์€ ๋‹จ์œ„์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•˜๋‚˜์˜ ํ† ํฐ๋งŒ์„ ์ž๋™์œผ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. +- ๊ธฐ๋ณธ ๋ชจ๋ธ์€ ํŒจ๋”ฉ ํ† ํฐ์ด ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” `pad_id = -1`์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๋กœ์ง์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋‹ˆ `tokenizer.add_special_tokens({"pad_token":""})`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ๋„ ํ™•์‹คํžˆ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `model.config.pad_token_id`๋„ ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ `embed_tokens` ๋ ˆ์ด์–ด๋Š” `self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.config.padding_idx)`๋กœ ์ดˆ๊ธฐํ™”๋˜๋ฉฐ, ํŒจ๋”ฉ ํ† ํฐ์„ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ด 0(zero)๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•  ๊ฒƒ์ธ์ง€ ๊ทธ๋ž˜์„œ ์ดˆ๊ธฐํ™”๊ฐ€ ์ถ”์ฒœ๋ ๋•Œ ์ด๋ฅผ ํ†ตํ™”์‹œํ‚ฌ ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. +- ์›๋ณธ ์ฒดํฌํฌ์ธํŠธ๋Š” ์ด [๋ณ€ํ™˜ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py)๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```bash + python src/transformers/models/llama/convert_llama_weights_to_hf.py \ + --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path --llama_version 3 + ``` + +- ๋ณ€ํ™˜ ํ›„, ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‹ค์Œ์„ ํ†ตํ•ด ๋กœ๋“œ๋œ๋‹ค. + + ```python + from transformers import AutoModelForCausalLM, AutoTokenizer + + tokenizer = AutoTokenizer.from_pretrained("/output/path") + model = AutoModelForCausalLM.from_pretrained("/output/path") + ``` + + ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ ค๋ฉด ๋ชจ๋ธ ์ „์ฒด๋ฅผ float16 ์ •๋ฐ€๋„๋กœ ํ˜ธ์ŠคํŒ…ํ•  ์ˆ˜ ์žˆ๋Š” ์ถฉ๋ถ„ํ•œ ๋ฉ”์ธ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•˜์„ธ์š”. ๊ฐ€์žฅ ํฐ ๋ฒ„์ „์ด ์—ฌ๋Ÿฌ ์ฒดํฌํฌ์ธํŠธ๋กœ ๋‚˜๋‰˜์–ด ์žˆ๋”๋ผ๋„, ๊ฐ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜ ์ผ๋ถ€๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ชจ๋‘ RAM์— ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 75B ๋ชจ๋ธ์„ ์˜ˆ๋กœ ๋“ค๋ฉด ๋Œ€๋žต 145GB์˜ RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +- `attn_implementation="flash_attention_2"`๋ฅผ ํ†ตํ•ด์„œ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, `from_pretrained` ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์— `torch_dtype`๋ฅผ ์ „๋‹ฌํ•˜์ง€ ๋ง๊ณ  ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„(Automatic Mixed-Precision) ํ•™์Šต์„ ์‚ฌ์šฉํ•˜์„ธ์š”. `Trainer`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹จ์ˆœํžˆ `fp16` ๋˜๋Š” `bf16`์„ `True`๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ฐ˜๋“œ์‹œ `torch.autocast`๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์€ `fp16`๊ณผ `bf16` ๋ฐ์ดํ„ฐ ์œ ํ˜•๋งŒ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +## ์ž๋ฃŒ[[resources]] + +[๋ผ๋งˆ2](./llama2) ๋ฌธ์„œ ํŽ˜์ด์ง€์—์„œ๋Š” ์ด๋ฏธ ์ˆ˜ ๋งŽ์€ ๋ฉ‹์ง€๊ณ  ์œ ์ตํ•œ ์ž๋ฃŒ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ณณ์— ๋ผ๋งˆ3์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ์ž๋ฃŒ๋ฅผ ๋”ํ•ด์ฃผ์‹ค ์ปจํŠธ๋ฆฌ๋ทฐํ„ฐ๋“ค์„ ์ดˆ๋Œ€ํ•ฉ๋‹ˆ๋‹ค! ๐Ÿค— diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba.md new file mode 100644 index 0000000000000000000000000000000000000000..001ea609932219fb59f944b5f21f6b53e090c2bb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba.md @@ -0,0 +1,89 @@ + + +# ๋ง˜๋ฐ”[[mamba]] + +## ๊ฐœ์š”[[overview]] + +๋ง˜๋ฐ”(Mamba) ๋ชจ๋ธ์€ Albert Gu, Tri Dao๊ฐ€ ์ œ์•ˆํ•œ [๋ง˜๋ฐ”: ์„ ํƒ์  ์ƒํƒœ ๊ณต๊ฐ„์„ ์ด์šฉํ•œ ์„ ํ˜• ์‹œ๊ฐ„ ์‹œํ€€์Šค ๋ชจ๋ธ๋ง](https://huggingface.co/papers/2312.00752)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ `state-space-models`์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์ƒˆ๋กœ์šด ํŒจ๋Ÿฌ๋‹ค์ž„ ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์ง๊ด€์ ์ธ ์ดํ•ด๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด [์ด๊ณณ](https://srush.github.io/annotated-s4/)์„ ์ฐธ๊ณ  ํ•˜์„ธ์š”. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*ํ˜„์žฌ ๋”ฅ๋Ÿฌ๋‹์—์„œ ํฅ๋ฏธ๋กœ์šด ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ๋™ํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์ดˆ ๋ชจ๋ธ๋“ค์€ ๊ฑฐ์˜ ๋ณดํŽธ์ ์œผ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜์™€ ๊ทธ ํ•ต์‹ฌ ์–ดํ…์…˜ ๋ชจ๋“ˆ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์„ ํ˜• ์–ดํ…์…˜, ๊ฒŒ์ดํŠธ๋œ ์ปจ๋ณผ๋ฃจ์…˜๊ณผ ์ˆœํ™˜ ๋ชจ๋ธ, ๊ตฌ์กฐํ™”๋œ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ(SSM) ๋“ฑ ๋งŽ์€ ์ค€์ด์ฐจ์‹œ๊ฐ„(subquadratic-time) ์•„ํ‚คํ…์ฒ˜๊ฐ€ ๊ธด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ณ„์‚ฐ ๋น„ํšจ์œจ์„ฑ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœ๋˜์—ˆ์ง€๋งŒ, ์–ธ์–ด์™€ ๊ฐ™์€ ์ค‘์š”ํ•œ ์–‘์‹์—์„œ๋Š” ์–ดํ…์…˜๋งŒํผ ์„ฑ๋Šฅ์„ ๋‚ด์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์˜ ์ฃผ์š” ์•ฝ์ ์ด ๋‚ด์šฉ ๊ธฐ๋ฐ˜ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์ ์ž„์„ ์•Œ๊ณ  ๋ช‡ ๊ฐ€์ง€๋ฅผ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ, SSM ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ์˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์ด์‚ฐ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ(discrete modalities)์˜ ์•ฝ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์–ด, ํ˜„์žฌ ํ† ํฐ์— ๋”ฐ๋ผ ์‹œํ€€์Šค ๊ธธ์ด ์ฐจ์›์„ ๋”ฐ๋ผ ์ •๋ณด๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์ „ํŒŒํ•˜๊ฑฐ๋‚˜ ์žŠ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ, ์ด๋Ÿฌํ•œ ๋ณ€๊ฒฝ์œผ๋กœ ํšจ์œจ์ ์ธ ์ปจ๋ณผ๋ฃจ์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์—ˆ์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” ์ˆœํ™˜ ๋ชจ๋“œ์—์„œ ํ•˜๋“œ์›จ์–ด๋ฅผ ์ธ์‹ํ•˜๋Š” ๋ณ‘๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค๊ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์„ ํƒ์  SSM์„ ์–ดํ…์…˜์ด๋‚˜ MLP ๋ธ”๋ก๋„ ์—†๋Š” ๋‹จ์ˆœํ™”๋œ ์ข…๋‹จ๊ฐ„ ์‹ ๊ฒฝ๋ง ์•„ํ‚คํ…์ฒ˜์ธ ๋ง˜๋ฐ”์— ํ†ตํ•ฉ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ๋ง˜๋ฐ”๋Š” ๋น ๋ฅธ ์ถ”๋ก (ํŠธ๋žœ์Šคํฌ๋จธ๋ณด๋‹ค 5๋ฐฐ ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰)๊ณผ ์‹œํ€€์Šค ๊ธธ์ด์— ๋Œ€ํ•œ ์„ ํ˜• ํ™•์žฅ์„ฑ์„ ๋ˆ„๋ฆฌ๋ฉฐ, ๋ฐฑ๋งŒ ๊ธธ์ด ์‹œํ€€์Šค๊นŒ์ง€ ์‹ค์ œ ๋ฐ์ดํ„ฐ์—์„œ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์‹œํ€€์Šค ๋ชจ๋ธ ๋ฐฑ๋ณธ์œผ๋กœ์„œ ๋ง˜๋ฐ”๋Š” ์–ธ์–ด, ์˜ค๋””์˜ค, ์œ ์ „์ฒดํ•™๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ์–‘์‹์—์„œ ์ตœ์ฒจ๋‹จ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ๋ง์—์„œ ์šฐ๋ฆฌ์˜ ๋ง˜๋ฐ”-3B ๋ชจ๋ธ์€ ๊ฐ™์€ ํฌ๊ธฐ์˜ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ๋Šฅ๊ฐ€ํ•˜๊ณ  ๋‘ ๋ฐฐ ํฌ๊ธฐ์˜ ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๋งž๋จน๋Š” ์„ฑ๋Šฅ์„ ๋ณด์ด๋ฉฐ, ์‚ฌ์ „ ํ›ˆ๋ จ๊ณผ ๋‹ค์šด์ŠคํŠธ๋ฆผ ํ‰๊ฐ€ ๋ชจ๋‘์—์„œ ์„ฑ๋Šฅ์„ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.* + +ํŒ: + +- ๋ง˜๋ฐ”๋Š” ๊ณ ์ „์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๊ฒฌ์ค„ ๋งŒํ•œ ์ƒˆ๋กœ์šด `์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ` ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๊ตฌ์กฐํ™”๋œ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ์˜ ๋ฐœ์ „ ์„ ์ƒ์— ์žˆ์œผ๋ฉฐ, [ํ”Œ๋ž˜์‹œ์–ดํ…์…˜](https://github.com/Dao-AILab/flash-attention)์˜ ์ •์‹ ์„ ๋”ฐ๋ฅด๋Š” ํšจ์œจ์ ์ธ ํ•˜๋“œ์›จ์–ด ์ธ์‹ ์„ค๊ณ„์™€ ๊ตฌํ˜„์„ ํŠน์ง•์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ง˜๋ฐ”๋Š” `์–ดํ…์…˜` ๋ ˆ์ด์–ด์™€ ๋™๋“ฑํ•œ `๋ฏน์„œ(mixer)` ๋ ˆ์ด์–ด๋ฅผ ์Œ“์Šต๋‹ˆ๋‹ค. `๋ง˜๋ฐ”`์˜ ํ•ต์‹ฌ ๋กœ์ง์€ `MambaMixer` ํด๋ž˜์Šค์— ์žˆ์Šต๋‹ˆ๋‹ค. +- ๋‘ ๊ฐ€์ง€ ๊ตฌํ˜„์ด ๊ณต์กดํ•ฉ๋‹ˆ๋‹ค: ํ•˜๋‚˜๋Š” ์ตœ์ ํ™”๋˜์–ด ๋น ๋ฅธ cuda์ปค๋„์„ ์‚ฌ์šฉํ•˜๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๋‹จ์ˆœํ•˜์ง€๋งŒ ๋ชจ๋“  ์žฅ์น˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! +- ํ˜„์žฌ ๊ตฌํ˜„์€ ์›๋ณธ cuda์ปค๋„์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค: ๋ง˜๋ฐ”๋ฅผ ์œ„ํ•œ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์€ [`mamba-ssm`](https://github.com/state-spaces/mamba)์™€ [`causal_conv1d`](https://github.com/Dao-AILab/causal-conv1d) ์ €์žฅ์†Œ์— ํ˜ธ์ŠคํŒ…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋“œ์›จ์–ด๊ฐ€ ์ง€์›ํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ์„ค์น˜ํ•˜์„ธ์š”! +- cuda ์ปค๋„์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ ๋ณด๋‹ค๋Š”, ๋‹จ์ˆœํ•˜์ง€๋งŒ ๋ชจ๋“  ์žฅ์น˜์—์„œ ์‹คํ–‰๊ฐ€๋Šฅํ•˜๋„๋กํ•˜๋Š” ๋ฐฉํ–ฅ์ธ '๋‹จ์ˆœ๊ตฌํ˜„'์˜ ์„ฑ๋Šฅ์„ ๋น ๋ฅด๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๊ธฐ์—ฌ๋ฅผ ๋” ํ™˜์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค— + +์ด ๋ชจ๋ธ์€ [ArthurZ](https://huggingface.co/ArthurZ)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/state-spaces/mamba)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +# ์‚ฌ์šฉ + +### ๊ฐ„๋‹จํ•œ ์ƒ์„ฑ ์˜ˆ์ œ + +```python +from transformers import MambaConfig, MambaForCausalLM, AutoTokenizer +import torch + +tokenizer = AutoTokenizer.from_pretrained("state-spaces/mamba-130m-hf") +model = MambaForCausalLM.from_pretrained("state-spaces/mamba-130m-hf") +input_ids = tokenizer("Hey how are you doing?", return_tensors= "pt")["input_ids"] + +out = model.generate(input_ids, max_new_tokens=10) +print(tokenizer.batch_decode(out)) +``` + +### Peft ํŒŒ์ธํŠœ๋‹ +๋А๋ฆฐ ๋ฒ„์ „์€ ํ•™์Šต์—์„œ ์•„์ฃผ ์•ˆ์ •์ ์ด์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ๋น ๋ฅธ ๋ฒ„์ „์€ `float32`๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค! + +```python +from datasets import load_dataset +from trl import SFTConfig, SFTTrainer +from peft import LoraConfig + +model_id = "state-spaces/mamba-130m-hf" +dataset = load_dataset("Abirate/english_quotes", split="train") +training_args = SFTConfig(dataset_text_field="quote") +lora_config = LoraConfig(target_modules=["x_proj", "embeddings", "in_proj", "out_proj"]) +trainer = SFTTrainer( + model=model_id, + args=training_args, + train_dataset=dataset, + peft_config=lora_config, +) +trainer.train() +``` + +## MambaConfig + +[[autodoc]] MambaConfig + +## MambaModel + +[[autodoc]] MambaModel + - forward + +## MambaLMHeadModel + +[[autodoc]] MambaForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba2.md new file mode 100644 index 0000000000000000000000000000000000000000..04ef4d070b87b99019f460ee36144e81cd4eab89 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mamba2.md @@ -0,0 +1,90 @@ + + +# ๋ง˜๋ฐ”2[[mamba-2]] + +## ๊ฐœ์š”[[overview]] + +๋ง˜๋ฐ”2 ๋ชจ๋ธ์€ Tri Dao, Albert Gu๊ฐ€ ์ œ์•ˆํ•œ [ํŠธ๋žœ์Šคํฌ๋จธ๋Š” SSM์ด๋‹ค: ๊ตฌ์กฐํ™”๋œ ์ƒํƒœ ๊ณต๊ฐ„ ์ด์ค‘์„ฑ์„ ํ†ตํ•œ ์ผ๋ฐ˜ํ™”๋œ ๋ชจ๋ธ๊ณผ ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜](https://huggingface.co/papers/2405.21060)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ง˜๋ฐ”2๋Š” ๋ง˜๋ฐ”1๊ณผ ์œ ์‚ฌํ•œ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ๋กœ, ๋‹จ์ˆœํ™”๋œ ์•„ํ‚คํ…์ฒ˜์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์–ธ์–ด ๋ชจ๋ธ๋ง์—์„œ ๋”ฅ๋Ÿฌ๋‹ ์„ฑ๊ณต์˜ ์ฃผ์š” ์•„ํ‚คํ…์ฒ˜์˜€์ง€๋งŒ, ๋ง˜๋ฐ”์™€ ๊ฐ™์€ ์ƒํƒœ ๊ณต๊ฐ„ ๋ชจ๋ธ(SSM)์ด ์ตœ๊ทผ ์†Œ๊ทœ๋ชจ ํ˜น์€ ์ค‘๊ฐ„ ๊ทœ๋ชจ์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๋Œ€๋“ฑํ•˜๊ฑฐ๋‚˜ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ๋ชจ๋ธ ๊ณ„์—ด๋“ค์ด ์‹ค์ œ๋กœ ๋งค์šฐ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ์Œ์„ ํŒŒ์•…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตฌ์กฐํ™”๋œ ์ค€๋ถ„๋ฆฌ(semiseparable) ํ–‰๋ ฌ ์ค‘ ์—ฐ๊ตฌ๊ฐ€ ์ž˜ ์ด๋ฃจ์–ด์ง„ ํด๋ž˜์Šค์˜ ๋‹ค์–‘ํ•œ ๋ถ„ํ•ด๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ SSM๊ณผ ์–ดํ…์…˜ ๋ณ€ํ˜• ์‚ฌ์ด์˜ ํ’๋ถ€ํ•œ ์ด๋ก ์  ์—ฐ๊ฒฐ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์ƒํƒœ ๊ณต๊ฐ„ ์ด์ค‘์„ฑ(SSD) ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ง˜๋ฐ”1์˜ ์„ ํƒ์  SSM์„ ๊ฐœ์„ ํ•œ ์ƒˆ๋กœ์šด ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ํŠธ๋žœ์Šคํฌ๋จธ์™€ ๊ฒฝ์Ÿ๋ ฅ์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์†๋„๋Š” 2~8๋ฐฐ ๋” ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋ƒ…๋‹ˆ๋‹ค.* + +ํŒ: + +์ด ๋ฒ„์ „์€ ๋ง˜๋ฐ”2 ๊ตฌํ˜„์„ ์ง€์›ํ•ด์•ผ ํ•˜๋ฉฐ, ํŠนํžˆ Mistral AI์˜ [Mamba-2 codestral](https://huggingface.co/mistralai/Mamba-Codestral-7B-v0.1)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ, mamba 2 codestral์€ 8๊ฐœ์˜ `groups`๋กœ ์ถœ์‹œ๋˜์—ˆ๋Š”๋ฐ, ์ด๋Š” ์–ดํ…์…˜ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์˜ KV ํ—ค๋“œ ์ˆ˜์™€ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ํŒ๋‹จ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ `torch_forward`์™€ `cuda_kernels_forward`๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ „๋ฐฉ ํŒจ์Šค๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. `cuda_kernels_forward`๋Š” ํ™˜๊ฒฝ์—์„œ cuda ์ปค๋„์„ ์ฐพ์œผ๋ฉด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, prefill์—์„œ๋Š” ๋” ๋А๋ฆฝ๋‹ˆ๋‹ค. ์ฆ‰, ๋†’์€ CPU ์˜ค๋ฒ„ํ—ค๋“œ๋กœ ์ธํ•ด "์›œ์—… ์‹คํ–‰"์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/389#issuecomment-2171755306)๊ณผ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/355#issuecomment-2147597457)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +์ปดํŒŒ์ผ ์—†์ด๋Š” `torch_forward` ๊ตฌํ˜„์ด 3~4๋ฐฐ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ด ๋ชจ๋ธ์—๋Š” ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์ด ์—†์ง€๋งŒ `attention_mask`์™€ ๋ฐฐ์น˜ ์ƒ์„ฑ์˜ ๊ฒฝ์šฐ ๋‘ ๊ณณ์—์„œ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๋งˆ์Šคํ‚นํ•˜๋Š” ํŠน์ • ๋กœ์ง์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋‚ด์šฉ์€ [์ด๊ณณ](https://github.com/state-spaces/mamba/issues/66#issuecomment-1863563829)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +์ด๋กœ์ธํ•ด ๋ง˜๋ฐ”2 ์ปค๋„์˜ ์žฌ๊ตฌํ˜„๊ณผ ํ•จ๊ป˜ ๋ฐฐ์น˜ ์ƒ์„ฑ ๋ฐ ์บ์‹œ๋œ ์ƒ์„ฑ์—์„œ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ cuda ์ปค๋„ ๋˜๋Š” torch forward๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. SSM ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ…์„œ ์ˆ˜์ถ•์— ํฌ๊ฒŒ ์˜์กดํ•˜๋Š”๋ฐ, ์ด๋Š” matmul๊ณผ ๋™๋“ฑํ•˜์ง€๋งŒ ์—ฐ์‚ฐ ์ˆœ์„œ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋ฉฐ, ์ด๋กœ ์ธํ•ด ๋” ์ž‘์€ ์ •๋ฐ€๋„์—์„œ ์ฐจ์ด๊ฐ€ ๋” ์ปค์ง‘๋‹ˆ๋‹ค. + +๋˜ ๋‹ค๋ฅธ ์ฐธ๊ณ ์‚ฌํ•ญ์œผ๋กœ, ํŒจ๋”ฉ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์€๋‹‰ ์ƒํƒœ(hidden state)์˜ ์ข…๋ฃŒ๋Š” ๋‘ ๊ณณ์—์„œ ์ด๋ฃจ์–ด์ง€๋ฉฐ ์ฃผ๋กœ ์™ผ์ชฝ ํŒจ๋”ฉ์œผ๋กœ ํ…Œ์ŠคํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ํŒจ๋”ฉ์€ ๋…ธ์ด์ฆˆ๋ฅผ ์ „ํŒŒํ•˜๋ฏ€๋กœ ๋งŒ์กฑ์Šค๋Ÿฌ์šด ๊ฒฐ๊ณผ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. `tokenizer.padding_side = "left"`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ํŒจ๋”ฉ ๋ฐฉํ–ฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [Molbap](https://huggingface.co/Molbap)์ด ๊ธฐ์—ฌํ–ˆ์œผ๋ฉฐ, [Anton Vlasjuk](https://github.com/vasqu)์˜ ํฐ ๋„์›€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/state-spaces/mamba)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +# ์‚ฌ์šฉ + +### ๊ฐ„๋‹จํ•œ ์ƒ์„ฑ ์˜ˆ: +```python +from transformers import Mamba2Config, Mamba2ForCausalLM, AutoTokenizer +import torch +model_id = 'mistralai/Mamba-Codestral-7B-v0.1' +tokenizer = AutoTokenizer.from_pretrained(model_id, revision='refs/pr/9', from_slow=True, legacy=False) +model = Mamba2ForCausalLM.from_pretrained(model_id, revision='refs/pr/9') +input_ids = tokenizer("Hey how are you doing?", return_tensors= "pt")["input_ids"] + +out = model.generate(input_ids, max_new_tokens=10) +print(tokenizer.batch_decode(out)) +``` + +์ด๊ณณ์€ ๋ฏธ์„ธ์กฐ์ •์„ ์œ„ํ•œ ์ดˆ์•ˆ ์Šคํฌ๋ฆฝํŠธ์ž…๋‹ˆ๋‹ค: +```python +from datasets import load_dataset +from peft import LoraConfig +from trl import SFTConfig, SFTTrainer + +model_id = "mistralai/Mamba-Codestral-7B-v0.1" +dataset = load_dataset("Abirate/english_quotes", split="train") +training_args = SFTConfig(dataset_text_field="quote", gradient_checkpointing=True, per_device_train_batch_size=4) +lora_config = LoraConfig(target_modules=["x_proj", "embeddings", "in_proj", "out_proj"]) +trainer = SFTTrainer( + model=model_id, + args=training_args, + train_dataset=dataset, + peft_config=lora_config, +) +trainer.train() +``` + + +## Mamba2Config + +[[autodoc]] Mamba2Config + +## Mamba2Model + +[[autodoc]] Mamba2Model + - forward + +## Mamba2LMHeadModel + +[[autodoc]] Mamba2ForCausalLM + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/marian.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/marian.md new file mode 100644 index 0000000000000000000000000000000000000000..79a9641401d01e400c42de70d65f2157fefc97fa --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/marian.md @@ -0,0 +1,217 @@ + + +# MarianMT[[MarianMT]] + +
+ +Models + + +Spaces + +
+ +## ๊ฐœ์š”[[Overview]] + +BART์™€ ๋™์ผํ•œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฒˆ์—ญ ๋ชจ๋ธ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋Š” ๊ฐ ๋ชจ๋ธ ์นด๋“œ์˜ ํ…Œ์ŠคํŠธ ์„ธํŠธ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€๋Š” ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ [sshleifer](https://huggingface.co/sshleifer)๊ฐ€ ์ œ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค. + + +## ๊ตฌํ˜„ ๋…ธํŠธ[[Implementation Notes]] + +- ๊ฐ ๋ชจ๋ธ์€ ์•ฝ 298 MB๋ฅผ ์ฐจ์ง€ํ•˜๋ฉฐ, 1,000๊ฐœ ์ด์ƒ์˜ ๋ชจ๋ธ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. +- ์ง€์›๋˜๋Š” ์–ธ์–ด ์Œ ๋ชฉ๋ก์€ [์—ฌ๊ธฐ](https://huggingface.co/Helsinki-NLP)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ๋ชจ๋ธ๋“ค์€ [Jรถrg Tiedemann](https://researchportal.helsinki.fi/en/persons/j%C3%B6rg-tiedemann)์— ์˜ํ•ด [Marian](https://marian-nmt.github.io/) C++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋น ๋ฅธ ํ•™์Šต๊ณผ ๋ฒˆ์—ญ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋“  ๋ชจ๋ธ์€ 6๊ฐœ ๋ ˆ์ด์–ด๋กœ ์ด๋ฃจ์–ด์ง„ Transformer ๊ธฐ๋ฐ˜์˜ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์€ ๋ชจ๋ธ ์นด๋“œ์— ๊ธฐ์ž…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- BPE ์ „์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ 80๊ฐœ์˜ OPUS ๋ชจ๋ธ์€ ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +- ๋ชจ๋ธ๋ง ์ฝ”๋“œ๋Š” [`BartForConditionalGeneration`]์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉฐ, ์ผ๋ถ€ ์ˆ˜์ •์‚ฌํ•ญ์ด ๋ฐ˜์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: + + - ์ •์  (์‚ฌ์ธ ํ•จ์ˆ˜ ๊ธฐ๋ฐ˜) ์œ„์น˜ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉ (`MarianConfig.static_position_embeddings=True`) + - ์ž„๋ฒ ๋”ฉ ๋ ˆ์ด์–ด ์ •๊ทœํ™” ์ƒ๋žต (`MarianConfig.normalize_embedding=False`) + - ๋ชจ๋ธ์€ ์ƒ์„ฑ ์‹œ ํ”„๋ฆฌํ”ฝ์Šค๋กœ `pad_token_id` (ํ•ด๋‹น ํ† ํฐ ์ž„๋ฒ ๋”ฉ ๊ฐ’์€ 0)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค (Bart๋Š” + ``๋ฅผ ์‚ฌ์šฉ), +- Marian ๋ชจ๋ธ์„ PyTorch๋กœ ๋Œ€๋Ÿ‰ ๋ณ€ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋Š” `convert_marian_to_pytorch.py`์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ๋ชจ๋ธ ์ด๋ฆ„ ๊ทœ์น™[[Naming]] + +- ๋ชจ๋“  ๋ชจ๋ธ ์ด๋ฆ„์€ `Helsinki-NLP/opus-mt-{src}-{tgt}` ํ˜•์‹์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์˜ ์–ธ์–ด ์ฝ”๋“œ ํ‘œ๊ธฐ๋Š” ์ผ๊ด€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘ ์ž๋ฆฌ ์ฝ”๋“œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ [์—ฌ๊ธฐ](https://developers.google.com/admin-sdk/directory/v1/languages)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์„ธ ์ž๋ฆฌ ์ฝ”๋“œ๋Š” "์–ธ์–ด ์ฝ”๋“œ {code}"๋กœ ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์ฐพ์Šต๋‹ˆ๋‹ค. +- `es_AR`๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋Š” `code_{region}` ํ˜•์‹์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ์˜ ์˜ˆ์‹œ๋Š” ์•„๋ฅดํ—จํ‹ฐ๋‚˜์˜ ์ŠคํŽ˜์ธ์–ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ ๋ณ€ํ™˜์€ ๋‘ ๋‹จ๊ณ„๋กœ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ 1,000๊ฐœ ๋ชจ๋ธ์€ ISO-639-2 ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๊ทธ๋ฃน์€ ISO-639-5์™€ ISO-639-2 ์ฝ”๋“œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์–ธ์–ด๋ฅผ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค. + + +## ์˜ˆ์‹œ[[Examples]] + +- Marian ๋ชจ๋ธ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค๋ฅธ ๋ฒˆ์—ญ ๋ชจ๋ธ๋“ค๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ์ž‘์•„ ํŒŒ์ธํŠœ๋‹ ์‹คํ—˜๊ณผ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. +- [GPU์—์„œ ํŒŒ์ธํŠœ๋‹ํ•˜๊ธฐ](https://github.com/huggingface/transformers/blob/master/examples/legacy/seq2seq/train_distil_marian_enro.sh) + +## ๋‹ค๊ตญ์–ด ๋ชจ๋ธ ์‚ฌ์šฉ๋ฒ•[[Multilingual Models]] + +- ๋ชจ๋“  ๋ชจ๋ธ ์ด๋ฆ„์€`Helsinki-NLP/opus-mt-{src}-{tgt}` ํ˜•์‹์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. +- ๋‹ค์ค‘ ์–ธ์–ด ์ถœ๋ ฅ์„ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์„ ์›ํ•˜๋Š” ์–ธ์–ด์˜ ์–ธ์–ด ์ฝ”๋“œ๋ฅผ `src_text`์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ ์นด๋“œ์—์„œ ์ง€์›๋˜๋Š” ์–ธ์–ด ์ฝ”๋“œ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ๋“ค์–ด [opus-mt-en-roa](https://huggingface.co/Helsinki-NLP/opus-mt-en-roa)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `Helsinki-NLP/opus-mt-roa-en`์ฒ˜๋Ÿผ ์†Œ์Šค ์ธก์—์„œ๋งŒ ๋‹ค๊ตญ์–ด๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ๋ณ„๋„์˜ ์–ธ์–ด ์ฝ”๋“œ ์ง€์ •์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +[Tatoeba-Challenge ๋ฆฌํฌ์ง€ํ† ๋ฆฌ](https://github.com/Helsinki-NLP/Tatoeba-Challenge)์˜ ์ƒˆ๋กœ์šด ๋‹ค๊ตญ์  ๋ชจ๋ธ์€ 3์ž๋ฆฌ ์–ธ์–ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + + +```python +>>> from transformers import MarianMTModel, MarianTokenizer + +>>> src_text = [ +... ">>fra<< this is a sentence in english that we want to translate to french", +... ">>por<< This should go to portuguese", +... ">>esp<< And this to Spanish", +... ] + +>>> model_name = "Helsinki-NLP/opus-mt-en-roa" +>>> tokenizer = MarianTokenizer.from_pretrained(model_name) +>>> print(tokenizer.supported_language_codes) +['>>zlm_Latn<<', '>>mfe<<', '>>hat<<', '>>pap<<', '>>ast<<', '>>cat<<', '>>ind<<', '>>glg<<', '>>wln<<', '>>spa<<', '>>fra<<', '>>ron<<', '>>por<<', '>>ita<<', '>>oci<<', '>>arg<<', '>>min<<'] + +>>> model = MarianMTModel.from_pretrained(model_name) +>>> translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True)) +>>> [tokenizer.decode(t, skip_special_tokens=True) for t in translated] +["c'est une phrase en anglais que nous voulons traduire en franรงais", + 'Isto deve ir para o portuguรชs.', + 'Y esto al espaรฑol'] +``` + +ํ—ˆ๋ธŒ์— ์žˆ๋Š” ๋ชจ๋“  ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค: + +```python +from huggingface_hub import list_models + +model_list = list_models() +org = "Helsinki-NLP" +model_ids = [x.id for x in model_list if x.id.startswith(org)] +suffix = [x.split("/")[1] for x in model_ids] +old_style_multi_models = [f"{org}/{s}" for s in suffix if s != s.lower()] +``` + +## ๊ตฌํ˜• ๋‹ค๊ตญ์–ด ๋ชจ๋ธ[[Old Style Multi-Lingual Models]] + +์ด ๋ชจ๋ธ๋“ค์€ OPUS-MT-Train ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ตฌํ˜• ๋‹ค๊ตญ์–ด ๋ชจ๋ธ๋“ค์ž…๋‹ˆ๋‹ค. ๊ฐ ์–ธ์–ด ๊ทธ๋ฃน์— ํฌํ•จ๋œ ์–ธ์–ด๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python no-style +['Helsinki-NLP/opus-mt-NORTH_EU-NORTH_EU', + 'Helsinki-NLP/opus-mt-ROMANCE-en', + 'Helsinki-NLP/opus-mt-SCANDINAVIA-SCANDINAVIA', + 'Helsinki-NLP/opus-mt-de-ZH', + 'Helsinki-NLP/opus-mt-en-CELTIC', + 'Helsinki-NLP/opus-mt-en-ROMANCE', + 'Helsinki-NLP/opus-mt-es-NORWAY', + 'Helsinki-NLP/opus-mt-fi-NORWAY', + 'Helsinki-NLP/opus-mt-fi-ZH', + 'Helsinki-NLP/opus-mt-fi_nb_no_nn_ru_sv_en-SAMI', + 'Helsinki-NLP/opus-mt-sv-NORWAY', + 'Helsinki-NLP/opus-mt-sv-ZH'] +GROUP_MEMBERS = { + 'ZH': ['cmn', 'cn', 'yue', 'ze_zh', 'zh_cn', 'zh_CN', 'zh_HK', 'zh_tw', 'zh_TW', 'zh_yue', 'zhs', 'zht', 'zh'], + 'ROMANCE': ['fr', 'fr_BE', 'fr_CA', 'fr_FR', 'wa', 'frp', 'oc', 'ca', 'rm', 'lld', 'fur', 'lij', 'lmo', 'es', 'es_AR', 'es_CL', 'es_CO', 'es_CR', 'es_DO', 'es_EC', 'es_ES', 'es_GT', 'es_HN', 'es_MX', 'es_NI', 'es_PA', 'es_PE', 'es_PR', 'es_SV', 'es_UY', 'es_VE', 'pt', 'pt_br', 'pt_BR', 'pt_PT', 'gl', 'lad', 'an', 'mwl', 'it', 'it_IT', 'co', 'nap', 'scn', 'vec', 'sc', 'ro', 'la'], + 'NORTH_EU': ['de', 'nl', 'fy', 'af', 'da', 'fo', 'is', 'no', 'nb', 'nn', 'sv'], + 'SCANDINAVIA': ['da', 'fo', 'is', 'no', 'nb', 'nn', 'sv'], + 'SAMI': ['se', 'sma', 'smj', 'smn', 'sms'], + 'NORWAY': ['nb_NO', 'nb', 'nn_NO', 'nn', 'nog', 'no_nb', 'no'], + 'CELTIC': ['ga', 'cy', 'br', 'gd', 'kw', 'gv'] +} +``` + +์˜์–ด๋ฅผ ์—ฌ๋Ÿฌ ๋กœ๋ง์Šค ์–ธ์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ตฌํ˜• 2์ž๋ฆฌ ์–ธ์–ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + + +```python +>>> from transformers import MarianMTModel, MarianTokenizer + +>>> src_text = [ +... ">>fr<< this is a sentence in english that we want to translate to french", +... ">>pt<< This should go to portuguese", +... ">>es<< And this to Spanish", +... ] + +>>> model_name = "Helsinki-NLP/opus-mt-en-ROMANCE" +>>> tokenizer = MarianTokenizer.from_pretrained(model_name) + +>>> model = MarianMTModel.from_pretrained(model_name) +>>> translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True)) +>>> tgt_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated] +["c'est une phrase en anglais que nous voulons traduire en franรงais", + 'Isto deve ir para o portuguรชs.', + 'Y esto al espaรฑol'] +``` + +## ์ž๋ฃŒ[[Resources]] + +- [๋ฒˆ์—ญ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/translation) +- [์š”์•ฝ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/summarization) +- [์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/language_modeling) + +## MarianConfig + +[[autodoc]] MarianConfig + +## MarianTokenizer + +[[autodoc]] MarianTokenizer + - build_inputs_with_special_tokens + + + + +## MarianModel + +[[autodoc]] MarianModel + - forward + +## MarianMTModel + +[[autodoc]] MarianMTModel + - forward + +## MarianForCausalLM + +[[autodoc]] MarianForCausalLM + - forward + + + + +## TFMarianModel + +[[autodoc]] TFMarianModel + - call + +## TFMarianMTModel + +[[autodoc]] TFMarianMTModel + - call + + + + +## FlaxMarianModel + +[[autodoc]] FlaxMarianModel + - __call__ + +## FlaxMarianMTModel + +[[autodoc]] FlaxMarianMTModel + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mistral.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mistral.md new file mode 100644 index 0000000000000000000000000000000000000000..746a9a036048579950a5b17f5d200e315d9a053a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/mistral.md @@ -0,0 +1,233 @@ + + +# Mistral[[mistral]] + +## ๊ฐœ์š”[[overview]] + +๋ฏธ์ŠคํŠธ๋ž„์€ Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lรฉlio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothรฉe Lacroix, William El Sayed๊ฐ€ ์ž‘์„ฑํ•œ [์ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://mistral.ai/news/announcing-mistral-7b/)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ์˜ ์„œ๋‘๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋ฏธ์ŠคํŠธ๋ž„ AIํŒ€์€ ํ˜„์กดํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ ์ค‘ ํฌ๊ธฐ ๋Œ€๋น„ ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ๋ฏธ์ŠคํŠธ๋ž„7B๋ฅผ ์ถœ์‹œํ•˜๊ฒŒ ๋˜์–ด ์ž๋ž‘์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.* + +๋ฏธ์ŠคํŠธ๋ž„-7B๋Š” [mistral.ai](https://mistral.ai/)์—์„œ ์ถœ์‹œํ•œ ์ฒซ ๋ฒˆ์งธ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ(LLM)์ž…๋‹ˆ๋‹ค. + +### ์•„ํ‚คํ…์ฒ˜ ์„ธ๋ถ€์‚ฌํ•ญ[[architectural-details]] + +๋ฏธ์ŠคํŠธ๋ž„-7B๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ์  ํŠน์ง•์„ ๊ฐ€์ง„ ๋””์ฝ”๋” ์ „์šฉ ํŠธ๋žœ์Šคํฌ๋จธ์ž…๋‹ˆ๋‹ค: + +- ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜: 8k ์ปจํ…์ŠคํŠธ ๊ธธ์ด์™€ ๊ณ ์ • ์บ์‹œ ํฌ๊ธฐ๋กœ ํ›ˆ๋ จ๋˜์—ˆ์œผ๋ฉฐ, ์ด๋ก ์ƒ 128K ํ† ํฐ์˜ ์–ดํ…์…˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. +- GQA(Grouped Query Attention): ๋” ๋น ๋ฅธ ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋” ์ž‘์€ ํฌ๊ธฐ์˜ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ๋ฐ”์ดํŠธ ํด๋ฐฑ(Byte-fallback) BPE ํ† ํฌ๋‚˜์ด์ €: ๋ฌธ์ž๋“ค์ด ์ ˆ๋Œ€ ์–ดํœ˜ ๋ชฉ๋ก ์™ธ์˜ ํ† ํฐ์œผ๋กœ ๋งคํ•‘๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. + +๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ถœ์‹œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://mistral.ai/news/announcing-mistral-7b/)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +### ๋ผ์ด์„ ์Šค[[license]] + +`๋ฏธ์ŠคํŠธ๋ž„-7B`๋Š” ์•„ํŒŒ์น˜ 2.0 ๋ผ์ด์„ ์Šค๋กœ ์ถœ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +๋ฏธ์ŠคํŠธ๋ž„ AIํŒ€์€ ๋‹ค์Œ 3๊ฐ€์ง€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๊ณต๊ฐœํ–ˆ์Šต๋‹ˆ๋‹ค: + +- ๊ธฐ๋ณธ ๋ชจ๋ธ์ธ [๋ฏธ์ŠคํŠธ๋ž„-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1)์€ ์ธํ„ฐ๋„ท ๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋„๋ก ์‚ฌ์ „ ํ›ˆ๋ จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +- ์ง€์‹œ ์กฐ์ • ๋ชจ๋ธ์ธ [๋ฏธ์ŠคํŠธ๋ž„-7B-Instruct-v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1)์€ ์ง€๋„ ๋ฏธ์„ธ ์กฐ์ •(SFT)๊ณผ ์ง์ ‘ ์„ ํ˜ธ๋„ ์ตœ์ ํ™”(DPO)๋ฅผ ์‚ฌ์šฉํ•œ ์ฑ„ํŒ…์— ์ตœ์ ํ™”๋œ ๊ธฐ๋ณธ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. +- ๊ฐœ์„ ๋œ ์ง€์‹œ ์กฐ์ • ๋ชจ๋ธ์ธ [๋ฏธ์ŠคํŠธ๋ž„-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)๋Š” v1์„ ๊ฐœ์„ ํ•œ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") + +>>> prompt = "My favourite condiment is" + +>>> model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device) +>>> model.to(device) + +>>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True) +>>> tokenizer.batch_decode(generated_ids)[0] +"My favourite condiment is to ..." +``` + +์ง€์‹œ ์กฐ์ • ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2") + +>>> messages = [ +... {"role": "user", "content": "What is your favourite condiment?"}, +... {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"}, +... {"role": "user", "content": "Do you have mayonnaise recipes?"} +... ] + +>>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device) + +>>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True) +>>> tokenizer.batch_decode(generated_ids)[0] +"Mayonnaise can be made as follows: (...)" +``` + +์ง€์‹œ ์กฐ์ • ๋ชจ๋ธ์€ ์ž…๋ ฅ์ด ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹์œผ๋กœ ์ค€๋น„๋˜๋„๋ก [์ฑ„ํŒ… ํ…œํ”Œ๋ฆฟ](../chat_templating)์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜์„ ์ด์šฉํ•œ ๋ฏธ์ŠคํŠธ๋ž„ ์†๋„ํ–ฅ์ƒ[[speeding-up-mistral-by-using-flash-attention]] + +์œ„์˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ๋“ค์€ ์–ด๋–ค ์ตœ์ ํ™” ๊ธฐ๋ฒ•๋„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ถ”๋ก  ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ๋” ๋น ๋ฅธ ๊ตฌํ˜„์ธ [ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2](../perf_train_gpu_one.md#flash-attention-2)์„ ํ™œ์šฉํ•˜๋ฉด ๋ชจ๋ธ์˜ ์†๋„๋ฅผ ํฌ๊ฒŒ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋จผ์ €, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜๋Š” ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2์˜ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```bash +pip install -U flash-attn --no-build-isolation +``` + +ํ•˜๋“œ์›จ์–ด์™€ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2์˜ ํ˜ธํ™˜์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜ ์ €์žฅ์†Œ](https://github.com/Dao-AILab/flash-attention)์˜ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16`)๋กœ ๋ถˆ๋Ÿฌ์™€์•ผํ•ฉ๋‹ˆ๋‹ค. + +ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹คํ–‰ํ•˜๋ ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์„ ์ฐธ์กฐํ•˜์„ธ์š”: + +```python +>>> import torch +>>> from transformers import AutoModelForCausalLM, AutoTokenizer + +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1", torch_dtype=torch.float16, attn_implementation="flash_attention_2", device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1") + +>>> prompt = "My favourite condiment is" + +>>> model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device) +>>> model.to(device) + +>>> generated_ids = model.generate(**model_inputs, max_new_tokens=100, do_sample=True) +>>> tokenizer.batch_decode(generated_ids)[0] +"My favourite condiment is to (...)" +``` + +### ๊ธฐ๋Œ€ํ•˜๋Š” ์†๋„ ํ–ฅ์ƒ[[expected-speedups]] + +๋‹ค์Œ์€ `mistralai/Mistral-7B-v0.1` ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„๊ณผ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2 ๋ฒ„์ „ ๋ชจ๋ธ ์‚ฌ์ด์˜ ์ˆœ์ˆ˜ ์ถ”๋ก  ์‹œ๊ฐ„์„ ๋น„๊ตํ•œ ์˜ˆ์ƒ ์†๋„ ํ–ฅ์ƒ ๋‹ค์ด์–ด๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. + +
+ +
+ +### ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜[[sliding-window-attention]] + +ํ˜„์žฌ ๊ตฌํ˜„์€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์บ์‹œ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์–ดํ…์…˜๊ณผ ํ˜ธํ™˜๋˜๋Š”`flash-attn`(`>=2.3.0`)๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +๋˜ํ•œ ํ”Œ๋ž˜์‹œ ์–ดํ…์…˜2 ๋ชจ๋ธ์€ ๋” ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์บ์‹œ ์Šฌ๋ผ์ด์‹ฑ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ์ŠคํŠธ๋ž„ ๋ชจ๋ธ์˜ ๊ณต์‹ ๊ตฌํ˜„์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋กค๋ง ์บ์‹œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋”ฐ๋ผ, ์บ์‹œ ํฌ๊ธฐ๋ฅผ ๊ณ ์ •(`self.config.sliding_window`)์œผ๋กœ ์œ ์ง€ํ•˜๊ณ , `padding_side="left"`์ธ ๊ฒฝ์šฐ์—๋งŒ ๋ฐฐ์น˜ ์ƒ์„ฑ(batch generation)์„ ์ง€์›ํ•˜๋ฉฐ, ํ˜„์žฌ ํ† ํฐ์˜ ์ ˆ๋Œ€ ์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•ด ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + +## ์–‘์žํ™”๋กœ ๋ฏธ์ŠคํŠธ๋ž„ ํฌ๊ธฐ ์ค„์ด๊ธฐ[[shrinking-down-mistral-using-quantization]] + +๋ฏธ์ŠคํŠธ๋ž„ ๋ชจ๋ธ์€ 70์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด, ์ ˆ๋ฐ˜์˜ ์ •๋ฐ€๋„(float16)๋กœ ์•ฝ 14GB์˜ GPU RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 2๋ฐ”์ดํŠธ๋กœ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ [์–‘์žํ™”](../quantization)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ 4๋น„ํŠธ(์ฆ‰, ํŒŒ๋ผ๋ฏธํ„ฐ๋‹น ๋ฐ˜ ๋ฐ”์ดํŠธ)๋กœ ์–‘์žํ™”ํ•˜๋ฉด ์•ฝ 3.5GB์˜ RAM๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๊ฒƒ์€ `quantization_config`๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ๋งŒํผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ๋Š” BitsAndBytes ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ์–‘์žํ™” ๋ฐฉ๋ฒ•์€ [์ด ํŽ˜์ด์ง€](../quantization)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”: + +```python +>>> import torch +>>> from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +>>> # specify how to quantize the model +>>> quantization_config = BitsAndBytesConfig( +... load_in_4bit=True, +... bnb_4bit_quant_type="nf4", +... bnb_4bit_compute_dtype="torch.float16", +... ) + +>>> model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", quantization_config=True, device_map="auto") +>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2") + +>>> prompt = "My favourite condiment is" + +>>> messages = [ +... {"role": "user", "content": "What is your favourite condiment?"}, +... {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"}, +... {"role": "user", "content": "Do you have mayonnaise recipes?"} +... ] + +>>> model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device) + +>>> generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=True) +>>> tokenizer.batch_decode(generated_ids)[0] +"The expected output" +``` + +์ด ๋ชจ๋ธ์€ [Younes Belkada](https://huggingface.co/ybelkada)์™€ [Arthur Zucker](https://huggingface.co/ArthurZ)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/mistralai/mistral-src)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค[[resources]] + +๋ฏธ์ŠคํŠธ๋ž„์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +- ๋ฏธ์ŠคํŠธ๋ž„-7B์˜ ์ง€๋„ํ˜• ๋ฏธ์„ธ์กฐ์ •(SFT)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์ด๊ณณ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/Mistral/Supervised_fine_tuning_(SFT)_of_an_LLM_using_Hugging_Face_tooling.ipynb)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ +- 2024๋…„์— Hugging Face ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด LLM์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ [๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://www.philschmid.de/fine-tune-llms-in-2024-with-trl). ๐ŸŒŽ +- Hugging Face์˜ [์ •๋ ฌ(Alignment) ํ•ธ๋“œ๋ถ](https://github.com/huggingface/alignment-handbook)์—๋Š” ๋ฏธ์ŠคํŠธ๋ž„-7B๋ฅผ ์‚ฌ์šฉํ•œ ์ง€๋„ํ˜• ๋ฏธ์„ธ ์กฐ์ •(SFT) ๋ฐ ์ง์ ‘ ์„ ํ˜ธ ์ตœ์ ํ™”(DPO)๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์Šคํฌ๋ฆฝํŠธ์™€ ๋ ˆ์‹œํ”ผ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋‹จ์ผ GPU์—์„œ QLoRa ๋ฐ ๋‹ค์ค‘ GPU๋ฅผ ์‚ฌ์šฉํ•œ ์ „์ฒด ๋ฏธ์„ธ ์กฐ์ •์„ ์œ„ํ•œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- [์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/language_modeling) + +## MistralConfig[[transformers.MistralConfig]] + +[[autodoc]] MistralConfig + +## MistralModel[[transformers.MistralModel]] + +[[autodoc]] MistralModel + - forward + +## MistralForCausalLM[[transformers.MistralForCausalLM]] + +[[autodoc]] MistralForCausalLM + - forward + +## MistralForSequenceClassification[[transformers.MistralForSequenceClassification]] + +[[autodoc]] MistralForSequenceClassification + - forward + +## MistralForTokenClassification[[transformers.MistralForTokenClassification]] + +[[autodoc]] MistralForTokenClassification + - forward + +## FlaxMistralModel[[transformers.FlaxMistralModel]] + +[[autodoc]] FlaxMistralModel + - __call__ + +## FlaxMistralForCausalLM[[transformers.FlaxMistralForCausalLM]] + +[[autodoc]] FlaxMistralForCausalLM + - __call__ + +## TFMistralModel[[transformers.TFMistralModel]] + +[[autodoc]] TFMistralModel + - call + +## TFMistralForCausalLM[[transformers.TFMistralForCausalLM]] + +[[autodoc]] TFMistralForCausalLM + - call + +## TFMistralForSequenceClassification[[transformers.TFMistralForSequenceClassification]] + +[[autodoc]] TFMistralForSequenceClassification + - call diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/openai-gpt.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/openai-gpt.md new file mode 100644 index 0000000000000000000000000000000000000000..7e8f57c743bf7173e4fd31657abfb0b27b469f83 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/openai-gpt.md @@ -0,0 +1,149 @@ + + +# OpenAI GPT [[openai-gpt]] + +
+ +Models + + +Spaces + +
+ +## ๊ฐœ์š” [[overview]] + +OpenAI GPT ๋ชจ๋ธ์€ Alec Radford, Karthik Narasimhan, Tim Salimans, Ilya Sutskever๊ฐ€ ์ž‘์„ฑํ•œ [Improving Language Understanding by Generative Pre-Training](https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Toronto Book Corpus์™€ ๊ฐ™์€ ์žฅ๊ธฐ ์˜์กด์„ฑ์„ ๊ฐ€์ง„ ๋Œ€๊ทœ๋ชจ ๋ง๋ญ‰์น˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ธ์–ด ๋ชจ๋ธ๋ง์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ๊ณผ์ (๋‹จ๋ฐฉํ–ฅ) ํŠธ๋žœ์Šคํฌ๋จธ์ž…๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ž์—ฐ์–ด ์ดํ•ด๋Š” ํ…์ŠคํŠธ ํ•จ์˜, ์งˆ๋ฌธ ์‘๋‹ต, ์˜๋ฏธ ์œ ์‚ฌ์„ฑ ํ‰๊ฐ€, ๋ฌธ์„œ ๋ถ„๋ฅ˜์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋น„๋ก ๋Œ€๊ทœ๋ชจ์˜ ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜๊ฐ€ ํ’๋ถ€ํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์ด๋Ÿฌํ•œ ํŠน์ • ์ž‘์—…์— ๋Œ€ํ•œ ํ•™์Šต์„ ์œ„ํ•œ ๋ ˆ์ด๋ธ”๋œ ๋ฐ์ดํ„ฐ๋Š” ๋ถ€์กฑํ•˜์—ฌ ํŒ๋ณ„์ ์œผ๋กœ ํ•™์Šต๋œ ๋ชจ๋ธ์ด ์ ์ ˆํ•˜๊ฒŒ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‹ค์–‘ํ•œ ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜์— ๋Œ€ํ•œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ƒ์„ฑ์  ์‚ฌ์ „ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฐ ํŠน์ • ๊ณผ์ œ์— ๋Œ€ํ•œ ํŒ๋ณ„์  ๋ฏธ์„ธ ์กฐ์ •์„ ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ์ด๋Ÿฌํ•œ ๊ณผ์ œ์—์„œ ํฐ ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด์ „ ์ ‘๊ทผ ๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ, ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์— ์ตœ์†Œํ•œ์˜ ๋ณ€ํ™”๋ฅผ ์š”๊ตฌํ•˜๋ฉด์„œ ํšจ๊ณผ์ ์ธ ์ „์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ • ์ค‘์— ๊ณผ์ œ ์ธ์‹ ์ž…๋ ฅ ๋ณ€ํ™˜(task-aware input transformation)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ž์—ฐ์–ด ์ดํ•ด๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฒค์น˜๋งˆํฌ์—์„œ ์šฐ๋ฆฌ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์˜ ํšจ๊ณผ๋ฅผ ์ž…์ฆํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ general task-agnostic ๋ชจ๋ธ์€ ๊ฐ ๊ณผ์ œ์— ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒ๋ณ„์ ์œผ๋กœ ํ•™์Šต๋œ ๋ชจ๋ธ๋ณด๋‹ค ๋›ฐ์–ด๋‚˜๋ฉฐ, ์—ฐ๊ตฌ๋œ 12๊ฐœ ๊ณผ์ œ ์ค‘ 9๊ฐœ ๋ถ€๋ฌธ์—์„œ ์ตœ์ฒจ๋‹จ ์„ฑ๋Šฅ(state of the art)์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.* + +[Write With Transformer](https://transformer.huggingface.co/doc/gpt)๋Š” Hugging Face๊ฐ€ ๋งŒ๋“  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ, ์—ฌ๋Ÿฌ ๋ชจ๋ธ์˜ ์ƒ์„ฑ ๋Šฅ๋ ฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ ๊ทธ ์ค‘์—๋Š” GPT๋„ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [thomwolf](https://huggingface.co/thomwolf)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์œผ๋ฉฐ, ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/openai/finetune-transformer-lm)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- GPT๋Š” ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ ์ž…๋ ฅ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ์™ผ์ชฝ๋ณด๋‹ค๋Š” ์˜ค๋ฅธ์ชฝ์— ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. +- GPT๋Š” ์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง(Causal Language Modeling, CLM) ๋ชฉํ‘œ๋กœ ํ•™์Šต๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œํ€€์Šค์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐ ๊ฐ•๋ ฅํ•œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด *run_generation.py* ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด GPT-2๋Š” ๊ตฌ๋ฌธ์ ์œผ๋กœ ์ผ๊ด€๋œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ฐธ๊ณ : + +*OpenAI GPT* ๋…ผ๋ฌธ์˜ ์›๋ž˜ ํ† ํฐํ™” ๊ณผ์ •์„ ์žฌํ˜„ํ•˜๋ ค๋ฉด `ftfy`์™€ `SpaCy`๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install spacy ftfy==4.4.3 +python -m spacy download en +``` + +`ftfy`์™€ `SpaCy`๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด [`OpenAIGPTTokenizer`]๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ BERT์˜ `BasicTokenizer`๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ Byte-Pair Encoding์„ ํ†ตํ•ด ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค(๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ์— ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”). + +## ๋ฆฌ์†Œ์Šค [[resources]] + +OpenAI GPT๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ณต์‹ Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ ํ‘œ์‹œ) ๋ฆฌ์†Œ์Šค ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ๋ฆฌ์†Œ์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, Pull Request๋ฅผ ์—ด์–ด์ฃผ์‹œ๋ฉด ๊ฒ€ํ† ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฆฌ์†Œ์Šค๋Š” ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณต์ œํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +- [SetFit์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์—์„œ OpenAI GPT-3์„ ๋Šฅ๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/getting-started-setfit) ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ. +- ์ถ”๊ฐ€ ์ž๋ฃŒ: [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ณผ์ œ ๊ฐ€์ด๋“œ](../tasks/sequence_classification) + + + +- [Hugging Face์™€ ํ•จ๊ป˜ ๋น„์˜์–ด GPT-2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://www.philschmid.de/fine-tune-a-non-english-gpt-2-model-with-huggingface) ๋ธ”๋กœ๊ทธ. +- GPT-2์™€ ํ•จ๊ป˜ [Transformers๋ฅผ ์‚ฌ์šฉํ•œ ์–ธ์–ด ์ƒ์„ฑ์˜ ๋‹ค์–‘ํ•œ ๋””์ฝ”๋”ฉ ๋ฐฉ๋ฒ•](https://huggingface.co/blog/how-to-generate)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ. +- [Scratch์—์„œ CodeParrot ๐Ÿฆœ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/blog/codeparrot), ๋Œ€๊ทœ๋ชจ GPT-2 ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ. +- GPT-2์™€ ํ•จ๊ป˜ [TensorFlow ๋ฐ XLA๋ฅผ ์‚ฌ์šฉํ•œ ๋” ๋น ๋ฅธ ํ…์ŠคํŠธ ์ƒ์„ฑ](https://huggingface.co/blog/tf-xla-generate)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ. +- [Megatron-LM์œผ๋กœ ์–ธ์–ด ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/blog/megatron-training)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ. +- [์ข‹์•„ํ•˜๋Š” ์•„ํ‹ฐ์ŠคํŠธ์˜ ์Šคํƒ€์ผ๋กœ ๊ฐ€์‚ฌ๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก GPT2๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://colab.research.google.com/github/AlekseyKorshuk/huggingartists/blob/master/huggingartists-demo.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๐ŸŒŽ +- [์ข‹์•„ํ•˜๋Š” ํŠธ์œ„ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์Šคํƒ€์ผ๋กœ ํŠธ์œ—์„ ์ƒ์„ฑํ•˜๋„๋ก GPT2๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://colab.research.google.com/github/borisdayma/huggingtweets/blob/master/huggingtweets-demo.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ. ๐ŸŒŽ +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง](https://huggingface.co/course/en/chapter7/6?fw=pt#training-a-causal-language-model-from-scratch) ์žฅ. +- [`OpenAIGPTLMHeadModel`]์€ [์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#gpt-2gpt-and-causal-language-modeling), [ํ…์ŠคํŠธ ์ƒ์„ฑ ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-generation/run_generation.py) ๋ฐ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์— ์˜ํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFOpenAIGPTLMHeadModel`]์€ [์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_clmpy) ๋ฐ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์— ์˜ํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ์ถ”๊ฐ€ ์ž๋ฃŒ: [์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง ๊ณผ์ œ ๊ฐ€์ด๋“œ](../tasks/language_modeling) + + + +- [Byte-Pair Encoding ํ† ํฐํ™”](https://huggingface.co/course/en/chapter6/5)์— ๋Œ€ํ•œ ๊ฐ•์˜ ์ž๋ฃŒ. + +## OpenAIGPTConfig [[transformers.OpenAIGPTConfig]] + +[[autodoc]] OpenAIGPTConfig + +## OpenAIGPTTokenizer [[transformers.OpenAIGPTTokenizer]] + +[[autodoc]] OpenAIGPTTokenizer + - save_vocabulary + +## OpenAIGPTTokenizerFast [[transformers.OpenAIGPTTokenizerFast]] + +[[autodoc]] OpenAIGPTTokenizerFast + +## OpenAI specific outputs [[transformers.models.openai.modeling_openai.OpenAIGPTDoubleHeadsModelOutput]] + +[[autodoc]] models.openai.modeling_openai.OpenAIGPTDoubleHeadsModelOutput + +[[autodoc]] models.openai.modeling_tf_openai.TFOpenAIGPTDoubleHeadsModelOutput + + + + +## OpenAIGPTModel [[transformers.OpenAIGPTModel]] + +[[autodoc]] OpenAIGPTModel + - forward + +## OpenAIGPTLMHeadModel [[transformers.OpenAIGPTLMHeadModel]] + +[[autodoc]] OpenAIGPTLMHeadModel + - forward + +## OpenAIGPTDoubleHeadsModel [[transformers.OpenAIGPTDoubleHeadsModel]] + +[[autodoc]] OpenAIGPTDoubleHeadsModel + - forward + +## OpenAIGPTForSequenceClassification [[transformers.OpenAIGPTForSequenceClassification]] + +[[autodoc]] OpenAIGPTForSequenceClassification + - forward + + + + +## TFOpenAIGPTModel [[transformers.TFOpenAIGPTModel]] + +[[autodoc]] TFOpenAIGPTModel + - call + +## TFOpenAIGPTLMHeadModel [[transformers.TFOpenAIGPTLMHeadModel]] + +[[autodoc]] TFOpenAIGPTLMHeadModel + - call + +## TFOpenAIGPTDoubleHeadsModel [[transformers.TFOpenAIGPTDoubleHeadsModel]] + +[[autodoc]] TFOpenAIGPTDoubleHeadsModel + - call + +## TFOpenAIGPTForSequenceClassification [[transformers.TFOpenAIGPTForSequenceClassification]] + +[[autodoc]] TFOpenAIGPTForSequenceClassification + - call + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/paligemma.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/paligemma.md new file mode 100644 index 0000000000000000000000000000000000000000..9f5b48086855094ed1d0ad9e746c486760c60ef5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/paligemma.md @@ -0,0 +1,80 @@ + + +# PaliGemma[[paligemma]] + +## ๊ฐœ์š”[[overview]] + +PaliGemma ๋ชจ๋ธ์€ ๊ตฌ๊ธ€์ด ์ œ์•ˆํ•œ [PaliGemma โ€“ Google์˜ ์ตœ์ฒจ๋‹จ ์˜คํ”ˆ ๋น„์ „ ์–ธ์–ด ๋ชจ๋ธ](https://huggingface.co/blog/paligemma)์—์„œ ์†Œ๊ฐœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. PaliGemma๋Š” [SigLIP](siglip) ๋น„์ „ ์ธ์ฝ”๋”์™€ [Gemma](gemma) ์–ธ์–ด ์ธ์ฝ”๋”๋กœ ๊ตฌ์„ฑ๋œ 3B ๊ทœ๋ชจ์˜ ๋น„์ „-์–ธ์–ด ๋ชจ๋ธ๋กœ, ๋‘ ์ธ์ฝ”๋”๊ฐ€ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์„ ํ˜• ํ”„๋กœ์ ์…˜์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€๋ฅผ ๊ณ ์ •๋œ ์ˆ˜์˜ VITํ† ํฐ์œผ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ์ด๋ฅผ ์„ ํƒ์  ํ”„๋กฌํ”„ํŠธ ์•ž์— ์ถ”๊ฐ€ ํ•˜๋ฉฐ, ๋ชจ๋“  ์ด๋ฏธ์ง€ ํ† ํฐ๊ณผ ์ž…๋ ฅ ํ…์ŠคํŠธ ํ† ํฐ์— ๋Œ€ํ•ด ์ „์ฒด ๋ธ”๋ก ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +PaliGemma๋Š” 224x224, 448x448, 896x896์˜ 3๊ฐ€์ง€ ํ•ด์ƒ๋„๋กœ ์ œ๊ณต๋˜๋ฉฐ, 3๊ฐœ์˜ ๊ธฐ๋ณธ ๋ชจ๋ธ๊ณผ 55๊ฐœ์˜ ๋‹ค์–‘ํ•œ ์ž‘์—…์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ฒ„์ „, ๊ทธ๋ฆฌ๊ณ  2๊ฐœ์˜ ํ˜ผํ•ฉ ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + + + + PaliGemma ์•„ํ‚คํ…์ฒ˜ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. + +์ด ๋ชจ๋ธ์€ [Molbap](https://huggingface.co/Molbap)์— ์˜ํ•ด ๊ธฐ์—ฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +PaliGemma์˜ ์ถ”๋ก ์€ ๋‹ค์Œ์ฒ˜๋Ÿผ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค: + +```python +from transformers import AutoProcessor, PaliGemmaForConditionalGeneration + +model_id = "google/paligemma-3b-mix-224" +model = PaliGemmaForConditionalGeneration.from_pretrained(model_id) +processor = AutoProcessor.from_pretrained(model_id) + +prompt = "What is on the flower?" +image_file = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg?download=true" +raw_image = Image.open(requests.get(image_file, stream=True).raw) +inputs = processor(raw_image, prompt, return_tensors="pt") +output = model.generate(**inputs, max_new_tokens=20) + +print(processor.decode(output[0], skip_special_tokens=True)[len(prompt):]) +``` + +- PaliGemma๋Š” ๋Œ€ํ™”์šฉ์œผ๋กœ ์„ค๊ณ„๋˜์ง€ ์•Š์•˜์œผ๋ฉฐ, ํŠน์ • ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•  ๋•Œ ๊ฐ€์žฅ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. PaliGemma๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•˜์œ„ ์ž‘์—…์—๋Š” ์ด๋ฏธ์ง€ ์บก์…”๋‹, ์‹œ๊ฐ์  ์งˆ๋ฌธ ๋‹ต๋ณ€(VQA), ์˜ค๋ธŒ์ ํŠธ ๋””ํ…์…˜, ์ฐธ์กฐ ํ‘œํ˜„ ๋ถ„ํ•  ๋ฐ ๋ฌธ์„œ ์ดํ•ด๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์— ํ•„์š”ํ•œ ์ด๋ฏธ์ง€, ํ…์ŠคํŠธ ๋ฐ ์„ ํƒ์  ๋ ˆ์ด๋ธ”์„ ์ค€๋น„ํ•˜๋Š”๋ฐ `PaliGemmaProcessor`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PaliGemma ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ๋•Œ๋Š”, ํ”„๋กœ์„ธ์„œ์— `suffix`์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๋‹ค์Œ ์ฒ˜๋Ÿผ ๋ชจ๋ธ์˜ `labels`๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +prompt = "What is on the flower?" +answer = "a bee" +inputs = processor(images=raw_image, text=prompt, suffix=answer, return_tensors="pt") +``` + +## ์ž๋ฃŒ[[resources]] + +PaliGemma๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค.์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- PaliGemma์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์†Œ๊ฐœํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋Š” [์ด๊ณณ](https://huggingface.co/blog/paligemma)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ +- Trainer API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ VQA(Visual Question Answering)๋ฅผ ์œ„ํ•ด PaliGemma๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ถ”๋ก ์— ๋Œ€ํ•œ ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์ด๊ณณ](https://github.com/huggingface/notebooks/tree/main/examples/paligemma)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ +- ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ์…‹(์˜์ˆ˜์ฆ ์ด๋ฏธ์ง€ -> JSON)์— ๋Œ€ํ•ด PaliGemma๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ถ”๋ก ์— ๋Œ€ํ•œ ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์ด๊ณณ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/PaliGemma)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ + +## PaliGemmaConfig[[transformers.PaliGemmaConfig]] + +[[autodoc]] PaliGemmaConfig + +## PaliGemmaProcessor[[transformers.PaliGemmaProcessor]] + +[[autodoc]] PaliGemmaProcessor + +## PaliGemmaForConditionalGeneration[[transformers.PaliGemmaForConditionalGeneration]] + +[[autodoc]] PaliGemmaForConditionalGeneration + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtsmixer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtsmixer.md new file mode 100644 index 0000000000000000000000000000000000000000..7af873b9ee04efbdfa66aed768ba3595ff518c64 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtsmixer.md @@ -0,0 +1,95 @@ + + +# PatchTSMixer[[patchtsmixer]] + +## ๊ฐœ์š”[[overview]] + +PatchTSMixer ๋ชจ๋ธ์€ Vijay Ekambaram, Arindam Jati, Nam Nguyen, Phanwadee Sinthong, Jayant Kalagnanam์ด ์ œ์•ˆํ•œ [TSMixer: ๋‹ค๋ณ€๋Ÿ‰ ์‹œ๊ณ„์—ด ์˜ˆ์ธก์„ ์œ„ํ•œ ๊ฒฝ๋Ÿ‰ MLP-Mixer ๋ชจ๋ธ](https://huggingface.co/papers/2306.09364)์ด๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + + +PatchTSMixer๋Š” MLP-Mixer ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๊ฒฝ๋Ÿ‰ ์‹œ๊ณ„์—ด ๋ชจ๋ธ๋ง ์ ‘๊ทผ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ—ˆ๊น…ํŽ˜์ด์Šค ๊ตฌํ˜„์—์„œ๋Š” PatchTSMixer์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ํŒจ์น˜, ์ฑ„๋„, ์ˆจ๊ฒจ์ง„ ํŠน์„ฑ ๊ฐ„์˜ ๊ฒฝ๋Ÿ‰ ํ˜ผํ•ฉ์„ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•˜์—ฌ ํšจ๊ณผ์ ์ธ ๋‹ค๋ณ€๋Ÿ‰ ์‹œ๊ณ„์—ด ๋ชจ๋ธ๋ง์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ„๋‹จํ•œ ๊ฒŒ์ดํŠธ ์–ดํ…์…˜๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž ์ •์˜๋œ ๋” ๋ณต์žกํ•œ ์…€ํ”„ ์–ดํ…์…˜ ๋ธ”๋ก๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ดํ›„ ์˜ˆ์ธก, ๋ถ„๋ฅ˜, ํšŒ๊ท€์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*TSMixer๋Š” ํŒจ์น˜ ์ฒ˜๋ฆฌ๋œ ์‹œ๊ณ„์—ด์˜ ๋‹ค๋ณ€๋Ÿ‰ ์˜ˆ์ธก ๋ฐ ํ‘œํ˜„ ํ•™์Šต์„ ์œ„ํ•ด ์„ค๊ณ„๋œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MLP) ๋ชจ๋“ˆ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๊ฒฝ๋Ÿ‰ ์‹ ๊ฒฝ๋ง ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชจ๋ธ์€ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ MLP-Mixer ๋ชจ๋ธ์˜ ์„ฑ๊ณต์—์„œ ์˜๊ฐ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” Vision MLP-Mixer๋ฅผ ์‹œ๊ณ„์—ด์— ์ ์šฉํ•˜๋Š” ๋ฐ ๋”ฐ๋ฅด๋Š” ๊ณผ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ์ •ํ™•๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๊ฒฝํ—˜์ ์œผ๋กœ ๊ฒ€์ฆ๋œ ๊ตฌ์„ฑ ์š”์†Œ๋“ค์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๊ณ„์ธต ๊ตฌ์กฐ ๋ฐ ์ฑ„๋„ ์ƒ๊ด€๊ด€๊ณ„์™€ ๊ฐ™์€ ์‹œ๊ณ„์—ด ํŠน์„ฑ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋ชจ๋ธ๋งํ•˜๊ธฐ ์œ„ํ•ด MLP-Mixer ๋ฐฑ๋ณธ์— ์˜จ๋ผ์ธ ์กฐ์ • ํ—ค๋“œ๋ฅผ ๋ถ€์ฐฉํ•˜๋Š” ์ƒˆ๋กœ์šด ์„ค๊ณ„ ํŒจ๋Ÿฌ๋‹ค์ž„์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ์กด ํŒจ์น˜ ์ฑ„๋„ ํ˜ผํ•ฉ ๋ฐฉ๋ฒ•์˜ ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ์ธ ๋…ธ์ด์ฆˆ๊ฐ€ ์žˆ๋Š” ์ฑ„๋„ ์ƒํ˜ธ์ž‘์šฉ์„ ํšจ๊ณผ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ์…‹์— ๊ฑธ์ณ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ฑ„๋„ ๋ชจ๋ธ๋ง ์ ‘๊ทผ๋ฒ•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ, ์ค‘์š”ํ•œ ํŠน์„ฑ์„ ์šฐ์„ ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฑ๋ณธ์— ๊ฐ„๋‹จํ•œ ๊ฒŒ์ดํŠธ ์ฃผ์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ๋Ÿ‰ ๊ตฌ์„ฑ ์š”์†Œ๋“ค์„ ํ†ตํ•ฉํ•จ์œผ๋กœ์จ, ์šฐ๋ฆฌ๋Š” ๋‹จ์ˆœํ•œ MLP ๊ตฌ์กฐ์˜ ํ•™์Šต ๋Šฅ๋ ฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œ์ผœ ์ตœ์†Œํ•œ์˜ ์ปดํ“จํŒ… ์‚ฌ์šฉ์œผ๋กœ ๋ณต์žกํ•œ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ๋“ค์„ ๋Šฅ๊ฐ€ํ•˜๋Š” ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋”์šฑ์ด, TSMixer์˜ ๋ชจ๋“ˆ์‹ ์„ค๊ณ„๋Š” ๊ฐ๋… ํ•™์Šต๊ณผ ๋งˆ์Šคํฌ ์ž๊ธฐ ๊ฐ๋… ํ•™์Šต ๋ฐฉ๋ฒ• ๋ชจ๋‘์™€ ํ˜ธํ™˜๋˜์–ด ์‹œ๊ณ„์—ด ๊ธฐ์ดˆ ๋ชจ๋ธ์˜ ์œ ๋งํ•œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. TSMixer๋Š” ์˜ˆ์ธก ์ž‘์—…์—์„œ ์ตœ์ฒจ๋‹จ MLP ๋ฐ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ๋“ค์„ ์ƒ๋‹นํ•œ ์ฐจ์ด(8-60%)๋กœ ๋Šฅ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ตœ์‹ ์˜ ๊ฐ•๋ ฅํ•œ Patch-Transformer ๋ชจ๋ธ ๋ฒค์น˜๋งˆํฌ๋“ค์„ ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹คํ–‰ ์‹œ๊ฐ„์„ ํฌ๊ฒŒ ์ค„์ด๋ฉด์„œ(2-3๋ฐฐ) ์„ฑ๋Šฅ ๋ฉด์—์„œ๋„ ์•ž์„ญ๋‹ˆ๋‹ค(1-2%).* + +์ด ๋ชจ๋ธ์€ [ajati](https://huggingface.co/ajati), [vijaye12](https://huggingface.co/vijaye12), +[gsinthong](https://huggingface.co/gsinthong), [namctin](https://huggingface.co/namctin), +[wmgifford](https://huggingface.co/wmgifford), [kashif](https://huggingface.co/kashif)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ[[usage-example]] + +์•„๋ž˜์˜ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์€ PatchTSMixer ๋ชจ๋ธ์„ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. +PatchTSMixer ๋ชจ๋ธ์€ [Trainer API](../trainer)์™€ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. + +```python + +from transformers import PatchTSMixerConfig, PatchTSMixerForPrediction +from transformers import Trainer, TrainingArguments, + + +config = PatchTSMixerConfig(context_length = 512, prediction_length = 96) +model = PatchTSMixerForPrediction(config) +trainer = Trainer(model=model, args=training_args, + train_dataset=train_dataset, + eval_dataset=valid_dataset) +trainer.train() +results = trainer.evaluate(test_dataset) +``` + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +์ด ๋ชจ๋ธ์€ ์‹œ๊ณ„์—ด ๋ถ„๋ฅ˜์™€ ์‹œ๊ณ„์—ด ํšŒ๊ท€์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ[`PatchTSMixerForTimeSeriesClassification`]์™€ [`PatchTSMixerForRegression`] ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ์ž๋ฃŒ[[resources]] + +- PatchTSMixer๋ฅผ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋Š” ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค [์ด๊ณณ](https://huggingface.co/blog/patchtsmixer). ์ด ๋ธ”๋กœ๊ทธ๋Š” Google Colab์—์„œ๋„ ์—ด์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## PatchTSMixerConfig[[transformers.PatchTSMixerConfig]] + +[[autodoc]] PatchTSMixerConfig + + +## PatchTSMixerModel[[transformers.PatchTSMixerModel]] + +[[autodoc]] PatchTSMixerModel + - forward + + +## PatchTSMixerForPrediction[[transformers.PatchTSMixerForPrediction]] + +[[autodoc]] PatchTSMixerForPrediction + - forward + + +## PatchTSMixerForTimeSeriesClassification[[transformers.PatchTSMixerForTimeSeriesClassification]] + +[[autodoc]] PatchTSMixerForTimeSeriesClassification + - forward + + +## PatchTSMixerForPretraining[[transformers.PatchTSMixerForPretraining]] + +[[autodoc]] PatchTSMixerForPretraining + - forward + + +## PatchTSMixerForRegression[[transformers.PatchTSMixerForRegression]] + +[[autodoc]] PatchTSMixerForRegression + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtst.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtst.md new file mode 100644 index 0000000000000000000000000000000000000000..d8f1b04e50ebc27a027be3d0c17d356b97afb8b4 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/patchtst.md @@ -0,0 +1,76 @@ + + +# PatchTST[[patchtst]] + +## ๊ฐœ์š”[[overview]] + +The PatchTST ๋ชจ๋ธ์€ Yuqi Nie, Nam H. Nguyen, Phanwadee Sinthong, Jayant Kalagnanam์ด ์ œ์•ˆํ•œ [์‹œ๊ณ„์—ด ํ•˜๋‚˜๊ฐ€ 64๊ฐœ์˜ ๋‹จ์–ด๋งŒํผ ๊ฐ€์น˜์žˆ๋‹ค: ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ด์šฉํ•œ ์žฅ๊ธฐ์˜ˆ์ธก](https://huggingface.co/papers/2211.14730)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ ๊ณ ์ˆ˜์ค€์—์„œ ์‹œ๊ณ„์—ด์„ ์ฃผ์–ด์ง„ ํฌ๊ธฐ์˜ ํŒจ์น˜๋กœ ๋ฒกํ„ฐํ™”ํ•˜๊ณ , ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ๋ฒกํ„ฐ ์‹œํ€€์Šค๋ฅผ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ํ†ตํ•ด ์ธ์ฝ”๋”ฉํ•œ ๋‹ค์Œ ์ ์ ˆํ•œ ํ—ค๋“œ๋ฅผ ํ†ตํ•ด ์˜ˆ์ธก ๊ธธ์ด์˜ ์˜ˆ์ธก์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋„์‹ํ™”๋ฉ๋‹ˆ๋‹ค: + +![๋ชจ๋ธ](https://github.com/namctin/transformers/assets/8100/150af169-29de-419a-8d98-eb78251c21fa) + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” ๋‹ค๋ณ€๋Ÿ‰ ์‹œ๊ณ„์—ด ์˜ˆ์ธก๊ณผ ์ž๊ธฐ ๊ฐ๋… ํ‘œํ˜„ ํ•™์Šต์„ ์œ„ํ•œ ํšจ์œจ์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ ์„ค๊ณ„๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค: + +(i) ์‹œ๊ณ„์—ด์„ ํ•˜์œ„ ์‹œ๋ฆฌ์ฆˆ ์ˆ˜์ค€์˜ ํŒจ์น˜๋กœ ๋ถ„ํ• ํ•˜์—ฌ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ +(ii) ๊ฐ ์ฑ„๋„์ด ๋ชจ๋“  ์‹œ๋ฆฌ์ฆˆ์— ๊ฑธ์ณ ๋™์ผํ•œ ์ž„๋ฒ ๋”ฉ๊ณผ ํŠธ๋žœ์Šคํฌ๋จธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋‹จ์ผ ๋‹จ๋ณ€๋Ÿ‰ ์‹œ๊ณ„์—ด์„ ํฌํ•จํ•˜๋Š” ์ฑ„๋„ ๋…๋ฆฝ์„ฑ. ํŒจ์นญ ์„ค๊ณ„๋Š” ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์„ธ ๊ฐ€์ง€ ์ด์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค: + - ์ง€์—ญ์  ์˜๋ฏธ ์ •๋ณด๊ฐ€ ์ž„๋ฒ ๋”ฉ์— ์œ ์ง€๋ฉ๋‹ˆ๋‹ค; + - ๋™์ผํ•œ ๋ฃฉ๋ฐฑ ์œˆ๋„์šฐ์— ๋Œ€ํ•ด ์–ดํ…์…˜ ๋งต์˜ ๊ณ„์‚ฐ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ œ๊ณฑ์œผ๋กœ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค + - ๋ชจ๋ธ์ด ๋” ๊ธด ๊ณผ๊ฑฐ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + ์šฐ๋ฆฌ์˜ ์ฑ„๋„ ๋…๋ฆฝ์  ํŒจ์น˜ ์‹œ๊ณ„์—ด ํŠธ๋žœ์Šคํฌ๋จธ(PatchTST)๋Š” ์ตœ์‹  ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ๋“ค๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์žฅ๊ธฐ ์˜ˆ์ธก ์ •ํ™•๋„๋ฅผ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋ธ์„ ์ž๊ธฐ์ง€๋„ ์‚ฌ์ „ ํ›ˆ๋ จ ์ž‘์—…์— ์ ์šฉํ•˜์—ฌ, ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ง€๋„ ํ•™์Šต์„ ๋Šฅ๊ฐ€ํ•˜๋Š” ์•„์ฃผ ๋›ฐ์–ด๋‚œ ๋ฏธ์„ธ ์กฐ์ • ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฐ์ดํ„ฐ์…‹์—์„œ ๋งˆ์Šคํฌ๋œ ์‚ฌ์ „ ํ›ˆ๋ จ ํ‘œํ˜„์„ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ์ „์ดํ•˜๋Š” ๊ฒƒ๋„ ์ตœ๊ณ  ์ˆ˜์ค€์˜ ์˜ˆ์ธก ์ •ํ™•๋„(SOTA)๋ฅผ ์‚ฐ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [namctin](https://huggingface.co/namctin), [gsinthong](https://huggingface.co/gsinthong), [diepi](https://huggingface.co/diepi), [vijaye12](https://huggingface.co/vijaye12), [wmgifford](https://huggingface.co/wmgifford), [kashif](https://huggingface.co/kashif)์— ์˜ํ•ด ๊ธฐ์—ฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/yuqinie98/PatchTST)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +์ด ๋ชจ๋ธ์€ ์‹œ๊ณ„์—ด ๋ถ„๋ฅ˜์™€ ์‹œ๊ณ„์—ด ํšŒ๊ท€์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ [`PatchTSTForClassification`]์™€ [`PatchTSTForRegression`] ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ์ž๋ฃŒ[[resources]] + +- PatchTST๋ฅผ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋Š” [์ด๊ณณ](https://huggingface.co/blog/patchtst)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด ๋ธ”๋กœ๊ทธ๋Š” Google Colab์—์„œ๋„ ์—ด์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## PatchTSTConfig[[transformers.PatchTSTConfig]] + +[[autodoc]] PatchTSTConfig + +## PatchTSTModel[[transformers.PatchTSTModel]] + +[[autodoc]] PatchTSTModel + - forward + +## PatchTSTForPrediction[[transformers.PatchTSTForPrediction]] + +[[autodoc]] PatchTSTForPrediction + - forward + +## PatchTSTForClassification[[transformers.PatchTSTForClassification]] + +[[autodoc]] PatchTSTForClassification + - forward + +## PatchTSTForPretraining[[transformers.PatchTSTForPretraining]] + +[[autodoc]] PatchTSTForPretraining + - forward + +## PatchTSTForRegression[[transformers.PatchTSTForRegression]] + +[[autodoc]] PatchTSTForRegression + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/qwen2_vl.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/qwen2_vl.md new file mode 100644 index 0000000000000000000000000000000000000000..5eb6f3bd11d48364184a6e4c5d70e716a3f11c7e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/qwen2_vl.md @@ -0,0 +1,303 @@ + + +# Qwen2-VL[[Qwen2-VL]] + +
+PyTorch +FlashAttention +
+ +## Overview[[Overview]] + +[Qwen2-VL](https://qwenlm.github.io/blog/qwen2-vl/) ๋ชจ๋ธ์€ ์•Œ๋ฆฌ๋ฐ”๋ฐ” ๋ฆฌ์„œ์น˜์˜ QwenํŒ€์—์„œ ๊ฐœ๋ฐœํ•œ [Qwen-VL](https://huggingface.co/papers/2308.12966) ๋ชจ๋ธ์˜ ์ฃผ์š” ์—…๋ฐ์ดํŠธ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. + +๋ธ”๋กœ๊ทธ์˜ ์š”์•ฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ด ๋ธ”๋กœ๊ทธ๋Š” ์ง€๋‚œ ๋ช‡ ๋…„๊ฐ„ Qwen-VL์—์„œ ์ค‘๋Œ€ํ•œ ๊ฐœ์„ ์„ ๊ฑฐ์ณ ๋ฐœ์ „๋œ Qwen2-VL ๋ชจ๋ธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ค‘์š” ๊ฐœ์„  ์‚ฌํ•ญ์€ ํ–ฅ์ƒ๋œ ์ด๋ฏธ์ง€ ์ดํ•ด, ๊ณ ๊ธ‰ ๋น„๋””์˜ค ์ดํ•ด, ํ†ตํ•ฉ ์‹œ๊ฐ ์—์ด์ „ํŠธ ๊ธฐ๋Šฅ, ํ™•์žฅ๋œ ๋‹ค์–ธ์–ด ์ง€์›์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋Š” Naive Dynamic Resolution ์ง€์›์„ ํ†ตํ•ด ์ž„์˜์˜ ์ด๋ฏธ์ง€ ํ•ด์ƒ๋„๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ตœ์ ํ™”๋˜์—ˆ์œผ๋ฉฐ, ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํšŒ์ „ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ(M-ROPE)์„ ํ™œ์šฉํ•˜์—ฌ 1D ํ…์ŠคํŠธ์™€ ๋‹ค์ฐจ์› ์‹œ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์—…๋ฐ์ดํŠธ๋œ ๋ชจ๋ธ์€ ์‹œ๊ฐ ๊ด€๋ จ ์ž‘์—…์—์„œ GPT-4o์™€ Claude 3.5 Sonnet ๊ฐ™์€ ์„ ๋„์ ์ธ AI ์‹œ์Šคํ…œ๊ณผ ๊ฒฝ์Ÿ๋ ฅ ์žˆ๋Š” ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, ํ…์ŠคํŠธ ๋Šฅ๋ ฅ์—์„œ๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ชจ๋ธ ์ค‘ ์ƒ์œ„๊ถŒ์— ๋žญํฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐœ์ „์€ Qwen2-VL์„ ๊ฐ•๋ ฅํ•œ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ฒ˜๋ฆฌ ๋ฐ ์ถ”๋ก  ๋Šฅ๋ ฅ์ด ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ์‘์šฉ ๋ถ„์•ผ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์žฌ๋‹ค๋Šฅํ•œ ๋„๊ตฌ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.* + + + + Qwen2-VL ๊ตฌ์กฐ. ์ถœ์ฒ˜: ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€ + +์ด ๋ชจ๋ธ์€ [simonJJJ](https://huggingface.co/simonJJJ)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ์‹œ[[Usage example]] + +### ๋‹จ์ผ ๋ฏธ๋””์–ด ์ถ”๋ก [[Single Media inference]] + +์ด ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€์™€ ๋น„๋””์˜ค๋ฅผ ๋ชจ๋‘ ์ธํ’‹์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ถ”๋ก ์„ ์œ„ํ•œ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. + +```python + +import torch +from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor + +# ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์žฅ์น˜์—์„œ ๋ชจ๋ธ์„ ๋ฐ˜ ์ •๋ฐ€๋„(half-precision)๋กœ ๋กœ๋“œ +model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", device_map="auto") +processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct") + + +conversation = [ + { + "role":"user", + "content":[ + { + "type":"image", + "url": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg" + }, + { + "type":"text", + "text":"Describe this image." + } + ] + } +] + +inputs = processor.apply_chat_template( + conversation, + add_generation_prompt=True, + tokenize=True, + return_dict=True, + return_tensors="pt" +).to(model.device) + +# ์ถ”๋ก : ์•„์›ƒํ’‹ ์ƒ์„ฑ +output_ids = model.generate(**inputs, max_new_tokens=128) +generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)] +output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True) +print(output_text) + + + +# ๋น„๋””์˜ค +conversation = [ + { + "role": "user", + "content": [ + {"type": "video", "path": "/path/to/video.mp4"}, + {"type": "text", "text": "What happened in the video?"}, + ], + } +] + +inputs = processor.apply_chat_template( + conversation, + video_fps=1, + add_generation_prompt=True, + tokenize=True, + return_dict=True, + return_tensors="pt" +).to(model.device) + + +# ์ถ”๋ก : ์•„์›ƒํ’‹ ์ƒ์„ฑ +output_ids = model.generate(**inputs, max_new_tokens=128) +generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)] +output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True) +print(output_text) +``` + +### ๋ฐฐ์น˜ ํ˜ผํ•ฉ ๋ฏธ๋””์–ด ์ถ”๋ก [[Batch Mixed Media Inference]] + +์ด ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€, ๋น„๋””์˜ค, ํ…์ŠคํŠธ ๋“ฑ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ๋ฐฐ์น˜ ์ž…๋ ฅ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. + +```python + +# ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๋Œ€ํ™” +conversation1 = [ + { + "role": "user", + "content": [ + {"type": "image", "path": "/path/to/image1.jpg"}, + {"type": "text", "text": "Describe this image."} + ] + } +] + +# ๋‘ ๊ฐœ์˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๋Œ€ํ™” +conversation2 = [ + { + "role": "user", + "content": [ + {"type": "image", "path": "/path/to/image2.jpg"}, + {"type": "image", "path": "/path/to/image3.jpg"}, + {"type": "text", "text": "What is written in the pictures?"} + ] + } +] + +# ์ˆœ์ˆ˜ ํ…์ŠคํŠธ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋Œ€ํ™” +conversation3 = [ + { + "role": "user", + "content": "who are you?" + } +] + + +# ํ˜ผํ•ฉ๋œ ๋ฏธ๋””์–ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋Œ€ํ™” +conversation4 = [ + { + "role": "user", + "content": [ + {"type": "image", "path": "/path/to/image3.jpg"}, + {"type": "image", "path": "/path/to/image4.jpg"}, + {"type": "video", "path": "/path/to/video.jpg"}, + {"type": "text", "text": "What are the common elements in these medias?"}, + ], + } +] + +conversations = [conversation1, conversation2, conversation3, conversation4] +# ๋ฐฐ์น˜ ์ถ”๋ก ์„ ์œ„ํ•œ ์ค€๋น„ +ipnuts = processor.apply_chat_template( + conversations, + video_fps=1, + add_generation_prompt=True, + tokenize=True, + return_dict=True, + return_tensors="pt" +).to(model.device) + + +# ๋ฐฐ์น˜ ์ถ”๋ก  +output_ids = model.generate(**inputs, max_new_tokens=128) +generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)] +output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True) +print(output_text) +``` + +### ์‚ฌ์šฉ ํŒ[[Usage Tips]] + +#### ์ด๋ฏธ์ง€ ํ•ด์ƒ๋„ ํŠธ๋ ˆ์ด๋“œ์˜คํ”„[[Image Resolution trade-off]] + +์ด ๋ชจ๋ธ์€ ๋‹ค์–‘ํ•œ ํ•ด์ƒ๋„์˜ ์ž…๋ ฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋””ํดํŠธ๋กœ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋„ค์ดํ‹ฐ๋ธŒ(native) ํ•ด์ƒ๋„๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋” ๋†’์€ ํ•ด์ƒ๋„๋ฅผ ์ ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด๋Š” ๋” ๋งŽ์€ ์—ฐ์‚ฐ ๋น„์šฉ์„ ์ดˆ๋ž˜ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ์ตœ์ ์˜ ์„ค์ •์„ ์œ„ํ•ด ์ตœ์†Œ ๋ฐ ์ตœ๋Œ€ ํ”ฝ์…€ ์ˆ˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +min_pixels = 224*224 +max_pixels = 2048*2048 +processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels) +``` + +์ œํ•œ๋œ GPU RAM์˜ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์ƒ๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +min_pixels = 256*28*28 +max_pixels = 1024*28*28 +processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels) +``` +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์ด๋ฏธ์ง€๊ฐ€ 256~1024๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 28์€ ๋ชจ๋ธ์ด 14 ํฌ๊ธฐ์˜ ํŒจ์น˜(patch)์™€ 2์˜ ์‹œ๊ฐ„ ํŒจ์น˜(temporal patch size)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์˜จ ๊ฐ’์ž…๋‹ˆ๋‹ค (14 ร— 2 = 28). + + +#### ๋‹ค์ค‘ ์ด๋ฏธ์ง€ ์ธํ’‹[[Multiple Image Inputs]] + +๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฏธ์ง€์™€ ๋น„๋””์˜ค ์ฝ˜ํ…์ธ ๋Š” ๋Œ€ํ™”์— ์ง์ ‘ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” ์ด๋ฏธ์ง€ ๋ฐ ๋น„๋””์˜ค์— ๋ผ๋ฒจ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ฐธ์กฐํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๋‹ค์Œ ์„ค์ •์„ ํ†ตํ•ด ์ด ๋™์ž‘์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +conversation = [ + { + "role": "user", + "content": [ + {"type": "image"}, + {"type": "text", "text": "Hello, how are you?"} + ] + }, + { + "role": "assistant", + "content": "I'm doing well, thank you for asking. How can I assist you today?" + }, + { + "role": "user", + "content": [ + {"type": "text", "text": "Can you describe these images and video?"}, + {"type": "image"}, + {"type": "image"}, + {"type": "video"}, + {"type": "text", "text": "These are from my vacation."} + ] + }, + { + "role": "assistant", + "content": "I'd be happy to describe the images and video for you. Could you please provide more context about your vacation?" + }, + { + "role": "user", + "content": "It was a trip to the mountains. Can you see the details in the images and video?" + } +] + +# ๋””ํดํŠธ: +prompt_without_id = processor.apply_chat_template(conversation, add_generation_prompt=True) +# ์˜ˆ์ƒ ์•„์›ƒํ’‹: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Hello, how are you?<|im_end|>\n<|im_start|>assistant\nI'm doing well, thank you for asking. How can I assist you today?<|im_end|>\n<|im_start|>user\nCan you describe these images and video?<|vision_start|><|image_pad|><|vision_end|><|vision_start|><|image_pad|><|vision_end|><|vision_start|><|video_pad|><|vision_end|>These are from my vacation.<|im_end|>\n<|im_start|>assistant\nI'd be happy to describe the images and video for you. Could you please provide more context about your vacation?<|im_end|>\n<|im_start|>user\nIt was a trip to the mountains. Can you see the details in the images and video?<|im_end|>\n<|im_start|>assistant\n' + + +# id ์ถ”๊ฐ€ +prompt_with_id = processor.apply_chat_template(conversation, add_generation_prompt=True, add_vision_id=True) +# ์˜ˆ์ƒ ์•„์›ƒํ’‹: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\nPicture 1: <|vision_start|><|image_pad|><|vision_end|>Hello, how are you?<|im_end|>\n<|im_start|>assistant\nI'm doing well, thank you for asking. How can I assist you today?<|im_end|>\n<|im_start|>user\nCan you describe these images and video?Picture 2: <|vision_start|><|image_pad|><|vision_end|>Picture 3: <|vision_start|><|image_pad|><|vision_end|>Video 1: <|vision_start|><|video_pad|><|vision_end|>These are from my vacation.<|im_end|>\n<|im_start|>assistant\nI'd be happy to describe the images and video for you. Could you please provide more context about your vacation?<|im_end|>\n<|im_start|>user\nIt was a trip to the mountains. Can you see the details in the images and video?<|im_end|>\n<|im_start|>assistant\n' + +``` + +#### ๋น ๋ฅธ ์ƒ์„ฑ์„ ์œ„ํ•œ Flash-Attention 2[[Flash-Attention 2 to speed up generation]] + +์ฒซ๋ฒˆ์งธ๋กœ, Flash Attention 2์˜ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install -U flash-attn --no-build-isolation +``` + +๋˜ํ•œ, Flash-Attention 2๋ฅผ ์ง€์›ํ•˜๋Š” ํ•˜๋“œ์›จ์–ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ ๋ฌธ์„œ์ธ [flash attention repository](https://github.com/Dao-AILab/flash-attention)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FlashAttention-2๋Š” ๋ชจ๋ธ์ด `torch.float16` ๋˜๋Š” `torch.bfloat16` ํ˜•์‹์œผ๋กœ ๋กœ๋“œ๋œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +Flash Attention-2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ๋•Œ `attn_implementation="flash_attention_2"` ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```python +from transformers import Qwen2VLForConditionalGeneration + +model = Qwen2VLForConditionalGeneration.from_pretrained( + "Qwen/Qwen2-VL-7B-Instruct", + torch_dtype=torch.bfloat16, + attn_implementation="flash_attention_2", +) +``` + +## Qwen2VLConfig + +[[autodoc]] Qwen2VLConfig + +## Qwen2VLImageProcessor + +[[autodoc]] Qwen2VLImageProcessor + - preprocess + +## Qwen2VLImageProcessorFast + +[[autodoc]] Qwen2VLImageProcessorFast + - preprocess + +## Qwen2VLProcessor + +[[autodoc]] Qwen2VLProcessor + +## Qwen2VLModel + +[[autodoc]] Qwen2VLModel + - forward + +## Qwen2VLForConditionalGeneration + +[[autodoc]] Qwen2VLForConditionalGeneration + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/rag.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/rag.md new file mode 100644 index 0000000000000000000000000000000000000000..0610eaa553bf791b0c84a35ef5278bdd9dc1b4bf --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/rag.md @@ -0,0 +1,100 @@ + + +# RAG(๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ) [[rag]] + +
+ +Models + +
+ +## ๊ฐœ์š” [[overview]] + +๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ(Retrieval-augmented generation, "RAG") ๋ชจ๋ธ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฐ€์ง‘ ๊ฒ€์ƒ‰(DPR)๊ณผ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์˜ ์žฅ์ ์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. RAG ๋ชจ๋ธ์€ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ์ด๋ฅผ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์— ์ „๋‹ฌํ•œ ๋‹ค์Œ, ์ฃผ๋ณ€ํ™”(marginalization)๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰๊ธฐ์™€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋“ˆ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ์ดˆ๊ธฐํ™”๋˜๋ฉฐ, ํ•จ๊ป˜ ๋ฏธ์„ธ ์กฐ์ •๋˜์–ด ๊ฒ€์ƒ‰๊ณผ ์ƒ์„ฑ ๋ชจ๋‘ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…(๋ชจ๋ธ์„ ํŠน์ • ํƒœ์Šคํฌ์— ์ ์šฉํ•˜๋Š” ๊ฒƒ)์— ์ ์‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Kรผttler, Mike Lewis, Wen-tau Yih, Tim Rocktรคschel, Sebastian Riedel, Douwe Kiela์˜ ๋…ผ๋ฌธ [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://huggingface.co/papers/2005.11401)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +*๋Œ€๊ทœ๋ชจ ์‚ฌ์ „ ํ›ˆ๋ จ ์–ธ์–ด ๋ชจ๋ธ๋“ค์€ ๊ทธ๋“ค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์‹ค์  ์ง€์‹์„ ์ €์žฅํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๋‹ค์šด์ŠคํŠธ๋ฆผ NLP ์ž‘์—…์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋  ๋•Œ ์ตœ์ฒจ๋‹จ ๊ฒฐ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ง€์‹์— ์ ‘๊ทผํ•˜๊ณ  ์ •ํ™•ํ•˜๊ฒŒ ์กฐ์ž‘ํ•˜๋Š” ๋Šฅ๋ ฅ์€ ์—ฌ์ „ํžˆ ์ œํ•œ์ ์ด๋ฉฐ, ๋”ฐ๋ผ์„œ ์ง€์‹ ์ง‘์•ฝ์  ์ž‘์—…์—์„œ ๊ทธ๋“ค์˜ ์„ฑ๋Šฅ์€ ์ž‘์—…๋ณ„ ์•„ํ‚คํ…์ฒ˜์— ๋น„ํ•ด ๋’ค๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ, ๊ทธ๋“ค์˜ ๊ฒฐ์ •์— ๋Œ€ํ•œ ๊ทผ๊ฑฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์„ธ๊ณ„ ์ง€์‹์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์€ ์—ฌ์ „ํžˆ ์—ด๋ฆฐ ์—ฐ๊ตฌ ๋ฌธ์ œ๋กœ ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช…์‹œ์  ๋น„๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ๋ฏธ๋ถ„ ๊ฐ€๋Šฅํ•œ ์ ‘๊ทผ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ฐ€์ง„ ์‚ฌ์ „ ํ›ˆ๋ จ ๋ชจ๋ธ์€ ์ด ๋ฌธ์ œ๋ฅผ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ถ”์ถœ์  ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ๋Œ€ํ•ด์„œ๋งŒ ์—ฐ๊ตฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์–ธ์–ด ์ƒ์„ฑ์„ ์œ„ํ•ด ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐ ๋น„๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ชจ๋ธ์ธ ๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ(RAG)์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๋ชฉ์ ์˜ ๋ฏธ์„ธ ์กฐ์ • ๋ฐฉ๋ฒ•์„ ํƒ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์ด๊ณ  ๋น„๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์‹ ๊ฒฝ ๊ฒ€์ƒ‰๊ธฐ๋กœ ์ ‘๊ทผ๋˜๋Š” ์œ„ํ‚คํ”ผ๋””์•„์˜ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ์ธ๋ฑ์Šค์ธ RAG ๋ชจ๋ธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ƒ์„ฑ๋œ ์ „์ฒด ์‹œํ€€์Šค์— ๊ฑธ์ณ ๋™์ผํ•œ ๊ฒ€์ƒ‰๋œ ๊ตฌ์ ˆ์„ ์กฐ๊ฑด์œผ๋กœ ํ•˜๋Š” RAG ๊ณต์‹๊ณผ ํ† ํฐ๋ณ„๋กœ ๋‹ค๋ฅธ ๊ตฌ์ ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” RAG ๊ณต์‹์„ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ์ง€์‹ ์ง‘์•ฝ์  NLP ์ž‘์—…์— ๋Œ€ํ•ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ํ‰๊ฐ€ํ•˜๋ฉฐ, ๋งค๊ฐœ๋ณ€์ˆ˜ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ๊ณผ ์ž‘์—…๋ณ„ ๊ฒ€์ƒ‰-์ถ”์ถœ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋Šฅ๊ฐ€ํ•˜์—ฌ ์„ธ ๊ฐ€์ง€ ๊ฐœ๋ฐฉํ˜• ๋„๋ฉ”์ธ QA ์ž‘์—…์—์„œ ์ตœ์ฒจ๋‹จ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ์ƒ์„ฑ ์ž‘์—…์˜ ๊ฒฝ์šฐ, RAG ๋ชจ๋ธ์ด ์ตœ์ฒจ๋‹จ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „์šฉ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๊ธฐ์ค€์„ ๋ณด๋‹ค ๋” ๊ตฌ์ฒด์ ์ด๊ณ , ๋‹ค์–‘ํ•˜๋ฉฐ, ์‚ฌ์‹ค์ ์ธ ์–ธ์–ด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [ola13](https://huggingface.co/ola13)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ(Retrieval-augmented generation, "RAG") ๋ชจ๋ธ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฐ€์ง‘ ๊ฒ€์ƒ‰(DPR)๊ณผ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์˜ ๊ฐ•์ ์„ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. RAG ๋ชจ๋ธ์€ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ์ด๋ฅผ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์— ์ „๋‹ฌํ•œ ๋‹ค์Œ, ์ฃผ๋ณ€ํ™”(marginalization)๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰๊ธฐ์™€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋“ˆ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ์ดˆ๊ธฐํ™”๋˜๋ฉฐ, ํ•จ๊ป˜ ๋ฏธ์„ธ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰๊ณผ ์ƒ์„ฑ ๋ชจ๋‘ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ์ ์‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + +## RagConfig [[transformers.RagConfig]] + +[[autodoc]] RagConfig + +## RagTokenizer [[transformers.RagTokenizer]] + +[[autodoc]] RagTokenizer + +## Rag specific outputs [[transformers.models.rag.modeling_rag.RetrievAugLMMarginOutput]] + +[[autodoc]] models.rag.modeling_rag.RetrievAugLMMarginOutput + +[[autodoc]] models.rag.modeling_rag.RetrievAugLMOutput + +## RagRetriever [[transformers.RagRetriever]] + +[[autodoc]] RagRetriever + + + + +## RagModel [[transformers.RagModel]] + +[[autodoc]] RagModel + - forward + +## RagSequenceForGeneration [[transformers.RagSequenceForGeneration]] + +[[autodoc]] RagSequenceForGeneration + - forward + - generate + +## RagTokenForGeneration [[transformers.RagTokenForGeneration]] + +[[autodoc]] RagTokenForGeneration + - forward + - generate + + + + +## TFRagModel [[transformers.TFRagModel]] + +[[autodoc]] TFRagModel + - call + +## TFRagSequenceForGeneration [[transformers.TFRagSequenceForGeneration]] + +[[autodoc]] TFRagSequenceForGeneration + - call + - generate + +## TFRagTokenForGeneration [[transformers.TFRagTokenForGeneration]] + +[[autodoc]] TFRagTokenForGeneration + - call + - generate + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/roberta.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/roberta.md new file mode 100644 index 0000000000000000000000000000000000000000..e17d7df90c9989b78b66d35bf7ecc360b201b86e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/roberta.md @@ -0,0 +1,230 @@ + + +# RoBERTa[[roberta]] + +
+PyTorch +TensorFlow +Flax +SDPA +
+ +## ๊ฐœ์š”[[overview]] + +RoBERTa ๋ชจ๋ธ์€ Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://huggingface.co/papers/1907.11692)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 2018๋…„์— ๊ตฌ๊ธ€์—์„œ ๋ฐœํ‘œํ•œ BERT ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +RoBERTa๋Š” BERT๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉฐ, ์ฃผ์š” ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ , ์‚ฌ์ „ ํ•™์Šต ๋‹จ๊ณ„์—์„œ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next Sentence Prediction)์„ ์ œ๊ฑฐํ–ˆ์œผ๋ฉฐ, ํ›จ์”ฌ ๋” ํฐ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ํ•™์Šต๋ฅ ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*์–ธ์–ด ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต์€ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œ์ผฐ์ง€๋งŒ, ์„œ๋กœ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์„ ๋ฉด๋ฐ€ํžˆ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ•™์Šต์€ ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋งŽ์ด ๋“ค๊ณ , ์ข…์ข… ํฌ๊ธฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๋น„๊ณต๊ฐœ ๋ฐ์ดํ„ฐ์…‹์—์„œ ์ˆ˜ํ–‰๋˜๋ฉฐ, ๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ณด์—ฌ์ฃผ๋“ฏ์ด ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ์„ ํƒ์ด ์ตœ์ข… ์„ฑ๋Šฅ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์นฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” BERT ์‚ฌ์ „ ํ•™์Šต(Devlin et al., 2019)์— ๋Œ€ํ•œ ์žฌํ˜„ ์—ฐ๊ตฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ, ์—ฌ๋Ÿฌ ํ•ต์‹ฌ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํ•™์Šต ๋ฐ์ดํ„ฐ ํฌ๊ธฐ์˜ ์˜ํ–ฅ์„ ๋ฉด๋ฐ€ํžˆ ์ธก์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, BERT๋Š” ์ถฉ๋ถ„ํžˆ ํ•™์Šต๋˜์ง€ ์•Š์•˜์œผ๋ฉฐ, ์ดํ›„ ๋ฐœํ‘œ๋œ ๋ชจ๋“  ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋งž์ถ”๊ฑฐ๋‚˜ ๋Šฅ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ œ์•ˆํ•œ ์ตœ์ƒ์˜ ๋ชจ๋ธ์€ GLUE, RACE, SQuAD์—์„œ ์ตœ๊ณ  ์„ฑ๋Šฅ(state-of-the-art)์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ณผ๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๊ฐ„๊ณผ๋˜์–ด ์˜จ ์„ค๊ณ„ ์„ ํƒ์˜ ์ค‘์š”์„ฑ์„ ๊ฐ•์กฐํ•˜๋ฉฐ, ์ตœ๊ทผ ๋ณด๊ณ ๋œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์˜ ๊ทผ์›์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ์˜๋ฌธ์„ ์ œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ณธ ์—ฐ๊ตฌ์—์„œ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ๊ณผ ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [julien-c](https://huggingface.co/julien-c)๊ฐ€ ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/pytorch/fairseq/tree/master/examples/roberta)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- ์ด ๊ตฌํ˜„์€ [`BertModel`]๊ณผ ๋™์ผํ•˜์ง€๋งŒ, ์ž„๋ฒ ๋”ฉ ๋ถ€๋ถ„์— ์•ฝ๊ฐ„์˜ ์ˆ˜์ •์ด ์žˆ์œผ๋ฉฐ RoBERTa ์‚ฌ์ „ํ•™์Šต ๋ชจ๋ธ์— ๋งž๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- RoBERTa๋Š” BERT์™€ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ํ† ํฌ๋‚˜์ด์ €๋กœ ๋ฐ”์ดํŠธ ์ˆ˜์ค€ BPE(Byte-Pair Encoding, GPT-2์™€ ๋™์ผ)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์‚ฌ์ „ํ•™์Šต ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. +- RoBERTa๋Š” `token_type_ids`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋–ค ํ† ํฐ์ด ์–ด๋–ค ๋ฌธ์žฅ(segment)์— ์†ํ•˜๋Š”์ง€ ๋ณ„๋„๋กœ ํ‘œ์‹œํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ฌธ์žฅ ๊ตฌ๋ถ„์€ ๋ถ„๋ฆฌ ํ† ํฐ `tokenizer.sep_token`(๋˜๋Š” `
`)์„ ์‚ฌ์šฉํ•ด ๋‚˜๋ˆ„๋ฉด ๋ฉ๋‹ˆ๋‹ค. +- RoBERTa๋Š” BERT์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋” ๋‚˜์€ ์‚ฌ์ „ํ•™์Šต ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + + * ๋™์  ๋งˆ์Šคํ‚น: RoBERTa๋Š” ๋งค ์—ํญ๋งˆ๋‹ค ํ† ํฐ์„ ๋‹ค๋ฅด๊ฒŒ ๋งˆ์Šคํ‚นํ•˜๋Š” ๋ฐ˜๋ฉด, BERT๋Š” ํ•œ ๋ฒˆ๋งŒ ๋งˆ์Šคํ‚นํ•ฉ๋‹ˆ๋‹ค. + * ๋ฌธ์žฅ ํŒจํ‚น: ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ตœ๋Œ€ 512 ํ† ํฐ๊นŒ์ง€ ํ•จ๊ป˜ ํŒจํ‚นํ•˜์—ฌ, ๋ฌธ์žฅ์ด ์—ฌ๋Ÿฌ ๋ฌธ์„œ์— ๊ฑธ์ณ ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + * ๋” ํฐ ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ: ํ•™์Šต ์‹œ ๋” ํฐ ๋ฏธ๋‹ˆ๋ฐฐ์น˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + * ๋ฐ”์ดํŠธ ์ˆ˜์ค€ BPE ์–ดํœ˜: ๋ฌธ์ž๋ฅผ ๋‹จ์œ„๋กœ ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ BPE๋ฅผ ์ ์šฉํ•˜์—ฌ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ๋” ์œ ์—ฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +- [CamemBERT](camembert)์€ RoBERTa๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ž˜ํผ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์ œ๋Š” ํ•ด๋‹น ๋ชจ๋ธ ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +## ์ž๋ฃŒ[[resources]] + +RoBERTa๋ฅผ ์ฒ˜์Œ ๋‹ค๋ฃฐ ๋•Œ ๋„์›€์ด ๋˜๋Š” Hugging Face ๊ณต์‹ ์ž๋ฃŒ์™€ ์ปค๋ฎค๋‹ˆํ‹ฐ ์ž๋ฃŒ(๐ŸŒŽ ์•„์ด์ฝ˜์œผ๋กœ ํ‘œ์‹œ๋จ) ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์ด ๋ชฉ๋ก์— ์ž๋ฃŒ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ Pull Request๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”! ์ €ํฌ๊ฐ€ ๊ฒ€ํ†  ํ›„ ๋ฐ˜์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋‹จ์ˆœํžˆ ๋ณต์ œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ƒˆ๋กญ๊ฑฐ๋‚˜ ์œ ์˜๋ฏธํ•œ ๋‚ด์šฉ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +- RoBERTa์™€ [Inference API](https://huggingface.co/inference-api)๋ฅผ ํ™œ์šฉํ•œ [ํŠธ์œ„ํ„ฐ ๊ฐ์„ฑ ๋ถ„์„ ์‹œ์ž‘ํ•˜๊ธฐ](https://huggingface.co/blog/sentiment-analysis-twitter) ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- RoBERTa๋ฅผ ํ™œ์šฉํ•œ [Kili ๋ฐ Hugging Face AutoTrain์„ ์ด์šฉํ•œ ์˜๊ฒฌ ๋ถ„๋ฅ˜](https://huggingface.co/blog/opinion-classification-with-kili)์— ๊ด€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [๊ฐ์„ฑ ๋ถ„์„์„ ์œ„ํ•œ RoBERTa ๋ฏธ์„ธ์กฐ์ •](https://colab.research.google.com/github/DhavalTaunk08/NLP_scripts/blob/master/sentiment_analysis_using_roberta.ipynb)์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ.๐ŸŒŽ +- ['RobertaForSequenceClassification']์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFRobertaForSequenceClassification`]๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxRobertaForSequenceClassification`]๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/sequence_classification) + + + +- [`RobertaForTokenClassification`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFRobertaForTokenClassification`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/token-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxRobertaForTokenClassification`]๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/token-classification)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [ํ† ํฐ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/2?fw=pt) +- [ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/token_classification) + + + +- RoBERTa๋ฅผ ํ™œ์šฉํ•œ [Transformers์™€ Tokenizers๋ฅผ ํ™œ์šฉํ•œ ์ƒˆ๋กœ์šด ์–ธ์–ด ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/blog/how-to-train)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [`RobertaForMaskedLM`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/language-modeling#robertabertdistilbert-and-masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFRobertaForMaskedLM`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/language-modeling#run_mlmpy)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxRobertaForMaskedLM`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling#masked-language-modeling)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ๋ง ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/3?fw=pt) +- [๋งˆ์Šคํ‚น ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/masked_language_modeling) + + + +- RoBERTa๋ฅผ ํ™œ์šฉํ•œ ์งˆ๋ฌธ ์‘๋‹ต ์ž‘์—…์—์„œ์˜ [Optimum๊ณผ Transformers ํŒŒ์ดํ”„๋ผ์ธ์„ ์ด์šฉํ•œ ์ถ”๋ก  ๊ฐ€์†ํ™”](https://huggingface.co/blog/optimum-inference)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ. +- [`RobertaForQuestionAnswering`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFRobertaForQuestionAnswering`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/question-answering)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`FlaxRobertaForQuestionAnswering`]์€ [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/flax/question-answering)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๐Ÿค— Hugging Face ์ฝ”์Šค์˜ [์งˆ์˜์‘๋‹ต ์ฑ•ํ„ฐ](https://huggingface.co/course/chapter7/7?fw=pt) +- [์งˆ์˜์‘๋‹ต ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/question_answering) + +**๋‹ค์ค‘ ์„ ํƒ** +- [`RobertaForMultipleChoice`]๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [`TFRobertaForMultipleChoice`]๋Š” [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/multiple-choice)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb)์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- [๋‹ค์ค‘ ์„ ํƒ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/multiple_choice) + +## RobertaConfig + +[[autodoc]] RobertaConfig + +## RobertaTokenizer + +[[autodoc]] RobertaTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## RobertaTokenizerFast + +[[autodoc]] RobertaTokenizerFast + - build_inputs_with_special_tokens + + + + +## RobertaModel + +[[autodoc]] RobertaModel + - forward + +## RobertaForCausalLM + +[[autodoc]] RobertaForCausalLM + - forward + +## RobertaForMaskedLM + +[[autodoc]] RobertaForMaskedLM + - forward + +## RobertaForSequenceClassification + +[[autodoc]] RobertaForSequenceClassification + - forward + +## RobertaForMultipleChoice + +[[autodoc]] RobertaForMultipleChoice + - forward + +## RobertaForTokenClassification + +[[autodoc]] RobertaForTokenClassification + - forward + +## RobertaForQuestionAnswering + +[[autodoc]] RobertaForQuestionAnswering + - forward + + + + +## TFRobertaModel + +[[autodoc]] TFRobertaModel + - call + +## TFRobertaForCausalLM + +[[autodoc]] TFRobertaForCausalLM + - call + +## TFRobertaForMaskedLM + +[[autodoc]] TFRobertaForMaskedLM + - call + +## TFRobertaForSequenceClassification + +[[autodoc]] TFRobertaForSequenceClassification + - call + +## TFRobertaForMultipleChoice + +[[autodoc]] TFRobertaForMultipleChoice + - call + +## TFRobertaForTokenClassification + +[[autodoc]] TFRobertaForTokenClassification + - call + +## TFRobertaForQuestionAnswering + +[[autodoc]] TFRobertaForQuestionAnswering + - call + + + + +## FlaxRobertaModel + +[[autodoc]] FlaxRobertaModel + - __call__ + +## FlaxRobertaForCausalLM + +[[autodoc]] FlaxRobertaForCausalLM + - __call__ + +## FlaxRobertaForMaskedLM + +[[autodoc]] FlaxRobertaForMaskedLM + - __call__ + +## FlaxRobertaForSequenceClassification + +[[autodoc]] FlaxRobertaForSequenceClassification + - __call__ + +## FlaxRobertaForMultipleChoice + +[[autodoc]] FlaxRobertaForMultipleChoice + - __call__ + +## FlaxRobertaForTokenClassification + +[[autodoc]] FlaxRobertaForTokenClassification + - __call__ + +## FlaxRobertaForQuestionAnswering + +[[autodoc]] FlaxRobertaForQuestionAnswering + - __call__ + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/siglip.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/siglip.md new file mode 100644 index 0000000000000000000000000000000000000000..cc9d93184baf45ef31029b0a08f51169eaca3060 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/siglip.md @@ -0,0 +1,253 @@ + + +# SigLIP[[siglip]] + +
+PyTorch +FlashAttention +SDPA +
+ +## ๊ฐœ์š”[[overview]] + +SigLIP ๋ชจ๋ธ์€ Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer์˜ [Sigmoid Loss for Language Image Pre-Training](https://huggingface.co/papers/2303.15343) ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. SigLIP์€ [CLIP](clip)์—์„œ ์‚ฌ์šฉ๋œ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ๊ฐ„๋‹จํ•œ ์Œ๋ณ„ ์‹œ๊ทธ๋ชจ์ด๋“œ ์†์‹ค(pairwise sigmoid loss)๋กœ ๋Œ€์ฒดํ•  ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ImageNet์—์„œ ์ œ๋กœ์ƒท ๋ถ„๋ฅ˜ ์ •ํ™•๋„ ์ธก๋ฉด์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” ์–ธ์–ด-์ด๋ฏธ์ง€ ์‚ฌ์ „ ํ•™์Šต(Language-Image Pre-training, SigLIP)์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์Œ๋ณ„ ์‹œ๊ทธ๋ชจ์ด๋“œ ์†์‹ค์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ์ •๊ทœํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ‘œ์ค€ ๋Œ€์กฐ ํ•™์Šต๊ณผ ๋‹ฌ๋ฆฌ, ์‹œ๊ทธ๋ชจ์ด๋“œ ์†์‹ค์€ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์Œ์—๋งŒ ์ž‘์šฉํ•˜๋ฉฐ ์ •๊ทœํ™”๋ฅผ ์œ„ํ•ด ์Œ๋ณ„ ์œ ์‚ฌ์„ฑ์˜ ์ „์—ญ์  ๊ด€์ ์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ์†์‹ค์€ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋”์šฑ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋™์‹œ์— ์ž‘์€ ๋ฐฐ์น˜ ํฌ๊ธฐ์—์„œ๋„ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. Locked-image Tuning๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ, ๋‹จ 4๊ฐœ์˜ TPUv4 ์นฉ๋งŒ์œผ๋กœ ์ดํ‹€ ๋งŒ์— 84.5%์˜ ImageNet ์ œ๋กœ์ƒท ์ •ํ™•๋„๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” SigLiT ๋ชจ๋ธ์„ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜์—์„œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋ถ„๋ฆฌํ•จ์œผ๋กœ์จ ์˜ˆ์ œ ๋Œ€ ์Œ์˜ ์˜ํ–ฅ๊ณผ Negative ๋Œ€ Positive ๋น„์œจ์„ ์—ฐ๊ตฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์šฐ๋ฆฌ๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 100๋งŒ ๊ฐœ๊นŒ์ง€ ๊ทน๋‹จ์ ์œผ๋กœ ๋Š˜๋ ค๋ณด์•˜๊ณ , ๋ฐฐ์น˜ ํฌ๊ธฐ ์ฆ๊ฐ€์˜ ์ด์ ์ด ๋น ๋ฅด๊ฒŒ ๊ฐ์†Œํ•˜๋ฉฐ 32k์˜ ๋” ํ•ฉ๋ฆฌ์ ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ๋„ ์ถฉ๋ถ„ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.* + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- SigLIP์˜ ์‚ฌ์šฉ๋ฒ•์€ [CLIP](clip)๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ํ•™์Šต ์†์‹ค ํ•จ์ˆ˜๋กœ, ๋ฐฐ์น˜ ๋‚ด ๋ชจ๋“  ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ๊ฐ„์˜ ์Œ๋ณ„ ์œ ์‚ฌ์„ฑ์— ๋Œ€ํ•œ ์ „์—ญ์  ๊ด€์ ์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ๋Œ€์‹  ๋กœ์ง“์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํ•™์Šต์€ ์ง€์›๋˜์ง€๋งŒ `torch.distributed` ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„ ๋ฐฐ์น˜ ํฌ๊ธฐ์˜ ํ™•์žฅ์„ฑ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์ผ ๋…ธ๋“œ ๋‹ค์ค‘ GPU ์„ค์ •์—์„œ๋Š” DDP์™€ FDSP๊ฐ€ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +- ๋…๋ฆฝํ˜• [`SiglipTokenizer`] ๋˜๋Š” [`SiglipProcessor`]๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ชจ๋ธ์ด ๊ทธ๋ ‡๊ฒŒ ํ•™์Šต๋˜์—ˆ์œผ๋ฏ€๋กœ `padding="max_length"`๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด "This is a photo of {label}."์˜ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + + CLIP๊ณผ ๋น„๊ตํ•œ SigLIP ํ‰๊ฐ€ ๊ฒฐ๊ณผ. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ฐœ์ทŒ. + +์ด ๋ชจ๋ธ์€ [nielsr](https://huggingface.co/nielsr)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/google-research/big_vision/tree/main)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ์˜ˆ์‹œ[[usage-example]] + +SigLIP์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค: ๋ชจ๋“  ๋ณต์žก์„ฑ์„ ์ถ”์ƒํ™”ํ•˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ API๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์ง์ ‘ `SiglipModel` ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +### ํŒŒ์ดํ”„๋ผ์ธ API[[pipeline-API]] + +ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import pipeline +>>> from PIL import Image +>>> import requests + +>>> # ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ +>>> image_classifier = pipeline(task="zero-shot-image-classification", model="google/siglip-base-patch16-224") + +>>> # ์ด๋ฏธ์ง€ ๋กœ๋“œ +>>> url = 'http://images.cocodataset.org/val2017/000000039769.jpg' +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> # ์ถ”๋ก  +>>> candidate_labels = ["2 cats", "a plane", "a remote"] +>>> outputs = image_classifier(image, candidate_labels=candidate_labels) +>>> outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs] +>>> print(outputs) +[{'score': 0.1979, 'label': '2 cats'}, {'score': 0.0, 'label': 'a remote'}, {'score': 0.0, 'label': 'a plane'}] +``` + +### ์ง์ ‘ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ[[using-the-model-yourself]] + +์ „์ฒ˜๋ฆฌ์™€ ํ›„์ฒ˜๋ฆฌ๋ฅผ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```python +>>> from PIL import Image +>>> import requests +>>> from transformers import AutoProcessor, AutoModel +>>> import torch + +>>> model = AutoModel.from_pretrained("google/siglip-base-patch16-224") +>>> processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> candidate_labels = ["2 cats", "2 dogs"] +# ํŒŒ์ดํ”„๋ผ์ธ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ์„ ๋”ฐ๋ผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค +>>> texts = [f'This is a photo of {label}.' for label in candidate_labels] +# ์ค‘์š”: ๋ชจ๋ธ์ด ์ด๋ ‡๊ฒŒ ํ•™์Šต๋˜์—ˆ์œผ๋ฏ€๋กœ `padding=max_length`๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค +>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt") + +>>> with torch.no_grad(): +... outputs = model(**inputs) + +>>> logits_per_image = outputs.logits_per_image +>>> probs = torch.sigmoid(logits_per_image) # ์‹œ๊ทธ๋ชจ์ด๋“œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค +>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'") +19.8% that image 0 is '2 cats' +``` + +## ๋ฆฌ์†Œ์Šค[[resources]] + +SigLIP์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ณต์‹ Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ) ๋ฆฌ์†Œ์Šค ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. + +- [์ œ๋กœ์ƒท ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/zero_shot_image_classification) +- SigLIP์— ๋Œ€ํ•œ ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์—ฌ๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/SigLIP)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐ŸŒŽ + +์—ฌ๊ธฐ์— ํฌํ•จ๋  ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ์ถœํ•˜๋Š” ๋ฐ ๊ด€์‹ฌ์ด ์žˆ์œผ์‹œ๋ฉด Pull Request๋ฅผ ์—ด์–ด์ฃผ์‹œ๋ฉด ๊ฒ€ํ† ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฆฌ์†Œ์Šค๋Š” ์ด์ƒ์ ์œผ๋กœ ๊ธฐ์กด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + +## SigLIP๊ณผ Flash Attention 2 ๊ฒฐํ•ฉํ•˜๊ธฐ[[combining-siglip-with-flash-attention-2]] + +๋จผ์ € Flash Attention 2์˜ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```bash +pip install -U flash-attn --no-build-isolation +``` + +๋˜ํ•œ Flash-Attention 2์™€ ํ˜ธํ™˜๋˜๋Š” ํ•˜๋“œ์›จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. flash-attn ์ €์žฅ์†Œ์˜ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด์„ธ์š”. ๋˜ํ•œ ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16`)๋กœ ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +Flash Attention 2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”: + +```python +>>> import torch +>>> import requests +>>> from PIL import Image +>>> from transformers import SiglipProcessor, SiglipModel +>>> device = "cuda" # ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์žฅ์น˜ + +>>> model = SiglipModel.from_pretrained( +... "google/siglip-so400m-patch14-384", +... attn_implementation="flash_attention_2", +... torch_dtype=torch.float16, +... device_map=device, +... ) +>>> processor = SiglipProcessor.from_pretrained("google/siglip-so400m-patch14-384") + +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" +>>> image = Image.open(requests.get(url, stream=True).raw) + +>>> candidate_labels = ["2 cats", "2 dogs"] +# ํŒŒ์ดํ”„๋ผ์ธ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ์„ ๋”ฐ๋ผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค +>>> texts = [f'This is a photo of {label}.' for label in candidate_labels] +# ์ค‘์š”: ๋ชจ๋ธ์ด ์ด๋ ‡๊ฒŒ ํ•™์Šต๋˜์—ˆ์œผ๋ฏ€๋กœ `padding=max_length`๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค +>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt").to(device) + +>>> with torch.no_grad(): +... with torch.autocast(device): +... outputs = model(**inputs) + +>>> logits_per_image = outputs.logits_per_image +>>> probs = torch.sigmoid(logits_per_image) # ์‹œ๊ทธ๋ชจ์ด๋“œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค +>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'") +19.8% that image 0 is '2 cats' +``` + + +## Scaled Dot Product Attention(SDPA) ์‚ฌ์šฉํ•˜๊ธฐ[using-scaled-dot-product-attention(SDPA)]] + +PyTorch๋Š” `torch.nn.functional`์˜ ์ผ๋ถ€๋กœ ์Šค์ผ€์ผ๋œ ์ ๊ณฑ ์–ดํ…์…˜(SDPA) ์—ฐ์‚ฐ์ž๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” +์ž…๋ ฅ๊ณผ ์‚ฌ์šฉ ์ค‘์ธ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ตฌํ˜„์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ +[๊ณต์‹ ๋ฌธ์„œ](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) +๋˜๋Š” [GPU ์ถ”๋ก ](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) +ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +`from_pretrained()`์—์„œ `attn_implementation="sdpa"`๋ฅผ ์„ค์ •ํ•˜์—ฌ SDPA๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `torch>=2.1.1`์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```python +>>> from transformers import SiglipModel + +>>> model = SiglipModel.from_pretrained( +... "google/siglip-so400m-patch14-384", +... attn_implementation="sdpa", +... torch_dtype=torch.float16, +... device_map=device, +... ) +``` + +์ตœ์ƒ์˜ ์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16` ๋˜๋Š” `torch.bfloat16`)๋กœ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + +## ์˜ˆ์ƒ ์†๋„ ํ–ฅ์ƒ[[expected-speedups]] + +์•„๋ž˜๋Š” `google/siglip-so400m-patch14-384` ์ฒดํฌํฌ์ธํŠธ๋ฅผ `float16` ์ •๋ฐ€๋„๋กœ ์‚ฌ์šฉํ•˜๋Š” transformers์˜ ๋„ค์ดํ‹ฐ๋ธŒ ๊ตฌํ˜„๊ณผ Flash Attention 2 / SDPA ๋ฒ„์ „์˜ ๋ชจ๋ธ์„ ๋‹ค์–‘ํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ๋น„๊ตํ•œ ์ถ”๋ก  ์‹œ๊ฐ„์˜ ์˜ˆ์ƒ ์†๋„ ํ–ฅ์ƒ ๋‹ค์ด์–ด๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. + +
+ +
+ + +## SiglipConfig + +[[autodoc]] SiglipConfig + - from_text_vision_configs + +## SiglipTextConfig + +[[autodoc]] SiglipTextConfig + +## SiglipVisionConfig + +[[autodoc]] SiglipVisionConfig + +## SiglipTokenizer + +[[autodoc]] SiglipTokenizer + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## SiglipImageProcessor + +[[autodoc]] SiglipImageProcessor + - preprocess + +## SiglipImageProcessorFast + +[[autodoc]] SiglipImageProcessorFast + - preprocess + +## SiglipProcessor + +[[autodoc]] SiglipProcessor + +## SiglipModel + +[[autodoc]] SiglipModel + - forward + - get_text_features + - get_image_features + +## SiglipTextModel + +[[autodoc]] SiglipTextModel + - forward + +## SiglipVisionModel + +[[autodoc]] SiglipVisionModel + - forward + + +## SiglipForImageClassification + +[[autodoc]] SiglipForImageClassification + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin.md new file mode 100644 index 0000000000000000000000000000000000000000..ba1088210fea33ffff729535f814a14c337fbcbb --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin.md @@ -0,0 +1,95 @@ + + +# Swin Transformer [[swin-transformer]] + +## ๊ฐœ์š” [[overview]] + +Swin Transformer๋Š” Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://huggingface.co/papers/2103.14030)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์ด ๋…ผ๋ฌธ์€ Swin Transformer๋ผ๋Š” ์ƒˆ๋กœ์šด ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ปดํ“จํ„ฐ ๋น„์ „์—์„œ ๋ฒ”์šฉ ๋ฐฑ๋ณธ(backbone)์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์–ธ์–ด์—์„œ ๋น„์ „์œผ๋กœ ์ ์šฉํ•  ๋•Œ์˜ ์–ด๋ ค์›€์€ ๋‘ ๋ถ„์•ผ ๊ฐ„์˜ ์ฐจ์ด์—์„œ ๋น„๋กฏ๋˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œ๊ฐ์  ๊ฐ์ฒด์˜ ํฌ๊ธฐ๊ฐ€ ํฌ๊ฒŒ ๋ณ€๋™ํ•˜๋ฉฐ, ์ด๋ฏธ์ง€์˜ ํ”ฝ์…€ ํ•ด์ƒ๋„๊ฐ€ ํ…์ŠคํŠธ์˜ ๋‹จ์–ด์— ๋น„ํ•ด ๋งค์šฐ ๋†’๋‹ค๋Š” ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฐจ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, ์šฐ๋ฆฌ๋Š” 'Shifted Windows'๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณ„์ธต์  ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. Shifted Windows ๋ฐฉ์‹์€ ๊ฒน์น˜์ง€ ์•Š๋Š” ๋กœ์ปฌ ์œˆ๋„์šฐ์—์„œ self-attention ๊ณ„์‚ฐ์„ ์ œํ•œํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๋†’์ด๋Š” ๋™์‹œ์— ์œˆ๋„์šฐ ๊ฐ„ ์—ฐ๊ฒฐ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณ„์ธต์  ๊ตฌ์กฐ๋Š” ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ ํŒจํ„ด์„ ๋ชจ๋ธ๋งํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋ฏธ์ง€ ํฌ๊ธฐ์— ๋น„๋ก€ํ•œ ์„ ํ˜• ๊ณ„์‚ฐ ๋ณต์žก์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Swin Transformer์˜ ์ด๋Ÿฌํ•œ ํŠน์ง•๋“ค์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜(Imagenet-1K์—์„œ 87.3์˜ top-1 ์ •ํ™•๋„) ๋ฐ ๊ฐ์ฒด ๊ฒ€์ถœ(COCO test-dev์—์„œ 58.7์˜ ๋ฐ•์Šค AP, 51.1์˜ ๋งˆ์Šคํฌ AP)๊ณผ ๊ฐ™์€ ๋ฐ€์ง‘ ์˜ˆ์ธก ์ž‘์—…, ์˜๋ฏธ์  ๋ถ„ํ• (ADE20K val์—์„œ 53.5์˜ mIoU)๊ณผ ๊ฐ™์€ ๊ด‘๋ฒ”์œ„ํ•œ ๋น„์ „ ์ž‘์—…์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ COCO์—์„œ ์ด์ „ ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋ฐ•์Šค AP์—์„œ +2.7, ๋งˆ์Šคํฌ AP์—์„œ +2.6, ADE20K์—์„œ mIoU์—์„œ +3.2๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ์„ฑ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋ฉฐ, ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์ด ๋น„์ „ ๋ฐฑ๋ณธ์œผ๋กœ์„œ์˜ ์ž ์žฌ๋ ฅ์„ ์ž…์ฆํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณ„์ธต์  ์„ค๊ณ„์™€ Shifted Windows ๋ฐฉ์‹์€ ์ˆœ์ˆ˜ MLP ์•„ํ‚คํ…์ฒ˜์—๋„ ์œ ๋ฆฌํ•˜๊ฒŒ ์ž‘์šฉํ•ฉ๋‹ˆ๋‹ค.* + + + + Swin Transformer ์•„ํ‚คํ…์ฒ˜. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ฐœ์ทŒ. + +์ด ๋ชจ๋ธ์€ [novice03](https://huggingface.co/novice03)์ด ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. Tensorflow ๋ฒ„์ „์€ [amyeroberts](https://huggingface.co/amyeroberts)๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/microsoft/Swin-Transformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- Swin์€ ์ž…๋ ฅ์˜ ๋†’์ด์™€ ๋„ˆ๋น„๊ฐ€ `32`๋กœ ๋‚˜๋ˆ„์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฉด ์–ด๋–ค ํฌ๊ธฐ๋“  ์ง€์›ํ•  ์ˆ˜ ์žˆ๋„๋ก ํŒจ๋”ฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +- Swin์€ *๋ฐฑ๋ณธ*์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `output_hidden_states = True`๋กœ ์„ค์ •ํ•˜๋ฉด, `hidden_states`์™€ `reshaped_hidden_states`๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. `reshaped_hidden_states`๋Š” `(batch, num_channels, height, width)` ํ˜•์‹์„ ๊ฐ€์ง€๋ฉฐ, ์ด๋Š” `(batch_size, sequence_length, num_channels)` ํ˜•์‹๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +Swin Transformer์˜ ์‚ฌ์šฉ์„ ๋„์šธ ์ˆ˜ ์žˆ๋Š” Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ)์˜ ๊ณต์‹ ์ž๋ฃŒ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. + + + +- [`SwinForImageClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)์„ ํ†ตํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๊ด€๋ จ ์ž๋ฃŒ: [์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/image_classification) + +๋˜ํ•œ: + +- [`SwinForMaskedImageModeling`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining)๋ฅผ ํ†ตํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +์ƒˆ๋กœ์šด ์ž๋ฃŒ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, ์–ธ์ œ๋“ ์ง€ Pull Request๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”! ์ €ํฌ๊ฐ€ ๊ฒ€ํ† ํ•ด ๋“œ๋ฆด๊ฒŒ์š”. ์ด๋•Œ, ์ถ”๊ฐ€ํ•˜๋Š” ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ์ž๋ฃŒ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## SwinConfig [[transformers.SwinConfig]] + +[[autodoc]] SwinConfig + + + + +## SwinModel [[transformers.SwinModel]] + +[[autodoc]] SwinModel + - forward + +## SwinForMaskedImageModeling [[transformers.SwinForMaskedImageModeling]] + +[[autodoc]] SwinForMaskedImageModeling + - forward + +## SwinForImageClassification [[transformers.SwinForImageClassification]] + +[[autodoc]] transformers.SwinForImageClassification + - forward + + + + +## TFSwinModel [[transformers.TFSwinModel]] + +[[autodoc]] TFSwinModel + - call + +## TFSwinForMaskedImageModeling [[transformers.TFSwinForMaskedImageModeling]] + +[[autodoc]] TFSwinForMaskedImageModeling + - call + +## TFSwinForImageClassification [[transformers.TFSwinForImageClassification]] + +[[autodoc]] transformers.TFSwinForImageClassification + - call + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin2sr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin2sr.md new file mode 100644 index 0000000000000000000000000000000000000000..30149ad31ab94643864d28d3a15a8f0e6487e9c5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swin2sr.md @@ -0,0 +1,59 @@ + + +# Swin2SR [[swin2sr]] + +## ๊ฐœ์š” [[overview]] + +Swin2SR ๋ชจ๋ธ์€ Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://huggingface.co/papers/2209.11345)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +Swin2SR์€ [SwinIR](https://github.com/JingyunLiang/SwinIR/) ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•˜๊ณ ์ž [Swin Transformer v2](swinv2) ๋ ˆ์ด์–ด๋ฅผ ๋„์ž…ํ•จ์œผ๋กœ์จ, ํ›ˆ๋ จ ๋ถˆ์•ˆ์ •์„ฑ, ์‚ฌ์ „ ํ›ˆ๋ จ๊ณผ ๋ฏธ์„ธ ์กฐ์ • ๊ฐ„์˜ ํ•ด์ƒ๋„ ์ฐจ์ด, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ ์˜์กด์„ฑ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์••์ถ•์€ ์ŠคํŠธ๋ฆฌ๋ฐ ์„œ๋น„์Šค, ๊ฐ€์ƒ ํ˜„์‹ค, ๋น„๋””์˜ค ๊ฒŒ์ž„๊ณผ ๊ฐ™์€ ๋Œ€์—ญํญ์ด ์ œํ•œ๋œ ์‹œ์Šคํ…œ์„ ํ†ตํ•ด ์ด๋ฏธ์ง€์™€ ์˜์ƒ์„ ํšจ์œจ์ ์œผ๋กœ ์ „์†กํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์••์ถ•์€ ํ•„์—ฐ์ ์œผ๋กœ ์›๋ณธ ์ •๋ณด์˜ ์†์‹ค๊ณผ ์•„ํ‹ฐํŒฉํŠธ๋ฅผ ์ดˆ๋ž˜ํ•˜๋ฉฐ, ์ด๋Š” ์‹œ๊ฐ์  ํ’ˆ์งˆ์„ ์‹ฌ๊ฐํ•˜๊ฒŒ ์ €ํ•˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ, ์••์ถ•๋œ ์ด๋ฏธ์ง€์˜ ํ’ˆ์งˆ ํ–ฅ์ƒ์€ ํ™œ๋ฐœํ•œ ์—ฐ๊ตฌ ์ฃผ์ œ๊ฐ€ ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋Œ€๋ถ€๋ถ„์˜ ์ตœ์ฒจ๋‹จ ์ด๋ฏธ์ง€ ๋ณต์› ๋ฐฉ๋ฒ•์€ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์ง€๋งŒ, SwinIR๊ณผ ๊ฐ™์€ ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•๋“ค๋„ ์ด ์ž‘์—…์—์„œ ์ธ์ƒ์ ์ธ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋…ผ๋ฌธ์—์„œ๋Š” Swin Transformer V2๋ฅผ ์‚ฌ์šฉํ•ด SwinIR์„ ๊ฐœ์„ ํ•˜์—ฌ ์ด๋ฏธ์ง€ ์ดˆํ•ด์ƒ๋„ ์ž‘์—…, ํŠนํžˆ ์••์ถ•๋œ ์ž…๋ ฅ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ํŠธ๋žœ์Šคํฌ๋จธ ๋น„์ „ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ฃผ์š” ๋ฌธ์ œ๋“ค, ์˜ˆ๋ฅผ ๋“ค์–ด ํ›ˆ๋ จ ๋ถˆ์•ˆ์ •์„ฑ, ์‚ฌ์ „ ํ›ˆ๋ จ๊ณผ ๋ฏธ์„ธ ์กฐ์ • ๊ฐ„ ํ•ด์ƒ๋„ ์ฐจ์ด, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ ์˜์กด์„ฑ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” JPEG ์••์ถ• ์•„ํ‹ฐํŒฉํŠธ ์ œ๊ฑฐ, ์ด๋ฏธ์ง€ ์ดˆํ•ด์ƒ๋„(ํด๋ž˜์‹ ๋ฐ ๊ฒฝ๋Ÿ‰), ๊ทธ๋ฆฌ๊ณ  ์••์ถ•๋œ ์ด๋ฏธ์ง€ ์ดˆํ•ด์ƒ๋„๋ผ๋Š” ์„ธ ๊ฐ€์ง€ ๋Œ€ํ‘œ์ ์ธ ์ž‘์—…์—์„œ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹คํ—˜ ๊ฒฐ๊ณผ, ์šฐ๋ฆฌ์˜ ๋ฐฉ๋ฒ•์ธ Swin2SR์€ SwinIR์˜ ํ›ˆ๋ จ ์ˆ˜๋ ด์„ฑ๊ณผ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, "AIM 2022 Challenge on Super-Resolution of Compressed Image and Video"์—์„œ ์ƒ์œ„ 5์œ„ ์†”๋ฃจ์…˜์œผ๋กœ ์„ ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.* + + + + Swin2SR ์•„ํ‚คํ…์ฒ˜. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ฐœ์ทŒ. + +์ด ๋ชจ๋ธ์€ [nielsr](https://huggingface.co/nielsr)๊ฐ€ ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/mv-lab/swin2sr)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +Swin2SR demo notebook์€ [์—ฌ๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Swin2SR)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +SwinSR์„ ํ™œ์šฉํ•œ image super-resolution demo space๋Š” [์—ฌ๊ธฐ](https://huggingface.co/spaces/jjourney1125/swin2sr)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## Swin2SRImageProcessor [[transformers.Swin2SRImageProcessor]] + +[[autodoc]] Swin2SRImageProcessor + - preprocess + +## Swin2SRConfig [[transformers.Swin2SRConfig]] + +[[autodoc]] Swin2SRConfig + +## Swin2SRModel [[transformers.Swin2SRModel]] + +[[autodoc]] Swin2SRModel + - forward + +## Swin2SRForImageSuperResolution [[transformers.Swin2SRForImageSuperResolution]] + +[[autodoc]] Swin2SRForImageSuperResolution + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swinv2.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swinv2.md new file mode 100644 index 0000000000000000000000000000000000000000..40b9268cb21e0c761ea8c4badbf2e0b56ff103b5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/swinv2.md @@ -0,0 +1,63 @@ + + +# Swin Transformer V2 [[swin-transformer-v2]] + +## ๊ฐœ์š” [[overview]] + +Swin Transformer V2๋Š” Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [Swin Transformer V2: Scaling Up Capacity and Resolution](https://huggingface.co/papers/2111.09883)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋Œ€๊ทœ๋ชจ NLP ๋ชจ๋ธ๋“ค์€ ์–ธ์–ด ์ž‘์—…์—์„œ์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒํ•˜๋ฉฐ, ์„ฑ๋Šฅ์ด ํฌํ™”ํ•˜๋Š” ์ง•ํ›„๋ฅผ ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์‚ฌ๋žŒ๊ณผ ์œ ์‚ฌํ•œ few-shot ํ•™์Šต ๋Šฅ๋ ฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์€ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ ํƒ๊ตฌํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ˜• ๋น„์ „ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ณ  ์ ์šฉํ•˜๋Š” ๋ฐ ์žˆ์–ด ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค: ํ›ˆ๋ จ ๋ถˆ์•ˆ์ •์„ฑ, ์‚ฌ์ „ ํ•™์Šต๊ณผ ํŒŒ์ธํŠœ๋‹ ๊ฐ„์˜ ํ•ด์ƒ๋„ ์ฐจ์ด, ๊ทธ๋ฆฌ๊ณ  ๋ ˆ์ด๋ธ”์ด ๋‹ฌ๋ฆฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋†’์€ ์š”๊ตฌ์ž…๋‹ˆ๋‹ค. ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๊ธฐ๋ฒ•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค: 1) ํ›ˆ๋ จ ์•ˆ์ •์„ฑ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ residual-post-norm ๋ฐฉ๋ฒ•๊ณผ cosine attention์˜ ๊ฒฐํ•ฉ; 2) ์ €ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๊ณ ํ•ด์ƒ๋„ ์ž…๋ ฅ์œผ๋กœ ์ „์ดํ•  ์ˆ˜ ์žˆ๋Š” log-spaced continuous position bias ๋ฐฉ๋ฒ•; 3) ๋ ˆ์ด๋ธ”์ด ๋‹ฌ๋ฆฐ ๋ฐฉ๋Œ€ํ•œ ์ด๋ฏธ์ง€์˜ ํ•„์š”์„ฑ์„ ์ค„์ด๊ธฐ ์œ„ํ•œ self-supervised ์‚ฌ์ „ ํ•™์Šต ๋ฐฉ๋ฒ•์ธ SimMIM์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋ฒ•๋“ค์„ ํ†ตํ•ด 30์–ต ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ Swin Transformer V2 ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ํ›ˆ๋ จํ•˜์˜€์œผ๋ฉฐ, ์ด๋Š” ํ˜„์žฌ๊นŒ์ง€ ๊ฐ€์žฅ ํฌ๊ณ  ๊ณ ๋ฐ€๋„์˜ ๋น„์ „ ๋ชจ๋ธ๋กœ, ์ตœ๋Œ€ 1,536ร—1,536 ํ•ด์ƒ๋„์˜ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ImageNet-V2 ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜, COCO ๊ฐ์ฒด ํƒ์ง€, ADE20K ์˜๋ฏธ๋ก ์  ๋ถ„ํ• , Kinetics-400 ๋น„๋””์˜ค ํ–‰๋™ ๋ถ„๋ฅ˜ ๋“ฑ ๋„ค ๊ฐ€์ง€ ๋Œ€ํ‘œ์ ์ธ ๋น„์ „ ์ž‘์—…์—์„œ ์ƒˆ๋กœ์šด ์„ฑ๋Šฅ ๊ธฐ๋ก์„ ์„ธ์› ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์šฐ๋ฆฌ์˜ ํ›ˆ๋ จ์€ Google์˜ billion-level ๋น„์ „ ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•ด 40๋ฐฐ ์ ์€ ๋ ˆ์ด๋ธ”์ด ๋‹ฌ๋ฆฐ ๋ฐ์ดํ„ฐ์™€ 40๋ฐฐ ์ ์€ ํ›ˆ๋ จ ์‹œ๊ฐ„์œผ๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค๋Š” ์ ์—์„œ ํ›จ์”ฌ ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [nandwalritik](https://huggingface.co/nandwalritik)์ด ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/microsoft/Swin-Transformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋ฆฌ์†Œ์Šค [[resources]] + +Swin Transformer v2์˜ ์‚ฌ์šฉ์„ ๋„์šธ ์ˆ˜ ์žˆ๋Š” Hugging Face ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ)์˜ ๊ณต์‹ ์ž๋ฃŒ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. + + + + +- [`Swinv2ForImageClassification`]์€ ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-classification)์™€ [๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)์„ ํ†ตํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ๊ด€๋ จ ์ž๋ฃŒ: [์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ž‘์—… ๊ฐ€์ด๋“œ](../tasks/image_classification) + +๋˜ํ•œ: + +- [`Swinv2ForMaskedImageModeling`]๋Š” ์ด [์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining)๋ฅผ ํ†ตํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +์ƒˆ๋กœ์šด ์ž๋ฃŒ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, ์–ธ์ œ๋“ ์ง€ Pull Request๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”! ์ €ํฌ๊ฐ€ ๊ฒ€ํ† ํ•ด ๋“œ๋ฆด๊ฒŒ์š”. ์ด๋•Œ, ์ถ”๊ฐ€ํ•˜๋Š” ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ์™€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ์ž๋ฃŒ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## Swinv2Config [[transformers.Swinv2Config]] + +[[autodoc]] Swinv2Config + +## Swinv2Model [[transformers.Swinv2Model]] + +[[autodoc]] Swinv2Model + - forward + +## Swinv2ForMaskedImageModeling [[transformers.Swinv2ForMaskedImageModeling]] + +[[autodoc]] Swinv2ForMaskedImageModeling + - forward + +## Swinv2ForImageClassification [[transformers.Swinv2ForImageClassification]] + +[[autodoc]] transformers.Swinv2ForImageClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/time_series_transformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/time_series_transformer.md new file mode 100644 index 0000000000000000000000000000000000000000..2473b0e143b7500ab13d911a34ab406f6f3d0906 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/time_series_transformer.md @@ -0,0 +1,62 @@ + + +# ์‹œ๊ณ„์—ด ํŠธ๋žœ์Šคํฌ๋จธ[[time-series-transformer]] + +## ๊ฐœ์š”[[overview]] + +์ด ์‹œ๊ณ„์—ด ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์€ ์‹œ๊ณ„์—ด ์˜ˆ์ธก์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ์˜ ํŠธ๋žœ์Šคํฌ๋จธ ์ž…๋‹ˆ๋‹ค. +์ด ๋ชจ๋ธ์€ [kashif](https://huggingface.co/kashif)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +- ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ชจ๋ธ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, [`TimeSeriesTransformerModel`]์€ ์ƒ๋‹จ์— ํ—ค๋“œ๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ์ ์ธ ํŠธ๋žœ์Šคํฌ๋จธ ์ž…๋‹ˆ๋‹ค. [`TimeSeriesTransformerForPrediction`]์€ ์ƒ๋‹จ์— ๋ถ„ํฌ ํ—ค๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‹œ๊ณ„์—ด ์˜ˆ์ธก์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ด๋ฅธ๋ฐ” ํ™•๋ฅ ์  ์˜ˆ์ธก ๋ชจ๋ธ์ด๋ฉฐ, ํฌ์ธํŠธ ์˜ˆ์ธก ๋ชจ๋ธ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ฆ‰ ์ƒ˜ํ”Œ๋งํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„ํฌ๋ฅผ ํ•™์Šตํ•˜๋ฉฐ, ๊ฐ’์„ ์ง์ ‘ ์ถœ๋ ฅ ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. +- [`TimeSeriesTransformerForPrediction`]์€ ๋‘๊ฐœ์˜ ๋ธ”๋ก์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋Š” `context_length`์˜ ์‹œ๊ณ„์—ด ๊ฐ’์„ ์ž…๋ ฅ(`past_values`๋ผ๊ณ  ๋ถ€๋ฆ„)์œผ๋กœ ๋ฐ›์•„๋“ค์ด๋ฉฐ, ๋””์ฝ”๋”๋Š” ๋ฏธ๋ž˜์˜ `prediction_length`๋งŒํผ ์‹œ๊ณ„์—ด ๊ฐ’์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค(`future_values`๋ผ๊ณ  ๋ถ€๋ฆ„). ํ•™์Šต์ค‘์—๋Š” ๋ชจ๋ธ์— `past_values` ์™€ `future_values`์Œ์„ ๋ชจ๋ธ์— ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ๊ฐ€๊ณตํ•˜์ง€ ์•Š์€ `past_values` ์™€ `future_values` ์Œ ์™ธ์—๋„, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ์— ์ถ”๊ฐ€์ ์ธ ํŠน์ง•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๊ทธ ํŠน์ง•๋“ค์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค: + - `past_time_features`: ๋ชจ๋ธ์ด `past_values`์— ์ถ”๊ฐ€ํ•  ์‹œ๊ฐ„์  ํŠน์„ฑ. ์ด๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”์˜ "์œ„์น˜ ์ธ์ฝ”๋”ฉ" ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. + ์˜ˆ๋ฅผ ๋“ค์–ด "์›”์˜ ์ผ", "์—ฐ๋„์˜ ์›”" ๋“ฑ์„ ์Šค์นผ๋ผ ๊ฐ’์œผ๋กœ (๊ทธ๋ฆฌ๊ณ  ๋ฒกํ„ฐ๋กœ ์Œ“์•„์„œ) ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + ์˜ˆ์‹œ: ํŠน์ • ์‹œ๊ณ„์—ด ๊ฐ’์ด 8์›” 11์ผ์— ๊ธฐ๋ก๋˜์—ˆ๋‹ค๋ฉด, [11, 8]์„ ์‹œ๊ฐ„ ํŠน์„ฑ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (11์€ "์›”์˜ ์ผ", 8์€ "์—ฐ๋„์˜ ์›”"). + - `future_time_features`: ๋ชจ๋ธ์ด `future_values`์— ์ถ”๊ฐ€ํ•  ์‹œ๊ฐ„์  ํŠน์„ฑ. ์ด๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋””์ฝ”๋”์˜ "์œ„์น˜ ์ธ์ฝ”๋”ฉ" ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. + ์˜ˆ๋ฅผ ๋“ค์–ด "์›”์˜ ์ผ", "์—ฐ๋„์˜ ์›”" ๋“ฑ์„ ์Šค์นผ๋ผ ๊ฐ’์œผ๋กœ (๊ทธ๋ฆฌ๊ณ  ๋ฒกํ„ฐ๋กœ ์Œ“์•„์„œ) ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + ์˜ˆ: ํŠน์ • ์‹œ๊ณ„์—ด ๊ฐ’์ด 8์›” 11์ผ์— ์–ป์–ด์กŒ๋‹ค๋ฉด, [11, 8]์„ ์‹œ๊ฐ„ ํŠน์„ฑ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (11์€ "์›”์˜ ์ผ", 8์€ "์—ฐ๋„์˜ ์›”"). + - `static_categorical_features`: ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฒ”์ฃผํ˜• ํŠน์„ฑ (์ฆ‰, ๋ชจ๋“  `past_values`์™€ `future_values`์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง). + ์˜ˆ๋ฅผ ๋“ค์–ด ํŠน์ • ์‹œ๊ณ„์—ด์„ ์‹๋ณ„ํ•˜๋Š” ๋งค์žฅ ID๋‚˜ ์ง€์—ญ ID๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + ์ด๋Ÿฌํ•œ ํŠน์„ฑ์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ(๋ฏธ๋ž˜์˜ ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ ํฌํ•จ)์— ๋Œ€ํ•ด ์•Œ๋ ค์ ธ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + - `static_real_features`: ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์‹ค์ˆ˜๊ฐ’ ํŠน์„ฑ (์ฆ‰, ๋ชจ๋“  `past_values`์™€ `future_values`์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง). + ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œ๊ณ„์—ด ๊ฐ’์„ ๊ฐ€์ง„ ์ œํ’ˆ์˜ ์ด๋ฏธ์ง€ ํ‘œํ˜„ (์‹œ๊ณ„์—ด์ด ์‹ ๋ฐœ ํŒ๋งค์— ๊ด€ํ•œ ๊ฒƒ์ด๋ผ๋ฉด "์‹ ๋ฐœ" ์‚ฌ์ง„์˜ [ResNet](resnet) ์ž„๋ฒ ๋”ฉ ์ฒ˜๋Ÿผ)์ด ์žˆ์Šต๋‹ˆ๋‹ค. + ์ด๋Ÿฌํ•œ ํŠน์„ฑ์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ(๋ฏธ๋ž˜์˜ ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ ํฌํ•จ)์— ๋Œ€ํ•ด ์•Œ๋ ค์ ธ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +- ์ด ๋ชจ๋ธ์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์„ ์œ„ํ•œ ํŠธ๋žœ์Šคํฌ๋จธ ํ›ˆ๋ จ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ "๊ต์‚ฌ ๊ฐ•์ œ(teacher-forcing)" ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ›ˆ๋ จ ์ค‘์— `future_values`๋ฅผ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์œ„์น˜ ์ด๋™์‹œํ‚ค๊ณ , `past_values`์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์•ž์— ๋ถ™์ž…๋‹ˆ๋‹ค. ๊ฐ ์‹œ๊ฐ„ ๋‹จ๊ณ„์—์„œ ๋ชจ๋ธ์€ ๋‹ค์Œ ํƒ€๊ฒŸ์„ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ›ˆ๋ จ ์„ค์ •์€ ์–ธ์–ด๋ฅผ ์œ„ํ•œ GPT ๋ชจ๋ธ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, `decoder_start_token_id` ๊ฐœ๋…์ด ์—†์Šต๋‹ˆ๋‹ค (์šฐ๋ฆฌ๋Š” ๋‹จ์ˆœํžˆ ์ปจํ…์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋””์ฝ”๋”์˜ ์ดˆ๊ธฐ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค). +- ์ถ”๋ก  ์‹œ์—๋Š” `past_values`์˜ ์ตœ์ข… ๊ฐ’์„ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋‹ค์Œ, ๋ชจ๋ธ์—์„œ ์ƒ˜ํ”Œ๋งํ•˜์—ฌ ๋‹ค์Œ ์‹œ๊ฐ„ ๋‹จ๊ณ„์—์„œ์˜ ์˜ˆ์ธก์„ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ ๋””์ฝ”๋”์— ๊ณต๊ธ‰ํ•˜์—ฌ ๋‹ค์Œ ์˜ˆ์ธก์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค (์ž๊ธฐํšŒ๊ท€ ์ƒ์„ฑ์ด๋ผ๊ณ ๋„ ํ•จ). + +## ์ž๋ฃŒ [[resources]] + +์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” Hugging Face์™€ community ์ž๋ฃŒ ๋ชฉ๋ก(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋จ) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด PR(Pull Request)๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. ๋ฆฌ๋ทฐ ํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž๋ฃŒ๋Š” ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +- HuggingFace ๋ธ”๋กœ๊ทธ์—์„œ ์‹œ๊ณ„์—ด ํŠธ๋žœ์Šคํฌ๋จธ ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: [๐Ÿค— ํŠธ๋žœ์Šคํฌ๋จธ์™€ ํ™•๋ฅ ์  ์‹œ๊ณ„์—ด ์˜ˆ์ธก](https://huggingface.co/blog/time-series-transformers) + +## TimeSeriesTransformerConfig[[transformers.TimeSeriesTransformerConfig]] + +[[autodoc]] TimeSeriesTransformerConfig + +## TimeSeriesTransformerModel[[transformers.TimeSeriesTransformerModel]] + +[[autodoc]] TimeSeriesTransformerModel + - forward + +## TimeSeriesTransformerForPrediction[[transformers.TimeSeriesTransformerForPrediction]] + +[[autodoc]] TimeSeriesTransformerForPrediction + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/timesformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/timesformer.md new file mode 100644 index 0000000000000000000000000000000000000000..fb2c2ed007727b2aa88e848d2a4017d1c4b14c60 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/timesformer.md @@ -0,0 +1,51 @@ + + +# TimeSformer [[timesformer]] + +## ๊ฐœ์š” [[overview]] + +TimeSformer ๋ชจ๋ธ์€ Facebook Research์—์„œ ์ œ์•ˆํ•œ [TimeSformer: Is Space-Time Attention All You Need for Video Understanding?](https://huggingface.co/papers/2102.05095)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์—ฐ๊ตฌ๋Š” ์ฒซ ๋ฒˆ์งธ ๋น„๋””์˜ค Transformer๋กœ์„œ, ํ–‰๋™ ์ธ์‹ ๋ถ„์•ผ์—์„œ ์ค‘์š”ํ•œ ์ด์ •ํ‘œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Transformer ๊ธฐ๋ฐ˜์˜ ๋น„๋””์˜ค ์ดํ•ด ๋ฐ ๋ถ„๋ฅ˜ ๋…ผ๋ฌธ์— ๋งŽ์€ ์˜๊ฐ์„ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + +*์šฐ๋ฆฌ๋Š” ๊ณต๊ฐ„๊ณผ ์‹œ๊ฐ„์— ๊ฑธ์ณ ์…€ํ”„ ์–ดํ…์…˜๋งŒ์„ ์‚ฌ์šฉํ•˜๋Š” ํ•ฉ์„ฑ๊ณฑ์ด ์—†๋Š”(convolution-free) ๋น„๋””์˜ค ๋ถ„๋ฅ˜ ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ โ€œTimeSformerโ€๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ, ํ‘œ์ค€ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋น„๋””์˜ค์— ์ ์šฉํ•˜์—ฌ ํ”„๋ ˆ์ž„ ์ˆ˜์ค€ ํŒจ์น˜ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์‹œ๊ณต๊ฐ„์  ํŠน์ง•์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ์‹คํ—˜์  ์—ฐ๊ตฌ๋Š” ๋‹ค์–‘ํ•œ ์…€ํ”„ ์–ดํ…์…˜ ๋ฐฉ์‹์„ ๋น„๊ตํ•˜๋ฉฐ, ์‹œ๊ฐ„์  ์–ดํ…์…˜๊ณผ ๊ณต๊ฐ„์  ์–ดํ…์…˜์„ ๊ฐ๊ฐ์˜ ๋ธ”๋ก ๋‚ด์—์„œ ๋ณ„๋„๋กœ ์ ์šฉํ•˜๋Š” โ€œ๋ถ„ํ•  ์–ดํ…์…˜โ€ ๋ฐฉ์‹์ด ๊ณ ๋ ค๋œ ์„ค๊ณ„ ์„ ํƒ ์ค‘ ๊ฐ€์žฅ ์šฐ์ˆ˜ํ•œ ๋น„๋””์˜ค ๋ถ„๋ฅ˜ ์ •ํ™•๋„๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์‹œ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜์‹ ์ ์ธ ์„ค๊ณ„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , TimeSformer๋Š” Kinetics-400 ๋ฐ Kinetics-600์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ํ–‰๋™ ์ธ์‹ ๋ฒค์น˜๋งˆํฌ์—์„œ ์ตœ์ฒจ๋‹จ ๊ฒฐ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ–ˆ์œผ๋ฉฐ, ํ˜„์žฌ๊นŒ์ง€ ๋ณด๊ณ ๋œ ๊ฐ€์žฅ ๋†’์€ ์ •ํ™•๋„๋ฅผ ๊ธฐ๋กํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, 3D ํ•ฉ์„ฑ๊ณฑ ๋„คํŠธ์›Œํฌ์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, TimeSformer๋Š” ๋” ๋น ๋ฅด๊ฒŒ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•ฝ๊ฐ„์˜ ์ •ํ™•๋„ ์ €ํ•˜๋ฅผ ๊ฐ์ˆ˜ํ•˜๋ฉด ํ…Œ์ŠคํŠธ ํšจ์œจ์„ฑ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋˜๊ณ , 1๋ถ„ ์ด์ƒ์˜ ๊ธด ๋น„๋””์˜ค ํด๋ฆฝ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์™€ ๋ชจ๋ธ์€ ๋‹ค์Œ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: [https URL ๋งํฌ](https://github.com/facebookresearch/TimeSformer).* + +์ด ๋ชจ๋ธ์€ [fcakyon](https://huggingface.co/fcakyon)์ด ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/facebookresearch/TimeSformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๋ณ€ํ˜•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ์…‹์— ๋งž์ถฐ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ชจ๋ธ ํฌ๊ธฐ์— ๋”ฐ๋ผ ํด๋ฆฝ๋‹น ์ž…๋ ฅ ํ”„๋ ˆ์ž„ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ, ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์„ ํƒํ•  ๋•Œ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + +## ๋ฆฌ์†Œ์Šค [[resources]] + +- [Video classification task guide](../tasks/video_classification) + +## TimesformerConfig [[transformers.TimesformerConfig]] + +[[autodoc]] TimesformerConfig + +## TimesformerModel [[transformers.TimesformerModel]] + +[[autodoc]] TimesformerModel + - forward + +## TimesformerForVideoClassification [[transformers.TimesformerForVideoClassification]] + +[[autodoc]] TimesformerForVideoClassification + - forward \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/trajectory_transformer.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/trajectory_transformer.md new file mode 100644 index 0000000000000000000000000000000000000000..9f72a6f71e6ddd98c108af235f94ef64829bed64 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/trajectory_transformer.md @@ -0,0 +1,49 @@ + + +# ๊ถค์  ํŠธ๋žœ์Šคํฌ๋จธ[[trajectory-transformer]] + + + + +์ด ๋ชจ๋ธ์€ ์œ ์ง€ ๋ณด์ˆ˜ ๋ชจ๋“œ๋กœ๋งŒ ์šด์˜๋˜๋ฉฐ, ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ƒˆ๋กœ์šด PR(Pull Request)์€ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +์ด ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์ด ๋ชจ๋ธ์„ ์ง€์›ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋ฒ„์ „์ธ v4.30.0๋ฅผ ๋‹ค์‹œ ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์žฌ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: `pip install -U transformers==4.30.0`. + + + +## ๊ฐœ์š”[[overview]] + +Trajectory Transformer ๋ชจ๋ธ์€ Michael Janner, Qiyang Li, Sergey Levine์ด ์ œ์•ˆํ•œ [ํ•˜๋‚˜์˜ ์ปค๋‹ค๋ž€ ์‹œํ€€์Šค ๋ชจ๋ธ๋ง ๋ฌธ์ œ๋กœ์„œ์˜ ์˜คํ”„๋ผ์ธ ๊ฐ•ํ™”ํ•™์Šต](https://huggingface.co/papers/2106.02039)๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +ํ•ด๋‹น ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ž…๋‹ˆ๋‹ค: + +*๊ฐ•ํ™”ํ•™์Šต(RL)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งˆ๋ฅด์ฝ”ํ”„ ์†์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ฌธ์ œ๋ฅผ ์ธ์ˆ˜๋ถ„ํ•ดํ•˜๋ฉด์„œ ์ •์  ์ •์ฑ…์ด๋‚˜ ๋‹จ์ผ ๋‹จ๊ณ„ ๋ชจ๋ธ์„ ์ถ”์ •ํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” RL์„ ๋†’์€ ๋ณด์ƒ ์‹œํ€€์Šค๋กœ ์ด์–ด์ง€๋Š” ํ–‰๋™ ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์‹œํ€€์Šค ๋ชจ๋ธ๋ง ๋ฌธ์ œ๋กœ ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€์ ์—์„œ, ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์—์„œ ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ณ ์šฉ๋Ÿ‰ ์‹œํ€€์Šค ์˜ˆ์ธก ๋ชจ๋ธ์ด RL ๋ฌธ์ œ์—๋„ ํšจ๊ณผ์ ์ธ ํ•ด๊ฒฐ์ฑ…์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ คํ•ด ๋ณผ ๋งŒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” RL์„ ์‹œํ€€์Šค ๋ชจ๋ธ๋ง์˜ ๋„๊ตฌ๋กœ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š”์ง€ ํƒ๊ตฌํ•˜๋ฉฐ, ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ถค์ ์— ๋Œ€ํ•œ ๋ถ„ํฌ๋ฅผ ๋ชจ๋ธ๋งํ•˜๊ณ  ๋น” ์„œ์น˜๋ฅผ ๊ณ„ํš ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์žฌํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. RL์„ ์‹œํ€€์Šค ๋ชจ๋ธ๋ง ๋ฌธ์ œ๋กœ ํ”„๋ ˆ์ž„ํ™”ํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์„ค๊ณ„ ๊ฒฐ์ •์ด ๋‹จ์ˆœํ™”๋˜์–ด, ์˜คํ”„๋ผ์ธ RL ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ํ”ํžˆ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ์ ‘๊ทผ ๋ฐฉ์‹์˜ ์œ ์—ฐ์„ฑ์„ ์žฅ๊ธฐ ๋™์—ญํ•™ ์˜ˆ์ธก, ๋ชจ๋ฐฉ ํ•™์Šต, ๋ชฉํ‘œ ์กฐ๊ฑด๋ถ€ RL, ์˜คํ”„๋ผ์ธ RL์— ๊ฑธ์ณ ์ž…์ฆํ•ฉ๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€, ์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ๊ธฐ์กด์˜ ๋ชจ๋ธ ํ”„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ํฌ์†Œ ๋ณด์ƒ, ์žฅ๊ธฐ ๊ณผ์ œ์—์„œ ์ตœ์‹  ๊ณ„ํš๊ธฐ(planner)๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [CarlCochet](https://huggingface.co/CarlCochet)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์ด๊ณณ](https://github.com/jannerm/trajectory-transformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ[[usage-tips]] + +์ด ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์‹ฌ์ธต ๊ฐ•ํ™”ํ•™์Šต์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด์ „์˜ ๋ชจ๋“  ํƒ€์ž„์Šคํ…์—์„œ์˜ ํ–‰๋™, ์ƒํƒœ, ๋ณด์ƒ์œผ๋กœ๋ถ€ํ„ฐ ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ด ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•จ๊ป˜ ํ•˜๋‚˜์˜ ํฐ ์‹œํ€€์Šค(๊ถค์ )๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. + +## TrajectoryTransformerConfig[[transformers.TrajectoryTransformerConfig]] + +[[autodoc]] TrajectoryTransformerConfig + +## TrajectoryTransformerModel[[transformers.TrajectoryTransformerModel]] + +[[autodoc]] TrajectoryTransformerModel + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/tvp.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/tvp.md new file mode 100644 index 0000000000000000000000000000000000000000..dba7e2182555ed1ed9f6e8b0f2967b22ed7fc793 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/tvp.md @@ -0,0 +1,189 @@ + + +# TVP [[tvp]] + +
+PyTorch +
+ +## ๊ฐœ์š” [[overview]] + +Text-Visual Prompting(TVP) ํ”„๋ ˆ์ž„์›Œํฌ๋Š” Yimeng Zhang, Xin Chen, Jinghan Jia, Sijia Liu, Ke Ding์ด ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ [Text-Visual Prompting for Efficient 2D Temporal Video Grounding](https://huggingface.co/papers/2303.04995)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ๊ธธ๊ณ , ํŽธ์ง‘๋˜์ง€ ์•Š์€ ๋น„๋””์˜ค์—์„œ ๋ฌธ์žฅ์œผ๋กœ ์„ค๋ช…๋œ ์ˆœ๊ฐ„์˜ ์‹œ์ž‘/์ข…๋ฃŒ ์‹œ์ ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” Temporal Video Grounding(TVG) ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์„ธ๋ฐ€ํ•œ 3D ์‹œ๊ฐ์  ํŠน์ง• ๋•๋ถ„์— TVG ๊ธฐ์ˆ ์€ ์ตœ๊ทผ ๋ช‡ ๋…„ ๋™์•ˆ ๋†€๋ผ์šด ๋ฐœ์ „์„ ์ด๋ค˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ 3D ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(CNN)์˜ ๋†’์€ ๋ณต์žก์„ฑ์œผ๋กœ ์ธํ•ด ๋ฐ€๋„ ๋†’์€ 3D ์‹œ๊ฐ์  ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ๊ทธ๋งŒํผ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ์™€ ์—ฐ์‚ฐ ์ž์›์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํšจ์œจ์ ์ธ TVG๋ฅผ ์œ„ํ•ด, ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” TVG ๋ชจ๋ธ์˜ ์‹œ๊ฐ์  ์ž…๋ ฅ๊ณผ ํ…์ŠคํŠธ ํŠน์ง• ๋ชจ๋‘์— ์ตœ์ ํ™”๋œ ๊ต๋ž€ ํŒจํ„ด('ํ”„๋กฌํ”„ํŠธ'๋ผ๊ณ  ๋ถ€๋ฆ„)์„ ํ†ตํ•ฉํ•˜๋Š” ์ƒˆ๋กœ์šด Text-Visual Prompting(TVP) ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. 3D CNN๊ณผ ๋šœ๋ ท์ด ๋Œ€๋น„๋˜๊ฒŒ TVP๊ฐ€ 2D TVG ๋ชจ๋ธ์—์„œ ๋น„์ „ ์ธ์ฝ”๋”์™€ ์–ธ์–ด ์ธ์ฝ”๋”๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๊ณต๋™ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ณ , ๋‚ฎ์€ ๋ณต์žก๋„์˜ ํฌ์†Œํ•œ 2D ์‹œ๊ฐ์  ํŠน์ง•๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ํฌ๋กœ์Šค ๋ชจ๋‹ฌ ํŠน์ง• ์œตํ•ฉ์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€, TVG์˜ ํšจ์œจ์ ์ธ ํ•™์Šต์„ ์œ„ํ•ด Temporal-Distance IoU(TDIoU) ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ๋ฒค์น˜๋งˆํฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ์ธ Charades-STA์™€ ActivityNet Captions ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์‹คํ—˜์„ ํ†ตํ•ด, ์ œ์•ˆ๋œ TVP๊ฐ€ 2D TVG์˜ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ค๊ณ (์˜ˆ: Charades-STA์—์„œ 9.79% ํ–ฅ์ƒ, ActivityNet Captions์—์„œ 30.77% ํ–ฅ์ƒ) 3D ์‹œ๊ฐ์  ํŠน์ง•์„ ์‚ฌ์šฉํ•˜๋Š” TVG์— ๋น„ํ•ด 5๋ฐฐ์˜ ์ถ”๋ก  ๊ฐ€์†์„ ๋‹ฌ์„ฑํ•จ์„ ์‹คํ—˜์ ์œผ๋กœ ์ž…์ฆํ•ฉ๋‹ˆ๋‹ค.* + +์ด ์—ฐ๊ตฌ๋Š” Temporal Video Grounding(TVG)์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. TVG๋Š” ๋ฌธ์žฅ์œผ๋กœ ์„ค๋ช…๋œ ํŠน์ • ์ด๋ฒคํŠธ์˜ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ์‹œ์ ์„ ๊ธด ๋น„๋””์˜ค์—์„œ ์ •ํ™•ํžˆ ์ฐพ์•„๋‚ด๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. TVG ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด Text-Visual Prompting(TVP)์ด ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. TVP๋Š” 'ํ”„๋กฌํ”„ํŠธ'๋ผ๊ณ  ์•Œ๋ ค์ง„ ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋œ ํŒจํ„ด์„ TVG ๋ชจ๋ธ์˜ ์‹œ๊ฐ์ (์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜) ๋ฐ ํ…์ŠคํŠธ(๋‹จ์–ด ๊ธฐ๋ฐ˜) ์ž…๋ ฅ ๊ตฌ์„ฑ ์š”์†Œ ๋ชจ๋‘์— ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ด ํ”„๋กฌํ”„ํŠธ๋Š” ์ถ”๊ฐ€์ ์ธ ์‹œ๊ณต๊ฐ„์  ์ปจํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ๋ชจ๋ธ์ด ๋น„๋””์˜ค ๋‚ด ์ด๋ฒคํŠธ ์‹œ์ ์˜ ์˜ˆ์ธก ์ •ํ™•๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ 3D ์‹œ๊ฐ์  ์ž…๋ ฅ ๋Œ€์‹  2D ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 3D ์ž…๋ ฅ์€ ๋ณด๋‹ค ํ’๋ถ€ํ•œ ์‹œ๊ณต๊ฐ„์  ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜์ง€๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋” ๋งŽ์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กฌํ”„ํŒ… ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ 2D ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์™€ ์œ ์‚ฌํ•œ ์ˆ˜์ค€์˜ ์ปจํ…์ŠคํŠธ์™€ ์ •ํ™•๋„๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. + + + + TVP ์•„ํ‚คํ…์ฒ˜. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ฐœ์ทŒ. + +์ด ๋ชจ๋ธ์€ [Jiqing Feng](https://huggingface.co/Jiqing)๋‹˜์ด ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ๋Š” [์ด ๊ณณ](https://github.com/intel/TVP)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์‚ฌ์šฉ ํŒ ๋ฐ ์˜ˆ์‹œ [[usage-tips-and-examples]] + +ํ”„๋กฌํ”„ํŠธ๋Š” ์ตœ์ ํ™”๋œ ๊ต๋ž€ ํŒจํ„ด์œผ๋กœ ์ž…๋ ฅ ๋น„๋””์˜ค ํ”„๋ ˆ์ž„์ด๋‚˜ ํ…์ŠคํŠธ ํŠน์ง•์— ์ถ”๊ฐ€๋˜๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค. ๋ฒ”์šฉ ์„ธํŠธ๋ž€ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋™์ผํ•œ ํ”„๋กฌํ”„ํŠธ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ž…๋ ฅ ๋‚ด์šฉ๊ณผ ๊ด€๊ณ„์—†์ด ๋ชจ๋“  ๋น„๋””์˜ค ํ”„๋ ˆ์ž„๊ณผ ํ…์ŠคํŠธ ํŠน์ง•์— ์ด ํ”„๋กฌํ”„ํŠธ๋“ค์„ ์ผ๊ด€์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +TVP๋Š” ์‹œ๊ฐ ์ธ์ฝ”๋”์™€ ํฌ๋กœ์Šค ๋ชจ๋‹ฌ ์ธ์ฝ”๋”๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฒ”์šฉ ์‹œ๊ฐ ํ”„๋กฌํ”„ํŠธ์™€ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ์„ธํŠธ๊ฐ€ ๊ฐ๊ฐ ์ƒ˜ํ”Œ๋ง๋œ ๋น„๋””์˜ค ํ”„๋ ˆ์ž„๊ณผ ํ…์ŠคํŠธ ํŠน์ง•์— ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค. ํŠนํžˆ, ์„œ๋กœ ๋‹ค๋ฅธ ์‹œ๊ฐ ํ”„๋กฌํ”„ํŠธ ์„ธํŠธ๊ฐ€ ํŽธ์ง‘๋˜์ง€ ์•Š์€ ํ•œ ๋น„๋””์˜ค์—์„œ ๊ท ์ผํ•˜๊ฒŒ ์ƒ˜ํ”Œ๋ง๋œ ํ”„๋ ˆ์ž„์— ์ˆœ์„œ๋Œ€๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์˜ ๋ชฉํ‘œ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‹œ๊ฐ์  ์ž…๋ ฅ๊ณผ ํ…์ŠคํŠธ ํŠน์ง• ๋ชจ๋‘์— ํ†ตํ•ฉํ•˜์—ฌ Temporal Video Grounding(TVG) ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์›์น™์ ์œผ๋กœ, ์ œ์•ˆ๋œ ์•„ํ‚คํ…์ฒ˜์—๋Š” ์–ด๋–ค ์‹œ๊ฐ ์ธ์ฝ”๋”๋‚˜ ํฌ๋กœ์Šค ๋ชจ๋‹ฌ ์ธ์ฝ”๋”๋ผ๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[TvpProcessor]๋Š” [BertTokenizer]์™€ [TvpImageProcessor]๋ฅผ ๋‹จ์ผ ์ธ์Šคํ„ด์Šค๋กœ ๋ž˜ํ•‘ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ๊ฐ๊ฐ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ ์˜ˆ์‹œ๋Š” [TvpProcessor]์™€ [TvpForVideoGrounding]์„ ์‚ฌ์šฉํ•˜์—ฌ TVG๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +```python +import av +import cv2 +import numpy as np +import torch +from huggingface_hub import hf_hub_download +from transformers import AutoProcessor, TvpForVideoGrounding + + +def pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps): + ''' + ์›๋ณธ fps์˜ ๋น„๋””์˜ค๋ฅผ ์ง€์ •ํ•œ fps(target_fps)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  PyAV ๋””์ฝ”๋”๋กœ ๋น„๋””์˜ค๋ฅผ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. + Args: + container (container): pyav ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. + sampling_rate (int): ํ”„๋ ˆ์ž„ ์ƒ˜ํ”Œ๋ง ์†๋„์ž…๋‹ˆ๋‹ค.(์ƒ˜ํ”Œ๋ง๋œ ๋‘๊ฐœ์˜ ํ”„๋ ˆ์ž„ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค) + num_frames (int): ์ƒ˜ํ”Œ๋งํ•  ํ”„๋ ˆ์ž„ ์ˆ˜์ž…๋‹ˆ๋‹ค. + clip_idx (int): clip_idx๊ฐ€ -1์ด๋ฉด ์‹œ๊ฐ„ ์ถ•์—์„œ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + clip_idx๊ฐ€ -1๋ณด๋‹ค ํฌ๋ฉด ๋น„๋””์˜ค๋ฅผ num_clips ๊ฐœ๋กœ ๊ท ๋“ฑ ๋ถ„ํ• ํ•œ ํ›„ + clip_idx๋ฒˆ์งธ ๋น„๋””์˜ค ํด๋ฆฝ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. + num_clips (int): ์ฃผ์–ด์ง„ ๋น„๋””์˜ค์—์„œ ๊ท ์ผํ•˜๊ฒŒ ์ƒ˜ํ”Œ๋งํ•  ์ „์ฒด ํด๋ฆฝ ์ˆ˜์ž…๋‹ˆ๋‹ค. + target_fps (int): ์ž…๋ ฅ ๋น„๋””์˜ค์˜ fps๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ƒ˜ํ”Œ๋ง ์ „์— + ์ง€์ •ํ•œ fps๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค + Returns: + frames (tensor): ๋น„๋””์˜ค์—์„œ ๋””์ฝ”๋”ฉ๋œ ํ”„๋ ˆ์ž„์ž…๋‹ˆ๋‹ค. ๋น„๋””์˜ค ์ŠคํŠธ๋ฆผ์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ + None์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + fps (float): ๋น„๋””์˜ค์˜ ์ดˆ๋‹น ํ”„๋ ˆ์ž„ ์ˆ˜์ž…๋‹ˆ๋‹ค. + ''' + video = container.streams.video[0] + fps = float(video.average_rate) + clip_size = sampling_rate * num_frames / target_fps * fps + delta = max(num_frames - clip_size, 0) + start_idx = delta * clip_idx / num_clips + end_idx = start_idx + clip_size - 1 + timebase = video.duration / num_frames + video_start_pts = int(start_idx * timebase) + video_end_pts = int(end_idx * timebase) + seek_offset = max(video_start_pts - 1024, 0) + container.seek(seek_offset, any_frame=False, backward=True, stream=video) + frames = {} + for frame in container.decode(video=0): + if frame.pts < video_start_pts: + continue + frames[frame.pts] = frame + if frame.pts > video_end_pts: + break + frames = [frames[pts] for pts in sorted(frames)] + return frames, fps + + +def decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps): + ''' + ๋น„๋””์˜ค๋ฅผ ๋””์ฝ”๋”ฉํ•˜๊ณ  ์‹œ๊ฐ„ ์ถ• ์ƒ˜ํ”Œ๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + Args: + container (container): pyav ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. + sampling_rate (int): ํ”„๋ ˆ์ž„ ์ƒ˜ํ”Œ๋ง ์†๋„์ž…๋‹ˆ๋‹ค.(์ƒ˜ํ”Œ๋ง๋œ ๋‘๊ฐœ์˜ ํ”„๋ ˆ์ž„ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค) + num_frames (int): ์ƒ˜ํ”Œ๋งํ•  ํ”„๋ ˆ์ž„ ์ˆ˜์ž…๋‹ˆ๋‹ค. + clip_idx (int): clip_idx๊ฐ€ -1์ด๋ฉด ์‹œ๊ฐ„ ์ถ•์—์„œ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + clip_idx๊ฐ€ -1๋ณด๋‹ค ํฌ๋ฉด ๋น„๋””์˜ค๋ฅผ num_clips ๊ฐœ๋กœ ๊ท ๋“ฑ ๋ถ„ํ• ํ•œ ํ›„ + clip_idx๋ฒˆ์งธ ๋น„๋””์˜ค ํด๋ฆฝ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. + num_clips (int): ์ฃผ์–ด์ง„ ๋น„๋””์˜ค์—์„œ ๊ท ์ผํ•˜๊ฒŒ ์ƒ˜ํ”Œ๋งํ•  ์ „์ฒด ํด๋ฆฝ ์ˆ˜์ž…๋‹ˆ๋‹ค. + target_fps (int): ์ž…๋ ฅ ๋น„๋””์˜ค์˜ fps๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ƒ˜ํ”Œ๋ง ์ „์— + ์ง€์ •ํ•œ fps๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค + Returns: + frames (tensor): ๋น„๋””์˜ค์—์„œ ๋””์ฝ”๋”ฉ๋œ ํ”„๋ ˆ์ž„์ž…๋‹ˆ๋‹ค. + ''' + assert clip_idx >= -2, "Not a valid clip_idx {}".format(clip_idx) + frames, fps = pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps) + clip_size = sampling_rate * num_frames / target_fps * fps + index = np.linspace(0, clip_size - 1, num_frames) + index = np.clip(index, 0, len(frames) - 1).astype(np.int64) + frames = np.array([frames[idx].to_rgb().to_ndarray() for idx in index]) + frames = frames.transpose(0, 3, 1, 2) + return frames + + +file = hf_hub_download(repo_id="Intel/tvp_demo", filename="AK2KG.mp4", repo_type="dataset") +model = TvpForVideoGrounding.from_pretrained("Intel/tvp-base") + +decoder_kwargs = dict( + container=av.open(file, metadata_errors="ignore"), + sampling_rate=1, + num_frames=model.config.num_frames, + clip_idx=0, + num_clips=1, + target_fps=3, +) +raw_sampled_frms = decode(**decoder_kwargs) + +text = "a person is sitting on a bed." +processor = AutoProcessor.from_pretrained("Intel/tvp-base") +model_inputs = processor( + text=[text], videos=list(raw_sampled_frms), return_tensors="pt", max_text_length=100#, size=size +) + +model_inputs["pixel_values"] = model_inputs["pixel_values"].to(model.dtype) +output = model(**model_inputs) + +def get_video_duration(filename): + cap = cv2.VideoCapture(filename) + if cap.isOpened(): + rate = cap.get(5) + frame_num = cap.get(7) + duration = frame_num/rate + return duration + return -1 + +duration = get_video_duration(file) +start, end = processor.post_process_video_grounding(output.logits, duration) + +print(f"The time slot of the video corresponding to the text \"{text}\" is from {start}s to {end}s") +``` + +ํŒ: +- ์ด TVP ๊ตฌํ˜„์€ ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด [BertTokenizer]๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์‹œ๊ฐ์  ์ž„๋ฒ ๋”ฉ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด Resnet-50 ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ์‚ฌ์ „ ํ•™์Šต๋œ [tvp-base](https://huggingface.co/Intel/tvp-base)์˜ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ๊ณต๊ฐœ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- ์‹œ๊ฐ„์  ๋น„๋””์˜ค ๊ทธ๋ผ์šด๋”ฉ ์ž‘์—…์— ๋Œ€ํ•œ TVP์˜ ์„ฑ๋Šฅ์€ [ํ‘œ 2](https://huggingface.co/papers/2303.04995)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +## TvpConfig [[transformers.TvpConfig]] + +[[autodoc]] TvpConfig + +## TvpImageProcessor [[transformers.TvpImageProcessor]] + +[[autodoc]] TvpImageProcessor + - preprocess + +## TvpProcessor [[transformers.TvpProcessor]] + +[[autodoc]] TvpProcessor + - __call__ + +## TvpModel [[transformers.TvpModel]] + +[[autodoc]] TvpModel + - forward + +## TvpForVideoGrounding [[transformers.TvpForVideoGrounding]] + +[[autodoc]] TvpForVideoGrounding + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vit.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vit.md new file mode 100644 index 0000000000000000000000000000000000000000..9ec7af81fdb4ff829a5e85b1b024917909bc1465 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vit.md @@ -0,0 +1,172 @@ + + +# Vision Transformer (ViT) [[vision-transformer-vit]] + +## ๊ฐœ์š” [[overview]] + +Vision Transformer (ViT) ๋ชจ๋ธ์€ Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://huggingface.co/papers/2010.11929)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Transformer ์ธ์ฝ”๋”๋ฅผ ImageNet์—์„œ ์„ฑ๊ณต์ ์œผ๋กœ ํ›ˆ๋ จ์‹œํ‚จ ์ฒซ ๋ฒˆ์งธ ๋…ผ๋ฌธ์œผ๋กœ, ๊ธฐ์กด์˜ ์ž˜ ์•Œ๋ ค์ง„ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(CNN) ๊ตฌ์กฐ์™€ ๋น„๊ตํ•ด ๋งค์šฐ ์šฐ์ˆ˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*Transformer ์•„ํ‚คํ…์ฒ˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์—์„œ ์‚ฌ์‹ค์ƒ ํ‘œ์ค€์œผ๋กœ ์ž๋ฆฌ ์žก์•˜์œผ๋‚˜, ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ์˜ ์ ์šฉ์€ ์—ฌ์ „ํžˆ ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค. ๋น„์ „์—์„œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ์ข…์ข… ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(CNN)๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ๋˜๊ฑฐ๋‚˜, ์ „์ฒด ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ํŠน์ • ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ CNN ์˜์กด์„ฑ์ด ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ด๋ฏธ์ง€ ํŒจ์น˜๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ์ˆœ์ˆ˜ํ•œ Transformer๊ฐ€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ž‘์—…์—์„œ ๋งค์šฐ ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ํ›„, ImageNet, CIFAR-100, VTAB ๋“ฑ ๋‹ค์–‘ํ•œ ์ค‘์†Œํ˜• ์ด๋ฏธ์ง€ ์ธ์‹ ๋ฒค์น˜๋งˆํฌ์— ์ ์šฉํ•˜๋ฉด Vision Transformer(ViT)๋Š” ์ตœ์‹  ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง๊ณผ ๋น„๊ตํ•ด ๋งค์šฐ ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜๋ฉด์„œ๋„ ํ›ˆ๋ จ์— ํ•„์š”ํ•œ ๊ณ„์‚ฐ ์ž์›์„ ์ƒ๋‹นํžˆ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.* + + + + ViT ์•„ํ‚คํ…์ฒ˜. ์›๋ณธ ๋…ผ๋ฌธ์—์„œ ๋ฐœ์ทŒ. + +์›๋ž˜์˜ Vision Transformer์— ์ด์–ด, ์—ฌ๋Ÿฌ ํ›„์† ์—ฐ๊ตฌ๋“ค์ด ์ง„ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค: + + +- [DeiT](deit) (Data-efficient Image Transformers) (Facebook AI ๊ฐœ๋ฐœ). DeiT ๋ชจ๋ธ์€ distilled vision transformers์ž…๋‹ˆ๋‹ค. + DeiT์˜ ์ €์ž๋“ค์€ ๋” ํšจ์œจ์ ์œผ๋กœ ํ›ˆ๋ จ๋œ ViT ๋ชจ๋ธ๋„ ๊ณต๊ฐœํ–ˆ์œผ๋ฉฐ, ์ด๋Š” [`ViTModel`] ๋˜๋Š” [`ViTForImageClassification`]์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” 3๊ฐ€์ง€ ํฌ๊ธฐ๋กœ 4๊ฐœ์˜ ๋ณ€ํ˜•์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค: *facebook/deit-tiny-patch16-224*, *facebook/deit-small-patch16-224*, *facebook/deit-base-patch16-224* and *facebook/deit-base-patch16-384*. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์— ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•˜๋ ค๋ฉด [`DeiTImageProcessor`]๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์‹ญ์‹œ์˜ค. + +- [BEiT](beit) (BERT pre-training of Image Transformers) (Microsoft Research ๊ฐœ๋ฐœ). BEiT ๋ชจ๋ธ์€ BERT (masked image modeling)์— ์˜๊ฐ์„ ๋ฐ›๊ณ  VQ-VAE์— ๊ธฐ๋ฐ˜ํ•œ self-supervised ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ supervised pre-trained vision transformers๋ณด๋‹ค ๋” ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. + +- DINO (Vision Transformers์˜ self-supervised ํ›ˆ๋ จ์„ ์œ„ํ•œ ๋ฐฉ๋ฒ•) (Facebook AI ๊ฐœ๋ฐœ). DINO ๋ฐฉ๋ฒ•์œผ๋กœ ํ›ˆ๋ จ๋œ Vision Transformer๋Š” ํ•™์Šต๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ๋„ ๊ฐ์ฒด๋ฅผ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ๋ณผ ์ˆ˜ ์—†๋Š” ๋งค์šฐ ํฅ๋ฏธ๋กœ์šด ๋Šฅ๋ ฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. DINO ์ฒดํฌํฌ์ธํŠธ๋Š” [hub](https://huggingface.co/models?other=dino)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +- [MAE](vit_mae) (Masked Autoencoders) (Facebook AI ๊ฐœ๋ฐœ). Vision Transformer๋ฅผ ๋น„๋Œ€์นญ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์Šคํฌ๋œ ํŒจ์น˜์˜ ๋†’์€ ๋น„์œจ(75%)์—์„œ ํ”ฝ์…€ ๊ฐ’์„ ์žฌ๊ตฌ์„ฑํ•˜๋„๋ก ์‚ฌ์ „ ํ•™์Šตํ•จ์œผ๋กœ์จ, ์ €์ž๋“ค์€ ์ด ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ๋ฏธ์„ธ ์กฐ์ • ํ›„ supervised ๋ฐฉ์‹์˜ ์‚ฌ์ „ ํ•™์Šต์„ ๋Šฅ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [nielsr](https://huggingface.co/nielsr)์— ์˜ํ•ด ๊ธฐ์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ(JAX๋กœ ์ž‘์„ฑ๋จ)์€ [์—ฌ๊ธฐ](https://github.com/google-research/vision_transformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +์ฐธ๊ณ ๋กœ, ์šฐ๋ฆฌ๋Š” Ross Wightman์˜ [timm ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ](https://github.com/rwightman/pytorch-image-models)์—์„œ JAX์—์„œ PyTorch๋กœ ๋ณ€ํ™˜๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ค์‹œ ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ณต๋กœ๋Š” ๊ทธ์—๊ฒŒ ๋Œ๋ฆฝ๋‹ˆ๋‹ค! + +## ์‚ฌ์šฉ ํŒ [[usage-tips]] + +- Transformer ์ธ์ฝ”๋”์— ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด, ๊ฐ ์ด๋ฏธ์ง€๋Š” ๊ณ ์ • ํฌ๊ธฐ์˜ ๊ฒน์น˜์ง€ ์•Š๋Š” ํŒจ์น˜๋“ค๋กœ ๋ถ„ํ• ๋œ ํ›„ ์„ ํ˜• ์ž„๋ฒ ๋”ฉ๋ฉ๋‹ˆ๋‹ค. ์ „์ฒด ์ด๋ฏธ์ง€๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” [CLS] ํ† ํฐ์ด ์ถ”๊ฐ€๋˜์–ด, ๋ถ„๋ฅ˜์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ๋˜ํ•œ ์ ˆ๋Œ€ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์ถ”๊ฐ€ํ•˜์—ฌ, ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฒกํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ‘œ์ค€ Transformer ์ธ์ฝ”๋”์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. +- Vision Transformer๋Š” ๋ชจ๋“  ์ด๋ฏธ์ง€๊ฐ€ ๋™์ผํ•œ ํฌ๊ธฐ(ํ•ด์ƒ๋„)์—ฌ์•ผ ํ•˜๋ฏ€๋กœ, [ViTImageProcessor]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ์— ๋งž๊ฒŒ ๋ฆฌ์‚ฌ์ด์ฆˆ(๋˜๋Š” ๋ฆฌ์Šค์ผ€์ผ)ํ•˜๊ณ  ์ •๊ทœํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์‚ฌ์ „ ํ•™์Šต์ด๋‚˜ ๋ฏธ์„ธ ์กฐ์ • ์‹œ ์‚ฌ์šฉ๋œ ํŒจ์น˜ ํ•ด์ƒ๋„์™€ ์ด๋ฏธ์ง€ ํ•ด์ƒ๋„๋Š” ๊ฐ ์ฒดํฌํฌ์ธํŠธ์˜ ์ด๋ฆ„์— ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, `google/vit-base-patch16-224`๋Š” ํŒจ์น˜ ํ•ด์ƒ๋„๊ฐ€ 16x16์ด๊ณ  ๋ฏธ์„ธ ์กฐ์ • ํ•ด์ƒ๋„๊ฐ€ 224x224์ธ ๊ธฐ๋ณธ ํฌ๊ธฐ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ๋Š” [hub](https://huggingface.co/models?search=vit)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฒดํฌํฌ์ธํŠธ๋Š” (1) [ImageNet-21k](http://www.image-net.org/) (1,400๋งŒ ๊ฐœ์˜ ์ด๋ฏธ์ง€์™€ 21,000๊ฐœ์˜ ํด๋ž˜์Šค)์—์„œ๋งŒ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ๊ฑฐ๋‚˜, ๋˜๋Š” (2) [ImageNet](http://www.image-net.org/challenges/LSVRC/2012/) (ILSVRC 2012, 130๋งŒ ๊ฐœ์˜ ์ด๋ฏธ์ง€์™€ 1,000๊ฐœ์˜ ํด๋ž˜์Šค)์—์„œ ์ถ”๊ฐ€๋กœ ๋ฏธ์„ธ ์กฐ์ •๋œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. +- Vision Transformer๋Š” 224x224 ํ•ด์ƒ๋„๋กœ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ • ์‹œ, ์‚ฌ์ „ ํ•™์Šต๋ณด๋‹ค ๋” ๋†’์€ ํ•ด์ƒ๋„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค ([(Touvron et al., 2019)](https://huggingface.co/papers/1906.06423), [(Kolesnikovet al., 2020)](https://huggingface.co/papers/1912.11370). ๋” ๋†’์€ ํ•ด์ƒ๋„๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด, ์ €์ž๋“ค์€ ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ์˜ ์œ„์น˜์— ๋”ฐ๋ผ ์‚ฌ์ „ ํ•™์Šต๋œ ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์˜ 2D ๋ณด๊ฐ„(interpolation)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. +- ์ตœ๊ณ ์˜ ๊ฒฐ๊ณผ๋Š” supervised ๋ฐฉ์‹์˜ ์‚ฌ์ „ ํ•™์Šต์—์„œ ์–ป์–ด์กŒ์œผ๋ฉฐ, ์ด๋Š” NLP์—์„œ๋Š” ํ•ด๋‹น๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ๋งˆ์Šคํฌ๋œ ํŒจ์น˜ ์˜ˆ์ธก(๋งˆ์Šคํฌ๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์—์„œ ์˜๊ฐ์„ ๋ฐ›์€ self-supervised ์‚ฌ์ „ ํ•™์Šต ๋ชฉํ‘œ)์„ ์‚ฌ์šฉํ•œ ์‹คํ—˜๋„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์œผ๋กœ ๋” ์ž‘์€ ViT-B/16 ๋ชจ๋ธ์€ ImageNet์—์„œ 79.9%์˜ ์ •ํ™•๋„๋ฅผ ๋‹ฌ์„ฑํ•˜์˜€์œผ๋ฉฐ, ์ด๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•œ ๊ฒƒ๋ณด๋‹ค 2% ๊ฐœ์„ ๋œ ๊ฒฐ๊ณผ์ด์ง€๋งŒ, ์—ฌ์ „ํžˆ supervised ์‚ฌ์ „ ํ•™์Šต๋ณด๋‹ค 4% ๋‚ฎ์Šต๋‹ˆ๋‹ค. + +### Scaled Dot Product Attention (SDPA) ์‚ฌ์šฉํ•˜๊ธฐ [[using-scaled-dot-product-attention-sdpa]] + +PyTorch๋Š” `torch.nn.functional`์˜ ์ผ๋ถ€๋กœ์„œ native scaled dot-product attention (SDPA) ์—ฐ์‚ฐ์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ๋ฐ ์‚ฌ์šฉ ์ค‘์ธ ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ž์„ธํ•œ ๋‚ด์šฉ์€ [๊ณต์‹ ๋ฌธ์„œ](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)๋‚˜ [GPU ์ถ”๋ก ](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. + +SDPA๋Š” `torch>=2.1.1`์—์„œ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€๋งŒ, `from_pretrained()`์—์„œ `attn_implementation="sdpa"`๋กœ ์„ค์ •ํ•˜์—ฌ SDPA๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์š”์ฒญํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +``` +from transformers import ViTForImageClassification +model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224", attn_implementation="sdpa", torch_dtype=torch.float16) +... +``` + +์ตœ์ ์˜ ์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฐ˜์ •๋ฐ€๋„(์˜ˆ: `torch.float16` ๋˜๋Š” `torch.bfloat16`)๋กœ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +๋กœ์ปฌ ๋ฒค์น˜๋งˆํฌ(A100-40GB, PyTorch 2.3.0, OS Ubuntu 22.04)์—์„œ `float32`์™€ `google/vit-base-patch16-224` ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์ถ”๋ก  ์‹œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†๋„ ํ–ฅ์ƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. + +| Batch size | Average inference time (ms), eager mode | Average inference time (ms), sdpa model | Speed up, Sdpa / Eager (x) | +|--------------|-------------------------------------------|-------------------------------------------|------------------------------| +| 1 | 7 | 6 | 1.17 | +| 2 | 8 | 6 | 1.33 | +| 4 | 8 | 6 | 1.33 | +| 8 | 8 | 6 | 1.33 | + +## ๋ฆฌ์†Œ์Šค [[resources]] + +ViT์˜ ์ถ”๋ก  ๋ฐ ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ฏธ์„ธ ์กฐ์ •๊ณผ ๊ด€๋ จ๋œ ๋ฐ๋ชจ ๋…ธํŠธ๋ถ์€ [์—ฌ๊ธฐ](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/VisionTransformer)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hugging Face์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ž๋ฃŒ์™€ ์ปค๋ฎค๋‹ˆํ‹ฐ(๐ŸŒŽ๋กœ ํ‘œ์‹œ๋œ) ์ž๋ฃŒ ๋ชฉ๋ก์€ ViT๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ชฉ๋ก์— ํฌํ•จ๋  ์ž๋ฃŒ๋ฅผ ์ œ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Pull Request๋ฅผ ์—ด์–ด ์ฃผ์‹œ๋ฉด ๊ฒ€ํ† ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์„ค๋ช…ํ•˜๋Š” ์ž๋ฃŒ๊ฐ€ ๊ฐ€์žฅ ์ด์ƒ์ ์ด๋ฉฐ, ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ์ค‘๋ณตํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์‹ญ์‹œ์˜ค. + +`ViTForImageClassification` ์€ ๋‹ค์Œ์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค: + + +- [Hugging Face Transformers๋กœ ViT๋ฅผ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ๋งž๊ฒŒ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/blog/fine-tune-vit)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ +- [Hugging Face Transformers์™€ `Keras`๋ฅผ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜](https://www.philschmid.de/image-classification-huggingface-transformers-keras)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ +- [Hugging Face Transformers๋ฅผ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฏธ์„ธ ์กฐ์ •](https://github.com/huggingface/notebooks/blob/main/examples/image_classification.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ +- [Hugging Face Trainer๋กœ CIFAR-10์—์„œ Vision Transformer ๋ฏธ์„ธ ์กฐ์ •](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_the_%F0%9F%A4%97_Trainer.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ +- [PyTorch Lightning์œผ๋กœ CIFAR-10์—์„œ Vision Transformer ๋ฏธ์„ธ ์กฐ์ •](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Fine_tuning_the_Vision_Transformer_on_CIFAR_10_with_PyTorch_Lightning.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ + +โš—๏ธ ์ตœ์ ํ™” + +- [Optimum์„ ์‚ฌ์šฉํ•œ ์–‘์žํ™”๋ฅผ ํ†ตํ•ด Vision Transformer(ViT) ๊ฐ€์†](https://www.philschmid.de/optimizing-vision-transformer)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ + +โšก๏ธ ์ถ”๋ก  + +- [Google Brain์˜ Vision Transformer(ViT) ๋น ๋ฅธ ๋ฐ๋ชจ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/VisionTransformer/Quick_demo_of_HuggingFace_version_of_Vision_Transformer_inference.ipynb)์— ๋Œ€ํ•œ ๋…ธํŠธ๋ถ + +๐Ÿš€ ๋ฐฐํฌ + +- [TF Serving์œผ๋กœ Hugging Face์—์„œ Tensorflow Vision ๋ชจ๋ธ ๋ฐฐํฌ](https://huggingface.co/blog/tf-serving-vision)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ +- [Vertex AI์—์„œ Hugging Face ViT ๋ฐฐํฌ](https://huggingface.co/blog/deploy-vertex-ai)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ +- [TF Serving์„ ์‚ฌ์šฉํ•˜์—ฌ Kubernetes์—์„œ Hugging Face ViT ๋ฐฐํฌ](https://huggingface.co/blog/deploy-tfserving-kubernetes)์— ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ + +## ViTConfig [[transformers.ViTConfig]] + +[[autodoc]] ViTConfig + +## ViTFeatureExtractor [[transformers.ViTFeatureExtractor]] + +[[autodoc]] ViTFeatureExtractor + - __call__ + +## ViTImageProcessor [[transformers.ViTImageProcessor]] + +[[autodoc]] ViTImageProcessor + - preprocess + +## ViTImageProcessorFast [[transformers.ViTImageProcessorFast]] + +[[autodoc]] ViTImageProcessorFast + - preprocess + + + + +## ViTModel [[transformers.ViTModel]] + +[[autodoc]] ViTModel + - forward + +## ViTForMaskedImageModeling [[transformers.ViTForMaskedImageModeling]] + +[[autodoc]] ViTForMaskedImageModeling + - forward + +## ViTForImageClassification [[transformers.ViTForImageClassification]] + +[[autodoc]] ViTForImageClassification + - forward + + + + +## TFViTModel [[transformers.TFViTModel]] + +[[autodoc]] TFViTModel + - call + +## TFViTForImageClassification [[transformers.TFViTForImageClassification]] + +[[autodoc]] TFViTForImageClassification + - call + + + + +## FlaxVitModel [[transformers.FlaxViTModel]] + +[[autodoc]] FlaxViTModel + - __call__ + +## FlaxViTForImageClassification [[transformers.FlaxViTForImageClassification]] + +[[autodoc]] FlaxViTForImageClassification + - __call__ + + + \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vivit.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vivit.md new file mode 100644 index 0000000000000000000000000000000000000000..2ae32af0441cf40788633ef123fd82688e886c01 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/vivit.md @@ -0,0 +1,42 @@ + + +# Video Vision Transformer (ViViT) [[video-vision-transformer-vivit]] + +## ๊ฐœ์š” [[overview]] + +Vivit ๋ชจ๋ธ์€ Anurag Arnab, Mostafa Dehghani, Georg Heigold, Chen Sun, Mario Luฤiฤ‡, Cordelia Schmid๊ฐ€ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ [ViViT: A Video Vision Transformer](https://huggingface.co/papers/2103.15691)์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋…ผ๋ฌธ์€ ๋น„๋””์˜ค ์ดํ•ด๋ฅผ ์œ„ํ•œ pure-transformer ๊ธฐ๋ฐ˜์˜ ๋ชจ๋ธ ์ง‘ํ•ฉ ์ค‘์—์„œ ์ตœ์ดˆ๋กœ ์„ฑ๊ณตํ•œ ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์—์„œ ์ตœ๊ทผ ์„ฑ๊ณต์„ ๊ฑฐ๋‘” ์ˆœ์ˆ˜ ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋น„๋””์˜ค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ๋ชจ๋ธ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ๋ชจ๋ธ์€ ์ž…๋ ฅ ๋น„๋””์˜ค๋กœ๋ถ€ํ„ฐ ์‹œ๊ณต๊ฐ„ ํ† ํฐ์„ ์ถ”์ถœํ•œ ํ›„, ์ด๋ฅผ ์ผ๋ จ์˜ ํŠธ๋žœ์Šคํฌ๋จธ ๋ ˆ์ด์–ด๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๋น„๋””์˜ค์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ธด ํ† ํฐ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด, ์ž…๋ ฅ์˜ ๊ณต๊ฐ„ ๋ฐ ์‹œ๊ฐ„ ์ฐจ์›์„ ๋ถ„๋ฆฌํ•˜๋Š” ์—ฌ๋Ÿฌ ํšจ์œจ์ ์ธ ๋ชจ๋ธ ๋ณ€ํ˜•์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์€ ๋Œ€๊ทœ๋ชจ ํ•™์Šต ๋ฐ์ดํ„ฐ์…‹์—์„œ๋งŒ ํšจ๊ณผ์ ์ด๋ผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” ํ•™์Šต ์ค‘ ๋ชจ๋ธ์„ ํšจ๊ณผ์ ์œผ๋กœ ์ •๊ทœํ™”ํ•˜๊ณ , ์‚ฌ์ „ ํ•™์Šต๋œ ์ด๋ฏธ์ง€ ๋ชจ๋ธ์„ ํ™œ์šฉํ•จ์œผ๋กœ์จ ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ ๋ฐ์ดํ„ฐ์…‹์—์„œ๋„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฒ ์ €ํ•œ ์†Œ๊ฑฐ(ablation) ์—ฐ๊ตฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  Kinetics 400 ๋ฐ 600, Epic Kitchens, Something-Something v2, Moments in Time์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ๋น„๋””์˜ค ๋ถ„๋ฅ˜ ๋ฒค์น˜๋งˆํฌ์—์„œ ์ตœ์ฒจ๋‹จ ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•˜์—ฌ, ๊ธฐ์กด์˜ 3D ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•๋“ค์„ ๋Šฅ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.* + +์ด ๋ชจ๋ธ์€ [jegormeister](https://huggingface.co/jegormeister)๊ฐ€ ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์›๋ณธ ์ฝ”๋“œ(JAX๋กœ ์ž‘์„ฑ๋จ)๋Š” [์—ฌ๊ธฐ](https://github.com/google-research/scenic/tree/main/scenic/projects/vivit)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## VivitConfig [[transformers.VivitConfig]] + +[[autodoc]] VivitConfig + +## VivitImageProcessor [[transformers.VivitImageProcessor]] + +[[autodoc]] VivitImageProcessor + - preprocess + +## VivitModel [[transformers.VivitModel]] + +[[autodoc]] VivitModel + - forward + +## VivitForVideoClassification [[transformers.VivitForVideoClassification]] + +[[autodoc]] transformers.VivitForVideoClassification + - forward diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/whisper.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/whisper.md new file mode 100644 index 0000000000000000000000000000000000000000..f48bae1e60f5d0d7ba7ffe7c69b26bca837f0fa9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_doc/whisper.md @@ -0,0 +1,128 @@ + + +# Whisper [[whisper]] + +## ๊ฐœ์š” [[overview]] + +Whisper ๋ชจ๋ธ์€ Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever์— ์˜ํ•ด [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf)์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ์˜ ์ดˆ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +*์šฐ๋ฆฌ๋Š” ์ธํ„ฐ๋„ท์—์„œ ๋Œ€๋Ÿ‰์˜ ์˜ค๋””์˜ค๋ฅผ ๊ธ€๋กœ ์˜ฎ๊ธด ๊ฒƒ์„ ์˜ˆ์ธกํ•˜๋„๋ก ๊ฐ„๋‹จํžˆ ํ›ˆ๋ จ๋œ ์Œ์„ฑ ์ฒ˜๋ฆฌ ์‹œ์Šคํ…œ์˜ ์„ฑ๋Šฅ์„ ์—ฐ๊ตฌํ•ฉ๋‹ˆ๋‹ค. 68๋งŒ ์‹œ๊ฐ„์˜ ๋‹ค๊ตญ์–ด ๋ฐ ๋‹ค์ค‘ ์ž‘์—… ์ง€๋„(multitask supervision)์— ํ™•์žฅํ–ˆ์„ ๋•Œ, ๊ฒฐ๊ณผ ๋ชจ๋ธ์€ ํ‘œ์ค€ ๋ฒค์น˜๋งˆํฌ์— ์ž˜ ์ผ๋ฐ˜ํ™”๋˜๋ฉฐ, ๋ฏธ์„ธ ์กฐ์ •์ด ํ•„์š” ์—†๋Š” ์ œ๋กœ์ƒท ์ „์†ก ์„ค์ •์—์„œ ์ด์ „์˜ ์™„์ „ํžˆ ์ง€๋„๋œ(fully-supervised) ๊ฒฐ๊ณผ์™€ ๊ฒฝ์Ÿํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ๊ณผ ๋น„๊ตํ•˜๋ฉด, ์ด ๋ชจ๋ธ์€ ์‚ฌ๋žŒ์˜ ์ •ํ™•๋„์™€ ๊ฒฌ๊ณ ์„ฑ์— ๊ทผ์ ‘ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ•๋ ฅํ•œ ์Œ์„ฑ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ถ”๊ฐ€ ์ž‘์—…์˜ ๊ธฐ๋ฐ˜์ด ๋  ๋ชจ๋ธ๊ณผ ์ถ”๋ก  ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•ฉ๋‹ˆ๋‹ค.* + + + +ํŒ: + +- ์ด ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ณ„๋„์˜ ๋ฏธ์„ธ ์กฐ์ • ์—†์ด๋„ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +- ์•„ํ‚คํ…์ฒ˜๋Š” ๊ณ ์ „์ ์ธ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ์ถ”๋ก ์„ ์œ„ํ•ด [`~generation.GenerationMixin.generate`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ํ˜„์žฌ ์ถ”๋ก ์€ ์งง์€ ํ˜•์‹์—๋งŒ ๊ตฌํ˜„๋˜์–ด ์žˆ์œผ๋ฉฐ, ์˜ค๋””์˜ค๋Š” 30์ดˆ ๋ฏธ๋งŒ์˜ ์„ธ๊ทธ๋จผํŠธ๋กœ ๋ฏธ๋ฆฌ ๋ถ„ํ• ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž„์Šคํƒฌํ”„๋ฅผ ํฌํ•จํ•œ ๊ธด ํ˜•์‹์— ๋Œ€ํ•œ ์ถ”๋ก ์€ ํ–ฅํ›„ ๋ฆด๋ฆฌ์Šค์—์„œ ๊ตฌํ˜„๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. +- [`WhisperProcessor`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์— ์‚ฌ์šฉํ•  ์˜ค๋””์˜ค๋ฅผ ์ค€๋น„ํ•˜๊ณ , ์˜ˆ์ธก๋œ ID๋ฅผ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +- ๋ชจ๋ธ๊ณผ ํ”„๋กœ์„ธ์„œ๋ฅผ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + +```bash +python src/transformers/models/whisper/convert_openai_to_hf.py --checkpoint_path "" --pytorch_dump_folder_path "Arthur/whisper-3" --convert_preprocessor True +``` +์Šคํฌ๋ฆฝํŠธ๋Š” OpenAI ์ฒดํฌํฌ์ธํŠธ์—์„œ ํ•„์š”ํ•œ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. OpenAI ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด `tiktoken` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ OpenAI ํ† ํฐํ™”๊ธฐ๋ฅผ `tokenizers` ๋ฒ„์ „์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ [Arthur Zucker](https://huggingface.co/ArthurZ)์— ์˜ํ•ด ์ œ๊ณต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์˜ Tensorflow ๋ฒ„์ „์€ [amyeroberts](https://huggingface.co/amyeroberts)์— ์˜ํ•ด ์ œ๊ณต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +์›๋ณธ ์ฝ”๋“œ๋Š” [์—ฌ๊ธฐ](https://github.com/openai/whisper)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## WhisperConfig [[whisperconfig]] + +[[autodoc]] WhisperConfig + +## WhisperTokenizer [[whispertokenizer]] + +[[autodoc]] WhisperTokenizer + - set_prefix_tokens + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## WhisperTokenizerFast [[whispertokenizerfast]] + +[[autodoc]] WhisperTokenizerFast + - set_prefix_tokens + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + +## WhisperFeatureExtractor [[whisperfeatureextractor]] + +[[autodoc]] WhisperFeatureExtractor + - __call__ + +## WhisperProcessor [[whisperprocessor]] + +[[autodoc]] WhisperProcessor + - __call__ + - from_pretrained + - save_pretrained + - batch_decode + - decode + +## WhisperModel [[whispermodel]] + +[[autodoc]] WhisperModel + - forward + - _mask_input_features + +## WhisperForConditionalGeneration [[whisperforconditionalgeneration]] + +[[autodoc]] WhisperForConditionalGeneration + - forward + +## WhisperForAudioClassification [[whisperforaudioclassification]] + +[[autodoc]] WhisperForAudioClassification + - forward + + + +## TFWhisperModel [[tfwhispermodel]] + +[[autodoc]] TFWhisperModel + - call + +## TFWhisperForConditionalGeneration [[tfwhisperforconditionalgeneration]] + +[[autodoc]] TFWhisperForConditionalGeneration + - call + + +## FlaxWhisperModel [[flaxwhispermodel]] + +[[autodoc]] FlaxWhisperModel + - __call__ + +## FlaxWhisperForConditionalGeneration [[flaxwhisperforconditionalgeneration]] + +[[autodoc]] FlaxWhisperForConditionalGeneration + - __call__ + +## FlaxWhisperForAudioClassification [[flaxwhisperforaudioclassification]] + +[[autodoc]] FlaxWhisperForAudioClassification + - __call__ + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_memory_anatomy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_memory_anatomy.md new file mode 100644 index 0000000000000000000000000000000000000000..a729b29a7c30a99b3d73fe4572ed0c2506aa38ff --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_memory_anatomy.md @@ -0,0 +1,242 @@ + + +# ๋ชจ๋ธ ํ•™์Šต ํ•ด๋ถ€ํ•˜๊ธฐ [[model-training-anatomy]] + +๋ชจ๋ธ ํ›ˆ๋ จ ์†๋„์™€ ๋ฉ”๋ชจ๋ฆฌ ํ™œ์šฉ์˜ ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ธฐ์ˆ ์„ ์ดํ•ดํ•˜๋ ค๋ฉด GPU๊ฐ€ ํ›ˆ๋ จ ์ค‘์— ์–ด๋–ป๊ฒŒ ํ™œ์šฉ๋˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์ˆ˜ํ–‰๋˜๋Š” ์—ฐ์‚ฐ์— ๋”ฐ๋ผ ์—ฐ์‚ฐ ๊ฐ•๋„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€์— ์ต์ˆ™ํ•ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋จผ์ € GPU ํ™œ์šฉ๊ณผ ๋ชจ๋ธ ํ›ˆ๋ จ ์‹คํ–‰์— ๋Œ€ํ•œ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ๋ชจ๋ฅผ ์œ„ํ•ด ๋ช‡๋ช‡ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install transformers datasets accelerate nvidia-ml-py3 +``` + +`nvidia-ml-py3` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Python ๋‚ด๋ถ€์—์„œ ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์˜ `nvidia-smi` ๋ช…๋ น์–ด์— ์ต์ˆ™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Python์—์„œ ์ง์ ‘ ๋™์ผํ•œ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. + +๊ทธ ๋‹ค์Œ, 100๊ณผ 30000 ์‚ฌ์ด์˜ ๋ฌด์ž‘์œ„ ํ† ํฐ ID์™€ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์œ„ํ•œ ์ด์ง„ ๋ ˆ์ด๋ธ”์ธ ๋”๋ฏธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. +๊ธธ์ด๊ฐ€ ๊ฐ๊ฐ 512์ธ ์ด 512๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ ธ์™€ PyTorch ํ˜•์‹์˜ [`~datasets.Dataset`]์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. + + +```py +>>> import numpy as np +>>> from datasets import Dataset + + +>>> seq_len, dataset_size = 512, 512 +>>> dummy_data = { +... "input_ids": np.random.randint(100, 30000, (dataset_size, seq_len)), +... "labels": np.random.randint(0, 1, (dataset_size)), +... } +>>> ds = Dataset.from_dict(dummy_data) +>>> ds.set_format("pt") +``` + +GPU ํ™œ์šฉ ๋ฐ [`Trainer`]๋กœ ์‹คํ–‰ํ•œ ํ›ˆ๋ จ ๊ณผ์ •์— ๋Œ€ํ•œ ์š”์•ฝ ํ†ต๊ณ„๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๋‘ ๊ฐœ์˜ ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> from pynvml import * + + +>>> def print_gpu_utilization(): +... nvmlInit() +... handle = nvmlDeviceGetHandleByIndex(0) +... info = nvmlDeviceGetMemoryInfo(handle) +... print(f"GPU memory occupied: {info.used//1024**2} MB.") + + +>>> def print_summary(result): +... print(f"Time: {result.metrics['train_runtime']:.2f}") +... print(f"Samples/second: {result.metrics['train_samples_per_second']:.2f}") +... print_gpu_utilization() +``` + +์‹œ์ž‘ํ•  ๋•Œ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค: + +```py +>>> print_gpu_utilization() +GPU memory occupied: 0 MB. +``` + +์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ธฐ ์ „์—๋Š” ์˜ˆ์ƒ๋Œ€๋กœ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ ์œ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์‚ฌ์šฉ์ž์˜ ๊ธฐ๊ธฐ์—์„œ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ค‘๋‹จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉ์ž๋Š” ๋ชจ๋“  ์—ฌ์œ  GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด GPU์— ๋กœ๋“œ๋  ๋•Œ ์ปค๋„๋„ ๋กœ๋“œ๋˜๋ฏ€๋กœ 1-2GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด GPU์— ์ž‘์€ ํ…์„œ๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์ปค๋„์ด ๋กœ๋“œ๋˜๋„๋ก ํŠธ๋ฆฌ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch + + +>>> torch.ones((1, 1)).to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 1343 MB. +``` + +์ปค๋„๋งŒ์œผ๋กœ๋„ GPU ๋ฉ”๋ชจ๋ฆฌ์˜ 1.3GB๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ๋ชจ๋ธ ๋กœ๋“œ [[load-model]] + +์šฐ์„ , `google-bert/bert-large-uncased` ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ง์ ‘ GPU์— ๋กœ๋“œํ•ด์„œ ๊ฐ€์ค‘์น˜๋งŒ์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +```py +>>> from transformers import AutoModelForSequenceClassification + + +>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-large-uncased").to("cuda") +>>> print_gpu_utilization() +GPU memory occupied: 2631 MB. +``` + +๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜๋งŒ์œผ๋กœ๋„ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 1.3 GB ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ˆซ์ž๋Š” ์‚ฌ์šฉํ•˜๋Š” GPU์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ตœ์‹  GPU์—์„œ๋Š” ๋ชจ๋ธ ์‚ฌ์šฉ ์†๋„๋ฅผ ๋†’์ด๋Š” ์ตœ์ ํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ๋กœ๋“œ๋˜๋ฏ€๋กœ, ๋ชจ๋ธ์ด ๋” ๋งŽ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ `nvidia-smi` CLI์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + +```bash +nvidia-smi +``` + +```bash +Tue Jan 11 08:58:05 2022 ++-----------------------------------------------------------------------------+ +| NVIDIA-SMI 460.91.03 Driver Version: 460.91.03 CUDA Version: 11.2 | +|-------------------------------+----------------------+----------------------+ +| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|===============================+======================+======================| +| 0 Tesla V100-SXM2... On | 00000000:00:04.0 Off | 0 | +| N/A 37C P0 39W / 300W | 2631MiB / 16160MiB | 0% Default | +| | | N/A | ++-------------------------------+----------------------+----------------------+ + ++-----------------------------------------------------------------------------+ +| Processes: | +| GPU GI CI PID Type Process name GPU Memory | +| ID ID Usage | +|=============================================================================| +| 0 N/A N/A 3721 C ...nvs/codeparrot/bin/python 2629MiB | ++-----------------------------------------------------------------------------+ +``` + +์ด์ „๊ณผ ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  16GB ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์ง„ V100 GPU๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด์ œ ๋ชจ๋ธ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•˜์—ฌ GPU ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋ช‡๋ช‡ ํ‘œ์ค€ ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค: + +```py +default_args = { + "output_dir": "tmp", + "eval_strategy": "steps", + "num_train_epochs": 1, + "log_level": "error", + "report_to": "none", +} +``` + + + +์—ฌ๋Ÿฌ ์‹คํ—˜์„ ์‹คํ–‰ํ•  ๊ณ„ํš์ด๋ผ๋ฉด, ์‹คํ—˜ ๊ฐ„์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ œ๋Œ€๋กœ ๋น„์šฐ๊ธฐ ์œ„ํ•ด์„œ Python ์ปค๋„์„ ์‹คํ—˜ ์‚ฌ์ด๋งˆ๋‹ค ์žฌ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +## ๊ธฐ๋ณธ ํ›ˆ๋ จ์—์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ํ™œ์šฉ [[memory-utilization-at-vanilla-training]] + +[`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, GPU ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 4์ธ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import TrainingArguments, Trainer, logging + +>>> logging.set_verbosity_error() + + +>>> training_args = TrainingArguments(per_device_train_batch_size=4, **default_args) +>>> trainer = Trainer(model=model, args=training_args, train_dataset=ds) +>>> result = trainer.train() +>>> print_summary(result) +``` + +``` +Time: 57.82 +Samples/second: 8.86 +GPU memory occupied: 14949 MB. +``` + +์šฐ๋ฆฌ๋Š” ๋น„๊ต์  ์ž‘์€ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ๋„ ์ „์ฒด GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฑฐ์˜ ๋‹ค ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ๋ชจ๋ธ ์ˆ˜๋ ด ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๊ณ  ์ตœ์ข… ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ƒ์ ์œผ๋กœ๋Š” GPU ์ œํ•œ์ด ์•„๋‹Œ ์šฐ๋ฆฌ ๋ชจ๋ธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํฅ๋ฏธ๋กญ๊ฒŒ๋„ ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์กฐ๊ธˆ ๋” ์ž˜ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์˜ ์—ฐ์‚ฐ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ๋ชจ๋ธ์˜ ์—ฐ์‚ฐ ํ•ด๋ถ€ํ•˜๊ธฐ [[anatomy-of-models-operations]] + +ํŠธ๋žœ์Šคํฌ๋จธ ์•„ํ‚คํ…์ฒ˜์—๋Š” ์—ฐ์‚ฐ ๊ฐ•๋„(compute-intensity)์— ๋”ฐ๋ผ ๊ทธ๋ฃนํ™”๋œ 3๊ฐ€์ง€ ์ฃผ์š” ์—ฐ์‚ฐ ๊ทธ๋ฃน์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +1. **ํ…์„œ ์ถ•์•ฝ(Tensor Contractions)** + + ์„ ํ˜• ๋ ˆ์ด์–ด์™€ ๋ฉ€ํ‹ฐํ—ค๋“œ ์–ดํ…์…˜์˜ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ๋ชจ๋‘ **ํ–‰๋ ฌ-ํ–‰๋ ฌ ๊ณฑ์…ˆ(matrix-matrix multiplications)**์„ ์ผ๊ด„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์€ ํŠธ๋žœ์Šคํฌ๋จธ ํ›ˆ๋ จ์—์„œ ๊ฐ€์žฅ ์—ฐ์‚ฐ ๊ฐ•๋„๊ฐ€ ๋†’์€ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. + +2. **ํ†ต๊ณ„ ์ •๊ทœํ™”(Statistical Normalizations)** + + ์†Œํ”„ํŠธ๋งฅ์Šค์™€ ๋ ˆ์ด์–ด ์ •๊ทœํ™”๋Š” ํ…์„œ ์ถ•์•ฝ๋ณด๋‹ค ์—ฐ์‚ฐ ๊ฐ•๋„๊ฐ€ ๋‚ฎ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜ ์ด์ƒ์˜ **๊ฐ์†Œ ์—ฐ์‚ฐ(reduction operations)**์„ ํฌํ•จํ•˜๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ๋Š” map์„ ํ†ตํ•ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + +3. **์›์†Œ๋ณ„ ์—ฐ์‚ฐ์ž(Element-wise Operators)** + + ๊ทธ ์™ธ ์—ฐ์‚ฐ์ž๋“ค, **ํŽธํ–ฅ(biases), ๋“œ๋กญ์•„์›ƒ(dropout), ํ™œ์„ฑํ™” ํ•จ์ˆ˜(activations), ์ž”์ฐจ ์—ฐ๊ฒฐ(residual connections)**์ด ์—ฌ๊ธฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ๋“ค์€ ์—ฐ์‚ฐ ๊ฐ•๋„๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์Šต๋‹ˆ๋‹ค. + +์ด๋Ÿฌํ•œ ์ง€์‹์€ ์„ฑ๋Šฅ ๋ณ‘๋ชฉ ํ˜„์ƒ์„ ๋ถ„์„ํ•  ๋•Œ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋‚ด์šฉ์€ [Data Movement Is All You Need: A Case Study on Optimizing Transformers 2020](https://huggingface.co/papers/2007.00072)์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. + + +## ๋ชจ๋ธ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ [[anatomy-of-models-memory]] + +๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐ๋Š” ๋‹จ์ˆœํžˆ GPU์— ๋ชจ๋ธ์„ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ›ˆ๋ จ ์ค‘ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋งŽ์€ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. GPU ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +1. ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ +2. ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ +3. ๊ทธ๋ผ๋””์–ธํŠธ +4. ๊ทธ๋ผ๋””์–ธํŠธ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์ €์žฅ๋œ ์ˆœ๋ฐฉํ–ฅ ํ™œ์„ฑํ™” +5. ์ž„์‹œ ๋ฒ„ํผ +6. ๊ธฐ๋Šฅ๋ณ„ ๋ฉ”๋ชจ๋ฆฌ + +AdamW๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ํ›ˆ๋ จ๋œ ์ผ๋ฐ˜์ ์ธ ๋ชจ๋ธ์€ ๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋‹น 18 ๋ฐ”์ดํŠธ์™€ ํ™œ์„ฑํ™” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๋ก  ๋‹จ๊ณ„์—์„œ๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ๊ทธ๋ผ๋””์–ธํŠธ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด๋“ค์€ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ์ถ”๋ก ์˜ ๊ฒฝ์šฐ ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋‹น 6 ๋ฐ”์ดํŠธ์™€ ํ™œ์„ฑํ™” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +**๋ชจ๋ธ ๊ฐ€์ค‘์น˜:** + +- fp32 ํ›ˆ๋ จ์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 4 ๋ฐ”์ดํŠธ +- ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ›ˆ๋ จ์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 6 ๋ฐ”์ดํŠธ (๋ฉ”๋ชจ๋ฆฌ์— fp32์™€ fp16 ๋‘ ๊ฐ€์ง€ ๋ชจ๋ธ์„ ์œ ์ง€) + +**์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ:** + +- ์ผ๋ฐ˜ AdamW์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 8 ๋ฐ”์ดํŠธ (2๊ฐ€์ง€ ์ƒํƒœ ์œ ์ง€) +- [bitsandbytes](https://github.com/TimDettmers/bitsandbytes)์™€ ๊ฐ™์€ 8๋น„ํŠธ AdamW ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 2 ๋ฐ”์ดํŠธ +- Momentum์„ ๊ฐ€์ง„ SGD์™€ ๊ฐ™์€ ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 4 ๋ฐ”์ดํŠธ (ํ•˜๋‚˜์˜ ์ƒํƒœ๋งŒ ์œ ์ง€) + +**๊ทธ๋ผ๋””์–ธํŠธ** + +- fp32 ๋˜๋Š” ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ›ˆ๋ จ์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜ * 4 ๋ฐ”์ดํŠธ (๊ทธ๋ผ๋””์–ธํŠธ๋Š” ํ•ญ์ƒ fp32์œผ๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.) + +**์ˆœ๋ฐฉํ–ฅ ํ™œ์„ฑํ™”** + +- ํฌ๊ธฐ๋Š” ์—ฌ๋Ÿฌ ์š”์ธ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋ฉฐ, ์ฃผ์š” ์š”์ธ์€ ์‹œํ€€์Šค ๊ธธ์ด, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ๋ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. + +์ˆœ๋ฐฉํ–ฅ ๋ฐ ์—ญ๋ฐฉํ–ฅ ํ•จ์ˆ˜์—์„œ ์ „๋‹ฌ ๋ฐ ๋ฐ˜ํ™˜๋˜๋Š” ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ์žˆ์œผ๋ฉฐ, ๊ทธ๋ผ๋””์–ธํŠธ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์ €์žฅ๋œ ์ˆœ๋ฐฉํ–ฅ ํ™œ์„ฑํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +**์ž„์‹œ ๋ฉ”๋ชจ๋ฆฌ** + +๋”๋ถˆ์–ด ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์ž„์‹œ ๋ณ€์ˆ˜๋Š” ์—ฐ์‚ฐ์ด ์™„๋ฃŒ๋˜๋ฉด ๊ณง๋ฐ”๋กœ ํ•ด์ œ๋˜์ง€๋งŒ, ๊ทธ ์ˆœ๊ฐ„์—๋Š” ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ๊ณ  OOM์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฝ”๋”ฉํ•  ๋•Œ ์ด๋Ÿฌํ•œ ์ž„์‹œ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์ „๋žต์ ์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๋•Œ๋กœ๋Š” ๋” ์ด์ƒ ํ•„์š” ์—†๋Š” ์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ์ฆ‰์‹œ ๋ช…์‹œ์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. + +**๊ธฐ๋Šฅ๋ณ„ ๋ฉ”๋ชจ๋ฆฌ** + +๊ทธ๋Ÿฐ ๋‹ค์Œ, ์†Œํ”„ํŠธ์›จ์–ด์—๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ ์‚ฌํ•ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋น” ๊ฒ€์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์†Œํ”„ํŠธ์›จ์–ด๋Š” ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์‚ฌ๋ณธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**`forward` vs `backward` ์‹คํ–‰ ์†๋„** + +ํ•ฉ์„ฑ๊ณฑ๊ณผ ์„ ํ˜• ๋ ˆ์ด์–ด์˜ ๊ฒฝ์šฐ ์ˆœ๋ฐฉํ–ฅ์— ๋น„ํ•ด ์—ญ๋ฐฉํ–ฅ์—์„œ๋Š” 2๋ฐฐ์˜ ํ”Œ๋กญ์Šค๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ 2๋ฐฐ ์ •๋„ ๋А๋ฆฌ๊ฒŒ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค(์—ญ๋ฐฉํ–ฅ์˜ ๊ฒฝ์šฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ถ€์ž์—ฐ์Šค๋Ÿฝ๊ธฐ ๋•Œ๋ฌธ์—, ๋•Œ๋กœ๋Š” ๋”์šฑ ๋А๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค). ํ™œ์„ฑํ™”๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋Œ€์—ญํญ์ด ์ œํ•œ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆœ๋ฐฉํ–ฅ๋ณด๋‹ค ์—ญ๋ฐฉํ–ฅ์—์„œ ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆœ๋ฐฉํ–ฅ ํ™œ์„ฑํ™” ์‹œ ํ•œ ๋ฒˆ ์”ฉ ์ฝ๊ณ  ์“ฐ์ง€๋งŒ, ์—ญ๋ฐฉํ–ฅ ํ™œ์„ฑํ™”์—์„œ๋Š” ์ˆœ๋ฐฉํ–ฅ gradOutput๊ณผ ์ถœ๋ ฅ์— ๋Œ€ํ•ด ์ด ๋‘ ๋ฒˆ ์ฝ๊ณ  gradInput์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ ์”๋‹ˆ๋‹ค.) + +๋ณด๋‹ค์‹œํ”ผ, GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ฑฐ๋‚˜ ์ž‘์—… ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. +์ด์ œ GPU ํ™œ์šฉ๊ณผ ๊ณ„์‚ฐ ์†๋„์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€๋ฅผ ์ดํ•ดํ–ˆ์œผ๋ฏ€๋กœ, [Methods and tools for efficient training on a single GPU](perf_train_gpu_one) ๋ฌธ์„œ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์„ธ์š”. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_sharing.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_sharing.md new file mode 100644 index 0000000000000000000000000000000000000000..934838c5ffe185dfba9ca1c3fddc06fd959d1d6e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/model_sharing.md @@ -0,0 +1,232 @@ + + +# ๋ชจ๋ธ ๊ณต์œ ํ•˜๊ธฐ[[share-a-model]] + +์ง€๋‚œ ๋‘ ํŠœํ† ๋ฆฌ์–ผ์—์„œ ๋ถ„์‚ฐ ์„ค์ •์„ ์œ„ํ•ด PyTorch, Keras ๋ฐ ๐Ÿค— Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๋ชจ๋ธ์„ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! Hugging Face๋Š” ์ธ๊ณต์ง€๋Šฅ์˜ ๋ฏผ์ฃผํ™”๋ฅผ ์œ„ํ•ด ๋ชจ๋‘์—๊ฒŒ ์ง€์‹๊ณผ ์ž์›์„ ๊ณต๊ฐœ์ ์œผ๋กœ ๊ณต์œ ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๋ฏฟ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์‹œ๊ฐ„๊ณผ ์ž์›์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด ๋ณด์„ธ์š”. + +์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ [Model Hub](https://huggingface.co/models)์—์„œ ํ›ˆ๋ จ๋˜๊ฑฐ๋‚˜ ๋ฏธ์„ธ ์กฐ์ • ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค: + +- API๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ Hub์— ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค. +- ์›น์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ Hub๋กœ ๋Œ์–ด๋‹ค ๋†“์Šต๋‹ˆ๋‹ค. + + + + + +์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๋ ค๋ฉด, [huggingface.co](https://huggingface.co/join)์— ๊ณ„์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ์กฐ์ง์— ๊ฐ€์ž…ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +## ์ €์žฅ์†Œ ํŠน์ง•[[repository-features]] + +๋ชจ๋ธ ํ—ˆ๋ธŒ์˜ ๊ฐ ์ €์žฅ์†Œ๋Š” ์ผ๋ฐ˜์ ์ธ GitHub ์ €์žฅ์†Œ์ฒ˜๋Ÿผ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ์†Œ๋Š” ๋ฒ„์ „ ๊ด€๋ฆฌ, ์ปค๋ฐ‹ ๊ธฐ๋ก, ์ฐจ์ด์  ์‹œ๊ฐํ™” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ ํ—ˆ๋ธŒ์— ๋‚ด์žฅ๋œ ๋ฒ„์ „ ๊ด€๋ฆฌ๋Š” git ๋ฐ [git-lfs](https://git-lfs.github.com/)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ•˜๋‚˜์˜ ๋ชจ๋ธ์„ ํ•˜๋‚˜์˜ ์ €์žฅ์†Œ๋กœ ์ทจ๊ธ‰ํ•˜์—ฌ ์ ‘๊ทผ ์ œ์–ด ๋ฐ ํ™•์žฅ์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋ฒ„์ „ ์ œ์–ด๋Š” ์ปค๋ฐ‹ ํ•ด์‹œ, ํƒœ๊ทธ ๋˜๋Š” ๋ธŒ๋žœ์น˜๋กœ ๋ชจ๋ธ์˜ ํŠน์ • ๋ฒ„์ „์„ ๊ณ ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ *revision*์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ `revision` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ชจ๋ธ ๋ฒ„์ „์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> model = AutoModel.from_pretrained( +... "julien-c/EsperBERTo-small", revision="4c77982" # tag name, or branch name, or commit hash +... ) +``` + +๋˜ํ•œ ์ €์žฅ์†Œ์—์„œ ํŒŒ์ผ์„ ์‰ฝ๊ฒŒ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ปค๋ฐ‹ ๊ธฐ๋ก๊ณผ ์ฐจ์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png) + +## ์„ค์ •[[setup]] + +๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜๊ธฐ ์ „์— Hugging Face ์ž๊ฒฉ ์ฆ๋ช…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ๐Ÿค— Transformers๊ฐ€ ์„ค์น˜๋œ ๊ฐ€์ƒ ํ™˜๊ฒฝ์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Hugging Face ์บ์‹œ ํด๋”(๊ธฐ๋ณธ์ ์œผ๋กœ `~/.cache/`)์— ์•ก์„ธ์Šค ํ† ํฐ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค: + +```bash +hf auth login +``` + +Jupyter ๋˜๋Š” Colaboratory์™€ ๊ฐ™์€ ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉ ์ค‘์ธ ๊ฒฝ์šฐ, [`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด API๋กœ ํ—ˆ๋ธŒ์™€ ์ƒํ˜ธ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +pip install huggingface_hub +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ `notebook_login`๋กœ ํ—ˆ๋ธŒ์— ๋กœ๊ทธ์ธํ•˜๊ณ , [์—ฌ๊ธฐ](https://huggingface.co/settings/token) ๋งํฌ์—์„œ ๋กœ๊ทธ์ธํ•  ํ† ํฐ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ„ ๋ชจ๋ธ ๋ณ€ํ™˜ํ•˜๊ธฐ[[convert-a-model-for-all-frameworks]] + +๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์ž‘์—…ํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ ค๋ฉด, PyTorch ๋ฐ TensorFlow ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ณ€ํ™˜ํ•˜๊ณ  ์—…๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋›ฐ์–ด๋„ ์‚ฌ์šฉ์ž๋Š” ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ๐Ÿค— Transformers๊ฐ€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ฆ‰์„์—์„œ ๋ณ€ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์†๋„๊ฐ€ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์Šต๋‹ˆ๋‹ค. PyTorch ๋ฐ TensorFlow๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ ๋‹ค์Œ(์„ค์น˜ ์ง€์นจ์€ [์—ฌ๊ธฐ](installation) ์ฐธ์กฐ) ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ž‘์—…์— ๋Œ€ํ•œ ํŠน์ • ๋ชจ๋ธ์„ ์ฐพ์Šต๋‹ˆ๋‹ค. + + + +์ฒดํฌํฌ์ธํŠธ๋ฅผ TensorFlow์—์„œ PyTorch๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด `from_tf=True`๋ฅผ ์ง€์ •ํ•˜์„ธ์š”: + +```py +>>> pt_model = DistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_tf=True) +>>> pt_model.save_pretrained("path/to/awesome-name-you-picked") +``` + + +์ฒดํฌํฌ์ธํŠธ๋ฅผ PyTorch์—์„œ TensorFlow๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด `from_pt=True`๋ฅผ ์ง€์ •ํ•˜์„ธ์š”: + +```py +>>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_pt=True) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์ƒˆ๋กœ์šด ์ฒดํฌํฌ์ธํŠธ์™€ ํ•จ๊ป˜ ์ƒˆ๋กœ์šด TensorFlow ๋ชจ๋ธ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> tf_model.save_pretrained("path/to/awesome-name-you-picked") +``` + + +Flax์—์„œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, PyTorch์—์„œ Flax๋กœ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( +... "path/to/awesome-name-you-picked", from_pt=True +... ) +``` + + + +## ํ›ˆ๋ จ ์ค‘ ๋ชจ๋ธ ํ‘ธ์‹œํ•˜๊ธฐ[[push-a-model-during-training]] + + + + + +๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์€ ์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋‚˜ ์ฝœ๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋งŒํผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. [๋ฏธ์„ธ ์กฐ์ • ํŠœํ† ๋ฆฌ์–ผ](training)์—์„œ [`TrainingArguments`] ํด๋ž˜์Šค๋Š” ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์ถ”๊ฐ€ ํ›ˆ๋ จ ์˜ต์…˜์„ ์ง€์ •ํ•˜๋Š” ๊ณณ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์„ธ์š”. ์ด๋Ÿฌํ•œ ํ›ˆ๋ จ ์˜ต์…˜ ์ค‘ ํ•˜๋‚˜๋Š” ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ๋กœ ์ง์ ‘ ํ‘ธ์‹œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. [`TrainingArguments`]์—์„œ `push_to_hub=True`๋ฅผ ์„ค์ •ํ•˜์„ธ์š”: + +```py +>>> training_args = TrainingArguments(output_dir="my-awesome-model", push_to_hub=True) +``` + +ํ‰์†Œ์™€ ๊ฐ™์ด ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ [`Trainer`]์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=small_train_dataset, +... eval_dataset=small_eval_dataset, +... compute_metrics=compute_metrics, +... ) +``` + +๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ํ›„, [`Trainer`]์—์„œ [`~transformers.Trainer.push_to_hub`]๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œํ•˜์„ธ์š”. ๐Ÿค— Transformers๋Š” ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ, ํ›ˆ๋ จ ๊ฒฐ๊ณผ ๋ฐ ํ”„๋ ˆ์ž„์›Œํฌ ๋ฒ„์ „์„ ๋ชจ๋ธ ์นด๋“œ์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค! + +```py +>>> trainer.push_to_hub() +``` + + +[`PushToHubCallback`]์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜๋ ค๋ฉด, [`PushToHubCallback`]์— ๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์ •์˜ํ•˜์„ธ์š”: + +- ์ถœ๋ ฅ๋œ ๋ชจ๋ธ์˜ ํŒŒ์ผ ๊ฒฝ๋กœ +- ํ† ํฌ๋‚˜์ด์ € +- `{Hub ์‚ฌ์šฉ์ž ์ด๋ฆ„}/{๋ชจ๋ธ ์ด๋ฆ„}` ํ˜•์‹์˜ `hub_model_id` + +```py +>>> from transformers import PushToHubCallback + +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="./your_model_save_path", tokenizer=tokenizer, hub_model_id="your-username/my-awesome-model" +... ) +``` + +[`fit`](https://keras.io/api/models/model_training_apis/)์— ์ฝœ๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ๐Ÿค— Transformers๊ฐ€ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback) +``` + + + +## `push_to_hub` ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ[[use-the-pushtohub-function]] + +๋ชจ๋ธ์—์„œ ์ง์ ‘ `push_to_hub`๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +`push_to_hub`์— ๋ชจ๋ธ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์„ธ์š”: + +```py +>>> pt_model.push_to_hub("my-awesome-model") +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์•„๋ž˜์— ๋ชจ๋ธ ์ด๋ฆ„ `my-awesome-model`๋กœ ์ €์žฅ์†Œ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์šฉ์ž๋Š” `from_pretrained` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModel + +>>> model = AutoModel.from_pretrained("your_username/my-awesome-model") +``` + +์กฐ์ง์— ์†ํ•˜๊ณ  ๋ชจ๋ธ์„ ์กฐ์ง ์ด๋ฆ„์œผ๋กœ ๋Œ€์‹  ํ‘ธ์‹œํ•˜๋ ค๋ฉด `repo_id`์— ์ถ”๊ฐ€ํ•˜์„ธ์š”: + +```py +>>> pt_model.push_to_hub("my-awesome-org/my-awesome-model") +``` + +`push_to_hub` ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ ์ €์žฅ์†Œ์— ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ชจ๋ธ ์ €์žฅ์†Œ์— ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> tokenizer.push_to_hub("my-awesome-model") +``` + +๋˜๋Š” ๋ฏธ์„ธ ์กฐ์ •๋œ PyTorch ๋ชจ๋ธ์˜ TensorFlow ๋ฒ„์ „์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> tf_model.push_to_hub("my-awesome-model") +``` + +์ด์ œ Hugging Face ํ”„๋กœํ•„๋กœ ์ด๋™ํ•˜๋ฉด, ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ๋ชจ๋ธ ์ €์žฅ์†Œ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. **Files** ํƒญ์„ ํด๋ฆญํ•˜๋ฉด ์ €์žฅ์†Œ์— ์—…๋กœ๋“œํ•œ ๋ชจ๋“  ํŒŒ์ผ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. + +์ €์žฅ์†Œ์— ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ—ˆ๋ธŒ ์„ค๋ช…์„œ [์—ฌ๊ธฐ](https://huggingface.co/docs/hub/how-to-upstream)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ์›น ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์—…๋กœ๋“œํ•˜๊ธฐ[[upload-with-the-web-interface]] + +์ฝ”๋“œ ์—†๋Š” ์ ‘๊ทผ ๋ฐฉ์‹์„ ์„ ํ˜ธํ•˜๋Š” ์‚ฌ์šฉ์ž๋Š” ํ—ˆ๋ธŒ์˜ ์›น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [huggingface.co/new](https://huggingface.co/new)๋ฅผ ๋ฐฉ๋ฌธํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ €์žฅ์†Œ๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + +![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png) + +์—ฌ๊ธฐ์„œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”: + +- ์ €์žฅ์†Œ์˜ **์†Œ์œ ์ž**๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‚ฌ์šฉ์ž ๋˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์†ํ•œ ์กฐ์ง์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ์ €์žฅ์†Œ ์ด๋ฆ„์ด ๋  ๋ชจ๋ธ์˜ ์ด๋ฆ„์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์ด ๊ณต๊ฐœ์ธ์ง€ ๋น„๊ณต๊ฐœ์ธ์ง€ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์˜ ๋ผ์ด์„ผ์Šค ์‚ฌ์šฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. + +์ด์ œ **Files** ํƒญ์„ ํด๋ฆญํ•˜๊ณ  **Add file** ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ €์žฅ์†Œ์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์—…๋กœ๋“œํ•  ํŒŒ์ผ์„ ๋Œ์–ด๋‹ค ๋†“๊ณ  ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. + +![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png) + +## ๋ชจ๋ธ ์นด๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ[[add-a-model-card]] + +์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ์˜ ๊ธฐ๋Šฅ, ์ œํ•œ, ์ž ์žฌ์  ํŽธํ–ฅ ๋ฐ ์œค๋ฆฌ์  ๊ณ ๋ ค ์‚ฌํ•ญ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์ €์žฅ์†Œ์— ๋ชจ๋ธ ์นด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. ๋ชจ๋ธ ์นด๋“œ๋Š” `README.md` ํŒŒ์ผ์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ชจ๋ธ ์นด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +* `README.md` ํŒŒ์ผ์„ ์ˆ˜๋™์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. +* ๋ชจ๋ธ ์ €์žฅ์†Œ์—์„œ **Edit model card** ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋ธ ์นด๋“œ์— ํฌํ•จํ•  ์ •๋ณด ์œ ํ˜•์— ๋Œ€ํ•œ ์ข‹์€ ์˜ˆ๋Š” DistilBert [๋ชจ๋ธ ์นด๋“œ](https://huggingface.co/distilbert/distilbert-base-uncased)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ๋ชจ๋ธ์˜ ํƒ„์†Œ ๋ฐœ์ž๊ตญ์ด๋‚˜ ์œ„์ ฏ ์˜ˆ์‹œ ๋“ฑ `README.md` ํŒŒ์ผ์—์„œ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ์˜ต์…˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์—ฌ๊ธฐ](https://huggingface.co/docs/hub/models-cards) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/modular_transformers.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/modular_transformers.md new file mode 100644 index 0000000000000000000000000000000000000000..096dd4f13a9889dad2c3ab0a6ca3c1308ab952b2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/modular_transformers.md @@ -0,0 +1,96 @@ +# ๋ชจ๋“ˆ์‹ ํŠธ๋žœ์Šคํฌ๋จธ [[modular-transformers]] + +`transformers`๋Š” opinionated(์ž๊ธฐ ์˜๊ฒฌ์ด ๊ฐ•ํ•œ) ํ”„๋ ˆ์ž„์›Œํฌ์ด๋ฉฐ, ์šฐ๋ฆฌ์˜ ์ฒ ํ•™์€ ๋‹ค์Œ์˜ [๊ฐœ๋… ๊ฐ€์ด๋“œ](./philosophy)์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ์ฒ ํ•™์˜ ํ•ต์‹ฌ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ [๋‹จ์ผ ๋ชจ๋ธ, ๋‹จ์ผ ํŒŒ์ผ](https://huggingface.co/blog/transformers-design-philosophy) ์ธก๋ฉด์—์„œ ์ž˜ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด ๊ตฌ์„ฑ ์š”์†Œ์˜ ๋‹จ์ ์€ ํŒŒ์ผ ๊ฐ„์— ๊ตฌ์„ฑ ์š”์†Œ์˜ ์ƒ์†๊ณผ ์ž„ํฌํŠธ ๊ฐ€๋Šฅ์„ฑ์„ ์ œํ•œํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๊ทธ ๊ฒฐ๊ณผ, ๋ชจ๋ธ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ๊ฑธ์ณ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. `transformers`์—๋Š” ๋ชจ๋ธ ์ˆ˜๋งŒํผ ๋งŽ์€ ์–ดํ…์…˜ ๋ ˆ์ด์–ด๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ทธ ์ค‘ ์ƒ๋‹น์ˆ˜๋Š” ์„œ๋กœ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์•ˆํƒ€๊น๊ฒŒ๋„, ์ˆ˜์ •๊ณผ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ฝ”๋“œ์˜ ํŠน์ • ๋ถ€๋ถ„์— ์ ์šฉ๋˜๋ฉด์„œ ๋…๋ฆฝ์ ์ธ ๊ตฌํ˜„๋“ค์ด ์„œ๋กœ ๋ถ„๊ธฐ๋˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ฌธ์ œ๋ฅผ ์ ์ ˆํžˆ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, ์šฐ๋ฆฌ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „์ฒด์— "๋ณต์‚ฌ๋ณธ"์˜ ๊ฐœ๋…์„ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ๋ณต์‚ฌ๋ณธ์ž„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ฃผ์„์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ, CI ๋ฐ ๋กœ์ปฌ ๋ช…๋ น์„ ํ†ตํ•ด ๋ณต์‚ฌ๋ณธ์ด ๋ถ„๊ธฐ๋˜์ง€ ์•Š๋„๋ก ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณต์žก์„ฑ์ด ๋‚ฎ๋”๋ผ๋„ ์ด๋Š” ์ข…์ข… ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์ž…๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ, ์ด ๋ฐฉ์‹์€ ์šฐ๋ฆฌ๊ฐ€ ์ค„์ด๊ณ ์ž ํ•˜๋Š” ์ƒ๋‹นํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ๋ชจ๋ธ ๊ธฐ์—ฌ ๊ณผ์ •์— ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ์ข…์ข… ๋ชจ๋ธ ๊ธฐ์—ฌ์— ๋ชจ๋ธ๋ง ์ฝ”๋“œ(~1,000์ค„), ํ”„๋กœ์„ธ์„œ(~500์ค„), ํ…Œ์ŠคํŠธ, ๋ฌธ์„œ ๋“ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๊ธฐ์—ฌ PR์€ ๋Œ€๋ถ€๋ถ„ 3,000~5,000์ค„ ์ด์ƒ์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉฐ, ์ด ์ค‘ ๋งŽ์€ ๋ถ€๋ถ„์ด ๋ณด์ผ๋Ÿฌํ”Œ๋ ˆ์ดํŠธ(boilerplate) ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. + +์ด๋Š” ๊ธฐ์—ฌ์˜ ์žฅ๋ฒฝ์„ ๋†’์ด๋ฉฐ, ๋ชจ๋“ˆ์‹ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์žฅ๋ฒฝ์„ ํ›จ์”ฌ ๋” ์ˆ˜์šฉ ๊ฐ€๋Šฅํ•œ ์ˆ˜์ค€์œผ๋กœ ๋‚ฎ์ถ”๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. + +## ๋ฌด์—‡์ธ๊ฐ€์š” [[what-is-it]] + +๋ชจ๋“ˆ์‹ ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๋ชจ๋ธ ํด๋”์— "๋ชจ๋“ˆ์‹" ํŒŒ์ผ์˜ ๊ฐœ๋…์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋“ˆ์‹ ํŒŒ์ผ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ๋ง/ํ”„๋กœ์„ธ์‹ฑ ํŒŒ์ผ์—์„œ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ์ฝ”๋“œ๋ฅผ ํ—ˆ์šฉํ•˜๋ฉฐ, ์ด๋Š” ์ธ์ ‘ํ•œ ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ์˜ ์ž„ํฌํŠธ์™€ ํด๋ž˜์Šค ๊ฐ„์˜ ์ƒ์†์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์ด ๋ชจ๋“ˆ์‹ ํŒŒ์ผ์€ ๊ฐ๊ฐ์˜ ๋ณ„๋„์˜ ๋ชจ๋“ˆ์—์„œ ์ •์˜๋˜์—ˆ์„ ๋ชจ๋ธ, ํ”„๋กœ์„ธ์„œ ๋ฐ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ, ์ด ๊ธฐ๋Šฅ์€ ๋ชจ๋“ˆ์‹ ํŒŒ์ผ์„ "ํ’€์–ด๋‚ด์–ด" ๋‹จ์ผ ๋ชจ๋ธ, ๋‹จ์ผ ํŒŒ์ผ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ƒˆ๋กœ์šด `linter`๋ฅผ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ๋“ค์€ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, ๊ธฐ์—ฌํ•ด์•ผ ํ•  ๋‚ด์šฉ์„ ๋ชจ๋“ˆ์‹ ํŒŒ์ผ, ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์—ฌ๋œ ๋ชจ๋ธ๊ณผ ๋‹ค๋ฅธ ๋ชจ๋ธ ๊ฐ„์˜ ์ฐจ์ด์ ์œผ๋กœ๋งŒ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. + +๋ชจ๋ธ ์‚ฌ์šฉ์ž๋Š” ๋‹จ์ผ ํŒŒ์ผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž„ํฌํŠธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ์—ฌ๊ธฐ์—๋Š” ๋ณ€ํ™”๊ฐ€ ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•œ ๊ธฐ์—ฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ฉด์„œ๋„ ์šฐ๋ฆฌ์˜ ์ฒ ํ•™์„ ์œ ์ง€ํ•˜๋Š” ์–‘์ชฝ์˜ ์žฅ์ ์„ ๊ฒฐํ•ฉํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ ์ด๋Š” `# Copied from` ๋งˆ์ปค์˜ ๋Œ€์ฒดํ’ˆ์ด๋ฉฐ, ์ด์ „์— ๊ธฐ์—ฌ๋œ ๋ชจ๋ธ์€ ์•ž์œผ๋กœ ๋ช‡ ๋‹ฌ ๋‚ด์— ์ƒˆ๋กœ์šด ๋ชจ๋“ˆ์‹ ํŠธ๋žœ์Šคํฌ๋จธ ํ˜•์‹์œผ๋กœ ์ „ํ™˜๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + +### ์ž์„ธํ•œ ๋‚ด์šฉ [[details]] + +โ€œlinterโ€๋Š” ์ƒ์† ๊ตฌ์กฐ๋ฅผ ํ’€์–ด์„œ ๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ชจ๋“  ๋‹จ์ผ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉฐ, Python ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ๋Š” ๊ทธ ๊ณผ์ •์ด ๋ณด์ด์ง€ ์•Š๋„๋ก ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ linter๋Š” **๋‹จ์ผ** ์ˆ˜์ค€์˜ ์ƒ์†๋งŒ์„ ํ‰ํƒ„ํ™”ํ•ฉ๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด: +- ๊ตฌ์„ฑ ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  ์ธ์ž๋ฅผ ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ, ์ƒ์„ฑ๋œ ํŒŒ์ผ์€ ์ง์ ‘ ์ฐธ์กฐ(์ถ”๊ฐ€์˜ ๊ฒฝ์šฐ)ํ•˜๊ฑฐ๋‚˜ ์™„์ „ํžˆ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค(์‚ญ์ œ์˜ ๊ฒฝ์šฐ). +- ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฒฝ์šฐ, ์˜ˆ๋ฅผ ๋“ค์–ด class GemmaModel(LlamaModel): ์˜ ๊ฒฝ์šฐ, ์ข…์†์„ฑ์ด ์ž๋™์œผ๋กœ ์ถ”๋ก ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์„œ๋ธŒ๋ชจ๋“ˆ์€ ์Šˆํผํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ ์ถ”๋ก ๋ฉ๋‹ˆ๋‹ค. + +ํ† ํฌ๋‚˜์ด์ €, ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ, ๋ชจ๋ธ, ๊ตฌ์„ฑ ๋“ฑ์„ ์ด `modular` ํŒŒ์ผ์— ๋ชจ๋‘ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•ด๋‹น ํŒŒ์ผ๋“ค์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. + +### ์‹œํ–‰ [[enforcement]] + +[TODO] ์šฐ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ํ…Œ์ŠคํŠธ๋ฅผ ๋„์ž…ํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ฝ˜ํ…์ธ ๊ฐ€ `modular_xxxx.py`์— ์žˆ๋Š” ๋‚ด์šฉ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. + +### ์˜ˆ์‹œ [[examples]] + +์—ฌ๊ธฐ BERT์™€ RoBERTa์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ชจ๋ธ์€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋ชจ๋ธ ๊ตฌํ˜„์˜ ์ฐจ์ด๋Š” ์ž„๋ฒ ๋”ฉ ๋ ˆ์ด์–ด์˜ ๋ณ€๊ฒฝ์—์„œ๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ์„ ์™„์ „ํžˆ ์žฌ์ •์˜ํ•˜๋Š” ๋Œ€์‹ , `modular_roberta.py` ํŒŒ์ผ์€ ๋ชจ๋ธ๋ง ๋ฐ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. (์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด, ํ† ํฌ๋‚˜์ด์ €๋Š” ๋งค์šฐ ๋‹ค๋ฅด๋ฏ€๋กœ ์ผ๋‹จ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.) + +```python +from torch import nn +from ..bert.configuration_bert import BertConfig +from ..bert.modeling_bert import ( + BertModel, + BertEmbeddings, + BertForMaskedLM +) + +# RoBERTa ๊ตฌ์„ฑ์€ BERT์˜ ๊ตฌ์„ฑ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค +class RobertaConfig(BertConfig): + model_type = 'roberta' + +# ์—ฌ๊ธฐ์„œ ํŒจ๋”ฉ ID ์ฐจ์ด๋ฅผ ๊ฐ•์กฐํ•˜๊ธฐ ์œ„ํ•ด ์ž„๋ฒ ๋”ฉ์„ ์žฌ์ •์˜ํ•˜๊ณ , ์œ„์น˜ ์ž„๋ฒ ๋”ฉ์„ ์žฌ์ •์˜ํ•ฉ๋‹ˆ๋‹ค +class RobertaEmbeddings(BertEmbeddings): + def __init__(self, config): + super().__init__(config()) + + self.padding_idx = config.pad_token_id + self.position_embeddings = nn.Embedding( + config.max_position_embeddings, config.hidden_size, padding_idx=self.padding_idx + ) + +# RoBERTa ๋ชจ๋ธ์€ ์ž„๋ฒ ๋”ฉ ๋ ˆ์ด์–ด๋ฅผ ์ œ์™ธํ•˜๋ฉด BERT ๋ชจ๋ธ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. +# ์œ„์—์„œ ์ž„๋ฒ ๋”ฉ์„ ์žฌ์ •์˜ํ–ˆ์œผ๋ฏ€๋กœ, ์—ฌ๊ธฐ์„œ๋Š” ์ถ”๊ฐ€ ์ž‘์—…์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค +class RobertaModel(BertModel): + def __init__(self, config): + super().__init__(config) + self.embeddings = RobertaEmbeddings(config) + +# ํ—ค๋“œ๋Š” ์ด์ œ ๋‚ด๋ถ€์—์„œ ์˜ฌ๋ฐ”๋ฅธ `RobertaModel`์„ ์žฌ์ •์˜ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค +class RobertaForMaskedLM(BertForMaskedLM): + def __init__(self, config): + super().__init__(config) + self.model = RobertaModel(config) +``` + +์ •์˜ํ•œ ์ข…์†์„ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค: + +```bash +ValueError: You defined `RobertaEmbeddings` in the modular_roberta.py, it should be used + when you define `BertModel`, as it is one of it's direct dependencies. Make sure + you use it in the `__init__` function. +``` + +๋˜ํ•œ, ๋‹ค์Œ์—์„œ ์˜ˆ์‹œ ๋ชฉ๋ก์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +## ๋ฌด์—‡์ด ์•„๋‹Œ๊ฐ€์š” [[what-it-is-not]] + +(์•„์ง์€?) ๋ชจ๋ธ๋ง ์ฝ”๋“œ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ชจ๋ธ์ด ์ง€๊ธˆ๊นŒ์ง€ ์กด์žฌํ–ˆ๋˜ ๋‹ค๋ฅธ ์–ด๋–ค ๊ฒƒ์—๋„ ๊ธฐ๋ฐ˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๊ธฐ์กด๊ณผ ๊ฐ™์ด `modeling` ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/optimizers.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/optimizers.md new file mode 100644 index 0000000000000000000000000000000000000000..64d5e4e2def83320c7c23970ad8664f09ec07559 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/optimizers.md @@ -0,0 +1,201 @@ + + +# ์˜ตํ‹ฐ๋งˆ์ด์ €[[optimizers]] + +Transformers๋Š” AdamW ๋ฐ AdaFactor์™€ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๊ธฐ๋ณธ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ณด๋‹ค ํŠนํ™”๋œ ์˜ตํ‹ฐ๋งˆ์ด์ €์™€์˜ ํ†ตํ•ฉ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ ํ›„, [`TrainingArguments`]์˜ `optim` ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•ด๋‹น ์˜ตํ‹ฐ๋งˆ์ด์ €๋ช…์„ ์ง€์ •ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์•„๋ž˜์— ์ œ์‹œ๋œ [`TrainingArguments`]์™€ ํ•จ๊ป˜ [`Trainer`]์—์„œ ์ด๋Ÿฌํ•œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. + +```py +import torch +from transformers import TrainingArguments, AutoTokenizer, AutoModelForCausalLM, Trainer + +args = TrainingArguments( + output_dir="./test-optimizer", + max_steps=1000, + per_device_train_batch_size=4, + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-5, + save_strategy="no", + run_name="optimizer-name", +) +``` + +## APOLLO[[apollo]] + +```bash +pip install apollo-torch +``` + +[Approximated Gradient Scaling for Memory Efficient LLM Optimization (APOLLO)](https://github.com/zhuhanqing/APOLLO) ๋Š” ์‚ฌ์ „ ํ•™์Šต๊ณผ ๋ฏธ์„ธ ์กฐ์ • ๋ชจ๋‘์— ๋Œ€ํ•ด ์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ ํ•™์Šต์„ ์ง€์›ํ•˜๋Š”, ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์˜ตํ‹ฐ๋งˆ์ด์ €์ž…๋‹ˆ๋‹ค. ์ด ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” SGD์™€ ์œ ์‚ฌํ•œ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์œผ๋กœ AdamW ์ˆ˜์ค€์˜ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ทนํ•œ์˜ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด APOLLO์˜ rank 1 ๋ณ€ํ˜•์ธ APOLLO-Mini๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. APOLLO ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +* ์ดˆ์ €๋žญํฌ(rank) ํšจ์œจ์„ฑ. [GaLoRE](./trainer#galore)๋ณด๋‹ค ํ›จ์”ฌ ๋‚ฎ์€ ๋žญํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋žญํฌ 1๋กœ๋„ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. +* ๊ณ ๋น„์šฉ SVD ์—ฐ์‚ฐ ํšŒํ”ผ. APOLLO๋Š” ํ•™์Šต ์ค‘๋‹จ(training stalls)์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์ž‘์œ„ ํˆฌ์˜(random projections)์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. + +ํ•™์Šตํ•  ๋ ˆ์ด์–ด๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด `optim_target_modules` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```diff +import torch +from transformers import TrainingArguments + +args = TrainingArguments( + output_dir="./test-apollo", + max_steps=100, + per_device_train_batch_size=2, ++ optim="apollo_adamw", ++ optim_target_modules=[r".*.attn.*", r".*.mlp.*"], + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-5, + save_strategy="no", + run_name="apollo_adamw", +) +``` + +์ถ”๊ฐ€์ ์ธ ํ•™์Šต ์˜ต์…˜์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, `optim_args`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `rank`, `scale` ๋“ฑ๊ณผ ๊ฐ™์€ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก์€ ์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +> [!TIP] +> `scale` ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” `n/r`์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, `n`์€ ์›๋ณธ ๊ณต๊ฐ„ ์ฐจ์›์ด๊ณ  `r`์€ ์ €๋žญํฌ(low-rank) ๊ณต๊ฐ„ ์ฐจ์›์ž…๋‹ˆ๋‹ค. `scale`์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ํ•™์Šต๋ฅ ๋งŒ ์กฐ์ •ํ•ด๋„ ๋น„์Šทํ•œ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +| ๋งค๊ฐœ ๋ณ€์ˆ˜ | ์„ค๋ช… | APOLLO | APOLLO-Mini | +|---|---|---|---| +| rank | ๊ทธ๋ž˜๋””์–ธํŠธ ์Šค์ผ€์ผ๋ง์„ ์œ„ํ•œ ๋ณด์กฐ ๋ถ€๋ถ„ ๊ณต๊ฐ„(sub-space)์˜ ๋žญํฌ | 256 | 1 | +| scale_type | ์Šค์ผ€์ผ๋ง ์ธ์ž(factor)๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• | `channel` (์ฑ„๋„๋ณ„ ์Šค์ผ€์ผ๋ง) | `tensor` (ํ…์„œ๋ณ„ ์Šค์ผ€์ผ๋ง) | +| scale | ๊ทธ๋ž˜๋””์–ธํŠธ ์—…๋ฐ์ดํŠธ๋ฅผ ์กฐ์ •ํ•˜์—ฌ ํ•™์Šต์„ ์•ˆ์ •ํ™” | 1.0 | 128 | +| update_proj_gap | ํˆฌ์˜ ํ–‰๋ ฌ(projection matrices)์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ „ ๋‹จ๊ณ„(step) ์ˆ˜ | 200 | 200 | +| proj | ํˆฌ์˜(projection) ์œ ํ˜• | `random` | `random` | + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” APOLLO-Mini ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + output_dir="./test-apollo_mini", + max_steps=100, + per_device_train_batch_size=2, + optim="apollo_adamw", + optim_target_modules=[r".*.attn.*", r".*.mlp.*"], + optim_args="proj=random,rank=1,scale=128.0,scale_type=tensor,update_proj_gap=200", +) +``` + +## GrokAdamW[[grokadamw]] + +```bash +pip install grokadamw +``` + +[GrokAdamW](https://github.com/cognitivecomputations/grokadamw)๋Š” *grokking* ํ˜„์ƒ(๊ธฐ์šธ๊ธฐ๊ฐ€ ์ฒœ์ฒœํžˆ ๋ณ€ํ™”ํ•ด ์ผ๋ฐ˜ํ™”๊ฐ€ ์ง€์—ฐ๋˜๋Š” ํ˜„์ƒ)์—์„œ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๋Š” ๋ชจ๋ธ๋“ค์—๊ฒŒ ์ ํ•ฉํ•˜๋„๋ก ์„ค๊ณ„๋œ ์˜ตํ‹ฐ๋งˆ์ด์ €์ž…๋‹ˆ๋‹ค. GrokAdamW๋Š” ๋” ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ๊ณผ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ๊ณ ๊ธ‰ ์ตœ์ ํ™” ๊ธฐ์ˆ ์ด ํ•„์š”ํ•œ ๋ชจ๋ธ์— ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```diff +import torch +from transformers import TrainingArguments + +args = TrainingArguments( + output_dir="./test-grokadamw", + max_steps=1000, + per_device_train_batch_size=4, ++ optim="grokadamw", + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-5, + save_strategy="no", + run_name="grokadamw", +) +``` + +## LOMO[[lomo]] + +```bash +pip install lomo-optim +``` + +[Low-Memory Optimization (LOMO)](https://github.com/OpenLMLab/LOMO)๋Š” LLM์˜ ์ „์ฒด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ œํ’ˆ๊ตฐ์ด๋ฉฐ, [LOMO](https://huggingface.co/papers/2306.09782)์™€ [AdaLomo](https://hf.co/papers/2310.10195) ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ LOMO ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๊ทธ๋ž˜๋””์–ธํŠธ ๊ณ„์‚ฐ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•œ ๋‹จ๊ณ„๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. AdaLomo๋Š” LOMO๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ, Adam ์˜ตํ‹ฐ๋งˆ์ด์ €์ฒ˜๋Ÿผ ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์ ์‘ํ˜• ํ•™์Šต๋ฅ ์„ ์ ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ๋” ๋‚˜์€ ์„ฑ๋Šฅ๊ณผ ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์œ„ํ•ด์„œ๋Š” `grad_norm` ์—†์ด AdaLomo๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +```diff +args = TrainingArguments( + output_dir="./test-lomo", + max_steps=1000, + per_device_train_batch_size=4, ++ optim="adalomo", + gradient_checkpointing=True, + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-6, + save_strategy="no", + run_name="adalomo", +) +``` + +## Schedule Free[[schedule-free]] + +```bash +pip install schedulefree +``` + +[Schedule Free optimizer (SFO)](https://hf.co/papers/2405.15682)๋Š” ๊ธฐ๋ณธ ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ๋ชจ๋ฉ˜ํ…€ ๋Œ€์‹  ํ‰๊ท ํ™”(averaging)์™€ ๋ณด๊ฐ„(interpolation)์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋•๋ถ„์— ๊ธฐ์กด์˜ ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„๋Ÿฌ์™€ ๋‹ฌ๋ฆฌ, SFO๋Š” ํ•™์Šต๋ฅ ์„ ์ ์ง„์ ์œผ๋กœ ๋‚ฎ์ถ”๋Š” ์ ˆ์ฐจ๊ฐ€ ์•„์˜ˆ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. + +SFO๋Š” RAdam(`schedule_free_radam`), AdamW(`schedule_free_adamw`), SGD(`schedule_free_sgd`) ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. RAdam ์Šค์ผ€์ค„๋Ÿฌ๋Š” `warmup_steps`๋‚˜ `warmup_ratio` ์„ค์ •์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์œผ๋กœ `lr_scheduler_type="constant"`๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ `lr_scheduler_type` ๊ฐ’๋„ ๋™์ž‘ํ•  ์ˆœ ์žˆ์œผ๋‚˜, SFO ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ๋‹ค๋ฅธ ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด SFO์˜ ์˜๋„๋œ ๋™์ž‘๊ณผ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```diff +args = TrainingArguments( + output_dir="./test-schedulefree", + max_steps=1000, + per_device_train_batch_size=4, ++ optim="schedule_free_radamw", ++ lr_scheduler_type="constant", + gradient_checkpointing=True, + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-6, + save_strategy="no", + run_name="sfo", +) +``` + +## StableAdamW[[stableadamw]] + +```bash +pip install torch-optimi +``` + +[StableAdamW](https://arxiv.org/pdf/2304.13013)๋Š” AdamW์™€ AdaFactor๋ฅผ ๊ฒฐํ•ฉํ•œ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์˜ตํ‹ฐ๋งˆ์ด์ €์ž…๋‹ˆ๋‹ค. AdaFactor์˜ ์—…๋ฐ์ดํŠธ ํด๋ฆฌํ•‘(update clipping)์ด AdamW์— ๋„์ž…๋˜์–ด ๋ณ„๋„์˜ ๊ทธ๋ž˜๋””์–ธํŠธ ํด๋ฆฌํ•‘(gradient clipping)์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์˜ ๋™์ž‘์—์„œ๋Š” AdamW์™€ ์™„๋ฒฝํžˆ ํ˜ธํ™˜๋˜๋Š” ๋Œ€์ฒด์ œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ๋ฐฐ์น˜(batch) ํฌ๊ธฐ๊ฐ€ ํฌ๊ฑฐ๋‚˜ ํ›ˆ๋ จ ์†์‹ค(training loss)์ด ๊ณ„์†ํ•ด์„œ ๊ธ‰๊ฒฉํ•˜๊ฒŒ ๋ณ€๋™ํ•œ๋‹ค๋ฉด, beta_2 ๊ฐ’์„ [0.95, 0.99] ์‚ฌ์ด๋กœ ์ค„์—ฌ๋ณด์„ธ์š”. + +```diff +args = TrainingArguments( + output_dir="./test-stable-adamw", + max_steps=1000, + per_device_train_batch_size=4, ++ optim="stable_adamw", + gradient_checkpointing=True, + logging_strategy="steps", + logging_steps=1, + learning_rate=2e-6, + save_strategy="no", + run_name="stable-adamw", +) +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pad_truncation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pad_truncation.md new file mode 100644 index 0000000000000000000000000000000000000000..9ee4dc839b8416f2114b0a12e7e86ada591926a6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pad_truncation.md @@ -0,0 +1,68 @@ + + +# ํŒจ๋”ฉ๊ณผ ์ž˜๋ผ๋‚ด๊ธฐ[[padding-and-truncation]] + +๋ฐฐ์น˜ ์ž…๋ ฅ์€ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ๊ณ ์ • ํฌ๊ธฐ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ๊ณผ ์ž˜๋ผ๋‚ด๊ธฐ๋Š” ๋‹ค์–‘ํ•œ ๊ธธ์ด์˜ ๋ฐฐ์น˜์—์„œ ์ง์‚ฌ๊ฐํ˜• ํ…์„œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์ „๋žต์ž…๋‹ˆ๋‹ค. ํŒจ๋”ฉ์€ ํŠน์ˆ˜ํ•œ **ํŒจ๋”ฉ ํ† ํฐ**์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์งง์€ ์‹œํ€€์Šค๊ฐ€ ๋ฐฐ์น˜์—์„œ ๊ฐ€์žฅ ๊ธด ์‹œํ€€์Šค ๋˜๋Š” ๋ชจ๋ธ์—์„œ ํ—ˆ์šฉํ•˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด์™€ ๋™์ผํ•œ ๊ธธ์ด๋ฅผ ๊ฐ–๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ž˜๋ผ๋‚ด๊ธฐ๋Š” ๊ธด ์‹œํ€€์Šค๋ฅผ ์ž˜๋ผ๋‚ด์–ด ํŒจ๋”ฉ๊ณผ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์‹œํ€€์Šค์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฐฐ์น˜์— ๊ฐ€์žฅ ๊ธด ์‹œํ€€์Šค์˜ ๊ธธ์ด๋กœ ํŒจ๋”ฉํ•˜๊ณ  ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๋Š” ๊ฒƒ์ด ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•„์š”ํ•˜๋‹ค๋ฉด API๊ฐ€ ์ง€์›ํ•˜๋Š” ๋” ๋งŽ์€ ์ „๋žต์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ์ธ์ˆ˜๋Š” `padding`, `truncation`, `max_length` ์„ธ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. + +`padding` ์ธ์ˆ˜๋Š” ํŒจ๋”ฉ์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ๋ฆฌ์–ธ ๋˜๋Š” ๋ฌธ์ž์—ด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + - `True` ๋˜๋Š” `'longest'`: ๋ฐฐ์น˜์—์„œ ๊ฐ€์žฅ ๊ธด ์‹œํ€€์Šค๋กœ ํŒจ๋”ฉํ•ฉ๋‹ˆ๋‹ค(๋‹จ์ผ ์‹œํ€€์Šค๋งŒ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ ํŒจ๋”ฉ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค). + - `'max_length'`: `max_length` ์ธ์ˆ˜๊ฐ€ ์ง€์ •ํ•œ ๊ธธ์ด๋กœ ํŒจ๋”ฉํ•˜๊ฑฐ๋‚˜, `max_length`๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ(`max_length=None`) ๋ชจ๋ธ์—์„œ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ์‹œํ€€์Šค๋งŒ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ํŒจ๋”ฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. + - `False` ๋˜๋Š” `'do_not_pad'`: ํŒจ๋”ฉ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ธฐ๋ณธ ๋™์ž‘์ž…๋‹ˆ๋‹ค. + +`truncation` ์ธ์ˆ˜๋Š” ์ž˜๋ผ๋‚ผ ๋ฐฉ๋ฒ•์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ๋ฆฌ์–ธ ๋˜๋Š” ๋ฌธ์ž์—ด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + - `True` ๋˜๋Š” `longest_first`: `max_length` ์ธ์ˆ˜๊ฐ€ ์ง€์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๊ฑฐ๋‚˜, + `max_length`๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ(`max_length=None`) ๋ชจ๋ธ์—์„œ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. + ์‹œํ€€์Šค ์Œ์—์„œ ๊ฐ€์žฅ ๊ธด ์‹œํ€€์Šค์˜ ํ† ํฐ์„ ์ ์ ˆํ•œ ๊ธธ์ด์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. + - `'only_second'`: `max_length` ์ธ์ˆ˜๊ฐ€ ์ง€์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๊ฑฐ๋‚˜, + `max_length`๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ(`max_length=None`) ๋ชจ๋ธ์—์„œ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. + ์‹œํ€€์Šค ์Œ(๋˜๋Š” ์‹œํ€€์Šค ์Œ์˜ ๋ฐฐ์น˜)๊ฐ€ ์ œ๊ณต๋œ ๊ฒฝ์šฐ ์Œ์˜ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ๋งŒ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. + - `'only_first'`: `max_length` ์ธ์ˆ˜๊ฐ€ ์ง€์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๊ฑฐ๋‚˜, + `max_length`๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ(`max_length=None`) ๋ชจ๋ธ์—์„œ ํ—ˆ์šฉ๋˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. + ์‹œํ€€์Šค ์Œ(๋˜๋Š” ์‹œํ€€์Šค ์Œ์˜ ๋ฐฐ์น˜)๊ฐ€ ์ œ๊ณต๋œ ๊ฒฝ์šฐ ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ๋งŒ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. + - `False` ๋˜๋Š” `'do_not_truncate'`: ์ž˜๋ผ๋‚ด๊ธฐ๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ธฐ๋ณธ ๋™์ž‘์ž…๋‹ˆ๋‹ค. + +`max_length` ์ธ์ˆ˜๋Š” ํŒจ๋”ฉ ๋ฐ ์ž˜๋ผ๋‚ด๊ธฐ๋ฅผ ์ ์šฉํ•  ๊ธธ์ด๋ฅผ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. ์ด ์ธ์ˆ˜๋Š” ์ •์ˆ˜ ๋˜๋Š” `None`์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, `None`์ผ ๊ฒฝ์šฐ ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ํŠน์ •ํ•œ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ `max_length`์— ๋Œ€ํ•œ ์ž˜๋ผ๋‚ด๊ธฐ ๋˜๋Š” ํŒจ๋”ฉ์ด ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. + +๋‹ค์Œ ํ‘œ์—๋Š” ํŒจ๋”ฉ ๋ฐ ์ž˜๋ผ๋‚ด๊ธฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ถŒ์žฅ ๋ฐฉ๋ฒ•์ด ์š”์•ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +์ž…๋ ฅ์œผ๋กœ ์‹œํ€€์Šค ์Œ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ ์˜ˆ์ œ์—์„œ `truncation=True`๋ฅผ `['only_first', 'only_second', 'longest_first']`์—์„œ ์„ ํƒํ•œ `STRATEGY`, ์ฆ‰ `truncation='only_second'` ๋˜๋Š” `truncation='longest_first'`๋กœ ๋ฐ”๊พธ๋ฉด ์•ž์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ ์Œ์˜ ๋‘ ์‹œํ€€์Šค๊ฐ€ ์ž˜๋ฆฌ๋Š” ๋ฐฉ์‹์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +| ์ž˜๋ผ๋‚ด๊ธฐ | ํŒจ๋”ฉ | ์‚ฌ์šฉ ๋ฐฉ๋ฒ• | +|--------------------------------------|-----------------------------------|------------------------------------------------------------------------------------------| +| ์ž˜๋ผ๋‚ด๊ธฐ ์—†์Œ | ํŒจ๋”ฉ ์—†์Œ | `tokenizer(batch_sentences)` | +| | ๋ฐฐ์น˜ ๋‚ด ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding=True)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, padding='longest')` | +| | ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding='max_length')` | +| | ํŠน์ • ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding='max_length', max_length=42)` | +| | ๋‹ค์–‘ํ•œ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding=True, pad_to_multiple_of=8)` | +| ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๊ธฐ | ํŒจ๋”ฉ ์—†์Œ | `tokenizer(batch_sentences, truncation=True)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, truncation=STRATEGY)` | +| | ๋ฐฐ์น˜ ๋‚ด ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding=True, truncation=True)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY)` | +| | ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding='max_length', truncation=True)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY)` | +| | ํŠน์ • ๊ธธ์ด๋กœ ํŒจ๋”ฉ | ์‚ฌ์šฉ ๋ถˆ๊ฐ€ | +| ํŠน์ • ๊ธธ์ด๋กœ ์ž˜๋ผ๋‚ด๊ธฐ | ํŒจ๋”ฉ ์—†์Œ | `tokenizer(batch_sentences, truncation=True, max_length=42)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, truncation=STRATEGY, max_length=42)` | +| | ๋ฐฐ์น˜ ๋‚ด ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding=True, truncation=True, max_length=42)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, padding=True, truncation=STRATEGY, max_length=42)` | +| | ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋กœ ํŒจ๋”ฉ | ์‚ฌ์šฉ ๋ถˆ๊ฐ€ | +| | ํŠน์ • ๊ธธ์ด๋กœ ํŒจ๋”ฉ | `tokenizer(batch_sentences, padding='max_length', truncation=True, max_length=42)` ๋˜๋Š” | +| | | `tokenizer(batch_sentences, padding='max_length', truncation=STRATEGY, max_length=42)` | diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/peft.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/peft.md new file mode 100644 index 0000000000000000000000000000000000000000..7655a2c6b587070dde15b3ff214623e5c48f789a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/peft.md @@ -0,0 +1,209 @@ + + +# ๐Ÿค— PEFT๋กœ ์–ด๋Œ‘ํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ [[load-adapters-with-peft]] + +[[open-in-colab]] + +[Parameter-Efficient Fine Tuning (PEFT)](https://huggingface.co/blog/peft) ๋ฐฉ๋ฒ•์€ ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฏธ์„ธ ์กฐ์ • ์ค‘ ๊ณ ์ •์‹œํ‚ค๊ณ , ๊ทธ ์œ„์— ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๋Š” ๋งค์šฐ ์ ์€ ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜(์–ด๋Œ‘ํ„ฐ)๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋Œ‘ํ„ฐ๋Š” ์ž‘์—…๋ณ„ ์ •๋ณด๋ฅผ ํ•™์Šตํ•˜๋„๋ก ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ์™„์ „ํžˆ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์— ํ•„์ ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ, ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ด๊ณ  ๋น„๊ต์  ์ ์€ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +๋˜ํ•œ PEFT๋กœ ํ›ˆ๋ จ๋œ ์–ด๋Œ‘ํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ „์ฒด ๋ชจ๋ธ๋ณด๋‹ค ํ›จ์”ฌ ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์œ , ์ €์žฅ ๋ฐ ๊ฐ€์ ธ์˜ค๊ธฐ๊ฐ€ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + +
+ +
Hub์— ์ €์žฅ๋œ OPTForCausalLM ๋ชจ๋ธ์˜ ์–ด๋Œ‘ํ„ฐ ๊ฐ€์ค‘์น˜๋Š” ์ตœ๋Œ€ 700MB์— ๋‹ฌํ•˜๋Š” ๋ชจ๋ธ ๊ฐ€์ค‘์น˜์˜ ์ „์ฒด ํฌ๊ธฐ์— ๋น„ํ•ด ์•ฝ 6MB์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค.
+
+ +๐Ÿค— PEFT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด [๋ฌธ์„œ](https://huggingface.co/docs/peft/index)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +## ์„ค์ • [[setup]] + +๐Ÿค— PEFT๋ฅผ ์„ค์น˜ํ•˜์—ฌ ์‹œ์ž‘ํ•˜์„ธ์š”: + +```bash +pip install peft +``` + +์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด, ๋‹ค์Œ ์†Œ์Šค์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + +```bash +pip install git+https://github.com/huggingface/peft.git +``` + +## ์ง€์›๋˜๋Š” PEFT ๋ชจ๋ธ [[supported-peft-models]] + +๐Ÿค— Transformers๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ๋ถ€ PEFT ๋ฐฉ๋ฒ•์„ ์ง€์›ํ•˜๋ฉฐ, ๋กœ์ปฌ์ด๋‚˜ Hub์— ์ €์žฅ๋œ ์–ด๋Œ‘ํ„ฐ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋งŒ์œผ๋กœ ์‰ฝ๊ฒŒ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฐฉ๋ฒ•์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค: + +- [Low Rank Adapters](https://huggingface.co/docs/peft/conceptual_guides/lora) +- [IA3](https://huggingface.co/docs/peft/conceptual_guides/ia3) +- [AdaLoRA](https://huggingface.co/papers/2303.10512) + +๐Ÿค— PEFT์™€ ๊ด€๋ จ๋œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•(์˜ˆ: ํ”„๋กฌํ”„ํŠธ ํ›ˆ๋ จ ๋˜๋Š” ํ”„๋กฌํ”„ํŠธ ํŠœ๋‹) ๋˜๋Š” ์ผ๋ฐ˜์ ์ธ ๐Ÿค— PEFT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด [๋ฌธ์„œ](https://huggingface.co/docs/peft/index)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + +## PEFT ์–ด๋Œ‘ํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ [[load-a-peft-adapter]] + +๐Ÿค— Transformers์—์„œ PEFT ์–ด๋Œ‘ํ„ฐ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‚ฌ์šฉํ•˜๋ ค๋ฉด Hub ์ €์žฅ์†Œ๋‚˜ ๋กœ์ปฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— `adapter_config.json` ํŒŒ์ผ๊ณผ ์–ด๋Œ‘ํ„ฐ ๊ฐ€์ค‘์น˜๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ `AutoModelFor` ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PEFT ์–ด๋Œ‘ํ„ฐ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ๊ณผ ๊ด€๊ณ„ ์–ธ์–ด ๋ชจ๋ธ์šฉ PEFT ์–ด๋Œ‘ํ„ฐ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฅด์‹ญ์‹œ์˜ค: + +1. PEFT ๋ชจ๋ธ ID๋ฅผ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค. +2. [`AutoModelForCausalLM`] ํด๋ž˜์Šค์— ์ „๋‹ฌํ•˜์‹ญ์‹œ์˜ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +peft_model_id = "ybelkada/opt-350m-lora" +model = AutoModelForCausalLM.from_pretrained(peft_model_id) +``` + + + +`AutoModelFor` ํด๋ž˜์Šค๋‚˜ ๊ธฐ๋ณธ ๋ชจ๋ธ ํด๋ž˜์Šค(์˜ˆ: `OPTForCausalLM` ๋˜๋Š” `LlamaForCausalLM`) ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PEFT ์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +`load_adapter` ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ PEFT ์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "facebook/opt-350m" +peft_model_id = "ybelkada/opt-350m-lora" + +model = AutoModelForCausalLM.from_pretrained(model_id) +model.load_adapter(peft_model_id) +``` + +## 8๋น„ํŠธ ๋˜๋Š” 4๋น„ํŠธ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ [[load-in-8bit-or-4bit]] + +`bitsandbytes` ํ†ตํ•ฉ์€ 8๋น„ํŠธ์™€ 4๋น„ํŠธ ์ •๋ฐ€๋„ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์ง€์›ํ•˜๋ฏ€๋กœ ํฐ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์œ ์šฉํ•˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ๋„ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ํ•˜๋“œ์›จ์–ด์— ํšจ๊ณผ์ ์œผ๋กœ ๋ถ„๋ฐฐํ•˜๋ ค๋ฉด [`~PreTrainedModel.from_pretrained`]์— `load_in_8bit` ๋˜๋Š” `load_in_4bit` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  `device_map="auto"`๋ฅผ ์„ค์ •ํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +peft_model_id = "ybelkada/opt-350m-lora" +model = AutoModelForCausalLM.from_pretrained(peft_model_id, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +## ์ƒˆ ์–ด๋Œ‘ํ„ฐ ์ถ”๊ฐ€ [[add-a-new-adapter]] + +์ƒˆ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ํ˜„์žฌ ์–ด๋Œ‘ํ„ฐ์™€ ๋™์ผํ•œ ์œ ํ˜•์ธ ๊ฒฝ์šฐ์— ํ•œํ•ด ๊ธฐ์กด ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์žˆ๋Š” ๋ชจ๋ธ์— ์ƒˆ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด [`~peft.PeftModel.add_adapter`]๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ชจ๋ธ์— ๊ธฐ์กด LoRA ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ: + +```py +from transformers import AutoModelForCausalLM, OPTForCausalLM, AutoTokenizer +from peft import PeftConfig + +model_id = "facebook/opt-350m" +model = AutoModelForCausalLM.from_pretrained(model_id) + +lora_config = LoraConfig( + target_modules=["q_proj", "k_proj"], + init_lora_weights=False +) + +model.add_adapter(lora_config, adapter_name="adapter_1") +``` + +์ƒˆ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด: + +```py +# attach new adapter with same config +model.add_adapter(lora_config, adapter_name="adapter_2") +``` + +์ด์ œ [`~peft.PeftModel.set_adapter`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์–ด๋Œ‘ํ„ฐ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +# use adapter_1 +model.set_adapter("adapter_1") +output = model.generate(**inputs) +print(tokenizer.decode(output_disabled[0], skip_special_tokens=True)) + +# use adapter_2 +model.set_adapter("adapter_2") +output_enabled = model.generate(**inputs) +print(tokenizer.decode(output_enabled[0], skip_special_tokens=True)) +``` + +## ์–ด๋Œ‘ํ„ฐ ํ™œ์„ฑํ™” ๋ฐ ๋น„ํ™œ์„ฑํ™” [[enable-and-disable-adapters]] + +๋ชจ๋ธ์— ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„ ์–ด๋Œ‘ํ„ฐ ๋ชจ๋“ˆ์„ ํ™œ์„ฑํ™” ๋˜๋Š” ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋Œ‘ํ„ฐ ๋ชจ๋“ˆ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด: + +```py +from transformers import AutoModelForCausalLM, OPTForCausalLM, AutoTokenizer +from peft import PeftConfig + +model_id = "facebook/opt-350m" +adapter_model_id = "ybelkada/opt-350m-lora" +tokenizer = AutoTokenizer.from_pretrained(model_id) +text = "Hello" +inputs = tokenizer(text, return_tensors="pt") + +model = AutoModelForCausalLM.from_pretrained(model_id) +peft_config = PeftConfig.from_pretrained(adapter_model_id) + +# to initiate with random weights +peft_config.init_lora_weights = False + +model.add_adapter(peft_config) +model.enable_adapters() +output = model.generate(**inputs) +``` + +์–ด๋Œ‘ํ„ฐ ๋ชจ๋“ˆ์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด: + +```py +model.disable_adapters() +output = model.generate(**inputs) +``` + +## PEFT ์–ด๋Œ‘ํ„ฐ ํ›ˆ๋ จ [[train-a-peft-adapter]] + +PEFT ์–ด๋Œ‘ํ„ฐ๋Š” [`Trainer`] ํด๋ž˜์Šค์—์„œ ์ง€์›๋˜๋ฏ€๋กœ ํŠน์ • ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ์–ด๋Œ‘ํ„ฐ๋ฅผ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด LoRA ์–ด๋Œ‘ํ„ฐ๋ฅผ ํ›ˆ๋ จํ•˜๋ ค๋ฉด: + + + +[`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด [์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ](training) ํŠœํ† ๋ฆฌ์–ผ์„ ํ™•์ธํ•˜์„ธ์š”. + + + +1. ์ž‘์—… ์œ ํ˜• ๋ฐ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์ •ํ•˜์—ฌ ์–ด๋Œ‘ํ„ฐ ๊ตฌ์„ฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [`~peft.LoraConfig`]๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +```py +from peft import LoraConfig + +peft_config = LoraConfig( + lora_alpha=16, + lora_dropout=0.1, + r=64, + bias="none", + task_type="CAUSAL_LM", +) +``` + +2. ๋ชจ๋ธ์— ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. + +```py +model.add_adapter(peft_config) +``` + +3. ์ด์ œ ๋ชจ๋ธ์„ [`Trainer`]์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +```py +trainer = Trainer(model=model, ...) +trainer.train() +``` + +ํ›ˆ๋ จํ•œ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด: + +```py +model.save_pretrained(save_dir) +model = AutoModelForCausalLM.from_pretrained(save_dir) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_hardware.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_hardware.md new file mode 100644 index 0000000000000000000000000000000000000000..01282a0c711147318121f71e07e1a638549906cc --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_hardware.md @@ -0,0 +1,156 @@ + + + +# ํ›ˆ๋ จ์šฉ ์‚ฌ์šฉ์ž ๋งž์ถคํ˜• ํ•˜๋“œ์›จ์–ด [[custom-hardware-for-training]] + +๋ชจ๋ธ ํ›ˆ๋ จ๊ณผ ์ถ”๋ก ์— ์‚ฌ์šฉํ•˜๋Š” ํ•˜๋“œ์›จ์–ด๋Š” ์„ฑ๋Šฅ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. GPU์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด, Tim Dettmer์˜ ํ›Œ๋ฅญํ•œ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•ด๋ณด์„ธ์š”. [๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ ๋งํฌ](https://timdettmers.com/2020/09/07/which-gpu-for-deep-learning/) (์˜์–ด๋กœ ์ž‘์„ฑ๋จ). + +GPU ์„ค์ •์— ๋Œ€ํ•œ ์‹ค์šฉ์ ์ธ ์กฐ์–ธ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## GPU [[gpu]] +๋” ํฐ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ฌ ๋•Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ธ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +- ๋” ํฐ GPU +- ๋” ๋งŽ์€ GPU +- ๋” ๋งŽ์€ CPU ๋ฐ NVMe ([DeepSpeed-Infinity](../en/main_classes/deepspeed#nvme-support)๋ฅผ ํ†ตํ•œ ์˜คํ”„๋กœ๋“œ(offload)) + +์šฐ์„ , ํ•˜๋‚˜์˜ GPU๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด๋ด…์‹œ๋‹ค. + +### ์ „์› ๊ณต๊ธ‰๊ณผ ๋ƒ‰๊ฐ [[power-and-cooling]] + +๋น„์‹ผ ๊ณ ์„ฑ๋Šฅ GPU๋ฅผ ๊ตฌ๋งคํ•œ ๊ฒฝ์šฐ, ์˜ฌ๋ฐ”๋ฅธ ์ „์› ๊ณต๊ธ‰๊ณผ ์ถฉ๋ถ„ํ•œ ๋ƒ‰๊ฐ์„ ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**์ „์› ๊ณต๊ธ‰**: + +์ผ๋ถ€ ๊ณ ์„ฑ๋Šฅ ์†Œ๋น„์ž์šฉ GPU๋Š” 2๊ฐœ ํ˜น์€ ๊ฐ€๋”๊ฐ€๋‹ค 3๊ฐœ์˜ PCI-E 8ํ•€ ์ „์› ์†Œ์ผ“์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์นด๋“œ์— ์žˆ๋Š” ์†Œ์ผ“ ์ˆ˜๋งŒํผ ๋…๋ฆฝ์ ์ธ 12V PCI-E 8ํ•€ ์ผ€์ด๋ธ”์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ๊ฐ™์€ ์ผ€์ด๋ธ”์˜ ํ•œ์ชฝ ๋์— ์žˆ๋Š” 2๊ฐœ์˜ ์Šคํ”Œ๋ฆฟ(๋˜๋Š” ํ”ผ๊ทธํ…Œ์ผ(pigtail) ์ผ€์ด๋ธ”)์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. ์ฆ‰, GPU์— 2๊ฐœ์˜ ์†Œ์ผ“์ด ์žˆ๋‹ค๋ฉด, PSU(์ „์› ๊ณต๊ธ‰ ์žฅ์น˜)์—์„œ ์นด๋“œ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” 2๊ฐœ์˜ PCI-E 8ํ•€ ์ผ€์ด๋ธ”์ด ํ•„์š”ํ•˜๋ฉฐ, ๋์— 2๊ฐœ์˜ PCI-E 8ํ•€ ์ปค๋„ฅํ„ฐ๊ฐ€ ์žˆ๋Š” ์ผ€์ด๋ธ”์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค! ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์นด๋“œ์˜ ์ „์ฒด ์„ฑ๋Šฅ์„ ์ œ๋Œ€๋กœ ๋ฐœํœ˜ํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฐ๊ฐ์˜ PCI-E 8ํ•€ ์ „์› ์ผ€์ด๋ธ”์€ PSU ์ชฝ์˜ 12V ๋ ˆ์ผ์— ์—ฐ๊ฒฐ๋˜์–ด์•ผ ํ•˜๋ฉฐ ์ตœ๋Œ€ 150W์˜ ์ „๋ ฅ์„ ๊ณต๊ธ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ผ๋ถ€ ๋‹ค๋ฅธ GPU๋Š” PCI-E 12ํ•€ ์ปค๋„ฅํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ์ปค๋„ฅํ„ฐ๋Š” ์ตœ๋Œ€ 500W-600W์˜ ์ „๋ ฅ์„ ๊ณต๊ธ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ €๊ฐ€ํ˜• GPU๋Š” 6ํ•€ ์ปค๋„ฅํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ตœ๋Œ€ 75W์˜ ์ „๋ ฅ์„ ๊ณต๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. + +๋˜ํ•œ GPU๊ฐ€ ์•ˆ์ •์ ์ธ ์ „์••์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๊ณ ๊ธ‰ PSU๋ฅผ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ €ํ’ˆ์งˆ์˜ PSU๋Š” GPU๊ฐ€ ์ตœ๊ณ  ์„ฑ๋Šฅ์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ „์••์„ ์•ˆ์ •์ ์œผ๋กœ ๊ณต๊ธ‰ํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฌผ๋ก , PSU๋Š” GPU์— ์ „์›์„ ๊ณต๊ธ‰ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•œ ์—ฌ๋ถ„์˜ ์ „๋ ฅ ์šฉ๋Ÿ‰์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**๋ƒ‰๊ฐ**: + +GPU๊ฐ€ ๊ณผ์—ด๋˜๋ฉด ์„ฑ๋Šฅ์ด ์ €ํ•˜๋˜๊ณ  ์ตœ๋Œ€ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋„ˆ๋ฌด ๋œจ๊ฑฐ์›Œ์ง€๋ฉด ์ค‘์ง€๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +GPU๊ฐ€ ๊ณผ์—ด๋  ๋•Œ ์ •ํ™•ํ•œ ์ ์ • ์˜จ๋„๋ฅผ ์•Œ๊ธฐ ์–ด๋ ค์šฐ๋‚˜, ์•„๋งˆ๋„ +80โ„ƒ ๋ฏธ๋งŒ์ด๋ฉด ์ข‹์ง€๋งŒ ๋” ๋‚ฎ์„์ˆ˜๋ก ์ข‹์Šต๋‹ˆ๋‹ค. 70โ„ƒ-75โ„ƒ ์ •๋„๊ฐ€ ํ›Œ๋ฅญํ•œ ์˜จ๋„ ๋ฒ”์œ„์ž…๋‹ˆ๋‹ค. ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ์˜จ๋„๋Š” ๋Œ€๋žต 84โ„ƒ-90โ„ƒ ์ •๋„์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„ฑ๋Šฅ ์ €ํ•˜ ์ด์™ธ์—๋„ ์ง€์†์ ์œผ๋กœ ๋งค์šฐ ๋†’์€ ์˜จ๋„๋Š” GPU ์ˆ˜๋ช…์„ ๋‹จ์ถ•์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด์–ด์„œ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ธก๋ฉด ์ค‘ ํ•˜๋‚˜์ธ GPU ๊ฐ„ ์—ฐ๊ฒฐ ๋ฐฉ์‹์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +### ๋‹ค์ค‘ GPU ์—ฐ๊ฒฐ ๋ฐฉ์‹ [[multigpu-connectivity]] + +๋‹ค์ค‘ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ GPU ๊ฐ„์˜ ์—ฐ๊ฒฐ ๋ฐฉ์‹์€ ์ „์ฒด ํ›ˆ๋ จ ์‹œ๊ฐ„์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ GPU๊ฐ€ ๋™์ผํ•œ ๋ฌผ๋ฆฌ์  ๋…ธ๋“œ์— ์žˆ์„ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +nvidia-smi topo -m +``` + +๋งŒ์•ฝ NVLink๋กœ ์—ฐ๊ฒฐ๋œ ๋“€์–ผ GPU ํ™˜๊ฒฝ์ด๋ผ๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X NV2 0-23 N/A +GPU1 NV2 X 0-23 N/A +``` + +NVLink๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋‹ค๋ฅธ ํ™˜๊ฒฝ์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +``` + GPU0 GPU1 CPU Affinity NUMA Affinity +GPU0 X PHB 0-11 N/A +GPU1 PHB X 0-11 N/A +``` + +์ด ๊ฒฐ๊ณผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ”๋ก€๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: + +``` + X = Self + SYS = Connection traversing PCIe as well as the SMP interconnect between NUMA nodes (e.g., QPI/UPI) + NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node + PHB = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU) + PXB = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge) + PIX = Connection traversing at most a single PCIe bridge + NV# = Connection traversing a bonded set of # NVLinks +``` + +๋”ฐ๋ผ์„œ ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ์˜ `NV2`๋Š” GPU๊ฐ€ 2๊ฐœ์˜ NVLink๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๊ณ , ๋‘ ๋ฒˆ์งธ ๊ฒฐ๊ณผ์˜ `PHB`๋Š” ์ผ๋ฐ˜์ ์ธ ์†Œ๋น„์ž์šฉ PCIe+๋ธŒ๋ฆฟ์ง€ ์„ค์ •์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + +์„ค์ •์—์„œ ์–ด๋–ค ์œ ํ˜•์˜ ์—ฐ๊ฒฐ ๋ฐฉ์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์ผ๋ถ€ ์—ฐ๊ฒฐ ๋ฐฉ์‹์€ GPU ๊ฐ„ ํ†ต์‹ ์„ ๋” ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ(NVLink์™€ ๊ฐ™์ด), ์–ด๋–ค ์—ฐ๊ฒฐ ๋ฐฉ์‹์€ ๋” ๋А๋ฆฌ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(PHB์™€ ๊ฐ™์ด). + +์‚ฌ์šฉํ•˜๋Š” ํ™•์žฅ์„ฑ ์†”๋ฃจ์…˜์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ์ฃผ์š”ํ•œ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜๋„ ์žˆ๊ณ  ๋ฏธ๋ฏธํ•œ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. DDP์™€ ๊ฐ™์ด GPU๊ฐ€ ๊ฑฐ์˜ ๋™๊ธฐํ™”ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒฝ์šฐ, ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ๋А๋ ค๋„ ํฐ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ZeRO-DP์™€ ๊ฐ™์ด GPU๊ฐ„ ํ†ต์‹ ์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ๋” ๋น ๋ฅธ ํ›ˆ๋ จ์„ ์œ„ํ•ด์„œ๋Š” ๋” ๋น ๋ฅธ ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. + +#### NVLink [[nvlink]] + +[NVLink](https://en.wikipedia.org/wiki/NVLink)๋Š” Nvidia์—์„œ ๊ฐœ๋ฐœํ•œ ์œ ์„  ๊ธฐ๋ฐ˜์˜ ์ง๋ ฌ ๋‹ค์ค‘ ๋ ˆ์ธ ๊ทผ๊ฑฐ๋ฆฌ ํ†ต์‹  ๋งํฌ์ž…๋‹ˆ๋‹ค. + +์ƒˆ๋กœ์šด ์„ธ๋Œ€์˜ NVLink๋Š” ๋” ๋น ๋ฅธ ๋Œ€์—ญํญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. [Nvidia Ampere GA102 GPU Architecture](https://www.nvidia.com/content/dam/en-zz/Solutions/geforce/ampere/pdf/NVIDIA-ampere-GA102-GPU-Architecture-Whitepaper-V1.pdf)์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +> 3์„ธ๋Œ€ NVLinkยฎ +> GA102 GPU๋Š” 4๊ฐœ์˜ x4 ๋งํฌ๋ฅผ ํฌํ•จํ•˜๋Š” NVIDIA์˜ 3์„ธ๋Œ€ NVLink ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•˜๋ฉฐ, +> ๊ฐ ๋งํฌ๋Š” ๋‘ ๊ฐœ์˜ GPU ๊ฐ„์— ๊ฐ ๋ฐฉํ–ฅ์œผ๋กœ ์ดˆ๋‹น 14.0625GB์˜ ๋Œ€์—ญํญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +> 4๊ฐœ์˜ ๋งํฌ๋Š” ๊ฐ ๋ฐฉํ–ฅ์— ์ดˆ๋‹น 56.25GB์˜ ๋Œ€์—ญํญ์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋‘ ๊ฐœ์˜ GPU ๊ฐ„์—๋Š” ์ดˆ๋‹น 112.5GB์˜ ์ด ๋Œ€์—ญํญ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +> ๋‘ ๊ฐœ์˜ RTX 3090 GPU๋ฅผ NVLink๋ฅผ ์‚ฌ์šฉํ•ด SLI๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +> (3-Way ๋ฐ 4-Way SLI ๊ตฌ์„ฑ์€ ์ง€์›๋˜์ง€ ์•Š์Œ์— ์œ ์˜ํ•˜์„ธ์š”.) + + +๋”ฐ๋ผ์„œ `nvidia-smi topo -m`์˜ ๊ฒฐ๊ณผ์—์„œ `NVX`์˜ ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ๋” ์ข‹์Šต๋‹ˆ๋‹ค. ์„ธ๋Œ€๋Š” GPU ์•„ํ‚คํ…์ฒ˜์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋ ‡๋‹ค๋ฉด, openai-community/gpt2๋ฅผ ์ž‘์€ wikitext ์ƒ˜ํ”Œ๋กœ ํ•™์Šต์‹œํ‚ค๋Š” ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด, NVLink๊ฐ€ ํ›ˆ๋ จ์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + + +| NVlink | Time | +| ----- | ---: | +| Y | 101s | +| N | 131s | + + +NVLink ์‚ฌ์šฉ ์‹œ ํ›ˆ๋ จ์ด ์•ฝ 23% ๋” ๋น ๋ฅด๊ฒŒ ์™„๋ฃŒ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฒค์น˜๋งˆํฌ์—์„œ๋Š” `NCCL_P2P_DISABLE=1`์„ ์‚ฌ์šฉํ•˜์—ฌ NVLink๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. + +์ „์ฒด ๋ฒค์น˜๋งˆํฌ ์ฝ”๋“œ์™€ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```bash +# DDP w/ NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train \ +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 101.9003, 'train_samples_per_second': 1.963, 'epoch': 0.69} + +# DDP w/o NVLink + +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 NCCL_P2P_DISABLE=1 torchrun \ +--nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py --model_name_or_path openai-community/gpt2 \ +--dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --do_train +--output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 131.4367, 'train_samples_per_second': 1.522, 'epoch': 0.69} +``` + +ํ•˜๋“œ์›จ์–ด: ๊ฐ๊ฐ 2๊ฐœ์˜ TITAN RTX 24GB + 2๊ฐœ์˜ NVLink (`NV2` in `nvidia-smi topo -m`) +์†Œํ”„ํŠธ์›จ์–ด: `pytorch-1.8-to-be` + `cuda-11.0` / `transformers==4.3.0.dev0` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..123e56b4f32c2fdf6d90f7e127c74a0a3803d5ea --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_cpu.md @@ -0,0 +1,73 @@ + + +# CPU์—์„œ ํšจ์œจ์ ์ธ ์ถ”๋ก ํ•˜๊ธฐ [[efficient-inference-on-cpu]] + +์ด ๊ฐ€์ด๋“œ๋Š” CPU์—์„œ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ํšจ์œจ์ ์œผ๋กœ ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ค‘์ ์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๋” ๋น ๋ฅธ ์ถ”๋ก ์„ ์œ„ํ•œ `BetterTransformer` [[bettertransformer-for-faster-inference]] + +์šฐ๋ฆฌ๋Š” ์ตœ๊ทผ CPU์—์„œ ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€ ๋ฐ ์˜ค๋””์˜ค ๋ชจ๋ธ์˜ ๋น ๋ฅธ ์ถ”๋ก ์„ ์œ„ํ•ด `BetterTransformer`๋ฅผ ํ†ตํ•ฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ†ตํ•ฉ์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ด ๋ฌธ์„œ](https://huggingface.co/docs/optimum/bettertransformer/overview)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## PyTorch JIT ๋ชจ๋“œ (TorchScript) [[pytorch-jitmode-torchscript]] +TorchScript๋Š” PyTorch ์ฝ”๋“œ์—์„œ ์ง๋ ฌํ™”์™€ ์ตœ์ ํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ์„ ์ƒ์„ฑํ• ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. TorchScript๋กœ ๋งŒ๋“ค์–ด์ง„ ํ”„๋กœ๊ทธ๋žจ์€ ๊ธฐ์กด Python ํ”„๋กœ์„ธ์Šค์—์„œ ์ €์žฅํ•œ ๋’ค, ์ข…์†์„ฑ์ด ์—†๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PyTorch์˜ ๊ธฐ๋ณธ ์„ค์ •์ธ `eager` ๋ชจ๋“œ์™€ ๋น„๊ตํ–ˆ์„๋•Œ, `jit` ๋ชจ๋“œ๋Š” ์—ฐ์‚ฐ์ž ๊ฒฐํ•ฉ๊ณผ ๊ฐ™์€ ์ตœ์ ํ™” ๋ฐฉ๋ฒ•๋ก ์„ ํ†ตํ•ด ๋ชจ๋ธ ์ถ”๋ก ์—์„œ ๋Œ€๋ถ€๋ถ„ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +TorchScript์— ๋Œ€ํ•œ ์นœ์ ˆํ•œ ์†Œ๊ฐœ๋Š” [PyTorch TorchScript ํŠœํ† ๋ฆฌ์–ผ](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html#tracing-modules)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +### JIT ๋ชจ๋“œ์™€ ํ•จ๊ป˜ํ•˜๋Š” IPEX ๊ทธ๋ž˜ํ”„ ์ตœ์ ํ™” [[ipex-graph-optimization-with-jitmode]] +Intelยฎ Extension for PyTorch(IPEX)๋Š” Transformers ๊ณ„์—ด ๋ชจ๋ธ์˜ jit ๋ชจ๋“œ์—์„œ ์ถ”๊ฐ€์ ์ธ ์ตœ์ ํ™”๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. jit ๋ชจ๋“œ์™€ ๋”๋ถˆ์–ด Intelยฎ Extension for PyTorch(IPEX)๋ฅผ ํ™œ์šฉํ•˜์‹œ๊ธธ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค. Transformers ๋ชจ๋ธ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ถ€ ์—ฐ์‚ฐ์ž ํŒจํ„ด์€ ์ด๋ฏธ jit ๋ชจ๋“œ ์—ฐ์‚ฐ์ž ๊ฒฐํ•ฉ(operator fusion)์˜ ํ˜•ํƒœ๋กœ Intelยฎ Extension for PyTorch(IPEX)์—์„œ ์ง€์›๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Multi-head-attention, Concat Linear, Linear+Add, Linear+Gelu, Add+LayerNorm ๊ฒฐํ•ฉ ํŒจํ„ด ๋“ฑ์ด ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ํ™œ์šฉํ–ˆ์„ ๋•Œ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ๊ฒฐํ•ฉ์˜ ์ด์ ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ณ ์Šค๋ž€ํžˆ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋ถ„์„์— ๋”ฐ๋ฅด๋ฉด, ์งˆ์˜ ์‘๋‹ต, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ฐ ํ† ํฐ ๋ถ„๋ฅ˜์™€ ๊ฐ™์€ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” NLP ํƒœ์Šคํฌ ์ค‘ ์•ฝ 70%๊ฐ€ ์ด๋Ÿฌํ•œ ๊ฒฐํ•ฉ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ Float32 ์ •๋ฐ€๋„์™€ BFloat16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ๋ชจ๋‘์—์„œ ์„ฑ๋Šฅ์ƒ์˜ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[IPEX ๊ทธ๋ž˜ํ”„ ์ตœ์ ํ™”](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/graph_optimization.html)์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +#### IPEX ์„ค์น˜: [[ipex-installation]] + +IPEX ๋ฐฐํฌ ์ฃผ๊ธฐ๋Š” PyTorch๋ฅผ ๋”ฐ๋ผ์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” [IPEX ์„ค์น˜ ๋ฐฉ๋ฒ•](https://intel.github.io/intel-extension-for-pytorch/)์„ ํ™•์ธํ•˜์„ธ์š”. + +### JIT ๋ชจ๋“œ ์‚ฌ์šฉ๋ฒ• [[usage-of-jitmode]] +ํ‰๊ฐ€ ๋˜๋Š” ์˜ˆ์ธก์„ ์œ„ํ•ด Trainer์—์„œ JIT ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Trainer์˜ ๋ช…๋ น ์ธ์ˆ˜์— `jit_mode_eval`์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +PyTorch์˜ ๋ฒ„์ „์ด 1.14.0 ์ด์ƒ์ด๋ผ๋ฉด, jit ๋ชจ๋“œ๋Š” jit.trace์—์„œ dict ์ž…๋ ฅ์ด ์ง€์›๋˜๋ฏ€๋กœ, ๋ชจ๋“  ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ณผ ํ‰๊ฐ€๊ฐ€ ๊ฐœ์„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +PyTorch์˜ ๋ฒ„์ „์ด 1.14.0 ๋ฏธ๋งŒ์ด๋ผ๋ฉด, ์งˆ์˜ ์‘๋‹ต ๋ชจ๋ธ๊ณผ ๊ฐ™์ด forward ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๊ฐ€ jit.trace์˜ ํŠœํ”Œ ์ž…๋ ฅ ์ˆœ์„œ์™€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋ธ์— ๋“์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ชจ๋ธ๊ณผ ๊ฐ™์ด forward ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆœ์„œ๊ฐ€ jit.trace์˜ ํŠœํ”Œ ์ž…๋ ฅ ์ˆœ์„œ์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ, jit.trace๊ฐ€ ์‹คํŒจํ•˜๋ฉฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ˆ์™ธ์ƒํ™ฉ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด Logging์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + + +[Transformers ์งˆ์˜ ์‘๋‹ต](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์˜ ์‚ฌ์šฉ ์‚ฌ๋ก€ ์˜ˆ์‹œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + +- CPU์—์„œ jit ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ถ”๋ก : +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--jit_mode_eval 
+ +- CPU์—์„œ IPEX์™€ ํ•จ๊ป˜ jit ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ถ”๋ก : +
python run_qa.py \
+--model_name_or_path csarron/bert-base-uncased-squad-v1 \
+--dataset_name squad \
+--do_eval \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/ \
+--no_cuda \
+--use_ipex \
+--jit_mode_eval
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_multi.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_multi.md new file mode 100644 index 0000000000000000000000000000000000000000..3d66b7398a2af223a90277c0f952b8ce2cdd08e5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_multi.md @@ -0,0 +1,311 @@ + + +# ๋ถ„์‚ฐ ์ถ”๋ก [[distributed-inference]] + +๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, [ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ](./perf_train_gpu_many#tensor-parallelism)๋ฅผ ์‚ฌ์šฉํ•œ ๋ถ„์‚ฐ ์ถ”๋ก ์ด ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์„œ ๋ณ‘๋ ฌํ™”๋Š” ๋ชจ๋ธ์„ ์—ฌ๋Ÿฌ ๊ฐ€์†๊ธฐ(CUDA GPU, Intel XPU ๋“ฑ)์— ๋ถ„ํ• ํ•˜์—ฌ ํ–‰๋ ฌ ๊ณฑ์…ˆ๊ณผ ๊ฐ™์€ ๊ณ„์‚ฐ์„ ๋ณ‘๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋” ํฐ ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ ๊ฐ€์†๊ธฐ๊ฐ€ ํ…์„œ์˜ ์ผ๋ถ€๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์ถ”๋ก  ์†๋„๊ฐ€ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฌ๋‚˜ ํ…์„œ ๋ณ‘๋ ฌํ™”๋Š” ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฏ€๋กœ, ๋น ๋ฅธ ๋…ธ๋“œ ๋‚ด ํ†ต์‹ ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์ค‘ ๊ฐ€์†๊ธฐ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ๋…ธ๋“œ ํ•™์Šต ํ™˜๊ฒฝ์—์„œ๋Š” ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋”ฐ๋ผ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌํ™”๋‚˜ ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ํ…์„œ ๋ณ‘๋ ฌํ™”์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด [Ultra-Scale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=tensor_parallelism)์˜ ํ…์„œ ๋ณ‘๋ ฌํ™” ์„น์…˜์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +์•„๋ž˜ ๋ชฉ๋ก์—์„œ ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ง€์›์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด GitHub ์ด์Šˆ๋‚˜ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. + +
+์ง€์›๋˜๋Š” ๋ชจ๋ธ ๋ณด๊ธฐ + +* [Cohere](./model_doc/cohere) ๋ฐ [Cohere 2](./model_doc/cohere2) +* [Gemma](./model_doc/gemma) ๋ฐ [Gemma 2](./model_doc/gemma2) +* [GLM](./model_doc/glm) +* [Granite](./model_doc/granite) +* [Llama](./model_doc/llama) +* [Mistral](./model_doc/mistral) +* [Mixtral](./model_doc/mixtral) +* [OLMo](./model_doc/olmo) ๋ฐ [OLMo2](./model_doc/olmo2) +* [Phi](./model_doc/phi) ๋ฐ [Phi-3](./model_doc/phi3) +* [Qwen2](./model_doc/qwen2), [Qwen2Moe](./model_doc/qwen2_moe), ๋ฐ [Qwen2-VL](./model_doc/qwen2_5_vl) +* [Starcoder2](./model_doc/starcoder2) + +
+ +์ด ๊ฐ€์ด๋“œ๋Š” Transformers์—์„œ ๋‹ค์–‘ํ•œ ๋ถ„ํ•  ์ „๋žต์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์„œ ๋ณ‘๋ ฌํ™”๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## ๋ชจ๋ธ ๋ถ„ํ• [[partitioning-a-model]] + +Transformers๋Š” `tp_plan`๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์— ๋Œ€ํ•ด ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋ถ„ํ•  ๋ฐฉ์‹์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +- `auto` ํ…์„œ ๋ณ‘๋ ฌํ™” ๊ณ„ํš์€ ์‚ฌ์ „ ์ •์˜๋œ ๊ตฌ์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ชจ๋ธ(์œ„์— ์–ธ๊ธ‰๋œ ์ง€์› ๋ชจ๋ธ)์„ ์ž๋™์œผ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. +- ์‚ฌ์šฉ์ž ์ง€์ • ๋ถ„ํ•  ๊ณ„ํš์„ ์ง์ ‘ ์ •์˜ํ•˜์—ฌ [~PreTrainedModel.from_pretrained] ๋ฉ”์†Œ๋“œ์˜ `tp_plan` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +```py +import os +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer + +# model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct" # ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์ „๋žต์„ ์‹œ๊ฐํ™”ํ•˜๊ธฐ์— ๋” ์ข‹์Œ +model_id = "meta-llama/Meta-Llama-3-8B-Instruct" # ์ ์€ ์ˆ˜์˜ GPU์— ๋” ์ข‹์Œ + +model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, tp_plan="auto") +print(model._tp_plan) + +tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") +prompt = "Can I help" +inputs = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device) + +# ๋ถ„์‚ฐ ์‹คํ–‰ +outputs = model(inputs) +``` + +์œ„์˜ ์ถ”๋ก  ์Šคํฌ๋ฆฝํŠธ๋ฅผ GPU๋‹น 4๊ฐœ ํ”„๋กœ์„ธ์Šค๋กœ [torchrun](https://pytorch.org/docs/stable/elastic/run.html)์—์„œ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +torchrun --nproc-per-node 4 demo.py +``` + + + + +๊ฐ ๋ ˆ์ด์–ด์— ๋Œ€ํ•œ ํ…์„œ ๋ณ‘๋ ฌ ๊ณ„ํš์„ `tp_plan`์— ์ •์˜ํ•œ ํ›„ [`~PreTrainedModel.from_pretrained`]์— ์ „๋‹ฌํ•˜์„ธ์š”. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์—ด ๋ฐ ํ–‰ ๋ถ„ํ• ์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ง€์›๋˜๋Š” ๋‹ค๋ฅธ ๋ถ„ํ•  ์ „๋žต์€ [๋ถ„ํ•  ์ „๋žต](#partitioning-strategies) ์„น์…˜์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +> [!WARNING] +> ์‚ฌ์šฉ์ž ์ง€์ • ๋ถ„ํ•  ๊ณ„ํš์„ ์ˆ˜๋™์œผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์™€ ๋ถ„ํ•  ์ „๋žต์ด ํ•จ๊ป˜ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„ํ•  ์ „๋žต์„ ์ž˜๋ชป ์„ค์ •ํ•˜๋ฉด ๋ชจ๋ธ์ด ๋งค์šฐ ๋А๋ ค์ง€๊ฑฐ๋‚˜, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜, ๋ถ€์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด [Ultra-Scale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=tensor_parallelism)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +```py +from transformers import AutoModelForCausalLM + +tp_plan = { + "model.layers.*.self_attn.q_proj": "colwise", + "model.layers.*.self_attn.k_proj": "colwise", + "model.layers.*.self_attn.v_proj": "colwise", + "model.layers.*.self_attn.o_proj": "rowwise", + ... +} + +model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, tp_plan=tp_plan) +print(model._tp_plan) +``` + + + + +## ๋ถ„ํ•  ์ „๋žต[[partitioning-strategies]] + +๋ชจ๋“  ๋ถ„ํ•  ์ „๋žต์€ ๋ฌธ์ž์—ด์„ ์ „๋žต ๊ตฌํ˜„์— ๋งคํ•‘ํ•˜๋Š” [`ParallelInterface`] ํด๋ž˜์Šค์—์„œ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ „๋žต์€ [`~PreTrainedModel.from_pretrained`]์˜ `tp_plan`์„ ํ†ตํ•ด ์„ค์ •๋˜๋ฏ€๋กœ ์ด ํด๋ž˜์Šค์™€ ์ง์ ‘ ์ƒํ˜ธ ์ž‘์šฉํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์–ด๋–ค ์ „๋žต์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```py +class ParallelInterface(MutableMapping): + """ + ํ—ˆ์šฉ๋œ ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ์ถ”์ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ™์€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. `register()` ํ˜ธ์ถœ๋กœ ์ƒˆ๋กœ์šด ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + ๋ชจ๋ธ์ด ๊ธฐ์กด ์–ดํ…์…˜ ํ•จ์ˆ˜(์˜ˆ: `sdpa`)๋ฅผ ๋กœ์ปฌ์—์„œ ๋ฎ์–ด์“ฐ๋ ค๋ฉด `modeling_.py` ๋‚ด๋ถ€์—์„œ ์ด ํด๋ž˜์Šค์˜ ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  + ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + """ + _global_mapping = { + "colwise": ColwiseParallel(), + "rowwise": RowwiseParallel(), + "colwise_rep": ColwiseParallel(output_layouts=Replicate()), + "rowwise_rep": RowwiseParallel(input_layouts=Replicate()), + "local_colwise": ColwiseParallel(use_dtensor=False), + "local_rowwise": RowwiseParallel(use_dtensor=False), + "local": IsolatedParallel(), + "gather": GatherParallel(), + "local_packed_rowwise": PackedRowwiseParallel(use_dtensor=False), + "sequence_parallel": SequenceParallel(), + "replicate": ReplicateParallel(), + } +``` + +๊ฐ ์ „๋žต์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +| ์ „๋žต | ์„ค๋ช… | +|---|---| +| `ColwiseParallel` | ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ์—ด ๋ฐฉํ–ฅ ๋ถ„ํ• . | +| `RowwiseParallel` | ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ํ–‰ ๋ฐฉํ–ฅ ๋ถ„ํ• . `nn.Embedding` ๋ชจ๋“ˆ ๋ถ„ํ• ๋„ ์ง€์›. | +| `SequenceParallel` | `LayerNorm`๊ณผ `Dropout` ๋ ˆ์ด์–ด๋ฅผ ์ง€์›ํ•˜๋Š” ์‹œํ€€์Šค ๋ณ‘๋ ฌ ๊ตฌํ˜„. [RMSNorm](https://github.com/facebookresearch/llama/blob/main/llama/model.py#L34)์˜ Python ๊ตฌํ˜„๋„ ์ง€์›. | +| `PackedColwiseParallel` | ํŒจํ‚น๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ง€์›ํ•˜๋Š” `ColwiseParallel`์˜ ๋ณ€ํ˜•(์˜ˆ: `up_proj`์™€ `gate_proj`๋ฅผ ํ•จ๊ป˜ ํŒจํ‚น). ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ฝ”๋“œ](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. | +| `PackedRowwiseParallel` | ํŒจํ‚น๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ง€์›ํ•˜๋Š” `RowwiseParallel`์˜ ๋ณ€ํ˜•([์ฝ”๋“œ](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108) ์ฐธ์กฐ). | +| `GatherParallel` | ๊ธฐ๊ธฐ ๊ฐ„ ๋ชจ๋“ˆ์˜ ์ถœ๋ ฅ์„ ์ˆ˜์ง‘. | +| `IsolatedParallel` | Mixture-of-Experts(MoE) ๋ ˆ์ด์–ด์˜ ์ „๋ฌธ๊ฐ€์— ์‚ฌ์šฉ๋˜์–ด ๋‹ค๋ฅธ ๊ธฐ๊ธฐ๋กœ๋ถ€ํ„ฐ ๋ชจ๋“ˆ์„ ๊ฒฉ๋ฆฌ. | +| `ReplicateParallel` | ๋ถ€๋ถ„์ ์œผ๋กœ ๋ถ„ํ• ๋œ ๋ชจ๋ธ๋กœ ์ธํ•ด `torch.distributed` API๊ฐ€ ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ๊ธฐ๊ธฐ์— ๋ชจ๋“ˆ์„ ๋ณต์ œ. | + +### ํŒจํ‚น๋œ ์ „๋žต[[packed-strategies]] + +๊ฐ€์ค‘์น˜ ํŒจํ‚น์€ ์—ฌ๋Ÿฌ ์„ ํ˜• ๋ ˆ์ด์–ด๋ฅผ ํ•˜๋‚˜์˜ ๋” ํฐ ๋ ˆ์ด์–ด๋กœ ํ•ฉ์น˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŒจํ‚น๋œ ์ „๋žต์ธ `PackedColwiseParallel`๊ณผ `PackedRowwiseParallel`์€ ํŒจํ‚น๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ `ColwiseParallel`์ด๋‚˜ `RowwiseParallel`์€ ํŒจํ‚น๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ถ„ํ• ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” `up_proj`์™€ `gate_proj`๋ฅผ ๋‹จ์ผ `gate_up_proj` ๋ชจ๋“ˆ๋กœ ํŒจํ‚นํ•˜๊ณ  `gate_up_proj`๋ฅผ ๋ถ„ํ• ํ•˜๊ธฐ ์œ„ํ•ด `PackedRowwiseParallel` ์ „๋žต์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +```python +class Llama4TextExperts(nn.Module): + ... + self.gate_up_proj = nn.Parameter(torch.empty(self.num_experts, self.hidden_size, 2 * self.expert_dim)) +``` + +๋ฐฐ์น˜ ํ–‰๋ ฌ ๊ณฑ์…ˆ์„ `forward` ํŒจ์Šค์—์„œ ์‚ฌ์šฉํ•˜์—ฌ `gate_up_proj` ๋ชจ๋“ˆ์˜ ์ถœ๋ ฅ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +def forward(self, hidden_states): + ... + gate_up = torch.bmm(hidden_states, self.gate_up_proj) # gate_up_proj ๋ชจ๋“ˆ์˜ ์ถœ๋ ฅ ๊ณ„์‚ฐ + gate, up = gate_up.chunk(2, dim=-1) # ์ถœ๋ ฅ์„ gate์™€ up์œผ๋กœ ๋ถ„ํ•  +``` + +> [!TIP] +> `Packed*`๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•œ ์‹œ๊ฐ์  ํ‘œํ˜„์€ [์ด ์ฃผ์„](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ๋กœ์ปฌ ์ „๋žต[[local-strategies]] + +๋กœ์ปฌ ์ „๋žต(`local_colwise`, `local_rowwise`, `local_packed_rowwise`)์€ [torch.chunk](https://docs.pytorch.org/docs/stable/generated/torch.chunk.html)์™€ ๊ฐ™์€ ์ผ๋ถ€ ์—ฐ์‚ฐ์—์„œ ์ง€์›๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— [DTensor](https://docs.pytorch.org/docs/stable/distributed.tensor.html)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ๋กœ์ปฌ ์ „๋žต์€ ๊ธฐ๋ณธ [torch.Tensor](https://docs.pytorch.org/docs/stable/tensors.html)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ผ๋ถ€ ๋ถ„์‚ฐ ๋กœ์ง์„ ์ˆ˜๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + + + +## ์‚ฌ์šฉ์ž ์ •์˜ ๋ถ„ํ•  ์ „๋žต[[custom-partitioning-strategies]] + +์‚ฌ์šฉ์ž ์ •์˜ ๋ถ„ํ•  ์ „๋žต์€ [`TensorParallelLayer`](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py)๋ฅผ ์ƒ์†ํ•˜๊ณ  `partition_tensor`, `_prepare_input_fn`, `_prepare_output_fn`์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฐ ๋‹ค์Œ `tp_plan`์—์„œ ํ•ด๋‹น ์ „๋žต์„ ์ง€์ •ํ–ˆ์„ ๋•Œ ๋””์ŠคํŒจ์นญ ๋กœ์ง์ด ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก `ParallelInterface` ๋งคํ•‘์— ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์ด ์›Œํฌํ”Œ๋กœ์šฐ๋กœ `ColwiseParallel`์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +1. `TensorParallelLayer`๋ฅผ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค. `__init__` ๋ฉ”์†Œ๋“œ์—์„œ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ํ…์„œ๊ฐ€ ๊ธฐ๊ธฐ์— ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๋Š” `input_layouts`๊ณผ `output_layouts`์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. `desired_input_layouts` ์†์„ฑ์€ ์ž…๋ ฅ์ด ๊ธฐ๊ธฐ์— ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜*๋˜์–ด์•ผ๋งŒ* ํ•˜๋Š”์ง€๋ฅผ ๋ช…์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + ```python + class ColwiseParallel(TensorParallelLayer): + def __init__( + self, + *, + input_layouts: Optional[Placement] = None, # ์ด์ „ ๋ ˆ์ด์–ด์—์„œ ์˜ค๋Š” ์ž…๋ ฅ ๋ ˆ์ด์•„์›ƒ + output_layouts: Optional[Placement] = None, # ๋‹ฌ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์ถœ๋ ฅ ๋ ˆ์ด์•„์›ƒ + use_local_output: bool = True, # ๋กœ์ปฌ ์ถœ๋ ฅ ์‚ฌ์šฉ ์—ฌ๋ถ€ + use_dtensor=True, # DTensor ์‚ฌ์šฉ ์—ฌ๋ถ€ + ): + self.input_layouts = (input_layouts or Replicate(),) # ์ด์ „ ๋ ˆ์ด์–ด์—์„œ ์˜ค๋Š” ์ž…๋ ฅ ๋ถ„ํ•  + self.output_layouts = (output_layouts or Shard(-1),) # ์›ํ•˜๋Š” ์ถœ๋ ฅ ๋ถ„ํ•  + self.desired_input_layouts = (Replicate(),) # ์›ํ•˜๋Š” ์ž…๋ ฅ ๋ถ„ํ• , ์ž…๋ ฅ์€ GPU ๊ฐ„์— ๋ณต์ œ๋˜์–ด์•ผ ํ•จ + self.use_local_output = use_local_output + self.use_dtensor = use_dtensor + ``` + +2. `partition_tensor`, `_prepare_input_fn`, `_prepare_output_fn` ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. + + `partition_tensor` ๋ฉ”์†Œ๋“œ๋Š” ํ…์„œ๋ฅผ ๋ถ„ํ• ํ•˜๊ณ  ๋ถ„ํ• ๋œ ํ…์„œ๋กœ `empty_param`์„ ์ฑ„์›๋‹ˆ๋‹ค. ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ `get_tensor_shard`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ๋žญํฌ์— ๋Œ€ํ•œ ์›๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์˜ฌ๋ฐ”๋ฅธ ๋ถ„ํ• ์„ ์–ป๊ณ , ํŒจํ‚น๋œ ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ๋Š” `get_packed_weights`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. + + ```python + def partition_tensor( + self, + param, # ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ „์ฒด ํ…์„œ + empty_param, # ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋นˆ ํ…์„œ, ๋ถ„ํ• ๋œ ํ…์„œ๋กœ ์ฑ„์›Œ์ง + param_type, # ๋งค๊ฐœ๋ณ€์ˆ˜ ์œ ํ˜•, `bias` ๋˜๋Š” `weight` + param_casting_dtype, # ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์บ์ŠคํŒ…ํ•  ์œ ํ˜• + to_contiguous, # ํ…์„œ๋ฅผ ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ณ€ํ™˜ํ• ์ง€ ์—ฌ๋ถ€ + rank, # ํ˜„์žฌ ๊ธฐ๊ธฐ์˜ ๋žญํฌ + device_mesh, # ๊ธฐ๊ธฐ ๋ฉ”์‹œ + ) -> nn.Parameter: # ๋ถ„ํ• ๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐ˜ํ™˜ + ... + ``` + + `_prepare_input_fn`๊ณผ `_prepare_output_fn` ๋ฉ”์†Œ๋“œ๋Š” [์‚ฌ์ „ ํฌ์›Œ๋“œ](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_pre_hook.html) ๋ฐ [ํฌ์›Œ๋“œ](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html) ํ›…์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. `__init__`์—์„œ ์ง€์ •๋œ ๋Œ€๋กœ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์›ํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ์žฌ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. + + ```python + def _prepare_input_fn(input_layouts, desired_input_layouts, mod, inputs, device_mesh): + ... + # ์‚ฌ์šฉ์ž ์ •์˜ ๋กœ์ง ์ˆ˜ํ–‰, DTensor๋กœ ์บ์ŠคํŒ… ๋“ฑ. + ... + return inputs.redistribute(placements=desired_input_layouts, device_mesh=device_mesh) + def _prepare_output_fn(output_layouts, use_local_output, mod, outputs, device_mesh): + ... + # ์‚ฌ์šฉ์ž ์ •์˜ ๋กœ์ง ์ˆ˜ํ–‰, DTensor๋กœ ์บ์ŠคํŒ… ๋“ฑ. + ... + return outputs.redistribute(placements=output_layouts, device_mesh=device_mesh) + ``` + +3. `tp_plan`๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „๋žต์„ [`ParallelInterface`]์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. + + ```python + from transformers.integrations.tensor_parallel import ParallelInterface + + ParallelInterface.register_strategy("colwise_custom", ColwiseParallel) + tp_plan = { + "model.layers.*.self_attn.q_proj": "colwise_custom", + ... + } + model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, tp_plan=tp_plan) + ``` + +## ๋ฒค์น˜๋งˆํฌ[[benchmarks]] + +ํ…์„œ ๋ณ‘๋ ฌํ™”๋Š” ํŠนํžˆ ํฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋‚˜ ๊ธด ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์ถ”๋ก  ์†๋„๋ฅผ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‹œํ€€์Šค ๊ธธ์ด๊ฐ€ 512์ธ [Llama](./model_doc/llama)์—์„œ ๋‹จ์ผ ํฌ์›Œ๋“œ ํŒจ์Šค์— ๋Œ€ํ•œ ์˜ˆ์ƒ ์†๋„ ํ–ฅ์ƒ ์ˆ˜์น˜๋Š” ์•„๋ž˜ ์ฐจํŠธ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +
+ +
+ +## ์„ค๊ณ„ ๊ตฌํ˜„[[design-implementation]] + +Transformers ํ…์„œ ๋ณ‘๋ ฌํ™” ๊ตฌํ˜„์€ ํ”„๋ ˆ์ž„์›Œํฌ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š์ง€๋งŒ, ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„์„ ์œ„ํ•ด์„œ๋Š” [DeviceMesh](https://docs.pytorch.org/tutorials/recipes/distributed_device_mesh.html)์™€ [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html)์˜ [DTensor](https://docs.pytorch.org/docs/stable/distributed.tensor.html)์— ์˜์กดํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +### DeviceMesh[[devicemesh]] + +`DeviceMesh`๋ฅผ ํ•จ๊ป˜ ํ†ต์‹ ํ•˜๋Š” ๊ธฐ๊ธฐ๋“ค์˜ ๋‹ค์ฐจ์› ๊ทธ๋ฆฌ๋“œ๋กœ ์ƒ์ƒํ•ด๋ณด์„ธ์š”. ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์ „๋žต๋งˆ๋‹ค ๊ฐ๊ธฐ ๋‹ค๋ฅธ ํ†ต์‹  ํŒจํ„ด์ด ํ•„์š”ํ•˜๋ฏ€๋กœ, ์—ฌ๋Ÿฌ ํ•˜์œ„ ๋ฉ”์‹œ๋ฅผ ๊ฐ€์ง„ `DeviceMesh`๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from torch.distributed.device_mesh import init_device_mesh + +# 4๊ฐœ GPU์˜ 1D ๋ฉ”์‹œ ์ƒ์„ฑ +device_mesh = init_device_mesh("cuda", (4,), mesh_dim_names=["tp"]) +``` + +`torch.distributed`์—์„œ ์ •์˜๋œ ๋Œ€๋ถ€๋ถ„์˜ ๋ณ‘๋ ฌํ™” ์ „๋žต์€ ๋ฉ”์‹œ ์ž์ฒด๋‚˜ ํ•˜์œ„ ๋ฉ”์‹œ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ž๋™์œผ๋กœ ํ†ต์‹  ํŒจํ„ด์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + +### DTensor[[dtensor]] + +`DTensor`(๋ถ„์‚ฐ ํ…์„œ)๋Š” ์ผ๋ฐ˜์ ์ธ ํ…์„œ ์—ฐ์‚ฐ ์œ„์— ๋ถ„์‚ฐ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ…์„œ ํ•˜์œ„ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ํ…์„œ ๋ณ‘๋ ฌํ™”์˜ ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋Š” `DTensor` ํ˜•ํƒœ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. + +DTensor์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ `placement` ์†์„ฑ์ž…๋‹ˆ๋‹ค. ์ด๋Š” PyTorch์—๊ฒŒ ํ…์„œ๊ฐ€ `DeviceMesh`์˜ ๊ธฐ๊ธฐ์— ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜๋˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. `placement` ์†์„ฑ์€ ๋‹ค์Œ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +- `Shard(dimension)` - `DTensor`๊ฐ€ ๊ตฌ์„ฑ๋œ `DeviceMesh`์—์„œ ์ฃผ์–ด์ง„ ์ฐจ์›์— ๊ฑธ์ณ ์–ด๋–ป๊ฒŒ ๋ถ„ํ• ๋˜๋Š”์ง€ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์—ด ๋ฐฉํ–ฅ ๋ถ„ํ• ์„ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์ฐจ์›์— ๊ฑธ์ณ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + ```python + weight = ... + weight = DTensor.from_local(weight, device_mesh["tp"], placements=[Shard(0)]) # ์ฒซ ๋ฒˆ์งธ(์—ด ๋ฐฉํ–ฅ) ์ฐจ์›์— ๊ฑธ์ณ ๋ถ„ํ•  + bias = ... + bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Shard(-1)]) # ์œ ์ผํ•œ ์ฐจ์›์— ๊ฑธ์ณ ๋ถ„ํ•  + ``` + + ์ด ์˜ˆ์‹œ๋Š” ํ–‰ ๋ฐฉํ–ฅ ๋ถ„ํ• ์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์ฐจ์›์— ๊ฑธ์ณ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + + ```python + weight = ... + weight = DTensor.from_local(weight, device_mesh["tp"], placements=[Shard(1)]) # ๋‘ ๋ฒˆ์งธ(ํ–‰ ๋ฐฉํ–ฅ) ์ฐจ์›์— ๊ฑธ์ณ ๋ถ„ํ•  + bias = ... + bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Replicate()]) # ๋ชจ๋“  GPU์— ํŽธํ–ฅ ๋ณต์ œ + ``` + +- `Replicate()` - `DTensor`๊ฐ€ `DeviceMesh`์— ๊ฑธ์ณ ๋ณต์ œ๋จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ฐ ๊ธฐ๊ธฐ์— ํ…์„œ์˜ ์ „์ฒด ์‚ฌ๋ณธ๋งŒ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + + ```py + bias = ... + bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Replicate()]) # ๋ชจ๋“  GPU์— ํŽธํ–ฅ ๋ณต์ œ + ``` + +- `Partial()` - ํ…์„œ๊ฐ€ ๊ฐ์†Œ ์—ฐ์‚ฐ์„ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ์ƒํƒœ์ž„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค (์ผ๋ฐ˜์ ์œผ๋กœ Transformers์—์„œ์˜ ์‚ฌ์šฉ ์‚ฌ๋ก€์™€๋Š” ์ง์ ‘์ ์ธ ๊ด€๋ จ์ด ์ ์Šต๋‹ˆ๋‹ค). diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..5936c82e07d76557897d2f5eb0c1762e28cbd780 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_infer_gpu_one.md @@ -0,0 +1,184 @@ + + +# ๋‹จ์ผ GPU์—์„œ ํšจ์œจ์ ์ธ ์ถ”๋ก  [[efficient-inference-on-a-single-gpu]] + +์ด ๊ฐ€์ด๋“œ ์™ธ์—๋„, [๋‹จ์ผ GPU์—์„œ์˜ ํ›ˆ๋ จ ๊ฐ€์ด๋“œ](perf_train_gpu_one)์™€ [CPU์—์„œ์˜ ์ถ”๋ก  ๊ฐ€์ด๋“œ](perf_infer_cpu)์—์„œ๋„ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## Better Transformer: PyTorch ๋„ค์ดํ‹ฐ๋ธŒ Transformer ํŒจ์ŠคํŠธํŒจ์Šค [[better-transformer-pytorchnative-transformer-fastpath]] + +PyTorch ๋„ค์ดํ‹ฐ๋ธŒ [`nn.MultiHeadAttention`](https://pytorch.org/blog/a-better-transformer-for-fast-transformer-encoder-inference/) ์–ดํ…์…˜ ํŒจ์ŠคํŠธํŒจ์Šค์ธ BetterTransformer๋Š” [๐Ÿค— Optimum ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ](https://huggingface.co/docs/optimum/bettertransformer/overview)์˜ ํ†ตํ•ฉ์„ ํ†ตํ•ด Transformers์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +PyTorch์˜ ์–ดํ…์…˜ ํŒจ์ŠคํŠธํŒจ์Šค๋Š” ์ปค๋„ ํ“จ์ „๊ณผ [์ค‘์ฒฉ๋œ ํ…์„œ](https://pytorch.org/docs/stable/nested.html)์˜ ์‚ฌ์šฉ์„ ํ†ตํ•ด ์ถ”๋ก  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋ฒค์น˜๋งˆํฌ๋Š” [์ด ๋ธ”๋กœ๊ทธ ๊ธ€](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[`optimum`](https://github.com/huggingface/optimum) ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ ํ›„์—๋Š” ์ถ”๋ก  ์ค‘ Better Transformer๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`~PreTrainedModel.to_bettertransformer`]๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ด€๋ จ ๋‚ด๋ถ€ ๋ชจ๋“ˆ์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค: + +```python +model = model.to_bettertransformer() +``` + +[`~PreTrainedModel.reverse_bettertransformer`] ๋ฉ”์†Œ๋“œ๋Š” ์ •๊ทœํ™”๋œ transformers ๋ชจ๋ธ๋ง์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ธฐ ์ „ ์›๋ž˜์˜ ๋ชจ๋ธ๋ง์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค: + +```python +model = model.reverse_bettertransformer() +model.save_pretrained("saved_model") +``` + +PyTorch 2.0๋ถ€ํ„ฐ๋Š” ์–ดํ…์…˜ ํŒจ์ŠคํŠธํŒจ์Šค๊ฐ€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ๋ชจ๋‘์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ์ง€์›๋˜๋Š” ์•„ํ‚คํ…์ฒ˜ ๋ชฉ๋ก์€ [์—ฌ๊ธฐ](https://huggingface.co/docs/optimum/bettertransformer/overview#supported-models)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## FP4 ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ์ถ”๋ก ์„ ์œ„ํ•œ `bitsandbytes` ํ†ตํ•ฉ [[bitsandbytes-integration-for-fp4-mixedprecision-inference]] + +`bitsandbytes`๋ฅผ ์„ค์น˜ํ•˜๋ฉด GPU์—์„œ ์†์‰ฝ๊ฒŒ ๋ชจ๋ธ์„ ์••์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FP4 ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์›๋ž˜์˜ ์ „์ฒด ์ •๋ฐ€๋„ ๋ฒ„์ „๊ณผ ๋น„๊ตํ•˜์—ฌ ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ตœ๋Œ€ 8๋ฐฐ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•˜์„ธ์š”. + + + +์ด ๊ธฐ๋Šฅ์€ ๋‹ค์ค‘ GPU ์„ค์ •์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +### ์š”๊ตฌ ์‚ฌํ•ญ [[requirements-for-fp4-mixedprecision-inference]] + +- ์ตœ์‹  `bitsandbytes` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ +`pip install bitsandbytes>=0.39.0` + +- ์ตœ์‹  `accelerate`๋ฅผ ์†Œ์Šค์—์„œ ์„ค์น˜ +`pip install git+https://github.com/huggingface/accelerate.git` + +- ์ตœ์‹  `transformers`๋ฅผ ์†Œ์Šค์—์„œ ์„ค์น˜ +`pip install git+https://github.com/huggingface/transformers.git` + +### FP4 ๋ชจ๋ธ ์‹คํ–‰ - ๋‹จ์ผ GPU ์„ค์ • - ๋น ๋ฅธ ์‹œ์ž‘ [[running-fp4-models-single-gpu-setup-quickstart]] + +๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋‹จ์ผ GPU์—์„œ ๋น ๋ฅด๊ฒŒ FP4 ๋ชจ๋ธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM + +model_name = "bigscience/bloom-2b5" +model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) +``` +`device_map`์€ ์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ `device_map = 'auto'`๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋””์ŠคํŒจ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๋ก ์— ์žˆ์–ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. + +### FP4 ๋ชจ๋ธ ์‹คํ–‰ - ๋‹ค์ค‘ GPU ์„ค์ • [[running-fp4-models-multi-gpu-setup]] + +๋‹ค์ค‘ GPU์—์„œ ํ˜ผํ•ฉ 4๋น„ํŠธ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ๋‹จ์ผ GPU ์„ค์ •๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค(๋™์ผํ•œ ๋ช…๋ น์–ด ์‚ฌ์šฉ): +```py +model_name = "bigscience/bloom-2b5" +model_4bit = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_4bit=True) +``` +ํ•˜์ง€๋งŒ `accelerate`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ GPU์— ํ• ๋‹นํ•  GPU RAM์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด `max_memory` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +max_memory_mapping = {0: "600MB", 1: "1GB"} +model_name = "bigscience/bloom-3b" +model_4bit = AutoModelForCausalLM.from_pretrained( + model_name, device_map="auto", load_in_4bit=True, max_memory=max_memory_mapping +) +``` +์ด ์˜ˆ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ GPU๊ฐ€ 600MB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ GPU๊ฐ€ 1GB๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +### ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ• [[advanced-usage]] + +์ด ๋ฐฉ๋ฒ•์˜ ๋” ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” [์–‘์žํ™”](main_classes/quantization) ๋ฌธ์„œ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## Int8 ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ–‰๋ ฌ ๋ถ„ํ•ด๋ฅผ ์œ„ํ•œ `bitsandbytes` ํ†ตํ•ฉ [[bitsandbytes-integration-for-int8-mixedprecision-matrix-decomposition]] + + + +์ด ๊ธฐ๋Šฅ์€ ๋‹ค์ค‘ GPU ์„ค์ •์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +[`LLM.int8() : 8-bit Matrix Multiplication for Transformers at Scale`](https://huggingface.co/papers/2208.07339) ๋…ผ๋ฌธ์—์„œ ์šฐ๋ฆฌ๋Š” ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋กœ Hub์˜ ๋ชจ๋“  ๋ชจ๋ธ์— ๋Œ€ํ•œ Hugging Face ํ†ตํ•ฉ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +์ด ๋ฐฉ๋ฒ•์€ `float16` ๋ฐ `bfloat16` ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด `nn.Linear` ํฌ๊ธฐ๋ฅผ 2๋ฐฐ๋กœ ์ค„์ด๊ณ , `float32` ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด 4๋ฐฐ๋กœ ์ค„์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ ˆ๋ฐ˜ ์ •๋ฐ€๋„์—์„œ ์ด์ƒ์น˜๋ฅผ ์ฒ˜๋ฆฌํ•จ์œผ๋กœ์จ ํ’ˆ์งˆ์— ๊ฑฐ์˜ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +![HFxbitsandbytes.png](https://cdn-uploads.huggingface.co/production/uploads/1659861207959-62441d1d9fdefb55a0b7d12c.png) + +Int8 ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ–‰๋ ฌ ๋ถ„ํ•ด๋Š” ํ–‰๋ ฌ ๊ณฑ์…ˆ์„ ๋‘ ๊ฐœ์˜ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค: (1) fp16๋กœ ๊ณฑํ•ด์ง€๋Š” ์ฒด๊ณ„์ ์ธ ํŠน์ด๊ฐ’ ์ด์ƒ์น˜ ์ŠคํŠธ๋ฆผ ํ–‰๋ ฌ(0.01%) ๋ฐ (2) int8 ํ–‰๋ ฌ ๊ณฑ์…ˆ์˜ ์ผ๋ฐ˜์ ์ธ ์ŠคํŠธ๋ฆผ(99.9%). ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ํฐ ๋ชจ๋ธ์— ๋Œ€ํ•ด ์˜ˆ์ธก ์ €ํ•˜ ์—†์ด int8 ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. +์ด ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๋…ผ๋ฌธ](https://huggingface.co/papers/2208.07339)์ด๋‚˜ [ํ†ตํ•ฉ์— ๊ด€ํ•œ ๋ธ”๋กœ๊ทธ ๊ธ€](https://huggingface.co/blog/hf-bitsandbytes-integration)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +![MixedInt8.gif](https://cdn-uploads.huggingface.co/production/uploads/1660567469965-62441d1d9fdefb55a0b7d12c.gif) + +์ปค๋„์€ GPU ์ „์šฉ์œผ๋กœ ์ปดํŒŒ์ผ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผํ•ฉ 8๋น„ํŠธ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด GPU๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ชจ๋ธ์˜ 1/4(๋˜๋Š” ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ์ ˆ๋ฐ˜ ์ •๋ฐ€๋„์ธ ๊ฒฝ์šฐ ์ ˆ๋ฐ˜)์„ ์ €์žฅํ•  ์ถฉ๋ถ„ํ•œ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. +์ด ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์ฐธ๊ณ  ์‚ฌํ•ญ์ด ์•„๋ž˜์— ๋‚˜์™€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” [Google colab](#colab-demos)์—์„œ ๋ฐ๋ชจ๋ฅผ ๋”ฐ๋ผํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +### ์š”๊ตฌ ์‚ฌํ•ญ [[requirements-for-int8-mixedprecision-matrix-decomposition]] + +- `bitsandbytes<0.37.0`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, 8๋น„ํŠธ ํ…์„œ ์ฝ”์–ด(Turing, Ampere ๋˜๋Š” ์ดํ›„ ์•„ํ‚คํ…์ฒ˜ - ์˜ˆ: T4, RTX20s RTX30s, A40-A100)๋ฅผ ์ง€์›ํ•˜๋Š” NVIDIA GPU์—์„œ ์‹คํ–‰ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. `bitsandbytes>=0.37.0`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“  GPU๊ฐ€ ์ง€์›๋ฉ๋‹ˆ๋‹ค. +- ์˜ฌ๋ฐ”๋ฅธ ๋ฒ„์ „์˜ `bitsandbytes`๋ฅผ ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์„ค์น˜ํ•˜์„ธ์š”: +`pip install bitsandbytes>=0.31.5` +- `accelerate`๋ฅผ ์„ค์น˜ํ•˜์„ธ์š” +`pip install accelerate>=0.12.0` + +### ํ˜ผํ•ฉ Int8 ๋ชจ๋ธ ์‹คํ–‰ - ๋‹จ์ผ GPU ์„ค์ • [[running-mixedint8-models-single-gpu-setup]] + +ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ ํ›„ ํ˜ผํ•ฉ 8๋น„ํŠธ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` + +ํ…์ŠคํŠธ ์ƒ์„ฑ์˜ ๊ฒฝ์šฐ: + +* `pipeline()` ํ•จ์ˆ˜ ๋Œ€์‹  ๋ชจ๋ธ์˜ `generate()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. `pipeline()` ํ•จ์ˆ˜๋กœ๋Š” ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ํ˜ผํ•ฉ 8๋น„ํŠธ ๋ชจ๋ธ์— ์ตœ์ ํ™”๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— `generate()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋А๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, nucleus ์ƒ˜ํ”Œ๋ง๊ณผ ๊ฐ™์€ ์ผ๋ถ€ ์ƒ˜ํ”Œ๋ง ์ „๋žต์€ ํ˜ผํ•ฉ 8๋น„ํŠธ ๋ชจ๋ธ์— ๋Œ€ํ•ด `pipeline()` ํ•จ์ˆ˜์—์„œ ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. +* ์ž…๋ ฅ์„ ๋ชจ๋ธ๊ณผ ๋™์ผํ•œ GPU์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_name = "bigscience/bloom-2b5" +tokenizer = AutoTokenizer.from_pretrained(model_name) +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) + +prompt = "Hello, my llama is cute" +inputs = tokenizer(prompt, return_tensors="pt").to("cuda") +generated_ids = model.generate(**inputs) +outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) +``` + + +### ํ˜ผํ•ฉ Int8 ๋ชจ๋ธ ์‹คํ–‰ - ๋‹ค์ค‘ GPU ์„ค์ • [[running-mixedint8-models-multi-gpu-setup]] + +๋‹ค์ค‘ GPU์—์„œ ํ˜ผํ•ฉ 8๋น„ํŠธ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹จ์ผ GPU ์„ค์ •๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค(๋™์ผํ•œ ๋ช…๋ น์–ด ์‚ฌ์šฉ): +```py +model_name = "bigscience/bloom-2b5" +model_8bit = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=BitsAndBytesConfig(load_in_8bit=True)) +``` +ํ•˜์ง€๋งŒ `accelerate`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ GPU์— ํ• ๋‹นํ•  GPU RAM์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด `max_memory` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +max_memory_mapping = {0: "1GB", 1: "2GB"} +model_name = "bigscience/bloom-3b" +model_8bit = AutoModelForCausalLM.from_pretrained( + model_name, device_map="auto", load_in_8bit=True, max_memory=max_memory_mapping +) +``` +์ด ์˜ˆ์‹œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ GPU๊ฐ€ 1GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ GPU๊ฐ€ 2GB๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +### Colab ๋ฐ๋ชจ [[colab-demos]] + +์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ „์— Google Colab์—์„œ ์ถ”๋ก ํ•  ์ˆ˜ ์—†์—ˆ๋˜ ๋ชจ๋ธ์— ๋Œ€ํ•ด ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +Google Colab์—์„œ 8๋น„ํŠธ ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ T5-11b(42GB in fp32)๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ๋ชจ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: + +[![Open In Colab: T5-11b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1YORPWx4okIHXnjW7MSAidXN29mPVNT7F?usp=sharing) + +๋˜๋Š” BLOOM-3B์— ๋Œ€ํ•œ ๋ฐ๋ชจ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: + +[![Open In Colab: BLOOM-3b demo](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1qOjXfQIAULfKvZqwCen8-MoWKGdSatZ4?usp=sharing) \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu.md new file mode 100644 index 0000000000000000000000000000000000000000..1a6c58b25afae10db053e7ae94b7f5c36c75fb7a --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu.md @@ -0,0 +1,67 @@ + + +# CPU์—์„œ ํšจ์œจ์ ์ธ ํ›ˆ๋ จ [[efficient-training-on-cpu]] + +์ด ๊ฐ€์ด๋“œ๋Š” CPU์—์„œ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ํšจ์œจ์ ์œผ๋กœ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ ์ดˆ์ ์„ ๋งž์ถฅ๋‹ˆ๋‹ค. + +## IPEX์™€ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ [[mixed-precision-with-ipex]] + +IPEX๋Š” AVX-512 ์ด์ƒ์„ ์ง€์›ํ•˜๋Š” CPU์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ, AVX2๋งŒ ์ง€์›ํ•˜๋Š” CPU์—๋„ ๊ธฐ๋Šฅ์ ์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ AVX-512 ์ด์ƒ์˜ Intel CPU ์„ธ๋Œ€์—์„œ๋Š” ์„ฑ๋Šฅ์ƒ ์ด์ ์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜์ง€๋งŒ, AVX2๋งŒ ์ง€์›ํ•˜๋Š” CPU (์˜ˆ: AMD CPU ๋˜๋Š” ์˜ค๋ž˜๋œ Intel CPU)์˜ ๊ฒฝ์šฐ์—๋Š” IPEX ์•„๋ž˜์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋Š” ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. IPEX๋Š” Float32์™€ BFloat16๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ CPU ํ›ˆ๋ จ์„ ์œ„ํ•œ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. BFloat16์˜ ์‚ฌ์šฉ์€ ๋‹ค์Œ ์„น์…˜์˜ ์ฃผ์š” ์ดˆ์ ์ž…๋‹ˆ๋‹ค. + +์ €์ •๋ฐ€๋„ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ธ BFloat16์€ 3์„ธ๋Œ€ Xeonยฎ Scalable ํ”„๋กœ์„ธ์„œ (์ฝ”๋“œ๋ช…: Cooper Lake)์—์„œ AVX512 ๋ช…๋ น์–ด ์ง‘ํ•ฉ์„ ๋„ค์ดํ‹ฐ๋ธŒ๋กœ ์ง€์›ํ•ด ์™”์œผ๋ฉฐ, ๋‹ค์Œ ์„ธ๋Œ€์˜ Intelยฎ Xeonยฎ Scalable ํ”„๋กœ์„ธ์„œ์—์„œ Intelยฎ Advanced Matrix Extensions (Intelยฎ AMX) ๋ช…๋ น์–ด ์ง‘ํ•ฉ์„ ์ง€์›ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. CPU ๋ฐฑ์—”๋“œ์˜ ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ๊ธฐ๋Šฅ์€ PyTorch-1.10๋ถ€ํ„ฐ ํ™œ์„ฑํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋™์‹œ์—, Intelยฎ Extension for PyTorch์—์„œ BFloat16์— ๋Œ€ํ•œ CPU์˜ ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ๋ฐ ์—ฐ์‚ฐ์ž์˜ BFloat16 ์ตœ์ ํ™”๋ฅผ ๋Œ€๊ทœ๋ชจ๋กœ ํ™œ์„ฑํ™”ํ•˜๊ณ , PyTorch ๋งˆ์Šคํ„ฐ ๋ธŒ๋žœ์น˜๋กœ ๋ถ€๋ถ„์ ์œผ๋กœ ์—…์ŠคํŠธ๋ฆผ์„ ๋ฐ˜์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋“ค์€ IPEX ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋‚˜์€ ์„ฑ๋Šฅ๊ณผ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +[์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/features/amp.html)์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. + +### IPEX ์„ค์น˜: [[ipex-installation]] + +IPEX ๋ฆด๋ฆฌ์Šค๋Š” PyTorch๋ฅผ ๋”ฐ๋ผ๊ฐ‘๋‹ˆ๋‹ค. pip๋ฅผ ํ†ตํ•ด ์„ค์น˜ํ•˜๋ ค๋ฉด: + +| PyTorch Version | IPEX version | +| :---------------: | :----------: | +| 1.13 | 1.13.0+cpu | +| 1.12 | 1.12.300+cpu | +| 1.11 | 1.11.200+cpu | +| 1.10 | 1.10.100+cpu | + +```bash +pip install intel_extension_for_pytorch== -f https://developer.intel.com/ipex-whl-stable-cpu +``` + +[IPEX ์„ค์น˜](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/installation.html)์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. + +### Trainer์—์„œ์˜ ์‚ฌ์šฉ๋ฒ• [[usage-in-trainer]] +Trainer์—์„œ IPEX์˜ ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž๋Š” ํ›ˆ๋ จ ๋ช…๋ น ์ธ์ˆ˜์— `use_ipex`, `bf16`, `no_cuda`๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +[Transformers ์งˆ๋ฌธ-์‘๋‹ต](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)์˜ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +- CPU์—์„œ BF16 ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ IPEX๋กœ ํ›ˆ๋ จํ•˜๊ธฐ: +
 python run_qa.py \
+--model_name_or_path google-bert/bert-base-uncased \
+--dataset_name squad \
+--do_train \
+--do_eval \
+--per_device_train_batch_size 12 \
+--learning_rate 3e-5 \
+--num_train_epochs 2 \
+--max_seq_length 384 \
+--doc_stride 128 \
+--output_dir /tmp/debug_squad/ \
+--use_ipex \
+--bf16 --no_cuda
+ +### ์‹ค์Šต ์˜ˆ์‹œ [[practice-example]] + +๋ธ”๋กœ๊ทธ: [Intel Sapphire Rapids๋กœ PyTorch Transformers ๊ฐ€์†ํ™”](https://huggingface.co/blog/intel-sapphire-rapids) \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..e7a68971a7dc54e465dfbefb3290a5a46daf0524 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_cpu_many.md @@ -0,0 +1,134 @@ + + +# ๋‹ค์ค‘ CPU์—์„œ ํšจ์œจ์ ์œผ๋กœ ํ›ˆ๋ จํ•˜๊ธฐ [[efficient-training-on-multiple-cpus]] + +ํ•˜๋‚˜์˜ CPU์—์„œ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ๋А๋ฆด ๋•Œ๋Š” ๋‹ค์ค‘ CPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ๋Š” PyTorch ๊ธฐ๋ฐ˜์˜ DDP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์‚ฐ CPU ํ›ˆ๋ จ์„ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +## PyTorch์šฉ Intelยฎ oneCCL ๋ฐ”์ธ๋”ฉ [[intel-oneccl-bindings-for-pytorch]] + +[Intelยฎ oneCCL](https://github.com/oneapi-src/oneCCL) (collective communications library)์€ allreduce, allgather, alltoall๊ณผ ๊ฐ™์€ ์ง‘ํ•ฉ ํ†ต์‹ (collective communications)์„ ๊ตฌํ˜„ํ•œ ํšจ์œจ์ ์ธ ๋ถ„์‚ฐ ๋”ฅ๋Ÿฌ๋‹ ํ›ˆ๋ จ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. oneCCL์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” [oneCCL ๋ฌธ์„œ](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)์™€ [oneCCL ์‚ฌ์–‘](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + +`oneccl_bindings_for_pytorch` ๋ชจ๋“ˆ (`torch_ccl`์€ ๋ฒ„์ „ 1.12 ์ด์ „์— ์‚ฌ์šฉ)์€ PyTorch C10D ProcessGroup API๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉฐ, ์™ธ๋ถ€ ProcessGroup๋กœ ๋™์ ์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ˜„์žฌ Linux ํ”Œ๋žซํผ์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +[oneccl_bind_pt](https://github.com/intel/torch-ccl)์—์„œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +### PyTorch์šฉ Intelยฎ oneCCL ๋ฐ”์ธ๋”ฉ ์„ค์น˜: [[intel-oneccl-bindings-for-pytorch-installation]] + +๋‹ค์Œ Python ๋ฒ„์ „์— ๋Œ€ํ•œ Wheel ํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +| Extension Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | Python 3.10 | +| :---------------: | :--------: | :--------: | :--------: | :--------: | :---------: | +| 1.13.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.100 | | โˆš | โˆš | โˆš | โˆš | +| 1.12.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.11.0 | | โˆš | โˆš | โˆš | โˆš | +| 1.10.0 | โˆš | โˆš | โˆš | โˆš | | + +```bash +pip install oneccl_bind_pt=={pytorch_version} -f https://developer.intel.com/ipex-whl-stable-cpu +``` +`{pytorch_version}`์€ 1.13.0๊ณผ ๊ฐ™์ด PyTorch ๋ฒ„์ „์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +[oneccl_bind_pt ์„ค์น˜](https://github.com/intel/torch-ccl)์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•ด ๋ณด์„ธ์š”. +oneCCL๊ณผ PyTorch์˜ ๋ฒ„์ „์€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +oneccl_bindings_for_pytorch 1.12.0 ๋ฒ„์ „์˜ ๋ฏธ๋ฆฌ ๋นŒ๋“œ๋œ Wheel ํŒŒ์ผ์€ PyTorch 1.12.1๊ณผ ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค(PyTorch 1.12.0์šฉ์ž…๋‹ˆ๋‹ค). +PyTorch 1.12.1์€ oneccl_bindings_for_pytorch 1.12.10 ๋ฒ„์ „๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +## Intelยฎ MPI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ [[intel-mpi-library]] +์ด ํ‘œ์ค€ ๊ธฐ๋ฐ˜ MPI ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•˜์—ฌ Intelยฎ ์•„ํ‚คํ…์ฒ˜์—์„œ ์œ ์—ฐํ•˜๊ณ  ํšจ์œจ์ ์ด๋ฉฐ ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํด๋Ÿฌ์Šคํ„ฐ ๋ฉ”์‹œ์ง•์„ ์ œ๊ณตํ•˜์„ธ์š”. ์ด ๊ตฌ์„ฑ ์š”์†Œ๋Š” Intelยฎ oneAPI HPC Toolkit์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. + +oneccl_bindings_for_pytorch๋Š” MPI ๋„๊ตฌ ์„ธํŠธ์™€ ํ•จ๊ป˜ ์„ค์น˜๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ํ™˜๊ฒฝ์„ ์†Œ์Šค๋กœ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +Intelยฎ oneCCL ๋ฒ„์ „ 1.12.0 ์ด์ƒ์ธ ๊ฒฝ์šฐ +```bash +oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") +source $oneccl_bindings_for_pytorch_path/env/setvars.sh +``` + +Intelยฎ oneCCL ๋ฒ„์ „์ด 1.12.0 ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ +```bash +torch_ccl_path=$(python -c "import torch; import torch_ccl; import os; print(os.path.abspath(os.path.dirname(torch_ccl.__file__)))") +source $torch_ccl_path/env/setvars.sh +``` + +#### IPEX ์„ค์น˜: [[ipex-installation]] + +IPEX๋Š” Float32์™€ BFloat16์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋Š” CPU ํ›ˆ๋ จ์„ ์œ„ํ•œ ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. [single CPU section](./perf_train_cpu)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + +์ด์–ด์„œ ๋‚˜์˜ค๋Š” "Trainer์—์„œ์˜ ์‚ฌ์šฉ"์€ Intelยฎ MPI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ mpirun์„ ์˜ˆ๋กœ ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. + + +## Trainer์—์„œ์˜ ์‚ฌ์šฉ [[usage-in-trainer]] +Trainer์—์„œ ccl ๋ฐฑ์—”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ€ํ‹ฐ CPU ๋ถ„์‚ฐ ํ›ˆ๋ จ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ๋ช…๋ น ์ธ์ˆ˜์— **`--ddp_backend ccl`**์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +[์งˆ์˜ ์‘๋‹ต ์˜ˆ์ œ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + + +๋‹ค์Œ ๋ช…๋ น์€ ํ•œ Xeon ๋…ธ๋“œ์—์„œ 2๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๋กœ ํ›ˆ๋ จ์„ ํ™œ์„ฑํ™”ํ•˜๋ฉฐ, ๊ฐ ์†Œ์ผ“๋‹น ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. OMP_NUM_THREADS/CCL_WORKER_COUNT ๋ณ€์ˆ˜๋Š” ์ตœ์ ์˜ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=127.0.0.1 + mpirun -n 2 -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex +``` +๋‹ค์Œ ๋ช…๋ น์€ ๋‘ ๊ฐœ์˜ Xeon(๋…ธ๋“œ0 ๋ฐ ๋…ธ๋“œ1, ์ฃผ ํ”„๋กœ์„ธ์Šค๋กœ ๋…ธ๋“œ0์„ ์‚ฌ์šฉ)์—์„œ ์ด 4๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๋กœ ํ›ˆ๋ จ์„ ํ™œ์„ฑํ™”ํ•˜๋ฉฐ, ๊ฐ ์†Œ์ผ“๋‹น ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. OMP_NUM_THREADS/CCL_WORKER_COUNT ๋ณ€์ˆ˜๋Š” ์ตœ์ ์˜ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋…ธ๋“œ0์—์„œ๋Š” ๊ฐ ๋…ธ๋“œ์˜ IP ์ฃผ์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ตฌ์„ฑ ํŒŒ์ผ(์˜ˆ: hostfile)์„ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ๊ตฌ์„ฑ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +```shell script + cat hostfile + xxx.xxx.xxx.xxx #node0 ip + xxx.xxx.xxx.xxx #node1 ip +``` +์ด์ œ ๋…ธ๋“œ0์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด **4DDP**๊ฐ€ ๋…ธ๋“œ0 ๋ฐ ๋…ธ๋“œ1์—์„œ BF16 ์ž๋™ ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋กœ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. +```shell script + export CCL_WORKER_COUNT=1 + export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip + mpirun -f hostfile -n 4 -ppn 2 \ + -genv OMP_NUM_THREADS=23 \ + python3 run_qa.py \ + --model_name_or_path google-bert/bert-large-uncased \ + --dataset_name squad \ + --do_train \ + --do_eval \ + --per_device_train_batch_size 12 \ + --learning_rate 3e-5 \ + --num_train_epochs 2 \ + --max_seq_length 384 \ + --doc_stride 128 \ + --output_dir /tmp/debug_squad/ \ + --no_cuda \ + --ddp_backend ccl \ + --use_ipex \ + --bf16 +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_many.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_many.md new file mode 100644 index 0000000000000000000000000000000000000000..801e06e276e428577a13efb117fb4e52ac994034 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_many.md @@ -0,0 +1,533 @@ + + +# ๋‹ค์ค‘ GPU์—์„œ ํšจ์œจ์ ์ธ ํ›ˆ๋ จ [[efficient-training-on-multiple-gpus]] + +๋‹จ์ผ GPU์—์„œ์˜ ํ›ˆ๋ จ์ด ๋„ˆ๋ฌด ๋А๋ฆฌ๊ฑฐ๋‚˜ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹จ์ผ GPU์˜ ๋ฉ”๋ชจ๋ฆฌ์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ๋‹ค์ค‘-GPU ์„ค์ •์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ GPU์—์„œ ๋‹ค์ค‘ GPU๋กœ ์ „ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž‘์—…์„ ๋ถ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ, ํ…์„œ ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๊ฐ™์€ ๋ณ‘๋ ฌํ™” ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—…์„ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ์„ค์ •์„ ๋ชจ๋‘์—๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์™„๋ฒฝํ•œ ํ•ด๊ฒฐ์ฑ…์€ ์—†์œผ๋ฉฐ, ์–ด๋–ค ์„ค์ •์ด ๊ฐ€์žฅ ์ ํ•ฉํ•œ์ง€๋Š” ์‚ฌ์šฉํ•˜๋Š” ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ๋Š” ์ฃผ๋กœ PyTorch ๊ธฐ๋ฐ˜์˜ ๊ตฌํ˜„์„ ์ค‘์‹ฌ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉฐ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฐœ๋…์€ ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์—๋„ ์ ์šฉ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. + + + + ์ฐธ๊ณ : [๋‹จ์ผ GPU ์„น์…˜](perf_train_gpu_one)์—์„œ ์†Œ๊ฐœ๋œ ์ „๋žต(ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ›ˆ๋ จ ๋˜๋Š” ๊ทธ๋ž˜๋””์–ธํŠธ ๋ˆ„์  ๋“ฑ)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ ํ›ˆ๋ จ์— ์ ์šฉ๋˜๋ฉฐ, ๋‹ค์ค‘-GPU ๋˜๋Š” CPU ํ›ˆ๋ จ๊ณผ ๊ฐ™์€ ๋‹ค์Œ ์„น์…˜์œผ๋กœ ์ง„์ž…ํ•˜๊ธฐ ์ „์— ํ•ด๋‹น ์„น์…˜์„ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +๋จผ์ € 1D ๋ณ‘๋ ฌํ™” ๊ธฐ์ˆ ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋…ผ์˜ํ•œ ํ›„, ์ด๋Ÿฌํ•œ ๊ธฐ์ˆ ์„ ๊ฒฐํ•ฉํ•˜์—ฌ 2D ๋ฐ 3D ๋ณ‘๋ ฌํ™”๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋” ๋น ๋ฅธ ํ›ˆ๋ จ๊ณผ ๋” ํฐ ๋ชจ๋ธ์„ ์ง€์›ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹ค๋ฅธ ํšจ๊ณผ์ ์ธ ๋Œ€์•ˆ ๋ฐฉ์‹๋„ ์†Œ๊ฐœ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + +## ๊ฐœ๋… [[concepts]] + +๋‹ค์Œ์€ ์ด ๋ฌธ์„œ์—์„œ ์ž์„ธํžˆ ์„ค๋ช…๋  ์ฃผ์š” ๊ฐœ๋…์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. + +1. **DataParallel (DP)** - ๋™์ผํ•œ ์„ค์ •์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ณต์ œ๋˜๊ณ , ๊ฐ ์„ค์ •์— ๋ฐ์ดํ„ฐ ์ผ๋ถ€๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌ๋Š” ๋ณ‘๋ ฌ๋กœ ์ˆ˜ํ–‰๋˜๋ฉฐ ๋ชจ๋“  ์„ค์ •์€ ๊ฐ ํ›ˆ๋ จ ๋‹จ๊ณ„์˜ ๋๋‚  ๋•Œ ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. +2. **TensorParallel (TP)** - ๊ฐ ํ…์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌถ์Œ์œผ๋กœ ๋ถ„ํ• ๋˜๊ธฐ์—, ์ „์ฒด ํ…์„œ๊ฐ€ ๋‹จ์ผ GPU์— ์ƒ์ฃผํ•˜๋Š” ๋Œ€์‹  ํ…์„œ์˜ ๊ฐ ์ƒค๋“œ๊ฐ€ ์ง€์ •๋œ GPU์— ์ƒ์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์•ˆ ๊ฐ ์ƒค๋“œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ GPU์—์„œ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋˜๋ฉฐ ๊ฒฐ๊ณผ๋Š” ๋‹จ๊ณ„๊ฐ€ ๋๋‚  ๋•Œ ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋ถ„ํ• ์ด ์ˆ˜ํ‰ ์ˆ˜์ค€์—์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ˆ˜ํ‰ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +3. **PipelineParallel (PP)** - ๋ชจ๋ธ์ด ์ˆ˜์ง์œผ๋กœ (๋ ˆ์ด์–ด ์ˆ˜์ค€) ์—ฌ๋Ÿฌ GPU์— ๋ถ„ํ• ๋˜์–ด ๋ชจ๋ธ์˜ ๋‹จ์ผ GPU์—๋Š” ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ ๋ ˆ์ด์–ด๊ฐ€ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. ๊ฐ GPU๋Š” ํŒŒ์ดํ”„๋ผ์ธ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ๊ณ„๋ฅผ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉฐ ์ž‘์€ ๋ฐฐ์น˜ ๋ฌถ์Œ์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +4. **Zero Redundancy Optimizer (ZeRO)** - TP์™€ ์œ ์‚ฌํ•˜๊ฒŒ ํ…์„œ๋ฅผ ์ƒค๋”ฉํ•˜์ง€๋งŒ, ์ „์ฒด ํ…์„œ๋Š” ์ˆœ๋ฐฉํ–ฅ ๋˜๋Š” ์—ญ๋ฐฉํ–ฅ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์žฌ๊ตฌ์„ฑ๋˜๋ฏ€๋กœ ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ œํ•œ๋œ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์˜คํ”„๋กœ๋“œ ๊ธฐ์ˆ ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +5. **Sharded DDP** - ZeRO์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์œผ๋กœ ๋‹ค๋ฅธ ZeRO ๊ตฌํ˜„์—์„œ๋„ ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค. + +๊ฐ ๊ฐœ๋…์˜ ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ๋Œ€๊ทœ๋ชจ ์ธํ”„๋ผ์—์„œ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๊ฒฝ์šฐ์˜ ๋Œ€๋žต์ ์ธ ๊ฒฐ์ • ๊ณผ์ •์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ํ™•์žฅ์„ฑ ์ „๋žต [[scalability-strategy]] + +**โ‡จ ๋‹จ์ผ ๋…ธ๋“œ / ๋‹ค์ค‘-GPU** +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž๋Š” ๊ฒฝ์šฐ: + + 1. DDP - ๋ถ„์‚ฐ DP + 2. ZeRO - ์ƒํ™ฉ๊ณผ ๊ตฌ์„ฑ์— ๋”ฐ๋ผ ๋” ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Œ + +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + + 1. PP + 2. ZeRO + 3. TP + + ๋…ธ๋“œ ๋‚ด ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅธ NVLINK ๋˜๋Š” NVSwitch์˜ ๊ฒฝ์šฐ ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๋Œ€๋ถ€๋ถ„ ๋น„์Šทํ•œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์•ผ ํ•˜๋ฉฐ, PP๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ TP ๋˜๋Š” ZeRO๋ณด๋‹ค ๋น ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. TP์˜ ์ •๋„๋„ ์ฐจ์ด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์„ค์ •์—์„œ ์Šน์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์‹คํ—˜ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. + + TP๋Š” ๊ฑฐ์˜ ํ•ญ์ƒ ๋‹จ์ผ ๋…ธ๋“œ ๋‚ด์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, TP ํฌ๊ธฐ <= ๋…ธ๋“œ๋‹น GPU ์ˆ˜์ž…๋‹ˆ๋‹ค. + +* ๊ฐ€์žฅ ํฐ ๋ ˆ์ด์–ด๊ฐ€ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + + 1. ZeRO๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ - PP๋งŒ์œผ๋กœ๋Š” ๋งž์ง€ ์•Š์œผ๋ฏ€๋กœ TP๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•จ + 2. ZeRO๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์œ„์˜ "๋‹จ์ผ GPU" ํ•ญ๋ชฉ๊ณผ ๋™์ผ + + +**โ‡จ ๋‹ค์ค‘ ๋…ธ๋“œ / ๋‹ค์ค‘ GPU** + +* ๋…ธ๋“œ ๊ฐ„ ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ๋น ๋ฅธ ๊ฒฝ์šฐ: + + 1. ZeRO - ๋ชจ๋ธ์— ๋Œ€๋ถ€๋ถ„์˜ ์ˆ˜์ •์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Œ + 2. PP+TP+DP - ํ†ต์‹ ์ด ์ ์ง€๋งŒ ๋ชจ๋ธ์— ๋Œ€๋Œ€์ ์ธ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•จ + +* ๋…ธ๋“œ ๊ฐ„ ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ๋А๋ฆฌ๋ฉฐ, GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์—ฌ์ „ํžˆ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ: + + 1. DP+PP+TP+ZeRO-1 + + + +## ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” [[data-parallelism]] + +2๊ฐœ์˜ GPU๋งŒ์œผ๋กœ๋„ ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ์ž๋“ค์€ `DataParallel` (DP)๊ณผ `DistributedDataParallel` (DDP)์„ ํ†ตํ•ด ํ–ฅ์ƒ๋œ ํ›ˆ๋ จ ์†๋„๋ฅผ ๋ˆ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” PyTorch์˜ ๋‚ด์žฅ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ DDP๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์œผ๋ฉฐ, DP๋Š” ์ผ๋ถ€ ๋ชจ๋ธ์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. [PyTorch ๋ฌธ์„œ](https://pytorch.org/docs/master/generated/torch.nn.DataParallel.html)์—์„œ๋„ DDP์˜ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +### DP vs DDP [[dp-vs-ddp]] + +`DistributedDataParallel` (DDP)์€ ์ผ๋ฐ˜์ ์œผ๋กœ `DataParallel` (DP)๋ณด๋‹ค ๋น ๋ฅด์ง€๋งŒ, ํ•ญ์ƒ ๊ทธ๋ ‡์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค: +* DP๋Š” ํŒŒ์ด์ฌ ์Šค๋ ˆ๋“œ ๊ธฐ๋ฐ˜์ธ ๋ฐ˜๋ฉด, DDP๋Š” ๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค ๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— GIL๊ณผ ๊ฐ™์€ ํŒŒ์ด์ฌ ์Šค๋ ˆ๋“œ ์ œํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค. +* ๊ทธ๋Ÿฌ๋‚˜ GPU ์นด๋“œ ๊ฐ„์˜ ๋А๋ฆฐ ์ƒํ˜ธ ์—ฐ๊ฒฐ์„ฑ์€ DDP๋กœ ์ธํ•ด ์‹ค์ œ๋กœ ๋А๋ฆฐ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋‘ ๋ชจ๋“œ ๊ฐ„์˜ GPU ๊ฐ„ ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ์˜ ์ฃผ์š” ์ฐจ์ด์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +[DDP](https://pytorch.org/docs/master/notes/ddp.html): + +- ์‹œ์ž‘ํ•  ๋•Œ, ์ฃผ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ชจ๋ธ์„ gpu 0์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  gpu๋กœ ๋ณต์ œํ•ฉ๋‹ˆ๋‹ค. +- ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ ๋ฐฐ์น˜์— ๋Œ€ํ•ด: + 1. ๊ฐ gpu๋Š” ์ž์ฒด ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + 2. `backward` ๋™์•ˆ ๋กœ์ปฌ ๊ทธ๋ž˜๋””์–ธํŠธ๊ฐ€ ์ค€๋น„๋˜๋ฉด, ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์— ํ‰๊ท ํ™”๋ฉ๋‹ˆ๋‹ค. + +[DP](https://pytorch.org/docs/master/generated/torch.nn.DataParallel.html): + +๊ฐ ๋ฐฐ์น˜์— ๋Œ€ํ•ด: + 1. gpu 0์€ ๋ฐ์ดํ„ฐ ๋ฐฐ์น˜๋ฅผ ์ฝ๊ณ  ๊ฐ gpu์— ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. + 2. ์—…๋ฐ์ดํŠธ๋œ ๋ชจ๋ธ์„ gpu 0์—์„œ ๊ฐ gpu๋กœ ๋ณต์ œํ•ฉ๋‹ˆ๋‹ค. + 3. `forward`๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฐ gpu์˜ ์ถœ๋ ฅ์„ gpu 0์œผ๋กœ ๋ณด๋‚ด๊ณ  ์†์‹ค์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + 4. gpu 0์—์„œ ๋ชจ๋“  gpu๋กœ ์†์‹ค์„ ๋ถ„์‚ฐํ•˜๊ณ  `backward`๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. + 5. ๊ฐ gpu์—์„œ ๊ทธ๋ž˜๋””์–ธํŠธ๋ฅผ gpu 0์œผ๋กœ ๋ณด๋‚ด๊ณ  ์ด๋ฅผ ํ‰๊ท ํ™”ํ•ฉ๋‹ˆ๋‹ค. + +DDP๋Š” ๊ฐ ๋ฐฐ์น˜๋งˆ๋‹ค ๊ทธ๋ž˜๋””์–ธํŠธ๋ฅผ ๋ณด๋‚ด๋Š” ํ†ต์‹ ๋งŒ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, DP๋Š” ๋ฐฐ์น˜๋งˆ๋‹ค 5๊ฐœ์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +DP๋Š” ํŒŒ์ด์ฌ ์Šค๋ ˆ๋“œ๋ฅผ ํ†ตํ•ด ํ”„๋กœ์„ธ์Šค ๋‚ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์ œํ•˜๋ฉฐ, DDP๋Š” [torch.distributed](https://pytorch.org/docs/master/distributed.html)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์ œํ•ฉ๋‹ˆ๋‹ค. + +DP์—์„œ๋Š” gpu 0์ด ๋‹ค๋ฅธ gpu๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ, gpu์˜ ํ™œ์šฉ๋„๊ฐ€ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. + +DDP๋Š” ์—ฌ๋Ÿฌ ๋Œ€์˜ ์ปดํ“จํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, DP์˜ ๊ฒฝ์šฐ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +DP์™€ DDP ์‚ฌ์ด์—๋Š” ๋‹ค๋ฅธ ์ฐจ์ด์ ์ด ์žˆ์ง€๋งŒ, ์ด ํ† ๋ก ๊ณผ๋Š” ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค. + +์ด 2๊ฐ€์ง€ ๋ชจ๋“œ๋ฅผ ๊นŠ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, [์ด ๋ฌธ์„œ](https://www.telesens.co/2019/04/04/distributed-data-parallel-training-using-pytorch-on-aws/)๋ฅผ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ๋Š” ๋ฉ‹์ง„ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๋‹ค์–‘ํ•œ ํ•˜๋“œ์›จ์–ด์—์„œ ์—ฌ๋Ÿฌ ๋ฒค์น˜๋งˆํฌ์™€ ํ”„๋กœํŒŒ์ผ๋Ÿฌ ์ถœ๋ ฅ์„ ์„ค๋ช…ํ•˜์—ฌ ํ•„์š”ํ•œ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +์‹ค์ œ ๋ฒค์น˜๋งˆํฌ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +| Type | NVlink | Time | +| :----- | ----- | ---: | +| 2:DP | Y | 110s | +| 2:DDP | Y | 101s | +| 2:DDP | N | 131s | + + +๋ถ„์„: + +์—ฌ๊ธฐ์„œ DP๋Š” NVlink๊ฐ€ ์žˆ๋Š” DDP๋ณด๋‹ค ์•ฝ 10% ๋А๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ NVlink๊ฐ€ ์—†๋Š” DDP๋ณด๋‹ค ์•ฝ 15% ๋น ๋ฆ…๋‹ˆ๋‹ค. + +์‹ค์ œ ์ฐจ์ด๋Š” ๊ฐ GPU๊ฐ€ ๋‹ค๋ฅธ GPU์™€ ๋™๊ธฐํ™”ํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋™๊ธฐํ™”ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋А๋ฆฐ ๋งํฌ๊ฐ€ ์ด ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋Šฆ์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ์ „์ฒด ๋ฒค์น˜๋งˆํฌ ์ฝ”๋“œ์™€ ์ถœ๋ ฅ์ž…๋‹ˆ๋‹ค: + +ํ•ด๋‹น ๋ฒค์น˜๋งˆํฌ์—์„œ `NCCL_P2P_DISABLE=1`์„ ์‚ฌ์šฉํ•˜์—ฌ NVLink ๊ธฐ๋Šฅ์„ ๋น„ํ™œ์„ฑํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค. + +```bash + +# DP +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 \ +python examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 110.5948, 'train_samples_per_second': 1.808, 'epoch': 0.69} + +# DDP w/ NVlink +rm -r /tmp/test-clm; CUDA_VISIBLE_DEVICES=0,1 \ +torchrun --nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 101.9003, 'train_samples_per_second': 1.963, 'epoch': 0.69} + +# DDP w/o NVlink +rm -r /tmp/test-clm; NCCL_P2P_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1 \ +torchrun --nproc_per_node 2 examples/pytorch/language-modeling/run_clm.py \ +--model_name_or_path openai-community/gpt2 --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 \ +--do_train --output_dir /tmp/test-clm --per_device_train_batch_size 4 --max_steps 200 + +{'train_runtime': 131.4367, 'train_samples_per_second': 1.522, 'epoch': 0.69} +``` + +ํ•˜๋“œ์›จ์–ด: ๊ฐ๊ฐ 24GB์˜ TITAN RTX 2๊ฐœ + NVlink๊ณผ 2๊ฐœ์˜ NVLink (`nvidia-smi topo -m`์—์„œ `NV2`์ž…๋‹ˆ๋‹ค.) +์†Œํ”„ํŠธ์›จ์–ด: `pytorch-1.8-to-be` + `cuda-11.0` / `transformers==4.3.0.dev0` + +## ZeRO ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” [[zero-data-parallelism]] + +ZeRO๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” (ZeRO-DP)๋Š” ๋‹ค์Œ [๋ธ”๋กœ๊ทธ ๊ธ€](https://www.microsoft.com/en-us/research/blog/zero-deepspeed-new-system-optimizations-enable-training-models-with-over-100-billion-parameters/)์˜ ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ์„ค๋ช…๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. +![DeepSpeed-Image-1](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-zero.png) + +์ด ๊ฐœ๋…์€ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์ธ `DataParallel` (DP)๊ณผ ๋™์ผํ•˜์ง€๋งŒ, ์ „์ฒด ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜, ๊ทธ๋ž˜๋””์–ธํŠธ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋Œ€์‹  ๊ฐ GPU๋Š” ๊ทธ ์ค‘ ์ผ๋ถ€๋งŒ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ ์‹œ๊ฐ„์—๋Š” ์ฃผ์–ด์ง„ ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด ์ „์ฒด ๋ ˆ์ด์–ด ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๋•Œ ๊ฐ GPU๊ฐ€ ์„œ๋กœ์—๊ฒŒ ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋™๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค - ๊ทธ๊ฒŒ ์ „๋ถ€์ž…๋‹ˆ๋‹ค. + +๊ฐ๊ฐ 3๊ฐœ์˜ ๋ ˆ์ด์–ด์™€ 3๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ์„ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค: +``` +La | Lb | Lc +---|----|--- +a0 | b0 | c0 +a1 | b1 | c1 +a2 | b2 | c2 +``` +๋ ˆ์ด์–ด La์—๋Š” ๊ฐ€์ค‘์น˜ a0, a1 ๋ฐ a2๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +3๊ฐœ์˜ GPU๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, Sharded DDP (= Zero-DP)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋ธ์„ 3๊ฐœ์˜ GPU์— ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค: + +``` +GPU0: +La | Lb | Lc +---|----|--- +a0 | b0 | c0 + +GPU1: +La | Lb | Lc +---|----|--- +a1 | b1 | c1 + +GPU2: +La | Lb | Lc +---|----|--- +a2 | b2 | c2 +``` + +์ผ๋ฐ˜์ ์ธ DNN ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์ƒ์ƒํ•ด๋ณด๋ฉด ์ด๋Š” ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์™€ ๊ฐ™์€ ์ˆ˜ํ‰ ์Šฌ๋ผ์ด์‹ฑ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ง ์Šฌ๋ผ์ด์‹ฑ์€ ์ „์ฒด ๋ ˆ์ด์–ด ๊ทธ๋ฃน์„ ๋‹ค๋ฅธ GPU์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์‹œ์ž‘์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. + +์ด์ œ ์ด๋Ÿฌํ•œ ๊ฐ๊ฐ์˜ GPU๋Š” DP์—์„œ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ผ๋ฐ˜์ ์ธ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค: +``` +x0 => GPU0 +x1 => GPU1 +x2 => GPU2 +``` + +์ž…๋ ฅ์€ ์ˆ˜์ •๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ผ๋ฐ˜ ๋ชจ๋ธ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋  ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. + +๋จผ์ €, ์ž…๋ ฅ์€ ๋ ˆ์ด์–ด La์— ๋„๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +GPU0์—๋งŒ ์ง‘์ค‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x0์€ ์ˆœ๋ฐฉํ–ฅ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด a0, a1, a2 ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ GPU0์—๋Š” a0๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. GPU1์—์„œ a1์„, GPU2์—์„œ a2๋ฅผ ์ „์†ก๋ฐ›์•„ ๋ชจ๋ธ์˜ ๋ชจ๋“  ์กฐ๊ฐ์„ ํ•˜๋‚˜๋กœ ๋ชจ์๋‹ˆ๋‹ค. + +๋ณ‘๋ ฌ์ ์œผ๋กœ, GPU1์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ x1์„ ๋ฐ›๊ณ  a1๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, a0 ๋ฐ a2 ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ GPU0 ๋ฐ GPU2์—์„œ ์ด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + +GPU2๋„ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ x2๋ฅผ ๋ฐ›๊ณ  GPU0 ๋ฐ GPU1์—์„œ ๊ฐ๊ฐ a0๊ณผ a1์„, ๊ทธ๋ฆฌ๊ณ  ์ž์‹ ์˜ a2์™€ ํ•จ๊ป˜ ์ „์ฒด ํ…์„œ๋ฅผ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค. + +3๊ฐœ์˜ GPU๋Š” ๋ณต์›๋œ ์ „์ฒด ํ…์„œ๋ฅผ ๋ฐ›๊ณ  forward๊ฐ€ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. + +๊ณ„์‚ฐ์ด ์™„๋ฃŒ๋˜๋ฉด ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋Š” ์‚ญ์ œ๋˜๊ณ , ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ๊ณ„์‚ฐ ์ค‘์—๋งŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ณต์›์€ ์‚ฌ์ „ ํŒจ์น˜๋ฅผ ํ†ตํ•ด ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. + +๊ทธ๋ฆฌ๊ณ  ์ „์ฒด ํ”„๋กœ์„ธ์Šค๋Š” ๋ ˆ์ด์–ด Lb์— ๋Œ€ํ•ด ๋ฐ˜๋ณต๋˜๊ณ , ๊ทธ ๋‹ค์Œ Lc๋กœ ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ, ๊ทธ๋‹ค์Œ์€ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ Lc -> Lb -> La๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. + +๊ฐœ์ธ์ ์œผ๋กœ ์ด๊ฒƒ์€ ํšจ์œจ์ ์ธ ๊ทธ๋ฃน ๋ฐฐ๋‚ญ ์—ฌํ–‰์ž์˜ ์ค‘๋Ÿ‰ ๋ถ„๋ฐฐ ์ „๋žต์ฒ˜๋Ÿผ ๋“ค๋ฆฝ๋‹ˆ๋‹ค: + +1. ์‚ฌ๋žŒ A๊ฐ€ ํ…ํŠธ๋ฅผ ์šด๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. +2. ์‚ฌ๋žŒ B๊ฐ€ ๋‚œ๋กœ๋ฅผ ์šด๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. +3. ์‚ฌ๋žŒ C๊ฐ€ ๋„๋ผ๋ฅผ ์šด๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. + +์ด์ œ ๋งค์ผ ๋ฐค ๊ฐ์ž ๊ฐ€์ง„ ๊ฒƒ์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ๊ณต์œ ํ•˜๊ณ , ๊ฐ€์ง€์ง€ ์•Š์€ ๊ฒƒ์€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๋กœ๋ถ€ํ„ฐ ๋ฐ›๊ณ , ์•„์นจ์—๋Š” ํ• ๋‹น๋œ ์œ ํ˜•์˜ ์žฅ๋น„๋ฅผ ์‹ธ๊ณ  ๊ณ„์†ํ•ด์„œ ์—ฌํ–‰์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด Sharded DDP / Zero DP์ž…๋‹ˆ๋‹ค. + +์ด ์ „๋žต์„ ๊ฐ๊ฐ ์ž์‹ ์˜ ํ…ํŠธ, ๋‚œ๋กœ ๋ฐ ๋„๋ผ๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์šด๋ฐ˜ํ•ด์•ผ ํ•˜๋Š” ๋‹จ์ˆœํ•œ ์ „๋žต๊ณผ ๋น„๊ตํ•ด๋ณด๋ฉด ํ›จ์”ฌ ๋น„ํšจ์œจ์ ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด Pytorch์˜ DataParallel (DP ๋ฐ DDP)์ž…๋‹ˆ๋‹ค. + +์ด ์ฃผ์ œ์— ๋Œ€ํ•ด ๋…ผ๋ฌธ์„ ์ฝ์„ ๋•Œ ๋‹ค์Œ ๋™์˜์–ด๋ฅผ ๋งŒ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: Sharded, Partitioned. + +ZeRO๊ฐ€ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ์‹์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด, ํ…์„œ ๋ณ‘๋ ฌํ™”์™€ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ดํ›„์— ์„ค๋ช…๋  ์ˆ˜์ง ๋ชจ๋ธ ๋ณ‘๋ ฌํ™”์™€๋Š” ๋‹ฌ๋ฆฌ ๊ฐ ๋ ˆ์ด์–ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„ํ• /๋ถ„ํ• ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๊ตฌํ˜„: + +- [DeepSpeed](https://www.deepspeed.ai/tutorials/zero/)๋Š” 1๋‹จ๊ณ„ + 2๋‹จ๊ณ„ + 3๋‹จ๊ณ„์˜ ZeRO-DP๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +- [Fairscale](https://github.com/facebookresearch/fairscale/#optimizer-state-sharding-zero)์€ 1๋‹จ๊ณ„ + 2๋‹จ๊ณ„ + 3๋‹จ๊ณ„์˜ ZeRO-DP๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +- [`transformers` ํ†ตํ•ฉ](main_classes/trainer#trainer-integrations) + +## ๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ(์ˆ˜์ง์ ) ๋ฐ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ[[naive-model-parallelism-vertical-and-pipeline-parallelism]] + +Naive Model Parallelism (MP)์€ ๋ชจ๋ธ ๋ ˆ์ด์–ด ๊ทธ๋ฃน์„ ๋‹ค์ค‘ GPU์— ๋ถ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ์ƒ๋Œ€์ ์œผ๋กœ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๋ ˆ์ด์–ด๋ฅผ `.to()`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ์žฅ์น˜๋กœ ์ „ํ™˜ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ํ•ด๋‹น ๋ ˆ์ด์–ด๋กœ ๋“ค์–ด์˜ค๊ณ  ๋‚˜๊ฐˆ ๋•Œ ๋ฐ์ดํ„ฐ๋„ ๋ ˆ์ด์–ด์™€ ๋™์ผํ•œ ์žฅ์น˜๋กœ ์ „ํ™˜๋˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์ˆ˜์ •๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ์ด ๊ทธ๋ ค์ง€๋Š” ๋ฐฉ์‹์ด ๋ ˆ์ด์–ด๋ฅผ ์„ธ๋กœ๋กœ ์Šฌ๋ผ์ด์Šคํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ˆ˜์ง ๋ชจ๋ธ ๋ณ‘๋ ฌํ™”๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์€ 8๋ ˆ์ด์–ด ๋ชจ๋ธ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค: + +``` +=================== =================== +| 0 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | +=================== =================== + gpu0 gpu1 +``` +์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์„ ์ˆ˜์ง์œผ๋กœ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜์—ฌ ๋ ˆ์ด์–ด 0-3์„ GPU0์— ๋ฐฐ์น˜ํ•˜๊ณ  ๋ ˆ์ด์–ด 4-7์„ GPU1์— ๋ฐฐ์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. + +์ด์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ ˆ์ด์–ด 0์—์„œ 1๋กœ, 1์—์„œ 2๋กœ, 2์—์„œ 3์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋™์•ˆ์—๋Š” ์ผ๋ฐ˜์ ์ธ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ ˆ์ด์–ด 3์—์„œ ๋ ˆ์ด์–ด 4๋กœ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•  ๋•Œ๋Š” GPU0์—์„œ GPU1๋กœ ์ด๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ฐธ์—ฌํ•˜๋Š” GPU๊ฐ€ ๋™์ผํ•œ ์ปดํ“จํŒ… ๋…ธ๋“œ(์˜ˆ: ๋™์ผํ•œ ๋ฌผ๋ฆฌ์ ์ธ ๊ธฐ๊ณ„)์— ์žˆ๋Š” ๊ฒฝ์šฐ ์ด ๋ณต์‚ฌ๋Š” ๋งค์šฐ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ GPU๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ปดํ“จํŒ… ๋…ธ๋“œ(์˜ˆ: ์—ฌ๋Ÿฌ ๊ธฐ๊ณ„)์— ์œ„์น˜ํ•œ ๊ฒฝ์šฐ ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๋Š” ์ƒ๋‹นํžˆ ํฌ๊ฒŒ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ ˆ์ด์–ด 4๋ถ€ํ„ฐ 5๋กœ, 6์œผ๋กœ, 7๋กœ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ธ ๋ชจ๋ธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰๋˜๊ณ , 7๋ฒˆ์งธ ๋ ˆ์ด์–ด๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๋ ˆ์ด์–ด 0์œผ๋กœ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋˜๋Š” ๋ ˆ์ด๋ธ”์„ ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด๋กœ ๋ณด๋‚ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์†์‹ค์„ ๊ณ„์‚ฐํ•˜๊ณ  ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ฌธ์ œ์ : +- ์ด ๋ฐฉ์‹์„ "naive" MP๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์ด์œ ๋Š” ์ฃผ์–ด์ง„ ์ƒํ™ฉ์— ํ•˜๋‚˜์˜ GPU๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  GPU๊ฐ€ ์œ ํœด ์ƒํƒœ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹จ์ผ GPU์˜ ๋ฉ”๋ชจ๋ฆฌ ์–‘์„ 4๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€ ํ•˜๋“œ์›จ์–ด๋Š” ๋ฌด์‹œํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์žฅ์น˜ ๊ฐ„ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ์˜ ์˜ค๋ฒ„ํ—ค๋“œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๊ฐœ์˜ 6GB ์นด๋“œ๋Š” naive MP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 1๊ฐœ์˜ 24GB ์นด๋“œ์™€ ๋™์ผํ•œ ํฌ๊ธฐ๋ฅผ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ›„์ž๋Š” ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ํ›ˆ๋ จ์„ ๋” ๋นจ๋ฆฌ ์™„๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์˜ˆ๋ฅผ ๋“ค์–ด 40GB ์นด๋“œ๊ฐ€ ์žˆ๊ณ  45GB ๋ชจ๋ธ์„ ๋งž์ถ”์–ด์•ผ ํ•  ๊ฒฝ์šฐ 4๊ฐœ์˜ 40GB ์นด๋“œ๋กœ ๋งž์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋””์–ธํŠธ์™€ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ ๋•Œ๋ฌธ์— ๊ฐ€๊นŒ์Šค๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค). +- ๊ณต์œ  ์ž„๋ฒ ๋”ฉ์€ GPU ๊ฐ„์— ๋ณต์‚ฌํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌํ™” (PP)์€ ๊ฑฐ์˜ naive MP์™€ ๋™์ผํ•˜์ง€๋งŒ GPU ์œ ํœด ์ƒํƒœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“ค์–ด์˜ค๋Š” ๋ฐฐ์น˜๋ฅผ ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜๋กœ ๋‚˜๋ˆ„๊ณ  ์ธ๊ณต์ ์œผ๋กœ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ GPU๊ฐ€ ๋™์‹œ์— ๊ณ„์‚ฐ์— ์ฐธ์—ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +[GPipe ๋…ผ๋ฌธ](https://ai.googleblog.com/2019/03/introducing-gpipe-open-source-library.html)์—์„œ ๊ฐ€์ ธ์˜จ ๊ทธ๋ฆผ์€ ์ƒ๋‹จ์— naive MP๋ฅผ, ํ•˜๋‹จ์—๋Š” PP๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค: + +![mp-pp](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-gpipe-bubble.png) + +ํ•˜๋‹จ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ PP๊ฐ€ ์œ ํœด ์˜์—ญ์ด ์ ์€ ๊ฒƒ์„ ์‰ฝ๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ํœด ๋ถ€๋ถ„์„ "bubble"์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์ด์–ด๊ทธ๋žจ์˜ ์–‘์ชฝ ๋ถ€๋ถ„์€ ์ฐธ์—ฌํ•˜๋Š” GPU๊ฐ€ 4๊ฐœ์ธ ๋ณ‘๋ ฌ์„ฑ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ฆ‰, 4๊ฐœ์˜ GPU๊ฐ€ ํŒŒ์ดํ”„๋ผ์ธ์— ์ฐธ์—ฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 4๊ฐœ์˜ ํŒŒ์ดํ”„ ๋‹จ๊ณ„ F0, F1, F2 ๋ฐ F3์˜ ์ˆœ๋ฐฉํ–ฅ ๊ฒฝ๋กœ์™€ B3, B2, B1 ๋ฐ B0์˜ ์—ญ๋ฐฉํ–ฅ ๊ฒฝ๋กœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +PP๋Š” ์กฐ์ •ํ•ด์•ผ ํ•  ์ƒˆ๋กœ์šด ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ์ธ `chunks`๋ฅผ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋™์ผํ•œ ํŒŒ์ดํ”„ ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด ์ผ๋ จ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์–ด์„œ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ `chunks=4`๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. GPU0์€ 0, 1, 2 ๋ฐ 3 (F0,0, F0,1, F0,2, F0,3) ๋ฌถ์Œ์—์„œ ๋™์ผํ•œ ์ˆœ๋ฐฉํ–ฅ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋‹ค๋ฅธ GPU๊ฐ€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๊ณ  ์™„๋ฃŒ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งŒ GPU0์ด ๋ฌถ์Œ์˜ ์—ญ์ˆœ์œผ๋กœ 3, 2, 1 ๋ฐ 0 (B0,3, B0,2, B0,1, B0,0) ๊ฒฝ๋กœ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +๊ฐœ๋…์ ์œผ๋กœ ์ด๋Š” ๊ทธ๋ž˜๋””์–ธํŠธ ๋ˆ„์  ๋‹จ๊ณ„ (GAS)์™€ ๋™์ผํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํŒŒ์ดํ† ์น˜์—์„œ๋Š” `chunks`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  DeepSpeed์—์„œ๋Š” ๋™์ผํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ GAS๋กœ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. + +๋ฌถ์Œ์œผ๋กœ ์ธํ•ด PP๋Š” ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜ (MBS)์˜ ๊ฐœ๋…์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. DP๋Š” ์ „์—ญ ๋ฐ์ดํ„ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ DP ์ฐจ์ˆ˜๊ฐ€ 4์ด๊ณ  ์ „์—ญ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1024์ด๋ฉด 256์”ฉ 4๊ฐœ์˜ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋กœ ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค (1024/4). ๊ทธ๋ฆฌ๊ณ  `chunks` (๋˜๋Š” GAS)์˜ ์ˆ˜๊ฐ€ 32์ด๋ฉด ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 8์ด ๋ฉ๋‹ˆ๋‹ค (256/32). ๊ฐ ํŒŒ์ดํ”„๋ผ์ธ ๋‹จ๊ณ„๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜์™€ ํ•จ๊ป˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +DP + PP ์„ค์ •์˜ ์ „์—ญ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด `mbs*chunks*dp_degree` (`8*32*4=1024`)๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋Œ์•„๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +`chunks=1`๋กœ ์„ค์ •ํ•˜๋ฉด ๋งค์šฐ ๋น„ํšจ์œจ์ ์ธ naive MP๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ, ๋งค์šฐ ํฐ `chunks` ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์•„์ฃผ ์ž‘์€ ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ํšจ์œจ์ ์ด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ GPU ํ™œ์šฉ์„ ์œ„ํ•ด ์–ด๋–ค ๊ฐ’์ด ๊ฐ€์žฅ ์ ์ ˆํ•œ์ง€ ์‹คํ—˜์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๋ณด์ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ "dead" ์‹œ๊ฐ„์˜ ๋ฒ„๋ธ”์ด ์กด์žฌํ•˜์—ฌ ๋งˆ์ง€๋ง‰ `forward` ๋‹จ๊ณ„๊ฐ€ `backward` ๋‹จ๊ณ„๊ฐ€ ํŒŒ์ดํ”„๋ผ์ธ์„ ์™„๋ฃŒํ•˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜์ง€๋งŒ, `chunks`์˜ ๊ฐ€์žฅ ์ ์ ˆํ•œ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์˜ ๋ชฉ์ ์€ ๋ชจ๋“  ์ฐธ์—ฌํ•˜๋Š” GPU์—์„œ ๋™์‹œ์— ๊ณ ๋„๋กœ ํ™œ์šฉ๋˜๋Š” GPU ํ™œ์šฉ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์—ฌ ๋ฒ„๋ธ”์˜ ํฌ๊ธฐ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +ํ•ด๊ฒฐ์ฑ…์€ ์ „ํ†ต์ ์ธ ํŒŒ์ดํ”„๋ผ์ธ API์™€ ๋” ํ˜„๋Œ€์ ์ธ ์†”๋ฃจ์…˜์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ ํŒŒ์ดํ”„๋ผ์ธ API ์†”๋ฃจ์…˜๊ณผ ํ˜„๋Œ€์ ์ธ ์†”๋ฃจ์…˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +์ „ํ†ต์ ์ธ ํŒŒ์ดํ”„๋ผ์ธ API ์†”๋ฃจ์…˜: +- ํŒŒ์ดํ† ์น˜ +- FairScale +- DeepSpeed +- Megatron-LM + +ํ˜„๋Œ€์ ์ธ ์†”๋ฃจ์…˜: +- Varuna +- Sagemaker + +์ „ํ†ต์ ์ธ ํŒŒ์ดํ”„๋ผ์ธ API ์†”๋ฃจ์…˜์˜ ๋ฌธ์ œ์ : +- ๋ชจ๋ธ์„ ์ƒ๋‹นํžˆ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ชจ๋“ˆ์˜ ์ •์ƒ์ ์ธ ํ๋ฆ„์„ `nn.Sequential` ์‹œํ€€์Šค๋กœ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ชจ๋ธ์˜ ์„ค๊ณ„๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- ํ˜„์žฌ ํŒŒ์ดํ”„๋ผ์ธ API๋Š” ๋งค์šฐ ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋งค์šฐ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ ์ „๋‹ฌ๋˜๋Š” ๋งŽ์€ ํŒŒ์ด์ฌ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ํŒŒ์ดํ”„๋ผ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•˜๋‚˜์˜ ํ…์„œ ๋˜๋Š” ํ…์„œ์˜ ํŠœํ”Œ์„ ์œ ์ผํ•œ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์œผ๋กœ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ…์„œ๋Š” ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜๋กœ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋กœ ๋ฌถ์„ ๊ฒƒ์ด๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์œผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฐœ์„  ์‚ฌํ•ญ์€ ์—ฌ๊ธฐ์—์„œ ๋…ผ์˜๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/pytorch/pytorch/pull/50693 +- ํŒŒ์ดํ”„ ๋‹จ๊ณ„ ์ˆ˜์ค€์—์„œ ์กฐ๊ฑด๋ถ€ ์ œ์–ด ํ๋ฆ„์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, T5์™€ ๊ฐ™์€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์€ ์กฐ๊ฑด๋ถ€ ์ธ์ฝ”๋” ๋‹จ๊ณ„๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ํ•ด๊ฒฐ์ฑ…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +- ๊ฐ ๋ ˆ์ด์–ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ด ๋‹ค๋ฅธ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์ด ๋˜๋„๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์šฐ๋ฆฌ๋Š” ์•„์ง Varuna์™€ SageMaker๋กœ ์‹คํ—˜ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ํ•ด๋‹น ๋…ผ๋ฌธ๋“ค์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋ฌธ์ œ๋“ค์˜ ๋ชฉ๋ก์„ ๊ทน๋ณตํ–ˆ๊ณ  ์‚ฌ์šฉ์ž์˜ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ํ›จ์”ฌ ์ ๊ฒŒ ํ•„์š”ํ•˜๋‹ค๊ณ  ๋ณด๊ณ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ตฌํ˜„: +- [ํŒŒ์ดํ† ์น˜](https://pytorch.org/docs/stable/pipeline.html) (ํŒŒ์ดํ† ์น˜-1.8์—์„œ ์ดˆ๊ธฐ ์ง€์›, 1.9์—์„œ ์ ์ง„์ ์œผ๋กœ ๊ฐœ์„ ๋˜๊ณ  1.10์—์„œ ๋” ๊ฐœ์„ ๋จ). [์˜ˆ์ œ](https://github.com/pytorch/pytorch/blob/master/benchmarks/distributed/pipeline/pipe.py)๋„ ์ฐธ๊ณ ํ•˜์„ธ์š”. +- [FairScale](https://fairscale.readthedocs.io/en/latest/tutorials/pipe.html) +- [DeepSpeed](https://www.deepspeed.ai/tutorials/pipeline/) +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM)์€ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค - API ์—†์Œ. +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) - ์ด๋Š” AWS์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์†Œ์œ  ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. +- [OSLO](https://github.com/tunib-ai/oslo) - ์ด๋Š” Hugging Face Transformers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋œ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌํ™”์ž…๋‹ˆ๋‹ค. + +๐Ÿค— Transformers ์ƒํƒœ: ์ด ์ž‘์„ฑ ์‹œ์ ์—์„œ ๋ชจ๋ธ ์ค‘ ์–ด๋А ๊ฒƒ๋„ ์™„์ „ํ•œ PP๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. GPT2์™€ T5 ๋ชจ๋ธ์€ naive MP๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์žฅ์• ๋ฌผ์€ ๋ชจ๋ธ์„ `nn.Sequential`๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ชจ๋“  ์ž…๋ ฅ์„ ํ…์„œ๋กœ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ชจ๋ธ์—๋Š” ์ด๋Ÿฌํ•œ ๋ณ€ํ™˜์„ ๋งค์šฐ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋งŽ์€ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๊ธฐํƒ€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•: + +DeepSpeed, Varuna ๋ฐ SageMaker๋Š” [๊ต์ฐจ ํŒŒ์ดํ”„๋ผ์ธ(Interleaved Pipeline)](https://docs.aws.amazon.com/sagemaker/latest/dg/model-parallel-core-features.html) ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +![interleaved-pipeline-execution](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-sagemaker-interleaved-pipeline.png) + +์—ฌ๊ธฐ์„œ๋Š” ๋ฒ„๋ธ”(์œ ํœด ์‹œ๊ฐ„)์„ ์—ญ๋ฐฉํ–ฅ ํŒจ์Šค์— ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค. + +Varuna๋Š” ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ์Šค์ผ€์ค„๋ง์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์Šค์ผ€์ค„์„ ๊ฐœ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. + +OSLO๋Š” `nn.Sequential`๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ  Transformers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌํ™”๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. + +## ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ [[tensor-parallelism]] + +ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์—์„œ๋Š” ๊ฐ GPU๊ฐ€ ํ…์„œ์˜ ์ผ๋ถ€๋ถ„๋งŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ „์ฒด ํ…์„œ๊ฐ€ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ์ „์ฒด ํ…์„œ๋ฅผ ์ง‘๊ณ„ํ•ฉ๋‹ˆ๋‹ค. + +์ด ์„น์…˜์—์„œ๋Š” [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) ๋…ผ๋ฌธ์ธ [Efficient Large-Scale Language Model Training on GPU Clusters](https://huggingface.co/papers/2104.04473)์—์„œ์˜ ๊ฐœ๋…๊ณผ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +Transformer์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ๋Š” fully connected `nn.Linear`์™€ ๋น„์„ ํ˜• ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ธ `GeLU`์ž…๋‹ˆ๋‹ค. + +Megatron ๋…ผ๋ฌธ์˜ ํ‘œ๊ธฐ๋ฒ•์„ ๋”ฐ๋ผ ํ–‰๋ ฌ์˜ ์ ๊ณฑ ๋ถ€๋ถ„์„ `Y = GeLU(XA)`๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ `X`์™€ `Y`๋Š” ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ๋ฒกํ„ฐ์ด๊ณ  `A`๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. + +ํ–‰๋ ฌ ํ˜•ํƒœ๋กœ ๊ณ„์‚ฐ์„ ์‚ดํŽด๋ณด๋ฉด, ํ–‰๋ ฌ ๊ณฑ์…ˆ์„ ๋‹ค์ค‘ GPU๋กœ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +![Parallel GEMM](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_gemm.png) + +๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ `A`๋ฅผ `N`๊ฐœ์˜ GPU์— ๋Œ€ํ•ด ์—ด๋ณ„๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๋ณ‘๋ ฌ๋กœ ํ–‰๋ ฌ ๊ณฑ์…ˆ `XA_1`์—์„œ `XA_n`๊นŒ์ง€ ์ˆ˜ํ–‰ํ•˜๋ฉด `N`๊ฐœ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ `Y_1, Y_2, ..., Y_n`๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ ๋…๋ฆฝ์ ์œผ๋กœ `GeLU`์— ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +![independent GeLU](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-independent-gelu.png) + +์ด ์›๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ GPU ๊ฐ„์˜ ์ž„์˜ ๊นŠ์ด์˜ MLP๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ๋ฅผ ์ƒค๋“œ๋กœ๋ถ€ํ„ฐ ์žฌ๊ตฌ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๊นŒ์ง€๋Š” GPU ๊ฐ„์˜ ๋™๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Megatron-LM ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ์ด์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ๊ทธ๋ฆผ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: +![parallel shard processing](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_shard_processing.png) + +๋‹ค์ค‘ ํ—ค๋“œ ์–ดํ…์…˜ ๋ ˆ์ด์–ด์˜ ๋ณ‘๋ ฌํ™”๋Š” ๋”์šฑ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ๋…๋ฆฝ์ ์ธ ๋‹ค์ค‘ ํ—ค๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ ๋ณ‘๋ ฌํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค! +![parallel self-attention](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-tp-parallel_self_attention.png) + +ํŠน๋ณ„ ๊ณ ๋ ค์‚ฌํ•ญ: TP๋Š” ๋งค์šฐ ๋น ๋ฅธ ๋„คํŠธ์›Œํฌ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋…ธ๋“œ์—์„œ TP๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋…ธ๋“œ์— 4๊ฐœ์˜ GPU๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ TP์˜ ์ตœ๋Œ€ ์ฐจ์ˆ˜๋Š” 4์ž…๋‹ˆ๋‹ค. TP ์ฐจ์ˆ˜๊ฐ€ 8์ธ ๊ฒฝ์šฐ ์ตœ์†Œํ•œ 8๊ฐœ์˜ GPU๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ด ์„น์…˜์€ ์›๋ž˜์˜ [๋” ์ž์„ธํ•œ TP ๊ฐœ์š”](https://github.com/huggingface/transformers/issues/10321#issuecomment-783543530)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. +์ž‘์„ฑ์ž๋Š” [@anton-l](https://github.com/anton-l)์ž…๋‹ˆ๋‹ค. + +SageMaker๋Š” ๋” ํšจ์œจ์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด TP์™€ DP๋ฅผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. + +๋Œ€์ฒด ์ด๋ฆ„: +- DeepSpeed๋Š” ์ด๋ฅผ [ํ…์„œ ์Šฌ๋ผ์ด์‹ฑ](https://www.deepspeed.ai/training/#model-parallelism)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. + +๊ตฌํ˜„: +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM)์€ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์— ๋งค์šฐ ํŠนํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +- [parallelformers](https://github.com/tunib-ai/parallelformers) (ํ˜„์žฌ๋Š” ์ถ”๋ก ์—๋งŒ ํ•ด๋‹น) +- [SageMaker](https://huggingface.co/papers/2111.05972) - ์ด๋Š” AWS์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์†Œ์œ  ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. +- [OSLO](https://github.com/tunib-ai/oslo)์€ Transformers๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ…์„œ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๊ตฌํ˜„์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers ํ˜„ํ™ฉ: +- core: ์•„์ง ํ•ต์‹ฌ ๋ถ€๋ถ„์— ๊ตฌํ˜„๋˜์ง€ ์•Š์Œ +- ๊ทธ๋Ÿฌ๋‚˜ ์ถ”๋ก ์„ ํ•˜๋ ค๋ฉด [parallelformers](https://github.com/tunib-ai/parallelformers)๊ฐ€ ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ต์‹ฌ ๋ถ€๋ถ„์— ๊ตฌํ˜„๋˜๊ธฐ ์ „๊นŒ์ง€ ๊ทธ๋“ค์˜ ๊ฒƒ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ›ˆ๋ จ ๋ชจ๋“œ๋„ ์ง€์›๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. +- Deepspeed-Inference๋Š” CUDA ์ปค๋„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋งค์šฐ ๋น ๋ฅธ ์ถ”๋ก  ๋ชจ๋“œ์—์„œ BERT, GPT-2 ๋ฐ GPT-Neo ๋ชจ๋ธ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์—ฌ๊ธฐ](https://www.deepspeed.ai/tutorials/inference-tutorial/)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## DP+PP [[dppp]] + +DeepSpeed [pipeline tutorial](https://www.deepspeed.ai/tutorials/pipeline/)์—์„œ ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์€ DP์™€ PP๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +![dp-pp-2d](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-zero-dp-pp.png) + +์—ฌ๊ธฐ์„œ DP ๋žญํฌ 0์€ GPU2๋ฅผ ๋ณด์ง€ ๋ชปํ•˜๊ณ , DP ๋žญํฌ 1์€ GPU3์„ ๋ณด์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. DP์—๊ฒŒ๋Š” ๋”ฑ 2๊ฐœ์˜ GPU์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. GPU0์€ PP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ GPU2์—๊ฒŒ ์ผ๋ถ€ ์ž‘์—…์„ "๋น„๋ฐ€๋ฆฌ์—" ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GPU1๋„ GPU3์„ ๋„์›€์œผ๋กœ ์‚ผ์•„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘์—…ํ•ฉ๋‹ˆ๋‹ค. + +๊ฐ ์ฐจ์›๋งˆ๋‹ค ์ ์–ด๋„ 2๊ฐœ์˜ GPU๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ตœ์†Œํ•œ 4๊ฐœ์˜ GPU๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +๊ตฌํ˜„: +- [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) +- [OSLO](https://github.com/tunib-ai/oslo) + +๐Ÿค— Transformers ํ˜„ํ™ฉ: ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์Œ + +## DP+PP+TP [[dppptp]] + +๋” ํšจ์œจ์ ์ธ ํ›ˆ๋ จ์„ ์œ„ํ•ด PP์™€ TP ๋ฐ DP๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ 3D ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +![dp-pp-tp-3d](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-deepspeed-3d.png) + +์ด ๋‹ค์ด์–ด๊ทธ๋žจ์€ [3D parallelism: Scaling to trillion-parameter models](https://www.microsoft.com/en-us/research/blog/deepspeed-extreme-scale-model-training-for-everyone/)์ด๋ผ๋Š” ๋ธ”๋กœ๊ทธ ๊ธ€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฐ ์ฐจ์›๋งˆ๋‹ค ์ ์–ด๋„ 2๊ฐœ์˜ GPU๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ตœ์†Œํ•œ 8๊ฐœ์˜ GPU๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +๊ตฌํ˜„: +- [DeepSpeed](https://github.com/deepspeedai/DeepSpeed) - DeepSpeed๋Š” ๋”์šฑ ํšจ์œจ์ ์ธ DP์ธ ZeRO-DP๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. +- [Megatron-LM](https://github.com/NVIDIA/Megatron-LM) +- [Varuna](https://github.com/microsoft/varuna) +- [SageMaker](https://huggingface.co/papers/2111.05972) +- [OSLO](https://github.com/tunib-ai/oslo) + +๐Ÿค— Transformers ํ˜„ํ™ฉ: ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์Œ. PP์™€ TP๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +## ZeRO DP+PP+TP [[zero-dppptp]] + +DeepSpeed์˜ ์ฃผ์š” ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜๋Š” DP์˜ ํ™•์žฅ์ธ ZeRO์ž…๋‹ˆ๋‹ค. ZeRO-DP์— ๋Œ€ํ•ด ์ด๋ฏธ [ZeRO Data Parallelism](#zero-data-parallelism)์—์„œ ๋…ผ์˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Š” PP๋‚˜ TP๋ฅผ ํ•„์š”๋กœํ•˜์ง€ ์•Š๋Š” ๋…๋ฆฝ์ ์ธ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ PP์™€ TP์™€ ๊ฒฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +ZeRO-DP๊ฐ€ PP์™€ (์„ ํƒ์ ์œผ๋กœ TP์™€) ๊ฒฐํ•ฉ๋˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ZeRO ๋‹จ๊ณ„ 1(์˜ตํ‹ฐ๋งˆ์ด์ € ๋ถ„ํ• )๋งŒ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. + +์ด๋ก ์ ์œผ๋กœ๋Š” ZeRO ๋‹จ๊ณ„ 2(๊ทธ๋ผ๋””์–ธํŠธ ๋ถ„ํ• )๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ด๋Š” ์„ฑ๋Šฅ์— ๋‚˜์œ ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜๋งˆ๋‹ค ๊ทธ๋ผ๋””์–ธํŠธ๋ฅผ ์ƒค๋”ฉํ•˜๊ธฐ ์ „์— ์ถ”๊ฐ€์ ์ธ ๋ฆฌ๋“€์Šค-์Šค์บํ„ฐ ์ปฌ๋ ‰ํ‹ฐ๋ธŒ๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด๋Š” ์ž ์žฌ์ ์œผ๋กœ ์ƒ๋‹นํ•œ ํ†ต์‹  ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์˜ ํŠน์„ฑ์ƒ ์ž‘์€ ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉฐ, ์‚ฐ์ˆ  ์—ฐ์‚ฐ ๊ฐ•๋„(๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ)๋ฅผ ๊ท ํ˜• ์žˆ๊ฒŒ ์œ ์ง€ํ•˜๋ฉด์„œ ํŒŒ์ดํ”„๋ผ์ธ ๋ฒ„๋ธ”(๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜ ์ˆ˜)์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์— ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํ†ต์‹  ๋น„์šฉ์€ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋˜ํ•œ, PP๋กœ ์ธํ•ด ์ •์ƒ๋ณด๋‹ค ์ ์€ ์ˆ˜์˜ ๋ ˆ์ด์–ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์€ ํฌ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. PP๋Š” ์ด๋ฏธ ๊ทธ๋ž˜๋””์–ธํŠธ ํฌ๊ธฐ๋ฅผ ``1/PP``๋กœ ์ค„์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ž˜๋””์–ธํŠธ ์ƒค๋”ฉ์˜ ์ ˆ์•ฝ ํšจ๊ณผ๋Š” ์ˆœ์ˆ˜ DP๋ณด๋‹ค๋Š” ๋ฏธ๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +ZeRO ๋‹จ๊ณ„ 3๋„ ๊ฐ™์€ ์ด์œ ๋กœ ์ข‹์€ ์„ ํƒ์ด ์•„๋‹™๋‹ˆ๋‹ค - ๋” ๋งŽ์€ ๋…ธ๋“œ ๊ฐ„ ํ†ต์‹ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +๊ทธ๋ฆฌ๊ณ  ZeRO๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ด์ ์€ ZeRO-Offload์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ๊ณ„ 1์ด๋ฏ€๋กœ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋ฅผ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ตฌํ˜„: +- [Megatron-DeepSpeed](https://github.com/microsoft/Megatron-DeepSpeed) ๋ฐ [BigScience์˜ Megatron-Deepspeed](https://github.com/bigscience-workshop/Megatron-DeepSpeed), ์ด์ „ ์ €์žฅ์†Œ์˜ ํฌํฌ์ž…๋‹ˆ๋‹ค. +- [OSLO](https://github.com/tunib-ai/oslo) + +์ค‘์š”ํ•œ ๋…ผ๋ฌธ: + +- [Using DeepSpeed and Megatron to Train Megatron-Turing NLG 530B, A Large-Scale Generative Language Model]( +https://huggingface.co/papers/2201.11990) + +๐Ÿค— Transformers ํ˜„ํ™ฉ: ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์Œ, PP์™€ TP๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +## FlexFlow [[flexflow]] + +[FlexFlow](https://github.com/flexflow/FlexFlow)๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋ณ‘๋ ฌํ™” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. + +๋…ผ๋ฌธ: ["Beyond Data and Model Parallelism for Deep Neural Networks" by Zhihao Jia, Matei Zaharia, Alex Aiken](https://huggingface.co/papers/1807.05358) + +์ด๋Š” Sample-Operator-Attribute-Parameter๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์ผ์ข…์˜ 4D ๋ณ‘๋ ฌํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +1. Sample = ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” (์ƒ˜ํ”Œ๋ณ„ ๋ณ‘๋ ฌ) +2. Operator = ๋‹จ์ผ ์—ฐ์‚ฐ์„ ์—ฌ๋Ÿฌ ํ•˜์œ„ ์—ฐ์‚ฐ์œผ๋กœ ๋ณ‘๋ ฌํ™” +3. Attribute = ๋ฐ์ดํ„ฐ ๋ณ‘๋ ฌํ™” (๊ธธ์ด๋ณ„ ๋ณ‘๋ ฌ) +4. Parameter = ๋ชจ๋ธ ๋ณ‘๋ ฌํ™” (์ˆ˜ํ‰ ๋˜๋Š” ์ˆ˜์ง๊ณผ ๊ด€๊ณ„์—†์ด) + +์˜ˆ์‹œ: +* Sample + +512 ๊ธธ์ด์˜ 10๊ฐœ์˜ ๋ฐฐ์น˜๋ฅผ ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ sample ์ฐจ์›์œผ๋กœ 2๊ฐœ์˜ ์žฅ์น˜์— ๋ณ‘๋ ฌํ™”ํ•˜๋ฉด, 10 x 512๋Š” 5 x 2 x 512๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. + +* Operator + +๋ ˆ์ด์–ด ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด, ์šฐ์„  std๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ๋กœ mean์„ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๊ทœํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Operator ๋ณ‘๋ ฌํ™”๋Š” std์™€ mean์„ ๋ณ‘๋ ฌ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ operator ์ฐจ์›์œผ๋กœ 2๊ฐœ์˜ ์žฅ์น˜ (cuda:0, cuda:1)์— ๋ณ‘๋ ฌํ™”ํ•˜๋ฉด, ๋จผ์ € ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋‘ ์žฅ์น˜๋กœ ๋ณต์‚ฌํ•œ ๋‹ค์Œ cuda:0์—์„œ std๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  cuda:1์—์„œ ๋™์‹œ์— mean์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + +* Attribute + +512 ๊ธธ์ด์˜ 10๊ฐœ์˜ ๋ฐฐ์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ attribute ์ฐจ์›์œผ๋กœ 2๊ฐœ์˜ ์žฅ์น˜์— ๋ณ‘๋ ฌํ™”ํ•˜๋ฉด, 10 x 512๋Š” 10 x 2 x 256์ด ๋ฉ๋‹ˆ๋‹ค. + +* Parameter + +์ด๋Š” tensor ๋ชจ๋ธ ๋ณ‘๋ ฌํ™” ๋˜๋Š” naive layer-wise ๋ชจ๋ธ ๋ณ‘๋ ฌํ™”์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. + +![flex-flow-soap](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/parallelism-flexflow.jpeg) + +์ด ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ค‘์š”ํ•œ ์ ์€ (1) GPU/TPU/CPU ๋Œ€ (2) RAM/DRAM ๋Œ€ (3) ๋น ๋ฅธ ์ธํŠธ๋ผ-์ปค๋„ฅํŠธ ๋Œ€ ๋А๋ฆฐ ์ธํ„ฐ-์ปค๋„ฅํŠธ์™€ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์–ด๋””์—์„œ ์–ด๋–ค ๋ณ‘๋ ฌํ™”๋ฅผ ์‚ฌ์šฉํ• ์ง€๋ฅผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ ์œผ๋กœ ์ž๋™์œผ๋กœ ์ตœ์ ํ™”ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +ํ•˜๋‚˜ ๋งค์šฐ ์ค‘์š”ํ•œ ์ธก๋ฉด์€ FlexFlow๊ฐ€ ์ •์ ์ด๊ณ  ๊ณ ์ •๋œ ์›Œํฌ๋กœ๋“œ๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์— ๋Œ€ํ•œ DNN ๋ณ‘๋ ฌํ™”๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋™์ ์ธ ๋™์ž‘์„ ๊ฐ€์ง„ ๋ชจ๋ธ์€ ๋ฐ˜๋ณต๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ณ‘๋ ฌํ™” ์ „๋žต์„ ์„ ํ˜ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋”ฐ๋ผ์„œ ์ด ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์žฅ์ ์€ ์„ ํƒํ•œ ํด๋Ÿฌ์Šคํ„ฐ์—์„œ 30๋ถ„ ๋™์•ˆ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‹คํ–‰ํ•˜๊ณ  ์ด ํŠน์ • ํ™˜๊ฒฝ์„ ์ตœ์ ์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์ƒ์˜ ์ „๋žต์„ ์ œ์•ˆํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ€ํ’ˆ์„ ์ถ”๊ฐ€/์ œ๊ฑฐ/๊ต์ฒดํ•˜๋ฉด ์‹คํ–‰ํ•˜๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๊ณ„ํš์„ ๋‹ค์‹œ ์ตœ์ ํ™”ํ•œ ํ›„ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์„ค์ •์€ ์ž์ฒด์ ์ธ ์‚ฌ์šฉ์ž ์ •์˜ ์ตœ์ ํ™”๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers ํ˜„ํ™ฉ: ์•„์ง ํ†ตํ•ฉ๋˜์ง€ ์•Š์Œ. ์ด๋ฏธ [transformers.utils.fx](https://github.com/huggingface/transformers/blob/master/src/transformers/utils/fx.py)๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์„ FX-์ถ”์ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” FlexFlow์˜ ์„ ํ–‰ ์กฐ๊ฑด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ FlexFlow๊ฐ€ ์šฐ๋ฆฌ์˜ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + +## ์–ด๋–ค ์ „๋žต์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ์š”? [[which-strategy-to-use-when]] + +๋‹ค์Œ์€ ์–ด๋–ค ๋ณ‘๋ ฌํ™” ์ „๋žต์„ ์–ธ์ œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋งค์šฐ ๋Œ€๋žต์ ์ธ ๊ฐœ์š”์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ชฉ๋ก์˜ ์ฒซ ๋ฒˆ์งธ ์ „๋žต์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๋น ๋ฆ…๋‹ˆ๋‹ค. + +**โ‡จ ๋‹จ์ผ GPU** + +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž๋Š” ๊ฒฝ์šฐ: + + 1. ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ + +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + + 1. ZeRO + CPU ๋ฐ ์˜ต์…˜์œผ๋กœ NVMe ์–ธ๋กœ๋“œ + 2. ์œ„์™€ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋˜, ๊ฐ€์žฅ ํฐ ๋ ˆ์ด์–ด๊ฐ€ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ Memory Centric Tiling(์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ์ฐธ์กฐ)์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉ + +* ๊ฐ€์žฅ ํฐ ๋ ˆ์ด์–ด๊ฐ€ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + +1. ZeRO - [Memory Centric Tiling](https://deepspeed.readthedocs.io/en/latest/zero3.html#memory-centric-tiling) (MCT) ํ™œ์„ฑํ™”. ์ด๋ฅผ ํ†ตํ•ด ํฌ๊ธฐ๊ฐ€ ๋งค์šฐ ํฐ ๋ ˆ์ด์–ด๋ฅผ ์ž„์˜๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MCT๋Š” GPU์— ํ™œ์„ฑํ™”๋œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ์ค„์ด์ง€๋งŒ ํ™œ์„ฑํ™” ๋ฉ”๋ชจ๋ฆฌ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ž‘์„ฑ ๊ธฐ์ค€์œผ๋กœ ์ด ์š”๊ตฌ์‚ฌํ•ญ์€ ๋งค์šฐ ๋“œ๋ฌผ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ `torch.nn.Linear`๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +**โ‡จ ๋‹จ์ผ ๋…ธ๋“œ / ๋‹ค์ค‘ GPU** + +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž๋Š” ๊ฒฝ์šฐ: + + 1. DDP - ๋ถ„์‚ฐ DP + 2. ZeRO - ์ƒํ™ฉ๊ณผ ๊ตฌ์„ฑ์— ๋”ฐ๋ผ ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +* ๋ชจ๋ธ์ด ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + + 1. PP + 2. ZeRO + 3. TP + + NVLINK ๋˜๋Š” NVSwitch๋ฅผ ํ†ตํ•œ ๋งค์šฐ ๋น ๋ฅธ ์ธํŠธ๋ผ-๋…ธ๋“œ ์—ฐ๊ฒฐ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ด ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๊ฑฐ์˜ ๋™๋“ฑํ•  ๊ฒƒ์ด๋ฉฐ, ์ด๋Ÿฌํ•œ ์—ฐ๊ฒฐ์ด ์—†๋Š” ๊ฒฝ์šฐ PP๊ฐ€ TP๋‚˜ ZeRO๋ณด๋‹ค ๋น ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ TP์˜ ์ฐจ์ˆ˜๋„ ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์„ค์ •์—์„œ ์šฐ์Šน์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์‹คํ—˜ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. + + TP๋Š” ๊ฑฐ์˜ ํ•ญ์ƒ ๋‹จ์ผ ๋…ธ๋“œ ๋‚ด์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, TP ํฌ๊ธฐ <= ๋…ธ๋“œ๋‹น GPU ์ˆ˜์ž…๋‹ˆ๋‹ค. + +* ๊ฐ€์žฅ ํฐ ๋ ˆ์ด์–ด๊ฐ€ ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ: + + 1. ZeRO๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ - PP๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ TP๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + 2. ZeRO๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, "๋‹จ์ผ GPU"์˜ ํ•ญ๋ชฉ๊ณผ ๋™์ผํ•œ ํ•ญ๋ชฉ ์ฐธ์กฐ + + +**โ‡จ ๋‹ค์ค‘ ๋…ธ๋“œ / ๋‹ค์ค‘ GPU** + +* ๋น ๋ฅธ ๋…ธ๋“œ ๊ฐ„ ์—ฐ๊ฒฐ์ด ์žˆ๋Š” ๊ฒฝ์šฐ: + + 1. ZeRO - ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ˆ˜์ •์ด ๊ฑฐ์˜ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + 2. PP+TP+DP - ํ†ต์‹ ์ด ์ ์ง€๋งŒ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋Œ€๊ทœ๋ชจ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +* ๋А๋ฆฐ ๋…ธ๋“œ ๊ฐ„ ์—ฐ๊ฒฐ ๋ฐ GPU ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ: + + 1. DP+PP+TP+ZeRO-1 diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_one.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_one.md new file mode 100644 index 0000000000000000000000000000000000000000..656da59adbcde0f115a86663861d1699bb8cfc9c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_gpu_one.md @@ -0,0 +1,294 @@ + + +# GPU[[gpu]] + +GPU๋Š” ๋†’์€ ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์—ญํญ๊ณผ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ ๋•๋ถ„์— ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ ํ•™์Šต์— ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. GPU ์‚ฌ์–‘๊ณผ ๋ชจ๋ธ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์ˆ˜์‹ญ์–ต ๊ฐœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ๋„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ GPU ๋ฉ”๋ชจ๋ฆฌ ํ™œ์šฉ๋„(๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Ÿ‰/ํ•™์Šต ์‹œ๊ฐ„)์™€ ํ•™์Šต ์†๋„ ์‚ฌ์ด์—์„œ ์ตœ์ ์˜ ๊ท ํ˜•์„ ์ฐพ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” Transformers์™€ PyTorch์—์„œ GPU๋ฅผ ํ™œ์šฉํ•ด ๋ชจ๋ธ์„ ํšจ์œจ์ ์œผ๋กœ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, ์ด ๊ธฐ๋Šฅ๋“ค์„ ์กฐํ•ฉํ•ด์„œ ํ•™์Šต์„ ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ž์‹ ์˜ ํ•™์Šต ์‹œ๋‚˜๋ฆฌ์˜ค์— ์ ํ•ฉํ•œ ๊ธฐ๋Šฅ์„ ๋น ๋ฅด๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +| ๊ธฐ๋Šฅ | ํ•™์Šต ์†๋„ ๊ฐ€์† | ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ ˆ์•ฝ | +| --------------------------- | --------- | ------------- | +| ๋ฐฐ์น˜ ํฌ๊ธฐ | ์˜ˆ | ์˜ˆ | +| ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  | ์•„๋‹ˆ์š” | ์˜ˆ | +| ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ… | ์•„๋‹ˆ์š” | ์˜ˆ | +| ํ˜ผํ•ฉ ์ •๋ฐ€๋„ | ์˜ˆ | ์กฐ๊ฑด๋ถ€ | +| ์˜ตํ‹ฐ๋งˆ์ด์ € | ์˜ˆ | ์˜ˆ | +| ๋ฐ์ดํ„ฐ ์‚ฌ์ „ ์ ์žฌ | ์˜ˆ | ์•„๋‹ˆ์š” | +| torch_empty_cache_steps | ์•„๋‹ˆ์š” | ์˜ˆ | +| torch.compile | ์˜ˆ | ์•„๋‹ˆ์š” | +| ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜ (SDPA) | ์˜ˆ | ์˜ˆ | + +## Trainer[[trainer]] + +Trainer๋Š” [`TrainingArguments`]๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์„น์…˜์—์„œ๋Š” ํ•™์Šต ์ตœ์ ํ™”์— ํŠนํžˆ ์œ ์šฉํ•œ ์ฃผ์š” ๊ธฐ๋Šฅ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. + +### ๋ฐฐ์น˜ ํฌ๊ธฐ[[batch-size]] + +๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” GPU ํ•™์Šต ํšจ์œจ์„ ์ขŒ์šฐํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜๋กœ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ํ•™์Šต ์†๋„์— ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ค๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ํฌ๊ฒŒ ํ•˜๋ฉด GPU์˜ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์„ ๊ทน๋Œ€ํ™”ํ•˜์—ฌ ํ•™์Šต ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ 8, 64, 128, 256, 512์ฒ˜๋Ÿผ 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” GPU ์‚ฌ์–‘๊ณผ ๋ชจ๋ธ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. + +๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” [`TrainingArguments`]์˜ [`~TrainingArguments.per_device_train_batch_size`] ์˜ต์…˜์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=256, + per_device_eval_batch_size=256, +) +``` + +์„ฑ๋Šฅ, ์ž…๋ ฅ ํ”ผ์ฒ˜ ์ˆ˜์™€ ์ถœ๋ ฅ ๋‰ด๋Ÿฐ ์ˆ˜, ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ์„ฑ๋Šฅ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์— ๋Œ€ํ•ด์„œ๋Š” NVIDIA [Performance](https://docs.nvidia.com/deeplearning/performance/dl-performance-fully-connected/index.html#input-features) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€ GPU์—์„œ ์‹คํ–‰๋˜๋Š” General Matrix Multiplications(GEMMs)์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํด์ˆ˜๋ก ๋ณ‘๋ ฌํ™”์™€ ํšจ์œจ์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. + +๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ GPU์— ๋”ฐ๋ฅธ ์ตœ์ ์˜ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์„ ํƒํ•ด ํ…์„œ ๊ณฑ์…ˆ ์†๋„๋ฅผ ๊ทน๋Œ€ํ™”ํ•˜๋ ค๋ฉด, [Tensor Core Requirements](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#requirements-tc) ์„น์…˜์„ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์˜ˆ์‹œ๋กœ, fp16์—์„œ๋Š” 8์˜ ๋ฐฐ์ˆ˜๊ฐ€ ๊ถŒ์žฅ๋˜์ง€๋งŒ, A100 GPU์—์„œ๋Š” 64์˜ ๋ฐฐ์ˆ˜๊ฐ€ ๋” ์ ํ•ฉํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋งˆ์ง€๋ง‰์œผ๋กœ, ์ž‘์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” [Dimension Quantization Effects](https://docs.nvidia.com/deeplearning/performance/dl-performance-matrix-multiplication/index.html#dim-quantization)๋ฅผ ๊ณ ๋ คํ•˜์„ธ์š”. ํ–‰๋ ฌ ์ฐจ์›์ด GPU ์Šค๋ ˆ๋“œ ๋ธ”๋ก์˜ ํƒ€์ผ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š์œผ๋ฉด ํƒ€์ผ ์–‘์žํ™”๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ GPU ์ž์›์„ ์ถฉ๋ถ„ํžˆ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ์ด ํƒ€์ผ ํฌ๊ธฐ๋กœ ์ •ํ™•ํžˆ ๋‚˜๋‰˜๋„๋ก ์˜ฌ๋ฐ”๋ฅธ ๋ฐฐ์น˜ ํฌ๊ธฐ ๋ฐฐ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋ฉฐ ํ•™์Šต ์†๋„๊ฐ€ ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. + +### ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ [[gradient-accumulation]] + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ ์€ ๋ฉ”๋ชจ๋ฆฌ ์ œ์•ฝ์„ ๊ทน๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋‹จ์ผ GPU์— ๋งž์ง€ ์•Š๋Š” ๋งค์šฐ ํฐ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ „์— ์—ฌ๋Ÿฌ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๊ฑธ์ณ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ๋ˆ„์ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, ์ €์žฅํ•ด์•ผ ํ•˜๋Š” ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ˆ˜๊ฐ€ ์ค„์–ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ค„์–ด๋“ค๊ณ , ์ผ๋ฐ˜์ ์œผ๋กœ ํ•˜๋‚˜์˜ ๋ฐฐ์น˜์—์„œ๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹๋ณด๋‹ค ๋” ํฐ ์œ ํšจ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ถ”๊ฐ€์ ์ธ ์ˆœ์ „ํŒŒ์™€ ์—ญ์ „ํŒŒ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•™์Šต ์†๋„๊ฐ€ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [`TrainingArguments.per_device_train_batch_size`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +# ํšจ์œจ์ ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ 64 +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, +) +``` + +ํ•™์Šต ์†๋„๊ฐ€ ๋А๋ ค์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  ๋‹จ๊ณ„๋ฅผ ๋„ˆ๋ฌด ํฌ๊ฒŒ ์„ค์ •ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”, GPU์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 4๋ผ๋ฉด GPU์˜ ํšจ์œจ์ ์ธ ์‚ฌ์šฉ์„ ์œ„ํ•ด ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 4๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +| ๋ฐฐ์น˜ ํฌ๊ธฐ | ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  ๋‹จ๊ณ„ | ํšจ์œจ์ ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ | | +| --------- | ---------------------- | ------------------ | --- | +| 1 | 64 | 64 | ๐Ÿ‘Ž | +| 4 | 16 | 64 | ๐Ÿ‘ | + +### ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ…[[gradient-checkpointing]] + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ์ฒดํฌํฌ์ธํŒ…์€ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ผ๋ถ€ ์ค‘๊ฐ„ ํ™œ์„ฑํ™” ๊ฐ’๋งŒ ์ €์žฅํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋‹ค์‹œ ๊ณ„์‚ฐํ•ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ˆœ์ „ํŒŒ ๊ณผ์ •์—์„œ ๋ชจ๋“  ์ค‘๊ฐ„ ํ™œ์„ฑํ™” ๊ฐ’์„ ์ €์žฅํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ํ•™์Šต ์†๋„๊ฐ€ ์•ฝ 20% ๋А๋ ค์ง€๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [`~TrainingArguments.gradient_checkpointing`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, +) +``` + +### ํ˜ผํ•ฉ ์ •๋ฐ€๋„[[mixed-precision]] + +ํ˜ผํ•ฉ ์ •๋ฐ€๋„๋Š” ์ผ๋ถ€ ๊ณ„์‚ฐ์„ ๋ฐ˜์ •๋ฐ€๋„(fp16)๋กœ, ๋‚˜๋จธ์ง€๋ฅผ ์ „์ •๋ฐ€๋„(fp32)๋กœ ์ˆ˜ํ–‰ํ•ด ํ•™์Šต ์†๋„๋ฅผ ๋†’์ด๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ˜์ •๋ฐ€๋„ ๊ณ„์‚ฐ์€ ์ „์ •๋ฐ€๋„๋ณด๋‹ค ๊ณ„์‚ฐ๋Ÿ‰์ด ์ ์–ด ๋” ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•œํŽธ, ์ „์ •๋ฐ€๋„๋กœ ์ผ๋ถ€ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ •ํ™•๋„๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + +ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์˜ ์ฃผ์š” ์žฅ์ ์€ ํ™œ์„ฑํ™” ๊ฐ’์„ fp16์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +fp16 ์ž๋ฃŒํ˜•์œผ๋กœ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [`~TrainingArguments.fp16`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + fp16=True. +) +``` + +fp16์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์— ์ตœ์ ํ™”๋œ ๋ฐฉ์‹์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Š” fp16์œผ๋กœ ๊ณ„์‚ฐ๋œ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๊ฐ€ ์ตœ์ ํ™” ๋‹จ๊ณ„์—์„œ fp32๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์„ ๋•Œ๋Š”, GPU์— ๋‘ ๊ฐ€์ง€ ์ž๋ฃŒํ˜•(fp16, fp32)์ด ์ ์žฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋งŽ์€ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. + + + +[bf16](https://cloud.google.com/blog/products/ai-machine-learning/bfloat16-the-secret-to-high-performance-on-cloud-tpus)์€ ์ผ๋ถ€ ์ •๋ฐ€๋„๋ฅผ ํฌ๊ธฐํ•˜๋Š” ๋Œ€์‹ , ํ›จ์”ฌ ๋” ๋„“์€ ๋™์  ๋ฒ”์œ„๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์˜ค๋ฒ„ํ”Œ๋กœ์™€ ์–ธ๋”ํ”Œ๋กœ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. bf16์€ fp16๊ณผ ๋‹ฌ๋ฆฌ ์†์‹ค ์Šค์ผ€์ผ๋ง ๊ธฐ๋ฒ•์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. bf16์€ NVIDIA์˜ Ampere ์•„ํ‚คํ…์ฒ˜ ์ด์ƒ์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +bf16 ์ž๋ฃŒํ˜•์œผ๋กœ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [`~TrainingArguments.bf16`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, +) +``` + + + + +[tf32](https://blogs.nvidia.com/blog/tensorfloat-32-precision-format/)๋Š” NVIDIA Ampere GPU์—์„œ ํ•ฉ์„ฑ๊ณฑ๊ณผ ํ–‰๋ ฌ๊ณฑ ์ž…๋ ฅ์„ tf32๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ชจ๋“œ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋“  ์ €์žฅ๊ณผ ์—ฐ์‚ฐ์€ fp32๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด tf32๋Š” fp32์™€ ๋™์ผํ•œ ๋ฒ”์œ„, fp16๊ณผ ๋™์ผํ•œ ์ •๋ฐ€๋„, ๊ทธ๋ฆฌ๊ณ  bf16๋ณด๋‹ค ๋” ๋†’์€ ์ •๋ฐ€๋„๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tf32๋ฅผ fp16 ๋˜๋Š” bf16 ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต๊ณผ ๊ฒฐํ•ฉํ•˜๋ฉด ์ฒ˜๋ฆฌ๋Ÿ‰์„ 16๋ฐฐ ํ–ฅ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +tf32๋Š” NVIDIA Ampere GPU์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™œ์„ฑํ™”๋˜์–ด ์žˆ์ง€๋งŒ, fp32 ํ•™์Šต ๋˜๋Š” ์ถ”๋ก  ์ฝ”๋“œ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ํ™œ์„ฑํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +import torch +torch.backends.cuda.matmul.allow_tf32 = True +torch.backends.cudnn.allow_tf32 = True +``` + +tf32 ๋ชจ๋“œ์—์„œ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [tf32()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.tf32) ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True. + tf32=True, +) +``` + + + + +### ์˜ตํ‹ฐ๋งˆ์ด์ €[[optimizers]] + +Transformers๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ PyTorch์˜ [AdamW (adamw_torch)](https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html) ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๊ณผ๊ฑฐ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ์˜ ๊ฐ€์ค‘ ํ‰๊ท ์„ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜์— ๋น„๋ก€ํ•œ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งค์šฐ ํฐ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌ๋ฉด ๋‹ค๋ฅธ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [NVIDIA](https://github.com/NVIDIA/apex) ๋˜๋Š” [AMD](https://github.com/ROCm/apex)์— [Apex](https://nvidia.github.io/apex/index.html)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋ฉด, ๋ชจ๋“  AdamW ์˜ตํ‹ฐ๋งˆ์ด์ € ์ค‘ `adamw_apex_fused` ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋น ๋ฅธ ํ•™์Šต ์†๋„๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” [`TrainingArguments`]์—์„œ [`~TrainingArguments.optim`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit" +) +``` +ํ•™์Šต ์‹œ๋‚˜๋ฆฌ์˜ค์— ๋งž๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (์ „์ฒด ์ง€์› ๋ชฉ๋ก์€ [OptimizerNames](https://github.com/huggingface/transformers/blob/34f4080ff59b1668d919a1ba9f8bc4a3a2a3f478/src/transformers/training_args.py#L145)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”) ์˜ˆ๋ฅผ ๋“ค์–ด Adafactor๋Š” ํ–‰๋ ฌ์˜ ๊ฐ ์š”์†Œ ๋Œ€์‹  ํ–‰ ๋˜๋Š” ์—ด ๋‹จ์œ„์˜ ๊ฐ€์ค‘ ํ‰๊ท ๋งŒ ์ €์žฅํ•ด ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ๋Ÿ‰์„ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ˆ˜๋ ด ์†๋„๋Š” ๋А๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ, bitandbytes์˜ [8-bit AdamW optimizer](https://huggingface.co/docs/bitsandbytes)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ์ƒํƒœ๋ฅผ 8๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ตํ‹ฐ๋งˆ์ด์ € ์ƒํƒœ๋Š” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ์ €์žฅ๋˜์—ˆ๋‹ค๊ฐ€ ์˜ตํ‹ฐ๋งˆ์ด์ € ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉ๋˜๊ธฐ ์ „์— ์—ญ ์–‘์žํ™”๋ฉ๋‹ˆ๋‹ค. + +ํŠนํ™”๋œ ์˜ตํ‹ฐ๋งˆ์ด์ €์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด [optimizer](./optimizers) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +### ๋ฐ์ดํ„ฐ ์‚ฌ์ „ ์ ์žฌ[[data-preloading]] + +๋ฐ์ดํ„ฐ ์‚ฌ์ „ ์ ์žฌ(Data preloading)๋Š” GPU๊ฐ€ ์ง€์†์ ์œผ๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋„๋ก CPU์—์„œ ๋ฏธ๋ฆฌ ๋ฐฐ์น˜ ๋‹จ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์žฌํ•˜๊ณ  ์ค€๋น„ํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด GPU ์œ ํœด ์‹œ๊ฐ„์„ ์ค„์ด๊ณ  ํ™œ์šฉ๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. GPU๊ฐ€ ํ•ญ์ƒ ์ž‘์—…์„ ๊ณ„์†ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ฐ์ดํ„ฐ ์‚ฌ์ „ ์ ์žฌ๋ฅผ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +1. ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๊ณ ์ • ๋ฉ”๋ชจ๋ฆฌ๋ฅผ CPU์— ํ• ๋‹นํ•œ ๋’ค, ์ด๋ฅผ GPU๋กœ ์ง์ ‘ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. +2. CPU ์Šค๋ ˆ๋“œ ๋ฐ ์›Œ์ปค ์ˆ˜๋ฅผ ๋Š˜๋ ค ๋ฐ์ดํ„ฐ๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์‚ฌ์ „ ์ ์žฌํ•ฉ๋‹ˆ๋‹ค. + +๊ณ ์ • ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ  ์›Œ์ปค ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” [`TrainingArguments`]์—์„œ [`~TrainingArguments.dataloader_pin_memory`]์™€ [`~TrainingArguments.dataloader_num_workers`] ์˜ต์…˜์„ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, +) +``` + +## PyTorch[[pytorch]] + +PyTorch๋Š” ๋ฉ”๋ชจ๋ฆฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ค„์ด๊ณ  ํ•™์Šต ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๋“ค์€ Transformers์—์„œ ๋ช‡ ์ค„์˜ ์ฝ”๋“œ๋งŒ ์ถ”๊ฐ€ํ•˜์—ฌ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +### torch.empty_cache_steps[[torchemptycachesteps]] + +[torch.cuda.empty_cache](https://pytorch.org/docs/stable/generated/torch.cuda.empty_cache.html#torch.cuda.empty_cache) ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์บ์‹œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ(OOM) ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•™์Šต ์†๋„๊ฐ€ ์•ฝ 10% ๋А๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํŠน์ • ํ•™์Šต ๋‹จ๊ณ„ ์ดํ›„์— ์ด ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, [`TrainingArguments`]์—์„œ [torch_empty_cache_steps()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.torch_empty_cache_steps)๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, + torch_empty_cache_steps=4, +) +``` + +### torch.compile[[torchcompile]] + +[torch.compile](https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html)์€ PyTorch ์ฝ”๋“œ๋ฅผ ์ตœ์ ํ™”๋œ ์ปค๋„๋กœ ์ปดํŒŒ์ผํ•ด ํ•™์Šต ์†๋„๋ฅผ ํฌ๊ฒŒ ๋†’์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ TorchDynamo๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋ ˆ์ž„ ํ‰๊ฐ€ API๋กœ๋ถ€ํ„ฐ PyTorch ๊ทธ๋ž˜ํ”„๋ฅผ ์บก์ฒ˜ํ•˜๋ฉฐ, ์ด๋ ‡๊ฒŒ ์บก์ฒ˜ํ•œ ๊ทธ๋ž˜ํ”„๋Š” ๋‹ค์–‘ํ•œ ๋ฐฑ์—”๋“œ์— ์ถ”๊ฐ€๋กœ ์ตœ์ ํ™”๋œ ์ปค๋„๋กœ ์ปดํŒŒ์ผ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`TrainingArguments`]์—์„œ [`~TrainingArguments.torch_compile`]๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. ๋ฐฑ์—”๋“œ๋Š” [torch_compile_backend()](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments.torch_compile_backend)๋ฅผ ํ†ตํ•ด ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import TrainingArguments + +args = TrainingArguments( + per_device_train_batch_size=4, + gradient_accumulation_steps=16, + gradient_checkpointing=True, + bf16=True, + optim="adamw_bnb_8bit", + dataloader_pin_memory=True, + dataloader_num_workers=4, + torch_empty_cache_steps=4, + torch_compile=True, + torch_compile_backend="inductor" +) +``` + +์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šต ์‹œ๋‚˜๋ฆฌ์˜ค์— ์ ํ•ฉํ•œ ๋ฐฑ์—”๋“œ๋ฅผ ์„ ํƒํ•˜์„ธ์š”. + +| ๋ฐฑ์—”๋“œ | ์„ค๋ช… | ๋ชฉํ‘œ | +| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | +| eager | PyTorch๋ฅผ ์‚ฌ์šฉํ•ด ์ถ”์ถœ๋œ GraphModule์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค | ๋””๋ฒ„๊น… | +| aot_eager | AOTAutograd๋กœ ์ถ”์ถœ๋œ ์ˆœ์ „ํŒŒ ๋ฐ ์—ญ์ „ํŒŒ ๊ทธ๋ž˜ํ”„๋ฅผ Pytorch eager ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค | ๋””๋ฒ„๊น… | +| inductor | Triton ์ปค๋„์„ ํ™œ์šฉํ•˜๋Š” TorchInductor์™€ AOTAutograd, CUDA Graphs๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ํ•™์Šต ๋ฐ ์ถ”๋ก  | +| nvfuser | TorchScript์™€ ํ•จ๊ป˜ nvFuser๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ํ•™์Šต ๋ฐ ์ถ”๋ก  | +| aot_nvfuser | AOTAutograd์™€ ํ•จ๊ป˜ nvFuser๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ํ•™์Šต ๋ฐ ์ถ”๋ก  | +| aot_cudagraphs | AOTAutograd์™€ ํ•จ๊ป˜ CUDA Graphs๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ํ•™์Šต ๋ฐ ์ถ”๋ก  | +| ofi | TorchScripts์˜ [optimize_for_inference](https://pytorch.org/docs/stable/generated/torch.jit.optimize_for_inference.html#torch-jit-optimize-for-inference)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ์ถ”๋ก  | +| fx2trt | [Torch-TensorRT](https://pytorch.org/TensorRT/tutorials/getting_started_with_fx_path.html)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ์ถ”๋ก  | +| onnxrt | CPU ๋ฐ GPU ์ถ”๋ก ์„ ์œ„ํ•ด [ONNX-RT](https://onnxruntime.ai/)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ์ถ”๋ก  | +| ipex | CPU ์ถ”๋ก ์„ ์œ„ํ•ด [IPEX](https://github.com/intel/intel-extension-for-pytorch)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค | ์ถ”๋ก  | + +### ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜[[scaled-dot-production-attention]] + +[torch.nn.functional.scaled_dot_product_attention](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) (SDPA)๋Š” ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ PyTorch์— ๋‚ด์žฅํ•ด ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. SDPA๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์˜ ๊ธฐ์กด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜๋ณด๋‹ค ๋” ํšจ์œจ์ ์ด๊ณ  ์ตœ์ ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ๊ฐ€์ง€ ์œ ํ˜•์˜ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +- [FlashAttention2](https://github.com/Dao-AILab/flash-attention)๋Š” fp16 ๋˜๋Š” bf16 torch ํƒ€์ž… ๋ชจ๋ธ์—์„œ ์ž๋™์œผ๋กœ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € ๋ชจ๋ธ์„ ์ ์ ˆํ•œ ํƒ€์ž…์œผ๋กœ ์บ์ŠคํŒ…ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. +- [xFormers](https://github.com/facebookresearch/xformers) ๋˜๋Š” Memory-Efficient Attention์€ fp32 torch ํƒ€์ž… ๋ชจ๋ธ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +- C++๋กœ ๊ตฌํ˜„๋œ ์Šค์ผ€์ผ๋œ ๋‚ด์  ์–ดํ…์…˜์ž…๋‹ˆ๋‹ค. + +SDPA๋Š” PyTorch 2.1.1 ๋ฒ„์ „ ์ด์ƒ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™œ์„ฑํ™”๋˜์–ด ์žˆ์ง€๋งŒ, [`~PreTrainedModel.from_pretrained`]์—์„œ `attn_implementation="sdpa"`๋ฅผ ์„ค์ •ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B", device_map="auto", attn_implementation="sdpa") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_special.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_special.md new file mode 100644 index 0000000000000000000000000000000000000000..188db542f7c01f00396e64184e376bfea13744ec --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perf_train_special.md @@ -0,0 +1,63 @@ + + +# Apple ์‹ค๋ฆฌ์ฝ˜์—์„œ Pytorch ํ•™์Šต [[PyTorch training on Apple silicon]] + +์ด์ „์—๋Š” Mac์—์„œ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๋•Œ CPU๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด์ œ PyTorch v1.12์˜ ์ถœ์‹œ๋กœ Apple์˜ ์‹ค๋ฆฌ์ฝ˜ GPU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›จ์”ฌ ๋” ๋น ๋ฅธ ์„ฑ๋Šฅ์œผ๋กœ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Pytorch์—์„œ Apple์˜ Metal Performance Shaders (MPS)๋ฅผ ๋ฐฑ์—”๋“œ๋กœ ํ†ตํ•ฉํ•˜๋ฉด์„œ ๊ฐ€๋Šฅํ•ด์กŒ์Šต๋‹ˆ๋‹ค. [MPS ๋ฐฑ์—”๋“œ](https://pytorch.org/docs/stable/notes/mps.html)๋Š” Pytorch ์—ฐ์‚ฐ์„ Metal ์„ธ์ด๋”๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์ด ๋ชจ๋“ˆ๋“ค์„ mps ์žฅ์น˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + + + +์ผ๋ถ€ Pytorch ์—ฐ์‚ฐ๋“ค์€ ์•„์ง MPS์—์„œ ์ง€์›๋˜์ง€ ์•Š์•„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด ํ™˜๊ฒฝ ๋ณ€์ˆ˜ `PYTORCH_ENABLE_MPS_FALLBACK=1` ๋ฅผ ์„ค์ •ํ•˜์—ฌ CPU ์ปค๋„์„ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(์ด๋•Œ `UserWarning`์ด ์—ฌ์ „ํžˆ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). + +
+ +๋‹ค๋ฅธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ [PyTorch](https://github.com/pytorch/pytorch/issues) ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์ด์Šˆ๋ฅผ ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”. ํ˜„์žฌ [`Trainer`]๋Š” MPS ๋ฐฑ์—”๋“œ๋งŒ ํ†ตํ•ฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +
+ +`mps` ์žฅ์น˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +* ๋กœ์ปฌ์—์„œ ๋” ํฐ ๋„คํŠธ์›Œํฌ๋‚˜ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ํ•™์Šต ๊ฐ€๋Šฅ +* GPU์˜ ํ†ตํ•ฉ ๋ฉ”๋ชจ๋ฆฌ ์•„ํ‚คํ…์ฒ˜๋กœ ์ธํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์ง€์—ฐ ๊ฐ์†Œ +* ํด๋ผ์šฐ๋“œ ๊ธฐ๋ฐ˜ GPU๋‚˜ ์ถ”๊ฐ€ GPU๊ฐ€ ํ•„์š” ์—†์œผ๋ฏ€๋กœ ๋น„์šฉ ์ ˆ๊ฐ ๊ฐ€๋Šฅ + +Pytorch๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹œ์ž‘ํ•˜์„ธ์š”. MPS ๊ฐ€์†์€ macOS 12.3 ์ด์ƒ์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +```bash +pip install torch torchvision torchaudio +``` + +[`TrainingArguments`]๋Š” `mps` ์žฅ์น˜๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์žฅ์น˜๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, MPS ๋ฐฑ์—”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ํ™œ์„ฑํ™”ํ•˜์—ฌ [run_glue.py](https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py) ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์•„๋ฌด ์ˆ˜์ • ์—†์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```diff +export TASK_NAME=mrpc + +python examples/pytorch/text-classification/run_glue.py \ + --model_name_or_path google-bert/bert-base-cased \ + --task_name $TASK_NAME \ +- --use_mps_device \ + --do_train \ + --do_eval \ + --max_seq_length 128 \ + --per_device_train_batch_size 32 \ + --learning_rate 2e-5 \ + --num_train_epochs 3 \ + --output_dir /tmp/$TASK_NAME/ \ + --overwrite_output_dir +``` + +`gloco`์™€ `nccl`๊ณผ ๊ฐ™์€ [๋ถ„์‚ฐ ํ•™์Šต ๋ฐฑ์—”๋“œ](https://pytorch.org/docs/stable/distributed.html#backends)๋Š” `mps` ์žฅ์น˜์—์„œ ์ง€์›๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ, MPS ๋ฐฑ์—”๋“œ์—์„œ๋Š” ๋‹จ์ผ GPU๋กœ๋งŒ ํ•™์Šต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. + +Mac์—์„œ ๊ฐ€์†๋œ PyTorch ํ•™์Šต์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Introducing Accelerated PyTorch Training on Mac](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perplexity.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perplexity.md new file mode 100644 index 0000000000000000000000000000000000000000..9de84a5f289b942875aa88386f430cc67e306bb9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/perplexity.md @@ -0,0 +1,135 @@ + + +# ๊ณ ์ • ๊ธธ์ด ๋ชจ๋ธ์˜ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ(Perplexity)[[perplexity-of-fixedlength-models]] + +[[open-in-colab]] + +ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ(Perplexity, PPL)๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์–ธ์–ด ๋ชจ๋ธ ํ‰๊ฐ€์ง€ํ‘œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. +์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์ „์— ์ด ํ‰๊ฐ€์ง€ํ‘œ๋Š” ๊ณ ์ „์ ์ธ ์–ธ์–ด ๋ชจ๋ธ(์ž๊ธฐํšŒ๊ท€ ๋˜๋Š” ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๊ณ ๋„ ํ•จ)์—๋งŒ ์ ์šฉ๋˜๋ฉฐ BERT์™€ ๊ฐ™์€ ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ์—๋Š” ์ž˜ ์ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค (BERT๋Š” [summary of the models](../en/model_summary) ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”). + +ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ๋Š” ์‹œํ€€์Šค์˜ ์Œ์˜ ๋กœ๊ทธ ์šฐ๋„(negative log-likelihood, NLL) ๊ฐ’์˜ ํ‰๊ท ์— ์ง€์ˆ˜(exponentiate)๋ฅผ ์ทจํ•œ ๊ฐ’์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. +ํ† ํฐํ™”๋œ ์‹œํ€€์Šค \\(X = (x_0, x_1, \dots, x_t)\\) ๊ฐ€ ์žˆ์„ ๋•Œ, \\(X\\) ์˜ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ๋Š” ์•„๋ž˜ ์ˆ˜์‹๊ณผ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +$$\text{PPL}(X) = \exp \left\{ {-\frac{1}{t}\sum_i^t \log p_\theta (x_i|x_{ + +๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋ธ์˜ ๊ทผ์‚ฌ์น˜๋ฅผ ๊ตฌํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฐ ์ˆ˜์— ์ œํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์žฅ ํฐ ๋ฒ„์ „์˜ [GPT-2](model_doc/gpt2)๋Š” ํ† ํฐ์˜ ๊ธธ์ด๊ฐ€ 1024๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ \\(t\\) ๊ฐ€ 1024๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์— \\(p_\theta(x_t|x_{ + +์ด ๋ฐฉ๋ฒ•์€ ๊ฐ ๋ถ€๋ถ„์˜ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ๋ฅผ ํ•œ ๋ฒˆ์˜ ํฌ์›Œ๋“œ ํŒจ์Šค๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์–ด ๋น ๋ฅด์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๋†’์€(๋” ๋‚˜์œ) PPL์„ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค. +์™œ๋ƒํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์˜ˆ์ธก ๋‹จ๊ณ„์—์„œ ๋ชจ๋ธ์˜ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋Œ€์‹ , ๊ณ ์ • ๊ธธ์ด ๋ชจ๋ธ์˜ PPL์€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ „๋žต์œผ๋กœ ํ‰๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์ด ์ „๋žต์—๋Š” ์ปจํ…์ŠคํŠธ ์œˆ๋„์šฐ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์Šฌ๋ผ์ด๋”ฉํ•ด ๋ชจ๋ธ์ด ๊ฐ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋” ๋งŽ์€ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ–๋„๋ก ํ•˜๋Š” ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. + +Sliding window PPL taking advantage of all available context + +์ด๋Š” ์‹œํ€€์Šค ํ™•๋ฅ ์˜ ์‹ค์ œ ๋ถ„ํ•ด์— ๋” ๊ฐ€๊นŒ์šด ๊ทผ์‚ฌ์น˜์ด๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ์œ ๋ฆฌํ•œ ์ ์ˆ˜๋ฅผ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค. +๋‹จ์ ์€ ๋ง๋ญ‰์น˜์˜ ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด ๋ณ„๋„์˜ ํฌ์›Œ๋“œ ํŒจ์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +ํ˜„์‹ค์ ์œผ๋กœ ์ข‹์€ ์ ˆ์ถฉ์•ˆ์€ ํ•œ ๋ฒˆ์— ํ•œ ํ† ํฐ์”ฉ ์Šฌ๋ผ์ด๋”ฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋” ํฐ ๊ฐ„๊ฒฉ์œผ๋กœ ์ปจํ…์ŠคํŠธ๋ฅผ ์ด๋™ํ•˜๋Š” ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ ์ ์šฉ๋œ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ณ„์‚ฐ์„ ํ›จ์”ฌ ๋” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•˜๋ฉด์„œ๋„ ๋ชจ๋ธ์— ๊ฐ ๋‹จ๊ณ„์—์„œ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์˜ˆ์ œ: ๐Ÿค— Transformers์—์„œ GPT-2๋กœ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ(perplexity) ๊ณ„์‚ฐํ•˜๊ธฐ[[example-calculating-perplexity-with-gpt2-in-transformers]] + +์ด์ œ GPT-2๋กœ ์œ„์˜ ๊ณผ์ •์„ ์‹œ์—ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import GPT2LMHeadModel, GPT2TokenizerFast + +device = "cuda" +model_id = "openai-community/gpt2-large" +model = GPT2LMHeadModel.from_pretrained(model_id).to(device) +tokenizer = GPT2TokenizerFast.from_pretrained(model_id) +``` + +WikiText-2 ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋ช‡ ๊ฐ€์ง€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ „๋žต์„ ์‚ฌ์šฉํ•ด ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +์ด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋Š” ํฌ๊ธฐ๊ฐ€ ์ž‘๊ณ  ํฌ์›Œ๋“œ ํŒจ์Šค ํ•œ ๋ฒˆ๋งŒ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ€์ ธ์˜ค๊ณ  ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from datasets import load_dataset + +test = load_dataset("wikitext", "wikitext-2-raw-v1", split="test") +encodings = tokenizer("\n\n".join(test["text"]), return_tensors="pt") +``` + +๐Ÿค— Transformers๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์˜ `labels`๋กœ `input_ids`๋ฅผ ์ „๋‹ฌํ•ด ๊ฐ ํ† ํฐ์— ๋Œ€ํ•œ ํ‰๊ท  ์Œ์˜ ์šฐ๋„ ๊ฐ’์„ ์†์‹ค๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ๋ฐ˜๋ณต๋งˆ๋‹ค ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๋Š” ํ† ํฐ์ด ๊ฒน์นฉ๋‹ˆ๋‹ค. +์ปจํ…์ŠคํŠธ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํ† ํฐ์— ๋Œ€ํ•œ ๋กœ๊ทธ ์šฐ๋„ ๊ฐ’์ด ์†์‹ค์— ํฌํ•จ๋˜๋Š” ๊ฒƒ์„ ์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ํ† ํฐ์˜ `input_ids`๋ฅผ `-100`์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ์ŠคํŠธ๋ผ์ด๋“œ(stride)๋ฅผ `512`๋กœ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. +์ฆ‰, ๋ชจ๋ธ์ด ํ•œ ํ† ํฐ์˜ ์กฐ๊ฑด๋ถ€ ์šฐ๋„ ๊ฐ’์„ ๊ณ„์‚ฐํ•  ๋•Œ ์ปจํ…์ŠคํŠธ์— ์ตœ์†Œํ•œ 512๊ฐœ์˜ ํ† ํฐ์ด ํฌํ•จ๋˜์–ด์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค (ํ•ด๋‹น ํ† ํฐ ์•ž์— 512๊ฐœ์˜ ํ† ํฐ์ด ์žˆ๋Š” ๊ฒฝ์šฐ). + +```python +import torch +from tqdm import tqdm + +max_length = model.config.n_positions +stride = 512 +seq_len = encodings.input_ids.size(1) + +nlls = [] +prev_end_loc = 0 +for begin_loc in tqdm(range(0, seq_len, stride)): + end_loc = min(begin_loc + max_length, seq_len) + trg_len = end_loc - prev_end_loc # ๋งˆ์ง€๋ง‰ ๋ฃจํ”„์˜ ์ŠคํŠธ๋ผ์ด๋“œ ๊ฐ’๊ณผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ + input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device) + target_ids = input_ids.clone() + target_ids[:, :-trg_len] = -100 + + with torch.no_grad(): + outputs = model(input_ids, labels=target_ids) + + # ์†์‹ค์€ ๋ชจ๋“  ์œ ํšจํ•œ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ(cross entropy)๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. + # ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ง€์•ˆ ๋ชจ๋ธ์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ ˆ์ด๋ธ”์„ ์™ผ์ชฝ์œผ๋กœ 1๊ฐœ์”ฉ ๋ฐ€๊ธฐ ๋•Œ๋ฌธ์—, (ํƒ€์ผ“ - 1)๊ฐœ ๋งŒํผ์˜ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด ์†์‹ค์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. + neg_log_likelihood = outputs.loss + + nlls.append(neg_log_likelihood) + + prev_end_loc = end_loc + if end_loc == seq_len: + break + +ppl = torch.exp(torch.stack(nlls).mean()) +``` + +์ŠคํŠธ๋ผ์ด๋“œ๋ฅผ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด์™€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•˜๋ฉด ์œ„์—์„œ ์„ค๋ช…ํ•œ ์ฐจ์„ ์ฑ…์ธ ๋น„์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ „๋žต๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. +์ผ๋ฐ˜์ ์œผ๋กœ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ ์ž‘์„์ˆ˜๋ก ๋ชจ๋ธ์ด ๊ฐ ์˜ˆ์ธก์„ ํ•  ๋•Œ ๋” ๋งŽ์€ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ ๊ฐ’์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค. + +์œ„์˜ ๊ณ„์‚ฐ์„ ํ† ํฐ์ด ๊ฒน์น˜์ง€ ์•Š๋„๋ก `stride = 1024`๋กœ ์„ค์ •ํ•˜๋ฉด PPL์€ `19.44`๋กœ GPT-2 ๋…ผ๋ฌธ์—์„œ ๋ณด๊ณ ๋œ `19.93`๊ณผ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. +`stride = 512`๋กœ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ „๋žต์„ ์‚ฌ์šฉํ•˜๋ฉด PPL์€ `16.45`๋กœ ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. +์ด๋Š” ๋” ์ข‹์€ ์ ์ˆ˜์ผ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹œํ€€์Šค ํ™•๋ฅ ์˜ ์‹ค์ œ ์ž๋™ ํšŒ๊ท€ ๋ถ„ํ•ด์— ๋” ๊ฐ€๊นŒ์šด ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/philosophy.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/philosophy.md new file mode 100644 index 0000000000000000000000000000000000000000..e303709a11b8336a3cae1f6a6c5321ef953c4bc9 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/philosophy.md @@ -0,0 +1,66 @@ + + +# ์ด๋…๊ณผ ๋ชฉํ‘œ [[philosophy]] + +๐Ÿค— Transformers๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋…์ž์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค: + +- ๋Œ€๊ทœ๋ชจ Transformers ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์—ฐ๊ตฌํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•˜๋ ค๋Š” ๊ธฐ๊ณ„ ํ•™์Šต ์—ฐ๊ตฌ์› ๋ฐ ๊ต์œก์ž๋ฅผ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ์ œ์ž‘์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์‹ค์ „ ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. +- ํŠน์ • ๊ธฐ๊ณ„ ํ•™์Šต ์ž‘์—…์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ ค๋Š” ์—”์ง€๋‹ˆ์–ด๋ฅผ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๋ชฉํ‘œ๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค: + +1. ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ: + +- ํ•™์Šตํ•ด์•ผ ํ•  ์‚ฌ์šฉ์ž ๋Œ€์ƒ ์ถ”์ƒํ™”์˜ ์ˆ˜๋ฅผ ์ œํ•œํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ฑฐ์˜ ์ถ”์ƒํ™”๊ฐ€ ์—†์œผ๋ฉฐ, ๊ฐ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์„ธ ๊ฐ€์ง€ ํ‘œ์ค€ ํด๋ž˜์Šค์ธ [configuration](main_classes/configuration), [models](main_classes/model) ๋ฐ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค์ธ ([tokenizer](main_classes/tokenizer)๋Š” NLP์šฉ, [image processor](main_classes/image_processor)๋Š” ๋น„์ „์šฉ, [feature extractor](main_classes/feature_extractor)๋Š” ์˜ค๋””์˜ค์šฉ, [processor](main_classes/processors)๋Š” ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž…๋ ฅ์šฉ)๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +- ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋Š” ๊ณตํ†ต์ ์ธ `from_pretrained()` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ๋ฆฌ ํ›ˆ๋ จ๋œ ์ธ์Šคํ„ด์Šค์—์„œ ๊ฐ„๋‹จํ•˜๊ณ  ํ†ต์ผ๋œ ๋ฐฉ์‹์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ๋ฏธ๋ฆฌ ํ›ˆ๋ จ๋œ ์ฒดํฌํฌ์ธํŠธ์—์„œ ๊ด€๋ จ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์™€ ๊ด€๋ จ ๋ฐ์ดํ„ฐ(๊ตฌ์„ฑ์˜ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ, ํ† ํฌ๋‚˜์ด์ €์˜ ์–ดํœ˜, ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜)๋ฅผ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ) ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œํ•˜๋ฉฐ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ฒดํฌํฌ์ธํŠธ๋Š” [Hugging Face Hub](https://huggingface.co/models)์—์„œ ์ œ๊ณต๋˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž ์ž์ฒด์˜ ์ €์žฅ๋œ ์ฒดํฌํฌ์ธํŠธ์—์„œ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. +- ์ด ์„ธ ๊ฐ€์ง€ ๊ธฐ๋ณธ ํด๋ž˜์Šค ์œ„์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” [`pipeline`] API๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ž‘์—…์— ๋Œ€ํ•ด ๋ชจ๋ธ์„ ๋น ๋ฅด๊ฒŒ ์ถ”๋ก ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๊ณ , [`Trainer`]๋ฅผ ์ œ๊ณตํ•˜์—ฌ PyTorch ๋ชจ๋ธ์„ ๋น ๋ฅด๊ฒŒ ํ›ˆ๋ จํ•˜๊ฑฐ๋‚˜ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค(๋ชจ๋“  TensorFlow ๋ชจ๋ธ์€ `Keras.fit`๊ณผ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค). +- ๊ฒฐ๊ณผ์ ์œผ๋กœ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‹ ๊ฒฝ๋ง์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ์‹ ๋„๊ตฌ ์ƒ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์ผ๋ฐ˜์ ์ธ Python, PyTorch, TensorFlow, Keras ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์—ฌ ๋ชจ๋ธ ๋กœ๋”ฉ ๋ฐ ์ €์žฅ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ฝ”๋”ฉ ์ฒ ํ•™์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด [Repeat Yourself](https://huggingface.co/blog/transformers-design-philosophy) ๋ธ”๋กœ๊ทธ ๊ธ€์„ ํ™•์ธํ•ด๋ณด์„ธ์š”. + +2. ์›๋ž˜ ๋ชจ๋ธ๊ณผ ๊ฐ€๋Šฅํ•œ ํ•œ ๊ทผ์ ‘ํ•œ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์ตœ์‹  ๋ชจ๋ธ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ: + +- ๊ฐ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด ๊ณต์‹ ์ €์ž๊ฐ€ ์ œ๊ณตํ•œ ๊ฒฐ๊ณผ๋ฅผ ์žฌํ˜„ํ•˜๋Š” ์ ์–ด๋„ ํ•œ ๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. +- ์ฝ”๋“œ๋Š” ์›๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ€๋Šฅํ•œ ํ•œ ์œ ์‚ฌํ•˜๊ฒŒ ์œ ์ง€๋˜๋ฏ€๋กœ PyTorch ์ฝ”๋“œ๋Š” TensorFlow ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜๋˜์–ด *pytorchic*ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. + +๊ธฐํƒ€ ๋ชฉํ‘œ ๋ช‡ ๊ฐ€์ง€: + +- ๋ชจ๋ธ์˜ ๋‚ด๋ถ€๋ฅผ ๊ฐ€๋Šฅํ•œ ์ผ๊ด€๋˜๊ฒŒ ๋…ธ์ถœ์‹œํ‚ค๊ธฐ: + + - ์ „์ฒด ์€๋‹‰ ์ƒํƒœ์™€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋ฅผ ๋‹จ์ผ API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + - ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค ๋ฐ ๊ธฐ๋ณธ ๋ชจ๋ธ API๋Š” ๋ชจ๋ธ ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ‘œ์ค€ํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +- ๋ฏธ์„ธ ์กฐ์ • ๋ฐ ๋ชจ๋ธ ํƒ์ƒ‰์„ ์œ„ํ•œ ์œ ๋งํ•œ ๋„๊ตฌ๋“ค์„ ์ฃผ๊ด€์ ์œผ๋กœ ์„ ํƒํ•˜๊ธฐ: + + - ๋ฏธ์„ธ ์กฐ์ •์„ ์œ„ํ•ด ์–ดํœ˜ ๋ฐ ์ž„๋ฒ ๋”ฉ์— ์ƒˆ๋กœ์šด ํ† ํฐ์„ ๊ฐ„๋‹จํ•˜๊ณ  ์ผ๊ด€๋œ ๋ฐฉ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + - Transformer ํ—ค๋“œ๋ฅผ ๋งˆ์Šคํ‚นํ•˜๊ณ  ๊ฐ€์ง€์น˜๊ธฐํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +- PyTorch, TensorFlow 2.0 ๋ฐ Flax ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ํ›ˆ๋ จํ•˜๊ณ  ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +## ์ฃผ์š” ๊ฐœ๋… [[main-concepts]] + +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ฐ ๋ชจ๋ธ์— ๋Œ€ํ•ด ์„ธ ๊ฐ€์ง€ ์œ ํ˜•์˜ ํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค: + +- **๋ชจ๋ธ ํด๋ž˜์Šค**๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜์™€ ํ•จ๊ป˜ ์ž‘๋™ํ•˜๋Š” PyTorch ๋ชจ๋ธ([torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)), Keras ๋ชจ๋ธ([tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model)), JAX/Flax ๋ชจ๋ธ([flax.linen.Module](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html))์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- **๊ตฌ์„ฑ ํด๋ž˜์Šค**๋Š” ๋ชจ๋ธ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ(์˜ˆ: ๋ ˆ์ด์–ด ์ˆ˜ ๋ฐ ์€๋‹‰ ํฌ๊ธฐ)๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์ธ์Šคํ„ด์Šคํ™”ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, ์ˆ˜์ • ์—†์ด ๊ณ  ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋ฉด ๋ชจ๋ธ์˜ ์ผ๋ถ€์ธ ๊ตฌ์„ฑ์„ ์ž๋™์œผ๋กœ ์ธ์Šคํ„ด์Šคํ™”๋ฉ๋‹ˆ๋‹ค. +- **์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค**๋Š” ์›์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ธ์ด ์ˆ˜์šฉํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. [Tokenizer](main_classes/tokenizer)๋Š” ๊ฐ ๋ชจ๋ธ์˜ ์–ดํœ˜๋ฅผ ์ €์žฅํ•˜๊ณ , ๋ฌธ์ž์—ด์„ ํ† ํฐ ์ž„๋ฒ ๋”ฉ ์ธ๋ฑ์Šค ๋ฆฌ์ŠคํŠธ๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ณ  ๋””์ฝ”๋”ฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. [Image processors](main_classes/image_processor)๋Š” ๋น„์ „ ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๊ณ , [feature extractors](main_classes/feature_extractor)๋Š” ์˜ค๋””์˜ค ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๋ฉฐ, [processor](main_classes/processors)๋Š” ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + +๋ชจ๋“  ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ธ์Šคํ„ด์Šค์—์„œ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ๋กœ์ปฌ๋กœ ์ €์žฅํ•˜๋ฉฐ, ์„ธ ๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hub์—์„œ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +- `from_pretrained()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฒ„์ „(์ง€์›๋˜๋Š” ๋ชจ๋ธ์€ [Model Hub](https://huggingface.co/models)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ)์ด๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ์ปฌ๋กœ ์ €์žฅํ•œ ๊ฒฝ์šฐ(๋˜๋Š” ์„œ๋ฒ„์— ์ €์žฅํ•œ ๊ฒฝ์šฐ)์˜ ๋ชจ๋ธ, ๊ตฌ์„ฑ ๋ฐ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `save_pretrained()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ, ๊ตฌ์„ฑ ๋ฐ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ๋กœ์ปฌ๋กœ ์ €์žฅํ•˜์—ฌ `from_pretrained()`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +- `push_to_hub()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ, ๊ตฌ์„ฑ ๋ฐ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ Hub์— ๊ณต์œ ํ•˜์—ฌ ๋ชจ๋‘์—๊ฒŒ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_gradio.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_gradio.md new file mode 100644 index 0000000000000000000000000000000000000000..418ebfa0fb2e03651d5c21870ebc0846ed0ccf09 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_gradio.md @@ -0,0 +1,52 @@ + + +# ๋จธ์‹ ๋Ÿฌ๋‹ ์•ฑ [[machine-learning-apps]] + +๋จธ์‹ ๋Ÿฌ๋‹ ์•ฑ์„ ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ๊ตฌ์ถ•ํ•˜๊ณ  ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ [Gradio](https://www.gradio.app/)๋Š” [`Pipeline`]๊ณผ ํ†ตํ•ฉ๋˜์–ด ์ถ”๋ก ์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋น ๋ฅด๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Gradio๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```py +!pip install gradio +``` + +์›ํ•˜๋Š” ์ž‘์—…์— ๋งž๋Š” pipeline์„ ์ƒ์„ฑํ•œ ๋‹ค์Œ, Gradio์˜ [Interface.from_pipeline](https://www.gradio.app/docs/gradio/interface#interface-from_pipeline) ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜์—ฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“œ์„ธ์š”. Gradio๋Š” [`Pipeline`]์— ๋งž๋Š” ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +[launch](https://www.gradio.app/main/docs/gradio/blocks#blocks-launch)๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์›น ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•ฑ์„ ์‹œ์ž‘ํ•˜์„ธ์š”. + +```py +from transformers import pipeline +import gradio as gr + +pipeline = pipeline("image-classification", model="google/vit-base-patch16-224") +gr.Interface.from_pipeline(pipeline).launch() +``` + +์›น ์•ฑ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋กœ์ปฌ ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์™€ ์•ฑ์„ ๊ณต์œ ํ•˜๋ ค๋ฉด [launch](https://www.gradio.app/main/docs/gradio/blocks#blocks-launch)์—์„œ `share=True`๋กœ ์„ค์ •ํ•˜์—ฌ ์ž„์‹œ ๊ณต๊ฐœ ๋งํฌ๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”. ๋” ์ง€์†์ ์ธ ์†”๋ฃจ์…˜์„ ์›ํ•œ๋‹ค๋ฉด Hugging Face [Spaces](https://hf.co/spaces)์—์„œ ์•ฑ์„ ํ˜ธ์ŠคํŒ…ํ•˜์„ธ์š”. + +```py +gr.Interface.from_pipeline(pipeline).launch(share=True) +``` + +์•„๋ž˜ Space๋Š” ์œ„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋˜์—ˆ์œผ๋ฉฐ, Spaces์—์„œ ํ˜ธ์ŠคํŒ…๋ฉ๋‹ˆ๋‹ค. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_tutorial.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_tutorial.md new file mode 100644 index 0000000000000000000000000000000000000000..2f166fc6939f3206f3b03490c140a5c3f360045f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_tutorial.md @@ -0,0 +1,243 @@ + + +# ์ถ”๋ก ์„ ์œ„ํ•œ Pipeline[[pipelines-for-inference]] + +[`pipeline`]์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ธ์–ด, ์ปดํ“จํ„ฐ ๋น„์ „, ์˜ค๋””์˜ค ๋ฐ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํƒœ์Šคํฌ์— ๋Œ€ํ•œ ์ถ”๋ก ์„ ์œ„ํ•ด [Hub](https://huggingface.co/models)์˜ ์–ด๋–ค ๋ชจ๋ธ์ด๋“  ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๋ถ„์•ผ์— ๋Œ€ํ•œ ๊ฒฝํ—˜์ด ์—†๊ฑฐ๋‚˜, ๋ชจ๋ธ์„ ์ด๋ฃจ๋Š” ์ฝ”๋“œ๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ [`pipeline`]์„ ์‚ฌ์šฉํ•ด์„œ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์–ด์š”! ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋‹ค์Œ์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +* ์ถ”๋ก ์„ ์œ„ํ•ด [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• +* ํŠน์ • ํ† ํฌ๋‚˜์ด์ € ๋˜๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• +* ์–ธ์–ด, ์ปดํ“จํ„ฐ ๋น„์ „, ์˜ค๋””์˜ค ๋ฐ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํƒœ์Šคํฌ์—์„œ [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• + + + +์ง€์›ํ•˜๋Š” ๋ชจ๋“  ํƒœ์Šคํฌ์™€ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‹ด์€ ๋ชฉ๋ก์€ [`pipeline`] ์„ค๋ช…์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. + + + +## Pipeline ์‚ฌ์šฉํ•˜๊ธฐ[[pipeline-usage]] + +๊ฐ ํƒœ์Šคํฌ๋งˆ๋‹ค ๊ณ ์œ ์˜ [`pipeline`]์ด ์žˆ์ง€๋งŒ, ๊ฐœ๋ณ„ ํŒŒ์ดํ”„๋ผ์ธ์„ ๋‹ด๊ณ ์žˆ๋Š” ์ถ”์ƒํ™”๋œ [`pipeline`]๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. [`pipeline`]์€ ํƒœ์Šคํฌ์— ์•Œ๋งž๊ฒŒ ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•œ ๊ธฐ๋ณธ ๋ชจ๋ธ๊ณผ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. + +1. ๋จผ์ € [`pipeline`]์„ ์ƒ์„ฑํ•˜๊ณ  ํƒœ์Šคํฌ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”. + +```py +>>> from transformers import pipeline + +>>> generator = pipeline(task="automatic-speech-recognition") +``` + +2. ๊ทธ๋ฆฌ๊ณ  [`pipeline`]์— ์ž…๋ ฅ์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. + +```py +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'} +``` + +๊ธฐ๋Œ€ํ–ˆ๋˜ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹Œ๊ฐ€์š”? Hub์—์„œ [๊ฐ€์žฅ ๋งŽ์ด ๋‹ค์šด๋กœ๋“œ๋œ ์ž๋™ ์Œ์„ฑ ์ธ์‹ ๋ชจ๋ธ](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=downloads)๋กœ ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์„ธ์š”. +๋‹ค์Œ์€ [openai/whisper-large](https://huggingface.co/openai/whisper-large)๋กœ ์‹œ๋„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```py +>>> generator = pipeline(model="openai/whisper-large") +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'} +``` + +ํ›จ์”ฌ ๋” ๋‚˜์•„์กŒ๊ตฐ์š”! +Hub์˜ ๋ชจ๋ธ๋“ค์€ ์—ฌ๋Ÿฌ ๋‹ค์–‘ํ•œ ์–ธ์–ด์™€ ์ „๋ฌธ๋ถ„์•ผ๋ฅผ ์•„์šฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์ž์‹ ์˜ ์–ธ์–ด๋‚˜ ๋ถ„์•ผ์— ํŠนํ™”๋œ ๋ชจ๋ธ์„ ์ฐพ์•„๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. +๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋ฒ—์–ด๋‚  ํ•„์š”์—†์ด Hub์—์„œ ์ง์ ‘ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ํ™•์ธํ•˜๊ณ  ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•ด์„œ ์ž์‹ ์˜ ์ƒํ™ฉ์— ๋” ์ ํ•ฉํ•œ์ง€, ์• ๋งคํ•œ ์ž…๋ ฅ์„ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋งŒ์•ฝ ์ƒํ™ฉ์— ์•Œ๋งž๋Š” ๋ชจ๋ธ์„ ์—†๋‹ค๋ฉด ์–ธ์ œ๋‚˜ ์ง์ ‘ [ํ›ˆ๋ จ](training)์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ž…๋ ฅ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +generator( + [ + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", + "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", + ] +) +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ์„ธํŠธ์„ ์ˆœํšŒํ•˜๊ฑฐ๋‚˜ ์›น์„œ๋ฒ„์— ์˜ฌ๋ ค๋‘์–ด ์ถ”๋ก ์— ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๊ฐ ์ƒ์„ธ ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +[๋ฐ์ดํ„ฐ์„ธํŠธ์—์„œ Pipeline ์‚ฌ์šฉํ•˜๊ธฐ](#using-pipelines-on-a-dataset) + +[์›น์„œ๋ฒ„์—์„œ Pipeline ์‚ฌ์šฉํ•˜๊ธฐ](./pipeline_webserver) + +## ๋งค๊ฐœ๋ณ€์ˆ˜[[parameters]] + +[`pipeline`]์€ ๋งŽ์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ํƒœ์Šคํฌ์šฉ์ธ ๊ฒƒ๋„ ์žˆ๊ณ , ๋ฒ”์šฉ์ธ ๊ฒƒ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. +์ผ๋ฐ˜์ ์œผ๋กœ ์›ํ•˜๋Š” ์œ„์น˜์— ์–ด๋””๋“  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +generator(model="openai/whisper-large", my_parameter=1) +out = generate(...) # This will use `my_parameter=1`. +out = generate(..., my_parameter=2) # This will override and use `my_parameter=2`. +out = generate(...) # This will go back to using `my_parameter=1`. +``` + +์ค‘์š”ํ•œ 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +### ๊ธฐ๊ธฐ(device)[[device]] + +`device=n`์ฒ˜๋Ÿผ ๊ธฐ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋ฉด ํŒŒ์ดํ”„๋ผ์ธ์ด ์ž๋™์œผ๋กœ ํ•ด๋‹น ๊ธฐ๊ธฐ์— ๋ชจ๋ธ์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. +ํŒŒ์ดํ† ์น˜์—์„œ๋‚˜ ํ…์„œํ”Œ๋กœ์šฐ์—์„œ๋„ ๋ชจ๋‘ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +```py +generator(model="openai/whisper-large", device=0) +``` + +๋ชจ๋ธ์ด GPU ํ•˜๋‚˜์— ๋Œ์•„๊ฐ€๊ธฐ ๋ฒ„๊ฒ๋‹ค๋ฉด, `device_map="auto"`๋ฅผ ์ง€์ •ํ•ด์„œ ๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate)๊ฐ€ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ์–ด๋–ป๊ฒŒ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ• ์ง€ ์ž๋™์œผ๋กœ ๊ฒฐ์ •ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +#!pip install accelerate +generator(model="openai/whisper-large", device_map="auto") +``` + +### ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ[[batch-size]] + +๊ธฐ๋ณธ์ ์œผ๋กœ ํŒŒ์ดํ”„๋ผ์ธ์€ [์—ฌ๊ธฐ](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching)์— ๋‚˜์˜จ ์ด์œ ๋กœ ์ถ”๋ก ์„ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด ์ผ๊ด„ ์ฒ˜๋ฆฌ๊ฐ€ ๋ฐ˜๋“œ์‹œ ๋” ๋น ๋ฅด์ง€ ์•Š๊ณ  ์˜คํžˆ๋ ค ๋” ๋А๋ ค์งˆ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +ํ•˜์ง€๋งŒ ์ž์‹ ์˜ ์ƒํ™ฉ์— ์ ํ•ฉํ•˜๋‹ค๋ฉด, ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```py +generator(model="openai/whisper-large", device=0, batch_size=2) +audio_filenames = [f"audio_{i}.flac" for i in range(10)] +texts = generator(audio_filenames) +``` + +ํŒŒ์ดํ”„๋ผ์ธ ์œ„ ์ œ๊ณต๋œ 10๊ฐœ์˜ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ถ”๊ฐ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ ์—†์ด (์ผ๊ด„ ์ฒ˜๋ฆฌ์— ๋ณด๋‹ค ํšจ๊ณผ์ ์ธ GPU ์œ„) ๋ชจ๋ธ์— 2๊ฐœ์”ฉ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +์ถœ๋ ฅ์€ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์™€ ๋˜‘๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์†๋„๋ฅผ ๋” ๋‚ผ ์ˆ˜๋„ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. + +ํŒŒ์ดํ”„๋ผ์ธ์€ ์ผ๊ด„ ์ฒ˜๋ฆฌ์˜ ๋ณต์žกํ•œ ๋ถ€๋ถ„์„ ์ค„์—ฌ์ฃผ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด ๊ธด ์˜ค๋””์˜ค ํŒŒ์ผ์ฒ˜๋Ÿผ) ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ ์•ผ ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ [*chunk batching*](./main_classes/pipelines#pipeline-chunk-batching)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋‚˜๋ˆ ์ค๋‹ˆ๋‹ค. + +### ํŠน์ • ํƒœ์Šคํฌ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜[[task-specific-parameters]] + +๊ฐ ํƒœ์Šคํฌ๋งˆ๋‹ค ๊ตฌํ˜„ํ•  ๋•Œ ์œ ์—ฐ์„ฑ๊ณผ ์˜ต์…˜์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ํƒœ์Šคํฌ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. +์˜ˆ๋ฅผ ๋“ค์–ด [`transformers.AutomaticSpeechRecognitionPipeline.__call__`] ๋ฉ”์„œ๋“œ์—๋Š” ๋™์˜์ƒ์˜ ์ž๋ง‰์„ ๋„ฃ์„ ๋•Œ ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™์€ `return_timestamps` ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> # Not using whisper, as it cannot provide timestamps. +>>> generator = pipeline(model="facebook/wav2vec2-large-960h-lv60-self", return_timestamps="word") +>>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") +{'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP AND LIVE OUT THE TRUE MEANING OF ITS CREED', 'chunks': [{'text': 'I', 'timestamp': (1.22, 1.24)}, {'text': 'HAVE', 'timestamp': (1.42, 1.58)}, {'text': 'A', 'timestamp': (1.66, 1.68)}, {'text': 'DREAM', 'timestamp': (1.76, 2.14)}, {'text': 'BUT', 'timestamp': (3.68, 3.8)}, {'text': 'ONE', 'timestamp': (3.94, 4.06)}, {'text': 'DAY', 'timestamp': (4.16, 4.3)}, {'text': 'THIS', 'timestamp': (6.36, 6.54)}, {'text': 'NATION', 'timestamp': (6.68, 7.1)}, {'text': 'WILL', 'timestamp': (7.32, 7.56)}, {'text': 'RISE', 'timestamp': (7.8, 8.26)}, {'text': 'UP', 'timestamp': (8.38, 8.48)}, {'text': 'AND', 'timestamp': (10.08, 10.18)}, {'text': 'LIVE', 'timestamp': (10.26, 10.48)}, {'text': 'OUT', 'timestamp': (10.58, 10.7)}, {'text': 'THE', 'timestamp': (10.82, 10.9)}, {'text': 'TRUE', 'timestamp': (10.98, 11.18)}, {'text': 'MEANING', 'timestamp': (11.26, 11.58)}, {'text': 'OF', 'timestamp': (11.66, 11.7)}, {'text': 'ITS', 'timestamp': (11.76, 11.88)}, {'text': 'CREED', 'timestamp': (12.0, 12.38)}]} +``` + +๋ณด์‹œ๋‹ค์‹œํ”ผ ๋ชจ๋ธ์ด ํ…์ŠคํŠธ๋ฅผ ์ถ”๋ก ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ ๋‹จ์–ด๋ฅผ ๋งํ•œ ์‹œ์ ๊นŒ์ง€๋„ ์ถœ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. + +ํƒœ์Šคํฌ๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ์š”. ์›ํ•˜๋Š” ํƒœ์Šคํฌ์˜ API๋ฅผ ์ฐธ์กฐํ•ด์„œ ๋ฐ”๊ฟ”๋ณผ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”! +์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ค„๋ณธ [`~transformers.AutomaticSpeechRecognitionPipeline`]์—๋Š” `chunk_length_s` ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ํ™”๋‚˜ 1์‹œ๊ฐ„ ๋ถ„๋Ÿ‰์˜ ๋™์˜์ƒ์˜ ์ž๋ง‰ ์ž‘์—…์„ ํ•  ๋•Œ์ฒ˜๋Ÿผ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ์ด ์ž์ฒด์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ๋งค์šฐ ๊ธด ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์œ ์šฉํ•˜์ฃ . + + +๋„์›€์ด ๋  ๋งŒํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ [์š”์ฒญ](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml)ํ•ด์ฃผ์„ธ์š”! + + +## ๋ฐ์ดํ„ฐ์„ธํŠธ์—์„œ Pipeline ์‚ฌ์šฉํ•˜๊ธฐ[[using-pipelines-on-a-dataset]] + +ํŒŒ์ดํ”„๋ผ์ธ์€ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์„ธํŠธ์—์„œ๋„ ์ถ”๋ก  ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฑธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. + +```py +def data(): + for i in range(1000): + yield f"My example {i}" + + +pipe = pipe(model="openai-community/gpt2", device=0) +generated_characters = 0 +for out in pipe(data()): + generated_characters += len(out["generated_text"]) +``` + +์ดํ„ฐ๋ ˆ์ดํ„ฐ `data()`๋Š” ๊ฐ ๊ฒฐ๊ณผ๋ฅผ ํ˜ธ์ถœ๋งˆ๋‹ค ์ƒ์„ฑํ•˜๊ณ , ํŒŒ์ดํ”„๋ผ์ธ์€ ์ž…๋ ฅ์ด ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž„์„ ์ž๋™์œผ๋กœ ์ธ์‹ํ•˜์—ฌ GPU์—์„œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” ๋™์•ˆ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.(์ด๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader)๋ฅผ ์‚ฌ์šฉํ•ด์š”.) ์ด ๊ณผ์ •์€ ์ „์ฒด ๋ฐ์ดํ„ฐ์„ธํŠธ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌํ•˜์ง€ ์•Š๊ณ ๋„ GPU์— ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ์ƒˆ๋กœ์šด ์ž‘์—…์„ ๊ณต๊ธ‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. + +๊ทธ๋ฆฌ๊ณ  ์ผ๊ด„ ์ฒ˜๋ฆฌ๊ฐ€ ๋” ๋น ๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, `batch_size` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์กฐ์ •ํ•ด๋ด๋„ ์ข‹์•„์š”. + +๋ฐ์ดํ„ฐ์„ธํŠธ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ๐Ÿค— [Datasets](https://github.com/huggingface/datasets/)๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ธ๋ฐ์š”. + +```py +# KeyDataset is a util that will just output the item we're interested in. +from transformers.pipelines.pt_utils import KeyDataset + +pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) +dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") + +for out in pipe(KeyDataset(dataset["audio"])): + print(out) +``` + + +## ์›น์„œ๋ฒ„์—์„œ Pipeline ์‚ฌ์šฉํ•˜๊ธฐ[[using-pipelines-for-a-webserver]] + + +์ถ”๋ก  ์—”์ง„์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์€ ๋”ฐ๋กœ ํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ• ๋งŒํ•œ ๋ณต์žกํ•œ ์ฃผ์ œ์ž…๋‹ˆ๋‹ค. + + +[Link](./pipeline_webserver) + +## ๋น„์ „ Pipeline[[vision-pipeline]] + +๋น„์ „ ํƒœ์Šคํฌ๋ฅผ ์œ„ํ•ด [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ์€ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. + +ํƒœ์Šคํฌ๋ฅผ ์ง€์ •ํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜๊ธฐ์— ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ์ธํ„ฐ๋„ท ๋งํฌ ๋˜๋Š” ๋กœ์ปฌ ๊ฒฝ๋กœ์˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์— ํ‘œ์‹œ๋œ ๊ณ ์–‘์ด๋Š” ์–ด๋–ค ์ข…์ธ๊ฐ€์š”? + +![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) + +```py +>>> from transformers import pipeline + +>>> vision_classifier = pipeline(model="google/vit-base-patch16-224") +>>> preds = vision_classifier( +... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" +... ) +>>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] +>>> preds +[{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}] +``` + +### ํ…์ŠคํŠธ Pipeline[[text-pipeline]] + +NLP ํƒœ์Šคํฌ๋ฅผ ์œ„ํ•ด [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋„ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> from transformers import pipeline + +>>> # This model is a `zero-shot-classification` model. +>>> # It will classify text, except you are free to choose any label you might imagine +>>> classifier = pipeline(model="facebook/bart-large-mnli") +>>> classifier( +... "I have a problem with my iphone that needs to be resolved asap!!", +... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], +... ) +{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]} +``` + +### ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ Pipeline[[multimodal-pipeline]] + +[`pipeline`]์€ ์—ฌ๋Ÿฌ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ(์—ญ์ฃผ: ์˜ค๋””์˜ค, ๋น„๋””์˜ค, ํ…์ŠคํŠธ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ)๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋กœ ์‹œ๊ฐ์  ์งˆ์˜์‘๋‹ต(VQA; Visual Question Answering) ํƒœ์Šคํฌ๋Š” ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์–ด๋–ค ์ด๋ฏธ์ง€ ๋งํฌ๋‚˜ ๋ฌป๊ณ  ์‹ถ์€ ์งˆ๋ฌธ๋„ ์ž์œ ๋กญ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” URL ๋˜๋Š” ๋กœ์ปฌ ๊ฒฝ๋กœ์˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•ด์ฃผ์„ธ์š”. + +์˜ˆ๋ฅผ ๋“ค์–ด ์ด [๊ฑฐ๋ž˜๋ช…์„ธ์„œ ์‚ฌ์ง„](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png)์—์„œ ๊ฑฐ๋ž˜๋ช…์„ธ์„œ ๋ฒˆํ˜ธ๋ฅผ ๋ฌป๊ณ  ์‹ถ๋‹ค๋ฉด, + +```py +>>> from transformers import pipeline + +>>> vqa = pipeline(model="impira/layoutlm-document-qa") +>>> vqa( +... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", +... question="What is the invoice number?", +... ) +[{'score': 0.42514941096305847, 'answer': 'us-001', 'start': 16, 'end': 16}] +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_webserver.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_webserver.md new file mode 100644 index 0000000000000000000000000000000000000000..b7d5366c57c4ef2edc7fb7505304299cf76317c2 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pipeline_webserver.md @@ -0,0 +1,144 @@ + + +# ์›น ์„œ๋ฒ„๋ฅผ ์œ„ํ•œ ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉํ•˜๊ธฐ[[using_pipelines_for_a_webserver]] + + +์ถ”๋ก  ์—”์ง„์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ณต์žกํ•œ ์ฃผ์ œ์ด๋ฉฐ, "์ตœ์„ ์˜" ์†”๋ฃจ์…˜์€ ๋ฌธ์ œ ๊ณต๊ฐ„์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. CPU ๋˜๋Š” GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ณ  ๋‚ฎ์€ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์›ํ•˜๋Š”์ง€, ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์›ํ•˜๋Š”์ง€, ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๊ธธ ์›ํ•˜๋Š”์ง€, ํ•˜๋‚˜์˜ ํŠน์ • ๋ชจ๋ธ์„ ๊ณ ๋„๋กœ ์ตœ์ ํ™”ํ•˜๊ธธ ์›ํ•˜๋Š”์ง€ ๋“ฑ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด ์ฃผ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, ์ด ์žฅ์—์„œ ์ œ์‹œํ•˜๋Š” ๊ฒƒ์€ ์ฒ˜์Œ ์‹œ๋„ํ•ด ๋ณด๊ธฐ์— ์ข‹์€ ์ถœ๋ฐœ์ ์ผ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ์ด ์žฅ์„ ์ฝ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ํ•„์š”๋กœ ํ•˜๋Š” ์ตœ์ ์˜ ์†”๋ฃจ์…˜์€ ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +ํ•ต์‹ฌ์ ์œผ๋กœ ์ดํ•ดํ•ด์•ผ ํ•  ์ ์€ [dataset](pipeline_tutorial#using-pipelines-on-a-dataset)๋ฅผ ๋‹ค๋ฃฐ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ์›น ์„œ๋ฒ„๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์š”์ฒญ์„ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ๋“ค์–ด์˜ค๋Š” ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹œ์Šคํ…œ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +๋ณดํ†ต ์›น ์„œ๋ฒ„๋Š” ๋‹ค์–‘ํ•œ ์š”์ฒญ์„ ๋™์‹œ์— ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ๋งค์šฐ ๋‹ค์ค‘ํ™”๋œ ๊ตฌ์กฐ(๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋”ฉ, ๋น„๋™๊ธฐ ๋“ฑ)๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์—, ํŒŒ์ดํ”„๋ผ์ธ(๋Œ€๋ถ€๋ถ„ ํŒŒ์ดํ”„๋ผ์ธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋ธ)์€ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์— ๊ทธ๋‹ค์ง€ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ํŒŒ์ดํ”„๋ผ์ธ์€ ๋งŽ์€ RAM์„ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํŒŒ์ดํ”„๋ผ์ธ์ด ์‹คํ–‰ ์ค‘์ด๊ฑฐ๋‚˜ ๊ณ„์‚ฐ ์ง‘์•ฝ์ ์ธ ์ž‘์—… ์ค‘์ผ ๋•Œ ๋ชจ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. + +์ด ๋ฌธ์ œ๋ฅผ ์šฐ๋ฆฌ๋Š” ์›น ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ๋ฐ›๊ณ  ๋ณด๋‚ด๋Š” ๊ฐ€๋ฒผ์šด ๋ถ€ํ•˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ์‹ค์ œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‹จ์ผ ์Šค๋ ˆ๋“œ๋ฅผ ๊ฐ–๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ๋Š” `starlette` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +์‹ค์ œ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์ง€๋งŒ, ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„  ์ฝ”๋“œ๋ฅผ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +`server.py`๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + +```py +from starlette.applications import Starlette +from starlette.responses import JSONResponse +from starlette.routing import Route +from transformers import pipeline +import asyncio + + +async def homepage(request): + payload = await request.body() + string = payload.decode("utf-8") + response_q = asyncio.Queue() + await request.app.model_queue.put((string, response_q)) + output = await response_q.get() + return JSONResponse(output) + + +async def server_loop(q): + pipe = pipeline(model="google-bert/bert-base-uncased") + while True: + (string, response_q) = await q.get() + out = pipe(string) + await response_q.put(out) + + +app = Starlette( + routes=[ + Route("/", homepage, methods=["POST"]), + ], +) + + +@app.on_event("startup") +async def startup_event(): + q = asyncio.Queue() + app.model_queue = q + asyncio.create_task(server_loop(q)) +``` + +์ด์ œ ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +uvicorn server:app +``` + +์ด์ œ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +curl -X POST -d "test [MASK]" http://localhost:8000/ +#[{"score":0.7742936015129089,"token":1012,"token_str":".","sequence":"test."},...] +``` + +์ž, ์ด์ œ ์›น ์„œ๋ฒ„๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ข‹์€ ๊ฐœ๋…์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! + +์ค‘์š”ํ•œ ์ ์€ ๋ชจ๋ธ์„ **ํ•œ ๋ฒˆ๋งŒ** ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›น ์„œ๋ฒ„์—๋Š” ๋ชจ๋ธ์˜ ์‚ฌ๋ณธ์ด ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์€ ๋ถˆํ•„์š”ํ•œ RAM์ด ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ +๋™์  ๋ฐฐ์น˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๋ก  ์ „ ๋‹จ๊ณ„์— ๋ช‡ ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ์ถ•์ ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฉ‹์ง„ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + +์ฝ”๋“œ๋Š” ์˜๋„์ ์œผ๋กœ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์˜์‚ฌ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! +์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘๋™์‹œํ‚ค๊ธฐ ์ „์— ์‹œ์Šคํ…œ ์ž์›์ด ์ถฉ๋ถ„ํ•œ์ง€ ํ™•์ธํ•˜์„ธ์š”! + + +```py +(string, rq) = await q.get() +strings = [] +queues = [] +while True: + try: + (string, rq) = await asyncio.wait_for(q.get(), timeout=0.001) # 1ms + except asyncio.exceptions.TimeoutError: + break + strings.append(string) + queues.append(rq) +strings +outs = pipe(strings, batch_size=len(strings)) +for rq, out in zip(queues, outs): + await rq.put(out) +``` + +๋‹ค์‹œ ๋ง์”€ ๋“œ๋ฆฌ์ž๋ฉด, ์ œ์•ˆ๋œ ์ฝ”๋“œ๋Š” ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์ตœ์ ํ™”๋˜์—ˆ์œผ๋ฉฐ, ์ตœ์ƒ์˜ ์ฝ”๋“œ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. +์ฒซ์งธ, ๋ฐฐ์น˜ ํฌ๊ธฐ ์ œํ•œ์ด ์—†์œผ๋ฉฐ ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ข‹์€ ๋ฐฉ์‹์ด ์•„๋‹™๋‹ˆ๋‹ค. +๋‘˜์งธ, ๋ชจ๋“  ํ ๊ฐ€์ ธ์˜ค๊ธฐ์—์„œ ํƒ€์ž„์•„์›ƒ์ด ์žฌ์„ค์ •๋˜๋ฏ€๋กœ ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— 1ms๋ณด๋‹ค ํ›จ์”ฌ ์˜ค๋ž˜ ๊ธฐ๋‹ค๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ฒซ ๋ฒˆ์งธ ์š”์ฒญ์„ ๊ทธ๋งŒํผ ์ง€์—ฐ์‹œํ‚ด). + +๋‹จ์ผ 1ms ๊ธธ์ด์˜ ๋ฐ๋“œ๋ผ์ธ์„ ๋‘๋Š” ํŽธ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. + +์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํ๊ฐ€ ๋น„์–ด ์žˆ์–ด๋„ ํ•ญ์ƒ 1ms๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. +ํ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์„ ๋•Œ ์ถ”๋ก ์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ตœ์„ ์˜ ๋ฐฉ๋ฒ•์ด ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๋ฐฐ์น˜ ์ž‘์—…์ด ์‚ฌ์šฉ๋ก€์— ๋”ฐ๋ผ ์ •๋ง๋กœ ์ค‘์š”ํ•˜๋‹ค๋ฉด ์˜๋ฏธ๊ฐ€ ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ตœ์ƒ์˜ ์†”๋ฃจ์…˜์€ ์—†์Šต๋‹ˆ๋‹ค. + +## ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์‚ฌํ•ญ[[few_things_you_might want_to_consider]] + +### ์—๋Ÿฌ ํ™•์ธ[[error_checking]] + +ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์—ฌ์ง€๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. +๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ชจ์ž๋ผ๊ฑฐ๋‚˜, ๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜๊ฑฐ๋‚˜, ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ์— ์‹คํŒจํ•˜๊ฑฐ๋‚˜, ์ฟผ๋ฆฌ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๊ฑฐ๋‚˜, ์ฟผ๋ฆฌ๋Š” ์ •ํ™•ํ•ด๋„ ๋ชจ๋ธ ์„ค์ •์ด ์ž˜๋ชป๋˜์–ด ์‹คํ–‰์— ์‹คํŒจํ•˜๋Š” ๋“ฑ๋“ฑ ๋งŽ์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. + +์ผ๋ฐ˜์ ์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์ข‹์œผ๋ฏ€๋กœ +์˜ค๋ฅ˜๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด `try...except` ๋ฌธ์„ ๋งŽ์ด ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๋ณด์•ˆ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์€ ๋ณด์•ˆ์ƒ ์œ„ํ—˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์ ์„ ๋ช…์‹ฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. + +### ์„œํ‚ท ๋ธŒ๋ ˆ์ดํ‚น[[circuit_breaking]] + +์›น ์„œ๋ฒ„๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์„œํ‚ท ๋ธŒ๋ ˆ์ดํ‚น์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋” ๋‚˜์€ ์ƒํ™ฉ์— ์ง๋ฉดํ•ฉ๋‹ˆ๋‹ค. +์ฆ‰, ์ด๋Š” ์„œ๋ฒ„๊ฐ€ ์ฟผ๋ฆฌ๋ฅผ ๋ฌด๊ธฐํ•œ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€์‹  ๊ณผ๋ถ€ํ•˜ ์ƒํƒœ์ผ ๋•Œ ์ ์ ˆํ•œ ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. +์„œ๋ฒ„๊ฐ€ ๋งค์šฐ ์˜ค๋žœ ์‹œ๊ฐ„ ๋™์•ˆ ๋Œ€๊ธฐํ•˜๊ฑฐ๋‚˜ ์ ๋‹นํ•œ ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„์— 504 ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹  503 ์—๋Ÿฌ๋ฅผ ๋น ๋ฅด๊ฒŒ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ œ์•ˆ๋œ ์ฝ”๋“œ์—๋Š” ๋‹จ์ผ ํ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๊ฐ€ ๋น„๊ต์  ์‰ฝ์Šต๋‹ˆ๋‹ค. +ํ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์€ ์›น ์„œ๋ฒ„๊ฐ€ ๊ณผ๋ถ€ํ•˜ ์ƒํ•ญ ํ•˜์— ์žˆ์„ ๋•Œ ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์ž‘์—…์ž…๋‹ˆ๋‹ค. + +### ๋ฉ”์ธ ์“ฐ๋ ˆ๋“œ ์ฐจ๋‹จ[[blocking_the_main_thread]] + +ํ˜„์žฌ PyTorch๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์‹คํ–‰ ์ค‘์—๋Š” ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๊ฐ€ ์ฐจ๋‹จ๋ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ PyTorch๋ฅผ ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ/ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰ํ•˜๋„๋ก ๊ฐ•์ œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. +์—ฌ๊ธฐ์„œ๋Š” ์ด ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๋” ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค(์ฃผ๋กœ ์Šค๋ ˆ๋“œ, ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ, ํ๊ฐ€ ์„œ๋กœ ์ž˜ ๋งž์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค). +ํ•˜์ง€๋งŒ ๊ถ๊ทน์ ์œผ๋กœ๋Š” ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +๋‹จ์ผ ํ•ญ๋ชฉ์˜ ์ถ”๋ก ์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋ฉด (> 1์ดˆ), ๋ฉ”์ธ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ฐจ๋‹จํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ด ๊ฒฝ์šฐ ์ถ”๋ก  ์ค‘ ๋ชจ๋“  ์ฟผ๋ฆฌ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐ›๊ธฐ ์ „์— 1์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +### ๋™์  ๋ฐฐ์น˜[[dynamic_batching]] + +์ผ๋ฐ˜์ ์œผ๋กœ, ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๊ฐ€ 1๊ฐœ ํ•ญ๋ชฉ์„ ํ•œ ๋ฒˆ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์— ๋น„ํ•ด ๋ฐ˜๋“œ์‹œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค(์ž์„ธํ•œ ๋‚ด์šฉ์€ [`batching details`](./main_classes/pipelines#pipeline-batching)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”). +ํ•˜์ง€๋งŒ ์˜ฌ๋ฐ”๋ฅธ ์„ค์ •์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ํšจ๊ณผ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +API์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์†๋„ ์ €ํ•˜์˜ ๊ฐ€๋Šฅ์„ฑ์ด ๋งค์šฐ ๋†’๊ธฐ ๋•Œ๋ฌธ์— ๋™์  ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. +ํ•˜์ง€๋งŒ ๋งค์šฐ ํฐ ๋ชจ๋ธ์ธ BLOOM ์ถ”๋ก ์˜ ๊ฒฝ์šฐ ๋™์  ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์—๊ฒŒ ์ ์ ˆํ•œ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ **ํ•„์ˆ˜**์ž…๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pr_checks.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pr_checks.md new file mode 100644 index 0000000000000000000000000000000000000000..1d155cd1fb9ddbc0f4c70b16faeba261b75f573f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/pr_checks.md @@ -0,0 +1,200 @@ + + +# Pull Request์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ [[checks-on-a-pull-request]] + +๐Ÿค— Transformers์—์„œ Pull Request๋ฅผ ์—ด ๋•Œ, ๊ธฐ์กด์— ์žˆ๋Š” ๊ฒƒ์„ ๋ง๊ฐ€๋œจ๋ฆฌ์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ƒ๋‹นํ•œ ์ˆ˜์˜ ๊ฒ€์‚ฌ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„ค ๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค: +- ์ผ๋ฐ˜์ ์ธ ํ…Œ์ŠคํŠธ +- ๋ฌธ์„œ ๋นŒ๋“œ +- ์ฝ”๋“œ ๋ฐ ๋ฌธ์„œ ์Šคํƒ€์ผ +- ์ผ๋ฐ˜ ์ €์žฅ์†Œ ์ผ๊ด€์„ฑ + +์ด ๋ฌธ์„œ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋‹ค์–‘ํ•œ ๊ฒ€์‚ฌ์™€ ๊ทธ ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜๊ณ , PR์—์„œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฒ€์‚ฌ๊ฐ€ ์‹คํŒจํ•œ ๊ฒฝ์šฐ ๋กœ์ปฌ์—์„œ ์–ด๋–ป๊ฒŒ ๋””๋ฒ„๊ทธํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +์ฐธ๊ณ ๋กœ, ์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐœ๋ฐœ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install transformers[dev] +``` + +๋˜๋Š” Transformers ์ €์žฅ์†Œ ๋‚ด์— ํŽธ์ง‘ ๊ฐ€๋Šฅํ•œ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install -e .[dev] +``` + +Transformers์˜ ์„ ํƒ์  ์ข…์†์„ฑ ์ˆ˜๊ฐ€ ๋งŽ์ด ๋Š˜์–ด๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ ์„ค์น˜๋ฅผ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ ์„ค์น˜๊ฐ€ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ, ์ž‘์—… ์ค‘์ธ Deep Learning ํ”„๋ ˆ์ž„์›Œํฌ (PyTorch, TensorFlow ๋ฐ/๋˜๋Š” Flax)๋ฅผ ์„ค์น˜ํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +pip install transformers[quality] +``` + +ํŽธ์ง‘ ๊ฐ€๋Šฅํ•œ ์„ค์น˜์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +pip install -e .[quality] +``` + + +## ํ…Œ์ŠคํŠธ [[tests]] + +`ci/circleci: run_tests_`๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์ž‘์—…์€ Transformers ํ…Œ์ŠคํŠธ ๋ชจ์Œ์˜ ์ผ๋ถ€๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์€ ํŠน์ • ํ™˜๊ฒฝ์—์„œ ์ผ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด `ci/circleci: run_tests_pipelines_tf`๋Š” TensorFlow๋งŒ ์„ค์น˜๋œ ํ™˜๊ฒฝ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ์—์„œ ์‹ค์ œ๋กœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์—†์„ ๋•Œ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด, ํ…Œ์ŠคํŠธ ๋ชจ์Œ์˜ ์ผ๋ถ€๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ณ€๊ฒฝ ์ „ํ›„์— ๋Œ€ํ•œ ์ฐจ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์œ ํ‹ธ๋ฆฌํ‹ฐ๊ฐ€ ์‹คํ–‰๋˜๊ณ , ํ•ด๋‹น ์ฐจ์ด์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ํ…Œ์ŠคํŠธ๊ฐ€ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. ์ด ์œ ํ‹ธ๋ฆฌํ‹ฐ๋Š” ๋กœ์ปฌ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +python utils/tests_fetcher.py +``` + +Transformers ์ €์žฅ์†Œ์˜ ์ตœ์ƒ๋‹จ์—์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด ์œ ํ‹ธ๋ฆฌํ‹ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค: + +1. ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋Š” ํŒŒ์ผ๋งˆ๋‹ค ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ฝ”๋“œ์ธ์ง€ ์ฃผ์„ ๋˜๋Š” ๋ฌธ์„œ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ ๋ณ€๊ฒฝ์ด ์žˆ๋Š” ํŒŒ์ผ๋งŒ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. +2. ์†Œ์Šค ์ฝ”๋“œ ํŒŒ์ผ์˜ ๊ฐ ํŒŒ์ผ์— ๋Œ€ํ•ด ์žฌ๊ท€์ ์œผ๋กœ ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ชจ๋“  ํŒŒ์ผ์„ ์ œ๊ณตํ•˜๋Š” ๋‚ด๋ถ€ ๋งต์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ B๊ฐ€ ๋ชจ๋“ˆ A๋ฅผ ๊ฐ€์ ธ์˜ค๋ฉด ๋ชจ๋“ˆ A๋Š” ๋ชจ๋“ˆ B์— ์˜ํ–ฅ์„ ์ค๋‹ˆ๋‹ค. ์žฌ๊ท€์ ์ธ ์˜ํ–ฅ์—๋Š” ๊ฐ ๋ชจ๋“ˆ์ด ์ด์ „ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ชจ๋“ˆ ์ฒด์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +3. ๋‹จ๊ณ„ 1์—์„œ ์ˆ˜์ง‘ํ•œ ํŒŒ์ผ์— ์ด ๋งต์„ ์ ์šฉํ•˜์—ฌ PR์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๋ชจ๋ธ ํŒŒ์ผ ๋ชฉ๋ก์„ ์–ป์Šต๋‹ˆ๋‹ค. +4. ๊ฐ ํŒŒ์ผ์„ ํ•ด๋‹นํ•˜๋Š” ํ…Œ์ŠคํŠธ ํŒŒ์ผ์— ๋งคํ•‘ํ•˜๊ณ  ์‹คํ–‰ํ•  ํ…Œ์ŠคํŠธ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + +๋กœ์ปฌ์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹จ๊ณ„ 1, 3 ๋ฐ 4์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์‹คํ–‰๋˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋Š” ๋˜ํ•œ `test_list.txt`๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•  ํ…Œ์ŠคํŠธ ๋ชฉ๋ก์„ ํฌํ•จํ•˜๋ฉฐ, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ํ•ด๋‹น ํ…Œ์ŠคํŠธ๋ฅผ ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt) +``` + +์ž˜๋ชป๋œ ์‚ฌํ•ญ์ด ๋ˆ„๋ฝ๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์ „์ฒด ํ…Œ์ŠคํŠธ ๋ชจ์Œ๋„ ๋งค์ผ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. + +## ๋ฌธ์„œ ๋นŒ๋“œ [[documentation-build]] + +`build_pr_documentation` ์ž‘์—…์€ ๋ฌธ์„œ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๋ฏธ๋ฆฌ ๋ณด๊ธฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ PR์ด ๋ณ‘ํ•ฉ๋œ ํ›„ ๋ชจ๋“  ๊ฒƒ์ด ์ œ๋Œ€๋กœ ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋กœ๋ด‡์€ PR์— ๋ฌธ์„œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. PR์—์„œ ๋งŒ๋“  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ์ž๋™์œผ๋กœ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์— ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋นŒ๋“œ์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ **์„ธ๋ถ€ ์ •๋ณด**๋ฅผ ํด๋ฆญํ•˜์—ฌ ์–ด๋””์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅ˜๋Š” ์ฃผ๋กœ `toctree`์— ๋ˆ„๋ฝ๋œ ํŒŒ์ผ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•œ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. + +๋กœ์ปฌ์—์„œ ๋ฌธ์„œ๋ฅผ ๋นŒ๋“œํ•˜๊ฑฐ๋‚˜ ๋ฏธ๋ฆฌ ๋ณผ ๊ฒฝ์šฐ, docs ํด๋”์˜ [`README.md`](https://github.com/huggingface/transformers/tree/main/docs)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ์ฝ”๋“œ ๋ฐ ๋ฌธ์„œ ์Šคํƒ€์ผ [[code-and-documentation-style]] + +`black`๊ณผ `ruff`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์†Œ์Šค ํŒŒ์ผ, ์˜ˆ์ œ ๋ฐ ํ…Œ์ŠคํŠธ์— ์ฝ”๋“œ ํ˜•์‹์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, `utils/style_doc.py`์—์„œ ๋ฌธ์„œ ๋ฌธ์ž์—ด๊ณผ `rst` ํŒŒ์ผ์˜ ํ˜•์‹, ๊ทธ๋ฆฌ๊ณ  Transformers์˜ `__init__.py` ํŒŒ์ผ์—์„œ ์‹คํ–‰๋˜๋Š” ์ง€์—ฐ๋œ ์ž„ํฌํŠธ์˜ ์ˆœ์„œ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ๋„๊ตฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์€ ๋‹ค์Œ์„ ์‹คํ–‰ํ•จ์œผ๋กœ์จ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```bash +make style +``` + +CI๋Š” ์ด๋Ÿฌํ•œ ์‚ฌํ•ญ์ด `ci/circleci: check_code_quality` ๊ฒ€์‚ฌ ๋‚ด์—์„œ ์ ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `ruff`๋„ ์‹คํ–‰๋˜๋ฉฐ, ์ •์˜๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋‚˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ๊ฒฝ๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒ€์‚ฌ๋ฅผ ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```bash +make quality +``` + +์ด ์ž‘์—…์€ ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ˜„์žฌ ๋ธŒ๋žœ์น˜์—์„œ ์ˆ˜์ •ํ•œ ํŒŒ์ผ์— ๋Œ€ํ•ด์„œ๋งŒ ๋™์ผํ•œ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์„ธ์š”. + +```bash +make fixup +``` + +์ด ๋ช…๋ น์€ ํ˜„์žฌ ๋ธŒ๋žœ์น˜์—์„œ ์ˆ˜์ •ํ•œ ํŒŒ์ผ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ถ”๊ฐ€์ ์ธ ๊ฒ€์‚ฌ๋„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +## ์ €์žฅ์†Œ ์ผ๊ด€์„ฑ [[repository-consistency]] + +์ด๋Š” PR์ด ์ €์žฅ์†Œ๋ฅผ ์ •์ƒ์ ์ธ ์ƒํƒœ๋กœ ์œ ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ๋ชจ์€ ๊ฒƒ์ด๋ฉฐ, `ci/circleci: check_repository_consistency` ๊ฒ€์‚ฌ์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์‹คํ–‰ํ•จ์œผ๋กœ์จ ๋กœ์ปฌ์—์„œ ์ด ๊ฒ€์‚ฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +make repo-consistency +``` + +์ด ๊ฒ€์‚ฌ๋Š” ๋‹ค์Œ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. + +- init์— ์ถ”๊ฐ€๋œ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๋ฌธ์„œํ™”๋˜์—ˆ๋Š”์ง€ (`utils/check_repo.py`์—์„œ ์ˆ˜ํ–‰) +- `__init__.py` ํŒŒ์ผ์˜ ๋‘ ์„น์…˜์— ๋™์ผํ•œ ๋‚ด์šฉ์ด ์žˆ๋Š”์ง€ (`utils/check_inits.py`์—์„œ ์ˆ˜ํ–‰) +- ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ๋ณต์‚ฌ๋œ ์ฝ”๋“œ๊ฐ€ ์›๋ณธ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ (`utils/check_copies.py`์—์„œ ์ˆ˜ํ–‰) +- ๋ชจ๋“  ๊ตฌ์„ฑ ํด๋ž˜์Šค์— docstring์— ์–ธ๊ธ‰๋œ ์œ ํšจํ•œ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์žˆ๋Š”์ง€ (`utils/check_config_docstrings.py`์—์„œ ์ˆ˜ํ–‰) +- ๋ชจ๋“  ๊ตฌ์„ฑ ํด๋ž˜์Šค๊ฐ€ ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋ธ๋ง ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ๋งŒ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ (`utils/check_config_attributes.py`์—์„œ ์ˆ˜ํ–‰) +- README์™€ ๋ฌธ์„œ ์ธ๋ฑ์Šค์˜ ๋ฒˆ์—ญ์ด ๋ฉ”์ธ README์™€ ๋™์ผํ•œ ๋ชจ๋ธ ๋ชฉ๋ก์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ (`utils/check_copies.py`์—์„œ ์ˆ˜ํ–‰) +- ๋ฌธ์„œ์˜ ์ž๋™ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์ด ์ตœ์‹  ์ƒํƒœ์ธ์ง€ (`utils/check_table.py`์—์„œ ์ˆ˜ํ–‰) +- ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ์„ ํƒ์  ์ข…์†์„ฑ์ด ์„ค์น˜๋˜์ง€ ์•Š์•˜๋”๋ผ๋„ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ (`utils/check_dummies.py`์—์„œ ์ˆ˜ํ–‰) + +์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ๊ฐ€ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ, ์ฒ˜์Œ ๋‘ ๊ฐ€์ง€ ํ•ญ๋ชฉ์€ ์ˆ˜๋™์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋ฉฐ, ๋‚˜๋จธ์ง€ ๋„ค ๊ฐ€์ง€ ํ•ญ๋ชฉ์€ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ์ž๋™์œผ๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```bash +make fix-copies +``` + +์ถ”๊ฐ€์ ์ธ ๊ฒ€์‚ฌ๋Š” ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” PR์— ๋Œ€ํ•œ ๊ฒƒ์œผ๋กœ, ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +- ์ถ”๊ฐ€๋œ ๋ชจ๋“  ๋ชจ๋ธ์ด Auto-mapping์— ์žˆ๋Š”์ง€ (`utils/check_repo.py`์—์„œ ์ˆ˜ํ–‰) + +- ๋ชจ๋“  ๋ชจ๋ธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ…Œ์ŠคํŠธ๋˜์—ˆ๋Š”์ง€ (`utils/check_repo.py`์—์„œ ์ˆ˜ํ–‰) + + + +### ๋ณต์‚ฌ๋ณธ ํ™•์ธ [[check-copies]] + +Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ชจ๋ธ ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋งค์šฐ ์™„๊ณ ํ•˜๋ฉฐ, ๊ฐ ๋ชจ๋ธ์€ ๋‹ค๋ฅธ ๋ชจ๋ธ์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์™„์ „ํžˆ ๋‹จ์ผ ํŒŒ์ผ๋กœ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํŠน์ • ๋ชจ๋ธ์˜ ์ฝ”๋“œ ๋ณต์‚ฌ๋ณธ์ด ์›๋ณธ๊ณผ ์ผ๊ด€๋œ ์ƒํƒœ๋กœ ์œ ์ง€๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒ„๊ทธ ์ˆ˜์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋ชจ๋ธ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ชจ๋“  ๋ชจ๋ธ์„ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ˆ˜์ •์„ ์ ์šฉํ• ์ง€ ์ˆ˜์ •๋œ ์‚ฌ๋ณธ์„ ์‚ญ์ œํ• ์ง€ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +ํŒŒ์ผ์ด ๋‹ค๋ฅธ ํŒŒ์ผ์˜ ์™„์ „ํ•œ ์‚ฌ๋ณธ์ธ ๊ฒฝ์šฐ ํ•ด๋‹น ํŒŒ์ผ์„ `utils/check_copies.py`์˜ `FULL_COPIES` ์ƒ์ˆ˜์— ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + + +์ด ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ `# Copied from xxx` ํ˜•์‹์˜ ์ฃผ์„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. `xxx`์—๋Š” ์•„๋ž˜์— ๋ณต์‚ฌ๋˜๋Š” ํด๋ž˜์Šค ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ „์ฒด ๊ฒฝ๋กœ๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด `RobertaSelfOutput`์€ `BertSelfOutput` ํด๋ž˜์Šค์˜ ๋ณต์‚ฌ๋ณธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L289)์—์„œ ์ฃผ์„์ด ์žˆ์Šต๋‹ˆ๋‹ค: + + +```py +# Copied from transformers.models.bert.modeling_bert.BertSelfOutput +``` + +ํด๋ž˜์Šค ์ „์ฒด์— ์ˆ˜์ •์„ ์ ์šฉํ•˜๋Š” ๋Œ€์‹ ์— ๋ณต์‚ฌ๋ณธ๊ณผ ๊ด€๋ จ์žˆ๋Š” ๋ฉ”์„œ๋“œ์— ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L598)์—์„œ `RobertaPreTrainedModel._init_weights`๊ฐ€ `BertPreTrainedModel`์˜ ๋™์ผํ•œ ๋ฉ”์„œ๋“œ์—์„œ ๋ณต์‚ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•ด๋‹น ์ฃผ์„์ด ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +# Copied from transformers.models.bert.modeling_bert.BertPreTrainedModel._init_weights +``` + +๋ณต์‚ฌ๋ณธ์ด ์ด๋ฆ„๋งŒ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: ์˜ˆ๋ฅผ ๋“ค์–ด `RobertaAttention`์—์„œ `BertSelfAttention` ๋Œ€์‹  `RobertaSelfAttention`์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ทธ ์™ธ์—๋Š” ์ฝ”๋“œ๊ฐ€ ์™„์ „ํžˆ ๋™์ผํ•ฉ๋‹ˆ๋‹ค: ์ด ๋•Œ `# Copied from`์€ `Copied from xxx with foo->bar`์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ๋ฌธ์ž์—ด ๋Œ€์ฒด๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋“  `foo` ์ธ์Šคํ„ด์Šค๋ฅผ `bar`๋กœ ๋ฐ”๊ฟ”์„œ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/2bd7a27a671fd1d98059124024f580f8f5c0f3b5/src/transformers/models/roberta/modeling_roberta.py#L304C1-L304C86)์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +# Copied from transformers.models.bert.modeling_bert.BertAttention with Bert->Roberta +``` + +ํ™”์‚ดํ‘œ ์ฃผ๋ณ€์—๋Š” ๊ณต๋ฐฑ์ด ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(๊ณต๋ฐฑ์ด ๋Œ€์ฒด ํŒจํ„ด์˜ ์ผ๋ถ€์ธ ๊ฒฝ์šฐ๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค). + +๋Œ€์ฒด ํŒจํ„ด์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ ํŒจํ„ด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด `CamemberForMaskedLM`์€ ๋‘ ๊ฐ€์ง€ ๋Œ€์ฒด ์‚ฌํ•ญ์„ ๊ฐ€์ง„ `RobertaForMaskedLM`์˜ ๋ณต์‚ฌ๋ณธ์ž…๋‹ˆ๋‹ค: `Roberta`๋ฅผ `Camembert`๋กœ ๋Œ€์ฒดํ•˜๊ณ  `ROBERTA`๋ฅผ `CAMEMBERT`๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/camembert/modeling_camembert.py#L929)์—์„œ ์ด๊ฒƒ์ด ์ฃผ์„์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +# Copied from transformers.models.roberta.modeling_roberta.RobertaForMaskedLM with Roberta->Camembert, ROBERTA->CAMEMBERT +``` + +์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ(์ด์ „ ์ˆ˜์ •๊ณผ ์ถฉ๋Œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ) ์ˆ˜์ •์€ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. + + + +์ƒˆ ๋ณ€๊ฒฝ์ด ์„œ์‹์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ(์งง์€ ์ด๋ฆ„์„ ๋งค์šฐ ๊ธด ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒฝ์šฐ) ์ž๋™ ์„œ์‹ ์ง€์ •๊ธฐ๋ฅผ ์ ์šฉํ•œ ํ›„ ๋ณต์‚ฌ๋ณธ์ด ๊ฒ€์‚ฌ๋ฉ๋‹ˆ๋‹ค. + + + +ํŒจํ„ด์˜ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ(๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ํ˜ผ์šฉ๋œ ๋Œ€์ฒด ์–‘์‹) `all-casing` ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. [์—ฌ๊ธฐ](https://github.com/huggingface/transformers/blob/15082a9dc6950ecae63a0d3e5060b2fc7f15050a/src/transformers/models/mobilebert/modeling_mobilebert.py#L1237)์—์„œ `MobileBertForSequenceClassification`์—์„œ ์‚ฌ์šฉ๋œ ์˜ˆ์‹œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +# Copied from transformers.models.bert.modeling_bert.BertForSequenceClassification with Bert->MobileBert all-casing +``` + +์ด ๊ฒฝ์šฐ, ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณต์‚ฌ๋ฉ๋‹ˆ๋‹ค: +- `MobileBert`์—์„œ `Bert`๋กœ(์˜ˆ: `MobileBertModel`์„ init์—์„œ ์‚ฌ์šฉํ•  ๋•Œ) +- `mobilebert`์—์„œ `bert`๋กœ(์˜ˆ: `self.mobilebert`๋ฅผ ์ •์˜ํ•  ๋•Œ) +- `MOBILEBERT`์—์„œ `BERT`๋กœ(`MOBILEBERT_INPUTS_DOCSTRING` ์ƒ์ˆ˜์—์„œ) diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/awq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/awq.md new file mode 100644 index 0000000000000000000000000000000000000000..3855b42a73525ab9bebbb15d37df9070f1ffa143 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/awq.md @@ -0,0 +1,233 @@ + + +# AWQ [[awq]] + + + +์ด [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1HzZH89yAXJaZgwJDhQj9LqSBux932BvY) ์œผ๋กœ AWQ ์–‘์žํ™”๋ฅผ ์‹ค์Šตํ•ด๋ณด์„ธ์š” ! + + + +[Activation-aware Weight Quantization (AWQ)](https://hf.co/papers/2306.00978)์€ ๋ชจ๋ธ์˜ ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋ฅผ ์–‘์žํ™”ํ•˜์ง€ ์•Š๊ณ , LLM ์„ฑ๋Šฅ์— ์ค‘์š”ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ์จ 4๋น„ํŠธ ์ •๋ฐ€๋„๋กœ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•ด๋„ ์„ฑ๋Šฅ ์ €ํ•˜ ์—†์ด ์–‘์žํ™” ์†์‹ค์„ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +AWQ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [llm-awq](https://github.com/mit-han-lab/llm-awq), [autoawq](https://github.com/casper-hansen/AutoAWQ) , [optimum-intel](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc) ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Transformers๋Š” llm-awq, autoawq ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์–‘์žํ™”๋œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” autoawq๋กœ ์–‘์žํ™”๋œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๋‚˜, llm-awq๋กœ ์–‘์žํ™”๋œ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ๋„ ์œ ์‚ฌํ•œ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. + +autoawq๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install autoawq +``` + +AWQ ์–‘์žํ™”๋œ ๋ชจ๋ธ์€ ํ•ด๋‹น ๋ชจ๋ธ์˜ [config.json](https://huggingface.co/TheBloke/zephyr-7B-alpha-AWQ/blob/main/config.json) ํŒŒ์ผ์˜ `quantization_config` ์†์„ฑ์„ ํ†ตํ•ด ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.: + +```json +{ + "_name_or_path": "/workspace/process/huggingfaceh4_zephyr-7b-alpha/source", + "architectures": [ + "MistralForCausalLM" + ], + ... + ... + ... + "quantization_config": { + "quant_method": "awq", + "zero_point": true, + "group_size": 128, + "bits": 4, + "version": "gemm" + } +} +``` + +์–‘์žํ™”๋œ ๋ชจ๋ธ์€ [`~PreTrainedModel.from_pretrained`] ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ CPU์— ๊ฐ€์ ธ์™”๋‹ค๋ฉด, ๋จผ์ € ๋ชจ๋ธ์„ GPU ์žฅ์น˜๋กœ ์˜ฎ๊ฒจ์•ผ ํ•ฉ๋‹ˆ๋‹ค. `device_map` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฐฐ์น˜ํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "TheBloke/zephyr-7B-alpha-AWQ" +model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0") +``` + +AWQ ์–‘์žํ™” ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ค๋ฉด ์ž๋™์œผ๋กœ ์„ฑ๋Šฅ์ƒ์˜ ์ด์œ ๋กœ ์ธํ•ด ๊ฐ€์ค‘์น˜๋“ค์˜ ๊ธฐ๋ณธ๊ฐ’์ด fp16์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด, `torch_dtype` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "TheBloke/zephyr-7B-alpha-AWQ" +model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32) +``` + +์ถ”๋ก ์„ ๋”์šฑ ๊ฐ€์†ํ™”ํ•˜๊ธฐ ์œ„ํ•ด AWQ ์–‘์žํ™”์™€ [FlashAttention-2](../perf_infer_gpu_one#flashattention-2) ๋ฅผ ๊ฒฐํ•ฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("TheBloke/zephyr-7B-alpha-AWQ", attn_implementation="flash_attention_2", device_map="cuda:0") +``` + +## ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ [[fused-modules]] + +ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์€ ์ •ํ™•๋„์™€ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค. ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์€ [Llama](https://huggingface.co/meta-llama) ์•„ํ‚คํ…์ฒ˜์™€ [Mistral](https://huggingface.co/mistralai/Mistral-7B-v0.1) ์•„ํ‚คํ…์ฒ˜์˜ AWQ๋ชจ๋“ˆ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ง€์›๋˜์ง€ ์•Š๋Š” ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด์„œ๋„ AWQ ๋ชจ๋“ˆ์„ ํ“จ์ฆˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์€ FlashAttention-2์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์ตœ์ ํ™” ๊ธฐ์ˆ ๊ณผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + + + + + + + +์ง€์›๋˜๋Š” ์•„ํ‚คํ…์ฒ˜์—์„œ ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด, [`AwqConfig`] ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜ `fuse_max_seq_len` ๊ณผ `do_fuse=True`๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. `fuse_max_seq_len` ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ „์ฒด ์‹œํ€€์Šค ๊ธธ์ด๋กœ, ์ปจํ…์ŠคํŠธ ๊ธธ์ด์™€ ์˜ˆ์ƒ ์ƒ์„ฑ ๊ธธ์ด๋ฅผ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋” ํฐ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด, [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) ๋ชจ๋ธ์˜ AWQ ๋ชจ๋“ˆ์„ ํ“จ์ฆˆํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +import torch +from transformers import AwqConfig, AutoModelForCausalLM + +model_id = "TheBloke/Mistral-7B-OpenOrca-AWQ" + +quantization_config = AwqConfig( + bits=4, + fuse_max_seq_len=512, + do_fuse=True, +) + +model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config).to(0) +``` + +[TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) ๋ชจ๋ธ์€ ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ `batch_size=1` ๋กœ ์„ฑ๋Šฅ ํ‰๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +
ํ“จ์ฆˆ๋˜์ง€ ์•Š์€ ๋ชจ๋“ˆ
+ +| ๋ฐฐ์น˜ ํฌ๊ธฐ | ํ”„๋ฆฌํ•„ ๊ธธ์ด | ๋””์ฝ”๋“œ ๊ธธ์ด | ํ”„๋ฆฌํ•„ ํ† ํฐ/์ดˆ | ๋””์ฝ”๋“œ ํ† ํฐ/์ดˆ | ๋ฉ”๋ชจ๋ฆฌ (VRAM) | +|-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| +| 1 | 32 | 32 | 60.0984 | 38.4537 | 4.50 GB (5.68%) | +| 1 | 64 | 64 | 1333.67 | 31.6604 | 4.50 GB (5.68%) | +| 1 | 128 | 128 | 2434.06 | 31.6272 | 4.50 GB (5.68%) | +| 1 | 256 | 256 | 3072.26 | 38.1731 | 4.50 GB (5.68%) | +| 1 | 512 | 512 | 3184.74 | 31.6819 | 4.59 GB (5.80%) | +| 1 | 1024 | 1024 | 3148.18 | 36.8031 | 4.81 GB (6.07%) | +| 1 | 2048 | 2048 | 2927.33 | 35.2676 | 5.73 GB (7.23%) | + +
ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ
+ +| ๋ฐฐ์น˜ ํฌ๊ธฐ | ํ”„๋ฆฌํ•„ ๊ธธ์ด | ๋””์ฝ”๋“œ ๊ธธ์ด | ํ”„๋ฆฌํ•„ ํ† ํฐ/์ดˆ | ๋””์ฝ”๋“œ ํ† ํฐ/์ดˆ | ๋ฉ”๋ชจ๋ฆฌ (VRAM) | +|-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| +| 1 | 32 | 32 | 81.4899 | 80.2569 | 4.00 GB (5.05%) | +| 1 | 64 | 64 | 1756.1 | 106.26 | 4.00 GB (5.05%) | +| 1 | 128 | 128 | 2479.32 | 105.631 | 4.00 GB (5.06%) | +| 1 | 256 | 256 | 1813.6 | 85.7485 | 4.01 GB (5.06%) | +| 1 | 512 | 512 | 2848.9 | 97.701 | 4.11 GB (5.19%) | +| 1 | 1024 | 1024 | 3044.35 | 87.7323 | 4.41 GB (5.57%) | +| 1 | 2048 | 2048 | 2715.11 | 89.4709 | 5.57 GB (7.04%) | + +ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ ๋ฐ ํ“จ์ฆˆ๋˜์ง€ ์•Š์€ ๋ชจ๋“ˆ์˜ ์†๋„์™€ ์ฒ˜๋ฆฌ๋Ÿ‰์€ [optimum-benchmark](https://github.com/huggingface/optimum-benchmark)๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +
+
+ generate throughput per batch size +
ํฌ์›Œ๋“œ ํ”ผํฌ ๋ฉ”๋ชจ๋ฆฌ (forward peak memory)/๋ฐฐ์น˜ ํฌ๊ธฐ
+
+
+ forward latency per batch size +
์ƒ์„ฑ ์ฒ˜๋ฆฌ๋Ÿ‰/๋ฐฐ์น˜ํฌ๊ธฐ
+
+
+ +
+ + +ํ“จ์ฆˆ๋œ ๋ชจ๋“ˆ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์•„ํ‚คํ…์ฒ˜์˜ ๊ฒฝ์šฐ, `modules_to_fuse` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ง์ ‘ ํ“จ์ฆˆ ๋งคํ•‘์„ ๋งŒ๋“ค์–ด ์–ด๋–ค ๋ชจ๋“ˆ์„ ํ“จ์ฆˆํ• ์ง€ ์ •์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋กœ, [TheBloke/Yi-34B-AWQ](https://huggingface.co/TheBloke/Yi-34B-AWQ) ๋ชจ๋ธ์˜ AWQ ๋ชจ๋“ˆ์„ ํ“จ์ฆˆํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +```python +import torch +from transformers import AwqConfig, AutoModelForCausalLM + +model_id = "TheBloke/Yi-34B-AWQ" + +quantization_config = AwqConfig( + bits=4, + fuse_max_seq_len=512, + modules_to_fuse={ + "attention": ["q_proj", "k_proj", "v_proj", "o_proj"], + "layernorm": ["ln1", "ln2", "norm"], + "mlp": ["gate_proj", "up_proj", "down_proj"], + "use_alibi": False, + "num_attention_heads": 56, + "num_key_value_heads": 8, + "hidden_size": 7168 + } +) + +model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config).to(0) +``` + + `modules_to_fuse` ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- `"attention"`: ์–ดํ…์…˜ ๋ ˆ์ด์–ด๋Š” ๋‹ค์Œ ์ˆœ์„œ๋กœ ํ“จ์ฆˆํ•˜์„ธ์š” : ์ฟผ๋ฆฌ (query), ํ‚ค (key), ๊ฐ’ (value) , ์ถœ๋ ฅ ํ”„๋กœ์ ์…˜ ๊ณ„์ธต (output projection layer). ํ•ด๋‹น ๋ ˆ์ด์–ด๋ฅผ ํ“จ์ฆˆํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”. +- `"layernorm"`: ์‚ฌ์šฉ์ž ์ •์˜ ํ“จ์ฆˆ ๋ ˆ์ด์–ด ์ •๊ทœํ™”๋กœ ๊ตํ•  ๋ ˆ์ด์–ด ์ •๊ทœํ™” ๋ ˆ์ด์–ด๋ช…. ํ•ด๋‹น ๋ ˆ์ด์–ด๋ฅผ ํ“จ์ฆˆํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”. +- `"mlp"`: ๋‹จ์ผ MLP ๋ ˆ์ด์–ด๋กœ ํ“จ์ฆˆํ•  MLP ๋ ˆ์ด์–ด ์ˆœ์„œ : (๊ฒŒ์ดํŠธ (gate) (๋ด์Šค(dense), ๋ ˆ์ด์–ด(layer), ํฌ์ŠคํŠธ ์–ดํ…์…˜(post-attention)) / ์œ„ / ์•„๋ž˜ ๋ ˆ์ด์–ด). +- `"use_alibi"`: ๋ชจ๋ธ์ด ALiBi positional embedding์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +- `"num_attention_heads"`: ์–ดํ…์…˜ ํ—ค๋“œ (attention heads)์˜ ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +- `"num_key_value_heads"`: ๊ทธ๋ฃนํ™” ์ฟผ๋ฆฌ ์–ดํ…์…˜ (GQA)์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค ๊ฐ’ ํ—ค๋“œ์˜ ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. `num_key_value_heads=num_attention_heads`๋กœ ์„ค์ •ํ•  ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ ๋‹ค์ค‘ ํ—ค๋“œ ์–ดํ…์…˜ (MHA)๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉฐ, `num_key_value_heads=1` ๋Š” ๋‹ค์ค‘ ์ฟผ๋ฆฌ ์–ดํ…์…˜ (MQA)๊ฐ€, ๋‚˜๋จธ์ง€๋Š” GQA๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. +- `"hidden_size"`: ์ˆจ๊ฒจ์ง„ ํ‘œํ˜„(hidden representations)์˜ ์ฐจ์›์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + + +
+ + + +## ExLlama-v2 ์„œํฌํŠธ [[exllama-v2-support]] + +์ตœ์‹  ๋ฒ„์ „ `autoawq`๋Š” ๋น ๋ฅธ ํ”„๋ฆฌํ•„๊ณผ ๋””์ฝ”๋”ฉ์„ ์œ„ํ•ด ExLlama-v2 ์ปค๋„์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ตœ์‹  ๋ฒ„์ „ `autoawq` ๋ฅผ ์„ค์น˜ํ•˜์„ธ์š” : + +```bash +pip install git+https://github.com/casper-hansen/AutoAWQ.git +``` + +๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ `version="exllama"`๋กœ ์„ค์ •ํ•ด `AwqConfig()`๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ชจ๋ธ์— ๋„˜๊ฒจ์ฃผ์„ธ์š”. + +```python +import torch +from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig + +quantization_config = AwqConfig(version="exllama") + +model = AutoModelForCausalLM.from_pretrained( + "TheBloke/Mistral-7B-Instruct-v0.1-AWQ", + quantization_config=quantization_config, + device_map="auto", +) + +input_ids = torch.randint(0, 100, (1, 128), dtype=torch.long, device="cuda") +output = model(input_ids) +print(output.logits) + +tokenizer = AutoTokenizer.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-AWQ") +input_ids = tokenizer.encode("How to make a cake", return_tensors="pt").to(model.device) +output = model.generate(input_ids, do_sample=True, max_length=50, pad_token_id=50256) +print(tokenizer.decode(output[0], skip_special_tokens=True)) +``` + + + +์ด ๊ธฐ๋Šฅ์€ AMD GPUs์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/bitsandbytes.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/bitsandbytes.md new file mode 100644 index 0000000000000000000000000000000000000000..f0420c2869ea13e104980c1847ac765909e0f1cd --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/bitsandbytes.md @@ -0,0 +1,307 @@ + + +# bitsandbytes [[bitsandbytes]] + +[bitsandbytes](https://github.com/TimDettmers/bitsandbytes)๋Š” ๋ชจ๋ธ์„ 8๋น„ํŠธ ๋ฐ 4๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 8๋น„ํŠธ ์–‘์žํ™”๋Š” fp16์˜ ์ด์ƒ์น˜์™€ int8์˜ ๋น„์ด์ƒ์น˜๋ฅผ ๊ณฑํ•œ ํ›„, ๋น„์ด์ƒ์น˜ ๊ฐ’์„ fp16์œผ๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋“ค์„ ํ•ฉ์‚ฐํ•˜์—ฌ fp16์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ด์ƒ์น˜ ๊ฐ’์ด ๋ชจ๋ธ ์„ฑ๋Šฅ์— ๋ฏธ์น˜๋Š” ์ €ํ•˜ ํšจ๊ณผ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4๋น„ํŠธ ์–‘์žํ™”๋Š” ๋ชจ๋ธ์„ ๋”์šฑ ์••์ถ•ํ•˜๋ฉฐ, [QLoRA](https://hf.co/papers/2305.14314)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์–‘์žํ™”๋œ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐ ํ”ํžˆ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + +bitsandbytes๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + + + + +```bash +pip install transformers accelerate bitsandbytes>0.37.0 +``` + + + + +```bash +pip install bitsandbytes>=0.39.0 +pip install --upgrade accelerate transformers +``` + + + + +์ด์ œ `BitsAndBytesConfig`๋ฅผ [`~PreTrainedModel.from_pretrained`] ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•˜์—ฌ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Accelerate ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ง€์›ํ•˜๊ณ  `torch.nn.Linear` ๋ ˆ์ด์–ด๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + + + + +๋ชจ๋ธ์„ 8๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์–ด๋“ค๋ฉฐ, ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•˜๋ ค๋ฉด `device_map="auto"`๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model_8bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + quantization_config=quantization_config +) +``` + +๊ธฐ๋ณธ์ ์œผ๋กœ `torch.nn.LayerNorm`๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์€ `torch.float16`์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์›ํ•œ๋‹ค๋ฉด `torch_dtype` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋“ค ๋ชจ๋“ˆ์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +import torch +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model_8bit = AutoModelForCausalLM.from_pretrained( + "facebook/opt-350m", + quantization_config=quantization_config, + torch_dtype=torch.float32 +) +model_8bit.model.decoder.layers[-1].final_layer_norm.weight.dtype +``` + +๋ชจ๋ธ์ด 8๋น„ํŠธ๋กœ ์–‘์žํ™”๋˜๋ฉด ์ตœ์‹  ๋ฒ„์ „์˜ Transformers์™€ bitsandbytes๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ ์–‘์žํ™”๋œ ๊ฐ€์ค‘์น˜๋ฅผ Hub์— ํ‘ธ์‹œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ตœ์‹  ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, [`~PreTrainedModel.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 8๋น„ํŠธ ๋ชจ๋ธ์„ Hub์— ํ‘ธ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–‘์žํ™” config.json ํŒŒ์ผ์ด ๋จผ์ € ํ‘ธ์‹œ๋˜๊ณ , ๊ทธ ๋‹ค์Œ ์–‘์žํ™”๋œ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๊ฐ€ ํ‘ธ์‹œ๋ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_8bit=True) + +model = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-560m", + quantization_config=quantization_config +) +tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-560m") + +model.push_to_hub("bloom-560m-8bit") +``` + + + + +๋ชจ๋ธ์„ 4๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด 4๋ฐฐ ์ค„์–ด๋“ค๋ฉฐ, ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•˜๋ ค๋ฉด `device_map="auto"`๋ฅผ ์„ค์ •ํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) + +model_4bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + quantization_config=quantization_config +) +``` + +๊ธฐ๋ณธ์ ์œผ๋กœ `torch.nn.LayerNorm`๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์€ `torch.float16`์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์›ํ•œ๋‹ค๋ฉด `torch_dtype` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ด๋“ค ๋ชจ๋“ˆ์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +import torch +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True) + +model_4bit = AutoModelForCausalLM.from_pretrained( + "facebook/opt-350m", + quantization_config=quantization_config, + torch_dtype=torch.float32 +) +model_4bit.model.decoder.layers[-1].final_layer_norm.weight.dtype +``` + +`bitsandbytes>=0.41.3`์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ 4๋น„ํŠธ ๋ชจ๋ธ์„ ์ง๋ ฌํ™”ํ•˜๊ณ  Hugging Face Hub์— ํ‘ธ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ 4๋น„ํŠธ ์ •๋ฐ€๋„๋กœ ๊ฐ€์ ธ์˜จ ํ›„ `model.push_to_hub()`๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ `model.save_pretrained()` ๋ช…๋ น์–ด๋กœ ๋กœ์ปฌ์— ์ง๋ ฌํ™”๋œ 4๋น„ํŠธ ๋ชจ๋ธ์„ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + + + + + +8๋น„ํŠธ ๋ฐ 4๋น„ํŠธ ๊ฐ€์ค‘์น˜๋กœ ํ›ˆ๋ จํ•˜๋Š” ๊ฒƒ์€ *์ถ”๊ฐ€* ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ๋งŒ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + + + +๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ํ™•์ธํ•˜๋ ค๋ฉด `get_memory_footprint`๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +print(model.get_memory_footprint()) +``` + +์–‘์žํ™”๋œ ๋ชจ๋ธ์€ [`~PreTrainedModel.from_pretrained`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `load_in_8bit` ๋˜๋Š” `load_in_4bit` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer + +model = AutoModelForCausalLM.from_pretrained("{your_username}/bloom-560m-8bit", device_map="auto") +``` + +## 8๋น„ํŠธ (LLM.int8() ์•Œ๊ณ ๋ฆฌ์ฆ˜)[[8-bit-(llm.int8()-algorithm)]] + + + +8๋น„ํŠธ ์–‘์žํ™”์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด [๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/hf-bitsandbytes-integration)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”! + + + +์ด ์„น์…˜์—์„œ๋Š” ์˜คํ”„๋กœ๋”ฉ, ์ด์ƒ์น˜ ์ž„๊ณ—๊ฐ’, ๋ชจ๋“ˆ ๋ณ€ํ™˜ ๊ฑด๋„ˆ๋›ฐ๊ธฐ ๋ฐ ๋ฏธ์„ธ ์กฐ์ •๊ณผ ๊ฐ™์€ 8๋น„ํŠธ ๋ชจ๋ธ์˜ ํŠน์ • ๊ธฐ๋Šฅ์„ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. + +### ์˜คํ”„๋กœ๋”ฉ [[offloading]] + +8๋น„ํŠธ ๋ชจ๋ธ์€ CPU์™€ GPU ๊ฐ„์— ๊ฐ€์ค‘์น˜๋ฅผ ์˜คํ”„๋กœ๋“œํ•˜์—ฌ ๋งค์šฐ ํฐ ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์žฅ์ฐฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CPU๋กœ ์ „์†ก๋œ ๊ฐ€์ค‘์น˜๋Š” ์‹ค์ œ๋กœ **float32**๋กœ ์ €์žฅ๋˜๋ฉฐ 8๋น„ํŠธ๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [bigscience/bloom-1b7](https://huggingface.co/bigscience/bloom-1b7) ๋ชจ๋ธ์˜ ์˜คํ”„๋กœ๋“œ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด [`BitsAndBytesConfig`]๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์„ธ์š”: + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload=True) +``` + +CPU์— ์ „๋‹ฌํ•  `lm_head`๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฒƒ์„ GPU์— ์ ์žฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์šฉ์ž ์ •์˜ ๋””๋ฐ”์ด์Šค ๋งต์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค: + +```py +device_map = { + "transformer.word_embeddings": 0, + "transformer.word_embeddings_layernorm": 0, + "lm_head": "cpu", + "transformer.h": 0, + "transformer.ln_f": 0, +} +``` + +์ด์ œ ์‚ฌ์šฉ์ž ์ •์˜ `device_map`๊ณผ `quantization_config`์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +model_8bit = AutoModelForCausalLM.from_pretrained( + "bigscience/bloom-1b7", + device_map=device_map, + quantization_config=quantization_config, +) +``` + +### ์ด์ƒ์น˜ ์ž„๊ณ—๊ฐ’[[outlier-threshold]] + +"์ด์ƒ์น˜"๋Š” ํŠน์ • ์ž„๊ณ—๊ฐ’์„ ์ดˆ๊ณผํ•˜๋Š” ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ๊ฐ’์€ fp16์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๊ฐ’์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ •๊ทœ ๋ถ„ํฌ ([-3.5, 3.5])๋ฅผ ๋”ฐ๋ฅด์ง€๋งŒ, ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ์ด ๋ถ„ํฌ๋Š” ๋งค์šฐ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ([-60, 6] ๋˜๋Š” [6, 60]). 8๋น„ํŠธ ์–‘์žํ™”๋Š” ~5 ์ •๋„์˜ ๊ฐ’์—์„œ ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ, ๊ทธ ์ด์ƒ์—์„œ๋Š” ์ƒ๋‹นํ•œ ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ข‹์€ ๊ธฐ๋ณธ ์ž„๊ณ—๊ฐ’ ๊ฐ’์€ 6์ด์ง€๋งŒ, ๋” ๋ถˆ์•ˆ์ •ํ•œ ๋ชจ๋ธ (์†Œํ˜• ๋ชจ๋ธ ๋˜๋Š” ๋ฏธ์„ธ ์กฐ์ •)์—๋Š” ๋” ๋‚ฎ์€ ์ž„๊ณ—๊ฐ’์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋ชจ๋ธ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์ž„๊ณ—๊ฐ’์„ ์ฐพ์œผ๋ ค๋ฉด [`BitsAndBytesConfig`]์—์„œ `llm_int8_threshold` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‹คํ—˜ํ•ด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_threshold=10, +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + device_map=device_map, + quantization_config=quantization_config, +) +``` + +### ๋ชจ๋“ˆ ๋ณ€ํ™˜ ๊ฑด๋„ˆ๋›ฐ๊ธฐ[[skip-module-conversion]] + +[Jukebox](model_doc/jukebox)์™€ ๊ฐ™์€ ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋ชจ๋“  ๋ชจ๋“ˆ์„ 8๋น„ํŠธ๋กœ ์–‘์žํ™”ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ, ์ด๋Š” ์‹ค์ œ๋กœ ๋ถˆ์•ˆ์ •์„ฑ์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Jukebox์˜ ๊ฒฝ์šฐ, [`BitsAndBytesConfig`]์˜ `llm_int8_skip_modules` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ `lm_head` ๋ชจ๋“ˆ์„ ๊ฑด๋„ˆ๋›ฐ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + +model_id = "bigscience/bloom-1b7" + +quantization_config = BitsAndBytesConfig( + llm_int8_skip_modules=["lm_head"], +) + +model_8bit = AutoModelForCausalLM.from_pretrained( + model_id, + device_map="auto", + quantization_config=quantization_config, +) +``` + +### ๋ฏธ์„ธ ์กฐ์ •[[finetuning]] + +[PEFT](https://github.com/huggingface/peft) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด [flan-t5-large](https://huggingface.co/google/flan-t5-large) ๋ฐ [facebook/opt-6.7b](https://huggingface.co/facebook/opt-6.7b)์™€ ๊ฐ™์€ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ 8๋น„ํŠธ ์–‘์žํ™”๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์‹œ `device_map` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ, ๋ชจ๋ธ์„ ์ž๋™์œผ๋กœ GPU์— ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์›ํ•˜๋Š” ๊ฒฝ์šฐ `device_map` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์žฅ์น˜ ๋งต์„ ์‚ฌ์šฉ์ž ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (`device_map="auto"`๋Š” ์ถ”๋ก ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). + +## 4๋น„ํŠธ (QLoRA ์•Œ๊ณ ๋ฆฌ์ฆ˜)[[4-bit-(qlora-algorithm)]] + + + +์ด [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1ge2F1QSK8Q7h0hn3YKuBCOAS0bK8E0wf)์—์„œ 4๋น„ํŠธ ์–‘์žํ™”๋ฅผ ์‹œ๋„ํ•ด๋ณด๊ณ  ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด [๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ](https://huggingface.co/blog/4bit-transformers-bitsandbytes)์—์„œ ํ™•์ธํ•˜์„ธ์š”. + + + +์ด ์„น์…˜์—์„œ๋Š” ๊ณ„์‚ฐ ๋ฐ์ดํ„ฐ ์œ ํ˜• ๋ณ€๊ฒฝ, Normal Float 4 (NF4) ๋ฐ์ดํ„ฐ ์œ ํ˜• ์‚ฌ์šฉ, ์ค‘์ฒฉ ์–‘์žํ™” ์‚ฌ์šฉ๊ณผ ๊ฐ™์€ 4๋น„ํŠธ ๋ชจ๋ธ์˜ ํŠน์ • ๊ธฐ๋Šฅ ์ผ๋ถ€๋ฅผ ํƒ๊ตฌํ•ฉ๋‹ˆ๋‹ค. + + +### ๋ฐ์ดํ„ฐ ์œ ํ˜• ๊ณ„์‚ฐ[[compute-data-type]] + +๊ณ„์‚ฐ ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด [`BitsAndBytesConfig`]์—์„œ `bnb_4bit_compute_dtype` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ float32(๊ธฐ๋ณธ๊ฐ’)์—์„œ bf16์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +import torch +from transformers import BitsAndBytesConfig + +quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16) +``` + +### Normal Float 4 (NF4)[[normal-float-4-(nf4)]] + +NF4๋Š” [QLoRA](https://hf.co/papers/2305.14314) ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋œ 4๋น„ํŠธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ, ์ •๊ทœ ๋ถ„ํฌ์—์„œ ์ดˆ๊ธฐํ™”๋œ ๊ฐ€์ค‘์น˜์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. 4๋น„ํŠธ ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ๋•Œ NF4๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” [`BitsAndBytesConfig`]์—์„œ `bnb_4bit_quant_type` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +from transformers import BitsAndBytesConfig + +nf4_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", +) + +model_nf4 = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=nf4_config) +``` + +์ถ”๋ก ์˜ ๊ฒฝ์šฐ, `bnb_4bit_quant_type`์€ ์„ฑ๋Šฅ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜์™€ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด `bnb_4bit_compute_dtype` ๋ฐ `torch_dtype` ๊ฐ’์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +### ์ค‘์ฒฉ ์–‘์žํ™”[[nested-quantization]] + +์ค‘์ฒฉ ์–‘์žํ™”๋Š” ์ถ”๊ฐ€์ ์ธ ์„ฑ๋Šฅ ์†์‹ค ์—†์ด ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ์ด๋ฏธ ์–‘์žํ™”๋œ ๊ฐ€์ค‘์น˜์˜ 2์ฐจ ์–‘์žํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋‹น ์ถ”๊ฐ€๋กœ 0.4๋น„ํŠธ๋ฅผ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ค‘์ฒฉ ์–‘์žํ™”๋ฅผ ํ†ตํ•ด 16GB NVIDIA T4 GPU์—์„œ ์‹œํ€€์Šค ๊ธธ์ด 1024, ๋ฐฐ์น˜ ํฌ๊ธฐ 1, ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  4๋‹จ๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ [Llama-13b](https://huggingface.co/meta-llama/Llama-2-13b) ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import BitsAndBytesConfig + +double_quant_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_use_double_quant=True, +) + +model_double_quant = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b", quantization_config=double_quant_config) +``` + +## `bitsandbytes` ๋ชจ๋ธ์˜ ๋น„์–‘์žํ™”[[dequantizing-`bitsandbytes`-models]] +์–‘์žํ™”๋œ ํ›„์—๋Š” ๋ชจ๋ธ์„ ์›๋ž˜์˜ ์ •๋ฐ€๋„๋กœ ๋น„์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋Š” ๋ชจ๋ธ์˜ ํ’ˆ์งˆ์ด ์•ฝ๊ฐ„ ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„์–‘์žํ™”๋œ ๋ชจ๋ธ์— ๋งž์ถœ ์ˆ˜ ์žˆ๋Š” ์ถฉ๋ถ„ํ•œ GPU RAM์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```python +from transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer + +model_id = "facebook/opt-125m" + +model = AutoModelForCausalLM.from_pretrained(model_id, BitsAndBytesConfig(load_in_4bit=True)) +tokenizer = AutoTokenizer.from_pretrained(model_id) + +model.dequantize() + +text = tokenizer("Hello my name is", return_tensors="pt").to(0) + +out = model.generate(**text) +print(tokenizer.decode(out[0])) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/eetq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/eetq.md new file mode 100644 index 0000000000000000000000000000000000000000..ef4f4a2684b9f00dd0bd82e1bb75cdf3639ed550 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/eetq.md @@ -0,0 +1,47 @@ + + +# EETQ [[eetq]] + +[EETQ](https://github.com/NetEase-FuXi/EETQ) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” NVIDIA GPU์— ๋Œ€ํ•ด int8 ์ฑ„๋„๋ณ„(per-channel) ๊ฐ€์ค‘์น˜ ์ „์šฉ ์–‘์žํ™”(weight-only quantization)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ณ ์„ฑ๋Šฅ GEMM ๋ฐ GEMV ์ปค๋„์€ FasterTransformer ๋ฐ TensorRT-LLM์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ๊ต์ •(calibration) ๋ฐ์ดํ„ฐ์…‹์ด ํ•„์š” ์—†์œผ๋ฉฐ, ๋ชจ๋ธ์„ ์‚ฌ์ „์— ์–‘์žํ™”ํ•  ํ•„์š”๋„ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฑ„๋„๋ณ„ ์–‘์žํ™”(per-channel quantization) ๋•๋ถ„์— ์ •ํ™•๋„ ์ €ํ•˜๊ฐ€ ๋ฏธ๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +[๋ฆด๋ฆฌ์Šค ํŽ˜์ด์ง€](https://github.com/NetEase-FuXi/EETQ/releases)์—์„œ eetq๋ฅผ ์„ค์น˜ํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. +``` +pip install --no-cache-dir https://github.com/NetEase-FuXi/EETQ/releases/download/v1.0.0/EETQ-1.0.0+cu121+torch2.1.2-cp310-cp310-linux_x86_64.whl +``` +๋˜๋Š” ์†Œ์Šค ์ฝ”๋“œ https://github.com/NetEase-FuXi/EETQ ์—์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EETQ๋Š” CUDA ๊ธฐ๋Šฅ์ด 8.9 ์ดํ•˜์ด๊ณ  7.0 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +``` +git clone https://github.com/NetEase-FuXi/EETQ.git +cd EETQ/ +git submodule update --init --recursive +pip install . +``` + +๋น„์–‘์žํ™” ๋ชจ๋ธ์€ "from_pretrained"๋ฅผ ํ†ตํ•ด ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +```py +from transformers import AutoModelForCausalLM, EetqConfig +path = "/path/to/model". +quantization_config = EetqConfig("int8") +model = AutoModelForCausalLM.from_pretrained(path, device_map="auto", quantization_config=quantization_config) +``` + +์–‘์žํ™”๋œ ๋ชจ๋ธ์€ "save_pretrained"๋ฅผ ํ†ตํ•ด ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, "from_pretrained"๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +quant_path = "/path/to/save/quantized/model" +model.save_pretrained(quant_path) +model = AutoModelForCausalLM.from_pretrained(quant_path, device_map="auto") +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/gptq.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/gptq.md new file mode 100644 index 0000000000000000000000000000000000000000..c54f09c94a33036400b8992bab8fb34fb2a657c5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/gptq.md @@ -0,0 +1,120 @@ + + +# GPTQ [[gptq]] + + + +PEFT๋ฅผ ํ™œ์šฉํ•œ GPTQ ์–‘์žํ™”๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์‹œ๋ ค๋ฉด ์ด [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/1_TIrmuKOFhuRRiTWN94iLKUFu6ZX4ceb)์„ ์ฐธ๊ณ ํ•˜์‹œ๊ณ , ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด [๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ](https://huggingface.co/blog/gptq-integration)์—์„œ ํ™•์ธํ•˜์„ธ์š”! + + + +[AutoGPTQ](https://github.com/PanQiWei/AutoGPTQ) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” GPTQ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ›ˆ๋ จ ํ›„ ์–‘์žํ™” ๊ธฐ๋ฒ•์œผ๋กœ, ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ๊ฐ ํ–‰์„ ๋…๋ฆฝ์ ์œผ๋กœ ์–‘์žํ™”ํ•˜์—ฌ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€์ค‘์น˜ ๋ฒ„์ „์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ค‘์น˜๋Š” int4๋กœ ์–‘์žํ™”๋˜์ง€๋งŒ, ์ถ”๋ก  ์ค‘์—๋Š” ์‹ค์‹œ๊ฐ„์œผ๋กœ fp16์œผ๋กœ ๋ณต์›๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” int4 ๊ฐ€์ค‘์น˜๊ฐ€ GPU์˜ ์ „์—ญ ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์‹  ๊ฒฐํ•ฉ๋œ ์ปค๋„์—์„œ ์—ญ์–‘์žํ™”๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ 4๋ฐฐ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋” ๋‚ฎ์€ ๋น„ํŠธ ๋„ˆ๋น„๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ํ†ต์‹  ์‹œ๊ฐ„์ด ์ค„์–ด๋“ค์–ด ์ถ”๋ก  ์†๋„๊ฐ€ ๋นจ๋ผ์งˆ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install auto-gptq +pip install --upgrade accelerate optimum transformers +``` + +๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋ ค๋ฉด(ํ˜„์žฌ ํ…์ŠคํŠธ ๋ชจ๋ธ๋งŒ ์ง€์›๋จ) [`GPTQConfig`] ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์–‘์žํ™”ํ•  ๋น„ํŠธ ์ˆ˜, ์–‘์žํ™”๋ฅผ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ ๊ต์ • ๋ฐ์ดํ„ฐ์…‹, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ์…‹์„ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig + +model_id = "facebook/opt-125m" +tokenizer = AutoTokenizer.from_pretrained(model_id) +gptq_config = GPTQConfig(bits=4, dataset="c4", tokenizer=tokenizer) +``` + +์ž์‹ ์˜ ๋ฐ์ดํ„ฐ์…‹์„ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, GPTQ ๋…ผ๋ฌธ์—์„œ ์‚ฌ์šฉํ•œ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +```py +dataset = ["auto-gptq is an easy-to-use model quantization library with user-friendly apis, based on GPTQ algorithm."] +gptq_config = GPTQConfig(bits=4, dataset=dataset, tokenizer=tokenizer) +``` + +์–‘์žํ™”ํ•  ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  `gptq_config`์„ [`~AutoModelForCausalLM.from_pretrained`] ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌํ•˜์„ธ์š”. ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด `device_map="auto"`๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋ธ์„ ์ž๋™์œผ๋กœ CPU๋กœ ์˜คํ”„๋กœ๋“œํ•˜๊ณ , ์–‘์žํ™”๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ ๋ชจ๋“ˆ์ด CPU์™€ GPU ๊ฐ„์— ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. + +```py +quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", quantization_config=gptq_config) +``` + +๋ฐ์ดํ„ฐ์…‹์ด ๋„ˆ๋ฌด ์ปค์„œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•œ ๋””์Šคํฌ ์˜คํ”„๋กœ๋“œ๋Š” ํ˜„์žฌ ์ง€์›ํ•˜์ง€ ์•Š๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” `max_memory` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋””๋ฐ”์ด์Šค(GPU ๋ฐ CPU)์—์„œ ์‚ฌ์šฉํ•  ๋ฉ”๋ชจ๋ฆฌ ์–‘์„ ํ• ๋‹นํ•ด ๋ณด์„ธ์š”: + +```py +quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", max_memory={0: "30GiB", 1: "46GiB", "cpu": "30GiB"}, quantization_config=gptq_config) +``` + + + +ํ•˜๋“œ์›จ์–ด์™€ ๋ชจ๋ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Ÿ‰์— ๋”ฐ๋ผ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์–‘์žํ™”ํ•˜๋Š” ๋ฐ ๋“œ๋Š” ์‹œ๊ฐ„์ด ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌด๋ฃŒ ๋“ฑ๊ธ‰์˜ Google Colab GPU๋กœ ๋น„๊ต์  ๊ฐ€๋ฒผ์šด [facebook/opt-350m](https://huggingface.co/facebook/opt-350m) ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๋ฐ ์•ฝ 5๋ถ„์ด ๊ฑธ๋ฆฌ์ง€๋งŒ, NVIDIA A100์œผ๋กœ 175B์— ๋‹ฌํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๋Š” ๋ฐ๋Š” ์•ฝ 4์‹œ๊ฐ„์— ๋‹ฌํ•˜๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•˜๊ธฐ ์ „์—, Hub์—์„œ ํ•ด๋‹น ๋ชจ๋ธ์˜ GPTQ ์–‘์žํ™” ๋ฒ„์ „์ด ์ด๋ฏธ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +๋ชจ๋ธ์ด ์–‘์žํ™”๋˜๋ฉด, ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ Hub์— ํ‘ธ์‹œํ•˜์—ฌ ์‰ฝ๊ฒŒ ๊ณต์œ ํ•˜๊ณ  ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`GPTQConfig`]๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด [`~PreTrainedModel.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +quantized_model.push_to_hub("opt-125m-gptq") +tokenizer.push_to_hub("opt-125m-gptq") +``` + +์–‘์žํ™”๋œ ๋ชจ๋ธ์„ ๋กœ์ปฌ์— ์ €์žฅํ•˜๋ ค๋ฉด [`~PreTrainedModel.save_pretrained`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด `device_map` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์–‘์žํ™”๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์ €์žฅํ•˜๊ธฐ ์ „์— ์ „์ฒด ๋ชจ๋ธ์„ GPU๋‚˜ CPU๋กœ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ CPU์— ์ €์žฅํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ฉ๋‹ˆ๋‹ค: + +```py +quantized_model.save_pretrained("opt-125m-gptq") +tokenizer.save_pretrained("opt-125m-gptq") + +# device_map์ด ์„ค์ •๋œ ์ƒํƒœ์—์„œ ์–‘์žํ™”๋œ ๊ฒฝ์šฐ +quantized_model.to("cpu") +quantized_model.save_pretrained("opt-125m-gptq") +``` + +์–‘์žํ™”๋œ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๋กœ๋“œํ•˜๋ ค๋ฉด [`~PreTrainedModel.from_pretrained`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , `device_map="auto"`๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋“  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU์— ๋ชจ๋ธ์„ ์ž๋™์œผ๋กœ ๋ถ„์‚ฐ์‹œ์ผœ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ชจ๋ธ์„ ๋” ๋น ๋ฅด๊ฒŒ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto") +``` + +## ExLlama [[exllama]] + +[ExLlama](https://github.com/turboderp/exllama)์€ [Llama](model_doc/llama) ๋ชจ๋ธ์˜ Python/C++/CUDA ๊ตฌํ˜„์ฒด๋กœ, 4๋น„ํŠธ GPTQ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋น ๋ฅธ ์ถ”๋ก ์„ ์œ„ํ•ด ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค(์ด [๋ฒค์น˜๋งˆํฌ](https://github.com/huggingface/optimum/tree/main/tests/benchmark#gptq-benchmark)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”). ['GPTQConfig'] ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ExLlama ์ปค๋„์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์ถ”๋ก  ์†๋„๋ฅผ ๋”์šฑ ๋†’์ด๊ธฐ ์œ„ํ•ด, `exllama_config` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ [ExLlamaV2](https://github.com/turboderp/exllamav2) ์ปค๋„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +import torch +from transformers import AutoModelForCausalLM, GPTQConfig + +gptq_config = GPTQConfig(bits=4, exllama_config={"version":2}) +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="auto", quantization_config=gptq_config) +``` + + + +4๋น„ํŠธ ๋ชจ๋ธ๋งŒ ์ง€์›๋˜๋ฉฐ, ์–‘์žํ™”๋œ ๋ชจ๋ธ์„ PEFT๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ ExLlama ์ปค๋„์„ ๋น„ํ™œ์„ฑํ™”ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + + + +ExLlama ์ปค๋„์€ ์ „์ฒด ๋ชจ๋ธ์ด GPU์— ์žˆ์„ ๋•Œ๋งŒ ์ง€์›๋ฉ๋‹ˆ๋‹ค. AutoGPTQ(๋ฒ„์ „ 0.4.2 ์ด์ƒ)๋กœ CPU์—์„œ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ExLlama ์ปค๋„์„ ๋น„ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด config.json ํŒŒ์ผ์˜ ์–‘์žํ™” ์„ค์ •์—์„œ ExLlama ์ปค๋„๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ์„ ๋ฎ์–ด์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +import torch +from transformers import AutoModelForCausalLM, GPTQConfig +gptq_config = GPTQConfig(bits=4, use_exllama=False) +model = AutoModelForCausalLM.from_pretrained("{your_username}/opt-125m-gptq", device_map="cpu", quantization_config=gptq_config) +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quanto.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quanto.md new file mode 100644 index 0000000000000000000000000000000000000000..7eff695051d6b8d14fed33672f337ace703d0c71 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quanto.md @@ -0,0 +1,67 @@ + + +# Quanto[[quanto]] + + + +์ด [๋…ธํŠธ๋ถ](https://colab.research.google.com/drive/16CXfVmtdQvciSh9BopZUDYcmXCDpvgrT?usp=sharing)์œผ๋กœ Quanto์™€ transformers๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์„ธ์š”! + + + + +[๐Ÿค— Quanto](https://github.com/huggingface/optimum-quanto) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค๋ชฉ์  ํŒŒ์ดํ† ์น˜ ์–‘์žํ™” ํˆดํ‚ท์ž…๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–‘์žํ™” ๋ฐฉ๋ฒ•์€ ์„ ํ˜• ์–‘์žํ™”์ž…๋‹ˆ๋‹ค. Quanto๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: + +- ๊ฐ€์ค‘์น˜ ์–‘์žํ™” (`float8`,`int8`,`int4`,`int2`) +- ํ™œ์„ฑํ™” ์–‘์žํ™” (`float8`,`int8`) +- ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š์Œ (e.g CV,LLM) +- ์žฅ์น˜์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š์Œ (e.g CUDA,MPS,CPU) +- `torch.compile` ํ˜ธํ™˜์„ฑ +- ํŠน์ • ์žฅ์น˜์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ์ปค๋„์˜ ์‰ฌ์šด ์ถ”๊ฐ€ +- QAT(์–‘์žํ™”๋ฅผ ๊ณ ๋ คํ•œ ํ•™์Šต) ์ง€์› + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install quanto accelerate transformers +``` + +์ด์ œ [`~PreTrainedModel.from_pretrained`] ๋ฉ”์†Œ๋“œ์— [`QuantoConfig`] ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๋ชจ๋ธ์„ ์–‘์žํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ `torch.nn.Linear` ๋ ˆ์ด์–ด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์˜ ๋ชจ๋“  ๋ชจ๋ธ์—์„œ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. + +ํ—ˆ๊น…ํŽ˜์ด์Šค์˜ transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ฐœ๋ฐœ์ž ํŽธ์˜๋ฅผ ์œ„ํ•ด quanto์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ผ๋ถ€ ํ†ตํ•ฉํ•˜์—ฌ ์ง€์›ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด ๋ฐฉ์‹์œผ๋กœ๋Š” ๊ฐ€์ค‘์น˜ ์–‘์žํ™”๋งŒ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ์–‘์žํ™”, ์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜, QAT ๊ฐ™์€ ๋” ๋ณต์žกํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” [quanto](https://github.com/huggingface/optimum-quanto) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfig + +model_id = "facebook/opt-125m" +tokenizer = AutoTokenizer.from_pretrained(model_id) +quantization_config = QuantoConfig(weights="int8") +quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0", quantization_config=quantization_config) +``` + +์ฐธ๊ณ ๋กœ, transformers์—์„œ๋Š” ์•„์ง ์ง๋ ฌํ™”๊ฐ€ ์ง€์›๋˜์ง€ ์•Š์ง€๋งŒ ๊ณง ์ง€์›๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค! +๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์œผ๋ฉด quanto ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +Quanto ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์–‘์žํ™”๋ฅผ ์œ„ํ•ด ์„ ํ˜• ์–‘์žํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋น„๋ก ๊ธฐ๋ณธ์ ์ธ ์–‘์žํ™” ๊ธฐ์ˆ ์ด์ง€๋งŒ, ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋ฐ ์•„์ฃผ ํฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค! ๋ฐ”๋กœ ์•„๋ž˜์— ์žˆ๋Š” ๋ฒค์น˜๋งˆํฌ(llama-2-7b์˜ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ ์ง€ํ‘œ)๋ฅผ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ๋” ๋งŽ์€ ๋ฒค์น˜๋งˆํฌ๋Š” [์—ฌ๊ธฐ](https://github.com/huggingface/quanto/tree/main/bench/generation) ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +
+
+ llama-2-7b-quanto-perplexity +
+
+ +์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„์˜ PTQ ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ˜ธํ™˜๋  ๋งŒํผ ์ถฉ๋ถ„ํžˆ ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ๊ณ„ํš์€ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜(AWQ, Smoothquant)์„ ์ตœ๋Œ€ํ•œ ๋งค๋„๋Ÿฝ๊ฒŒ ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quark.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quark.md new file mode 100644 index 0000000000000000000000000000000000000000..eb1d18bf0f90a61d89e6431ba77d22c5a6b02091 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quantization/quark.md @@ -0,0 +1,85 @@ + + +# Quark[[quark]] + +[Quark](https://quark.docs.amd.com/latest/)๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ ํƒ€์ž…, ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ•˜๋“œ์›จ์–ด์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๋„๋ก ์„ค๊ณ„๋œ ๋”ฅ๋Ÿฌ๋‹ ์–‘์žํ™” ํˆดํ‚ท์ž…๋‹ˆ๋‹ค. Quark์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ์ „๋žต, ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Transformers๋ฅผ ํ†ตํ•ด ํ†ตํ•ฉ๋œ PyTorch ์ง€์›์€ ์ฃผ๋กœ AMD CPU ๋ฐ GPU๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋ฉฐ, ์ฃผ๋กœ ํ‰๊ฐ€ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness)๋ฅผ ๐Ÿค— Transformers ๋ฐฑ์—”๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ Quark๋กœ ์–‘์žํ™”๋œ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์„ ์›ํ™œํ•˜๊ฒŒ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +Quark์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ์‚ฌ์šฉ์ž๋Š” [๋ฌธ์„œ](https://quark.docs.amd.com/latest/)๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋ชจ๋ธ ์–‘์žํ™”๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ์ง€์›๋˜๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +Quark๋Š” ์ž์ฒด ์ฒดํฌํฌ์ธํŠธ/[์„ค์ • ํฌ๋งท](https://huggingface.co/amd/Llama-3.1-8B-Instruct-FP8-KV-Quark-test/blob/main/config.json#L26)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ์–‘์žํ™”/๋Ÿฐํƒ€์ž„ ๊ตฌํ˜„์ฒด ([AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq), [๋„ค์ดํ‹ฐ๋ธŒ fp8](https://huggingface.co/docs/transformers/quantization/finegrained_fp8))์™€ ํ˜ธํ™˜๋˜๋Š” ์ง๋ ฌํ™” ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. + +Transformer์—์„œ Quark ์–‘์žํ™” ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋ ค๋ฉด ๋จผ์ € ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install amd-quark +``` + +## ์ง€์› ๋งคํŠธ๋ฆญ์Šค[[Support matrix]] + +Quark๋ฅผ ํ†ตํ•ด ์–‘์žํ™”๋œ ๋ชจ๋ธ์€ ํ•จ๊ป˜ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์„ฑ์— ๊ด€๊ณ„์—†์ด ๋ชจ๋“  ์–‘์žํ™”๋œ ๋ชจ๋ธ์€ `PretrainedModel.from_pretrained`๋ฅผ ํ†ตํ•ด ์›ํ™œํ•˜๊ฒŒ ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์•„๋ž˜ ํ‘œ๋Š” Quark์—์„œ ์ง€์›ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค: + +| **๊ธฐ๋Šฅ** | **Quark์—์„œ ์ง€์›ํ•˜๋Š” ํ•ญ๋ชฉ** | | +|---------------------------------|-----------------------------------------------------------------------------------------------------------|---| +| ๋ฐ์ดํ„ฐ ํƒ€์ž… | int8, int4, int2, bfloat16, float16, fp8_e5m2, fp8_e4m3, fp6_e3m2, fp6_e2m3, fp4, OCP MX, MX6, MX9, bfp16 | | +| ์–‘์žํ™” ์ „ ๋ชจ๋ธ ๋ณ€ํ™˜ | SmoothQuant, QuaRot, SpinQuant, AWQ | | +| ์–‘์žํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ | GPTQ | | +| ์ง€์› ์—ฐ์‚ฐ์ž | ``nn.Linear``, ``nn.Conv2d``, ``nn.ConvTranspose2d``, ``nn.Embedding``, ``nn.EmbeddingBag`` | | +| ์„ธ๋ถ„์„ฑ(Granularity) | per-tensor, per-channel, per-block, per-layer, per-layer type | | +| KV ์บ์‹œ | fp8 | | +| ํ™œ์„ฑํ™” ์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜ | MinMax / Percentile / MSE | | +| ์–‘์žํ™” ์ „๋žต | weight-only, static, dynamic, with or without output quantization | | + +## Hugging Face Hub์˜ ๋ชจ๋ธ[[Models on Hugging Face Hub]] + +Quark ๋„ค์ดํ‹ฐ๋ธŒ ์ง๋ ฌํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณต๊ฐœ ๋ชจ๋ธ์€ https://huggingface.co/models?other=quark ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +Quark๋Š” [`quant_method="fp8"`์„ ์ด์šฉํ•˜๋Š” ๋ชจ๋ธ](https://huggingface.co/models?other=fp8)๊ณผ [`quant_method="awq"`์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ](https://huggingface.co/models?other=awq)๋„ ์ง€์›ํ•˜์ง€๋งŒ, Transformers๋Š” ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์„ [AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq)๋ฅผ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ +[๐Ÿค— Transformers์˜ ๋„ค์ดํ‹ฐ๋ธŒ fp8 ์ง€์›](https://huggingface.co/docs/transformers/quantization/finegrained_fp8)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## Transformers์—์„œ Quark๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ[[Using Quark models in Transformers]] + +๋‹ค์Œ์€ Transformers์—์„œ Quark ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer + +model_id = "EmbeddedLLM/Llama-3.1-8B-Instruct-w_fp8_per_channel_sym" +model = AutoModelForCausalLM.from_pretrained(model_id) +model = model.to("cuda") + +print(model.model.layers[0].self_attn.q_proj) +# QParamsLinear( +# (weight_quantizer): ScaledRealQuantizer() +# (input_quantizer): ScaledRealQuantizer() +# (output_quantizer): ScaledRealQuantizer() +# ) + +tokenizer = AutoTokenizer.from_pretrained(model_id) +inp = tokenizer("Where is a good place to cycle around Tokyo?", return_tensors="pt") +inp = inp.to("cuda") + +res = model.generate(**inp, min_new_tokens=50, max_new_tokens=100) + +print(tokenizer.batch_decode(res)[0]) +# <|begin_of_text|>Where is a good place to cycle around Tokyo? There are several places in Tokyo that are suitable for cycling, depending on your skill level and interests. Here are a few suggestions: +# 1. Yoyogi Park: This park is a popular spot for cycling and has a wide, flat path that's perfect for beginners. You can also visit the Meiji Shrine, a famous Shinto shrine located in the park. +# 2. Imperial Palace East Garden: This beautiful garden has a large, flat path that's perfect for cycling. You can also visit the +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quicktour.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quicktour.md new file mode 100644 index 0000000000000000000000000000000000000000..4c3b137aa00ff9d5221258a4e788ccdfd4841fd6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/quicktour.md @@ -0,0 +1,557 @@ + + +# ๋‘˜๋Ÿฌ๋ณด๊ธฐ [[quick-tour]] + +[[open-in-colab]] + +๐Ÿค— Transformers๋ฅผ ์‹œ์ž‘ํ•ด๋ณด์„ธ์š”! ๊ฐœ๋ฐœํ•ด๋ณธ ์ ์ด ์—†๋”๋ผ๋„ ์‰ฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ์“ฐ์ธ ์ด ๊ธ€์€ [`pipeline`](./main_classes/pipelines)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก ํ•˜๊ณ , ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ์ „์ฒ˜๋ฆฌ๊ธฐ๋ฅผ [AutoClass](./model_doc/auto)๋กœ ๋กœ๋“œํ•˜๊ณ , PyTorch ๋˜๋Š” TensorFlow๋กœ ๋ชจ๋ธ์„ ๋น ๋ฅด๊ฒŒ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ด ๋“œ๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ณธ ๊ฐ€์ด๋“œ์—์„œ ์†Œ๊ฐœ๋˜๋Š” ๊ฐœ๋…์„ (ํŠนํžˆ ์ดˆ๋ณด์ž์˜ ๊ด€์ ์œผ๋กœ) ๋” ์นœ์ ˆํ•˜๊ฒŒ ์ ‘ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ํŠœํ† ๋ฆฌ์–ผ์ด๋‚˜ [์ฝ”์Šค](https://huggingface.co/course/chapter1/1)๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +!pip install transformers datasets evaluate accelerate +``` + +๋˜ํ•œ ์„ ํ˜ธํ•˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + + + + +```bash +pip install torch +``` + + + +```bash +pip install tensorflow +``` + + + +## ํŒŒ์ดํ”„๋ผ์ธ [[pipeline]] + + + +[`pipeline`](./main_classes/pipelines)์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ์ถ”๋ก ํ•˜๊ธฐ์— ๊ฐ€์žฅ ์‰ฝ๊ณ  ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. [`pipeline`]์€ ์—ฌ๋Ÿฌ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์—์„œ ๋‹ค์–‘ํ•œ ๊ณผ์—…์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ž˜ ํ‘œ์— ํ‘œ์‹œ๋œ ๋ช‡ ๊ฐ€์ง€ ๊ณผ์—…์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค: + + + +์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ž‘์—…์˜ ์ „์ฒด ๋ชฉ๋ก์€ [Pipelines API ์ฐธ์กฐ](./main_classes/pipelines)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + + + +| **ํƒœ์Šคํฌ** | **์„ค๋ช…** | **๋ชจ๋‹ฌ๋ฆฌํ‹ฐ** | **ํŒŒ์ดํ”„๋ผ์ธ ID** | +|-----------------|----------------------------------------------------------------------|------------------|-----------------------------------------------| +| ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ | ํ…์ŠคํŠธ์— ์•Œ๋งž์€ ๋ ˆ์ด๋ธ” ๋ถ™์ด๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="sentiment-analysis") | +| ํ…์ŠคํŠธ ์ƒ์„ฑ | ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ์ž…๋ ฅ๊ณผ ์ด์–ด์ง€๋Š” ํ…์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="text-generation") | +| ๊ฐœ์ฒด๋ช… ์ธ์‹ | ๋ฌธ์ž์—ด์˜ ๊ฐ ํ† ํฐ๋งˆ๋‹ค ์•Œ๋งž์€ ๋ ˆ์ด๋ธ” ๋ถ™์ด๊ธฐ (์ธ๋ฌผ, ์กฐ์ง, ์žฅ์†Œ ๋“ฑ๋“ฑ) | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="ner") | +| ์งˆ์˜์‘๋‹ต | ์ฃผ์–ด์ง„ ๋ฌธ๋งฅ๊ณผ ์งˆ๋ฌธ์— ๋”ฐ๋ผ ์˜ฌ๋ฐ”๋ฅธ ๋Œ€๋‹ตํ•˜๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="question-answering") | +| ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ | ๋ฌธ์ž์—ด์˜ ๋นˆ์นธ์— ์•Œ๋งž์€ ํ† ํฐ ๋งž์ถ”๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="fill-mask") | +| ์š”์•ฝ | ํ…์ŠคํŠธ๋‚˜ ๋ฌธ์„œ๋ฅผ ์š”์•ฝํ•˜๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="summarization") | +| ๋ฒˆ์—ญ | ํ…์ŠคํŠธ๋ฅผ ํ•œ ์–ธ์–ด์—์„œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ | ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP) | pipeline(task="translation") | +| ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ | ์ด๋ฏธ์ง€์— ์•Œ๋งž์€ ๋ ˆ์ด๋ธ” ๋ถ™์ด๊ธฐ | ์ปดํ“จํ„ฐ ๋น„์ „(CV) | pipeline(task="image-classification") | +| ์ด๋ฏธ์ง€ ๋ถ„ํ•  | ์ด๋ฏธ์ง€์˜ ํ”ฝ์…€๋งˆ๋‹ค ๋ ˆ์ด๋ธ” ๋ถ™์ด๊ธฐ(์‹œ๋งจํ‹ฑ, ํŒŒ๋†‰ํ‹ฑ ๋ฐ ์ธ์Šคํ„ด์Šค ๋ถ„ํ•  ํฌํ•จ) | ์ปดํ“จํ„ฐ ๋น„์ „(CV) | pipeline(task="image-segmentation") | +| ๊ฐ์ฒด ํƒ์ง€ | ์ด๋ฏธ์ง€ ์† ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„ ์ƒ์ž๋ฅผ ๊ทธ๋ฆฌ๊ณ  ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ | ์ปดํ“จํ„ฐ ๋น„์ „(CV) | pipeline(task="object-detection") | +| ์˜ค๋””์˜ค ๋ถ„๋ฅ˜ | ์˜ค๋””์˜ค ํŒŒ์ผ์— ์•Œ๋งž์€ ๋ ˆ์ด๋ธ” ๋ถ™์ด๊ธฐ | ์˜ค๋””์˜ค | pipeline(task="audio-classification") | +| ์ž๋™ ์Œ์„ฑ ์ธ์‹ | ์˜ค๋””์˜ค ํŒŒ์ผ ์† ์Œ์„ฑ์„ ํ…์ŠคํŠธ๋กœ ๋ฐ”๊พธ๊ธฐ | ์˜ค๋””์˜ค | pipeline(task="automatic-speech-recognition") | +| ์‹œ๊ฐ ์งˆ์˜์‘๋‹ต | ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์™€ ์งˆ๋ฌธ์— ๋Œ€ํ•ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋Œ€๋‹ตํ•˜๊ธฐ | ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ | pipeline(task="vqa") | +| ๋ฌธ์„œ ์งˆ์˜์‘๋‹ต | ์ฃผ์–ด์ง„ ๋ฌธ์„œ์™€ ์งˆ๋ฌธ์— ๋Œ€ํ•ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋Œ€๋‹ตํ•˜๊ธฐ | ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ | pipeline(task="document-question-answering") | +| ์ด๋ฏธ์ง€ ์บก์…˜ ๋‹ฌ๊ธฐ | ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์˜ ์บก์…˜ ์ƒ์„ฑํ•˜๊ธฐ | ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ | pipeline(task="image-to-text") | + +๋จผ์ € [`pipeline`]์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ž‘์—…์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๊ฐ์ • ๋ถ„์„์„ ์œ„ํ•ด [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("sentiment-analysis") +``` + +[`pipeline`]์€ ๊ฐ์ • ๋ถ„์„์„ ์œ„ํ•œ [์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ](https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english)๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ž๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ `classifier`๋ฅผ ๋Œ€์ƒ ํ…์ŠคํŠธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> classifier("We are very happy to show you the ๐Ÿค— Transformers library.") +[{'label': 'POSITIVE', 'score': 0.9998}] +``` + +๋งŒ์•ฝ ์ž…๋ ฅ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ, ์ž…๋ ฅ์„ ๋ฆฌ์ŠคํŠธ๋กœ [`pipeline`]์— ์ „๋‹ฌํ•˜์—ฌ, ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> results = classifier(["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."]) +>>> for result in results: +... print(f"label: {result['label']}, with score: {round(result['score'], 4)}") +label: POSITIVE, with score: 0.9998 +label: NEGATIVE, with score: 0.5309 +``` + +[`pipeline`]์€ ์ฃผ์–ด์ง„ ๊ณผ์—…์— ๊ด€๊ณ„์—†์ด ๋ฐ์ดํ„ฐ์…‹ ์ „๋ถ€๋ฅผ ์ˆœํšŒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ๊ณผ์—…์œผ๋กœ ์„ ํƒํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> import torch +>>> from transformers import pipeline + +>>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h") +``` + +๋ฐ์ดํ„ฐ์…‹์„ ๋กœ๋“œํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. (์ž์„ธํ•œ ๋‚ด์šฉ์€ ๐Ÿค— Datasets [์‹œ์ž‘ํ•˜๊ธฐ](https://huggingface.co/docs/datasets/quickstart#audio)์„ ์ฐธ์กฐํ•˜์„ธ์š”) ์—ฌ๊ธฐ์—์„œ๋Š” [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ๋ฐ์ดํ„ฐ์…‹์„ ๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT +``` + +๋ฐ์ดํ„ฐ์…‹์˜ ์ƒ˜ํ”Œ๋ง ๋ ˆ์ดํŠธ๊ฐ€ ๊ธฐ์กด ๋ชจ๋ธ์ธ [`facebook/wav2vec2-base-960h`](https://huggingface.co/facebook/wav2vec2-base-960h)์˜ ํ›ˆ๋ จ ๋‹น์‹œ ์ƒ˜ํ”Œ๋ง ๋ ˆ์ดํŠธ์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate)) +``` + +`"audio"` ์—ด์„ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ์ƒ˜ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฒซ 4๊ฐœ ์ƒ˜ํ”Œ์—์„œ ์›์‹œ ์›จ์ด๋ธŒํผ ๋ฐฐ์—ด์„ ์ถ”์ถœํ•˜๊ณ  ํŒŒ์ดํ”„๋ผ์ธ์— ๋ฆฌ์ŠคํŠธ๋กœ ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> result = speech_recognizer(dataset[:4]["audio"]) +>>> print([d["text"] for d in result]) +['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FONDERING HOW I'D SET UP A JOIN TO HELL T WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE APSO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AN I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I FURN A JOINA COUT'] +``` + +์Œ์„ฑ์ด๋‚˜ ๋น„์ „๊ณผ ๊ฐ™์ด ์ž…๋ ฅ์ด ํฐ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์…‹์˜ ๊ฒฝ์šฐ, ๋ชจ๋“  ์ž…๋ ฅ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ ๋Œ€์‹  ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Pipelines API ์ฐธ์กฐ](./main_classes/pipelines)๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +### ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ € ์‚ฌ์šฉํ•˜๊ธฐ [[use-another-model-and-tokenizer-in-the-pipeline]] + +[`pipeline`]์€ [Hub](https://huggingface.co/models)์˜ ๋ชจ๋“  ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, [`pipeline`]์„ ๋‹ค๋ฅธ ์šฉ๋„์— ๋งž๊ฒŒ ์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋ž‘์Šค์–ด ํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  Hub์˜ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ์ ˆํ•œ ๋ชจ๋ธ์„ ํ•„ํ„ฐ๋งํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋ง๋œ ๊ฒฐ๊ณผ์˜ ์ƒ์œ„ ํ•ญ๋ชฉ์œผ๋กœ๋Š” ํ”„๋ž‘์Šค์–ด ํ…์ŠคํŠธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๊ตญ์–ด [BERT ๋ชจ๋ธ](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment)์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค: + +```py +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +``` + + + +[`AutoModelForSequenceClassification`]๊ณผ [`AutoTokenizer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๊ณผ ๊ด€๋ จ๋œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜์„ธ์š” (๋‹ค์Œ ์„น์…˜์—์„œ [`AutoClass`]์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค): + +```py +>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification + +>>> model = AutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + +[`TFAutoModelForSequenceClassification`]๊ณผ [`AutoTokenizer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๊ณผ ๊ด€๋ จ๋œ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜์„ธ์š” (๋‹ค์Œ ์„น์…˜์—์„œ [`TFAutoClass`]์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค): + +```py +>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification + +>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + + + +[`pipeline`]์—์„œ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•˜๋ฉด, ์ด์ œ `classifier`๋ฅผ ํ”„๋ž‘์Šค์–ด ํ…์ŠคํŠธ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) +>>> classifier("Nous sommes trรจs heureux de vous prรฉsenter la bibliothรจque ๐Ÿค— Transformers.") +[{'label': '5 stars', 'score': 0.7273}] +``` + +๋งˆ๋•…ํ•œ ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ์„ธ์กฐ์ • ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๋ฏธ์„ธ์กฐ์ • ํŠœํ† ๋ฆฌ์–ผ](./training)์„ ์ฐธ์กฐํ•˜์„ธ์š”. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ์กฐ์ •ํ•œ ํ›„์—๋Š” ๋ชจ๋ธ์„ Hub์˜ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•˜์—ฌ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ฏผ์ฃผํ™”์— ๊ธฐ์—ฌํ•ด์ฃผ์„ธ์š”! ๐Ÿค— + +## AutoClass [[autoclass]] + + + +[`AutoModelForSequenceClassification`]๊ณผ [`AutoTokenizer`] ํด๋ž˜์Šค๋Š” ์œ„์—์„œ ๋‹ค๋ฃฌ [`pipeline`]์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. [AutoClass](./model_doc/auto)๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ด๋ฆ„์ด๋‚˜ ๊ฒฝ๋กœ์—์„œ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” '๋ฐ”๋กœ๊ฐ€๊ธฐ'์ž…๋‹ˆ๋‹ค. ๊ณผ์—…์— ์ ํ•ฉํ•œ `AutoClass`๋ฅผ ์„ ํƒํ•˜๊ณ  ํ•ด๋‹น ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +์ด์ „ ์„น์…˜์˜ ์˜ˆ์ œ๋กœ ๋Œ์•„๊ฐ€์„œ [`pipeline`]์˜ ๊ฒฐ๊ณผ๋ฅผ `AutoClass`๋ฅผ ํ™œ์šฉํ•ด ๋ณต์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +### AutoTokenizer [[autotokenizer]] + +ํ† ํฌ๋‚˜์ด์ €๋Š” ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ˆซ์ž ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™” ๊ณผ์ •์—๋Š” ๋‹จ์–ด๋ฅผ ์–ด๋””์—์„œ ๋Š์„์ง€, ์–ด๋А ์ˆ˜์ค€๊นŒ์ง€ ๋‚˜๋ˆŒ์ง€์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ทœ์น™๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค (ํ† ํฐํ™”์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ† ํฌ๋‚˜์ด์ € ์š”์•ฝ](./tokenizer_summary)์„ ์ฐธ์กฐํ•˜์„ธ์š”). ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ๋ชจ๋ธ์ด ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๊ณผ ๋™์ผํ•œ ํ† ํฐํ™” ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋™์ผํ•œ ๋ชจ๋ธ ์ด๋ฆ„์œผ๋กœ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +[`AutoTokenizer`]๋กœ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoTokenizer + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tokenizer = AutoTokenizer.from_pretrained(model_name) +``` + +ํ…์ŠคํŠธ๋ฅผ ํ† ํฌ๋‚˜์ด์ €์— ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> encoding = tokenizer("We are very happy to show you the ๐Ÿค— Transformers library.") +>>> print(encoding) +{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102], + 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} +``` + +ํ† ํฌ๋‚˜์ด์ €๋Š” ๋‹ค์Œ์„ ํฌํ•จํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +* [input_ids](./glossary#input-ids): ํ† ํฐ์˜ ์ˆซ์ž ํ‘œํ˜„. +* [attention_mask](.glossary#attention-mask): ์–ด๋–ค ํ† ํฐ์— ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + +ํ† ํฌ๋‚˜์ด์ €๋Š” ์ž…๋ ฅ์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ…์ŠคํŠธ๋ฅผ ํŒจ๋”ฉํ•˜๊ณ  ์ž˜๋ผ๋‚ด์–ด ์ผ์ •ํ•œ ๊ธธ์ด์˜ ๋ฌถ์Œ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + + + + +```py +>>> pt_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="pt", +... ) +``` + + + +```py +>>> tf_batch = tokenizer( +... ["We are very happy to show you the ๐Ÿค— Transformers library.", "We hope you don't hate it."], +... padding=True, +... truncation=True, +... max_length=512, +... return_tensors="tf", +... ) +``` + + + + + +[์ „์ฒ˜๋ฆฌ](./preprocessing) ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ์กฐํ•˜์‹œ๋ฉด ํ† ํฐํ™”์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ํ•จ๊ป˜ ์ด๋ฏธ์ง€, ์˜ค๋””์˜ค์™€ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ [`AutoImageProcessor`]์™€ [`AutoFeatureExtractor`], [`AutoProcessor`]์˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•๋„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + + +### AutoModel [[automodel]] + + + +๐Ÿค— Transformers๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ„๋‹จํ•˜๊ณ  ํ†ตํ•ฉ๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, [`AutoTokenizer`]์ฒ˜๋Ÿผ [`AutoModel`]์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ๊ณผ์—…์— ์•Œ๋งž์€ [`AutoModel`]์„ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ (๋˜๋Š” ์‹œํ€€์Šค) ๋ถ„๋ฅ˜์˜ ๊ฒฝ์šฐ [`AutoModelForSequenceClassification`]์„ ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +[`AutoModel`] ํด๋ž˜์Šค์—์„œ ์ง€์›ํ•˜๋Š” ๊ณผ์—…์— ๋Œ€ํ•ด์„œ๋Š” [๊ณผ์—… ์š”์•ฝ](./task_summary)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +์ด์ œ ์ „์ฒ˜๋ฆฌ๋œ ์ž…๋ ฅ ๋ฌถ์Œ์„ ์ง์ ‘ ๋ชจ๋ธ์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ `**`๋ฅผ ์•ž์— ๋ถ™์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ’€์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> pt_outputs = pt_model(**pt_batch) +``` + +๋ชจ๋ธ์˜ ์ตœ์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ์€ `logits` ์†์„ฑ์— ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. `logits`์— softmax ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ํ™•๋ฅ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from torch import nn + +>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) +>>> print(pt_predictions) +tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], + [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=) +``` + + +๐Ÿค— Transformers๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ„๋‹จํ•˜๊ณ  ํ†ตํ•ฉ๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, [`AutoTokenizer`]์ฒ˜๋Ÿผ [`TFAutoModel`]์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ๊ณผ์—…์— ์•Œ๋งž์€ [`TFAutoModel`]์„ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ (๋˜๋Š” ์‹œํ€€์Šค) ๋ถ„๋ฅ˜์˜ ๊ฒฝ์šฐ [`TFAutoModelForSequenceClassification`]์„ ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForSequenceClassification + +>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) +``` + + + +[`AutoModel`] ํด๋ž˜์Šค์—์„œ ์ง€์›ํ•˜๋Š” ๊ณผ์—…์— ๋Œ€ํ•ด์„œ๋Š” [๊ณผ์—… ์š”์•ฝ](./task_summary)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +์ด์ œ ์ „์ฒ˜๋ฆฌ๋œ ์ž…๋ ฅ ๋ฌถ์Œ์„ ์ง์ ‘ ๋ชจ๋ธ์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ๊ทธ๋Œ€๋กœ ํ…์„œ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> tf_outputs = tf_model(tf_batch) +``` + +๋ชจ๋ธ์˜ ์ตœ์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ์€ `logits` ์†์„ฑ์— ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. `logits`์— softmax ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ํ™•๋ฅ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> import tensorflow as tf + +>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) +>>> tf_predictions # doctest: +IGNORE_RESULT +``` + + + + + +๋ชจ๋“  ๐Ÿค— Transformers ๋ชจ๋ธ(PyTorch ๋˜๋Š” TensorFlow)์€ (softmax์™€ ๊ฐ™์€) ์ตœ์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜ *์ด์ „์—* ํ…์„œ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ตœ์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์€ ์ข…์ข… ์†์‹ค ํ•จ์ˆ˜ ์ถœ๋ ฅ๊ณผ ๊ฒฐํ•ฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ ์ถœ๋ ฅ์€ ํŠน์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค์ด๋ฏ€๋กœ IDE์—์„œ ์ž๋™ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์ถœ๋ ฅ์€ ํŠœํ”Œ์ด๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋ฉฐ (์ •์ˆ˜, ์Šฌ๋ผ์ด์Šค ๋˜๋Š” ๋ฌธ์ž์—ด๋กœ ์ธ๋ฑ์‹ฑ ๊ฐ€๋Šฅ), None์ธ ์†์„ฑ์€ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. + + + +### ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ [[save-a-model]] + + + +๋ฏธ์„ธ์กฐ์ •๋œ ๋ชจ๋ธ์„ ํ† ํฌ๋‚˜์ด์ €์™€ ํ•จ๊ป˜ ์ €์žฅํ•˜๋ ค๋ฉด [`PreTrainedModel.save_pretrained`]๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +>>> pt_save_directory = "./pt_save_pretrained" +>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT +>>> pt_model.save_pretrained(pt_save_directory) +``` + +๋ชจ๋ธ์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด [`PreTrainedModel.from_pretrained`]๋กœ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๋กœ๋“œํ•˜์„ธ์š”: + +```py +>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") +``` + + +๋ฏธ์„ธ์กฐ์ •๋œ ๋ชจ๋ธ์„ ํ† ํฌ๋‚˜์ด์ €์™€ ํ•จ๊ป˜ ์ €์žฅํ•˜๋ ค๋ฉด [`TFPreTrainedModel.save_pretrained`]๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”: + +```py +>>> tf_save_directory = "./tf_save_pretrained" +>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT +>>> tf_model.save_pretrained(tf_save_directory) +``` + +๋ชจ๋ธ์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด [`TFPreTrainedModel.from_pretrained`]๋กœ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๋กœ๋“œํ•˜์„ธ์š”: + +```py +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") +``` + + + +๐Ÿค— Transformers์˜ ๋ฉ‹์ง„ ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜๋Š” ๋ชจ๋ธ์„ PyTorch ๋˜๋Š” TensorFlow ๋ชจ๋ธ๋กœ ์ €์žฅํ•ด๋’€๋‹ค๊ฐ€ ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ž…๋‹ˆ๋‹ค. `from_pt` ๋˜๋Š” `from_tf` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + + + + +```py +>>> from transformers import AutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) +>>> pt_model = AutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True) +``` + + + +```py +>>> from transformers import TFAutoModel + +>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) +>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) +``` + + + +## ์ปค์Šคํ…€ ๋ชจ๋ธ ๊ตฌ์ถ•ํ•˜๊ธฐ [[custom-model-builds]] + +๋ชจ๋ธ์˜ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์€๋‹‰์ธต์ด๋‚˜ ์–ดํ…์…˜ ํ—ค๋“œ์˜ ์ˆ˜์™€ ๊ฐ™์€) ๋ชจ๋ธ์˜ ์†์„ฑ์€ ๊ตฌ์„ฑ์—์„œ ์ง€์ •๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ปค์Šคํ…€ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋กœ ๋ชจ๋ธ์„ ๋งŒ๋“ค๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์†์„ฑ์€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋˜๋ฏ€๋กœ ์˜๋ฏธ ์žˆ๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋จผ์ € ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +๋จผ์ € [`AutoConfig`]๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€ ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์„ธ์š”. [`AutoConfig.from_pretrained`] ๋‚ด๋ถ€์—์„œ (์–ดํ…์…˜ ํ—ค๋“œ ์ˆ˜์™€ ๊ฐ™์ด) ๋ณ€๊ฒฝํ•˜๋ ค๋Š” ์†์„ฑ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoConfig + +>>> my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12) +``` + + + +[`AutoModel.from_config`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๊พผ ๊ตฌ์„ฑ๋Œ€๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoModel + +>>> my_model = AutoModel.from_config(my_config) +``` + + +[`TFAutoModel.from_config`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๊พผ ๊ตฌ์„ฑ๋Œ€๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜์„ธ์š”: + +```py +>>> from transformers import TFAutoModel + +>>> my_model = TFAutoModel.from_config(my_config) +``` + + + +์ปค์Šคํ…€ ๊ตฌ์„ฑ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [์ปค์Šคํ…€ ์•„ํ‚คํ…์ฒ˜ ๋งŒ๋“ค๊ธฐ](./create_a_model) ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +## Trainer - PyTorch์— ์ตœ์ ํ™”๋œ ํ›ˆ๋ จ ๋ฃจํ”„ [[trainer-a-pytorch-optimized-training-loop]] + +๋ชจ๋“  ๋ชจ๋ธ์€ [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)์ด๋ฏ€๋กœ ์ผ๋ฐ˜์ ์ธ ํ›ˆ๋ จ ๋ฃจํ”„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง์ ‘ ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๐Ÿค— Transformers๋Š” PyTorch๋ฅผ ์œ„ํ•œ [`Trainer`] ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค์—๋Š” ๊ธฐ๋ณธ ํ›ˆ๋ จ ๋ฃจํ”„๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ ๋ถ„์‚ฐ ํ›ˆ๋ จ, ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ๋“ฑ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +๊ณผ์—…์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ [`Trainer`]์— ๋‹ค์Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: + +1. [`PreTrainedModel`] ๋˜๋Š” [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: + + ```py + >>> from transformers import AutoModelForSequenceClassification + + >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. [`TrainingArguments`]๋Š” ํ•™์Šต๋ฅ , ๋ฐฐ์น˜ ํฌ๊ธฐ, ํ›ˆ๋ จํ•  ์—ํฌํฌ ์ˆ˜์™€ ๊ฐ™์€ ๋ชจ๋ธ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์ธ์ž๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค: + + ```py + >>> from transformers import TrainingArguments + + >>> training_args = TrainingArguments( + ... output_dir="path/to/save/folder/", + ... learning_rate=2e-5, + ... per_device_train_batch_size=8, + ... per_device_eval_batch_size=8, + ... num_train_epochs=2, + ... ) + ``` + +3. ํ† ํฌ๋‚˜์ด์ €, ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ, ํŠน์ง• ์ถ”์ถœ๊ธฐ(feature extractor) ๋˜๋Š” ํ”„๋กœ์„ธ์„œ์™€ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ๋กœ๋“œํ•˜์„ธ์š”: + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +4. ๋ฐ์ดํ„ฐ์…‹์„ ๋กœ๋“œํ•˜์„ธ์š”: + + ```py + >>> from datasets import load_dataset + + >>> dataset = load_dataset("rotten_tomatoes") # doctest: +IGNORE_RESULT + ``` + +5. ๋ฐ์ดํ„ฐ์…‹์„ ํ† ํฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + + ```py + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) + ``` + + ๊ทธ๋ฆฌ๊ณ  [`~datasets.Dataset.map`]๋กœ ๋ฐ์ดํ„ฐ์…‹ ์ „์ฒด์— ์ ์šฉํ•˜์„ธ์š”: + + ```py + >>> dataset = dataset.map(tokenize_dataset, batched=True) + ``` + +6. [`DataCollatorWithPadding`]์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์…‹์˜ ํ‘œ๋ณธ ๋ฌถ์Œ์„ ๋งŒ๋“œ์„ธ์š”: + + ```py + >>> from transformers import DataCollatorWithPadding + + >>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer) + ``` + +์ด์ œ ์œ„์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ [`Trainer`]๋กœ ๋ชจ์œผ์„ธ์š”: + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=dataset["train"], +... eval_dataset=dataset["test"], +... processing_class=tokenizer, +... data_collator=data_collator, +... ) # doctest: +SKIP +``` + +์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฉด [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•˜์„ธ์š”: + +```py +>>> trainer.train() # doctest: +SKIP +``` + + + +๋ฒˆ์—ญ์ด๋‚˜ ์š”์•ฝ๊ณผ ๊ฐ™์ด ์‹œํ€€์Šค-์‹œํ€€์Šค ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ์—…์—๋Š” [`Seq2SeqTrainer`] ๋ฐ [`Seq2SeqTrainingArguments`] ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. + + + +[`Trainer`] ๋‚ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์„œ๋ธŒํด๋ž˜์Šคํ™”ํ•˜์—ฌ ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ์†์‹ค ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €, ์Šค์ผ€์ค„๋Ÿฌ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ ๋˜ํ•œ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด์„œ๋Š” [`Trainer`] ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ [Callbacks](./main_classes/callback)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Callbacks๋กœ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ†ตํ•ฉํ•˜๊ณ , ํ›ˆ๋ จ ๋ฃจํ”„๋ฅผ ์ฒดํฌํ•˜์—ฌ ์ง„ํ–‰ ์ƒํ™ฉ์„ ๋ณด๊ณ ๋ฐ›๊ฑฐ๋‚˜, ํ›ˆ๋ จ์„ ์กฐ๊ธฐ์— ์ค‘๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Callbacks์€ ํ›ˆ๋ จ ๋ฃจํ”„ ์ž์ฒด๋ฅผ ๋ฐ”๊พธ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๊ฒƒ์„ ๋ฐ”๊พธ๋ ค๋ฉด [`Trainer`]๋ฅผ ์„œ๋ธŒํด๋ž˜์Šคํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +## TensorFlow๋กœ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ [[train-with-tensorflow]] + +๋ชจ๋“  ๋ชจ๋ธ์€ [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)์ด๋ฏ€๋กœ [Keras](https://keras.io/) API๋ฅผ ํ†ตํ•ด TensorFlow์—์„œ ํ›ˆ๋ จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค— Transformers๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ์‰ฝ๊ฒŒ `tf.data.Dataset` ํ˜•ํƒœ๋กœ ์‰ฝ๊ฒŒ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” [`~TFPreTrainedModel.prepare_tf_dataset`] ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Keras์˜ [`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ๋ฐ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) ๋ฉ”์†Œ๋“œ๋กœ ๋ฐ”๋กœ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +1. [`TFPreTrainedModel`] ๋˜๋Š” [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: + + ```py + >>> from transformers import TFAutoModelForSequenceClassification + + >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +2. ํ† ํฌ๋‚˜์ด์ €, ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ, ํŠน์ง• ์ถ”์ถœ๊ธฐ(feature extractor) ๋˜๋Š” ํ”„๋กœ์„ธ์„œ์™€ ๊ฐ™์€ ์ „์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ๋กœ๋“œํ•˜์„ธ์š”: + + ```py + >>> from transformers import AutoTokenizer + + >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") + ``` + +3. ๋ฐ์ดํ„ฐ์…‹์„ ํ† ํฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”: + + ```py + >>> def tokenize_dataset(dataset): + ... return tokenizer(dataset["text"]) # doctest: +SKIP + ``` + +4. [`~datasets.Dataset.map`]์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ๋ฐ์ดํ„ฐ์…‹์— ํ† ํฐํ™” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ , ๋ฐ์ดํ„ฐ์…‹๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ [`~TFPreTrainedModel.prepare_tf_dataset`]์— ์ „๋‹ฌํ•˜์„ธ์š”. ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ์…‹์„ ์„ž์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + + ```py + >>> dataset = dataset.map(tokenize_dataset) # doctest: +SKIP + >>> tf_dataset = model.prepare_tf_dataset( + ... dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer + ... ) # doctest: +SKIP + ``` + +5. ์ค€๋น„๋˜์—ˆ์œผ๋ฉด `compile` ๋ฐ `fit`๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•˜์„ธ์š”. ๐Ÿค— Transformers์˜ ๋ชจ๋“  ๋ชจ๋ธ์€ ๊ณผ์—…๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋ณธ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค: + + ```py + >>> from tensorflow.keras.optimizers import Adam + + >>> model.compile(optimizer=Adam(3e-5)) # No loss argument! + >>> model.fit(tf_dataset) # doctest: +SKIP + ``` + +## ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? [[whats-next]] + +๐Ÿค— Transformers ๋‘˜๋Ÿฌ๋ณด๊ธฐ๋ฅผ ๋ชจ๋‘ ์ฝ์œผ์…จ๋‹ค๋ฉด, ๊ฐ€์ด๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๋” ๊ตฌ์ฒด์ ์ธ ๊ฒƒ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์„ธ์š”. ์ด๋ฅผํ…Œ๋ฉด ์ปค์Šคํ…€ ๋ชจ๋ธ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ณผ์—…์— ์•Œ๋งž๊ฒŒ ๋ชจ๋ธ์„ ๋ฏธ์„ธ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์Šคํฌ๋ฆฝํŠธ๋กœ ๋ชจ๋ธ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค— Transformers ํ•ต์‹ฌ ๊ฐœ๋…์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๋ ค๋ฉด ์ปคํ”ผ ํ•œ ์ž” ๋“ค๊ณ  ๊ฐœ๋… ๊ฐ€์ด๋“œ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/run_scripts.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/run_scripts.md new file mode 100644 index 0000000000000000000000000000000000000000..7cbf2288880cf02812d0ad0ea88604d14e972bf1 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/run_scripts.md @@ -0,0 +1,375 @@ + + +# ์Šคํฌ๋ฆฝํŠธ๋กœ ์‹คํ–‰ํ•˜๊ธฐ[[train-with-a-script]] + +๐Ÿค— Transformers ๋…ธํŠธ๋ถ๊ณผ ํ•จ๊ป˜ [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch), [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow), ๋˜๋Š” [JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax)๋ฅผ ์‚ฌ์šฉํ•ด ํŠน์ • ํƒœ์Šคํฌ์— ๋Œ€ํ•œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋˜ํ•œ [์—ฐ๊ตฌ ํ”„๋กœ์ ํŠธ](https://github.com/huggingface/transformers-research-projects/) ๋ฐ [๋ ˆ๊ฑฐ์‹œ ์˜ˆ์ œ](https://github.com/huggingface/transformers/tree/main/examples/legacy)์—์„œ ๋Œ€๋ถ€๋ถ„ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ œ๊ณตํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ์Šคํฌ๋ฆฝํŠธ๋Š” ์ ๊ทน์ ์œผ๋กœ ์œ ์ง€ ๊ด€๋ฆฌ๋˜์ง€ ์•Š์œผ๋ฉฐ ์ตœ์‹  ๋ฒ„์ „์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ˜ธํ™˜๋˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ํŠน์ • ๋ฒ„์ „์˜ ๐Ÿค— Transformers๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ชจ๋“  ๋ฌธ์ œ์—์„œ ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ฉฐ, ํ•ด๊ฒฐํ•˜๋ ค๋Š” ๋ฌธ์ œ์— ๋งž๊ฒŒ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋ฅผ ์œ„ํ•ด ๋Œ€๋ถ€๋ถ„์˜ ์Šคํฌ๋ฆฝํŠธ์—๋Š” ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ด ๋‚˜์™€์žˆ์–ด ํ•„์š”์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ์— ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์€ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด pull request๋ฅผ ์ œ์ถœํ•˜๊ธฐ ์ „์— [ํฌ๋Ÿผ](https://discuss.huggingface.co/) ๋˜๋Š” [์ด์Šˆ](https://github.com/huggingface/transformers/issues)์—์„œ ๋…ผ์˜ํ•ด ์ฃผ์„ธ์š”. +๋ฒ„๊ทธ ์ˆ˜์ •์€ ํ™˜์˜ํ•˜์ง€๋งŒ ๊ฐ€๋…์„ฑ์„ ํฌ์ƒํ•˜๋ฉด์„œ๊นŒ์ง€ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” pull request๋Š” ๋ณ‘ํ•ฉ(merge)ํ•˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) ๋ฐ [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization)์—์„œ ์š”์•ฝ ํ›ˆ๋ จํ•˜๋Š” + ์Šคํฌ๋ฆฝํŠธ ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. +ํŠน๋ณ„ํ•œ ์„ค๋ช…์ด ์—†๋Š” ํ•œ ๋ชจ๋“  ์˜ˆ์ œ๋Š” ๋‘ ํ”„๋ ˆ์ž„์›Œํฌ ๋ชจ๋‘์—์„œ ์ž‘๋™ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. + +## ์„ค์ •ํ•˜๊ธฐ[[setup]] + +์ตœ์‹  ๋ฒ„์ „์˜ ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ƒˆ ๊ฐ€์ƒ ํ™˜๊ฒฝ์—์„œ **์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๐Ÿค— Transformers๋ฅผ ์„ค์น˜**ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```bash +git clone https://github.com/huggingface/transformers +cd transformers +pip install . +``` + +์ด์ „ ๋ฒ„์ „์˜ ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณด๋ ค๋ฉด ์•„๋ž˜ ํ† ๊ธ€์„ ํด๋ฆญํ•˜์„ธ์š”: + +
+ ์ด์ „ ๋ฒ„์ „์˜ ๐Ÿค— Transformers ์˜ˆ์ œ + +
+ +๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณต์ œ(clone)ํ•ด์˜จ ๐Ÿค— Transformers ๋ฒ„์ „์„ ํŠน์ • ๋ฒ„์ „(์˜ˆ: v3.5.1)์œผ๋กœ ์ „ํ™˜ํ•˜์„ธ์š”: + +```bash +git checkout tags/v3.5.1 +``` + +์˜ฌ๋ฐ”๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฒ„์ „์„ ์„ค์ •ํ•œ ํ›„ ์›ํ•˜๋Š” ์˜ˆ์ œ ํด๋”๋กœ ์ด๋™ํ•˜์—ฌ ์˜ˆ์ œ๋ณ„๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ(requirements)์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค: + +```bash +pip install -r requirements.txt +``` + +## ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ํ•˜๊ธฐ[[run-a-script]] + + + +์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๋Š” ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ „์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋Š” ์š”์•ฝ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ์•„ํ‚คํ…์ฒ˜์—์„œ [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +๋‹ค์Œ ์˜ˆ๋Š” [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ [T5-small](https://huggingface.co/google-t5/t5-small)์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +T5 ๋ชจ๋ธ์€ ํ›ˆ๋ จ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์ถ”๊ฐ€ `source_prefix` ์ธ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด ํ”„๋กฌํ”„ํŠธ๋Š” ์š”์•ฝ ์ž‘์—…์ž„์„ T5์— ์•Œ๋ ค์ค๋‹ˆ๋‹ค. + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๋Š” ๐Ÿค— [Datasets](https://huggingface.co/docs/datasets/) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ „์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋Š” ์š”์•ฝ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ์•„ํ‚คํ…์ฒ˜์—์„œ Keras๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +๋‹ค์Œ ์˜ˆ๋Š” [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ [T5-small](https://huggingface.co/google-t5/t5-small)์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +T5 ๋ชจ๋ธ์€ ํ›ˆ๋ จ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์ถ”๊ฐ€ `source_prefix` ์ธ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด ํ”„๋กฌํ”„ํŠธ๋Š” ์š”์•ฝ ์ž‘์—…์ž„์„ T5์— ์•Œ๋ ค์ค๋‹ˆ๋‹ค. +```bash +python examples/tensorflow/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## ํ˜ผํ•ฉ ์ •๋ฐ€๋„(mixed precision)๋กœ ๋ถ„์‚ฐ ํ›ˆ๋ จํ•˜๊ธฐ[[distributed-training-and-mixed-precision]] + +[Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) ํด๋ž˜์Šค๋Š” ๋ถ„์‚ฐ ํ›ˆ๋ จ๊ณผ ํ˜ผํ•ฉ ์ •๋ฐ€๋„(mixed precision)๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด ๋‘ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- `fp16` ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ํ˜ผํ•ฉ ์ •๋ฐ€๋„(mixed precision)๋ฅผ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. +- `nproc_per_node` ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```bash +torchrun \ + --nproc_per_node 8 pytorch/summarization/run_summarization.py \ + --fp16 \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +TensorFlow ์Šคํฌ๋ฆฝํŠธ๋Š” ๋ถ„์‚ฐ ํ›ˆ๋ จ์„ ์œ„ํ•ด [`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy)๋ฅผ ํ™œ์šฉํ•˜๋ฉฐ, ํ›ˆ๋ จ ์Šคํฌ๋ฆฝํŠธ์— ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. +๋‹ค์ค‘ GPU ํ™˜๊ฒฝ์ด๋ผ๋ฉด, TensorFlow ์Šคํฌ๋ฆฝํŠธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ GPU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## TPU ์œ„์—์„œ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ํ•˜๊ธฐ[[run-a-script-on-a-tpu]] + + + +Tensor Processing Units (TPUs)๋Š” ์„ฑ๋Šฅ์„ ๊ฐ€์†ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +PyTorch๋Š” [XLA](https://www.tensorflow.org/xla) ๋”ฅ๋Ÿฌ๋‹ ์ปดํŒŒ์ผ๋Ÿฌ์™€ ํ•จ๊ป˜ TPU๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค(์ž์„ธํ•œ ๋‚ด์šฉ์€ [์—ฌ๊ธฐ](https://github.com/pytorch/xla/blob/master/README.md) ์ฐธ์กฐ). +TPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด `xla_spawn.py` ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  `num_cores` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ ค๋Š” TPU ์ฝ”์–ด ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```bash +python xla_spawn.py --num_cores 8 \ + summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + + +Tensor Processing Units (TPUs)๋Š” ์„ฑ๋Šฅ์„ ๊ฐ€์†ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +TensorFlow ์Šคํฌ๋ฆฝํŠธ๋Š” TPU๋ฅผ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด [`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy)๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. +TPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด TPU ๋ฆฌ์†Œ์Šค์˜ ์ด๋ฆ„์„ `tpu` ์ธ์ˆ˜์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```bash +python run_summarization.py \ + --tpu name_of_tpu_resource \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size 8 \ + --per_device_eval_batch_size 16 \ + --num_train_epochs 3 \ + --do_train \ + --do_eval +``` + + + +## ๐Ÿค— Accelerate๋กœ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ํ•˜๊ธฐ[[run-a-script-with-accelerate]] + +๐Ÿค— [Accelerate](https://huggingface.co/docs/accelerate)๋Š” PyTorch ํ›ˆ๋ จ ๊ณผ์ •์— ๋Œ€ํ•œ ์™„์ „ํ•œ ๊ฐ€์‹œ์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ์„ค์ •(CPU ์ „์šฉ, ๋‹ค์ค‘ GPU, TPU)์—์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ๋Š” ํ†ตํ•ฉ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” PyTorch ์ „์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. +๐Ÿค— Accelerate๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +> ์ฐธ๊ณ : Accelerate๋Š” ๋น ๋ฅด๊ฒŒ ๊ฐœ๋ฐœ ์ค‘์ด๋ฏ€๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด accelerate๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +```bash +pip install git+https://github.com/huggingface/accelerate +``` + +`run_summarization.py` ์Šคํฌ๋ฆฝํŠธ ๋Œ€์‹  `run_summarization_no_trainer.py` ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +๐Ÿค— Accelerate ํด๋ž˜์Šค๊ฐ€ ์ง€์›๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋Š” ํด๋”์— `task_no_trainer.py` ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. +๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ๊ตฌ์„ฑ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค: +```bash +accelerate config +``` + +์„ค์ •์„ ํ…Œ์ŠคํŠธํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค: + +```bash +accelerate test +``` + +์ด์ œ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: + +```bash +accelerate launch run_summarization_no_trainer.py \ + --model_name_or_path google-t5/t5-small \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir ~/tmp/tst-summarization +``` + +## ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ ์„ธํŠธ ์‚ฌ์šฉํ•˜๊ธฐ[[use-a-custom-dataset]] + +์š”์•ฝ ์Šคํฌ๋ฆฝํŠธ๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ๊ฐ€ CSV ๋˜๋Š” JSON ํŒŒ์ผ์ธ ๊ฒฝ์šฐ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. +์‚ฌ์šฉ์ž ์ง€์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ถ”๊ฐ€ ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +- `train_file`๊ณผ `validation_file`์€ ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. +- `text_column`์€ ์š”์•ฝํ•  ์ž…๋ ฅ ํ…์ŠคํŠธ์ž…๋‹ˆ๋‹ค. +- `summary_column`์€ ์ถœ๋ ฅํ•  ๋Œ€์ƒ ํ…์ŠคํŠธ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž ์ง€์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์š”์•ฝ ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --train_file path_to_csv_or_jsonlines_file \ + --validation_file path_to_csv_or_jsonlines_file \ + --text_column text_column_name \ + --summary_column summary_column_name \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --overwrite_output_dir \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --predict_with_generate +``` + +## ์Šคํฌ๋ฆฝํŠธ ํ…Œ์ŠคํŠธํ•˜๊ธฐ[[test-a-script]] + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ›ˆ๋ จ์„ ์™„๋ฃŒํ•˜๋Š”๋ฐ ๊ฝค ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—, ์ž‘์€ ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ๋ชจ๋“  ๊ฒƒ์ด ์˜ˆ์ƒ๋Œ€๋กœ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์ตœ๋Œ€ ์ƒ˜ํ”Œ ์ˆ˜๋กœ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค: +- `max_train_samples` +- `max_eval_samples` +- `max_predict_samples` + +```bash +python examples/pytorch/summarization/run_summarization.py \ + --model_name_or_path google-t5/t5-small \ + --max_train_samples 50 \ + --max_eval_samples 50 \ + --max_predict_samples 50 \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` + +๋ชจ๋“  ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ `max_predict_samples` ์ธ์ˆ˜๋ฅผ ์ง€์›ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. +์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ด ์ธ์ˆ˜๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ `-h` ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ™•์ธํ•˜์„ธ์š”: + +```bash +examples/pytorch/summarization/run_summarization.py -h +``` + +## ์ฒดํฌํฌ์ธํŠธ(checkpoint)์—์„œ ํ›ˆ๋ จ ์ด์–ด์„œ ํ•˜๊ธฐ[[resume-training-from-checkpoint]] + +๋˜ ๋‹ค๋ฅธ ์œ ์šฉํ•œ ์˜ต์…˜์€ ์ด์ „ ์ฒดํฌํฌ์ธํŠธ์—์„œ ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ›ˆ๋ จ์ด ์ค‘๋‹จ๋˜๋”๋ผ๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ  ์ค‘๋‹จํ•œ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ฒดํฌํฌ์ธํŠธ์—์„œ ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ฒซ ๋ฒˆ์งธ๋Š” `output_dir previous_output_dir` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `output_dir`์— ์ €์žฅ๋œ ์ตœ์‹  ์ฒดํฌํฌ์ธํŠธ๋ถ€ํ„ฐ ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. +์ด ๊ฒฝ์šฐ `overwrite_output_dir`์„ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --output_dir previous_output_dir \ + --predict_with_generate +``` + +๋‘ ๋ฒˆ์งธ๋Š” `resume_from_checkpoint path_to_specific_checkpoint` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์ฒดํฌํฌ์ธํŠธ ํด๋”์—์„œ ํ›ˆ๋ จ์„ ์žฌ๊ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --resume_from_checkpoint path_to_specific_checkpoint \ + --predict_with_generate +``` + +## ๋ชจ๋ธ ๊ณต์œ ํ•˜๊ธฐ[[share-your-model]] + +๋ชจ๋“  ์Šคํฌ๋ฆฝํŠธ๋Š” ์ตœ์ข… ๋ชจ๋ธ์„ [Model Hub](https://huggingface.co/models)์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Hugging Face์— ๋กœ๊ทธ์ธํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: +```bash +hf auth login +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ์— `push_to_hub` ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +์ด ์ธ์ˆ˜๋Š” Hugging Face ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ `output_dir`์— ์ง€์ •๋œ ํด๋” ์ด๋ฆ„์œผ๋กœ ์ €์žฅ์†Œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +์ €์žฅ์†Œ์— ํŠน์ • ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ ค๋ฉด `push_to_hub_model_id` ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +์ €์žฅ์†Œ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์•„๋ž˜์— ์ž๋™์œผ๋กœ ๋‚˜์—ด๋ฉ๋‹ˆ๋‹ค. +๋‹ค์Œ ์˜ˆ๋Š” ํŠน์ • ์ €์žฅ์†Œ ์ด๋ฆ„์œผ๋กœ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค: + +```bash +python examples/pytorch/summarization/run_summarization.py + --model_name_or_path google-t5/t5-small \ + --do_train \ + --do_eval \ + --dataset_name cnn_dailymail \ + --dataset_config "3.0.0" \ + --source_prefix "summarize: " \ + --push_to_hub \ + --push_to_hub_model_id finetuned-t5-cnn_dailymail \ + --output_dir /tmp/tst-summarization \ + --per_device_train_batch_size=4 \ + --per_device_eval_batch_size=4 \ + --overwrite_output_dir \ + --predict_with_generate +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serialization.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serialization.md new file mode 100644 index 0000000000000000000000000000000000000000..2e521e2b7b4af83b4a95df0c3e28f17f957ba411 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serialization.md @@ -0,0 +1,181 @@ + + +# ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ [[export-to-onnx]] + +๐Ÿค— Transformers ๋ชจ๋ธ์„ ์ œํ’ˆ ํ™˜๊ฒฝ์—์„œ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋ธ์„ ์ง๋ ฌํ™”๋œ ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ณ  ํŠน์ • ๋Ÿฐํƒ€์ž„๊ณผ ํ•˜๋“œ์›จ์–ด์—์„œ ๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +๐Ÿค— Optimum์€ Transformers์˜ ํ™•์žฅ์œผ๋กœ, PyTorch ๋˜๋Š” TensorFlow์—์„œ ๋ชจ๋ธ์„ ONNX์™€ TFLite์™€ ๊ฐ™์€ ์ง๋ ฌํ™”๋œ ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” `exporters` ๋ชจ๋“ˆ์„ ํ†ตํ•ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๐Ÿค— Optimum์€ ๋˜ํ•œ ์„ฑ๋Šฅ ์ตœ์ ํ™” ๋„๊ตฌ ์„ธํŠธ๋ฅผ ์ œ๊ณตํ•˜์—ฌ ํŠน์ • ํ•˜๋“œ์›จ์–ด์—์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ณ  ์‹คํ–‰ํ•  ๋•Œ ์ตœ๋Œ€ ํšจ์œจ์„ฑ์„ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ์•ˆ๋‚ด์„œ๋Š” ๐Ÿค— Optimum์„ ์‚ฌ์šฉํ•˜์—ฌ ๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. TFLite๋กœ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ด๋Š” ์•ˆ๋‚ด์„œ๋Š” [TFLite๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ํŽ˜์ด์ง€](tflite)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +## ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ [[export-to-onnx]] + +[ONNX (Open Neural Network eXchange)](http://onnx.ai)๋Š” PyTorch์™€ TensorFlow๋ฅผ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‹ฌ์ธต ํ•™์Šต ๋ชจ๋ธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ณตํ†ต ์—ฐ์‚ฐ์ž ์„ธํŠธ์™€ ๊ณตํ†ต ํŒŒ์ผ ํ˜•์‹์„ ์ •์˜ํ•˜๋Š” ์˜คํ”ˆ ํ‘œ์ค€์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด์ง€๋ฉด ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ ๊ฒฝ๋ง์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ํ๋ฅด๋Š” ํ๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ณ„์‚ฐ ๊ทธ๋ž˜ํ”„(์ผ๋ฐ˜์ ์œผ๋กœ _์ค‘๊ฐ„ ํ‘œํ˜„_์ด๋ผ๊ณ  ํ•จ)๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. + +ํ‘œ์ค€ํ™”๋œ ์—ฐ์‚ฐ์ž์™€ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๊ฐ€์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋…ธ์ถœํ•จ์œผ๋กœ์จ, ONNX๋Š” ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ„์— ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, PyTorch์—์„œ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ณ  TensorFlow์—์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๊ทธ ๋ฐ˜๋Œ€๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค). + +ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: +- [๊ทธ๋ž˜ํ”„ ์ตœ์ ํ™”](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization) ๋ฐ [์–‘์žํ™”](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization)์™€ ๊ฐ™์€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก ์„ ์œ„ํ•ด ์ตœ์ ํ™”๋ฉ๋‹ˆ๋‹ค. +- ONNX Runtime์„ ํ†ตํ•ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`ORTModelForXXX` ํด๋ž˜์Šค๋“ค](https://huggingface.co/docs/optimum/onnxruntime/package_reference/modeling_ort)์„ ํ†ตํ•ด ๋™์ผํ•œ `AutoModel` API๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ด API๋Š” ๐Ÿค— Transformers์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. +- [์ตœ์ ํ™”๋œ ์ถ”๋ก  ํŒŒ์ดํ”„๋ผ์ธ](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๐Ÿค— Transformers์˜ [`pipeline`] ํ•จ์ˆ˜์™€ ๋™์ผํ•œ API๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + +๐Ÿค— Optimum์€ ๊ตฌ์„ฑ ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ONNX ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์„ฑ ๊ฐ์ฒด๋Š” ์—ฌ๋Ÿฌ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ ์ค€๋น„๋˜์–ด ์žˆ์œผ๋ฉฐ ๋‹ค๋ฅธ ์•„ํ‚คํ…์ฒ˜์— ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +๋ฏธ๋ฆฌ ์ค€๋น„๋œ ๊ตฌ์„ฑ ๋ชฉ๋ก์€ [๐Ÿค— Optimum ๋ฌธ์„œ](https://huggingface.co/docs/optimum/exporters/onnx/overview)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + +๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค: + +- ๐Ÿค— Optimum์„ ์‚ฌ์šฉํ•˜์—ฌ CLI๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ +- `optimum.onnxruntime`์„ ์‚ฌ์šฉํ•˜์—ฌ ๐Ÿค— Optimum์œผ๋กœ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ + +### CLI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ [[exporting-a-transformers-model-to-onnx-with-cli]] + +๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด ๋จผ์ € ์ถ”๊ฐ€ ์ข…์†์„ฑ์„ ์„ค์น˜ํ•˜์„ธ์š”: + +```bash +pip install optimum[exporters] +``` + +์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ธ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด [๐Ÿค— Optimum ๋ฌธ์„œ](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli)๋ฅผ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ๋ช…๋ น์ค„์—์„œ ๋„์›€๋ง์„ ๋ณด์„ธ์š”. + +```bash +optimum-cli export onnx --help +``` + +์˜ˆ๋ฅผ ๋“ค์–ด, ๐Ÿค— Hub์—์„œ `distilbert/distilbert-base-uncased-distilled-squad`์™€ ๊ฐ™์€ ๋ชจ๋ธ์˜ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”: + +```bash +optimum-cli export onnx --model distilbert/distilbert-base-uncased-distilled-squad distilbert_base_uncased_squad_onnx/ +``` + +์œ„์™€ ๊ฐ™์ด ์ง„ํ–‰ ์ƒํ™ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋กœ๊ทธ๊ฐ€ ํ‘œ์‹œ๋˜๊ณ  ๊ฒฐ๊ณผ์ธ `model.onnx`๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. + +```bash +Validating ONNX model distilbert_base_uncased_squad_onnx/model.onnx... + -[โœ“] ONNX model output names match reference model (start_logits, end_logits) + - Validating ONNX Model output "start_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) + - Validating ONNX Model output "end_logits": + -[โœ“] (2, 16) matches (2, 16) + -[โœ“] all values close (atol: 0.0001) +The ONNX export succeeded and the exported model was saved at: distilbert_base_uncased_squad_onnx +``` + +์œ„์˜ ์˜ˆ์ œ๋Š” ๐Ÿค— Hub์—์„œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒƒ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ผ ๋•Œ์—๋Š” ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜์™€ ํ† ํฌ๋‚˜์ด์ € ํŒŒ์ผ์„ ๋™์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ(`local_path`)์— ์ €์žฅํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. CLI๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๐Ÿค— Hub์˜ ์ฒดํฌํฌ์ธํŠธ ์ด๋ฆ„ ๋Œ€์‹  `model` ์ธ์ˆ˜์— `local_path`๋ฅผ ์ „๋‹ฌํ•˜๊ณ  `--task` ์ธ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์„ธ์š”. ์ง€์›๋˜๋Š” ์ž‘์—…์˜ ๋ชฉ๋ก์€ [๐Ÿค— Optimum ๋ฌธ์„œ](https://huggingface.co/docs/optimum/exporters/task_manager)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. `task` ์ธ์ˆ˜๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์œผ๋ฉด ์ž‘์—…์— ํŠนํ™”๋œ ํ—ค๋“œ ์—†์ด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋กœ ๊ธฐ๋ณธ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. + +```bash +optimum-cli export onnx --model local_path --task question-answering distilbert_base_uncased_squad_onnx/ +``` + +๊ทธ ๊ฒฐ๊ณผ๋กœ ์ƒ์„ฑ๋œ `model.onnx` ํŒŒ์ผ์€ ONNX ํ‘œ์ค€์„ ์ง€์›ํ•˜๋Š” ๋งŽ์€ [๊ฐ€์†๊ธฐ](https://onnx.ai/supported-tools.html#deployModel) ์ค‘ ํ•˜๋‚˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [ONNX Runtime](https://onnxruntime.ai/)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoTokenizer +>>> from optimum.onnxruntime import ORTModelForQuestionAnswering + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> model = ORTModelForQuestionAnswering.from_pretrained("distilbert_base_uncased_squad_onnx") +>>> inputs = tokenizer("What am I using?", "Using DistilBERT with ONNX Runtime!", return_tensors="pt") +>>> outputs = model(**inputs) +``` + +Hub์˜ TensorFlow ์ฒดํฌํฌ์ธํŠธ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [Keras organization](https://huggingface.co/keras-io)์—์„œ ์ˆœ์ˆ˜ํ•œ TensorFlow ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```bash +optimum-cli export onnx --model keras-io/transformers-qa distilbert_base_cased_squad_onnx/ +``` + +### `optimum.onnxruntime`์„ ์‚ฌ์šฉํ•˜์—ฌ ๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ [[exporting-a-transformers-model-to-onnx-with-optimumonnxruntime]] + +CLI ๋Œ€์‹ ์— `optimum.onnxruntime`์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ•˜์„ธ์š”: + +```python +>>> from optimum.onnxruntime import ORTModelForSequenceClassification +>>> from transformers import AutoTokenizer + +>>> model_checkpoint = "distilbert_base_uncased_squad" +>>> save_directory = "onnx/" + +>>> # Load a model from transformers and export it to ONNX +>>> ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=True) +>>> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) + +>>> # Save the onnx model and tokenizer +>>> ort_model.save_pretrained(save_directory) +>>> tokenizer.save_pretrained(save_directory) +``` + +### ์ง€์›๋˜์ง€ ์•Š๋Š” ์•„ํ‚คํ…์ฒ˜์˜ ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ [[exporting-a-model-for-an-unsupported-architecture]] + +ํ˜„์žฌ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์—†๋Š” ๋ชจ๋ธ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์—ฌํ•˜๋ ค๋ฉด, ๋จผ์ € [`optimum.exporters.onnx`](https://huggingface.co/docs/optimum/exporters/onnx/overview)์—์„œ ์ง€์›๋˜๋Š”์ง€ ํ™•์ธํ•œ ํ›„ ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” [๐Ÿค— Optimum์— ๊ธฐ์—ฌ](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/contribute)ํ•˜์„ธ์š”. + +### `transformers.onnx`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ [[exporting-a-model-with-transformersonnx]] + + + +`tranformers.onnx`๋Š” ๋” ์ด์ƒ ์œ ์ง€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ ๐Ÿค— Optimum์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ด์„ธ์š”. ์ด ์„น์…˜์€ ํ–ฅํ›„ ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. + + + +๐Ÿค— Transformers ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด ์ถ”๊ฐ€ ์ข…์†์„ฑ์„ ์„ค์น˜ํ•˜์„ธ์š”: + +```bash +pip install transformers[onnx] +``` + +`transformers.onnx` ํŒจํ‚ค์ง€๋ฅผ Python ๋ชจ๋“ˆ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ค€๋น„๋œ ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค: + +```bash +python -m transformers.onnx --model=distilbert/distilbert-base-uncased onnx/ +``` + +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด `--model` ์ธ์ˆ˜์— ์ •์˜๋œ ์ฒดํฌํฌ์ธํŠธ์˜ ONNX ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚ด๋ณด๋‚ด์ง‘๋‹ˆ๋‹ค. ๐Ÿค— Hub์—์„œ ์ œ๊ณตํ•˜๋Š” ์ฒดํฌํฌ์ธํŠธ๋‚˜ ๋กœ์ปฌ์— ์ €์žฅ๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋กœ ์ƒ์„ฑ๋œ `model.onnx` ํŒŒ์ผ์€ ONNX ํ‘œ์ค€์„ ์ง€์›ํ•˜๋Š” ๋งŽ์€ ๊ฐ€์†๊ธฐ ์ค‘ ํ•˜๋‚˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ONNX Runtime์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers import AutoTokenizer +>>> from onnxruntime import InferenceSession + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased") +>>> session = InferenceSession("onnx/model.onnx") +>>> # ONNX Runtime expects NumPy arrays as input +>>> inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="np") +>>> outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs)) +``` + +ํ•„์š”ํ•œ ์ถœ๋ ฅ ์ด๋ฆ„(์˜ˆ: `["last_hidden_state"]`)์€ ๊ฐ ๋ชจ๋ธ์˜ ONNX ๊ตฌ์„ฑ์„ ํ™•์ธํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, DistilBERT์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +```python +>>> from transformers.models.distilbert import DistilBertConfig, DistilBertOnnxConfig + +>>> config = DistilBertConfig() +>>> onnx_config = DistilBertOnnxConfig(config) +>>> print(list(onnx_config.outputs.keys())) +["last_hidden_state"] +``` + +Hub์˜ TensorFlow ์ฒดํฌํฌ์ธํŠธ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆœ์ˆ˜ํ•œ TensorFlow ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค: + +```bash +python -m transformers.onnx --model=keras-io/transformers-qa onnx/ +``` + +๋กœ์ปฌ์— ์ €์žฅ๋œ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ด๋ ค๋ฉด ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜ ํŒŒ์ผ๊ณผ ํ† ํฌ๋‚˜์ด์ € ํŒŒ์ผ์„ ๋™์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ์— ์ €์žฅํ•œ ๋‹ค์Œ, transformers.onnx ํŒจํ‚ค์ง€์˜ --model ์ธ์ˆ˜๋ฅผ ์›ํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ง€์ •ํ•˜์—ฌ ONNX๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค: + +```bash +python -m transformers.onnx --model=local-pt-checkpoint onnx/ +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serving.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serving.md new file mode 100644 index 0000000000000000000000000000000000000000..ae9bc730f1952d4fe35e3f7c1f2b819d569e9942 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/serving.md @@ -0,0 +1,64 @@ + + +# ๋ชจ๋ธ ์„œ๋น™ [[Serving]] + +Text Generation Inference (TGI) ๋ฐ vLLM๊ณผ ๊ฐ™์€ ํŠน์ˆ˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด Transformer ๋ชจ๋ธ์„ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” vLLM์˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์œผ๋ฉฐ, Transformers์—๋Š” ํฌํ•จ๋˜์ง€ ์•Š์€ ๊ณ ์œ ํ•œ ์ตœ์ ํ™” ๊ธฐ๋Šฅ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +## TGI [[TGI]] + +[๋„ค์ดํ‹ฐ๋ธŒ๋กœ ๊ตฌํ˜„๋œ ๋ชจ๋ธ](https://huggingface.co/docs/text-generation-inference/supported_models)์ด ์•„๋‹ˆ๋”๋ผ๋„ TGI๋กœ Transformers ๊ตฌํ˜„ ๋ชจ๋ธ์„ ์„œ๋น™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TGI์—์„œ ์ œ๊ณตํ•˜๋Š” ์ผ๋ถ€ ๊ณ ์„ฑ๋Šฅ ๊ธฐ๋Šฅ์€ ์ง€์›ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฐ์† ๋ฐฐ์นญ์ด๋‚˜ ์ŠคํŠธ๋ฆฌ๋ฐ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +> [!TIP] +> ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ [๋…ผ-์ฝ”์–ด ๋ชจ๋ธ ์„œ๋น™](https://huggingface.co/docs/text-generation-inference/basic_tutorials/non_core_models) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +TGI ๋ชจ๋ธ์„ ์„œ๋น™ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ Transformer ๊ตฌํ˜„ ๋ชจ๋ธ์„ ์„œ๋น™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```docker +docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:latest --model-id gpt2 +``` + +์ปค์Šคํ…€ Transformers ๋ชจ๋ธ์„ ์„œ๋น™ํ•˜๋ ค๋ฉด `--trust-remote_code`๋ฅผ ๋ช…๋ น์–ด์— ์ถ”๊ฐ€ํ•˜์„ธ์š”. + +```docker +docker run --gpus all --shm-size 1g -p 8080:80 -v $volume:/data ghcr.io/huggingface/text-generation-inference:latest --model-id --trust-remote-code +``` + +## vLLM [[vLLM]] + +[vLLM](https://docs.vllm.ai/en/latest/index.html)์€ ํŠน์ • ๋ชจ๋ธ์ด vLLM์—์„œ [๋„ค์ดํ‹ฐ๋ธŒ๋กœ ๊ตฌํ˜„๋œ ๋ชจ๋ธ](https://docs.vllm.ai/en/latest/models/supported_models.html#list-of-text-only-language-models)์ด ์•„๋‹ ๊ฒฝ์šฐ, Transformers ๊ตฌํ˜„ ๋ชจ๋ธ์„ ์„œ๋น™ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +Transformers ๊ตฌํ˜„์—์„œ๋Š” ์–‘์žํ™”, LoRA ์–ด๋Œ‘ํ„ฐ, ๋ถ„์‚ฐ ์ถ”๋ก  ๋ฐ ์„œ๋น™๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +> [!TIP] +> [Transformers fallback](https://docs.vllm.ai/en/latest/models/supported_models.html#transformers-fallback) ์„น์…˜์—์„œ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ธฐ๋ณธ์ ์œผ๋กœ vLLM์€ ๋„ค์ดํ‹ฐ๋ธŒ ๊ตฌํ˜„์„ ์„œ๋น™ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๊ตฌํ˜„์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด Transformers ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ `--model-impl transformers` ์˜ต์…˜์„ ์„ค์ •ํ•˜๋ฉด ๋ช…์‹œ์ ์œผ๋กœ Transformers ๋ชจ๋ธ ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```shell +vllm serve Qwen/Qwen2.5-1.5B-Instruct \ + --task generate \ + --model-impl transformers \ +``` + +`trust-remote-code` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด ์›๊ฒฉ ์ฝ”๋“œ ๋ชจ๋ธ ๋กœ๋“œ๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```shell +vllm serve Qwen/Qwen2.5-1.5B-Instruct \ + --task generate \ + --model-impl transformers \ + --trust-remote-code \ +``` \ No newline at end of file diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/asr.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/asr.md new file mode 100644 index 0000000000000000000000000000000000000000..6c8ad6fc320123861935d232592c18fefc71094f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/asr.md @@ -0,0 +1,375 @@ + + +# ์ž๋™ ์Œ์„ฑ ์ธ์‹[[automatic-speech-recognition]] + +[[open-in-colab]] + + + +์ž๋™ ์Œ์„ฑ ์ธ์‹(Automatic Speech Recognition, ASR)์€ ์Œ์„ฑ ์‹ ํ˜ธ๋ฅผ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์Œ์„ฑ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์ถœ๋ ฅ์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. +Siri์™€ Alexa์™€ ๊ฐ™์€ ๊ฐ€์ƒ ์–ด์‹œ์Šคํ„ดํŠธ๋Š” ASR ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ์ƒ์ ์œผ๋กœ ์‚ฌ์šฉ์ž๋ฅผ ๋•๊ณ  ์žˆ์œผ๋ฉฐ, ํšŒ์˜ ์ค‘ ๋ผ์ด๋ธŒ ์บก์…˜ ๋ฐ ๋ฉ”๋ชจ ์ž‘์„ฑ๊ณผ ๊ฐ™์€ ์œ ์šฉํ•œ ์‚ฌ์šฉ์ž ์นœํ™”์  ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ๋„ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ ์†Œ๊ฐœํ•  ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +1. [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base)๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์˜ค๋””์˜ค๋ฅผ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +2. ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์ถ”๋ก ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/automatic-speech-recognition)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate jiwer +``` + +Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜๋ฉด ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•˜์„ธ์š”. + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## MInDS-14 ๋ฐ์ดํ„ฐ ์„ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ[[load-minds-14-dataset]] + +๋จผ์ €, ๐Ÿค— Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ์ผ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜ค์„ธ์š”. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋Œ€ํ•œ ํ›ˆ๋ จ์— ์‹œ๊ฐ„์„ ๋“ค์ด๊ธฐ ์ „์— ๋ชจ๋“  ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€ ์‹คํ—˜ํ•˜๊ณ  ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train[:100]") +``` + +[`~Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ `train`์„ ํ›ˆ๋ จ ์„ธํŠธ์™€ ํ…Œ์ŠคํŠธ ์„ธํŠธ๋กœ ๋‚˜๋ˆ„์„ธ์š”: + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 16 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 4 + }) +}) +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์—๋Š” `lang_id`์™€ `english_transcription`๊ณผ ๊ฐ™์€ ์œ ์šฉํ•œ ์ •๋ณด๊ฐ€ ๋งŽ์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ, ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” `audio`์™€ `transcription`์— ์ดˆ์ ์„ ๋งž์ถœ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์—ด์€ [`~datasets.Dataset.remove_columns`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๊ฑฐํ•˜์„ธ์š”: + +```py +>>> minds = minds.remove_columns(["english_transcription", "intent_class", "lang_id"]) +``` + +์˜ˆ์‹œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ณด์„ธ์š”: + +```py +>>> minds["train"][0] +{'audio': {'array': array([-0.00024414, 0. , 0. , ..., 0.00024414, + 0.00024414, 0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 8000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +๋‘ ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +- `audio`: ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌ์ƒ˜ํ”Œ๋งํ•˜๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ์Œ์„ฑ ์‹ ํ˜ธ์˜ 1์ฐจ์› `array(๋ฐฐ์—ด)` +- `transcription`: ๋ชฉํ‘œ ํ…์ŠคํŠธ + +## ์ „์ฒ˜๋ฆฌ[[preprocess]] + +๋‹ค์Œ์œผ๋กœ ์˜ค๋””์˜ค ์‹ ํ˜ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ Wav2Vec2 ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base") +``` + +MInDS-14 ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ์ƒ˜ํ”Œ๋ง ๋ ˆ์ดํŠธ๋Š” 8000kHz์ด๋ฏ€๋กœ([๋ฐ์ดํ„ฐ ์„ธํŠธ ์นด๋“œ](https://huggingface.co/datasets/PolyAI/minds14)์—์„œ ํ™•์ธ), ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Wav2Vec2 ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ 16000kHz๋กœ ๋ฆฌ์ƒ˜ํ”Œ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([-2.38064706e-04, -1.58618059e-04, -5.43987835e-06, ..., + 2.78103951e-04, 2.38446111e-04, 1.18740834e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'sampling_rate': 16000}, + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602ba9e2963e11ccd901cd4f.wav', + 'transcription': "hi I'm trying to use the banking app on my phone and currently my checking and savings account balance is not refreshing"} +``` + +์œ„์˜ 'transcription'์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ํ…์ŠคํŠธ๋Š” ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. Wav2Vec2 ํ† ํฌ๋‚˜์ด์ €๋Š” ๋Œ€๋ฌธ์ž ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ํ›ˆ๋ จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ํ…์ŠคํŠธ๊ฐ€ ํ† ํฌ๋‚˜์ด์ €์˜ ์–ดํœ˜์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> def uppercase(example): +... return {"transcription": example["transcription"].upper()} + + +>>> minds = minds.map(uppercase) +``` + +์ด์ œ ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +1. `audio` ์—ด์„ ํ˜ธ์ถœํ•˜์—ฌ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌ์ƒ˜ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค. +2. ์˜ค๋””์˜ค ํŒŒ์ผ์—์„œ `input_values`๋ฅผ ์ถ”์ถœํ•˜๊ณ  ํ”„๋กœ์„ธ์„œ๋กœ `transcription` ์—ด์„ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> def prepare_dataset(batch): +... audio = batch["audio"] +... batch = processor(audio["array"], sampling_rate=audio["sampling_rate"], text=batch["transcription"]) +... batch["input_length"] = len(batch["input_values"][0]) +... return batch +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๋ ค๋ฉด ๐Ÿค— Datasets [`~datasets.Dataset.map`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. `num_proc` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๋ฉด `map`์˜ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [`~datasets.Dataset.remove_columns`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•˜์ง€ ์•Š์€ ์—ด์„ ์ œ๊ฑฐํ•˜์„ธ์š”: + +```py +>>> encoded_minds = minds.map(prepare_dataset, remove_columns=minds.column_names["train"], num_proc=4) +``` + +๐Ÿค— Transformers์—๋Š” ์ž๋™ ์Œ์„ฑ ์ธ์‹์šฉ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์˜ˆ์ œ ๋ฐฐ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด [`DataCollatorWithPadding`]์„ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๋Š” ํ…์ŠคํŠธ์™€ ๋ ˆ์ด๋ธ”์„ ๋ฐฐ์น˜์—์„œ ๊ฐ€์žฅ ๊ธด ์š”์†Œ์˜ ๊ธธ์ด์— ๋™์ ์œผ๋กœ ํŒจ๋”ฉํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๊ท ์ผํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. `tokenizer` ํ•จ์ˆ˜์—์„œ `padding=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ํŒจ๋”ฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋™์  ํŒจ๋”ฉ์ด ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. + +๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ์™€ ๋‹ฌ๋ฆฌ ์ด ํŠน์ • ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๋Š” `input_values`์™€ `labels`์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ํŒจ๋”ฉ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch + +>>> from dataclasses import dataclass, field +>>> from typing import Any, Dict, List, Optional, Union + + +>>> @dataclass +... class DataCollatorCTCWithPadding: +... processor: AutoProcessor +... padding: Union[bool, str] = "longest" + +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: +... # ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค +... # ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๊ณ , ๊ฐ๊ฐ ๋‹ค๋ฅธ ํŒจ๋”ฉ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค +... input_features = [{"input_values": feature["input_values"][0]} for feature in features] +... label_features = [{"input_ids": feature["labels"]} for feature in features] + +... batch = self.processor.pad(input_features, padding=self.padding, return_tensors="pt") + +... labels_batch = self.processor.pad(labels=label_features, padding=self.padding, return_tensors="pt") + +... # ํŒจ๋”ฉ์— ๋Œ€ํ•ด ์†์‹ค์„ ์ ์šฉํ•˜์ง€ ์•Š๋„๋ก -100์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค +... labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) + +... batch["labels"] = labels + +... return batch +``` + +์ด์ œ `DataCollatorForCTCWithPadding`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> data_collator = DataCollatorCTCWithPadding(processor=processor, padding="longest") +``` + +## ํ‰๊ฐ€ํ•˜๊ธฐ[[evaluate]] + +ํ›ˆ๋ จ ์ค‘์— ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ํฌํ•จํ•˜๋ฉด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ๋น ๋ฅด๊ฒŒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด ์ž‘์—…์—์„œ๋Š” [๋‹จ์–ด ์˜ค๋ฅ˜์œจ(Word Error Rate, WER)](https://huggingface.co/spaces/evaluate-metric/wer) ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. +(ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๐Ÿค— Evaluate [๋‘˜๋Ÿฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”): + +```py +>>> import evaluate + +>>> wer = evaluate.load("wer") +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์˜ˆ์ธก๊ฐ’๊ณผ ๋ ˆ์ด๋ธ”์„ [`~evaluate.EvaluationModule.compute`]์— ์ „๋‹ฌํ•˜์—ฌ WER์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: + +```py +>>> import numpy as np + + +>>> def compute_metrics(pred): +... pred_logits = pred.predictions +... pred_ids = np.argmax(pred_logits, axis=-1) + +... pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id + +... pred_str = processor.batch_decode(pred_ids) +... label_str = processor.batch_decode(pred.label_ids, group_tokens=False) + +... wer = wer.compute(predictions=pred_str, references=label_str) + +... return {"wer": wer} +``` + +์ด์ œ `compute_metrics` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฉฐ, ํ›ˆ๋ จ์„ ์„ค์ •ํ•  ๋•Œ ์ด ํ•จ์ˆ˜๋กœ ๋˜๋Œ์•„์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +## ํ›ˆ๋ จํ•˜๊ธฐ[[train]] + + + + + +[`Trainer`]๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด, [์—ฌ๊ธฐ](../training#train-with-pytorch-trainer)์—์„œ ๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! + + + +์ด์ œ ๋ชจ๋ธ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! [`AutoModelForCTC`]๋กœ Wav2Vec2๋ฅผ ๊ฐ€์ ธ์˜ค์„ธ์š”. `ctc_loss_reduction` ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ CTC ์†์‹ค์— ์ ์šฉํ•  ์ถ•์†Œ(reduction) ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•˜์„ธ์š”. ๊ธฐ๋ณธ๊ฐ’์ธ ํ•ฉ๊ณ„ ๋Œ€์‹  ํ‰๊ท ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์€ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForCTC, TrainingArguments, Trainer + +>>> model = AutoModelForCTC.from_pretrained( +... "facebook/wav2vec2-base", +... ctc_loss_reduction="mean", +... pad_token_id=processor.tokenizer.pad_token_id, +... ) +``` + +์ด์ œ ์„ธ ๋‹จ๊ณ„๋งŒ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค: + +1. [`TrainingArguments`]์—์„œ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜์„ธ์š”. `output_dir`์€ ๋ชจ๋ธ์„ ์ €์žฅํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” ์œ ์ผํ•œ ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. `push_to_hub=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋ธ์„ Hub์— ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). [`Trainer`]๋Š” ๊ฐ ์—ํญ๋งˆ๋‹ค WER์„ ํ‰๊ฐ€ํ•˜๊ณ  ํ›ˆ๋ จ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. +2. ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ, ํ† ํฌ๋‚˜์ด์ €, ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ, `compute_metrics` ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ [`Trainer`]์— ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”. +3. [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์„ธ์š”. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_asr_mind_model", +... per_device_train_batch_size=8, +... gradient_accumulation_steps=2, +... learning_rate=1e-5, +... warmup_steps=500, +... max_steps=2000, +... gradient_checkpointing=True, +... fp16=True, +... group_by_length=True, +... eval_strategy="steps", +... per_device_eval_batch_size=8, +... save_steps=1000, +... eval_steps=1000, +... logging_steps=25, +... load_best_model_at_end=True, +... metric_for_best_model="wer", +... greater_is_better=False, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=processor.feature_extractor, +... data_collator=data_collator, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋‘๊ฐ€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`~transformers.Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ Hub์— ๊ณต์œ ํ•˜์„ธ์š”: + +```py +>>> trainer.push_to_hub() +``` + + + + + +์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋” ์ž์„ธํ•œ ์˜ˆ์ œ๋Š” ์˜์–ด ์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ์œ„ํ•œ [๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ](https://huggingface.co/blog/fine-tune-wav2vec2-english)์™€ ๋‹ค๊ตญ์–ด ์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ์œ„ํ•œ [ํฌ์ŠคํŠธ](https://huggingface.co/blog/fine-tune-xlsr-wav2vec2)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +## ์ถ”๋ก ํ•˜๊ธฐ[[inference]] + +์ข‹์•„์š”, ์ด์ œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์œผ๋‹ˆ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค์„ธ์š”. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์˜ค๋””์˜ค ํŒŒ์ผ์˜ ์ƒ˜ํ”Œ๋ง ๋น„์œจ์„ ๋ชจ๋ธ์˜ ์ƒ˜ํ”Œ๋ง ๋ ˆ์ดํŠธ์— ๋งž๊ฒŒ ๋ฆฌ์ƒ˜ํ”Œ๋งํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์„ธ์š”! + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", "en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์‹œํ—˜ํ•ด๋ณด๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ์œ„ํ•œ `pipeline`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> from transformers import pipeline + +>>> transcriber = pipeline("automatic-speech-recognition", model="stevhliu/my_awesome_asr_minds_model") +>>> transcriber(audio_file) +{'text': 'I WOUD LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'} +``` + + + +ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๊ฐ€ ๊ฝค ๊ดœ์ฐฎ์ง€๋งŒ ๋” ์ข‹์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋” ๋งŽ์€ ์˜ˆ์ œ๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์„ธ์š”! + + + +`pipeline`์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์žฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + + + +์˜ค๋””์˜ค ํŒŒ์ผ๊ณผ ํ…์ŠคํŠธ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ณ  PyTorch ํ…์„œ๋กœ `input`์„ ๋ฐ˜ํ™˜ํ•  ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค์„ธ์š”: + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +์ž…๋ ฅ์„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ณ  ๋กœ์ง“์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoModelForCTC + +>>> model = AutoModelForCTC.from_pretrained("stevhliu/my_awesome_asr_mind_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์˜ `input_ids`๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ํ”„๋กœ์„ธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก๋œ `input_ids`๋ฅผ ๋‹ค์‹œ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•˜์„ธ์š”: + +```py +>>> import torch + +>>> predicted_ids = torch.argmax(logits, dim=-1) +>>> transcription = processor.batch_decode(predicted_ids) +>>> transcription +['I WOUL LIKE O SET UP JOINT ACOUNT WTH Y PARTNER'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/audio_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/audio_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..2defa691edef75ffaefb7a66a3d8ebfc3a95ae5e --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/audio_classification.md @@ -0,0 +1,324 @@ + + +# ์˜ค๋””์˜ค ๋ถ„๋ฅ˜[[audio_classification]] + +[[open-in-colab]] + + + +์˜ค๋””์˜ค ๋ถ„๋ฅ˜๋Š” ํ…์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ํด๋ž˜์Šค ๋ ˆ์ด๋ธ” ์ถœ๋ ฅ์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ํ…์ŠคํŠธ ์ž…๋ ฅ ๋Œ€์‹  ์›์‹œ ์˜ค๋””์˜ค ํŒŒํ˜•์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ค๋””์˜ค ๋ถ„๋ฅ˜์˜ ์‹ค์ œ ์ ์šฉ ๋ถ„์•ผ์—๋Š” ํ™”์ž์˜ ์˜๋„ ํŒŒ์•…, ์–ธ์–ด ๋ถ„๋ฅ˜, ์†Œ๋ฆฌ๋กœ ๋™๋ฌผ ์ข…์„ ์‹๋ณ„ํ•˜๋Š” ๊ฒƒ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ฌธ์„œ์—์„œ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +1. [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base)๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ํ™”์ž์˜ ์˜๋„๋ฅผ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. +2. ์ถ”๋ก ์— ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/audio-classification)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate +``` + +๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ๊น…ํŽ˜์ด์Šค ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋˜๋ฉด ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## MInDS-14 ๋ฐ์ดํ„ฐ์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[load_minds_14_dataset]] + +๋จผ์ € ๐Ÿค— Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ MinDS-14 ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from datasets import load_dataset, Audio + +>>> minds = load_dataset("PolyAI/minds14", name="en-US", split="train") +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ `train` ๋ถ„ํ• ์„ [`~datasets.Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋” ์ž‘์€ ํ›ˆ๋ จ ๋ฐ ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ์†Œ๋น„ํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€ ์‹คํ—˜ํ•˜๊ณ  ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> minds = minds.train_test_split(test_size=0.2) +``` + +์ด์ œ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ์‚ดํŽด๋ณผ๊ฒŒ์š”: + +```py +>>> minds +DatasetDict({ + train: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 450 + }) + test: Dataset({ + features: ['path', 'audio', 'transcription', 'english_transcription', 'intent_class', 'lang_id'], + num_rows: 113 + }) +}) +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์—๋Š” `lang_id` ๋ฐ `english_transcription`๊ณผ ๊ฐ™์€ ์œ ์šฉํ•œ ์ •๋ณด๊ฐ€ ๋งŽ์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” `audio` ๋ฐ `intent_class`์— ์ค‘์ ์„ ๋‘˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์—ด์€ [`~datasets.Dataset.remove_columns`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> minds = minds.remove_columns(["path", "transcription", "english_transcription", "lang_id"]) +``` + +์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> minds["train"][0] +{'audio': {'array': array([ 0. , 0. , 0. , ..., -0.00048828, + -0.00024414, -0.00024414], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 8000}, + 'intent_class': 2} +``` + +๋‘ ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +- `audio`: ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌ์ƒ˜ํ”Œ๋งํ•˜๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ์Œ์„ฑ ์‹ ํ˜ธ์˜ 1์ฐจ์› `๋ฐฐ์—ด`์ž…๋‹ˆ๋‹ค. +- `intent_class`: ํ™”์ž์˜ ์˜๋„์— ๋Œ€ํ•œ ํด๋ž˜์Šค ID๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + +๋ชจ๋ธ์ด ๋ ˆ์ด๋ธ” ID์—์„œ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์„ ์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋ ˆ์ด๋ธ” ์ด๋ฆ„์„ ์ •์ˆ˜๋กœ ๋งคํ•‘ํ•˜๋Š” ์‚ฌ์ „์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋งคํ•‘ํ•˜๋Š” ์‚ฌ์ „์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค: + +```py +>>> labels = minds["train"].features["intent_class"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +์ด์ œ ๋ ˆ์ด๋ธ” ID๋ฅผ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> id2label[str(2)] +'app_error' +``` + +## ์ „์ฒ˜๋ฆฌ[[preprocess]] + +๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์˜ค๋””์˜ค ์‹ ํ˜ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Wav2Vec2 ํŠน์ง• ์ถ”์ถœ๊ธฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") +``` + +MinDS-14 ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ์ƒ˜ํ”Œ๋ง ์†๋„๋Š” 8khz์ด๋ฏ€๋กœ(์ด ์ •๋ณด๋Š” [๋ฐ์ดํ„ฐ์„ธํŠธ ์นด๋“œ](https://huggingface.co/datasets/PolyAI/minds14)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค), ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Wav2Vec2 ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ 16kHz๋กœ ๋ฆฌ์ƒ˜ํ”Œ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> minds = minds.cast_column("audio", Audio(sampling_rate=16_000)) +>>> minds["train"][0] +{'audio': {'array': array([ 2.2098757e-05, 4.6582241e-05, -2.2803260e-05, ..., + -2.8419291e-04, -2.3305941e-04, -1.1425107e-04], dtype=float32), + 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~APP_ERROR/602b9a5fbb1e6d0fbce91f52.wav', + 'sampling_rate': 16000}, + 'intent_class': 2} +``` + +์ด์ œ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: + +1. ๊ฐ€์ ธ์˜ฌ `์˜ค๋””์˜ค` ์—ด์„ ํ˜ธ์ถœํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๋ฆฌ์ƒ˜ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค. +2. ์˜ค๋””์˜ค ํŒŒ์ผ์˜ ์ƒ˜ํ”Œ๋ง ์†๋„๊ฐ€ ๋ชจ๋ธ์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์˜ค๋””์˜ค ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ๋ง ์†๋„์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋Š” Wav2Vec2 [๋ชจ๋ธ ์นด๋“œ](https://huggingface.co/facebook/wav2vec2-base)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +3. ๊ธด ์ž…๋ ฅ์ด ์ž˜๋ฆฌ์ง€ ์•Š๊ณ  ์ผ๊ด„ ์ฒ˜๋ฆฌ๋˜๋„๋ก ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> def preprocess_function(examples): +... audio_arrays = [x["array"] for x in examples["audio"]] +... inputs = feature_extractor( +... audio_arrays, sampling_rate=feature_extractor.sampling_rate, max_length=16000, truncation=True +... ) +... return inputs +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์ „์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•˜๋ ค๋ฉด ๐Ÿค— Datasets [`~datasets.Dataset.map`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. `batched=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์˜ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋ฉด `map`์˜ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•˜์ง€ ์•Š์€ ์—ด์„ ์ œ๊ฑฐํ•˜๊ณ  `intent_class`์˜ ์ด๋ฆ„์„ ๋ชจ๋ธ์ด ์˜ˆ์ƒํ•˜๋Š” ์ด๋ฆ„์ธ `label`๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> encoded_minds = minds.map(preprocess_function, remove_columns="audio", batched=True) +>>> encoded_minds = encoded_minds.rename_column("intent_class", "label") +``` + +## ํ‰๊ฐ€ํ•˜๊ธฐ[[evaluate]] + +ํ›ˆ๋ จ ์ค‘์— ๋ฉ”ํŠธ๋ฆญ์„ ํฌํ•จํ•˜๋ฉด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์—์„œ๋Š” [accuracy(์ •ํ™•๋„)](https://huggingface.co/spaces/evaluate-metric/accuracy) ๋ฉ”ํŠธ๋ฆญ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค(๋ฉ”ํŠธ๋ฆญ์„ ๊ฐ€์ ธ์˜ค๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๐Ÿค— Evalutate [๋น ๋ฅธ ๋‘˜๋Ÿฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour) ์ฐธ์กฐํ•˜์„ธ์š”): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ [`~evaluate.EvaluationModule.compute`]์— ์ „๋‹ฌํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions = np.argmax(eval_pred.predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=eval_pred.label_ids) +``` + +์ด์ œ `compute_metrics` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฉฐ, ํŠธ๋ ˆ์ด๋‹์„ ์„ค์ •ํ•  ๋•Œ ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +## ํ›ˆ๋ จ[[train]] + + + + + +[`Trainer`]๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐ ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ [์—ฌ๊ธฐ](../training#train-with-pytorch-trainer)์„ ์‚ดํŽด๋ณด์„ธ์š”! + + + +์ด์ œ ๋ชจ๋ธ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! [`AutoModelForAudioClassification`]์„ ์ด์šฉํ•ด์„œ Wav2Vec2๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ƒ๋˜๋Š” ๋ ˆ์ด๋ธ” ์ˆ˜์™€ ๋ ˆ์ด๋ธ” ๋งคํ•‘์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForAudioClassification, TrainingArguments, Trainer + +>>> num_labels = len(id2label) +>>> model = AutoModelForAudioClassification.from_pretrained( +... "facebook/wav2vec2-base", num_labels=num_labels, label2id=label2id, id2label=id2label +... ) +``` + +์ด์ œ ์„ธ ๋‹จ๊ณ„๋งŒ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค: + +1. ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ [`TrainingArguments`]์— ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์œ ์ผํ•œ ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ชจ๋ธ์„ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” `output_dir`์ž…๋‹ˆ๋‹ค. `push_to_hub = True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ด ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค(๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด ํ—ˆ๊น… ํŽ˜์ด์Šค์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). ๊ฐ ์—ํญ์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค [`Trainer`]๊ฐ€ ์ •ํ™•๋„๋ฅผ ํ‰๊ฐ€ํ•˜๊ณ  ํ›ˆ๋ จ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. +2. ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ, ํ† ํฌ๋‚˜์ด์ €, ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ, `compute_metrics` ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ํ›ˆ๋ จ ์ธ์ž๋ฅผ [`Trainer`]์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +3. [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_mind_model", +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=3e-5, +... per_device_train_batch_size=32, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=32, +... num_train_epochs=10, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=encoded_minds["train"], +... eval_dataset=encoded_minds["test"], +... processing_class=feature_extractor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋“  ์‚ฌ๋žŒ์ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`~transformers.Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜์„ธ์š”: + +```py +>>> trainer.push_to_hub() +``` + + + + + +For a more in-depth example of how to finetune a model for audio classification, take a look at the corresponding [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb). + + + +## ์ถ”๋ก [[inference]] + +์ด์ œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์œผ๋‹ˆ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ถ”๋ก ์„ ์‹คํ–‰ํ•  ์˜ค๋””์˜ค ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์˜ค๋””์˜ค ํŒŒ์ผ์˜ ์ƒ˜ํ”Œ๋ง ์†๋„๋ฅผ ๋ชจ๋ธ์˜ ์ƒ˜ํ”Œ๋ง ์†๋„์™€ ์ผ์น˜ํ•˜๋„๋ก ๋ฆฌ์ƒ˜ํ”Œ๋งํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์„ธ์š”! + +```py +>>> from datasets import load_dataset, Audio + +>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") +>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) +>>> sampling_rate = dataset.features["audio"].sampling_rate +>>> audio_file = dataset[0]["audio"]["path"] +``` + +์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์‹œํ—˜ํ•ด ๋ณด๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`pipeline`]์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋””์˜ค ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ `pipeline`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("audio-classification", model="stevhliu/my_awesome_minds_model") +>>> classifier(audio_file) +[ + {'score': 0.09766869246959686, 'label': 'cash_deposit'}, + {'score': 0.07998877018690109, 'label': 'app_error'}, + {'score': 0.0781070664525032, 'label': 'joint_account'}, + {'score': 0.07667109370231628, 'label': 'pay_bill'}, + {'score': 0.0755252093076706, 'label': 'balance'} +] +``` + +์›ํ•˜๋Š” ๊ฒฝ์šฐ `pipeline`์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ณต์ œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + + + +ํŠน์ง• ์ถ”์ถœ๊ธฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์˜ค๋””์˜ค ํŒŒ์ผ์„ ์ „์ฒ˜๋ฆฌํ•˜๊ณ  `์ž…๋ ฅ`์„ PyTorch ํ…์„œ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoFeatureExtractor + +>>> feature_extractor = AutoFeatureExtractor.from_pretrained("stevhliu/my_awesome_minds_model") +>>> inputs = feature_extractor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") +``` + +๋ชจ๋ธ์— ์ž…๋ ฅ์„ ์ „๋‹ฌํ•˜๊ณ  ๋กœ์ง“์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForAudioClassification + +>>> model = AutoModelForAudioClassification.from_pretrained("stevhliu/my_awesome_minds_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ ๋ชจ๋ธ์˜ `id2label` ๋งคํ•‘์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> import torch + +>>> predicted_class_ids = torch.argmax(logits).item() +>>> predicted_label = model.config.id2label[predicted_class_ids] +>>> predicted_label +'cash_deposit' +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/document_question_answering.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/document_question_answering.md new file mode 100644 index 0000000000000000000000000000000000000000..6c2d04f4ee859836401cf1e5d9c37c12c77f5827 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/document_question_answering.md @@ -0,0 +1,476 @@ + + +# ๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต(Document Question Answering) [[document_question_answering]] + +[[open-in-colab]] + +๋ฌธ์„œ ์‹œ๊ฐ์  ์งˆ์˜ ์‘๋‹ต(Document Visual Question Answering)์ด๋ผ๊ณ ๋„ ํ•˜๋Š” +๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต(Document Question Answering)์€ ๋ฌธ์„œ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€์„ ์ฃผ๋Š” ํƒœ์Šคํฌ์ž…๋‹ˆ๋‹ค. +์ด ํƒœ์Šคํฌ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋ฏธ์ง€์™€ ์งˆ๋ฌธ์˜ ์กฐํ•ฉ์ด๊ณ , ์ถœ๋ ฅ์€ ์ž์—ฐ์–ด๋กœ ๋œ ๋‹ต๋ณ€์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ํ…์ŠคํŠธ, ๋‹จ์–ด์˜ ์œ„์น˜(๋ฐ”์šด๋”ฉ ๋ฐ•์Šค), ์ด๋ฏธ์ง€ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” ๋‹ค์Œ ๋‚ด์šฉ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค: + +- [DocVQA dataset](https://huggingface.co/datasets/nielsr/docvqa_1200_examples_donut)์„ ์‚ฌ์šฉํ•ด [LayoutLMv2](../model_doc/layoutlmv2) ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ +- ์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/image-to-text)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +LayoutLMv2๋Š” ํ† ํฐ์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰์ธต ์œ„์— ์งˆ์˜ ์‘๋‹ต ํ—ค๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋‹ต๋ณ€์˜ ์‹œ์ž‘ ํ† ํฐ๊ณผ ๋ ํ† ํฐ์˜ ์œ„์น˜๋ฅผ ์˜ˆ์ธกํ•จ์œผ๋กœ์จ ๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต ํƒœ์Šคํฌ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌธ๋งฅ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๋Š” ์ถ”์ถœํ˜• ์งˆ์˜ ์‘๋‹ต(Extractive question answering)์œผ๋กœ ๋ฌธ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. +๋ฌธ๋งฅ์€ OCR ์—”์ง„์˜ ์ถœ๋ ฅ์—์„œ ๊ฐ€์ ธ์˜ค๋ฉฐ, ์—ฌ๊ธฐ์„œ๋Š” Google์˜ Tesseract๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. LayoutLMv2๋Š” detectron2, torchvision ๋ฐ ํ…Œ์„œ๋ž™ํŠธ๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. + +```bash +pip install -q transformers datasets +``` + +```bash +pip install 'git+https://github.com/facebookresearch/detectron2.git' +pip install torchvision +``` + +```bash +sudo apt install tesseract-ocr +pip install -q pytesseract +``` + +ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๋ชจ๋‘ ์„ค์น˜ํ•œ ํ›„ ๋Ÿฐํƒ€์ž„์„ ๋‹ค์‹œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. + +์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋‹น์‹ ์˜ ๋ชจ๋ธ์„ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•ด์„œ ๋ชจ๋ธ์„ ๐Ÿค— Hub์— ์—…๋กœ๋“œํ•˜์„ธ์š”. +ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์‹คํ–‰๋˜๋ฉด, ๋กœ๊ทธ์ธ์„ ์œ„ํ•ด ํ† ํฐ์„ ์ž…๋ ฅํ•˜์„ธ์š”: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +๋ช‡ ๊ฐ€์ง€ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```py +>>> model_checkpoint = "microsoft/layoutlmv2-base-uncased" +>>> batch_size = 4 +``` + +## ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ [[load-the-data]] + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๐Ÿค— Hub์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ „์ฒ˜๋ฆฌ๋œ DocVQA์˜ ์ž‘์€ ์ƒ˜ํ”Œ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +DocVQA์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, [DocVQA homepage](https://rrc.cvc.uab.es/?ch=17)์— ๊ฐ€์ž… ํ›„ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ–ˆ๋‹ค๋ฉด, ์ด ๊ฐ€์ด๋“œ๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด [๐Ÿค— dataset์— ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•](https://huggingface.co/docs/datasets/loading#local-and-remote-files)์„ ํ™•์ธํ•˜์„ธ์š”. + +```py +>>> from datasets import load_dataset + +>>> dataset = load_dataset("nielsr/docvqa_1200_examples") +>>> dataset +DatasetDict({ + train: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 1000 + }) + test: Dataset({ + features: ['id', 'image', 'query', 'answers', 'words', 'bounding_boxes', 'answer'], + num_rows: 200 + }) +}) +``` + +๋ณด์‹œ๋‹ค์‹œํ”ผ, ๋ฐ์ดํ„ฐ ์„ธํŠธ๋Š” ์ด๋ฏธ ํ›ˆ๋ จ ์„ธํŠธ์™€ ํ…Œ์ŠคํŠธ ์„ธํŠธ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์ž‘์œ„๋กœ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด์„œ ํŠน์„ฑ์„ ํ™•์ธํ•ด๋ณด์„ธ์š”. + +```py +>>> dataset["train"].features +``` + +๊ฐ ํ•„๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: +* `id`: ์˜ˆ์ œ์˜ id +* `image`: ๋ฌธ์„œ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•˜๋Š” PIL.Image.Image ๊ฐ์ฒด +* `query`: ์งˆ๋ฌธ ๋ฌธ์ž์—ด - ์—ฌ๋Ÿฌ ์–ธ์–ด์˜ ์ž์—ฐ์–ด๋กœ ๋œ ์งˆ๋ฌธ +* `answers`: ์‚ฌ๋žŒ์ด ์ฃผ์„์„ ๋‹จ ์ •๋‹ต ๋ฆฌ์ŠคํŠธ +* `words` and `bounding_boxes`: OCR์˜ ๊ฒฐ๊ณผ๊ฐ’๋“ค์ด๋ฉฐ ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์˜ˆ์ • +* `answer`: ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ์ผ์น˜ํ•˜๋Š” ๋‹ต๋ณ€์ด๋ฉฐ ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์˜ˆ์ • + +์˜์–ด๋กœ ๋œ ์งˆ๋ฌธ๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‹ค๋ฅธ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ํฌํ•จํ•˜๋Š” `answer` ํŠน์„ฑ์„ ์‚ญ์ œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +๊ทธ๋ฆฌ๊ณ  ์ฃผ์„ ์ž‘์„ฑ์ž๊ฐ€ ์ œ๊ณตํ•œ ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ์ฒซ ๋ฒˆ์งธ ๋‹ต๋ณ€์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋˜๋Š” ๋ฌด์ž‘์œ„๋กœ ์ƒ˜ํ”Œ์„ ์ถ”์ถœํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> updated_dataset = dataset.map(lambda example: {"question": example["query"]["en"]}, remove_columns=["query"]) +>>> updated_dataset = updated_dataset.map( +... lambda example: {"answer": example["answers"][0]}, remove_columns=["answer", "answers"] +... ) +``` + +์ด ๊ฐ€์ด๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” LayoutLMv2 ์ฒดํฌํฌ์ธํŠธ๋Š” `max_position_embeddings = 512`๋กœ ํ›ˆ๋ จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค(์ด ์ •๋ณด๋Š” [์ฒดํฌํฌ์ธํŠธ์˜ `config.json` ํŒŒ์ผ](https://huggingface.co/microsoft/layoutlmv2-base-uncased/blob/main/config.json#L18)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). +๋ฐ”๋กœ ์˜ˆ์ œ๋ฅผ ์ž˜๋ผ๋‚ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ธด ๋ฌธ์„œ์˜ ๋์— ๋‹ต๋ณ€์ด ์žˆ์–ด ์ž˜๋ฆฌ๋Š” ์ƒํ™ฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๊ธฐ์„œ๋Š” ์ž„๋ฒ ๋”ฉ์ด 512๋ณด๋‹ค ๊ธธ์–ด์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์žˆ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์„œ๊ฐ€ ๊ธด ๊ฒฝ์šฐ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค - ์ž์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ด [๋…ธํŠธ๋ถ](https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb)์„ ํ™•์ธํ•˜์„ธ์š”. + +```py +>>> updated_dataset = updated_dataset.filter(lambda x: len(x["words"]) + len(x["question"].split()) < 512) +``` + +์ด ์‹œ์ ์—์„œ ์ด ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ OCR ํŠน์„ฑ๋„ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. OCR ํŠน์„ฑ์€ ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ, ์ด ๊ฐ€์ด๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ ์š”๊ตฌ ์‚ฌํ•ญ๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด ํŠน์„ฑ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ๋ถ€ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +๋Œ€์‹ , ์›๋ณธ ๋ฐ์ดํ„ฐ์— [`LayoutLMv2Processor`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ OCR ๋ฐ ํ† ํฐํ™”๋ฅผ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ์ด ์š”๊ตฌํ•˜๋Š” ์ž…๋ ฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋ฏธ์ง€๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด, [`LayoutLMv2` model documentation](../model_doc/layoutlmv2)์—์„œ ๋ชจ๋ธ์ด ์š”๊ตฌํ•˜๋Š” ์ž…๋ ฅ ํฌ๋งท์„ ํ™•์ธํ•ด๋ณด์„ธ์š”. + +```py +>>> updated_dataset = updated_dataset.remove_columns("words") +>>> updated_dataset = updated_dataset.remove_columns("bounding_boxes") +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฐ์ดํ„ฐ ํƒ์ƒ‰์„ ์™„๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค. + +```py +>>> updated_dataset["train"][11]["image"] +``` + +
+ DocVQA Image Example +
+ +## ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ [[preprocess-the-data]] + + +๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต ํƒœ์Šคํฌ๋Š” ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ํƒœ์Šคํฌ์ด๋ฉฐ, ๊ฐ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์˜ ์ž…๋ ฅ์ด ๋ชจ๋ธ์˜ ์š”๊ตฌ์— ๋งž๊ฒŒ ์ „์ฒ˜๋ฆฌ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ์™€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๊ฒฐํ•ฉํ•œ [`LayoutLMv2Processor`]๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```py +>>> from transformers import AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(model_checkpoint) +``` + +### ๋ฌธ์„œ ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ [[preprocessing-document-images]] + +๋จผ์ €, ํ”„๋กœ์„ธ์„œ์˜ `image_processor`๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฌธ์„œ ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +๊ธฐ๋ณธ๊ฐ’์œผ๋กœ, ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋Š” ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ 224x224๋กœ ์กฐ์ •ํ•˜๊ณ  ์ƒ‰์ƒ ์ฑ„๋„์˜ ์ˆœ์„œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•œ ํ›„ ๋‹จ์–ด์™€ ์ •๊ทœํ™”๋œ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ํ…Œ์„œ๋ž™ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด OCR๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. +์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ๊ณผ ๊ธฐ๋ณธ๊ฐ’์€ ์™„์ „ํžˆ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ฐฐ์น˜์— ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜๊ณ  OCR์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> image_processor = processor.image_processor + + +>>> def get_ocr_words_and_boxes(examples): +... images = [image.convert("RGB") for image in examples["image"]] +... encoded_inputs = image_processor(images) + +... examples["image"] = encoded_inputs.pixel_values +... examples["words"] = encoded_inputs.words +... examples["boxes"] = encoded_inputs.boxes + +... return examples +``` + +์ด ์ „์ฒ˜๋ฆฌ๋ฅผ ๋ฐ์ดํ„ฐ ์„ธํŠธ ์ „์ฒด์— ๋น ๋ฅด๊ฒŒ ์ ์šฉํ•˜๋ ค๋ฉด [`~datasets.Dataset.map`]๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. + +```py +>>> dataset_with_ocr = updated_dataset.map(get_ocr_words_and_boxes, batched=True, batch_size=2) +``` + +### ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ [[preprocessing-text-data]] + +์ด๋ฏธ์ง€์— OCR์„ ์ ์šฉํ–ˆ์œผ๋ฉด ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ํ…์ŠคํŠธ ๋ถ€๋ถ„์„ ๋ชจ๋ธ์— ๋งž๊ฒŒ ์ธ์ฝ”๋”ฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +์ด ์ธ์ฝ”๋”ฉ์—๋Š” ์ด์ „ ๋‹จ๊ณ„์—์„œ ๊ฐ€์ ธ์˜จ ๋‹จ์–ด์™€ ๋ฐ•์Šค๋ฅผ ํ† ํฐ ์ˆ˜์ค€์˜ `input_ids`, `attention_mask`, `token_type_ids` ๋ฐ `bbox`๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. +ํ…์ŠคํŠธ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ํ”„๋กœ์„ธ์„œ์˜ `tokenizer`๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> tokenizer = processor.tokenizer +``` + +์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์ „์ฒ˜๋ฆฌ ์™ธ์—๋„ ๋ชจ๋ธ์„ ์œ„ํ•ด ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿค— Transformers์˜ `xxxForQuestionAnswering` ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, ๋ ˆ์ด๋ธ”์€ `start_positions`์™€ `end_positions`๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ์–ด๋–ค ํ† ํฐ์ด ๋‹ต๋ณ€์˜ ์‹œ์ž‘๊ณผ ๋์— ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. + +๋ ˆ์ด๋ธ” ์ถ”๊ฐ€๋ฅผ ์œ„ํ•ด์„œ, ๋จผ์ € ๋” ํฐ ๋ฆฌ์ŠคํŠธ(๋‹จ์–ด ๋ฆฌ์ŠคํŠธ)์—์„œ ํ•˜์œ„ ๋ฆฌ์ŠคํŠธ(๋‹จ์–ด๋กœ ๋ถ„ํ• ๋œ ๋‹ต๋ณ€)์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ํ—ฌํผ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. + +์ด ํ•จ์ˆ˜๋Š” `words_list`์™€ `answer_list`, ์ด๋ ‡๊ฒŒ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ `words_list`๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ `words_list`์˜ ํ˜„์žฌ ๋‹จ์–ด(words_list[i])๊ฐ€ `answer_list`์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด(answer_list[0])์™€ ๊ฐ™์€์ง€, +ํ˜„์žฌ ๋‹จ์–ด์—์„œ ์‹œ์ž‘ํ•ด `answer_list`์™€ ๊ฐ™์€ ๊ธธ์ด๋งŒํผ์˜ `words_list`์˜ ํ•˜์œ„ ๋ฆฌ์ŠคํŠธ๊ฐ€ `answer_list`์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. +์ด ์กฐ๊ฑด์ด ์ฐธ์ด๋ผ๋ฉด ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์„ ๋ฐœ๊ฒฌํ–ˆ์Œ์„ ์˜๋ฏธํ•˜๋ฉฐ, ํ•จ์ˆ˜๋Š” ์ผ์น˜ ํ•ญ๋ชฉ, ์‹œ์ž‘ ์ธ๋ฑ์Šค(idx) ๋ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค(idx + len(answer_list) - 1)๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์ด ๋‘ ๊ฐœ ์ด์ƒ ๋ฐœ๊ฒฌ๋˜๋ฉด ํ•จ์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์ด ์—†๋‹ค๋ฉด ํ•จ์ˆ˜๋Š” (`None`, 0, 0)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> def subfinder(words_list, answer_list): +... matches = [] +... start_indices = [] +... end_indices = [] +... for idx, i in enumerate(range(len(words_list))): +... if words_list[i] == answer_list[0] and words_list[i : i + len(answer_list)] == answer_list: +... matches.append(answer_list) +... start_indices.append(idx) +... end_indices.append(idx + len(answer_list) - 1) +... if matches: +... return matches[0], start_indices[0], end_indices[0] +... else: +... return None, 0, 0 +``` + +์ด ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์ •๋‹ต์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”์ง€ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> example = dataset_with_ocr["train"][1] +>>> words = [word.lower() for word in example["words"]] +>>> match, word_idx_start, word_idx_end = subfinder(words, example["answer"].lower().split()) +>>> print("Question: ", example["question"]) +>>> print("Words:", words) +>>> print("Answer: ", example["answer"]) +>>> print("start_index", word_idx_start) +>>> print("end_index", word_idx_end) +Question: Who is in cc in this letter? +Words: ['wie', 'baw', 'brown', '&', 'williamson', 'tobacco', 'corporation', 'research', '&', 'development', 'internal', 'correspondence', 'to:', 'r.', 'h.', 'honeycutt', 'ce:', 't.f.', 'riehl', 'from:', '.', 'c.j.', 'cook', 'date:', 'may', '8,', '1995', 'subject:', 'review', 'of', 'existing', 'brainstorming', 'ideas/483', 'the', 'major', 'function', 'of', 'the', 'product', 'innovation', 'graup', 'is', 'to', 'develop', 'marketable', 'nove!', 'products', 'that', 'would', 'be', 'profitable', 'to', 'manufacture', 'and', 'sell.', 'novel', 'is', 'defined', 'as:', 'of', 'a', 'new', 'kind,', 'or', 'different', 'from', 'anything', 'seen', 'or', 'known', 'before.', 'innovation', 'is', 'defined', 'as:', 'something', 'new', 'or', 'different', 'introduced;', 'act', 'of', 'innovating;', 'introduction', 'of', 'new', 'things', 'or', 'methods.', 'the', 'products', 'may', 'incorporate', 'the', 'latest', 'technologies,', 'materials', 'and', 'know-how', 'available', 'to', 'give', 'then', 'a', 'unique', 'taste', 'or', 'look.', 'the', 'first', 'task', 'of', 'the', 'product', 'innovation', 'group', 'was', 'to', 'assemble,', 'review', 'and', 'categorize', 'a', 'list', 'of', 'existing', 'brainstorming', 'ideas.', 'ideas', 'were', 'grouped', 'into', 'two', 'major', 'categories', 'labeled', 'appearance', 'and', 'taste/aroma.', 'these', 'categories', 'are', 'used', 'for', 'novel', 'products', 'that', 'may', 'differ', 'from', 'a', 'visual', 'and/or', 'taste/aroma', 'point', 'of', 'view', 'compared', 'to', 'canventional', 'cigarettes.', 'other', 'categories', 'include', 'a', 'combination', 'of', 'the', 'above,', 'filters,', 'packaging', 'and', 'brand', 'extensions.', 'appearance', 'this', 'category', 'is', 'used', 'for', 'novel', 'cigarette', 'constructions', 'that', 'yield', 'visually', 'different', 'products', 'with', 'minimal', 'changes', 'in', 'smoke', 'chemistry', 'two', 'cigarettes', 'in', 'cne.', 'emulti-plug', 'te', 'build', 'yaur', 'awn', 'cigarette.', 'eswitchable', 'menthol', 'or', 'non', 'menthol', 'cigarette.', '*cigarettes', 'with', 'interspaced', 'perforations', 'to', 'enable', 'smoker', 'to', 'separate', 'unburned', 'section', 'for', 'future', 'smoking.', 'ยซshort', 'cigarette,', 'tobacco', 'section', '30', 'mm.', 'ยซextremely', 'fast', 'buming', 'cigarette.', 'ยซnovel', 'cigarette', 'constructions', 'that', 'permit', 'a', 'significant', 'reduction', 'iretobacco', 'weight', 'while', 'maintaining', 'smoking', 'mechanics', 'and', 'visual', 'characteristics.', 'higher', 'basis', 'weight', 'paper:', 'potential', 'reduction', 'in', 'tobacco', 'weight.', 'ยซmore', 'rigid', 'tobacco', 'column;', 'stiffing', 'agent', 'for', 'tobacco;', 'e.g.', 'starch', '*colored', 'tow', 'and', 'cigarette', 'papers;', 'seasonal', 'promotions,', 'e.g.', 'pastel', 'colored', 'cigarettes', 'for', 'easter', 'or', 'in', 'an', 'ebony', 'and', 'ivory', 'brand', 'containing', 'a', 'mixture', 'of', 'all', 'black', '(black', 'paper', 'and', 'tow)', 'and', 'ail', 'white', 'cigarettes.', '499150498'] +Answer: T.F. Riehl +start_index 17 +end_index 18 +``` + +ํ•œํŽธ, ์œ„ ์˜ˆ์ œ๊ฐ€ ์ธ์ฝ”๋”ฉ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> encoding = tokenizer(example["question"], example["words"], example["boxes"]) +>>> tokenizer.decode(encoding["input_ids"]) +[CLS] who is in cc in this letter? [SEP] wie baw brown & williamson tobacco corporation research & development ... +``` + +์ด์ œ ์ธ์ฝ”๋”ฉ๋œ ์ž…๋ ฅ์—์„œ ์ •๋‹ต์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. +* `token_type_ids`๋Š” ์–ด๋–ค ํ† ํฐ์ด ์งˆ๋ฌธ์— ์†ํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ํ† ํฐ์ด ๋ฌธ์„œ์˜ ๋‹จ์–ด์— ํฌํ•จ๋˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. +* `tokenizer.cls_token_id` ์ž…๋ ฅ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์žˆ๋Š” ํŠน์ˆ˜ ํ† ํฐ์„ ์ฐพ๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค. +* `word_ids`๋Š” ์›๋ณธ `words`์—์„œ ์ฐพ์€ ๋‹ต๋ณ€์„ ์ „์ฒด ์ธ์ฝ”๋”ฉ๋œ ์ž…๋ ฅ์˜ ๋™์ผํ•œ ๋‹ต๊ณผ ์ผ์น˜์‹œํ‚ค๊ณ  ์ธ์ฝ”๋”ฉ๋œ ์ž…๋ ฅ์—์„œ ๋‹ต๋ณ€์˜ ์‹œ์ž‘/๋ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +์œ„ ๋‚ด์šฉ๋“ค์„ ์—ผ๋‘์— ๋‘๊ณ  ๋ฐ์ดํ„ฐ ์„ธํŠธ ์˜ˆ์ œ์˜ ๋ฐฐ์น˜๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> def encode_dataset(examples, max_length=512): +... questions = examples["question"] +... words = examples["words"] +... boxes = examples["boxes"] +... answers = examples["answer"] + +... # ์˜ˆ์ œ ๋ฐฐ์น˜๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ณ  start_positions์™€ end_positions๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค +... encoding = tokenizer(questions, words, boxes, max_length=max_length, padding="max_length", truncation=True) +... start_positions = [] +... end_positions = [] + +... # ๋ฐฐ์น˜์˜ ์˜ˆ์ œ๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค +... for i in range(len(questions)): +... cls_index = encoding["input_ids"][i].index(tokenizer.cls_token_id) + +... # ์˜ˆ์ œ์˜ words์—์„œ ๋‹ต๋ณ€์˜ ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค +... words_example = [word.lower() for word in words[i]] +... answer = answers[i] +... match, word_idx_start, word_idx_end = subfinder(words_example, answer.lower().split()) + +... if match: +... # ์ผ์น˜ํ•˜๋Š” ํ•ญ๋ชฉ์„ ๋ฐœ๊ฒฌํ•˜๋ฉด, `token_type_ids`๋ฅผ ์‚ฌ์šฉํ•ด ์ธ์ฝ”๋”ฉ์—์„œ ๋‹จ์–ด๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค +... token_type_ids = encoding["token_type_ids"][i] +... token_start_index = 0 +... while token_type_ids[token_start_index] != 1: +... token_start_index += 1 + +... token_end_index = len(encoding["input_ids"][i]) - 1 +... while token_type_ids[token_end_index] != 1: +... token_end_index -= 1 + +... word_ids = encoding.word_ids(i)[token_start_index : token_end_index + 1] +... start_position = cls_index +... end_position = cls_index + +... # words์˜ ๋‹ต๋ณ€ ์œ„์น˜์™€ ์ผ์น˜ํ•  ๋•Œ๊นŒ์ง€ word_ids๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  `token_start_index`๋ฅผ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค +... # ์ผ์น˜ํ•˜๋ฉด `token_start_index`๋ฅผ ์ธ์ฝ”๋”ฉ์—์„œ ๋‹ต๋ณ€์˜ `start_position`์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค +... for id in word_ids: +... if id == word_idx_start: +... start_position = token_start_index +... else: +... token_start_index += 1 + +... # ๋น„์Šทํ•˜๊ฒŒ, ๋์—์„œ ์‹œ์ž‘ํ•ด `word_ids`๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋‹ต๋ณ€์˜ `end_position`์„ ์ฐพ์Šต๋‹ˆ๋‹ค +... for id in word_ids[::-1]: +... if id == word_idx_end: +... end_position = token_end_index +... else: +... token_end_index -= 1 + +... start_positions.append(start_position) +... end_positions.append(end_position) + +... else: +... start_positions.append(cls_index) +... end_positions.append(cls_index) + +... encoding["image"] = examples["image"] +... encoding["start_positions"] = start_positions +... encoding["end_positions"] = end_positions + +... return encoding +``` + +์ด์ œ ์ด ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋‹ˆ ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์ธ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> encoded_train_dataset = dataset_with_ocr["train"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["train"].column_names +... ) +>>> encoded_test_dataset = dataset_with_ocr["test"].map( +... encode_dataset, batched=True, batch_size=2, remove_columns=dataset_with_ocr["test"].column_names +... ) +``` + +์ธ์ฝ”๋”ฉ๋œ ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ํŠน์„ฑ์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> encoded_train_dataset.features +{'image': Sequence(feature=Sequence(feature=Sequence(feature=Value(dtype='uint8', id=None), length=-1, id=None), length=-1, id=None), length=-1, id=None), + 'input_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None), + 'token_type_ids': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'attention_mask': Sequence(feature=Value(dtype='int8', id=None), length=-1, id=None), + 'bbox': Sequence(feature=Sequence(feature=Value(dtype='int64', id=None), length=-1, id=None), length=-1, id=None), + 'start_positions': Value(dtype='int64', id=None), + 'end_positions': Value(dtype='int64', id=None)} +``` + +## ํ‰๊ฐ€ [[evaluation]] + +๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต์„ ํ‰๊ฐ€ํ•˜๋ ค๋ฉด ์ƒ๋‹นํ•œ ์–‘์˜ ํ›„์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋งŽ์ด ๊ฑธ๋ฆฌ์ง€ ์•Š๋„๋ก ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ํ‰๊ฐ€ ๋‹จ๊ณ„๋ฅผ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. +[`Trainer`]๊ฐ€ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ํ‰๊ฐ€ ์†์‹ค(evaluation loss)์„ ๊ณ„์† ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋Œ€๋žต์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ถ”์ถœ์ (Extractive) ์งˆ์˜ ์‘๋‹ต์€ ๋ณดํ†ต F1/exact match ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด ํ‰๊ฐ€๋ฉ๋‹ˆ๋‹ค. +์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, Hugging Face course์˜ [Question Answering chapter](https://huggingface.co/course/chapter7/7?fw=pt#postprocessing)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. + +## ํ›ˆ๋ จ [[train]] + +์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ์ด ๊ฐ€์ด๋“œ์˜ ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ์œผ๋‹ˆ ์ด์ œ ๋‚˜๋งŒ์˜ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +ํ›ˆ๋ จ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค: +* ์ „์ฒ˜๋ฆฌ์—์„œ์˜ ๋™์ผํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด [`AutoModelForDocumentQuestionAnswering`]์œผ๋กœ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. +* [`TrainingArguments`]๋กœ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. +* ์˜ˆ์ œ๋ฅผ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” [`DefaultDataCollator`]๊ฐ€ ์ ๋‹นํ•ฉ๋‹ˆ๋‹ค. +* ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ, ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ(Data collator)์™€ ํ•จ๊ป˜ [`Trainer`]์— ํ›ˆ๋ จ ์ธ์ˆ˜๋“ค์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +* [`~Trainer.train`]์„ ํ˜ธ์ถœํ•ด์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained(model_checkpoint) +``` + +[`TrainingArguments`]์—์„œ `output_dir`์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ , ์ ์ ˆํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +๋ชจ๋ธ์„ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•˜๋ ค๋ฉด `push_to_hub`๋ฅผ `True`๋กœ ์„ค์ •ํ•˜์„ธ์š” (๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). +์ด ๊ฒฝ์šฐ `output_dir`์€ ๋ชจ๋ธ์˜ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ‘ธ์‹œํ•  ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฆ„์ด ๋ฉ๋‹ˆ๋‹ค. + +```py +>>> from transformers import TrainingArguments + +>>> # ๋ณธ์ธ์˜ ๋ ˆํฌ์ง€ํ† ๋ฆฌ ID๋กœ ๋ฐ”๊พธ์„ธ์š” +>>> repo_id = "MariaK/layoutlmv2-base-uncased_finetuned_docvqa" + +>>> training_args = TrainingArguments( +... output_dir=repo_id, +... per_device_train_batch_size=4, +... num_train_epochs=20, +... save_steps=200, +... logging_steps=50, +... eval_strategy="steps", +... learning_rate=5e-5, +... save_total_limit=2, +... remove_unused_columns=False, +... push_to_hub=True, +... ) +``` + +๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜์—ฌ ์˜ˆ์ œ๋ฅผ ํ•จ๊ป˜ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ชจ๋“  ๊ฒƒ์„ ํ•œ ๊ณณ์— ๋ชจ์•„ [`~Trainer.train`]์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import Trainer + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=encoded_train_dataset, +... eval_dataset=encoded_test_dataset, +... processing_class=processor, +... ) + +>>> trainer.train() +``` + +์ตœ์ข… ๋ชจ๋ธ์„ ๐Ÿค— Hub์— ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด, ๋ชจ๋ธ ์นด๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  `push_to_hub`๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> trainer.create_model_card() +>>> trainer.push_to_hub() +``` + +## ์ถ”๋ก  [[inference]] + +์ด์ œ LayoutLMv2 ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ๐Ÿค— Hub์— ์—…๋กœ๋“œํ–ˆ์œผ๋‹ˆ ์ถ”๋ก ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ณด๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`Pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. + +์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: +```py +>>> example = dataset["test"][2] +>>> question = example["query"]["en"] +>>> image = example["image"] +>>> print(question) +>>> print(example["answers"]) +'Who is โ€˜presidingโ€™ TRRF GENERAL SESSION (PART 1)?' +['TRRF Vice President', 'lee a. waller'] +``` + +๊ทธ ๋‹ค์Œ, ๋ชจ๋ธ๋กœ ๋ฌธ์„œ ์งˆ์˜ ์‘๋‹ต์„ ํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ดํ”„๋ผ์ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์ด๋ฏธ์ง€ + ์งˆ๋ฌธ ์กฐํ•ฉ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> from transformers import pipeline + +>>> qa_pipeline = pipeline("document-question-answering", model="MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> qa_pipeline(image, question) +[{'score': 0.9949808120727539, + 'answer': 'Lee A. Waller', + 'start': 55, + 'end': 57}] +``` + +์›ํ•œ๋‹ค๋ฉด ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ณต์ œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: +1. ์ด๋ฏธ์ง€์™€ ์งˆ๋ฌธ์„ ๊ฐ€์ ธ์™€ ๋ชจ๋ธ์˜ ํ”„๋กœ์„ธ์„œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋ธ์— ๋งž๊ฒŒ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. +2. ๋ชจ๋ธ์„ ํ†ตํ•ด ๊ฒฐ๊ณผ ๋˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +3. ๋ชจ๋ธ์€ ์–ด๋–ค ํ† ํฐ์ด ๋‹ต๋ณ€์˜ ์‹œ์ž‘์— ์žˆ๋Š”์ง€, ์–ด๋–ค ํ† ํฐ์ด ๋‹ต๋ณ€์ด ๋์— ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” `start_logits`์™€ `end_logits`๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค (batch_size, sequence_length) ํ˜•ํƒœ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. +4. `start_logits`์™€ `end_logits`์˜ ๋งˆ์ง€๋ง‰ ์ฐจ์›์„ ์ตœ๋Œ€๋กœ ๋งŒ๋“œ๋Š” ๊ฐ’์„ ์ฐพ์•„ ์˜ˆ์ƒ `start_idx`์™€ `end_idx`๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. +5. ํ† ํฌ๋‚˜์ด์ €๋กœ ๋‹ต๋ณ€์„ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch +>>> from transformers import AutoProcessor +>>> from transformers import AutoModelForDocumentQuestionAnswering + +>>> processor = AutoProcessor.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") +>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained("MariaK/layoutlmv2-base-uncased_finetuned_docvqa") + +>>> with torch.no_grad(): +... encoding = processor(image.convert("RGB"), question, return_tensors="pt") +... outputs = model(**encoding) +... start_logits = outputs.start_logits +... end_logits = outputs.end_logits +... predicted_start_idx = start_logits.argmax(-1).item() +... predicted_end_idx = end_logits.argmax(-1).item() + +>>> processor.tokenizer.decode(encoding.input_ids.squeeze()[predicted_start_idx : predicted_end_idx + 1]) +'lee a. waller' +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/idefics.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/idefics.md new file mode 100644 index 0000000000000000000000000000000000000000..331d94c8cbf7df81fdf12bb3ac3706ca373efeb6 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/idefics.md @@ -0,0 +1,389 @@ + + +# IDEFICS๋ฅผ ์ด์šฉํ•œ ์ด๋ฏธ์ง€ ์ž‘์—…[[image-tasks-with-idefics]] + +[[open-in-colab]] + +๊ฐœ๋ณ„ ์ž‘์—…์€ ํŠนํ™”๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ตœ๊ทผ ๋“ฑ์žฅํ•˜์—ฌ ์ธ๊ธฐ๋ฅผ ์–ป๊ณ  ์žˆ๋Š” ๋ฐฉ์‹์€ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ • ์—†์ด ๋‹ค์–‘ํ•œ ์ž‘์—…์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์€ ์š”์•ฝ, ๋ฒˆ์—ญ, ๋ถ„๋ฅ˜ ๋“ฑ๊ณผ ๊ฐ™์€ ์ž์—ฐ์–ด์ฒ˜๋ฆฌ (NLP) ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ํ…์ŠคํŠธ์™€ ๊ฐ™์€ ๋‹จ์ผ ๋ชจ๋‹ฌ๋ฆฌํ‹ฐ์— ๊ตญํ•œ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” IDEFICS๋ผ๋Š” ๋Œ€๊ทœ๋ชจ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€-ํ…์ŠคํŠธ ์ž‘์—…์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. + +[IDEFICS](../model_doc/idefics)๋Š” [Flamingo](https://huggingface.co/papers/2204.14198)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์˜คํ”ˆ ์•ก์„ธ์Šค ๋น„์ „ ๋ฐ ์–ธ์–ด ๋ชจ๋ธ๋กœ, DeepMind์—์„œ ์ฒ˜์Œ ๊ฐœ๋ฐœํ•œ ์ตœ์‹  ์‹œ๊ฐ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ž„์˜์˜ ์ด๋ฏธ์ง€ ๋ฐ ํ…์ŠคํŠธ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋ฐ›์•„ ์ผ๊ด€์„ฑ ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜๊ณ , ์‹œ๊ฐ์ ์ธ ๋‚ด์šฉ์„ ์„ค๋ช…ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€์— ๊ธฐ๋ฐ˜ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. IDEFICS๋Š” [800์–ต ํŒŒ๋ผ๋ฏธํ„ฐ](https://huggingface.co/HuggingFaceM4/idefics-80b)์™€ [90์–ต ํŒŒ๋ผ๋ฏธํ„ฐ](https://huggingface.co/HuggingFaceM4/idefics-9b) ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋‘ ๋ฒ„์ „ ๋ชจ๋‘ ๐Ÿค— Hub์—์„œ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฒ„์ „์—๋Š” ๋Œ€ํ™”ํ˜• ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ฒ„์ „๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๋ชจ๋ธ์€ ๋งค์šฐ ๋‹ค์žฌ๋‹ค๋Šฅํ•˜๋ฉฐ ๊ด‘๋ฒ”์œ„ํ•œ ์ด๋ฏธ์ง€ ๋ฐ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ์ž‘์—…์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋‹นํ•œ ์ปดํ“จํŒ… ์ž์›๊ณผ ์ธํ”„๋ผ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ฐœ๋ณ„ ์ž‘์—…์— ํŠนํ™”๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ชจ๋ธ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ํ•ฉํ•œ์ง€๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํŒ๋‹จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ์„ ๋ฐฐ์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: +- [IDEFICS ๋กœ๋“œํ•˜๊ธฐ](#loading-the-model) ๋ฐ [์–‘์žํ™”๋œ ๋ฒ„์ „์˜ ๋ชจ๋ธ ๋กœ๋“œํ•˜๊ธฐ](#quantized-model) +- IDEFICS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ: + - [์ด๋ฏธ์ง€ ์บก์…”๋‹](#image-captioning) + - [ํ”„๋กฌํ”„ํŠธ ์ด๋ฏธ์ง€ ์บก์…”๋‹](#prompted-image-captioning) + - [ํ“จ์ƒท ํ”„๋กฌํ”„ํŠธ](#few-shot-prompting) + - [์‹œ๊ฐ์  ์งˆ์˜ ์‘๋‹ต](#visual-question-answering) + - [์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜](#image-classification) + - [์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜ ํ…์ŠคํŠธ ์ƒ์„ฑ](#image-guided-text-generation) +- [๋ฐฐ์น˜ ๋ชจ๋“œ์—์„œ ์ถ”๋ก  ์‹คํ–‰](#running-inference-in-batch-mode) +- [๋Œ€ํ™”ํ˜• ์‚ฌ์šฉ์„ ์œ„ํ•œ IDEFICS ์ธ์ŠคํŠธ๋ŸญํŠธ ์‹คํ–‰](#idefics-instruct-for-conversational-use) + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. + +```bash +pip install -q bitsandbytes sentencepiece accelerate transformers +``` + + +๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ๋น„์–‘์žํ™”๋œ ๋ฒ„์ „์˜ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋กœ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ตœ์†Œ 20GB์˜ GPU ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + + +## ๋ชจ๋ธ ๋กœ๋“œ[[loading-the-model]] + +๋ชจ๋ธ์„ 90์–ต ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฒ„์ „์˜ ์ฒดํฌํฌ์ธํŠธ๋กœ ๋กœ๋“œํ•ด ๋ด…์‹œ๋‹ค: + +```py +>>> checkpoint = "HuggingFaceM4/idefics-9b" +``` + +๋‹ค๋ฅธ Transformers ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์ฒดํฌํฌ์ธํŠธ์—์„œ ํ”„๋กœ์„ธ์„œ์™€ ๋ชจ๋ธ ์ž์ฒด๋ฅผ ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. +IDEFICS ํ”„๋กœ์„ธ์„œ๋Š” [`LlamaTokenizer`]์™€ IDEFICS ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์„œ๋กœ ๊ฐ์‹ธ์„œ ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€ ์ž…๋ ฅ์„ ๋ชจ๋ธ์— ๋งž๊ฒŒ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch + +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +``` + +`device_map`์„ `"auto"`๋กœ ์„ค์ •ํ•˜๋ฉด ์‚ฌ์šฉ ์ค‘์ธ ์žฅ์น˜๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์žฅ ์ตœ์ ํ™”๋œ ๋ฐฉ์‹์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž๋™์œผ๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +### ์–‘์žํ™”๋œ ๋ชจ๋ธ[[quantized-model]] + +๊ณ ์šฉ๋Ÿ‰ GPU ์‚ฌ์šฉ์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ, ๋ชจ๋ธ์˜ ์–‘์žํ™”๋œ ๋ฒ„์ „์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ํ”„๋กœ์„ธ์„œ๋ฅผ 4๋น„ํŠธ ์ •๋ฐ€๋„๋กœ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ, `from_pretrained` ๋ฉ”์†Œ๋“œ์— `BitsAndBytesConfig`๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ชจ๋ธ์ด ๋กœ๋“œ๋˜๋Š” ๋™์•ˆ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์••์ถ•๋ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor, BitsAndBytesConfig + +>>> quantization_config = BitsAndBytesConfig( +... load_in_4bit=True, +... bnb_4bit_compute_dtype=torch.float16, +... ) + +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> model = IdeficsForVisionText2Text.from_pretrained( +... checkpoint, +... quantization_config=quantization_config, +... device_map="auto" +... ) +``` + +์ด์ œ ๋ชจ๋ธ์„ ์ œ์•ˆ๋œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ ๋กœ๋“œํ–ˆ์œผ๋‹ˆ, IDEFICS๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…๋“ค์„ ํƒ๊ตฌํ•ด๋ด…์‹œ๋‹ค. + +## ์ด๋ฏธ์ง€ ์บก์…”๋‹[[image-captioning]] +์ด๋ฏธ์ง€ ์บก์…”๋‹์€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์บก์…˜์„ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์‘์šฉ ๋ถ„์•ผ๋Š” ์‹œ๊ฐ ์žฅ์• ์ธ์ด ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์˜จ๋ผ์ธ์—์„œ ์ด๋ฏธ์ง€ ์ฝ˜ํ…์ธ ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ž‘์—…์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์บก์…˜์„ ๋‹ฌ ์ด๋ฏธ์ง€ ์˜ˆ์‹œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์‹œ: + +
+ Image of a puppy in a flower bed +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Hendo Wang](https://unsplash.com/@hendoo). + +IDEFICS๋Š” ํ…์ŠคํŠธ ๋ฐ ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋ชจ๋‘ ์ˆ˜์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฏธ์ง€๋ฅผ ์บก์…˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์— ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ œ๊ณตํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๋œ ์ž…๋ ฅ ์ด๋ฏธ์ง€๋งŒ ์ œ๊ณตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ์—†์ด ๋ชจ๋ธ์€ BOS(์‹œํ€€์Šค ์‹œ์ž‘) ํ† ํฐ๋ถ€ํ„ฐ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์‹œ์ž‘ํ•˜์—ฌ ์บก์…˜์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. + +๋ชจ๋ธ์— ์ด๋ฏธ์ง€ ์ž…๋ ฅ์œผ๋กœ๋Š” ์ด๋ฏธ์ง€ ๊ฐ์ฒด(`PIL.Image`) ๋˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” URL์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1583160247711-2191776b4b91?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3542&q=80", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +A puppy in a flower bed +``` + + + +`max_new_tokens`์˜ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ฌ ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด `generate` ํ˜ธ์ถœ ์‹œ `bad_words_ids`๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์—†์„ ๋•Œ ์ƒˆ๋กœ์šด `` ๋˜๋Š” `` ํ† ํฐ์„ ์ƒ์„ฑํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. +์ด ๊ฐ€์ด๋“œ์—์„œ์ฒ˜๋Ÿผ `bad_words_ids`๋ฅผ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, [ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต](../generation_strategies) ๊ฐ€์ด๋“œ์— ์„ค๋ช…๋œ ๋Œ€๋กœ `GenerationConfig`์— ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + + +## ํ”„๋กฌํ”„ํŠธ ์ด๋ฏธ์ง€ ์บก์…”๋‹[[prompted-image-captioning]] + +ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์บก์…”๋‹์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๊ณ„์† ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ด๋ฏธ์ง€๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +
+ Image of the Eiffel Tower at night +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Denys Nevozhai](https://unsplash.com/@dnevozhai). + +ํ…์ŠคํŠธ ๋ฐ ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ๋Š” ์ ์ ˆํ•œ ์ž…๋ ฅ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์˜ ํ”„๋กœ์„ธ์„œ์— ํ•˜๋‚˜์˜ ๋ชฉ๋ก์œผ๋กœ ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> prompt = [ +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +This is an image of the Eiffel Tower in Paris, France. +``` + +## ํ“จ์ƒท ํ”„๋กฌํ”„ํŠธ[[few-shot-prompting]] + +IDEFICS๋Š” ํ›Œ๋ฅญํ•œ ์ œ๋กœ์ƒท ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ์ง€๋งŒ, ์ž‘์—…์— ํŠน์ • ํ˜•์‹์˜ ์บก์…˜์ด ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ์ž‘์—…์˜ ๋ณต์žก์„ฑ์„ ๋†’์ด๋Š” ๋‹ค๋ฅธ ์ œํ•œ ์‚ฌํ•ญ์ด๋‚˜ ์š”๊ตฌ ์‚ฌํ•ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ํ“จ์ƒท ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งฅ๋ฝ ๋‚ด ํ•™์Šต(In-Context Learning)์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ”„๋กฌํ”„ํŠธ์— ์˜ˆ์‹œ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ๋ชจ๋ธ์ด ์ฃผ์–ด์ง„ ์˜ˆ์‹œ์˜ ํ˜•์‹์„ ๋ชจ๋ฐฉํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ์œ ๋„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด์ „์˜ ์—ํŽ ํƒ‘ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ์— ์˜ˆ์‹œ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๋ชจ๋ธ์—๊ฒŒ ์ด๋ฏธ์ง€์˜ ๊ฐ์ฒด๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ํฅ๋ฏธ๋กœ์šด ์ •๋ณด๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๋Š” ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค. +๊ทธ๋Ÿฐ ๋‹ค์Œ ์ž์œ ์˜ ์—ฌ์‹ ์ƒ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ๋™์ผํ•œ ์‘๋‹ต ํ˜•์‹์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค: + +
+ Image of the Statue of Liberty +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Juan Mayobre](https://unsplash.com/@jmayobres). + +```py +>>> prompt = ["User:", +... "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "Describe this image.\nAssistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building.\n", +... "User:", +... "https://images.unsplash.com/photo-1524099163253-32b7f0256868?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3387&q=80", +... "Describe this image.\nAssistant:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=30, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +User: Describe this image. +Assistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building. +User: Describe this image. +Assistant: An image of the Statue of Liberty. Fun fact: the Statue of Liberty is 151 feet tall. +``` + +๋‹จ ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋งŒ์œผ๋กœ๋„(์ฆ‰, 1-shot) ๋ชจ๋ธ์ด ์ž‘์—… ์ˆ˜ํ–‰ ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ–ˆ๋‹ค๋Š” ์ ์ด ์ฃผ๋ชฉํ•  ๋งŒํ•ฉ๋‹ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ์ž‘์—…์˜ ๊ฒฝ์šฐ, ๋” ๋งŽ์€ ์˜ˆ์‹œ(์˜ˆ: 3-shot, 5-shot ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ—˜ํ•ด ๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. + +## ์‹œ๊ฐ์  ์งˆ์˜ ์‘๋‹ต[[visual-question-answering]] + +์‹œ๊ฐ์  ์งˆ์˜ ์‘๋‹ต(VQA)์€ ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐฉํ˜• ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ‘๊ทผ์„ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ต์œก(์‹œ๊ฐ ์ž๋ฃŒ์— ๋Œ€ํ•œ ์ถ”๋ก ), ๊ณ ๊ฐ ์„œ๋น„์Šค(์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์ œํ’ˆ ์งˆ๋ฌธ), ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ ๋“ฑ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ์ž‘์—…์„ ์œ„ํ•ด ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +
+ Image of a couple having a picnic +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Jarritos Mexican Soda](https://unsplash.com/@jarritos). + +์ ์ ˆํ•œ ์ง€์‹œ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ์ง€ ์บก์…”๋‹์—์„œ ์‹œ๊ฐ์  ์งˆ์˜ ์‘๋‹ต์œผ๋กœ ๋ชจ๋ธ์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> prompt = [ +... "Instruction: Provide an answer to the question. Use the image to answer.\n", +... "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Question: Where are these people and what's the weather like? Answer:" +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=20, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Provide an answer to the question. Use the image to answer. + Question: Where are these people and what's the weather like? Answer: They're in a park in New York City, and it's a beautiful day. +``` + +## ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜[[image-classification]] + +IDEFICS๋Š” ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋ผ๋ฒจ์ด ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋กœ ๋ช…์‹œ์ ์œผ๋กœ ํ•™์Šต๋˜์ง€ ์•Š์•„๋„ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์–‘ํ•œ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก์ด ์ฃผ์–ด์ง€๋ฉด, ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ์ดํ•ด ๋Šฅ๋ ฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๊ฐ€ ์†ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์—ฌ๊ธฐ์— ์•ผ์ฑ„ ๊ฐ€ํŒ๋Œ€ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +
+ Image of a vegetable stand +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Peter Wendt](https://unsplash.com/@peterwendt). + +์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์—๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง„ ์นดํ…Œ๊ณ ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋„๋ก ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> categories = ['animals','vegetables', 'city landscape', 'cars', 'office'] +>>> prompt = [f"Instruction: Classify the following image into a single category from the following list: {categories}.\n", +... "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "Category: " +... ] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=6, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Classify the following image into a single category from the following list: ['animals', 'vegetables', 'city landscape', 'cars', 'office']. +Category: Vegetables +``` + +์œ„ ์˜ˆ์ œ์—์„œ๋Š” ๋ชจ๋ธ์—๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ๋‹จ์ผ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋ถ„๋ฅ˜ํ•˜๋„๋ก ์ง€์‹œํ–ˆ์ง€๋งŒ, ์ˆœ์œ„ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋„๋ก ๋ชจ๋ธ์— ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ์ด๋ฏธ์ง€ ๊ธฐ๋ฐ˜ ํ…์ŠคํŠธ ์ƒ์„ฑ[[image-guided-text-generation]] + +์ด๋ฏธ์ง€๋ฅผ ํ™œ์šฉํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋”์šฑ ์ฐฝ์˜์ ์ธ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ์ˆ ์€ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋ฉฐ, ์ œํ’ˆ ์„ค๋ช…, ๊ด‘๊ณ  ๋ฌธ๊ตฌ, ์žฅ๋ฉด ๋ฌ˜์‚ฌ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ, ๋นจ๊ฐ„ ๋ฌธ ์ด๋ฏธ์ง€๋ฅผ IDEFICS์— ์ž…๋ ฅํ•˜์—ฌ ์ด์•ผ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +
+ Image of a red door with a pumpkin on the steps +
+ +์‚ฌ์ง„ ์ œ๊ณต: [Craig Tidball](https://unsplash.com/@devonshiremedia). + +```py +>>> prompt = ["Instruction: Use the image to write a story. \n", +... "https://images.unsplash.com/photo-1517086822157-2b0358e7684a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2203&q=80", +... "Story: \n"] + +>>> inputs = processor(prompt, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, num_beams=2, max_new_tokens=200, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> print(generated_text[0]) +Instruction: Use the image to write a story. + Story: +Once upon a time, there was a little girl who lived in a house with a red door. She loved her red door. It was the prettiest door in the whole world. + +One day, the little girl was playing in her yard when she noticed a man standing on her doorstep. He was wearing a long black coat and a top hat. + +The little girl ran inside and told her mother about the man. + +Her mother said, โ€œDonโ€™t worry, honey. Heโ€™s just a friendly ghost.โ€ + +The little girl wasnโ€™t sure if she believed her mother, but she went outside anyway. + +When she got to the door, the man was gone. + +The next day, the little girl was playing in her yard again when she noticed the man standing on her doorstep. + +He was wearing a long black coat and a top hat. + +The little girl ran +``` + +IDEFICS๊ฐ€ ๋ฌธ ์•ž์— ์žˆ๋Š” ํ˜ธ๋ฐ•์„ ๋ณด๊ณ  ์œ ๋ น์— ๋Œ€ํ•œ ์œผ์Šค์Šคํ•œ ํ• ๋กœ์œˆ ์ด์•ผ๊ธฐ๋ฅผ ๋งŒ๋“  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. + + + +์ด์ฒ˜๋Ÿผ ๊ธด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต์„ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ƒ์„ฑ๋œ ๊ฒฐ๊ณผ๋ฌผ์˜ ํ’ˆ์งˆ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต](../generation_strategies)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + +## ๋ฐฐ์น˜ ๋ชจ๋“œ์—์„œ ์ถ”๋ก  ์‹คํ–‰[[running-inference-in-batch-mode]] + +์•ž์„  ๋ชจ๋“  ์„น์…˜์—์„œ๋Š” ๋‹จ์ผ ์˜ˆ์‹œ์— ๋Œ€ํ•ด IDEFICS๋ฅผ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ, ํ”„๋กฌํ”„ํŠธ ๋ชฉ๋ก์„ ์ „๋‹ฌํ•˜์—ฌ ์—ฌ๋Ÿฌ ์˜ˆ์‹œ์— ๋Œ€ํ•œ ์ถ”๋ก ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> prompts = [ +... [ "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... [ "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80", +... "This is an image of ", +... ], +... ] + +>>> inputs = processor(prompts, return_tensors="pt").to(model.device) +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i,t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +0: +This is an image of the Eiffel Tower in Paris, France. + +1: +This is an image of a couple on a picnic blanket. + +2: +This is an image of a vegetable stand. +``` + +## ๋Œ€ํ™”ํ˜• ์‚ฌ์šฉ์„ ์œ„ํ•œ IDEFICS ์ธ์ŠคํŠธ๋ŸญํŠธ ์‹คํ–‰[[idefics-instruct-for-conversational-use]] + +๋Œ€ํ™”ํ˜• ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์œ„ํ•ด, ๐Ÿค— Hub์—์„œ ๋ช…๋ น์–ด ์ˆ˜ํ–‰์— ์ตœ์ ํ™”๋œ ๋ฒ„์ „์˜ ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ณณ์—๋Š” `HuggingFaceM4/idefics-80b-instruct`์™€ `HuggingFaceM4/idefics-9b-instruct`๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ์ฒดํฌํฌ์ธํŠธ๋Š” ์ง€๋„ ํ•™์Šต ๋ฐ ๋ช…๋ น์–ด ๋ฏธ์„ธ ์กฐ์ • ๋ฐ์ดํ„ฐ์…‹์˜ ํ˜ผํ•ฉ์œผ๋กœ ๊ฐ๊ฐ์˜ ๊ธฐ๋ณธ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์˜ ํ•˜์œ„ ์ž‘์—… ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋™์‹œ์— ๋Œ€ํ™”ํ˜• ํ™˜๊ฒฝ์—์„œ ๋ชจ๋ธ์„ ๋” ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. + +๋Œ€ํ™”ํ˜• ์‚ฌ์šฉ์„ ์œ„ํ•œ ์‚ฌ์šฉ๋ฒ• ๋ฐ ํ”„๋กฌํ”„ํŠธ๋Š” ๊ธฐ๋ณธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> import torch +>>> from transformers import IdeficsForVisionText2Text, AutoProcessor + +>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct" +>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto") +>>> processor = AutoProcessor.from_pretrained(checkpoint) + +>>> prompts = [ +... [ +... "User: What is in this image?", +... "https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG", +... "", + +... "\nAssistant: This picture depicts Idefix, the dog of Obelix in Asterix and Obelix. Idefix is running on the ground.", + +... "\nUser:", +... "https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052", +... "And who is that?", + +... "\nAssistant:", +... ], +... ] + +>>> # --batched mode +>>> inputs = processor(prompts, add_end_of_utterance_token=False, return_tensors="pt").to(device) +>>> # --single sample mode +>>> # inputs = processor(prompts[0], return_tensors="pt").to(device) + +>>> # args ์ƒ์„ฑ +>>> exit_condition = processor.tokenizer("", add_special_tokens=False).input_ids +>>> bad_words_ids = processor.tokenizer(["", ""], add_special_tokens=False).input_ids + +>>> generated_ids = model.generate(**inputs, eos_token_id=exit_condition, bad_words_ids=bad_words_ids, max_length=100) +>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True) +>>> for i, t in enumerate(generated_text): +... print(f"{i}:\n{t}\n") +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_captioning.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_captioning.md new file mode 100644 index 0000000000000000000000000000000000000000..c4d0f99b6170ee067c49a2a6bf7734617bb5851f --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_captioning.md @@ -0,0 +1,281 @@ + + + +# ์ด๋ฏธ์ง€ ์บก์…”๋‹[[image-captioning]] + +[[open-in-colab]] + +์ด๋ฏธ์ง€ ์บก์…”๋‹(Image captioning)์€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์บก์…˜์„ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. +์ด๋ฏธ์ง€ ์บก์…”๋‹์€ ์‹œ๊ฐ ์žฅ์• ์ธ์ด ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋„๋ก ์‹œ๊ฐ ์žฅ์• ์ธ์„ ๋ณด์กฐํ•˜๋Š” ๋“ฑ ์‹ค์ƒํ™œ์—์„œ ํ”ํžˆ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์ด๋ฏธ์ง€ ์บก์…”๋‹์€ ์ด๋ฏธ์ง€๋ฅผ ์„ค๋ช…ํ•จ์œผ๋กœ์จ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ˜ํ…์ธ  ์ ‘๊ทผ์„ฑ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์†Œ๊ฐœํ•  ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +* ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋ชจ๋ธ์„ ํŒŒ์ธํŠœ๋‹ํ•ฉ๋‹ˆ๋‹ค. +* ํŒŒ์ธํŠœ๋‹๋œ ๋ชจ๋ธ์„ ์ถ”๋ก ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate -q +pip install jiwer -q +``` + +Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜๋ฉด ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•˜์„ธ์š”. + + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +## ํฌ์ผ“๋ชฌ BLIP ์บก์…˜ ๋ฐ์ดํ„ฐ์„ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ[[load-the-pokmon-blip-captions-dataset]] + +{์ด๋ฏธ์ง€-์บก์…˜} ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ์„ธํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๐Ÿค— Dataset ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +PyTorch์—์„œ ์ž์‹ ๋งŒ์˜ ์ด๋ฏธ์ง€ ์บก์…˜ ๋ฐ์ดํ„ฐ์„ธํŠธ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด [์ด ๋…ธํŠธ๋ถ](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/GIT/Fine_tune_GIT_on_an_image_captioning_dataset.ipynb)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + +```python +from datasets import load_dataset + +ds = load_dataset("lambdalabs/pokemon-blip-captions") +ds +``` +```bash +DatasetDict({ + train: Dataset({ + features: ['image', 'text'], + num_rows: 833 + }) +}) +``` + +์ด ๋ฐ์ดํ„ฐ์„ธํŠธ๋Š” `image`์™€ `text`๋ผ๋Š” ๋‘ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. + + + +๋งŽ์€ ์ด๋ฏธ์ง€ ์บก์…˜ ๋ฐ์ดํ„ฐ์„ธํŠธ์—๋Š” ์ด๋ฏธ์ง€๋‹น ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์บก์…˜์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ํ•™์Šต ์ค‘์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์บก์…˜ ์ค‘์—์„œ ๋ฌด์ž‘์œ„๋กœ ์ƒ˜ํ”Œ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. + + + +[`~datasets.Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์„ธํŠธ์˜ ํ•™์Šต ๋ถ„ํ• ์„ ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ์„ธํŠธ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค: + + +```python +ds = ds["train"].train_test_split(test_size=0.1) +train_ds = ds["train"] +test_ds = ds["test"] +``` + +ํ•™์Šต ์„ธํŠธ์˜ ์ƒ˜ํ”Œ ๋ช‡ ๊ฐœ๋ฅผ ์‹œ๊ฐํ™”ํ•ด ๋ด…์‹œ๋‹ค. +Let's visualize a couple of samples from the training set. + + +```python +from textwrap import wrap +import matplotlib.pyplot as plt +import numpy as np + + +def plot_images(images, captions): + plt.figure(figsize=(20, 20)) + for i in range(len(images)): + ax = plt.subplot(1, len(images), i + 1) + caption = captions[i] + caption = "\n".join(wrap(caption, 12)) + plt.title(caption) + plt.imshow(images[i]) + plt.axis("off") + + +sample_images_to_visualize = [np.array(train_ds[i]["image"]) for i in range(5)] +sample_captions = [train_ds[i]["text"] for i in range(5)] +plot_images(sample_images_to_visualize, sample_captions) +``` + +
+ Sample training images +
+ +## ๋ฐ์ดํ„ฐ์„ธํŠธ ์ „์ฒ˜๋ฆฌ[[preprocess-the-dataset]] + +๋ฐ์ดํ„ฐ์„ธํŠธ์—๋Š” ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์–‘์‹์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ „์ฒ˜๋ฆฌ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์ด๋ฏธ์ง€์™€ ์บก์…˜์„ ๋ชจ๋‘ ์ „์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + +์ „์ฒ˜๋ฆฌ ์ž‘์—…์„ ์œ„ํ•ด, ํŒŒ์ธํŠœ๋‹ํ•˜๋ ค๋Š” ๋ชจ๋ธ์— ์—ฐ๊ฒฐ๋œ ํ”„๋กœ์„ธ์„œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + +```python +from transformers import AutoProcessor + +checkpoint = "microsoft/git-base" +processor = AutoProcessor.from_pretrained(checkpoint) +``` + +ํ”„๋กœ์„ธ์„œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ํฌ๊ธฐ ์กฐ์ • ๋ฐ ํ”ฝ์…€ ํฌ๊ธฐ ์กฐ์ •์„ ํฌํ•จํ•œ ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์บก์…˜์„ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. + +```python +def transforms(example_batch): + images = [x for x in example_batch["image"]] + captions = [x for x in example_batch["text"]] + inputs = processor(images=images, text=captions, padding="max_length") + inputs.update({"labels": inputs["input_ids"]}) + return inputs + + +train_ds.set_transform(transforms) +test_ds.set_transform(transforms) +``` + +๋ฐ์ดํ„ฐ์„ธํŠธ๊ฐ€ ์ค€๋น„๋˜์—ˆ์œผ๋‹ˆ ์ด์ œ ํŒŒ์ธํŠœ๋‹์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +## ๊ธฐ๋ณธ ๋ชจ๋ธ ๊ฐ€์ ธ์˜ค๊ธฐ[[load-a-base-model]] + +["microsoft/git-base"](https://huggingface.co/microsoft/git-base)๋ฅผ [`AutoModelForCausalLM`](https://huggingface.co/docs/transformers/model_doc/auto#transformers.AutoModelForCausalLM) ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. + + +```python +from transformers import AutoModelForCausalLM + +model = AutoModelForCausalLM.from_pretrained(checkpoint) +``` + +## ํ‰๊ฐ€[[evaluate]] + +์ด๋ฏธ์ง€ ์บก์…˜ ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ [Rouge ์ ์ˆ˜](https://huggingface.co/spaces/evaluate-metric/rouge) ๋˜๋Š” [๋‹จ์–ด ์˜ค๋ฅ˜์œจ(Word Error Rate)](https://huggingface.co/spaces/evaluate-metric/wer)๋กœ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹จ์–ด ์˜ค๋ฅ˜์œจ(WER)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ฅผ ์œ„ํ•ด ๐Ÿค— Evaluate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +WER์˜ ์ž ์žฌ์  ์ œํ•œ ์‚ฌํ•ญ ๋ฐ ๊ธฐํƒ€ ๋ฌธ์ œ์ ์€ [์ด ๊ฐ€์ด๋“œ](https://huggingface.co/spaces/evaluate-metric/wer)๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. + + +```python +from evaluate import load +import torch + +wer = load("wer") + + +def compute_metrics(eval_pred): + logits, labels = eval_pred + predicted = logits.argmax(-1) + decoded_labels = processor.batch_decode(labels, skip_special_tokens=True) + decoded_predictions = processor.batch_decode(predicted, skip_special_tokens=True) + wer_score = wer.compute(predictions=decoded_predictions, references=decoded_labels) + return {"wer_score": wer_score} +``` + +## ํ•™์Šต![[train!]] + +์ด์ œ ๋ชจ๋ธ ํŒŒ์ธํŠœ๋‹์„ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๐Ÿค— [`Trainer`]๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +๋จผ์ €, [`TrainingArguments`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ์ธ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. + + +```python +from transformers import TrainingArguments, Trainer + +model_name = checkpoint.split("/")[1] + +training_args = TrainingArguments( + output_dir=f"{model_name}-pokemon", + learning_rate=5e-5, + num_train_epochs=50, + fp16=True, + per_device_train_batch_size=32, + per_device_eval_batch_size=32, + gradient_accumulation_steps=2, + save_total_limit=3, + eval_strategy="steps", + eval_steps=50, + save_strategy="steps", + save_steps=50, + logging_steps=50, + remove_unused_columns=False, + push_to_hub=True, + label_names=["labels"], + load_best_model_at_end=True, +) +``` + +ํ•™์Šต ์ธ์ˆ˜๋ฅผ ๋ฐ์ดํ„ฐ์„ธํŠธ, ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ๐Ÿค— Trainer์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```python +trainer = Trainer( + model=model, + args=training_args, + train_dataset=train_ds, + eval_dataset=test_ds, + compute_metrics=compute_metrics, +) +``` + +ํ•™์Šต์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด [`Trainer`] ๊ฐ์ฒด์—์„œ [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. + +```python +trainer.train() +``` + +ํ•™์Šต์ด ์ง„ํ–‰๋˜๋ฉด์„œ ํ•™์Šต ์†์‹ค์ด ์›ํ™œํ•˜๊ฒŒ ๊ฐ์†Œํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋“  ์‚ฌ๋žŒ์ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`~Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜์„ธ์š”: + + +```python +trainer.push_to_hub() +``` + +## ์ถ”๋ก [[inference]] + +`test_ds`์—์„œ ์ƒ˜ํ”Œ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์™€ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. + + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png" +image = Image.open(requests.get(url, stream=True).raw) +image +``` + +
+ Test image +
+ +๋ชจ๋ธ์— ์‚ฌ์šฉํ•  ์ด๋ฏธ์ง€๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. + +```python +device = "cuda" if torch.cuda.is_available() else "cpu" + +inputs = processor(images=image, return_tensors="pt").to(device) +pixel_values = inputs.pixel_values +``` + +[`generate`]๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์˜ˆ์ธก์„ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. + +```python +generated_ids = model.generate(pixel_values=pixel_values, max_length=50) +generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] +print(generated_caption) +``` +```bash +a drawing of a pink and blue pokemon +``` + +ํŒŒ์ธํŠœ๋‹๋œ ๋ชจ๋ธ์ด ๊ฝค ๊ดœ์ฐฎ์€ ์บก์…˜์„ ์ƒ์„ฑํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..4955bd6cdf8108624fb706a0d5c0605814858eb5 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_classification.md @@ -0,0 +1,542 @@ + + +# ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜[[image-classification]] + +[[open-in-colab]] + + + +์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋Š” ์ด๋ฏธ์ง€์— ๋ ˆ์ด๋ธ” ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋˜๋Š” ์˜ค๋””์˜ค ๋ถ„๋ฅ˜์™€ ๋‹ฌ๋ฆฌ ์ž…๋ ฅ์€ +์ด๋ฏธ์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํ”ฝ์…€ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์—๋Š” ์ž์—ฐ์žฌํ•ด ํ›„ ํ”ผํ•ด ๊ฐ์ง€, ๋†์ž‘๋ฌผ ๊ฑด๊ฐ• ๋ชจ๋‹ˆํ„ฐ๋ง, ์˜๋ฃŒ ์ด๋ฏธ์ง€์—์„œ ์งˆ๋ณ‘์˜ ์ง•ํ›„ ๊ฒ€์‚ฌ ์ง€์› ๋“ฑ +๋‹ค์–‘ํ•œ ์‘์šฉ ์‚ฌ๋ก€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค: + +1. [Food-101](https://huggingface.co/datasets/food101) ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ [ViT](model_doc/vit)๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์ด๋ฏธ์ง€์—์„œ ์‹ํ’ˆ ํ•ญ๋ชฉ์„ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. +2. ์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ • ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/image-classification)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์—, ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate +``` + +Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜์—ฌ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋˜๋ฉด, ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•˜์„ธ์š”: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## Food-101 ๋ฐ์ดํ„ฐ ์„ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ[[load-food101-dataset]] + +๐Ÿค— Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Food-101 ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ๋” ์ž‘์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋Œ€ํ•œ +ํ›ˆ๋ จ์— ๋งŽ์€ ์‹œ๊ฐ„์„ ํ• ์• ํ•˜๊ธฐ ์ „์— ์‹คํ—˜์„ ํ†ตํ•ด ๋ชจ๋“  ๊ฒƒ์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from datasets import load_dataset + +>>> food = load_dataset("food101", split="train[:5000]") +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ `train`์„ [`~datasets.Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ ํ…Œ์ŠคํŠธ ์„ธํŠธ๋กœ ๋ถ„ํ• ํ•˜์„ธ์š”: + +```py +>>> food = food.train_test_split(test_size=0.2) +``` + +๊ทธ๋ฆฌ๊ณ  ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”: + +```py +>>> food["train"][0] +{'image': , + 'label': 79} +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ๊ฐ ์˜ˆ์ œ์—๋Š” ๋‘ ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: + +- `image`: ์‹ํ’ˆ ํ•ญ๋ชฉ์˜ PIL ์ด๋ฏธ์ง€ +- `label`: ์‹ํ’ˆ ํ•ญ๋ชฉ์˜ ๋ ˆ์ด๋ธ” ํด๋ž˜์Šค + +๋ชจ๋ธ์ด ๋ ˆ์ด๋ธ” ID์—์„œ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์„ ์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก +๋ ˆ์ด๋ธ” ์ด๋ฆ„์„ ์ •์ˆ˜๋กœ ๋งคํ•‘ํ•˜๊ณ , ์ •์ˆ˜๋ฅผ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์œผ๋กœ ๋งคํ•‘ํ•˜๋Š” ์‚ฌ์ „์„ ๋งŒ๋“œ์„ธ์š”: + +```py +>>> labels = food["train"].features["label"].names +>>> label2id, id2label = dict(), dict() +>>> for i, label in enumerate(labels): +... label2id[label] = str(i) +... id2label[str(i)] = label +``` + +์ด์ œ ๋ ˆ์ด๋ธ” ID๋ฅผ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> id2label[str(79)] +'prime_rib' +``` + +## ์ „์ฒ˜๋ฆฌ[[preprocess]] + +๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ…์„œ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ViT ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoImageProcessor + +>>> checkpoint = "google/vit-base-patch16-224-in21k" +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +``` + + + +์ด๋ฏธ์ง€์— ๋ช‡ ๊ฐ€์ง€ ์ด๋ฏธ์ง€ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜์—ฌ ๊ณผ์ ํ•ฉ์— ๋Œ€ํ•ด ๋ชจ๋ธ์„ ๋” ๊ฒฌ๊ณ ํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Torchvision์˜ [`transforms`](https://pytorch.org/vision/stable/transforms.html) ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์›ํ•˜๋Š” ์ด๋ฏธ์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด๋ฏธ์ง€์˜ ์ž„์˜ ๋ถ€๋ถ„์„ ํฌ๋กญํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•œ ๋‹ค์Œ, ์ด๋ฏธ์ง€ ํ‰๊ท ๊ณผ ํ‘œ์ค€ ํŽธ์ฐจ๋กœ ์ •๊ทœํ™”ํ•˜์„ธ์š”: + +```py +>>> from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor + +>>> normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std) +>>> size = ( +... image_processor.size["shortest_edge"] +... if "shortest_edge" in image_processor.size +... else (image_processor.size["height"], image_processor.size["width"]) +... ) +>>> _transforms = Compose([RandomResizedCrop(size), ToTensor(), normalize]) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณ€ํ™˜์„ ์ ์šฉํ•˜๊ณ  ์ด๋ฏธ์ง€์˜ `pixel_values`(๋ชจ๋ธ์— ๋Œ€ํ•œ ์ž…๋ ฅ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”: + +```py +>>> def transforms(examples): +... examples["pixel_values"] = [_transforms(img.convert("RGB")) for img in examples["image"]] +... del examples["image"] +... return examples +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์ „์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•˜๋ ค๋ฉด ๐Ÿค— Datasets [`~datasets.Dataset.with_transform`]์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋ณ€ํ™˜์ด ์ฆ‰์‹œ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> food = food.with_transform(transforms) +``` + +์ด์ œ [`DefaultDataCollator`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ œ ๋ฐฐ์น˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๐Ÿค— Transformers์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ์™€ ๋‹ฌ๋ฆฌ, `DefaultDataCollator`๋Š” ํŒจ๋”ฉ๊ณผ ๊ฐ™์€ ์ถ”๊ฐ€์ ์ธ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator() +``` + + + + + + + +๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•˜๊ณ  ๋ชจ๋ธ์„ ๋ณด๋‹ค ๊ฒฌ๊ณ ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ํ›ˆ๋ จ ๋ถ€๋ถ„์— ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. +์—ฌ๊ธฐ์„œ Keras ์ „์ฒ˜๋ฆฌ ๋ ˆ์ด์–ด๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ณ€ํ™˜(๋ฐ์ดํ„ฐ ์ฆ๊ฐ• ํฌํ•จ)๊ณผ +๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ณ€ํ™˜(์ค‘์•™ ํฌ๋กœํ•‘, ํฌ๊ธฐ ์กฐ์ •, ์ •๊ทœํ™”๋งŒ)์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. +`tf.image` ๋˜๋Š” ๋‹ค๋ฅธ ์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from tensorflow import keras +>>> from tensorflow.keras import layers + +>>> size = (image_processor.size["height"], image_processor.size["width"]) + +>>> train_data_augmentation = keras.Sequential( +... [ +... layers.RandomCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... layers.RandomFlip("horizontal"), +... layers.RandomRotation(factor=0.02), +... layers.RandomZoom(height_factor=0.2, width_factor=0.2), +... ], +... name="train_data_augmentation", +... ) + +>>> val_data_augmentation = keras.Sequential( +... [ +... layers.CenterCrop(size[0], size[1]), +... layers.Rescaling(scale=1.0 / 127.5, offset=-1), +... ], +... name="val_data_augmentation", +... ) +``` + +๋‹ค์Œ์œผ๋กœ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๊ฐ€ ์•„๋‹ˆ๋ผ ์ด๋ฏธ์ง€ ๋ฐฐ์น˜์— ์ ์ ˆํ•œ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. + +```py +>>> import numpy as np +>>> import tensorflow as tf +>>> from PIL import Image + + +>>> def convert_to_tf_tensor(image: Image): +... np_image = np.array(image) +... tf_image = tf.convert_to_tensor(np_image) +... # `expand_dims()` is used to add a batch dimension since +... # the TF augmentation layers operates on batched inputs. +... return tf.expand_dims(tf_image, 0) + + +>>> def preprocess_train(example_batch): +... """Apply train_transforms across a batch.""" +... images = [ +... train_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch + + +... def preprocess_val(example_batch): +... """Apply val_transforms across a batch.""" +... images = [ +... val_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"] +... ] +... example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images] +... return example_batch +``` + +๐Ÿค— Datasets [`~datasets.Dataset.set_transform`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฆ‰์‹œ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜์„ธ์š”: + +```py +food["train"].set_transform(preprocess_train) +food["test"].set_transform(preprocess_val) +``` + +์ตœ์ข… ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋กœ `DefaultDataCollator`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ œ ๋ฐฐ์น˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๐Ÿค— Transformers์˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ์™€ ๋‹ฌ๋ฆฌ +`DefaultDataCollator`๋Š” ํŒจ๋”ฉ๊ณผ ๊ฐ™์€ ์ถ”๊ฐ€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +```py +>>> from transformers import DefaultDataCollator + +>>> data_collator = DefaultDataCollator(return_tensors="tf") +``` + + + +## ํ‰๊ฐ€[[evaluate]] + +ํ›ˆ๋ จ ์ค‘์— ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ํฌํ•จํ•˜๋ฉด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. +๐Ÿค— [Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์—์„œ๋Š” +[accuracy](https://huggingface.co/spaces/evaluate-metric/accuracy) ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. (๐Ÿค— Evaluate [๋น ๋ฅธ ๋‘˜๋Ÿฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์„ธ์š”): + +```py +>>> import evaluate + +>>> accuracy = evaluate.load("accuracy") +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ [`~evaluate.EvaluationModule.compute`]์— ์ „๋‹ฌํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: + +```py +>>> import numpy as np + + +>>> def compute_metrics(eval_pred): +... predictions, labels = eval_pred +... predictions = np.argmax(predictions, axis=1) +... return accuracy.compute(predictions=predictions, references=labels) +``` + +์ด์ œ `compute_metrics` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฉฐ, ํ›ˆ๋ จ์„ ์„ค์ •ํ•˜๋ฉด ์ด ํ•จ์ˆ˜๋กœ ๋˜๋Œ์•„์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +## ํ›ˆ๋ จ[[train]] + + + + + +[`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, [์—ฌ๊ธฐ](../training#train-with-pytorch-trainer)์—์„œ ๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ์„ ํ™•์ธํ•˜์„ธ์š”! + + + +์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ฌ ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! [`AutoModelForImageClassification`]๋กœ ViT๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ƒ๋˜๋Š” ๋ ˆ์ด๋ธ” ์ˆ˜, ๋ ˆ์ด๋ธ” ๋งคํ•‘ ๋ฐ ๋ ˆ์ด๋ธ” ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoModelForImageClassification, TrainingArguments, Trainer + +>>> model = AutoModelForImageClassification.from_pretrained( +... checkpoint, +... num_labels=len(labels), +... id2label=id2label, +... label2id=label2id, +... ) +``` + +์ด์ œ ์„ธ ๋‹จ๊ณ„๋งŒ ๊ฑฐ์น˜๋ฉด ๋์ž…๋‹ˆ๋‹ค: + +1. [`TrainingArguments`]์—์„œ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜์„ธ์š”. `image` ์—ด์ด ์‚ญ์ œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฏธ์‚ฌ์šฉ ์—ด์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. `image` ์—ด์ด ์—†์œผ๋ฉด `pixel_values`์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋™์ž‘์„ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด `remove_unused_columns=False`๋กœ ์„ค์ •ํ•˜์„ธ์š”! ๋‹ค๋ฅธ ์œ ์ผํ•œ ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ชจ๋ธ ์ €์žฅ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” `output_dir`์ž…๋‹ˆ๋‹ค. `push_to_hub=True`๋กœ ์„ค์ •ํ•˜๋ฉด ์ด ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค(๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). ๊ฐ ์—ํญ์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค, [`Trainer`]๊ฐ€ ์ •ํ™•๋„๋ฅผ ํ‰๊ฐ€ํ•˜๊ณ  ํ›ˆ๋ จ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. +2. [`Trainer`]์— ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ, ํ† ํฌ๋‚˜์ด์ €, ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ ๋ฐ `compute_metrics` ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”. +3. [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์„ธ์š”. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_food_model", +... remove_unused_columns=False, +... eval_strategy="epoch", +... save_strategy="epoch", +... learning_rate=5e-5, +... per_device_train_batch_size=16, +... gradient_accumulation_steps=4, +... per_device_eval_batch_size=16, +... num_train_epochs=3, +... warmup_ratio=0.1, +... logging_steps=10, +... load_best_model_at_end=True, +... metric_for_best_model="accuracy", +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... data_collator=data_collator, +... train_dataset=food["train"], +... eval_dataset=food["test"], +... processing_class=image_processor, +... compute_metrics=compute_metrics, +... ) + +>>> trainer.train() +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด, ๋ชจ๋“  ์‚ฌ๋žŒ์ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก [`~transformers.Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋กœ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜์„ธ์š”: + +```py +>>> trainer.push_to_hub() +``` + + + + + + + + +Keras๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋จผ์ € [๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ](./training#train-a-tensorflow-model-with-keras)์„ ํ™•์ธํ•˜์„ธ์š”! + + + +TensorFlow์—์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฅด์„ธ์š”: +1. ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ํ•™์Šต๋ฅ  ์Šค์ผ€์ฅด์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. +2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค. +3. ๐Ÿค— Dataset์„ `tf.data.Dataset`์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +4. ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. +5. ์ฝœ๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ํ›ˆ๋ จ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด `fit()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +6. ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ์„ ๐Ÿค— Hub์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. + +ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ, ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ ํ•™์Šต๋ฅ  ์Šค์ผ€์ฅด์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import create_optimizer + +>>> batch_size = 16 +>>> num_epochs = 5 +>>> num_train_steps = len(food["train"]) * num_epochs +>>> learning_rate = 3e-5 +>>> weight_decay_rate = 0.01 + +>>> optimizer, lr_schedule = create_optimizer( +... init_lr=learning_rate, +... num_train_steps=num_train_steps, +... weight_decay_rate=weight_decay_rate, +... num_warmup_steps=0, +... ) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ ˆ์ด๋ธ” ๋งคํ•‘๊ณผ ํ•จ๊ป˜ [`TFAuto ModelForImageClassification`]์œผ๋กœ ViT๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained( +... checkpoint, +... id2label=id2label, +... label2id=label2id, +... ) +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ [`~datasets.Dataset.to_tf_dataset`]์™€ `data_collator`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `tf.data.Dataset` ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์„ธ์š”: + +```py +>>> # converting our train dataset to tf.data.Dataset +>>> tf_train_dataset = food["train"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) + +>>> # converting our test dataset to tf.data.Dataset +>>> tf_eval_dataset = food["test"].to_tf_dataset( +... columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator +... ) +``` + +`compile()`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ชจ๋ธ์„ ๊ตฌ์„ฑํ•˜์„ธ์š”: + +```py +>>> from tensorflow.keras.losses import SparseCategoricalCrossentropy + +>>> loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) +>>> model.compile(optimizer=optimizer, loss=loss) +``` + +์˜ˆ์ธก์—์„œ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๋ชจ๋ธ์„ ๐Ÿค— Hub๋กœ ํ‘ธ์‹œํ•˜๋ ค๋ฉด [Keras callbacks](../main_classes/keras_callbacks)๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. +`compute_metrics` ํ•จ์ˆ˜๋ฅผ [KerasMetricCallback](../main_classes/keras_callbacks#transformers.KerasMetricCallback)์— ์ „๋‹ฌํ•˜๊ณ , +[PushToHubCallback](../main_classes/keras_callbacks#transformers.PushToHubCallback)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback + +>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset) +>>> push_to_hub_callback = PushToHubCallback( +... output_dir="food_classifier", +... tokenizer=image_processor, +... save_strategy="no", +... ) +>>> callbacks = [metric_callback, push_to_hub_callback] +``` + +์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์„ธํŠธ, ์—ํญ ์ˆ˜์™€ ํ•จ๊ป˜ `fit()`์„ ํ˜ธ์ถœํ•˜๊ณ , +์ฝœ๋ฐฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks) +Epoch 1/5 +250/250 [==============================] - 313s 1s/step - loss: 2.5623 - val_loss: 1.4161 - accuracy: 0.9290 +Epoch 2/5 +250/250 [==============================] - 265s 1s/step - loss: 0.9181 - val_loss: 0.6808 - accuracy: 0.9690 +Epoch 3/5 +250/250 [==============================] - 252s 1s/step - loss: 0.3910 - val_loss: 0.4303 - accuracy: 0.9820 +Epoch 4/5 +250/250 [==============================] - 251s 1s/step - loss: 0.2028 - val_loss: 0.3191 - accuracy: 0.9900 +Epoch 5/5 +250/250 [==============================] - 238s 949ms/step - loss: 0.1232 - val_loss: 0.3259 - accuracy: 0.9890 +``` + +์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ณ  ๐Ÿค— Hub์— ๊ณต์œ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + + + + + + +์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ์ž์„ธํ•œ ์˜ˆ์ œ๋Š” ๋‹ค์Œ [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +## ์ถ”๋ก [[inference]] + +์ข‹์•„์š”, ์ด์ œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์œผ๋‹ˆ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค: + +```py +>>> ds = load_dataset("food101", split="validation[:10]") +>>> image = ds["image"][0] +``` + +
+ image of beignets +
+ +๋ฏธ์„ธ ์กฐ์ • ๋ชจ๋ธ๋กœ ์ถ”๋ก ์„ ์‹œ๋„ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ๋กœ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ `pipeline`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import pipeline + +>>> classifier = pipeline("image-classification", model="my_awesome_food_model") +>>> classifier(image) +[{'score': 0.31856709718704224, 'label': 'beignets'}, + {'score': 0.015232225880026817, 'label': 'bruschetta'}, + {'score': 0.01519392803311348, 'label': 'chicken_wings'}, + {'score': 0.013022331520915031, 'label': 'pork_chop'}, + {'score': 0.012728818692266941, 'label': 'prime_rib'}] +``` + +์›ํ•œ๋‹ค๋ฉด, `pipeline`์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ณต์ œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: + + + +์ด๋ฏธ์ง€๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  `input`์„ PyTorch ํ…์„œ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoImageProcessor +>>> import torch + +>>> image_processor = AutoImageProcessor.from_pretrained("my_awesome_food_model") +>>> inputs = image_processor(image, return_tensors="pt") +``` + +์ž…๋ ฅ์„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ณ  logits์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForImageClassification + +>>> model = AutoModelForImageClassification.from_pretrained("my_awesome_food_model") +>>> with torch.no_grad(): +... logits = model(**inputs).logits +``` + +ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์˜ค๊ณ , ๋ชจ๋ธ์˜ `id2label` ๋งคํ•‘์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> predicted_label = logits.argmax(-1).item() +>>> model.config.id2label[predicted_label] +'beignets' +``` + + + + + +์ด๋ฏธ์ง€๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  `input`์„ TensorFlow ํ…์„œ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoImageProcessor + +>>> image_processor = AutoImageProcessor.from_pretrained("MariaK/food_classifier") +>>> inputs = image_processor(image, return_tensors="tf") +``` + +์ž…๋ ฅ์„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ณ  logits์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForImageClassification + +>>> model = TFAutoModelForImageClassification.from_pretrained("MariaK/food_classifier") +>>> logits = model(**inputs).logits +``` + +ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์˜ค๊ณ , ๋ชจ๋ธ์˜ `id2label` ๋งคํ•‘์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0]) +>>> model.config.id2label[predicted_class_id] +'beignets' +``` + + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_feature_extraction.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_feature_extraction.md new file mode 100644 index 0000000000000000000000000000000000000000..dac40e590b420521d92c3afe9b245f92d3f7002c --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_feature_extraction.md @@ -0,0 +1,136 @@ + + +# ์ด๋ฏธ์ง€ ํŠน์ง• ์ถ”์ถœ[[image-feature-extraction]] + +[[open-in-colab]] + +์ด๋ฏธ์ง€ ํŠน์ง• ์ถ”์ถœ์€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์—์„œ ์˜๋ฏธ๋ก ์ ์œผ๋กœ ์˜๋ฏธ ์žˆ๋Š” ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ด๋ฏธ์ง€ ์œ ์‚ฌ์„ฑ ๋ฐ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ ๋“ฑ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ ์‚ฌ๋ก€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. +๊ฒŒ๋‹ค๊ฐ€ ๋Œ€๋ถ€๋ถ„์˜ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ชจ๋ธ์€ ์ด๋ฏธ์ง€ ํŠน์ง• ์ถ”์ถœ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฌ๊ธฐ์„œ ์ž‘์—… ํŠนํ™” ํ—ค๋“œ(์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜, ๋ฌผ์ฒด ๊ฐ์ง€ ๋“ฑ)๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํŠน์ง•์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์ง•์€ ๊ฐ€์žฅ์ž๋ฆฌ ๊ฐ์ง€, ๋ชจ์„œ๋ฆฌ ๊ฐ์ง€ ๋“ฑ ๊ณ ์ฐจ์› ์ˆ˜์ค€์—์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋˜ํ•œ ๋ชจ๋ธ์˜ ๊นŠ์ด์— ๋”ฐ๋ผ ์‹ค์ œ ์„ธ๊ณ„์— ๋Œ€ํ•œ ์ •๋ณด(์˜ˆ: ๊ณ ์–‘์ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€)๋ฅผ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ์ถœ๋ ฅ์€ ํŠน์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š”: + +- `image-feature-extraction` ํŒŒ์ดํ”„๋ผ์ธ์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์ด๋ฏธ์ง€ ์œ ์‚ฌ์„ฑ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. +- ๊ธฐ๋ณธ ๋ชจ๋ธ ์ถ”๋ก ์œผ๋กœ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. + +## `image-feature-extraction` ํŒŒ์ดํ”„๋ผ์ธ์„ ์ด์šฉํ•œ ์ด๋ฏธ์ง€ ์œ ์‚ฌ์„ฑ[[image-similarity-using-image-feature-extraction-pipeline]] + +๋ฌผ๊ณ ๊ธฐ ๊ทธ๋ฌผ ์œ„์— ์•‰์•„ ์žˆ๋Š” ๋‘ ์žฅ์˜ ๊ณ ์–‘์ด ์‚ฌ์ง„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘ ํ•˜๋‚˜๋Š” ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. + +```python +from PIL import Image +import requests + +img_urls = ["https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.jpeg"] +image_real = Image.open(requests.get(img_urls[0], stream=True).raw).convert("RGB") +image_gen = Image.open(requests.get(img_urls[1], stream=True).raw).convert("RGB") +``` + +ํŒŒ์ดํ”„๋ผ์ธ์„ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ๋จผ์ € ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•˜์„ธ์š”. ๋ชจ๋ธ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, ํŒŒ์ดํ”„๋ผ์ธ์€ ์ž๋™์œผ๋กœ [google/vit-base-patch16-224](google/vit-base-patch16-224) ๋ชจ๋ธ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด `pool`์„ True๋กœ ์„ค์ •ํ•˜์„ธ์š”. + + +```python +import torch +from transformers import pipeline, infer_device + +DEVICE = infer_device() +pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True) +``` + +`pipe`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก ํ•˜๋ ค๋ฉด ๋‘ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋‘ ์ „๋‹ฌํ•˜์„ธ์š”. + +```python +outputs = pipe([image_real, image_gen]) +``` + +์ถœ๋ ฅ์—๋Š” ๋‘ ์ด๋ฏธ์ง€์˜ ํ’€๋ง๋œ(pooled) ์ž„๋ฒ ๋”ฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +# ๋‹จ์ผ ์ถœ๋ ฅ์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ +print(len(outputs[0][0])) +# ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ‘œ์‹œํ•˜๊ธฐ +print(outputs) + +# 768 +# [[[-0.03909236937761307, 0.43381670117378235, -0.06913255900144577, +``` + +์œ ์‚ฌ๋„ ์ ์ˆ˜๋ฅผ ์–ป์œผ๋ ค๋ฉด, ์ด๋“ค์„ ์œ ์‚ฌ๋„ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +from torch.nn.functional import cosine_similarity + +similarity_score = cosine_similarity(torch.Tensor(outputs[0]), + torch.Tensor(outputs[1]), dim=1) + +print(similarity_score) + +# tensor([0.6043]) +``` + +ํ’€๋ง ์ด์ „์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด, `pool` ๋งค๊ฐœ๋ณ€์ˆ˜์— ์•„๋ฌด ๊ฐ’๋„ ์ „๋‹ฌํ•˜์ง€ ๋งˆ์„ธ์š”. ๋˜ํ•œ, ๊ธฐ๋ณธ๊ฐ’์€ `False`๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์€๋‹‰ ์ƒํƒœ๋Š” ๋ชจ๋ธ์˜ ํŠน์ง•์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ๋ถ„๋ฅ˜๊ธฐ๋‚˜ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + +```python +pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-224", device=DEVICE) +output = pipe(image_real) +``` + +์•„์ง ์ถœ๋ ฅ์ด ํ’€๋ง๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์€ ๋ฐฐ์น˜ ํฌ๊ธฐ์ด๊ณ  ๋งˆ์ง€๋ง‰ ๋‘ ์ฐจ์›์€ ์ž„๋ฒ ๋”ฉ ํ˜•ํƒœ์ธ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +import numpy as np +print(np.array(outputs).shape) +# (1, 197, 768) +``` + +## `AutoModel`์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ง•๊ณผ ์œ ์‚ฌ์„ฑ ์–ป๊ธฐ[[getting-features-and-similarities-using-automodel]] + +transformers์˜ `AutoModel` ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ง•์„ ์–ป์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. `AutoModel`์€ ์ž‘์—… ํŠนํ™” ํ—ค๋“œ ์—†์ด ๋ชจ๋“  transformers ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ํŠน์ง•์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import AutoImageProcessor, AutoModel + +processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") +model = AutoModel.from_pretrained("google/vit-base-patch16-224").to(DEVICE) +``` + +์ถ”๋ก ์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ž…๋ ฅ๊ฐ’์„ `processor`์— ์ „๋‹ฌํ•œ ๋‹ค์Œ, ๊ทธ ์ถœ๋ ฅ๊ฐ’์„ `model`์— ์ „๋‹ฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```python +def infer(image): + inputs = processor(image, return_tensors="pt").to(DEVICE) + outputs = model(**inputs) + return outputs.pooler_output +``` + +์ด ํ•จ์ˆ˜์— ์ด๋ฏธ์ง€๋ฅผ ์ง์ ‘ ์ „๋‹ฌํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +embed_real = infer(image_real) +embed_gen = infer(image_gen) +``` + +๊ทธ๋ฆฌ๊ณ  ์ด ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์‹œ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +from torch.nn.functional import cosine_similarity + +similarity_score = cosine_similarity(embed_real, embed_gen, dim=1) +print(similarity_score) + +# tensor([0.6061], device='cuda:0', grad_fn=) +``` diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_to_image.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_to_image.md new file mode 100644 index 0000000000000000000000000000000000000000..f76122f78445055872188d382bec93cf3af75799 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/image_to_image.md @@ -0,0 +1,132 @@ + + +# Image-to-Image ์ž‘์—… ๊ฐ€์ด๋“œ [[image-to-image-task-guide]] + +[[open-in-colab]] + +Image-to-Image ์ž‘์—…์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋˜ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์ด๋ฏธ์ง€ ํ–ฅ์ƒ(์ดˆ๊ณ ํ•ด์ƒ๋„, ์ €์กฐ๋„ ํ–ฅ์ƒ, ๋น—์ค„๊ธฐ ์ œ๊ฑฐ ๋“ฑ), ์ด๋ฏธ์ง€ ๋ณต์› ๋“ฑ ๋‹ค์–‘ํ•œ ํ•˜์œ„ ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. +- ์ดˆ๊ณ ํ•ด์ƒ๋„ ์ž‘์—…์„ ์œ„ํ•œ image-to-image ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉ, +- ํŒŒ์ดํ”„๋ผ์ธ ์—†์ด ๋™์ผํ•œ ์ž‘์—…์„ ์œ„ํ•œ image-to-image ๋ชจ๋ธ ์‹คํ–‰ + +์ด ๊ฐ€์ด๋“œ๊ฐ€ ๋ฐœํ‘œ๋œ ์‹œ์ ์—์„œ๋Š”, `image-to-image` ํŒŒ์ดํ”„๋ผ์ธ์€ ์ดˆ๊ณ ํ•ด์ƒ๋„ ์ž‘์—…๋งŒ ์ง€์›ํ•œ๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•˜์„ธ์š”. + +ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + +```bash +pip install transformers +``` + +์ด์ œ [Swin2SR ๋ชจ๋ธ](https://huggingface.co/caidas/swin2SR-lightweight-x2-64)์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋ฏธ์ง€์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ด ํŒŒ์ดํ”„๋ผ์ธ์—์„œ๋Š” [Swin2SR ๋ชจ๋ธ](https://huggingface.co/caidas/swin2SR-lightweight-x2-64)๋งŒ ์ง€์›๋ฉ๋‹ˆ๋‹ค. + +```python +from transformers import pipeline + +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') +pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device) +``` + +์ด์ œ ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์™€ ๋ด…์‹œ๋‹ค. + +```python +from PIL import Image +import requests + +url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/cat.jpg" +image = Image.open(requests.get(url, stream=True).raw) + +print(image.size) +``` +```bash +# (532, 432) +``` +
+ Photo of a cat +
+ +์ด์ œ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์–‘์ด ์ด๋ฏธ์ง€์˜ ์—…์Šค์ผ€์ผ๋œ ๋ฒ„์ „์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +upscaled = pipe(image) +print(upscaled.size) +``` +```bash +# (1072, 880) +``` + +ํŒŒ์ดํ”„๋ผ์ธ ์—†์ด ์ง์ ‘ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด Transformers์˜ `Swin2SRForImageSuperResolution` ๋ฐ `Swin2SRImageProcessor` ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋™์ผํ•œ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ํ”„๋กœ์„ธ์„œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import Swin2SRForImageSuperResolution, Swin2SRImageProcessor + +model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-lightweight-x2-64").to(device) +processor = Swin2SRImageProcessor("caidas/swin2SR-lightweight-x2-64") +``` + +`pipeline` ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ „์ฒ˜๋ฆฌ์™€ ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋ฅผ ์ถ”์ƒํ™”ํ•˜๋ฏ€๋กœ, ์ด๋ฏธ์ง€๋ฅผ ์ „์ฒ˜๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌํ•œ ๋‹ค์Œ ํ”ฝ์…€๊ฐ’์„ GPU๋กœ ์ด๋™์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +pixel_values = processor(image, return_tensors="pt").pixel_values +print(pixel_values.shape) + +pixel_values = pixel_values.to(device) +``` + +์ด์ œ ํ”ฝ์…€๊ฐ’์„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +import torch + +with torch.no_grad(): + outputs = model(pixel_values) +``` +์ถœ๋ ฅ์€ ์•„๋ž˜์™€ ๊ฐ™์€ `ImageSuperResolutionOutput` ์œ ํ˜•์˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค ๐Ÿ‘‡ + +``` +(loss=None, reconstruction=tensor([[[[0.8270, 0.8269, 0.8275, ..., 0.7463, 0.7446, 0.7453], + [0.8287, 0.8278, 0.8283, ..., 0.7451, 0.7448, 0.7457], + [0.8280, 0.8273, 0.8269, ..., 0.7447, 0.7446, 0.7452], + ..., + [0.5923, 0.5933, 0.5924, ..., 0.0697, 0.0695, 0.0706], + [0.5926, 0.5932, 0.5926, ..., 0.0673, 0.0687, 0.0705], + [0.5927, 0.5914, 0.5922, ..., 0.0664, 0.0694, 0.0718]]]], + device='cuda:0'), hidden_states=None, attentions=None) +``` +`reconstruction`๋ฅผ ๊ฐ€์ ธ์™€ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด ํ›„์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€ ์‚ดํŽด๋ด…์‹œ๋‹ค. + +```python +outputs.reconstruction.data.shape +# torch.Size([1, 3, 880, 1072]) +``` + +์ถœ๋ ฅ ํ…์„œ์˜ ์ฐจ์›์„ ์ถ•์†Œํ•˜๊ณ  0๋ฒˆ์งธ ์ถ•์„ ์ œ๊ฑฐํ•œ ๋‹ค์Œ, ๊ฐ’์„ ํด๋ฆฌํ•‘ํ•˜๊ณ  NumPy ๋ถ€๋™์†Œ์ˆ˜์  ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ [1072, 880] ๋ชจ์–‘์„ ๊ฐ–๋„๋ก ์ถ•์„ ์žฌ์ •๋ ฌํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ถœ๋ ฅ์„ 0๊ณผ 255 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–๋„๋ก ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค. + +```python +import numpy as np + +# ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ณ , CPU๋กœ ์ด๋™ํ•˜๊ณ , ๊ฐ’์„ ํด๋ฆฌํ•‘ +output = outputs.reconstruction.data.squeeze().cpu().clamp_(0, 1).numpy() +# ์ถ•์„ ์žฌ์ •๋ ฌ +output = np.moveaxis(output, source=0, destination=-1) +# ๊ฐ’์„ ํ”ฝ์…€๊ฐ’ ๋ฒ”์œ„๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ +output = (output * 255.0).round().astype(np.uint8) +Image.fromarray(output) +``` +
+ Upscaled photo of a cat +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/keypoint_detection.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/keypoint_detection.md new file mode 100644 index 0000000000000000000000000000000000000000..bffca9b284cf75c605ee8b07e599ed05f7fbe534 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/keypoint_detection.md @@ -0,0 +1,155 @@ + + +# ํ‚คํฌ์ธํŠธ ํƒ์ง€ [[keypoint-detection]] + +[[open-in-colab]] + +ํ‚คํฌ์ธํŠธ ๊ฐ์ง€(Keypoint detection)์€ ์ด๋ฏธ์ง€ ๋‚ด์˜ ํŠน์ • ํฌ์ธํŠธ๋ฅผ ์‹๋ณ„ํ•˜๊ณ  ์œ„์น˜๋ฅผ ํƒ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ‚คํฌ์ธํŠธ๋Š” ๋žœ๋“œ๋งˆํฌ๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋ฉฐ ์–ผ๊ตด ํŠน์ง•์ด๋‚˜ ๋ฌผ์ฒด์˜ ์ผ๋ถ€์™€ ๊ฐ™์€ ์˜๋ฏธ ์žˆ๋Š” ํŠน์ง•์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. +ํ‚คํฌ์ธํŠธ ๊ฐ์ง€ ๋ชจ๋ธ๋“ค์€ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์•„๋ž˜์™€ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +- **ํ‚คํฌ์ธํŠธ๋“ค๊ณผ ์ ์ˆ˜**: ๊ด€์‹ฌ ํฌ์ธํŠธ๋“ค๊ณผ ํ•ด๋‹น ํฌ์ธํŠธ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„ ์ ์ˆ˜ +- **๋””์Šคํฌ๋ฆฝํ„ฐ(Descriptors)**: ๊ฐ ํ‚คํฌ์ธํŠธ๋ฅผ ๋‘˜๋Ÿฌ์‹ผ ์ด๋ฏธ์ง€ ์˜์—ญ์˜ ํ‘œํ˜„์œผ๋กœ ํ…์Šค์ฒ˜, ๊ทธ๋ผ๋ฐ์ด์…˜, ๋ฐฉํ–ฅ ๋ฐ ๊ธฐํƒ€ ์†์„ฑ์„ ์บก์ฒ˜ํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ฒˆ ๊ฐ€์ด๋“œ์—์„œ๋Š” ์ด๋ฏธ์ง€์—์„œ ํ‚คํฌ์ธํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃจ์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +์ด๋ฒˆ ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ํ‚คํฌ์ธํŠธ ๊ฐ์ง€์˜ ๊ธฐ๋ณธ์ด ๋˜๋Š” ๋ชจ๋ธ์ธ [SuperPoint](./model_doc/superpoint)๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import AutoImageProcessor, SuperPointForKeypointDetection +processor = AutoImageProcessor.from_pretrained("magic-leap-community/superpoint") +model = SuperPointForKeypointDetection.from_pretrained("magic-leap-community/superpoint") +``` +์•„๋ž˜์˜ ์ด๋ฏธ์ง€๋กœ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +
+ Bee + Cats +
+ + +```python +import torch +from PIL import Image +import requests +import cv2 + + +url_image_1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" +image_1 = Image.open(requests.get(url_image_1, stream=True).raw) +url_image_2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png" +image_2 = Image.open(requests.get(url_image_2, stream=True).raw) + +images = [image_1, image_2] +``` + +์ด์ œ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ถ”๋ก ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + + +```python +inputs = processor(images,return_tensors="pt").to(model.device, model.dtype) +outputs = model(**inputs) +``` +๋ชจ๋ธ ์ถœ๋ ฅ์—๋Š” ๋ฐฐ์น˜ ๋‚ด์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ƒ๋Œ€์ ์ธ ํ‚คํฌ์ธํŠธ, ๋””์Šคํฌ๋ฆฝํ„ฐ, ๋งˆ์Šคํฌ์™€ ์ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ๋Š” ์ด๋ฏธ์ง€์—์„œ ํ‚คํฌ์ธํŠธ๊ฐ€ ์žˆ๋Š” ์˜์—ญ์„ ๊ฐ•์กฐํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. + +```python +SuperPointKeypointDescriptionOutput(loss=None, keypoints=tensor([[[0.0437, 0.0167], + [0.0688, 0.0167], + [0.0172, 0.0188], + ..., + [0.5984, 0.9812], + [0.6953, 0.9812]]]), + scores=tensor([[0.0056, 0.0053, 0.0079, ..., 0.0125, 0.0539, 0.0377], + [0.0206, 0.0058, 0.0065, ..., 0.0000, 0.0000, 0.0000]], + grad_fn=), descriptors=tensor([[[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + ...], + grad_fn=), mask=tensor([[1, 1, 1, ..., 1, 1, 1], + [1, 1, 1, ..., 0, 0, 0]], dtype=torch.int32), hidden_states=None) +``` + +์ด๋ฏธ์ง€์— ์‹ค์ œ ํ‚คํฌ์ธํŠธ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ฒฐ๊ณผ๊ฐ’์„ ํ›„์ฒ˜๋ฆฌ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์‹ค์ œ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ๊ฒฐ๊ณผ๊ฐ’๊ณผ ํ•จ๊ป˜ `post_process_keypoint_detection`์— ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +image_sizes = [(image.size[1], image.size[0]) for image in images] +outputs = processor.post_process_keypoint_detection(outputs, image_sizes) +``` + +์œ„ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๊ฐ’์€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋˜๊ณ , ๊ฐ ๋”•์…”๋„ˆ๋ฆฌ๋“ค์€ ํ›„์ฒ˜๋ฆฌ๋œ ํ‚คํฌ์ธํŠธ, ์ ์ˆ˜ ๋ฐ ๋””์Šคํฌ๋ฆฝํ„ฐ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. + + +```python +[{'keypoints': tensor([[ 226, 57], + [ 356, 57], + [ 89, 64], + ..., + [3604, 3391]], dtype=torch.int32), + 'scores': tensor([0.0056, 0.0053, ...], grad_fn=), + 'descriptors': tensor([[-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357], + [-0.0807, 0.0114, -0.1210, ..., -0.1122, 0.0899, 0.0357]], + grad_fn=)}, + {'keypoints': tensor([[ 46, 6], + [ 78, 6], + [422, 6], + [206, 404]], dtype=torch.int32), + 'scores': tensor([0.0206, 0.0058, 0.0065, 0.0053, 0.0070, ...,grad_fn=), + 'descriptors': tensor([[-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211], + [-0.0525, 0.0726, 0.0270, ..., 0.0389, -0.0189, -0.0211]}] +``` + +์ด์ œ ์œ„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‚คํฌ์ธํŠธ๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +import matplotlib.pyplot as plt +import torch + +for i in range(len(images)): + keypoints = outputs[i]["keypoints"] + scores = outputs[i]["scores"] + descriptors = outputs[i]["descriptors"] + keypoints = outputs[i]["keypoints"].detach().numpy() + scores = outputs[i]["scores"].detach().numpy() + image = images[i] + image_width, image_height = image.size + + plt.axis('off') + plt.imshow(image) + plt.scatter( + keypoints[:, 0], + keypoints[:, 1], + s=scores * 100, + c='cyan', + alpha=0.4 + ) + plt.show() +``` + +์•„๋ž˜์—์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +
+ Bee + Cats +
+ diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/knowledge_distillation_for_image_classification.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/knowledge_distillation_for_image_classification.md new file mode 100644 index 0000000000000000000000000000000000000000..72cfbcf57aabad802d481e933c6f99057b525741 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/knowledge_distillation_for_image_classification.md @@ -0,0 +1,193 @@ + +# ์ปดํ“จํ„ฐ ๋น„์ „์„ ์œ„ํ•œ ์ง€์‹ ์ฆ๋ฅ˜[[Knowledge-Distillation-for-Computer-Vision]] + +[[open-in-colab]] + +์ง€์‹ ์ฆ๋ฅ˜(Knowledge distillation)๋Š” ๋” ํฌ๊ณ  ๋ณต์žกํ•œ ๋ชจ๋ธ(๊ต์‚ฌ)์—์„œ ๋” ์ž‘๊ณ  ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ(ํ•™์ƒ)๋กœ ์ง€์‹์„ ์ „๋‹ฌํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ํ•œ ๋ชจ๋ธ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋ธ๋กœ ์ง€์‹์„ ์ฆ๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด, ํŠน์ • ์ž‘์—…(์ด ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜)์— ๋Œ€ํ•ด ํ•™์Šต๋œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ต์‚ฌ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ณ , ๋žœ๋ค์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ํ•™์ƒ ๋ชจ๋ธ์„ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ž‘์—…์— ๋Œ€ํ•ด ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ, ํ•™์ƒ ๋ชจ๋ธ์ด ๊ต์‚ฌ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์„ ๋ชจ๋ฐฉํ•˜์—ฌ ๋‘ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ ์ฐจ์ด๋ฅผ ์ตœ์†Œํ™”ํ•˜๋„๋ก ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋ฒ•์€ Hinton ๋“ฑ ์—ฐ๊ตฌ์ง„์˜ [Distilling the Knowledge in a Neural Network](https://huggingface.co/papers/1503.02531)์—์„œ ์ฒ˜์Œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ํŠน์ • ์ž‘์—…์— ๋งž์ถ˜ ์ง€์‹ ์ฆ๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” [beans dataset](https://huggingface.co/datasets/beans)์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ๋Š” [๋ฏธ์„ธ ์กฐ์ •๋œ ViT ๋ชจ๋ธ](https://huggingface.co/merve/vit-mobilenet-beans-224) (๊ต์‚ฌ ๋ชจ๋ธ)์„ [MobileNet](https://huggingface.co/google/mobilenet_v2_1.4_224) (ํ•™์ƒ ๋ชจ๋ธ)์œผ๋กœ ์ฆ๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๐Ÿค— Transformers์˜ [Trainer API](https://huggingface.co/docs/transformers/en/main_classes/trainer#trainer) ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. + +์ฆ๋ฅ˜์™€ ๊ณผ์ • ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด ๋ด…์‹œ๋‹ค. + + +```bash +pip install transformers datasets accelerate tensorboard evaluate --upgrade +``` + +์ด ์˜ˆ์ œ์—์„œ๋Š” `merve/beans-vit-224` ๋ชจ๋ธ์„ ๊ต์‚ฌ ๋ชจ๋ธ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ beans ๋ฐ์ดํ„ฐ์…‹์—์„œ ํŒŒ์ธ ํŠœ๋‹๋œ `google/vit-base-patch16-224-in21k` ๊ธฐ๋ฐ˜์˜ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ MobileNetV2๋กœ ์ฆ๋ฅ˜ํ•ด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด์ œ ๋ฐ์ดํ„ฐ์…‹์„ ๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +from datasets import load_dataset + +dataset = load_dataset("beans") +``` + +์ด ๊ฒฝ์šฐ ๋‘ ๋ชจ๋ธ์˜ ์ด๋ฏธ์ง€ ํ”„๋กœ์„ธ์„œ๊ฐ€ ๋™์ผํ•œ ํ•ด์ƒ๋„๋กœ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‘๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์…‹์˜ ๋ชจ๋“  ๋ถ„ํ• ๋งˆ๋‹ค ์ „์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด `dataset`์˜ `map()` ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ ์ž…๋‹ˆ๋‹ค. + + +```python +from transformers import AutoImageProcessor +teacher_processor = AutoImageProcessor.from_pretrained("merve/beans-vit-224") + +def process(examples): + processed_inputs = teacher_processor(examples["image"]) + return processed_inputs + +processed_datasets = dataset.map(process, batched=True) +``` + +ํ•™์ƒ ๋ชจ๋ธ(๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ MobileNet)์ด ๊ต์‚ฌ ๋ชจ๋ธ(ํŒŒ์ธ ํŠœ๋‹๋œ ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ)์„ ๋ชจ๋ฐฉํ•˜๋„๋ก ํ•  ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋จผ์ € ๊ต์‚ฌ์™€ ํ•™์ƒ ๋ชจ๋ธ์˜ ๋กœ์ง“ ์ถœ๋ ฅ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ ์ถœ๋ ฅ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜ `temperature` ๊ฐ’์œผ๋กœ ๋‚˜๋ˆ„๋Š”๋ฐ, ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฐ ์†Œํ”„ํŠธ ํƒ€๊ฒŸ์˜ ์ค‘์š”๋„๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ `lambda` ๋Š” ์ฆ๋ฅ˜ ์†์‹ค์˜ ์ค‘์š”๋„์— ๊ฐ€์ค‘์น˜๋ฅผ ์ค๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” `temperature=5`์™€ `lambda=0.5`๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•™์ƒ๊ณผ ๊ต์‚ฌ ๊ฐ„์˜ ๋ฐœ์‚ฐ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด Kullback-Leibler Divergence ์†์‹ค์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฐ์ดํ„ฐ P์™€ Q๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, KL Divergence๋Š” Q๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ P๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์–ผ๋งŒํผ์˜ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ์ง€๋ฅผ ๋งํ•ด์ค๋‹ˆ๋‹ค. ๋‘ ๋ฐ์ดํ„ฐ๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด, KL Divergence๋Š” 0์ด๋ฉฐ, Q๋กœ P๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ฐ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ง€์‹ ์ฆ๋ฅ˜์˜ ๋งฅ๋ฝ์—์„œ KL Divergence๋Š” ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. + + +```python +from transformers import TrainingArguments, Trainer +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class ImageDistilTrainer(Trainer): + def __init__(self, teacher_model=None, student_model=None, temperature=None, lambda_param=None, *args, **kwargs): + super().__init__(model=student_model, *args, **kwargs) + self.teacher = teacher_model + self.student = student_model + self.loss_function = nn.KLDivLoss(reduction="batchmean") + device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') + self.teacher.to(device) + self.teacher.eval() + self.temperature = temperature + self.lambda_param = lambda_param + + def compute_loss(self, student, inputs, return_outputs=False): + student_output = self.student(**inputs) + + with torch.no_grad(): + teacher_output = self.teacher(**inputs) + + # ๊ต์‚ฌ์™€ ํ•™์ƒ์˜ ์†Œํ”„ํŠธ ํƒ€๊ฒŸ(soft targets) ๊ณ„์‚ฐ + + soft_teacher = F.softmax(teacher_output.logits / self.temperature, dim=-1) + soft_student = F.log_softmax(student_output.logits / self.temperature, dim=-1) + + # ์†์‹ค(loss) ๊ณ„์‚ฐ + distillation_loss = self.loss_function(soft_student, soft_teacher) * (self.temperature ** 2) + + # ์‹ค์ œ ๋ ˆ์ด๋ธ” ์†์‹ค ๊ณ„์‚ฐ + student_target_loss = student_output.loss + + # ์ตœ์ข… ์†์‹ค ๊ณ„์‚ฐ + loss = (1. - self.lambda_param) * student_target_loss + self.lambda_param * distillation_loss + return (loss, student_output) if return_outputs else loss +``` + +์ด์ œ Hugging Face Hub์— ๋กœ๊ทธ์ธํ•˜์—ฌ `Trainer`๋ฅผ ํ†ตํ•ด Hugging Face Hub์— ๋ชจ๋ธ์„ ํ‘ธ์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + + +```python +from huggingface_hub import notebook_login + +notebook_login() +``` + +์ด์ œ `TrainingArguments`, ๊ต์‚ฌ ๋ชจ๋ธ๊ณผ ํ•™์ƒ ๋ชจ๋ธ์„ ์„ค์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + + +```python +from transformers import AutoModelForImageClassification, MobileNetV2Config, MobileNetV2ForImageClassification + +training_args = TrainingArguments( + output_dir="my-awesome-model", + num_train_epochs=30, + fp16=True, + logging_dir=f"{repo_name}/logs", + logging_strategy="epoch", + eval_strategy="epoch", + save_strategy="epoch", + load_best_model_at_end=True, + metric_for_best_model="accuracy", + report_to="tensorboard", + push_to_hub=True, + hub_strategy="every_save", + hub_model_id=repo_name, + ) + +num_labels = len(processed_datasets["train"].features["labels"].names) + +# ๋ชจ๋ธ ์ดˆ๊ธฐํ™” +teacher_model = AutoModelForImageClassification.from_pretrained( + "merve/beans-vit-224", + num_labels=num_labels, + ignore_mismatched_sizes=True +) + +# MobileNetV2 ๋ฐ‘๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ํ•™์Šต +student_config = MobileNetV2Config() +student_config.num_labels = num_labels +student_model = MobileNetV2ForImageClassification(student_config) +``` + +`compute_metrics` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ์„ธํŠธ์—์„œ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๋ชจ๋ธ์˜ `accuracy`์™€ `f1`์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. + + +```python +import evaluate +import numpy as np + +accuracy = evaluate.load("accuracy") + +def compute_metrics(eval_pred): + predictions, labels = eval_pred + acc = accuracy.compute(references=labels, predictions=np.argmax(predictions, axis=1)) + return {"accuracy": acc["accuracy"]} +``` + +์ •์˜ํ•œ ํ›ˆ๋ จ ์ธ์ˆ˜๋กœ `Trainer`๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด๋ด…์‹œ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ(data collator)๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. + +```python +from transformers import DefaultDataCollator + +data_collator = DefaultDataCollator() +trainer = ImageDistilTrainer( + student_model=student_model, + teacher_model=teacher_model, + training_args=training_args, + train_dataset=processed_datasets["train"], + eval_dataset=processed_datasets["validation"], + data_collator=data_collator, + tokenizer=teacher_processor, + compute_metrics=compute_metrics, + temperature=5, + lambda_param=0.5 +) +``` + +์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +trainer.train() +``` + +๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธ ์„ธํŠธ์—์„œ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +trainer.evaluate(processed_datasets["test"]) +``` + + +ํ…Œ์ŠคํŠธ ์„ธํŠธ์—์„œ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋Š” 72%์— ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ๋ฅ˜์˜ ํšจ์œจ์„ฑ์„ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•ด ๋™์ผํ•œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋กœ beans ๋ฐ์ดํ„ฐ์…‹์—์„œ MobileNet์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ›ˆ๋ จํ•˜์˜€๊ณ , ํ…Œ์ŠคํŠธ ์„ธํŠธ์—์„œ์˜ ์ •ํ™•๋„๋Š” 63% ์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ต์‚ฌ ๋ชจ๋ธ, ํ•™์ƒ ๊ตฌ์กฐ, ์ฆ๋ฅ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‹œ๋„ํ•ด๋ณด์‹œ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ๋ฅ˜๋œ ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๋กœ๊ทธ์™€ ์ฒดํฌํฌ์ธํŠธ๋Š” [์ด ์ €์žฅ์†Œ](https://huggingface.co/merve/vit-mobilenet-beans-224)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฒ˜์Œ๋ถ€ํ„ฐ ํ›ˆ๋ จ๋œ MobileNetV2๋Š” ์ด [์ €์žฅ์†Œ](https://huggingface.co/merve/resnet-mobilenet-beans-5)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..1b531e49d6176df4c27e2bab1e96378535bccfc7 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/language_modeling.md @@ -0,0 +1,411 @@ + + +# ์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง[[causal-language-modeling]] + +[[open-in-colab]] + +์–ธ์–ด ๋ชจ๋ธ๋ง์€ ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง๊ณผ ๋งˆ์Šคํฌ๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง, ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. +์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ์ƒ์„ฑ์— ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋˜ ์ฐฝ์˜์ ์ธ ๋ฐฉํ–ฅ์œผ๋กœ ์‘์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ง์ ‘ ์‚ฌ์šฉํ•˜๋ฉฐ ์žฌ๋ฏธ์žˆ๋Š” ํƒ๊ตฌ๋ฅผ ํ•ด๋ณด๊ฑฐ๋‚˜, Copilot ๋˜๋Š” CodeParrot์™€ ๊ฐ™์€ ์ง€๋Šฅํ˜• ์ฝ”๋”ฉ ์–ด์‹œ์Šคํ„ดํŠธ์˜ ๊ธฐ๋ฐ˜์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. + + + +์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง์€ ํ† ํฐ ์‹œํ€€์Šค์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋ฉฐ, ๋ชจ๋ธ์€ ์™ผ์ชฝ์˜ ํ† ํฐ์—๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ด๋Š” ๋ชจ๋ธ์ด ๋ฏธ๋ž˜์˜ ํ† ํฐ์„ ๋ณผ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ๋กœ GPT-2๊ฐ€ ์žˆ์ฃ . + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค: + +1. [DistilGPT2](https://huggingface.co/distilbert/distilgpt2) ๋ชจ๋ธ์„ [ELI5](https://huggingface.co/datasets/eli5) ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ [r/askscience](https://www.reddit.com/r/askscience/) ํ•˜์œ„ ์ง‘ํ•ฉ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ • +2. ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์ถ”๋ก ์— ์‚ฌ์šฉ + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/text-generation)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate +``` + +์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•ด Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์•Œ๋ฆผ์ด ํ‘œ์‹œ๋˜๋ฉด ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•˜์„ธ์š”: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## ELI5 ๋ฐ์ดํ„ฐ ์„ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[load-eli5-dataset]] + +๋จผ์ €, ๐Ÿค— Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ r/askscience์˜ ์ž‘์€ ํ•˜์œ„ ์ง‘ํ•ฉ์ธ ELI5 ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. +์ด๋ฅผ ํ†ตํ•ด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ํ•™์Šตํ•˜๋Š” ๋ฐ ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ํˆฌ์žํ•˜๊ธฐ ์ „์—, ์‹คํ—˜ํ•ด๋ด„์œผ๋กœ์จ ๋ชจ๋“  ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5", split="train_asks[:5000]") +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ `train_asks` ๋ถ„ํ• ์„ [`~datasets.Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ์„ธํŠธ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”: + +```py +>>> eli5["train"][0] +{'answers': {'a_id': ['c3d1aib', 'c3d4lya'], + 'score': [6, 3], + 'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]}, + 'answers_urls': {'url': []}, + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']}, + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls': {'url': []}} +``` + +๋งŽ์•„ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” `text` ํ•„๋“œ๋งŒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—…์˜ ์žฅ์ ์€ ๋ ˆ์ด๋ธ”์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹จ์–ด *์ž์ฒด๊ฐ€* ๋ ˆ์ด๋ธ”์ž…๋‹ˆ๋‹ค. (์ด๋ ‡๊ฒŒ ๋ ˆ์ด๋ธ”์„ ์ œ๊ณตํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ํ•™์Šต์„ ๋น„์ง€๋„ ํ•™์Šต์ด๋ผ๊ณ  ์ผ์ปซ์Šต๋‹ˆ๋‹ค) + +## ์ „์ฒ˜๋ฆฌ[[preprocess]] + + + +๋‹ค์Œ ๋‹จ๊ณ„๋Š” `text` ํ•„๋“œ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด DistilGPT2 ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2") +``` + +์œ„์˜ ์˜ˆ์ œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, `text` ํ•„๋“œ๋Š” `answers` ์•„๋ž˜์— ์ค‘์ฒฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [`flatten`](https://huggingface.co/docs/datasets/process#flatten) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘์ฒฉ ๊ตฌ์กฐ์—์„œ `text` ํ•˜์œ„ ํ•„๋“œ๋ฅผ ์ถ”์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'answers.a_id': ['c3d1aib', 'c3d4lya'], + 'answers.score': [6, 3], + 'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"], + 'answers_urls.url': [], + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'], + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls.url': []} +``` + +๊ฐ ํ•˜์œ„ ํ•„๋“œ๋Š” ์ด์ œ `answers` ์ ‘๋‘์‚ฌ๋ฅผ ๊ฐ€์ง„ ๋ณ„๋„์˜ ์—ด๋กœ ๋‚˜๋‰˜์—ˆ์œผ๋ฉฐ, `text` ํ•„๋“œ๋Š” ์ด์ œ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์žฅ์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ํ† ํฐํ™”ํ•˜๋Š” ๋Œ€์‹ , ๋จผ์ € ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•œ๊บผ๋ฒˆ์— ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒฐํ•ฉํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค: + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +์ด ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์ ์šฉํ•˜๋ ค๋ฉด ๐Ÿค— Datasets [`~datasets.Dataset.map`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. `batched=True`๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์…‹์˜ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ณ , `num_proc`๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ํ”„๋กœ์„ธ์Šค ์ˆ˜๋ฅผ ๋Š˜๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š” ์—†๋Š” ์—ด์€ ์ œ๊ฑฐํ•˜์„ธ์š”: + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +์ด์ œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋Š” ์‹œํ€€์Šค๊ฐ€ ํ† ํฐํ™”๋์ง€๋งŒ, ์ผ๋ถ€ ์‹œํ€€์Šค๋Š” ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋ณด๋‹ค ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +์ด์ œ ๋‘ ๋ฒˆ์งธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ +- ๋ชจ๋“  ์‹œํ€€์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ , +- `block_size`๋กœ ์ •์˜๋œ ๊ธธ์ด๋กœ ์—ฐ๊ฒฐ๋œ ์‹œํ€€์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์งง์€ ๋ฌถ์Œ์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด์™€ GPU RAM์„ ๊ณ ๋ คํ•ด ์ถฉ๋ถ„ํžˆ ์งง์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... result["labels"] = result["input_ids"].copy() +... return result +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— `group_texts` ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์„ธ์š”: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ [`DataCollatorForLanguageModeling`]์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ œ์˜ ๋ฐฐ์น˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์„ธํŠธ ์ „์ฒด๋ฅผ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ์ทจํ•ฉ ๋‹จ๊ณ„์—์„œ ๊ฐ ๋ฐฐ์น˜์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๋ฌธ์žฅ์„ *๋™์ ์œผ๋กœ ํŒจ๋”ฉ*ํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. + + + +ํŒจ๋”ฉ ํ† ํฐ์œผ๋กœ ์ข…๊ฒฐ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜๊ณ  `mlm=False`๋กœ ์„ค์ •ํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž…๋ ฅ์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ์‹œํ”„ํŠธํ•œ ๊ฐ’์„ ๋ ˆ์ด๋ธ”๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False) +``` + + + +ํŒจ๋”ฉ ํ† ํฐ์œผ๋กœ ์ข…๊ฒฐ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜๊ณ  `mlm=False`๋กœ ์„ค์ •ํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž…๋ ฅ์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ์‹œํ”„ํŠธํ•œ ๊ฐ’์„ ๋ ˆ์ด๋ธ”๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False, return_tensors="tf") +``` + + + + + +## ํ›ˆ๋ จ[[train]] + + + + + +[`Trainer`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชจ๋ฅด์‹ ๋‹ค๋ฉด [๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ](../training#train-with-pytorch-trainer)์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! + + + +์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ธฐ ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! [`AutoModelForCausalLM`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DistilGPT2๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForCausalLM, TrainingArguments, Trainer + +>>> model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰ํ•˜๋ฉด ์„ธ ๋‹จ๊ณ„๋งŒ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค: + +1. [`TrainingArguments`]์—์„œ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜์„ธ์š”. `output_dir`์€ ์œ ์ผํ•œ ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ, ๋ชจ๋ธ์„ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. (๋จผ์ € Hugging Face์— ๋กœ๊ทธ์ธ ํ•„์ˆ˜) `push_to_hub=True`๋กœ ์„ค์ •ํ•˜์—ฌ ์ด ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +2. ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ [`Trainer`]์— ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ ๋ฐ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์ „๋‹ฌํ•˜์„ธ์š”. +3. [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์„ธ์š”. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_clm-model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด [`~transformers.Trainer.evaluate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ณ  ํผํ”Œ๋ ‰์„œํ‹ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 49.61 +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ [`~transformers.Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ๊ณต์œ ํ•˜์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด [๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ](../training#train-a-tensorflow-model-with-keras)์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! + + +TensorFlow์—์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋ ค๋ฉด, ๋จผ์ € ์˜ตํ‹ฐ๋งˆ์ด์ € ํ•จ์ˆ˜, ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„ ๋ฐ ์ผ๋ถ€ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•˜์„ธ์š”: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ [`TFAutoModelForCausalLM`]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DistilGPT2๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("distilbert/distilgpt2") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`]์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ `tf.data.Dataset` ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์„ธ์š”: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์„ฑํ•˜์„ธ์š”. Transformers ๋ชจ๋ธ์€ ๋ชจ๋‘ ๊ธฐ๋ณธ์ ์ธ ์ž‘์—… ๊ด€๋ จ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์›ํ•œ๋‹ค๋ฉด ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) # ๋ณ„๋„๋กœ loss ์ธ์ž๋ฅผ ๋„ฃ์ง€ ์•Š์•˜์–ด์š”! +``` + +[`~transformers.PushToHubCallback`]์—์„œ ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์—…๋กœ๋“œํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_clm-model", +... tokenizer=tokenizer, +... ) +``` + +๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด [`fit`](https://keras.io/api/models/model_training_apis/#fit-method)์„ ํ˜ธ์ถœํ•˜์„ธ์š”. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์„ธํŠธ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์„ธํŠธ, ์—ํญ ์ˆ˜ ๋ฐ ์ฝœ๋ฐฑ์„ ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋ธ์ด ์ž๋™์œผ๋กœ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋˜์–ด ๋ชจ๋‘๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + + + + + +์ธ๊ณผ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋” ์ž์„ธํ•œ ์˜ˆ์ œ๋Š” ํ•ด๋‹นํ•˜๋Š” [PyTorch ๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) ๋˜๋Š” [TensorFlow ๋…ธํŠธ๋ถ](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + + +## ์ถ”๋ก [[inference]] + +์ข‹์•„์š”, ์ด์ œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์œผ๋ฏ€๋กœ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +์ƒ์„ฑํ•  ํ…์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”: + +```py +>>> prompt = "Somatic hypermutation allows the immune system to" +``` + +์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ๊ฐ„๋‹จํžˆ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`pipeline`]์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์œ„ํ•œ `pipeline`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ํ…์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜์„ธ์š”: + +```py +>>> from transformers import pipeline + +>>> generator = pipeline("text-generation", model="my_awesome_eli5_clm-model") +>>> generator(prompt) +[{'generated_text': "Somatic hypermutation allows the immune system to be able to effectively reverse the damage caused by an infection.\n\n\nThe damage caused by an infection is caused by the immune system's ability to perform its own self-correcting tasks."}] +``` + + + +ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  `input_ids`๋ฅผ PyTorch ํ…์„œ๋กœ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="pt").input_ids +``` + +[`~generation.GenerationMixin.generate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”. ์ƒ์„ฑ์„ ์ œ์–ดํ•˜๋Š” ๋‹ค์–‘ํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต](../generation_strategies) ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +```py +>>> from transformers import AutoModelForCausalLM + +>>> model = AutoModelForCausalLM.from_pretrained("my_awesome_eli5_clm-model") +>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +์ƒ์„ฑ๋œ ํ† ํฐ ID๋ฅผ ๋‹ค์‹œ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•˜์„ธ์š”: + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +["Somatic hypermutation allows the immune system to react to drugs with the ability to adapt to a different environmental situation. In other words, a system of 'hypermutation' can help the immune system to adapt to a different environmental situation or in some cases even a single life. In contrast, researchers at the University of Massachusetts-Boston have found that 'hypermutation' is much stronger in mice than in humans but can be found in humans, and that it's not completely unknown to the immune system. A study on how the immune system"] +``` + + +ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  `input_ids`๋ฅผ TensorFlow ํ…์„œ๋กœ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_clm-model") +>>> inputs = tokenizer(prompt, return_tensors="tf").input_ids +``` + +[`~transformers.generation_tf_utils.TFGenerationMixin.generate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์•ฝ์„ ์ƒ์„ฑํ•˜์„ธ์š”. ์ƒ์„ฑ์„ ์ œ์–ดํ•˜๋Š” ๋‹ค์–‘ํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [ํ…์ŠคํŠธ ์ƒ์„ฑ ์ „๋žต](../generation_strategies) ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜์„ธ์š”. + +```py +>>> from transformers import TFAutoModelForCausalLM + +>>> model = TFAutoModelForCausalLM.from_pretrained("my_awesome_eli5_clm-model") +>>> outputs = model.generate(input_ids=inputs, max_new_tokens=100, do_sample=True, top_k=50, top_p=0.95) +``` + +์ƒ์„ฑ๋œ ํ† ํฐ ID๋ฅผ ๋‹ค์‹œ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•˜์„ธ์š”: + +```py +>>> tokenizer.batch_decode(outputs, skip_special_tokens=True) +['Somatic hypermutation allows the immune system to detect the presence of other viruses as they become more prevalent. Therefore, researchers have identified a high proportion of human viruses. The proportion of virus-associated viruses in our study increases with age. Therefore, we propose a simple algorithm to detect the presence of these new viruses in our samples as a sign of improved immunity. A first study based on this algorithm, which will be published in Science on Friday, aims to show that this finding could translate into the development of a better vaccine that is more effective for'] +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/mask_generation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/mask_generation.md new file mode 100644 index 0000000000000000000000000000000000000000..7a937399391b7149b36a8f1d1374b3b30fc5e798 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/mask_generation.md @@ -0,0 +1,228 @@ + + +# ๋งˆ์Šคํฌ ์ƒ์„ฑ[[mask-generation]] + +๋งˆ์Šคํฌ ์ƒ์„ฑ(Mask generation)์€ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์˜๋ฏธ ์žˆ๋Š” ๋งˆ์Šคํฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. +์ด ์ž‘์—…์€ [์ด๋ฏธ์ง€ ๋ถ„ํ• ](semantic_segmentation)๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋งŽ์€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ์€ ๋ผ๋ฒจ์ด ๋‹ฌ๋ฆฐ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ํ•™์Šต๋˜๋ฉฐ, ํ•™์Šต ์ค‘์— ๋ณธ ํด๋ž˜์Šค๋“ค๋กœ๋งŒ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๋งˆ์Šคํฌ์™€ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. + +๋ฐ˜๋ฉด, ๋งˆ์Šคํฌ ์ƒ์„ฑ ๋ชจ๋ธ์€ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋˜๋ฉฐ ๋‘ ๊ฐ€์ง€ ๋ชจ๋“œ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. +- ํ”„๋กฌํ”„ํŠธ ๋ชจ๋“œ(Prompting mode): ์ด ๋ชจ๋“œ์—์„œ๋Š” ๋ชจ๋ธ์ด ์ด๋ฏธ์ง€์™€ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ๋Š” ์ด๋ฏธ์ง€ ๋‚ด ๊ฐ์ฒด์˜ 2D ์ขŒํ‘œ(XY ์ขŒํ‘œ)๋‚˜ ๊ฐ์ฒด๋ฅผ ๋‘˜๋Ÿฌ์‹ผ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ ๋ชจ๋“œ์—์„œ๋Š” ๋ชจ๋ธ์ด ํ”„๋กฌํ”„ํŠธ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด์˜ ๋งˆ์Šคํฌ๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +- ์ „์ฒด ๋ถ„ํ•  ๋ชจ๋“œ(Segment Everything mode): ์ด ๋ชจ๋“œ์—์„œ๋Š” ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€ ๋‚ด์—์„œ ๋ชจ๋“  ๋งˆ์Šคํฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๊ทธ๋ฆฌ๋“œ ํ˜•ํƒœ์˜ ์ ๋“ค์„ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ์ด๋ฏธ์ง€์— ์˜ค๋ฒ„๋ ˆ์ดํ•˜์—ฌ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค. + +๋งˆ์Šคํฌ ์ƒ์„ฑ ์ž‘์—…์€ [์ „์ฒด ๋ถ„ํ•  ๋ชจ๋“œ(Segment Anything Model, SAM)](model_doc/sam)์— ์˜ํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. SAM์€ Vision Transformer ๊ธฐ๋ฐ˜ ์ด๋ฏธ์ง€ ์ธ์ฝ”๋”, ํ”„๋กฌํ”„ํŠธ ์ธ์ฝ”๋”, ๊ทธ๋ฆฌ๊ณ  ์–‘๋ฐฉํ–ฅ ํŠธ๋žœ์Šคํฌ๋จธ ๋งˆ์Šคํฌ ๋””์ฝ”๋”๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ•๋ ฅํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์™€ ํ”„๋กฌํ”„ํŠธ๋Š” ์ธ์ฝ”๋”ฉ๋˜๊ณ , ๋””์ฝ”๋”๋Š” ์ด๋Ÿฌํ•œ ์ž„๋ฒ ๋”ฉ์„ ๋ฐ›์•„ ์œ ํšจํ•œ ๋งˆ์Šคํฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. + +
+ SAM Architecture +
+ +SAM์€ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋ถ„ํ•  ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 100๋งŒ ๊ฐœ์˜ ์ด๋ฏธ์ง€์™€ 11์–ต ๊ฐœ์˜ ๋งˆ์Šคํฌ๋ฅผ ํฌํ•จํ•˜๋Š” [SA-1B](https://ai.meta.com/datasets/segment-anything/) ๋ฐ์ดํ„ฐ ์„ธํŠธ๋กœ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. + +์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋ฐฐ์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: +- ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ์™€ ํ•จ๊ป˜ ์ „์ฒด ๋ถ„ํ•  ๋ชจ๋“œ์—์„œ ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ• +- ํฌ์ธํŠธ ํ”„๋กฌํ”„ํŒ… ๋ชจ๋“œ์—์„œ ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ• +- ๋ฐ•์Šค ํ”„๋กฌํ”„ํŒ… ๋ชจ๋“œ์—์„œ ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ• + +๋จผ์ €, `transformers`๋ฅผ ์„ค์น˜ํ•ด ๋ด…์‹œ๋‹ค: + +```bash +pip install -q transformers +``` + +## ๋งˆ์Šคํฌ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ[[mask-generation-pipeline]] + +๋งˆ์Šคํฌ ์ƒ์„ฑ ๋ชจ๋ธ๋กœ ์ถ”๋ก ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ `mask-generation` ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +```python +>>> from transformers import pipeline + +>>> checkpoint = "facebook/sam-vit-base" +>>> mask_generator = pipeline(model=checkpoint, task="mask-generation") +``` + +์ด๋ฏธ์ง€๋ฅผ ์˜ˆ์‹œ๋กœ ๋ด…์‹œ๋‹ค. + +```python +from PIL import Image +import requests + +img_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg" +image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB") +``` + +
+ Example Image +
+ +์ „์ฒด์ ์œผ๋กœ ๋ถ„ํ• ํ•ด๋ด…์‹œ๋‹ค. `points-per-batch`๋Š” ์ „์ฒด ๋ถ„ํ•  ๋ชจ๋“œ์—์„œ ์ ๋“ค์˜ ๋ณ‘๋ ฌ ์ถ”๋ก ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ถ”๋ก  ์†๋„๊ฐ€ ๋นจ๋ผ์ง€์ง€๋งŒ, ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์†Œ๋ชจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, SAM์€ ์ด๋ฏธ์ง€๊ฐ€ ์•„๋‹Œ ์ ๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. `pred_iou_thresh`๋Š” IoU ์‹ ๋ขฐ ์ž„๊ณ„๊ฐ’์œผ๋กœ, ์ด ์ž„๊ณ„๊ฐ’์„ ์ดˆ๊ณผํ•˜๋Š” ๋งˆ์Šคํฌ๋งŒ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. + +```python +masks = mask_generator(image, points_per_batch=128, pred_iou_thresh=0.88) +``` + +`masks` ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค: + +```bash +{'masks': [array([[False, False, False, ..., True, True, True], + [False, False, False, ..., True, True, True], + [False, False, False, ..., True, True, True], + ..., + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False]]), + array([[False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + [False, False, False, ..., False, False, False], + ..., +'scores': tensor([0.9972, 0.9917, + ..., +} +``` + +์œ„ ๋‚ด์šฉ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```python +import matplotlib.pyplot as plt + +plt.imshow(image, cmap='gray') + +for i, mask in enumerate(masks["masks"]): + plt.imshow(mask, cmap='viridis', alpha=0.1, vmin=0, vmax=1) + +plt.axis('off') +plt.show() +``` + +์•„๋ž˜๋Š” ํšŒ์ƒ‰์กฐ ์›๋ณธ ์ด๋ฏธ์ง€์— ๋‹ค์ฑ„๋กœ์šด ์ƒ‰์ƒ์˜ ๋งต์„ ๊ฒน์ณ๋†“์€ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ๋งค์šฐ ์ธ์ƒ์ ์ธ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. + +
+ Visualized +
+ +## ๋ชจ๋ธ ์ถ”๋ก [[model-inference]] + +### ํฌ์ธํŠธ ํ”„๋กฌํ”„ํŒ…[[point-prompting]] + +ํŒŒ์ดํ”„๋ผ์ธ ์—†์ด๋„ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ๊ณผ ํ”„๋กœ์„ธ์„œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +from transformers import SamModel, SamProcessor +import torch + +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') + +model = SamModel.from_pretrained("facebook/sam-vit-base").to(device) +processor = SamProcessor.from_pretrained("facebook/sam-vit-base") +``` + +ํฌ์ธํŠธ ํ”„๋กฌํ”„ํŒ…์„ ํ•˜๊ธฐ ์œ„ํ•ด, ์ž…๋ ฅ ํฌ์ธํŠธ๋ฅผ ํ”„๋กœ์„ธ์„œ์— ์ „๋‹ฌํ•œ ๋‹ค์Œ, ํ”„๋กœ์„ธ์„œ ์ถœ๋ ฅ์„ ๋ฐ›์•„ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜์—ฌ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์ถœ๋ ฅ์„ ํ›„์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด, ์ถœ๋ ฅ๊ณผ ํ•จ๊ป˜ ํ”„๋กœ์„ธ์„œ์˜ ์ดˆ๊ธฐ ์ถœ๋ ฅ์—์„œ ๊ฐ€์ ธ์˜จ `original_sizes`์™€ `reshaped_input_sizes`๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ํ”„๋กœ์„ธ์„œ๊ฐ€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๊ณ  ์ถœ๋ ฅ์„ ์ถ”์ •ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. + +```python +input_points = [[[2592, 1728]]] # ๋ฒŒ์˜ ํฌ์ธํŠธ ์œ„์น˜ + +inputs = processor(image, input_points=input_points, return_tensors="pt").to(device) +with torch.no_grad(): + outputs = model(**inputs) +masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu()) +``` + +`masks` ์ถœ๋ ฅ์œผ๋กœ ์„ธ ๊ฐ€์ง€ ๋งˆ์Šคํฌ๋ฅผ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +import matplotlib.pyplot as plt +import numpy as np + +fig, axes = plt.subplots(1, 4, figsize=(15, 5)) + +axes[0].imshow(image) +axes[0].set_title('Original Image') +mask_list = [masks[0][0][0].numpy(), masks[0][0][1].numpy(), masks[0][0][2].numpy()] + +for i, mask in enumerate(mask_list, start=1): + overlayed_image = np.array(image).copy() + + overlayed_image[:,:,0] = np.where(mask == 1, 255, overlayed_image[:,:,0]) + overlayed_image[:,:,1] = np.where(mask == 1, 0, overlayed_image[:,:,1]) + overlayed_image[:,:,2] = np.where(mask == 1, 0, overlayed_image[:,:,2]) + + axes[i].imshow(overlayed_image) + axes[i].set_title(f'Mask {i}') +for ax in axes: + ax.axis('off') + +plt.show() +``` + +
+ Visualized +
+ +### ๋ฐ•์Šค ํ”„๋กฌํ”„ํŒ…[[box-prompting]] + +๋ฐ•์Šค ํ”„๋กฌํ”„ํŒ…๋„ ํฌ์ธํŠธ ํ”„๋กฌํ”„ํŒ…๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ•์Šค๋ฅผ `[x_min, y_min, x_max, y_max]` ํ˜•์‹์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•˜์—ฌ ์ด๋ฏธ์ง€์™€ ํ•จ๊ป˜ `processor`์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์„œ ์ถœ๋ ฅ์„ ๋ฐ›์•„ ๋ชจ๋ธ์— ์ง์ ‘ ์ „๋‹ฌํ•œ ํ›„, ๋‹ค์‹œ ์ถœ๋ ฅ์„ ํ›„์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + +```python +# ๋ฒŒ ์ฃผ์œ„์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค +box = [2350, 1600, 2850, 2100] + +inputs = processor( + image, + input_boxes=[[[box]]], + return_tensors="pt" + ).to("cuda") + +with torch.no_grad(): + outputs = model(**inputs) + +mask = processor.image_processor.post_process_masks( + outputs.pred_masks.cpu(), + inputs["original_sizes"].cpu(), + inputs["reshaped_input_sizes"].cpu() +)[0][0][0].numpy() +``` + +์ด์ œ ์•„๋ž˜์™€ ๊ฐ™์ด, ๋ฒŒ ์ฃผ์œ„์˜ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๋ฅผ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +import matplotlib.patches as patches + +fig, ax = plt.subplots() +ax.imshow(image) + +rectangle = patches.Rectangle((2350, 1600), 500, 500, linewidth=2, edgecolor='r', facecolor='none') +ax.add_patch(rectangle) +ax.axis("off") +plt.show() +``` + +
+ Visualized Bbox +
+ +์•„๋ž˜์—์„œ ์ถ”๋ก  ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```python +fig, ax = plt.subplots() +ax.imshow(image) +ax.imshow(mask, cmap='viridis', alpha=0.4) + +ax.axis("off") +plt.show() +``` + +
+ Visualized Inference +
diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/masked_language_modeling.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/masked_language_modeling.md new file mode 100644 index 0000000000000000000000000000000000000000..74df085c5b558fcaecc6828076a7cd4e9b6601c8 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/masked_language_modeling.md @@ -0,0 +1,441 @@ + + +# ๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง(Masked language modeling)[[masked-language-modeling]] + +[[open-in-colab]] + + + +๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์€ ์‹œํ€€์Šค์—์„œ ๋งˆ์Šคํ‚น๋œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋ฉฐ, ๋ชจ๋ธ์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ† ํฐ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +์ฆ‰, ๋ชจ๋ธ์€ ํ† ํฐ์˜ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์–‘์ชฝ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์€ ์ „์ฒด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ๋ฌธ๋งฅ์  ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…์— ์ ํ•ฉํ•˜๋ฉฐ, BERT๊ฐ€ ๊ทธ ์˜ˆ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. + +์ด๋ฒˆ ๊ฐ€์ด๋“œ์—์„œ ๋‹ค๋ฃฐ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +1. [ELI5](https://huggingface.co/datasets/eli5) ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ [r/askscience](https://www.reddit.com/r/askscience/) ๋ถ€๋ถ„์„ ์‚ฌ์šฉํ•ด [DistilRoBERTa](https://huggingface.co/distilbert/distilroberta-base) ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. +2. ์ถ”๋ก  ์‹œ์— ์ง์ ‘ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/fill-mask)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install transformers datasets evaluate +``` + +Hugging Face ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜์—ฌ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์™€์˜ ๊ณต์œ ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋˜๋ฉด(When prompted) ํ† ํฐ์„ ์ž…๋ ฅํ•˜์—ฌ ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from huggingface_hub import notebook_login + +>>> notebook_login() +``` + +## ELI5 ๋ฐ์ดํ„ฐ ์„ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ[[load-eli5-dataset]] + +๋จผ์ € ๐Ÿค— Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ELI5 ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ r/askscience ์ค‘ ์ผ๋ถ€๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. +์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ ํ•™์Šต์— ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ํ• ์• ํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š”์ง€ ์‹คํ—˜ํ•˜๊ณ  ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +```py +>>> from datasets import load_dataset + +>>> eli5 = load_dataset("eli5", split="train_asks[:5000]") +``` + +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ `train_asks`๋ฅผ [`~datasets.Dataset.train_test_split`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> eli5 = eli5.train_test_split(test_size=0.2) +``` + +๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”: + +```py +>>> eli5["train"][0] +{'answers': {'a_id': ['c3d1aib', 'c3d4lya'], + 'score': [6, 3], + 'text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"]}, + 'answers_urls': {'url': []}, + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls': {'url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg']}, + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls': {'url': []}} +``` + +๋งŽ์•„ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” `text` ํ•„๋“œ์—๋งŒ ์ง‘์ค‘ํ•˜๋ฉด ๋ฉ๋‚˜๋‹ค. +์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—…์˜ ๋ฉ‹์ง„ ์ ์€ (๋น„์ง€๋„ ํ•™์Šต์œผ๋กœ) *๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ๋ ˆ์ด๋ธ”*์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ ˆ์ด๋ธ”์ด ๋”ฐ๋กœ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +## ์ „์ฒ˜๋ฆฌ[[preprocess]] + + + +๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด, ๋‹ค์Œ ๋‹จ๊ณ„๋กœ DistilRoBERTa ํ† ํฌ๋‚˜์ด์ €๋ฅผ ๊ฐ€์ ธ์™€์„œ `text` ํ•˜์œ„ ํ•„๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilroberta-base") +``` + +์œ„์˜ ์˜ˆ์ œ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, `text` ํ•„๋“œ๋Š” `answers` ์•ˆ์— ์ค‘์ฒฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. +๋”ฐ๋ผ์„œ ์ค‘์ฒฉ๋œ ๊ตฌ์กฐ์—์„œ [`flatten`](https://huggingface.co/docs/datasets/process#flatten) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `text` ํ•˜์œ„ ํ•„๋“œ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> eli5 = eli5.flatten() +>>> eli5["train"][0] +{'answers.a_id': ['c3d1aib', 'c3d4lya'], + 'answers.score': [6, 3], + 'answers.text': ["The velocity needed to remain in orbit is equal to the square root of Newton's constant times the mass of earth divided by the distance from the center of the earth. I don't know the altitude of that specific mission, but they're usually around 300 km. That means he's going 7-8 km/s.\n\nIn space there are no other forces acting on either the shuttle or the guy, so they stay in the same position relative to each other. If he were to become unable to return to the ship, he would presumably run out of oxygen, or slowly fall into the atmosphere and burn up.", + "Hope you don't mind me asking another question, but why aren't there any stars visible in this photo?"], + 'answers_urls.url': [], + 'document': '', + 'q_id': 'nyxfp', + 'selftext': '_URL_0_\n\nThis was on the front page earlier and I have a few questions about it. Is it possible to calculate how fast the astronaut would be orbiting the earth? Also how does he stay close to the shuttle so that he can return safely, i.e is he orbiting at the same speed and can therefore stay next to it? And finally if his propulsion system failed, would he eventually re-enter the atmosphere and presumably die?', + 'selftext_urls.url': ['http://apod.nasa.gov/apod/image/1201/freeflyer_nasa_3000.jpg'], + 'subreddit': 'askscience', + 'title': 'Few questions about this space walk photograph.', + 'title_urls.url': []} +``` + +์ด์ œ ๊ฐ ํ•˜์œ„ ํ•„๋“œ๋Š” `answers` ์ ‘๋‘์‚ฌ(prefix)๋กœ ํ‘œ์‹œ๋œ ๋Œ€๋กœ ๋ณ„๋„์˜ ์—ด์ด ๋˜๊ณ , `text` ํ•„๋“œ๋Š” ์ด์ œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. +๊ฐ ๋ฌธ์žฅ์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ํ† ํฐํ™”ํ•˜๋Š” ๋Œ€์‹  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•œ๋ฒˆ์— ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. + +๋‹ค์Œ์€ ๊ฐ ์˜ˆ์ œ์— ๋Œ€ํ•ด ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ๋ฅผ `join`ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค: + +```py +>>> def preprocess_function(examples): +... return tokenizer([" ".join(x) for x in examples["answers.text"]]) +``` + +์ด ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๐Ÿค— Datasets [`~datasets.Dataset.map`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. +๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋„๋ก `batched=True`๋ฅผ ์„ค์ •ํ•˜๊ณ  `num_proc`๋กœ ์ฒ˜๋ฆฌ ํšŸ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๋ฉด `map` ํ•จ์ˆ˜์˜ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +ํ•„์š”ํ•˜์ง€ ์•Š์€ ์—ด์€ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> tokenized_eli5 = eli5.map( +... preprocess_function, +... batched=True, +... num_proc=4, +... remove_columns=eli5["train"].column_names, +... ) +``` + +์ด ๋ฐ์ดํ„ฐ ์„ธํŠธ์—๋Š” ํ† ํฐ ์‹œํ€€์Šค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ ์ด ์ค‘ ์ผ๋ถ€๋Š” ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋ณด๋‹ค ๊น๋‹ˆ๋‹ค. + +์ด์ œ ๋‘ ๋ฒˆ์งธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด +- ๋ชจ๋“  ์‹œํ€€์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  +- ์—ฐ๊ฒฐ๋œ ์‹œํ€€์Šค๋ฅผ ์ •์˜ํ•œ `block_size` ๋ณด๋‹ค ๋” ์งง์€ ๋ฉ์–ด๋ฆฌ๋กœ ๋ถ„ํ• ํ•˜๋Š”๋ฐ, ์ด ๋ฉ์–ด๋ฆฌ๋Š” ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ž…๋ ฅ ๊ธธ์ด๋ณด๋‹ค ์งง๊ณ  GPU RAM์ด ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. + + +```py +>>> block_size = 128 + + +>>> def group_texts(examples): +... # Concatenate all texts. +... concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} +... total_length = len(concatenated_examples[list(examples.keys())[0]]) +... # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can +... # customize this part to your needs. +... if total_length >= block_size: +... total_length = (total_length // block_size) * block_size +... # Split by chunks of block_size. +... result = { +... k: [t[i : i + block_size] for i in range(0, total_length, block_size)] +... for k, t in concatenated_examples.items() +... } +... result["labels"] = result["input_ids"].copy() +... return result +``` + +์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์— `group_texts` ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> lm_dataset = tokenized_eli5.map(group_texts, batched=True, num_proc=4) +``` + +์ด์ œ [`DataCollatorForLanguageModeling`]์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์˜ˆ์ œ์˜ ๋ฐฐ์น˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. +๋ฐ์ดํ„ฐ ์„ธํŠธ ์ „์ฒด๋ฅผ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค collation ๋‹จ๊ณ„์—์„œ ๋งค ๋ฐฐ์น˜์•ˆ์—์„œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๋ฌธ์žฅ์„ *๋™์ ์œผ๋กœ ํŒจ๋”ฉ*ํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. + + + + +์‹œํ€€์Šค ๋ ํ† ํฐ์„ ํŒจ๋”ฉ ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ํ† ํฐ์„ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚นํ•˜๋„๋ก `mlm_-probability`๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> tokenizer.pad_token = tokenizer.eos_token +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15) +``` + + + +์‹œํ€€์Šค ๋ ํ† ํฐ์„ ํŒจ๋”ฉ ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ํ† ํฐ์„ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚นํ•˜๋„๋ก `mlm_-probability`๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import DataCollatorForLanguageModeling + +>>> data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15, return_tensors="tf") +``` + + + +## ํ›ˆ๋ จ[[train]] + + + + + +[`Trainer`]๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐ ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ [์—ฌ๊ธฐ](../training#train-with-pytorch-trainer)๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”! + + +์ด์ œ ๋ชจ๋ธ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! [`AutoModelForMaskedLM`]๋ฅผ ์‚ฌ์šฉํ•ด DistilRoBERTa ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +์ด์ œ ์„ธ ๋‹จ๊ณ„๊ฐ€ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค: + +1. [`TrainingArguments`]์˜ ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์ €์žฅ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” `output_dir`์€ ์œ ์ผํ•œ ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. `push_to_hub=True`๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ด ๋ชจ๋ธ์„ Hub์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค (๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋ ค๋ฉด Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). +2. ๋ชจ๋ธ, ๋ฐ์ดํ„ฐ ์„ธํŠธ ๋ฐ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ์ดํ„ฐ(collator)์™€ ํ•จ๊ป˜ ํ›ˆ๋ จ ์ธ์ˆ˜๋ฅผ [`Trainer`]์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +3. [`~Trainer.train`]์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> training_args = TrainingArguments( +... output_dir="my_awesome_eli5_mlm_model", +... eval_strategy="epoch", +... learning_rate=2e-5, +... num_train_epochs=3, +... weight_decay=0.01, +... push_to_hub=True, +... ) + +>>> trainer = Trainer( +... model=model, +... args=training_args, +... train_dataset=lm_dataset["train"], +... eval_dataset=lm_dataset["test"], +... data_collator=data_collator, +... ) + +>>> trainer.train() +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด [`~transformers.Trainer.evaluate`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŽ„ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> import math + +>>> eval_results = trainer.evaluate() +>>> print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}") +Perplexity: 8.76 +``` + +๊ทธ๋ฆฌ๊ณ  [`~transformers.Trainer.push_to_hub`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก, Hub๋กœ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> trainer.push_to_hub() +``` + + + + +Keras๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐ ์ต์ˆ™ํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ธฐ๋ณธ ํŠœํ† ๋ฆฌ์–ผ [์—ฌ๊ธฐ](../training#train-a-tensorflow-model-with-keras)๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”! + + +TensorFlow๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €(optimizer) ํ•จ์ˆ˜ ์„ค์ •, ํ•™์Šต๋ฅ (learning rate) ์Šค์ผ€์ฅด๋ง, ํ›ˆ๋ จ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ •๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์„ธ์š”: + +```py +>>> from transformers import create_optimizer, AdamWeightDecay + +>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01) +``` + +๋‹ค์Œ์œผ๋กœ [`TFAutoModelForMaskedLM`]๋ฅผ ์‚ฌ์šฉํ•ด DistilRoBERTa ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("distilbert/distilroberta-base") +``` + +[`~transformers.TFPreTrainedModel.prepare_tf_dataset`] ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ `tf.data.Dataset` ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์„ธ์š”: + +```py +>>> tf_train_set = model.prepare_tf_dataset( +... lm_dataset["train"], +... shuffle=True, +... batch_size=16, +... collate_fn=data_collator, +... ) + +>>> tf_test_set = model.prepare_tf_dataset( +... lm_dataset["test"], +... shuffle=False, +... batch_size=16, +... collate_fn=data_collator, +... ) +``` + +[`compile`](https://keras.io/api/models/model_training_apis/#compile-method) ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ ํ›ˆ๋ จ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> import tensorflow as tf + +>>> model.compile(optimizer=optimizer) +``` + +์ด๋Š” ์—…๋กœ๋“œํ•  ๋ชจ๋ธ๊ณผ ํ† ํฌ๋‚˜์ด์ €์˜ ์œ„์น˜๋ฅผ [`~transformers.PushToHubCallback`]์— ์ง€์ •ํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers.keras_callbacks import PushToHubCallback + +>>> callback = PushToHubCallback( +... output_dir="my_awesome_eli5_mlm_model", +... tokenizer=tokenizer, +... ) +``` + +๋“œ๋””์–ด ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! +๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ๋•Œ ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์„ธํŠธ, ์—ํฌํฌ ์ˆ˜, ์ฝœ๋ฐฑ์ด ํฌํ•จ๋œ [`fit`](https://keras.io/api/models/model_training_apis/#fit-method)์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=[callback]) +``` + +ํ›ˆ๋ จ์ด ์™„๋ฃŒ๋˜๋ฉด, ์ž๋™์œผ๋กœ Hub๋กœ ์—…๋กœ๋“œ๋˜์–ด ๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + + + + + +๋งˆ์Šคํ‚น๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ณด๋‹ค ์‹ฌ์ธต์ ์ธ ์˜ˆ์ œ๋Š” +[PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) +๋˜๋Š” [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb)์„ ์ฐธ์กฐํ•˜์„ธ์š”. + + +## ์ถ”๋ก [[inference]] + +์ง€๊ธˆ๊นŒ์ง€ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •์„ ์ž˜ ํ–ˆ์œผ๋‹ˆ, ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! + +๋ชจ๋ธ์ด ๋นˆ์นธ์„ ์ฑ„์šธ ํ…์ŠคํŠธ๋ฅผ ์ŠคํŽ˜์…œ ํ† ํฐ(special token)์ธ `` ํ† ํฐ์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค: + + +```py +>>> text = "The Milky Way is a galaxy." +``` +์ถ”๋ก ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ [`pipeline`]์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +`fill-mask`ํƒœ์Šคํฌ๋กœ `pipeline`์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ํ…์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. +`top_k` ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ์˜ˆ์ธก์˜ ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: + +```py +>>> from transformers import pipeline + +>>> mask_filler = pipeline("fill-mask", "stevhliu/my_awesome_eli5_mlm_model") +>>> mask_filler(text, top_k=3) +[{'score': 0.5150994658470154, + 'token': 21300, + 'token_str': ' spiral', + 'sequence': 'The Milky Way is a spiral galaxy.'}, + {'score': 0.07087188959121704, + 'token': 2232, + 'token_str': ' massive', + 'sequence': 'The Milky Way is a massive galaxy.'}, + {'score': 0.06434620916843414, + 'token': 650, + 'token_str': ' small', + 'sequence': 'The Milky Way is a small galaxy.'}] +``` + + + +ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  `input_ids`๋ฅผ PyTorch ํ…์„œ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +๋˜ํ•œ, `` ํ† ํฐ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="pt") +>>> mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] +``` + +๋ชจ๋ธ์— `inputs`๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋งˆ์Šคํ‚น๋œ ํ† ํฐ์˜ `logits`๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoModelForMaskedLM + +>>> model = AutoModelForMaskedLM.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์€ ๊ฐ€์ง„ ๋งˆ์Šคํฌ ํ† ํฐ 3๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค: +```py +>>> top_3_tokens = torch.topk(mask_token_logits, 3, dim=1).indices[0].tolist() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + +ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  `input_ids`๋ฅผ TensorFlow ํ…์„œ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +๋˜ํ•œ, `` ํ† ํฐ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: +```py +>>> from transformers import AutoTokenizer + +>>> tokenizer = AutoTokenizer.from_pretrained("my_awesome_eli5_mlm_model") +>>> inputs = tokenizer(text, return_tensors="tf") +>>> mask_token_index = tf.where(inputs["input_ids"] == tokenizer.mask_token_id)[0, 1] +``` + +๋ชจ๋ธ์— `inputs`๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋งˆ์Šคํ‚น๋œ ํ† ํฐ์˜ `logits`๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import TFAutoModelForMaskedLM + +>>> model = TFAutoModelForMaskedLM.from_pretrained("stevhliu/my_awesome_eli5_mlm_model") +>>> logits = model(**inputs).logits +>>> mask_token_logits = logits[0, mask_token_index, :] +``` + +๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์€ ๊ฐ€์ง„ ๋งˆ์Šคํฌ ํ† ํฐ 3๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค: +```py +>>> top_3_tokens = tf.math.top_k(mask_token_logits, 3).indices.numpy() + +>>> for token in top_3_tokens: +... print(text.replace(tokenizer.mask_token, tokenizer.decode([token]))) +The Milky Way is a spiral galaxy. +The Milky Way is a massive galaxy. +The Milky Way is a small galaxy. +``` + + diff --git a/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/monocular_depth_estimation.md b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/monocular_depth_estimation.md new file mode 100644 index 0000000000000000000000000000000000000000..2c640d2a86db3d59e63111be73a314313e7aee08 --- /dev/null +++ b/tasks/tasksmith-5cbfd9f64dbc/tests/source/docs/source/ko/tasks/monocular_depth_estimation.md @@ -0,0 +1,144 @@ + + +# ๋‹จ์ผ ์˜์ƒ ๊ธฐ๋ฐ˜ ๊นŠ์ด ์ถ”์ •[[depth-estimation-pipeline]] + +๋‹จ์ผ ์˜์ƒ ๊ธฐ๋ฐ˜ ๊นŠ์ด ์ถ”์ •์€ ํ•œ ์žฅ๋ฉด์˜ ๋‹จ์ผ ์ด๋ฏธ์ง€์—์„œ ์žฅ๋ฉด์˜ ๊นŠ์ด ์ •๋ณด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ปดํ“จํ„ฐ ๋น„์ „ ์ž‘์—…์ž…๋‹ˆ๋‹ค. +์ฆ‰, ๋‹จ์ผ ์นด๋ฉ”๋ผ ์‹œ์ ์˜ ์žฅ๋ฉด์— ์žˆ๋Š” ๋ฌผ์ฒด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. + +๋‹จ์ผ ์˜์ƒ ๊ธฐ๋ฐ˜ ๊นŠ์ด ์ถ”์ •์€ 3D ์žฌ๊ตฌ์„ฑ, ์ฆ๊ฐ• ํ˜„์‹ค, ์ž์œจ ์ฃผํ–‰, ๋กœ๋ด‡ ๊ณตํ•™ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‘์šฉ๋ฉ๋‹ˆ๋‹ค. +์กฐ๋ช… ์กฐ๊ฑด, ๊ฐ€๋ ค์ง, ํ…์Šค์ฒ˜์™€ ๊ฐ™์€ ์š”์†Œ์˜ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์žฅ๋ฉด ๋‚ด ๋ฌผ์ฒด์™€ ํ•ด๋‹น ๊นŠ์ด ์ •๋ณด ๊ฐ„์˜ ๋ณต์žกํ•œ ๊ด€๊ณ„๋ฅผ ๋ชจ๋ธ์ด ์ดํ•ดํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊นŒ๋‹ค๋กœ์šด ์ž‘์—…์ž…๋‹ˆ๋‹ค. + + + + +์ด ์ž‘์—…๊ณผ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜์™€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณด๋ ค๋ฉด [์ž‘์—… ํŽ˜์ด์ง€](https://huggingface.co/tasks/depth-estimation)๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. + + + +์ด๋ฒˆ ๊ฐ€์ด๋“œ์—์„œ ๋ฐฐ์šธ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: + +* ๊นŠ์ด ์ถ”์ • ํŒŒ์ดํ”„๋ผ์ธ ๋งŒ๋“ค๊ธฐ +* ์ง์ ‘ ๊นŠ์ด ์ถ”์ • ์ถ”๋ก ํ•˜๊ธฐ + +์‹œ์ž‘ํ•˜๊ธฐ ์ „์—, ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”: + +```bash +pip install -q transformers +``` + +## ๊นŠ์ด ์ถ”์ • ํŒŒ์ดํ”„๋ผ์ธ[[depth-estimation-inference-by-hand]] + +๊นŠ์ด ์ถ”์ •์„ ์ถ”๋ก ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” [`pipeline`]์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. +[Hugging Face Hub ์ฒดํฌํฌ์ธํŠธ](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import pipeline + +>>> checkpoint = "vinvino02/glpn-nyu" +>>> depth_estimator = pipeline("depth-estimation", model=checkpoint) +``` + + +๋‹ค์Œ์œผ๋กœ, ๋ถ„์„ํ•  ์ด๋ฏธ์ง€๋ฅผ ํ•œ ์žฅ ์„ ํƒํ•˜์„ธ์š”: + +```py +>>> from PIL import Image +>>> import requests + +>>> url = "https://unsplash.com/photos/HwBAsSbPBDU/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzR8fGNhciUyMGluJTIwdGhlJTIwc3RyZWV0fGVufDB8MHx8fDE2Nzg5MDEwODg&force=true&w=640" +>>> image = Image.open(requests.get(url, stream=True).raw) +>>> image +``` + +
+ Photo of a busy street +
+ +์ด๋ฏธ์ง€๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```py +>>> predictions = depth_estimator(image) +``` + +ํŒŒ์ดํ”„๋ผ์ธ์€ ๋‘ ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. +์ฒซ ๋ฒˆ์งธ๋Š” `predicted_depth`๋กœ ๊ฐ ํ”ฝ์…€์˜ ๊นŠ์ด๋ฅผ ๋ฏธํ„ฐ๋กœ ํ‘œํ˜„ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ…์„œ์ž…๋‹ˆ๋‹ค. +๋‘ ๋ฒˆ์งธ๋Š” `depth`๋กœ ๊นŠ์ด ์ถ”์ • ๊ฒฐ๊ณผ๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋Š” PIL ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. + +์ด์ œ ์‹œ๊ฐํ™”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: + +```py +>>> predictions["depth"] +``` + +
+ Depth estimation visualization +
+ +## ์ง์ ‘ ๊นŠ์ด ์ถ”์ • ์ถ”๋ก ํ•˜๊ธฐ[[depth-estimation-inference-by-hand]] + +์ด์ œ ๊นŠ์ด ์ถ”์ • ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์œผ๋‹ˆ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณต์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. +[Hugging Face Hub ์ฒดํฌํฌ์ธํŠธ](https://huggingface.co/models?pipeline_tag=depth-estimation&sort=downloads)์—์„œ ๋ชจ๋ธ๊ณผ ๊ด€๋ จ ํ”„๋กœ์„ธ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. +์—ฌ๊ธฐ์„œ ์ด์ „์— ์‚ฌ์šฉํ•œ ์ฒดํฌํฌ์ธํŠธ์™€ ๋™์ผํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> from transformers import AutoImageProcessor, AutoModelForDepthEstimation + +>>> checkpoint = "vinvino02/glpn-nyu" + +>>> image_processor = AutoImageProcessor.from_pretrained(checkpoint) +>>> model = AutoModelForDepthEstimation.from_pretrained(checkpoint) +``` + +ํ•„์š”ํ•œ ์ด๋ฏธ์ง€ ๋ณ€ํ™˜์„ ์ฒ˜๋ฆฌํ•˜๋Š” `image_processor`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€ ์ž…๋ ฅ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. +`image_processor`๋Š” ํฌ๊ธฐ ์กฐ์ • ๋ฐ ์ •๊ทœํ™” ๋“ฑ ํ•„์š”ํ•œ ์ด๋ฏธ์ง€ ๋ณ€ํ™˜์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> pixel_values = image_processor(image, return_tensors="pt").pixel_values +``` + +์ค€๋น„ํ•œ ์ž…๋ ฅ์„ ๋ชจ๋ธ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> import torch + +>>> with torch.no_grad(): +... outputs = model(pixel_values) +... predicted_depth = outputs.predicted_depth +``` + +๊ฒฐ๊ณผ๋ฅผ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค: + +```py +>>> import numpy as np + +>>> # ์›๋ณธ ์‚ฌ์ด์ฆˆ๋กœ ๋ณต์› +>>> prediction = torch.nn.functional.interpolate( +... predicted_depth.unsqueeze(1), +... size=image.size[::-1], +... mode="bicubic", +... align_corners=False, +... ).squeeze() +>>> output = prediction.numpy() + +>>> formatted = (output * 255 / np.max(output)).astype("uint8") +>>> depth = Image.fromarray(formatted) +>>> depth +``` + +
+ Depth estimation visualization +