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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1da8fd
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import asyncio
import logging
import os
import time
from pprint import pprint
from threading import Thread
from typing import Any, Dict, List

# isort: off
from unsloth import (
    FastLanguageModel,
    FastModel,
    FastVisionModel,
    is_bfloat16_supported,
)  # noqa: E402
from unsloth.chat_templates import get_chat_template  # noqa: E402

# isort: on

import asyncio
import json
import threading
import uuid
from datetime import datetime
from typing import Dict, List, Optional

from datasets import (
    Dataset,
    DatasetDict,
    IterableDataset,
    IterableDatasetDict,
    load_dataset,
)
from fastapi import FastAPI, HTTPException, Request
from openai.types.chat.chat_completion import ChatCompletion
from openai.types.chat.chat_completion import Choice as ChatCompletionChoice
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
from openai.types.chat.chat_completion_chunk import Choice as ChatCompletionChunkChoice
from openai.types.chat.chat_completion_chunk import ChoiceDelta
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.chat.completion_create_params import CompletionCreateParams
from openai.types.fine_tuning import FineTuningJob
from peft import PeftModel
from pydantic import TypeAdapter
from ray import serve
from smolagents import CodeAgent, LiteLLMModel, Model, TransformersModel, VLLMModel
from smolagents.monitoring import LogLevel
from sse_starlette import EventSourceResponse
from starlette.responses import JSONResponse
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    DataCollatorForLanguageModeling,
    Trainer,
    TrainingArguments,
)
from transformers.generation.streamers import AsyncTextIteratorStreamer
from transformers.image_utils import load_image
from trl import SFTTrainer

dtype = (
    None  # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
)
load_in_4bit = True  # Use 4bit quantization to reduce memory usage. Can be False.
max_seq_length = 2048  # Supports RoPE Scaling interally, so choose any!
# max_seq_length = 4096 # Choose any! We auto support RoPE Scaling internally!


logger = logging.getLogger("ray.serve")

os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"

app = FastAPI()

# middlewares = [
#     middleware
#     for middleware in ConnexionMiddleware.default_middlewares
#     if middleware is not SecurityMiddleware
# ]

# connexion_app = AsyncApp(import_name=__name__, middlewares=middlewares)

# connexion_app.add_api(
#     # "api/openai/v1/openapi/openapi.yaml",
#     "api/v1/openapi/openapi.yaml",
#     # base_path="/openai/v1",
#     base_path="/v1",
#     pythonic_params=True,
#     resolver_error=501,
# )

# # fastapi_app.mount("/api", ConnexionMiddleware(app=connexion_app, import_name=__name__))
# # app.mount("/api", ConnexionMiddleware(app=connexion_app, import_name=__name__))
# app.mount(
#     "/",
#     ConnexionMiddleware(
#         app=connexion_app,
#         import_name=__name__,
#         # middlewares=middlewares,
#     ),
# )


@serve.deployment(
    autoscaling_config={
        # https://docs.ray.io/en/latest/serve/advanced-guides/advanced-autoscaling.html#required-define-upper-and-lower-autoscaling-limits
        "max_replicas": 1,
        "min_replicas": 1,  # TOOD: set to 0
        "target_ongoing_requests": 2,  # https://docs.ray.io/en/latest/serve/advanced-guides/advanced-autoscaling.html#target-ongoing-requests-default-2
    },
    max_ongoing_requests=5,  # https://docs.ray.io/en/latest/serve/advanced-guides/advanced-autoscaling.html#max-ongoing-requests-default-5
    ray_actor_options={"num_gpus": 1},
)
@serve.ingress(app)
class ModelDeployment:
    def __init__(
        self,
        model_name: str,
    ):
        self.model_name = model_name
        self.fine_tuning_jobs: Dict[str, FineTuningJob] = {}
        self.training_threads: Dict[str, threading.Thread] = {}

        # Load base model and processor
        self.model, self.processor = FastModel.from_pretrained(
            load_in_4bit=load_in_4bit,
            max_seq_length=max_seq_length,
            model_name=self.model_name,
        )

        # Configure LoRA for fine-tuning
        self.model = FastModel.get_peft_model(
            self.model,
            r=16,  # LoRA rank
            target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
            lora_alpha=32,
            lora_dropout=0.05,
            bias="none",
            use_gradient_checkpointing=True,
            random_state=42,
            use_rslora=False,
        )

        FastModel.for_inference(self.model)  # Enable native 2x faster inference

    def reconfigure(self, config: Dict[str, Any]):
        print("=== reconfigure ===")
        print("config:")
        print(config)
        # https://docs.ray.io/en/latest/serve/production-guide/config.html#dynamically-change-parameters-without-restarting-replicas-user-config

    def _run_training(self, job_id: str, training_file: str, model_name: str):
        """Run the training process in a separate thread."""
        try:
            # Update job status to queued
            self.fine_tuning_jobs[job_id].status = "queued"

            # Simulate file validation
            time.sleep(2)

            # Update job status to running
            self.fine_tuning_jobs[job_id].status = "running"
            self.fine_tuning_jobs[job_id].started_at = int(datetime.now().timestamp())

            # Load and prepare dataset
            dataset = load_dataset("json", data_files=training_file)

            # Configure chat template
            tokenizer = get_chat_template(
                self.processor,
                chat_template="chatml",
                mapping={
                    "role": "from",
                    "content": "value",
                    "user": "human",
                    "assistant": "gpt",
                },
                map_eos_token=True,
            )

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

            dataset = dataset.map(formatting_prompts_func, batched=True)

            # Configure training arguments
            training_args = TrainingArguments(
                output_dir=f"models/{job_id}",
                num_train_epochs=3,
                per_device_train_batch_size=4,
                gradient_accumulation_steps=4,
                learning_rate=2e-4,
                fp16=True,
                logging_steps=10,
                save_strategy="epoch",
                optim="adamw_torch",
                warmup_ratio=0.1,
                lr_scheduler_type="cosine",
                weight_decay=0.01,
            )

            # Create data collator
            data_collator = DataCollatorForLanguageModeling(
                tokenizer=tokenizer,
                mlm=False,
            )

            # Create trainer
            trainer = SFTTrainer(
                model=self.model,
                tokenizer=tokenizer,
                train_dataset=dataset["train"],
                args=training_args,
                data_collator=data_collator,
                max_seq_length=max_seq_length,
                packing=False,
            )

            # Train
            trainer.train()

            # Save model and adapter
            output_dir = f"models/{job_id}"
            os.makedirs(output_dir, exist_ok=True)

            # Save the base model config and tokenizer
            self.model.config.save_pretrained(output_dir)
            tokenizer.save_pretrained(output_dir)

            # Save the adapter weights
            self.model.save_pretrained(output_dir)

            # Save the merged model in 16-bit format
            try:
                # First try to merge and save in 16-bit
                self.model.save_pretrained_merged(
                    output_dir,
                    tokenizer,
                    save_method="merged_16bit",
                )
            except Exception as merge_error:
                print(f"Failed to merge weights: {str(merge_error)}")
                # If merging fails, just save the adapter weights
                self.model.save_pretrained(output_dir)

            # Update job status to succeeded
            self.fine_tuning_jobs[job_id].status = "succeeded"
            self.fine_tuning_jobs[job_id].finished_at = int(datetime.now().timestamp())
            self.fine_tuning_jobs[job_id].trained_tokens = (
                trainer.state.global_step * training_args.per_device_train_batch_size
            )

            # Add result files
            result_files = [
                f"{output_dir}/config.json",
                f"{output_dir}/tokenizer.json",
                f"{output_dir}/adapter_config.json",
                f"{output_dir}/adapter_model.bin",
            ]

            # Add merged model files if they exist
            if os.path.exists(f"{output_dir}/pytorch_model.bin"):
                result_files.append(f"{output_dir}/pytorch_model.bin")

            self.fine_tuning_jobs[job_id].result_files = result_files

        except Exception as e:
            # Update job status to failed
            self.fine_tuning_jobs[job_id].status = "failed"
            self.fine_tuning_jobs[job_id].finished_at = int(datetime.now().timestamp())
            self.fine_tuning_jobs[job_id].error = str(e)
            print(f"Training failed: {str(e)}")
            import traceback

            print(traceback.format_exc())

    @app.post("/v1/fine_tuning/jobs")
    async def create_fine_tuning_job(self, body: dict):
        """Create a fine-tuning job."""
        try:
            # Validate required fields
            if "training_file" not in body:
                raise HTTPException(status_code=400, detail="training_file is required")
            if "model" not in body:
                raise HTTPException(status_code=400, detail="model is required")

            # Generate job ID
            job_id = f"ftjob-{uuid.uuid4().hex[:8]}"

            # Create job object
            job = FineTuningJob(
                id=job_id,
                object="fine_tuning.job",
                created_at=int(datetime.now().timestamp()),
                finished_at=None,
                model=body["model"],
                fine_tuned_model=None,
                organization_id="org-123",
                status="validating_files",  # Start with validating_files
                hyperparameters=body.get("hyperparameters", {}),
                training_file=body["training_file"],
                trained_tokens=None,
                error=None,
                result_files=[],  # Required field
                seed=42,  # Required field
            )

            # Store job
            self.fine_tuning_jobs[job_id] = job

            # Start training in background thread
            thread = threading.Thread(
                target=self._run_training,
                args=(job_id, body["training_file"], body["model"]),
            )
            thread.start()
            self.training_threads[job_id] = thread

            return job.model_dump()

        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    @app.get("/v1/fine_tuning/jobs")
    async def list_fine_tuning_jobs(self):
        """List all fine-tuning jobs."""
        return {
            "object": "list",
            "data": [job.model_dump() for job in self.fine_tuning_jobs.values()],
        }

    @app.get("/v1/fine_tuning/jobs/{job_id}")
    async def get_fine_tuning_job(self, job_id: str):
        """Get a specific fine-tuning job."""
        if job_id not in self.fine_tuning_jobs:
            raise HTTPException(status_code=404, detail="Job not found")
        return self.fine_tuning_jobs[job_id].model_dump()

    @app.post("/v1/fine_tuning/jobs/{job_id}/cancel")
    async def cancel_fine_tuning_job(self, job_id: str):
        """Cancel a fine-tuning job."""
        if job_id not in self.fine_tuning_jobs:
            raise HTTPException(status_code=404, detail="Job not found")

        job = self.fine_tuning_jobs[job_id]
        if job.status not in ["created", "running"]:
            raise HTTPException(status_code=400, detail="Job cannot be cancelled")

        job.status = "cancelled"
        job.finished_at = int(datetime.now().timestamp())

        return job.model_dump()

    @app.post("/v1/chat/completions")
    async def create_chat_completion(self, body: dict, raw_request: Request):
        """Creates a model response for the given chat conversation. Learn more in the [text generation](/docs/guides/text-generation), [vision](/docs/guides/vision), and [audio](/docs/guides/audio) guides.  Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of  unsupported parameters in reasoning models,  [refer to the reasoning guide](/docs/guides/reasoning).

        # noqa: E501

        :param create_chat_completion_request:
        :type create_chat_completion_request: dict | bytes

        :rtype: Union[CreateChatCompletionResponse, Tuple[CreateChatCompletionResponse, int], Tuple[CreateChatCompletionResponse, int, Dict[str, str]]
        """
        print("=== create_chat_completion ===")

        print("body:")
        pprint(body)

        ta = TypeAdapter(CompletionCreateParams)

        print("ta.validate_python...")
        pprint(ta.validate_python(body))

        max_new_tokens = body.get("max_completion_tokens", body.get("max_tokens"))
        messages = body.get("messages")
        model_name = body.get("model")
        stream = body.get("stream", False)
        temperature = body.get("temperature")
        tools = body.get("tools")

        images = []

        for message in messages:
            for content in message["content"]:
                if "type" in content and content["type"] == "image_url":
                    image_url = content["image_url"]["url"]
                    image = load_image(image_url)
                    images.append(image)

                    content["type"] = "image"
                    del content["image_url"]
                elif isinstance(content, dict) and "text" in content:
                    # Convert content to string if it's a dict with text
                    message["content"] = content["text"]
                elif isinstance(content, list):
                    # Join list items with newlines if content is a list
                    message["content"] = "\n".join(content)

        images = images if images else None

        if model_name != self.model_name:
            # adapter_path = model_name
            # self.model.load_adapter(adapter_path)

            return JSONResponse(content={"error": "Model not found"}, status_code=404)

        prompt = self.processor.apply_chat_template(
            add_generation_prompt=True,
            conversation=messages,
            # documents=documents,
            tools=tools,
            tokenize=False,  # Return string instead of token IDs
        )

        print("prompt:")
        print(prompt)

        if images:
            inputs = self.processor(text=prompt, images=images, return_tensors="pt")
        else:
            inputs = self.processor(text=prompt, return_tensors="pt")

        inputs = inputs.to(self.model.device)
        input_ids = inputs.input_ids

        class GeneratorThread(Thread):
            """Thread to generate completions in the background."""

            def __init__(self, model, **generation_kwargs):
                super().__init__()

                self.chat_completion = None
                self.generation_kwargs = generation_kwargs
                self.model = model

            def run(self):
                import torch
                import torch._dynamo.config

                try:
                    try:
                        self.generated_ids = self.model.generate(
                            **self.generation_kwargs
                        )

                    except torch._dynamo.exc.BackendCompilerFailed as e:
                        print(e)
                        print("Disabling dynamo...")

                        torch._dynamo.config.disable = True

                        self.generated_ids = self.model.generate(
                            **self.generation_kwargs
                        )

                except Exception as e:
                    print(e)
                    print("Warning: Exception in GeneratorThread")
                    self.generated_ids = []

            def join(self, timeout=None):
                super().join()

                return self.generated_ids

        decode_kwargs = dict(skip_special_tokens=True)

        streamer = (
            AsyncTextIteratorStreamer(
                self.processor,
                skip_prompt=True,
                **decode_kwargs,
            )
            if stream
            else None
        )

        generation_kwargs = dict(
            **inputs,
            max_new_tokens=max_new_tokens,
            streamer=streamer,
            temperature=temperature,
            use_cache=True,
        )

        thread = GeneratorThread(self.model, **generation_kwargs)
        thread.start()

        if stream:

            async def event_publisher():
                i = 0

                try:
                    async for new_text in streamer:
                        print("new_text:")
                        print(new_text)

                        choices: List[ChatCompletionChunkChoice] = [
                            ChatCompletionChunkChoice(
                                _request_id=None,
                                delta=ChoiceDelta(
                                    _request_id=None,
                                    content=new_text,
                                    function_call=None,
                                    refusal=None,
                                    role="assistant",
                                    tool_calls=None,
                                ),
                                finish_reason=None,
                                index=0,
                                logprobs=None,
                            )
                        ]

                        chat_completion_chunk = ChatCompletionChunk(
                            _request_id=None,
                            choices=choices,
                            created=int(time.time()),
                            id=str(i),
                            model=model_name,
                            object="chat.completion.chunk",
                            service_tier=None,
                            system_fingerprint=None,
                            usage=None,
                        )

                        yield chat_completion_chunk.model_dump_json()

                        i += 1

                except asyncio.CancelledError as e:
                    print("Disconnected from client (via refresh/close)")
                    raise e

                except Exception as e:
                    print(f"Exception: {e}")
                    raise e

            return EventSourceResponse(event_publisher())

        generated_ids = thread.join()
        input_length = input_ids.shape[1]

        batch_decoded_outputs = self.processor.batch_decode(
            generated_ids[:, input_length:],
            skip_special_tokens=True,
        )

        choices: List[ChatCompletionChoice] = []

        for i, response in enumerate(batch_decoded_outputs):
            print("response:")
            print(response)

            # try:
            # response = json.loads(response)

            #         finish_reason: str = response.get("finish_reason")
            #         tool_calls_json = response.get("tool_calls")
            #         tool_calls: List[ToolCall] = []

            #         for tool_call_json in tool_calls_json:
            #             tool_call = ToolCall(
            #                 function=FunctionToolCallArguments(
            #                     arguments=tool_call_json.get("arguments"),
            #                     name=tool_call_json.get("name"),
            #                 ),
            #                 id=tool_call_json.get("id"),
            #                 type="function",
            #             )

            #             tool_calls.append(tool_call)

            #         message: ChatMessage = ChatMessage(
            #             role="assistant",
            #             tool_calls=tool_calls,
            #         )

            #     except json.JSONDecodeError:
            #         finish_reason: str = "stop"
            #         message: ChatMessage = ChatMessage(
            #             role="assistant",
            #             content=response,
            #         )

            message = ChatCompletionMessage(
                audio=None,
                content=response,
                refusal=None,
                role="assistant",
                tool_calls=None,
            )

            choices.append(
                ChatCompletionChoice(
                    index=i,
                    finish_reason="stop",
                    logprobs=None,
                    message=message,
                )
            )

        chat_completion = ChatCompletion(
            choices=choices,
            created=int(time.time()),
            id="1",
            model=model_name,
            object="chat.completion",
            service_tier=None,
            system_fingerprint=None,
            usage=None,
        )

        return chat_completion.model_dump(mode="json")


def build_app(cli_args: Dict[str, str]) -> serve.Application:
    """Builds the Serve app based on CLI arguments."""
    return ModelDeployment.options().bind(
        cli_args.get("model_name"),
    )


# uv run serve run serve:build_app model_name="HuggingFaceTB/SmolVLM-Instruct"
# uv run serve run serve:build_app model_name="unsloth/SmolLM2-135M-Instruct-bnb-4bit"