File size: 3,646 Bytes
bc87776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9926c60
bc87776
 
0dc3f81
bc87776
 
 
0dc3f81
bc87776
0eadf65
 
 
 
bc87776
 
 
 
 
 
 
a620053
 
 
bc87776
a620053
 
 
 
bc87776
a620053
 
 
 
 
 
 
 
bc87776
a620053
 
 
0dc3f81
a620053
 
 
0dc3f81
a620053
 
 
 
0dc3f81
 
bc87776
 
 
 
 
 
 
 
f9051e9
0dc3f81
bc87776
 
9926c60
 
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
# Copyright 2025 NVIDIA CORPORATION & AFFILIATES
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Modified from LLaDA repos: https://github.com/ML-GSAI/LLaDA

import torch
import argparse

from generate import generate, generate_with_prefix_cache, generate_with_dual_cache
from transformers import AutoTokenizer, AutoModel
from model.modeling_llada import LLaDAModelLM

def chat(args):  
    model = LLaDAModelLM.from_pretrained('GSAI-ML/LLaDA-8B-Instruct', trust_remote_code=True, torch_dtype=torch.float16, device_map = 'auto').eval()
    tokenizer = AutoTokenizer.from_pretrained('GSAI-ML/LLaDA-8B-Instruct', trust_remote_code=True)
    device = next(iter(model.parameters())).device.type

    gen_length = args.gen_length
    steps = args.steps
    print('*' * 66)
    print(f'**  Answer Length: {gen_length}  |  Sampling Steps: {steps}  **')
    print('*' * 66)

    conversation_num = 0
    #while True:
    #user_input = input("Enter your question: ")
    user_input = args.question

    m = [{"role": "user", "content": user_input}]
    user_input = tokenizer.apply_chat_template(m, add_generation_prompt=True, tokenize=False)
    input_ids = tokenizer(user_input)['input_ids']
    input_ids = torch.tensor(input_ids).to(device).unsqueeze(0)

    if conversation_num == 0:
        prompt = input_ids
    else:
        prompt = torch.cat([prompt, input_ids[:, 1:]], dim=1)
    print(f'use cache: {args.use_cache} use cache position: {args.if_cache_position} threshold: {args.threshold} block size: {args.block_size}')
    if args.use_cache:
        if args.if_cache_position:
            out, nfe = generate_with_dual_cache(model, prompt, steps=steps, gen_length=gen_length, block_length=args.block_size, temperature=0., remasking='low_confidence', threshold=args.threshold)
        else:
            out, nfe = generate_with_prefix_cache(model, prompt, steps=steps, gen_length=gen_length, block_length=args.block_size, temperature=0., remasking='low_confidence', threshold=args.threshold)
    else:
        out, nfe = generate(model, prompt, steps=steps, gen_length=gen_length, block_length=args.block_size, temperature=0., remasking='low_confidence', threshold=args.threshold)

    answer = tokenizer.batch_decode(out[:, prompt.shape[1]:], skip_special_tokens=True)[0]
    print(f"Bot's reply: {answer}")
    print(f"Number of forward passes: {nfe}")

    # remove the <EOS>
    prompt = out[out != 126081].unsqueeze(0)
    conversation_num += 1
    #print('-----------------------------------------------------------------------')


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--gen_length", type=int, default=128)
    parser.add_argument("--steps", type=int, default=128)
    parser.add_argument("--block_size", type=int, default=32)
    parser.add_argument("--use_cache", action="store_true")
    parser.add_argument("--if_cache_position", action="store_true")
    parser.add_argument("--threshold", type=float, default=None)
    parser.add_argument("--question", type=str, default='How are you ?')

    args = parser.parse_args()
    chat(args)