File size: 2,014 Bytes
646e327
 
 
 
 
 
 
 
 
 
c3bb0d5
646e327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6abe5e
646e327
 
 
 
 
 
 
 
 
 
 
 
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
---
language: en
license: apache-2.0
---

# CodeRosetta
## Pushing the Boundaries of Unsupervised Code Translation for Parallel Programming ([📃Paper](https://arxiv.org/abs/2410.20527), [🔗Website](https://coderosetta.com/)).


CodeRosetta is an EncoderDecoder translation model. It supports the translation of C++, CUDA, and Fortran. \
This is the **base** version of **C++-Fortran** translation model without being fine-tuned.

### How to use

```python
from transformers import AutoTokenizer, EncoderDecoderModel

# Load the CodeRosetta model and tokenizer
model = EncoderDecoderModel.from_pretrained('CodeRosetta/CodeRosetta_cpp_fortran_base')
tokenizer = AutoTokenizer.from_pretrained('CodeRosetta/CodeRosetta_cpp_fortran_base')

# Encode the input Fortran Code
input_fortran_code = "program DRB047_doallchar_orig_no\n use omp_lib\n implicit none\n\n character(len=100), dimension(:), allocatable :: a\n character(50) :: str\n integer :: i\n\n allocate (a(100))\n\n !$omp parallel do private(str)\n do i = 1, 100\n write( str, '(i10)' ) i\n a(i) = str\n end do\n !$omp end parallel do\n\n print*,'a(i)',a(23)\nend program"
input_ids = tokenizer.encode(input_fortran_code, return_tensors="pt")

# Set the start token to <CPP>
start_token = "<CPP>" # set start token to <FORTAN> if input code is C++
decoder_start_token_id = tokenizer.convert_tokens_to_ids(start_token)

# Generate the C++ code
output = model.generate(
    input_ids=input_ids, 
    decoder_start_token_id=decoder_start_token_id,
    max_length=256
)

# Decode and print the generated output
generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_code)
```

### BibTeX 

```bibtex
@inproceedings{coderosetta:neurips:2024,
  title = {CodeRosetta: Pushing the Boundaries of Unsupervised Code Translation for Parallel Programming},
  author = {TehraniJamsaz, Ali and Bhattacharjee, Arijit and Chen, Le and Ahmed, Nesreen K and Yazdanbakhsh, Amir and Jannesari, Ali},
  booktitle = {NeurIPS},
  year = {2024},
}